- Home /
is it possible to define multiple vertex/fragment shader in one pass?
hi all,
I am translating a old XNA *.fx shader effect to Unity5. previously I defined multiple vertex and pixel shaders, and compiled them in arrays, and use some parameter to tell the rendering pipeline which vertex/fragment shader combinations to use in one pass:
VertexShader VSArray[3] = { compile vs_2_0 VSSin(), compile vs_2_0 VSSquare(), compile vs_2_0 VSLinear(), };
PixelShader PSArray[2] = { compile ps_2_0 PSNone(), compile ps_2_0 PSGaussian(), };
technique Technique1 { pass Pass1 { VertexShader = (VSArray[VSIndex]); PixelShader = (PSArray[PSIndex]); } }
but in unity5 shaderlab, I don't have a clue how to achieve this. I tried to define more than one vertex/fragment shaders:
Shader "myshader" { Properties {
sigma("Sigma", Float) = 0.05
sf("SF", Float) = 0.2
time("Time", Float)=0
}
SubShader
{
Tags { "RenderType" = "Opaque" "Queue" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma vertex vert1
#pragma fragment frag
# include "UnityCG.cginc"
//.....
ENDCG
}
}
}
and unity5 editor showed "Duplicated #pragma...."
I am wondering is it possible to do something similar to my old .fx effect. or I have to separate each vertex/fragment shader in each shader file, and use some other parameter to combine them in one pass?
thanks,
Defining more than one vertex/fragment shader in shader obviously won't work as there can only be one vertex and only one fragment shader in single shader. A slow-working solution is using if(...)else... statements in shader to choose which function to call, but if statement is generally slowest thing you can do in shader code. So the only thing I can advise you (not pleasant solution either so not posting as answer) is to compile all vertex/fragment shader combinations into separate shaders and change them when you need.
I have thought about the if() solution, since I'll probably do branching in the shader anyway, I'll try to put all staff in the shader for now.
I found some articles saying in most recent harderwares, dynamic branching is achieved in $$anonymous$$I$$anonymous$$D(multi-instruction-multi-data) way, so branching in shader wouldn't be a serious performance problems anymore. I don't know if it's true. I guess whenever performance become degraded, I could switch to static branching, or separate functions in different shaders.
Still, feel disappointed that I could do in XNA which is kind of outdated for several years, but can't do the same thing in the newest unity5!!
Answer by tanoshimi · Jan 28, 2016 at 09:05 PM
Sounds like you're looking for #pragma multi_compile
http://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html