| 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 |
{ Resource connected to rendering (so connected to OpenGL context) |
| 19 |
of a given X3D node. |
| 20 |
|
| 21 |
Prepare it to increase reference count, |
| 22 |
Unprepare it (also done at destruction) to decrease reference count. |
| 23 |
|
| 24 |
Descendants implementors: Be careful when implementing this. |
| 25 |
The lifetime of this instance is connected to the lifetime of the X3D node. |
| 26 |
It should not assume anything else, e.g. that some TRenderer |
| 27 |
or TCastleRenderOptions instance continues existing. } |
| 28 |
TRendererResource = class(TInternalRendererResource) |
| 29 |
strict private |
| 30 |
FNode: TX3DNode; |
| 31 |
|
| 32 |
{ Release stuff allocated by Prepare. |
| 33 |
For now this just calls virtual UnprepareCore. } |
| 34 |
procedure Unprepare; |
| 35 |
protected |
| 36 |
{ Do the time-consuming preparations before using this node with renderer. |
| 37 |
|
| 38 |
Called always from our Prepare method. Our Prepare method takes care to catch |
| 39 |
common exceptions from this (EFramebufferError, ETextureLoadError), |
| 40 |
convert them to WritelnWarning and make Unprepare to undo the preparations. |
| 41 |
So Prepare-Unprepare work like constructor-destructor, with Unprepare |
| 42 |
having to be prepared to eventually finalize even incomplete instance. } |
| 43 |
procedure PrepareCore(const RenderOptions: TCastleRenderOptions); virtual; abstract; |
| 44 |
|
| 45 |
{ Release all resources allocated by Prepare method. |
| 46 |
This is exposed so that you can override it. } |
| 47 |
procedure UnprepareCore; virtual; abstract; |
| 48 |
public |
| 49 |
constructor Create(const ANode: TX3DNode); virtual; |
| 50 |
destructor Destroy; override; |
| 51 |
|
| 52 |
{ Reference to handled X3D node. |
| 53 |
Never @nil. } |
| 54 |
property Node: TX3DNode read FNode; |
| 55 |
|
| 56 |
{ Do the time-consuming preparations before using this node with renderer. |
| 57 |
Calls PrepareCore. } |
| 58 |
procedure Prepare(const RenderOptions: TCastleRenderOptions); |
| 59 |
end; |
| 60 |
|
| 61 |
{$endif read_interface} |
| 62 |
|
| 63 |
{$ifdef read_implementation} |
| 64 |
|
| 65 |
{ TRendererResource ---------------------------------------------------------- } |
| 66 |
|
| 67 |
constructor TRendererResource.Create(const ANode: TX3DNode); |
| 68 |
begin |
| 69 |
inherited Create; |
| 70 |
FNode := ANode; |
| 71 |
end; |
| 72 |
|
| 73 |
destructor TRendererResource.Destroy; |
| 74 |
begin |
| 75 |
Unprepare; |
| 76 |
inherited; |
| 77 |
end; |
| 78 |
|
| 79 |
procedure TRendererResource.Prepare(const RenderOptions: TCastleRenderOptions); |
| 80 |
begin |
| 81 |
try |
| 82 |
PrepareCore(RenderOptions); |
| 83 |
except |
| 84 |
on E: EFramebufferError do |
| 85 |
begin |
| 86 |
Unprepare; |
| 87 |
WritelnWarning('Renderer', 'Framebuffer error, generated texture not possible: ' |
| 88 |
+ E.Message); |
| 89 |
end; |
| 90 |
|
| 91 |
on E: ETextureLoadError do |
| 92 |
begin |
| 93 |
Unprepare; |
| 94 |
WritelnWarning('Renderer', 'Cannot load texture to OpenGL: ' + E.Message); |
| 95 |
end; |
| 96 |
end; |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TRendererResource.Unprepare; |
| 100 |
begin |
| 101 |
UnprepareCore; |
| 102 |
end; |
| 103 |
|
| 104 |
{$endif} |