-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
sdf.v
69 lines (59 loc) · 1.83 KB
/
sdf.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// A Signed Distance Field rendering demo, ported from https://github.com/floooh/sokol-samples/blob/master/sapp/sdf-sapp.c
// which in turn is based on https://iquilezles.org/articles/mandelbulb/ and https://www.shadertoy.com/view/ltfSWn
import sokol.sapp
import sokol.gfx
#include "@VMODROOT/sdf.h" # It should be generated with `v shader .`
fn C.sdf_shader_desc(gfx.Backend) &gfx.ShaderDesc
@[packed]
struct C.vs_params_t {
mut:
aspect f32
time f32
}
struct State {
mut:
pip gfx.Pipeline
bind gfx.Bindings
paction gfx.PassAction
params C.vs_params_t
}
fn init(mut state State) {
gfx.setup(sapp.create_desc())
fsq_verts := [f32(-1.0), -3.0, 3.0, 1.0, -1.0, 1.0]!
state.bind.vertex_buffers[0] = gfx.make_buffer(gfx.BufferDesc{
label: c'fsq vertices'
data: unsafe { gfx.Range{&fsq_verts[0], sizeof(fsq_verts)} }
})
mut pipeline := gfx.PipelineDesc{}
pipeline.layout.attrs[C.ATTR_vs_position].format = .float2
pipeline.shader = gfx.make_shader(C.sdf_shader_desc(gfx.query_backend()))
state.pip = gfx.make_pipeline(&pipeline)
// No need to clear the window, since the shader will overwrite the whole framebuffer
state.paction.colors[0].load_action = .dontcare
}
fn frame(mut state State) {
w, h := sapp.width(), sapp.height()
state.params.time += f32(sapp.frame_duration())
state.params.aspect = f32(w) / f32(h)
gfx.begin_pass(sapp.create_default_pass(state.paction))
gfx.apply_pipeline(state.pip)
gfx.apply_bindings(state.bind)
gfx.apply_uniforms(.vs, C.SLOT_vs_params, unsafe { gfx.Range{&state.params, sizeof(state.params)} })
gfx.draw(0, 3, 1)
gfx.end_pass()
gfx.commit()
}
fn main() {
sapp.run(sapp.Desc{
window_title: c'SDF Rendering'
width: 512
height: 512
frame_userdata_cb: frame
init_userdata_cb: init
cleanup_cb: gfx.shutdown
icon: sapp.IconDesc{
sokol_default: true
}
user_data: &State{}
})
}