| 1 |
{ |
| 2 |
Copyright 2017-2019, 2022 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 |
{ A skeleton how you can test a new rendering target implementation |
| 17 |
(like Metal or Vulkan or Direct3D). |
| 18 |
|
| 19 |
In the future, Castle Game Engine should allow to "plug" a new renderer, |
| 20 |
such that the underlying renderer can be switched at runtime, |
| 21 |
and the public engine API stays the same. |
| 22 |
So all the engine classes (like TCastleScene, TCastleViewport, TCastleWindow, |
| 23 |
TCastleButton, TDrawableImage...) will work, regardless of the renderer. |
| 24 |
|
| 25 |
Also, you will then no longer need to initialize some semi-internal things |
| 26 |
shown in this program: initializing Params, ProjectionMatrix, RenderingCamera |
| 27 |
should be done automatically by TCastleViewport (once TCastleViewport |
| 28 |
is independent from OpenGL). |
| 29 |
|
| 30 |
In the meantime, you can use this approach, which creates a descendant |
| 31 |
of TCastleSceneCore, to implement a new renderer. |
| 32 |
|
| 33 |
This approach should allow you to quickly test whether your renderer works. |
| 34 |
Once we have an alternative renderer working, Michalis promises to quickly |
| 35 |
implement an architecture to comfortably "plug" your renderer |
| 36 |
to Castle Game Engine! |
| 37 |
} |
| 38 |
|
| 39 |
uses SysUtils, TypInfo, Classes, |
| 40 |
CastleVectors, CastleCameras, X3DNodes, CastleSceneCore, CastleShapes, |
| 41 |
CastleTransform, CastleProjection, CastleFrustum, CastleViewport, CastleScene, |
| 42 |
CastleInternalGeometryArrays; |
| 43 |
|
| 44 |
var |
| 45 |
{ Projection, as a trivial global variable. } |
| 46 |
ProjectionMatrix: TMatrix4; |
| 47 |
|
| 48 |
{ Vulkan shape and scene ----------------------------------------------------- } |
| 49 |
|
| 50 |
type |
| 51 |
{ In this class you can store Vulkan-specific information that is per-shape. |
| 52 |
Inside the TCastleSceneVulkan implementation, |
| 53 |
you know that all your shapes are descendants of TVulkanShape. } |
| 54 |
TVulkanShape = class(TShape) |
| 55 |
end; |
| 56 |
|
| 57 |
{ Scene that can be rendered using Vulkan. } |
| 58 |
TCastleSceneVulkan = class(TCastleSceneCore) |
| 59 |
protected |
| 60 |
function CreateShape(const AGeometry: TAbstractGeometryNode; |
| 61 |
const AState: TX3DGraphTraverseState; |
| 62 |
const ParentInfo: PTraversingInfo): TShape; override; |
| 63 |
public |
| 64 |
procedure PrepareResources(const Options: TPrepareResourcesOptions; |
| 65 |
const Params: TPrepareParams); override; |
| 66 |
procedure LocalRender(const Params: TRenderParams); override; |
| 67 |
end; |
| 68 |
|
| 69 |
function TCastleSceneVulkan.CreateShape(const AGeometry: TAbstractGeometryNode; |
| 70 |
const AState: TX3DGraphTraverseState; const ParentInfo: PTraversingInfo): TShape; |
| 71 |
begin |
| 72 |
Result := TVulkanShape.Create(Self, AGeometry, AState, ParentInfo); |
| 73 |
end; |
| 74 |
|
| 75 |
procedure TCastleSceneVulkan.PrepareResources( |
| 76 |
const Options: TPrepareResourcesOptions; |
| 77 |
const Params: TPrepareParams); |
| 78 |
var |
| 79 |
ShapeList: TShapeList; |
| 80 |
Shape: TShape; |
| 81 |
begin |
| 82 |
ShapeList := Shapes.TraverseList(false, false); |
| 83 |
for Shape in ShapeList do |
| 84 |
begin |
| 85 |
Writeln('Prepare to render shape: ', Shape.NiceName); |
| 86 |
|
| 87 |
{ TODO: Load Shape data to GPU now |
| 88 |
(you can cast Shape to TShapeVulkan as necessary). |
| 89 |
|
| 90 |
- E.g. load geometry data (Shape.GeometryArrays) to VBO. |
| 91 |
You should call Shape.GeometryArrays.FreeData afterwards, |
| 92 |
to not keep the data on CPU anymore. |
| 93 |
|
| 94 |
- E.g. load textures to GPU. |
| 95 |
|
| 96 |
- You should be prepared that some data may be already loaded. |
| 97 |
So all the loading should look like |
| 98 |
|
| 99 |
if not Shape.SomethingLoaded then |
| 100 |
begin |
| 101 |
Shape.SomethingLoaded := true; |
| 102 |
// load something here ... |
| 103 |
end; |
| 104 |
|
| 105 |
For a first renderer test you can also instead load on-demand |
| 106 |
from the LocalRender implementation. |
| 107 |
} |
| 108 |
|
| 109 |
end; |
| 110 |
end; |
| 111 |
|
| 112 |
procedure TCastleSceneVulkan.LocalRender(const Params: TRenderParams); |
| 113 |
|
| 114 |
function GetSceneModelView: TMatrix4; |
| 115 |
begin |
| 116 |
Result := Params.RenderingCamera.CurrentMatrix * Params.Transformation^.Transform; |
| 117 |
end; |
| 118 |
|
| 119 |
function PrimitiveToStr(const Primitive: TGeometryPrimitive): string; |
| 120 |
begin |
| 121 |
Result := GetEnumName(TypeInfo(TGeometryPrimitive), Ord(Primitive)); |
| 122 |
end; |
| 123 |
|
| 124 |
var |
| 125 |
SceneModelView, ShapeModelView: TMatrix4; |
| 126 |
ShapeList: TShapeList; |
| 127 |
Shape: TShape; |
| 128 |
GeometryArrays: TGeometryArrays; |
| 129 |
begin |
| 130 |
SceneModelView := GetSceneModelView; |
| 131 |
ShapeList := Shapes.TraverseList(true, true); |
| 132 |
for Shape in ShapeList do |
| 133 |
begin |
| 134 |
ShapeModelView := SceneModelView * Shape.State.Transformation.Transform; |
| 135 |
Writeln('Rendering shape: ', Shape.NiceName); |
| 136 |
Writeln('Projection matrix:'); |
| 137 |
Writeln(ProjectionMatrix.ToString(' ')); |
| 138 |
Writeln('Modelview matrix: '); |
| 139 |
Writeln(ShapeModelView.ToString(' ')); |
| 140 |
|
| 141 |
GeometryArrays := Shape.GeometryArrays; |
| 142 |
try |
| 143 |
Writeln('Geometry:', |
| 144 |
' Primitive: ', PrimitiveToStr(GeometryArrays.Primitive), |
| 145 |
', HasIndexes: ', GeometryArrays.HasIndexes, |
| 146 |
', IndexesCount: ', GeometryArrays.IndexesCount, |
| 147 |
', Count: ', GeometryArrays.Count); |
| 148 |
|
| 149 |
{ TODO: Render Shape here (you can cast it to TShapeVulkan as necessary). |
| 150 |
Load Shape.GeometryArrays to GPU, |
| 151 |
and pass parameters (like projection and modelview matrix) to shaders. |
| 152 |
} |
| 153 |
finally FreeAndNil(GeometryArrays) end; |
| 154 |
end; |
| 155 |
end; |
| 156 |
|
| 157 |
{ Vulkan application and window ---------------------------------------------- } |
| 158 |
|
| 159 |
type |
| 160 |
TVulkanApplication = class(TComponent) |
| 161 |
public |
| 162 |
// TODO: Process some inputs in TVulkanWindow, allow setting this to true. |
| 163 |
Quit: boolean; |
| 164 |
end; |
| 165 |
|
| 166 |
TVulkanWindow = class(TComponent) |
| 167 |
public |
| 168 |
Width, Height: Integer; |
| 169 |
procedure Open; |
| 170 |
end; |
| 171 |
|
| 172 |
procedure TVulkanWindow.Open; |
| 173 |
begin |
| 174 |
// TODO: create the Vulkan context, show the window |
| 175 |
end; |
| 176 |
|
| 177 |
{ initialization ------------------------------------------------------------- } |
| 178 |
|
| 179 |
var |
| 180 |
Application: TVulkanApplication; |
| 181 |
Window: TVulkanWindow; |
| 182 |
Viewport: TCastleViewport; |
| 183 |
Scene: TCastleSceneVulkan; |
| 184 |
Camera: TCastleCamera; |
| 185 |
RenderParams: TRenderParams; |
| 186 |
begin |
| 187 |
Application := TVulkanApplication.Create(nil); |
| 188 |
try |
| 189 |
Window := TVulkanWindow.Create(Application); |
| 190 |
Window.Width := 1024; |
| 191 |
Window.Height := 768; |
| 192 |
Window.Open; |
| 193 |
|
| 194 |
Viewport := TCastleViewport.Create(Application); |
| 195 |
Viewport.FullSize := true; |
| 196 |
//Window.Controls.InsertFront(Viewport); |
| 197 |
|
| 198 |
Camera := Viewport.Camera; |
| 199 |
Camera.SetWorldView( |
| 200 |
Vector3(0, 0, 0), // position |
| 201 |
Vector3(0, 0, -1), // direction |
| 202 |
Vector3(0, 0, 1) // up |
| 203 |
); |
| 204 |
|
| 205 |
Scene := TCastleSceneVulkan.Create(Application); |
| 206 |
Scene.Load('castle-data:/car.gltf'); |
| 207 |
Scene.PrepareResources([], nil); |
| 208 |
|
| 209 |
{ Prepare rendering parameters |
| 210 |
(this is done by TCastleViewport in normal circumstances). |
| 211 |
|
| 212 |
Note: Creating and using TRenderParams instance (or any descendant of it) |
| 213 |
means that you deal with internal stuff. |
| 214 |
|
| 215 |
- The API of TRenderParams is internal, it may change at any moment. |
| 216 |
|
| 217 |
- You should not construct or modify TRenderParams in a normal CGE application. |
| 218 |
In normal application, TCastleViewport prepares TRenderParams instance, |
| 219 |
and TCastleScene uses it. |
| 220 |
How the TRenderParams is prepared, and how is it used -- it's |
| 221 |
internal for normal applications. |
| 222 |
} |
| 223 |
RenderParams := TBasicRenderParams.Create; |
| 224 |
RenderParams.RenderingCamera := TRenderingCamera.Create; |
| 225 |
RenderParams.RenderingCamera.Target := rtScreen; |
| 226 |
RenderParams.RenderingCamera.FromCameraObject(Camera); |
| 227 |
RenderParams.Frustum := @RenderParams.RenderingCamera.Frustum; |
| 228 |
|
| 229 |
while not Application.Quit do |
| 230 |
begin |
| 231 |
{ TODO: Clear the screen contents (color, depth) now. } |
| 232 |
|
| 233 |
{ Prepare projection |
| 234 |
(this is done by TCastleViewport in normal circumstances). } |
| 235 |
ProjectionMatrix := PerspectiveProjectionMatrixDeg( |
| 236 |
60, Window.Width / Window.Height, 0.1, 1000); |
| 237 |
|
| 238 |
{ In a real rendering, Scene.Render may be called more than once |
| 239 |
per frame, with different values of |
| 240 |
RenderParams.Transparent and RenderParams.ShadowVolumesReceivers, |
| 241 |
that filter various shapes. |
| 242 |
You can temporarily ignore this issue (until you will want to |
| 243 |
support blending (partial transparency) in your renderer). } |
| 244 |
|
| 245 |
// Render the Scene |
| 246 |
Scene.Render(RenderParams); |
| 247 |
|
| 248 |
{ TODO: do something like Window.Flush or Window.SwapBuffers, |
| 249 |
to make sure GPU will execute the rendering commands ASAP. } |
| 250 |
|
| 251 |
{ Testing: Wait for a key press, give user's a chance to press Ctrl + C :) } |
| 252 |
Readln; |
| 253 |
end; |
| 254 |
finally |
| 255 |
if RenderParams <> nil then |
| 256 |
FreeAndNil(RenderParams.RenderingCamera); |
| 257 |
FreeAndNil(RenderParams); |
| 258 |
FreeAndNil(Application); |
| 259 |
end; |
| 260 |
end. |