Testing WSGL directly

This commit is contained in:
Clément Pons 2025-05-07 17:32:36 +02:00
parent 0059d8fc3b
commit b02b2b02d5
10 changed files with 4237 additions and 34 deletions

View File

@ -21,6 +21,7 @@
"sokol_log.h": "c",
"syslog.h": "c",
"base.h": "c",
"stdarg.h": "c"
"stdarg.h": "c",
"rand.h": "c"
}
}

View File

View File

@ -1,2 +0,0 @@
sokol-shdc --slang=wgsl -i src/shaders/base.glsl -o src/generated/base.h --ifdef
emcc src/main.c -o app.html -sUSE_WEBGPU -sASSERTIONS -sWASM_BIGINT -sALLOW_MEMORY_GROWTH -pthread -I../sokol -I../sokol_gp -I../flecs/distr --shell-file=shell.html -sFILESYSTEM=0 -g

2
debug.sh Normal file
View File

@ -0,0 +1,2 @@
xxd -i src/shaders/sprite.wgsl src/generated/sprite.h
emcc src/main.c -o app.html -sUSE_WEBGPU -sASSERTIONS -sWASM_BIGINT -sALLOW_MEMORY_GROWTH -pthread -I../sokol -I../sokol_gp -I../flecs/distr --shell-file=shell.html -sFILESYSTEM=0 -g

102
src/generated/sprite.h Normal file
View File

