Skip to content

Commit b5a54b7

Browse files
anikom15rb6502
authored andcommitted
HLSL Color Transforms and 3D LUT (#4043)
* Remove broken scanline uniform from post_pass * Add 3D LUT to HLSL * Allow individual LUTs for screen and UI * WIP: Port 3D LUT to BGFX * Finish porting LUT to BGFX * Add individual phosphor color conversion for HLSL new file: hlsl/chroma.fx Shader for converting xyY3 to sRGB modified: hlsl/phosphor.fx Minor changes to emphasize idea that phosphors are color agnostic modified: hlsl/post.fx Conversion from signal RGB to xyY3 modified: src/osd/modules/render/d3d/d3dhlsl.cpp modified: src/osd/modules/render/d3d/d3dhlsl.h modified: src/osd/windows/winmain.cpp modified: src/osd/windows/winmain.h * Add phosphor examples and update presets * Port phosphor color shaders to BGFX * Fix missing newlines at EOF
1 parent 7b42e2f commit b5a54b7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1026
-172
lines changed

bgfx/chains/hlsl.json

+123-60
Large diffs are not rendered by default.

bgfx/chains/lut.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "3D LUT",
3+
"author": "W. M. Martinez",
4+
"targets": [
5+
{
6+
"name": "temp",
7+
"mode": "guest"
8+
}
9+
],
10+
"passes": [
11+
{
12+
"effect": "misc/blit",
13+
"name": "Copy To Filtered Texture",
14+
"input": [
15+
{ "sampler": "s_tex", "texture": "screen" }
16+
],
17+
"output": "temp"
18+
}, {
19+
"effect": "misc/lut",
20+
"name": "Apply LUT",
21+
"input": [
22+
{
23+
"sampler": "s_tex",
24+
"target": "temp"
25+
}, {
26+
"sampler": "s_3dlut",
27+
"option": "bgfx_lut",
28+
"bilinear": false,
29+
"clamp": true,
30+
"selection": "LUT Texture"
31+
}
32+
],
33+
"output": "output"
34+
}
35+
]
36+
}

bgfx/effects/hlsl/chroma.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// license:BSD-3-Clause
2+
// copyright-holders:W. M. Martinez
3+
//============================================================
4+
//
5+
// chroma.json: Phosphor chromaticity conversion
6+
//
7+
//============================================================
8+
{
9+
"blend": {
10+
"equation": "add",
11+
"srcColor": "srcalpha",
12+
"dstColor": "1-srcalpha",
13+
"srcAlpha": "srcalpha",
14+
"dstAlpha": "1-srcalpha"
15+
},
16+
"depth": {
17+
"function": "always"
18+
},
19+
"cull": { "mode": "none" },
20+
"write": {
21+
"rgb": true,
22+
"alpha": true
23+
},
24+
"vertex": "chains/hlsl/vs_chroma",
25+
"fragment": "chains/hlsl/fs_chroma",
26+
"uniforms": [
27+
{ "name": "s_tex", "type": "int", "values": [ 0.0 ] },
28+
{ "name": "u_y_gain", "type": "vec4", "values": [ 0.2124, 0.7011, 0.0866, 0.0 ] },
29+
{ "name": "u_chroma_a", "type": "vec4", "values": [ 0.630, 0.340, 0.0, 0.0 ] },
30+
{ "name": "u_chroma_b", "type": "vec4", "values": [ 0.310, 0.595, 0.0, 0.0 ] },
31+
{ "name": "u_chroma_c", "type": "vec4", "values": [ 0.155, 0.070, 0.0, 0.0 ] }
32+
]
33+
}

bgfx/effects/hlsl/post.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
{ "name": "u_shadow_uv", "type": "vec4", "values": [ 0.25, 0.25, 0.0, 0.0 ] },
4343
{ "name": "u_shadow_uv_offset", "type": "vec4", "values": [ 0.0, 0.0, 0.0, 0.0 ] },
4444
{ "name": "u_power", "type": "vec4", "values": [ 1.0, 1.0, 1.0, 0.0 ] },
45-
{ "name": "u_floor", "type": "vec4", "values": [ 0.0, 0.0, 0.0, 0.0 ] }
45+
{ "name": "u_floor", "type": "vec4", "values": [ 0.0, 0.0, 0.0, 0.0 ] },
46+
{ "name": "u_chroma_mode", "type": "vec4", "values": [ 3.0, 0.0, 0.0, 0.0 ] },
47+
{ "name": "u_conversion_gain", "type": "vec4", "values": [ 0.299, 0.587, 0.114, 0.0 ] }
4648
]
47-
}
49+
}

