summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/render/vulkan/VULKAN_PixelShader_Common.hlsli
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/render/vulkan/VULKAN_PixelShader_Common.hlsli')
-rw-r--r--contrib/SDL-3.2.8/src/render/vulkan/VULKAN_PixelShader_Common.hlsli181
1 files changed, 181 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/render/vulkan/VULKAN_PixelShader_Common.hlsli b/contrib/SDL-3.2.8/src/render/vulkan/VULKAN_PixelShader_Common.hlsli
new file mode 100644
index 0000000..04ca409
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/render/vulkan/VULKAN_PixelShader_Common.hlsli
@@ -0,0 +1,181 @@
1SamplerState sampler0 : register(s0);
2Texture2D texture0 : register(t0);
3
4struct PixelShaderInput
5{
6 float4 pos : SV_POSITION;
7 float2 tex : TEXCOORD0;
8 float4 color : COLOR0;
9};
10
11// These should mirror the definitions in SDL_render_vulkan.c
12static const float TONEMAP_NONE = 0;
13static const float TONEMAP_LINEAR = 1;
14static const float TONEMAP_CHROME = 2;
15
16static const float INPUTTYPE_UNSPECIFIED = 0;
17static const float INPUTTYPE_SRGB = 1;
18static const float INPUTTYPE_SCRGB = 2;
19static const float INPUTTYPE_HDR10 = 3;
20
21cbuffer Constants : register(b1)
22{
23 float scRGB_output;
24 float input_type;
25 float color_scale;
26 float unused_pad0;
27
28 float tonemap_method;
29 float tonemap_factor1;
30 float tonemap_factor2;
31 float sdr_white_point;
32};
33
34static const float3x3 mat709to2020 = {
35 { 0.627404, 0.329283, 0.043313 },
36 { 0.069097, 0.919541, 0.011362 },
37 { 0.016391, 0.088013, 0.895595 }
38};
39
40static const float3x3 mat2020to709 = {
41 { 1.660496, -0.587656, -0.072840 },
42 { -0.124547, 1.132895, -0.008348 },
43 { -0.018154, -0.100597, 1.118751 }
44};
45
46float sRGBtoLinear(float v)
47{
48 if (v <= 0.04045) {
49 v = (v / 12.92);
50 } else {
51 v = pow(abs(v + 0.055) / 1.055, 2.4);
52 }
53 return v;
54}
55
56float sRGBfromLinear(float v)
57{
58 if (v <= 0.0031308) {
59 v = (v * 12.92);
60 } else {
61 v = (pow(abs(v), 1.0 / 2.4) * 1.055 - 0.055);
62 }
63 return v;
64}
65
66float3 PQtoLinear(float3 v)
67{
68 const float c1 = 0.8359375;
69 const float c2 = 18.8515625;
70 const float c3 = 18.6875;
71 const float oo_m1 = 1.0 / 0.1593017578125;
72 const float oo_m2 = 1.0 / 78.84375;
73
74 float3 num = max(pow(abs(v), oo_m2) - c1, 0.0);
75 float3 den = c2 - c3 * pow(abs(v), oo_m2);
76 return (10000.0 * pow(abs(num / den), oo_m1) / sdr_white_point);
77}
78
79float3 ApplyTonemap(float3 v)
80{
81 if (tonemap_method == TONEMAP_LINEAR) {
82 v *= tonemap_factor1;
83 } else if (tonemap_method == TONEMAP_CHROME) {
84 if (input_type == INPUTTYPE_SCRGB) {
85 // Convert to BT.2020 colorspace for tone mapping
86 v = mul(mat709to2020, v);
87 }
88
89 float vmax = max(v.r, max(v.g, v.b));
90 if (vmax > 0.0) {
91 float scale = (1.0 + tonemap_factor1 * vmax) / (1.0 + tonemap_factor2 * vmax);
92 v *= scale;
93 }
94
95 if (input_type == INPUTTYPE_SCRGB) {
96 // Convert to BT.709 colorspace after tone mapping
97 v = mul(mat2020to709, v);
98 }
99 }
100 return v;
101}
102
103float4 GetInputColor(PixelShaderInput input)
104{
105 float4 rgba;
106
107 rgba = texture0.Sample(sampler0, input.tex).rgba;
108
109 return rgba;
110}
111
112float4 GetOutputColor(float4 rgba)
113{
114 float4 output;
115
116 output.rgb = rgba.rgb * color_scale;
117 output.a = rgba.a;
118
119 return output;
120}
121
122float3 GetOutputColorFromSRGB(float3 rgb)
123{
124 float3 output;
125
126 if (scRGB_output) {
127 rgb.r = sRGBtoLinear(rgb.r);
128 rgb.g = sRGBtoLinear(rgb.g);
129 rgb.b = sRGBtoLinear(rgb.b);
130 }
131
132 output.rgb = rgb * color_scale;
133
134 return output;
135}
136
137float3 GetOutputColorFromLinear(float3 rgb)
138{
139 float3 output;
140
141 output.rgb = rgb * color_scale;
142
143 if (!scRGB_output) {
144 output.r = sRGBfromLinear(output.r);
145 output.g = sRGBfromLinear(output.g);
146 output.b = sRGBfromLinear(output.b);
147 output.rgb = saturate(output.rgb);
148 }
149
150 return output;
151}
152
153float4 AdvancedPixelShader(PixelShaderInput input)
154{
155 float4 rgba = GetInputColor(input);
156 float4 output;
157
158 if (input_type == INPUTTYPE_HDR10) {
159 rgba.rgb = PQtoLinear(rgba.rgb);
160 }
161
162 if (tonemap_method != TONEMAP_NONE) {
163 rgba.rgb = ApplyTonemap(rgba.rgb);
164 }
165
166 if (input_type == INPUTTYPE_SRGB) {
167 output.rgb = GetOutputColorFromSRGB(rgba.rgb);
168 output.a = rgba.a;
169 } else if (input_type == INPUTTYPE_SCRGB) {
170 output.rgb = GetOutputColorFromLinear(rgba.rgb);
171 output.a = rgba.a;
172 } else if (input_type == INPUTTYPE_HDR10) {
173 rgba.rgb = mul(mat2020to709, rgba.rgb);
174 output.rgb = GetOutputColorFromLinear(rgba.rgb);
175 output.a = rgba.a;
176 } else {
177 output = GetOutputColor(rgba);
178 }
179
180 return output * input.color;
181}