Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sokol_gfx.h: Partial buffer/image update #323

Open
lithiumtoast opened this issue Jun 15, 2020 · 1 comment
Open

sokol_gfx.h: Partial buffer/image update #323

lithiumtoast opened this issue Jun 15, 2020 · 1 comment

Comments

@lithiumtoast
Copy link

lithiumtoast commented Jun 15, 2020

Currently, I'm not aware if there is a way to do partial updates with an image or buffer. What I'm looking for is something like the following:

void sg_update_buffer(sg_buffer buf, const void* data_ptr, int data_size, int start_index);

My use case is I have a large 2d texture like a texture atlas or tiled map which I only want to update a sub-region once per frame.

Is there already a way to do what I want with the current API?

@floooh
Copy link
Owner

floooh commented Jun 15, 2020

I haven't come up with a good way to do partial image updates yet, currently the only solution is to maintain a complete copy of the image data in memory, accumulate partial updates into this with the CPU, and then do a complete sg_update_image().

This is basically what fontstash is doing to dynamically update the font atlas, it maintains a complete CPU-side copy of the font atlas, and sets a dirty flag to tell the backend API to update the font texture:

sokol/util/sokol_fontstash.h

Lines 1000 to 1011 in 57515a1

SOKOL_API_IMPL void sfons_flush(FONScontext* ctx) {
SOKOL_ASSERT(ctx && ctx->params.userPtr);
_sfons_t* sfons = (_sfons_t*) ctx->params.userPtr;
if (sfons->img_dirty) {
sfons->img_dirty = false;
sg_image_content content;
memset(&content, 0, sizeof(content));
content.subimage[0][0].ptr = ctx->texData;
content.subimage[0][0].size = sfons->width * sfons->height;
sg_update_image(sfons->img, &content);
}
}

For buffers, there's an "append API" (sg_append_buffer), this allows to append new data to a buffer and interleave the data updates with draw calls using that newly appended data. This is only useful for per-frame updated data though (it uses the MAP_WRITE_DISCARD strategy in D3D11).

Long term I've got some ideas how to clean up the data update functions (add a map/unmap API for transient per-frame data, and a copy/blit API for "persistent" uploads into buffers and images), but not today or tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants