Skip to contentSkip to conversation

Enable 3D LUT to AMD display drivers

RFC PATCH In review Melissa Wen 2 participants 11 messages Sep 6, 2022 – Sep 29, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover Enable 3D LUT to AMD display drivers

Hi,

From all feedback at [3DLUT_RFC] and an extensive AMD driver examination, here I am back with a first attempt to wire up a user 3D LUT (post-blending) to DC interface via DM CRTC color mgmt :)

I'm following some specific approaches to handle user shaper LUT and user 3D LUT that I would like to validate if the path taken is correct.

I used a modified version [igt_tests] of Ville's IGT 3D LUT test to verify that the shaper and 3D LUT programming is happening. However, I still have doubts about hw behavior and DC MPC's current implementation for 3D LUT.

Despite some initial patches for code cleanup and DRM interface, my focus here is the inclusion of a user 3D LUT in the Display Manager, which is done in the last five patches of this series:

- drm/amd/display: enable DRM shaper and 3D LUT properties
- drm/amd/display: update lut3d and shaper lut to stream
- drm/amd/display: add user shaper LUT support to amdgpu_dm color pipeline
- drm/amd/display: add user 3D LUT support to the amdgpu_dm color pipeline
- drm/amd/display: encapsulate atomic regamma operation

Things to take into account:

- 3D LUT (and shaper LUT) is only available in the atomic pipeline (I didn't work on any implicit conversions that are done in the legacy path)

- Therefore, I'm not doing any implicit conversions for shaper LUT considering the input space, which means: it's set or not. When there is no shaper LUT, it's set to BYPASS, but unfortunately, it seems that the BYPASS mode for shaper LUT is not supported in the current DC dcn30_set_mpc_shaper_3dlut(), since it returns false when mpc3_program_shaper returns false (no params). Is the combination of a user 3D LUT with a bypassed shaper LUT accepted by the hw?

- I also see in dcn30_set_mpc_shaper_3dlut() that some bits need to be set in lut3d_func to have the 3D LUT programmed on the MPC block. In this sense, I used the dc_acquire_release_mpc_3dlut() function to get the lut3d_func from the resource pool, but I'm not sure if the timing to acquire and release the lut3d_func from the resource pool is correct (and if I can really use it directly or I should make a copy).

- Still, on this topic, I use for lut3d the same bit.out_tf to update the stream in the commit_tail because it triggers .set_output_transfer_func that is in charge of setting both OGAM and 3D LUT on MPC. There is a chance I got it wrong here, so I appreciate any input on this topic.

- Finally, in set_output_transfer_func, AFAIU, even if a user OGAM is set, it won't be programmed if the shaper LUT and 3D LUT programming are successful. However, if shaper/3DLUT programming fails, OGAM can be considered. Should DM only accept DRM regamma if no DRM 3D LUT is passed, or allowing the programming of both is still desirable?

Regarding the other patches:

- drm/drm_color_mgmt: add 3D LUT to color mgmt properties
- drm/drm_color_mgmt: add shaper LUT to color mgmt properties

Here, an initial DRM 3D LUT interface is exposed to enable the entire kernel path and is only available for the atomic pipeline. So far, it only includes the LUT data and its size, but improvements on this interface may also add stride and bit depth [VA_API]. Additionally, I'm aware of the current work on exposing a color pipeline API [KMS_pipe_API].

I'm including some minor changes in this series that I made to better understand the current DM color mgmt behavior:

- drm/amd/display: add comments to describe DM crtc color mgmt behavior
- drm/amd/display: remove unused regamma condition

...but, there are other code cleanup patches that I'm not including in this series to avoid unnecessary noise.

You can check the entire work here:
https://gitlab.freedesktop.org/mwen/linux-amd/-/commits/drm_lut3d

[igt_tests] IGT exploratory tests here:
https://gitlab.freedesktop.org/mwen/igt-gpu-tools/-/commits/kms_color_3dlut

[3DLUT_RFC] https://lore.kernel.org/amd-gfx/[email protected]/
[VA_API] http://intel.github.io/libva/structVAProcFilterParameterBuffer3DLUT.html
[KMS_pipe_API] https://gitlab.freedesktop.org/pq/color-and-hdr/-/issues/11

Let me know your thoughts.

Thanks in advance,

Melissa

Melissa Wen (9):
drm/amd/display: remove unused regamma condition drm/amd/display: add comments to describe DM crtc color mgmt behavior drm/drm_color_mgmt: add shaper LUT to color mgmt properties drm/drm_color_mgmt: add 3D LUT to color mgmt properties drm/amd/display: encapsulate atomic regamma operation drm/amd/display: add user 3D LUT support to the amdgpu_dm color
pipeline
drm/amd/display: add user shaper LUT support to amdgpu_dm color
pipeline
drm/amd/display: update lut3d and shaper lut to stream drm/amd/display: enable DRM shaper and 3D LUT properties

 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c |   6 +
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h |   5 +
 .../amd/display/amdgpu_dm/amdgpu_dm_color.c   | 297 ++++++++++++++++--
 .../amd/display/amdgpu_dm/amdgpu_dm_crtc.c    |   6 +
 drivers/gpu/drm/amd/display/dc/core/dc.c      |  10 +-
 .../gpu/drm/amd/display/dc/core/dc_stream.c   |  13 +
 drivers/gpu/drm/drm_atomic_state_helper.c     |   7 +
 drivers/gpu/drm/drm_atomic_uapi.c             |  20 ++
 drivers/gpu/drm/drm_color_mgmt.c              |  89 +++++-
 drivers/gpu/drm/drm_fb_helper.c               |   5 +
 drivers/gpu/drm/drm_mode_config.c             |  28 ++
 include/drm/drm_color_mgmt.h                  |   4 +
 include/drm/drm_crtc.h                        |  24 +-
 include/drm/drm_mode_config.h                 |  25 ++
 14 files changed, 495 insertions(+), 44 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

RFC PATCH v2 1/9 drm/amd/display: remove unused regamma condition

The function __set_output_tf is only called by amdgpu_dm_update_crtc_color_mgmt() when using atomic regamma. In this situation, func->tf == TRANSFER_FUNCTION_LINEAR (the original if condition) and it never falls into tf != LINEAR (the else condition). Therefore, remove unused condition to avoid misunderstandings here.

---
 .../amd/display/amdgpu_dm/amdgpu_dm_color.c   | 32 ++++++-------------
 1 file changed, 10 insertions(+), 22 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +10 -22
index a4cb23d059bd..10a29d131424 100644
@@ -255,14 +255,13 @@ static int __set_legacy_tf(struct dc_transfer_func *func,
  * @func: transfer function
  * @lut: lookup table that defines the color space
  * @lut_size: size of respective lut
- * @has_rom: if ROM can be used for hardcoded curve
  *
  * Returns:
  * 0 in case of success. -ENOMEM if fails.
  */
 static int __set_output_tf(struct dc_transfer_func *func,
-			   const struct drm_color_lut *lut, uint32_t lut_size,
-			   bool has_rom)
+			   const struct drm_color_lut *lut,
+			   uint32_t lut_size)
 {
 	struct dc_gamma *gamma = NULL;
 	struct calculate_buffer cal_buffer = {0};
@@ -279,24 +278,13 @@ static int __set_output_tf(struct dc_transfer_func *func,
 	gamma->num_entries = lut_size;
 	__drm_lut_to_dc_gamma(lut, gamma, false);
 
-	if (func->tf == TRANSFER_FUNCTION_LINEAR) {
-		/*
-		 * Color module doesn't like calculating regamma params
-		 * on top of a linear input. But degamma params can be used
-		 * instead to simulate this.
-		 */
-		gamma->type = GAMMA_CUSTOM;
-		res = mod_color_calculate_degamma_params(NULL, func,
-							gamma, true);
-	} else {
-		/*
-		 * Assume sRGB. The actual mapping will depend on whether the
-		 * input was legacy or not.
-		 */
-		gamma->type = GAMMA_CS_TFM_1D;
-		res = mod_color_calculate_regamma_params(func, gamma, false,
-							 has_rom, NULL, &cal_buffer);
-	}
+	/*
+	 * Color module doesn't like calculating regamma params
+	 * on top of a linear input. But degamma params can be used
+	 * instead to simulate this.
+	 */
+	gamma->type = GAMMA_CUSTOM;
+	res = mod_color_calculate_degamma_params(NULL, func, gamma, true);
 
 	dc_gamma_release(&gamma);
 
@@ -450,7 +438,7 @@ int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc)
 		stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
 
 		r = __set_output_tf(stream->out_transfer_func, regamma_lut,
-				    regamma_size, has_rom);
+				    regamma_size);
 		if (r)
 			return r;
 	} else {
Signature · 1 line
2.35.1
#2 lore raw

RFC PATCH v2 2/9 drm/amd/display: add comments to describe DM crtc color mgmt behavior

In reply to Melissa Wen

Describe some expected behavior of the AMD DM color mgmt programming.

---
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +12 -1
index 10a29d131424..b54ef1392895 100644
@@ -428,12 +428,23 @@ int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc)
 		stream->out_transfer_func->type = TF_TYPE_DISTRIBUTED_POINTS;
 		stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
 
