[英]Why won't fragment shader compile?
我已在許多Windows 7和8 PC上使用以下片段着色器,而沒有出現問題。
#ifndef STRINGIFY
#define STRINGIFY(a) #a
#endif
char *fsFont = STRINGIFY(
precision mediump float;
varying vec2 v_texCoord;
vec2 v_texCoord;
uniform sampler2D s_texture;
uniform vec4 vColor4;
varying lowp vec4 DestinationColor;
void main()
{
gl_FragColor = vec4( 1, 1, 1, texture2D( s_texture, v_texCoord).a ) * vColor4;
}
);
最近,我嘗試在Windows 10機器上運行我的應用程序,嘗試編譯着色器時出現以下錯誤:
0x0059f3dc "0(1) : warning C7022: unrecognized profile specifier "mediump"
0(1) : warning C7022: unrecognized profile specifier "precision"
0(1) : error C1038: declaration of "v_texCoord" conflicts with previous declaration at 0(1)
0(1) : error C0000: syntax error, u"
如果我注釋掉precision mediump float
線。
0x0137f2c8 "0(1) : error C0000: syntax error, unexpected identifier, expecting "::" at token "lowp"
"
有什么問題? 是因為我使用的PC支持與我期望的版本不同的OpenGL嗎?
更新 : 這篇文章建議精確的中浮浮線僅適用於OpenGL ES。 這仍然不能解釋為什么它可以在我的大多數PC上運行。 繼續, lowp
聲明怎么了?
這是我的SDL初始化代碼,用於選擇OpenGL配置文件/版本:
#ifndef _WIN32
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 0 );
#else
// Use OpenGL 3.1 core
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
#endif
更新:這是更新的着色器,現在由於每個人的評論而可以使用:
char *fsFont =
"#version 330\n"
"precision mediump float;\n"
"varying vec2 v_texCoord;\n"
"uniform sampler2D s_texture;"
"uniform vec4 vColor4;"
"varying lowp vec4 DestinationColor;"
"void main()"
"{"
"gl_FragColor = vec4( 1, 1, 1, texture2D( s_texture, v_texCoord).a ) * vColor4;"
"}"
;
更新 :我從所有着色器中刪除了STRINGIFY,移至OpenGL 3.3。 我所有的着色器都進行編譯,但有一個例外:
char *vs3DShader =
"#version 330\n"
"layout (location = 0) in vec3 Position;"
"layout (location = 1) in vec4 color;"
"layout (location = 2) in vec3 normal;"
"out vec4 frag_color;"
"uniform mat4 transform;"
"void main()"
"{"
" gl_Position = transform * vec4( Position.x, Position.y, Position.z, 1.0 );"
" // Pass vertex color to fragment shader.."
" frag_color = color;"
"}"
;
錯誤:
0x0055f370 "(0) : error C0000: syntax error, unexpected $end at token "<EOF>"
"
着色器無法編譯,因為:
#version
行(版本未定義,並且在其他計算機上可能是默認值) STRINGIFY()
宏不適用於#
字符,因此最好將着色器作為文件中的文本加載,或在每行周圍用雙引號指定它,記住在以' #
'或注釋開頭的行之后使用\\n
最好每條線都安全 STRINGIFY()
, 必須在任何注釋后添加換行符,否則注釋后附加的所有內容都將被注釋掉 mediump
關鍵字和varying
關鍵字生成警告/錯誤時,引入了重復變量 #version
(即OpenGL3.1必須使用#version 140
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.