A second take on HDR support with an SDR white point and HDR headroom
This better reflects how HDR content is actually used, e.g. most content is in the SDR range, with specular highlights and bright details beyond the SDR range, in the HDR headroom. This more closely matches how HDR is handled on Apple platforms, as EDR. This also greatly simplifies application code which no longer has to think about color scaling. SDR content is rendered at the appropriate brightness automatically, and HDR content is scaled to the correct range for the display HDR headroom.
This commit is contained in:
@@ -18,18 +18,6 @@
|
||||
#include <emscripten/emscripten.h>
|
||||
#endif
|
||||
|
||||
/* The value for the SDR white level on an SDR display, scRGB 1.0 */
|
||||
#define SDR_DISPLAY_WHITE_LEVEL 80.0f
|
||||
|
||||
/* The default value for the SDR white level on an HDR display */
|
||||
#define DEFAULT_SDR_WHITE_LEVEL 200.0f
|
||||
|
||||
/* The default value for the HDR white level on an HDR display */
|
||||
#define DEFAULT_HDR_WHITE_LEVEL 400.0f
|
||||
|
||||
/* The maximum value for the HDR white level on an HDR display */
|
||||
#define MAXIMUM_HDR_WHITE_LEVEL 1000.0f
|
||||
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
|
||||
@@ -46,13 +34,7 @@ static int renderer_count = 0;
|
||||
static int renderer_index = 0;
|
||||
static int stage_index = 0;
|
||||
static int done;
|
||||
static SDL_bool HDR_enabled = SDL_FALSE;
|
||||
static float SDR_white_level = SDR_DISPLAY_WHITE_LEVEL;
|
||||
static float SDR_color_scale = 1.0f;
|
||||
static SDL_FRect SDR_calibration_rect;
|
||||
static float HDR_white_level = SDR_DISPLAY_WHITE_LEVEL;
|
||||
static float HDR_color_scale = 1.0f;
|
||||
static SDL_FRect HDR_calibration_rect;
|
||||
static float HDR_headroom = 1.0f;
|
||||
|
||||
enum
|
||||
{
|
||||
@@ -60,7 +42,6 @@ enum
|
||||
StageDrawBackground,
|
||||
StageBlendDrawing,
|
||||
StageBlendTexture,
|
||||
StageHDRCalibration,
|
||||
StageGradientDrawing,
|
||||
StageGradientTexture,
|
||||
StageCount
|
||||
@@ -73,84 +54,22 @@ static void FreeRenderer(void)
|
||||
renderer = NULL;
|
||||
}
|
||||
|
||||
static float GetDisplaySDRWhiteLevel(void)
|
||||
{
|
||||
SDL_PropertiesID props;
|
||||
|
||||
if (!HDR_enabled) {
|
||||
/* HDR is not enabled, use the SDR white level */
|
||||
return SDR_DISPLAY_WHITE_LEVEL;
|
||||
}
|
||||
|
||||
props = SDL_GetRendererProperties(renderer);
|
||||
if (SDL_GetNumberProperty(props, SDL_PROP_RENDERER_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB) != SDL_COLORSPACE_SCRGB) {
|
||||
/* We're not displaying in HDR, use the SDR white level */
|
||||
return SDR_DISPLAY_WHITE_LEVEL;
|
||||
}
|
||||
|
||||
props = SDL_GetDisplayProperties(SDL_GetDisplayForWindow(window));
|
||||
return SDL_GetFloatProperty(props, SDL_PROP_DISPLAY_SDR_WHITE_LEVEL_FLOAT, DEFAULT_SDR_WHITE_LEVEL);
|
||||
}
|
||||
|
||||
static void SetSDRWhiteLevel(float value)
|
||||
{
|
||||
if (value == SDR_white_level) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_Log("SDR white level set to %g nits\n", value);
|
||||
SDR_white_level = value;
|
||||
SDR_color_scale = SDR_white_level / SDR_DISPLAY_WHITE_LEVEL;
|
||||
SDL_SetRenderColorScale(renderer, SDR_color_scale);
|
||||
}
|
||||
|
||||
static float GetDisplayHDRWhiteLevel(void)
|
||||
{
|
||||
SDL_PropertiesID props;
|
||||
|
||||
if (!HDR_enabled) {
|
||||
/* HDR is not enabled, use the SDR white level */
|
||||
return SDR_DISPLAY_WHITE_LEVEL;
|
||||
}
|
||||
|
||||
props = SDL_GetRendererProperties(renderer);
|
||||
if (SDL_GetNumberProperty(props, SDL_PROP_RENDERER_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB) != SDL_COLORSPACE_SCRGB) {
|
||||
/* We're not displaying in HDR, use the SDR white level */
|
||||
return SDR_DISPLAY_WHITE_LEVEL;
|
||||
}
|
||||
|
||||
props = SDL_GetDisplayProperties(SDL_GetDisplayForWindow(window));
|
||||
return SDL_GetFloatProperty(props, SDL_PROP_DISPLAY_HDR_WHITE_LEVEL_FLOAT, DEFAULT_HDR_WHITE_LEVEL);
|
||||
}
|
||||
|
||||
static void SetHDRWhiteLevel(float value)
|
||||
{
|
||||
if (value == HDR_white_level) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_Log("HDR white level set to %g nits\n", value);
|
||||
HDR_white_level = value;
|
||||
HDR_color_scale = HDR_white_level / SDR_DISPLAY_WHITE_LEVEL;
|
||||
}
|
||||
|
||||
static void UpdateHDRState(void)
|
||||
{
|
||||
SDL_PropertiesID props;
|
||||
SDL_bool HDR_enabled;
|
||||
|
||||
props = SDL_GetDisplayProperties(SDL_GetDisplayForWindow(window));
|
||||
HDR_enabled = SDL_GetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, SDL_FALSE);
|
||||
|
||||
SetHDRWhiteLevel(GetDisplayHDRWhiteLevel());
|
||||
SetSDRWhiteLevel(GetDisplaySDRWhiteLevel());
|
||||
|
||||
SDL_Log("HDR %s\n", HDR_enabled ? "enabled" : "disabled");
|
||||
|
||||
if (HDR_enabled) {
|
||||
props = SDL_GetRendererProperties(renderer);
|
||||
if (SDL_GetNumberProperty(props, SDL_PROP_RENDERER_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB) != SDL_COLORSPACE_SCRGB) {
|
||||
SDL_Log("Run with --colorspace scRGB to display HDR colors\n");
|
||||
if (SDL_GetNumberProperty(props, SDL_PROP_RENDERER_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB) != SDL_COLORSPACE_SRGB_LINEAR) {
|
||||
SDL_Log("Run with --colorspace linear to display HDR colors\n");
|
||||
}
|
||||
HDR_headroom = SDL_GetFloatProperty(props, SDL_PROP_RENDERER_HDR_HEADROOM_FLOAT, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,6 +157,9 @@ static SDL_bool ReadPixel(int x, int y, SDL_Color *c)
|
||||
|
||||
surface = SDL_RenderReadPixels(renderer, &r);
|
||||
if (surface) {
|
||||
/* We don't want to do any HDR -> SDR tone mapping */
|
||||
SDL_SetFloatProperty(SDL_GetSurfaceProperties(surface), SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT, 0.0f);
|
||||
|
||||
if (SDL_ReadSurfacePixel(surface, 0, 0, &c->r, &c->g, &c->b, &c->a) == 0) {
|
||||
result = SDL_TRUE;
|
||||
} else {
|
||||
@@ -266,20 +188,6 @@ static void DrawText(float x, float y, const char *fmt, ...)
|
||||
SDL_free(text);
|
||||
}
|
||||
|
||||
static void DrawTextWhite(float x, float y, const char *fmt, ...)
|
||||
{
|
||||
char *text;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
SDL_vasprintf(&text, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
SDLTest_DrawString(renderer, x, y, text);
|
||||
SDL_free(text);
|
||||
}
|
||||
|
||||
static void RenderClearBackground(void)
|
||||
{
|
||||
/* Draw a 50% gray background.
|
||||
@@ -488,85 +396,38 @@ static void DrawGradient(float x, float y, float width, float height, float star
|
||||
SDL_RenderGeometryRawFloat(renderer, NULL, xy, xy_stride, color, color_stride, NULL, 0, num_vertices, indices, num_indices, size_indices);
|
||||
}
|
||||
|
||||
static float scRGBtoNits(float v)
|
||||
{
|
||||
return v * 80.0f;
|
||||
}
|
||||
|
||||
static float scRGBfromNits(float v)
|
||||
{
|
||||
return v / 80.0f;
|
||||
}
|
||||
|
||||
static float sRGBtoLinear(float v)
|
||||
{
|
||||
if (v <= 0.04045f) {
|
||||
v = (v / 12.92f);
|
||||
} else {
|
||||
v = SDL_powf(((v + 0.055f) / 1.055f), 2.4f);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static float sRGBFromLinear(float v)
|
||||
{
|
||||
if (v <= 0.0031308f) {
|
||||
v = (v * 12.92f);
|
||||
} else {
|
||||
v = (SDL_powf(v, 1.0f / 2.4f) * 1.055f - 0.055f);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static float sRGBtoNits(float v)
|
||||
{
|
||||
return scRGBtoNits(sRGBtoLinear(v));
|
||||
}
|
||||
|
||||
static float sRGBfromNits(float v)
|
||||
{
|
||||
return sRGBFromLinear(scRGBfromNits(v));
|
||||
}
|
||||
|
||||
static void RenderGradientDrawing(void)
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
float x = TEXT_START_X;
|
||||
float y = TEXT_START_Y;
|
||||
DrawTextWhite(x, y, "%s %s", renderer_name, colorspace_name);
|
||||
DrawText(x, y, "%s %s", renderer_name, colorspace_name);
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
DrawTextWhite(x, y, "Test: Draw SDR and HDR gradients");
|
||||
DrawText(x, y, "Test: Draw SDR and HDR gradients");
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
DrawTextWhite(x, y, "SDR gradient (%g nits)", SDR_white_level);
|
||||
DrawText(x, y, "SDR gradient");
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
SDL_SetRenderColorScale(renderer, 1.0f);
|
||||
DrawGradient(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, sRGBfromNits(SDR_white_level));
|
||||
SDL_SetRenderColorScale(renderer, SDR_color_scale);
|
||||
DrawGradient(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, 1.0f);
|
||||
y += 64.0f;
|
||||
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
DrawTextWhite(x, y, "HDR gradient (%g nits)", HDR_white_level);
|
||||
if (HDR_headroom > 1.0f) {
|
||||
DrawText(x, y, "HDR gradient");
|
||||
} else {
|
||||
DrawText(x, y, "No HDR headroom, HDR and SDR gradient are the same");
|
||||
}
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
/* Drawing is in the sRGB colorspace, so we need to use the color scale, which is applied in linear space, to get into high dynamic range */
|
||||
SDL_SetRenderColorScale(renderer, HDR_headroom);
|
||||
DrawGradient(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, 1.0f);
|
||||
SDL_SetRenderColorScale(renderer, 1.0f);
|
||||
DrawGradient(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, sRGBfromNits(HDR_white_level));
|
||||
SDL_SetRenderColorScale(renderer, SDR_color_scale);
|
||||
y += 64.0f;
|
||||
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
DrawTextWhite(x, y, "HDR gradient (%g nits)", MAXIMUM_HDR_WHITE_LEVEL);
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
SDL_SetRenderColorScale(renderer, 1.0f);
|
||||
DrawGradient(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, sRGBfromNits(MAXIMUM_HDR_WHITE_LEVEL));
|
||||
SDL_SetRenderColorScale(renderer, SDR_color_scale);
|
||||
y += 64.0f;
|
||||
}
|
||||
|
||||
@@ -575,20 +436,12 @@ static SDL_Texture *CreateGradientTexture(int width, float start, float end)
|
||||
SDL_Texture *texture;
|
||||
float *pixels;
|
||||
|
||||
/* Floating point textures are in the scRGB colorspace by default */
|
||||
/* Floating point textures are in the linear colorspace by default */
|
||||
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA128_FLOAT, SDL_TEXTUREACCESS_STATIC, width, 1);
|
||||
if (!texture) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (colorspace == SDL_COLORSPACE_SCRGB) {
|
||||
/* The input values are in the sRGB colorspace, but we're blending in linear space */
|
||||
start = sRGBtoLinear(start);
|
||||
end = sRGBtoLinear(end);
|
||||
} else if (colorspace == SDL_COLORSPACE_SRGB) {
|
||||
/* The input values are in the sRGB colorspace, and we're blending in sRGB space */
|
||||
}
|
||||
|
||||
pixels = (float *)SDL_malloc(width * sizeof(float) * 4);
|
||||
if (pixels) {
|
||||
int i;
|
||||
@@ -617,129 +470,35 @@ static void DrawGradientTexture(float x, float y, float width, float height, flo
|
||||
|
||||
static void RenderGradientTexture(void)
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
float x = TEXT_START_X;
|
||||
float y = TEXT_START_Y;
|
||||
DrawTextWhite(x, y, "%s %s", renderer_name, colorspace_name);
|
||||
DrawText(x, y, "%s %s", renderer_name, colorspace_name);
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
DrawTextWhite(x, y, "Test: Texture SDR and HDR gradients");
|
||||
DrawText(x, y, "Test: Texture SDR and HDR gradients");
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
DrawTextWhite(x, y, "SDR gradient (%g nits)", SDR_white_level);
|
||||
DrawText(x, y, "SDR gradient");
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
SDL_SetRenderColorScale(renderer, 1.0f);
|
||||
DrawGradientTexture(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, sRGBfromNits(SDR_white_level));
|
||||
SDL_SetRenderColorScale(renderer, SDR_color_scale);
|
||||
DrawGradientTexture(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, 1.0f);
|
||||
y += 64.0f;
|
||||
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
DrawTextWhite(x, y, "HDR gradient (%g nits)", HDR_white_level);
|
||||
if (HDR_headroom > 1.0f) {
|
||||
DrawText(x, y, "HDR gradient");
|
||||
} else {
|
||||
DrawText(x, y, "No HDR headroom, HDR and SDR gradient are the same");
|
||||
}
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
SDL_SetRenderColorScale(renderer, 1.0f);
|
||||
DrawGradientTexture(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, sRGBfromNits(HDR_white_level));
|
||||
SDL_SetRenderColorScale(renderer, SDR_color_scale);
|
||||
/* The gradient texture is in the linear colorspace, so we can use the HDR_headroom value directly */
|
||||
DrawGradientTexture(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, HDR_headroom);
|
||||
y += 64.0f;
|
||||
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
DrawTextWhite(x, y, "HDR gradient (%g nits)", MAXIMUM_HDR_WHITE_LEVEL);
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
SDL_SetRenderColorScale(renderer, 1.0f);
|
||||
DrawGradientTexture(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, sRGBfromNits(MAXIMUM_HDR_WHITE_LEVEL));
|
||||
SDL_SetRenderColorScale(renderer, SDR_color_scale);
|
||||
y += 64.0f;
|
||||
}
|
||||
|
||||
static void RenderHDRCalibration(void)
|
||||
{
|
||||
SDL_FRect rect;
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
float x = TEXT_START_X;
|
||||
float y = TEXT_START_Y;
|
||||
DrawTextWhite(x, y, "%s %s", renderer_name, colorspace_name);
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
DrawTextWhite(x, y, "Test: HDR calibration");
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
if (!HDR_enabled) {
|
||||
DrawTextWhite(x, y, "HDR not enabled");
|
||||
return;
|
||||
}
|
||||
|
||||
DrawTextWhite(x, y, "Select HDR maximum brightness (%g nits)", HDR_white_level);
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
DrawTextWhite(x, y, "The square in the middle should just barely be visible");
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
HDR_calibration_rect.x = x;
|
||||
HDR_calibration_rect.y = y;
|
||||
HDR_calibration_rect.w = WINDOW_WIDTH - 2 * x;
|
||||
HDR_calibration_rect.h = 64.0f;
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
SDL_SetRenderColorScale(renderer, MAXIMUM_HDR_WHITE_LEVEL / SDR_DISPLAY_WHITE_LEVEL);
|
||||
SDL_RenderFillRect(renderer, &HDR_calibration_rect);
|
||||
SDL_SetRenderColorScale(renderer, HDR_color_scale * 0.90f);
|
||||
rect = HDR_calibration_rect;
|
||||
rect.h -= 4.0f;
|
||||
rect.w = 60.0f;
|
||||
rect.x = (WINDOW_WIDTH - rect.w) / 2.0f;
|
||||
rect.y += 2.0f;
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
SDL_SetRenderColorScale(renderer, SDR_color_scale);
|
||||
y += 64.0f;
|
||||
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
|
||||
DrawTextWhite(x, y, "Select SDR maximum brightness (%g nits)", SDR_white_level);
|
||||
y += TEXT_LINE_ADVANCE;
|
||||
SDR_calibration_rect.x = x;
|
||||
SDR_calibration_rect.y = y;
|
||||
SDR_calibration_rect.w = WINDOW_WIDTH - 2 * x;
|
||||
SDR_calibration_rect.h = 64.0f;
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
SDL_RenderFillRect(renderer, &SDR_calibration_rect);
|
||||
}
|
||||
|
||||
static void OnHDRCalibrationMouseHeld(float x, float y)
|
||||
{
|
||||
SDL_FPoint mouse = { x, y };
|
||||
|
||||
if (SDL_PointInRectFloat(&mouse, &HDR_calibration_rect)) {
|
||||
float v = (x - HDR_calibration_rect.x) / HDR_calibration_rect.w;
|
||||
v *= (sRGBfromNits(MAXIMUM_HDR_WHITE_LEVEL) - 1.0f);
|
||||
v += 1.0f;
|
||||
v = sRGBtoNits(v);
|
||||
SetHDRWhiteLevel(v);
|
||||
return;
|
||||
}
|
||||
|
||||
if (SDL_PointInRectFloat(&mouse, &SDR_calibration_rect)) {
|
||||
float v = (x - SDR_calibration_rect.x) / SDR_calibration_rect.w;
|
||||
v *= (sRGBfromNits(MAXIMUM_HDR_WHITE_LEVEL) - 1.0f);
|
||||
v += 1.0f;
|
||||
v = sRGBtoNits(v);
|
||||
SetSDRWhiteLevel(v);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void OnMouseHeld(float x, float y)
|
||||
{
|
||||
if (stage_index == StageHDRCalibration) {
|
||||
OnHDRCalibrationMouseHeld(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
static void loop(void)
|
||||
@@ -768,12 +527,6 @@ static void loop(void)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
OnMouseHeld(event.button.x, event.button.y);
|
||||
} else if (event.type == SDL_EVENT_MOUSE_MOTION) {
|
||||
if (event.motion.state) {
|
||||
OnMouseHeld(event.button.x, event.button.y);
|
||||
}
|
||||
} else if (event.type == SDL_EVENT_DISPLAY_HDR_STATE_CHANGED) {
|
||||
UpdateHDRState();
|
||||
} else if (event.type == SDL_EVENT_QUIT) {
|
||||
@@ -798,9 +551,6 @@ static void loop(void)
|
||||
case StageBlendTexture:
|
||||
RenderBlendTexture();
|
||||
break;
|
||||
case StageHDRCalibration:
|
||||
RenderHDRCalibration();
|
||||
break;
|
||||
case StageGradientDrawing:
|
||||
RenderGradientDrawing();
|
||||
break;
|
||||
@@ -844,8 +594,8 @@ int main(int argc, char *argv[])
|
||||
colorspace_name = argv[i + 1];
|
||||
if (SDL_strcasecmp(colorspace_name, "sRGB") == 0) {
|
||||
colorspace = SDL_COLORSPACE_SRGB;
|
||||
} else if (SDL_strcasecmp(colorspace_name, "scRGB") == 0) {
|
||||
colorspace = SDL_COLORSPACE_SCRGB;
|
||||
} else if (SDL_strcasecmp(colorspace_name, "linear") == 0) {
|
||||
colorspace = SDL_COLORSPACE_SRGB_LINEAR;
|
||||
/* Not currently supported
|
||||
} else if (SDL_strcasecmp(colorspace_name, "HDR10") == 0) {
|
||||
colorspace = SDL_COLORSPACE_HDR10;
|
||||
|
||||
@@ -57,15 +57,6 @@
|
||||
|
||||
#include "icon.h"
|
||||
|
||||
/* The value for the SDR white level on an SDR display, scRGB 1.0 */
|
||||
#define SDR_DISPLAY_WHITE_LEVEL 80.0f
|
||||
|
||||
/* The default value for the SDR white level on an HDR display */
|
||||
#define DEFAULT_SDR_WHITE_LEVEL 200.0f
|
||||
|
||||
/* The default value for the HDR white level on an HDR display */
|
||||
#define DEFAULT_HDR_WHITE_LEVEL 400.0f
|
||||
|
||||
|
||||
static SDL_Texture *sprite;
|
||||
static SDL_FRect *positions;
|
||||
@@ -97,56 +88,6 @@ struct SwsContextContainer
|
||||
static const char *SWS_CONTEXT_CONTAINER_PROPERTY = "SWS_CONTEXT_CONTAINER";
|
||||
static int done;
|
||||
|
||||
/* This function isn't platform specific, but we haven't hooked up HDR video support on other platforms yet */
|
||||
#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_APPLE)
|
||||
static void GetDisplayHDRProperties(SDL_bool *HDR_display, float *SDR_white_level, float *HDR_white_level)
|
||||
{
|
||||
SDL_PropertiesID props;
|
||||
|
||||
*HDR_display = SDL_FALSE;
|
||||
*SDR_white_level = SDR_DISPLAY_WHITE_LEVEL;
|
||||
*HDR_white_level = SDR_DISPLAY_WHITE_LEVEL;
|
||||
|
||||
props = SDL_GetRendererProperties(renderer);
|
||||
if (SDL_GetNumberProperty(props, SDL_PROP_RENDERER_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB) != SDL_COLORSPACE_SCRGB) {
|
||||
/* We're not displaying in HDR, use the SDR white level */
|
||||
return;
|
||||
}
|
||||
|
||||
props = SDL_GetDisplayProperties(SDL_GetDisplayForWindow(window));
|
||||
if (!SDL_GetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, SDL_FALSE)) {
|
||||
/* HDR is not enabled on the display */
|
||||
return;
|
||||
}
|
||||
|
||||
*HDR_display = SDL_TRUE;
|
||||
*SDR_white_level = SDL_GetFloatProperty(props, SDL_PROP_DISPLAY_SDR_WHITE_LEVEL_FLOAT, DEFAULT_SDR_WHITE_LEVEL);
|
||||
*HDR_white_level = SDL_GetFloatProperty(props, SDL_PROP_DISPLAY_HDR_WHITE_LEVEL_FLOAT, DEFAULT_HDR_WHITE_LEVEL);
|
||||
}
|
||||
|
||||
static void UpdateVideoColorScale(SDL_bool HDR_video)
|
||||
{
|
||||
SDL_bool HDR_display = SDL_FALSE;
|
||||
float SDR_white_level, HDR_white_level, video_white_level;
|
||||
|
||||
GetDisplayHDRProperties(&HDR_display, &SDR_white_level, &HDR_white_level);
|
||||
|
||||
if (HDR_video) {
|
||||
video_white_level = DEFAULT_HDR_WHITE_LEVEL;
|
||||
} else {
|
||||
video_white_level = SDR_DISPLAY_WHITE_LEVEL;
|
||||
}
|
||||
|
||||
if (HDR_display && HDR_video) {
|
||||
/* Scale the HDR range of the video to the HDR range of the display */
|
||||
SDL_SetRenderColorScale(renderer, HDR_white_level / video_white_level);
|
||||
} else {
|
||||
/* Scale the range of the video to the SDR range of the display */
|
||||
SDL_SetRenderColorScale(renderer, SDR_white_level / video_white_level);
|
||||
}
|
||||
}
|
||||
#endif /* SDL_PLATFORM_WIN32 || SDL_PLATFORM_APPLE */
|
||||
|
||||
static SDL_bool CreateWindowAndRenderer(Uint32 window_flags, const char *driver)
|
||||
{
|
||||
SDL_PropertiesID props;
|
||||
@@ -178,7 +119,7 @@ static SDL_bool CreateWindowAndRenderer(Uint32 window_flags, const char *driver)
|
||||
props = SDL_CreateProperties();
|
||||
SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, driver);
|
||||
SDL_SetProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window);
|
||||
SDL_SetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SCRGB);
|
||||
SDL_SetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB_LINEAR);
|
||||
renderer = SDL_CreateRendererWithProperties(props);
|
||||
if (!renderer) {
|
||||
/* Try again with the sRGB colorspace */
|
||||
@@ -699,7 +640,6 @@ static SDL_bool GetTextureForD3D11Frame(AVFrame *frame, SDL_Texture **texture)
|
||||
}
|
||||
if (!*texture || (UINT)texture_width != desc.Width || (UINT)texture_height != desc.Height) {
|
||||
Uint32 format;
|
||||
SDL_bool HDR_video = SDL_FALSE;
|
||||
|
||||
switch (desc.Format) {
|
||||
case DXGI_FORMAT_NV12:
|
||||
@@ -707,11 +647,6 @@ static SDL_bool GetTextureForD3D11Frame(AVFrame *frame, SDL_Texture **texture)
|
||||
break;
|
||||
case DXGI_FORMAT_P010:
|
||||
format = SDL_PIXELFORMAT_P010;
|
||||
HDR_video = SDL_TRUE;
|
||||
break;
|
||||
case DXGI_FORMAT_P016:
|
||||
format = SDL_PIXELFORMAT_P016;
|
||||
HDR_video = SDL_TRUE;
|
||||
break;
|
||||
default:
|
||||
SDL_SetError("Unsupported texture format %d", desc.Format);
|
||||
@@ -733,8 +668,6 @@ static SDL_bool GetTextureForD3D11Frame(AVFrame *frame, SDL_Texture **texture)
|
||||
if (!*texture) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
UpdateVideoColorScale(HDR_video);
|
||||
}
|
||||
|
||||
ID3D11Resource *dx11_resource = SDL_GetProperty(SDL_GetTextureProperties(*texture), SDL_PROP_TEXTURE_D3D11_TEXTURE_POINTER, NULL);
|
||||
@@ -759,7 +692,6 @@ static SDL_bool GetTextureForVideoToolboxFrame(AVFrame *frame, SDL_Texture **tex
|
||||
size_t nPixelBufferHeight = CVPixelBufferGetHeightOfPlane(pPixelBuffer, 0);
|
||||
SDL_PropertiesID props;
|
||||
Uint32 format;
|
||||
SDL_bool HDR_video = SDL_FALSE;
|
||||
|
||||
switch (nPixelBufferType) {
|
||||
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
||||
@@ -769,7 +701,6 @@ static SDL_bool GetTextureForVideoToolboxFrame(AVFrame *frame, SDL_Texture **tex
|
||||
case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
|
||||
case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
|
||||
format = SDL_PIXELFORMAT_P010;
|
||||
HDR_video = SDL_TRUE;
|
||||
break;
|
||||
default:
|
||||
SDL_SetError("Unsupported texture format %c%c%c%c",
|
||||
@@ -799,8 +730,6 @@ static SDL_bool GetTextureForVideoToolboxFrame(AVFrame *frame, SDL_Texture **tex
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
UpdateVideoColorScale(HDR_video);
|
||||
|
||||
return SDL_TRUE;
|
||||
#else
|
||||
return SDL_FALSE;
|
||||
|
||||
@@ -78,7 +78,7 @@ static SDL_bool verify_yuv_data(Uint32 format, SDL_Colorspace colorspace, const
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
if (SDL_ConvertPixelsAndColorspace(surface->w, surface->h, format, colorspace, yuv, yuv_pitch, surface->format->format, SDL_COLORSPACE_SRGB, rgb, surface->pitch) == 0) {
|
||||
if (SDL_ConvertPixelsAndColorspace(surface->w, surface->h, format, colorspace, 0, yuv, yuv_pitch, surface->format->format, SDL_COLORSPACE_SRGB, 0, rgb, surface->pitch) == 0) {
|
||||
int x, y;
|
||||
result = SDL_TRUE;
|
||||
for (y = 0; y < surface->h; ++y) {
|
||||
@@ -156,7 +156,7 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
|
||||
/* Verify conversion to YUV formats */
|
||||
for (i = 0; i < SDL_arraysize(formats); ++i) {
|
||||
yuv1_pitch = CalculateYUVPitch(formats[i], pattern->w) + extra_pitch;
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, pattern->format->format, SDL_COLORSPACE_SRGB, pattern->pixels, pattern->pitch, formats[i], colorspace, yuv1, yuv1_pitch) < 0) {
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, pattern->format->format, SDL_COLORSPACE_SRGB, 0, pattern->pixels, pattern->pitch, formats[i], colorspace, 0, yuv1, yuv1_pitch) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(pattern->format->format), SDL_GetPixelFormatName(formats[i]), SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
@@ -171,11 +171,11 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
|
||||
for (j = 0; j < SDL_arraysize(formats); ++j) {
|
||||
yuv1_pitch = CalculateYUVPitch(formats[i], pattern->w) + extra_pitch;
|
||||
yuv2_pitch = CalculateYUVPitch(formats[j], pattern->w) + extra_pitch;
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, pattern->format->format, SDL_COLORSPACE_SRGB, pattern->pixels, pattern->pitch, formats[i], colorspace, yuv1, yuv1_pitch) < 0) {
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, pattern->format->format, SDL_COLORSPACE_SRGB, 0, pattern->pixels, pattern->pitch, formats[i], colorspace, 0, yuv1, yuv1_pitch) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(pattern->format->format), SDL_GetPixelFormatName(formats[i]), SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, formats[i], colorspace, yuv1, yuv1_pitch, formats[j], colorspace, yuv2, yuv2_pitch) < 0) {
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, formats[i], colorspace, 0, yuv1, yuv1_pitch, formats[j], colorspace, 0, yuv2, yuv2_pitch) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]), SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
@@ -196,11 +196,11 @@ static int run_automated_tests(int pattern_size, int extra_pitch)
|
||||
|
||||
yuv1_pitch = CalculateYUVPitch(formats[i], pattern->w) + extra_pitch;
|
||||
yuv2_pitch = CalculateYUVPitch(formats[j], pattern->w) + extra_pitch;
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, pattern->format->format, SDL_COLORSPACE_SRGB, pattern->pixels, pattern->pitch, formats[i], colorspace, yuv1, yuv1_pitch) < 0) {
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, pattern->format->format, SDL_COLORSPACE_SRGB, 0, pattern->pixels, pattern->pitch, formats[i], colorspace, 0, yuv1, yuv1_pitch) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(pattern->format->format), SDL_GetPixelFormatName(formats[i]), SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, formats[i], colorspace, yuv1, yuv1_pitch, formats[j], colorspace, yuv1, yuv2_pitch) < 0) {
|
||||
if (SDL_ConvertPixelsAndColorspace(pattern->w, pattern->h, formats[i], colorspace, 0, yuv1, yuv1_pitch, formats[j], colorspace, 0, yuv1, yuv2_pitch) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]), SDL_GetError());
|
||||
goto done;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user