Skip to content

Commit

Permalink
Merge pull request #140 from floooh/storage-buffers
Browse files Browse the repository at this point in the history
Storage buffer update.
  • Loading branch information
floooh authored May 9, 2024
2 parents 5986000 + c197a4c commit 7a296f6
Show file tree
Hide file tree
Showing 63 changed files with 2,365 additions and 90 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ jobs:
- uses: actions/checkout@v4
- uses: seanmiddleditch/gha-setup-ninja@master
- name: prepare
run: python3 fips emsdk install latest
run: |
sudo apt update
sudo apt install webp
python3 fips emsdk install latest
- name: build
run: |
python3 fips set local on
Expand All @@ -27,7 +30,10 @@ jobs:
- uses: actions/checkout@v4
- uses: seanmiddleditch/gha-setup-ninja@master
- name: prepare
run: python3 fips emsdk install latest
run: |
sudo apt update
sudo apt install webp
python3 fips emsdk install latest
- name: build
run: |
python3 fips set local on
Expand Down
14 changes: 9 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ elseif (FIPS_ANDROID)
set(slang "glsl300es")
elseif (SOKOL_USE_D3D11)
set(sokol_backend SOKOL_D3D11)
set(slang "hlsl4")
set(slang "hlsl5")
elseif (SOKOL_USE_METAL)
set(sokol_backend SOKOL_METAL)
if (FIPS_IOS)
Expand All @@ -64,15 +64,19 @@ elseif (SOKOL_USE_EGL_GLES3)
set(sokol_backend SOKOL_GLES3)
set(slang "glsl300es")
elseif (SOKOL_USE_EGL_GL)
set(sokol_backend SOKOL_GLCORE33)
set(slang "glsl330")
set(sokol_backend SOKOL_GLCORE)
set(slang "glsl430")
else()
if (FIPS_IOS)
set(sokol_backend SOKOL_GLES3)
set(slang "glsl300es")
else()
set(sokol_backend SOKOL_GLCORE33)
set(slang "glsl330")
set(sokol_backend SOKOL_GLCORE)
if (FIPS_MACOS)
set(slang "glsl410")
else()
set(slang "glsl430")
endif()
endif()
endif()

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ To build one of the sokol-app samples for GL on macOS:
```sh
> cd sokol-samples/sapp
> ../../sokol-tools-bin/bin/osx/sokol-shdc -i cube-sapp.glsl -o cube-sapp.glsl.h -l glsl330
> cc cube-sapp.c ../libs/sokol/sokol.m -o cube-sapp -fobjc-arc -DSOKOL_GLCORE33 -I../../sokol -I../libs -framework OpenGL -framework Cocoa -framework AudioToolbox
> cc cube-sapp.c ../libs/sokol/sokol.m -o cube-sapp -fobjc-arc -DSOKOL_GLCORE -I../../sokol -I../libs -framework OpenGL -framework Cocoa -framework AudioToolbox
```

### Building manually on Windows with MSVC
Expand Down Expand Up @@ -348,7 +348,7 @@ To build one of the sokol-app samples for GL on Windows:
```sh
> cd sokol-samples\sapp
> ..\..\sokol-tools-bin\bin\win32\sokol-shdc -i cube-sapp.glsl -o cube-sapp.glsl.h -l glsl330
> cl cube-sapp.c ..\libs\sokol\sokol.c /DSOKOL_GLCORE33 /I..\..\sokol /I..\libs kernel32.lib user32.lib gdi32.lib
> cl cube-sapp.c ..\libs\sokol\sokol.c /DSOKOL_GLCORE /I..\..\sokol /I..\libs kernel32.lib user32.lib gdi32.lib
```

### Building manually on Windows with MSYS2/mingw gcc:
Expand All @@ -365,7 +365,7 @@ From the MSYS2 shell:
> gcc cube-sapp.c ../libs/sokol/sokol.c -o cube-sapp-d3d11 -mwin32 -O2 -DSOKOL_D3D11 -I../../sokol -I ../libs -lkernel32 -luser32 -lshell32 -ldxgi -ld3d11 -lole32 -lgdi32
> ./cube-sapp-d3d11
# build and run with GL backend:
> gcc cube-sapp.c ../libs/sokol/sokol.c -o cube-sapp-gl -mwin32 -O2 -DSOKOL_GLCORE33 -I../../sokol -I ../libs -lkernel32 -luser32 -lshell32 -lgdi32 -lole32
> gcc cube-sapp.c ../libs/sokol/sokol.c -o cube-sapp-gl -mwin32 -O2 -DSOKOL_GLCORE -I../../sokol -I ../libs -lkernel32 -luser32 -lshell32 -lgdi32 -lole32
> ./cube-sapp-gl
```

Expand All @@ -384,7 +384,7 @@ need to specify the link libraries manually.
> clang cube-sapp.c ../libs/sokol/sokol.c -o cube-sapp-d3d11.exe -O2 -DSOKOL_D3D11 -I ../../sokol -I ../libs
> cube-sapp-d3d11
# build and run with GL backend:
> clang cube-sapp.c ../libs/sokol/sokol.c -o cube-sapp-gl.exe -O2 -DSOKOL_GLCORE33 -I ../../sokol -I ../libs
> clang cube-sapp.c ../libs/sokol/sokol.c -o cube-sapp-gl.exe -O2 -DSOKOL_GLCORE -I ../../sokol -I ../libs
> cube-sapp-gl
```

