Jump to content
  • Advertisement
Sign in to follow this  
npk48

DX12 [Dx12] Need help with DescriptorHeap

This topic is 1182 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

Hi, I was playing around with dx12 using sharpdx beta.

I'm able to render simple models now. However I went into troubles when trying to render models with several sub parts. The texture I uploaded is not displaying for second part.

 

This is how I do it:

 

1. Create a root signature

var rootSignatureDesc = new RootSignatureDescription(

                RootSignatureFlags.AllowInputAssemblerInputLayout,
                // Root Parameters
                new[]
                {
                    new RootParameter(ShaderVisibility.All,
                        new []
                        {
                            new DescriptorRange()
                            {
                                RangeType = DescriptorRangeType.ShaderResourceView,
                                DescriptorCount = 2,
                                OffsetInDescriptorsFromTableStart = -1,
                                BaseShaderRegister = 0
                            },
                            new DescriptorRange()
                            {
                                RangeType = DescriptorRangeType.ConstantBufferView,
                                DescriptorCount = 1,
                                OffsetInDescriptorsFromTableStart = -1,
                                BaseShaderRegister = 0
                            }
                        }),
                    new RootParameter(ShaderVisibility.Pixel,
                        new DescriptorRange()
                        {
                            RangeType = DescriptorRangeType.Sampler,
                            DescriptorCount = 1,
                            OffsetInDescriptorsFromTableStart = -1,
                            BaseShaderRegister = 0
                        }),
                });

2. Create Descriptor Heap for srv and cbf


var srvCbvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = 3,
                Flags = DescriptorHeapFlags.ShaderVisible,
                Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView
            };

            srvCbvHeap = device.CreateDescriptorHeap(srvCbvHeapDesc);

3.Upload textures

var tex = Texture.LoadFromFile("../../models/test.tga");
            byte[] textureData = tex.Data;
            TextureWidth = tex.Width; TextureHeight = tex.Height;

            var textureDesc = ResourceDescription.Texture2D(Format.B8G8R8A8_UNorm, TextureWidth, TextureHeight, 1, 1, 1, 0, ResourceFlags.None, TextureLayout.Unknown, 0);
            texture = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, textureDesc, ResourceStates.GenericRead, null);
          
            texture.Name = "Texture";

            var handle = GCHandle.Alloc(textureData, GCHandleType.Pinned);
            var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(textureData, 0);
            texture.WriteToSubresource(0, null, ptr, TextureWidth * 4, textureData.Length);
            handle.Free();

4. Create Shader Resource View Description for each texture by offsetting the heap address


var Step = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);
......
device.CreateShaderResourceView(texture[i], srvDesc, srvCbvHeap.CPUDescriptorHandleForHeapStart + I * Step);

5.When building command list I set the descriptor table for each texture resource by offsetting


commandList.SetGraphicsRootSignature(rootSignature);
            DescriptorHeap[] descHeaps = new[] { srvCbvHeap, samplerViewHeap };
            commandList.SetDescriptorHeaps(descHeaps.GetLength(0), descHeaps);
            commandList.SetGraphicsRootDescriptorTable(0, srvCbvHeap.GPUDescriptorHandleForHeapStart);
            commandList.SetGraphicsRootDescriptorTable(1, samplerViewHeap.GPUDescriptorHandleForHeapStart);

......
draw first
......

commandList.SetGraphicsRootDescriptorTable(0, srvCbvHeap.GPUDescriptorHandleForHeapStart + Step * i);

......
draw second
......

But the thing is only the first part will have texture. I'm still trying to understand how the whole resource management works but it looks so complex. Please help, I checked the cpp sample code on GitHub but can't find what I did wrong.

 

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

Hi, I'm using SharpDx too.

Well, how I can see you are using 3 items (2 for textures and 1 constant buffer) in the same RootParameters

 

so, from any position in your heap, D3D expects this 3 items

For example

tex1,tex2,constant buffer.

If you set offset to 0 D3D will start loading resource from tex1 position.

 

But if you increase just by one you'll have it starting from tex2 and read like this

tex2,constant buffer,??? 

so you must pay attention. It doesn't seem right.

 

 

When you combine many resources in one RootParameter you must pay attention that items are in the right number and order.

 

I've published some demo in SharpDx on Github

 

https://github.com/RobyDX/SharpDX_D3D12HelloWorld

and I've a website too (http://www.notjustcode.net) where I've started Direct3D12 tutorial using SharpDx. It's in italian but you can try to translate with bar I've added?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!