How to Set Pixel Shader Constants

Started by
7 comments, last by MysteryX 8 years, 7 months ago

Alright, I'm getting back into trying to run a shader through DirectX. This time I'm looking at DirectX11 and DirectCompute as it was suggested. It appears to make it simpler than with DX9 to run the shader without needing to configure tons of "stuff" that isn't directly related to what I'm doing (like vertexes).

Now I have a question as to how to configure the shader parameters. In DirectX 9, this works


m_pPixelConstantTable->SetInt(m_pDevice, name, value);
m_pPixelConstantTable->SetFloat(m_pDevice, name, value);
m_pPixelConstantTable->SetBool(m_pDevice, name, value);

In DirectX 11, it appers I have to instead use ID3D11DeviceContext::PSSetConstantBuffers and create a ID3D11Buffer for each parameter. This would require considerably more code, and more importantly... how does the shader know the data type of a parameter?

What's the simplest syntax for defining parameters for the shader?

Thanks

Advertisement

The idea is to create one buffer per update frequency. So for example, one buffer for all the parameters that change only once per frame, one buffer for parameters that change occasionally and a buffer for parameters that change with every draw call.

Each cbuffer in HLSL essentially declares a struct. You then duplicate that struct in C++ and fill a buffer with that data. The memory layout of the HLSL and the C++ struct have to be identical, so watch out for the packing rules.

current project: Roa


In DirectX 11, it appers I have to instead use ID3D11DeviceContext::PSSetConstantBuffers and create a ID3D11Buffer for each parameter. This would require considerably more code, and more importantly... how does the shader know the data type of a parameter?

No, no.

You create a constant buffer in shader with all your variables in it like this

cbuffer cbPerFrameData : register(b0)

{

float3 SomeData;

float3 AnotherData;

};

and you create same struct in your C++ code like this:

struct CBPerFrameData

{

XMFLOAT4 SomeData;

XMFLOAT4 AnotherData;

};

and you create constant buffer of it using the bind flag D3D11_BIND_CONSTANT_BUFFER and then you use PSSetConstantBuffers or VSSetConstantBuffers to upload it to the shader.

Check this out:

https://github.com/newtechnology/NT-Engine/blob/master/NT%20Engine/ConstantBuffer.h

https://github.com/newtechnology/NT-Engine/blob/master/NT%20Engine/ConstantBuffer.cpp

https://github.com/newtechnology/NT-Engine/blob/master/NT%20Engine/Shaders.h

https://github.com/newtechnology/NT-Engine/tree/master/NT%20Engine/Shaders

I don't get to define how they are being received in the HLSL shaders as I'm using pre-written shaders

https://github.com/zachsaw/MPDN_Extensions/blob/master/Extensions/RenderScripts/SuperRes/SuperResEx.hlsl


sampler s0    : register(s0);
sampler sDiff : register(s1);
float4 p0      : register(c0);
float2 p1      : register(c1);
float4 size1  : register(c2); // Original size
float4 args0  : register(c3);

In this case, do I have to create a buffer for each one? Also it is using PS_3_0

Well samplers (and other resources) aren't set in a constant buffer. Those are separate calls (e.g. PSSetSamplers)

Those parameters in that HLSL file will wind up in a default global constant buffer (named $Global). So while not defined in the same fashion the other posters are talking about, at the end of the day it'll be treated as any other constant buffer that contains p0, p1, size1, args0.

Edit: Fixed the link...huh not sure why it was broken.

The link you posted is not valid, but I suppose that I have to use PSSetShaderResources for samplers?

https://msdn.microsoft.com/en-us/library/windows/desktop/ff476473%28v=vs.85%29.aspx

So basically, I only need to create one buffer, with the byte order being the same as in the shader code?

2 questions.

1. How do I define the buffer as being the default named "$Global" ?

2. In the sample I posted, the data types are float4, float2, float4 and float4. How do I use these data types in C++? Is it full float or half-float?

And in my case, parameters are assigned dynamically to run any shader (at least those who follow this parameters format) so I can't hard-code it as a struct. I suppose it will work just the same by filling the buffer with the right data in the right order?

I fixed the link (not sure why copy and paste failed me...!). I was trying link to PSSetSamplers.

So the point of the default constant buffer is that you don't define it. If any of your parameters are defined in the global scope (as in, not inside an explicitly defined cbuffer or tbuffer) then they automatically will be in a buffer called $Global. There's also another default constant buffer called $Param for uniforms that are defined in the parameter list of your main function. (See this article). For pre-D3D10 shaders to work those parameters have to end up somewhere, so HLSL puts them in $Global rather than failing and forcing you to rewrite your shader code. I think $Global will always be bound to slot 0, but don't quote me on that.

As for the data types, they'll be 32-bit floats. See the HLSL data types page. Knowing the data type sizes is important. E.g. Bool in HLSL will be 4 bytes, but in C# it's 1 byte...I *think* the same is also in C++. I agree somewhat with not wanting to have to replicate your constant buffer as a struct on the CPU side. People just do that for convenience more than anything (although padding and alignment rules can throw a wrench in your direction). You may want to look up on how to do shader reflection when you compile your HLSL fragments. That way you can obtain metadata about what constant buffers have what parameters, the size/type of those parameters, and the offsets of each parameter in the buffer. I have something similar in my setup, where I can reference parameters by index/name and set individual floats, vectors, etc or even come up with a struct that represents everything and set it once. The buffer is just a bunch of bytes and can be treated as such. Doing graphics programming in C#, I tend to not want to worry about struct padding and just reference each parameter individually...

Depending on what you want to do and learn, you could also use Effects11. Everything I described above, Effects11 does for you. It's distributed separately from Direct3D now, but it still seems to be maintained by Microsoft.

How do I set the buffer variables without setting a buffer constant? Should I use PSGetConstantBuffers(0, 1) instead of PSSetConstantBuffers to get a buffer already created by default?

DirectCompute was looking easy so far, but now configuring its parameters seems to be a real headache.

Creating and passing a texture object is pretty easy, but if I have to instead use PSSetSamplers, how do I bind that to the texture? I'm not finding any argument for that in CreateSamplerState or D3D11_SAMPLER_DESC. It seems I simply have to call CreateSamplerState instead of CreateShaderResourceView after creating the texture, except that I don't see where to pass in the texture.

And then, will DX11 fully support running PS_3_0 shaders? It seems to be designed for PS_4_0.

This topic is closed to new replies.

Advertisement