Expand All @@ -406,7 +406,7 @@ To build one of the sokol-app samples on Linux:
```sh
> cd sokol-samples/sapp
> ../../sokol-tools-bin/bin/linux/sokol-shdc -i cube-sapp.glsl -o cube-sapp.glsl.h -l glsl330
> cc cube-sapp.c ../libs/sokol/sokol.c -o cube-sapp -DSOKOL_GLCORE33 -pthread -I../../sokol -I../libs -lGL -ldl -lm -lX11 -lasound -lXi -lXcursor
> cc cube-sapp.c ../libs/sokol/sokol.c -o cube-sapp -DSOKOL_GLCORE -pthread -I../../sokol -I../libs -lGL -ldl -lm -lX11 -lasound -lXi -lXcursor
```

### Building for WASM / WebGL2
Expand Down
5 changes: 5 additions & 0 deletions d3d11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ fips_begin_app(bufferoffsets-d3d11 windowed)
fips_files(bufferoffsets-d3d11.c)
fips_deps(d3d11entry)
fips_end_app()

fips_begin_app(vertexpulling-d3d11 windowed)
fips_files(vertexpulling-d3d11.c)
fips_deps(d3d11entry)
fips_end_app()
4 changes: 2 additions & 2 deletions d3d11/d3d11entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ void d3d11_init(const d3d11_desc_t* desc) {
},
.OutputWindow = state.hwnd,
.Windowed = true,
.SwapEffect = DXGI_SWAP_EFFECT_DISCARD,
.BufferCount = 1,
.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD,
.BufferCount = 2,
.SampleDesc = {
.Count = (UINT) 1,
.Quality = (UINT) 0,
Expand Down
153 changes: 153 additions & 0 deletions d3d11/vertexpulling-d3d11.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//------------------------------------------------------------------------------
// vertexpulling-d3d11.c
//
// Demonstrates vertex pulling from storage buffers.
//------------------------------------------------------------------------------
#include "d3d11entry.h"
#define SOKOL_IMPL
#define SOKOL_D3D11
#include "sokol_gfx.h"
#include "sokol_log.h"
#define HANDMADE_MATH_IMPLEMENTATION
#define HANDMADE_MATH_NO_SSE
#include "HandmadeMath.h"

typedef struct {
float pos[4];
float color[4];
} vertex_t;

typedef struct {
hmm_mat4 mvp;
} vs_params_t;

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
(void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;

d3d11_init(&(d3d11_desc_t){ .width = 640, .height = 480, .sample_count = 4, .title = L"vertexpulling-d3d11.c" });
sg_setup(&(sg_desc){
.environment = d3d11_environment(),
.logger.func = slog_func,
});

const sg_pass_action pass_action = {
.colors[0] = { .load_action = SG_LOADACTION_CLEAR, .clear_value = { 0.5f, 0.5f, 1.0f, 1.0f } },
};

const vertex_t vertices[] = {
{ .pos = { -1.0, -1.0, -1.0, 1.0 }, .color = { 1.0, 0.0, 0.0, 1.0 } },
{ .pos = { 1.0, -1.0, -1.0, 1.0 }, .color = { 1.0, 0.0, 0.0, 1.0 } },
{ .pos = { 1.0, 1.0, -1.0, 1.0 }, .color = { 1.0, 0.0, 0.0, 1.0 } },
{ .pos = { -1.0, 1.0, -1.0, 1.0 }, .color = { 1.0, 0.0, 0.0, 1.0 } },

{ .pos = { -1.0, -1.0, 1.0, 1.0 }, .color = { 0.0, 1.0, 0.0, 1.0 } },
{ .pos = { 1.0, -1.0, 1.0, 1.0 }, .color = { 0.0, 1.0, 0.0, 1.0 } },
{ .pos = { 1.0, 1.0, 1.0, 1.0 }, .color = { 0.0, 1.0, 0.0, 1.0 } },
{ .pos = { -1.0, 1.0, 1.0, 1.0 }, .color = { 0.0, 1.0, 0.0, 1.0 } },

{ .pos = { -1.0, -1.0, -1.0, 1.0 }, .color = { 0.0, 0.0, 1.0, 1.0 } },
{ .pos = { -1.0, 1.0, -1.0, 1.0 }, .color = { 0.0, 0.0, 1.0, 1.0 } },
{ .pos = { -1.0, 1.0, 1.0, 1.0 }, .color = { 0.0, 0.0, 1.0, 1.0 } },
{ .pos = { -1.0, -1.0, 1.0, 1.0 }, .color = { 0.0, 0.0, 1.0, 1.0 } },

{ .pos = { 1.0, -1.0, -1.0, 1.0 }, .color = {1.0, 0.5, 0.0, 1.0, } },
{ .pos = { 1.0, 1.0, -1.0, 1.0 }, .color = {1.0, 0.5, 0.0, 1.0, } },
{ .pos = { 1.0, 1.0, 1.0, 1.0 }, .color = {1.0, 0.5, 0.0, 1.0, } },
{ .pos = { 1.0, -1.0, 1.0, 1.0 }, .color = {1.0, 0.5, 0.0, 1.0, } },

{ .pos = { -1.0, -1.0, -1.0, 1.0 }, .color = { 0.0, 0.5, 1.0, 1.0 } },
{ .pos = { -1.0, -1.0, 1.0, 1.0 }, .color = { 0.0, 0.5, 1.0, 1.0 } },
{ .pos = { 1.0, -1.0, 1.0, 1.0 }, .color = { 0.0, 0.5, 1.0, 1.0 } },
{ .pos = { 1.0, -1.0, -1.0, 1.0 }, .color = { 0.0, 0.5, 1.0, 1.0 } },

{ .pos = { -1.0, 1.0, -1.0, 1.0 }, .color = { 1.0, 0.0, 0.5, 1.0 } },
{ .pos = { -1.0, 1.0, 1.0, 1.0 }, .color = { 1.0, 0.0, 0.5, 1.0 } },
{ .pos = { 1.0, 1.0, 1.0, 1.0 }, .color = { 1.0, 0.0, 0.5, 1.0 } },
{ .pos = { 1.0, 1.0, -1.0, 1.0 }, .color = { 1.0, 0.0, 0.5, 1.0 } }
};
sg_buffer sbuf = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_STORAGEBUFFER,
.data = SG_RANGE(vertices),
});

