Skip to content

Commit

Permalink
Use the texture-stride in the Texture sample
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 25, 2024
1 parent c4b93ab commit b5cc419
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Texture/Sources/texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static void update(void *data) {
if (first_update) {
kope_g5_image_copy_buffer source = {0};
source.buffer = &image_buffer;
source.bytes_per_row = 250 * 4;
source.bytes_per_row = kope_g5_device_align_texture_row_bytes(&device, 250 * 4);

kope_g5_image_copy_texture destination = {0};
destination.texture = &texture;
Expand Down Expand Up @@ -107,15 +107,29 @@ int kickstart(int argc, char **argv) {
#endif

kope_g5_buffer_parameters buffer_parameters;
buffer_parameters.size = 250 * 250 * 4;
buffer_parameters.size = kope_g5_device_align_texture_row_bytes(&device, 250 * 4) * 250;
buffer_parameters.usage_flags = KOPE_G5_BUFFER_USAGE_CPU_WRITE;
kope_g5_device_create_buffer(&device, &buffer_parameters, &image_buffer);

// kinc_image can not load images with row-alignment directly because stb_image doesn't support that :-(
uint32_t *image_data = (uint32_t *)malloc(250 * 4 * 250);
assert(image_data != NULL);

kinc_image_t image;
kinc_image_init_from_file(&image, kope_g5_buffer_lock(&image_buffer), "parrot.png");
kinc_image_init_from_file(&image, image_data, "parrot.png");
kinc_image_destroy(&image);

uint32_t stride = kope_g5_device_align_texture_row_bytes(&device, 250 * 4) / 4;
uint32_t *gpu_image_data = (uint32_t *)kope_g5_buffer_lock(&image_buffer);
for (int y = 0; y < 250; ++y) {
for (int x = 0; x < 250; ++x) {
gpu_image_data[y * stride + x] = image_data[y * 250 + x];
}
}
kope_g5_buffer_unlock(&image_buffer);

free(image_data);

kope_g5_texture_parameters texture_parameters;
texture_parameters.width = 250;
texture_parameters.height = 250;
Expand Down

0 comments on commit b5cc419

Please sign in to comment.