Skip to content

Commit

Permalink
fix cube-wgpu sample
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Aug 13, 2023
1 parent f6763a8 commit d1f80d7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 55 deletions.
11 changes: 5 additions & 6 deletions wgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ fips_begin_app(bufferoffsets-wgpu windowed)
fips_deps(wgpu_entry)
fips_end_app()

#fips_begin_app(cube-wgpu windowed)
# fips_files(cube-wgpu.c)
# sokol_shader(cube-wgpu.glsl wgpu)
# fips_deps(wgpu_entry)
#fips_end_app()
#
fips_begin_app(cube-wgpu windowed)
fips_files(cube-wgpu.c)
fips_deps(wgpu_entry)
fips_end_app()

#fips_begin_app(noninterleaved-wgpu windowed)
# fips_files(noninterleaved-wgpu.c)
# sokol_shader(noninterleaved-wgpu.glsl wgpu)
Expand Down
2 changes: 1 addition & 1 deletion wgpu/HandmadeMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ HMM_Power(float Base, int Exponent)
{
float Result = 1.0f;
float Mul = Exponent < 0 ? 1.f / Base : Base;
unsigned int X = Exponent < 0 ? -Exponent : Exponent;
int X = Exponent < 0 ? -Exponent : Exponent;
while (X)
{
if (X & 1)
Expand Down
67 changes: 47 additions & 20 deletions wgpu/cube-wgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// cube-wgpu.c
// Rendering with uniform updates.
//------------------------------------------------------------------------------
#include "wgpu_entry.h"
#define HANDMADE_MATH_IMPLEMENTATION
#define HANDMADE_MATH_NO_SSE
#include "HandmadeMath.h"
#define SOKOL_IMPL
#define SOKOL_WGPU
#include "sokol_gfx.h"
#include "sokol_log.h"
#include "wgpu_entry.h"
#include "cube-wgpu.glsl.h"

#define SAMPLE_COUNT (4)

Expand All @@ -21,18 +20,22 @@ static struct {
sg_bindings bind;
} state = {
.pass_action = {
.colors[0] = { .action = SG_ACTION_CLEAR, .value = { 0.25f, 0.5f, 0.75f, 1.0f } }
.colors[0] = { .load_action = SG_LOADACTION_CLEAR, .clear_value = { 0.25f, 0.5f, 0.75f, 1.0f } }
}
};

typedef struct {
hmm_mat4 mvp;
} vs_params_t;

void init(void) {
sg_setup(&(sg_desc){
.context = wgpu_get_context(),
.logger.func = slog_func,
});

/* cube vertex buffer */
float vertices[] = {
// cube vertex buffer
const float vertices[] = {
-1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
Expand Down Expand Up @@ -68,8 +71,8 @@ void init(void) {
.label = "cube-vertices"
});

/* create an index buffer for the cube */
uint16_t indices[] = {
// create an index buffer for the cube
const uint16_t indices[] = {
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
Expand All @@ -83,17 +86,42 @@ void init(void) {
.label = "cube-indices"
});

/* create shader */
sg_shader shd = sg_make_shader(cube_shader_desc(sg_query_backend()));
// create shader
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vs = {
.uniform_blocks = {
[0] = { .size = 16 * 4 },
},
.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"
"};\n"
"@vertex fn main(@location(0) pos: vec4f, @location(1) color: vec4f) -> vs_out {\n"
" var out: vs_out;\n"
" out.pos = in.mvp * 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",
});

/* create pipeline object */
// create pipeline object
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
/* test to provide buffer stride, but no attr offsets */
// 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
[0].format = SG_VERTEXFORMAT_FLOAT3,
[1].format = SG_VERTEXFORMAT_FLOAT4
}
},
.shader = shd,
Expand All @@ -106,16 +134,14 @@ void init(void) {
.label = "cube-pipeline"
});

/* setup resource bindings */
// setup resource bindings
state.bind = (sg_bindings) {
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf
};
}

void frame(void) {
/* NOTE: the vs_params_t struct has been code-generated by the shader-code-gen */
vs_params_t vs_params;
const float w = (float) wgpu_width();
const float h = (float) wgpu_height();
hmm_mat4 proj = HMM_Perspective(60.0f, w/h, 0.01f, 10.0f);
Expand All @@ -125,12 +151,13 @@ void frame(void) {
hmm_mat4 rxm = HMM_Rotate(state.rx, HMM_Vec3(1.0f, 0.0f, 0.0f));
hmm_mat4 rym = HMM_Rotate(state.ry, HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 model = HMM_MultiplyMat4(rxm, rym);
vs_params.mvp = HMM_MultiplyMat4(view_proj, model);

sg_begin_default_pass(&state.pass_action, (int)w, (int)h);
const vs_params_t vs_params = {
.mvp = HMM_MultiplyMat4(view_proj, model)
};
sg_begin_default_passf(&state.pass_action, w, h);
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(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
sg_end_pass();
sg_commit();
Expand Down
28 changes: 0 additions & 28 deletions wgpu/cube-wgpu.glsl

This file was deleted.

1 change: 1 addition & 0 deletions wgpu/wgpu_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ void wgpu_start(const wgpu_desc_t* desc) {

wgpuDeviceSetUncapturedErrorCallback(state.device, error_cb, 0);
wgpuDeviceSetLoggingCallback(state.device, logging_cb, 0);
wgpuDeviceSetDeviceLostCallback(state.device, 0, 0);
wgpuDevicePushErrorScope(state.device, WGPUErrorFilter_Validation);

#if !__EMSCRIPTEN__
Expand Down

0 comments on commit d1f80d7

Please sign in to comment.