Search Unity

Problems Trying To Write To RWTextures From Frag Shaders..

Discussion in 'Shaders' started by brilliantgames, Jun 20, 2017.

  1. brilliantgames

    brilliantgames

    Joined:
    Jan 7, 2012
    Posts:
    1,937
    No matter what I do, I cannot seem to write to an RWTexture from a frag shader. Simply nothing happens, and there is absolutely no documentation on this. I've looked through the forums and lots of others seem to have similar problems, but I never see any real solutions.


    In this shader I attempt to write red into 0,0 of the RWTexture, but the value always comes out black.

    Code (CSharp):
    1. Shader "Custom/RenderToVolume"
    2. {
    3.      Properties
    4.      {
    5.          _MainTex ("Diffuse (RGBA)", 2D) = "white" {}
    6.      }
    7.      SubShader
    8.      {
    9.          Pass
    10.          {
    11.              Tags { "RenderType"="Opaque" }
    12.              Cull Off ZWrite Off ZTest Always Fog { Mode Off }
    13.              CGPROGRAM
    14.              #pragma target 5.0
    15.              #pragma vertex vert
    16.              #pragma fragment frag
    17.              #pragma exclude_renderers flash gles opengl
    18.              #include "UnityCG.cginc"
    19.              sampler2D _MainTex;
    20.              RWTexture2D<float4> volTex;
    21.              float volumeResolution;
    22.              float4 volumeParams;
    23.  
    24.  
    25.  
    26.  
    27.              struct ApplicationToVertex
    28.              {
    29.                  float4 vertex : POSITION;
    30.                  float4 texcoord : TEXCOORD0;
    31.              };
    32.              struct VertexToFragment
    33.              {
    34.                  float4 pos : SV_POSITION;
    35.                  float4 wPos : TEXCOORD0;
    36.                  float2 uv : TEXCOORD1;
    37.              };
    38.              void vert(ApplicationToVertex input, out VertexToFragment output)
    39.              {
    40.                  output.pos = UnityObjectToClipPos (input.vertex);
    41.                  output.wPos = mul(unity_ObjectToWorld, input.vertex);
    42.                  output.uv = input.texcoord.xy;
    43.              }
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50.              void frag(VertexToFragment input)
    51.              {
    52.                  float4 color = tex2D(_MainTex, input.uv);
    53.                
    54.            
    55.                  volTex[float2(0,0)] = float4(1,0,0,1);
    56.                
    57.              }
    58.              ENDCG
    59.          }
    60.      }
    61.      Fallback Off
    62. }


    Here is the C# part.
    Code (CSharp):
    1.     void OnPostRender(){
    2.         Graphics.ClearRandomWriteTargets ();
    3.  
    4.  
    5.  
    6.         Graphics.SetRandomWriteTarget(1, RText);
    7.         cam.SetReplacementShader(RShader, "RenderType");
    8.  
    9.  
    10.         Shader.SetGlobalTexture ("volTex",RText);
    11.  
    12.         cam.SetReplacementShader(RShader, "RenderType");
    13.         ComputeVol.SetTexture (1,"VolumeIn",RText);
    14.         ComputeVol.SetTexture (0,"VolumeIn",RText);
    15.  
    16.         Graphics.ClearRandomWriteTargets ();
    17.  
    18.  
    19.     }
    Here's where the render texture(RText) is intialized

    Code (CSharp):
    1.     RenderTexture NewText(int Res)
    2.     {
    3.         RenderTexture Diftex = new RenderTexture(Res, Res, 16, RenderTextureFormat.ARGBFloat);
    4.  
    5.         Diftex.hideFlags = HideFlags.HideAndDontSave;
    6.         Diftex.filterMode = FilterMode.Point;
    7.  
    8.         Diftex.enableRandomWrite = true;
    9.         Diftex.useMipMap = false;
    10.  
    11.         Diftex.Create();
    12.         Graphics.SetRenderTarget(Diftex);
    13.         GL.Clear(false, true, new Color(0,0,0,0));
    14.         return Diftex;
    15.     }
     
  2. brilliantgames

    brilliantgames

    Joined:
    Jan 7, 2012
    Posts:
    1,937
    Bump. Anyone solved this problem?
     
  3. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    Bump: Also having a similar issue. Can't get a RWTexture2D written to in a pixel shader. I've defined mine like this based on another thread:

    uniform RWTexture2D<float4> _RWTexture : register(u1);

    supposedly specifying the register is key.
     
  4. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    NedMakesGames likes this.