problem with Creating textures in DirectX 12

843 Views Asked by At

As in the title I have problem with a simple texture in DirectX 12. It's hard for me to describe the whole thing so looking at the code will explain the situation that I'm stuck with.It gives me a black color. Texture:

    auto commandqueue = D3D12Core::Get().GetCommandQueue(D3D12_COMMAND_LIST_TYPE_DIRECT);
auto commandlist = commandqueue->GetCommandList();

stbi_uc* data = stbi_load(filePath.c_str(), &m_Width, &m_Height, &m_BytesPerPixel, 0);
m_TextureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
m_TextureDesc.Alignment = 0; 
m_TextureDesc.Width = m_Width;
m_TextureDesc.Height = m_Height;
m_TextureDesc.DepthOrArraySize = 1;
m_TextureDesc.MipLevels = 1; 
m_TextureDesc.Format = DXGI_FORMAT_R16G16B16A16_UNORM; 
m_TextureDesc.SampleDesc.Count = 1;
m_TextureDesc.SampleDesc.Quality = 0; 
m_TextureDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; 
m_TextureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;

auto device = D3D12Core::Get().GetDevice();

device->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE, &m_TextureDesc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&texture));

UINT64 textureUploadBufferSize;

device->GetCopyableFootprints(&m_TextureDesc, 0, 1, 0, nullptr, nullptr, nullptr, &textureUploadBufferSize);

device->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer(textureUploadBufferSize), D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&UplodeTexture));

subresource.pData = data;
subresource.RowPitch = m_Height * m_BytesPerPixel;
subresource.SlicePitch = subresource.RowPitch * m_Width;

UpdateSubresources(commandlist.Get(), texture, UplodeTexture,0,0,1, &subresource);

commandlist->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(texture, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));

Shader Resource View:

    auto device = D3D12Core::Get().GetDevice();

    D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
    srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
    srvDesc.Format = m_TextureDesc.Format;
    srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
    srvDesc.Texture2D.MipLevels = 1;
    device->CreateShaderResourceView(texture,&srvDesc, m_SRVHeap->GetCPUDescriptorHandleForHeapStart());

Descriptor table parameter in root signature :

 D3D12_DESCRIPTOR_RANGE  descriptorTableRanges; 
descriptorTableRanges.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; 
descriptorTableRanges.NumDescriptors = 1; 
descriptorTableRanges.BaseShaderRegister = 0; 
descriptorTableRanges.RegisterSpace = 0; 
descriptorTableRanges.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND; 

D3D12_ROOT_DESCRIPTOR_TABLE descriptorTable = {};
descriptorTable.NumDescriptorRanges = 1;
descriptorTable.pDescriptorRanges = &descriptorTableRanges;

m_RootParameters[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
m_RootParameters[1].DescriptorTable = descriptorTable;
m_RootParameters[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;

The static sampler that I gave it to root signature:

D3D12_STATIC_SAMPLER_DESC sampler = {};
sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
sampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
sampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
sampler.MipLODBias = 0;
sampler.MaxAnisotropy = 0;
sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
sampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
sampler.MinLOD = 0.0f;
sampler.MaxLOD = D3D12_FLOAT32_MAX;
sampler.ShaderRegister = 0;
sampler.RegisterSpace = 0;
sampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;

And I set the descriptor table through command list:

m_SRV = m_SRVHeap->GetGPUDescriptorHandleForHeapStart();
ID3D12DescriptorHeap* heap = m_SRVHeap.Get();
commandList->SetDescriptorHeaps(1, &heap);
commandList->SetGraphicsRootDescriptorTable(1, m_SRV);

Pixel shader:

struct PixelShaderInput
{
float2 TexCoord : TEXCOORD;
float4 Color    : COLOR;
};
Texture2D tex : register(t0);
SamplerState s1 : register(s0);
float4 main(PixelShaderInput IN) : SV_TARGET
{
return tex.Sample(s1, IN.TexCoord);
}

EDIT:

I haven't enabled the GPUBasedValidation debug layer and after I enabled it it gives the following error:

D3D12 ERROR: GPU-BASED VALIDATION: Draw, Incompatible resource state: Resource: 0x0D1FFFF8:'TextureResource', Subresource Index: [0], Descriptor heap index to DescriptorTableStart: [0], Descriptor heap index FromTableStart: [0], Binding Type In Descriptor: SRV, Resource State: D3D12_RESOURCE_STATE_COPY_DEST(0x400), Index of Descriptor Range: 0, Shader Stage: PIXEL, Root Parameter Index: [1], Draw Index: [0], Shader Code: C:\DevPractice\D3D12Test\D3D12Test\src\Shaders\Pixel.hlsl(10,2-40), Asm Instruction Range: [0x60-0x8b], Asm Operand Index: [3], Command List: 0x0D385EE0:'Unnamed ID3D12GraphicsCommandList Object', SRV/UAV/CBV Descriptor Heap: 0x03F2AEC0:'Unnamed ID3D12DescriptorHeap Object', Sampler Descriptor Heap: , Pipeline State: 0x0D4A9788:'Unnamed ID3D12PipelineState Object', [ EXECUTION ERROR #942: GPU_BASED_VALIDATION_INCOMPATIBLE_RESOURCE_STATE]

0

There are 0 best solutions below