Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Use computer buffers in pixel shaders with HD Render Pipeline

Discussion in 'Shaders' started by Tracecat, Feb 4, 2019.

  1. Tracecat

    Tracecat

    Joined:
    Mar 20, 2014
    Posts:
    8
    Hi all!
    For my current project I created some shader code which is working on LWRP but not on HDRP. The question is: Why is that? In particular, I raycast a mesh to get a hit position and then pass this position to another shader working on the same mesh.
    In the pixel (fragment) shader part i then calculate the distance of each texel to the hit position and store the result in a compute buffer. This way, I get the uvs of all surrounding texels of a given global 3d postition, which I then use to paint on a texture for the specific mesh.

    All this works(!!) fine using the Lightweight Render Pipeline with the following code: ´

    c# Code:
    Code (CSharp):
    1.              
    2. //Done once in Awake()
    3.              //UvsOfPaintedTexelsComputeBuffer = new ComputeBuffer(256 * 256, sizeof(float),            ComputeBufferType.Default);
    4.  
    5.                 Graphics.ClearRandomWriteTargets();
    6.                 meshPainterMaterial = GetComponentInChildren<Renderer>().material;
    7.                 meshPainterMaterial.SetPass(0);
    8.                 meshPainterMaterial.SetBuffer("data", UvsOfPaintedTexelsComputeBuffer);
    9.              
    10.                 //some global position - currentHitPosition = hitpoint.transform.position
    11.                 meshPainterMaterial.SetVector("_PaintCenter", currentHitPosition);
    12.                 meshPainterMaterial.SetFloat("_StencilSize", selectedRepairItemConfig.StencilSize);
    13.              
    14.                 //already tried but did not work
    15.                 //Graphics.SetRandomWriteTarget(1, UvsOfPaintedTexelsComputeBuffer, true);
    16.                 Graphics.SetRandomWriteTarget(1, UvsOfPaintedTexelsComputeBuffer, false);
    17.  
    18.  
    19.                 //Get the result
    20.                 UvsOfPaintedTexelsComputeBuffer.GetData(UVsOfPaintedTexels);
    Shader code:
    Code (CSharp):
    1.  
    2. Shader "MeshPainterShader"
    3. {
    4.     Properties
    5.     {
    6.         _PaintCenter("PaintCenter", vector) = (0,0,0,0)
    7.         _StencilSize("StencilSize", float) = 0
    8.     }
    9.     Subshader
    10.     {
    11.          Tags {"Queue"="Transparent" "RenderType"="Transparent" }
    12.  
    13.           ZWrite Off
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.             #pragma vertex vertex_shader
    19.             #pragma fragment pixel_shader
    20.             #pragma target 5.0
    21.             float4 _PaintCenter;
    22.             float _StencilSize;
    23.  
    24.             //Register read write compute buffer
    25.             uniform RWStructuredBuffer<float> data : register(u1);
    26.             struct APPDATA
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.                 uint id : SV_VertexID;  
    31.             };
    32.             struct SHADERDATA
    33.             {
    34.                 float4 vertex : SV_POSITION;
    35.                 float2 uv : TEXCOORD0;
    36.                 float3 worldPos : TEXCOORD1;
    37.             };
    38.             SHADERDATA vertex_shader (APPDATA IN)
    39.             {
    40.                 SHADERDATA vs;
    41.                 //calculate world position to be used in pixel shader
    42.                 vs.worldPos = mul (unity_ObjectToWorld, IN.vertex);
    43.                 vs.vertex = UnityObjectToClipPos(IN.vertex);
    44.                 vs.uv = IN.uv;
    45.                 return vs;
    46.             }
    47.  
    48.  
    49.             float4 pixel_shader (SHADERDATA ps) : SV_TARGET
    50.             {
    51.               //Calcualte x,y of a 256x256 sized texture map
    52.               int y = ((int)(ps.uv.y*256.0))*256;
    53.               int x = (int)( ps.uv.x*256.0);
    54.  
    55.               //If the current pixel world position is near the given global position(paint center) store in 'data'
    56.               if(distance(_PaintCenter.xyz, ps.worldPos.xyz) < _StencilSize)
    57.                 data[x + y]  = (_StencilSize - distance(_PaintCenter.xyz, ps.worldPos.xyz)) * (1/_StencilSize);
    58.                        
    59.            
    60.               // Not used. Only data buffer is important.
    61.               return float4(0,0,0,0);
    62.             }
    63.             ENDCG
    64.         }
    65.     }
    66. }
    67.  
    68.  
    Now I moved to the HD RenderPipeline and it fails without an error. It seams that the "data" compute buffer is neither written nor read in the pixel shader. There are some posts about binding the compute buffer to the pixel shader with Graphics.SetRandomWriteTarget(1, UvsOfPaintedTexelsComputeBuffer, false) and that this sometimes fails. However, so far, I had no success in solving this. It is really hard to debug whats going wrong here and i am not sure if the current version of the hd pipeline(4.9.0) even support this. At the moment I really don't know what else to try and I would appreciate any help.
     
    Last edited: Feb 11, 2019 at 9:14 AM
  2. Tracecat

    Tracecat

    Joined:
    Mar 20, 2014
    Posts:
    8
    Bump. I still didn't find any solution for this. I edited the question to clarify my problem a little bit.
    I would really appreciate any help on this because without this solved I am not able to move to HDRP.