Skip to content

Commit

Permalink
fix emscripten samples
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Feb 13, 2024
1 parent 3af935f commit a7a03d3
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 48 deletions.
3 changes: 2 additions & 1 deletion html5/arraytex-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int main() {
emsc_init("#canvas", EMSC_ANTIALIAS);
// setup sokol_gfx
sg_setup(&(sg_desc){
.environment = emsc_environment(),
.logger.func = slog_func,
});
assert(sg_isvalid());
Expand Down Expand Up @@ -237,7 +238,7 @@ static EM_BOOL draw(double time, void* userdata) {
.offset2 = HMM_Vec2(0.0f, 0.0f)
};

sg_begin_default_pass(&state.pass_action, emsc_width(), emsc_height());
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));
Expand Down
3 changes: 2 additions & 1 deletion html5/blend-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int main() {
// setup sokol_gfx (need to increase pipeline pool size)
sg_setup(&(sg_desc){
.pipeline_pool_size = NUM_BLEND_FACTORS * NUM_BLEND_FACTORS + 1,
.environment = emsc_environment(),
.logger.func = slog_func,
});

Expand Down Expand Up @@ -194,7 +195,7 @@ static EM_BOOL draw(double time, void* userdata) {
hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 0.0f, 25.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view);

sg_begin_default_pass(&state.pass_action, emsc_width(), emsc_height());
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = emsc_swapchain() });

// draw a background quad
sg_apply_pipeline(state.bg_pip);
Expand Down
7 changes: 5 additions & 2 deletions html5/bufferoffsets-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ int main() {
emsc_init("#canvas", EMSC_NONE);

// setup sokol_gfx
sg_setup(&(sg_desc){ .logger.func = slog_func });
sg_setup(&(sg_desc){
.environment = emsc_environment(),
.logger.func = slog_func
});
assert(sg_isvalid());

// a 2D triangle and quad in 1 vertex buffer and 1 index buffer
Expand Down Expand Up @@ -98,7 +101,7 @@ int main() {

static EM_BOOL draw(double time, void* userdata) {
(void)time; (void)userdata;
sg_begin_default_pass(&state.pass_action, emsc_width(), emsc_height());
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = emsc_swapchain() });
sg_apply_pipeline(state.pip);
// render triangle
state.bind.vertex_buffer_offsets[0] = 0;
Expand Down
7 changes: 5 additions & 2 deletions html5/clear-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ int main() {
emsc_init("#canvas", EMSC_NONE);

// setup sokol_gfx
sg_setup(&(sg_desc){ .logger.func = slog_func });
sg_setup(&(sg_desc){
.environment = emsc_environment(),
.logger.func = slog_func
});
assert(sg_isvalid());

// setup pass action to clear to red
Expand All @@ -37,7 +40,7 @@ static EM_BOOL draw(double time, void* userdata) {
pass_action.colors[0].clear_value.g = g;

// draw one frame
sg_begin_default_pass(&pass_action, emsc_width(), emsc_height());
sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = emsc_swapchain() });
sg_end_pass();
sg_commit();
return EM_TRUE;
Expand Down
7 changes: 5 additions & 2 deletions html5/cube-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ int main() {
emsc_init("#canvas", EMSC_ANTIALIAS);

// setup sokol_gfx
sg_setup(&(sg_desc){ .logger.func = slog_func });
sg_setup(&(sg_desc){
.environment = emsc_environment(),
.logger.func = slog_func
});
assert(sg_isvalid());

// cube vertex buffer
Expand Down Expand Up @@ -154,7 +157,7 @@ static EM_BOOL draw(double time, void* userdata) {
};

// ...and draw
sg_begin_default_pass(&state.pass_action, emsc_width(), emsc_height());
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));
Expand Down
7 changes: 5 additions & 2 deletions html5/dyntex-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ int main() {
emsc_init("#canvas", EMSC_ANTIALIAS);

// setup sokol_gfx
sg_setup(&(sg_desc){ .logger.func = slog_func });
sg_setup(&(sg_desc){
.environment = emsc_environment(),
.logger.func = slog_func
});
assert(sg_isvalid());

// a 128x128 image with streaming-update strategy
Expand Down Expand Up @@ -209,7 +212,7 @@ static EM_BOOL draw(double time, void* userdata) {
sg_update_image(state.bind.fs.images[0], &(sg_image_data){ .subimage[0][0] = SG_RANGE(pixels) });

// draw pass
sg_begin_default_pass(&state.pass_action, emsc_width(), emsc_height());
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));
Expand Down
38 changes: 36 additions & 2 deletions html5/emsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
/* common emscripten platform helper functions */
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <GLES3/gl3.h>

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#pragma clang diagnostic ignored "-Wmissing-braces"
#endif

static const char* _emsc_canvas_name = 0;
static int _emsc_sample_count = 0;
static double _emsc_width = 0;
static double _emsc_height = 0;
static GLint _emsc_framebuffer = 0;

