我正在尝试创建另一个rendertargetview并对其进行渲染
之前,我只是从swapchain取回缓冲区,然后从中创建rtv,这很好。但是只有从自定义资源创建rtv时,我才遇到问题。
D3D11_TEXTURE2D_DESC bb_desc;
bb_desc.Width = width;
bb_desc.Height = height;
bb_desc.MipLevels = 1;
bb_desc.ArraySize = 1;
bb_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
bb_desc.Usage = D3D11_USAGE_DEFAULT;
bb_desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
bb_desc.CPUAccessFlags = 0;
bb_desc.MiscFlags = 0;
bb_desc.SampleDesc.Count = msCount;
bb_desc.SampleDesc.Quality = msQuality;
R_CHECK(device->CreateTexture2D(
&bb_desc,
nullptr,
backBuffer.GetAddressOf()));
D3D11_RENDER_TARGET_VIEW_DESC rtv_desc;
rtv_desc.Format = bb_desc.Format;
rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtv_desc.Texture2D.MipSlice = 0;
R_CHECK(device->CreateRenderTargetView(
backBuffer.Get(),
&rtv_desc,
bbRTV.GetAddressOf()));
D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc;
srv_desc.Format = bb_desc.Format;
srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srv_desc.Texture2D.MostDetailedMip = 0;
srv_desc.Texture2D.MipLevels = 1;
R_CHECK(device->CreateShaderResourceView(
backBuffer.Get(),
&srv_desc,
bbSRV.GetAddressOf()));
dContext->OMSetRenderTargets(1, bbRTV.GetAddressOf(), nullptr);
我渲染它
float bgColor[3] = { 1,0,0 };
dContext->ClearRenderTargetView(bbRTV.Get(), bgColor);
swapchain->Present(1, NULL);
期望红色屏幕,但仅显示黑色。 而且也没有错误日志。 任何建议将不胜感激〜
答案 0 :(得分:0)
ClearRenderTargetView
的第二个参数无效。它必须是指向4个float
值数组的第一个元素的指针。由于您将指针传递给3个float
值数组的第一个元素,因此会发生缓冲区溢出,从而导致未定义行为。
但是最重要的是,当您渲染到纹理时,调用Present
时它不会显示在屏幕上,因为它不是交换链返回缓冲区。您可以使用DirectX调试器检查纹理内容。