Closed trace-andreason closed 1 month ago
Ah I see. You don't actually need to do that.
You can create the texture in two steps like I do here in the loadpng-sapp sample:
first in the init-callback, call sg.allocImage()
which "pre-allocates" an image object handle without an actual texture object behind it: https://github.com/floooh/sokol-samples/blob/7b85538bb974eecb12027db9b5e66f274797461a/sapp/loadpng-sapp.c#L61-L66
in the per-frame code, you can simply use this handle as if it would be a valid image, sokol-gfx will skip any draw calls with invalid handles: https://github.com/floooh/sokol-samples/blob/7b85538bb974eecb12027db9b5e66f274797461a/sapp/loadpng-sapp.c#L195-L224
in the fetch callback, call sg.initImage()
to complete the image creation, this takes the handle from the allocImage() call, and the usual sg.ImageDesc struct: https://github.com/floooh/sokol-samples/blob/7b85538bb974eecb12027db9b5e66f274797461a/sapp/loadpng-sapp.c#L175-L184
...next time the frame callback is called, the image handle will be valid and rendering works as expected.
The vanilla sg.makeImage()
function is basically alloc + init in the same call.
You can also explicitly skip rendering by testing if the image is valid, smth like:
if (sg.queryImageState(img) == .VALID) {
// image has been initialized and it's valid to render
} else {
// image still loading, or failed to load
}
...theoretically the same works for all resource types, but is mainly intended for images and buffers.
Also see this documentation blurb: https://github.com/floooh/sokol/blob/c2d01bc25c58be460787d5f027eed73d1c036e6f/sokol_gfx.h#L1276-L1437
Ah ok, makes sense. Interesting in the openGL examples I'm working from they actually have the same comment copied from that example presumably. Wonder why they felt the need to define a header 🤔
Tbf the two-step initialization and deinitialization of sokol-gfx resource objects isn't a prominent feature, and also had some inconsistencies until sometime last year.
When I'm using sokol fetch to get a texture, I need to initialize the texture first with a placeholder texture. If you don't initialize the texture, because fetch won't finish on the first frame, your program crashes. It would be nice to have a function like this so everyone doesn't have to roll their own temporary texture binding function: https://github.com/zeromake/learnopengl-examples/blob/master/libs/sokol/sokol_helper.h
I'm working in zig, so I just wrote this:
So its definitely not hard to work around, but I think that if everyone is going to need to do this when using fetch for textures, it might be good to have a more canonical solution to this problem.