Skip to content

Commit

Permalink
fix most glfw samples for new binding model
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Sep 21, 2024
1 parent 6b02958 commit 6311b3d
Show file tree
Hide file tree
Showing 17 changed files with 435 additions and 440 deletions.
100 changes: 50 additions & 50 deletions glfw/arraytex-glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,60 +120,60 @@ int main() {
sg_bindings bind = {
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf,
.fs = {
.images[0] = img,
.samplers[0] = smp,
}
.images[0] = img,
.samplers[0] = smp,
};

/* shader to sample from array texture */
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vs = {
.uniform_blocks[0] = {
.size = sizeof(params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 },
[1] = { .name="offset0", .type=SG_UNIFORMTYPE_FLOAT2 },
[2] = { .name="offset1", .type=SG_UNIFORMTYPE_FLOAT2 },
[3] = { .name="offset2", .type=SG_UNIFORMTYPE_FLOAT2 }
}
},
.source =
"#version 330\n"
"uniform mat4 mvp;\n"
"uniform vec2 offset0;\n"
"uniform vec2 offset1;\n"
"uniform vec2 offset2;\n"
"layout(location=0) in vec4 position;\n"
"layout(location=1) in vec2 texcoord0;\n"
"out vec3 uv0;\n"
"out vec3 uv1;\n"
"out vec3 uv2;\n"
"void main() {\n"
" gl_Position = mvp * position;\n"
" uv0 = vec3(texcoord0 + offset0, 0.0);\n"
" uv1 = vec3(texcoord0 + offset1, 1.0);\n"
" uv2 = vec3(texcoord0 + offset2, 2.0);\n"
"}\n",
.vertex_func.source =
"#version 410\n"
"uniform mat4 mvp;\n"
"uniform vec2 offset0;\n"
"uniform vec2 offset1;\n"
"uniform vec2 offset2;\n"
"layout(location=0) in vec4 position;\n"
"layout(location=1) in vec2 texcoord0;\n"
"out vec3 uv0;\n"
"out vec3 uv1;\n"
"out vec3 uv2;\n"
"void main() {\n"
" gl_Position = mvp * position;\n"
" uv0 = vec3(texcoord0 + offset0, 0.0);\n"
" uv1 = vec3(texcoord0 + offset1, 1.0);\n"
" uv2 = vec3(texcoord0 + offset2, 2.0);\n"
"}\n",
.fragment_func.source =
"#version 410\n"
"uniform sampler2DArray tex;\n"
"in vec3 uv0;\n"
"in vec3 uv1;\n"
"in vec3 uv2;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" vec4 c0 = texture(tex, uv0);\n"
" vec4 c1 = texture(tex, uv1);\n"
" vec4 c2 = texture(tex, uv2);\n"
" frag_color = vec4(c0.xyz + c1.xyz + c2.xyz, 1.0);\n"
"}\n",
.uniform_blocks[0] = {
.stage = SG_SHADERSTAGE_VERTEX,
.size = sizeof(params_t),
.glsl_uniforms = {
[0] = { .glsl_name="mvp", .type=SG_UNIFORMTYPE_MAT4 },
[1] = { .glsl_name="offset0", .type=SG_UNIFORMTYPE_FLOAT2 },
[2] = { .glsl_name="offset1", .type=SG_UNIFORMTYPE_FLOAT2 },
[3] = { .glsl_name="offset2", .type=SG_UNIFORMTYPE_FLOAT2 }
}
},
.images[0] = { .stage = SG_SHADERSTAGE_FRAGMENT, .image_type = SG_IMAGETYPE_ARRAY },
.samplers[0] = { .stage = SG_SHADERSTAGE_FRAGMENT },
.image_sampler_pairs[0] = {
.stage = SG_SHADERSTAGE_FRAGMENT,
.image_slot = 0,
.sampler_slot = 0,
.glsl_name = "tex",
},
.fs = {
.images[0] = { .used = true, .image_type = SG_IMAGETYPE_ARRAY },
.samplers[0].used = true,
.image_sampler_pairs[0] = { .used = true, .glsl_name = "tex", .image_slot = 0, .sampler_slot = 0 },
.source =
"#version 330\n"
"uniform sampler2DArray tex;\n"
"in vec3 uv0;\n"
"in vec3 uv1;\n"
"in vec3 uv2;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" vec4 c0 = texture(tex, uv0);\n"
" vec4 c1 = texture(tex, uv1);\n"
" vec4 c2 = texture(tex, uv2);\n"
" frag_color = vec4(c0.xyz + c1.xyz + c2.xyz, 1.0);\n"
"}\n",
}
});

