Jump to content
  • Advertisement
Sign in to follow this  
thecheeselover

Variable index

This topic is 2526 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 am a beginner with hlsl and I'd like to know if you actually can do something like this, which is setting a global variable's index with another global scalar variable:


int t = 5;
float g[t];


If you can't do something like this in hlsl, then can you change the array to a greater or lower number through xna?

Thank you smile.png
Thecheeselover

Share this post


Link to post
Share on other sites
Advertisement
Hidden
Yes you can, just make sure you actually have a 5th element, otherwise you go out-of-bounds on your array and that = crash.

If you have an array that needs constant re-sizing or changing, look into STL Vector. Arrays are usually one time created and then you typically don't change it's size after creation, only change whats in the array.

Share this post


Link to post
You can do something like this:

#define Global 5;

float g[Global];
int t = Global;
int z = Global;

Share this post


Link to post
Share on other sites
Arrays in HLSL need to have a length that is static at compile time. So in other words..

// You can do this...
static const uint ArraySize = 5
float Array[ArraySize];

// Or you can do this...
#define ArraySize 5
float Array[ArraySize];

// But you can't do this
uint ArraySize = 5;
float Array[ArraySize];


Usually what you do, is you define the array to some maximum size and then set another variable to a value representing how many values to actually use from the array. Like this:

static const uint MaxValues = 5;
float Values[MaxValues];
uint NumValuesToUse;


Just be careful...depending on the shader profile you use the compiler's ability to represent loops in shader assembly may be limited.

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!