| 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 |
{ Types used by TRendererContextCache. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
//type |
| 21 |
TTextureImageCache = class |
| 22 |
{ Full URL of used texture image. Empty ('') if not known |
| 23 |
(or maybe this texture didn't come from any URL, e.g. it's generated). } |
| 24 |
FullUrl: String; |
| 25 |
FlipVertically: Boolean; |
| 26 |
Filter: TTextureFilter; |
| 27 |
Anisotropy: TGLfloat; |
| 28 |
Wrap: TTextureWrap2D; |
| 29 |
GUITexture: boolean; |
| 30 |
References: Cardinal; |
| 31 |
GLName: TGLuint; |
| 32 |
end; |
| 33 |
TTextureImageCacheList = {$ifdef FPC}specialize{$endif} TObjectList<TTextureImageCache>; |
| 34 |
|
| 35 |
TTextureVideoCache = class |
| 36 |
{ Full URL of used texture image. Empty ('') if not known |
| 37 |
(or maybe this texture didn't come from any URL, e.g. it's generated). } |
| 38 |
FullUrl: String; |
| 39 |
FlipVertically: Boolean; |
| 40 |
Filter: TTextureFilter; |
| 41 |
Anisotropy: TGLfloat; |
| 42 |
Wrap: TTextureWrap2D; |
| 43 |
GUITexture: boolean; |
| 44 |
References: Cardinal; |
| 45 |
GLVideo: TGLVideo3D; |
| 46 |
end; |
| 47 |
TTextureVideoCacheList = {$ifdef FPC}specialize{$endif} TObjectList<TTextureVideoCache>; |
| 48 |
|
| 49 |
TTextureCubeMapCache = class |
| 50 |
{ Full URL of used texture image. Empty ('') if not known |
| 51 |
(or maybe this texture didn't come from any URL, e.g. it's generated). } |
| 52 |
FullUrl: String; |
| 53 |
Filter: TTextureFilter; |
| 54 |
Anisotropy: TGLfloat; |
| 55 |
References: Cardinal; |
| 56 |
GLName: TGLuint; |
| 57 |
end; |
| 58 |
TTextureCubeMapCacheList = {$ifdef FPC}specialize{$endif} TObjectList<TTextureCubeMapCache>; |
| 59 |
|
| 60 |
TTexture3DCache = class |
| 61 |
{ Full URL of used texture image. Empty ('') if not known |
| 62 |
(or maybe this texture didn't come from any URL, e.g. it's generated). } |
| 63 |
FullUrl: String; |
| 64 |
Filter: TTextureFilter; |
| 65 |
Anisotropy: TGLfloat; |
| 66 |
Wrap: TTextureWrap3D; |
| 67 |
References: Cardinal; |
| 68 |
GLName: TGLuint; |
| 69 |
end; |
| 70 |
TTexture3DCacheList = {$ifdef FPC}specialize{$endif} TObjectList<TTexture3DCache>; |
| 71 |
|
| 72 |
{ Cached depth or float texture. |
| 73 |
For now, depth and float textures require the same fields. } |
| 74 |
TTextureDepthOrFloatCache = class |
| 75 |
{ Full URL of used texture image. Empty ('') if not known |
| 76 |
(or maybe this texture didn't come from any URL, e.g. it's generated). } |
| 77 |
FullUrl: String; |
| 78 |
Wrap: TTextureWrap2D; |
| 79 |
References: Cardinal; |
| 80 |
GLName: TGLuint; |
| 81 |
end; |
| 82 |
TTextureDepthOrFloatCacheList = {$ifdef FPC}specialize{$endif} TObjectList<TTextureDepthOrFloatCache>; |
| 83 |
|
| 84 |
TX3DRendererShape = class; |
| 85 |
TVboType = (vtCoordinate, vtAttribute, vtIndex); |
| 86 |
TVboTypes = set of TVboType; |
| 87 |
TVboArrays = array [TVboType] of TGLuint; |
| 88 |
|
| 89 |
{ Cached shape resources. } |
| 90 |
TShapeCache = class |
| 91 |
private |
| 92 |
RenderOptions: TCastleRenderOptions; |
| 93 |
Geometry: TAbstractGeometryNode; |
| 94 |
State: TX3DGraphTraverseState; |
| 95 |
FogVolumetric: boolean; |
| 96 |
FogVolumetricDirection: TVector3; |
| 97 |
FogVolumetricVisibilityStart: Single; |
| 98 |
References: Cardinal; |
| 99 |
|
| 100 |
{ An instance of TGeometryArrays, decomposing this shape geometry. |
| 101 |
Used to easily render and process this geometry. |
| 102 |
|
| 103 |
When VBO are supported (all non-ancient GPUs) then TShapeCache |
| 104 |
uses an instance of it only once (in LoadArraysToVbo, |
| 105 |
to load contents to GPU, and then calls Arrays.FreeData). |
| 106 |
But it is later used by outside code multiple times to make draw calls |
| 107 |
(as it is passed by TBaseCoordinateRenderer). |
| 108 |
So storing it in TShapeCache (to be shared when possible) makes sense. |
| 109 |
|
| 110 |
TShapeCache owns this -- it will be freed when TShapeCache is freed. } |
| 111 |
Arrays: TGeometryArrays; |
| 112 |
|
| 113 |
{ What VBOs do we need to reload at next LoadArraysToVbo call. |
| 114 |
This also implies that content for this VBOs needs to be created, |
| 115 |
which may mean we need to change Arrays. |
| 116 |
This is extended at each InvalidateVertexData call. } |
| 117 |
VboToReload: TVboTypes; |
| 118 |
|
| 119 |
Vbo: TVboArrays; |
| 120 |
VboAllocatedUsage: TGLenum; |
| 121 |
VboAllocatedSize: array [TVboType] of Cardinal; |
| 122 |
VboCoordinatePreserveGeometryOrder: Boolean; //< copied from TGeometryArrays.CoordinatePreserveGeometryOrder |
| 123 |
|
| 124 |
Vao: TVertexArrayObject; |
| 125 |
|
| 126 |
{ Like TX3DRendererShape.LoadArraysToVbo, |
| 127 |
but takes explicit DynamicGeometry. } |
| 128 |
procedure LoadArraysToVbo(const DynamicGeometry: boolean); |
| 129 |
procedure GLContextClose; |
| 130 |
public |
| 131 |
constructor Create; |
| 132 |
destructor Destroy; override; |
| 133 |
procedure InvalidateVertexData(const Changed: TVboTypes); |
| 134 |
{ Debug description of this shape cache. } |
| 135 |
function ToString: String; override; |
| 136 |
end; |
| 137 |
|
| 138 |
TShapeCacheList = {$ifdef FPC}specialize{$endif} TObjectList<TShapeCache>; |
| 139 |
|
| 140 |
TX3DGLSLProgram = class; |
| 141 |
|
| 142 |
TShaderProgramCache = class |
| 143 |
public |
| 144 |
{ Hash of TShader code when initializing this shader |
| 145 |
by LinkProgram. Used to decide when shader needs to be regenerated, |
| 146 |
and when it can be shared. } |
| 147 |
Hash: TShaderCodeHash; |
| 148 |
|
| 149 |
{ Actual GLSL program. May be @nil (if it failed to link). } |
| 150 |
ShaderProgram: TX3DGLSLProgram; |
| 151 |
|
| 152 |
References: Cardinal; |
| 153 |
|
| 154 |
destructor Destroy; override; |
| 155 |
end; |
| 156 |
|
| 157 |
TShaderProgramCacheList = {$ifdef FPC}specialize{$endif} TObjectList<TShaderProgramCache>; |
| 158 |
{$endif read_interface} |
| 159 |
|
| 160 |
{$ifdef read_implementation} |
| 161 |
|
| 162 |
{ TShapeCache ---------------------------------------------------------------- } |
| 163 |
|
| 164 |
constructor TShapeCache.Create; |
| 165 |
begin |
| 166 |
inherited; |
| 167 |
VboToReload := AllVboTypes; |
| 168 |
end; |
| 169 |
|
| 170 |
destructor TShapeCache.Destroy; |
| 171 |
begin |
| 172 |
FreeAndNil(Arrays); |
| 173 |
GLContextClose; |
| 174 |
inherited; |
| 175 |
end; |
| 176 |
|
| 177 |
procedure TShapeCache.GLContextClose; |
| 178 |
var |
| 179 |
I: TVboType; |
| 180 |
begin |
| 181 |
if Vbo[vtCoordinate] <> 0 then |
| 182 |
begin |
| 183 |
{ All Vbo must be zero, or none. } |
| 184 |
for I := Low(I) to High(I) do |
| 185 |
Assert(Vbo[I] <> 0); |
| 186 |
|
| 187 |
glDeleteBuffers(Ord(High(Vbo)) + 1, @Vbo); |
| 188 |
|
| 189 |
for I := Low(I) to High(I) do |
| 190 |
Vbo[I] := 0; |
| 191 |
end; |
| 192 |
|
| 193 |
FreeAndNil(Vao); |
| 194 |
end; |
| 195 |
|
| 196 |
procedure TShapeCache.InvalidateVertexData(const Changed: TVboTypes); |
| 197 |
begin |
| 198 |
VboToReload := VboToReload + Changed; |
| 199 |
end; |
| 200 |
|
| 201 |
procedure TShapeCache.LoadArraysToVbo(const DynamicGeometry: boolean); |
| 202 |
var |
| 203 |
DataUsage: TGLenum; |
| 204 |
NewVbos: boolean; |
| 205 |
|
| 206 |
{ Bind Vbo buffer and load data. Updates AllocatedSize. |
| 207 |
|
| 208 |
Uses glBufferSubData if possible, as it may be faster than glBufferData |
| 209 |
(not confirmed by tests, but OpenGL docs suggest it: |
| 210 |
https://registry.khronos.org/OpenGL-Refpages/gl4/html/glBufferSubData.xhtml |
| 211 |
"When replacing the entire data store, consider using glBufferSubData |
| 212 |
rather than completely recreating the data store with glBufferData. |
| 213 |
This avoids the cost of reallocating the data store. " ). } |
| 214 |
procedure BufferData(const VboType: TVboType; |
| 215 |
const Target: TBufferTarget; const Size: Cardinal; const Data: Pointer); |
| 216 |
var |
| 217 |
TargetGL: TGLenum; |
| 218 |
begin |
| 219 |
{ It may happen that Data = nil, because Arrays.AttributeSize may be 0 |
| 220 |
(some shapes just don't need extra per-vertex attributes). |
| 221 |
|
| 222 |
We should not proceed then. |
| 223 |
Passing Data = nil to glBufferSubData can make actual bugs: |
| 224 |
see the testcase from |
| 225 |
https://github.com/castle-engine/castle-engine/issues/389 : |
| 226 |
open dragon.json, turn on "Dynamic Batching", run some animation |
| 227 |
-- it will update VBOs each frame, and on at least some systems |
| 228 |
(confirmed on 3 systems with NVidia on Windows) it will cause |
| 229 |
weird problems (like unrelated buffers are trashed with garbage). |
| 230 |
Strangely glBufferData doesn't have a problem with Data = nil, |
| 231 |
but we also avoid it. } |
| 232 |
if Data = nil then |
| 233 |
begin |
| 234 |
Assert(Size = 0); |
| 235 |
Exit; |
| 236 |
end; |
| 237 |
|
| 238 |
if NewVbos or |
| 239 |
(VboType in VboToReload) or |
| 240 |
{ In normal circumstances, when vbo is already loaded, |
| 241 |
it should have already the appropriate size. But through VRML/X3D |
| 242 |
events, user may be able to actually incorrectly change |
| 243 |
coordinates, such that new ones have different size than the old ones |
| 244 |
--- in this case, VboToReload optimization fails, and we have |
| 245 |
to reload data (or we'll get terrible OpenGL segfaults later, |
| 246 |
as it tries to access non-existent data from vertex arrays). } |
| 247 |
(VboAllocatedSize[VboType] <> Size) then |
| 248 |
begin |
| 249 |
RenderContext.BindBuffer[Target] := Vbo[VboType]; |
| 250 |
TargetGL := BufferTargetGL[Target]; |
| 251 |
if NewVbos or |
| 252 |
(VboAllocatedUsage <> DataUsage) or |
| 253 |
(VboAllocatedSize[VboType] <> Size) then |
| 254 |
begin |
| 255 |
glBufferData(TargetGL, Size, Data, DataUsage); |
| 256 |
VboAllocatedSize[VboType] := Size; |
| 257 |
end else |
| 258 |
glBufferSubData(TargetGL, 0, Size, Data); |
| 259 |
end; |
| 260 |
end; |
| 261 |
|
| 262 |
function VboTypesToStr(const VboTypes: TVboTypes): string; |
| 263 |
const |
| 264 |
Names: array [TVboType] of string = |
| 265 |
( 'Coordinate', 'Attribute', 'Index' ); |
| 266 |
var |
| 267 |
I: TVboType; |
| 268 |
begin |
| 269 |
Result := ''; |
| 270 |
for I := Low(I) to High(I) do |
| 271 |
if I in VboTypes then |
| 272 |
begin |
| 273 |
if Result <> '' then Result := Result + ','; |
| 274 |
Result := Result + Names[I]; |
| 275 |
end; |
| 276 |
Result := '[' + Result + ']'; |
| 277 |
end; |
| 278 |
|
| 279 |
begin |
| 280 |
if GLFeatures.VertexBufferObject then |
| 281 |
begin |
| 282 |
{ In case of VBO not supported (ancient GPUs), we do not free data in Arrays, |
| 283 |
it will be used to provide data at each draw call. } |
| 284 |
Assert(not Arrays.DataFreed); |
| 285 |
|
| 286 |
NewVbos := Vbo[vtCoordinate] = 0; |
| 287 |
if NewVbos then |
| 288 |
begin |
| 289 |
glGenBuffers(Ord(High(Vbo)) + 1, @Vbo); |
| 290 |
if LogRenderer then |
| 291 |
WritelnLog('Renderer', Format('Creating and loading data to VBOs (%d,%d,%d)', |
| 292 |
[Vbo[vtCoordinate], Vbo[vtAttribute], Vbo[vtIndex]])); |
| 293 |
end else |
| 294 |
begin |
| 295 |
if LogRenderer then |
| 296 |
WritelnLog('Renderer', Format('Loading data to existing VBOs (%d,%d,%d), reloading %s', |
| 297 |
[Vbo[vtCoordinate], Vbo[vtAttribute], Vbo[vtIndex], |
| 298 |
VboTypesToStr(VboToReload)])); |
| 299 |
end; |
| 300 |
|
| 301 |
if DynamicGeometry then |
| 302 |
{ GL_STREAM_DRAW is most suitable if we will modify this every frame, |
| 303 |
according to |
| 304 |
https://www.khronos.org/opengl/wiki/Buffer_Object |
| 305 |
https://computergraphics.stackexchange.com/questions/5712/gl-static-draw-vs-gl-dynamic-draw-vs-gl-stream-draw-does-it-matter |
| 306 |
} |
| 307 |
DataUsage := GL_STREAM_DRAW |
| 308 |
else |
| 309 |
DataUsage := GL_STATIC_DRAW; |
| 310 |
|
| 311 |
VboCoordinatePreserveGeometryOrder := Arrays.CoordinatePreserveGeometryOrder; |
| 312 |
|
| 313 |
BufferData(vtCoordinate, btArray, |
| 314 |
Arrays.Count * Arrays.CoordinateSize, Arrays.CoordinateArray); |
| 315 |
|
| 316 |
BufferData(vtAttribute, btArray, |
| 317 |
Arrays.Count * Arrays.AttributeSize, Arrays.AttributeArray); |
| 318 |
|
| 319 |
if Arrays.Indexes <> nil then |
| 320 |
BufferData(vtIndex, btElementArray, |
| 321 |
Arrays.Indexes.Count * SizeOf(TGeometryIndex), Arrays.Indexes.L); |
| 322 |
|
| 323 |
VboAllocatedUsage := DataUsage; |
| 324 |
|
| 325 |
Arrays.FreeData; |
| 326 |
|
| 327 |
if GLFeatures.VertexArrayObject and (Vao = nil) then |
| 328 |
Vao := TVertexArrayObject.Create(nil); |
| 329 |
end; |
| 330 |
|
| 331 |
VboToReload := []; |
| 332 |
end; |
| 333 |
|
| 334 |
function TShapeCache.ToString: String; |
| 335 |
var |
| 336 |
ShapeNodeName: String; |
| 337 |
begin |
| 338 |
if State.ShapeNode <> nil then |
| 339 |
ShapeNodeName := ' ' + State.ShapeNode.X3DName |
| 340 |
else |
| 341 |
ShapeNodeName := ''; |
| 342 |
Result := Format('Shape%s %s (%s): %d', [ |
| 343 |
ShapeNodeName, |
| 344 |
PointerToStr(Self), |
| 345 |
Geometry.X3DType, |
| 346 |
References |
| 347 |
]); |
| 348 |
end; |
| 349 |
|
| 350 |
{ TShaderProgramCache -------------------------------------------------------- } |
| 351 |
|
| 352 |
destructor TShaderProgramCache.Destroy; |
| 353 |
begin |
| 354 |
FreeAndNil(ShaderProgram); |
| 355 |
inherited; |
| 356 |
end; |
| 357 |
|
| 358 |
{$endif} |