Skip to content

Commit

Permalink
fix bufferoffsets-wgpu
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Aug 13, 2023
1 parent 80e970e commit f6763a8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 43 deletions.
11 changes: 5 additions & 6 deletions wgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 31 additions & 14 deletions wgpu/bufferoffsets-wgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 }
}
}
};
Expand All @@ -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
};
Expand All @@ -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 = {
Expand All @@ -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);
Expand Down
23 changes: 0 additions & 23 deletions wgpu/bufferoffsets-wgpu.glsl

This file was deleted.

0 comments on commit f6763a8

Please sign in to comment.