Jump to content

  • Log In with Google      Sign In   
  • Create Account

[SharpDX] Questions about DirectX11


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 cephalo   Members   -  Reputation: 575

Like
0Likes
Like

Posted 06 December 2012 - 09:55 AM

I've been trying to convert a dx9 project of mine to dx11, and there are some concepts that are still confusing to me.

All of the samples I have seen use texture arrays for multiple textures, and they use PixelShader.SetShaderResource(0, myTextureView) to set the variously named texture global variable in the shader, let's say its called 'picture' or whatever. Since there is only one texture resource in the shader in these samples, there is no ambiguity (I guess), but what if you do need multiple texture resources? Will it organize them in order by slot number?

For example, I have a simple water plane shader in my dx9 project that, instead of using fog in the distance, it blends with the same cubemap that I use for the sky box. So in this shader I need a texture array for the cubemap, plus another texture for the water ripples. So, if in my shader I have the globals:
[source lang="plain"]Texture2D skyTextureTexture2D waterTexture[/source]
Will the texture parameters be filled in the order I call the SetShaderResource function? In slot order? How does it know which texture view to put into which variable?

I'm still a little confused on just what a 'slot' is. It's kindof like the different vertex streams in dx9 but not really. There's a limited number of slots so there's a limited number of each type of resource right? I'm sure I don't get it.

Sponsor:

#2 Zaoshi Kaba   Crossbones+   -  Reputation: 4656

Like
1Likes
Like

Posted 06 December 2012 - 11:20 AM

You have to specify slots in the shader, that's how it'll know; ex.:
SamplerState sampler0 : register(s0);
Texture2D texNormal : register(t0);
Texture2D texDepth : register(t1);


#3 MJP   Moderators   -  Reputation: 11844

Like
1Likes
Like

Posted 06 December 2012 - 03:50 PM

You don't have to specify registers in the shader. It can be convenient to do so if you want to be explicit, but it's not required. If you don't assign one yourself, the compiler will bind all used resources to a register based on the resource type. There are 16 "s" registers for samplers, 128 "t" registers for shader resource views, 16 "b" registers for constant buffers, and 8 "u" registers for unordered access viewers. These registers then correspond to the "slots" that you specify to API calls like PSSetShaderResources. So if a texture is bound to register t2, then in your app code you'll want to bind your shader resource view to slot 2.

If you're going to hard-code the slots in your app code, then you'll want probably want to explicitly specify the register in your shader code. Otherwise you might run into the cases where the compiler doesn't allocate the register that you think it will, often because a resource doesn't end up getting used in the shader and therefore gets optimized out entirely.

#4 xoofx   Members   -  Reputation: 891

Like
2Likes
Like

Posted 06 December 2012 - 06:55 PM

Also, in case you want to access by the name of the variable in your shader and get the slot associated with this name, you can query these slots using D3DReflect (via the ID3D11ShaderReflection or ShaderReflection in SharpDX), and shaderReflection.GetResourceBindingDesc() methods. This is used by legacy D3DX Effect systems. Note that the reflection API is not certified API on the Windows Store App so if you really need this access at runtime, you will have to store them along the bytecode of your shader.

While it can be practical to hardcode these specifics register slots in the original shader, you have less opportunity after this to combine includes/shaders (as you could have some conflicts: for example if you declare in a include TextureA.h a TextureA mapped to register t0, and in TextureB.h a TextureB mapped to register t0, the HLSL compiler will failed at compiling a shader that is using these two variables for the same stage). Though assigning a specific register can be handy in some cases where you want a specific resources to be accessible at a specific slot (for example a particular constant buffer used across all your shaders... etc.), while the others variable could be assigned automatically to the available registers.

Edited by xoofx, 06 December 2012 - 06:55 PM.


#5 cephalo   Members   -  Reputation: 575

Like
0Likes
Like

Posted 07 December 2012 - 08:59 AM

Thanks everyone. So in the SharpDX case of PixelShader.SetShaderResource(0, myTextureView), the function specifically requires a slot number.

If the effects framework is legacy, I probably don't want to become dependent on it.

So let me get this straight. Let's say I set 3 constant buffers with these API calls:

[source lang="csharp"]PixelShader.SetConstantBuffer(0,cb0);PixelShader.SetConstantBuffer(1,cb1);PixelShader.SetConstantBuffer(2,cb2);[/source]

If I don't specify registers in the shader code, and cb1 is not used and optimized away, then cb2 would end up in register b1? I still don't understand how its possible to know which buffer goes into which global variable, especially if they happen to be the same size. Perhaps its the order that the names appear in the shader file?

Edited by cephalo, 07 December 2012 - 09:50 AM.


#6 MJP   Moderators   -  Reputation: 11844

Like
1Likes
Like

Posted 07 December 2012 - 04:50 PM

If you set a constant buffer into slot 2, it gets bound to register 2. No exceptions.

What we were talking about was how the compiler assigns registers to resources declared in your shader code. In general the compiler will assign in order of declaration, but this isn't really something you can rely on. If you're not going to assign registers in your code, then you probably want to use the reflection interfaces to query the register based on the resource name and type.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS