| 1 |
{%MainUnit castlerendererinternalshader.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2024 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Generating shader code for shadow maps. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
//type |
| 22 |
{ Generating shadow map shader code. |
| 23 |
|
| 24 |
Initially, TShader.EnableShadowMap only initialized this and calls PrepareHash. |
| 25 |
|
| 26 |
Later, once we know we need to generate new code, |
| 27 |
and once light shader code is fully known |
| 28 |
(e.g. we know lighting model, physical or Phong) -> then GenerateCode is called. |
| 29 |
|
| 30 |
Uniforms are set by SetUniforms (once, after linking) |
| 31 |
and SetDynamicUniforms (each frame). } |
| 32 |
TShadowMapShader = record |
| 33 |
strict private |
| 34 |
function ShadowMapName: String; |
| 35 |
function ProjectorMatrixName: String; |
| 36 |
public |
| 37 |
TextureUnit: Integer; |
| 38 |
ShadowMap: TGeneratedShadowMapNode; |
| 39 |
procedure PrepareHash(var Hash: TShaderCodeHash); |
| 40 |
procedure GenerateCode(const Shader: TShader); |
| 41 |
procedure SetUniformsOnce(const AProgram: TX3DShaderProgram); |
| 42 |
procedure SetDynamicUniforms(const AProgram: TX3DShaderProgram; |
| 43 |
const RenderingCamera: TRenderingCamera); |
| 44 |
end; |
| 45 |
|
| 46 |
{ List of TShadowMapShader, containing all shadow maps used |
| 47 |
when rendering one shape. |
| 48 |
The methods here just call corresponding TShadowMapShader methods |
| 49 |
on all children. } |
| 50 |
TShadowMapShaderList = class({$ifdef FPC}specialize{$endif} TStructList<TShadowMapShader>) |
| 51 |
public |
| 52 |
procedure GenerateCode(const Shader: TShader); |
| 53 |
procedure SetUniformsOnce(const AProgram: TX3DShaderProgram); |
| 54 |
procedure SetDynamicUniforms(const AProgram: TX3DShaderProgram; |
| 55 |
const RenderingCamera: TRenderingCamera); |
| 56 |
end; |
| 57 |
|
| 58 |
{$endif read_interface} |
| 59 |
|
| 60 |
{$ifdef read_implementation} |
| 61 |
|
| 62 |
{ TShadowMapShader ----------------------------------------------------------- } |
| 63 |
|
| 64 |
procedure TShadowMapShader.PrepareHash(var Hash: TShaderCodeHash); |
| 65 |
begin |
| 66 |
Hash.AddInteger(6353 * TextureUnit); |
| 67 |
Hash.AddPointer(ShadowMap); |
| 68 |
end; |
| 69 |
|
| 70 |
function TShadowMapShader.ShadowMapName: String; |
| 71 |
begin |
| 72 |
Result := 'castle_shadow_map_' + IntToStr(TextureUnit); |
| 73 |
end; |
| 74 |
|
| 75 |
function TShadowMapShader.ProjectorMatrixName: String; |
| 76 |
begin |
| 77 |
Result := 'castle_projector_matrix_' + IntToStr(TextureUnit); |
| 78 |
end; |
| 79 |
|
| 80 |
procedure TShadowMapShader.GenerateCode(const Shader: TShader); |
| 81 |
var |
| 82 |
ProjectedTexCoordName: String; |
| 83 |
SamplerType: String; |
| 84 |
ShadowLightShader: TLightShader; |
| 85 |
begin |
| 86 |
// we pass here only TGeneratedShadowMapNode with light node set |
| 87 |
Assert(ShadowMap.Light <> nil); |
| 88 |
|
| 89 |
if not Shader.LightShaders.Find(ShadowMap.Light, ShadowLightShader) then |
| 90 |
Exit; |
| 91 |
|
| 92 |
{ For variance shadow maps, use normal sampler2D, not sampler2DShadow } |
| 93 |
if Shader.ShadowSampling = ssVarianceShadowMaps then |
| 94 |
SamplerType := 'sampler2D' |
| 95 |
else |
| 96 |
SamplerType := 'sampler2DShadow'; |
| 97 |
|
| 98 |
ProjectedTexCoordName := 'castle_projected_tex_coord_' + IntToStr(TextureUnit); |
| 99 |
|
| 100 |
{ Vertex shader calculates varying vec4: |
| 101 |
current vertex projected into the light node coordinate system. } |
| 102 |
Shader.Plug(stVertex, |
| 103 |
Format('uniform mat4 %s;', [ProjectorMatrixName]) + NL + |
| 104 |
Format('varying vec4 %s;', [ProjectedTexCoordName]) + NL + |
| 105 |
'void PLUG_vertex_eye_space(const in vec4 vertex_eye, const in vec3 normal_eye)' + NL + |
| 106 |
'{' + NL + |
| 107 |
Format(' %s = %s * vertex_eye;', [ProjectedTexCoordName, ProjectorMatrixName]) + NL + |
| 108 |
'}' + NL); |
| 109 |
|
| 110 |
{ Fragment shader uses this varying vec4 to calculate shadow, |
| 111 |
when given light is applied. } |
| 112 |
Shader.Plug(stFragment, |
| 113 |
Format('uniform %s %s;', [SamplerType, ShadowMapName]) + NL + |
| 114 |
Format('varying vec4 %s;', [ProjectedTexCoordName]) + NL + |
| 115 |
|
| 116 |
// Do not redeclare shadow() and friends on OpenGLES |
| 117 |
{$ifndef OpenGLES} |
| 118 |
Shader.DeclareShadowFunctions + NL + |
| 119 |
{$endif} |
| 120 |
|
| 121 |
'void PLUG_light_scale(inout float scale, const in vec3 normal_eye, const in vec3 light_dir)' + NL + |
| 122 |
'{' +NL+ |
| 123 |
Format( |
| 124 |
' scale *= shadow(%s, %s, %d.0);', [ |
| 125 |
ShadowMapName, |
| 126 |
ProjectedTexCoordName, |
| 127 |
ShadowMap.Size |
| 128 |
] |
| 129 |
) + NL + |
| 130 |
'}', |
| 131 |
ShadowLightShader.Code); |
| 132 |
end; |
| 133 |
|
| 134 |
procedure TShadowMapShader.SetUniformsOnce(const AProgram: TX3DShaderProgram); |
| 135 |
begin |
| 136 |
AProgram.SetUniform(ShadowMapName, TextureUnit); |
| 137 |
end; |
| 138 |
|
| 139 |
procedure TShadowMapShader.SetDynamicUniforms(const AProgram: TX3DShaderProgram; |
| 140 |
const RenderingCamera: TRenderingCamera); |
| 141 |
const |
| 142 |
ProjectorScalingMatrix: TMatrix4 = (Data: |
| 143 |
( (0.5, 0, 0, 0), |
| 144 |
( 0, 0.5, 0, 0), |
| 145 |
( 0, 0, 0.5, 0), |
| 146 |
(0.5, 0.5, 0.5, 1) )); |
| 147 |
var |
| 148 |
ProjectorMatrix: TMatrix4; |
| 149 |
begin |
| 150 |
RenderingCamera.InverseMatrixNeeded; |
| 151 |
ProjectorMatrix := (ProjectorScalingMatrix * |
| 152 |
ShadowMap.Light.GetProjectorMatrix) * |
| 153 |
RenderingCamera.InverseMatrix; |
| 154 |
AProgram.SetUniform(ProjectorMatrixName, ProjectorMatrix); |
| 155 |
end; |
| 156 |
|
| 157 |
{ TShadowMapShaderList ----------------------------------------------------------- } |
| 158 |
|
| 159 |
procedure TShadowMapShaderList.GenerateCode(const Shader: TShader); |
| 160 |
var |
| 161 |
I: Integer; |
| 162 |
begin |
| 163 |
for I := 0 to Count - 1 do |
| 164 |
L[I].GenerateCode(Shader); |
| 165 |
end; |
| 166 |
|
| 167 |
procedure TShadowMapShaderList.SetUniformsOnce(const AProgram: TX3DShaderProgram); |
| 168 |
var |
| 169 |
I: Integer; |
| 170 |
begin |
| 171 |
for I := 0 to Count - 1 do |
| 172 |
L[I].SetUniformsOnce(AProgram); |
| 173 |
end; |
| 174 |
|
| 175 |
procedure TShadowMapShaderList.SetDynamicUniforms(const AProgram: TX3DShaderProgram; |
| 176 |
const RenderingCamera: TRenderingCamera); |
| 177 |
var |
| 178 |
I: Integer; |
| 179 |
begin |
| 180 |
for I := 0 to Count - 1 do |
| 181 |
L[I].SetDynamicUniforms(AProgram, RenderingCamera); |
| 182 |
end; |
| 183 |
|
| 184 |
{$endif read_implementation} |