bgfx/effects/misc/lut.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"blend": {
3+
"equation": "add",
4+
"srcColor": "srcalpha",
5+
"dstColor": "1-srcalpha",
6+
"srcAlpha": "srcalpha",
7+
"dstAlpha": "1-srcalpha"
8+
},
9+
"depth": {
10+
"function": "always"
11+
},
12+
"cull": {
13+
"mode": "none"
14+
},
15+
"write": {
16+
"rgb": true,
17+
"alpha": true
18+
},
19+
"vertex": "chains/misc/vs_lut",
20+
"fragment": "chains/misc/fs_lut",
21+
"uniforms": [
22+
{
23+
"name": "s_tex",
24+
"type": "int",
25+
"values": [ 1.0 ]
26+
}, {
27+
"name": "s_3dlut",
28+
"type": "int",
29+
"values": [ 1.0 ]
30+
}
31+
]
32+
}
1.52 KB
Binary file not shown.
224 Bytes
Binary file not shown.
577 Bytes
Binary file not shown.
756 Bytes
Binary file not shown.
577 Bytes
Binary file not shown.
1 KB
Binary file not shown.
256 Bytes
Binary file not shown.
320 Bytes
Binary file not shown.
501 Bytes
Binary file not shown.
320 Bytes
Binary file not shown.
1.65 KB
Binary file not shown.
471 Bytes
Binary file not shown.
419 Bytes
Binary file not shown.
828 Bytes
Binary file not shown.
419 Bytes
Binary file not shown.
1.57 KB
Binary file not shown.
459 Bytes
Binary file not shown.
377 Bytes
Binary file not shown.
786 Bytes
Binary file not shown.
377 Bytes
Binary file not shown.
2.19 KB
Binary file not shown.
479 Bytes
Binary file not shown.
778 Bytes
Binary file not shown.
1.27 KB
Binary file not shown.
778 Bytes
Binary file not shown.

hlsl/chroma.fx

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// license:BSD-3-Clause
2+
// copyright-holders:W. M. Martinez
3+
//-----------------------------------------------------------------------------
4+
// Phosphor Chromaticity to sRGB Transform Effect
5+
//-----------------------------------------------------------------------------
6+
7+
//-----------------------------------------------------------------------------
8+
// Sampler Definitions
9+
//-----------------------------------------------------------------------------
10+
11+
texture Diffuse;
12+
13+
sampler DiffuseSampler = sampler_state
14+
{
15+
Texture = <Diffuse>;
16+
MipFilter = LINEAR;
17+
MinFilter = LINEAR;
18+
MagFilter = LINEAR;
19+
AddressU = CLAMP;
20+
AddressV = CLAMP;
21+
AddressW = CLAMP;
22+
};
23+
24+
//-----------------------------------------------------------------------------
25+
// Vertex Definitions
26+
//-----------------------------------------------------------------------------
27+
28+
struct VS_OUTPUT
29+
{
30+
float4 Position : POSITION;
31+
float4 Color : COLOR0;
32+
float2 TexCoord : TEXCOORD0;
33+
float2 PrevCoord : TEXCOORD1;
34+
};
35+
36+
struct VS_INPUT
37+
{
38+
float3 Position : POSITION;
39+
float4 Color : COLOR0;
40+
float2 TexCoord : TEXCOORD0;
41+
};
42+
43+
struct PS_INPUT
44+
{
45+
float4 Color : COLOR0;
46+
float2 TexCoord : TEXCOORD0;
47+
float2 PrevCoord : TEXCOORD1;
48+
};
49+
50+
//-----------------------------------------------------------------------------
51+
// Chroma Vertex Shader
52+
//-----------------------------------------------------------------------------
53+
54+
uniform float2 ScreenDims;
55+
uniform float2 TargetDims;
56+
57+
uniform bool Passthrough;
58+
59+
VS_OUTPUT vs_main(VS_INPUT Input)
60+
{
61+
VS_OUTPUT Output = (VS_OUTPUT)0.0;
62+
63+
Output.Position = float4(Input.Position.xyz, 1.0);
64+
Output.Position.xy /= ScreenDims;
65+
Output.Position.y = 1.0 - Output.Position.y; // flip y
66+
Output.Position.xy -= 0.5; // center
67+
Output.Position.xy *= 2.0; // zoom
68+
69+
Output.TexCoord = Input.TexCoord;
70+
Output.TexCoord += 0.5 / TargetDims; // half texel offset correction (DX9)
71+
72+
Output.PrevCoord = Output.TexCoord;
73+
74+
Output.Color = Input.Color;
75+
76+
return Output;
77+
}
78+
79+
//-----------------------------------------------------------------------------
80+
// Chroma Pixel Shader
81+
//-----------------------------------------------------------------------------
82+
83+
uniform float3 YGain = float3(0.2126, 0.7152, 0.0722);
84+
uniform float2 ChromaA = float2(0.630, 0.340);
85+
uniform float2 ChromaB = float2(0.310, 0.595);
86+
uniform float2 ChromaC = float2(0.155, 0.070);
87+
88+
static const float3x3 XYZ_TO_sRGB = {
89+
3.2406, -1.5372, -0.4986,
90+
-0.9689, 1.8758, 0.0415,
91+
0.0557, -0.2040, 1.0570
92+
};
93+
94+
float4 ps_main(PS_INPUT Input) : COLOR
95+
{
96+
const float4 cin = tex2D(DiffuseSampler, Input.TexCoord);
97+
float4 cout = float4(0.0, 0.0, 0.0, cin.a);
98+
const float3x2 xy = { ChromaA, ChromaB, ChromaC };
99+
100+
for (int i = 0; i < 3; ++i) {
101+
const float Y = YGain[i] * cin[i];
102+
const float X = xy[i].x * (Y / xy[i].y);
103+
const float Z = (1.0 - xy[i].x - xy[i].y) * (Y / xy[i].y);
104+
cout.rgb += mul(XYZ_TO_sRGB, float3(X, Y, Z));
105+
}
106+
return cout;
107+
}
108+
109+
//-----------------------------------------------------------------------------
110+
// Phosphor Technique
111+
//-----------------------------------------------------------------------------
112+
113+
technique DefaultTechnique
114+
{
115+
pass Pass0
116+
{
117+
Lighting = FALSE;
118+
119+
VertexShader = compile vs_2_0 vs_main();
120+
PixelShader = compile ps_2_0 ps_main();
121+
}
122+
}

hlsl/phosphor.fx

+20-21
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ uniform bool Passthrough;
7171

7272
VS_OUTPUT vs_main(VS_INPUT Input)
7373
{
74-
VS_OUTPUT Output = (VS_OUTPUT)0;
74+
VS_OUTPUT Output = (VS_OUTPUT)0.0;
7575

76-
Output.Position = float4(Input.Position.xyz, 1.0f);
76+
Output.Position = float4(Input.Position.xyz, 1.0);
7777
Output.Position.xy /= ScreenDims;
78-
Output.Position.y = 1.0f - Output.Position.y; // flip y
79-
Output.Position.xy -= 0.5f; // center
80-
Output.Position.xy *= 2.0f; // zoom
78+
Output.Position.y = 1.0 - Output.Position.y; // flip y
79+
Output.Position.xy -= 0.5; // center
80+
Output.Position.xy *= 2.0; // zoom
8181

8282
Output.TexCoord = Input.TexCoord;
83-
Output.TexCoord += 0.5f / TargetDims; // half texel offset correction (DX9)
83+
Output.TexCoord += 0.5 / TargetDims; // half texel offset correction (DX9)
8484

8585
Output.PrevCoord = Output.TexCoord;
8686

@@ -93,24 +93,23 @@ VS_OUTPUT vs_main(VS_INPUT Input)
9393
// Phosphor Pixel Shader
9494
//-----------------------------------------------------------------------------
9595

96-
uniform float3 Phosphor = float3(0.0f, 0.0f, 0.0f);
97-
uniform float DeltaTime = 0.0f;
98-
static const float F = 30.0f;
96+
uniform float DeltaTime = 0.0;
97+
uniform float3 Phosphor = float3(0.0, 0.0, 0.0);
98+
99+
static const float F = 30.0;
99100

100101
float4 ps_main(PS_INPUT Input) : COLOR
101102
{
102-
float4 CurrPix = tex2D(DiffuseSampler, Input.TexCoord);
103-
float3 PrevPix = tex2D(PreviousSampler, Input.PrevCoord).rgb;
104-
105-
PrevPix.r *= Phosphor.r == 0 ? 0 : pow(Phosphor.r, F * DeltaTime);
106-
PrevPix.g *= Phosphor.g == 0 ? 0 : pow(Phosphor.g, F * DeltaTime);
107-
PrevPix.b *= Phosphor.b == 0 ? 0 : pow(Phosphor.b, F * DeltaTime);
108-
float RedMax = max(CurrPix.r, PrevPix.r);
109-
float GreenMax = max(CurrPix.g, PrevPix.g);
110-
float BlueMax = max(CurrPix.b, PrevPix.b);
111-
112-
return Passthrough ?
113-
CurrPix : float4(RedMax, GreenMax, BlueMax, CurrPix.a);
103+
float4 CurrY = tex2D(DiffuseSampler, Input.TexCoord);
104+
float3 PrevY = tex2D(PreviousSampler, Input.PrevCoord).rgb;
105+
106+
PrevY[0] *= Phosphor[0] == 0.0 ? 0.0 : pow(Phosphor[0], F * DeltaTime);
107+
PrevY[1] *= Phosphor[1] == 0.0 ? 0.0 : pow(Phosphor[1], F * DeltaTime);
108+
PrevY[2] *= Phosphor[2] == 0.0 ? 0.0 : pow(Phosphor[2], F * DeltaTime);
109+
float a = max(PrevY[0], CurrY[0]);
110+
float b = max(PrevY[1], CurrY[1]);
111+
float c = max(PrevY[2], CurrY[2]);
112+
return Passthrough ? CurrY : float4(a, b, c, CurrY.a);
114113
}
115114

116115
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)