You mean in a .compute file, a compute shader? If so, then it depends what kind of data your trying to keep a collection of, and what your gonna do with it. If you want to put a collection of data on the GPU memory for doing stuff to, in the shader, then you would probably use a “StructuredBuffer” and send data from the CPU side (scripting side) by using a compute buffer, and do myComputeBuffer.SetData(myFloatArray); to get it into GPU memory, then operate on it in myComputeShader.Dispatch() method. If you want to get information BACK from the GPU, it would need to be a RWStructuredBuffer… at least that is my understanding.
I’m not very good at compute shaders but I think this is what your asking right? Beyond that, I can’t help you
Yeah then you should probably use a structured buffer, send the data over from the c#/ CPU side, to the GPU with a compute buffer. Afterwards you can work on the data over there in the shader, like I described above.
I’d say that your best bet is to assign the first version from CPU side, then operate from the GPU side after the data is assigned from a compute buffer… because it allows you to do the whole “set big array to this” kind of funtionality… and you use structured buffers in a very similar way to arrays…
EDIT: another silly idea might be to write a simple editor script that outputs all that data into a text file, in the format you need it for the shader code, and just copy paste the big array part in that might sound a little overkill, but you could for instance, take an array of pixels of a texture (color array) and then save all the floats in the same format as the shader handles it, and save yourself the time of manually typing out all those numbers in the shader file…
And I’d like a “make the game for me” button, but we don’t always get what we want
Haha, no seriously, I can’t think of a better way, you might be stuck doing some funky workaround if you want to avoid typing the data in, or handing it off in a compute buffer…
I don’t want to avoid typing the data in, I just want to create an array in hlsl that contains data from the start, that I don’t need to change without passing it from a script and i can access it from different functions
Then this is pretty much the only way I can imagine to do it (I’ll reiterate how I don’t know much about shaders and might not be the best person to answer - however this topic is so poorly documented I doubt anybody else is gonna be too helpful ) :
OP wants the array to be a lookup table with 1000 entires. This, in most cases, means that the values have some meaning and cannot simply be inserted using a loop. But either way… was it really necessary to necro a 4 year old post?
Please do not shoot me for necroing this thread (again). It is very highly ranked on Google when searching for how to do this, and I want to help other people have an easier time figuring it out than I did.
The answer is the static keyword.
static int values[] = { 1, 2, 3, 4, 5 };
If you leave out the static keyword, the compiler will complain that initial values must be literals.