好的,终于想通了。事实证明,只遵循教程会比尝试从 XNA 移植我的代码更好。我想通过以某种方式将渲染目标资源“复制”到每一帧的着色器资源视图来做到这一点,但事实证明,如果它们都是在渲染之前设置的,它们只需要链接到同一个纹理 2D 资源,渲染目标将自动写入链接的纹理,shaderresourceview 会在调用时自动查找它。
所以代码,如果有人关心的话:
//global declarations:
ID3D11Device* g_pd3dDevice;
ID3D11DeviceContext* g_pImmediateContext;
ID3D11Texture2D* refTex;
ID3D11RenderTargetView* refRen;
ID3D11ShaderResourceView* refRes;
void SetUp()
{
D3D11_TEXTURE2D_DESC textureDesc;
ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.Width = SCREENWIDTH;
textureDesc.Height = SCREENHEIGHT;
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;
g_pd3dDevice->CreateTexture2D(&textureDesc, NULL, &refTex);
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
renderTargetViewDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
g_pd3dDevice->CreateRenderTargetView(refTex, &renderTargetViewDesc, &refRen);
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
shaderResourceViewDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
g_pd3dDevice->CreateShaderResourceView(refTex, &shaderResourceViewDesc, &refRes);
}
void DrawToTexture()
{
g_pImmediateContext->OMSetRenderTargets(1, &refRen,NULL);
float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
g_pImmediateContext->ClearRenderTargetView(refRen, ClearColor);
//then draw what you want to the target
}