| 1 |
{ |
| 2 |
Copyright 2002-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 |
{ Core of managing resources for shape rendering (like textures, shaders) |
| 17 |
and using them to render shapes. |
| 18 |
|
| 19 |
This unit is used by internal TShapesRenderer, |
| 20 |
which is used by public TCastleScene in turn. |
| 21 |
Normal user code renders by placing TCastleScene instances in TCastleViewport |
| 22 |
hierarchy, never directly using this unit. |
| 23 |
|
| 24 |
@bold(Usage:) |
| 25 |
|
| 26 |
@orderedList( |
| 27 |
@item( |
| 28 |
Before rendering prepare textures using TTextureResources.Prepare |
| 29 |
for all textures. Failure to do so will result in textures not being |
| 30 |
used. |
| 31 |
|
| 32 |
TGLShape takes care of doing this. |
| 33 |
|
| 34 |
TTextureResource and descendants have to be freed when the context |
| 35 |
is active. TAbstractTextureNode uses ApplicationProperties.OnGLContextClose |
| 36 |
to free them automatically when last GL context is lost. |
| 37 |
) |
| 38 |
|
| 39 |
@item( |
| 40 |
Shaders, VBOs and other things in TShapeCache will be prepared |
| 41 |
automatically at first render. |
| 42 |
You can also render with RenderMode = rmPrepare* to prepare them |
| 43 |
explicitly. |
| 44 |
|
| 45 |
TGLShape takes care of releasing TShapeCache. |
| 46 |
|
| 47 |
TCastleScene takes care of rendering stuff with |
| 48 |
RenderMode = rmPrepare*. |
| 49 |
|
| 50 |
Note that TShapeCache has some links to nodes. |
| 51 |
These node references must stay valid (otherwise we could get weird |
| 52 |
bugs, not only SEGFAULTS, also cache mistakes if these nodes would |
| 53 |
be freed and the same pointer allocated for different instance). |
| 54 |
Be sure to unprepare the TShapeCache before nodes are freed. |
| 55 |
|
| 56 |
Both TX3DRendererShape.Cache and TX3DRendererShape.ProgramCache |
| 57 |
have to be freed when the context is active. |
| 58 |
TGLShape in GLContextClose releases TX3DRendererShape.Cache and |
| 59 |
TX3DRendererShape.ProgramCache. |
| 60 |
That's why both TCastleViewport and TCastleScreenEffects take good |
| 61 |
care to pass GLContextClose event to TCastleScene.GLContextClose |
| 62 |
which passes it to TGLShape.GLContextClose for all shapes. |
| 63 |
) |
| 64 |
|
| 65 |
@item( |
| 66 |
The resulting OpenGL resources should only live until |
| 67 |
last OpenGL context is lost. |
| 68 |
|
| 69 |
They can be shared across all OpenGL contexts. |
| 70 |
CGE TCastleWindow and TCastleControl make |
| 71 |
all OpenGL contexts share all resources, so RendererCache can be just |
| 72 |
used freely. Use ApplicationProperties.OnGLContextClose to free |
| 73 |
things when last context is closed. |
| 74 |
) |
| 75 |
|
| 76 |
@item( |
| 77 |
Notes for things that render manually inside TCastleTransform.LocalRender |
| 78 |
(and thus are part of TCastleViewport), but are not TCastleScene: |
| 79 |
|
| 80 |
( This affects e.g. TMyMesh that does rendering using TCastleRenderUnlitMesh |
| 81 |
in ../../examples/research_special_rendering_methods/test_rendering_old_opengl/code/gameinitialize.pas . |
| 82 |
) |
| 83 |
|
| 84 |
Custom rendering inside TCastleTransform.LocalRender |
| 85 |
never happens between RenderBegin and RenderEnd calls. |
| 86 |
It didn't happen in the past (before TShapesRenderer) because |
| 87 |
each scene did it's own RenderBegin+RenderEnd. |
| 88 |
It doesn't happen now (after TShapesRenderer) because |
| 89 |
the RenderBegin+RenderEnd is done at the end of viewport rendering, |
| 90 |
after all TCastleTransform.LocalRender calls ended. |
| 91 |
|
| 92 |
( There was a period when we had ViewportRenderBegin/End, |
| 93 |
but not anymore. ) |
| 94 |
|
| 95 |
See ClassRenderEnd for the up-to-date list of state |
| 96 |
(some managed using RenderContext, some using direct OpenGL(ES) calls) |
| 97 |
that is reset at the end of viewport rendering, |
| 98 |
because before each scene or before each shape it may change and *not* |
| 99 |
be reset. |
| 100 |
|
| 101 |
When you render manually inside TCastleTransform.LocalRender, |
| 102 |
this state is undefined when you start, |
| 103 |
and you can change it carelessly (since next shape |
| 104 |
will adjust it). Though we advise you save/restore it |
| 105 |
(it case it will matter to other custom rendering code in |
| 106 |
TCastleTransform.LocalRender, that will (even if it shouldn't) assume that |
| 107 |
the state is reasonable). |
| 108 |
|
| 109 |
But note that, since your rendering happens outside of RenderBegin/End |
| 110 |
and RenderEnd (with ClassRenderEnd) set reasonable state, |
| 111 |
you will not have any "weird" state (like depth range) unless |
| 112 |
some other custom renderer sets it. |
| 113 |
) |
| 114 |
|
| 115 |
@item( |
| 116 |
Surround shapes rendering between TRenderer.RenderBegin and TRenderer.RenderEnd. |
| 117 |
|
| 118 |
Between these calls, you should not touch OpenGL state or RenderContext |
| 119 |
yourself at all. |
| 120 |
The renderer may depend that every state change goes through it. |
| 121 |
) |
| 122 |
|
| 123 |
@item( |
| 124 |
Between TRenderer.RenderBegin and TRenderer.RenderEnd |
| 125 |
you should render the shapes by calling RenderShape. |
| 126 |
) |
| 127 |
|
| 128 |
@item( |
| 129 |
All render commands that affect state (from RenderBegin to RenderEnd) |
| 130 |
must be done in the same OpenGL context. |
| 131 |
) |
| 132 |
) |
| 133 |
|
| 134 |
@bold(OpenGL state affecting X3D rendering:) |
| 135 |
|
| 136 |
Some OpenGL state is unconditionally set by rendering |
| 137 |
(TRenderer.RenderBegin, or just each shape rendering). |
| 138 |
|
| 139 |
But there's also some OpenGL state that we let affect our rendering. |
| 140 |
This allows you to customize rendering by using normal OpenGL commands |
| 141 |
or RenderContext settings. |
| 142 |
|
| 143 |
@unorderedList( |
| 144 |
@item(Current glPolygonMode. |
| 145 |
This is used by @link(TCastleScene) to optionally render wireframe. |
| 146 |
) |
| 147 |
|
| 148 |
@item(Blending settings, controlled using RenderContext.BlendingBegin, |
| 149 |
RenderContext.BlendingEnd. |
| 150 |
|
| 151 |
Also RenderContext.DepthBufferUpdate. |
| 152 |
|
| 153 |
These are used by @link(TShapesRenderer) to render |
| 154 |
scenes with a mix of tranparent and opaque objects. |
| 155 |
Only @link(TShapesRenderer) deals with it (not this renderer), |
| 156 |
as doing it correctly requires ordering the shapes. |
| 157 |
) |
| 158 |
) |
| 159 |
|
| 160 |
The renderer uses arrays in GPU-friendly format defined by TGeometryArrays. |
| 161 |
} |
| 162 |
unit CastleInternalRenderer; |
| 163 |
|
| 164 |
{$I castleconf.inc} |
| 165 |
|
| 166 |
interface |
| 167 |
|
| 168 |
uses Classes, SysUtils, Generics.Collections, |
| 169 |
{$ifdef OpenGLES} CastleGLES, {$else} CastleGL, {$endif} |
| 170 |
CastleUtils, CastleVectors, X3DFields, X3DNodes, CastleColors, |
| 171 |
CastleInternalX3DLexer, CastleImages, CastleGLUtils, CastleRendererInternalLights, |
| 172 |
CastleGLShaders, CastleGLImages, CastleTextureImages, CastleVideos, X3DTime, |
| 173 |
CastleShapes, CastleClassUtils, CastleInternalCompositeImage, |
| 174 |
CastleInternalGeometryArrays, CastleInternalArraysGenerator, CastleRendererInternalShader, |
| 175 |
CastleRendererInternalTextureEnv, CastleBoxes, CastleTransform, CastleRenderOptions; |
| 176 |
|
| 177 |
{$define read_interface} |
| 178 |
|
| 179 |
type |
| 180 |
{$I castleinternalrenderer_initial_types.inc} |
| 181 |
{$I castleinternalrenderer_cache_types.inc} |
| 182 |
{$I castleinternalrenderer_cache.inc} |
| 183 |
{$I castleinternalrenderer_resource.inc} |
| 184 |
{$I castleinternalrenderer_texture.inc} |
| 185 |
{$I castleinternalrenderer_glsl.inc} |
| 186 |
{$I castleinternalrenderer_pass.inc} |
| 187 |
{$I castleinternalrenderer_shape.inc} |
| 188 |
{$I castleinternalrenderer_custom_shaders.inc} |
| 189 |
{$I castleinternalrenderer_renderer.inc} |
| 190 |
{$I castleinternalrenderer_screen_effects.inc} |
| 191 |
|
| 192 |
{$I castleinternalrenderer_final_globals.inc} |
| 193 |
|
| 194 |
{$undef read_interface} |
| 195 |
|
| 196 |
implementation |
| 197 |
|
| 198 |
uses Math, |
| 199 |
CastleStringUtils, CastleGLVersion, CastleLog, CastleInternalGLCubeMaps, |
| 200 |
X3DCameraUtils, CastleProjection, CastleRectangles, CastleTriangles, |
| 201 |
CastleCameras, CastleSceneInternalShape, CastleApplicationProperties, |
| 202 |
CastleRenderContext, CastleInternalGLUtils, CastleTimeUtils; |
| 203 |
|
| 204 |
{$define read_implementation} |
| 205 |
|
| 206 |
{$I castleinternalrenderer_initial_types.inc} |
| 207 |
{$I castleinternalrenderer_cache_types.inc} |
| 208 |
{$I castleinternalrenderer_cache.inc} |
| 209 |
{$I castleinternalrenderer_pass.inc} |
| 210 |
{$I castleinternalrenderer_shape.inc} |
| 211 |
{$I castleinternalrenderer_meshrenderer.inc} // must be before TRenderer |
| 212 |
{$I castleinternalrenderer_custom_shaders.inc} |
| 213 |
{$I castleinternalrenderer_renderer.inc} |
| 214 |
{$I castleinternalrenderer_resource.inc} |
| 215 |
{$I castleinternalrenderer_texture.inc} |
| 216 |
{$I castleinternalrenderer_surfacetextures.inc} |
| 217 |
{$I castleinternalrenderer_glsl.inc} |
| 218 |
{$I castleinternalrenderer_screen_effects.inc} |
| 219 |
{$I castleinternalrenderer_final_globals.inc} |
| 220 |
|
| 221 |
initialization |
| 222 |
TCastleRenderOptions.DefaultMinificationFilter := minLinearMipmapLinear; |
| 223 |
TCastleRenderOptions.DefaultMagnificationFilter := magLinear; |
| 224 |
|
| 225 |
RendererCache := TRendererCache.Create; |
| 226 |
finalization |
| 227 |
RendererCache.FreeWhenEmpty(@RendererCache); |
| 228 |
end. |