[PATCH v2 5/7] drm/vkms: add a 3D LUT colorop

From: Oarora Etimis

Date: Sat Nov 28 2026 - 13:20:38 EST


Add a 3D LUT colorop of 17x17x17 entries, interpolated tetrahedrally.
The composer locates the cell of the pixel and weights the four corners
of the tetrahedron its fractions select. Red is the fastest moving
index, as the uapi documents.

The existing colorops and their order are unchanged.

Signed-off-by: Oarora Etimis <oaroraetimis@xxxxxxxxx>
---
drivers/gpu/drm/vkms/vkms_colorop.c | 19 +++++
drivers/gpu/drm/vkms/vkms_composer.c | 140 +++++++++++++++++++++++++++++++++++
drivers/gpu/drm/vkms/vkms_composer.h | 2 +
drivers/gpu/drm/vkms/vkms_drv.h | 2 +
4 files changed, 163 insertions(+)

diff --git a/drivers/gpu/drm/vkms/vkms_colorop.c b/drivers/gpu/drm/vkms/vkms_colorop.c
index fdbdad103412..9ffc6e354206 100644
--- a/drivers/gpu/drm/vkms/vkms_colorop.c
+++ b/drivers/gpu/drm/vkms/vkms_colorop.c
@@ -138,6 +138,25 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr

drm_colorop_set_next_property(ops[i - 1], ops[i]);

+ i++;
+
+ /* 7th op: 3D LUT */
+ ops[i] = kzalloc_obj(*ops[i]);
+ if (!ops[i]) {
+ drm_err(dev, "KMS: Failed to allocate colorop\n");
+ ret = -ENOMEM;
+ goto cleanup;
+ }
+
+ ret = drm_plane_colorop_3dlut_init(dev, ops[i], plane, &vkms_colorop_funcs,
+ VKMS_LUT3D_SIZE,
+ DRM_COLOROP_LUT3D_INTERPOLATION_TETRAHEDRAL,
+ DRM_COLOROP_FLAG_ALLOW_BYPASS);
+ if (ret)
+ goto cleanup;
+
+ drm_colorop_set_next_property(ops[i - 1], ops[i]);
+
list->name = kasprintf(GFP_KERNEL, "Color Pipeline %d", ops[0]->base.id);

return 0;
diff --git a/drivers/gpu/drm/vkms/vkms_composer.c b/drivers/gpu/drm/vkms/vkms_composer.c
index 56749ecdd17f..671c16854a49 100644
--- a/drivers/gpu/drm/vkms/vkms_composer.c
+++ b/drivers/gpu/drm/vkms/vkms_composer.c
@@ -255,6 +255,129 @@ VISIBLE_IF_KUNIT u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *
}
EXPORT_SYMBOL_IF_KUNIT(apply_lut32_to_channel_value);

