Search Unity

[LWRP] Custom ScriptableRenderFeature shader works in editor, goes pink in build?

Discussion in 'Shaders' started by Raul_T, Oct 23, 2019.

  1. Raul_T

    Raul_T

    Joined:
    Jan 10, 2015
    Posts:
    363
    Hello!

    I'm running in a bit of an issue with ScriptableRendererFeatures and at this point I don't know if I'm doing something wrong or if it's an issue with the pipeline itself.

    Basically I'm trying to port some rendering features of our SSAA plugin from HDRP to LWRP, and I'm stuck at the shaders.

    The issue is, everything renders correctly in editor, but if I run a build, screen goes pink (seems like our shaders go invalid/missing in build?) Already tried to add the shaders to the always included shader list in Graphics settings with no luck, there are no compilation errors or warnings within our shaders, and all of them work correctly in editor - its just the builds where the problem appears.

    Tried with latest LWRP on 2019.2 and with URP on 2019.3 - same results

    In editor


    In build


    Anyways this is one of the shaders we have (supposed to only blit a texture over the screen). It's straight up copy-paste from our HDRP variant. Wondering if there is anything that needs to be changed for LWRP? Oddly it works perfectly on LWRP in editor...
    Code (CSharp):
    1. Shader "Hidden/SSAA_Def_HDRP"
    2. {
    3.     Properties
    4.     {
    5.         [HideInInspector] _MainTex("Base (RGB)", 2D) = "white" {}
    6.     }
    7.     HLSLINCLUDE
    8.  
    9.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    10.     #include "UnityShaderVariables.cginc"
    11.     float2 FixVFlip(float2 texcoord)
    12.     {
    13. #if UNITY_UV_STARTS_AT_TOP
    14.         bool flip = _ProjectionParams.x > 0;
    15. #else
    16.         bool flip = _ProjectionParams.x < 0;
    17. #endif
    18.         if (flip) texcoord.y = 1 - texcoord.y;
    19.         return texcoord;
    20.     }
    21.     TEXTURE2D(_MainTex);
    22.     SAMPLER(sampler_MainTex);
    23.     // Vertex shader (procedural fullscreen triangle)
    24.     void Vertex(
    25.         uint vertexID : SV_VertexID,
    26.         out float4 positionCS : SV_POSITION,
    27.         out float2 texcoord : TEXCOORD0
    28.     )
    29.     {
    30.         positionCS = GetFullScreenTriangleVertexPosition(vertexID);
    31.         texcoord = FixVFlip(GetFullScreenTriangleTexCoord(vertexID));
    32.     }
    33.     // Fragment shader
    34.     float4 Fragment(
    35.         float4 positionCS : SV_POSITION,
    36.         float2 texcoord : TEXCOORD0
    37.     ) : SV_Target
    38.     {
    39.         // just output the texture
    40.         return SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, texcoord);
    41.     }
    42.     ENDHLSL
    43.     SubShader
    44.     {
    45.         Cull Off ZWrite Off ZTest Always
    46.         Pass
    47.         {
    48.             HLSLPROGRAM
    49.             #pragma vertex Vertex
    50.             #pragma fragment Fragment
    51.             ENDHLSL
    52.         }
    53.     }
    54. }
    This is the Execute() method of our custom render feature:
    Code (CSharp):
    1.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    2.         {
    3.             var ssaa = renderingData.cameraData.camera.GetComponent<MadGoat.SSAA.MadGoatSSAA>();
    4.  
    5.             if (ssaa == null) return;
    6.             if (!ssaa.enabled) return;
    7.  
    8.             var isVR = ssaa is MadGoat.SSAA.MadGoatSSAA_VR;
    9.  
    10. #if UNITY_EDITOR
    11.             if (!UnityEditor.EditorApplication.isPlaying && isVR) return;
    12. #endif
    13.  
    14.             var material = ssaa.MaterialCurrent;
    15.  
    16.             // set blending - no stacking support on lwrp/urp
    17.             material.SetOverrideTag("RenderType", "Opaque");
    18.             material.SetInt("_SrcBlend", (int)BlendMode.One);
    19.             material.SetInt("_DstBlend", (int)BlendMode.Zero);
    20.             material.SetInt("_ZWrite", 1);
    21.             material.renderQueue = -1;
    22.  
    23.             // update values
    24.             material.SetFloat("_ResizeWidth", ssaa.CameraTargetWidth);
    25.             material.SetFloat("_ResizeHeight", ssaa.CameraTargetHeight);
    26.             material.SetFloat("_Sharpness", ssaa.DownsamplerSharpness);
    27.             material.SetFloat("_SampleDistance", ssaa.DownsamplerDistance);
    28.  
    29.             if (!isVR) {
    30.                 ssaa.OnBeginCameraRender(renderingData.cameraData.camera);
    31.                 material.SetTexture("_MainTex", ssaa.RenderCamera.targetTexture);
    32.  
    33.                 // setup command buffer
    34.                 var cmd = CommandBufferPool.Get("SSAA_HDRP_DOWNSAMPLER");
    35.                 var camera = renderingData.cameraData.camera;
    36.  
    37.                 CoreUtils.ClearRenderTarget(cmd, ClearFlag.All, Color.clear);
    38.                 CoreUtils.SetRenderTarget(cmd, BuiltinRenderTextureType.CurrentActive);
    39.  
    40.                 Blit(cmd, ssaa.RenderCamera.targetTexture, BuiltinRenderTextureType.CurrentActive, material, 0);
    41.  
    42.                 context.ExecuteCommandBuffer(cmd);
    43.                 CommandBufferPool.Release(cmd);
    44.             } else { ... }
    45.         }
    46.     }

    Any help/suggestion would be appreciated. Thanks :)
     
  2. Raul_T

    Raul_T

    Joined:
    Jan 10, 2015
    Posts:
    363
    Just figured out I probably posted this in the wrong forum.

    Is it possible for someone with moderator rights to move the thread on the Universal Render Pipeline forums?

    Thanks :)
     
  3. jakob-leitner

    jakob-leitner

    Joined:
    Nov 11, 2020
    Posts:
    18
    Im stumbling across the same issue. Did you find a solution? :)
     
  4. Raul_T

    Raul_T

    Joined:
    Jan 10, 2015
    Posts:
    363
    Hey, haven't touched that code in a long time so not sure exactly what solved it (our asset got deprecated some years ago, then acquired by another publisher on which I freelanced on again at some point briefly but not on SRP stuff, then deprecated again by them recently) but this is the latest version as per my personal git, prior to initial deprecation, of that bit of code that iirc worked correctly

    Code (CSharp):
    1.        
    2.             // set blending - no stacking support on lwrp/urp
    3.             material.SetOverrideTag("RenderType", "Opaque");
    4.             material.SetInt("_SrcBlend", (int)BlendMode.One);
    5.             material.SetInt("_DstBlend", (int)BlendMode.Zero);
    6.             material.SetInt("_ZWrite", 1);
    7.             material.renderQueue = -1;
    8.  
    9.             // update values
    10.             material.SetFloat("_ResizeWidth", ssaa.CameraTargetWidth);
    11.             material.SetFloat("_ResizeHeight", ssaa.CameraTargetHeight);
    12.             material.SetFloat("_Sharpness", ssaa.DownsamplerSharpness);
    13.             material.SetFloat("_SampleDistance", ssaa.DownsamplerDistance);
    14.  
    15.             if (!isVR) {
    16.                 // setup command buffer
    17.                 var cmd = CommandBufferPool.Get("SSAA_URP_DOWNSAMPLER");
    18.                 CoreUtils.ClearRenderTarget(cmd, ClearFlag.All, Color.clear);
    19.                 CoreUtils.SetRenderTarget(cmd, BuiltinRenderTextureType.CurrentActive);
    20.  
    21.                 Blit(cmd, ssaa.RenderCamera.targetTexture, BuiltinRenderTextureType.CurrentActive, material, 0);
    22.  
    23.                 // execute command buffer
    24.                 context.ExecuteCommandBuffer(cmd);
    25.                 CommandBufferPool.Release(cmd);
    26.             }
    27.  
    And shader updated to use unity's macros for sampling and everything (iirc this was mostly for VR but not sure if it's this or the previous changes in renderpass code that solved it)
    Code (CSharp):
    1. Shader "Hidden/SSAA_Def"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "" {}
    6.     }
    7.     SubShader {
    8.         Tags{ "RenderType" = "Opaque"  "Queue" = "Geometry+0" }
    9.         Blend [_SrcBlend] [_DstBlend]
    10.         Pass {
    11.              ZTest Always Cull Off ZWrite On
    12.  
    13.             CGPROGRAM
    14.             // Includes
    15.             #include "UnityCG.cginc"
    16.             #include "Include/_SSAA_Utils.cginc"
    17.  
    18.             // Shader Construct
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             // Samplers
    23.             UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);
    24.  
    25.             struct Input{
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    29.             };
    30.  
    31.             struct Varying{
    32.                 float4 vertex : SV_POSITION;
    33.                 float2 uv : TEXCOORD0;
    34.                 UNITY_VERTEX_OUTPUT_STEREO
    35.             };
    36.  
    37.             Varying vert(Input input){
    38.                 Varying o;
    39.                 UNITY_SETUP_INSTANCE_ID(input);
    40.                 UNITY_INITIALIZE_OUTPUT(Varying, o);
    41.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    42.  
    43.                 o.vertex = UnityObjectToClipPos(input.vertex);
    44.                 o.uv = input.uv;
    45.                 return o;
    46.             }
    47.  
    48.             float4 frag(Varying i) : SV_Target {
    49.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    50.                 return UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv);
    51.             }
    52.             ENDCG
    53.         }
    54.     }
    55.     Fallback Off
    56. }
    Note that I haven't tested any of this on LWRP/URP versions newer than 2020.1-2020.2
     
    Last edited: Dec 5, 2022