+		/* Note: even if we pass has_rom as parameter here, we never
+		 * actually use ROM because the color module only takes the ROM
+		 * path if transfer_func->type == PREDEFINED.
+		 *
+		 * See more in mod_color_calculate_regamma_params()
+		 */
 		r = __set_legacy_tf(stream->out_transfer_func, regamma_lut,
 				    regamma_size, has_rom);
 		if (r)
 			return r;
 	} else if (has_regamma) {
-		/* If atomic regamma, CRTC RGM goes into RGM LUT. */
+		/* CRTC RGM goes into RGM LUT.
+		 *
+		 * Note: here there is no implicit sRGB regamma. We are using
+		 * degamma calculation from color module to calculate the curve
+		 * from a linear base.
+		 */
 		stream->out_transfer_func->type = TF_TYPE_DISTRIBUTED_POINTS;
 		stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
 
Signature · 1 line
2.35.1
#3 lore raw

RFC PATCH v2 3/9 drm/drm_color_mgmt: add shaper LUT to color mgmt properties

In reply to Melissa Wen

Shaper LUT is used to shape the contect after blending, i.e., de-linearize space before applying 3D LUT color correction. In the next patch, we are adding 3D LUT property to DRM color mgmt.

---
 drivers/gpu/drm/drm_atomic_state_helper.c |  4 +++
 drivers/gpu/drm/drm_atomic_uapi.c         | 10 ++++++++
 drivers/gpu/drm/drm_color_mgmt.c          | 31 ++++++++++++++++++-----
 drivers/gpu/drm/drm_fb_helper.c           |  3 +++
 drivers/gpu/drm/drm_mode_config.c         | 14 ++++++++++
 include/drm/drm_crtc.h                    | 14 ++++++++--
 include/drm/drm_mode_config.h             | 12 +++++++++
 7 files changed, 79 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_atomic_state_helper.c +4 -0
index 3b6d3bdbd099..bf3222fbf4bb 100644
@@ -139,8 +139,11 @@ void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
 		drm_property_blob_get(state->degamma_lut);
 	if (state->ctm)
 		drm_property_blob_get(state->ctm);
+	if (state->shaper_lut)
+		drm_property_blob_get(state->shaper_lut);
 	if (state->gamma_lut)
 		drm_property_blob_get(state->gamma_lut);
+
 	state->mode_changed = false;
 	state->active_changed = false;
 	state->planes_changed = false;
@@ -212,6 +215,7 @@ void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
 	drm_property_blob_put(state->mode_blob);
 	drm_property_blob_put(state->degamma_lut);
 	drm_property_blob_put(state->ctm);
+	drm_property_blob_put(state->shaper_lut);
 	drm_property_blob_put(state->gamma_lut);
 }
 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
drivers/gpu/drm/drm_atomic_uapi.c +10 -0
index 434f3d4cb8a2..ee78f4f5976d 100644
@@ -429,6 +429,14 @@ static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
 					&replaced);
 		state->color_mgmt_changed |= replaced;
 		return ret;