@ -0,0 +1,102 @@
unsigned char src_shaders_sprite_wgsl[] = {
0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x43, 0x65, 0x6c, 0x6c, 0x20,
0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x70, 0x6f, 0x73, 0x69, 0x74,
0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x32, 0x66, 0x2c, 0x0d,
0x0a, 0x7d, 0x3b, 0x0d, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20,
0x56, 0x73, 0x49, 0x20, 0x7b, 0x20, 0x2f, 0x2f, 0x56, 0x65, 0x72, 0x74,
0x65, 0x78, 0x20, 0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x69, 0x6e,
0x70, 0x75, 0x74, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x62, 0x75,
0x69, 0x6c, 0x74, 0x69, 0x6e, 0x28, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e,
0x63, 0x65, 0x5f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x29, 0x20, 0x69, 0x6e,
0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x3a, 0x20, 0x75, 0x33, 0x32, 0x2c,
0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x28, 0x30, 0x29, 0x20, 0x70, 0x6f, 0x73, 0x69, 0x74,
0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x76, 0x65, 0x63, 0x32, 0x66, 0x2c, 0x0d,
0x0a, 0x7d, 0x3b, 0x0d, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20,
0x56, 0x73, 0x32, 0x46, 0x73, 0x20, 0x7b, 0x20, 0x2f, 0x2f, 0x56, 0x65,
0x72, 0x74, 0x65, 0x78, 0x20, 0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20,
0x74, 0x6f, 0x20, 0x46, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x20,
0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x40, 0x62, 0x75, 0x69, 0x6c, 0x74, 0x69, 0x6e, 0x28, 0x70, 0x6f, 0x73,
0x69, 0x74, 0x69, 0x6f, 0x6e, 0x29, 0x20, 0x70, 0x6f, 0x73, 0x3a, 0x20,
0x76, 0x65, 0x63, 0x34, 0x66, 0x2c, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30, 0x29,
0x20, 0x40, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x6f, 0x6c, 0x61, 0x74,
0x65, 0x28, 0x66, 0x6c, 0x61, 0x74, 0x29, 0x20, 0x69, 0x6e, 0x73, 0x74,
0x61, 0x6e, 0x63, 0x65, 0x3a, 0x20, 0x75, 0x33, 0x32, 0x2c, 0x0d, 0x0a,
0x7d, 0x3b, 0x0d, 0x0a, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x46,
0x73, 0x4f, 0x20, 0x7b, 0x20, 0x2f, 0x2f, 0x46, 0x72, 0x61, 0x67, 0x6d,
0x65, 0x6e, 0x74, 0x20, 0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x6f,
0x75, 0x74, 0x70, 0x75, 0x74, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x40,
0x62, 0x75, 0x69, 0x6c, 0x74, 0x69, 0x6e, 0x28, 0x66, 0x72, 0x61, 0x67,
0x5f, 0x64, 0x65, 0x70, 0x74, 0x68, 0x29, 0x20, 0x64, 0x65, 0x70, 0x74,
0x68, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x2c, 0x0d, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x40, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x30,
0x29, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x76, 0x65, 0x63,
0x34, 0x66, 0x2c, 0x0d, 0x0a, 0x7d, 0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x66,
0x6e, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x28, 0x69, 0x3a, 0x20, 0x75,
0x33, 0x32, 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x76, 0x65, 0x63, 0x33, 0x66,
0x0d, 0x0a, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74,
0x20, 0x72, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x20, 0x3d, 0x20, 0x66, 0x33,
0x32, 0x28, 0x28, 0x28, 0x69, 0x20, 0x3e, 0x3e, 0x20, 0x20, 0x30, 0x29,
0x20, 0x26, 0x20, 0x30, 0x78, 0x66, 0x66, 0x29, 0x29, 0x3b, 0x0d, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x6c, 0x65, 0x74, 0x20, 0x67, 0x3a, 0x20, 0x66,
0x33, 0x32, 0x20, 0x3d, 0x20, 0x66, 0x33, 0x32, 0x28, 0x28, 0x28, 0x69,
0x20, 0x3e, 0x3e, 0x20, 0x20, 0x38, 0x29, 0x20, 0x26, 0x20, 0x30, 0x78,
0x66, 0x66, 0x29, 0x29, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6c,
0x65, 0x74, 0x20, 0x62, 0x3a, 0x20, 0x66, 0x33, 0x32, 0x20, 0x3d, 0x20,
0x66, 0x33, 0x32, 0x28, 0x28, 0x28, 0x69, 0x20, 0x3e, 0x3e, 0x20, 0x31,
0x36, 0x29, 0x20, 0x26, 0x20, 0x30, 0x78, 0x66, 0x66, 0x29, 0x29, 0x3b,
0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e,
0x20, 0x76, 0x65, 0x63, 0x33, 0x66, 0x28, 0x72, 0x20, 0x2f, 0x20, 0x32,
0x35, 0x35, 0x2c, 0x20, 0x67, 0x20, 0x2f, 0x20, 0x32, 0x35, 0x35, 0x2c,
0x20, 0x62, 0x20, 0x2f, 0x20, 0x32, 0x35, 0x35, 0x29, 0x3b, 0x0d, 0x0a,
0x7d, 0x0d, 0x0a, 0x0d, 0x0a, 0x2f, 0x2f, 0x40, 0x62, 0x69, 0x6e, 0x64,
0x69, 0x6e, 0x67, 0x28, 0x30, 0x29, 0x20, 0x40, 0x67, 0x72, 0x6f, 0x75,
0x70, 0x28, 0x30, 0x29, 0x20, 0x76, 0x61, 0x72, 0x3c, 0x75, 0x6e, 0x69,
0x66, 0x6f, 0x72, 0x6d, 0x3e, 0x20, 0x6d, 0x76, 0x70, 0x3a, 0x20, 0x6d,
0x61, 0x74, 0x34, 0x78, 0x34, 0x66, 0x3b, 0x0d, 0x0a, 0x40, 0x62, 0x69,
0x6e, 0x64, 0x69, 0x6e, 0x67, 0x28, 0x30, 0x29, 0x20, 0x40, 0x67, 0x72,
0x6f, 0x75, 0x70, 0x28, 0x31, 0x29, 0x20, 0x76, 0x61, 0x72, 0x3c, 0x73,
0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x3e, 0x20, 0x63, 0x65, 0x6c, 0x6c,
0x73, 0x3a, 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x3c, 0x43, 0x65, 0x6c,
0x6c, 0x3e, 0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x40, 0x76, 0x65, 0x72, 0x74,
0x65, 0x78, 0x0d, 0x0a, 0x66, 0x6e, 0x20, 0x76, 0x73, 0x5f, 0x6d, 0x61,
0x69, 0x6e, 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x56, 0x73,
0x49, 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x56, 0x73, 0x32, 0x46, 0x73, 0x20,
0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72, 0x20, 0x6f,
0x75, 0x74, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x56, 0x73, 0x32, 0x46, 0x73,
0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74,
0x70, 0x75, 0x74, 0x2e, 0x70, 0x6f, 0x73, 0x20, 0x3d, 0x20, 0x76, 0x65,
0x63, 0x34, 0x66, 0x28, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x5b, 0x69, 0x6e,
0x70, 0x75, 0x74, 0x2e, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65,
0x5d, 0x2e, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x78,
0x79, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x29,
0x2f, 0x2a, 0x20, 0x2a, 0x20, 0x6d, 0x76, 0x70, 0x2a, 0x2f, 0x3b, 0x0d,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e,
0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x20, 0x3d, 0x20, 0x69,
0x6e, 0x70, 0x75, 0x74, 0x2e, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63,
0x65, 0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65,
0x74, 0x75, 0x72, 0x6e, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b,
0x0d, 0x0a, 0x7d, 0x0d, 0x0a, 0x0d, 0x0a, 0x40, 0x66, 0x72, 0x61, 0x67,
0x6d, 0x65, 0x6e, 0x74, 0x0d, 0x0a, 0x66, 0x6e, 0x20, 0x66, 0x73, 0x5f,
0x6d, 0x61, 0x69, 0x6e, 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x3a, 0x20,
0x56, 0x73, 0x32, 0x46, 0x73, 0x29, 0x20, 0x2d, 0x3e, 0x20, 0x46, 0x73,
0x4f, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x61, 0x72,
0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3a, 0x20, 0x46, 0x73, 0x4f,
0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75, 0x74,
0x70, 0x75, 0x74, 0x2e, 0x64, 0x65, 0x70, 0x74, 0x68, 0x20, 0x3d, 0x20,
0x31, 0x2e, 0x30, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6f, 0x75,
0x74, 0x70, 0x75, 0x74, 0x2e, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d,
0x20, 0x76, 0x65, 0x63, 0x34, 0x66, 0x28, 0x31, 0x2e, 0x30, 0x2c, 0x20,
0x30, 0x2e, 0x30, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x2c, 0x20, 0x31, 0x2e,
0x30, 0x29, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x2f, 0x2f, 0x6f,
0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x20,
0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x66, 0x28, 0x63, 0x6f, 0x6c, 0x6f,
0x72, 0x28, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x2e, 0x69, 0x6e, 0x73, 0x74,
0x61, 0x6e, 0x63, 0x65, 0x29, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b,
0x0d, 0x0a, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75,
0x72, 0x6e, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x3b, 0x0d, 0x0a,
0x7d, 0x0d, 0x0a
};
unsigned int src_shaders_sprite_wgsl_len = 1179;

