Skip to content

Commit

Permalink
fix texcube-wgpu for bindings cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Oct 1, 2024
1 parent d05406b commit a3bb2d0
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions wgpu/texcube-wgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,50 +103,60 @@ static void init(void) {
0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF, 0xFF000000,
0xFF000000, 0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF,
};
state.bind.fs.images[0] = sg_make_image(&(sg_image_desc){
state.bind.images[0] = sg_make_image(&(sg_image_desc){
.width = 4,
.height = 4,
.data.subimage[0][0] = SG_RANGE(pixels),
.label = "cube-texture"
});
state.bind.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){
state.bind.samplers[0] = sg_make_sampler(&(sg_sampler_desc){
.min_filter = SG_FILTER_NEAREST,
.mag_filter = SG_FILTER_NEAREST,
});

// create shader with WGSL source code
const sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vs = {
.uniform_blocks[0].size = sizeof(vs_params_t),
.source =
"struct vs_params {\n"
" mvp: mat4x4f,\n"
"}\n"
"@group(0) @binding(0) var<uniform> in: vs_params;\n"
"struct vs_out {\n"
" @builtin(position) pos: vec4f,\n"
" @location(0) color: vec4f,\n"
" @location(1) uv: vec2f,\n"
"}\n"
"@vertex fn main(@location(0) pos: vec4f, @location(1) color: vec4f, @location(2) uv: vec2f) -> vs_out {\n"
" var out: vs_out;\n"
" out.pos = in.mvp * pos;\n"
" out.color = color;\n"
" out.uv = uv * 5.0;\n"
" return out;\n"
"}\n",
.vertex_func.source =
"struct vs_params {\n"
" mvp: mat4x4f,\n"
"}\n"
"@group(0) @binding(0) var<uniform> in: vs_params;\n"
"struct vs_out {\n"
" @builtin(position) pos: vec4f,\n"
" @location(0) color: vec4f,\n"
" @location(1) uv: vec2f,\n"
"}\n"
"@vertex fn main(@location(0) pos: vec4f, @location(1) color: vec4f, @location(2) uv: vec2f) -> vs_out {\n"
" var out: vs_out;\n"
" out.pos = in.mvp * pos;\n"
" out.color = color;\n"
" out.uv = uv * 5.0;\n"
" return out;\n"
"}\n",
.fragment_func.source =
"@group(1) @binding(0) var tex: texture_2d<f32>;\n"
"@group(1) @binding(1) var smp: sampler;\n"
"@fragment fn main(@location(0) color: vec4f, @location(1) uv: vec2f) -> @location(0) vec4f {\n"
" return textureSample(tex, smp, uv) * color;\n"
"}\n",
.uniform_blocks[0] = {
.stage = SG_SHADERSTAGE_VERTEX,
.size = sizeof(vs_params_t),
.wgsl_group0_binding_n = 0,
},
.images[0] = {
.stage = SG_SHADERSTAGE_FRAGMENT,
.wgsl_group1_binding_n = 0,
},
.samplers[0] = {
.stage = SG_SHADERSTAGE_FRAGMENT,
.wgsl_group1_binding_n = 1,
},
.image_sampler_pairs[0] = {
.stage = SG_SHADERSTAGE_FRAGMENT,
.image_slot = 0,
.sampler_slot = 0,
},
.fs = {
.images[0].used = true,
.samplers[0].used = true,
.image_sampler_pairs[0] = { .used = true, .image_slot = 0, .sampler_slot = 0 },
.source =
"@group(1) @binding(48) var tex: texture_2d<f32>;\n"
"@group(1) @binding(64) var smp: sampler;\n"
"@fragment fn main(@location(0) color: vec4f, @location(1) uv: vec2f) -> @location(0) vec4f {\n"
" return textureSample(tex, smp, uv) * color;\n"
"}\n",
}
});

// a pipeline state object
Expand Down Expand Up @@ -190,7 +200,7 @@ static void frame(void) {
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = wgpu_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

0 comments on commit a3bb2d0

Please sign in to comment.