Skip to content

Instantly share code, notes, and snippets.

@floooh
Last active May 1, 2019 12:48
Show Gist options
  • Save floooh/cf00e79676660dc752440efd39359b14 to your computer and use it in GitHub Desktop.
Save floooh/cf00e79676660dc752440efd39359b14 to your computer and use it in GitHub Desktop.
Sokol shader-code-genenerator example
//------------------------------------------------------------------------------
// shaders for cubemaprt-sapp sample
//------------------------------------------------------------------------------
@ctype mat4 hmm_mat4
@ctype vec4 hmm_vec4
// same vertex shader for offscreen- and default-pass
@vs vs
uniform shape_uniforms {
mat4 mvp; // model-view-projection matrix
mat4 model; // model matrix
vec4 shape_color;
vec4 light_dir; // light-direction in world space
vec4 eye_pos; // eye-pos in world space
};
in vec4 pos;
in vec3 norm;
out vec3 world_position;
out vec3 world_normal;
out vec3 world_eyepos;
out vec3 world_lightdir;
out vec4 color;
void main() {
gl_Position = mvp * pos;
world_position = vec4(model * pos).xyz;
world_normal = vec4(model * vec4(norm, 0.0)).xyz;
world_eyepos = eye_pos.xyz;
world_lightdir = light_dir.xyz;
color = shape_color;
}
@end
// shared code for fragment shaders
@block lighting
vec3 light(vec3 base_color, vec3 eye_vec, vec3 normal, vec3 light_vec) {
float ambient = 0.25;
float n_dot_l = max(dot(normal, light_vec), 0.0);
float diff = n_dot_l + ambient;
float spec_power = 16.0;
vec3 r = reflect(-light_vec, normal);
float r_dot_v = max(dot(r, eye_vec), 0.0);
float spec = pow(r_dot_v, spec_power) * n_dot_l;
return base_color * (diff+ambient) + vec3(spec,spec,spec);
}
@end
@block fs_inputs
in vec3 world_position;
in vec3 world_normal;
in vec3 world_eyepos;
in vec3 world_lightdir;
in vec4 color;
@end
// fragment shader for shaped rendering, both offscreen and display
@fs fs_shapes
@include_block lighting
@include_block fs_inputs
out vec4 frag_color;
void main() {
vec3 eye_vec = normalize(world_eyepos - world_position);
vec3 nrm = normalize(world_normal);
vec3 light_dir = normalize(world_lightdir);
frag_color = vec4(light(color.xyz, eye_vec, nrm, light_dir), 1.0);
}
@end
// fragment shader for the central cubemapped cube
@fs fs_cube
@include_block lighting
@include_block fs_inputs
uniform samplerCube tex;
out vec4 frag_color;
void main() {
vec3 eye_vec = normalize(world_eyepos - world_position);
vec3 nrm = normalize(world_normal);
vec3 light_dir = normalize(world_lightdir);
vec3 refl_vec = normalize(world_position);
vec3 refl_color = texture(tex, refl_vec).xyz;
frag_color = vec4(light(refl_color * color.xyz, eye_vec, nrm, light_dir), 1.0);
}
@end
@program shapes vs fs_shapes
@program cube vs fs_cube
#pragma once
/*
#version:1# (machine generated, don't edit!)
Generated by sokol-shdc (https://github.com/floooh/sokol-tools)
Overview:
Shader program 'cube':
Desc struct: cube_shader_desc
Vertex shader: vs
Attribute slots:
vs_pos = 0
vs_norm = 1
Uniform block 'shape_uniforms':
C struct: shape_uniforms_t
Bind slot: shape_uniforms_slot = 0
Fragment shader: fs_cube
Image 'tex':
Type: SG_IMAGETYPE_CUBE
Bind slot: tex_slot = 0
Shader program 'shapes':
Desc struct: shapes_shader_desc
Vertex shader: vs
Attribute slots:
vs_pos = 0
vs_norm = 1
Uniform block 'shape_uniforms':
C struct: shape_uniforms_t
Bind slot: shape_uniforms_slot = 0
Fragment shader: fs_shapes
Shader descriptor structs:
sg_shader cube = sg_make_shader(&cube_shader_desc);
sg_shader shapes = sg_make_shader(&shapes_shader_desc);
Vertex attribute locations for vertex shader 'vs':
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[vs_pos] = { ... },
[vs_norm] = { ... },
},
},
...});
Image bind slots, use as index in sg_bindings.vs_images[] or .fs_images[]
tex_slot = 0;
Bind slot and C-struct for uniform block 'shape_uniforms':
shape_uniforms_t shape_uniforms = {
.mvp = ...;
.model = ...;
.shape_color = ...;
.light_dir = ...;
.eye_pos = ...;
};
sg_apply_uniforms(SG_SHADERSTAGE_[VS|FS], shape_uniforms_slot, &shape_uniforms, sizeof(shape_uniforms));
*/
#if !defined(SOKOL_GFX_INCLUDED)
#error "Please include sokol_gfx.h before cubemaprt-sapp.glsl.h"
#endif
static const int vs_pos = 0;
static const int vs_norm = 1;
static const int tex_slot = 0;
static const int shape_uniforms_slot = 0;
#pragma pack(push,1)
typedef struct shape_uniforms_t {
hmm_mat4 mvp;
hmm_mat4 model;
hmm_vec4 shape_color;
hmm_vec4 light_dir;
hmm_vec4 eye_pos;
} shape_uniforms_t;
#pragma pack(pop)
static sg_shader_desc cube_shader_desc = {
0, /* _start_canary */
{ /*attrs*/{"pos","TEXCOORD",0},{"norm","TEXCOORD",1},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
{ /* vs */
"#version 330\n"
"\n"
"uniform vec4 shape_uniforms[11];\n"
"layout(location = 0) in vec4 pos;\n"
"out vec3 world_position;\n"
"out vec3 world_normal;\n"
"layout(location = 1) in vec3 norm;\n"
"out vec3 world_eyepos;\n"
"out vec3 world_lightdir;\n"
"out vec4 color;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = mat4(shape_uniforms[0], shape_uniforms[1], shape_uniforms[2], shape_uniforms[3]) * pos;\n"
" world_position = vec4(mat4(shape_uniforms[4], shape_uniforms[5], shape_uniforms[6], shape_uniforms[7]) * pos).xyz;\n"
" world_normal = vec4(mat4(shape_uniforms[4], shape_uniforms[5], shape_uniforms[6], shape_uniforms[7]) * vec4(norm, 0.0)).xyz;\n"
" world_eyepos = shape_uniforms[10].xyz;\n"
" world_lightdir = shape_uniforms[9].xyz;\n"
" color = shape_uniforms[8];\n"
"}\n"
"\n"
,
0, /* bytecode */
0, /* bytecode_size */
"main", /* entry */
{ /* uniform blocks */
{
176, /* size */
{ /* uniforms */{"shape_uniforms",SG_UNIFORMTYPE_FLOAT4,11},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
},
{ /* images */ {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, },
},
{ /* fs */
"#version 330\n"
"\n"
"uniform samplerCube tex;\n"
"\n"
"in vec3 world_eyepos;\n"
"in vec3 world_position;\n"
"in vec3 world_normal;\n"
"in vec3 world_lightdir;\n"
"layout(location = 0) out vec4 frag_color;\n"
"in vec4 color;\n"
"\n"
"vec3 light(vec3 base_color, vec3 eye_vec, vec3 normal, vec3 light_vec)\n"
"{\n"
" float ambient = 0.25;\n"
" float n_dot_l = max(dot(normal, light_vec), 0.0);\n"
" float diff = n_dot_l + ambient;\n"
" float spec_power = 16.0;\n"
" vec3 r = reflect(-light_vec, normal);\n"
" float r_dot_v = max(dot(r, eye_vec), 0.0);\n"
" float spec = pow(r_dot_v, spec_power) * n_dot_l;\n"
" return (base_color * (diff + ambient)) + vec3(spec, spec, spec);\n"
"}\n"
"\n"
"void main()\n"
"{\n"
" vec3 eye_vec = normalize(world_eyepos - world_position);\n"
" vec3 nrm = normalize(world_normal);\n"
" vec3 light_dir = normalize(world_lightdir);\n"
" vec3 refl_vec = normalize(world_position);\n"
" vec3 refl_color = texture(tex, refl_vec).xyz;\n"
" vec3 param = refl_color * color.xyz;\n"
" vec3 param_1 = eye_vec;\n"
" vec3 param_2 = nrm;\n"
" vec3 param_3 = light_dir;\n"
" frag_color = vec4(light(param, param_1, param_2, param_3), 1.0);\n"
"}\n"
"\n"
,
0, /* bytecode */
0, /* bytecode_size */
"main", /* entry */
{ /* uniform blocks */
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
},
{ /* images */ {"tex",SG_IMAGETYPE_CUBE},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, },
},
"cube_shader", /* label */
0, /* _end_canary */
};
static sg_shader_desc shapes_shader_desc = {
0, /* _start_canary */
{ /*attrs*/{"pos","TEXCOORD",0},{"norm","TEXCOORD",1},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
{ /* vs */
"#version 330\n"
"\n"
"uniform vec4 shape_uniforms[11];\n"
"layout(location = 0) in vec4 pos;\n"
"out vec3 world_position;\n"
"out vec3 world_normal;\n"
"layout(location = 1) in vec3 norm;\n"
"out vec3 world_eyepos;\n"
"out vec3 world_lightdir;\n"
"out vec4 color;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = mat4(shape_uniforms[0], shape_uniforms[1], shape_uniforms[2], shape_uniforms[3]) * pos;\n"
" world_position = vec4(mat4(shape_uniforms[4], shape_uniforms[5], shape_uniforms[6], shape_uniforms[7]) * pos).xyz;\n"
" world_normal = vec4(mat4(shape_uniforms[4], shape_uniforms[5], shape_uniforms[6], shape_uniforms[7]) * vec4(norm, 0.0)).xyz;\n"
" world_eyepos = shape_uniforms[10].xyz;\n"
" world_lightdir = shape_uniforms[9].xyz;\n"
" color = shape_uniforms[8];\n"
"}\n"
"\n"
,
0, /* bytecode */
0, /* bytecode_size */
"main", /* entry */
{ /* uniform blocks */
{
176, /* size */
{ /* uniforms */{"shape_uniforms",SG_UNIFORMTYPE_FLOAT4,11},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
},
{ /* images */ {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, },
},
{ /* fs */
"#version 330\n"
"\n"
"in vec3 world_eyepos;\n"
"in vec3 world_position;\n"
"in vec3 world_normal;\n"
"in vec3 world_lightdir;\n"
"layout(location = 0) out vec4 frag_color;\n"
"in vec4 color;\n"
"\n"
"vec3 light(vec3 base_color, vec3 eye_vec, vec3 normal, vec3 light_vec)\n"
"{\n"
" float ambient = 0.25;\n"
" float n_dot_l = max(dot(normal, light_vec), 0.0);\n"
" float diff = n_dot_l + ambient;\n"
" float spec_power = 16.0;\n"
" vec3 r = reflect(-light_vec, normal);\n"
" float r_dot_v = max(dot(r, eye_vec), 0.0);\n"
" float spec = pow(r_dot_v, spec_power) * n_dot_l;\n"
" return (base_color * (diff + ambient)) + vec3(spec, spec, spec);\n"
"}\n"
"\n"
"void main()\n"
"{\n"
" vec3 eye_vec = normalize(world_eyepos - world_position);\n"
" vec3 nrm = normalize(world_normal);\n"
" vec3 light_dir = normalize(world_lightdir);\n"
" vec3 param = color.xyz;\n"
" vec3 param_1 = eye_vec;\n"
" vec3 param_2 = nrm;\n"
" vec3 param_3 = light_dir;\n"
" frag_color = vec4(light(param, param_1, param_2, param_3), 1.0);\n"
"}\n"
"\n"
,
0, /* bytecode */
0, /* bytecode_size */
"main", /* entry */
{ /* uniform blocks */
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
{
0, /* size */
{ /* uniforms */{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}, },
},
},
{ /* images */ {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, },
},
"shapes_shader", /* label */
0, /* _end_canary */
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment