Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Debug: building lightMapping shader permutation with macro: none
- Warn: Source for shader program lightMappingMaterial:
- 0: #version 460 core
- 1: #define HAVE_EXT_gpu_shader4 1
- 2: #define HAVE_ARB_gpu_shader5 1
- 3: #define HAVE_ARB_texture_gather 1
- 4: #define HAVE_EXT_texture_integer 1
- 5: #define HAVE_ARB_texture_rg 1
- 6: #define HAVE_ARB_uniform_buffer_object 1
- 7: #extension GL_ARB_bindless_texture : require
- 8: #define HAVE_ARB_bindless_texture 1
- 9: #extension GL_ARB_shader_draw_parameters : require
- 10: #define HAVE_ARB_shader_draw_parameters 1
- 11: #define HAVE_ARB_shader_storage_buffer_object 1
- 12: #define HAVE_ARB_explicit_attrib_location 1
- 13: #define IN(mode) mode in
- 14: #define DECLARE_OUTPUT(type) out type outputColor;
- 15: #define textureCube texture
- 16: #define texture2D texture
- 17: #define texture2DProj textureProj
- 18: #define texture3D texture
- 19: layout(bindless_sampler) uniform;
- 20: IN(flat) int in_drawID;
- 21: IN(flat) int in_baseInstance;
- 22: #define drawID in_drawID
- 23: #define baseInstance in_baseInstance
- 24: IN(flat) uint in_MatID;
- 25: #define matID in_MatID
- 26:
- 27: #ifndef r_highPrecisionRendering
- 28: #define r_highPrecisionRendering 1
- 29: #endif
- 30: #ifndef r_texturePacks
- 31: #define r_texturePacks 1
- 32: #endif
- 33: #ifndef r_precomputedLighting
- 34: #define r_precomputedLighting 1
- 35: #endif
- 36: #ifndef r_vertexSkinning
- 37: #define r_vertexSkinning 1
- 38: #endif
- 39: const int MAX_GLSL_BONES = 256;
- 40: #ifndef r_halfLambertLighting
- 41: #define r_halfLambertLighting 1
- 42: #endif
- 43: #ifndef r_rimLighting
- 44: #define r_rimLighting 1
- 45: #endif
- 46: const float r_RimExponent = 3.00000000e+00;
- 47: #ifndef r_normalMapping
- 48: #define r_normalMapping 1
- 49: #endif
- 50: #ifndef r_specularMapping
- 51: #define r_specularMapping 1
- 52: #endif
- 53: #ifndef r_physicalMapping
- 54: #define r_physicalMapping 1
- 55: #endif
- 56: #ifndef r_glowMapping
- 57: #define r_glowMapping 1
- 58: #endif
- 59: #define USE_MATERIAL_SYSTEM
- 60:
- 61: struct Material {
- 62: vec3 u_DiffuseMapModifier;
- 63: int u_DiffuseMapModifier_padding;
- 64: vec3 u_MaterialMapModifier;
- 65: int u_MaterialMapModifier_padding;
- 66: vec3 u_LightMapModifier;
- 67: int u_LightMapModifier_padding;
- 68: vec3 u_DeluxeMapModifier;
- 69: int u_DeluxeMapModifier_padding;
- 70: vec3 u_GlowMapModifier;
- 71: int u_GlowMapModifier_padding;
- 72: vec3 u_NormalMapModifier;
- 73: int u_NormalMapModifier_padding;
- 74: vec3 u_HeightMapModifier;
- 75: int u_HeightMapModifier_padding;
- 76: mat4 u_TextureMatrix;
- 77: vec4 u_ColorModulate;
- 78: vec4 u_Color;
- 79: vec3 u_NormalScale;
- 80: int u_NormalScale_padding;
- 81: uvec2 u_DiffuseMap;
- 82: uvec2 u_NormalMap;
- 83: uvec2 u_HeightMap;
- 84: uvec2 u_MaterialMap;
- 85: uvec2 u_LightMap;
- 86: uvec2 u_DeluxeMap;
- 87: uvec2 u_GlowMap;
- 88: uvec2 u_EnvironmentMap0;
- 89: uvec2 u_EnvironmentMap1;
- 90: uvec2 u_LightGrid1;
- 91: uvec2 u_LightGrid2;
- 92: uvec2 u_LightTilesInt;
- 93: vec2 u_SpecularExponent;
- 94: float u_AlphaThreshold;
- 95: float u_InverseLightFactor;
- 96: float u_VertexInterpolation;
- 97: float u_ReliefDepthScale;
- 98: float u_ReliefOffsetBias;
- 99: float u_EnvironmentInterpolation;
- 100: bool u_ShowTris;
- 101: float u_Time;
- 102: int material_padding0;
- 103: int material_padding1;
- 104: };
- 105:
- 106: layout(std430, binding = 0) readonly buffer materialsSSBO {
- 107: Material materials[];
- 108: };
- 109:
- 110: #define u_DiffuseMapModifier materials[823].u_DiffuseMapModifier
- 111: #define u_MaterialMapModifier materials[823].u_MaterialMapModifier
- 112: #define u_LightMapModifier materials[823].u_LightMapModifier
- 113: #define u_DeluxeMapModifier materials[823].u_DeluxeMapModifier
- 114: #define u_GlowMapModifier materials[823].u_GlowMapModifier
- 115: #define u_NormalMapModifier materials[823].u_NormalMapModifier
- 116: #define u_HeightMapModifier materials[823].u_HeightMapModifier
- 117: #define u_TextureMatrix materials[823].u_TextureMatrix
- 118: #define u_ColorModulate materials[823].u_ColorModulate
- 119: #define u_Color materials[823].u_Color
- 120: #define u_NormalScale materials[823].u_NormalScale
- 121: #define u_DiffuseMap_initial uvec2( materials[823].u_DiffuseMap )
- 122: #define u_NormalMap_initial uvec2( materials[823].u_NormalMap )
- 123: #define u_HeightMap_initial uvec2( materials[823].u_HeightMap )
- 124: #define u_MaterialMap_initial uvec2( materials[823].u_MaterialMap )
- 125: #define u_LightMap_initial uvec2( materials[823].u_LightMap )
- 126: #define u_DeluxeMap_initial uvec2( materials[823].u_DeluxeMap )
- 127: #define u_GlowMap_initial uvec2( materials[823].u_GlowMap )
- 128: #define u_EnvironmentMap0_initial uvec2( materials[823].u_EnvironmentMap0 )
- 129: #define u_EnvironmentMap1_initial uvec2( materials[823].u_EnvironmentMap1 )
- 130: #define u_LightGrid1_initial uvec2( materials[823].u_LightGrid1 )
- 131: #define u_LightGrid2_initial uvec2( materials[823].u_LightGrid2 )
- 132: #define u_LightTilesInt_initial uvec2( materials[823].u_LightTilesInt )
- 133: #define u_SpecularExponent materials[823].u_SpecularExponent
- 134: #define u_AlphaThreshold materials[823].u_AlphaThreshold
- 135: #define u_InverseLightFactor materials[823].u_InverseLightFactor
- 136: #define u_VertexInterpolation materials[823].u_VertexInterpolation
- 137: #define u_ReliefDepthScale materials[823].u_ReliefDepthScale
- 138: #define u_ReliefOffsetBias materials[823].u_ReliefOffsetBias
- 139: #define u_EnvironmentInterpolation materials[823].u_EnvironmentInterpolation
- 140: #define u_ShowTris materials[823].u_ShowTris
- 141: #define u_Time materials[823].u_Time
- 142:
- 143: uniform vec3 u_ViewOrigin;
- 144: uniform mat4 u_ModelMatrix;
- 145: uniform mat4 u_ModelViewProjectionMatrix;
- 146: uniform vec3 u_LightGridOrigin;
- 147: uniform vec3 u_LightGridScale;
- 148: uniform int u_numLights;
- 149:
- 150: #ifndef r_AmbientScale
- 151: #define r_AmbientScale 1.00000000e+00
- 152: #endif
- 153: #ifndef r_SpecularScale
- 154: #define r_SpecularScale 1.00000000e+00
- 155: #endif
- 156: #ifndef r_zNear
- 157: #define r_zNear 3.00000000e+00
- 158: #endif
- 159: #ifndef M_PI
- 160: #define M_PI 3.14159274e+00
- 161: #endif
- 162: #ifndef MAX_SHADOWMAPS
- 163: #define MAX_SHADOWMAPS 5
- 164: #endif
- 165: #ifndef MAX_REF_LIGHTS
- 166: #define MAX_REF_LIGHTS 1024
- 167: #endif
- 168: #ifndef TILE_SIZE
- 169: #define TILE_SIZE 16
- 170: #endif
- 171: #ifndef r_FBufSize
- 172: #define r_FBufSize vec2(2.56000000e+03, 1.44000000e+03)
- 173: #endif
- 174: #ifndef r_tileStep
- 175: #define r_tileStep vec2(6.24999963e-03, 1.11111114e-02)
- 176: #endif
- 0: #line 0
- 1: /*
- 2: ===========================================================================
- 3: Copyright (C) 2009-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
- 4:
- 5: This file is part of XreaL source code.
- 6:
- 7: XreaL source code is free software; you can redistribute it
- 8: and/or modify it under the terms of the GNU General Public License as
- 9: published by the Free Software Foundation; either version 2 of the License,
- 10: or (at your option) any later version.
- 11:
- 12: XreaL source code is distributed in the hope that it will be
- 13: useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
- 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- 15: GNU General Public License for more details.
- 16:
- 17: You should have received a copy of the GNU General Public License
- 18: along with XreaL source code; if not, write to the Free Software
- 19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- 20: ===========================================================================
- 21: */
- 22: // computeLight_fp.glsl - Light computing helper functions
- 23:
- 24: #define COMPUTELIGHT_GLSL
- 25:
- 26: #if !defined(USE_BSP_SURFACE)
- 27: #define USE_MODEL_SURFACE
- 28: #endif
- 29:
- 30: #if !defined(USE_GRID_LIGHTING)
- 31: #define USE_LIGHT_MAPPING
- 32: #endif
- 33:
- 34: #if defined(USE_REFLECTIVE_SPECULAR)
- 35: #endif // USE_REFLECTIVE_SPECULAR
- 36:
- 37: #if defined(HAVE_ARB_uniform_buffer_object)
- 38: struct light {
- 39: vec4 center_radius;
- 40: vec4 color_type;
- 41: vec4 direction_angle;
- 42: };
- 43:
- 44: layout(std140) uniform u_Lights {
- 45: light lights[ MAX_REF_LIGHTS ];
- 46: };
- 47: #define GetLight(idx, component) lights[idx].component
- 48: #else // !HAVE_ARB_uniform_buffer_object
- 49: #define idxToTC( idx, w, h ) vec2( floor( ( idx * ( 1.0 / w ) ) + 0.5 ) * ( 1.0 / h ), \
- 50: fract( ( idx + 0.5 ) * (1.0 / w ) ) )
- 51: const struct GetLightOffsets {
- 52: int center_radius;
- 53: int color_type;
- 54: int direction_angle;
- 55: } getLightOffsets = GetLightOffsets(0, 1, 2);
- 56: #define GetLight(idx, component) texture2D( u_LightsTexture, idxToTC(3 * idx + getLightOffsets.component, 64.0, float( 3 * MAX_REF_LIGHTS / 64 ) ) )
- 57: #endif // HAVE_ARB_uniform_buffer_object
- 58:
- 59:
- 60:
- 61: // lighting helper functions
- 62:
- 63: void ReadLightGrid(in vec4 texel, out vec3 ambientColor, out vec3 lightColor) {
- 64: float ambientScale = 2.0 * texel.a;
- 65: float directedScale = 2.0 - ambientScale;
- 66: ambientColor = ambientScale * texel.rgb;
- 67: lightColor = directedScale * texel.rgb;
- 68: }
- 69:
- 70: void computeLight(in vec3 lightColor, vec4 diffuseColor, inout vec4 color) {
- 71: color.rgb += lightColor.rgb * diffuseColor.rgb;
- 72: }
- 73:
- 74: #if defined(USE_REFLECTIVE_SPECULAR)
- 75: void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
- 76: vec4 diffuseColor, vec4 materialColor,
- 77: inout vec4 color, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
- 78: #else // !USE_REFLECTIVE_SPECULAR
- 79: void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
- 80: vec4 diffuseColor, vec4 materialColor,
- 81: inout vec4 color )
- 82: #endif // !USE_REFLECTIVE_SPECULAR
- 83: {
- 84: vec3 H = normalize( lightDir + viewDir );
- 85:
- 86: #if defined(USE_PHYSICAL_MAPPING) || defined(r_specularMapping)
- 87: float NdotH = clamp( dot( normal, H ), 0.0, 1.0 );
- 88: #endif // USE_PHYSICAL_MAPPING || r_specularMapping
- 89:
- 90: // clamp( NdotL, 0.0, 1.0 ) is done below
- 91: float NdotL = dot( normal, lightDir );
- 92:
- 93: #if !defined(USE_BSP_SURFACE) && defined(r_halfLambertLighting)
- 94: // http://developer.valvesoftware.com/wiki/Half_Lambert
- 95: NdotL = NdotL * 0.5 + 0.5;
- 96: NdotL *= NdotL;
- 97: #endif
- 98:
- 99: NdotL = clamp( NdotL, 0.0, 1.0 );
- 100:
- 101: #if defined(USE_PHYSICAL_MAPPING)
- 102: // Daemon PBR packing defaults to ORM like glTF 2.0 defines
- 103: // https://www.khronos.org/blog/art-pipeline-for-gltf
- 104: // > ORM texture for Occlusion, Roughness, and Metallic
- 105: // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/material.pbrMetallicRoughness.schema.json
- 106: // > The metalness values are sampled from the B channel. The roughness values are sampled from the G channel.
- 107: // > These values are linear. If other channels are present (R or A), they are ignored for metallic-roughness calculations.
- 108: // https://docs.blender.org/manual/en/2.80/addons/io_scene_gltf2.html
- 109: // > glTF stores occlusion in the red (R) channel, allowing it to optionally share the same image
- 110: // > with the roughness and metallic channels.
- 111: float roughness = materialColor.g;
- 112: float metalness = materialColor.b;
- 113:
- 114: float NdotV = clamp( dot( normal, viewDir ), 0.0, 1.0);
- 115: float VdotH = clamp( dot( viewDir, H ), 0.0, 1.0);
- 116:
- 117: float alpha = roughness * roughness;
- 118: float k = 0.125 * (roughness + 1.0) * (roughness + 1.0);
- 119:
- 120: float D = alpha / ((NdotH * NdotH) * (alpha * alpha - 1.0) + 1.0);
- 121: D *= D;
- 122:
- 123: float FexpNH = pow(1.0 - NdotH, 5.0);
- 124: float FexpNV = pow(1.0 - NdotV, 5.0);
- 125: vec3 F = mix(vec3(0.04), diffuseColor.rgb, metalness);
- 126: F = F + (1.0 - F) * FexpNH;
- 127:
- 128: float G = NdotL / (NdotL * (1.0 - k) + k);
- 129: G *= NdotV / (NdotV * (1.0 - k) + k);
- 130:
- 131: vec3 diffuseBRDF = NdotL * diffuseColor.rgb * (1.0 - metalness);
- 132: vec3 specularBRDF = vec3((D * F * G) / max(4.0 * NdotL * NdotV, 0.0001f));
- 133: color.rgb += (diffuseBRDF + specularBRDF) * lightColor.rgb * NdotL;
- 134: color.a = mix(diffuseColor.a, 1.0, FexpNV);
- 135: #else // !USE_PHYSICAL_MAPPING
- 136:
- 137: #if defined(USE_REFLECTIVE_SPECULAR)
- 138: // not implemented for PBR yet
- 139: vec4 envColor0 = textureCube(u_EnvironmentMap0, reflect(-viewDir, normal));
- 140: vec4 envColor1 = textureCube(u_EnvironmentMap1, reflect(-viewDir, normal));
- 141:
- 142: materialColor.rgb *= mix(envColor0, envColor1, u_EnvironmentInterpolation).rgb;
- 143: #endif // USE_REFLECTIVE_SPECULAR
- 144:
- 145: color.rgb += lightColor.rgb * NdotL * diffuseColor.rgb;
- 146: #if defined(r_specularMapping)
- 147: // The minimal specular exponent should preferably be nonzero to avoid the undefined pow(0, 0)
- 148: color.rgb += lightColor.rgb * materialColor.rgb * pow( NdotH, u_SpecularExponent.x * materialColor.a + u_SpecularExponent.y) * r_SpecularScale;
- 149: #endif // r_specularMapping
- 150: #endif // !USE_PHYSICAL_MAPPING
- 151: }
- 152:
- 153: #if defined(HAVE_EXT_texture_integer) && defined(r_highPrecisionRendering)
- 154: const int lightsPerLayer = 16;
- 155: #define idxs_t uvec4
- 156: idxs_t fetchIdxs( in vec3 coords, in usampler3D u_LightTilesInt ) {
- 157: return texture3D( u_LightTilesInt, coords );
- 158: }
- 159: int nextIdx( inout idxs_t idxs ) {
- 160: uvec4 tmp = ( idxs & uvec4( 3 ) ) * uvec4( 0x40, 0x10, 0x04, 0x01 );
- 161: idxs = idxs >> 2;
- 162: return int( tmp.x + tmp.y + tmp.z + tmp.w );
- 163: }
- 164: #else // !HAVE_EXT_texture_integer
- 165: const int lightsPerLayer = 4;
- 166: #define idxs_t vec4
- 167: idxs_t fetchIdxs( in vec3 coords ) {
- 168: return texture3D( u_LightTiles, coords ) * 255.0;
- 169: }
- 170: int nextIdx( inout idxs_t idxs ) {
- 171: vec4 tmp = idxs;
- 172: idxs = floor(idxs * 0.25);
- 173: tmp -= 4.0 * idxs;
- 174: return int( dot( tmp, vec4( 64.0, 16.0, 4.0, 1.0 ) ) );
- 175: }
- 176: #endif // HAVE_EXT_texture_integer
- 177:
- 178: const int numLayers = MAX_REF_LIGHTS / 256;
- 179:
- 180: #if defined(r_dynamicLight) && r_dynamicLightRenderer == 1
- 181: #if defined(USE_REFLECTIVE_SPECULAR)
- 182: void computeDynamicLight( int idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
- 183: vec4 material, inout vec4 color, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
- 184: #else // !USE_REFLECTIVE_SPECULAR
- 185: void computeDynamicLight( int idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
- 186: vec4 material, inout vec4 color )
- 187: #endif // !USE_REFLECTIVE_SPECULAR
- 188: {
- 189: vec4 center_radius = GetLight( idx, center_radius );
- 190: vec4 color_type = GetLight( idx, color_type );
- 191: vec3 L;
- 192: float attenuation;
- 193:
- 194: if( color_type.w == 0.0 ) {
- 195: // point light
- 196: L = center_radius.xyz - P;
- 197: // 2.57 ~= 8.0 ^ ( 1.0 / 2.2 ), adjusted after overbright changes
- 198: float t = 1.0 + 2.57 * length(L) / center_radius.w;
- 199: // Quadratic attenuation function instead of linear because of overbright changes
- 200: attenuation = 1.0 / ( t * t );
- 201: L = normalize(L);
- 202: } else if( color_type.w == 1.0 ) {
- 203: // spot light
- 204: vec4 direction_angle = GetLight( idx, direction_angle );
- 205: L = center_radius.xyz - P;
- 206: // 2.57 ~= 8.0 ^ ( 1.0 / 2.2 ), adjusted after overbright changes
- 207: float t = 1.0 + 2.57 * length(L) / center_radius.w;
- 208: // Quadratic attenuation function instead of linear because of overbright changes
- 209: attenuation = 1.0 / ( t * t );
- 210: L = normalize( L );
- 211:
- 212: if( dot( L, direction_angle.xyz ) <= direction_angle.w ) {
- 213: attenuation = 0.0;
- 214: }
- 215: } else if( color_type.w == 2.0 ) {
- 216: // sun (directional) light
- 217: L = GetLight( idx, direction_angle ).xyz;
- 218: attenuation = 1.0;
- 219: }
- 220: #if defined(USE_REFLECTIVE_SPECULAR)
- 221: computeDeluxeLight( L, normal, viewDir,
- 222: attenuation * attenuation * color_type.xyz,
- 223: diffuse, material, color, u_EnvironmentMap0, u_EnvironmentMap1 );
- 224: #else // !USE_REFLECTIVE_SPECULAR
- 225: computeDeluxeLight( L, normal, viewDir,
- 226: attenuation * attenuation * color_type.xyz,
- 227: diffuse, material, color );
- 228: #endif // !USE_REFLECTIVE_SPECULAR
- 229: }
- 230:
- 231: #if defined(USE_REFLECTIVE_SPECULAR)
- 232: void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 material,
- 233: inout vec4 color, in usampler3D u_LightTilesInt, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
- 234: #else // !USE_REFLECTIVE_SPECULAR
- 235: void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 material,
- 236: inout vec4 color, in usampler3D u_LightTilesInt )
- 237: #endif // !USE_REFLECTIVE_SPECULAR
- 238: {
- 239: vec2 tile = floor( gl_FragCoord.xy * (1.0 / float( TILE_SIZE ) ) ) + 0.5;
- 240: vec3 tileScale = vec3( r_tileStep, 1.0/numLayers );
- 241:
- 242: #if defined(r_showLightTiles)
- 243: float numLights = 0.0;
- 244: #endif
- 245:
- 246: for( int layer = 0; layer < numLayers; layer++ ) {
- 247: idxs_t idxs = fetchIdxs( tileScale * vec3( tile, float( layer ) + 0.5 ), u_LightTilesInt );
- 248: for( int i = 0; i < lightsPerLayer; i++ ) {
- 249: int idx = numLayers * nextIdx( idxs ) + layer;
- 250:
- 251: if( idx >= u_numLights )
- 252: {
- 253: break;
- 254: }
- 255:
- 256: #if defined(USE_REFLECTIVE_SPECULAR)
- 257: computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color, u_EnvironmentMap0, u_EnvironmentMap1 );
- 258: #else // !USE_REFLECTIVE_SPECULAR
- 259: computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color );
- 260: #endif // !USE_REFLECTIVE_SPECULAR
- 261:
- 262: #if defined(r_showLightTiles)
- 263: numLights++;
- 264: #endif
- 265: }
- 266: }
- 267:
- 268: #if defined(r_showLightTiles)
- 269: if (numLights > 0.0)
- 270: {
- 271: color = vec4(numLights/(lightsPerLayer*numLayers), numLights/(lightsPerLayer*numLayers), numLights/(lightsPerLayer*numLayers), 1.0);
- 272: }
- 273: #endif
- 274: }
- 275: #endif
- 0: #line 0
- 1: /*
- 2: ===========================================================================
- 3: Copyright (C) 2009-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
- 4:
- 5: This file is part of XreaL source code.
- 6:
- 7: XreaL source code is free software; you can redistribute it
- 8: and/or modify it under the terms of the GNU General Public License as
- 9: published by the Free Software Foundation; either version 2 of the License,
- 10: or (at your option) any later version.
- 11:
- 12: XreaL source code is distributed in the hope that it will be
- 13: useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
- 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- 15: GNU General Public License for more details.
- 16:
- 17: You should have received a copy of the GNU General Public License
- 18: along with XreaL source code; if not, write to the Free Software
- 19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- 20: ===========================================================================
- 21: */
- 22: // reliefMapping_fp.glsl - Relief mapping helper functions
- 23:
- 24: #define RELIEFMAPPING_GLSL
- 25:
- 26: #if defined(r_normalMapping) || defined(USE_HEIGHTMAP_IN_NORMALMAP)
- 27: #if defined(r_texturePacks)
- 28: #else
- 29: #endif
- 30: #endif // r_normalMapping || USE_HEIGHTMAP_IN_NORMALMAP
- 31:
- 32: #if defined(r_normalMapping)
- 33: #endif // r_normalMapping
- 34:
- 35: #if defined(USE_RELIEF_MAPPING)
- 36: #if !defined(USE_HEIGHTMAP_IN_NORMALMAP)
- 37: #if defined(r_texturePacks)
- 38: #else
- 39: #endif
- 40: #endif // !USE_HEIGHTMAP_IN_NORMALMAP
- 41: #endif // USE_RELIEF_MAPPING
- 42:
- 43: // compute normal in tangent space
- 44: #if defined(r_normalMapping)
- 45: vec3 NormalInTangentSpace(vec2 texNormal, in sampler2D u_NormalMap)
- 46: #else // !r_normalMapping
- 47: vec3 NormalInTangentSpace(vec2 texNormal)
- 48: #endif // !r_normalMapping
- 49: {
- 50: vec3 normal;
- 51:
- 52: #if defined(r_normalMapping)
- 53: #if defined(USE_HEIGHTMAP_IN_NORMALMAP)
- 54: // alpha channel contains the height map so do not try to reconstruct normal map from it
- 55: #if defined(r_texturePacks)
- 56: normal = texture2D(u_NormalMap, vec3( texNormal * u_NormalMapModifier.xy, u_NormalMapModifier.z )).rgb;
- 57: #else
- 58: normal = texture2D(u_NormalMap, texNormal).rgb;
- 59: #endif
- 60: normal = 2.0 * normal - 1.0;
- 61: #else // !USE_HEIGHTMAP_IN_NORMALMAP
- 62: // the Capcom trick abusing alpha channel of DXT1/5 formats to encode normal map
- 63: // https://github.com/DaemonEngine/Daemon/issues/183#issuecomment-473691252
- 64: //
- 65: // the algorithm also works with normal maps in rgb format without alpha channel
- 66: // but we still must be sure there is no height map in alpha channel hence the test
- 67: //
- 68: // crunch -dxn seems to produce such files, since alpha channel is abused such format
- 69: // is unsuitable to embed height map, then height map must be distributed as loose file
- 70: normal = texture2D(u_NormalMap, texNormal).rga;
- 71: #if defined(r_texturePacks)
- 72: normal = texture2D(u_NormalMap, vec3( texNormal * u_NormalMapModifier.xy, u_NormalMapModifier.z )).rga;
- 73: #else
- 74: normal = texture2D(u_NormalMap, texNormal).rga;
- 75: #endif
- 76: normal.x *= normal.z;
- 77: normal.xy = 2.0 * normal.xy - 1.0;
- 78: // In a perfect world this code must be enough:
- 79: // normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
- 80: //
- 81: // Unvanquished texture known to trigger black normalmap artifacts
- 82: // when doing Z reconstruction:
- 83: // textures/shared_pk02_src/rock01_n
- 84: //
- 85: // Although the normal vector is supposed to have a length of 1,
- 86: // dot(normal.xy, normal.xy) may be greater than 1 due to compression
- 87: // artifacts: values as large as 1.27 have been observed with crunch -dxn.
- 88: // https://github.com/DaemonEngine/Daemon/pull/260#issuecomment-571010935
- 89: //
- 90: // This might happen with other formats too. So we must take care not to
- 91: // take the square root of a negative number here.
- 92: normal.z = sqrt(max(0, 1.0 - dot(normal.xy, normal.xy)));
- 93: #endif // !USE_HEIGHTMAP_IN_NORMALMAP
- 94: /* Disable normal map scaling when normal Z scale is set to zero.
- 95:
- 96: This happens when r_normalScale is set to zero because
- 97: u_NormalScale.z is premultiplied with r_normalScale. User can
- 98: disable normal map scaling by setting r_normalScale to zero.
- 99:
- 100: Normal Z component equal to zero would be wrong anyway.
- 101: */
- 102: if (u_NormalScale.z != 0)
- 103: {
- 104: normal *= u_NormalScale;
- 105: }
- 106:
- 107: // HACK: the GLSL code is currently assuming
- 108: // DirectX normal map format (+X -Y +Z)
- 109: // but engine is assuming the OpenGL way (+X +Y +Z)
- 110: normal.y *= -1;
- 111: #else // !r_normalMapping
- 112: // Flat normal map is {0.5, 0.5, 1.0} in [ 0.0, 1.0]
- 113: // which is stored as {0.0, 0.0, 1.0} in [-1.0, 1.0].
- 114: normal = vec3(0.0, 0.0, 1.0);
- 115: #endif // !r_normalMapping
- 116:
- 117: return normal;
- 118: }
- 119:
- 120: // compute normal in worldspace from normalmap
- 121: #if defined(r_normalMapping)
- 122: vec3 NormalInWorldSpace(vec2 texNormal, mat3 tangentToWorldMatrix, in sampler2D u_NormalMap)
- 123: {
- 124: // compute normal in tangent space from normalmap
- 125: vec3 normal = NormalInTangentSpace(texNormal, u_NormalMap);
- 126: // transform normal into world space
- 127: return normalize(tangentToWorldMatrix * normal);
- 128: }
- 129: #else // !r_normalMapping
- 130: vec3 NormalInWorldSpace(vec2 texNormal, mat3 tangentToWorldMatrix)
- 131: {
- 132: // compute normal in tangent space from normalmap
- 133: vec3 normal = NormalInTangentSpace(texNormal);
- 134: // transform normal into world space
- 135: return normalize(tangentToWorldMatrix * normal);
- 136: }
- 137: #endif // !r_normalMapping
- 138:
- 139: #if defined(USE_RELIEF_MAPPING)
- 140: // compute texcoords offset from heightmap
- 141: // most of the code doing somewhat the same is likely to be named
- 142: // RayIntersectDisplaceMap in other id tech3-based engines
- 143: // so please keep the comment above to enable cross-tree look-up
- 144: vec2 ReliefTexOffset(vec2 rayStartTexCoords, vec3 viewDir, mat3 tangentToWorldMatrix, in sampler2D u_HeightMap)
- 145: {
- 146: // compute view direction in tangent space
- 147: vec3 tangentViewDir = normalize(viewDir * tangentToWorldMatrix);
- 148:
- 149: vec2 displacement = tangentViewDir.xy * -u_ReliefDepthScale / tangentViewDir.z;
- 150:
- 151: const int linearSearchSteps = 16;
- 152: const int binarySearchSteps = 6;
- 153:
- 154: float depthStep = 1.0 / float(linearSearchSteps);
- 155: float topDepth = 1.0 - u_ReliefOffsetBias;
- 156:
- 157: // current size of search window
- 158: float currentSize = depthStep;
- 159:
- 160: // current depth position
- 161: float currentDepth = 0.0;
- 162:
- 163: // best match found (starts with last position 1.0)
- 164: float bestDepth = 1.0;
- 165:
- 166: // search front to back for first point inside object
- 167: for(int i = 0; i < linearSearchSteps - 1; ++i)
- 168: {
- 169: currentDepth += currentSize;
- 170:
- 171: #if defined(USE_HEIGHTMAP_IN_NORMALMAP)
- 172: #if defined(r_texturePacks)
- 173: float depth = texture2D(u_HeightMap, vec3( ( rayStartTexCoords + displacement * currentDepth )
- 174: * u_HeightMapModifier.xy, u_HeightMapModifier.z )).a;
- 175: #else
- 176: float depth = texture2D(u_HeightMap, rayStartTexCoords + displacement * currentDepth).a;
- 177: #endif
- 178: #else // !USE_HEIGHTMAP_IN_NORMALMAP
- 179: #if defined(r_texturePacks)
- 180: float depth = texture2D(u_HeightMap, vec3( ( rayStartTexCoords + displacement * currentDepth )
- 181: * u_HeightMapModifier.xy, u_HeightMapModifier.z )).g;
- 182: #else
- 183: float depth = texture2D(u_HeightMap, rayStartTexCoords + displacement * currentDepth).g;
- 184: #endif
- 185: #endif // !USE_HEIGHTMAP_IN_NORMALMAP
- 186:
- 187: float heightMapDepth = topDepth - depth;
- 188:
- 189: if(bestDepth > 0.996) // if no depth found yet
- 190: {
- 191: if(currentDepth >= heightMapDepth)
- 192: {
- 193: bestDepth = currentDepth;
- 194: }
- 195: }
- 196: }
- 197:
- 198: currentDepth = bestDepth;
- 199:
- 200: // recurse around first point (depth) for closest match
- 201: for(int i = 0; i < binarySearchSteps; ++i)
- 202: {
- 203: currentSize *= 0.5;
- 204:
- 205: #if defined(USE_HEIGHTMAP_IN_NORMALMAP)
- 206: float depth = texture2D(u_HeightMap, rayStartTexCoords + displacement * currentDepth).a;
- 207: #else // !USE_HEIGHTMAP_IN_NORMALMAP
- 208: float depth = texture2D(u_HeightMap, rayStartTexCoords + displacement * currentDepth).g;
- 209: #endif // !USE_HEIGHTMAP_IN_NORMALMAP
- 210:
- 211: float heightMapDepth = topDepth - depth;
- 212:
- 213: if(currentDepth >= heightMapDepth)
- 214: {
- 215: bestDepth = currentDepth;
- 216: currentDepth -= 2.0 * currentSize;
- 217: }
- 218:
- 219: currentDepth += currentSize;
- 220: }
- 221:
- 222: return bestDepth * displacement;
- 223: }
- 224: #endif // USE_RELIEF_MAPPING
- 0: #line 0
- 1: /*
- 2: ===========================================================================
- 3: Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
- 4:
- 5: This file is part of XreaL source code.
- 6:
- 7: XreaL source code is free software; you can redistribute it
- 8: and/or modify it under the terms of the GNU General Public License as
- 9: published by the Free Software Foundation; either version 2 of the License,
- 10: or (at your option) any later version.
- 11:
- 12: XreaL source code is distributed in the hope that it will be
- 13: useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
- 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- 15: GNU General Public License for more details.
- 16:
- 17: You should have received a copy of the GNU General Public License
- 18: along with XreaL source code; if not, write to the Free Software
- 19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- 20: ===========================================================================
- 21: */
- 22:
- 23: /* lightMapping_fp.glsl */
- 24:
- 25: #define LIGHTMAPPING_GLSL
- 26:
- 27: #if defined(r_texturePacks)
- 28: #else
- 29: #endif
- 30:
- 31:
- 32: IN(smooth) vec3 var_Position;
- 33: IN(smooth) vec2 var_TexCoords;
- 34: IN(smooth) vec4 var_Color;
- 35: IN(smooth) vec3 var_Tangent;
- 36: IN(smooth) vec3 var_Binormal;
- 37: IN(smooth) vec3 var_Normal;
- 38:
- 39: #if defined(r_texturePacks)
- 40: #else
- 41: #endif
- 42:
- 43:
- 44: #if defined(USE_LIGHT_MAPPING) || defined(USE_DELUXE_MAPPING)
- 45: IN(smooth) vec2 var_TexLight;
- 46: #endif
- 47:
- 48: #if defined(USE_GRID_LIGHTING) || defined(USE_GRID_DELUXE_MAPPING)
- 49: #endif
- 50:
- 51:
- 52: DECLARE_OUTPUT(vec4)
- 53:
- 54: void main()
- 55: {
- 10000: #line 10000 // material_fp.glsl
- 10001: /*
- 10002: ===========================================================================
- 10003:
- 10004: Daemon BSD Source Code
- 10005: Copyright (c) 2024 Daemon Developers
- 10006: All rights reserved.
- 10007:
- 10008: This file is part of the Daemon BSD Source Code (Daemon Source Code).
- 10009:
- 10010: Redistribution and use in source and binary forms, with or without
- 10011: modification, are permitted provided that the following conditions are met:
- 10012: * Redistributions of source code must retain the above copyright
- 10013: notice, this list of conditions and the following disclaimer.
- 10014: * Redistributions in binary form must reproduce the above copyright
- 10015: notice, this list of conditions and the following disclaimer in the
- 10016: documentation and/or other materials provided with the distribution.
- 10017: * Neither the name of the Daemon developers nor the
- 10018: names of its contributors may be used to endorse or promote products
- 10019: derived from this software without specific prior written permission.
- 10020:
- 10021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- 10022: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- 10023: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- 10024: DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
- 10025: DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- 10026: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- 10027: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- 10028: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- 10029: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- 10030: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- 10031:
- 10032: ===========================================================================
- 10033: */
- 10034:
- 10035: /* material_fp.glsl */
- 10036:
- 10037: /*
- 10038: * For use with material system
- 10039: * All changes to uniform samplers in shaders which use this system must be reflected here
- 10040: * Any shader using this system should add #define SHADERNAME_GLSL in the beginning (including lib shaders)
- 10041: * It should then be added here in the material system and bindless texture ifdefs:
- 10042: * #if defined(SHADERNAME_GLSL)
- 10043: * sampler* samplerName = sampler*( samplerName_initial );
- 10044: * #endif // !SHADERNAME_GLSL
- 10045: * In the main shader add
- 10046: * #insert material
- 10047: * in the beginning of main() once
- 10048: * Any texture samplers should be passed to functions from main() or other functions
- 10049: */
- 10050:
- 10051: #if defined(USE_MATERIAL_SYSTEM)
- 10052:
- 10053: #if defined(r_texturePacks)
- 10054: #define samplerPack sampler2DArray
- 10055: #else
- 10056: #define samplerPack sampler2D
- Warn: Compile log:
- 0:71(11): error: no matching function for call to `texture(sampler2D, vec3)'; candidates are:
- 0:71(11): error: vec4 texture(sampler1D, float)
- 0:71(11): error: ivec4 texture(isampler1D, float)
- 0:71(11): error: uvec4 texture(usampler1D, float)
- 0:71(11): error: vec4 texture(sampler2D, vec2)
- 0:71(11): error: ivec4 texture(isampler2D, vec2)
- 0:71(11): error: uvec4 texture(usampler2D, vec2)
- 0:71(11): error: vec4 texture(sampler3D, vec3)
- 0:71(11): error: ivec4 texture(isampler3D, vec3)
- 0:71(11): error: uvec4 texture(usampler3D, vec3)
- 0:71(11): error: vec4 texture(samplerCube, vec3)
- 0:71(11): error: ivec4 texture(isamplerCube, vec3)
- 0:71(11): error: uvec4 texture(usamplerCube, vec3)
- 0:71(11): error: float texture(sampler1DShadow, vec3)
- 0:71(11): error: float texture(sampler2DShadow, vec3)
- 0:71(11): error: float texture(samplerCubeShadow, vec4)
- 0:71(11): error: vec4 texture(sampler1DArray, vec2)
- 0:71(11): error: ivec4 texture(isampler1DArray, vec2)
- 0:71(11): error: uvec4 texture(usampler1DArray, vec2)
- 0:71(11): error: vec4 texture(sampler2DArray, vec3)
- 0:71(11): error: ivec4 texture(isampler2DArray, vec3)
- 0:71(11): error: uvec4 texture(usampler2DArray, vec3)
- 0:71(11): error: vec4 texture(samplerCubeArray, vec4)
- 0:71(11): error: ivec4 texture(isamplerCubeArray, vec4)
- 0:71(11): error: uvec4 texture(usamplerCubeArray, vec4)
- 0:71(11): error: float texture(sampler1DArrayShadow, vec3)
- 0:71(11): error: float texture(sampler2DArrayShadow, vec4)
- 0:71(11): error: float texture(samplerCubeArrayShadow, vec4, float)
- 0:71(11): error: vec4 texture(sampler2DRect, vec2)
- 0:71(11): error: ivec4 texture(isampler2DRect, vec2)
- 0:71(11): error: uvec4 texture(usampler2DRect, vec2)
- 0:71(11): error: float texture(sampler2DRectShadow, vec3)
- 0:71(11): error: vec4 texture(sampler1D, float, float)
- 0:71(11): error: ivec4 texture(isampler1D, float, float)
- 0:71(11): error: uvec4 texture(usampler1D, float, float)
- 0:71(11): error: vec4 texture(sampler2D, vec2, float)
- 0:71(11): error: ivec4 texture(isampler2D, vec2, float)
- 0:71(11): error: uvec4 texture(usampler2D, vec2, float)
- 0:71(11): error: vec4 texture(sampler3D, vec3, float)
- 0:71(11): error: ivec4 texture(isampler3D, vec3, float)
- 0:71(11): error: uvec4 texture(usampler3D, vec3, float)
- 0:71(11): error: vec4 texture(samplerCube, vec3, float)
- 0:71(11): error: ivec4 texture(isamplerCube, vec3, float)
- 0:71(11): error: uvec4 texture(usamplerCube, vec3, float)
- 0:71(11): error: float texture(sampler1DShadow, vec3, float)
- 0:71(11): error: float texture(sampler2DShadow, vec3, float)
- 0:71(11): error: float texture(samplerCubeShadow, vec4, float)
- 0:71(11): error: vec4 texture(sampler1DArray, vec2, float)
- 0:71(11): error: ivec4 texture(isampler1DArray, vec2, float)
- 0:71(11): error: uvec4 texture(usampler1DArray, vec2, float)
- 0:71(11): error: vec4 texture(sampler2DArray, vec3, float)
- 0:71(11): error: ivec4 texture(isampler2DArray, vec3, float)
- 0:71(11): error: uvec4 texture(usampler2DArray, vec3, float)
- 0:71(11): error: vec4 texture(samplerCubeArray, vec4, float)
- 0:71(11): error: ivec4 texture(isamplerCubeArray, vec4, float)
- 0:71(11): error: uvec4 texture(usamplerCubeArray, vec4, float)
- 0:71(11): error: float texture(sampler1DArrayShadow, vec3, float)
- 0:71(11): error: type mismatch
- 0:117(16): error: no matching function for call to `NormalInWorldSpace(vec2, mat3, sampler2DArray)'; candidates are:
- 0:117(16): error: vec3 NormalInWorldSpace(vec2, mat3, sampler2D)
- Warn: Unhandled exception (15ShaderException): Couldn't compile fragment shader: lightMappingMaterial
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement