Skip to contentSkip to conversation

drm/amd/display: better LUT segmentation for EOTFs

RFC PATCH Superseded Melissa Wen 2 participants 7 messages Apr 14 – Apr 21

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover drm/amd/display: better LUT segmentation for EOTFs

With an external HDR monitor, we can see gradient banding around the sun in the intro of Ori and the Will of the Wisps game on steamOS/Gamescope. Gamescope uses AMD predefined transfer functions for degamma, shaper/pre-3D-LUT and blend/post-3D-LUT plus CRTC regamma, however, only degamma block has hardcoded curves. Shaper, blend, regamma predefined TFs are software-computed by AMD color module into PWL LUTs.

For this translation, the driver originally used the same helper for EOTFs and inverse EOTFs, even though they differ in input domain, number of regions and number of TF points per region.

This series targets DCN3+ hw families. It was verified with steamOS/gamescope on DCN301 and DCN321 both HDMI-connected to an external HDR monitor (Samsung G6 OLED 27").

Patch 1 reverts commit 8b89acc0b2ba ("drm/amd/display: Remove unused cm3_helper_translate_curve_to_degamma_hw_format") to reintroduce a degamma-specific helper and patch 2 wire it up for encoded -> linear-light LUTs (degamma/blend). With 16 samples per region across 12 regions for blend LUT (where hardware fixed-function curves are not available and predefined TFs are software-computed into LUTs), banding on Ori becomes almost imperceptible.

Patch 3 and 4 increase precision in the brightest half, where PQ/SRGB EOTFs are steeper, by enabling up to 256 samples per region and halving the per-region point count across 9 regions (128 in [0.5, 1], 64 in [0.25, 0.5], …). This better matches the shape of PQ/SRGB EOTFs. Although patches 3 and 4 seem conceptually correct to me, I couldn't see clear improvement in the bright end with or without them.

With this series:
- degamma and blend LUTs use cm3_helper_translate_curve_to_degamma_hw_format(): encoded input, non-zero end slope, up to 256 points linearly interpolated between adjacent TF pts, fitting [0,1] encoded input range.
- shaper and regamma LUTs continue using cm3_helper_translate_curve_to_hw_format(): linear-light input, zero end slope, 16 points per region across 32 regions.

Screenshots are not available: my available cameras couldn't capture the banding cleanly againts the HDR monitor.

Let me know your thoughts!

Melissa

Melissa Wen (4):
Revert "drm/amd/display: Remove unused
cm3_helper_translate_curve_to_degamma_hw_format"
drm/amd/display: use a separate helper to translate degamma curves drm/amd/display: support up to 256 samples per region drm/amd/display: rework segmentation for linearizing LUT

 .../amd/display/dc/dcn30/dcn30_cm_common.c    | 184 ++++++++++++++++++
 .../display/dc/dwb/dcn30/dcn30_cm_common.h    |   4 +
 .../amd/display/dc/hwss/dcn32/dcn32_hwseq.c   |  10 +-
 3 files changed, 192 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/4 Revert "drm/amd/display: Remove unused cm3_helper_translate_curve_to_degamma_hw_format"

This reverts commit 8b89acc0b2baecfe331f5336e7ff1fcc5a44b062.

---
 .../amd/display/dc/dcn30/dcn30_cm_common.c    | 151 ++++++++++++++++++
 .../display/dc/dwb/dcn30/dcn30_cm_common.h    |   4 +
 2 files changed, 155 insertions(+)
drivers/gpu/drm/amd/display/dc/dcn30/dcn30_cm_common.c +151 -0
index 9dbccf58dde5..a439bb6c5e56 100644
@@ -301,6 +301,157 @@ bool cm3_helper_translate_curve_to_hw_format(struct dc_context *ctx,
 	return true;
 }
 
+#define NUM_DEGAMMA_REGIONS    12
+
+
+bool cm3_helper_translate_curve_to_degamma_hw_format(
+				const struct dc_transfer_func *output_tf,
+				struct pwl_params *lut_params)
+{
+	struct curve_points3 *corner_points;
+	struct pwl_result_data *rgb_resulted;
+	struct pwl_result_data *rgb;
+	struct pwl_result_data *rgb_plus_1;
+
+	int32_t region_start, region_end;
+	int32_t i;
+	uint32_t j, k, seg_distr[MAX_REGIONS_NUMBER], increment, start_index, hw_points;
+
+	if (output_tf == NULL || lut_params == NULL || output_tf->type == TF_TYPE_BYPASS)
+		return false;
+
+	corner_points = lut_params->corner_points;
+	rgb_resulted = lut_params->rgb_resulted;
+	hw_points = 0;
+
+	memset(lut_params, 0, sizeof(struct pwl_params));
+	memset(seg_distr, 0, sizeof(seg_distr));
+
+	region_start = -NUM_DEGAMMA_REGIONS;
+	region_end   = 0;
+
+
+	for (i = region_end - region_start; i < MAX_REGIONS_NUMBER ; i++)
+		seg_distr[i] = -1;
+	/* 12 segments
+	 * segments are from 2^-12 to 0
+	 */
+	for (i = 0; i < NUM_DEGAMMA_REGIONS ; i++)
+		seg_distr[i] = 4;
+
+	for (k = 0; k < MAX_REGIONS_NUMBER; k++) {
+		if (seg_distr[k] != -1)
+			hw_points += (1 << seg_distr[k]);
+	}
+
+	j = 0;
+	for (k = 0; k < (region_end - region_start); k++) {
+		increment = NUMBER_SW_SEGMENTS / (1 << seg_distr[k]);
+		start_index = (region_start + k + MAX_LOW_POINT) *
+				NUMBER_SW_SEGMENTS;
+		for (i = start_index; i < start_index + NUMBER_SW_SEGMENTS;
+				i += increment) {
+			if (j == hw_points - 1)
+				break;
+			if (i >= TRANSFER_FUNC_POINTS)
+				return false;
+			rgb_resulted[j].red = output_tf->tf_pts.red[i];
+			rgb_resulted[j].green = output_tf->tf_pts.green[i];
+			rgb_resulted[j].blue = output_tf->tf_pts.blue[i];
+			j++;
+		}
+	}
+
+	/* last point */
+	start_index = (region_end + MAX_LOW_POINT) * NUMBER_SW_SEGMENTS;
+	rgb_resulted[hw_points - 1].red = output_tf->tf_pts.red[start_index];
+	rgb_resulted[hw_points - 1].green = output_tf->tf_pts.green[start_index];
+	rgb_resulted[hw_points - 1].blue = output_tf->tf_pts.blue[start_index];
+
+	corner_points[0].red.x = dc_fixpt_pow(dc_fixpt_from_int(2),
+					     dc_fixpt_from_int(region_start));
+	corner_points[0].green.x = corner_points[0].red.x;
+	corner_points[0].blue.x = corner_points[0].red.x;
+	corner_points[1].red.x = dc_fixpt_pow(dc_fixpt_from_int(2),
+					     dc_fixpt_from_int(region_end));
+	corner_points[1].green.x = corner_points[1].red.x;
+	corner_points[1].blue.x = corner_points[1].red.x;
+
+	corner_points[0].red.y = rgb_resulted[0].red;
+	corner_points[0].green.y = rgb_resulted[0].green;
+	corner_points[0].blue.y = rgb_resulted[0].blue;
+
+	/* see comment above, m_arrPoints[1].y should be the Y value for the
+	 * region end (m_numOfHwPoints), not last HW point(m_numOfHwPoints - 1)
+	 */
+	corner_points[1].red.y = rgb_resulted[hw_points - 1].red;
+	corner_points[1].green.y = rgb_resulted[hw_points - 1].green;
+	corner_points[1].blue.y = rgb_resulted[hw_points - 1].blue;
+	corner_points[1].red.slope = dc_fixpt_zero;
+	corner_points[1].green.slope = dc_fixpt_zero;
+	corner_points[1].blue.slope = dc_fixpt_zero;
+
+	if (output_tf->tf == TRANSFER_FUNCTION_PQ) {
+		/* for PQ, we want to have a straight line from last HW X point,
+		 * and the slope to be such that we hit 1.0 at 10000 nits.
+		 */
+		const struct fixed31_32 end_value =
+				dc_fixpt_from_int(125);
+
+		corner_points[1].red.slope = dc_fixpt_div(
+			dc_fixpt_sub(dc_fixpt_one, corner_points[1].red.y),
+			dc_fixpt_sub(end_value, corner_points[1].red.x));
+		corner_points[1].green.slope = dc_fixpt_div(
+			dc_fixpt_sub(dc_fixpt_one, corner_points[1].green.y),
+			dc_fixpt_sub(end_value, corner_points[1].green.x));
+		corner_points[1].blue.slope = dc_fixpt_div(
+			dc_fixpt_sub(dc_fixpt_one, corner_points[1].blue.y),
+			dc_fixpt_sub(end_value, corner_points[1].blue.x));
+	}
+
+	lut_params->hw_points_num = hw_points;
+
+	k = 0;
+	for (i = 1; i < MAX_REGIONS_NUMBER; i++) {
+		if (seg_distr[k] != -1) {
+			lut_params->arr_curve_points[k].segments_num =
+					seg_distr[k];
+			lut_params->arr_curve_points[i].offset =
+					lut_params->arr_curve_points[k].offset + (1 << seg_distr[k]);
+		}
+		k++;
+	}
+
+	if (seg_distr[k] != -1)
+		lut_params->arr_curve_points[k].segments_num = seg_distr[k];
+
+	rgb = rgb_resulted;
+	rgb_plus_1 = rgb_resulted + 1;
+
+	i = 1;
+	while (i != hw_points + 1) {
+		if (dc_fixpt_lt(rgb_plus_1->red, rgb->red))
+			rgb_plus_1->red = rgb->red;
+		if (dc_fixpt_lt(rgb_plus_1->green, rgb->green))
+			rgb_plus_1->green = rgb->green;
+		if (dc_fixpt_lt(rgb_plus_1->blue, rgb->blue))
+			rgb_plus_1->blue = rgb->blue;
+
+		rgb->delta_red   = dc_fixpt_sub(rgb_plus_1->red,   rgb->red);
+		rgb->delta_green = dc_fixpt_sub(rgb_plus_1->green, rgb->green);
+		rgb->delta_blue  = dc_fixpt_sub(rgb_plus_1->blue,  rgb->blue);
+
+		++rgb_plus_1;
+		++rgb;
+		++i;
+	}
+	cm3_helper_convert_to_custom_float(rgb_resulted,
+						lut_params->corner_points,
+						hw_points, false);
+
+	return true;
+}
+
 bool cm3_helper_convert_to_custom_float(
 		struct pwl_result_data *rgb_resulted,
 		struct curve_points3 *corner_points,
drivers/gpu/drm/amd/display/dc/dwb/dcn30/dcn30_cm_common.h +4 -0
index 95f9318a54ef..c23dc1bb29bf 100644
@@ -63,6 +63,10 @@ bool cm3_helper_translate_curve_to_hw_format(struct dc_context *ctx,
 	const struct dc_transfer_func *output_tf,
 	struct pwl_params *lut_params, bool fixpoint);
 
+bool cm3_helper_translate_curve_to_degamma_hw_format(
+				const struct dc_transfer_func *output_tf,
+				struct pwl_params *lut_params);
+
 bool cm3_helper_convert_to_custom_float(
 		struct pwl_result_data *rgb_resulted,
 		struct curve_points3 *corner_points,
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/4 drm/amd/display: use a separate helper to translate degamma curves

In reply to Melissa Wen

In newer DCN families, there is no hw predefined curves. So, when setting predefined TFs to gamm_corr, shaper, blend and regamma, the driver resorts to the color modules and program those predefined curves using LUTs. The driver is using the same LUT segmentation when translating EOTF and inverse EOTF to hw points by using the same color management helper to translate curve to hw points, however, LUT representing EOTF may not follow the same region/segment distributions of inverse EOTF. This is causing banding on blend when PQ predefined curve is set. Use different helpers when linearizing and delinearizing. This is the first step to fix banding but already mitigates the issue by increase the number of points per segment from 8 to 16.

---
 .../gpu/drm/amd/display/dc/hwss/dcn32/dcn32_hwseq.c    | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/display/dc/hwss/dcn32/dcn32_hwseq.c +4 -6
index b45ceb570a5c..17d54aadb5e1 100644
@@ -493,9 +493,8 @@ bool dcn32_set_mcm_luts(
 	if (plane_state->blend_tf.type == TF_TYPE_HWPWL)
 		lut_params = &plane_state->blend_tf.pwl;
 	else if (plane_state->blend_tf.type == TF_TYPE_DISTRIBUTED_POINTS) {
-		result = cm3_helper_translate_curve_to_hw_format(plane_state->ctx,
-								 &plane_state->blend_tf,
-								 &dpp_base->regamma_params, false);
+		result = cm3_helper_translate_curve_to_degamma_hw_format(&plane_state->blend_tf,
+									 &dpp_base->regamma_params);
 		if (!result)
 			return result;
 
@@ -551,9 +550,8 @@ bool dcn32_set_input_transfer_func(struct dc *dc,
 	if (plane_state->in_transfer_func.type == TF_TYPE_HWPWL)
 		params = &plane_state->in_transfer_func.pwl;
 	else if (plane_state->in_transfer_func.type == TF_TYPE_DISTRIBUTED_POINTS &&
-		cm3_helper_translate_curve_to_hw_format(plane_state->ctx,
-							&plane_state->in_transfer_func,
-							&dpp_base->degamma_params, false))
+		cm3_helper_translate_curve_to_degamma_hw_format(&plane_state->in_transfer_func,
+								&dpp_base->degamma_params))
 		params = &dpp_base->degamma_params;
 
 	dpp_base->funcs->dpp_program_gamcor_lut(dpp_base, params);
Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH 2/4] drm/amd/display: use a separate helper to translate degamma curves

Thanks Melissa for following up.
I like the changes themselves, just some clarifications.
We do have a LUT with predefined HW curve, but it has some limitations: it cannot be used with subsampled 4:2:0/4:2:2 formats and it affects the linearity of color space in which HW scaler operates. In your case, we use the LUT after tone mapping in 3D LUT; that LUT doesn't have predefined curve. So it's not uncommon that we need to program our own degamma.

The motivation for the new segment distribution is best explained with an 8-bit sRGB case. Input has 256 possible values and HW LUTs have 256 points. Ideally in this case we'd use the LUT as basically plain indexing with no interpolation. The new distribution accomplishes this, it aligns 256 HW points with 256 possible inputs. Due to float representations of input, it's not aligned perfectly, but LERP-ing between two HW entries where input is always within small epsilon from one of entries doesn't materially change things.

Quoted 45 lines · Melissa Wen

In newer DCN families, there is no hw predefined curves. So, when setting predefined TFs to gamm_corr, shaper, blend and regamma, the driver resorts to the color modules and program those predefined curves using LUTs. The driver is using the same LUT segmentation when translating EOTF and inverse EOTF to hw points by using the same color management helper to translate curve to hw points, however, LUT representing EOTF may not follow the same region/segment distributions of inverse EOTF. This is causing banding on blend when PQ predefined curve is set. Use different helpers when linearizing and delinearizing. This is the first step to fix banding but already mitigates the issue by increase the number of points per segment from 8 to 16.

---
  .../gpu/drm/amd/display/dc/hwss/dcn32/dcn32_hwseq.c    | 10 ++++------
  1 file changed, 4 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/display/dc/hwss/dcn32/dcn32_hwseq.c +4 -6
index b45ceb570a5c..17d54aadb5e1 100644
@@ -493,9 +493,8 @@ bool dcn32_set_mcm_luts(
  	if (plane_state->blend_tf.type == TF_TYPE_HWPWL)
  		lut_params = &plane_state->blend_tf.pwl;
  	else if (plane_state->blend_tf.type == TF_TYPE_DISTRIBUTED_POINTS) {
-		result = cm3_helper_translate_curve_to_hw_format(plane_state->ctx,
-								 &plane_state->blend_tf,
-								 &dpp_base->regamma_params, false);
+		result = cm3_helper_translate_curve_to_degamma_hw_format(&plane_state->blend_tf,
+									 &dpp_base->regamma_params);
  		if (!result)
  			return result;
 
@@ -551,9 +550,8 @@ bool dcn32_set_input_transfer_func(struct dc *dc,
  	if (plane_state->in_transfer_func.type == TF_TYPE_HWPWL)
  		params = &plane_state->in_transfer_func.pwl;
  	else if (plane_state->in_transfer_func.type == TF_TYPE_DISTRIBUTED_POINTS &&
-		cm3_helper_translate_curve_to_hw_format(plane_state->ctx,
-							&plane_state->in_transfer_func,
-							&dpp_base->degamma_params, false))
+		cm3_helper_translate_curve_to_degamma_hw_format(&plane_state->in_transfer_func,
+								&dpp_base->degamma_params))
  		params = &dpp_base->degamma_params;
 
  	dpp_base->funcs->dpp_program_gamcor_lut(dpp_base, params);
#4 lore raw

Re: [PATCH 2/4] drm/amd/display: use a separate helper to translate degamma curves

Quoted 3 lines · Krunoslav

Thanks Melissa for following up.
I like the changes themselves, just some clarifications.
We do have a LUT with predefined HW curve, but it has some limitations: it cannot be used with subsampled 4:2:0/4:2:2 formats and it affects the linearity of color space in which HW scaler operates. In your case, we use the LUT after tone mapping in 3D LUT; that LUT doesn't have predefined curve. So it's not uncommon that we need to program our own degamma.

Hi Kruno,

Right, I reviewed this part in the cover letter after checking my notes that there is a hw curve for degamma, but not for shaper, blend and post-blend re-gamma, right? I forgot to correct this info here. I wasn't aware about the subsampled limitation.

Quoted 2 lines

The motivation for the new segment distribution is best explained with an 8-bit sRGB case. Input has 256 possible values and HW LUTs have 256 points. Ideally in this case we'd use the LUT as basically plain indexing with no interpolation. The new distribution accomplishes this, it aligns 256 HW points with 256 possible inputs. Due to float representations of input, it's not aligned perfectly, but LERP-ing between two HW entries where input is always within small epsilon from one of entries doesn't materially change things.

I'll rewrite commit messages to include this explanation too. Thanks for sharing these details.

Melissa

Quoted 49 lines · Melissa Wen
Quoted 46 lines

In newer DCN families, there is no hw predefined curves. So, when setting predefined TFs to gamm_corr, shaper, blend and regamma, the driver resorts to the color modules and program those predefined curves using LUTs. The driver is using the same LUT segmentation when translating EOTF and inverse EOTF to hw points by using the same color management helper to translate curve to hw points, however, LUT representing EOTF may not follow the same region/segment distributions of inverse EOTF. This is causing banding on blend when PQ predefined curve is set. Use different helpers when linearizing and delinearizing. This is the first step to fix banding but already mitigates the issue by increase the number of points per segment from 8 to 16.

---
  .../gpu/drm/amd/display/dc/hwss/dcn32/dcn32_hwseq.c    | 10 ++++------

  1 file changed, 4 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/display/dc/hwss/dcn32/dcn32_hwseq.c +2 -3
index b45ceb570a5c..17d54aadb5e1 100644
@@ -493,9 +493,8 @@ bool dcn32_set_mcm_luts(
       if (plane_state->blend_tf.type == TF_TYPE_HWPWL)
           lut_params = &plane_state->blend_tf.pwl;
       else if (plane_state->blend_tf.type == TF_TYPE_DISTRIBUTED_POINTS) {
-        result = 
 cm3_helper_translate_curve_to_hw_format(plane_state->ctx,
-                                 &plane_state->blend_tf,
- &dpp_base->regamma_params, false);
+        result = 
 cm3_helper_translate_curve_to_degamma_hw_format(&plane_state->blend_tf,
+ &dpp_base->regamma_params);
           if (!result)

              return result;
  @@ -551,9 +550,8 @@ bool dcn32_set_input_transfer_func(struct dc *dc, if (plane_state->in_transfer_func.type == TF_TYPE_HWPWL)
          params = &plane_state->in_transfer_func.pwl;
      else if (plane_state->in_transfer_func.type == TF_TYPE_DISTRIBUTED_POINTS &&
- cm3_helper_translate_curve_to_hw_format(plane_state->ctx,
- &plane_state->in_transfer_func,
-                            &dpp_base->degamma_params, false))
+ cm3_helper_translate_curve_to_degamma_hw_format(&plane_state->in_transfer_func,
+ &dpp_base->degamma_params))
          params = &dpp_base->degamma_params; dpp_base->funcs->dpp_program_gamcor_lut(dpp_base, params);

#5 lore raw

PATCH 3/4 drm/amd/display: support up to 256 samples per region

In reply to Melissa Wen

Next commit makes degamma PQ and SRGB EOTF halving distribute points per region which fits better the perceptually uniform shape of inputs, with 9 regions, and a region needs up to 128 samples from TF points (from 2^0 to 2^7). As a preparation, enable up to 256 samples per region (instead of 16). Create a function to linearly interpolate between neighboring TF points for readability.

---
 .../amd/display/dc/dcn30/dcn30_cm_common.c    | 32 +++++++++++++++----
 1 file changed, 25 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/display/dc/dcn30/dcn30_cm_common.c +25 -7
index a439bb6c5e56..b5cb2cd312ba 100644
@@ -303,6 +303,22 @@ bool cm3_helper_translate_curve_to_hw_format(struct dc_context *ctx,
 
 #define NUM_DEGAMMA_REGIONS    12
 
+/* Linear interpolation of tf_pts entries, where (i >> 4) is the integer tf_pts
+ * index, (i & 0xf) is the 1/16 sub-position.
+ */
+static struct fixed31_32 interp_tf_pts(const struct fixed31_32 *output_tf_channel, int i)
+{
+	struct fixed31_32 in_plus_one, in, value;
+	uint32_t t = i & 0xf;
+
+	in_plus_one = output_tf_channel[(i >> 4) + 1];
+	in = output_tf_channel[i >> 4];
+	value = dc_fixpt_sub(in_plus_one, in);
+	value = dc_fixpt_shr(dc_fixpt_mul_int(value, t), 4);
+	value = dc_fixpt_add(in, value);
+
+	return value;
+}
 
 bool cm3_helper_translate_curve_to_degamma_hw_format(
 				const struct dc_transfer_func *output_tf,
@@ -346,18 +362,20 @@ bool cm3_helper_translate_curve_to_degamma_hw_format(
 
 	j = 0;
 	for (k = 0; k < (region_end - region_start); k++) {
-		increment = NUMBER_SW_SEGMENTS / (1 << seg_distr[k]);
+		increment = (NUMBER_SW_SEGMENTS << 4) / (1 << seg_distr[k]);
 		start_index = (region_start + k + MAX_LOW_POINT) *
 				NUMBER_SW_SEGMENTS;
-		for (i = start_index; i < start_index + NUMBER_SW_SEGMENTS;
-				i += increment) {
+		for (i = (start_index << 4);
+		     i < (start_index << 4) + (NUMBER_SW_SEGMENTS << 4);
+		     i += increment) {
 			if (j == hw_points - 1)
 				break;
-			if (i >= TRANSFER_FUNC_POINTS)
+			if ((i >> 4) + 1 >= TRANSFER_FUNC_POINTS)
 				return false;
-			rgb_resulted[j].red = output_tf->tf_pts.red[i];
-			rgb_resulted[j].green = output_tf->tf_pts.green[i];
-			rgb_resulted[j].blue = output_tf->tf_pts.blue[i];
+
+			rgb_resulted[j].red = interp_tf_pts(output_tf->tf_pts.red, i);
+			rgb_resulted[j].green = interp_tf_pts(output_tf->tf_pts.green, i);
+			rgb_resulted[j].blue = interp_tf_pts(output_tf->tf_pts.blue, i);
 			j++;
 		}
 	}
Signature · 1 line
2.53.0
#6 lore raw

PATCH 4/4 drm/amd/display: rework segmentation for linearizing LUT

In reply to Melissa Wen

When linearizing (degamma), the input is an encoded signal bounded to [0,1] and PQ/sRGB EOTFs are steepest near 1. To increase precision in the brightest half without increasing total LUT usage, change region/segment distribution only for PQ/sRGB pre-defined degamma TF by halving the #pts as you go down, so 128 pts in the top region, then 64 in the next smaller, 32 etc. This way the spacing in [0.5,1] region is (1-0.5)/128, in [0.25,0.5] region it's (0.5-0.25)/64 == 0.5/128, and so on.

---
 .../amd/display/dc/dcn30/dcn30_cm_common.c    | 33 ++++++++++++++-----
 1 file changed, 24 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/display/dc/dcn30/dcn30_cm_common.c +24 -9
index b5cb2cd312ba..dc91054134ee 100644
@@ -301,8 +301,6 @@ bool cm3_helper_translate_curve_to_hw_format(struct dc_context *ctx,
 	return true;
 }
 
-#define NUM_DEGAMMA_REGIONS    12
-
 /* Linear interpolation of tf_pts entries, where (i >> 4) is the integer tf_pts
  * index, (i & 0xf) is the 1/16 sub-position.
  */
@@ -343,17 +341,34 @@ bool cm3_helper_translate_curve_to_degamma_hw_format(
 	memset(lut_params, 0, sizeof(struct pwl_params));
 	memset(seg_distr, 0, sizeof(seg_distr));
 
-	region_start = -NUM_DEGAMMA_REGIONS;
-	region_end   = 0;
+	if (output_tf->tf == TRANSFER_FUNCTION_PQ ||
+	    output_tf->tf == TRANSFER_FUNCTION_SRGB) {
+		/* 9 segments
+		 * segments are from 2^-9 to 0
+		 */
+		const uint8_t SEG_COUNT = 9;
+		seg_distr[0] = 0; // Since we only have one point in darkest region
+		for (k = 1; k < SEG_COUNT; k++)
+			seg_distr[k] = k - 1; // 2^(k-1) points per region; halves as k decreases
 
+		region_start = -SEG_COUNT;
+		region_end = 0;
+	} else {
+		/* 12 segments
+		 * segments are from 2^-12 to 2^0
+		 * There are less than 256 points, for optimization
+		 */
+		const uint8_t SEG_COUNT = 12;
+
+		for (i = 0; i < SEG_COUNT; i++)
+			seg_distr[i] = 4;
+
+		region_start = -SEG_COUNT;
+		region_end = 0;
+	}
 
 	for (i = region_end - region_start; i < MAX_REGIONS_NUMBER ; i++)
 		seg_distr[i] = -1;
-	/* 12 segments
-	 * segments are from 2^-12 to 0
-	 */
-	for (i = 0; i < NUM_DEGAMMA_REGIONS ; i++)
-		seg_distr[i] = 4;
 
 	for (k = 0; k < MAX_REGIONS_NUMBER; k++) {
 		if (seg_distr[k] != -1)
Signature · 1 line
2.53.0