- Home /
Parse error: Syntax Error in compiled shader, Unity 4.3.1 [free]
Greeting everyone.
I am trying to follow the Cg Tutorial from nvidia's developer site to get familiar with shaders, and I am also consulting the porting tips from the wiki here: [http://wiki.unity3d.com/index.php/Cg_Tutorial_to_Unity][1]
Now I am trying to re-create the shader from example "C3E5_DoubleVision". At first I tried following only the cg tutorial from nvidia's site and correcting my errors alone. When it was all ready, with no compile errors i got this one:
"Shader error in 'Custom/doubleTexture': Parse error: syntax error at line 1049"
This Error appears in the compiled shader file and the line reads:
SetTexture 0 [$_mainTex] 2D 0
I could not find what's wrong so I tried making a new shader file named test and copying the example as it was from the wiki. Same Error happens (although the compiled shader has less lines because that code doesn't #include "UnityCG.cginc" like mine does, so the error occures in line 799 of the compiled shader).
Also, if I select any of the shader file, in the inspector this message appears:
Shader has Errors or is not supported by your graphics cards.
Any Ideas why that is? My graphics card is quite new so I doubt it doesn't support some shader model... Could it have something to do with vertex/fragment profiles mentioned in the cg tutorial? and If so what can i do in unity to set a profile? Or could it be sometihng else entirely?
Thank you very much in advanced.
[EDIT:] I also tried to implement the fix for older GPUs, using two texture units instead of one (in case my gpu did not indeed support the shader), from Cg tutorial as seen in:
([http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter03.html][2]) but I get the same problem. [EDIT2] I tried to implement other shaders too, and it seems the problem is produced every time by a call to: float4 tex2D(sampler2D samp, float2 s) and in the compiled shader this errors in the "d3d11" subprogram block. (i.e. I tried adding #pragma only_renderers d3d9 and it compiled but the shader did not behave as it should). [1]: http://wiki.unity3d.com/index.php/Cg_Tutorial_to_Unity [2]: http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter03.htmlExample 3-7. The C3E7f_twoTextures Fragment Program
"Shader has Errors or is not supported by your graphics cards." is the generic error that just means you've got an error in your shader. Please post the source code you're trying to compile and we might be able to spot the problem.
It is the code exactly as it appears in the wiki here: Shader C3E6 : two textures double vision
I have tried writing other shaders too, trying not to get stuck with it, and it seems that everytime i try to use
tex2D(decal, texCoords);
It produces an error. Which is weird because earlier I had tried a shader (This one) and it had worked. Now it doesn't. And I can't figure out what changed.
Answer by tanoshimi · Jan 16, 2014 at 09:05 AM
The code on that wiki page looks incomplete/wrong. Try this instead:
Shader "Custom/C3E5_DoubleVision"
{
Properties
{
_MainTex ("Diffuse", 2D) = "white" {}
_leftSeparation ("Left Separation", Vector) = (-0.5,0,0,0)
_rightSeparation ("Right Separation", Vector) = (0.5,0,0,0)
_lerpValue ("Linear Interpolation Value", Float) = 0.5
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform float2 _leftSeparation;
uniform float2 _rightSeparation;
uniform sampler2D _MainTex;
uniform float _lerpValue;
struct a2v {
float2 pos : POSITION;
float2 texCoord : TEXCOORD0;
};
struct v2f {
float4 oPos : SV_POSITION;
float2 leftTexCoord : TEXCOORD0;
float2 rightTexCoord : TEXCOORD1;
};
v2f vert(a2v IN)
{
v2f o;
o.oPos = float4(IN.pos, 0, 1);
o.leftTexCoord = IN.texCoord + _leftSeparation;
o.rightTexCoord = IN.texCoord + _rightSeparation;
return o;
}
float4 frag(v2f IN) : COLOR
{
float4 leftColor = tex2D(_MainTex, IN.leftTexCoord);
float4 rightColor = tex2D(_MainTex, IN.rightTexCoord);
return lerp(leftColor, rightColor, _lerpValue);
}
ENDCG
}
}
}
This works Fine. Thank you very much.
But could you care to explain to me what the problem was? It seems to me the only difference is that you are using "struct" for input-output values ins$$anonymous$$d of "out" parameters in the functions. Does cause that problem somehow? Because anycode I have written with "out" but not using tex2D was ok...
Hmmmm compiling both you code and the code form the wiki, i get this difference in the compiled shader:
(old wiki code) SubProgram "d3d11 " { $$anonymous$$eywords { } ConstBuffer "$Params" 16 // 4 used size, 1 vars Float 0 [_lerpValue] BindCB "$Params" 0 SetTexture 0 [$_$$anonymous$$ainTex] 2D 0 ...blah blah..}
(your code) SubProgram "d3d11 " { $$anonymous$$eywords { } ConstBuffer "$Globals" 32 // 20 used size, 3 vars Float 16 [_lerpValue] BindCB "$Globals" 0 SetTexture 0 [_$$anonymous$$ainTex] 2D 0 ...blah blah..}
$$anonymous$$aybe the error was because of [_lerpValue] being declared as Float 0 in the old code or the Constbuffer... but why could this be?
Your answer
Follow this Question
Related Questions
Shader compiler: internal error compiling shader snippet type=0 platform=0: 1 Answer
CG shader is treated as GLSL and doesn't compile 0 Answers
How to render a Cube using Shaders? 0 Answers
cross product in cg shader - error "can't find __getVectorIndex" 1 Answer
How to force the compilation of a shader in Unity? 5 Answers