Advertisement Jump to content
Sign in to follow this  

DX12 [D3D12] Uploading Textures

This topic is 1176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



I have been trying to upload a texture using directx12 and I cant figure out what I'm missing. I have almost the same exact code as the "D3D12DynamicIndexing" sample, but the texture appears completely black. I have checked that the data that I'm trying to upload is not null so im guessing that I'm messing up somewhere else.




This is the code that I'm using to upload the texture, does anybody know where I'm messing up?

  int x = 512, y = 512, n = 4;
  unsigned char *data = stbi_load( "image.png", &x, &y, &n, 4 );

  D3D12_RESOURCE_DESC textureDesc{};
  textureDesc.MipLevels = 1;
  textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  textureDesc.Width = x;
  textureDesc.Height = y;
  textureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
  textureDesc.DepthOrArraySize = 1;
  textureDesc.SampleDesc.Count = 1;
  textureDesc.SampleDesc.Quality = 0;
  textureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;

  result = m_device->CreateCommittedResource(
    IID_PPV_ARGS( &m_texture ) );
  assert( result == S_OK && "CreateCommittedResource FALILED" );

  const UINT subresourceCount = textureDesc.DepthOrArraySize * textureDesc.MipLevels;
  const UINT64 uploadBufferSize = GetRequiredIntermediateSize( m_texture.Get(), 0, subresourceCount );

  result = m_device->CreateCommittedResource(
    &CD3DX12_RESOURCE_DESC::Buffer( uploadBufferSize ),
    IID_PPV_ARGS( &m_texture_upload ) );
  assert( result == S_OK && "CreateCommittedResource FALILED" );

  D3D12_SUBRESOURCE_DATA textureData = {};
  textureData.pData = data;
  textureData.RowPitch = static_cast< LONG_PTR >( ( 4 * x ) );;
  textureData.SlicePitch = textureData.RowPitch*y;

  UpdateSubresources( m_commandList.Get(), m_texture.Get(), m_texture_upload.Get(), 0, 0, subresourceCount, &textureData );
  m_commandList->ResourceBarrier( 1, &CD3DX12_RESOURCE_BARRIER::Transition( m_texture.Get(),

  D3D12_SAMPLER_DESC samplerDesc = {};
  samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
  samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  samplerDesc.MinLOD = 0;
  samplerDesc.MaxLOD = D3D12_FLOAT32_MAX;
  samplerDesc.MipLODBias = 0.0f;
  samplerDesc.MaxAnisotropy = 1;
  samplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
  m_device->CreateSampler( &samplerDesc, m_samplerHeap->GetCPUDescriptorHandleForHeapStart() );

  D3D12_SHADER_RESOURCE_VIEW_DESC diffuseSrvDesc = {};
  diffuseSrvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  diffuseSrvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  diffuseSrvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  diffuseSrvDesc.Texture2D.MipLevels = 1;
  m_device->CreateShaderResourceView( m_texture.Get(), &diffuseSrvDesc, cbvSrvHandle );
  cbvSrvHandle.Offset( m_cbvSrvDescriptorSize );


Share this post

Link to post
Share on other sites

Ok, just realized reading the code for the sample that its recorded inside a command list and then executed.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!