Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tessellation Shader has stopped working

Discussion in 'Shaders' started by Radeg90, Apr 13, 2018.

  1. Radeg90

    Radeg90

    Joined:
    Feb 28, 2017
    Posts:
    38
    Hi
    In my game I was trying to add a tessellation there. When I opened an example project it was working perfectly. When I added tessellation shader to my game, whole material got pink and even after pasting object from example to my game it was pink as well. After adding another shader, tessellation suddenly seemed to work for a while, but after some changes in shader files everything went pink again.

    Here is the console output:

    What can I do to make it working? In the player settings in Unity I'm using DirectX11 and 12, there is no difference on both. Other shaders are working.

    The shader from below is added to the graphic settings to always included shaders.

    Normal shader:


    Tessellation:
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    post the code or itll be hard to help
     
  3. Radeg90

    Radeg90

    Joined:
    Feb 28, 2017
    Posts:
    38
    This comes from Standard Assets, so there is no need to show code because it should work and everybody can see it from Unity, but I will if you wish.

    Code (CSharp):
    1. Shader "Tessellation/Bumped Specular (displacement)" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    5.     _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    6.     _Parallax ("Height", Range (0.0, 1.0)) = 0.5
    7.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    8.     _BumpMap ("Normalmap", 2D) = "bump" {}
    9.     _ParallaxMap ("Heightmap (A)", 2D) = "black" {}
    10.  
    11.     _EdgeLength ("Edge length", Range(3,50)) = 10
    12. }
    13. SubShader {
    14.     Tags { "RenderType"="Opaque" }
    15.     LOD 800
    16.  
    17. CGPROGRAM
    18. #pragma surface surf BlinnPhong addshadow vertex:disp tessellate:tessEdge
    19. #include "Tessellation.cginc"
    20.  
    21. struct appdata {
    22.     float4 vertex : POSITION;
    23.     float4 tangent : TANGENT;
    24.     float3 normal : NORMAL;
    25.     float2 texcoord : TEXCOORD0;
    26.     float2 texcoord1 : TEXCOORD1;
    27.     float2 texcoord2 : TEXCOORD2;
    28. };
    29.  
    30. float _EdgeLength;
    31. float _Parallax;
    32.  
    33. float4 tessEdge (appdata v0, appdata v1, appdata v2)
    34. {
    35.     return UnityEdgeLengthBasedTessCull (v0.vertex, v1.vertex, v2.vertex, _EdgeLength, _Parallax * 1.5f);
    36. }
    37.  
    38. sampler2D _ParallaxMap;
    39.  
    40. void disp (inout appdata v)
    41. {
    42.     float d = tex2Dlod(_ParallaxMap, float4(v.texcoord.xy,0,0)).a * _Parallax;
    43.     v.vertex.xyz += v.normal * d;
    44. }
    45.  
    46. sampler2D _MainTex;
    47. sampler2D _BumpMap;
    48. fixed4 _Color;
    49. half _Shininess;
    50.  
    51. struct Input {
    52.     float2 uv_MainTex;
    53.     float2 uv_BumpMap;
    54. };
    55.  
    56. void surf (Input IN, inout SurfaceOutput o) {
    57.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    58.     o.Albedo = tex.rgb * _Color.rgb;
    59.     o.Gloss = tex.a;
    60.     o.Alpha = tex.a * _Color.a;
    61.     o.Specular = _Shininess;
    62.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    63. }
    64. ENDCG
    65. }
    66.  
    67. FallBack "Bumped Specular"
    68. }
    69.  
     
  4. Radeg90

    Radeg90

    Joined:
    Feb 28, 2017
    Posts:
    38
    Bump... ( ͡° ʖ̯ ͡°)

    Does anybody know what does that error mean when setting material to tessellation?

    It happens with any tessellation shader from store or standard assets. And why other projects seem to be working but on mine tessellation just suddenly stopped working?
     
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    Im not sure what this is, but it seems to have problems with stuff related to GPU instancing. Also seeing as the error is on line 167 which is beyond what you have posted, I am guessing the error is in another script? Post that file and itll be easier to track it down.

    As far as I can see from this code, you are not actually setting any properties as instanced, so the instancing must be in the tesselation cginc you have.
     
  6. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,697
    Tessellation and gpu instancing is not supported together I believe
     
  7. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Why?
     
    antoripa likes this.
  8. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,697
    Not sure, this is what I was told and it seems to be true, turning on gpu instancing breaks tessellation. Is this a bug ?


    I cant find the article, maybe it was specific to distance based tessellation
     
    Last edited: Oct 29, 2018
  9. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Well, I would expect those two things to work together :)
     
  10. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,697
    I will try dig up where I found that info, could have been on the Unity blog
     
  11. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    46
    And how did you fixed it?
     
  12. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    Feel free to see the bug I logged about this in 2018.3’s terrain instancing. When the displacement function is called, the instance id has not been set yet, so the compiler complains about an uninitialized structure.
    Bug 1111579
     
  13. florianalexandru05

    florianalexandru05

    Joined:
    Mar 31, 2014
    Posts:
    1,794
    This tread looks old but I'm having the same issue. I'm making a tessellation shader in Amplify and when I turn on Instancing it breaks.
     
  14. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    This is broken and Unity has resolved the bug as won't fix.. So it's instancing or tessellation, or write a vertex/fragment shader instead.
     
    TrazerX and florianalexandru05 like this.
  15. Foraplaying

    Foraplaying

    Joined:
    Jun 20, 2020
    Posts:
    1
    I got tesselation and GPU instancing working together. However, the shader is written for URP:

    Code (CSharp):
    1. Shader "Custom/InstancedTesselation"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1, 1, 1, 1)
    6.         _Tesselation("Tesselation", Range(0, 32)) = 1
    7.     }
    8.         SubShader
    9.     {
    10.         Pass
    11.         {
    12.             //Tags {"LightMode" = "ForwardBase"}
    13.             HLSLPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             #pragma hull hull
    17.             #pragma domain domain
    18.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    19.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    20.             #pragma multi_compile _ _SHADOWS_SOFT
    21.             #pragma multi_compile_instancing
    22.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    23.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    24.  
    25.             float random(float2 st) {
    26.             return frac(sin(dot(st.xy, float2(12.9898,78.233))) * 43758.5453123);
    27.             }
    28.  
    29.             struct appdata
    30.             {
    31.                 float4 vertex : POSITION;
    32.                 float3 normal : NORMAL;
    33.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    34.             };
    35.  
    36.             struct t2f
    37.             {
    38.                 float4 pos : SV_POSITION;
    39.                 float3 posWS : TEXCOORD0;
    40.                 float3 normal : NORMAL;
    41.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    42.             };
    43.  
    44.             struct v2d
    45.             {
    46.                 float4 vertex : SV_POSITION;
    47.                 float3 normal : NORMAL;
    48.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    49.             };
    50.  
    51.             struct TessellationFactors
    52.             {
    53.                 float edge[3] : SV_TessFactor;
    54.                 float inside : SV_InsideTessFactor;
    55.             };
    56.  
    57.             v2d vert(appdata v)
    58.             {
    59.                 v2d output;
    60.                 UNITY_SETUP_INSTANCE_ID(v);
    61.                 UNITY_TRANSFER_INSTANCE_ID(v, output);
    62.                 output.vertex = v.vertex;
    63.                 output.normal = v.normal;
    64.                 return output;
    65.             }
    66.  
    67.             t2f tessVert(appdata v)
    68.             {
    69.                 t2f output;
    70.                 UNITY_SETUP_INSTANCE_ID(v); //Insert
    71.                 UNITY_TRANSFER_INSTANCE_ID(v, output);
    72.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz + random(v.vertex.xz));
    73.                 output.pos = vertexInput.positionCS;
    74.                 output.posWS = vertexInput.positionWS;
    75.                 output.normal = v.normal;
    76.                 return output;
    77.             }
    78.  
    79.             float _Tesselation;
    80.  
    81.             TessellationFactors patchConstantFunction(InputPatch<v2d, 3> patch)
    82.             {
    83.                 UNITY_SETUP_INSTANCE_ID(patch[0]);
    84.                 VertexPositionInputs vertexInput1 = GetVertexPositionInputs(patch[0].vertex.xyz);
    85.                 VertexPositionInputs vertexInput2 = GetVertexPositionInputs(patch[1].vertex.xyz);
    86.                 VertexPositionInputs vertexInput3 = GetVertexPositionInputs(patch[2].vertex.xyz);
    87.                 TessellationFactors f;
    88.                 f.edge[0] = _Tesselation;
    89.                 f.edge[1] = _Tesselation;
    90.                 f.edge[2] = _Tesselation;
    91.                 f.inside = _Tesselation;
    92.                 return f;
    93.             }
    94.  
    95.             [patchconstantfunc("patchConstantFunction")]
    96.             [domain("tri")]
    97.             [partitioning("integer")]
    98.             [outputtopology("triangle_cw")]
    99.             [outputcontrolpoints(3)]
    100.             v2d hull(InputPatch<v2d, 3> patch, uint id : SV_OutputControlPointID)
    101.             {
    102.                 return patch[id];
    103.             }
    104.  
    105.             [domain("tri")]
    106.             t2f domain(TessellationFactors factors, OutputPatch<v2d, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
    107.             {
    108.                 v2d v;
    109.                 #define MY_DOMAIN_PROGRAM_INTERPOLATE(fieldName) v.fieldName = patch[0].fieldName * barycentricCoordinates.x + patch[1].fieldName * barycentricCoordinates.y + patch[2].fieldName * barycentricCoordinates.z;
    110.                 MY_DOMAIN_PROGRAM_INTERPOLATE(vertex)
    111.                 MY_DOMAIN_PROGRAM_INTERPOLATE(normal)
    112.                 UNITY_TRANSFER_INSTANCE_ID(patch[0], v);
    113.                 return tessVert(v);
    114.             }
    115.  
    116.  
    117.             float4 _Color;
    118.  
    119.             float4 frag(t2f i) : SV_Target
    120.             {
    121.                 UNITY_SETUP_INSTANCE_ID(i);
    122.  
    123.                 half3 viewDirectionWS = SafeNormalize(GetCameraPositionWS() - i.posWS.xyz);
    124.  
    125.                 BRDFData brdfData;
    126.                 float alpha = 1;
    127.                 InitializeBRDFData(_Color.xyz, 0, 0, 0, alpha, brdfData);
    128.  
    129.  
    130.                 //lighting
    131.                 half3 bakedGI = SampleSH(i.normal);
    132.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(i.posWS.xyz);
    133.                 Light mainLight = GetMainLight(TransformWorldToShadowCoord(i.posWS.xyz));
    134.                 half3 col = GlobalIllumination(brdfData, bakedGI, 1, i.normal, viewDirectionWS);
    135.                 col += LightingPhysicallyBased(brdfData, mainLight, i.normal, viewDirectionWS);
    136.  
    137.                 return float4(col, 1);
    138.             }
    139.         ENDHLSL
    140.     }
    141.     UsePass "Universal Render Pipeline/Lit/ShadowCaster"
    142.     UsePass "Universal Render Pipeline/Lit/DepthOnly"
    143.     }
    144.         Fallback "Diffuse"
    145. }
    francois85 probably got the information that instancing and tesselation is incompatible from here:
    https://catlikecoding.com/unity/tutorials/advanced-rendering/tessellation/
     
    PDE26jjk and ryanslikesocool like this.