View File

@ -10,12 +10,12 @@
#include "sokol_glue.h"
#include "sokol_log.h"
#include "util/sokol_memtrack.h"
#include "math.h"
#include "rand.h"
#include "generated/base.h"
#include "generated/sprite.h"
#ifndef _STDIO_H
#include <stdio.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
@ -37,11 +37,27 @@ void js_log(int severity, const char* format, ...)
va_end(va);
slog_js_log(severity, buffer);
}
typedef struct position_t
void compute_mvp(Mat4 *ptr, Vec2 pan, float zoom)
{
float x, y;
} position_t;
const int width = sapp_width(), height = sapp_height();
ptr->Elements[0][0] = (2.0f * zoom)/width;
ptr->Elements[0][1] = 0.0f;
ptr->Elements[0][2] = 0.0f;
ptr->Elements[0][3] = 0.0f;
ptr->Elements[1][0] = 0.0f;
ptr->Elements[1][1] = (2.0f * zoom)/height;
ptr->Elements[1][2] = 0.0f;
ptr->Elements[1][3] = 0.0f;
ptr->Elements[2][0] = 0.0f;
ptr->Elements[2][1] = 0.0f;
ptr->Elements[2][2] = 1.0f;
ptr->Elements[2][3] = 0.0f;
ptr->Elements[3][0] = (-2.0f * pan.X * zoom)/width - 1.0f;
ptr->Elements[3][1] = (-2.0f * pan.Y * zoom)/height - 1.0f;
ptr->Elements[3][2] = 0.0f;
ptr->Elements[3][3] = 0.0f;
}
typedef struct renderer_t {
sg_pipeline pipeline;
@ -49,13 +65,14 @@ typedef struct renderer_t {
sg_bindings binding;
sg_range uniform;
sg_pass_action pass;
sg_shader sprite_shader;
} renderer_t;
typedef struct userdata_t
{
position_t pan;
typedef struct userdata_t {
Vec2 pan;
float zoom;
renderer_t renderer;
Mat4 *mvp;
} userdata_t;
// Called on every frame of the application.
@ -70,9 +87,7 @@ static void frame(void* _userdata)
sg_apply_pipeline(userdata->renderer.pipeline);
sg_apply_bindings(&userdata->renderer.binding);
sg_apply_uniforms(UB_vs_uniform, &userdata->renderer.uniform);
sg_apply_uniforms(UB_fs_uniform, &userdata->renderer.uniform);
//sg_apply_uniforms(0, &userdata->renderer.uniform);
sg_draw(0, 4, SAMPLE_COUNT);
@ -83,6 +98,8 @@ static void frame(void* _userdata)
// Called when the application is initializing.
static void init(void* _userdata)
{
rand_seed(1);
userdata_t* userdata = (userdata_t*) _userdata;
// Initialize Sokol GFX.
@ -96,7 +113,7 @@ static void init(void* _userdata)
exit(-1);
}
const position_t quad[4] = {
const Vec2 quad[4] = {
{0.0f, 1.0f}, // bottom left
{1.0f, 1.0f}, // bottom right
{1.0f, 0.0f}, // top right
@ -106,23 +123,54 @@ static void init(void* _userdata)
0, 1, 2, 0, 2, 3,
};
const vs_uniform_t uniform = {
.radius = 1,
};
Mat4 mvp = M4();
compute_mvp(&mvp, V2(0, 0), 1);
void* tmp_buffer = malloc(sizeof(position_t) * SAMPLE_COUNT);
Vec2* tmp_buffer = malloc(sizeof(Vec2) * SAMPLE_COUNT);
for(uint32_t i = 0; i < SAMPLE_COUNT; i++)
{
tmp_buffer[i].X = next_float();
tmp_buffer[i].Y = next_float();
}
sg_shader sprite_shader = sg_make_shader(&(sg_shader_desc) {
.vertex_func = {
.source = src_shaders_sprite_wgsl,
.entry = "vs_main",
},
.fragment_func = {
.source = src_shaders_sprite_wgsl,
.entry = "fs_main",
},
.storage_buffers[0] = {
.wgsl_group1_binding_n = 0,
.stage = SG_SHADERSTAGE_VERTEX,
.readonly = true,
},
/*.uniform_blocks[0] = {
.size = sizeof(float) * 16, //mat4x4f
.stage = SG_SHADERSTAGE_VERTEX,
.wgsl_group0_binding_n = 0,
},*/
.attrs[0] = {
.base_type = SG_SHADERATTRBASETYPE_FLOAT,
},
.label = "Sprite shader",
});
userdata->renderer = (renderer_t) {
.pass = (sg_pass_action) {
.colors[0] = { .clear_value = { 0.0f, 0.0f, 0.0f, 1.0f }, .load_action = SG_LOADACTION_CLEAR }
},
.pipeline = sg_make_pipeline(&(sg_pipeline_desc) {
.shader = sg_make_shader(base_shader_desc(sg_query_backend())),
.shader = sprite_shader,
.depth.write_enabled = true,
.index_type = SG_INDEXTYPE_UINT16,
.layout.attrs = {
[ATTR_base_in_quad].format = SG_VERTEXFORMAT_FLOAT2,
[0].format = SG_VERTEXFORMAT_FLOAT2,
},
.label = "Sprite pipeline",
}),
/*.compute = sg_make_pipeline(&(sg_pipeline_desc) {
.compute = true,
@ -130,30 +178,34 @@ static void init(void* _userdata)
}),*/
.binding = (sg_bindings) {
.vertex_buffers = {
[ATTR_base_in_quad] = sg_make_buffer(&(sg_buffer_desc) {
[0] = sg_make_buffer(&(sg_buffer_desc) {
.type = SG_BUFFERTYPE_VERTEXBUFFER,
.data = SG_RANGE(quad),
.label = "Vertices"
}),
},
.index_buffer = sg_make_buffer(&(sg_buffer_desc) {
.type = SG_BUFFERTYPE_INDEXBUFFER,
.data = SG_RANGE(indices),
.label = "Indices"
}),
.storage_buffers = {
[SBUF_vs_ssbo] = sg_make_buffer(&(sg_buffer_desc) {
[0] = sg_make_buffer(&(sg_buffer_desc) {
.type = SG_BUFFERTYPE_STORAGEBUFFER,
.data = {
.ptr = tmp_buffer,
.size = sizeof(position_t) * SAMPLE_COUNT,
.size = sizeof(Vec2) * SAMPLE_COUNT,
},
.label = "SSBO"
}),
},
},
.uniform = SG_RANGE(uniform),
.uniform = SG_RANGE(mvp),
};
userdata->pan = (position_t) { .x = 0, .y = 0 };
userdata->pan = (Vec2) { .X = 0, .Y = 0 };
userdata->zoom = 1;
userdata->mvp = &mvp;
FREE(tmp_buffer);
}
@ -192,11 +244,13 @@ static void event(const sapp_event* event, void* _userdata)
const float diff = expf(event->scroll_y * 0.01f);
const float width = sapp_widthf(), height = sapp_heightf();
userdata->pan.x = userdata->pan.x - (event->mouse_x / (diff * userdata->zoom) - event->mouse_x / userdata->zoom);
userdata->pan.y = userdata->pan.y - (event->mouse_x / (diff * userdata->zoom) - event->mouse_x / userdata->zoom);
userdata->pan.X = userdata->pan.X - (event->mouse_x / (diff * userdata->zoom) - event->mouse_x / userdata->zoom);
userdata->pan.Y = userdata->pan.Y - (event->mouse_y / (diff * userdata->zoom) - event->mouse_y / userdata->zoom);
userdata->zoom = _sg_clamp(userdata->zoom * diff, 0.1f, 3.0f);
js_log(3, "Zoom: %f", userdata->zoom);
compute_mvp(userdata->mvp, userdata->pan, userdata->zoom);
js_log(3, "Zoom: %f. Pan: %f/%f", userdata->zoom, userdata->pan.X, userdata->pan.Y);
break;
default:

3939
src/math.h Normal file

File diff suppressed because it is too large Load Diff

59
src/rand.h Normal file
View File

@ -0,0 +1,59 @@
uint32_t seed;
uint32_t xorshift32(void);
void rand_seed(uint32_t _seed);
uint32_t next_int(void);
uint32_t next_int_max(uint32_t max);
uint32_t next_int_minmax(uint32_t min, uint32_t max);
float next_float(void);
float next_float_max(float_t max);
float next_float_minmax(float_t min, float_t max);
uint32_t xorshift32(void)
{
seed ^= seed<<13;
seed ^= seed>>17;
seed ^= seed<<5;
return seed;
}
void rand_seed(uint32_t _seed)
{
if(_seed == 0)
return;
seed = _seed;
xorshift32();
}
// PRNG [0-UINT32_MAX]
uint32_t next_int(void)
{
return xorshift32();
}
// PRNG [0-max]
uint32_t next_int_max(uint32_t max)
{
return (uint32_t) floorf(xorshift32() / (float) UINT32_MAX * max);
}
// PRNG [min-max]
uint32_t next_int_minmax(uint32_t min, uint32_t max)
{
const float x = xorshift32() / (float) UINT32_MAX;
//(1.0f - Time) * A + Time * B
return (1.0f - x) * min + x * max;
}
// PRNG [0-1]
float next_float(void)
{
return xorshift32() / (float) UINT32_MAX;
}
// PRNG [0-max]
float next_float_max(float_t max)
{
return xorshift32() / (float) UINT32_MAX * max;
}
// PRNG [min-max]
float next_float_minmax(float_t min, float_t max)
{
const float x = xorshift32() / (float) UINT32_MAX;
return (1.0f - x) * min + x * max;
}

View File

@ -1,4 +1,6 @@
@block common
@ctype vec2 position_t;
struct cell
{
vec2 pos;
@ -23,8 +25,7 @@ layout(location = 1) out vec2 _quad;
layout(location = 2) out vec2 _centroid;
layout(location = 3) flat out vec3 _color;
layout(binding = 0) readonly buffer vs_ssbo
{
readonly: layout(binding = 0) readonly buffer vs_ssbo {
cell cells[];
};

47
src/shaders/sprite.wgsl Normal file
View File

@ -0,0 +1,47 @@
struct Cell {
position: vec2f,
};
struct VsI { //Vertex shader input
@builtin(instance_index) instance: u32,
@location(0) position: vec2f,
};
struct Vs2Fs { //Vertex shader to Fragment shader
@builtin(position) pos: vec4f,
@location(0) @interpolate(flat) instance: u32,
};
struct FsO { //Fragment shader output
@builtin(frag_depth) depth: f32,
@location(0) color: vec4f,
};
fn color(i: u32) -> vec3f
{
let r: f32 = f32(((i >> 0) & 0xff));
let g: f32 = f32(((i >> 8) & 0xff));
let b: f32 = f32(((i >> 16) & 0xff));
return vec3f(r / 255, g / 255, b / 255);
}
//@binding(0) @group(0) var<uniform> mvp: mat4x4f;
@binding(0) @group(1) var<storage> cells: array<Cell>;
@vertex
fn vs_main(input: VsI) -> Vs2Fs {
var output: Vs2Fs;
output.pos = vec4f(cells[input.instance].position.xy, 0.0, 0.0)/* * mvp*/;
output.instance = input.instance;
return output;
}
@fragment
fn fs_main(input: Vs2Fs) -> FsO {
var output: FsO;
output.depth = 1.0;
output.color = vec4f(1.0, 0.0, 1.0, 1.0);
//output.color = vec4f(color(input.instance), 1.0);
return output;
}