1

我来自 XNA,在那里我可以轻松创建渲染目标并将其用作简单的“Texture2D”。我创建了一个 ID3D11RenderTargetView 对象和一个 ID3D11ShaderResourceView 对象。我将精灵绘制到渲染目标,然后想将其用作 ShaderResourceView 以将其发送到着色器。

我尝试查询渲染目标的界面,例如:

ID3D11ShaderResourceView *resource= NULL;
renderTargetobject->QueryInterface<ID3D11ShaderResourceView >(&resource);

上面的代码不会为“资源”对象生成任何可用的值,我想每帧都将其发送到我的着色器(我正在尝试做一个简单的反射)。

4

1 回答 1

7

好的,终于想通了。事实证明,只遵循教程会比尝试从 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


}
于 2012-11-11T08:55:14.157 回答