Skip to content

Commit

Permalink
emscripten samples bindings cleanup wip
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Sep 22, 2024
1 parent 7282bec commit 53ffb78
Show file tree
Hide file tree
Showing 9 changed files with 398 additions and 349 deletions.
36 changes: 27 additions & 9 deletions html5/bufferoffsets-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,41 @@ int main() {

// create a shader to render 2D colored shapes
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.attrs = {
[0].glsl_name = "pos",
[1].glsl_name = "color0",
},
.vertex_func.source =
"#version 300 es\n"
"layout(location=0) in vec2 pos;"
"layout(location=1) in vec3 color0;"
"out vec4 color;"
"attribute vec2 pos;"
"attribute vec3 color0;"
"varying vec4 color;"
"void main() {"
" gl_Position = vec4(pos, 0.5, 1.0);\n"
" color = vec4(color0, 1.0);\n"
"}\n",
// FIXME: for some reason using WebGL2 shaders causes corruption
//"#version 300 es\n"
//"in vec2 pos;"
//"in vec3 color0;"
//"out vec4 color;"
//"void main() {"
//" gl_Position = vec4(pos, 0.5, 1.0);\n"
//" color = vec4(color0, 1.0);\n"
//"}\n",
.fragment_func.source =
"#version 300 es\n",
"precision mediump float;\n"
"in vec4 color;\n"
"out vec4 frag_color;\n"
"varying vec4 color;\n"
"void main() {\n"
" frag_color = color;\n"
"}\n"
" gl_FragColor = color;\n"
"}\n",
// FIXME!
//"#version 300 es\n",
//"precision mediump float;\n"
//"in vec4 color;\n"
//"out vec4 frag_color;\n"
//"void main() {\n"
//" frag_color = color;\n"
//"}\n"
});

// a pipeline state object, default states are fine
Expand Down
80 changes: 39 additions & 41 deletions html5/dyntex-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,52 +117,50 @@ int main() {
state.bind = (sg_bindings){
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf,
.fs = {
.images[0] = img,
.samplers[0] = smp,
}
.images[0] = img,
.samplers[0] = smp,
};

// a shader to render textured cube
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vertex_func.source =
"#version 300 es\n"
"uniform mat4 mvp;\n"
"in vec4 position;\n"
"in vec4 color0;\n"
"in vec2 texcoord0;\n"
"out vec2 uv;"
"out vec4 color;"
"void main() {\n"
" gl_Position = mvp * position;\n"
" uv = texcoord0;\n"
" color = color0;\n"
"}\n",
.fragment_func.source =
"#version 300 es\n"
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"in vec4 color;\n"
"in vec2 uv;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = texture(tex, uv) * color;\n"
"}\n",
.attrs = {
[0].name = "position",
[1].name = "color0",
[2].name = "texcoord0"
[0].glsl_name = "position",
[1].glsl_name = "color0",
[2].glsl_name = "texcoord0"
},
.vs = {
.uniform_blocks[0] = {
.size = sizeof(vs_params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 }
}
},
.source =
"uniform mat4 mvp;\n"
"attribute vec4 position;\n"
"attribute vec4 color0;\n"
"attribute vec2 texcoord0;\n"
"varying vec2 uv;"
"varying vec4 color;"
"void main() {\n"
" gl_Position = mvp * position;\n"
" uv = texcoord0;\n"
" color = color0;\n"
"}\n",
.uniform_blocks[0] = {
.stage = SG_SHADERSTAGE_VERTEX,
.size = sizeof(vs_params_t),
.glsl_uniforms = {
[0] = { .glsl_name = "mvp", .type = SG_UNIFORMTYPE_MAT4 }
}
},
.fs = {
.images[0].used = true,
.samplers[0].used = true,
.image_sampler_pairs[0] = { .used = true, .glsl_name = "tex", .image_slot = 0, .sampler_slot = 0 },
.source =
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"varying vec4 color;\n"
"varying vec2 uv;\n"
"void main() {\n"
" gl_FragColor = texture2D(tex, uv) * color;\n"
"}\n"
}
.images[0].stage = SG_SHADERSTAGE_FRAGMENT,
.samplers[0].stage = SG_SHADERSTAGE_FRAGMENT,
.image_sampler_pairs[0] = { .stage = SG_SHADERSTAGE_FRAGMENT, .glsl_name = "tex", .image_slot = 0, .sampler_slot = 0 },
});

