Skip to content

Commit

Permalink
gg,sokol,examples: add example of overriding _SGL_DEFAULT_MAX_VERTICE…
Browse files Browse the repository at this point in the history
…S in code
  • Loading branch information
spytheman committed Jan 6, 2025
1 parent 3c9d376 commit 7aa2fcb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
33 changes: 33 additions & 0 deletions examples/gg/many_thousands_of_circles_overriding_max_vertices.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import gg
import rand

// The flags here override the default limits for Sokol
#flag -D_SGL_DEFAULT_MAX_VERTICES=4194304
#flag -D_SGL_DEFAULT_MAX_COMMANDS=65536

// Without the flags, `max_circles` > 5040, will just show
// a blue screen without *any circles* drawn.
const max_circles = 10_000

fn main() {
gg.start(
window_title: 'Hello'
bg_color: gg.Color{50, 50, 150, 255}
width: 800
height: 600
frame_fn: fn (ctx &gg.Context) {
wsize := gg.window_size()
ctx.begin()
for _ in 0 .. max_circles {
rx := rand.int_in_range(0, wsize.width) or { 0 }
ry := rand.int_in_range(0, wsize.height) or { 0 }
cr := rand.u8()
cg := rand.u8()
cb := rand.u8()
ctx.draw_circle_filled(rx, ry, 10, gg.Color{cr, cg, cb, 255})
}
ctx.show_fps()
ctx.end()
}
)
}
29 changes: 28 additions & 1 deletion vlib/gg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,31 @@ fn frame(mut ctx gg.Context) {
ctx.draw_triangle_filled(450, 142, 530, 280, 370, 280, gx.red)
ctx.end()
}
```
```

## Troubleshooting

A common problem, if you draw a lot of primitive elements in the same
frame, is that there is a chance that your program can exceed the maximum
allowed amount of vertices and commands, imposed by `sokol`.
The symptom is that your frame will be suddenly black, after it becomes more complex.
Sokol's default for vertices is 131072.
Sokol's default for commands is 32768.

To solve that, you can try adding these lines at the top of your program:
`#flag -D_SGL_DEFAULT_MAX_VERTICES=4194304`
`#flag -D_SGL_DEFAULT_MAX_COMMANDS=65536`
You can see an example of that in:
https://github.com/vlang/v/blob/master/examples/gg/many_thousands_of_circles_overriding_max_vertices.v

Another approach is to use several draw passes, and limit the amount
of draw calls that you make in each, demonstrated in:
https://github.com/vlang/v/blob/master/examples/gg/many_thousands_of_circles.v

Another approach to that problem, is to draw everything yourself in a streaming
texture, then upload that streaming texture as a single draw command to the GPU.
You can see an example of that done in:
https://github.com/vlang/v/blob/master/examples/gg/random.v

A third approach, is to only upload your changing inputs to the GPU, and do all
the calculations and drawing there in shaders.
26 changes: 26 additions & 0 deletions vlib/sokol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,29 @@ fn main() {
audio.shutdown()
}
```

## Troubleshooting sokol apps

A common problem, if you draw a lot of primitive elements in the same frame,
is that there is a chance that your program can exceed the maximum
allowed amount of vertices and commands, imposed by `sokol`.

The symptom is that your frame will be suddenly black, after it
becomes more complex.

Sokol's default for vertices is 131072.
Sokol's default for commands is 32768.

To solve that, you can try adding these lines at the top of your program:
`#flag -D_SGL_DEFAULT_MAX_VERTICES=4194304`
`#flag -D_SGL_DEFAULT_MAX_COMMANDS=65536`
You can see an example of that in:
https://github.com/vlang/v/blob/master/examples/gg/examples/gg/many_thousands_of_circles_overriding_max_vertices.v

Another approach to that problem, is to draw everything yourself in a streaming
texture, then upload that streaming texture as a single draw command to the GPU.
You can see an example of that done in:
https://github.com/vlang/v/blob/master/examples/gg/random.v

A third approach, is to only upload your changing inputs to the GPU, and do all
the calculations and drawing there in shaders.

0 comments on commit 7aa2fcb

Please sign in to comment.