method to access HLSL constants like members of a struct in shadercode

Started by
9 comments, last by evelyn4you 10 months ago

hi,

i have reorganized a lot of my HLSL shadercode but still could not solve the following problem

// e.g. the include file of global usable constants

static const uint gc_diffuse_lambert = 1; // global const for lambert diffuse brdf option
static const uint gc_material_opaque = 5; // global const for material type opaque

in my shadercode i want to access them like

if ( diffuse_type == globals.gc_diffuse_lambert)
{
// do something
}

I know how to set a constantbuffer definition in HLSL shadercode that does exactly what i want (i use them this way all the time), but a constantbuffer cannot be initialized in the shadercode.

I search a method that lets me define the constants as part of a “pseudo struct”
something.member or somethin::member

thanks for all answers

Advertisement

You can declare structs and then global instances of them in HLSL, if that's what you are asking.

@Gnollrunner

it already tried this way but a struct cannot have const members :-(

@evelyn4you I think you can declare the struct and in a separate statement declare an instance of it with initializers using braces. Something like: SMyStruct stVar = { }.;

Edit: I just tried it. It compiles without errors, but I didn't run it. However, I initialize multi-dimensioned arrays like this all the time and it works fine.

@Gnollrunner

this it technically right, but
1. i have to initialize the struct in every shader i use
=> dont know whether runtime init of hundreds of const values gives performance penalty
2. the value is changeable at runtime problem of unwanted change by user
3. possible optimization loss because the value is changeable at runtime and not a explicit const

evelyn4you said:

@Gnollrunner

this it technically right, but
1. i have to initialize the struct in every shader i use

In situations like that I just put them in an include file.


=> dont know wheter runtime init of hundreds of const values gives performance penalty
2. the value is changeable at runtime problem of unwanted change by user

I don't think they are actually, and in any case, you can't meaningfully change them since there is no way to coordinate between calls of a shader. Also, I declare mine as static const.


3. possible optimization loss because the value is changeable at runtime and not a explicit const

Anything is possible, but I wouldn't make that assumption. As I said I've used this in plenty of shaders and declaring globals is not an obscure feature.

@Gnollrunner

hi Gnollrunner,

many thanks for your infos.

Maybe i have missed something. ( i hope :-) )

Could you give me a simple working example how you declare AND INITIALIZE
a global instance of a struct in a include file ?

I know how to init e.g. a global defined const array of ints but i failed to do this with a structure

struct own_struct
{
float A ;
float B ;
float C
}

static const own_struct a_instance = { 1.0f, 2.0f, 3.0f }; => failed so far what is wrong ??

@evelyn4you You missed a couple semi-colons for one. The first one after “float C” and the other after the closing brace of the struct declaration. Other than that, I pasted your code into one of my shaders and it still compiles. I'm using shader model 6.0, but I doubt that would make a difference.

@Gnollrunner

again many thanks.

When i formerly tried to implement it in the way you did, i always got compile errors.

But i will give it a try and report back.

hi Gnollrunner,

problem is solved, it works perfectly.

Simply dont know, what i formerly made wrong.

YOU ARE THE BEST !!!

This topic is closed to new replies.

Advertisement