diff --git a/wgpu/CMakeLists.txt b/wgpu/CMakeLists.txt index a465e626..7deb5782 100644 --- a/wgpu/CMakeLists.txt +++ b/wgpu/CMakeLists.txt @@ -21,12 +21,11 @@ fips_begin_app(quad-wgpu windowed) fips_deps(wgpu_entry) fips_end_app() -#fips_begin_app(bufferoffsets-wgpu windowed) -# fips_files(bufferoffsets-wgpu.c) -# sokol_shader(bufferoffsets-wgpu.glsl wgpu) -# fips_deps(wgpu_entry) -#fips_end_app() -# +fips_begin_app(bufferoffsets-wgpu windowed) + fips_files(bufferoffsets-wgpu.c) + fips_deps(wgpu_entry) +fips_end_app() + #fips_begin_app(cube-wgpu windowed) # fips_files(cube-wgpu.c) # sokol_shader(cube-wgpu.glsl wgpu) diff --git a/wgpu/bufferoffsets-wgpu.c b/wgpu/bufferoffsets-wgpu.c index 408f0e7c..68e5a421 100644 --- a/wgpu/bufferoffsets-wgpu.c +++ b/wgpu/bufferoffsets-wgpu.c @@ -3,12 +3,11 @@ // Render separate geometries in vertex- and index-buffers with // buffer offsets. //------------------------------------------------------------------------------ +#include "wgpu_entry.h" #define SOKOL_IMPL #define SOKOL_WGPU #include "sokol_gfx.h" #include "sokol_log.h" -#include "wgpu_entry.h" -#include "bufferoffsets-wgpu.glsl.h" static struct { sg_pass_action pass_action; @@ -17,8 +16,8 @@ static struct { } state = { .pass_action = { .colors[0] = { - .action = SG_ACTION_CLEAR, - .value = { 0.5f, 0.5f, 1.0f, 1.0f } + .load_action = SG_LOADACTION_CLEAR, + .clear_value = { 0.5f, 0.5f, 1.0f, 1.0f } } } }; @@ -33,20 +32,19 @@ static void init(void) { .logger.func = slog_func, }); - /* a 2D triangle and quad in 1 vertex buffer and 1 index buffer */ - vertex_t vertices[7] = { - /* triangle */ + // a 2D triangle and quad in 1 vertex buffer and 1 index buffer + const vertex_t vertices[7] = { + // triangle { 0.0f, 0.55f, 1.0f, 0.0f, 0.0f }, { 0.25f, 0.05f, 0.0f, 1.0f, 0.0f }, { -0.25f, 0.05f, 0.0f, 0.0f, 1.0f }, - - /* quad */ + // quad { -0.25f, -0.05f, 0.0f, 0.0f, 1.0f }, { 0.25f, -0.05f, 0.0f, 1.0f, 0.0f }, { 0.25f, -0.55f, 1.0f, 0.0f, 0.0f }, { -0.25f, -0.55f, 1.0f, 1.0f, 0.0f } }; - uint16_t indices[9] = { + const uint16_t indices[9] = { 0, 1, 2, 0, 1, 2, 0, 2, 3 }; @@ -58,9 +56,28 @@ static void init(void) { .data = SG_RANGE(indices) }); - /* a shader and pipeline to render 2D shapes */ + // a shader object with wgsl code + sg_shader shd = sg_make_shader(&(sg_shader_desc){ + .vs.source = + "struct vs_out {\n" + " @builtin(position) pos: vec4f,\n" + " @location(0) color: vec4f,\n" + "}\n" + "@vertex fn main(@location(0) pos: vec4f, @location(1) color: vec4f) -> vs_out {\n" + " var out: vs_out;\n" + " out.pos = pos;\n" + " out.color = color;\n" + " return out;\n" + "}\n", + .fs.source = + "@fragment fn main(@location(0) color: vec4f) -> @location(0) vec4f {\n" + " return color;\n" + "}\n", + }); + + // a shader and pipeline to render 2D shapes state.pip = sg_make_pipeline(&(sg_pipeline_desc){ - .shader = sg_make_shader(bufferoffsets_shader_desc(sg_query_backend())), + .shader = shd, .index_type = SG_INDEXTYPE_UINT16, .layout = { .attrs = { @@ -74,12 +91,12 @@ static void init(void) { static void frame(void) { sg_begin_default_pass(&state.pass_action, wgpu_width(), wgpu_height()); sg_apply_pipeline(state.pip); - /* render the triangle */ + // render the triangle state.bind.vertex_buffer_offsets[0] = 0; state.bind.index_buffer_offset = 0; sg_apply_bindings(&state.bind); sg_draw(0, 3, 1); - /* render the quad */ + // render the quad state.bind.vertex_buffer_offsets[0] = 3 * sizeof(vertex_t); state.bind.index_buffer_offset = 3 * sizeof(uint16_t); sg_apply_bindings(&state.bind); diff --git a/wgpu/bufferoffsets-wgpu.glsl b/wgpu/bufferoffsets-wgpu.glsl deleted file mode 100644 index 57007bae..00000000 --- a/wgpu/bufferoffsets-wgpu.glsl +++ /dev/null @@ -1,23 +0,0 @@ -@vs vs -layout(location=0) in vec4 position; -layout(location=1) in vec4 color0; - -out vec4 color; - -void main() { - gl_Position = position; - color = color0; -} -@end - -@fs fs -in vec4 color; -out vec4 frag_color; - -void main() { - frag_color = color; -} -@end - -@program bufferoffsets vs fs -