[SOLVED]HLSL Syntax Clarification

Started by
2 comments, last by MichaelBarth 7 years, 7 months ago

Hi there! I hope I'm in the proper spot here. I've been trying for the life of me to try to figure out how a parallax shader works and have been trying to implement in the Source Engine. Needless to say I'm really held back by not having any math knowledge involving tangents, matrices, and whatnot.

But anyway, the question I'm asking is a little more simple than all that, and that's about the syntax of "const" in HLSL. There seem to be some shader header files that reference matrices and vectors that are labeled as const. Such as:


const HALF3 g_EyePos : register(c10);

I dunno if that's part of the HLSL language by default, I just know that in VS it says HALF3 is just a redefinition of float3. But why would something like g_EyePos, which clearly references eye position or camera position, be marked as const? Isn't that something that would be changing quite often? When I looked up the const syntax on Microsoft's website it says that it means it can't be changed in the shader.

Does it mean just that? That it can't be changed in the shader, but it's value may constantly be updated like in the C++ shader code through that c10 register? (I'm sorry, I don't know what you call it, so I'm calling it the c10 register, I know slightly more about GLSL than I do HLSL) Or does it mean it does not update at all?

Sorry for something that's probably a simple question, I just don't understand.

Advertisement

I think it means that it's value cannot be changed within the shader (unless you copy it to a local variable in your shader).

This makes sense, because your eyePos is sent from your application to the shader, to be used as a 'constant input'.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

So by default a global variable in HLSL is considered to have the uniform storage class, as if you put the "uniform" keyword before the variable. The uniform modifier works the same as it does in GLSL: it indicates that the value is constant for the lifetime of the shader program, and the value of that variable is specified by the app code. Global variables are also considered to be "const" by default, so it's a bit superfluous in this particular case. The "const" basically enforces that the variable is read-only, and generates a compiler error if the shader code tries to assign a value to the variable. In D3D9 your global uniform variables are mapped to constant registers, which you set from your app code using functions like SetVertexShaderConstantF. In D3D10 and higher, global uniform variables must be placed in contiguous buffers called constant buffers.

The only other common use case for global variables is for for "truely" const values, where the value is always the same for the program and isn't specified by the app code. To do this you need to use the "static" modifier.

Ah, thank you both very much, that clears it up for me. I'm not worried about DX10+, as the Source Engine I believe only supports up to DX9, but that makes a lot of sense.

This topic is closed to new replies.

Advertisement