zoukankan      html  css  js  c++  java
  • DXLesson1 关键元素 device chain context

    今决定先看dx sample browser上的内容,毕竟好理解些。

    Tutorial 0: Win32 Basics

    1.setting up an empty window to prepare for Direct3D

    基本上没看懂啥意思。。。。。。。。。待有空解读:

    LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )??

    Tutorial 1: Direct3D 11 Basics:

    create three objects: a device, an immediate context, and a swap chain. The immediate context is a new object in Direct3D 11.

    the immediate context is used by the application to perform rendering onto a buffer, and the device contains methods to  create resources.

    The swap chain is responsible for taking the buffer to which the device renders and displaying the content on the actual monitor screen. The swap chaincontains two or more buffers, mainly the front and the back.

    The front bufferis what is being  presented currently to the user. This buffer is read-only and cannot be modified. The back buffer is the render target to which the device will draw.Once it finishes the drawing operation, the swap chain will present the back buffer by swapping the two buffers. The back buffer becomes the front buffer, and vice versa.

        DXGI_SWAP_CHAIN_DESC sd;   //describes the swap chain
        ZeroMemory( &sd, sizeof(sd) );
        sd.BufferCount = 1;
        sd.BufferDesc.Width = 640;
        sd.BufferDesc.Height = 480;
        sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        sd.BufferDesc.RefreshRate.Numerator = 60;
        sd.BufferDesc.RefreshRate.Denominator = 1;
        sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;//?什么意思? swapBuffer(back和front)  输出renderBuffer类型
        sd.OutputWindow = g_hWnd;//在g_hWnd这个window上
        sd.SampleDesc.Count = 1;//多重采样数 multi-Sampling Nums
        sd.SampleDesc.Quality = 0;//关闭multi-Sampling 
        sd.Windowed = TRUE;
        if( FAILED( D3D11CreateDeviceAndSwapChain( NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, featureLevels, numFeatureLevels,
                         D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, NULL, &g_pImmediateContext ) ) )
        {
            return FALSE;
        } 

    In Direct3D 10, the device object was used to perform both rendering and resource creation. In Direct3D 11, the immediate context is used by the application to perform rendering onto a buffer, and the device contains methods to create resources.//为什么会有这样的转变呢?有什么好处呢?

    create a render target view,A render target view is a type of resource view in Direct3D 11.. A resource view allows a resource to be bound to the graphics pipeline at a specific stage.Think of resource views as typecast in C.  A chunk of raw memory in C can be cast to any data type.We can cast that chunk of memory to an array of integers, an array of floats, a structure, an array of structures, and so on. The raw memory itself is not very useful to us if we don't know its type. Direct3D 11 resource views act in a similar way. For instance, a 2D texture, analogous to the raw memory chunk, is the raw underlying resource. Once we have that resource we can create different resource views to bind that texture to different stages in the graphics pipeline with different formats: as a render target to which to render, as a depth stencil buffer that will receive depth information, or as a texture resource.

     // Create a render target view
        ID3D11Texture2D *pBackBuffer;//创建一个未定型的buffer,可以为renderView ,depth stencil buffer....as typecast in C
        if( FAILED( g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID*)&pBackBuffer ) ) )//bind the back buffer of our swap chain as a render target.. This enables Direct3D 11 to render onto it.
          
        hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );//device create resource  实例化此buffer为RenderTargetView
        pBackBuffer->Release();
        g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );//to bind render target view to the pipeline. ensures the output that the pipeline renders gets  written to the back buffer.
    

      

    小结:

    1.   context:perform  rendering on the buffer....//context set在buffer上执行渲染

     g_pImmediateContext->OMSetRenderTargets   //to bind render target view to the pipeline.

      2.     device : create resources 

    g_pd3dDevice->CreateRenderTargetView      
     g_pd3dDevice->ClearRenderTargetView( g_pRenderTargetView, ClearColor );

      3.  chain :  taking the buffer to which the device renders and displaying the content on the actual monitor screen

    g_pSwapChain->GetBuffer //to get the back buffer object.负责前后buffer的交换
     g_pSwapChain->Present( 0, 0 );//Present() is responsible for displaying the swap chain's back buffer content onto the screen 
    
    
    
    

    Tutorial 2: Rendering a Triangle

    the application must specify a buffer size in bytes when creating a buffer resource.

    how many bytes does each vertex need? To answer that question requires an understanding of vertex layout.

    More often than not(通常,多半)

    Vertex layout defines how these attributes lie in memory: what data type each attribute uses, what size each attribute has, and the order of the attributes in memory.

    when we feed the GPU the vertex buffer containing our vertices, we are just feeding it a chunk of memory. The GPU must also know about the vertex layout in order to extract correct attributes out from the buffer.

    当推送含有vertex属性的buffer给GPU时,我们仅是给GPU一块内存,GPU通过vertex layout来明确vertexBuffer的属性结构)

    HRESULT??

    // Compile the shader

    CompileShaderFromFile(WCHAR* szFileName,LPCSTR szEntryPoint,LPCSTR szShaderModel,ID3DBlob **ppBlobOut)

    szFileName 文件名,szEntryPoint  shader的入口点(函数名称)(是vertexShader还是pixelShader),shaderModel 4.0 3.0.... ID3DBlob(翻译是 二进制大物件....)应该是说把shader编译成二进制形式,ppBlobOut用于地址索引吧

     vertex shaders are tightly coupled with this vertex layout.    Creating a vertex layout object requires the vertex shader's input signature

    编译vertexShader:

    hr = D3DX11CompileFromFile(szFileName,NULL,NULL,szEntryPoint,szShaderModel,dwShaderFlags,0,NULL,ppBlobOut,&pErrorBlob,NULL);

    创建shader:

    device->CreateVertexShader(pVSBlob->GetBufferPointer(),pVSBlob->GetBufferSize(),NULL,&g_pVertexShader);

    We use the ID3DBlob object returned from D3DX11CompileFromFile to retrieve the binary data that represents the input signature of the vertex shader. Once we have this data, we can call ID3D11Device::CreateInputLayout() to create a vertex layout object, and ID3D11DeviceContext::IASetInputLayout() to set it as the active vertex layout. 

    1.define input layout 2.create 3.active 即定义     device创建     context来激活。

    D3D11_BUFFER_DESC describes the vertex buffer object to be created, and D3D11_SUBRESOURCE_DATA describes the actual data that will be copied to the vertex buffer during creation. The creation and initialization of the vertex buffer is done at once so that we don't need to initialize the buffer later.After the vertex buffer is created, we can call ID3D11DeviceContext::IASetVertexBuffers() to bind it to the device.

    The vertex shader is responsible for transforming the individual vertices of the triangles to their correct locations. And the pixel shader is responsible for calculating the final output color for each pixel of the triangle. To use these shaders we must call ID3D11DeviceContext::VSSetShader() and ID3D11DeviceContext::PSSetShader() respectively. The last thing that we do is call ID3D11DeviceContext::Draw(), which commands the GPU to render using the current vertex buffer, vertex layout, and primitive topology. The first parameter to Draw() is the number of vertices to send to the GPU, and the second parameter is the index of the first vertex to begin sending. 

     when we called VSSetShader() and PSSetShader(), we actually bound our shader to a stage in the pipeline. Then, when we called Draw, we start processing the vertex data passed into the graphics pipeline.

    A vertex shader takes a vertex as input. It is run once for every vertex passed to the GPU via vertex buffers.

    the vertices in the vertex buffer will usually be in object space,the vertex shader receives input vertex data in object space.

    Clipping against this volume is complicated because to clip against one view frustum plane, the GPU must compare every vertex to the plane's equation. Instead, the GPU generally performs projection transformation first, and then clips against the view frustum volume。(GPU先执行投影变换,再进行裁剪。)

    The effect of projection transformation on the view frustum is that the pyramid shaped view frustum becomes a box in projection space........................in projection space the X and Y coordinates are based on the X/Z and Y/Z in 3D space. Therefore, point a and point b will have the same X and Y coordinates in projection space, which is why the view frustum becomes a box.

    anything with X or Y coordinate that's outside the [-1 1] range will be clipped out.....any Z values outside [0 1] will be clipped out by the GPU.

    Constant buffers are used to store data that the application needs to pass to shaders.  global variables。

    when we initiate the rendering by calling Draw(), our vertex shader reads the matrices stored in the constant buffer.

    because matrices are arranged differently in memory in C++ and HLSL, we must transpose the matrices before updating them.

    Screen Space

    Screen space is often used to refer to locations in the frame buffer. Because frame buffer is usually a 2D texture, screen space is a 2D space. The top-left corner is the origin with coordinates (0, 0). The positive X goes to right and positive Y goes down. For a buffer that is w pixels wide and h pixels high, the most lower-right pixel has the coordinates (w - 1, h - 1).

    当我们进行复杂的绘图操作时,画面便可能有明显的闪烁。解决这个问题的关键在于使绘制的东西同时出现在屏幕上。所谓双缓冲技术, 是指使用两个缓冲区: 前台缓冲和后台缓冲。前台缓冲即我们看到的屏幕,后台缓冲则在内存当中,对我们来说是不可见的。每次的所有绘图操作都在后台缓冲中进行, 当绘制完成时, 把绘制的最终结果复制到屏幕上, 这样, 我们看到所有GDI元素同时出现在屏幕上,从而解决了频繁刷新导致的画面闪烁问题。

    问题:DX的矩阵 行列的问题。。。。

    DX11加载纹理的过程:

    编译生成遇到的问题:

    FXC : error X3501: 'main': entrypoint not found

    If you compile a .fx file, you can set the shader type to "fx" as below:

    1 Right click your project in VS and select properties

    2 Expand the HLSL compiler option, select "fx" for Shader Type, you can also specify a entry point function for Entrypoint Name.

  • 相关阅读:
    rsyslog+loganalyzer配置
    Python字符串格式化
    awk
    haproxy配置
    CentOS7服务管理
    Nginx缓存
    父指针 子指针
    矩阵 特征向量
    指针的运算
    const 与指针 的用法
  • 原文地址:https://www.cnblogs.com/dust-fly/p/3599228.html
Copyright © 2011-2022 走看看