// a pipeline-state-object for the textured cube
Expand Down Expand Up @@ -209,13 +207,13 @@ static EM_BOOL draw(double time, void* userdata) {
game_of_life_update();

// update the dynamic image
sg_update_image(state.bind.fs.images[0], &(sg_image_data){ .subimage[0][0] = SG_RANGE(pixels) });
sg_update_image(state.bind.images[0], &(sg_image_data){ .subimage[0][0] = SG_RANGE(pixels) });

// draw pass
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = emsc_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.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
76 changes: 38 additions & 38 deletions html5/imgui-emsc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ int main() {
unsigned char* font_pixels;
int font_width, font_height;
io.Fonts->GetTexDataAsRGBA32(&font_pixels, &font_width, &font_height);
bind.fs.images[0] = sg_make_image({
bind.images[0] = sg_make_image({
.width = font_width,
.height = font_height,
.pixel_format = SG_PIXELFORMAT_RGBA8,
Expand All @@ -170,51 +170,51 @@ int main() {
.size = size_t(font_width * font_height * 4)
}
});
bind.fs.samplers[0] = sg_make_sampler({
bind.samplers[0] = sg_make_sampler({
.wrap_u = SG_WRAP_CLAMP_TO_EDGE,
.wrap_v = SG_WRAP_CLAMP_TO_EDGE,
});

// shader object for imgui rendering
sg_shader shd = sg_make_shader({
.vertex_func.source =
"#version 300 es\n"
"uniform vec2 disp_size;\n"
"in vec2 position;\n"
"in vec2 texcoord0;\n"
"in vec4 color0;\n"
"out vec2 uv;\n"
"out vec4 color;\n"
"void main() {\n"
" gl_Position = vec4(((position/disp_size)-0.5)*vec2(2.0,-2.0), 0.5, 1.0);\n"
" uv = texcoord0;\n"
" color = color0;\n"
"}\n",
.fragment_func.source =
"#version 300 es\n"
"precision mediump float;"
"uniform sampler2D tex;\n"
"in vec2 uv;\n"
"in vec4 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = texture(tex, uv) * color;\n"
"}\n",
.attrs = {
[0].name = "position",
[1].name = "texcoord0",
[2].name = "color0"
[0].glsl_name = "position",
[1].glsl_name = "texcoord0",
[2].glsl_name = "color0"
},
.vs = {
.uniform_blocks[0] = {
.size = sizeof(vs_params_t),
.uniforms = {
[0] = { .name="disp_size", .type=SG_UNIFORMTYPE_FLOAT2}
}
},
.source =
"uniform vec2 disp_size;\n"
"attribute vec2 position;\n"
"attribute vec2 texcoord0;\n"
"attribute vec4 color0;\n"
"varying vec2 uv;\n"
"varying vec4 color;\n"
"void main() {\n"
" gl_Position = vec4(((position/disp_size)-0.5)*vec2(2.0,-2.0), 0.5, 1.0);\n"
" uv = texcoord0;\n"
" color = color0;\n"
"}\n",
.uniform_blocks[0] = {
.stage = SG_SHADERSTAGE_FRAGMENT,
.size = sizeof(vs_params_t),
.glsl_uniforms = {
[0] = { .glsl_name = "disp_size", .type = SG_UNIFORMTYPE_FLOAT2}
}
},
.fs = {
.images[0] = { .used = true },
.samplers[0] = { .used = true },
.image_sampler_pairs[0] = { .used = true, .glsl_name = "tex", .image_slot = 0, .sampler_slot = 0 },
.source =
"precision mediump float;"
"uniform sampler2D tex;\n"
"varying vec2 uv;\n"
"varying vec4 color;\n"
"void main() {\n"
" gl_FragColor = texture2D(tex, uv) * color;\n"
"}\n"
}
.images[0].stage = SG_SHADERSTAGE_FRAGMENT,
.samplers[0].stage = SG_SHADERSTAGE_FRAGMENT,
.image_sampler_pairs[0] = { .stage = SG_SHADERSTAGE_FRAGMENT, .glsl_name = "tex", .image_slot = 0, .sampler_slot = 0 },
});

// pipeline object for imgui rendering
Expand Down Expand Up @@ -316,7 +316,7 @@ void draw_imgui(ImDrawData* draw_data) {
vs_params.disp_size.x = ImGui::GetIO().DisplaySize.x;
vs_params.disp_size.y = ImGui::GetIO().DisplaySize.y;
sg_apply_pipeline(pip);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE(vs_params));
sg_apply_uniforms(0, SG_RANGE(vs_params));
for (int cl_index = 0; cl_index < draw_data->CmdListsCount; cl_index++) {
const ImDrawList* cl = draw_data->CmdLists[cl_index];

Expand Down
66 changes: 33 additions & 33 deletions html5/inject-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ int main() {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, IMG_WIDTH, IMG_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}
sg_reset_state_cache();
state.bind.fs.images[0] = sg_make_image(&img_desc);
state.bind.images[0] = sg_make_image(&img_desc);

// create a GL sampler object
sg_sampler_desc smp_desc = {
Expand All @@ -144,43 +144,43 @@ int main() {
glSamplerParameteri(smp_desc.gl_sampler, GL_TEXTURE_WRAP_S, GL_REPEAT);
glSamplerParameteri(smp_desc.gl_sampler, GL_TEXTURE_WRAP_T, GL_REPEAT);
sg_reset_state_cache();
state.bind.fs.samplers[0] = sg_make_sampler(&smp_desc);
state.bind.samplers[0] = sg_make_sampler(&smp_desc);

// create shader
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vertex_func.source =
"#version 300 es\n"
"uniform mat4 mvp;\n"
"in vec4 position;\n"
"in vec2 texcoord0;\n"
"out vec2 uv;"
"void main() {\n"
" gl_Position = mvp * position;\n"
" uv = texcoord0;\n"
"}\n",
.fragment_func.source =
"#version 300 es\n"
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"in vec2 uv;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = texture(tex, uv);\n"
"}\n",
.attrs = {
[0].name = "position",
[1].name = "texcoord0"
[0].glsl_name = "position",
[1].glsl_name = "texcoord0"
},
.vs = {
.uniform_blocks[0] = {
.size = sizeof(vs_params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 }
},
.uniform_blocks[0] = {
.stage = SG_SHADERSTAGE_VERTEX,
.size = sizeof(vs_params_t),
.glsl_uniforms = {
[0] = { .glsl_name="mvp", .type=SG_UNIFORMTYPE_MAT4 }
},
.source =
"uniform mat4 mvp;\n"
"attribute vec4 position;\n"
"attribute vec2 texcoord0;\n"
"varying vec2 uv;"
"void main() {\n"
" gl_Position = mvp * position;\n"
" uv = texcoord0;\n"
"}\n",
},
.fs = {
.images[0].used = true,
.samplers[0].used = true,
.image_sampler_pairs[0] = { .used = true, .glsl_name = "tex", .image_slot = 0, .sampler_slot = 0 },
.source =
"precision mediump float;\n"
"uniform sampler2D tex;\n"
"varying vec2 uv;\n"
"void main() {\n"
" gl_FragColor = texture2D(tex, uv);\n"
"}\n"
},
.images[0].stage = SG_SHADERSTAGE_FRAGMENT,
.samplers[0].stage = SG_SHADERSTAGE_FRAGMENT,
.image_sampler_pairs[0] = { .stage = SG_SHADERSTAGE_FRAGMENT, .glsl_name = "tex", .image_slot = 0, .sampler_slot = 0 },
});

// create pipeline object
Expand Down Expand Up @@ -230,13 +230,13 @@ static EM_BOOL draw(double time, void* userdata) {
}
}
state.counter++;
sg_update_image(state.bind.fs.images[0], &(sg_image_data){ .subimage[0][0] = SG_RANGE(pixels) });
sg_update_image(state.bind.images[0], &(sg_image_data){ .subimage[0][0] = SG_RANGE(pixels) });

// ...and draw
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = emsc_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.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 53ffb78

Please sign in to comment.