Skip to content

Commit

Permalink
Merge pull request #31 from Nazariglez/master
Browse files Browse the repository at this point in the history
Added support for wasm using emscripten
  • Loading branch information
floooh committed Jul 30, 2024
2 parents 4b52682 + 9606119 commit 3fe27d2
Show file tree
Hide file tree
Showing 17 changed files with 1,423 additions and 18 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,47 @@ nimble build_all
nimble -d:gl build_debug
nimble -d:gl build_all
```

## Emscriptern - Web

To use `sokol-nim` with `emscripten` we need the [toolchain installed](https://github.com/emscripten-core/emsdk),
a [shell-file](https://github.com/emscripten-core/emscripten/blob/main/src/shell_minimal.html) and the following `config.nims` file:
(based on [this](https://github.com/treeform/nim_emscripten_tutorial?tab=readme-ov-file#step-1-using-nim-with-emscripten))

```nim
when defined(emscripten):
# This path will only run if -d:emscripten is passed to nim.
--nimcache:tmp # Store intermediate files close by in the tmp dir.
--os:linux # Emscripten pretends to be linux.
--cpu:wasm32 # Emscripten is 32bits.
--cc:clang # Emscripten is very close to clang, so we will replace it.
when defined(windows):
--clang.exe:emcc.bat # Replace C
--clang.linkerexe:emcc.bat # Replace C linker
--clang.cpp.exe:emcc.bat # Replace C++
--clang.cpp.linkerexe:emcc.bat # Replace C++ linker.
else:
--clang.exe:emcc # Replace C
--clang.linkerexe:emcc # Replace C linker
--clang.cpp.exe:emcc # Replace C++
--clang.cpp.linkerexe:emcc # Replace C++ linker.
--listCmd # List what commands we are running so that we can debug them.
--exceptions:goto # Goto exceptions are friendlier with crazy platforms.
--define:noSignalHandler # Emscripten doesn't support signal handlers.
--threads:off
# Pass this to Emscripten linker to generate html file scaffold for us.
switch("passL", "-o build/index.html --shell-file template.html")
# next lines are optional to optimize build for speed
# when defined(release):
# --opt:speed
```

If you're using `sokol` as dependenciy in your proyect you can just do `nimble build -d:release -d:emscripten` and serve
your template file using any HTTP server.
150 changes: 149 additions & 1 deletion examples/shaders/blend.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ../math/mat4
# Generated by sokol-shdc (https://github.com/floooh/sokol-tools)
#
# Cmdline:
# sokol-shdc -i examples/shaders/blend.glsl -o examples/shaders/blend.nim -l glsl430:metal_macos:hlsl5 -f sokol_nim
# sokol-shdc -i examples/shaders/blend.glsl -o examples/shaders/blend.nim -l glsl430:metal_macos:hlsl5:glsl300es -f sokol_nim
#
# Overview:
# =========
Expand Down Expand Up @@ -158,6 +158,131 @@ const fsQuadSourceGlsl430: array[135, uint8] = [
0x72,0x3b,0x0a,0x7d,0x0a,0x0a,0x00,
]
#
# #version 300 es
#
# layout(location = 0) in vec2 position;
#
# void main()
# {
# gl_Position = vec4(position, 0.5, 1.0);
# }
#
#
const vsBgSourceGlsl300es: array[119, uint8] = [
0x23'u8,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x30,0x30,0x20,0x65,0x73,0x0a,
0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,
0x20,0x3d,0x20,0x30,0x29,0x20,0x69,0x6e,0x20,0x76,0x65,0x63,0x32,0x20,0x70,0x6f,
0x73,0x69,0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x0a,0x76,0x6f,0x69,0x64,0x20,0x6d,0x61,
0x69,0x6e,0x28,0x29,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x67,0x6c,0x5f,0x50,0x6f,
0x73,0x69,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x76,0x65,0x63,0x34,0x28,0x70,0x6f,
0x73,0x69,0x74,0x69,0x6f,0x6e,0x2c,0x20,0x30,0x2e,0x35,0x2c,0x20,0x31,0x2e,0x30,
0x29,0x3b,0x0a,0x7d,0x0a,0x0a,0x00,
]
#
# #version 300 es
# precision mediump float;
# precision highp int;
#
# uniform highp vec4 bg_fs_params[1];
# layout(location = 0) out highp vec4 frag_color;
#
# void main()
# {
# highp vec2 _28 = fract((gl_FragCoord.xy - vec2(bg_fs_params[0].x)) * vec2(0.0199999995529651641845703125));
# highp float _39 = _28.x * _28.y;
# frag_color = vec4(_39, _39, _39, 1.0);
# }
#
#
const fsBgSourceGlsl300es: array[358, uint8] = [
0x23'u8,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x30,0x30,0x20,0x65,0x73,0x0a,
0x70,0x72,0x65,0x63,0x69,0x73,0x69,0x6f,0x6e,0x20,0x6d,0x65,0x64,0x69,0x75,0x6d,
0x70,0x20,0x66,0x6c,0x6f,0x61,0x74,0x3b,0x0a,0x70,0x72,0x65,0x63,0x69,0x73,0x69,
0x6f,0x6e,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x69,0x6e,0x74,0x3b,0x0a,0x0a,0x75,
0x6e,0x69,0x66,0x6f,0x72,0x6d,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x76,0x65,0x63,
0x34,0x20,0x62,0x67,0x5f,0x66,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x31,
0x5d,0x3b,0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,
0x6f,0x6e,0x20,0x3d,0x20,0x30,0x29,0x20,0x6f,0x75,0x74,0x20,0x68,0x69,0x67,0x68,
0x70,0x20,0x76,0x65,0x63,0x34,0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,
0x72,0x3b,0x0a,0x0a,0x76,0x6f,0x69,0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,0x0a,
0x7b,0x0a,0x20,0x20,0x20,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x76,0x65,0x63,0x32,
0x20,0x5f,0x32,0x38,0x20,0x3d,0x20,0x66,0x72,0x61,0x63,0x74,0x28,0x28,0x67,0x6c,
0x5f,0x46,0x72,0x61,0x67,0x43,0x6f,0x6f,0x72,0x64,0x2e,0x78,0x79,0x20,0x2d,0x20,
0x76,0x65,0x63,0x32,0x28,0x62,0x67,0x5f,0x66,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,
0x73,0x5b,0x30,0x5d,0x2e,0x78,0x29,0x29,0x20,0x2a,0x20,0x76,0x65,0x63,0x32,0x28,
0x30,0x2e,0x30,0x31,0x39,0x39,0x39,0x39,0x39,0x39,0x39,0x35,0x35,0x32,0x39,0x36,
0x35,0x31,0x36,0x34,0x31,0x38,0x34,0x35,0x37,0x30,0x33,0x31,0x32,0x35,0x29,0x29,
0x3b,0x0a,0x20,0x20,0x20,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x66,0x6c,0x6f,0x61,
0x74,0x20,0x5f,0x33,0x39,0x20,0x3d,0x20,0x5f,0x32,0x38,0x2e,0x78,0x20,0x2a,0x20,
0x5f,0x32,0x38,0x2e,0x79,0x3b,0x0a,0x20,0x20,0x20,0x20,0x66,0x72,0x61,0x67,0x5f,
0x63,0x6f,0x6c,0x6f,0x72,0x20,0x3d,0x20,0x76,0x65,0x63,0x34,0x28,0x5f,0x33,0x39,
0x2c,0x20,0x5f,0x33,0x39,0x2c,0x20,0x5f,0x33,0x39,0x2c,0x20,0x31,0x2e,0x30,0x29,
0x3b,0x0a,0x7d,0x0a,0x0a,0x00,
]
#
# #version 300 es
#
# uniform vec4 quad_vs_params[4];
# layout(location = 0) in vec4 position;
# out vec4 color;
# layout(location = 1) in vec4 color0;
#
# void main()
# {
# gl_Position = mat4(quad_vs_params[0], quad_vs_params[1], quad_vs_params[2], quad_vs_params[3]) * position;
# color = color0;
# }
#
#
const vsQuadSourceGlsl300es: array[291, uint8] = [
0x23'u8,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x30,0x30,0x20,0x65,0x73,0x0a,
0x0a,0x75,0x6e,0x69,0x66,0x6f,0x72,0x6d,0x20,0x76,0x65,0x63,0x34,0x20,0x71,0x75,
0x61,0x64,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x34,0x5d,0x3b,
0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,
0x20,0x3d,0x20,0x30,0x29,0x20,0x69,0x6e,0x20,0x76,0x65,0x63,0x34,0x20,0x70,0x6f,
0x73,0x69,0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x6f,0x75,0x74,0x20,0x76,0x65,0x63,0x34,
0x20,0x63,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,
0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x31,0x29,0x20,0x69,0x6e,0x20,
0x76,0x65,0x63,0x34,0x20,0x63,0x6f,0x6c,0x6f,0x72,0x30,0x3b,0x0a,0x0a,0x76,0x6f,
0x69,0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,
0x67,0x6c,0x5f,0x50,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x6d,0x61,
0x74,0x34,0x28,0x71,0x75,0x61,0x64,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,
0x73,0x5b,0x30,0x5d,0x2c,0x20,0x71,0x75,0x61,0x64,0x5f,0x76,0x73,0x5f,0x70,0x61,
0x72,0x61,0x6d,0x73,0x5b,0x31,0x5d,0x2c,0x20,0x71,0x75,0x61,0x64,0x5f,0x76,0x73,
0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x32,0x5d,0x2c,0x20,0x71,0x75,0x61,0x64,
0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x33,0x5d,0x29,0x20,0x2a,
0x20,0x70,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x20,0x20,0x20,0x20,0x63,
0x6f,0x6c,0x6f,0x72,0x20,0x3d,0x20,0x63,0x6f,0x6c,0x6f,0x72,0x30,0x3b,0x0a,0x7d,
0x0a,0x0a,0x00,
]
#
# #version 300 es
# precision mediump float;
# precision highp int;
#
# layout(location = 0) out highp vec4 frag_color;
# in highp vec4 color;
#
# void main()
# {
# frag_color = color;
# }
#
#
const fsQuadSourceGlsl300es: array[175, uint8] = [
0x23'u8,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x30,0x30,0x20,0x65,0x73,0x0a,
0x70,0x72,0x65,0x63,0x69,0x73,0x69,0x6f,0x6e,0x20,0x6d,0x65,0x64,0x69,0x75,0x6d,
0x70,0x20,0x66,0x6c,0x6f,0x61,0x74,0x3b,0x0a,0x70,0x72,0x65,0x63,0x69,0x73,0x69,
0x6f,0x6e,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x69,0x6e,0x74,0x3b,0x0a,0x0a,0x6c,
0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,
0x20,0x30,0x29,0x20,0x6f,0x75,0x74,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x76,0x65,
0x63,0x34,0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x69,
0x6e,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x76,0x65,0x63,0x34,0x20,0x63,0x6f,0x6c,
0x6f,0x72,0x3b,0x0a,0x0a,0x76,0x6f,0x69,0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,
0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,
0x72,0x20,0x3d,0x20,0x63,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x7d,0x0a,0x0a,0x00,
]
#
# static float4 gl_Position;
# static float2 position;
#
Expand Down Expand Up @@ -686,6 +811,17 @@ proc bgShaderDesc*(backend: sg.Backend): sg.ShaderDesc =
result.fs.uniformBlocks[0].uniforms[0].name = "bg_fs_params"
result.fs.uniformBlocks[0].uniforms[0].type = uniformTypeFloat4
result.fs.uniformBlocks[0].uniforms[0].arrayCount = 1
of backendGles3:
result.attrs[0].name = "position"
result.vs.source = cast[cstring](addr(vsBgSourceGlsl300es))
result.vs.entry = "main"
result.fs.source = cast[cstring](addr(fsBgSourceGlsl300es))
result.fs.entry = "main"
result.fs.uniformBlocks[0].size = 16
result.fs.uniformBlocks[0].layout = uniformLayoutStd140
result.fs.uniformBlocks[0].uniforms[0].name = "bg_fs_params"
result.fs.uniformBlocks[0].uniforms[0].type = uniformTypeFloat4
result.fs.uniformBlocks[0].uniforms[0].arrayCount = 1
of backendD3d11:
result.attrs[0].semName = "TEXCOORD"
result.attrs[0].semIndex = 0
Expand Down Expand Up @@ -720,6 +856,18 @@ proc quadShaderDesc*(backend: sg.Backend): sg.ShaderDesc =
result.vs.uniformBlocks[0].uniforms[0].arrayCount = 4
result.fs.source = cast[cstring](addr(fsQuadSourceGlsl430))
result.fs.entry = "main"
of backendGles3:
result.attrs[0].name = "position"
result.attrs[1].name = "color0"
result.vs.source = cast[cstring](addr(vsQuadSourceGlsl300es))
result.vs.entry = "main"
result.vs.uniformBlocks[0].size = 64
result.vs.uniformBlocks[0].layout = uniformLayoutStd140
result.vs.uniformBlocks[0].uniforms[0].name = "quad_vs_params"
result.vs.uniformBlocks[0].uniforms[0].type = uniformTypeFloat4
result.vs.uniformBlocks[0].uniforms[0].arrayCount = 4
result.fs.source = cast[cstring](addr(fsQuadSourceGlsl300es))
result.fs.entry = "main"
of backendD3d11:
result.attrs[0].semName = "TEXCOORD"
result.attrs[0].semIndex = 0
Expand Down
64 changes: 63 additions & 1 deletion examples/shaders/bufferoffsets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sokol/gfx as sg
# Generated by sokol-shdc (https://github.com/floooh/sokol-tools)
#
# Cmdline:
# sokol-shdc -i examples/shaders/bufferoffsets.glsl -o examples/shaders/bufferoffsets.nim -l glsl430:metal_macos:hlsl5 -f sokol_nim
# sokol-shdc -i examples/shaders/bufferoffsets.glsl -o examples/shaders/bufferoffsets.nim -l glsl430:metal_macos:hlsl5:glsl300es -f sokol_nim
#
# Overview:
# =========
Expand Down Expand Up @@ -72,6 +72,61 @@ const fsSourceGlsl430: array[135, uint8] = [
0x72,0x3b,0x0a,0x7d,0x0a,0x0a,0x00,
]
#
# #version 300 es
#
# layout(location = 0) in vec4 position;
# out vec4 color;
# layout(location = 1) in vec4 color0;
#
# void main()
# {
# gl_Position = position;
# color = color0;
# }
#
#
const vsSourceGlsl300es: array[176, uint8] = [
0x23'u8,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x30,0x30,0x20,0x65,0x73,0x0a,
0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,
0x20,0x3d,0x20,0x30,0x29,0x20,0x69,0x6e,0x20,0x76,0x65,0x63,0x34,0x20,0x70,0x6f,
0x73,0x69,0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x6f,0x75,0x74,0x20,0x76,0x65,0x63,0x34,
0x20,0x63,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,
0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x31,0x29,0x20,0x69,0x6e,0x20,
0x76,0x65,0x63,0x34,0x20,0x63,0x6f,0x6c,0x6f,0x72,0x30,0x3b,0x0a,0x0a,0x76,0x6f,
0x69,0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,
0x67,0x6c,0x5f,0x50,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x70,0x6f,
0x73,0x69,0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x20,0x20,0x20,0x20,0x63,0x6f,0x6c,0x6f,
0x72,0x20,0x3d,0x20,0x63,0x6f,0x6c,0x6f,0x72,0x30,0x3b,0x0a,0x7d,0x0a,0x0a,0x00,

]
#
# #version 300 es
# precision mediump float;
# precision highp int;
#
# layout(location = 0) out highp vec4 frag_color;
# in highp vec4 color;
#
# void main()
# {
# frag_color = color;
# }
#
#
const fsSourceGlsl300es: array[175, uint8] = [
0x23'u8,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x30,0x30,0x20,0x65,0x73,0x0a,
0x70,0x72,0x65,0x63,0x69,0x73,0x69,0x6f,0x6e,0x20,0x6d,0x65,0x64,0x69,0x75,0x6d,
0x70,0x20,0x66,0x6c,0x6f,0x61,0x74,0x3b,0x0a,0x70,0x72,0x65,0x63,0x69,0x73,0x69,
0x6f,0x6e,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x69,0x6e,0x74,0x3b,0x0a,0x0a,0x6c,
0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,
0x20,0x30,0x29,0x20,0x6f,0x75,0x74,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x76,0x65,
0x63,0x34,0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x69,
0x6e,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x76,0x65,0x63,0x34,0x20,0x63,0x6f,0x6c,
0x6f,0x72,0x3b,0x0a,0x0a,0x76,0x6f,0x69,0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,
0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,
0x72,0x20,0x3d,0x20,0x63,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x7d,0x0a,0x0a,0x00,
]
#
# static float4 gl_Position;
# static float4 position;
# static float4 color;
Expand Down Expand Up @@ -319,6 +374,13 @@ proc bufferoffsetsShaderDesc*(backend: sg.Backend): sg.ShaderDesc =
result.vs.entry = "main"
result.fs.source = cast[cstring](addr(fsSourceGlsl430))
result.fs.entry = "main"
of backendGles3:
result.attrs[0].name = "position"
result.attrs[1].name = "color0"
result.vs.source = cast[cstring](addr(vsSourceGlsl300es))
result.vs.entry = "main"
result.fs.source = cast[cstring](addr(fsSourceGlsl300es))
result.fs.entry = "main"
of backendD3d11:
result.attrs[0].semName = "TEXCOORD"
result.attrs[0].semIndex = 0
Expand Down
Loading

0 comments on commit 3fe27d2

Please sign in to comment.