You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DrawCube before DrawModel renders after it: rlgl batching, a transparent-texel decal, and the one-line flush (four-frame repro)
#6395
Summary. In a frame that calls DrawCube(...) and then DrawModelEx(...), the GPU renders the model first. DrawCube and the other immediate-mode shapes append to rlgl's render batch, which is flushed later (at EndMode3D/EndDrawing, on a texture, shader or blend-mode change, when the batch fills, or on an explicit rlDrawRenderBatchActive()), while DrawMesh renders at once through the mesh's own buffers. With opaque geometry the depth test hides the reordering. With a model whose texture has transparent texels it does not: the default shader has no alpha discard, so those fragments are blended away but still write depth, and the cube drawn "before" them (really after) fails the depth test behind them. You see the background through the model, with its opaque texels on top.
Frames (zig build run in the repo saves all four):
DrawCube, then a textured shell
the same with rlDrawRenderBatchActive() between
shell first, then the cube
depth writes off for the shell, no flush
the cube vanishes where the shell covers it
the cube wears the decal
the identical hole: the order the GPU saw in frame 1
the decal is lost instead
The fix we used is one call between the batched shapes and the models:
DrawCube(...);
rlDrawRenderBatchActive(); // the cube reaches the GPU nowDrawModelEx(decalShell, ...);
Turning depth writes off for the decal (frame 4) is not a substitute: without the flush the cube still renders after the shell and paints over it. An alpha-test shader (if (texelColor.a < 0.5) discard;) also resolves it, at the cost of a custom shader.
Where it came from. A game engine on raylib (via raylib-zig, raylib 6.0) drew its world's blocks with DrawCube and dressed a damaged block in a slightly larger textured cube model carrying a crack decal on a transparent ground. The cracks appeared over the grass behind the wall. The block's position, the code's draw order and the entity collector were all checked and all correct; comparing the frame with and without the shell was what separated the two, and rlgl's batching was the explanation.
Question for the maintainers. This is documented behaviour of rlgl, and rlDrawRenderBatchActive() is the intended tool. Would a sentence in the DrawMesh or DrawModel documentation ("renders immediately; shapes drawn before it through the batch reach the GPU only when the batch flushes") be welcome as a PR? It is the one line we would have wanted to read.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
A note on draw order that cost us an afternoon, with a four-frame repro: https://github.com/torial/raylib-batch-order
Summary. In a frame that calls
DrawCube(...)and thenDrawModelEx(...), the GPU renders the model first.DrawCubeand the other immediate-mode shapes append to rlgl's render batch, which is flushed later (atEndMode3D/EndDrawing, on a texture, shader or blend-mode change, when the batch fills, or on an explicitrlDrawRenderBatchActive()), whileDrawMeshrenders at once through the mesh's own buffers. With opaque geometry the depth test hides the reordering. With a model whose texture has transparent texels it does not: the default shader has no alpha discard, so those fragments are blended away but still write depth, and the cube drawn "before" them (really after) fails the depth test behind them. You see the background through the model, with its opaque texels on top.Frames (
zig build runin the repo saves all four):DrawCube, then a textured shellrlDrawRenderBatchActive()betweenThe fix we used is one call between the batched shapes and the models:
Turning depth writes off for the decal (frame 4) is not a substitute: without the flush the cube still renders after the shell and paints over it. An alpha-test shader (
if (texelColor.a < 0.5) discard;) also resolves it, at the cost of a custom shader.Where it came from. A game engine on raylib (via raylib-zig, raylib 6.0) drew its world's blocks with
DrawCubeand dressed a damaged block in a slightly larger textured cube model carrying a crack decal on a transparent ground. The cracks appeared over the grass behind the wall. The block's position, the code's draw order and the entity collector were all checked and all correct; comparing the frame with and without the shell was what separated the two, and rlgl's batching was the explanation.Question for the maintainers. This is documented behaviour of rlgl, and
rlDrawRenderBatchActive()is the intended tool. Would a sentence in theDrawMeshorDrawModeldocumentation ("renders immediately; shapes drawn before it through the batch reach the GPU only when the batch flushes") be welcome as a PR? It is the one line we would have wanted to read.All reactions