我试图渲染到纹理以用于后期处理。我已经阅读了很多关于这些东西的教程,现在我已经尝试过了,但显然我做错了。我已经在问另一个关于这个话题的问题,但由于这个问题不同,我认为创建一个新问题更好。 首先,这些是我反复出现的错误信息:
D3D11: ERROR: ID3D11DeviceContext::OMSetRenderTargets: The RenderTargetView at slot 0 is not compatable with the DepthStencilView. DepthStencilViews may only be used with RenderTargetViews if the effective dimensions of the Views are equal, as well as the Resource types, multisample count, and multisample quality. The RenderTargetView at slot 0 has (w:400,h:300,as:1), while the Resource is a Texture2D with (mc:1,mq:0). The DepthStencilView has (w:1280,h:720,as:1), while the Resource is a Texture2D with (mc:1,mq:0). D3D11_RESOURCE_MISC_TEXTURECUBE factors into the Resource type, unless GetFeatureLevel() returns D3D_FEATURE_LEVEL_10_1 or greater. [ STATE_SETTING ERROR #388: OMSETRENDERTARGETS_INVALIDVIEW ]
D3D11: ERROR: ID3D11DeviceContext::Draw: Rasterization Unit is enabled (PixelShader is not NULL or Depth/Stencil test is enabled and RasterizedStream is not D3D11_SO_NO_RASTERIZED_STREAM) but position is not provided by the last shader before the Rasterization Unit. [ EXECUTION ERROR #362: DEVICE_DRAW_POSITION_NOT_PRESENT ]
和
D3D11: ERROR: ID3D11DeviceContext::Draw: Vertex Shader - Geometry Shader linkage error: Signatures between stages are incompatible. The input stage requires Semantic/Index (RADIUS,0) as input, but it is not provided by the output stage. [ EXECUTION ERROR #342: DEVICE_SHADER_LINKAGE_SEMANTICNAME_NOT_FOUND ]
D3D11: ERROR: ID3D11DeviceContext::Draw: Vertex Shader - Geometry Shader linkage error: Signatures between stages are incompatible. The input stage requires Semantic/Index (TEXTUREINDEX,0) as input, but it is not provided by the output stage. [ EXECUTION ERROR #342: DEVICE_SHADER_LINKAGE_SEMANTICNAME_NOT_FOUND ]
D3D11: ERROR: ID3D11DeviceContext::Draw: Geometry Shader - Pixel Shader linkage error: Signatures between stages are incompatible. The input stage requires Semantic/Index (POSITION,0) as input, but it is not provided by the output stage. [ EXECUTION ERROR #342: DEVICE_SHADER_LINKAGE_SEMANTICNAME_NOT_FOUND ]
D3D11: ERROR: ID3D11DeviceContext::OMSetRenderTargets: The RenderTargetView at slot 0 is not compatable with the DepthStencilView. DepthStencilViews may only be used with RenderTargetViews if the effective dimensions of the Views are equal, as well as the Resource types, multisample count, and multisample quality. The RenderTargetView at slot 0 has (w:400,h:300,as:1), while the Resource is a Texture2D with (mc:1,mq:0). The DepthStencilView has (w:1280,h:720,as:1), while the Resource is a Texture2D with (mc:1,mq:0). D3D11_RESOURCE_MISC_TEXTURECUBE factors into the Resource type, unless GetFeatureLevel() returns D3D_FEATURE_LEVEL_10_1 or greater. [ STATE_SETTING ERROR #388: OMSETRENDERTARGETS_INVALIDVIEW ]
我很确定我是误解了这个rendertarget切换的主要方面和东西。 当我的DirectX设备创建时,我也创建了这个:
D3D11_TEXTURE2D_DESC textureDesc;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.Width = 800/2;
textureDesc.Height = 600/2;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
pDevice->CreateTexture2D(&textureDesc, NULL, &renderTargetTextureMap);
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
.
pDevice->CreateRenderTargetView(renderTargetTextureMap, &renderTargetViewDesc, &renderTargetViewMap);
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
pDevice->CreateShaderResourceView(renderTargetTextureMap, &shaderResourceViewDesc, &shaderResourceViewMap);
D3DVERTEX quad[4] = {
{D3DXVECTOR3(0,0,0), D3DXVECTOR2(0,0)},
{D3DXVECTOR3(1,1,0), D3DXVECTOR2(1,1)},
{D3DXVECTOR3(1,0,0), D3DXVECTOR2(1,0)},
{D3DXVECTOR3(0,1,0), D3DXVECTOR2(0,1)}};
HRESULT hr;
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = &quad[0];
InitData.SysMemPitch = sizeof(D3DVERTEX);
InitData.SysMemSlicePitch = 0;
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth = 4 * InitData.SysMemPitch;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
V_RETURN(pDevice->CreateBuffer( &bufferDesc, &InitData, &m_pVertexBuffer ));
const D3D11_INPUT_ELEMENT_DESC layout[] = // http://msdn.microsoft.com/en-us/library/bb205117%28v=vs.85%29.aspx
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = sizeof( layout ) / sizeof( layout[0] );
D3DX11_PASS_DESC passDesc;
m_pEffect->GetTechniqueByName("Render")->GetPassByName("P0_PP")->GetDesc(&passDesc);
V_RETURN( pDevice->CreateInputLayout( layout, numElements, passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize, &m_pInputLayout ) );
所以我创建了一个ShaderResourceView和一个RenderTargetView以及一个带有顶点缓冲区的立方体来绘制这个纹理将被绘制的立方体。
现在在所有其他内容更新之后,使用以下方法绘制所有其他内容:
pDevice->GetImmediateContext(&pd3dImmediateContext);
pd3dImmediateContext->OMSetRenderTargets( 1, &renderTargetViewMap, pDSV );
pd3dImmediateContext->ClearRenderTargetView(renderTargetViewMap, g_ClearColor);
ID3D11Buffer* vbs[] = { m_pVertexBuffer, };
unsigned int strides[] = { sizeof(D3DVERTEX), }, offsets[] = { 0, };
pd3dImmediateContext->IASetVertexBuffers(0,1, vbs, strides, offsets);
pd3dImmediateContext->IASetInputLayout(m_pInputLayout);
pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
//TODO: Move to header file.
ID3DX11EffectShaderResourceVariable* g_SpriteEV;
g_SpriteEV = m_pEffect->GetVariableByName("g_PostProcessTex")->AsShaderResource();
g_SpriteEV->SetResource(shaderResourceViewMap);
m_pEffect->GetTechniqueByName("Render")->GetPassByName("P0_PP")->Apply(0, pd3dImmediateContext);
pd3dImmediateContext->Draw(4, 0);
我没有明确创建一个depthStencilView,我猜这是我的主要错误。但我不知道如何创造这样的东西。 我很困惑这一切是如何在后台工作的......也许有人在乎解释这一点。 我在这做什么,或者这是错的吗? :P
感谢任何关心帮助的人。