+	} else if (property == config->shaper_lut_property) {
+		ret = drm_atomic_replace_property_blob_from_id(dev,
+					&state->shaper_lut,
+					val,
+					-1, sizeof(struct drm_color_lut),
+					&replaced);
+		state->color_mgmt_changed |= replaced;
+		return ret;
 	} else if (property == config->gamma_lut_property) {
 		ret = drm_atomic_replace_property_blob_from_id(dev,
 					&state->gamma_lut,
@@ -480,6 +488,8 @@ drm_atomic_crtc_get_property(struct drm_crtc *crtc,
 		*val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
 	else if (property == config->ctm_property)
 		*val = (state->ctm) ? state->ctm->base.id : 0;
+	else if (property == config->shaper_lut_property)
+		*val = (state->shaper_lut) ? state->shaper_lut->base.id : 0;
 	else if (property == config->gamma_lut_property)
 		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
 	else if (property == config->prop_out_fence_ptr)
drivers/gpu/drm/drm_color_mgmt.c +24 -7
index 17c6c3eefcd6..873cca35f078 100644
@@ -69,6 +69,24 @@
  *	boot-up state too. Drivers can access the blob for the color conversion
  *	matrix through &drm_crtc_state.ctm.
  *
+ * “SHAPER_LUT”:
+ *	Blob property to set the shaper lut shaping pixel data after the color
+ *	transformation matrix and before applying 3D Lookup Table (3D LUT). It
+ *	can be used to delinearize content to get an effective 3D LUT mapping.
+ *	The data is interpreted as an array of &struct drm_color_lut elements.
+ *
+ *	Setting this to NULL (blob property value set to 0) means the output
+ *	color is identical to the input color. This is generally the driver
+ *	boot-up state too. Drivers can access this blob through
+ *	&drm_crtc_state.gamma_lut.
+ *
+ * “SHAPER_LUT_SIZE”:
+ *	Unsigned range property to give the size of the shaper lookup table to
+ *	be set on the SHAPER_LUT property (the size depends on the underlying
+ *	hardware). If drivers support multiple LUT sizes then they should
+ *	publish the largest size, and sub-sample smaller sized LUTs
+ *	appropriately.
+ *
  * “GAMMA_LUT”:
  *	Blob property to set the gamma lookup table (LUT) mapping pixel data
  *	after the transformation matrix to data sent to the connector. The
@@ -153,13 +171,12 @@ EXPORT_SYMBOL(drm_color_ctm_s31_32_to_qm_n);
  * @has_ctm: whether to attach ctm_property for CSC matrix
  * @gamma_lut_size: the size of the gamma lut (after CSC)
  *
- * This function lets the driver enable the color correction
- * properties on a CRTC. This includes 3 degamma, csc and gamma
- * properties that userspace can set and 2 size properties to inform
- * the userspace of the lut sizes. Each of the properties are
- * optional. The gamma and degamma properties are only attached if
- * their size is not 0 and ctm_property is only attached if has_ctm is
- * true.
+ * This function lets the driver enable the color correction properties on a
+ * CRTC. This includes 3 properties (degamma, csc and gamma) that
+ * userspace can set and 2 size properties to inform the userspace of the lut
+ * sizes. Each of the properties are optional. The gamma and degamma
+ * properties are only attached if their size is not 0 and ctm_property is only
+ * attached if has_ctm is true.
  */
 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
 				uint degamma_lut_size,
drivers/gpu/drm/drm_fb_helper.c +3 -0
index 5ad2b6a2778c..20c4878bd4d9 100644
@@ -1098,8 +1098,11 @@ static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
 		replaced  = drm_property_replace_blob(&crtc_state->degamma_lut,
 						      NULL);
 		replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
+		replaced |= drm_property_replace_blob(&crtc_state->shaper_lut,
+						      NULL);
 		replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
 						      gamma_lut);
+
 		crtc_state->color_mgmt_changed |= replaced;
 	}
 
drivers/gpu/drm/drm_mode_config.c +14 -0
index 37b4b9f0e468..4ba2a95b88e8 100644
@@ -350,6 +350,20 @@ static int drm_mode_create_standard_properties(struct drm_device *dev)
 		return -ENOMEM;
 	dev->mode_config.ctm_property = prop;
 
+	prop = drm_property_create(dev,
+			DRM_MODE_PROP_BLOB,
+			"SHAPER_LUT", 0);
+	if (!prop)
+		return -ENOMEM;
+	dev->mode_config.shaper_lut_property = prop;
+
+	prop = drm_property_create_range(dev,
+			DRM_MODE_PROP_IMMUTABLE,
+			"SHAPER_LUT_SIZE", 0, UINT_MAX);
+	if (!prop)
+		return -ENOMEM;
+	dev->mode_config.shaper_lut_size_property = prop;
+
 	prop = drm_property_create(dev,
 			DRM_MODE_PROP_BLOB,
 			"GAMMA_LUT", 0);
include/drm/drm_crtc.h +12 -2
index a70baea0636c..21938780d4bd 100644
@@ -165,8 +165,9 @@ struct drm_crtc_state {
 	bool zpos_changed : 1;
 	/**
 	 * @color_mgmt_changed: Color management properties have changed
-	 * (@gamma_lut, @degamma_lut or @ctm). Used by the atomic helpers and
-	 * drivers to steer the atomic commit control flow.
+	 * (@shaper_lut, @gamma_lut, @degamma_lut or @ctm). Used by
+	 * the atomic helpers and drivers to steer the atomic commit control
+	 * flow.
 	 */
 	bool color_mgmt_changed : 1;
 
@@ -292,6 +293,15 @@ struct drm_crtc_state {
 	 */
 	struct drm_property_blob *gamma_lut;
 
+	/**
+	 * @shaper_lut:
+	 *
+	 * Lookup table used to de-linearize pixel data for gamma correction.
+	 * See drm_crtc_enable_color_mgmt(). The blob (if not NULL) is an array
+	 * of &struct drm_color_lut.
+	 */
+	struct drm_property_blob *shaper_lut;
+
 	/**
 	 * @target_vblank:
 	 *
include/drm/drm_mode_config.h +12 -0
index 6b5e01295348..c174d3a7c951 100644
@@ -800,6 +800,18 @@ struct drm_mode_config {
 	 * degamma LUT.
 	 */
 	struct drm_property *ctm_property;
+
+	/**
+	 * @shaper_lut_property: Optional CRTC property to set the shaper LUT used to
+	 * convert colors before 3D LUT conversion.
+	 */
+	struct drm_property *shaper_lut_property;
+	/**
+	 * @shaper_lut_size_property: Optional CRTC property for the size of the
+	 * shaper LUT as supported by the driver (read-only).
+	 */
+	struct drm_property *shaper_lut_size_property;
+
 	/**
 	 * @gamma_lut_property: Optional CRTC property to set the LUT used to
 	 * convert the colors, after the CTM matrix, to the gamma space of the
Signature · 1 line
2.35.1
#4 lore raw

RFC PATCH v2 4/9 drm/drm_color_mgmt: add 3D LUT to color mgmt properties

In reply to Melissa Wen

Add 3D LUT for gammar correction using a 3D lookup table. The position in the color correction pipeline where 3D LUT is applied depends on hw design, being after CTM or gamma. If just after CTM, a shaper lut must be set to shape the content for a non-linear space. That details should be handled by the driver according to its color capabilities.

---
 drivers/gpu/drm/drm_atomic_state_helper.c |  3 ++
 drivers/gpu/drm/drm_atomic_uapi.c         | 10 ++++
 drivers/gpu/drm/drm_color_mgmt.c          | 58 +++++++++++++++++++++++
 drivers/gpu/drm/drm_fb_helper.c           |  2 +
 drivers/gpu/drm/drm_mode_config.c         | 14 ++++++
 include/drm/drm_color_mgmt.h              |  4 ++
 include/drm/drm_crtc.h                    | 12 ++++-
 include/drm/drm_mode_config.h             | 13 +++++
 8 files changed, 115 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_atomic_state_helper.c +3 -0
index bf3222fbf4bb..cad579c66248 100644
@@ -141,6 +141,8 @@ void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
 		drm_property_blob_get(state->ctm);
 	if (state->shaper_lut)
 		drm_property_blob_get(state->shaper_lut);
+	if (state->lut3d)
+		drm_property_blob_get(state->lut3d);
 	if (state->gamma_lut)
 		drm_property_blob_get(state->gamma_lut);
 
@@ -216,6 +218,7 @@ void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
 	drm_property_blob_put(state->degamma_lut);
 	drm_property_blob_put(state->ctm);
 	drm_property_blob_put(state->shaper_lut);
+	drm_property_blob_put(state->lut3d);
 	drm_property_blob_put(state->gamma_lut);
 }
 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
drivers/gpu/drm/drm_atomic_uapi.c +10 -0
index ee78f4f5976d..041b00faebe9 100644
@@ -437,6 +437,14 @@ static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
 					&replaced);
 		state->color_mgmt_changed |= replaced;
 		return ret;
+	} else if (property == config->lut3d_property) {
+		ret = drm_atomic_replace_property_blob_from_id(dev,
+					&state->lut3d,
+					val,
+					-1, sizeof(struct drm_color_lut),
+					&replaced);
+		state->color_mgmt_changed |= replaced;
+		return ret;
 	} else if (property == config->gamma_lut_property) {
 		ret = drm_atomic_replace_property_blob_from_id(dev,
 					&state->gamma_lut,
@@ -490,6 +498,8 @@ drm_atomic_crtc_get_property(struct drm_crtc *crtc,
 		*val = (state->ctm) ? state->ctm->base.id : 0;
 	else if (property == config->shaper_lut_property)
 		*val = (state->shaper_lut) ? state->shaper_lut->base.id : 0;
+	else if (property == config->lut3d_property)
+		*val = (state->lut3d) ? state->lut3d->base.id : 0;
 	else if (property == config->gamma_lut_property)
 		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
 	else if (property == config->prop_out_fence_ptr)
drivers/gpu/drm/drm_color_mgmt.c +58 -0

70 lines not on this page · raw message

drivers/gpu/drm/drm_fb_helper.c +2 -0
index 20c4878bd4d9..a91db904392e 100644
@@ -1100,6 +1100,8 @@ static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
 		replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
 		replaced |= drm_property_replace_blob(&crtc_state->shaper_lut,
 						      NULL);
+		replaced |= drm_property_replace_blob(&crtc_state->lut3d,
+						      NULL);
 		replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
 						      gamma_lut);
 
drivers/gpu/drm/drm_mode_config.c +14 -0
index 4ba2a95b88e8..5458a7dfbe63 100644
@@ -364,6 +364,20 @@ static int drm_mode_create_standard_properties(struct drm_device *dev)
 		return -ENOMEM;
 	dev->mode_config.shaper_lut_size_property = prop;
 
+	prop = drm_property_create(dev,
+			DRM_MODE_PROP_BLOB,
+			"LUT3D", 0);
+	if (!prop)
+		return -ENOMEM;
+	dev->mode_config.lut3d_property = prop;
+
+	prop = drm_property_create_range(dev,
+			DRM_MODE_PROP_IMMUTABLE,
+			"LUT3D_SIZE", 0, UINT_MAX);
+	if (!prop)
+		return -ENOMEM;
+	dev->mode_config.lut3d_size_property = prop;
+
 	prop = drm_property_create(dev,
 			DRM_MODE_PROP_BLOB,
 			"GAMMA_LUT", 0);
include/drm/drm_color_mgmt.h +4 -0
index 81c298488b0c..a4f054e0108f 100644
@@ -59,6 +59,10 @@ void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
 				bool has_ctm,
 				uint gamma_lut_size);
 
+void drm_crtc_enable_lut3d(struct drm_crtc *crtc,
+			   uint shaper_lut_size,
+			   uint lut3d_size);
+
 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
 				 int gamma_size);
 
include/drm/drm_crtc.h +11 -1
index 21938780d4bd..1ac61001e0e7 100644
@@ -165,7 +165,7 @@ struct drm_crtc_state {
 	bool zpos_changed : 1;
 	/**
 	 * @color_mgmt_changed: Color management properties have changed
-	 * (@shaper_lut, @gamma_lut, @degamma_lut or @ctm). Used by
+	 * (@shaper_lut, @lut3d, @gamma_lut, @degamma_lut or @ctm). Used by
 	 * the atomic helpers and drivers to steer the atomic commit control
 	 * flow.
 	 */
@@ -302,6 +302,16 @@ struct drm_crtc_state {
 	 */
 	struct drm_property_blob *shaper_lut;
 
+	/**
+	 * @lut3d:
+	 *
+	 * 3D Lookup table for converting pixel data. Position where it takes
+	 * place depends on hw design, after @ctm or @gamma_lut. See
+	 * drm_crtc_enable_color_mgmt(). The blob (if not NULL) is an array of
+	 * &struct drm_color_lut.
+	 */
+	struct drm_property_blob *lut3d;
+
 	/**
 	 * @target_vblank:
 	 *
include/drm/drm_mode_config.h +13 -0
index c174d3a7c951..c15be4e88f6b 100644
@@ -812,6 +812,19 @@ struct drm_mode_config {
 	 */
 	struct drm_property *shaper_lut_size_property;
 
+	/**
+	 * @lut3d_property: Optional CRTC property to set the 3D LUT used to
+	 * convert colors; before or after gamma conversion depends on hw
+	 * design. A shaper LUT can be used to delinearize content before apply
+	 * 3D LUT correction.
+	 */
+	struct drm_property *lut3d_property;
+	/**
+	 * @lut3d_size_property: Optional CRTC property for the size of the
+	 * 3D LUT as supported by the driver (read-only).
+	 */
+	struct drm_property *lut3d_size_property;
+
 	/**
 	 * @gamma_lut_property: Optional CRTC property to set the LUT used to
 	 * convert the colors, after the CTM matrix, to the gamma space of the
Signature · 1 line
2.35.1
#5 lore raw

RFC PATCH v2 5/9 drm/amd/display: encapsulate atomic regamma operation

In reply to Melissa Wen

We are introducing DRM 3D LUT property to DM color pipeline in the next patch, but so far, only for atomic interface. By checking .set_output_transfer_func in DC drivers with MPC 3D LUT support, we can verify that regamma is only programmed when 3D LUT programming fails. As a groundwork to introduce 3D LUT programming and better understand each step, detach atomic regamma programming from the crtc colocr updating code.

---
 .../amd/display/amdgpu_dm/amdgpu_dm_color.c   | 52 ++++++++++++-------
 1 file changed, 33 insertions(+), 19 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +33 -19
index b54ef1392895..54d95745f0f0 100644
@@ -291,6 +291,36 @@ static int __set_output_tf(struct dc_transfer_func *func,
 	return res ? 0 : -ENOMEM;
 }
 
+static int amdgpu_dm_set_atomic_regamma(struct dc_stream_state *stream,
+					const struct drm_color_lut *regamma_lut,
+					uint32_t regamma_size)
+{
+	int ret = 0;
+
+	if (regamma_size) {
+		/* CRTC RGM goes into RGM LUT.
+		 *
+		 * Note: here there is no implicit sRGB regamma. We are using
+		 * degamma calculation from color module to calculate the curve
+		 * from a linear base.
+		 */
+		stream->out_transfer_func->type = TF_TYPE_DISTRIBUTED_POINTS;
+		stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
+
+		ret = __set_output_tf(stream->out_transfer_func, regamma_lut,
+				      regamma_size);
+	} else {
+		/*
+		 * No CRTC RGM means we can just put the block into bypass
+		 * since we don't have any plane level adjustments using it.
+		 */
+		stream->out_transfer_func->type = TF_TYPE_BYPASS;
+		stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
+	}
+
+	return ret;
+}
+
 /**
  * __set_input_tf - calculates the input transfer function based on expected
  * input space.
@@ -438,27 +468,11 @@ int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc)
 				    regamma_size, has_rom);
 		if (r)
 			return r;
-	} else if (has_regamma) {
-		/* CRTC RGM goes into RGM LUT.
-		 *
-		 * Note: here there is no implicit sRGB regamma. We are using
-		 * degamma calculation from color module to calculate the curve
-		 * from a linear base.
-		 */
-		stream->out_transfer_func->type = TF_TYPE_DISTRIBUTED_POINTS;
-		stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
-
-		r = __set_output_tf(stream->out_transfer_func, regamma_lut,
-				    regamma_size);
+	} else {
+		regamma_size = has_regamma ? regamma_size : 0;
+		r = amdgpu_dm_set_atomic_regamma(stream, regamma_lut, regamma_size);
 		if (r)
 			return r;
-	} else {
-		/*
-		 * No CRTC RGM means we can just put the block into bypass
-		 * since we don't have any plane level adjustments using it.
-		 */
-		stream->out_transfer_func->type = TF_TYPE_BYPASS;
-		stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
 	}
 
 	/*
Signature · 1 line
2.35.1
#6 lore raw

RFC PATCH v2 6/9 drm/amd/display: add user 3D LUT support to the amdgpu_dm color pipeline

In reply to Melissa Wen

Map DRM 3D LUT in the atomic color mgmt pipeline to DC. 3D LUT works better in a non-linear color space, therefore using a degamma to linearize the input space may produce unexpected results. The next patch introduces shaper LUT support that can be used to delinearize the color space before applying 3D LUT conversion.

Note that there is no implicit sRGB degamma/regamma in the original implementation for DRM atomic color mgmt. Atomic degamma/regamma 1D LUT is applied considering a linear base.
For reference, see IGT test amdgpu/amd_color and commit cf020d49b3c4 ("drm/amd/display: Rework CRTC color management")

dc_acquire_release_mpc_3dlut initializes the bits required to program 3DLUT in DC MPC hw block, that is applied by set_output_transfer_func(). But we should verify if the timing to acquire and release shaper and 3D LUTs from the resource pool in this patch is correct.

---
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c |   6 +
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h |   5 +
 .../amd/display/amdgpu_dm/amdgpu_dm_color.c   | 149 +++++++++++++++++-
 .../gpu/drm/amd/display/dc/core/dc_stream.c   |  13 ++
 4 files changed, 172 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +6 -0
index dbe76b85552e..05d96978925c 100644
@@ -9435,6 +9435,12 @@ static int amdgpu_dm_atomic_check(struct drm_device *dev,
 			goto fail;
 		}
 
+		ret = amdgpu_dm_verify_3dlut_size(new_crtc_state, adev);
+		if (ret) {
+			DRM_DEBUG_DRIVER("amdgpu_dm_verify_lut_sizes() failed\n");
+			goto fail;
+		}
+
 		if (!new_crtc_state->enable)
 			continue;
 
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h +5 -0
index b44faaad9b0b..435cf6b4efc1 100644
@@ -784,12 +784,17 @@ void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
 
 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev);
 
+/* 3D LUT max size is 17x17x17 */
+#define MAX_COLOR_3DLUT_ENTRIES 4913
+/* 1D LUT degamma, regamma and shaper*/
 #define MAX_COLOR_LUT_ENTRIES 4096
 /* Legacy gamm LUT users such as X doesn't like large LUT sizes */
 #define MAX_COLOR_LEGACY_LUT_ENTRIES 256
 
 void amdgpu_dm_init_color_mod(void);
 int amdgpu_dm_verify_lut_sizes(const struct drm_crtc_state *crtc_state);
+int amdgpu_dm_verify_3dlut_size(const struct drm_crtc_state *crtc_state,
+				struct amdgpu_device *adev);
 int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc);
 int amdgpu_dm_update_plane_color_mgmt(struct dm_crtc_state *crtc,
 				      struct dc_plane_state *dc_plane_state);
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +148 -1

