public
Last active

Shader causing driver issues on OSX

  • Download Gist
osx-bad-shader.glslf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#version 150 core
 
uniform sampler2D t_DiffuseDirt, t_SpecBumpReflect, t_Reflection;
uniform vec4 u_SpecularParams;
 
in vec3 v_Eye, v_NormalWorld;
in vec2 v_Tex;
in mat3 TBN;
 
const float c_BumpFactor = 2.0;
const float c_ReflectFactor = 0.1;
const vec3 c_ColorDiffuse = vec3(1.0);
const vec3 c_ColorSpecular = vec3(1.0);
 
vec3 getWorldNormal() {
return normalize( v_NormalWorld );
}
 
vec4 getColor() {
return texture(t_DiffuseDirt,v_Tex);
}
 
struct Context {
vec3 normal,eye;
vec4 albedo;
float specular;
}ct;
 
vec3 computeNormal(float height) {
const ivec3 off = ivec3(-1,0,1);
float hx0 = textureOffset( t_SpecBumpReflect, v_Tex, off.xy ).y;
float hx1 = textureOffset( t_SpecBumpReflect, v_Tex, off.zy ).y;
float hy0 = textureOffset( t_SpecBumpReflect, v_Tex, off.yx ).y;
float hy1 = textureOffset( t_SpecBumpReflect, v_Tex, off.yz ).y;
return vec3( hx0-hx1, hy0-hy1, 2.0/c_BumpFactor );
}
 
vec2 envir_coords(vec3 vOrig) {
const float PI = 3.1415926;
vec3 R = normalize(vOrig);
float u = (atan(R.x/R.z) + PI)/(2.0*PI);
float v = (asin(R.y)+0.5*PI)/PI;
return vec2(u,1.0-v);
}
 
vec4 initSurface() {
vec4 param = texture(t_SpecBumpReflect,v_Tex);
vec3 rawNormal = computeNormal( param.y );
ct.normal = normalize(rawNormal);
ct.eye = normalize(v_Eye);
vec3 refl_tbn = reflect( -ct.eye, ct.normal );
vec2 refl_tc = envir_coords( TBN*refl_tbn );
vec4 refl_color = texture(t_Reflection,refl_tc);
vec3 reflected = c_ReflectFactor*param.z*refl_color.xyz;
ct.albedo = texture(t_DiffuseDirt,v_Tex);
ct.specular = param.x;
return vec4( reflected, 1.0 );
}
 
vec3 computeLight(float ambient, float reflected, vec3 light) {
//return vec3(0.0);
// Blinn-Phong model BRDF, normal mapped
float diff = max( 0.0, dot(ct.normal,light) );
vec3 halfVector = normalize(ct.eye+light);
float spec = max( 0.01, dot(ct.normal,halfVector) );
vec3 diffColor = c_ColorDiffuse * (ambient + reflected*diff) * ct.albedo.rgb;
vec3 specColor = c_ColorSpecular * reflected *
u_SpecularParams.x * pow(spec,u_SpecularParams.y);
return diffColor + specColor;
}
 
 
out vec4 o_Color;
 
void main() {
o_Color = getColor();
}
osx-bad-shader.glslv
GLSL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#version 150 core
 
vec3 modifyInit(vec3 v) {return v};
vec3 modifyVector(vec3 v) {return v};
 
uniform mat4 u_World;
uniform vec4 u_Tex0Transform;
uniform vec4 u_CameraPos, u_WorldQuat;
 
in vec3 a_Position;
in vec4 a_Normal;
in vec4 a_Tangent;
in vec2 a_Tex0;
 
out vec3 v_Eye,v_NormalWorld;
out vec2 v_Tex;
out mat3 TBN;
 
vec3 cookVector(vec3 v) {
return v * TBN;
}
 
vec3 normalize_alt(vec3 v) {
return v*(1.5 - 0.5*dot(v,v));
}
 
vec3 initMaterial() {
vec3 pos = modifyInit( a_Position );
vec3 nor = modifyVector( a_Normal.xyz );
vec3 tan = modifyVector( a_Tangent.xyz );
mat3 m3w = mat3( u_World );
vec3 wp = m3w*pos + u_World[3].xyz;
vec3 normal = normalize_alt( m3w * nor );
vec3 tangent = normalize_alt( m3w * tan );
vec3 bit = cross(normal,tangent) * a_Tangent.w;
vec3 bitangent = normalize_alt( bit );
TBN = mat3(tangent,bitangent,normal);
v_Eye = cookVector( u_CameraPos.xyz - wp );
v_NormalWorld = normal;
vec2 tc = vec2( a_Tex0.x, 1.0-a_Tex0.y );
v_Tex = u_Tex0Transform.xy*tc + u_Tex0Transform.zw;
return wp;
}
 
uniform mat4 u_ViewProj;
uniform vec4 u_LightPos;
 
out vec3 v_Light;
 
void main() {
vec3 wp = initMaterial();
v_Light = cookVector(u_LightPos.xyz - wp * u_LightPos.w);
gl_Position = u_ViewProj * vec4(wp,1.0);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.