|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// customresolve-sapp.c |
| 3 | +// |
| 4 | +// Demonstrate custom MSAA resolve in a render pass which reads individual |
| 5 | +// MSAA samples in the fragment shader. |
| 6 | +//------------------------------------------------------------------------------ |
| 7 | +#include "sokol_app.h" |
| 8 | +#include "sokol_gfx.h" |
| 9 | +#include "sokol_log.h" |
| 10 | +#include "sokol_glue.h" |
| 11 | +#define SOKOL_IMGUI_IMPL |
| 12 | +#define SOKOL_GFX_IMGUI_IMPL |
| 13 | +#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS |
| 14 | +#include "cimgui/cimgui.h" |
| 15 | +#include "sokol_imgui.h" |
| 16 | +#include "sokol_gfx_imgui.h" |
| 17 | +#include "customresolve-sapp.glsl.h" |
| 18 | + |
| 19 | +#define WIDTH (160) |
| 20 | +#define HEIGHT (120) |
| 21 | + |
| 22 | +static struct { |
| 23 | + struct { |
| 24 | + sg_image img; |
| 25 | + sg_pipeline pip; |
| 26 | + sg_attachments atts; |
| 27 | + sg_pass_action action; |
| 28 | + } msaa; |
| 29 | + struct { |
| 30 | + sg_image img; |
| 31 | + sg_pipeline pip; |
| 32 | + sg_attachments atts; |
| 33 | + sg_pass_action action; |
| 34 | + sg_bindings bind; |
| 35 | + fs_params_t fs_params; |
| 36 | + } resolve; |
| 37 | + struct { |
| 38 | + sg_pipeline pip; |
| 39 | + sg_pass_action action; |
| 40 | + sg_bindings bind; |
| 41 | + } display; |
| 42 | + struct { |
| 43 | + sgimgui_t sgimgui; |
| 44 | + } ui; |
| 45 | + sg_sampler smp; // a common non-filtering sampler |
| 46 | +} state; |
| 47 | + |
| 48 | +const fs_params_t default_weights = { |
| 49 | + .weight0 = 0.25f, |
| 50 | + .weight1 = 0.25f, |
| 51 | + .weight2 = 0.25f, |
| 52 | + .weight3 = 0.25f, |
| 53 | +}; |
| 54 | + |
| 55 | +static void draw_fallback(void); |
| 56 | +static void draw_ui(void); |
| 57 | + |
| 58 | +static void init(void) { |
| 59 | + sg_setup(&(sg_desc){ |
| 60 | + .environment = sglue_environment(), |
| 61 | + .logger.func = slog_func, |
| 62 | + }); |
| 63 | + simgui_setup(&(simgui_desc_t){ .logger.func = slog_func }); |
| 64 | + sgimgui_init(&state.ui.sgimgui, &(sgimgui_desc_t){0}); |
| 65 | + |
| 66 | + // catch WebGL2/GLES3 |
| 67 | + if (!sg_query_features().msaa_image_bindings) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // common objects |
| 72 | + state.smp = sg_make_sampler(&(sg_sampler_desc){ |
| 73 | + .min_filter = SG_FILTER_NEAREST, |
| 74 | + .mag_filter = SG_FILTER_NEAREST, |
| 75 | + }); |
| 76 | + |
| 77 | + // msaa-render-pass objects |
| 78 | + state.msaa.img = sg_make_image(&(sg_image_desc){ |
| 79 | + .render_target = true, |
| 80 | + .width = WIDTH, |
| 81 | + .height = HEIGHT, |
| 82 | + .pixel_format = SG_PIXELFORMAT_RGBA8, |
| 83 | + .sample_count = 4, |
| 84 | + .label = "msaa image", |
| 85 | + }); |
| 86 | + state.msaa.pip = sg_make_pipeline(&(sg_pipeline_desc){ |
| 87 | + .shader = sg_make_shader(msaa_shader_desc(sg_query_backend())), |
| 88 | + .sample_count = 4, |
| 89 | + .depth.pixel_format = SG_PIXELFORMAT_NONE, |
| 90 | + .colors[0].pixel_format = SG_PIXELFORMAT_RGBA8, |
| 91 | + .label = "msaa pipeline", |
| 92 | + }); |
| 93 | + state.msaa.atts = sg_make_attachments(&(sg_attachments_desc){ |
| 94 | + .colors[0].image = state.msaa.img, |
| 95 | + .label = "msaa pass attachments", |
| 96 | + }); |
| 97 | + state.msaa.action = (sg_pass_action){ |
| 98 | + .colors[0] = { |
| 99 | + .load_action = SG_LOADACTION_CLEAR, |
| 100 | + .store_action = SG_STOREACTION_STORE, |
| 101 | + .clear_value = { 0, 0, 0, 1 }, |
| 102 | + }, |
| 103 | + }; |
| 104 | + |
| 105 | + // resolve-render-pass objects |
| 106 | + state.resolve.img = sg_make_image(&(sg_image_desc){ |
| 107 | + .render_target = true, |
| 108 | + .width = WIDTH, |
| 109 | + .height = HEIGHT, |
| 110 | + .pixel_format = SG_PIXELFORMAT_RGBA8, |
| 111 | + .sample_count = 1, |
| 112 | + .label = "resolve image", |
| 113 | + }); |
| 114 | + state.resolve.pip = sg_make_pipeline(&(sg_pipeline_desc){ |
| 115 | + .shader = sg_make_shader(resolve_shader_desc(sg_query_backend())), |
| 116 | + .sample_count = 1, |
| 117 | + .depth.pixel_format = SG_PIXELFORMAT_NONE, |
| 118 | + .colors[0].pixel_format = SG_PIXELFORMAT_RGBA8, |
| 119 | + .label = "resolve pipeline", |
| 120 | + }); |
| 121 | + state.resolve.atts = sg_make_attachments(&(sg_attachments_desc){ |
| 122 | + .colors[0].image = state.resolve.img, |
| 123 | + .label = "resolve pass attachments", |
| 124 | + }); |
| 125 | + state.resolve.action = (sg_pass_action){ |
| 126 | + .colors[0] = { |
| 127 | + .load_action = SG_LOADACTION_DONTCARE, |
| 128 | + .store_action = SG_STOREACTION_STORE |
| 129 | + }, |
| 130 | + }; |
| 131 | + state.resolve.bind = (sg_bindings){ |
| 132 | + .images[IMG_texms] = state.msaa.img, |
| 133 | + .samplers[SMP_smp] = state.smp, |
| 134 | + }; |
| 135 | + state.resolve.fs_params = default_weights; |
| 136 | + |
| 137 | + // swapchain-render-pass objects |
| 138 | + state.display.pip = sg_make_pipeline(&(sg_pipeline_desc){ |
| 139 | + .shader = sg_make_shader(display_shader_desc(sg_query_backend())), |
| 140 | + .label = "display pipeline", |
| 141 | + }); |
| 142 | + state.display.action = (sg_pass_action){ |
| 143 | + .colors[0] = { .load_action = SG_LOADACTION_DONTCARE } |
| 144 | + }; |
| 145 | + state.display.bind = (sg_bindings){ |
| 146 | + .images[IMG_tex] = state.resolve.img, |
| 147 | + .samplers[SMP_smp] = state.smp, |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +static void frame(void) { |
| 152 | + draw_ui(); |
| 153 | + if (!sg_query_features().msaa_image_bindings) { |
| 154 | + draw_fallback(); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + // draw a triangle into an msaa render target |
| 159 | + sg_begin_pass(&(sg_pass){ .action = state.msaa.action, .attachments = state.msaa.atts }); |
| 160 | + sg_apply_pipeline(state.msaa.pip); |
| 161 | + sg_draw(0, 3, 1); |
| 162 | + sg_end_pass(); |
| 163 | + |
| 164 | + // custom resolve pass (via a 'fullscreen triangle') |
| 165 | + sg_begin_pass(&(sg_pass){ .action = state.resolve.action, .attachments = state.resolve.atts }); |
| 166 | + sg_apply_pipeline(state.resolve.pip); |
| 167 | + sg_apply_bindings(&state.resolve.bind); |
| 168 | + sg_apply_uniforms(UB_fs_params, &SG_RANGE(state.resolve.fs_params)); |
| 169 | + sg_draw(0, 3, 1); |
| 170 | + sg_end_pass(); |
| 171 | + |
| 172 | + // the final swapchain pass (also via a 'fullscreen triangle') |
| 173 | + sg_begin_pass(&(sg_pass){ .action = state.display.action, .swapchain = sglue_swapchain() }); |
| 174 | + sg_apply_pipeline(state.display.pip); |
| 175 | + sg_apply_bindings(&state.display.bind); |
| 176 | + sg_draw(0, 3, 1); |
| 177 | + simgui_render(); |
| 178 | + sg_end_pass(); |
| 179 | + sg_commit(); |
| 180 | +} |
| 181 | + |
| 182 | +static void cleanup(void) { |
| 183 | + sgimgui_discard(&state.ui.sgimgui); |
| 184 | + simgui_shutdown(); |
| 185 | + sg_shutdown(); |
| 186 | +} |
| 187 | + |
| 188 | +static void draw_ui(void) { |
| 189 | + simgui_new_frame(&(simgui_frame_desc_t){ |
| 190 | + .width = sapp_width(), |
| 191 | + .height = sapp_height(), |
| 192 | + .dpi_scale = sapp_dpi_scale(), |
| 193 | + .delta_time = sapp_frame_duration(), |
| 194 | + }); |
| 195 | + if (igBeginMainMenuBar()) { |
| 196 | + sgimgui_draw_menu(&state.ui.sgimgui, "sokol-gfx"); |
| 197 | + igEndMainMenuBar(); |
| 198 | + } |
| 199 | + sgimgui_draw(&state.ui.sgimgui); |
| 200 | + |
| 201 | + igSetNextWindowPos((ImVec2){10, 20}, ImGuiCond_Once, (ImVec2){0,0}); |
| 202 | + if (igBegin("#window", 0, ImGuiWindowFlags_NoDecoration|ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoBackground)) { |
| 203 | + if (sg_query_features().msaa_image_bindings) { |
| 204 | + igText("Sample Weights:"); |
| 205 | + igSliderFloat("0", &state.resolve.fs_params.weight0, 0.0f, 1.0f, "%.2f", 0); |
| 206 | + igSliderFloat("1", &state.resolve.fs_params.weight1, 0.0f, 1.0f, "%.2f", 0); |
| 207 | + igSliderFloat("2", &state.resolve.fs_params.weight2, 0.0f, 1.0f, "%.2f", 0); |
| 208 | + igSliderFloat("3", &state.resolve.fs_params.weight3, 0.0f, 1.0f, "%.2f", 0); |
| 209 | + igCheckboxFlags_IntPtr("show complex pixels", &state.resolve.fs_params.coverage, 1); |
| 210 | + if (igButton("Reset", (ImVec2){0,0})) { |
| 211 | + state.resolve.fs_params = default_weights; |
| 212 | + } |
| 213 | + } else { |
| 214 | + igText("MSAA TEXTURES NOT SUPPORTED ON WEBGL2/GLES3/macOS+GL"); |
| 215 | + } |
| 216 | + } |
| 217 | + igEnd(); |
| 218 | +} |
| 219 | + |
| 220 | +static void draw_fallback(void) { |
| 221 | + sg_begin_pass(&(sg_pass){ |
| 222 | + .action = { |
| 223 | + .colors[0] = { .load_action = SG_LOADACTION_CLEAR, .clear_value = { 0.5f, 0, 0, 1} }, |
| 224 | + }, |
| 225 | + .swapchain = sglue_swapchain(), |
| 226 | + }); |
| 227 | + simgui_render(); |
| 228 | + sg_end_pass(); |
| 229 | + sg_commit(); |
| 230 | +} |
| 231 | + |
| 232 | +static void input(const sapp_event* ev) { |
| 233 | + simgui_handle_event(ev); |
| 234 | +} |
| 235 | + |
| 236 | +sapp_desc sokol_main(int argc, char* argv[]) { |
| 237 | + (void)argc; (void)argv; |
| 238 | + return (sapp_desc){ |
| 239 | + .init_cb = init, |
| 240 | + .frame_cb = frame, |
| 241 | + .cleanup_cb = cleanup, |
| 242 | + .event_cb = input, |
| 243 | + .width = 640, |
| 244 | + .height = 480, |
| 245 | + .sample_count = 1, |
| 246 | + .window_title = "customresolve-sapp.c", |
| 247 | + .icon.sokol_default = true, |
| 248 | + .logger.func = slog_func, |
| 249 | + }; |
| 250 | +} |
0 commit comments