Advertisement
Guest User

Depth Buffer Views

a guest
Mar 24th, 2011
1,892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1.     //Create the depth/stencil texture. We create two views here. One for the DepthStencilView,
  2.     //and the other for binding to shaders. This could crash if we are using any sort of multisampling in DX10.
  3.     //DX10.1 allows us to bind a multisampled DepthView to a Shader resource, but DX10 does not.
  4.     ID3D10Texture2D* depthStencilTexture;
  5.     D3D10_TEXTURE2D_DESC depthStencilTexDesc;
  6.     depthStencilTexDesc.Width = engineParams.BackbufferWidth;
  7.     depthStencilTexDesc.Height = engineParams.BackbufferHeight;
  8.     depthStencilTexDesc.MipLevels = 1;
  9.     depthStencilTexDesc.ArraySize = 1;
  10.  
  11.     //Setting the format as DXGI_FORMAT_R32_TYPELESS allows us to
  12.     //delay setting of the actual format until a bit later. This is necessary
  13.     //as this texture will be regestered under two different formats. One as
  14.     //DXGI_FORMAT_D32_FLOAT for the depthStencilView, and the other as DXGI_FORMAT_R32_FLOAT
  15.     //for the shaderResourceView.
  16.     depthStencilTexDesc.Format = DXGI_FORMAT_R32_TYPELESS;
  17.     depthStencilTexDesc.Usage = D3D10_USAGE_DEFAULT;
  18.  
  19.     //The bind must be set to D3D10_BIND_SHADER_RESOURCE to allow the depth buffer to be
  20.     //bound as a shader input.
  21.     depthStencilTexDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL | D3D10_BIND_SHADER_RESOURCE;
  22.     depthStencilTexDesc.SampleDesc.Count = engineParams.MultisampleCount;
  23.     depthStencilTexDesc.SampleDesc.Quality = engineParams.MultisampleQuality;
  24.     depthStencilTexDesc.CPUAccessFlags = 0;
  25.     depthStencilTexDesc.MiscFlags = 0;
  26.  
  27.     //Create our texture.
  28.     HRESULT texResult = GraphicsManager::GraphicsDevice->CreateTexture2D(&depthStencilTexDesc, NULL, &depthStencilTexture);
  29.    
  30.     //Check for errors.
  31.     if(FAILED(texResult)){
  32.         MessageBox(0, L"Creating the DepthStencilTexture failed!", 0, 0);
  33.         return false;
  34.     }
  35.  
  36.     //Here we specify the details of the depth stencil view.
  37.     //This is the view that we will attach to our DepthStencil buffer.
  38.     //Note that we set the format to D32 to specify a 32bit depth buffer.
  39.     D3D10_DEPTH_STENCIL_VIEW_DESC depthDesc;
  40.     ZeroMemory(&depthDesc, sizeof(depthDesc));
  41.     depthDesc.Format = DXGI_FORMAT_D32_FLOAT;
  42.     depthDesc.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
  43.     depthDesc.Texture2D.MipSlice = 0;
  44.     HRESULT viewResult = GraphicsManager::GraphicsDevice->CreateDepthStencilView(depthStencilTexture, &depthDesc, &GraphicsManager::DepthStencilView);
  45.  
  46.     if(FAILED(viewResult)){
  47.         MessageBox(0, L"Creating the DepthStencilView failed!", 0, 0);
  48.         return false;
  49.     }
  50.  
  51.     //Specify the details of the shader resource view. This is the view that
  52.     //we will attach to our shader.
  53.     D3D10_SHADER_RESOURCE_VIEW_DESC resDesc;
  54.     ZeroMemory(&resDesc, sizeof(resDesc));
  55.     resDesc.Format = DXGI_FORMAT_R32_FLOAT;
  56.     resDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
  57.     resDesc.Texture2D.MostDetailedMip = 0;
  58.     resDesc.Texture2D.MipLevels = 1;
  59.     viewResult = GraphicsManager::GraphicsDevice->CreateShaderResourceView(depthStencilTexture, &resDesc, &GraphicsManager::DepthStencilShaderResourceView);
  60.  
  61.     if(FAILED(viewResult)){
  62.         MessageBox(0, L"Creating the DepthStencilShaderResourceView failed!", 0, 0);
  63.         return false;
  64.     }
  65.  
  66.     //Free our texture.
  67.     depthStencilTexture->Release();
  68.  
  69.     //Bind our views to the graphics device.
  70.     GraphicsManager::GraphicsDevice->OMSetRenderTargets(1, &GraphicsManager::RenderTargetView, GraphicsManager::DepthStencilView);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement