| 1 |
{%MainUnit castleinternalrenderer.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2023 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 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
{ GLSL program integrated with TRenderer. |
| 20 |
This class adds ability to bind textures to uniform variables |
| 21 |
of GLSL shader. } |
| 22 |
TX3DGLSLProgram = class(TX3DShaderProgram) |
| 23 |
public |
| 24 |
constructor Create; |
| 25 |
function SetupUniforms(var BoundTextureUnits: Cardinal): boolean; override; |
| 26 |
end; |
| 27 |
|
| 28 |
{$endif read_interface} |
| 29 |
|
| 30 |
{$ifdef read_implementation} |
| 31 |
|
| 32 |
{ TX3DGLSLProgram ----------------------------------------------------------- } |
| 33 |
|
| 34 |
constructor TX3DGLSLProgram.Create; |
| 35 |
begin |
| 36 |
inherited Create; |
| 37 |
end; |
| 38 |
|
| 39 |
function TX3DGLSLProgram.SetupUniforms(var BoundTextureUnits: Cardinal): boolean; |
| 40 |
|
| 41 |
{ Bind texture node to a uniform value. |
| 42 |
Returns false if setting this texture node resulted in GLSL error |
| 43 |
(invalid name or type in shader code) |
| 44 |
and it should be removed from UniformsTextures. } |
| 45 |
function BindTextureUniform(TextureField: TX3DField): boolean; |
| 46 |
|
| 47 |
{ If TextureNode <> @nil and is a texture node, prepare it. |
| 48 |
Returns bound texture number (or -1 if failed). |
| 49 |
You should set shader uniform value to this number. } |
| 50 |
function BindSingleTexture(TextureNode: TX3DNode): Integer; |
| 51 |
begin |
| 52 |
Result := -1; |
| 53 |
if (TextureNode <> nil) and |
| 54 |
(TextureNode is TAbstractTextureNode) then |
| 55 |
begin |
| 56 |
if BoundTextureUnits < GLFeatures.MaxTextureUnits then |
| 57 |
begin |
| 58 |
if TTextureResources.Bind( |
| 59 |
TAbstractTextureNode(TextureNode), BoundTextureUnits) then |
| 60 |
begin |
| 61 |
Result := BoundTextureUnits; |
| 62 |
Inc(BoundTextureUnits); |
| 63 |
end else |
| 64 |
SetupUniforms := false; |
| 65 |
end else |
| 66 |
WritelnWarning('Renderer', 'Not enough free texture units to bind all textures for this GLSL shader'); |
| 67 |
end; |
| 68 |
end; |
| 69 |
|
| 70 |
var |
| 71 |
J: Integer; |
| 72 |
BoundValue: Int32; |
| 73 |
UniformValue: TInt32List; |
| 74 |
AUniformMissing: TUniformMissing; |
| 75 |
begin |
| 76 |
AUniformMissing := UniformMissingFromNode(TextureField.ParentNode as TX3DNode); |
| 77 |
|
| 78 |
if TextureField is TSFNode then |
| 79 |
begin |
| 80 |
BoundValue := BindSingleTexture(TSFNode(TextureField).Value); |
| 81 |
if BoundValue <> -1 then |
| 82 |
SetUniform(TextureField.X3DName, BoundValue, AUniformMissing); |
| 83 |
end else |
| 84 |
begin |
| 85 |
Assert(TextureField is TMFNode); |
| 86 |
|
| 87 |
UniformValue := TInt32List.Create; |
| 88 |
try |
| 89 |
UniformValue.Count := TMFNode(TextureField).Count; |
| 90 |
for J := 0 to TMFNode(TextureField).Count - 1 do |
| 91 |
begin |
| 92 |
BoundValue := BindSingleTexture(TMFNode(TextureField)[J]); |
| 93 |
{ We cannot just resign from setting the field entirely, |
| 94 |
and setting to -1 would likely produce wild OpenGL warnings |
| 95 |
(setting texture sampler to -1...). |
| 96 |
So on failure, assume safe 0. BindTexture already did |
| 97 |
necessary warnings for user. } |
| 98 |
if BoundValue = -1 then BoundValue := 0; |
| 99 |
UniformValue[J] := BoundValue; |
| 100 |
end; |
| 101 |
SetUniform(TextureField.X3DName, UniformValue, AUniformMissing); |
| 102 |
finally FreeAndNil(UniformValue) end; |
| 103 |
end; |
| 104 |
Result := true; |
| 105 |
end; |
| 106 |
|
| 107 |
var |
| 108 |
I: Integer; |
| 109 |
begin |
| 110 |
Result := inherited SetupUniforms(BoundTextureUnits); |
| 111 |
I := 0; |
| 112 |
while I < UniformsTextures.Count do |
| 113 |
begin |
| 114 |
if BindTextureUniform(UniformsTextures[I]) then |
| 115 |
Inc(I) |
| 116 |
else |
| 117 |
UniformsTextures.Delete(I); |
| 118 |
end; |
| 119 |
end; |
| 120 |
|
| 121 |
{$endif read_implementation} |