gboisse / gfx

A minimalist and easy to use graphics API.
MIT License
502 stars 36 forks source link

Dumping textures to file #15

Closed sayan1an closed 2 years ago

sayan1an commented 2 years ago

Well, what are my options here?

I don't see a gfxCommandCopyTextureToBuffer(), but I can copy Gpu-Buffers to Cpu memory and access it for processing.

Maybe this is an option - copy texture (gpu) to gpu-buffer with a shader, then copy gpu-buffer to mapped cpu-memory?

Is there a better option?

gboisse commented 2 years ago

Hey, the solution for this is to allocate 2 buffers the size of your texture:

Now, write a shader that reads from your texture and writes to buffer1 as a RWBuffer<...>. Then, perform a copy from buffer1 to buffer2 using gfxCommandCopyBuffer(gfx, buffer2, buffer1). You can now read the data on the CPU by calling gfxBufferGetData(gfx, buffer2).

Note however that you'll need to ensure that the GPU has completed the copy. Meaning you'll either need to wait for at least 3 frames to complete (i.e., kGfxConstant_BackBufferCount) or call gfxFinish(), which will stall the CPU until the GPU has completed its queued commands.

gboisse commented 2 years ago

Oops, read a bit too quickly but you're already describing the solution in your post 🙂

This is the best option indeed.

sayan1an commented 2 years ago

Thank you for the detailed confirmation. I didn't think about the frame queuing issue, so thanks again :)