Skip to content

Commit

Permalink
Mind the texture alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 27, 2024
1 parent f6f05fc commit 92fed3a
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions TextureArray/Sources/shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void update(void *data) {
{
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 All @@ -52,7 +52,7 @@ static void update(void *data) {
{
kope_g5_image_copy_buffer source = {0};
source.buffer = &image_buffer2;
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 @@ -113,25 +113,49 @@ int kickstart(int argc, char **argv) {

{
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);
}

{
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_buffer2);

// 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_buffer2), "parrot2.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_buffer2);
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_buffer2);
}

Expand Down

0 comments on commit 92fed3a

Please sign in to comment.