渲染到纹理-ClearRenderTargetView()可以工作,但是没有对象渲染到纹理(渲染到屏幕可以正常工作) [英] Rendering to texture - ClearRenderTargetView() works, but none objects are rendered to texture (rendering to screen works fine)

查看:483
本文介绍了渲染到纹理-ClearRenderTargetView()可以工作,但是没有对象渲染到纹理(渲染到屏幕可以正常工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将场景渲染为纹理,然后将其显示在屏幕的一角。

I try to render the scene to texture which should be then displayed in corner of the screen.

我可以这样做:


  1. 渲染场景(我的 Engine :: render()方法将设置着色器并进行绘制调用)-正常

  2. 将渲染目标更改为纹理。

  3. 再次渲染场景-无效。 context-> ClearRenderTargetView(texture-> getRenderTargetView(),{1.0f,0.0f,0.0f,1.0f}) 确实将我的纹理设置为红色(对于步骤1中的场景,我使用了不同的颜色),但是没有对象正在其上呈现。

  4. 将渲染目标更改回原始目标。

  5. 最后一次渲染场景,在角处的矩形具有我在步骤3中渲染的纹理。-可以正常运行。我看到了场景,角落里的小矩形也是如此。问题是,它只是红色(我猜想步骤3中的渲染出了点问题)。

  1. Render the scene (my Engine::render() method that will set shaders and make draw calls) - works ok.
  2. Change render target to the texture.
  3. Render the scene again - does not work. The context->ClearRenderTargetView(texture->getRenderTargetView(), { 1.0f, 0.0f, 0.0f, 1.0f } ) does set my texture to red color (for scene in step 1. I use different color), but none objects are being rendered on it.
  4. Change render target back to original.
  5. Render the scene for the last time, with rectangle at corner that has the texture I've rendered in step 3. - works ok. I see the scene, the little rectangle in the corner too. The problem is, it's just red (something went wrong with rendering in step 3., I guess).

结果(应该是图像中的图像而不是红色矩形):

The result (there should be "image in image" instead of red rectangle):

第2步的代码。- 4。:

The code for steps 2. - 4.:

context->OMSetRenderTargets(1, &textureRenderTargetView, depthStencilView); 
float bg[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
context->ClearRenderTargetView(textureRenderTargetView, bg); //backgroundColor - red, green, blue, alpha
render();
context->OMSetRenderTargets(1, &myRenderTargetView, depthStencilView); //bind  render target back to previous value (not to texture)

render()方法不会更改(在步骤1中起作用,为什么在渲染到纹理时它不起作用?)并以 swapChain-> Present(0,0)。

The render() method does not change (it works in step 1., why it doesn't work when I render to texture?) and ends with swapChain->Present(0, 0).

我知道 ClearRenderTargetView 影响我的纹理(没有它,它不会将颜色更改为红色)。但是其余渲染要么不输出,要么存在另一个问题。

I know that ClearRenderTargetView affects my texture (without it, it's doesn't change color to red). But the rest of rendering either do not output to it or there's another problem.

我错过了什么吗?

我基于本教程(也许我的 D3D11_TEXTURE2D_DESC 有错误?):

I create the texture, shader resource view and render target for it based on this tutorial (maybe there is an error in my D3D11_TEXTURE2D_DESC?):

D3D11_TEXTURE2D_DESC textureDesc;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;

//1. create render target

ZeroMemory(&textureDesc, sizeof(textureDesc));

//setup the texture description
//we will need to have this texture bound as a render target AND a shader resource
textureDesc.Width = size.getX();
textureDesc.Height = size.getY();
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;

//create the texture
device->CreateTexture2D(&textureDesc, NULL, &textureRenderTarget);

//2. create render target view

//setup the description of the render target view.
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;

//create the render target view
device->CreateRenderTargetView(textureRenderTarget, &renderTargetViewDesc, &textureRenderTargetView);

//3. create shader resource view

//setup the description of the shader resource view.
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;

//create the shader resource view.
device->CreateShaderResourceView(textureRenderTarget, &shaderResourceViewDesc, &texture);

深度缓冲区:

D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = sampleCount;
descDepth.SampleDesc.Quality = maxQualityLevel;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;

这里是交换链:

DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1;
sd.BufferDesc.Width = width;
sd.BufferDesc.Height = height;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = numerator; //60
sd.BufferDesc.RefreshRate.Denominator = denominator; //1
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = *hwnd;
sd.SampleDesc.Count = sampleCount; //1 (and 0 for quality) to turn off multisampling
sd.SampleDesc.Quality = maxQualityLevel;
sd.Windowed = fullScreen ? FALSE : TRUE;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; //allow full-screen switchin

// Set the scan line ordering and scaling to unspecified.
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
// Discard the back buffer contents after presenting.
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

我以这种方式创建默认的渲染目标视图:

I create the default render target view that way:

//create a render target view
ID3D11Texture2D* pBackBuffer = NULL;
result = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
ERROR_HANDLE(SUCCEEDED(result), L"The swapChain->GetBuffer() failed.", MOD_GRAPHIC);

//Create the render target view with the back buffer pointer.
result = device->CreateRenderTargetView(pBackBuffer, NULL, &myRenderTargetView);


推荐答案

经过一些调试,如@Gnietschow建议,发现错误:

After some debugging, as @Gnietschow suggested, I have found an error:


D3D11错误:ID3D11DeviceContext :: OMSetRenderTargets:

D3D11 ERROR: ID3D11DeviceContext::OMSetRenderTargets:

The插槽0处的RenderTargetView与
DepthStencilView不兼容。如果View的有效尺寸,
以及资源类型,多样本计数和多样本
质量相等,则DepthStencilViews只能与
RenderTargetViews一起使用。

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.

插槽0处的RenderTargetView具有(w:1680,h:1050,as:1),而
资源是带有(mc:1,mq:0的Texture2D)

The RenderTargetView at slot 0 has (w:1680,h:1050,as:1), while the Resource is a Texture2D with (mc:1,mq:0).

DepthStencilView具有
(w:1680,h:1050,as:1),而Resource是带有$的Texture2D b $ b (mc:8,mq:16)

The DepthStencilView has (w:1680,h:1050,as:1), while the Resource is a Texture2D with (mc:8,mq:16).

基本上,我的渲染目标(

So basically, my render target (texture) was not using anti-aliasing while my back buffer/depth buffer do.

我不得不更改 SampleDesc.Count 1 SampleDesc.Quality 0 DXGI_SWAP_CHAIN_DESC D3D11_TEXTURE2D_DESC 都可以匹配我渲染的纹理中的值。换句话说,渲染到纹理时必须关闭抗锯齿功能。

I had to change SampleDesc.Count to 1 and SampleDesc.Quality to 0 in both DXGI_SWAP_CHAIN_DESC and D3D11_TEXTURE2D_DESC to match the values from texture to which I render. In other words I had to turn off anti-aliasing when rendering to texture.

我想知道,为什么渲染到纹理不支持抗锯齿功能?

I wonder, why render to texture does not support anti-aliasing? When I set SampleDesc.Count and SampleDesc.Quality to my standard values (8 and 16, those works fine on my GPU when rendering the scene) for my texture render target, the device->CreateTexture2D(...) fails with "invalid parameter" (even when I use those same values everywhere).

这篇关于渲染到纹理-ClearRenderTargetView()可以工作,但是没有对象渲染到纹理(渲染到屏幕可以正常工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