Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

StructuredBuffer with Surface shader

Discussion in 'Shaders' started by Arkhivrag, Mar 6, 2013.

  1. Arkhivrag

    Arkhivrag

    Joined:
    Apr 25, 2012
    Posts:
    1,871
    Hi,
    Is there a way to use StructuredBuffer inside Surface shader?
     
    Last edited: Mar 10, 2013
  2. Arkhivrag

    Arkhivrag

    Joined:
    Apr 25, 2012
    Posts:
    1,871
    Unfortunately I could not find a way to use StructuredBuffer with Surface shader.
    I want to finish and release my "Defragment " shader, that I used for - DirectX 11 contest. I added a lot of new features and now its time to add forward/defferred lighting and shadowing. And as shader uses StructuredBuffers I will have to rewrite all rendering passes for that. I think it is just a wasting of time. Hope next Unity version will support - StructuredBuffer with Surface shader.

    $fragmentum.png
     
    Last edited: Mar 12, 2013
  3. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,526
    I think it should work. Here's a snipped that will be in the documentation really soon:

    "DirectX 11 HLSL syntax and Surface Shaders

    Currently some parts of surface shader compilation pipeline do not understand DX11-specific HLSL syntax. If you're HLSL features like StructuredBuffers, RWTextures and other non-DX9 syntax, you have to wrap it into a DX11-only preprocessor macro:

    #ifdef SHADER_API_D3D11
    // DX11-specific code, e.g.
    StructuredBuffer<float4> myColors;
    RWTexture2D<float4> myRandomWriteTexture;
    #endif
    "
     
    osblow and richardkettlewell like this.
  4. Arkhivrag

    Arkhivrag

    Joined:
    Apr 25, 2012
    Posts:
    1,871
    @Aras
    thanks
     
  5. AgentFire

    AgentFire

    Joined:
    Jul 19, 2014
    Posts:
    4
    @Aras well, yeah, it now compiles, though it compiles as if there is no definition of a sctructured-array variable at all. It simply throws "variable is not defined" exception. So how can one use StructuredBuffer inside a surface shader?
     
  6. AgentFire

    AgentFire

    Joined:
    Jul 19, 2014
    Posts:
    4
    Okay, for the future visitors: you will gonna have to wrap in [#ifdef SHADER_API_D3D11 ... #endif] not only the definition of a structured buffer, but all the referenced to it as well in a cascade manner.

    And yet it won't be working as expected. I'm still trying to figure out HOW is it working, it is, somehow, yet the workflow is so annoying in its inconsistency.
     
    Last edited: Apr 30, 2016
  7. v3_matt

    v3_matt

    Joined:
    Aug 10, 2015
    Posts:
    8
    Bump. @Aras, This "works," but it is incredibly difficult to use the data in conjunction with other input. Inside the #ifdef, all of my Input values are junk. For instance (for example only), I am trying to access vertex color:

    Code (CSharp):
    1.  
    2. struct Input
    3. {
    4.     float4 vColor : COLOR;
    5. };
    6.  
    7. // ...
    8.  
    9. void surf (Input IN, inout SurfaceOutputStandard o)
    10. {
    11.     #ifdef SHADER_API_D3D11
    12.     o.Albedo = IN.vColor.rgb;
    13.     #endif
    14. }
    The result paints the whole mesh red, but I have varied vertex color throughout my model. If I place that statement outside the #ifdef, it works fine. I know for sure my code is being reached, because if I write o.Albedo = fixed4(0, 0, 1) inside the #ifdef, the result paints the whole mesh blue.

    Why can't #pragma target 5.0 solve this issue? Why are we getting the compiler error in the first place? And how does it make any sense that the Inputs change inside the #ifdef?