180 lines not on this page · raw message

drivers/gpu/drm/amd/display/dc/core/dc_stream.c +13 -0
index f62d50901d92..53043fe02a96 100644
@@ -129,11 +129,24 @@ static bool dc_stream_construct(struct dc_stream_state *stream,
 
 static void dc_stream_destruct(struct dc_stream_state *stream)
 {
+	struct dc_3dlut *lut3d_old = (struct dc_3dlut *) stream->lut3d_func;
+	struct dc_transfer_func *shaper_old = (struct dc_transfer_func *) stream->func_shaper;
+	struct dc *dc = stream->ctx->dc;
+
 	dc_sink_release(stream->sink);
 	if (stream->out_transfer_func != NULL) {
 		dc_transfer_func_release(stream->out_transfer_func);
 		stream->out_transfer_func = NULL;
 	}
+
+	/* TODO: Check if the timing to acquire and release is correct */
+	if (lut3d_old && shaper_old) {
+		if (!dc_acquire_release_mpc_3dlut(dc, false, stream,
+						  &lut3d_old, &shaper_old)) {
+			DRM_DEBUG_DRIVER("Failed to release. Shaper or 3D LUT not found.\n");
+		}
+	}
+
 }
 
 void dc_stream_retain(struct dc_stream_state *stream)
Signature · 1 line
2.35.1
#7 lore raw

RFC PATCH v2 7/9 drm/amd/display: add user shaper LUT support to amdgpu_dm color pipeline

In reply to Melissa Wen

Now, we can use shaper LUT to delinearize and/or normalize the color space for a more efficient 3D LUT support (so far, only for DRM atomic color mgmt). If a degamma 1D LUT is passed to linearize the color space, a custom shaper 1D LUT can be used before applying 3D LUT.

---
 .../amd/display/amdgpu_dm/amdgpu_dm_color.c   | 105 +++++++++++++++---
 1 file changed, 87 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c +87 -18
index 9777252191b1..b590fc83a88c 100644
@@ -352,18 +352,64 @@ static int __set_input_tf(struct dc_transfer_func *func,
 	return res ? 0 : -ENOMEM;
 }
 
-static int amdgpu_dm_atomic_shaper_lut(struct dc_stream_state *stream,
-				       struct dc_transfer_func *func_shaper_new)
+static int __set_func_shaper(struct dc_transfer_func *shaper_func,
+			     const struct drm_color_lut *lut, uint32_t lut_size)
 {
+	struct dc_gamma *gamma = NULL;
+	struct calculate_buffer cal_buffer = {0};
+	bool res;
+
+	ASSERT(lut && lut_size == MAX_COLOR_LUT_ENTRIES);
+
+	cal_buffer.buffer_index = -1;
+
+	gamma = dc_create_gamma();
+	if (!gamma)
+		return -ENOMEM;
+
+	gamma->num_entries = lut_size;
+	__drm_lut_to_dc_gamma(lut, gamma, false);
+
+	/*
+	 * Color module doesn't like calculating gamma params
+	 * on top of a linear input. But degamma params can be used
+	 * instead to simulate this.
+	 */
+	gamma->type = GAMMA_CUSTOM;
+	res = mod_color_calculate_degamma_params(NULL, shaper_func, gamma, true);
+
+	dc_gamma_release(&gamma);
+
+	return res ? 0 : -ENOMEM;
+}
 
-	/* We don't get DRM shaper LUT yet. We assume the input color space is
-	 * already delinearized, so we don't need a shaper LUT and we can just
-	 * BYPASS.  However, checking dcn30_set_mpc_shaper_3dlut() it seems
+static int amdgpu_dm_atomic_shaper_lut(struct dc_stream_state *stream,
+				       struct dc_transfer_func *func_shaper_new,
+				       const struct drm_color_lut *shaper_lut,
+				       uint32_t shaper_size)
+{
+	/* If no DRM shaper LUT, we assume the input color space is already
+	 * delinearized, so we don't need a shaper LUT and we can just BYPASS
+	 * However, checking dcn30_set_mpc_shaper_3dlut() it seems
 	 * that setting shaper LUT to BYPASS is not currently supported in the
 	 * DC level, since shaper LUT programming just fails without params.
 	 */
-	func_shaper_new->type = TF_TYPE_BYPASS;
-	func_shaper_new->tf = TRANSFER_FUNCTION_LINEAR;
+	if (!shaper_size) {
+		func_shaper_new->type = TF_TYPE_BYPASS;
+		func_shaper_new->tf = TRANSFER_FUNCTION_LINEAR;
+	} else {
+		int r;
+
+		/* If DRM shaper LUT is set, we follow the same behavior of the
+		 * atomic regamma and assume a linear base */
+		func_shaper_new->type = TF_TYPE_DISTRIBUTED_POINTS;
+		func_shaper_new->tf = TRANSFER_FUNCTION_LINEAR;
+
+		r = __set_func_shaper(func_shaper_new, shaper_lut,
+				shaper_size);
+		if (r)
+			return r;
+	}
 
 	stream->func_shaper = func_shaper_new;
 
@@ -427,12 +473,27 @@ static uint32_t amdgpu_dm_get_3dlut_size(uint32_t lut_size,
 int amdgpu_dm_verify_3dlut_size(const struct drm_crtc_state *crtc_state,
 				struct amdgpu_device *adev)
 {
-	const struct drm_color_lut *lut3d = NULL;
+	const struct drm_color_lut *shaper = NULL, *lut3d = NULL;
 	uint32_t exp_size, size;
 
+	/* shaper LUT is only available if 3D LUT color caps*/
+	exp_size = amdgpu_dm_get_3dlut_size(MAX_COLOR_LUT_ENTRIES, adev);
+
+	shaper = __extract_blob_lut(crtc_state->shaper_lut, &size);
+	if (shaper && size != exp_size) {
+		DRM_DEBUG_DRIVER(
+			"Invalid Shaper LUT size. Should be %u but got %u.\n",
+			exp_size, size);
+		return -EINVAL;
+	}
+
 	exp_size = amdgpu_dm_get_3dlut_size(MAX_COLOR_3DLUT_ENTRIES, adev);
 	lut3d = __extract_blob_lut(crtc_state->lut3d, &size);
 
+	/* shaper LUT implies 3D LUT. See dcn30_set_output_transfer_func() */
+	if (shaper && !lut3d)
+		DRM_DEBUG_DRIVER("Shaper LUT is set without 3D LUT.\n");
+
 	if (lut3d && size != exp_size) {
 		DRM_DEBUG_DRIVER("Invalid Gamma 3D LUT size. Should be %u but got %u.\n",
 				 exp_size, size);
@@ -444,6 +505,8 @@ int amdgpu_dm_verify_3dlut_size(const struct drm_crtc_state *crtc_state,
 
 static int amdgpu_dm_atomic_shaper_3dlut(struct dc *dc,
 					 struct dc_stream_state *stream,
+					 const struct drm_color_lut *drm_shaper_lut,
+					 uint32_t drm_shaper_size,
 					 const struct drm_color_lut *drm_lut3d,
 					 uint32_t drm_lut3d_size)
 {
@@ -451,7 +514,6 @@ static int amdgpu_dm_atomic_shaper_3dlut(struct dc *dc,
 	struct dc_transfer_func *shaper_old = (struct dc_transfer_func *) stream->func_shaper;
 	struct dc_3dlut *lut3d_new = NULL;
 	struct dc_transfer_func *shaper_new = NULL;
-	int r;
 
 	if (lut3d_old && shaper_old) {
 		if (!dc_acquire_release_mpc_3dlut(dc, false, stream,
@@ -466,15 +528,11 @@ static int amdgpu_dm_atomic_shaper_3dlut(struct dc *dc,
 		return -EFAULT;
 	}
 
-	r = amdgpu_dm_atomic_shaper_lut(stream, shaper_new);
-	if (r)
-		return r;
-
 	__drm_3dlut_to_dc_3dlut(drm_lut3d, drm_lut3d_size, lut3d_new);
 
 	stream->lut3d_func = lut3d_new;
 
-	return 0;
+	return amdgpu_dm_atomic_shaper_lut(stream, shaper_new, drm_shaper_lut, drm_shaper_size);
 }
 
 /**
@@ -542,10 +600,10 @@ int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc)
 	bool has_rom = adev->asic_type <= CHIP_RAVEN;
 	struct drm_color_ctm *ctm = NULL;
 	const struct drm_color_lut *degamma_lut, *regamma_lut;
-	const struct drm_color_lut *lut3d;
+	const struct drm_color_lut *shaper_lut, *lut3d;
 	uint32_t degamma_size, regamma_size;
-	uint32_t lut3d_size;
-	bool has_regamma, has_degamma, has_lut3d;
+	uint32_t shaper_size, lut3d_size;
+	bool has_regamma, has_degamma, has_shaper_lut, has_lut3d;
 	bool is_legacy;
 	int r;
 
@@ -558,12 +616,16 @@ int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc)
 		return r;
 
 	degamma_lut = __extract_blob_lut(crtc->base.degamma_lut, &degamma_size);
+	shaper_lut = __extract_blob_lut(crtc->base.shaper_lut, &shaper_size);
 	lut3d = __extract_blob_lut(crtc->base.lut3d, &lut3d_size);
 	regamma_lut = __extract_blob_lut(crtc->base.gamma_lut, &regamma_size);
 
 	has_degamma =
 		degamma_lut && !__is_lut_linear(degamma_lut, degamma_size);
 
+	has_shaper_lut =
+		shaper_lut && !__is_lut_linear(shaper_lut, shaper_size);
+
 	has_lut3d =
 		lut3d && !__is_lut_linear(lut3d, lut3d_size);
 
@@ -605,10 +667,17 @@ int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc)
 			return r;
 	} else {
 		if (has_lut3d) {
+			/* enable 3D LUT only for DRM atomic regamma */
+			shaper_size = has_shaper_lut ? shaper_size : 0;
+
 			r = amdgpu_dm_atomic_shaper_3dlut(adev->dm.dc, stream,
+							  shaper_lut, shaper_size,
 							  lut3d, lut3d_size);
-			if (r)
+
+			if (r) {
+				DRM_DEBUG_DRIVER("Failed to set shaper and 3D LUT\n");
 				return r;
+			}
 		}
 		/* Note: OGAM is disabled if 3D LUT is successfully programmed.
 		 * For reference, see params and set_output_gamma in
Signature · 1 line
2.35.1
#8 lore raw

RFC PATCH v2 8/9 drm/amd/display: update lut3d and shaper lut to stream

In reply to Melissa Wen

It follows the same path of out_transfer_func for stream updates, since shaper LUT and 3D LUT is programmed in funcs.set_output_transfer_func() and this function is called in the atomic commit_tail when update_flags.bits.out_tf is set.

---
 drivers/gpu/drm/amd/display/dc/core/dc.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/display/dc/core/dc.c +9 -1
index 9860bf38c547..d1fa87ddf1dd 100644
@@ -2405,7 +2405,7 @@ static enum surface_update_type check_update_surfaces_for_stream(
 			stream_update->integer_scaling_update)
 			su_flags->bits.scaling = 1;
 
-		if (stream_update->out_transfer_func)
+		if (stream_update->out_transfer_func || stream_update->lut3d_func)
 			su_flags->bits.out_tf = 1;
 
 		if (stream_update->abm_level)
@@ -2754,6 +2754,14 @@ static void copy_stream_update_to_stream(struct dc *dc,
 		       sizeof(struct dc_transfer_func_distributed_points));
 	}
 
+	if (update->func_shaper &&
+	    stream->func_shaper != update->func_shaper)
+		stream->func_shaper = update->func_shaper;
+
+	if (update->lut3d_func &&
+	    stream->lut3d_func != update->lut3d_func)
+		stream->lut3d_func = update->lut3d_func;
+
 	if (update->hdr_static_metadata)
 		stream->hdr_static_metadata = *update->hdr_static_metadata;
 
Signature · 1 line
2.35.1
#9 lore raw

RFC PATCH v2 9/9 drm/amd/display: enable DRM shaper and 3D LUT properties

In reply to Melissa Wen

Shaper LUT and 3D LUT programming is done, so make the DRM color properties available.

---
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +6 -0
index c89594f3a5cb..b165a0c269fa 100644
@@ -452,6 +452,12 @@ int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
 	dm->adev->mode_info.crtcs[crtc_index] = acrtc;
 	drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
 				   true, MAX_COLOR_LUT_ENTRIES);
+
+	if (dm->dc->caps.color.mpc.num_3dluts)
+		drm_crtc_enable_lut3d(&acrtc->base,
+				      MAX_COLOR_LUT_ENTRIES,
+				      MAX_COLOR_3DLUT_ENTRIES);
+
 	drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
 
 	return 0;
Signature · 1 line
2.35.1
maintainer
#10 lore raw
In reply to Melissa Wen
Quoted 30 lines · Melissa Wen

Hi,

From all feedback at [3DLUT_RFC] and an extensive AMD driver examination, here I am back with a first attempt to wire up a user 3D LUT (post-blending) to DC interface via DM CRTC color mgmt :)

I'm following some specific approaches to handle user shaper LUT and user 3D LUT that I would like to validate if the path taken is correct.

I used a modified version [igt_tests] of Ville's IGT 3D LUT test to verify that the shaper and 3D LUT programming is happening. However, I still have doubts about hw behavior and DC MPC's current implementation for 3D LUT.

Despite some initial patches for code cleanup and DRM interface, my focus here is the inclusion of a user 3D LUT in the Display Manager, which is done in the last five patches of this series:

- drm/amd/display: enable DRM shaper and 3D LUT properties
- drm/amd/display: update lut3d and shaper lut to stream
- drm/amd/display: add user shaper LUT support to amdgpu_dm color pipeline
- drm/amd/display: add user 3D LUT support to the amdgpu_dm color pipeline
- drm/amd/display: encapsulate atomic regamma operation

Things to take into account:

- 3D LUT (and shaper LUT) is only available in the atomic pipeline (I didn't work on any implicit conversions that are done in the legacy path)

Quoted 8 lines

- Therefore, I'm not doing any implicit conversions for shaper LUT considering the input space, which means: it's set or not. When there is no shaper LUT, it's set to BYPASS, but unfortunately, it seems that the BYPASS mode for shaper LUT is not supported in the current DC dcn30_set_mpc_shaper_3dlut(), since it returns false when mpc3_program_shaper returns false (no params). Is the combination of a user 3D LUT with a bypassed shaper LUT accepted by the hw?

AFAIK this should be supported. The current DC code is centered on use-cases for other OSes but I'm not sure this is the key issue here, though it might explain why things won't always look as expected.

mpc3_program_shaper will program the shaper LUT to bypass when params are NULL. It will return false so dcn30_set_output_transfer_func knows no shaper LUT is set. The reason behind this is that shaper LUT and output gamma tend to work in tandem.

If you have linear content incoming and want linear content on the output side of the 3DLUT you'll need to program both shaper LUT and output gamma LUTs with EOTF^-1 and EOTF respectively.

(Btw, please watch my HDR presentation at XDC or look through the slides, as I'll show why the current DRM LUTs can't represent EOTF^-1 functions.)

In your case I don't expect you're operating on linear content. Your content will already be in sRGB, so shaper LUT and output gamma should really be both in bypass mode. You might need to debug dcn30_set_output_transfer_func to make sure it's doing what you're expecting (and add new logic if needed).

Quoted 7 lines

- I also see in dcn30_set_mpc_shaper_3dlut() that some bits need to be set in lut3d_func to have the 3D LUT programmed on the MPC block. In this sense, I used the dc_acquire_release_mpc_3dlut() function to get the lut3d_func from the resource pool, but I'm not sure if the timing to acquire and release the lut3d_func from the resource pool is correct (and if I can really use it directly or I should make a copy).

There are a limited number of MPC 3DLUT blocks (shaper + 3DLUT) available. Acquiring and releasing them in DM is the right way to go about this.

Unfortunately dc_acquire_release_mpc_3dlut can fail. For this reason we should run it in atomic_check. Unfortunately this function operates on the current state, which makes it unsuitable for atomic_check.

We need a new function that does what dc_acquire_release_mpc_3dlut does but takes a dm_state->context as parameter and operates on that instead. Once we have that you can call that in dm_update_crtc_state to acquire or release the 3DLUT on a CRTC as needed.

Quoted 6 lines

- Still, on this topic, I use for lut3d the same bit.out_tf to update the stream in the commit_tail because it triggers .set_output_transfer_func that is in charge of setting both OGAM and 3D LUT on MPC. There is a chance I got it wrong here, so I appreciate any input on this topic.

This looks correct to me.

Quoted 6 lines

- Finally, in set_output_transfer_func, AFAIU, even if a user OGAM is set, it won't be programmed if the shaper LUT and 3D LUT programming are successful. However, if shaper/3DLUT programming fails, OGAM can be considered. Should DM only accept DRM regamma if no DRM 3D LUT is passed, or allowing the programming of both is still desirable?

I don't know why we skip the OGAM programming when shaper + 3DLUT are set. This doesn't make sense to me. As described above, the thing with the shaper LUT is that you might need to sandwich the 3DLUT with two 1DLUTs, one being the shaper, the other the OGAM. Obviously it depends on your intended transformations on which of these you want to program but in general I would expect we still need the ability to set OGAM when the 3DLUT is programmed.

Quoted 5 lines

Regarding the other patches:

- drm/drm_color_mgmt: add 3D LUT to color mgmt properties
- drm/drm_color_mgmt: add shaper LUT to color mgmt properties

Quoted 7 lines

Here, an initial DRM 3D LUT interface is exposed to enable the entire kernel path and is only available for the atomic pipeline. So far, it only includes the LUT data and its size, but improvements on this interface may also add stride and bit depth [VA_API]. Additionally, I'm aware of the current work on exposing a color pipeline API [KMS_pipe_API].

Alex Hung will send an RFC on a pre-blending 3DLUT. Similar to your patchset it's not fully where we need it to be but he has a good definition of what I think we'll need to describe a 3DLUT accurately. His proposal is missing the shaper LUT, though, and I really value how your patchset is figuring out how to combine the shaper LUT with the 3DLUT.

Thanks for your work on this and I hope we can chat more at XDC next week.

Harry

Quoted 54 lines

I'm including some minor changes in this series that I made to better understand the current DM color mgmt behavior:

- drm/amd/display: add comments to describe DM crtc color mgmt behavior
- drm/amd/display: remove unused regamma condition

...but, there are other code cleanup patches that I'm not including in this series to avoid unnecessary noise.

You can check the entire work here:
https://gitlab.freedesktop.org/mwen/linux-amd/-/commits/drm_lut3d

[igt_tests] IGT exploratory tests here:
https://gitlab.freedesktop.org/mwen/igt-gpu-tools/-/commits/kms_color_3dlut

[3DLUT_RFC] https://lore.kernel.org/amd-gfx/[email protected]/
[VA_API] http://intel.github.io/libva/structVAProcFilterParameterBuffer3DLUT.html
[KMS_pipe_API] https://gitlab.freedesktop.org/pq/color-and-hdr/-/issues/11

Let me know your thoughts.

Thanks in advance,

Melissa

Melissa Wen (9):
drm/amd/display: remove unused regamma condition drm/amd/display: add comments to describe DM crtc color mgmt behavior drm/drm_color_mgmt: add shaper LUT to color mgmt properties drm/drm_color_mgmt: add 3D LUT to color mgmt properties drm/amd/display: encapsulate atomic regamma operation drm/amd/display: add user 3D LUT support to the amdgpu_dm color
pipeline
drm/amd/display: add user shaper LUT support to amdgpu_dm color
pipeline
drm/amd/display: update lut3d and shaper lut to stream drm/amd/display: enable DRM shaper and 3D LUT properties

 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c |   6 +
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h |   5 +
 .../amd/display/amdgpu_dm/amdgpu_dm_color.c   | 297 ++++++++++++++++--
 .../amd/display/amdgpu_dm/amdgpu_dm_crtc.c    |   6 +
 drivers/gpu/drm/amd/display/dc/core/dc.c      |  10 +-
 .../gpu/drm/amd/display/dc/core/dc_stream.c   |  13 +
 drivers/gpu/drm/drm_atomic_state_helper.c     |   7 +
 drivers/gpu/drm/drm_atomic_uapi.c             |  20 ++
 drivers/gpu/drm/drm_color_mgmt.c              |  89 +++++-
 drivers/gpu/drm/drm_fb_helper.c               |   5 +
 drivers/gpu/drm/drm_mode_config.c             |  28 ++
 include/drm/drm_color_mgmt.h                  |   4 +
 include/drm/drm_crtc.h                        |  24 +-
 include/drm/drm_mode_config.h                 |  25 ++
 14 files changed, 495 insertions(+), 44 deletions(-)