- Home /
Branching with SHADER_API_D3D11 directive compiles wrong in surface shader
I was creating an animation effect using surface shader and SHADER_API_D3D11 directive. I noticed that using this directive breaks the correct work. After removing this directive shader works as I expect.
After several tests, I found that the problem is in how the branching compiles, and the problem is only with mentioned directive. Yes, I understand that if-s can slow the work, but that doesn`t mean incorrect work.
It seems to me that there is something strange in optimization. Compiler takes the same uv coordinate for all cases, but in the right way color values are always different. So, my question is: why does it happening and what should I do?
Here`s shader I use. You can see smooth animation. If you uncomment #if, there will be just a color change in a second.
Shader "Custom/NewSurfaceShader"
{
Properties
{
_MainTex("Albedo (RGB)", 2D) = "white" {}
}
SubShader
{
CGPROGRAM
#pragma enable_d3d11_debug_symbols
#pragma surface surf Standard fullforwardshadows
#pragma target 5.0
struct Input
{
float2 uv_MainTex;
};
sampler2D _MainTex;
void surf(Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = fixed4(sin(IN.uv_MainTex.x), cos(IN.uv_MainTex.y), sin(IN.uv_MainTex.x*3.14), 0);
//#if defined(SHADER_API_D3D11)
if (c.r > _Time.y / 2)
o.Albedo = c;
else
o.Albedo = fixed3(.2f, .2f, .9f);
//#endif
}
ENDCG
}
}
Answer by Danilan · Nov 10, 2018 at 09:12 PM
By the way, I finally solved it by adding an '#else' branch
Your answer
Follow this Question
Related Questions
Unity Terrain - shader for custom post-splat? 0 Answers
Transparent Blend by Shader Variant in Surface Shader 0 Answers
CG diffuse shader 1 Answer
Blood dripping on character (HLSL shader?) 0 Answers
Problem with custom shaders (surface function error) 0 Answers