sgsrules

06-20-2010, 12:48 PM

I've been using glsl for a few months now and i know a few things as far ass optimizations like MADD instructions are fast sampling is slow avoid division. I'd love to get some input on the subject, if anyone could give me any pointers or refer me somewhere else that would be brilliant.

I made this geometry instance vertex shader that works great (so much better than go through a loop). Is there anything i should change or optimize? I'm guessing the texel fetches are the slowest part.

#version 150 core

in vec3 vertex;

in vec3 normal;

in vec2 texCoord;

in vec3 tangent;

in vec3 binormal;

uniform samplerBuffer tboSampler; // contains model matrix for each instance

uniform samplerBuffer colorTboSampler; //contains separate colors for each instance

uniform mat4 viewMatrix;

uniform mat4 projectionMatrix;

uniform float far; // 1/farclipplane

out vec4 eyeSpaceVert;

out vec3 eyeSpaceTangent;

out vec3 eyeSpaceBinormal;

out vec3 eyeSpaceNormal;

out vec2 tuv; //texcoord

out vec4 instanceColor; //separate color per model

out float depth; //linear depth

mat4 getModelMatrix()

{

mat4 tbo;

for (int i =0;i<4;i++)

{

tbo[i] = texelFetch(tboSampler,(gl_InstanceID )* 4 + i);

}

return tbo;

}

mat3 getNormalMatrix(mat4 mvMatrix)

{

return mat3(transpose(inverse(mvMatrix)));

}

void main(void)

{

tuv = texCoord;

mat4 mvMatrix = viewMatrix * getModelMatrix();

mat3 normalMatrix = getNormalMatrix(mvmatrix);

eyeSpaceVert = mvMatrix * vec4(vertex,1.0);

eyeSpaceTangent = normalMatrix * tangent;

eyeSpaceBinormal = normalMatrix * binormal;

eyeSpaceNormal = normalMatrix * normal;

instanceColor = texelFetch(colorTboSampler,gl_InstanceID);

depth = -eyeSpaceVert.z * far;

gl_Position = projectionMatrix * eyeSpaceVert;

}

I made this geometry instance vertex shader that works great (so much better than go through a loop). Is there anything i should change or optimize? I'm guessing the texel fetches are the slowest part.

#version 150 core

in vec3 vertex;

in vec3 normal;

in vec2 texCoord;

in vec3 tangent;

in vec3 binormal;

uniform samplerBuffer tboSampler; // contains model matrix for each instance

uniform samplerBuffer colorTboSampler; //contains separate colors for each instance

uniform mat4 viewMatrix;

uniform mat4 projectionMatrix;

uniform float far; // 1/farclipplane

out vec4 eyeSpaceVert;

out vec3 eyeSpaceTangent;

out vec3 eyeSpaceBinormal;

out vec3 eyeSpaceNormal;

out vec2 tuv; //texcoord

out vec4 instanceColor; //separate color per model

out float depth; //linear depth

mat4 getModelMatrix()

{

mat4 tbo;

for (int i =0;i<4;i++)

{

tbo[i] = texelFetch(tboSampler,(gl_InstanceID )* 4 + i);

}

return tbo;

}

mat3 getNormalMatrix(mat4 mvMatrix)

{

return mat3(transpose(inverse(mvMatrix)));

}

void main(void)

{

tuv = texCoord;

mat4 mvMatrix = viewMatrix * getModelMatrix();

mat3 normalMatrix = getNormalMatrix(mvmatrix);

eyeSpaceVert = mvMatrix * vec4(vertex,1.0);

eyeSpaceTangent = normalMatrix * tangent;

eyeSpaceBinormal = normalMatrix * binormal;

eyeSpaceNormal = normalMatrix * normal;

instanceColor = texelFetch(colorTboSampler,gl_InstanceID);

depth = -eyeSpaceVert.z * far;

gl_Position = projectionMatrix * eyeSpaceVert;

}