uint16_t indices[] = {
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
};
sg_buffer ibuf = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_INDEXBUFFER,
.data = SG_RANGE(indices)
});

sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vs.uniform_blocks[0].size = sizeof(vs_params_t),
.vs.storage_buffers[0] = { .used = true, .readonly = true },
.vs.source =
"cbuffer params: register(b0) {\n"
" float4x4 mvp;\n"
"};\n"
"ByteAddressBuffer vertices: register(t16);\n"
"struct vs_out {\n"
" float4 color: COLOR0;\n"
" float4 pos: SV_Position;\n"
"};\n"
"vs_out main(uint vidx: SV_VertexID) {\n"
" vs_out outp;\n"
" float4 pos = asfloat(vertices.Load4(vidx * 32 + 0));\n"
" float4 color = asfloat(vertices.Load4(vidx * 32 + 16));\n"
" outp.pos = mul(mvp, pos);\n"
" outp.color = color;\n"
" return outp;\n"
"};\n",
.fs.source =
"float4 main(float4 color: COLOR0): SV_Target0 {\n"
" return color;\n"
"}\n"
});

sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.shader = shd,
.index_type = SG_INDEXTYPE_UINT16,
.depth = {
.compare = SG_COMPAREFUNC_LESS_EQUAL,
.write_enabled = true
},
.cull_mode = SG_CULLMODE_BACK,
});

sg_bindings bind = {
.index_buffer = ibuf,
.vs.storage_buffers[0] = sbuf,
};

float rx = 0.0f, ry = 0.0f;
while (d3d11_process_events()) {
// view-projection matrix
const hmm_mat4 proj = HMM_Perspective(60.0f, (float)d3d11_width()/(float)d3d11_height(), 0.01f, 10.0f);
const hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
const hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view);
// model-view-proj matrix for vertex shader
vs_params_t vs_params;
rx += 1.0f; ry += 2.0f;
const hmm_mat4 rxm = HMM_Rotate(rx, HMM_Vec3(1.0f, 0.0f, 0.0f));
const hmm_mat4 rym = HMM_Rotate(ry, HMM_Vec3(0.0f, 1.0f, 0.0f));
const hmm_mat4 model = HMM_MultiplyMat4(rxm, rym);
vs_params.mvp = HMM_MultiplyMat4(view_proj, model);

sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = d3d11_swapchain() });
sg_apply_pipeline(pip);
sg_apply_bindings(&bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
sg_end_pass();
sg_commit();
d3d11_present();
}
sg_shutdown();
d3d11_shutdown();
return 0;
}
Loading

0 comments on commit 7a296f6

Please sign in to comment.