Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Using Shader Framebuffer Fetch with LWRP

Discussion in 'Shaders' started by Deleted User, May 8, 2019.

  1. Deleted User

    Deleted User

    Guest

    I'm attempting to use Framebuffer fetch on mobile with LWRP. The code example in the docs (https://docs.unity3d.com/Manual/SL-PlatformDifferences.html) is written in CG and it uses SV_Target for the input semantic. This doesn't seem to work in an HLSLPROGRAM when compiling for OpenGL ES 3.1.

    When I attempt to compile a simple fragment function like this:
    Code (JavaScript):
    1. void frag(Varyings input, inout half4 ocol : SV_Target) {
    2.     ocol /= 2;
    3. }
    I get the compiler error: `invalid ps_5_0 input semantic 'SV_Target'`. Does anyone know the correct inout semantic to use or if there's some other mistake here?
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    1,537
    LWRP doesn't (yet) define the macros for framebuffer fetch.
    You can use "CoLoR" instead of "SV_Target" or indexed versions if you need those (e.g. CoLoR1 instead of SV_Target1).
     
  3. Deleted User

    Deleted User

    Guest

    CoLoR1 works in editor, it now compiles a valid shader but it doesn't seem to run on device. I'll just find another solution until this is supported, thanks for the help.
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    1,537
    If you're not rendering to multiple render targets, it should be CoLoR or CoLoR0, not CoLoR1.
     
  5. Deleted User

    Deleted User

    Guest

    So that still didn't quite work, it seems to generate the wrong GLSL code. If I write the shader directly in GLSL I can make it work:

    before-draw.PNG after-draw.PNG

    GLSL:

    Code (JavaScript):
    1. #version 310 es
    2. #extension GL_EXT_shader_framebuffer_fetch : require
    3.  
    4. precision highp float;
    5. precision highp int;
    6. layout(location = 0) inout lowp vec4 SV_Target0;
    7.  
    8. void main()
    9. {
    10.     float gray = (SV_Target0.x + SV_Target0.y + SV_Target0.z) / 3.0;
    11.     SV_Target0 = vec4(gray, gray, gray, 1);
    12.     return;
    13. }
     
  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    1,537
    I tested it last week, and it worked for me :)

    Are you still testing with the shader you provided in the first snippet?
    Which Unity version are you on?
     
  7. wangtianyu

    wangtianyu

    Joined:
    Jul 29, 2020
    Posts:
    7
    I have a same problem in 2019.3.1f1 and 2020.1.0f1c1.
    Change "SV_Target" to "CoLoR" can resolve the compiler error, but it's not work when i generate to GLSL for GLES3.2 android device.
    by the way CG is work. upload_2020-7-29_17-31-11.png
     
  8. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    1,537
    @wangtianyu What do the logs on the device say?
    What's the device you are trying this on?
     
  9. wangtianyu

    wangtianyu

    Joined:
    Jul 29, 2020
    Posts:
    7
    sorry i miss the log... it's should be like "vs_SV_Target0" is undefined ? i guess
    And my company has the source code, in CompilerHLSL.cpp ParseNeedForFramebufferFetchEnabled , should we add KCoLoRToken to match the "inout <type> <var> : CoLoR" oneliner?
     
  10. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    1,537
    There's no "vs_SV_Target0", SV_Target<X> is a fragment shader thing.

    No, it should work without adding this.
     
  11. wangtianyu

    wangtianyu

    Joined:
    Jul 29, 2020
    Posts:
    7
    Log:
    07-30 16:26:57.052: E/Unity(6904): -------- GLSL link error: Error: input vs_SV_Target0 not declared in output from previous stage.
    07-30 16:26:57.052: E/Unity(6904): Error: Linking failed.


    No, it should work without adding this.[/QUOTE]
    My goal is not use framebuffer in HLSL,I want is right generate to GLSL.

    I provided two demo, I expect them to achieve the same result, may be is wrong?
     

    Attached Files:

  12. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    1,537
    So, which one works and which one doesn't?
    Looks like you have one for built-in render pipeline and one for SRP.
     
  13. wangtianyu

    wangtianyu

    Joined:
    Jul 29, 2020
    Posts:
    7
    yes ,i try to transplant to srp
    SRP has problem.

    HLSLSupport.cginc
    #if defined(UNITY_FRAMEBUFFER_FETCH_AVAILABLE) && defined(UNITY_FRAMEBUFFER_FETCH_ENABLED) && defined(UNITY_COMPILER_HLSLCC)
    #define SV_Target CoLoR

    I found this code, maybe i should define like cg to make generate match SV_Target?
     
  14. wangtianyu

    wangtianyu

    Joined:
    Jul 29, 2020
    Posts:
    7
    anyway is work..but uglily
     

    Attached Files:

  15. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    1,537
    Well, this means you can just change SV_Target to CoLoR in your code and it will work.
     
  16. wangtianyu

    wangtianyu

    Joined:
    Jul 29, 2020
    Posts:
    7
    yes , use CoLoR replace SV_Target and keep the code is SV_Target for generate to glsl...great...
     
  17. wangtianyu

    wangtianyu

    Joined:
    Jul 29, 2020
    Posts:
    7
    hi.me again.
    about the EXT_shader_framebuffer_fetch, i found the OpenGL document about "How is framebuffer data treated during multisample rendering?" ,the answer was
    RESOLVED: Reading the value of gl_LastFragData produces a different
    result for each sample. This implies that all or part of the shader be
    run once for each sample, but has no additional implications on fragment
    shader input variables which may still be interpolated per pixel by the
    implementation.
    what the "This implies that all or part of the shader be run once for each sample" mean.If I use the msaa x4 and framebuffer fetch ,the pixel shader will be run 4 times for each pixel?

    https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_shader_framebuffer_fetch.txt
     
    liam_unity628 likes this.
unityunity