Skip to content

Instantly share code, notes, and snippets.

@FelixK15
Created August 15, 2016 20:31
Show Gist options
  • Save FelixK15/5402f10d802546465ef165c0c14edb25 to your computer and use it in GitHub Desktop.
Save FelixK15/5402f10d802546465ef165c0c14edb25 to your computer and use it in GitHub Desktop.
Shader processing (non API specific)
Shader file (e.g. vertex shader):
----
float4 TransformVertex(in float4 pos : POSITION,
in float2 uvIn : TEXCOORD0,
in float4x4 worldMatrix : WORLDMATRIX,
in float4x4 projMatrix : PROJECTIONMATRIX,
in float4x4 viewMatrix : VIEWMATRIX,
out float2 uvOut : TEXCOORD0)
{
float4x4 ModelViewProjection = worldMatrix * viewMatrix * projMatrix;
float4 transformedVertex = ModelViewProjection * pos;
uvOut = uvIn;
return transformedVertex;
}
----
Gets processed by custom shader processor into this:
----
float4 TransformVertex(in float4 pos,
in float2 uvIn,
in float4x4 worldMatrix,
in float4x4 projMatrix,
in float4x4 viewMatrix,
out K15_Vector2 uvOut)
{
float4x4 ModelViewProjection = worldMatrix * viewMatrix * projMatrix;
float4 transformedVertex = ModelViewProjection * pos;
uvOut = uvIn;
return transformedVertex;
}
Plus data structure 'ShaderInformation' holding informations about the semantics of the parameter
Argument[0].name = "pos";
Argument[0].semantic = Position; (enum value)
Argument[0].type = Vector4; (enum value)
Argument[1].name = "uvIn";
Argument[1].semantic = TexCoord0; (enum value)
Argument[1].type = Vector2; (enum value)
...
----
This shader gets saved (+ the ShaderInformation structure) and can be loaded by the engine.
Once this shader is getting load by a specific render API (e.g. OpenGL), the 'ShaderInformation' data structure gets used to produce
wrapper code for this specific API.
In OpenGL the wrapper code for the above shader would be:
----
#define float4 vec4
#define float2 vec2
#define float4x4 mat4
in vec4 pos;
in vec2 uvIn;
uniform mat4 g_WorldMatrix;
uniform mat4 g_ProjMatrix;
uniform mat4 g_ViewMatrix;
out vec4 uvOut;
void main(void)
{
gl_Position = TransformVertex(pos, uvIn, g_WorldMatrix, g_ProjMatrix, g_ViewMatrix, uvOut);
}
//processed shader will be placed here
----
During the wrapper code generation, the names for auto uniforms (like world matrix, projection matrix and view matrix) are getting replaced with their appropriate names, so we can set them
automatically in the code.
This also allows us to generate wrapper code based on different supported opengl extensions e.g. uniform buffer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment