Skip to content

Commit d92c2f9

Browse files
authored
Merge pull request #852 from bjornbytes/hdr
HDR
2 parents ae02fab + 354bf7e commit d92c2f9

File tree

7 files changed

+104
-10
lines changed

7 files changed

+104
-10
lines changed

etc/boot.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ local conf = {
2727
vsync = true,
2828
stencil = false,
2929
antialias = true,
30+
hdr = false,
3031
shadercache = true
3132
},
3233
headset = {

etc/shaders/lovr.glsl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,58 @@ vec3 linearToGamma(vec3 color) {
448448
return mix(1.055 * pow(color, vec3(1. / 2.4)) - .055, color * 12.92, lessThanEqual(color, vec3(.0031308)));
449449
}
450450

451+
vec3 pqToLinear(vec3 color) {
452+
float c1 = .835975;
453+
float c2 = 18.8515625;
454+
float c3 = 18.6875;
455+
float m1 = .1593017578125;
456+
float m2 = 78.84375;
457+
vec3 em2 = pow(color, vec3(1. / m2));
458+
return pow(max(em2 - c1, 0.) / (c2 - c3 * em2), vec3(1. / m1));
459+
}
460+
461+
vec3 linearToPQ(vec3 color) {
462+
float c1 = .835975;
463+
float c2 = 18.8515625;
464+
float c3 = 18.6875;
465+
float m1 = .1593017578125;
466+
float m2 = 78.84375;
467+
vec3 ym1 = pow(color, vec3(m1));
468+
return pow((c1 + c2 * ym1) / (1. + c3 * ym1), vec3(m2));
469+
}
470+
471+
vec3 sRGBToRec2020(vec3 color) {
472+
mat3 xyz_from_srgb = transpose(mat3(
473+
vec3(0.4123908, 0.3575843, 0.1804808),
474+
vec3(0.2126390, 0.7151687, 0.0721923),
475+
vec3(0.0193308, 0.1191948, 0.9505322)
476+
));
477+
478+
mat3 rec2020_from_xyz = transpose(mat3(
479+
vec3( 1.7166512, -0.3556708, -0.2533663),
480+
vec3(-0.6666844, 1.6164812, 0.0157685),
481+
vec3( 0.0176399, -0.0427706, 0.9421031)
482+
));
483+
484+
return rec2020_from_xyz * xyz_from_srgb * color;
485+
}
486+
487+
vec3 rec2020ToSRGB(vec3 color) {
488+
mat3 xyz_from_rec2020 = transpose(mat3(
489+
vec3(0.6369580, 0.1446169, 0.1688810),
490+
vec3(0.2627002, 0.6779981, 0.0593017),
491+
vec3(0.0000000, 0.0280727, 1.0609851)
492+
));
493+
494+
mat3 srgb_from_xyz = transpose(mat3(
495+
vec3( 3.2409699, -1.5373832, -0.4986108),
496+
vec3(-0.9692436, 1.8759675, 0.0415551),
497+
vec3( 0.0556301, -0.2039770, 1.0569715)
498+
));
499+
500+
return srgb_from_xyz * xyz_from_rec2020 * color;
501+
}
502+
451503
uint packSnorm10x3(vec4 v) {
452504
return
453505
((int(v.x * 511.) & 0x3ff) << 0) |

src/api/l_graphics.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ static int l_lovrGraphicsInitialize(lua_State* L) {
325325
.debug = false,
326326
.vsync = false,
327327
.stencil = false,
328-
.antialias = true
328+
.antialias = true,
329+
.hdr = false
329330
};
330331

331332
bool shaderCache = true;
@@ -349,6 +350,10 @@ static int l_lovrGraphicsInitialize(lua_State* L) {
349350
config.antialias = lua_toboolean(L, -1);
350351
lua_pop(L, 1);
351352

353+
lua_getfield(L, -1, "hdr");
354+
config.hdr = lua_toboolean(L, -1);
355+
lua_pop(L, 1);
356+
352357
lua_getfield(L, -1, "shadercache");
353358
shaderCache = lua_toboolean(L, -1);
354359
lua_pop(L, 1);
@@ -531,6 +536,12 @@ static int l_lovrGraphicsIsFormatSupported(lua_State* L) {
531536
return 2;
532537
}
533538

539+
static int l_lovrGraphicsIsHDR(lua_State* L) {
540+
bool hdr = lovrGraphicsIsHDR();
541+
lua_pushboolean(L, hdr);
542+
return 1;
543+
}
544+
534545
static int l_lovrGraphicsGetBackgroundColor(lua_State* L) {
535546
float color[4];
536547
lovrGraphicsGetBackgroundColor(color);
@@ -1541,6 +1552,7 @@ static const luaL_Reg lovrGraphics[] = {
15411552
{ "getFeatures", l_lovrGraphicsGetFeatures },
15421553
{ "getLimits", l_lovrGraphicsGetLimits },
15431554
{ "isFormatSupported", l_lovrGraphicsIsFormatSupported },
1555+
{ "isHDR", l_lovrGraphicsIsHDR },
15441556
{ "getBackgroundColor", l_lovrGraphicsGetBackgroundColor },
15451557
{ "setBackgroundColor", l_lovrGraphicsSetBackgroundColor },
15461558
{ "getWindowPass", l_lovrGraphicsGetWindowPass },

src/core/gpu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ typedef struct {
163163
uint32_t width;
164164
uint32_t height;
165165
bool vsync;
166+
bool hdr;
166167
union {
167168
struct {
168169
uintptr_t window;
@@ -180,6 +181,7 @@ typedef struct {
180181

181182
bool gpu_surface_init(gpu_surface_info* info);
182183
gpu_texture_format gpu_surface_get_format(void);
184+
bool gpu_surface_is_hdr(void);
183185
bool gpu_surface_resize(uint32_t width, uint32_t height);
184186
bool gpu_surface_acquire(gpu_texture** texture);
185187
bool gpu_surface_present(void);

src/core/gpu_vk.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -813,15 +813,29 @@ bool gpu_surface_init(gpu_surface_info* info) {
813813
uint32_t formatCount = COUNTOF(formats);
814814
vkGetPhysicalDeviceSurfaceFormatsKHR(state.adapter, surface->handle, &formatCount, formats);
815815

816-
for (uint32_t i = 0; i < formatCount; i++) {
817-
if (formats[i].format == VK_FORMAT_R8G8B8A8_SRGB) {
818-
surface->format = GPU_FORMAT_RGBA8;
819-
surface->vkformat = formats[i];
820-
break;
821-
} else if (formats[i].format == VK_FORMAT_B8G8R8A8_SRGB) {
822-
surface->format = GPU_FORMAT_BGRA8;
823-
surface->vkformat = formats[i];
824-
break;
816+
surface->vkformat.format = VK_FORMAT_UNDEFINED;
817+
818+
if (info->hdr && state.extensions.colorspace) {
819+
for (uint32_t i = 0; i < formatCount; i++) {
820+
if (formats[i].format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 && formats[i].colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT) {
821+
surface->format = GPU_FORMAT_RGB10A2;
822+
surface->vkformat = formats[i];
823+
break;
824+
}
825+
}
826+
}
827+
828+
if (!surface->vkformat.format) {
829+
for (uint32_t i = 0; i < formatCount; i++) {
830+
if (formats[i].format == VK_FORMAT_R8G8B8A8_SRGB) {
831+
surface->format = GPU_FORMAT_RGBA8;
832+
surface->vkformat = formats[i];
833+
break;
834+
} else if (formats[i].format == VK_FORMAT_B8G8R8A8_SRGB) {
835+
surface->format = GPU_FORMAT_BGRA8;
836+
surface->vkformat = formats[i];
837+
break;
838+
}
825839
}
826840
}
827841

@@ -844,6 +858,10 @@ gpu_texture_format gpu_surface_get_format(void) {
844858
return state.surface.format;
845859
}
846860

861+
bool gpu_surface_is_hdr(void) {
862+
return state.surface.vkformat.format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 && state.surface.vkformat.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT;
863+
}
864+
847865
bool gpu_surface_resize(uint32_t width, uint32_t height) {
848866
if (width == 0 || height == 0) {
849867
state.surface.valid = false;

src/modules/graphics/graphics.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,11 @@ void lovrGraphicsGetShaderCache(void* data, size_t* size) {
10241024
gpu_pipeline_get_cache(data, size);
10251025
}
10261026

1027+
bool lovrGraphicsIsHDR(void) {
1028+
Texture* texture = NULL;
1029+
return lovrGraphicsGetWindowTexture(&texture) && gpu_surface_is_hdr();
1030+
}
1031+
10271032
void lovrGraphicsGetBackgroundColor(float background[4]) {
10281033
background[0] = lovrMathLinearToGamma(state.background[0]);
10291034
background[1] = lovrMathLinearToGamma(state.background[1]);
@@ -2262,6 +2267,7 @@ bool lovrGraphicsGetWindowTexture(Texture** texture) {
22622267
.width = width,
22632268
.height = height,
22642269
.vsync = vsync,
2270+
.hdr = state.config.hdr,
22652271
#if defined(_WIN32)
22662272
.win32.window = os_get_win32_window(),
22672273
.win32.instance = os_get_win32_instance()

src/modules/graphics/graphics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ typedef struct {
2525
bool vsync;
2626
bool stencil;
2727
bool antialias;
28+
bool hdr;
2829
void* cacheData;
2930
size_t cacheSize;
3031
} GraphicsConfig;
@@ -101,6 +102,8 @@ void lovrGraphicsGetLimits(GraphicsLimits* limits);
101102
uint32_t lovrGraphicsGetFormatSupport(uint32_t format, uint32_t features);
102103
void lovrGraphicsGetShaderCache(void* data, size_t* size);
103104

105+
bool lovrGraphicsIsHDR(void);
106+
104107
void lovrGraphicsGetBackgroundColor(float background[4]);
105108
void lovrGraphicsSetBackgroundColor(float background[4]);
106109

0 commit comments

Comments
 (0)