Friday, 15 May 2015

c++ - GLSL shader not working on AMD/ATI, but works on NVIDIA -



c++ - GLSL shader not working on AMD/ATI, but works on NVIDIA -

i have weird problem cannot pin down days now. making simple per-vertex lighting , works fine on nvidia, doesn't render shaded lights on amd/ati. tracked downwards problem beingness attributes - color attribute. vertex shader:

#version 140 uniform mat4 modelviewprojectionmatrix; in vec3 in_position; // (x,y,z) in vec4 in_color; // (r,g,b,a) in vec2 in_texturecoord; // (u,v) out vec2 v_texturecoord; out vec4 v_color; uniform bool en_colorenabled; uniform bool en_lightingenabled; void main() { if (en_lightingenabled == true){ v_color = vec4(0.0,1.0,0.0,1.0); }else{ if (en_colorenabled == true){ v_color = in_color; }else{ v_color = vec4(0.0,0.0,1.0,1.0); } } gl_position = modelviewprojectionmatrix * vec4( in_position.xyz, 1.0); v_texturecoord = in_texturecoord; }

this pixel shader:

#version 140 uniform sampler2d en_texsampler; uniform bool en_texturingenabled; uniform bool en_colorenabled; uniform vec4 en_bound_color; in vec2 v_texturecoord; in vec4 v_color; out vec4 out_fragcolor; void main() { vec4 texcolor; if (en_texturingenabled == true){ texcolor = texture2d( en_texsampler, v_texturecoord.st ) * v_color; }else if (en_colorenabled == true){ texcolor = v_color; }else{ texcolor = en_bound_color; } out_fragcolor = texcolor; }

so shader allows 3 states - when utilize lights, when don't utilize lights, utilize per-vertex color , when utilize single color vertices. removed lite calculation code, because tracked downwards problem attribute. output shader: can see, bluish (so renders doesn't have shading or color (en_lightingenabled == false , en_colorenabled == false)).

when replace code in vertex shader:

if (en_colorenabled == true){ v_color = in_color; }else{

with this:

if (en_colorenabled == true){ v_color = vec4(1.0,0.0,0.0,1.0); }else{

i following: can see renders without shading still blue, renders uses lights (en_lightingenabled == true) in green. how should be. problem in_color attribute should not ever interfere lights, wouldn't ever run if lights enabled. can see nil in image red, means in scene en_colorenabled false. in_color totally useless here, yet causes bugs in places logically shouldn't. on nvidia works fine , greenish areas draw or without in_color.

am using amd doesn't support? using outside glsl spec? know amd stricter gl spec nvidia, it's possible undefined. made code simple possible , still have problem, don't see it. also, noticed won't draw (even bluish areas) on screen if in_color vertex attribute disabled (no glenablevertexattribarray), why? don't utilize color. disabled attributes need homecoming (0,0,0,1) spec right? tried using glvertexattrib4f alter color, still got black. compiler , linker doesn't homecoming errors or warnings. attribute in_color found fine glgetattriblocation. said, nil of should matter, branch never executed in code. ideas?

tested on nvidia660ti , works fine, tested on ati mobility radeon hd 5000 laptop , have error. thought maybe exceed capabilities of gpu (too many attributes , on), when reduced code stopped believing that. utilize 3 attributes here. also, of friends tested on much newer amd cards , got same problem.

update 1

vertex attributes bound so:

if (usecolors){ gluniform1i(uni_colorenable,1); glenablevertexattribarray(att_color); glvertexattribpointer(att_color, 4, gl_unsigned_byte, gl_true, stride, offset(offset)); }else{ gluniform1i(uni_colorenable,0); }

also, found how show weirdness easier - take vertex shader:

void main() { v_color = clamp(vec4(1.,1.,1.,1.) * vec4(0.5,0.5,0.25,1.),0.0,1.0) + in_color; gl_position = modelviewprojectionmatrix * vec4( in_position.xyz, 1.0); v_texturecoord = in_texturecoord; }

so no branching. , this: if alter code removing in_color so:

void main() { v_color = clamp(vec4(1.,1.,1.,1.) * vec4(0.5,0.5,0.25,1.),0.0,1.0); gl_position = modelviewprojectionmatrix * vec4( in_position.xyz, 1.0); v_texturecoord = in_texturecoord; }

i this:

this lastly 1 expect, previous 1 doesn't draw though add-on (+) instead of else. if attrib not bound , (0,0,0,0) or (0,0,0,1) given, shouldn't have done anything. , yet still fails.

also, logs empty - compilation, linking , validation. no errors or warnings. though noticed shader debug info amd/ati gives vastly inferior nvidia gives.

update 2

the prepare found enable attribarray in cases (so sends junk gpu when color not enabled), utilize uniform check color, don't utilize it. it's this:

if (usecolors){ gluniform1i(uni_colorenable,1); }else{ gluniform1i(uni_colorenable,0); } glenablevertexattribarray(att_color); glvertexattribpointer(att_color, 4, gl_unsigned_byte, gl_true, stride, offset(offset));

i have no thought on why happening or why need utilize this. don't need on nvidia. guess slower , wasting bandwidth? @ to the lowest degree works... help on how improve useful though.

this happening because specifying 3 input values shader (in_position, in_color , in_texturecoord) vertex info not contain 3. when vertex attributes not match format exactly, you're getting undefined behavior territory , each implementation can wants.

it appears though nvidia driver binds have appropriate vertex attributes , renders that.

that is, info looks arrangement below shader:

in_position = position.xyz in_color = ??? in_texturecoords = texturecoords.xy

the amd driver trying interpret vertex info 3 attributes though color isn't included. after first vertex, position out of sync position in info array cause rendered pretty much anywhere. suspect reason works on amd when remove usage of in_color attribute because shader compiler sees not used , optimizes away fact specified it.

in_position = position[0].xyz in_color = texturecoords[0].xy,position[1].xy in_texturecoords = position[1].z,texturecoords[1].x

possible solutions have done , specify garbage color info when don't need or can split vertex shader 2 separate ones different vertex attributes , bind appropriate 1 depending on whether or not have vertex color data.

c++ opengl glsl ati

No comments:

Post a Comment