From 4a58046caac569d2e8c87c843d8181c310fe3ab8 Mon Sep 17 00:00:00 2001 From: Aitor Camacho Date: Fri, 5 Dec 2025 20:49:24 +0900 Subject: [PATCH] kk: Fix emulated format's swizzle Signed-off-by: Aitor Camacho Part-of: --- src/kosmickrisp/vulkan/kk_buffer_view.c | 8 ++++---- src/kosmickrisp/vulkan/kk_cmd_draw.c | 4 ++-- src/kosmickrisp/vulkan/kk_format.c | 8 ++++---- src/kosmickrisp/vulkan/kk_format.h | 5 +++-- src/kosmickrisp/vulkan/kk_image_layout.c | 8 ++++---- src/kosmickrisp/vulkan/kk_image_view.c | 2 +- src/kosmickrisp/vulkan/kk_shader.c | 14 +++++++------- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/kosmickrisp/vulkan/kk_buffer_view.c b/src/kosmickrisp/vulkan/kk_buffer_view.c index c2497474687..dffb9aa8473 100644 --- a/src/kosmickrisp/vulkan/kk_buffer_view.c +++ b/src/kosmickrisp/vulkan/kk_buffer_view.c @@ -89,10 +89,10 @@ kk_CreateBufferView(VkDevice _device, const VkBufferViewCreateInfo *pCreateInfo, .format = {.pipe = p_format, .mtl = supported_format->mtl_pixel_format}, .swizzle = { - .red = supported_format->swizzle.red, - .green = supported_format->swizzle.green, - .blue = supported_format->swizzle.blue, - .alpha = supported_format->swizzle.alpha, + .red = supported_format->unswizzle.red, + .green = supported_format->unswizzle.green, + .blue = supported_format->unswizzle.blue, + .alpha = supported_format->unswizzle.alpha, }, .linear_stride_B = view->vk.range, }; diff --git a/src/kosmickrisp/vulkan/kk_cmd_draw.c b/src/kosmickrisp/vulkan/kk_cmd_draw.c index 6cf9a008547..d99dccecef1 100644 --- a/src/kosmickrisp/vulkan/kk_cmd_draw.c +++ b/src/kosmickrisp/vulkan/kk_cmd_draw.c @@ -155,8 +155,8 @@ vk_clear_color_value_to_mtl_clear_color(union VkClearColorValue color, const struct kk_va_format *supported_format = kk_get_va_format(format); struct mtl_clear_color swizzled_color; for (uint32_t i = 0u; i < 4; ++i) - swizzled_color.channel[i] = - value.channel[supported_format->swizzle.channels[i]]; + swizzled_color.channel[supported_format->unswizzle.channels[i]] = + value.channel[i]; return swizzled_color; } diff --git a/src/kosmickrisp/vulkan/kk_format.c b/src/kosmickrisp/vulkan/kk_format.c index adc65ef4bbc..933c5a1ec59 100644 --- a/src/kosmickrisp/vulkan/kk_format.c +++ b/src/kosmickrisp/vulkan/kk_format.c @@ -105,7 +105,7 @@ } #define MTL_SWIZZLE_IDENTITY \ - .swizzle = { \ + .unswizzle = { \ .red = PIPE_SWIZZLE_X, \ .green = PIPE_SWIZZLE_Y, \ .blue = PIPE_SWIZZLE_Z, \ @@ -113,7 +113,7 @@ } #define MTL_SWIZZLE_ABGR \ - .swizzle = { \ + .unswizzle = { \ .red = PIPE_SWIZZLE_W, \ .green = PIPE_SWIZZLE_Z, \ .blue = PIPE_SWIZZLE_Y, \ @@ -121,7 +121,7 @@ } #define MTL_SWIZZLE_BGRA \ - .swizzle = { \ + .unswizzle = { \ .red = PIPE_SWIZZLE_Z, \ .green = PIPE_SWIZZLE_Y, \ .blue = PIPE_SWIZZLE_X, \ @@ -129,7 +129,7 @@ } #define MTL_SWIZZLE_RGB1 \ - .swizzle = { \ + .unswizzle = { \ .red = PIPE_SWIZZLE_X, \ .green = PIPE_SWIZZLE_Y, \ .blue = PIPE_SWIZZLE_Z, \ diff --git a/src/kosmickrisp/vulkan/kk_format.h b/src/kosmickrisp/vulkan/kk_format.h index 64541a659e5..b5288e6e437 100644 --- a/src/kosmickrisp/vulkan/kk_format.h +++ b/src/kosmickrisp/vulkan/kk_format.h @@ -17,7 +17,8 @@ enum pipe_format; enum mtl_pixel_format; struct kk_va_format { - /* Would love to use enum pipe_swizzle, but it's bigger than the required + /* Transforms from the native format to the emulated format. + * Would love to use enum pipe_swizzle, but it's bigger than the required * type for util_format_compose_swizzles... */ struct { union { @@ -29,7 +30,7 @@ struct kk_va_format { }; uint8_t channels[4]; }; - } swizzle; + } unswizzle; uint32_t mtl_pixel_format; uint8_t bit_widths; uint8_t filter : 1; diff --git a/src/kosmickrisp/vulkan/kk_image_layout.c b/src/kosmickrisp/vulkan/kk_image_layout.c index 06d4ed8754a..fce9234d460 100644 --- a/src/kosmickrisp/vulkan/kk_image_layout.c +++ b/src/kosmickrisp/vulkan/kk_image_layout.c @@ -94,10 +94,10 @@ kk_image_layout_init(const struct kk_device *dev, create_info->usage, create_info->flags, supported_format->atomic); layout->format.pipe = format; layout->format.mtl = supported_format->mtl_pixel_format; - layout->swizzle.red = supported_format->swizzle.red; - layout->swizzle.green = supported_format->swizzle.green; - layout->swizzle.blue = supported_format->swizzle.blue; - layout->swizzle.alpha = supported_format->swizzle.alpha; + layout->swizzle.red = supported_format->unswizzle.red; + layout->swizzle.green = supported_format->unswizzle.green; + layout->swizzle.blue = supported_format->unswizzle.blue; + layout->swizzle.alpha = supported_format->unswizzle.alpha; layout->sample_count_sa = create_info->samples; mtl_heap_texture_size_and_align_with_descriptor(dev->mtl_handle, layout); diff --git a/src/kosmickrisp/vulkan/kk_image_view.c b/src/kosmickrisp/vulkan/kk_image_view.c index ed8bd485972..49fab5d6926 100644 --- a/src/kosmickrisp/vulkan/kk_image_view.c +++ b/src/kosmickrisp/vulkan/kk_image_view.c @@ -119,7 +119,7 @@ kk_image_view_init(struct kk_device *dev, struct kk_image_view *view, vk_swizzle_to_pipe(view->vk.swizzle.g), vk_swizzle_to_pipe(view->vk.swizzle.b), vk_swizzle_to_pipe(view->vk.swizzle.a)}; - util_format_compose_swizzles(supported_format->swizzle.channels, + util_format_compose_swizzles(supported_format->unswizzle.channels, view_swizzle, view_layout.swizzle.channels); /* When sampling a depth/stencil texture Metal returns (d, d, d, 1), but diff --git a/src/kosmickrisp/vulkan/kk_shader.c b/src/kosmickrisp/vulkan/kk_shader.c index e19df761439..519b6f3899f 100644 --- a/src/kosmickrisp/vulkan/kk_shader.c +++ b/src/kosmickrisp/vulkan/kk_shader.c @@ -252,21 +252,21 @@ kk_nir_swizzle_fragment_output(nir_builder *b, nir_intrinsic_instr *intrin, /* Check if we have to apply any swizzle */ if (!supported_format->is_native) { - unsigned channel_swizzle[] = { - supported_format->swizzle.red, supported_format->swizzle.green, - supported_format->swizzle.blue, supported_format->swizzle.alpha}; + unsigned channel_unswizzle[] = { + supported_format->unswizzle.red, supported_format->unswizzle.green, + supported_format->unswizzle.blue, supported_format->unswizzle.alpha}; if (intrin->intrinsic == nir_intrinsic_store_output) { + unsigned channel_swizzle[4] = {0u}; + for (uint32_t i = 0u; i < 4; ++i) + channel_swizzle[channel_unswizzle[i]] = i; + b->cursor = nir_before_instr(&intrin->instr); nir_def *to_replace = intrin->src[0].ssa; nir_def *swizzled = nir_swizzle(b, to_replace, channel_swizzle, to_replace->num_components); nir_src_rewrite(&intrin->src[0], swizzled); } else { - unsigned channel_unswizzle[4] = {0u}; - for (uint32_t i = 0u; i < 4; ++i) - channel_unswizzle[channel_swizzle[i]] = i; - b->cursor = nir_after_instr(&intrin->instr); nir_def *to_replace = &intrin->def; nir_def *swizzled = nir_swizzle(b, to_replace, channel_unswizzle,