Variable index

Started by
2 comments, last by bdubreuil 12 years, 3 months ago
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

Hide yo cheese! Hide yo wife!

Advertisement
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.
Black Sky A Star Control 2/Elite like game
You can do something like this:

#define Global 5;

float g[Global];
int t = Global;
int z = Global;
Black Sky A Star Control 2/Elite like game
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.
Thank you both! :)

Hide yo cheese! Hide yo wife!

This topic is closed to new replies.

Advertisement