2015-08-15 102 views
1

對於SSAO效果,我必須生成兩個紋理:法線(視圖空間)和深度。錯誤的深度緩衝區(紋理)輸出?

我決定使用深度緩衝區作爲紋理根據Microsoft tutorial讀取深度 - 模板緩衝區作爲紋理一章)。

不幸的是,在渲染之後我從深度緩衝(降低影像)無信息:

enter image description here

我想這是不正確的。奇怪的是,深度緩衝區似乎工作(我得到正確的面孔順序等)。

深度緩衝器的代碼:

//create depth stencil texture (depth buffer) 
D3D11_TEXTURE2D_DESC descDepth; 
ZeroMemory(&descDepth, sizeof(descDepth)); 
descDepth.Width = width; 
descDepth.Height = height; 
descDepth.MipLevels = 1; 
descDepth.ArraySize = 1; 
descDepth.Format = DXGI_FORMAT_R24G8_TYPELESS; 
descDepth.SampleDesc.Count = antiAliasing.getCount(); 
descDepth.SampleDesc.Quality = antiAliasing.getQuality(); 
descDepth.Usage = D3D11_USAGE_DEFAULT; 
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE; 
descDepth.CPUAccessFlags = 0; 
descDepth.MiscFlags = 0; 

ID3D11Texture2D* depthStencil = NULL; 
result = device->CreateTexture2D(&descDepth, NULL, &depthStencil); 
ERROR_HANDLE(SUCCEEDED(result), L"Could not create depth stencil texture.", MOD_GRAPHIC); 

D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc; 
//setup the description of the shader resource view 
shaderResourceViewDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; 
shaderResourceViewDesc.ViewDimension = antiAliasing.isOn() ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D; 
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0; 
shaderResourceViewDesc.Texture2D.MipLevels = 1; 

//create the shader resource view. 
ERROR_HANDLE(SUCCEEDED(device->CreateShaderResourceView(depthStencil, &shaderResourceViewDesc, &depthStencilShaderResourceView)), 
    L"Could not create shader resource view for depth buffer.", MOD_GRAPHIC); 

createDepthStencilStates(); 
//set the depth stencil state. 
context->OMSetDepthStencilState(depthStencilState3D, 1); 

D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc; 
// Initialize the depth stencil view. 
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc)); 

// Set up the depth stencil view description. 
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; 
depthStencilViewDesc.ViewDimension = antiAliasing.isOn() ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D; 
depthStencilViewDesc.Texture2D.MipSlice = 0; 
//depthStencilViewDesc.Flags = D3D11_DSV_READ_ONLY_DEPTH; 

// Create the depth stencil view. 
result = device->CreateDepthStencilView(depthStencil, &depthStencilViewDesc, &depthStencilView); 
ERROR_HANDLE(SUCCEEDED(result), L"Could not create depth stencil view.", MOD_GRAPHIC); 

與第一遍渲染後,我設置的深度模版作爲紋理資源與其它渲染目標(顏色,法線)一起追加到數組:

ID3D11ShaderResourceView ** textures = new ID3D11ShaderResourceView *[targets.size()+1]; 
for (unsigned i = 0; i < targets.size(); i++) { 
    textures[i] = targets[i]->getShaderResourceView(); 
} 
textures[targets.size()] = depthStencilShaderResourceView; 
context->PSSetShaderResources(0, targets.size()+1, textures); 

我打電話context->OMSetRenderTargets(1, &myRenderTargetView, NULL);秒通之前解除綁定深度緩衝器(所以可以使用它作爲紋理)。

然後,我使我的紋理(渲染從第一通+深度緩存目標)與瑣碎的後期處理着色器,只是爲了調試的目的(第二通路):

Texture2D ColorTexture[3]; 
SamplerState ObjSamplerState; 

float4 main(VS_OUTPUT input) : SV_TARGET0{ 
    float4 Color; 
    Color = float4(0, 1, 1, 1); 
    float2 textureCoordinates = input.textureCoordinates.xy * 2; 
    if (input.textureCoordinates.x < 0.5f && input.textureCoordinates.y < 0.5f) { 
     Color = ColorTexture[0].Sample(ObjSamplerState, textureCoordinates); 
    } 
    if (input.textureCoordinates.x > 0.5f && input.textureCoordinates.y < 0.5f) { 
     textureCoordinates.x -= 0.5f; 
     Color = ColorTexture[1].Sample(ObjSamplerState, textureCoordinates); 
    } 
    if (input.textureCoordinates.x < 0.5f && input.textureCoordinates.y > 0.5f) { //depth texture 
     textureCoordinates.y -= 0.5f; 
     Color = ColorTexture[2].Sample(ObjSamplerState, textureCoordinates); 
    } 
... 

它工作正常的法線貼圖。爲什麼它不適用於深度緩衝區(作爲着色器資源視圖)?

+1

深度緩衝器將通常看起來均勻紅色觀察時,因爲通常的值的範圍約0.99和1與設置不當遠近剪輯平面之間。您是否通過視覺檢查以外的任何其他方式驗證數據是否有誤? –

+0

@AdamMiles你是對的!經過調查後,我發現數值的確在0.999f和1.000f之間。我用'DirectX :: XMMatrixPerspectiveFovLH(DirectX :: XM_PIDIV4,screenWidth /(FLOAT)screenHeight,0.01f,20000.0f)設置我的遠近剪貼板;'我應該改變它們嗎?像'0.01f,200.0f'這樣的值不會**提供更好的值(在更大的範圍內),並且另外一些幾何體會被剪切掉。還有什麼我可以操縱以獲得更好的結果嗎? – PolGraphic

+1

這些值應儘可能緊密地限制場景的可見範圍。近剪輯值爲0。01會建議你不要在物體距離攝像機1cm的地方剪下物體,在這裏你至少可以逃脫0.1。一個20000的短片顯示20公里的抽籤距離(假設米是你的單位),這似乎也是過度的。即使有0.1/200的機會,它仍然會大於0.9,但除了當您嘗試按照您所做的方式對數據進行可視化時,這並不是什麼大問題。可視化時可能重新規模0.9-1 - > 0.0-1.0? –

回答

1

作爲每評論:

紋理被渲染和正確地採樣,但數據似乎是均勻的紅色,由於數據和0.999之間1.0F躺着。

有幾件事情可以做,以改善現有的深度精度,但其中最簡單的是簡單地確保您的近及遠夾子距離不是過小/大,你繪製的場景。

假設米是您的單元,0.1(10釐米)的近夾子和200(米)遠剪輯是大於1cm和20公里更合理。即使如此,也不要期望看到太多的黑色/黑暗區域,z緩衝區的非線性特性仍然意味着大部分深度值都被分流到1。如果可視化深度緩衝區很重要,然後在顯示之前將數據重新調整到標準化的0-1範圍。