| 1 |
{ |
| 2 |
Copyright 2003-2024 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Render shapes, possibly coming from different TCastleScene instances. } |
| 17 |
unit CastleInternalShapesRenderer; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleVectors, CastleSceneInternalShape, CastleRenderOptions, |
| 24 |
CastleInternalOcclusionCulling, CastleSceneInternalBlending, |
| 25 |
CastleInternalBatchShapes, CastleInternalRenderer, CastleTransform, |
| 26 |
X3DNodes, CastleShapes; |
| 27 |
|
| 28 |
type |
| 29 |
{ Collect shapes, possibly coming from different TCastleScene instances, |
| 30 |
to render them together using TShapesRenderer. } |
| 31 |
TShapesCollector = class |
| 32 |
private |
| 33 |
FCollected: TCollectedShapeList; |
| 34 |
public |
| 35 |
constructor Create; |
| 36 |
destructor Destroy; override; |
| 37 |
|
| 38 |
{ Clear shapes to render. } |
| 39 |
procedure Clear; |
| 40 |
|
| 41 |
{ Add a shape to render. } |
| 42 |
procedure Add(const Shape: TGLShape; const RenderOptions: TCastleRenderOptions; |
| 43 |
const SceneTransform: TMatrix4; const DepthRange: TDepthRange); |
| 44 |
end; |
| 45 |
|
| 46 |
{ Render collected shapes. |
| 47 |
Applies cross-scene shape optimizations and features, |
| 48 |
like dynamic batching (so shapes from different TCastleScene instances |
| 49 |
may be batched too), |
| 50 |
and sorting shapes for blending (so shapes from different TCastleScene instances |
| 51 |
are sorted with each other). } |
| 52 |
TShapesRenderer = class |
| 53 |
strict private |
| 54 |
FOcclusionCullingRenderer: TOcclusionCullingRenderer; |
| 55 |
FBlendingRenderer: TBlendingRenderer; |
| 56 |
FRenderer: TRenderer; |
| 57 |
FBatching: TBatchShapes; |
| 58 |
FDynamicBatching: Boolean; |
| 59 |
FOcclusionCulling: Boolean; |
| 60 |
FOcclusionSort, FBlendingSort: TShapeSortNoAuto; |
| 61 |
FOnCustomShapeSort: TShapeSortEvent; |
| 62 |
|
| 63 |
{ Checks we need DynamicBatching and we don't use occlusion culling. } |
| 64 |
function EffectiveDynamicBatching: Boolean; |
| 65 |
|
| 66 |
{ Checks we need occlusion culling, and GPU has capability for it. } |
| 67 |
function EffectiveOcclusionCulling: Boolean; |
| 68 |
|
| 69 |
{ Created on demand TBatchShapes instance. |
| 70 |
Use only if EffectiveDynamicBatching to not create needlessly. } |
| 71 |
function Batching: TBatchShapes; |
| 72 |
|
| 73 |
{ Like LightRender, additionally turn off lights that are not |
| 74 |
supposed to light in the shadow (for shadow volumes). |
| 75 |
This simply turns LightOn to @false if the light has |
| 76 |
shadowVolumes = TRUE (see |
| 77 |
[https://castle-engine.io/x3d_extensions.php#section_ext_shadows]). |
| 78 |
|
| 79 |
It's useful to pass this as LightRenderEvent to @link(Render) |
| 80 |
when you use shadow algorithm that requires |
| 81 |
you to make a first pass rendering the scene all shadowed. } |
| 82 |
procedure LightRenderInShadow(const Shape: TShape; |
| 83 |
const Light: TLightInstance; |
| 84 |
const IsGlobalLight: Boolean; var LightOn: boolean); |
| 85 |
|
| 86 |
{ Turn off global lights that are duplicated in current scene. |
| 87 |
This way we render our own lights through SceneLights at each shape, |
| 88 |
not through GlobalLights, |
| 89 |
and so they work regardless of TCastleScene.CastGlobalLights |
| 90 |
and RenderOptions.ReceiveGlobalLights, |
| 91 |
and are controled by RenderOptions.ReceiveSceneLights. } |
| 92 |
procedure LightRender(const Shape: TShape; |
| 93 |
const Light: TLightInstance; |
| 94 |
const IsGlobalLight: Boolean; var LightOn: boolean); |
| 95 |
|
| 96 |
procedure SetOcclusionCulling(const Value: Boolean); |
| 97 |
|
| 98 |
procedure RenderShape_NoTests( |
| 99 |
const CollectedShape: TCollectedShape; const Params: TRenderParams); |
| 100 |
procedure RenderShape_OcclusionTests( |
| 101 |
const CollectedShape: TCollectedShape; const Params: TRenderParams); |
| 102 |
public |
| 103 |
constructor Create; |
| 104 |
destructor Destroy; override; |
| 105 |
|
| 106 |
procedure PrepareResources; |
| 107 |
procedure GLContextClose; |
| 108 |
|
| 109 |
{ Shapes can use this to make a "dummy render" to prepare |
| 110 |
for future rendering. } |
| 111 |
property Renderer: TRenderer read FRenderer; |
| 112 |
|
| 113 |
{ Render all given shapes. |
| 114 |
|
| 115 |
Note: Params.Transformation may be ignored by this routine. |
| 116 |
They should be set to indicate identity. |
| 117 |
They are meaningless here: we have scene transformation in each collected |
| 118 |
shape. |
| 119 |
Same for Params.DepthRange (we follow this on shape now), |
| 120 |
|
| 121 |
TODO: Make sure they are ignored, assert they are identity. |
| 122 |
|
| 123 |
TODO: Split TRenderParams into stuff needed at collection, |
| 124 |
and needed at rendering collecting shapes. |
| 125 |
Transformation:TTransformation should be only in former. } |
| 126 |
procedure Render(const Shapes: TShapesCollector; |
| 127 |
const Params: TRenderParams); |
| 128 |
|
| 129 |
{ Combine (right before rendering) multiple shapes with a similar appearance into one. |
| 130 |
This can drastically reduce the number of "draw calls", |
| 131 |
making rendering much faster. } |
| 132 |
property DynamicBatching: Boolean |
| 133 |
read FDynamicBatching write FDynamicBatching default false; |
| 134 |
|
| 135 |
{ Use the occlusion culling to optimize the rendering. |
| 136 |
The shapes obscured by other shapes will not be rendered. |
| 137 |
This makes sense when in your view, many shapes are typically obscured by others. |
| 138 |
|
| 139 |
See the https://castle-engine.io/occlusion_culling |
| 140 |
for details how does this work. |
| 141 |
|
| 142 |
This is ignored if GPU doesn't support the necessary functionality |
| 143 |
(@link(TGLFeatures.OcclusionQuery)). } |
| 144 |
property OcclusionCulling: boolean |
| 145 |
read FOcclusionCulling write SetOcclusionCulling default false; |
| 146 |
|
| 147 |
{ Sort the opaque shapes when rendering, from front to back. |
| 148 |
@seealso TCastleViewport.OcclusionSort } |
| 149 |
property OcclusionSort: TShapeSortNoAuto |
| 150 |
read FOcclusionSort write FOcclusionSort default sortNone; |
| 151 |
|
| 152 |
{ Sort the blending (partially-transparent) shapes when rendering, |
| 153 |
from back to front. |
| 154 |
@seealso TCastleViewport.BlendingSort } |
| 155 |
property BlendingSort: TShapeSortNoAuto |
| 156 |
read FBlendingSort write FBlendingSort default sortNone; |
| 157 |
|
| 158 |
{ Used to sort shapes, if @link(TCastleViewport.BlendingSort) or |
| 159 |
@link(TCastleViewport.OcclusionSort) indicate sortCustom. |
| 160 |
@seealso TCastleViewport.OnCustomShapeSort } |
| 161 |
property OnCustomShapeSort: TShapeSortEvent |
| 162 |
read FOnCustomShapeSort write FOnCustomShapeSort; |
| 163 |
end; |
| 164 |
|
| 165 |
implementation |
| 166 |
|
| 167 |
uses SysUtils, |
| 168 |
{$ifdef OpenGLES} CastleGLES, {$else} CastleGL, {$endif} |
| 169 |
CastleScene, CastleGLUtils, CastleRenderContext, CastleColors, CastleUtils, |
| 170 |
X3DCameraUtils, CastleTimeUtils; |
| 171 |
|
| 172 |
{ TShapesCollector ----------------------------------------------------------- } |
| 173 |
|
| 174 |
constructor TShapesCollector.Create; |
| 175 |
begin |
| 176 |
inherited; |
| 177 |
FCollected := TCollectedShapeList.Create(true); |
| 178 |
end; |
| 179 |
|
| 180 |
destructor TShapesCollector.Destroy; |
| 181 |
begin |
| 182 |
FreeAndNil(FCollected); |
| 183 |
inherited; |
| 184 |
end; |
| 185 |
|
| 186 |
procedure TShapesCollector.Clear; |
| 187 |
begin |
| 188 |
FCollected.Clear; |
| 189 |
end; |
| 190 |
|
| 191 |
procedure TShapesCollector.Add(const Shape: TGLShape; |
| 192 |
const RenderOptions: TCastleRenderOptions; |
| 193 |
const SceneTransform: TMatrix4; |
| 194 |
const DepthRange: TDepthRange); |
| 195 |
var |
| 196 |
NewCollected: TCollectedShape; |
| 197 |
begin |
| 198 |
NewCollected := TCollectedShape.Create; |
| 199 |
NewCollected.Shape := Shape; |
| 200 |
NewCollected.RenderOptions := RenderOptions; |
| 201 |
NewCollected.SceneTransform := SceneTransform; |
| 202 |
NewCollected.DepthRange := DepthRange; |
| 203 |
FCollected.Add(NewCollected); |
| 204 |
end; |
| 205 |
|
| 206 |
{ TShapesRenderer ----------------------------------------------------------- } |
| 207 |
|
| 208 |
constructor TShapesRenderer.Create; |
| 209 |
begin |
| 210 |
inherited; |
| 211 |
FOcclusionCullingRenderer := TOcclusionCullingRenderer.Create; |
| 212 |
FBlendingRenderer := TBlendingRenderer.Create; |
| 213 |
FRenderer := TRenderer.Create(nil); |
| 214 |
FOcclusionSort := sortNone; |
| 215 |
FBlendingSort := sortNone; |
| 216 |
end; |
| 217 |
|
| 218 |
destructor TShapesRenderer.Destroy; |
| 219 |
begin |
| 220 |
FreeAndNil(FOcclusionCullingRenderer); |
| 221 |
FreeAndNil(FBlendingRenderer); |
| 222 |
FreeAndNil(FRenderer); |
| 223 |
FreeAndNil(FBatching); |
| 224 |
inherited; |
| 225 |
end; |
| 226 |
|
| 227 |
procedure TShapesRenderer.PrepareResources; |
| 228 |
|
| 229 |
{ Call TGLShape.PrepareResources for shapes in "batching pool". } |
| 230 |
procedure BatchingShapesPrepareResources; |
| 231 |
var |
| 232 |
I: Integer; |
| 233 |
DummyRenderOptions: TCastleRenderOptions; |
| 234 |
NeedsRenderOptions: Boolean; |
| 235 |
begin |
| 236 |
// TODO: should not be required here? |
| 237 |
DummyRenderOptions := TCastleRenderOptions.Create(nil); |
| 238 |
try |
| 239 |
for I := 0 to Batching.PoolShapesCount - 1 do |
| 240 |
begin |
| 241 |
Assert(Batching.PoolShapes[I] <> nil); |
| 242 |
NeedsRenderOptions := Batching.PoolShapes[I].OverrideRenderOptions = nil; |
| 243 |
{ We need to set OverrideRenderOptions, |
| 244 |
otherwise the PoolShapes[I] would not have any RenderOptions |
| 245 |
instance, which is necessary to be prepared. } |
| 246 |
if NeedsRenderOptions then |
| 247 |
Batching.PoolShapes[I].OverrideRenderOptions := DummyRenderOptions; |
| 248 |
Batching.PoolShapes[I].PrepareResources; |
| 249 |
if NeedsRenderOptions then |
| 250 |
Batching.PoolShapes[I].OverrideRenderOptions := nil; |
| 251 |
end; |
| 252 |
finally FreeAndNil(DummyRenderOptions) end; |
| 253 |
end; |
| 254 |
|
| 255 |
{ Simulate rendering for shapes in "batching pool". } |
| 256 |
procedure BatchingShapesRender; |
| 257 |
var |
| 258 |
I: Integer; |
| 259 |
DummyStatistics: TRenderStatistics; |
| 260 |
DummyCamera: TRenderingCamera; |
| 261 |
DummyRenderOptions: TCastleRenderOptions; |
| 262 |
SavedRenderMode: TRenderer.TRenderMode; |
| 263 |
Shape: TGLShape; |
| 264 |
begin |
| 265 |
// TODO: should not be required for rendering here? |
| 266 |
DummyRenderOptions := TCastleRenderOptions.Create(nil); |
| 267 |
try |
| 268 |
SavedRenderMode := Renderer.RenderMode; |
| 269 |
Renderer.RenderMode := rmPrepareRenderSelf; |
| 270 |
|
| 271 |
DummyCamera := TRenderingCamera.Create; |
| 272 |
try |
| 273 |
DummyCamera.FromViewVectors(DefaultX3DCameraView, TMatrix4.Identity); |
| 274 |
|
| 275 |
Renderer.RenderBegin(nil, DummyCamera, nil, 0, 0, 0, @DummyStatistics); |
| 276 |
|
| 277 |
for I := 0 to Batching.PoolShapesCount - 1 do |
| 278 |
begin |
| 279 |
Shape := Batching.PoolShapes[I]; |
| 280 |
TGLShape(Shape).Fog := nil; |
| 281 |
Renderer.RenderShape(TGLShape(Shape), DummyRenderOptions, |
| 282 |
TMatrix4.Identity, drFull); |
| 283 |
end; |
| 284 |
|
| 285 |
Renderer.RenderEnd; |
| 286 |
finally FreeAndNil(DummyCamera) end; |
| 287 |
|
| 288 |
Renderer.RenderMode := SavedRenderMode; // restore Renderer.RenderMode |
| 289 |
finally FreeAndNil(DummyRenderOptions) end; |
| 290 |
end; |
| 291 |
|
| 292 |
begin |
| 293 |
if EffectiveDynamicBatching then |
| 294 |
begin |
| 295 |
BatchingShapesPrepareResources; |
| 296 |
BatchingShapesRender; |
| 297 |
end; |
| 298 |
end; |
| 299 |
|
| 300 |
procedure TShapesRenderer.GLContextClose; |
| 301 |
begin |
| 302 |
if FOcclusionCullingRenderer <> nil then |
| 303 |
FOcclusionCullingRenderer.Utils.GLContextClose; |
| 304 |
|
| 305 |
if FBatching <> nil then |
| 306 |
FBatching.GLContextClose; |
| 307 |
end; |
| 308 |
|
| 309 |
function TShapesRenderer.Batching: TBatchShapes; |
| 310 |
begin |
| 311 |
if FBatching = nil then |
| 312 |
FBatching := TBatchShapes.Create; |
| 313 |
Result := FBatching; |
| 314 |
end; |
| 315 |
|
| 316 |
procedure TShapesRenderer.LightRenderInShadow(const Shape: TShape; |
| 317 |
const Light: TLightInstance; |
| 318 |
const IsGlobalLight: Boolean; var LightOn: boolean); |
| 319 |
begin |
| 320 |
if Light.Node.FdShadowVolumes.Value then |
| 321 |
LightOn := false; |
| 322 |
LightRender(Shape, Light, IsGlobalLight, LightOn); |
| 323 |
end; |
| 324 |
|
| 325 |
procedure TShapesRenderer.LightRender(const Shape: TShape; |
| 326 |
const Light: TLightInstance; |
| 327 |
const IsGlobalLight: Boolean; var LightOn: boolean); |
| 328 |
begin |
| 329 |
if IsGlobalLight and |
| 330 |
(*Do not filter out headlight nodes, even if they belong to current scene. |
| 331 |
Headlight nodes are always considered "global lights" and are not present |
| 332 |
on our GlobalLights list, so we don't want to filter them out here. |
| 333 |
|
| 334 |
Testcase: castle-game, with "Tower" level that defines in basic_castle_final.x3dv |
| 335 |
headlight like this: |
| 336 |
|
| 337 |
NavigationInfo { |
| 338 |
headlight TRUE |
| 339 |
headlightNode DirectionalLight { |
| 340 |
... |
| 341 |
} |
| 342 |
} |
| 343 |
*) |
| 344 |
(not Light.Node.InternalHeadlight) and |
| 345 |
(Light.Node.Scene = Shape.ParentScene) then |
| 346 |
LightOn := false; |
| 347 |
end; |
| 348 |
|
| 349 |
procedure TShapesRenderer.RenderShape_NoTests( |
| 350 |
const CollectedShape: TCollectedShape; const Params: TRenderParams); |
| 351 |
var |
| 352 |
RenderOptions: TCastleRenderOptions; //< a shortcut for CollectedShape.RenderOptions |
| 353 |
Shape: TGLShape; //< a shortcut for CollectedShape.Shape |
| 354 |
|
| 355 |
procedure RenderNormal; |
| 356 |
var |
| 357 |
DepthRange: TDepthRange; |
| 358 |
begin |
| 359 |
{ Shadow maps require normal DepthRange, |
| 360 |
don't mess with DepthRange in case when we render shadow maps. } |
| 361 |
if Params.RenderingCamera.Target in [rtShadowMap, rtVarianceShadowMap] then |
| 362 |
DepthRange := drFull |
| 363 |
else |
| 364 |
DepthRange := CollectedShape.DepthRange; |
| 365 |
|
| 366 |
Renderer.RenderShape(Shape, |
| 367 |
RenderOptions, CollectedShape.SceneTransform, DepthRange); |
| 368 |
end; |
| 369 |
|
| 370 |
procedure RenderWireframe(UseWireframeColor: boolean); |
| 371 |
var |
| 372 |
SavedMode: TRenderingMode; |
| 373 |
SavedSolidColor: TCastleColorRGB; |
| 374 |
begin |
| 375 |
{$ifndef OpenGLES} // TODO-es For OpenGLES, wireframe must be done differently |
| 376 |
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
| 377 |
{$endif} |
| 378 |
|
| 379 |
if UseWireframeColor then |
| 380 |
begin |
| 381 |
SavedMode := RenderOptions.Mode; |
| 382 |
SavedSolidColor := RenderOptions.SolidColor; |
| 383 |
RenderOptions.Mode := rmSolidColor; |
| 384 |
RenderOptions.SolidColor := RenderOptions.WireframeColor; |
| 385 |
|
| 386 |
RenderNormal; |
| 387 |
|
| 388 |
RenderOptions.Mode := SavedMode; |
| 389 |
RenderOptions.SolidColor := SavedSolidColor; |
| 390 |
end else |
| 391 |
begin |
| 392 |
RenderNormal; |
| 393 |
end; |
| 394 |
|
| 395 |
{ We restore by just assuming that default mode is GL_FILL. |
| 396 |
Nothing else in CGE changes glPolygonMode for now, so this is trivially true. |
| 397 |
|
| 398 |
This way we avoid using glPushAttrib / glPopAttrib to save state. |
| 399 |
They are |
| 400 |
|
| 401 |
1. deprecated, |
| 402 |
2. using them would break RenderContext state knowledge, causing problems later. |
| 403 |
|
| 404 |
Testcase: |
| 405 |
- in CGE editor, |
| 406 |
- activate shadow volumes on 1 light, |
| 407 |
- add 2nd light, not casting shadows (maybe not needed to reproduce), |
| 408 |
- make plane larger 100x100 (maybe not needed to reproduce), |
| 409 |
- add sphere and box, |
| 410 |
- add on them sphere and box collider, |
| 411 |
- activate "Physics -> Show Colliders". |
| 412 |
|
| 413 |
Using glPushAttrib / glPopAttrib would break rendering, making some |
| 414 |
objects weirdly wireframe depending on what was last hovered-over |
| 415 |
with a mouse in editor. } |
| 416 |
|
| 417 |
{$ifndef OpenGLES} // TODO-es For OpenGLES, wireframe must be done differently |
| 418 |
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 419 |
{$endif} |
| 420 |
end; |
| 421 |
|
| 422 |
{ Render taking RenderOptions.WireframeEffect into account. |
| 423 |
Also controls Renderer.WireframePass, |
| 424 |
this way shaders for non-wireframe and wireframe can coexist, |
| 425 |
which avoids FPS drops e.g. at weSilhouette rendering a single 3D model |
| 426 |
(which would otherwise recreate all shaders in all frames). } |
| 427 |
procedure RenderWithWireframeEffect; |
| 428 |
var |
| 429 |
WireframeEffect: TWireframeEffect; |
| 430 |
SavedPolygonOffset: TPolygonOffset; |
| 431 |
begin |
| 432 |
WireframeEffect := RenderOptions.WireframeEffect; |
| 433 |
if InternalForceWireframe <> weNormal then |
| 434 |
begin |
| 435 |
{ Do not allow InternalForceWireframe to fill (make non-wireframe) polygons |
| 436 |
that were supposed to be wireframe. This would look weird, e.g. some wireframe |
| 437 |
gizmos would become filled. } |
| 438 |
if not ( (WireframeEffect = weWireframeOnly) and |
| 439 |
(InternalForceWireframe = weSolidWireframe) ) then |
| 440 |
WireframeEffect := InternalForceWireframe; |
| 441 |
end; |
| 442 |
case WireframeEffect of |
| 443 |
weNormal: |
| 444 |
begin |
| 445 |
Renderer.WireframePass := 0; |
| 446 |
RenderNormal; |
| 447 |
end; |
| 448 |
weWireframeOnly: |
| 449 |
begin |
| 450 |
Renderer.WireframePass := 1; |
| 451 |
RenderWireframe(RenderOptions.Mode = rmSolidColor); |
| 452 |
end; |
| 453 |
weSolidWireframe: |
| 454 |
begin |
| 455 |
Renderer.WireframePass := 0; |
| 456 |
SavedPolygonOffset := RenderContext.PolygonOffset; |
| 457 |
RenderContext.PolygonOffsetEnable(RenderOptions.SolidWireframeScale, RenderOptions.SolidWireframeBias); |
| 458 |
RenderNormal; |
| 459 |
RenderContext.PolygonOffset := SavedPolygonOffset; |
| 460 |
|
| 461 |
Renderer.WireframePass := 1; |
| 462 |
RenderWireframe(true); |
| 463 |
end; |
| 464 |
weSilhouette: |
| 465 |
begin |
| 466 |
Renderer.WireframePass := 0; |
| 467 |
RenderNormal; |
| 468 |
|
| 469 |
Renderer.WireframePass := 1; |
| 470 |
SavedPolygonOffset := RenderContext.PolygonOffset; |
| 471 |
RenderContext.PolygonOffsetEnable(RenderOptions.SilhouetteScale, RenderOptions.SilhouetteBias); |
| 472 |
|
| 473 |
(* Old idea, may be resurrected one day: |
| 474 |
|
| 475 |
{ rmSolidColor still does backface culling. |
| 476 |
This is very good in this case. When rmSolidColor and weSilhouette, |
| 477 |
and objects are solid (so backface culling is used) we can |
| 478 |
significantly improve the effect by reverting glFrontFace, |
| 479 |
this way we will cull *front* faces. This will not be noticed |
| 480 |
in case of rmSolidColor will single solid color, and it will |
| 481 |
improve the silhouette look, since front-face edges will not be |
| 482 |
rendered at all (no need to even hide them by glPolygonOffset, |
| 483 |
which is somewhat sloppy). |
| 484 |
|
| 485 |
TODO: this is probably incorrect now, that some meshes |
| 486 |
may have FrontFaceCcw = false. |
| 487 |
What we really would like to is to negate the FrontFaceCcw |
| 488 |
interpretation inside this RenderWireframe call. |
| 489 |
} |
| 490 |
if RenderOptions.Mode = rmSolidColor then |
| 491 |
glFrontFace(GL_CW); |
| 492 |
*) |
| 493 |
|
| 494 |
RenderWireframe(true); |
| 495 |
RenderContext.PolygonOffset := SavedPolygonOffset; |
| 496 |
end; |
| 497 |
else raise EInternalError.Create('Render: RenderOptions.WireframeEffect ?'); |
| 498 |
end; |
| 499 |
end; |
| 500 |
|
| 501 |
begin |
| 502 |
if Params.InternalPass = 0 then |
| 503 |
begin |
| 504 |
Inc(Params.Statistics.ShapesRendered); |
| 505 |
if Params.Transparent then |
| 506 |
Inc(Params.Statistics.ShapesRenderedBlending); |
| 507 |
end; |
| 508 |
|
| 509 |
Shape := CollectedShape.Shape; |
| 510 |
RenderOptions := CollectedShape.RenderOptions; |
| 511 |
|
| 512 |
// OcclusionBoxStateEnd will do nothing if OcclusionCulling = false |
| 513 |
FOcclusionCullingRenderer.Utils.OcclusionBoxStateEnd(false); |
| 514 |
|
| 515 |
FBlendingRenderer.BeforeRenderShape(Shape, RenderOptions); |
| 516 |
|
| 517 |
RenderWithWireframeEffect; |
| 518 |
end; |
| 519 |
|
| 520 |
procedure TShapesRenderer.RenderShape_OcclusionTests( |
| 521 |
const CollectedShape: TCollectedShape; const Params: TRenderParams); |
| 522 |
begin |
| 523 |
{ About "Params.RenderingCamera.Target = rtScreen" below: |
| 524 |
|
| 525 |
We do not make occlusion query when rendering to something else |
| 526 |
than screen (like shadow map or cube map environment for mirror). |
| 527 |
Such views are drastically different from normal camera view, |
| 528 |
so the whole idea that "what is visible in this frame is similar |
| 529 |
to what was visible in previous frame" breaks down there. |
| 530 |
|
| 531 |
TODO: In the future, this could be solved nicer, by having separate |
| 532 |
occlusion query states for different views. But this isn't easy |
| 533 |
to implement, as occlusion query state is part of TShape and |
| 534 |
octree nodes (for hierarchical occ query), so all these things |
| 535 |
should have a map "target->oq state" for various rendering targets. } |
| 536 |
|
| 537 |
if EffectiveOcclusionCulling and |
| 538 |
(Params.RenderingCamera.Target = rtScreen) then |
| 539 |
begin |
| 540 |
FOcclusionCullingRenderer.Render(CollectedShape, Params, |
| 541 |
{$ifdef FPC}@{$endif} RenderShape_NoTests); |
| 542 |
end else |
| 543 |
RenderShape_NoTests(CollectedShape, Params); |
| 544 |
end; |
| 545 |
|
| 546 |
procedure TShapesRenderer.Render(const Shapes: TShapesCollector; |
| 547 |
const Params: TRenderParams); |
| 548 |
|
| 549 |
procedure BatchingCommit; |
| 550 |
var |
| 551 |
Shape: TCollectedShape; |
| 552 |
begin |
| 553 |
if EffectiveDynamicBatching then |
| 554 |
begin |
| 555 |
Batching.Commit; |
| 556 |
for Shape in Batching.Batched do |
| 557 |
begin |
| 558 |
{ Otherwise, shapes from batching FPool |
| 559 |
would never have PrepareResources called. |
| 560 |
Note: Unsure if this is still necessary. } |
| 561 |
Shape.Shape.PrepareResources; |
| 562 |
|
| 563 |
{ Should we call RenderShape_NoTests or RenderShape_OcclusionTests? |
| 564 |
Doesn't matter, we know that occlusion culling is not done when |
| 565 |
batching is used, so RenderShape_OcclusionTests just calls |
| 566 |
RenderShape_NoTests. } |
| 567 |
RenderShape_NoTests(Shape, Params); |
| 568 |
end; |
| 569 |
Batching.FreeBatched; |
| 570 |
end; |
| 571 |
end; |
| 572 |
|
| 573 |
var |
| 574 |
LightRenderEvent: TLightRenderEvent; |
| 575 |
CollectedShape: TCollectedShape; |
| 576 |
begin |
| 577 |
{ TODO: This optimization should be useless, remove. |
| 578 |
For now it helps with hack that access 0th scene RenderOptions below. } |
| 579 |
if Shapes.FCollected.Count = 0 then |
| 580 |
Exit; |
| 581 |
|
| 582 |
if Params.InShadow then |
| 583 |
LightRenderEvent := {$ifdef FPC}@{$endif}LightRenderInShadow |
| 584 |
else |
| 585 |
LightRenderEvent := {$ifdef FPC}@{$endif}LightRender; |
| 586 |
|
| 587 |
if EffectiveOcclusionCulling then |
| 588 |
begin |
| 589 |
FOcclusionCullingRenderer.Utils.ProjectionMatrix := |
| 590 |
RenderContext.ProjectionMatrix; |
| 591 |
FOcclusionCullingRenderer.Utils.CameraMatrix := |
| 592 |
Params.RenderingCamera.Matrix; |
| 593 |
end; |
| 594 |
|
| 595 |
{ Initialize Batching. |
| 596 |
PreserveShapeOrder may be overridden to true below. } |
| 597 |
if EffectiveDynamicBatching then |
| 598 |
begin |
| 599 |
Batching.PreserveShapeOrder := false; |
| 600 |
Assert(Batching.Batched.Count = 0); |
| 601 |
end; |
| 602 |
|
| 603 |
FrameProfiler.Start(fmRenderCollectedShapesSort); |
| 604 |
|
| 605 |
if Params.Transparent then |
| 606 |
begin |
| 607 |
{ We'll draw partially transparent objects now, |
| 608 |
only from scenes with RenderOptions.Blending. } |
| 609 |
|
| 610 |
if EffectiveDynamicBatching then |
| 611 |
Batching.PreserveShapeOrder := true; |
| 612 |
|
| 613 |
{ TODO: The sorting is repeated at every render call. |
| 614 |
This is not optimal, |
| 615 |
we could instead reuse results from sorting in previous render call, |
| 616 |
if everything is still OK (nothing was added/removed from the world, |
| 617 |
order is still OK). } |
| 618 |
Shapes.FCollected.OnCustomShapeSort := OnCustomShapeSort; |
| 619 |
Shapes.FCollected.SortBackToFront( |
| 620 |
Params.RenderingCamera.View, |
| 621 |
BlendingSort); |
| 622 |
|
| 623 |
FBlendingRenderer.RenderBegin; |
| 624 |
end else |
| 625 |
begin |
| 626 |
Shapes.FCollected.OnCustomShapeSort := OnCustomShapeSort; |
| 627 |
Shapes.FCollected.SortFrontToBack( |
| 628 |
Params.RenderingCamera.View, |
| 629 |
OcclusionSort); |
| 630 |
end; |
| 631 |
|
| 632 |
FrameProfiler.Stop(fmRenderCollectedShapesSort); |
| 633 |
|
| 634 |
Renderer.RenderBegin(Params.GlobalLights as TLightInstancesList, |
| 635 |
Params.RenderingCamera, |
| 636 |
LightRenderEvent, Params.InternalPass, { wireframe pass } 0, Params.UserPass, |
| 637 |
@Params.Statistics); |
| 638 |
try |
| 639 |
for CollectedShape in Shapes.FCollected do |
| 640 |
begin |
| 641 |
if not (EffectiveDynamicBatching and Batching.Batch(CollectedShape)) then |
| 642 |
RenderShape_OcclusionTests(CollectedShape, Params); |
| 643 |
end; |
| 644 |
|
| 645 |
BatchingCommit; |
| 646 |
|
| 647 |
{ This must be called after BatchingCommit, |
| 648 |
since BatchingCommit may render some shapes } |
| 649 |
if Params.Transparent then |
| 650 |
FBlendingRenderer.RenderEnd; |
| 651 |
|
| 652 |
{ As each RenderShape_OcclusionTests inside could set OcclusionBoxState, |
| 653 |
be sure to restore state now. |
| 654 |
OcclusionBoxStateEnd will do nothing if EffectiveOcclusionCulling = false. } |
| 655 |
FOcclusionCullingRenderer.Utils.OcclusionBoxStateEnd(true); |
| 656 |
finally Renderer.RenderEnd end; |
| 657 |
end; |
| 658 |
|
| 659 |
function TShapesRenderer.EffectiveDynamicBatching: boolean; |
| 660 |
begin |
| 661 |
{$warnings off} // using deprecated CastleScene.DynamicBatching to keep it working |
| 662 |
Result := |
| 663 |
(Self.DynamicBatching or CastleScene.DynamicBatching) and |
| 664 |
(not EffectiveOcclusionCulling); |
| 665 |
{$warnings on} |
| 666 |
end; |
| 667 |
|
| 668 |
function TShapesRenderer.EffectiveOcclusionCulling: Boolean; |
| 669 |
begin |
| 670 |
Result := |
| 671 |
OcclusionCulling and |
| 672 |
(GLFeatures <> nil) and // paranoid check from old code, maybe not needed anymore |
| 673 |
GLFeatures.OcclusionQuery and |
| 674 |
GLFeatures.VertexBufferObject and |
| 675 |
(GLFeatures.OcclusionQueryCounterBits > 0); |
| 676 |
end; |
| 677 |
|
| 678 |
procedure TShapesRenderer.SetOcclusionCulling(const Value: Boolean); |
| 679 |
begin |
| 680 |
if FOcclusionCulling <> Value then |
| 681 |
begin |
| 682 |
FOcclusionCulling := Value; |
| 683 |
end; |
| 684 |
|
| 685 |
(* |
| 686 |
TODO: |
| 687 |
|
| 688 |
If OcclusionCulling just changed: |
| 689 |
If you switch OcclusionCulling on, then off, then move around the scene |
| 690 |
a lot, then switch OcclusionCulling back on --- you don't want to use |
| 691 |
results from previous query that was done many frames ago. } |
| 692 |
|
| 693 |
Old approach below. Should be done differently, looking at frame ids? |
| 694 |
|
| 695 |
procedure TCastleScene.ViewChangedSuddenly; |
| 696 |
var |
| 697 |
ShapeList: TShapeList; |
| 698 |
Shape: TShape; |
| 699 |
begin |
| 700 |
inherited; |
| 701 |
|
| 702 |
if EffectiveOcclusionCulling then |
| 703 |
begin |
| 704 |
{ Set OcclusionQueryAsked := false for all shapes. } |
| 705 |
ShapeList := Shapes.TraverseList(false, false, false); |
| 706 |
for Shape in ShapeList do |
| 707 |
TGLShape(Shape).OcclusionQueryAsked := false; |
| 708 |
end; |
| 709 |
end; |
| 710 |
*) |
| 711 |
|
| 712 |
{ TODO: also Invalidate occlusion culling if camera moved a lot. |
| 713 |
|
| 714 |
Should take care of all viewpoints switching, like |
| 715 |
- switching to other viewpoint through castle-model-viewer "viewpoints" menu, |
| 716 |
- just getting an event set_bind = true through vrml route. |
| 717 |
- calling Camera.SetView / SetWorldView to teleport. |
| 718 |
} |
| 719 |
end; |
| 720 |
|
| 721 |
end. |