How to write to RenderTextureFormat.RGFloat?

So i have created a texture with RenderTextureFormat.RGFloat format, but how do i write to it?

So here is my texture creation code

var lightTexture = RenderTexture.GetTemporary(-1, -1, 0, RenderTextureFormat.RGFloat);

This is how i set it as a render target and then try render to it

ImageEffectCamera.SetTargetBuffers(lightTexture.colorBuffer, source.depthBuffer);
ImageEffectCamera.Render();

but how to write to it? i need to write some color values encoded into a float, and the values are greater than 1.

i try to write to it in the shader like this:

Shader "01DEVS/Light 2D/Global Light Transition Into Override Mask"
{
    Properties
    {
        _MainTex ("Mask (A)", 2D) = "white" {}
    }
 
    SubShader
    {
      
        Tags
        {
            "Queue"="Transparent-1"
            "RenderType"="Transparent"
        }
     
        Pass
        {
            Name "BASE"
            Cull Back
            Lighting Off
            ZWrite Off
            ZTest LEqual
            ColorMask RGBA
            Blend One One
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
          
            sampler2D _MainTex;
            half4 _MainTex_ST;
            struct appdata_mine {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
            };
         
            struct v2f {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR;
            };
         
            v2f vert( appdata_mine v ){
                v2f o;
                o.vertex = mul( UNITY_MATRIX_MVP, v.vertex );
                o.uv = TRANSFORM_TEX( v.texcoord, _MainTex );
                o.color = v.color;
                return o;
            }
         
            float4 frag( v2f i ) : COLOR
            {
                float4 c = (0,0,0,0);
                c.r = tex2D( _MainTex, i.uv).a * i.color.a;
                //encode into float a channel
                c.r = c.r * 255 * 256 * 256 * 256;
                return c;
            }
            ENDCG
        }
    }
}

but the problem is, the render texture has only RG channles, so should i return float2 from fragment shader?
I also get an error trying to return float4

i suppose its because i did something wrong, i don’t know what exactly tho.
Also the color semantic clamps values in 0…1 range as far as i know, which i don’t want it to do, what is the solution here?

Texture format are simply an indication of how the texture is built, to control precisely your storage cost.
RGFloat just a texture with two channel per pixel, 32bit each, so each pixel (e.g. pixels[0,0] ) will be (32 * 2)bit of texture data.

The Shader language don’t care to what it wrote, it will be remapped to the texture.

Meaning writing to color.b or color.a will have no effect. color.r & color.g have each 32bit of precision. so just wrote to those channel, and return a float4 it’s fine.

As for the error it’s pretty strange. Is it using the same shader you pasted here? I can’t see any problem, compile fine for me =/

Also use SV_Target instead of COLOR (color is old D3D semantic) and if your camera is HDR, there is no reason the output should be clamped I think.

1 Like

Very good explanation, thanks!
Camera was not in HDR mode, i will set it. One thing left, the error message.
Here is the full error message

And the shader code is the same, i just removed some commented code from it. Here it is again, full source

Shader "01DEVS/Light 2D/Global Light Transition Into Override Mask"
{
    Properties
    {
        _MainTex ("Mask (A)", 2D) = "white" {}
    }

    SubShader
    {
    
        Tags
        {
            "Queue"="Transparent-1"
            "RenderType"="Transparent"
        }
   
        Pass
        {
            Name "BASE"
            Cull Back
            Lighting Off
            ZWrite Off
            ZTest LEqual
            ColorMask RGBA
            Blend One One
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
       
            #include "UnityCG.cginc"
        
            sampler2D _MainTex;
            half4 _MainTex_ST;
            struct appdata_mine {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
            };
       
            struct v2f {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR;
            };
       
            v2f vert( appdata_mine v ){
                v2f o;
                o.vertex = mul( UNITY_MATRIX_MVP, v.vertex );
                o.uv = TRANSFORM_TEX( v.texcoord, _MainTex );
                o.color = v.color;
                return o;
            }
       
            float4 frag( v2f i ) : SV_Target
            {
    //            float4 c = (0,0,0,0);
                //c.a = tex2D( _MainTex, i.uv).a * i.color.a;//write global transition into override to Alpha channel
                //return c;

                float4 c = (0,0,0,0);
                c.r = tex2D( _MainTex, i.uv).a * i.color.a;//write global transition into override to Alpha channel
                //encode into float a1 channel
                c.r = c.r * 255 * 256 * 256 * 256;
                return c;
            }
            ENDCG
        }
    }
}

I’ve tried to compile it on Standalone platform and on android platform, same error.
Here are the player settings:

I solved the problem with the error.
I copied the shader in another project, deleted from my project, and copied it back, and all of a sudden it was compiling.
There was some kind of bug related to the material, i don’t even know, it makes little to no sense. I tested the shader on different versions of unity, it worked. It worked even on the same version, just in another project.
After copying the shader, i ended up creating a new material from the context menu, because it still glitched out with compile error for some reason, and it worked.