Skip to content

Commit

Permalink
Align the copy-back-texture
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 24, 2024
1 parent 103c007 commit 4738ffe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
72 changes: 37 additions & 35 deletions screenshot.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

static kope_g5_buffer screenshot_buffer;

static void screenshot_init_buffer(kope_g5_device *device, int width, int height) {
kope_g5_buffer_parameters buffer_parameters;
buffer_parameters.size = width * 4 * height;
buffer_parameters.usage_flags = KOPE_G5_BUFFER_USAGE_CPU_READ;
kope_g5_device_create_buffer(device, &buffer_parameters, &screenshot_buffer);
}

static void screenshot_take(kope_g5_device *device, kope_g5_command_list *list, kope_g5_texture *framebuffer, int width, int height) {
kope_g5_image_copy_texture source = {0};
source.texture = framebuffer;
source.mip_level = 0;

kope_g5_image_copy_buffer destination = {0};
destination.buffer = &screenshot_buffer;
destination.bytes_per_row = width * 4;

kope_g5_command_list_copy_texture_to_buffer(list, &source, &destination, width, height, 1);

kope_g5_device_execute_command_list(device, list);

kope_g5_device_wait_until_idle(device);

uint8_t *pixels = (uint8_t *)kope_g5_buffer_lock(&screenshot_buffer);

stbi_write_png("test.png", width, height, 4, pixels, width * 4);

kope_g5_buffer_unlock(&screenshot_buffer);

exit(0);
}
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include <kope/util/align.h>

static kope_g5_buffer screenshot_buffer;

static void screenshot_init_buffer(kope_g5_device *device, int width, int height) {
kope_g5_buffer_parameters buffer_parameters;
buffer_parameters.size = kope_g5_device_align_texture_row_bytes(device, width * 4) * height;
buffer_parameters.usage_flags = KOPE_G5_BUFFER_USAGE_CPU_READ;
kope_g5_device_create_buffer(device, &buffer_parameters, &screenshot_buffer);
}

static void screenshot_take(kope_g5_device *device, kope_g5_command_list *list, kope_g5_texture *framebuffer, int width, int height) {
kope_g5_image_copy_texture source = {0};
source.texture = framebuffer;
source.mip_level = 0;

kope_g5_image_copy_buffer destination = {0};
destination.buffer = &screenshot_buffer;
destination.bytes_per_row = kope_g5_device_align_texture_row_bytes(device, width * 4);

kope_g5_command_list_copy_texture_to_buffer(list, &source, &destination, width, height, 1);

kope_g5_device_execute_command_list(device, list);

kope_g5_device_wait_until_idle(device);

uint8_t *pixels = (uint8_t *)kope_g5_buffer_lock(&screenshot_buffer);

stbi_write_png("test.png", width, height, 4, pixels, width * 4);

kope_g5_buffer_unlock(&screenshot_buffer);

exit(0);
}

0 comments on commit 4738ffe

Please sign in to comment.