我来自XNA,在那里我可以轻松创建一个rendertarget并将其用作简单的“Texture2D”。 我创建了一个ID3D11RenderTargetView对象和一个ID3D11ShaderResourceView对象。我将我的精灵绘制到渲染目标,然后将其用作ShaderResourceView将其发送到着色器。
我尝试查询rendertarget的界面,如:
ID3D11ShaderResourceView *resource= NULL;
renderTargetobject->QueryInterface<ID3D11ShaderResourceView >(&resource);
上面的代码没有为“资源”对象生成任何可用的值,我希望每帧都将其发送到我的着色器(我正在尝试进行简单的反射)。
答案 0 :(得分:7)
好的,终于明白了。事实证明,仅仅按照教程更好,而不是尝试从XNA移植我的代码。 我希望通过某种方式将rendertarget资源“复制”到每个帧的shaderresourceview来实现这一点,但事实证明,如果它们都在渲染之前设置,它们只需要链接到相同的texture2D资源,并且rendertarget将自动写入该链接的纹理,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
}