/
800.PixelShader_piece_ps.any
403 lines (333 loc) · 16.5 KB
/
800.PixelShader_piece_ps.any
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "/media/matias/Datos/SyntaxHighlightingMisc.h"
@undefpiece( MaterialStructDecl )
@undefpiece( InstanceStructDecl )
@piece( DefaultTerraHeaderPS )
@insertpiece( DefaultHeaderPS )
@property( !hlms_shadowcaster )
@insertpiece( TerraMaterialStructDecl )
@insertpiece( TerraInstanceStructDecl )
@else
@insertpiece( DeclShadowCasterMacros )
@property( hlms_shadowcaster_point || exponential_shadow_maps )
@insertpiece( PassStructDecl )
@end
@end
@property( !hlms_prepass && needs_view_dir )
@insertpiece( DeclareBRDF )
@insertpiece( DeclareBRDF_InstantRadiosity )
@insertpiece( DeclareBRDF_AreaLightApprox )
@end
#define SampleMetalness0( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2D( tex, sampler, uv, arrayIdx )
#define SampleMetalness1( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2D( tex, sampler, uv, arrayIdx )
#define SampleMetalness2( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2D( tex, sampler, uv, arrayIdx )
#define SampleMetalness3( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2D( tex, sampler, uv, arrayIdx )
#define SampleRoughness0( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2D( tex, sampler, uv, arrayIdx )
#define SampleRoughness1( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2D( tex, sampler, uv, arrayIdx )
#define SampleRoughness2( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2D( tex, sampler, uv, arrayIdx )
#define SampleRoughness3( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2D( tex, sampler, uv, arrayIdx )
#define OGRE_UNPACK_X_2x16( packedInt ) ((packedInt) & 0x0000FFFFu)
#define OGRE_UNPACK_Y_2x16( packedInt ) ((packedInt) >> 16u)
@end
//-----------------------------------------------------------------------------
// BODY CODE
//-----------------------------------------------------------------------------
@undefpiece( UnpackTextureIndices0 )
@piece( UnpackTextureIndices0 )
@property( syntax == metal )
@property( diffuse_map ) ushort texIndex_diffuseIdx = material.diffuseIdx;@end
@property( detail_weight_map ) ushort texIndex_weightMapIdx = material.weightMapIdx;@end
@property( detail_map0 ) ushort texIndex_detailMapIdx0 = material.detailMapIdx0;@end
@property( detail_map1 ) ushort texIndex_detailMapIdx1 = material.detailMapIdx1;@end
@property( detail_map2 ) ushort texIndex_detailMapIdx2 = material.detailMapIdx2;@end
@property( detail_map3 ) ushort texIndex_detailMapIdx3 = material.detailMapIdx3;@end
@property( detail_map_nm0 ) ushort texIndex_detailNormMapIdx0 = material.detailNormMapIdx0;@end
@property( detail_map_nm1 ) ushort texIndex_detailNormMapIdx1 = material.detailNormMapIdx1;@end
@property( detail_map_nm2 ) ushort texIndex_detailNormMapIdx2 = material.detailNormMapIdx2;@end
@property( detail_map_nm3 ) ushort texIndex_detailNormMapIdx3 = material.detailNormMapIdx3;@end
@property( roughness_map0 ) ushort texIndex_detailRoughnessIdx0 = material.detailRoughnessIdx0;@end
@property( roughness_map1 ) ushort texIndex_detailRoughnessIdx1 = material.detailRoughnessIdx1;@end
@property( roughness_map2 ) ushort texIndex_detailRoughnessIdx2 = material.detailRoughnessIdx2;@end
@property( roughness_map3 ) ushort texIndex_detailRoughnessIdx3 = material.detailRoughnessIdx3;@end
@property( metalness_map0 ) ushort texIndex_detailMetalnessIdx0 = material.detailMetalnessIdx0;@end
@property( metalness_map1 ) ushort texIndex_detailMetalnessIdx1 = material.detailMetalnessIdx1;@end
@property( metalness_map2 ) ushort texIndex_detailMetalnessIdx2 = material.detailMetalnessIdx2;@end
@property( metalness_map3 ) ushort texIndex_detailMetalnessIdx3 = material.detailMetalnessIdx3;@end
@else
@property( diffuse_map ) ushort texIndex_diffuseIdx = OGRE_UNPACK_X_2x16( material.indices0_7.x );@end
@property( detail_weight_map ) ushort texIndex_weightMapIdx = OGRE_UNPACK_Y_2x16( material.indices0_7.x );@end
@property( detail_map0 ) ushort texIndex_detailMapIdx0 = OGRE_UNPACK_X_2x16( material.indices0_7.y );@end
@property( detail_map1 ) ushort texIndex_detailMapIdx1 = OGRE_UNPACK_Y_2x16( material.indices0_7.y );@end
@property( detail_map2 ) ushort texIndex_detailMapIdx2 = OGRE_UNPACK_X_2x16( material.indices0_7.z );@end
@property( detail_map3 ) ushort texIndex_detailMapIdx3 = OGRE_UNPACK_Y_2x16( material.indices0_7.z );@end
@property( detail_map_nm0 ) ushort texIndex_detailNormMapIdx0 = OGRE_UNPACK_X_2x16( material.indices0_7.w );@end
@property( detail_map_nm1 ) ushort texIndex_detailNormMapIdx1 = OGRE_UNPACK_Y_2x16( material.indices0_7.w );@end
@property( detail_map_nm2 ) ushort texIndex_detailNormMapIdx2 = OGRE_UNPACK_X_2x16( material.indices8_15.x );@end
@property( detail_map_nm3 ) ushort texIndex_detailNormMapIdx3 = OGRE_UNPACK_Y_2x16( material.indices8_15.x );@end
@property( roughness_map0 ) ushort texIndex_detailRoughnessIdx0 = OGRE_UNPACK_X_2x16( material.indices8_15.y );@end
@property( roughness_map1 ) ushort texIndex_detailRoughnessIdx1 = OGRE_UNPACK_Y_2x16( material.indices8_15.y );@end
@property( roughness_map2 ) ushort texIndex_detailRoughnessIdx2 = OGRE_UNPACK_X_2x16( material.indices8_15.z );@end
@property( roughness_map3 ) ushort texIndex_detailRoughnessIdx3 = OGRE_UNPACK_Y_2x16( material.indices8_15.z );@end
@property( metalness_map0 ) ushort texIndex_detailMetalnessIdx0 = OGRE_UNPACK_X_2x16( material.indices8_15.w );@end
@property( metalness_map1 ) ushort texIndex_detailMetalnessIdx1 = OGRE_UNPACK_Y_2x16( material.indices8_15.w );@end
@property( metalness_map2 ) ushort texIndex_detailMetalnessIdx2 = OGRE_UNPACK_X_2x16( material.indices16_24.x );@end
@property( metalness_map3 ) ushort texIndex_detailMetalnessIdx3 = OGRE_UNPACK_Y_2x16( material.indices16_24.x );@end
@end
@end
@undefpiece( UnpackTextureIndices1 )
@undefpiece( SampleDiffuseMap )
@piece( SampleDiffuseMap )
/// DIFFUSE MAP
@property( diffuse_map )
pixelData.diffuse = SampleDiffuse( textureMaps@value( diffuse_map_idx ),
samplerState@value(diffuse_map_sampler),
UV_DIFFUSE( inPs.uv@value(uv_diffuse).xy ),
texIndex_diffuseIdx );
@else
/// If there are no diffuse maps, we must initialize it to some value.
pixelData.diffuse.xyzw = float4( 1, 1, 1, 1 );
@end
@foreach( detail_maps_diffuse, n )
@property( !detail_map@n )float3 detailCol@n = float3( 0.0f, 0.0f, 0.0f );@end
@end
@property( !detail_maps_diffuse && !detail_maps_normal )
float4 detailWeights = float4( 0.25f, 0.25f, 0.25f, 0.25f );
@else
pixelData.diffuse.xyz *= (detailCol0.xyz * detailWeights.x + detailCol1.xyz * detailWeights.y) +
(detailCol2.xyz * detailWeights.z + detailCol3.xyz * detailWeights.w);
@end
/// Apply the material's diffuse over the textures
pixelData.diffuse.xyz *= material.kD.xyz;
@end
@undefpiece( SampleSpecularMap )
@piece( SampleSpecularMap )
/// SPECUlAR MAP
@foreach( 4, n )
@property( metalness_map@n )
float metalness@n = SampleMetalness@n( textureMaps@value( metalness_map@n_idx ),
samplerState@value(metalness_map@n_sampler),
inPs.uv0.xy * material.detailOffsetScale[@n].zw +
material.detailOffsetScale[@n].xy,
texIndex_detailMetalnessIdx@n ).x;
@else
float metalness@n = 0;
@end
@end
pixelData.specular.xyz = float3( 1.0f, 1.0f, 1.0f );
float metalness = (metalness0 * detailWeights.x * material.metalness.x +
metalness1 * detailWeights.y * material.metalness.y) +
(metalness2 * detailWeights.z * material.metalness.z +
metalness3 * detailWeights.w * material.metalness.w);
pixelData.F0 = lerp( float3( 0.03f, 0.03f, 0.03f ), pixelData.diffuse.xyz * 3.14159f, metalness );
pixelData.diffuse.xyz = pixelData.diffuse.xyz - pixelData.diffuse.xyz * metalness;
@end
@undefpiece( SampleRoughnessMap )
@piece( SampleRoughnessMap )
/// ROUGHNESS MAP
@foreach( 4, n )
@property( roughness_map@n )
float roughness@n = SampleRoughness@n( textureMaps@value( roughness_map@n_idx ),
samplerState@value(roughness_map@n_sampler),
inPs.uv0.xy * material.detailOffsetScale[@n].zw +
material.detailOffsetScale[@n].xy,
texIndex_detailRoughnessIdx@n ).x;
@else
float roughness@n = 0;
@end
@end
pixelData.perceptualRoughness = (roughness0 * detailWeights.x * material.roughness.x +
roughness1 * detailWeights.y * material.roughness.y) +
(roughness2 * detailWeights.z * material.roughness.z +
roughness3 * detailWeights.w * material.roughness.w);
@property( perceptual_roughness )
pixelData.roughness = max( pixelData.perceptualRoughness * pixelData.perceptualRoughness, 0.001f );
@else
pixelData.roughness = max( pixelData.perceptualRoughness, 0.001f );
@end
@end
@undefpiece( LoadNormalData )
@piece( LoadNormalData )
// Geometric normal
pixelData.geomNormal = OGRE_Sample( terrainNormals, samplerStateTerra, inPs.uv0.xy ).xyz * 2.0 - 1.0;
@property( z_up )
pixelData.geomNormal.yz = float2( -pixelData.geomNormal.z, pixelData.geomNormal.y );
@end
pixelData.geomNormal = mul( pixelData.geomNormal, toFloat3x3( passBuf.view ) );
@property( normal_map )
//Get the TBN matrix
float3 viewSpaceUnitX = mul( float3( 1, 0, 0 ) , toFloat3x3( passBuf.view ) );
float3 vTangent = normalize( cross( pixelData.geomNormal, viewSpaceUnitX ) );
float3 vBinormal = cross( vTangent, pixelData.geomNormal );
float3x3 TBN = buildFloat3x3( vBinormal, vTangent, pixelData.geomNormal );
@end
@end
@piece( DefaultTerraBodyPS )
PixelData pixelData;
@insertpiece( LoadMaterial )
@insertpiece( UnpackTextureIndices0 )
@insertpiece( UnpackTextureIndices1 )
@insertpiece( DeclareObjLightMask )
@insertpiece( custom_ps_posMaterialLoad )
@insertpiece( LoadDetailWeights )
@insertpiece( SampleDetailMaps )
@property( !hlms_prepass )
@insertpiece( SampleDiffuseMap )
@end
@insertpiece( SampleSpecularMap )
@insertpiece( SampleRoughnessMap )
@insertpiece( forwardPlusDoDecals )
@property( !hlms_use_prepass )
@insertpiece( LoadNormalData )
@insertpiece( SampleAndApplyDetailNormalMaps )
@insertpiece( custom_ps_posSampleNormal )
@insertpiece( forwardPlusApplyDecalsNormal )
@property( normal_map )
pixelData.normal = normalize( mul( TBN, pixelData.normal ) );
@end
@insertpiece( DoDirectionalShadowMaps )
@end @property( hlms_use_prepass )
rshort2 iFragCoord = rshort2( gl_FragCoord.x,
@property( !hlms_forwardplus_flipY && syntax == glsl )passBuf.windowHeight.x - @end
gl_FragCoord.y );
@property( hlms_use_prepass_msaa )
//SV_Coverage/gl_SampleMaskIn is always before depth & stencil tests,
//so we need to perform the test ourselves
//See http://www.yosoygames.com.ar/wp/2017/02/beware-of-sv_coverage/
uint sampleMask = uint( gl_SampleMaskIn0 );
float msaaDepth;
uint subsampleDepthMask;
float pixelDepthZ;
float pixelDepthW;
float2 pixelDepthZW;
float pixelDepth;
int intPixelDepth;
int intMsaaDepth;
//Unfortunately there are precision errors, so we allow some ulp errors.
//200 & 5 are arbitrary, but were empirically found to be very good values.
int ulpError = int( lerp( 200.0, 5.0, gl_FragCoord.z ) );
@foreach( hlms_use_prepass_msaa, n )
pixelDepthZW = interpolateAtSample( inPs.zwDepth, @n );
pixelDepthZ = pixelDepthZW.x;
pixelDepthW = pixelDepthZW.y;
pixelDepth = pixelDepthZ / pixelDepthW;
msaaDepth = OGRE_Load2DMS( gBuf_depthTexture, iFragCoord.xy, @n ).x;
intPixelDepth = floatBitsToInt( pixelDepth );
intMsaaDepth = floatBitsToInt( msaaDepth );
subsampleDepthMask = (abs( intPixelDepth - intMsaaDepth ) <= ulpError) ? 0xffffffffu : ~(1u << @nu);
//subsampleDepthMask = int( (pixelDepth <= msaaDepth) ? 0xffffffffu : ~(1u << @nu) );
sampleMask &= subsampleDepthMask;
@end
sampleMask = sampleMask == 0u ? 1u : sampleMask;
int gBufSubsample = int( findLSB( sampleMask ) );
pixelData.normal = normalize( OGRE_Load2DMS( gBuf_normals, iFragCoord, gBufSubsample ).xyz * 2.0 - 1.0 );
float2 shadowRoughness = OGRE_Load2DMS( gBuf_shadowRoughness, iFragCoord, gBufSubsample ).xy;
@else
pixelData.normal = normalize( OGRE_Load2D( gBuf_normals, iFragCoord, 0 ).xyz * 2.0 - 1.0 );
float2 shadowRoughness = OGRE_Load2D( gBuf_shadowRoughness, iFragCoord, 0 ).xy;
@end
float fShadow = shadowRoughness.x;
@property( roughness_map )
pixelData.roughness = shadowRoughness.y * 0.98 + 0.02;
@end
@end
@property( !hlms_prepass )
@insertpiece( LightingHeader )
@insertpiece( custom_ps_preLights )
@property( !custom_disable_directional_lights )
float fTerrainShadow = OGRE_Sample( terrainShadows, samplerStateTerra, inPs.uv0.xy ).x;
@property( !(hlms_pssm_splits || (!hlms_pssm_splits && hlms_num_shadow_map_lights && hlms_lights_directional)) )
float fShadow = 1.0f;
@end
fShadow *= fTerrainShadow;
@insertpiece( DoDirectionalLights )
@end
@insertpiece( DoPointLights )
@insertpiece( DoSpotLights )
@insertpiece( DoAreaApproxLights )
@insertpiece( DoAreaLtcLights )
@insertpiece( forward3dLighting )
@property( needs_env_brdf && (use_envprobe_map || hlms_use_ssr || use_planar_reflections || vct_num_probes) )
pixelData.envColourS = float3( 0, 0, 0 );
pixelData.envColourD = float3( 0, 0, 0 );
@end
@insertpiece( applyVoxelConeTracing )
@insertpiece( forwardPlusDoCubemaps )
@insertpiece( applyIrradianceVolumes )
@insertpiece( DoEmissiveLight )
@property( use_envprobe_map )
@property( use_parallax_correct_cubemaps && !hlms_enable_cubemaps_auto )
@insertpiece( CubemapManualPcc )
@end @property( !use_parallax_correct_cubemaps )
@insertpiece( CubemapGlobal )
@end
@end
@property( hlms_use_ssr )
//TODO: SSR pass should be able to combine global & local cubemap.
float4 ssrReflection = OGRE_Load2D( ssrTexture, iFragCoord, 0 ).xyzw;
@property( use_envprobe_map )
pixelData.envColourS = lerp( pixelData.envColourS.xyz, ssrReflection.xyz, ssrReflection.w );
@else
pixelData.envColourS += ssrReflection.xyz * ssrReflection.w;
@end
@end
@insertpiece( DoPlanarReflectionsPS )
@property( ambient_hemisphere )
@property( use_envprobe_map || hlms_use_ssr || use_planar_reflections || vct_num_probes )
@property( vct_num_probes )
//Only use ambient lighting if object is outside any VCT probe
if( vctSpecular.w == 0 )
{
@end
pixelData.envColourS += lerp( passBuf.ambientLowerHemi.xyz, passBuf.ambientUpperHemi.xyz, ambientWD );
pixelData.envColourD += lerp( passBuf.ambientLowerHemi.xyz, passBuf.ambientUpperHemi.xyz, ambientWS );
@property( vct_num_probes )
}
@end
@else
pixelData.envColourS = lerp( passBuf.ambientLowerHemi.xyz, passBuf.ambientUpperHemi.xyz, ambientWD );
pixelData.envColourD = lerp( passBuf.ambientLowerHemi.xyz, passBuf.ambientUpperHemi.xyz, ambientWS );
@end
@end
@property( ambient_fixed && vct_num_probes )
//Only use ambient lighting if object is outside any VCT probe
finalColour += vctSpecular.w == 0 ? float3( 0, 0, 0 ) :
(passBuf.ambientUpperHemi.xyz * pixelData.diffuse.xyz);
@end
@property( needs_env_brdf )
@insertpiece( BRDF_EnvMap )
@end
@end ///!hlms_prepass
@property( !hlms_render_depth_only )
@property( !hlms_prepass )
@property( !hw_gamma_write )
//Linear to Gamma space
outPs_colour0.xyz = sqrt( finalColour );
@else
outPs_colour0.xyz = finalColour;
@end
@property( hlms_alphablend )
@property( use_texture_alpha )
outPs_colour0.w = material.F0.w * pixelData.diffuse.w;
@else
outPs_colour0.w = material.F0.w;
@end
@else
outPs_colour0.w = 1.0;
@end
//** fog
@insertpiece( FogPS )
@property( debug_pssm_splits )
outPs_colour0.xyz = mix( outPs_colour0.xyz, debugPssmSplit.xyz, 0.2f );
@end
@property( hlms_gen_normals_gbuffer )
outPs_normals = float4( pixelData.normal * 0.5 + 0.5, 1.0 );
@end
@else
outPs_normals = float4( pixelData.normal * 0.5 + 0.5, 1.0 );
@property( hlms_pssm_splits )
outPs_shadowRoughness = float2( fShadow, (pixelData.roughness - 0.02) * 1.02040816 );
@end @property( !hlms_pssm_splits )
outPs_shadowRoughness = float2( 1.0, (pixelData.roughness - 0.02) * 1.02040816 );
@end
@end
@end
@end ///DefaultTerraBodyPS