fragcolor-xyz / shards

High-performance, multi-platform, type-safe programming language designed for visual & AI assisted development
BSD 3-Clause "New" or "Revised" License
68 stars 14 forks source link

Convert GFX texture into an image #495

Open Kryptos-FR opened 1 year ago

guusw commented 1 year ago

Hey team! Please add your planning poker estimate with Zenhub @Kryptos-FR @sinkingsugar @tdbe

guusw commented 1 year ago

Quote from some discussion:

This should be a shard that takes a texture and basically runs this code:

WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(
    device, &(WGPUCommandEncoderDescriptor){.label = NULL});

wgpuCommandEncoderCopyTextureToBuffer(
    encoder,
    &(WGPUImageCopyTexture){
        .texture = texture,
        .mipLevel = 0,
        .origin =
            (WGPUOrigin3D){
                .x = 0,
                .y = 0,
                .z = 0,
            },
    },
    &(WGPUImageCopyBuffer){
        .buffer = outputBuffer,
        .layout =
            (WGPUTextureDataLayout){
                .offset = 0,
                .bytesPerRow = bufferDimensions.padded_bytes_per_row,
                .rowsPerImage = 0,
            }},
    &textureExtent);

WGPUQueue queue = wgpuDeviceGetQueue(device);
WGPUCommandBuffer cmdBuffer = wgpuCommandEncoderFinish(
    encoder, &(WGPUCommandBufferDescriptor){.label = NULL});
wgpuQueueSubmit(queue, 1, &cmdBuffer);

wgpuBufferMapAsync(outputBuffer, WGPUMapMode_Read, 0, bufferSize,
                    readBufferMap, NULL);
wgpuDevicePoll(device, true, NULL);

uint8_t *data =
    (uint8_t *)wgpuBufferGetMappedRange(outputBuffer, 0, bufferSize);

const char *filename = "red.png";
save_png(filename, data, &bufferDimensions);

wgpuBufferUnmap(outputBuffer);

from the (src\gfx\rust\wgpu-native\examples\capture\main.c) wgpu-native sample