Writing to RWTexture3D<float4> in Pixel Shader (518425)

It seems like writing to a RWTexture2D in a pixel shader is possible; I have succeeded in doing this. But when it comes to writing to a RWTexture3D, it doesn’t seem to work at all.

Shader "Custom/RenderToVolume"
{
	Properties 
	{
		_MainTex ("Diffuse (RGBA)", 2D) = "white" {}
	}

	SubShader 
	{
		Pass 
		{
			Tags { "RenderType"="Opaque" }
			Cull Off ZWrite Off ZTest Always Fog { Mode Off }

			CGPROGRAM
			#pragma target 5.0
			#pragma vertex vert
			#pragma fragment frag
			#pragma exclude_renderers flash gles opengl

			#include "UnityCG.cginc"


			sampler2D _MainTex;

			RWTexture3D<float4> volumeTex;
			float volumeResolution;
			float4 volumeParams;

			struct ApplicationToVertex 
			{
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
			};

			struct VertexToFragment 
			{
				float4 pos : SV_POSITION;
				float4 wPos : TEXCOORD0;
				float2 uv : TEXCOORD1;
			};

			void vert(ApplicationToVertex input, out VertexToFragment output)
			{
				output.pos = mul (UNITY_MATRIX_MVP, input.vertex);
				output.wPos = mul(_Object2World, input.vertex);
				output.uv = input.texcoord.xy;
			}

			void frag(VertexToFragment input)
			{
				float4 color = tex2D(_MainTex, input.uv);
				
				float3 volumePos = ((input.wPos.xyz - volumeParams.xyz) / volumeParams.w) * volumeResolution;

				int3 volumeCoords;
				volumeCoords.x = int(volumePos.x); 
				volumeCoords.y = int(volumePos.y); 
				volumeCoords.z = int(volumePos.z); 
				
				volumeTex[volumeCoords] = color;
			}
			ENDCG
		}
	}
	Fallback Off
}

This is what I am trying to do, and it doesn’t seem to work, but if I try and write to a 2D texture, it does. Not sure where I am messing it up.

Bump

Sorry, I can’t really help, but this looks like something I’ve wanted to understand for a while (even just the Texture 2D technique), could you provide an example of how you would use this? Presumably in a multipass shader, yeah? The syntax for that is something I’ve long needed to understand.

There is ample opportunity to ask questions in your own threads or in Unity’s Answers section. All you need to do, is define a RWTexture2D texture much like you would with a regular sampler2D. A render texture would be bound to this from C# / Java script with either Shader.SetGlobalTexture() or Material.SetTexture() functions. Don’t highjack this thread anymore please.

Bump

So, I was able to contact Aras (Unity Graphics Dev), and he believes that there is not currently support for writing to RWTexture3D in shaders, but you CAN write to them in Compute Shaders. You can also write to ComputeBuffers in shaders. I will have to write to ComputeBuffer in my shader, and then use a Compute Shader to copy the contents into a Texture3D with RWTexture3D.

Hello Daniel, I’m trying to write to a RWTexture2D in my pixel shader, but can’t seem to get the grasp of it. Could you tell me how?