Search Unity

How to use "Hidden/BlitCopy" shader

Discussion in 'Shaders' started by Simod, Dec 19, 2016.

  1. Simod

    Simod

    Joined:
    Jan 29, 2014
    Posts:
    176
    Hi there,

    I am trying to reproduce Blit (src, dest). But my test code fails:

    Code (CSharp):
    1. Shader shader = Shader.Find ("Hidden/BlitCopy");
    2. Material material = new Material (shader);
    3.  
    4. commandBuffer.Blit (src, dest, material, 0);
    What I am doing wrong?

    =============================================

    Another question should I use my own quad in order to use "Hidden/BlitCopy"? Ex.:
    Code (CSharp):
    1. Shader shader = Shader.Find ("Hidden/BlitCopy");
    2. Material material = new Material (shader);
    3.  
    4. commandBuffer.SetGlobalTexture ("_MainTex", src);
    5.  
    6. commandBuffer.SetRenderTarget (dest);
    7. commandBuffer.DrawMesh (quad, Matrix4x4.identity, material, 0, 0);
    =============================================

    Just everyone know the shader source Ill put this here:
    Code (CSharp):
    1. Shader "Hidden/BlitCopy" {
    2.     Properties { _MainTex ("Texture", any) = "" {} }
    3.     SubShader {
    4.         Pass {
    5.              ZTest Always Cull Off ZWrite Off
    6.  
    7.             CGPROGRAM
    8.             #pragma vertex vert
    9.             #pragma fragment frag
    10.             #pragma target 2.0
    11.  
    12.             #include "UnityCG.cginc"
    13.  
    14.             sampler2D _MainTex;
    15.             uniform float4 _MainTex_ST;
    16.  
    17.             struct appdata_t {
    18.                 float4 vertex : POSITION;
    19.                 float2 texcoord : TEXCOORD0;
    20.             };
    21.  
    22.             struct v2f {
    23.                 float4 vertex : SV_POSITION;
    24.                 float2 texcoord : TEXCOORD0;
    25.             };
    26.  
    27.             v2f vert (appdata_t v)
    28.             {
    29.                 v2f o;
    30.                 o.vertex = UnityObjectToClipPos(v.vertex);
    31.                 o.texcoord = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
    32.                 return o;
    33.             }
    34.  
    35.             fixed4 frag (v2f i) : SV_Target
    36.             {
    37.                 return tex2D(_MainTex, i.texcoord);
    38.             }
    39.             ENDCG
    40.  
    41.         }
    42.     }
    43.     Fallback Off
    44. }
    45.  
     
    unitybru likes this.
  2. Simod

    Simod

    Joined:
    Jan 29, 2014
    Posts:
    176
    UPDATE:

    Well hello Aaron again :)
    Found his post with the same issue at: https://forum.unity3d.com/threads/c...er-blit-with-internal_blitcopy-shader.432699/

    Thanks to his workaround I found the solution to force _MainTex being declared:
    Code (CSharp):
    1. Shader shader = Shader.Find ("Hidden/BlitCopy");
    2. Material material = new Material (shader);
    3. commandBuffer.SetGlobalTexture ("_MainTex", src);
    4. commandBuffer.Blit (src, dest, material, 0);
    This IS the bug, as long as the Unity's documentation states the Blit source will be declared in _MainTex, which is not in this case.

    ===========================================

    Now the question is how can I do this with the "quad" ?
     
    Last edited: Dec 19, 2016