Search Unity

Question Sending enums to compute shader?

Discussion in 'Shaders' started by MiConStudio, Apr 13, 2022.

  1. MiConStudio

    MiConStudio

    Joined:
    Sep 29, 2020
    Posts:
    1
    Hey guys! I'm having problems sending an enum value to a compute shader.

    What I basically have is a component Shape with a public enum ShapeType and a public attribute shapeType of type ShapeType, so I can select it from the inspector.

    Code (CSharp):
    1.      
    2. public class Shape : MonoBehaviour
    3. {
    4.     public enum ShapeType {Sphere, Cube, Torus}
    5.    
    6.     public ShapeType shapeType;
    7. }
    In my main script I'm rendering a texture using a compute shader which looks like this

    Code (CSharp):
    1. Texture2D<float4> source;
    2. RWTexture2D<float4> result;
    3.  
    4. struct Shape {
    5.      int shapeType;
    6. };
    7.  
    8. StructuredBuffer<Shape> shapes;
    9. int numShapes = 0;
    10.  
    11. [numthreads(8,8,1)]
    12. void CSMain (uint3 id : SV_DispatchThreadID)
    13. {
    14.      Shape shape = shapes[0];
    15.      result[id.xy] = float4(63.75 * shape.shapeType, 0, 0, 0.0);
    16. }
    The texture is rendered on OnRenderImage which is also where I set the structured buffer and its size. There I get all the objects with Shape in my scene and convert them to an array of ShapeData struct which looks like this.

    Code (CSharp):
    1.    
    2. struct ShapeData {
    3.      public int shapeType;
    4.    
    5.      public static int GetSize() {
    6.             return sizeof (int);
    7.       }
    8. }


    The problem is, when I change the shape type of an object in the scene, the screen stays black, indicating that the shape type remains 0, even though it should be a shade of red. Am I missing something?