+struct lut3d_cell {
+ u32 r_index, g_index, b_index;
+ u32 r_fraction, g_fraction, b_fraction;
+};
+
+/*
+ * Locate the cell of the 3D LUT a pixel falls in and the position inside it.
+ * The three axes share the same coordinates, so they are computed once for the
+ * pixel instead of once for each channel.
+ */
+static void lut3d_locate_cell(struct lut3d_cell *cell, u32 size,
+ u16 r, u16 g, u16 b)
+{
+ u32 max_index = size - 1;
+ u64 position;
+
+ position = (u64)r * max_index;
+ cell->r_index = div_u64_rem(position, U16_MAX, &cell->r_fraction);
+
+ position = (u64)g * max_index;
+ cell->g_index = div_u64_rem(position, U16_MAX, &cell->g_fraction);
+
+ position = (u64)b * max_index;
+ cell->b_index = div_u64_rem(position, U16_MAX, &cell->b_fraction);
+
+ if (cell->r_index >= max_index) {
+ cell->r_index = max_index - 1;
+ cell->r_fraction = U16_MAX;
+ }
+
+ if (cell->g_index >= max_index) {
+ cell->g_index = max_index - 1;
+ cell->g_fraction = U16_MAX;
+ }
+
+ if (cell->b_index >= max_index) {
+ cell->b_index = max_index - 1;
+ cell->b_fraction = U16_MAX;
+ }
+}
+
+/*
+ * Tetrahedral interpolation of one channel in a 3D LUT. The fractions pick one
+ * of the six tetrahedra the cell is split into, and the four corners of that
+ * tetrahedron are weighted.
+ */
+static u16 apply_3dlut_to_channel_value(const struct drm_color_lut32 *lut, u32 size,
+ enum lut_channel channel,
+ const struct lut3d_cell *cell)
+{
+ const struct drm_color_lut32 *v000;
+ u64 sum;
+ u16 v000_c, v100_c, v010_c, v001_c;
+ u16 v110_c, v101_c, v011_c, v111_c;
+
+ v000 = &lut[cell->r_index + size * (cell->g_index + size * cell->b_index)];
+
+ v000_c = lut32_channel_value(v000, channel);
+ v100_c = lut32_channel_value(v000 + 1, channel);
+ v010_c = lut32_channel_value(v000 + size, channel);
+ v001_c = lut32_channel_value(v000 + size * size, channel);
+ v110_c = lut32_channel_value(v000 + size + 1, channel);
+ v101_c = lut32_channel_value(v000 + size * size + 1, channel);
+ v011_c = lut32_channel_value(v000 + size * size + size, channel);
+ v111_c = lut32_channel_value(v000 + size * size + size + 1, channel);
+
+ if (cell->r_fraction >= cell->g_fraction) {
+ if (cell->g_fraction >= cell->b_fraction)
+ sum = (u64)v000_c * (U16_MAX - cell->r_fraction) +
+ (u64)v100_c * (cell->r_fraction - cell->g_fraction) +
+ (u64)v110_c * (cell->g_fraction - cell->b_fraction) +
+ (u64)v111_c * cell->b_fraction;
+ else if (cell->r_fraction >= cell->b_fraction)
+ sum = (u64)v000_c * (U16_MAX - cell->r_fraction) +
+ (u64)v100_c * (cell->r_fraction - cell->b_fraction) +
+ (u64)v101_c * (cell->b_fraction - cell->g_fraction) +
+ (u64)v111_c * cell->g_fraction;
+ else
+ sum = (u64)v000_c * (U16_MAX - cell->b_fraction) +
+ (u64)v001_c * (cell->b_fraction - cell->r_fraction) +
+ (u64)v101_c * (cell->r_fraction - cell->g_fraction) +
+ (u64)v111_c * cell->g_fraction;
+ } else {
+ if (cell->r_fraction >= cell->b_fraction)
+ sum = (u64)v000_c * (U16_MAX - cell->g_fraction) +
+ (u64)v010_c * (cell->g_fraction - cell->r_fraction) +
+ (u64)v110_c * (cell->r_fraction - cell->b_fraction) +
+ (u64)v111_c * cell->b_fraction;
+ else if (cell->g_fraction >= cell->b_fraction)
+ sum = (u64)v000_c * (U16_MAX - cell->g_fraction) +
+ (u64)v010_c * (cell->g_fraction - cell->b_fraction) +
+ (u64)v011_c * (cell->b_fraction - cell->r_fraction) +
+ (u64)v111_c * cell->r_fraction;
+ else
+ sum = (u64)v000_c * (U16_MAX - cell->b_fraction) +
+ (u64)v001_c * (cell->b_fraction - cell->g_fraction) +
+ (u64)v011_c * (cell->g_fraction - cell->r_fraction) +
+ (u64)v111_c * cell->r_fraction;
+ }
+
+ return (u16)min_t(u64, DIV_ROUND_CLOSEST_ULL(sum, U16_MAX), U16_MAX);
+}
+
+VISIBLE_IF_KUNIT void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
+ const struct drm_color_lut32 *lut,
+ u32 size)
+{
+ struct lut3d_cell cell;
+ u16 r = clamp_val(pixel->r, 0, 0xffff);
+ u16 g = clamp_val(pixel->g, 0, 0xffff);
+ u16 b = clamp_val(pixel->b, 0, 0xffff);
+
+ if (size < 2)
+ return;
+
+ lut3d_locate_cell(&cell, size, r, g, b);
+
+ pixel->r = apply_3dlut_to_channel_value(lut, size, LUT_RED, &cell);
+ pixel->g = apply_3dlut_to_channel_value(lut, size, LUT_GREEN, &cell);
+ pixel->b = apply_3dlut_to_channel_value(lut, size, LUT_BLUE, &cell);
+}
+EXPORT_SYMBOL_IF_KUNIT(apply_3dlut_to_pixel);
+

static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buffer *output_buffer)
{
@@ -346,6 +469,20 @@ static void apply_1d_lut(struct pixel_argb_s32 *pixel,
clamp_val(pixel->b, 0, 0xffff), LUT_BLUE);
}

+static void apply_3d_lut(struct pixel_argb_s32 *pixel,
+ const struct vkms_colorop_state *state)
+{
+ const struct drm_colorop *colorop = state->colorop;
+ const struct drm_color_lut32 *lut;
+
+ if (!state->data)
+ return;
+
+ lut = state->data->data;
+
+ apply_3dlut_to_pixel(pixel, lut, colorop->size);
+}
+
static void apply_colorop(struct pixel_argb_s32 *pixel,
const struct vkms_colorop_state *state)
{
@@ -362,6 +499,9 @@ static void apply_colorop(struct pixel_argb_s32 *pixel,
case DRM_COLOROP_1D_LUT:
apply_1d_lut(pixel, state);
break;
+ case DRM_COLOROP_3D_LUT:
+ apply_3d_lut(pixel, state);
+ break;
case DRM_COLOROP_FIXED_MATRIX:
break;
default:
diff --git a/drivers/gpu/drm/vkms/vkms_composer.h b/drivers/gpu/drm/vkms/vkms_composer.h
index 6da85efc5524..28323df662a4 100644
--- a/drivers/gpu/drm/vkms/vkms_composer.h
+++ b/drivers/gpu/drm/vkms/vkms_composer.h
@@ -24,6 +24,8 @@ u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_val
enum lut_channel channel);
u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *lut, u32 lut_size,
u16 channel_value, enum lut_channel channel);
+void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
+ const struct drm_color_lut32 *lut, u32 size);
void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_color_ctm_3x4 *matrix);
#endif

diff --git a/drivers/gpu/drm/vkms/vkms_drv.h b/drivers/gpu/drm/vkms/vkms_drv.h
index 05fba864cf36..d975cb684fd9 100644
--- a/drivers/gpu/drm/vkms/vkms_drv.h
+++ b/drivers/gpu/drm/vkms/vkms_drv.h
@@ -38,6 +38,8 @@
/* Entries in the 1D LUT colorop of a plane's color pipeline. */
#define VKMS_LUT1D_SIZE 256

+/* Entries per dimension in the 3D LUT colorop of a plane's color pipeline. */
+#define VKMS_LUT3D_SIZE 17
/**
* struct vkms_frame_info - Structure to store the state of a frame
*

--
2.56.1