// create pipeline object
Expand Down Expand Up @@ -224,7 +224,7 @@ int main() {
sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = glfw_swapchain() });
sg_apply_pipeline(pip);
sg_apply_bindings(&bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
sg_apply_uniforms(0, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
sg_end_pass();
sg_commit();
Expand Down
60 changes: 30 additions & 30 deletions glfw/blend-glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,26 @@ int main() {

// a shader for the fullscreen background quad
sg_shader bg_shd = sg_make_shader(&(sg_shader_desc){
.vs.source =
"#version 330\n"
.vertex_func.source =
"#version 410\n"
"layout(location=0) in vec2 position;\n"
"void main() {\n"
" gl_Position = vec4(position, 0.5, 1.0);\n"
"}\n",
.fs = {
.uniform_blocks[0] = {
.size = sizeof(fs_params_t),
.uniforms = {
[0] = { .name="tick", .type=SG_UNIFORMTYPE_FLOAT }
}
},
.source =
"#version 330\n"
"uniform float tick;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" vec2 xy = fract((gl_FragCoord.xy-vec2(tick)) / 50.0);\n"
" frag_color = vec4(vec3(xy.x*xy.y), 1.0);\n"
"}\n"
.fragment_func.source =
"#version 410\n"
"uniform float tick;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" vec2 xy = fract((gl_FragCoord.xy-vec2(tick)) / 50.0);\n"
" frag_color = vec4(vec3(xy.x*xy.y), 1.0);\n"
"}\n",
.uniform_blocks[0] = {
.stage = SG_SHADERSTAGE_FRAGMENT,
.size = sizeof(fs_params_t),
.glsl_uniforms = {
[0] = { .glsl_name = "tick", .type = SG_UNIFORMTYPE_FLOAT },
}
}
});

Expand All @@ -85,14 +84,8 @@ int main() {

// a shader for the blended quads
sg_shader quad_shd = sg_make_shader(&(sg_shader_desc){
.vs.uniform_blocks[0] = {
.size = sizeof(vs_params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 }
}
},
.vs.source =
"#version 330\n"
.vertex_func.source =
"#version 410\n"
"uniform mat4 mvp;\n"
"layout(location=0) in vec4 position;\n"
"layout(location=1) in vec4 color0;\n"
Expand All @@ -101,13 +94,20 @@ int main() {
" gl_Position = mvp * position;\n"
" color = color0;\n"
"}\n",
.fs.source =
"#version 330\n"
.fragment_func.source =
"#version 410\n"
"in vec4 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = color;\n"
"}"
"}",
.uniform_blocks[0] = {
.stage = SG_SHADERSTAGE_VERTEX,
.size = sizeof(vs_params_t),
.glsl_uniforms = {
[0] = { .glsl_name = "mvp", .type = SG_UNIFORMTYPE_MAT4 }
}
},
});

// one pipeline object per blend-factor combination
Expand Down Expand Up @@ -161,7 +161,7 @@ int main() {
// draw a background quad
sg_apply_pipeline(bg_pip);
sg_apply_bindings(&bind);
sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, &SG_RANGE(fs_params));
sg_apply_uniforms(0, &SG_RANGE(fs_params));
sg_draw(0, 4, 1);

// draw the blended quads
Expand All @@ -177,7 +177,7 @@ int main() {

sg_apply_pipeline(pips[src][dst]);
sg_apply_bindings(&bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
sg_apply_uniforms(0, &SG_RANGE(vs_params));
sg_draw(0, 4, 1);
}
}
Expand Down
10 changes: 5 additions & 5 deletions glfw/bufferoffsets-glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ int main() {

// create a shader to render 2D colored shapes
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vs.source =
"#version 330\n"
.vertex_func.source =
"#version 410\n"
"layout(location=0) in vec2 pos;"
"layout(location=1) in vec3 color0;"
"out vec4 color;"
"void main() {"
" gl_Position = vec4(pos, 0.5, 1.0);\n"
" color = vec4(color0, 1.0);\n"
"}\n",
.fs.source =
"#version 330\n"
.fragment_func.source =
"#version 410\n"
"in vec4 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = color;\n"
"}\n"
"}\n",
});

// a pipeline state object, default states are fine
Expand Down
25 changes: 13 additions & 12 deletions glfw/cube-glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,11 @@ int main() {

// create shader
sg_shader shd = sg_make_shader(&(sg_shader_desc) {
.vs.uniform_blocks[0] = {
.size = sizeof(params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 }
}
},
/* NOTE: since the shader defines explicit attribute locations,
we don't need to provide an attribute name lookup table in the shader
*/
.vs.source =
"#version 330\n"
.vertex_func.source =
"#version 410\n"
"uniform mat4 mvp;\n"
"layout(location=0) in vec4 position;\n"
"layout(location=1) in vec4 color0;\n"
Expand All @@ -104,13 +98,20 @@ int main() {
" gl_Position = mvp * position;\n"
" color = color0;\n"
"}\n",
.fs.source =
"#version 330\n"
.fragment_func.source =
"#version 410\n"
"in vec4 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = color;\n"
"}\n"
"}\n",
.uniform_blocks[0] = {
.stage = SG_SHADERSTAGE_VERTEX,
.size = sizeof(params_t),
.glsl_uniforms = {
[0] = { .type = SG_UNIFORMTYPE_MAT4, .glsl_name = "mvp" },
}
},
});

// create pipeline object
Expand Down Expand Up @@ -155,7 +156,7 @@ int main() {
sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = glfw_swapchain() });
sg_apply_pipeline(pip);
sg_apply_bindings(&bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
sg_apply_uniforms(0, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
sg_end_pass();
sg_commit();
Expand Down
Loading

0 comments on commit 6311b3d

Please sign in to comment.