0

I'm trying to render to a texture in D3D11, but whenever I do, the texture's data is empty and nothing draws. I create and render to a texture in the following function:

_TextureBufferDescription.Width = int( vecMax.x - vecMin.x );
_TextureBufferDescription.Height = int( vecMax.y - vecMin.y );
_TextureBufferDescription.MipLevels = 1;
_TextureBufferDescription.ArraySize = 1;
_TextureBufferDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
_TextureBufferDescription.SampleDesc.Count = 1;
_TextureBufferDescription.SampleDesc.Quality = 0;
_TextureBufferDescription.Usage = D3D11_USAGE_DEFAULT;
_TextureBufferDescription.BindFlags = D3D11_BIND_RENDER_TARGET;
_TextureBufferDescription.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
_TextureBufferDescription.MiscFlags = 0;

_TextureDescription.Width = int( vecMax.x - vecMin.x );
_TextureDescription.Height = int( vecMax.y - vecMin.y );
_TextureDescription.MipLevels = 1;
_TextureDescription.ArraySize = 1;
_TextureDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
_TextureDescription.SampleDesc.Count = 1;
_TextureDescription.SampleDesc.Quality = 0;
_TextureDescription.Usage = D3D11_USAGE_STAGING;
_TextureDescription.BindFlags = 0;
_TextureDescription.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
_TextureDescription.MiscFlags = 0;

_RenderTargetViewDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
_RenderTargetViewDescription.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
_RenderTargetViewDescription.Texture2D.MipSlice = 0;

_Drawing.pDevice->CreateTexture2D( &_TextureBufferDescription, nullptr, &pRenderedTextureBuffer );
_Drawing.pDevice->CreateTexture2D( &_TextureDescription, nullptr, &pRenderedTexture );
_Drawing.pDevice->CreateRenderTargetView( pRenderedTextureBuffer, &_RenderTargetViewDescription, &pNewRenderTarget );
_Drawing.pContext->OMSetRenderTargets( 1, &pNewRenderTarget, _Drawing.pDepthStencilView );
_Drawing.pContext->ClearRenderTargetView( pNewRenderTarget, D3DXCOLOR( 0.f, 0.f, 0.f, 0.f ) );
Draw( );
_Drawing.pContext->OMSetRenderTargets( 1, &_Drawing.pRenderTargetView, _Drawing.pDepthStencilView );
_Drawing.pContext->CopyResource( pRenderedTexture, pRenderedTextureBuffer );
pNewRenderTarget->Release( );

return pRenderedTexture;

The draw function is defined as this:

auto uStride = sizeof( vertex_t ), uOffset = 0u;

_Drawing.pContext->PSSetShader( pTexture == nullptr ? _Drawing.pStandardPixelShader : _Drawing.pTexturedPixelShader, nullptr, 0 );
_Drawing.pContext->PSSetShaderResources( 0, 1, &pTexture );
_Drawing.pContext->IASetIndexBuffer( pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );
_Drawing.pContext->IASetVertexBuffers( 0, 1, &pVertexBuffer, &uStride, &uOffset );
_Drawing.pContext->IASetPrimitiveTopology( _Topology );
_Drawing.pContext->DrawIndexed( vecIndices.size( ), 0, 0 );

All of the functions return S_OK, so none of them appear to be failing. Everything that is created is not nullptr. Any help is appreciated.

Asesh
  • 3,186
  • 2
  • 21
  • 31

1 Answers1

0

i am not really sure but i am just wondering, you don't need any staging resource or CPUaccessFlag set here since you are not reading from CPU.

and i think that pTexture that you passed to shader should be srv not a resource.

and it would be helpful if i can see the shader code. i assume that you defined the texture variable with Texture2D< float4> type which it should be.

KIM CHANGJUN
  • 99
  • 11
  • What do you mean by a "srv" –  May 21 '19 at 04:04
  • I'm also not trying to render the texture after rendering to it. I just need to access the pixel data of it. –  May 21 '19 at 04:10
  • ShaderResourceView. you can create using deviceContext->createShaderResourceView() – KIM CHANGJUN May 21 '19 at 04:41
  • as you set CPUaccess READ, i guess you try to read pixel data from CPU, then use deviceContext->map() – KIM CHANGJUN May 21 '19 at 04:45
  • Right, that's what I'm doing but for some reason, that pointer to the data that is mapped contains nothing but null values. –  May 21 '19 at 06:00
  • https://stackoverflow.com/questions/56070883/reading-gpu-resource-data-by-cpu. this one will be right solution i guess – KIM CHANGJUN May 21 '19 at 06:40