| 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 |
{ Shape class necessary by TRenderer. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
//type |
| 21 |
{ Shape that can be rendered. } |
| 22 |
TX3DRendererShape = class(TShape) |
| 23 |
private |
| 24 |
{ Generate VBO if needed, and reload VBO contents. |
| 25 |
Assumes GLVertexBufferObject is true. |
| 26 |
|
| 27 |
Arrays data @italic(must not) be freed (by TGeometryArrays.FreeData) |
| 28 |
before calling this method. Also, this method will always call |
| 29 |
Arrays.FreeData. So do not load the same TGeometryArrays instance |
| 30 |
twice to the Vbo. |
| 31 |
|
| 32 |
We always keep assertion that Vbo is loaded <=> Arrays data is freed. } |
| 33 |
procedure LoadArraysToVbo; |
| 34 |
public |
| 35 |
{ Non-nil means that we have obtained TShaderProgramCache instance, |
| 36 |
with valid Hash and ShaderProgram. Note that ShaderProgram may still |
| 37 |
be @nil, if it failed to link. |
| 38 |
|
| 39 |
Separate values for each rendering pass, since different rendering |
| 40 |
passes probably have different GlobalLights and so will require different |
| 41 |
shaders. This makes multi-pass rendering, like for shadow volumes, |
| 42 |
play nicely with shaders. Otherwise we could recreate shaders at each |
| 43 |
rendering pass. } |
| 44 |
ProgramCache: array [TTotalRenderingPass] of TShaderProgramCache; |
| 45 |
|
| 46 |
Cache: TShapeCache; |
| 47 |
|
| 48 |
{ Assign this each time before passing this shape to RenderShape. } |
| 49 |
Fog: TFogFunctionality; |
| 50 |
|
| 51 |
{ For implementing TextureCoordinateGenerator.mode = "MIRROR-PLANE". } |
| 52 |
MirrorPlaneUniforms: TMirrorPlaneUniforms; |
| 53 |
|
| 54 |
{ Is bump mapping allowed by the current shape. |
| 55 |
Fully calculated only after InitMeshRenderer, as determining GeneratorClass |
| 56 |
is needed to set this. } |
| 57 |
BumpMappingAllowed: Boolean; |
| 58 |
|
| 59 |
{ Is bump mapping used for current shape. |
| 60 |
Fully calculated only during render, after BumpMappingAllowed is calculated |
| 61 |
and after textures are applied. |
| 62 |
This is determined by BumpMappingAllowed, |
| 63 |
global BumpMapping, and by the texture information for current |
| 64 |
shape (whether user provided normal map, height map etc.) } |
| 65 |
BumpMappingUsed: Boolean; |
| 66 |
|
| 67 |
{ Along with Shape.BumpMappingUsed, this is calculated (use only if Shape.BumpMappingUsed). } |
| 68 |
BumpMappingTextureCoordinatesId: Cardinal; |
| 69 |
|
| 70 |
destructor Destroy; override; |
| 71 |
end; |
| 72 |
{$endif read_interface} |
| 73 |
|
| 74 |
{$ifdef read_implementation} |
| 75 |
|
| 76 |
{ Sometimes the updates indicated by Shape.Cache.VboToReload can be performed without |
| 77 |
actually recreating the Shape.Cache.Arrays (so no need to create TArraysGenerator |
| 78 |
and call TArraysGenerator.GenerateArrays). |
| 79 |
Detect and perform such optimized case. |
| 80 |
|
| 81 |
This is crucial for skinned animation, that only requires updating coords/normals/tangents |
| 82 |
each frame, no need for updating other attributes. } |
| 83 |
function FastUpdateArrays(const Shape: TX3DRendererShape): Boolean; |
| 84 |
|
| 85 |
{ If possible, update the coordinate/normal/tangent data in VBO fast. |
| 86 |
|
| 87 |
This is carefully implemented to do a specific case of TShapeCache.LoadArraysToVbo. |
| 88 |
It optimizes the case of animating coordinates/normals/tangents using CoordinateInterpolator, |
| 89 |
which is very important to optimize, since that's how glTF skinned animations |
| 90 |
are done. |
| 91 |
|
| 92 |
Pass non-nil Tangents if VBO should have tangent data, |
| 93 |
pass nil Tangents if VBO should not have it. } |
| 94 |
function FastCoordinateNormalUpdate(const Cache: TShapeCache; |
| 95 |
const Coords, Normals, Tangents: TVector3List): Boolean; |
| 96 |
var |
| 97 |
NewCoordinates, NewCoord: Pointer; |
| 98 |
Count, Size, ItemSize: Cardinal; |
| 99 |
I: Integer; |
| 100 |
begin |
| 101 |
Result := false; |
| 102 |
|
| 103 |
if Cache.VboCoordinatePreserveGeometryOrder and |
| 104 |
(Normals.Count = Coords.Count) and |
| 105 |
( (Tangents = nil) or |
| 106 |
(Tangents.Count = Coords.Count) ) then |
| 107 |
begin |
| 108 |
Count := Coords.Count; |
| 109 |
|
| 110 |
ItemSize := SizeOf(TVector3) * 2; |
| 111 |
if Tangents <> nil then |
| 112 |
ItemSize := ItemSize + SizeOf(TVector3); |
| 113 |
Size := Count * ItemSize; |
| 114 |
|
| 115 |
if (Cache.VboAllocatedUsage = GL_STREAM_DRAW) and |
| 116 |
{ Comparing the byte sizes also makes sure that previous and new coordinates |
| 117 |
count stayed the same. |
| 118 |
(Well, assuming we didn't change the Count, and simultaneously changed |
| 119 |
the Tangent existence, which in theory could cause Size to match |
| 120 |
for different structures.) |
| 121 |
} |
| 122 |
(Cache.VboAllocatedSize[vtCoordinate] = Size) then |
| 123 |
begin |
| 124 |
Result := true; |
| 125 |
Cache.VboToReload := Cache.VboToReload - [vtCoordinate]; // vtCoordinate are done |
| 126 |
if Count <> 0 then |
| 127 |
begin |
| 128 |
// calculate NewCoordinates |
| 129 |
NewCoordinates := GetMem(Size); |
| 130 |
try |
| 131 |
NewCoord := NewCoordinates; |
| 132 |
|
| 133 |
if Tangents <> nil then |
| 134 |
begin |
| 135 |
for I := 0 to Count - 1 do |
| 136 |
begin |
| 137 |
PVector3(NewCoord)^ := Coords.L[I]; |
| 138 |
PtrUInt(NewCoord) := PtrUInt(NewCoord) + SizeOf(TVector3); |
| 139 |
|
| 140 |
PVector3(NewCoord)^ := Normals.L[I]; |
| 141 |
PtrUInt(NewCoord) := PtrUInt(NewCoord) + SizeOf(TVector3); |
| 142 |
|
| 143 |
PVector3(NewCoord)^ := Tangents.L[I]; |
| 144 |
PtrUInt(NewCoord) := PtrUInt(NewCoord) + SizeOf(TVector3); |
| 145 |
end; |
| 146 |
end else |
| 147 |
begin |
| 148 |
for I := 0 to Count - 1 do |
| 149 |
begin |
| 150 |
PVector3(NewCoord)^ := Coords.L[I]; |
| 151 |
PtrUInt(NewCoord) := PtrUInt(NewCoord) + SizeOf(TVector3); |
| 152 |
|
| 153 |
PVector3(NewCoord)^ := Normals.L[I]; |
| 154 |
PtrUInt(NewCoord) := PtrUInt(NewCoord) + SizeOf(TVector3); |
| 155 |
end; |
| 156 |
end; |
| 157 |
|
| 158 |
// load NewCoordinates to GPU |
| 159 |
RenderContext.BindBuffer[btArray] := Cache.Vbo[vtCoordinate]; |
| 160 |
glBufferSubData(GL_ARRAY_BUFFER, 0, Size, NewCoordinates); |
| 161 |
finally FreeMemNiling(NewCoordinates) end; |
| 162 |
end; |
| 163 |
end; |
| 164 |
end; |
| 165 |
end; |
| 166 |
|
| 167 |
var |
| 168 |
Cache: TShapeCache; |
| 169 |
Coords, Normals, Tangents: TVector3List; |
| 170 |
begin |
| 171 |
Result := false; |
| 172 |
|
| 173 |
Cache := Shape.Cache; |
| 174 |
|
| 175 |
// this only optimizes the specific case of VboToReload = [vtCoordinate] now |
| 176 |
if Cache.VboToReload <> [vtCoordinate] then |
| 177 |
Exit; |
| 178 |
|
| 179 |
{ When VBO of coordinates is 0 then |
| 180 |
- VBOs not supported (ancient GPU) -> we need to really update Arrays contents each time |
| 181 |
- or VBO of coordinates was not initialized yet } |
| 182 |
if Cache.Vbo[vtCoordinate] = 0 then |
| 183 |
Exit; |
| 184 |
if Cache.Arrays = nil then // not initialized yet |
| 185 |
Exit; |
| 186 |
|
| 187 |
if // Shape has coordinates expressed in simple way |
| 188 |
(Shape.Geometry.CoordField <> nil) and |
| 189 |
(Shape.Geometry.CoordField.Value is TCoordinateNode) then // checks also Value <> nil |
| 190 |
begin |
| 191 |
Coords := TCoordinateNode(Shape.Geometry.CoordField.Value).FdPoint.Items; |
| 192 |
|
| 193 |
if // Shape has normals expressed in simple way |
| 194 |
(Shape.Geometry.NormalField <> nil) and |
| 195 |
(Shape.Geometry.NormalField.Value is TNormalNode) then // checks also Value <> nil |
| 196 |
begin |
| 197 |
Normals := TNormalNode(Shape.Geometry.NormalField.Value).FdVector.Items; |
| 198 |
|
| 199 |
// Shape has normals expressed in tangents way, or doesn't use tangents (when no bump mapping used) |
| 200 |
if ( (not Cache.Arrays.HasTangent) or |
| 201 |
( (Shape.Geometry.TangentField <> nil) and |
| 202 |
(Shape.Geometry.TangentField.Value is TTangentNode) ) ) then // checks also Value <> nil |
| 203 |
begin |
| 204 |
if Cache.Arrays.HasTangent then |
| 205 |
Tangents := TTangentNode(Shape.Geometry.TangentField.Value).FdVector.Items |
| 206 |
else |
| 207 |
Tangents := nil; |
| 208 |
|
| 209 |
Result := FastCoordinateNormalUpdate(Cache, Coords, Normals, Tangents); |
| 210 |
end; |
| 211 |
end; |
| 212 |
end; |
| 213 |
end; |
| 214 |
|
| 215 |
{ TX3DRendererShape --------------------------------------------------------- } |
| 216 |
|
| 217 |
destructor TX3DRendererShape.Destroy; |
| 218 |
begin |
| 219 |
FreeAndNil(MirrorPlaneUniforms); |
| 220 |
inherited; |
| 221 |
end; |
| 222 |
|
| 223 |
procedure TX3DRendererShape.LoadArraysToVbo; |
| 224 |
begin |
| 225 |
Assert(Cache <> nil); |
| 226 |
Cache.LoadArraysToVbo(DynamicGeometry); |
| 227 |
end; |
| 228 |
|
| 229 |
{$endif} |