1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
// DYNAMIC: "MODE" "0..9"
// STATIC: "CONVERT_TO_SRGB" "0..1" [ps20b][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
// STATIC: "CONVERT_TO_SRGB" "0..0" [= 0] [XBOX]
// STATIC: "LINEAR_TO_SRGB" "0..1" [ps20b]
#define HDRTYPE HDR_TYPE_NONE
#include "common_ps_fxc.h"
const float g_Alpha : register( c0 );
sampler BaseTextureSampler : register( s0 );
sampler BaseTextureSampler2 : register( s1 );
struct PS_INPUT
{
float2 baseTexCoord : TEXCOORD0;
};
float3 RGBtoHSV( in float3 rgb )
{
float3 hsv;
float fmin, fmax, delta;
fmin = min( min( rgb.r, rgb.g ), rgb.b );
fmax = max( max( rgb.r, rgb.g) , rgb.b );
hsv.b = fmax; // v
delta = fmax - fmin;
if( delta != 0 )
{
hsv.g = delta / fmax; // s
if( rgb.r == fmax )
hsv.r = ( rgb.g - rgb.b ) / delta; // between yellow & magenta
else if( rgb.g == fmax )
hsv.r = 2 + ( rgb.b - rgb.r ) / delta; // between cyan & yellow
else
hsv.r = 4 + ( rgb.r - rgb.g ) / delta; // between magenta & cyan
hsv.r *= 60; // degrees
if( hsv.r < 0 )
hsv.r += 360;
}
else
{
// r = g = b = 0 // s = 0, v is undefined
hsv.g = 0;
hsv.r = -1;
}
return hsv;
}
float3 HSVtoRGB( in float3 hsv )
{
int i;
float3 rgb;
float h = hsv.r;
float s = hsv.g;
float v = hsv.b;
float f, p, q, t;
if( s == 0 )
{
// achromatic (grey)
rgb.rgb = v;
}
else
{
h /= 60; // sector 0 to 5
i = floor( h );
f = h - i; // factorial part of h
p = v * ( 1 - s );
q = v * ( 1 - s * f );
t = v * ( 1 - s * ( 1 - f ) );
if( h < 1)
{
rgb.r = v;
rgb.g = t;
rgb.b = p;
}
else if( h >= 1 && h < 2 )
{
rgb.r = q;
rgb.g = v;
rgb.b = p;
}
else if( h >= 2 && h < 3 )
{
rgb.r = p;
rgb.g = v;
rgb.b = t;
}
else if( h >= 3 && h < 4 )
{
rgb.r = p;
rgb.g = q;
rgb.b = v;
}
else if( h >= 4 && h < 5 )
{
rgb.r = t;
rgb.g = p;
rgb.b = v;
}
else // if ( h >= 5 )
{
rgb.r = v;
rgb.g = p;
rgb.b = q;
}
}
return rgb;
}
// We have to run through this input converter on OpenGL if the
// rest of the shader code is expecting sRGB values
float3 SampleTexture( sampler texSampler, float2 tc )
{
float3 c = tex2D( texSampler, tc ).xyz;
#if ( LINEAR_TO_SRGB )
{
c = LinearToGamma( c );
}
#endif
return c;
}
// We have to run through this output converter on OpenGL if we
// expect to be writing out sRGB values (since sRGB will be forced on)
float3 OutputColor( float3 result )
{
#if ( LINEAR_TO_SRGB )
{
return GammaToLinear( result );
}
#endif
return result;
}
float4 main( PS_INPUT i ) : COLOR
{
float3 result;
#if MODE == 0
// negative greyscale of scene * gman
float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
float scale = 1.0f / 3.0f;
scene.xyz = dot( float3( scale, scale, scale), scene.xyz );
scene = float3( 1, 1, 1 ) - scene;
return FinalOutput( float4( OutputColor( scene * gman ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
#endif
#if MODE == 1
float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
float scale = 1.0f / 3.0f;
scene.xyz = dot( float3( scale, scale, scale ), scene.xyz );
float gmanLum = dot( float3( scale, scale, scale ), gman );
if( gmanLum < 0.3 )
{
result = OutputColor( float3( 1, 1, 1 ) - gman );
return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
}
else
{
result = OutputColor( ( float3( 1, 1, 1 ) - gman ) * scene );
return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
}
#endif
#if MODE == 2
float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
float startRamp = .2;
float endRamp = .5;
float scale = 1.0f / 3.0f;
float gmanLum = dot( float3( scale, scale, scale ), gman );
float sceneLum = dot( float3( scale, scale, scale ), scene );
float blend = ( gmanLum - startRamp ) * ( 1.0f / ( endRamp - startRamp ) );
blend = saturate( blend );
// return gmanLum * ( 1.0f - blend ) + scene * blend;
result = OutputColor( min( gmanLum.xxx, scene ) );
return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
#endif
#if MODE == 3
float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
float scale = 1.0f / 3.0f;
float gmanLum = dot( float3( scale, scale, scale ), gman );
float sceneLum = dot( float3( scale, scale, scale ), scene );
float a = 0.0f;
float b = 0.4f;
float c = 0.7f;
float d = 1.0f;
float blend;
if( gmanLum < b )
{
blend = ( gmanLum - a ) / ( b - a );
}
else if( gmanLum > c )
{
blend = 1.0f - ( ( gmanLum - c) / ( d - c ) );
}
else
{
blend = 1.0f;
}
blend = saturate( blend );
result = OutputColor( gmanLum.xxx * ( float3( 1, 1, 1 ) - blend.xxx ) + scene * blend.xxx );
return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
#endif
#if MODE == 4
float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
float scale = 1.0f / 3.0f;
float gmanLum = dot( float3( scale, scale, scale ), gman );
float sceneLum = dot( float3( scale, scale, scale ), scene );
float a = 0.0f;
float b = 0.4f;
float c = 0.7f;
float d = 1.0f;
float blend;
if( gmanLum < b )
{
blend = ( gmanLum - a ) / ( b - a );
}
else if( gmanLum > c )
{
blend = 1.0f - ( ( gmanLum - c) / ( d - c ) );
}
else
{
blend = 1.0f;
}
blend = saturate( blend );
result = OutputColor( gman * ( float3( 1, 1, 1 ) - blend.xxx ) + scene * blend.xxx );
return FinalOutput( float4( result, g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
#endif
#if MODE == 5
float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
float scale = 1.0f / 3.0f;
// float sceneLum = dot( float3( scale, scale, scale ), scene );
float sceneLum = scene.r;
if( sceneLum > 0.0f )
{
return FinalOutput( float4( OutputColor( scene ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
}
else
{
float3 hsv = RGBtoHSV( gman );
// float blend = saturate( hsv.b - .5 );
float blend = hsv.b - .5;
hsv.b *= 1.0f + blend;
hsv.g *= 1.0f - blend;
return FinalOutput( float4( OutputColor( HSVtoRGB( hsv ) ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
}
#endif
#if MODE == 6
float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
return FinalOutput( float4( OutputColor( scene + gman ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
#endif
#if MODE == 7
float3 scene = SampleTexture( BaseTextureSampler, i.baseTexCoord );
return FinalOutput( float4( OutputColor( scene ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
#endif
#if MODE == 8
float3 gman = SampleTexture( BaseTextureSampler2, i.baseTexCoord );
return FinalOutput( float4( OutputColor( gman ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
#endif
#if MODE == 9
// Fetch textures
float3 cLayer1 = SampleTexture( BaseTextureSampler, i.baseTexCoord.xy );
float3 cLayer2 = SampleTexture( BaseTextureSampler2, i.baseTexCoord.xy );
/*
// Put colors roughly back into gamma space
float3 cGammaLayer1 = pow( cLayer1, 0.454545f );
float3 cGammaLayer2 = pow( cLayer2, 0.454545f );
// Brightness
//float flLayer1Brightness = saturate( dot( cGammaLayer1.rgb, float3( 0.3f, 0.59f, 0.11f ) ) );
//float flLayer2Brightness = saturate( dot( cGammaLayer2.rgb, float3( 0.3f, 0.59f, 0.11f ) ) );
float flLayer1Brightness = saturate( dot( cGammaLayer1.rgb, float3( 0.333f, 0.334f, 0.333f ) ) );
float flLayer2Brightness = saturate( dot( cGammaLayer2.rgb, float3( 0.333f, 0.334f, 0.333f ) ) );
// Blend layers in rough gamma space
float3 cGammaOverlayResult;
if ( flLayer1Brightness < 0.5f )
{
cGammaOverlayResult.rgb = ( 2.0f * cGammaLayer1.rgb * cGammaLayer2.rgb );
}
else
{
cGammaOverlayResult.rgb = ( 1.0f - ( 2.0f * ( 1.0f - cGammaLayer1.rgb ) * ( 1.0f - cGammaLayer2.rgb ) ) );
}
// Convert back to linear space
float3 cLinearOverlayResult = pow( cGammaOverlayResult.rgb, 2.2f );
//*/
float flLayer1Brightness = saturate( dot( cLayer1.rgb, float3( 0.333f, 0.334f, 0.333f ) ) );
float flLayer2Brightness = saturate( dot( cLayer2.rgb, float3( 0.333f, 0.334f, 0.333f ) ) );
// Modify layer 1 to be more contrasty
cLayer1.rgb = saturate( cLayer1.rgb * cLayer1.rgb * 2.0f );
float3 cLinearOverlayResult = cLayer1.rgb + cLayer2.rgb * saturate( 1.0f - flLayer1Brightness * 2.0f );
// Tonemap, fog, etc.
return FinalOutput( float4( OutputColor( cLinearOverlayResult.rgb ), g_Alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
#endif
}
|