Skip to content

Commit

Permalink
fix sapp samples for bindings cleanup wip
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Oct 6, 2024
1 parent 5007ea3 commit c043eed
Show file tree
Hide file tree
Showing 24 changed files with 143 additions and 159 deletions.
12 changes: 5 additions & 7 deletions sapp/arraytex-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ void init(void) {
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[ATTR_vs_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_texcoord0].format = SG_VERTEXFORMAT_FLOAT2
[ATTR_arraytex_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_arraytex_texcoord0].format = SG_VERTEXFORMAT_FLOAT2
}
},
.shader = sg_make_shader(arraytex_shader_desc(sg_query_backend())),
Expand All @@ -144,10 +144,8 @@ void init(void) {
state.bind = (sg_bindings) {
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf,
.fs = {
.images[SLOT_tex] = img,
.samplers[SLOT_smp] = smp,
}
.images[IMG_arraytex_tex] = img,
.samplers[SMP_arraytex_smp] = smp,
};
}

Expand Down Expand Up @@ -175,7 +173,7 @@ void frame(void) {
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_apply_uniforms(UB_arraytex_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
__dbgui_draw();
sg_end_pass();
Expand Down
10 changes: 5 additions & 5 deletions sapp/blend-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void init(void) {
.layout = {
.buffers[0].stride = 28,
.attrs = {
[ATTR_vs_bg_position].format=SG_VERTEXFORMAT_FLOAT2
[ATTR_bg_position].format=SG_VERTEXFORMAT_FLOAT2
}
},
.shader = bg_shd,
Expand All @@ -77,8 +77,8 @@ void init(void) {
sg_pipeline_desc pip_desc = {
.layout = {
.attrs = {
[ATTR_vs_quad_position].format=SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_quad_color0].format=SG_VERTEXFORMAT_FLOAT4
[ATTR_quad_position].format=SG_VERTEXFORMAT_FLOAT3,
[ATTR_quad_color0].format=SG_VERTEXFORMAT_FLOAT4
}
},
.shader = quad_shd,
Expand Down Expand Up @@ -114,7 +114,7 @@ void frame(void) {
/* draw a background quad */
sg_apply_pipeline(state.bg_pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_FS, SLOT_bg_fs_params, &SG_RANGE(state.bg_fs_params));
sg_apply_uniforms(UB_bg_bg_fs_params, &SG_RANGE(state.bg_fs_params));
sg_draw(0, 4, 1);

// draw the blended quads
Expand All @@ -130,7 +130,7 @@ void frame(void) {

sg_apply_pipeline(state.pips[src][dst]);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_quad_vs_params, &SG_RANGE(state.quad_vs_params));
sg_apply_uniforms(UB_quad_quad_vs_params, &SG_RANGE(state.quad_vs_params));
sg_draw(0, 4, 1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions sapp/bufferoffsets-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void init(void) {
.index_type = SG_INDEXTYPE_UINT16,
.layout = {
.attrs = {
[0].format=SG_VERTEXFORMAT_FLOAT2,
[1].format=SG_VERTEXFORMAT_FLOAT3
[ATTR_bufferoffsets_position].format=SG_VERTEXFORMAT_FLOAT2,
[ATTR_bufferoffsets_color0].format=SG_VERTEXFORMAT_FLOAT3
}
},
.label = "pipeline",
Expand Down
6 changes: 3 additions & 3 deletions sapp/cgltf-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,9 @@ static sg_vertex_format gltf_to_vertex_format(cgltf_accessor* acc) {

static int gltf_attr_type_to_vs_input_slot(cgltf_attribute_type attr_type) {
switch (attr_type) {
case cgltf_attribute_type_position: return ATTR_vs_position;
case cgltf_attribute_type_normal: return ATTR_vs_normal;
case cgltf_attribute_type_texcoord: return ATTR_vs_texcoord;
case cgltf_attribute_type_position: return ATTR_cgltf_metallic_position;
case cgltf_attribute_type_normal: return ATTR_cgltf_metallic_normal;
case cgltf_attribute_type_texcoord: return ATTR_cgltf_metallic_texcoord;
default: return SCENE_INVALID_INDEX;
}
}
Expand Down
6 changes: 3 additions & 3 deletions sapp/cube-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ void init(void) {
/* test to provide buffer stride, but no attr offsets */
.buffers[0].stride = 28,
.attrs = {
[ATTR_vs_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_color0].format = SG_VERTEXFORMAT_FLOAT4
[ATTR_cube_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_cube_color0].format = SG_VERTEXFORMAT_FLOAT4
}
},
.shader = shd,
Expand Down Expand Up @@ -132,7 +132,7 @@ void frame(void) {
});
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_apply_uniforms(UB_cube_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
__dbgui_draw();
sg_end_pass();
Expand Down
25 changes: 12 additions & 13 deletions sapp/cubemaprt-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,13 @@ void init(void) {
// vertex- and index-buffers for cube
app.cube = make_cube_mesh();

// same vertex layout for all shaders
sg_vertex_layout_state layout = {
.attrs = {
[ATTR_vs_pos] = { .offset=offsetof(vertex_t, pos), .format=SG_VERTEXFORMAT_FLOAT3 },
[ATTR_vs_norm] = { .offset=offsetof(vertex_t, norm), .format=SG_VERTEXFORMAT_FLOAT3 }
}
};

// shader and pipeline objects for offscreen-rendering
sg_pipeline_desc pip_desc = {
.shader = sg_make_shader(shapes_shader_desc(sg_query_backend())),
.layout = layout,
.layout.attrs = {
[ATTR_shapes_pos] = { .offset = offsetof(vertex_t, pos), .format = SG_VERTEXFORMAT_FLOAT3 },
[ATTR_shapes_norm] = { .offset=offsetof(vertex_t, norm), .format=SG_VERTEXFORMAT_FLOAT3 },
},
.index_type = SG_INDEXTYPE_UINT16,
.cull_mode = SG_CULLMODE_BACK,
.sample_count = OFFSCREEN_SAMPLE_COUNT,
Expand All @@ -161,7 +156,10 @@ void init(void) {
// shader and pipeline objects for display-rendering
app.display_cube_pip = sg_make_pipeline(&(sg_pipeline_desc){
.shader = sg_make_shader(cube_shader_desc(sg_query_backend())),
.layout = layout,
.layout.attrs = {
[ATTR_cube_pos] = { .offset = offsetof(vertex_t, pos), .format = SG_VERTEXFORMAT_FLOAT3 },
[ATTR_cube_norm] = { .offset=offsetof(vertex_t, norm), .format=SG_VERTEXFORMAT_FLOAT3 },
},
.index_type = SG_INDEXTYPE_UINT16,
.cull_mode = SG_CULLMODE_BACK,
.sample_count = DISPLAY_SAMPLE_COUNT,
Expand Down Expand Up @@ -258,7 +256,8 @@ void frame(void) {
sg_apply_bindings(&(sg_bindings){
.vertex_buffers[0] = app.cube.vbuf,
.index_buffer = app.cube.ibuf,
.fs = { .images[SLOT_tex] = app.cubemap, .samplers[SLOT_smp] = app.smp },
.images[IMG_cube_tex] = app.cubemap,
.samplers[SMP_cube_smp] = app.smp,
});
shape_uniforms_t uniforms = {
.mvp = HMM_MultiplyMat4(view_proj, model),
Expand All @@ -267,7 +266,7 @@ void frame(void) {
.light_dir = app.light_dir,
.eye_pos = HMM_Vec4v(eye_pos, 1.0f)
};
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_shape_uniforms, &SG_RANGE(uniforms));
sg_apply_uniforms(UB_cube_shape_uniforms, &SG_RANGE(uniforms));
sg_draw(0, app.cube.num_elements, 1);

__dbgui_draw();
Expand Down Expand Up @@ -312,7 +311,7 @@ static void draw_cubes(sg_pipeline pip, hmm_vec3 eye_pos, hmm_mat4 view_proj) {
.light_dir = app.light_dir,
.eye_pos = HMM_Vec4v(eye_pos, 1.0f)
};
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_shape_uniforms, &SG_RANGE(uniforms));
sg_apply_uniforms(UB_cube_shape_uniforms, &SG_RANGE(uniforms));
sg_draw(0, app.cube.num_elements, 1);
}
}
Expand Down
13 changes: 7 additions & 6 deletions sapp/dyntex-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ void init(void) {
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[ATTR_vs_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_color0].format = SG_VERTEXFORMAT_FLOAT4,
[ATTR_vs_texcoord0].format = SG_VERTEXFORMAT_FLOAT2
[ATTR_dyntex_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_dyntex_color0].format = SG_VERTEXFORMAT_FLOAT4,
[ATTR_dyntex_texcoord0].format = SG_VERTEXFORMAT_FLOAT2
}
},
.shader = shd,
Expand All @@ -131,7 +131,8 @@ void init(void) {
state.bind = (sg_bindings) {
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf,
.fs = { .images[SLOT_tex] = img, .samplers[SLOT_smp] = smp },
.images[IMG_dyntex_tex] = img,
.samplers[SMP_dyntex_smp] = smp,
};

// initialize the game-of-life state
Expand All @@ -155,15 +156,15 @@ void frame(void) {
game_of_life_update();

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

// render the frame
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_apply_uniforms(UB_dyntex_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
__dbgui_draw();
sg_end_pass();
Expand Down
8 changes: 4 additions & 4 deletions sapp/instancing-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ void init(void) {
// vertex buffer at slot 1 must step per instance
.buffers[1].step_func = SG_VERTEXSTEP_PER_INSTANCE,
.attrs = {
[ATTR_vs_pos] = { .format=SG_VERTEXFORMAT_FLOAT3, .buffer_index=0 },
[ATTR_vs_color0] = { .format=SG_VERTEXFORMAT_FLOAT4, .buffer_index=0 },
[ATTR_vs_inst_pos] = { .format=SG_VERTEXFORMAT_FLOAT3, .buffer_index=1 }
[ATTR_instancing_pos] = { .format=SG_VERTEXFORMAT_FLOAT3, .buffer_index=0 },
[ATTR_instancing_color0] = { .format=SG_VERTEXFORMAT_FLOAT4, .buffer_index=0 },
[ATTR_instancing_inst_pos] = { .format=SG_VERTEXFORMAT_FLOAT3, .buffer_index=1 }
}
},
.shader = shd,
Expand Down Expand Up @@ -150,7 +150,7 @@ void frame(void) {
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_apply_uniforms(UB_instancing_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 24, state.cur_num_particles);
__dbgui_draw();
sg_end_pass();
Expand Down
18 changes: 8 additions & 10 deletions sapp/layerrender-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ static void init(void) {
.layout = {
.buffers[0].stride = sizeof(sshape_vertex_t),
.attrs = {
[ATTR_vs_offscreen_in_pos] = sshape_position_vertex_attr_state(),
[ATTR_vs_offscreen_in_nrm] = sshape_normal_vertex_attr_state(),
[ATTR_offscreen_in_pos] = sshape_position_vertex_attr_state(),
[ATTR_offscreen_in_nrm] = sshape_normal_vertex_attr_state(),
},
},
.shader = sg_make_shader(offscreen_shader_desc(sg_query_backend())),
Expand All @@ -143,8 +143,8 @@ static void init(void) {
.layout = {
.buffers[0].stride = sizeof(sshape_vertex_t),
.attrs = {
[ATTR_vs_display_in_pos] = sshape_position_vertex_attr_state(),
[ATTR_vs_display_in_uv] = sshape_texcoord_vertex_attr_state(),
[ATTR_display_in_pos] = sshape_position_vertex_attr_state(),
[ATTR_display_in_uv] = sshape_texcoord_vertex_attr_state(),
},
},
.shader = sg_make_shader(display_shader_desc(sg_query_backend())),
Expand All @@ -165,10 +165,8 @@ static void init(void) {
state.display.bindings = (sg_bindings){
.vertex_buffers[0] = state.vbuf,
.index_buffer = state.ibuf,
.fs = {
.images[SLOT_tex] = state.img,
.samplers[SLOT_smp] = state.smp,
},
.images[IMG_display_tex] = state.img,
.samplers[SMP_display_smp] = state.smp,
};

// initialize pass actions
Expand Down Expand Up @@ -204,7 +202,7 @@ static void frame(void) {
default: ry = -ry; break;
}
const vs_params_t offscreen_vsparams = compute_offscreen_vsparams(rx, ry);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(offscreen_vsparams));
sg_apply_uniforms(UB_offscreen_vs_params, &SG_RANGE(offscreen_vsparams));
const sshape_element_range_t shape = state.offscreen.shapes[i];
sg_draw(shape.base_element, shape.num_elements, 1);
sg_end_pass();
Expand All @@ -214,7 +212,7 @@ static void frame(void) {
sg_begin_pass(&(sg_pass){ .action = state.display.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.display.pip);
sg_apply_bindings(&state.display.bindings);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(display_vsparams));
sg_apply_uniforms(UB_display_vs_params, &SG_RANGE(display_vsparams));
sg_draw(state.display.plane.base_element, state.display.plane.num_elements, 1);
__dbgui_draw();
sg_end_pass();
Expand Down
12 changes: 6 additions & 6 deletions sapp/loadpng-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ static void init(void) {
Any draw calls containing such an "incomplete" image handle
will be silently dropped.
*/
state.bind.fs.images[SLOT_tex] = sg_alloc_image();
state.bind.images[IMG_loadpng_tex] = sg_alloc_image();

// a sampler object
state.bind.fs.samplers[SLOT_smp] = sg_make_sampler(&(sg_sampler_desc){
state.bind.samplers[SMP_loadpng_smp] = sg_make_sampler(&(sg_sampler_desc){
.min_filter = SG_FILTER_LINEAR,
.mag_filter = SG_FILTER_LINEAR,
});
Expand Down Expand Up @@ -129,8 +129,8 @@ static void init(void) {
.shader = sg_make_shader(loadpng_shader_desc(sg_query_backend())),
.layout = {
.attrs = {
[ATTR_vs_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_texcoord0].format = SG_VERTEXFORMAT_SHORT2N
[ATTR_loadpng_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_loadpng_texcoord0].format = SG_VERTEXFORMAT_SHORT2N
}
},
.index_type = SG_INDEXTYPE_UINT16,
Expand Down Expand Up @@ -173,7 +173,7 @@ static void fetch_callback(const sfetch_response_t* response) {
&num_channels, desired_channels);
if (pixels) {
// ok, time to actually initialize the sokol-gfx texture
sg_init_image(state.bind.fs.images[SLOT_tex], &(sg_image_desc){
sg_init_image(state.bind.images[IMG_loadpng_tex], &(sg_image_desc){
.width = png_width,
.height = png_height,
.pixel_format = SG_PIXELFORMAT_RGBA8,
Expand Down Expand Up @@ -216,7 +216,7 @@ static void frame(void) {
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_apply_uniforms(UB_loadpng_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
__dbgui_draw();
sg_end_pass();
Expand Down
10 changes: 5 additions & 5 deletions sapp/mipmap-sapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ void init(void) {
state.pip = sg_make_pipeline(&(sg_pipeline_desc) {
.layout = {
.attrs = {
[ATTR_vs_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_uv0].format = SG_VERTEXFORMAT_FLOAT2
[ATTR_mipmap_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_mipmap_uv0].format = SG_VERTEXFORMAT_FLOAT2
}
},
.shader = sg_make_shader(mipmap_shader_desc(sg_query_backend())),
Expand All @@ -151,7 +151,7 @@ void frame(void) {

sg_bindings bind = {
.vertex_buffers[0] = state.vbuf,
.fs.images[SLOT_tex] = state.img,
.images[IMG_mipmap_tex] = state.img,
};
sg_begin_pass(&(sg_pass){ .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip);
Expand All @@ -161,9 +161,9 @@ void frame(void) {
hmm_mat4 model = HMM_MultiplyMat4(HMM_Translate(HMM_Vec3(x, y, 0.0f)), rm);
vs_params.mvp = HMM_MultiplyMat4(view_proj, model);

bind.fs.samplers[SLOT_smp] = state.smp[i];
bind.samplers[SMP_mipmap_smp] = state.smp[i];
sg_apply_bindings(&bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_apply_uniforms(UB_mipmap_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 4, 1);
}
__dbgui_draw();
Expand Down
Loading

0 comments on commit c043eed

Please sign in to comment.