enum {
EMSC_NONE = 0,
Expand Down Expand Up @@ -33,14 +42,39 @@ void emsc_init(const char* canvas_name, int flags) {
emscripten_webgl_init_context_attributes(&attrs);
attrs.antialias = flags & EMSC_ANTIALIAS;
attrs.majorVersion = 2;
_emsc_sample_count = (flags & EMSC_ANTIALIAS) ? 4 : 1;
ctx = emscripten_webgl_create_context(canvas_name, &attrs);
emscripten_webgl_make_context_current(ctx);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&_emsc_framebuffer);
}

int emsc_width() {
int emsc_width(void) {
return (int) _emsc_width;
}

int emsc_height() {
int emsc_height(void) {
return (int) _emsc_height;
}

sg_environment emsc_environment(void) {
return (sg_environment){
.defaults = {
.color_format = SG_PIXELFORMAT_RGBA8,
.depth_format = SG_PIXELFORMAT_DEPTH_STENCIL,
.sample_count = _emsc_sample_count,
}
};
}

sg_swapchain emsc_swapchain(void) {
return (sg_swapchain) {
.width = (int)_emsc_width,
.height = (int)_emsc_height,
.sample_count = _emsc_sample_count,
.color_format = SG_PIXELFORMAT_RGBA8,
.depth_format = SG_PIXELFORMAT_DEPTH_STENCIL,
.gl = {
.framebuffer = (uint32_t)_emsc_framebuffer,
}
};
}
3 changes: 2 additions & 1 deletion html5/imgui-emsc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ int main() {
// setup sokol_gfx and sokol_time
stm_setup();
sg_setup(sg_desc{
.environment = emsc_environment(),
.logger = {
.func = slog_func
}
Expand Down Expand Up @@ -295,7 +296,7 @@ static EM_BOOL draw(double time, void* userdata) {
}

// the sokol_gfx draw pass
sg_begin_default_pass(&pass_action, emsc_width(), emsc_height());
sg_begin_pass({ .action = pass_action, .swapchain = emsc_swapchain() });
ImGui::Render();
draw_imgui(ImGui::GetDrawData());
sg_end_pass();
Expand Down
7 changes: 5 additions & 2 deletions html5/inject-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ int main() {
emsc_init("#canvas", EMSC_ANTIALIAS);

// setup sokol_gfx
sg_setup(&(sg_desc){ .logger.func = slog_func });
sg_setup(&(sg_desc){
.environment = emsc_environment(),
.logger.func = slog_func
});

// create a native GL vertex and index buffer
float vertices[] = {
Expand Down Expand Up @@ -230,7 +233,7 @@ static EM_BOOL draw(double time, void* userdata) {
sg_update_image(state.bind.fs.images[0], &(sg_image_data){ .subimage[0][0] = SG_RANGE(pixels) });

// ...and draw
sg_begin_default_pass(&state.pass_action, emsc_width(), emsc_height());
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));
Expand Down
7 changes: 5 additions & 2 deletions html5/instancing-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ int main() {
emsc_init("#canvas", EMSC_ANTIALIAS);

// setup sokol_gfx
sg_setup(&(sg_desc){ .logger.func = slog_func });
sg_setup(&(sg_desc){
.environment = emsc_environment(),
.logger.func = slog_func
});
assert(sg_isvalid());

// vertex buffer for static geometry (goes into vertex buffer bind slot 0)
Expand Down Expand Up @@ -186,7 +189,7 @@ static EM_BOOL draw(double time, void* userdata) {
};

// and the actual draw pass...
sg_begin_default_pass(&state.pass_action, emsc_width(), emsc_height());
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));
Expand Down
7 changes: 4 additions & 3 deletions html5/mipmap-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ int main() {

// setup sokol_gfx
sg_desc desc = {
.environment = emsc_environment(),
.logger.func = slog_func,
};
sg_setup(&desc);
Expand Down Expand Up @@ -130,8 +131,8 @@ int main() {
// the last 4 samplers use different anistropy levels
smp_desc.min_lod = 0.0f;
smp_desc.max_lod = 0.0f; // for max_lod, zero-initialized means "FLT_MAX"
smp_desc.min_filter = SG_FILTER_NEAREST;
smp_desc.mag_filter = SG_FILTER_NEAREST;
smp_desc.min_filter = SG_FILTER_LINEAR;
smp_desc.mag_filter = SG_FILTER_LINEAR;
smp_desc.mipmap_filter = SG_FILTER_LINEAR;
for (int i = 0; i < 4; i++) {
smp_desc.max_anisotropy = 1<<i;
Expand Down Expand Up @@ -205,7 +206,7 @@ static EM_BOOL draw(double time, void* userdata) {
vs_params_t vs_params;
hmm_mat4 rm = HMM_Rotate(state.r, HMM_Vec3(1.0f, 0.0f, 0.0f));

sg_begin_default_pass(&(sg_pass_action){0}, emsc_width(), emsc_height());
sg_begin_pass(&(sg_pass){ .swapchain = emsc_swapchain() });
sg_apply_pipeline(state.pip);
for (int i = 0; i < 12; i++) {
const float x = ((float)(i & 3) - 1.5f) * 2.0f;
Expand Down
33 changes: 20 additions & 13 deletions html5/mrt-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
static struct {
float rx, ry;
struct {
sg_pass_desc pass_desc;
sg_pass pass;
sg_attachments_desc attachments_desc;
sg_attachments attachments;
sg_pass_action pass_action;
sg_pipeline pip;
sg_bindings bind;
Expand Down Expand Up @@ -51,6 +51,7 @@ int main() {

// setup sokol_gfx
sg_desc desc = {
.environment = emsc_environment(),
.logger.func = slog_func,
};
sg_setup(&desc);
Expand Down Expand Up @@ -79,20 +80,20 @@ int main() {
.pixel_format = SG_PIXELFORMAT_DEPTH,
.sample_count = offscreen_sample_count
};
state.offscreen.pass_desc = (sg_pass_desc){
.color_attachments = {
state.offscreen.attachments_desc = (sg_attachments_desc){
.colors = {
[0].image = sg_make_image(&color_img_desc),
[1].image = sg_make_image(&color_img_desc),
[2].image = sg_make_image(&color_img_desc)
},
.resolve_attachments = {
.resolves = {
[0].image = sg_make_image(&resolve_img_desc),
[1].image = sg_make_image(&resolve_img_desc),
[2].image = sg_make_image(&resolve_img_desc)
},
.depth_stencil_attachment.image = sg_make_image(&depth_img_desc)
.depth_stencil.image = sg_make_image(&depth_img_desc)
};
state.offscreen.pass = sg_make_pass(&state.offscreen.pass_desc);
state.offscreen.attachments = sg_make_attachments(&state.offscreen.attachments_desc);

// a matching pass action with clear colors
state.offscreen.pass_action = (sg_pass_action) {
Expand Down Expand Up @@ -248,9 +249,9 @@ int main() {
.vertex_buffers[0] = quad_buf,
.fs = {
.images = {
[0] = state.offscreen.pass_desc.resolve_attachments[0].image,
[1] = state.offscreen.pass_desc.resolve_attachments[1].image,
[2] = state.offscreen.pass_desc.resolve_attachments[2].image,
[0] = state.offscreen.attachments_desc.resolves[0].image,
[1] = state.offscreen.attachments_desc.resolves[1].image,
[2] = state.offscreen.attachments_desc.resolves[2].image,
},
.samplers[0] = smp,
}
Expand Down Expand Up @@ -391,7 +392,10 @@ static EM_BOOL draw(double time, void* userdata) {
};

// render cube into MRT offscreen render targets
sg_begin_pass(state.offscreen.pass, &state.offscreen.pass_action);
sg_begin_pass(&(sg_pass){
.action = state.offscreen.pass_action,
.attachments = state.offscreen.attachments,
});
sg_apply_pipeline(state.offscreen.pip);
sg_apply_bindings(&state.offscreen.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(offscreen_params));
Expand All @@ -400,15 +404,18 @@ static EM_BOOL draw(double time, void* userdata) {

// render fullscreen quad with the 'composed image', plus 3 small
// debug-views with the content of the offscreen render targets
sg_begin_default_pass(&state.display.pass_action, emsc_width(), emsc_height());
sg_begin_pass(&(sg_pass){
.action = state.display.pass_action,
.swapchain = emsc_swapchain()
});
sg_apply_pipeline(state.display.fsq_pip);
sg_apply_bindings(&state.display.fsq_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(params));
sg_draw(0, 4, 1);
sg_apply_pipeline(state.display.dbg_pip);
for (int i = 0; i < 3; i++) {
sg_apply_viewport(i*100, 0, 100, 100, false);
state.display.dbg_bind.fs.images[0] = state.offscreen.pass_desc.resolve_attachments[i].image;
state.display.dbg_bind.fs.images[0] = state.offscreen.attachments_desc.resolves[i].image;
sg_apply_bindings(&state.display.dbg_bind);
sg_draw(0, 4, 1);
}
Expand Down
7 changes: 5 additions & 2 deletions html5/noninterleaved-emsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ int main() {
emsc_init("#canvas", EMSC_ANTIALIAS);

// setup sokol_gfx
sg_setup(&(sg_desc){ .logger.func = slog_func });
sg_setup(&(sg_desc){
.environment = emsc_environment(),
.logger.func = slog_func
});
assert(sg_isvalid());

// cube vertex buffer
Expand Down Expand Up @@ -162,7 +165,7 @@ static EM_BOOL draw(double time, void* userdata) {
};

// ...and draw
sg_begin_default_pass(&state.pass_action, emsc_width(), emsc_height());
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));
Expand Down
Loading

0 comments on commit a7a03d3

Please sign in to comment.