Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Compute shader] - Defining an array (Still in need of help)

Discussion in 'Scripting' started by gorbit99, Dec 13, 2016.

  1. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I have a compute shader, and I want to either define, or create an array, in a way so I can reach it form multiple functions at once. Having it as:
    Code (CSharp):
    1. #define float[] array = float[](4.2, 1.4, 4.6);
    2.  
    3. array[0];
    Obviously doesn't work, and I can't think of a way that define might work

    Having it as simply:
    float array[] = float[](4.2, 1.4, 4.6);
    at the beginning of the file doesn't work either, it throws a lot of errors.

    What should I do?
     
  2. Andenberg

    Andenberg

    Joined:
    Nov 5, 2016
    Posts:
    11
    JavaScript version:
    Code (JavaScript):
    1. var floats = [1.4, 1.3, 1.2];
    C# version:
    Code (CSharp):
    1. float[] floats = new float[5/*<---This number represents the amount of items in your array*/] { 1f, 2f, 3f, 4f, 5f };
    Hope this helped bro :)
     
    Last edited: Dec 13, 2016
  3. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    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<float>" 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 :p
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    in a .compute file, so basically in the hlsl language
     
    Last edited: Dec 14, 2016
  5. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    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.

    Good luck!
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    My problem is, that this array is a lookup table, and I want to store it in the file itself, because it is the same every time.
     
    Maxium likes this.
  7. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Ahhh okay gotcha.Then I guess you'd just define variables like this:

    Code (COMPUTE SHADER):
    1.  
    2.  
    3. float4 myArray[3] = { float4(0, 0, 0, 0) , float4(0, 0, 0, 0), float4(0, 0, 0, 0) } ;
    4.  
    (not tested)

    Is that what you mean?

    (and again, I don't know enough about compute shaders to offer much more advice)

    EDIT: changed code - previously wouldn't compile
     
    Last edited: Dec 14, 2016
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    This would be fine if I had like max 10 values in that array, the problem is, I have like 1000 values in it
     
  9. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    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 :p 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....
     
  10. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I want it to work without doing things in other scripts
     
  11. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    And I'd like a "make the game for me" button, but we don't always get what we want :D

    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...
     
  12. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    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
     
  13. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    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 :/ ) :

    Code (COMPUTE SHADER):
    1. float4 myArray[3] = { float4(0, 0, 0, 0) , float4(0, 0, 0, 0), float4(0, 0, 0, 0) } ;
    If that won't work, I honestly don't know any other options :( sorry!

    EDIT: Just to clarify, that is shader code!
     
    Last edited: Dec 14, 2016
  14. UnrealTati

    UnrealTati

    Joined:
    Aug 18, 2017
    Posts:
    5
    int items[1000];

    for(int i=0; i<1000; i++)
    items[ i ] = 0; // 0 can be replaced with anything.
     
    ndeji69 likes this.
  15. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    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?
     
    Tibor0991 likes this.
  16. samf1111

    samf1111

    Joined:
    Sep 18, 2019
    Posts:
    16
    You should probably make a python script or something that generates your array into a text file and then just copy paste it.
     
  17. Nuvix

    Nuvix

    Joined:
    Feb 9, 2014
    Posts:
    4
    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.

    Code (CSharp):
    1. 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.
     
  18. CodeSlacker

    CodeSlacker

    Joined:
    Dec 5, 2014
    Posts:
    3
    Hey I found another HLSL example!
    https://www.gamedev.net/forums/topic/669039-the-most-efficient-way-to-create-a-const-array-in-hlsl/
    The compiler made me use a constant when declaring the array:

    static float2 poissonDisk[16] =
    {
    float2(0.2770745f, 0.6951455f),
    float2(0.1874257f, -0.02561589f),
    float2(-0.3381929f, 0.8713168f),
    float2(0.5867746f, 0.1087471f),
    float2(-0.3078699f, 0.188545f),
    float2(0.7993396f, 0.4595091f),
    float2(-0.09242552f, 0.5260149f),
    float2(0.3657553f, -0.5329605f),
    float2(-0.3829718f, -0.2476171f),
    float2(-0.01085108f, -0.6966301f),
    float2(0.8404155f, -0.3543923f),
    float2(-0.5186161f, -0.7624033f),
    float2(-0.8135794f, 0.2328489f),
    float2(-0.784665f, -0.2434929f),
    float2(0.9920505f, 0.0855163f),
    float2(-0.687256f, 0.6711345f)
    };
     
    GamerLordMat likes this.
  19. GamerLordMat

    GamerLordMat

    Joined:
    Oct 10, 2019
    Posts:
    185
    That is actually very useful, thanks!
     
  20. triangle4studios

    triangle4studios

    Joined:
    Jun 28, 2020
    Posts:
    33
    Just to rebang the drum What Nuvix said, is confirmed the correct Solution.

     
  21. GamerLordMat

    GamerLordMat

    Joined:
    Oct 10, 2019
    Posts:
    185
    I am just happy how easy it got with ChatGPT. Such very simple questions would bother me for hours, but now it is a 30 seconds request.