Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Error when attempting to read from input RWTexture2D<float4>

Discussion in 'Shaders' started by Technokid2000, Dec 28, 2021.

  1. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    Hello!

    I am fairly new to writing compute shaders, so please bare with me.
    I am creating a custom mesh in Unity3D, but I am doing it through the use of the GPU. However, while I can happily write to a RWTexture2D<float4> without issue, reading from it is a bit different.

    The offending HLSL code (I only need the first line of pixels at this stage):
    Code (CSharp):
    1. #pragma kernel SubdivideMesh
    2.  
    3. float size;
    4. RWTexture2D<float4> InputTrigData;
    5. RWTexture2D<float4> OutputTrigData;
    6.  
    7. [numthreads(8, 8, 1)]
    8. void SubdivideMesh(uint3 id : SV_DispatchThreadID) {
    9.   int index = id.x;
    10.  
    11.   float4 origPos1 = InputTrigData[index][0];
    12.  
    13.   //Make modifications to "origPos1" here
    14.  
    15.   OutputTrigData[index][0] = origPos1;
    16. }
    17.  
    The error being thrown is:
    Code (CSharp):
    1. Shader error in 'PGSFirstWorld.compute': invalid type for index - index must be a scalar, or a vector with the correct number of dimensions at kernel SubdivideMesh at PGSFirstWorld.compute(54) (on d3d11)
    which is referring to how I'm trying to get the float4 (RGBA) out of the RWTexture2D<float4>. Am I missing something here about how textures work on the GPU? I thought that RWTexture2D variables are treated just like a standard 2D array of float4's, so you can just do "InputTrigData[X co-ordinate][Y co-ordinate]" and just have a float4 come out?

    I am incredibly confused, and it seems like such an incredibly simple thing I'm trying to do. Just read a pixel from the incoming texture and save that as a float4.

    Any help would be greatly appreciated!
     
  2. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    Edit: I found a different way around it using "RWStructuredBuffer<float3>" using the "ComputeBuffer" class which works perfectly for this project. However, I am still curious as to how to access 2D textures on the GPU like above for a different project later on.
     
  3. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
    Almost!
    Code (CSharp):
    1. uint2 index = uint2(id.x, 0);
    2. OutputTrigData[index] = InputTrigData[index];
     
    Technokid2000 likes this.
  4. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    Thankyou so much! I knew I couldn't have been far off the mark.

    I shall keep this in mind next time I work with 2D textures, which should be very soon!