| 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 "plane mirror" feature, |
| 18 |
https://castle-engine.io/x3d_extensions_mirror_plane.php . } |
| 19 |
|
| 20 |
{$ifdef read_interface} |
| 21 |
|
| 22 |
//type |
| 23 |
TSurfaceTexture = ( |
| 24 |
stEmissive, |
| 25 |
stAmbient, |
| 26 |
stSpecular, |
| 27 |
stShininess, |
| 28 |
stMetallicRoughness, |
| 29 |
stOcclusion |
| 30 |
); |
| 31 |
|
| 32 |
TSurfaceTextureShader = record |
| 33 |
Enable: boolean; |
| 34 |
TextureUnit, TextureCoordinatesId: Cardinal; |
| 35 |
UniformTextureName: String; |
| 36 |
PlugCode: String; |
| 37 |
procedure PrepareHash(var Hash: TShaderCodeHash; |
| 38 |
const SurfaceTexture: TSurfaceTexture); |
| 39 |
procedure GenerateCode(const Shader: TShader); |
| 40 |
procedure SetUniformsOnce(const AProgram: TX3DShaderProgram); |
| 41 |
end; |
| 42 |
|
| 43 |
TSurfaceTextureShaderList = record |
| 44 |
Items: array [TSurfaceTexture] of TSurfaceTextureShader; |
| 45 |
procedure Clear; |
| 46 |
procedure GenerateCode(const Shader: TShader); |
| 47 |
procedure SetUniformsOnce(const AProgram: TX3DShaderProgram); |
| 48 |
end; |
| 49 |
|
| 50 |
{$endif read_interface} |
| 51 |
|
| 52 |
{$ifdef read_implementation} |
| 53 |
|
| 54 |
{ TSurfaceTextureShader ------------------------------------------------------ } |
| 55 |
|
| 56 |
procedure TSurfaceTextureShader.PrepareHash(var Hash: TShaderCodeHash; |
| 57 |
const SurfaceTexture: TSurfaceTexture); |
| 58 |
var |
| 59 |
HashMultiplier: UInt32; |
| 60 |
begin |
| 61 |
if Enable then |
| 62 |
begin |
| 63 |
HashMultiplier := 2063 * (1 + Ord(SurfaceTexture)); |
| 64 |
Hash.AddInteger(HashMultiplier * ( |
| 65 |
2069 * (1 + TextureUnit) + |
| 66 |
2081 * (1 + TextureCoordinatesId) |
| 67 |
)); |
| 68 |
{ TODO: add Hash.AddString(PlugCode, 2083 * HashMultiplier); |
| 69 |
to account that PlugCode may change? |
| 70 |
But in reality it never changes, only in case of CommonSurfaceShader |
| 71 |
the ChannelMask is configurable. } |
| 72 |
end; |
| 73 |
end; |
| 74 |
|
| 75 |
procedure TSurfaceTextureShader.GenerateCode(const Shader: TShader); |
| 76 |
begin |
| 77 |
if Enable then |
| 78 |
begin |
| 79 |
Shader.Plug(stFragment, SReplacePatterns( |
| 80 |
'uniform sampler2D {uniform_texture_name};' + NL + |
| 81 |
{$ifndef OpenGLES} // avoid redeclaring stuff when no "separate compilation units" (OpenGLES) |
| 82 |
'varying vec4 {coord_name};' + NL + |
| 83 |
'// Declare functions defined in tone_mapping.fs' + NL + |
| 84 |
'vec4 castle_texture_color_to_linear(const in vec4 srgbIn);' + NL + |
| 85 |
'vec3 castle_texture_color_to_linear(const in vec3 srgbIn);' + NL + |
| 86 |
{$endif} |
| 87 |
PlugCode, |
| 88 |
[ |
| 89 |
'{uniform_texture_name}', |
| 90 |
'{coord_name}' |
| 91 |
], |
| 92 |
[ |
| 93 |
UniformTextureName, |
| 94 |
TTextureCoordinateShader.CoordName(TextureCoordinatesId) |
| 95 |
], false)); |
| 96 |
end; |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TSurfaceTextureShader.SetUniformsOnce(const AProgram: TX3DShaderProgram); |
| 100 |
begin |
| 101 |
if Enable then |
| 102 |
AProgram.SetUniform(UniformTextureName, Integer(TextureUnit)); |
| 103 |
end; |
| 104 |
|
| 105 |
{ TSurfaceTextureShaderList -------------------------------------------------- } |
| 106 |
|
| 107 |
procedure TSurfaceTextureShaderList.Clear; |
| 108 |
var |
| 109 |
SurfaceTexture: TSurfaceTexture; |
| 110 |
begin |
| 111 |
for SurfaceTexture := Low(TSurfaceTexture) to High(TSurfaceTexture) do |
| 112 |
{ No need to reset other Items[SurfaceTexture] fields. } |
| 113 |
Items[SurfaceTexture].Enable := false; |
| 114 |
end; |
| 115 |
|
| 116 |
procedure TSurfaceTextureShaderList.GenerateCode(const Shader: TShader); |
| 117 |
var |
| 118 |
SurfaceTexture: TSurfaceTexture; |
| 119 |
begin |
| 120 |
for SurfaceTexture := Low(TSurfaceTexture) to High(TSurfaceTexture) do |
| 121 |
Items[SurfaceTexture].GenerateCode(Shader); |
| 122 |
end; |
| 123 |
|
| 124 |
procedure TSurfaceTextureShaderList.SetUniformsOnce(const AProgram: TX3DShaderProgram); |
| 125 |
var |
| 126 |
SurfaceTexture: TSurfaceTexture; |
| 127 |
begin |
| 128 |
for SurfaceTexture := Low(TSurfaceTexture) to High(TSurfaceTexture) do |
| 129 |
Items[SurfaceTexture].SetUniformsOnce(AProgram); |
| 130 |
end; |
| 131 |
|
| 132 |
{$endif read_implementation} |