-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRenderer.cpp
More file actions
495 lines (468 loc) · 22.2 KB
/
Copy pathRenderer.cpp
File metadata and controls
495 lines (468 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
Copyright (C) 2016,2018,2019,2021,2025,2026 Rodrigo Jose Hernandez Cordoba
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "Factory.h"
#include "aeongames/StringId.hpp"
#include "aeongames/Renderer.hpp"
#include "aeongames/Scene.hpp"
#include "aeongames/Node.hpp"
#include "aeongames/Mesh.hpp"
#include "aeongames/Pipeline.hpp"
#include "aeongames/Material.hpp"
#include "aeongames/AABB.hpp"
#include "aeongames/Transform.hpp"
#include "aeongames/Frustum.hpp"
#include "aeongames/CRC.hpp"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string_view>
namespace AeonGames
{
Renderer::~Renderer() = default;
using RendererFactory = Factory<Renderer, void*, const RendererSettings&>;
std::unique_ptr<Renderer> ConstructRenderer ( uint32_t aIdentifier, void* aWindow, const RendererSettings& aSettings )
{
return RendererFactory::Construct ( aIdentifier, aWindow, aSettings );
}
std::unique_ptr<Renderer> ConstructRenderer ( const std::string& aIdentifier, void* aWindow, const RendererSettings& aSettings )
{
return RendererFactory::Construct ( aIdentifier, aWindow, aSettings );
}
std::unique_ptr<Renderer> ConstructRenderer ( const StringId& aIdentifier, void* aWindow, const RendererSettings& aSettings )
{
return ConstructRenderer ( aIdentifier.GetId(), aWindow, aSettings );
}
bool RegisterRendererConstructor ( const StringId& aIdentifier,
const std::function<std::unique_ptr<Renderer> ( void*, const RendererSettings& ) >& aConstructor )
{
return RendererFactory::RegisterConstructor ( aIdentifier, aConstructor );
}
bool UnregisterRendererConstructor ( const StringId& aIdentifier )
{
return RendererFactory::UnregisterConstructor ( aIdentifier );
}
void EnumerateRendererConstructors ( const std::function<bool ( const StringId& ) >& aEnumerator )
{
RendererFactory::EnumerateConstructors ( aEnumerator );
}
std::vector<std::string> GetRendererConstructorNames()
{
return RendererFactory::GetConstructorNames();
}
void Renderer::RenderScene ( void* aWindowId, const Scene& aScene, const GuiOverlay* aGuiOverlay )
{
if ( !IsValidWindow ( aWindowId ) )
{
return;
}
// Skip the entire frame while the device is lost: recording any pass
// against dead GPU handles would fault. The backend rebuilds the device
// at the next BeginFrame, after which this gate reopens.
if ( IsDeviceLost() )
{
return;
}
// The per-frame protocol, expressed once as an ordered sequence of step
// primitives the backends implement. Keeping it here (rather than
// duplicated per backend) means OpenGL and Vulkan render the scene
// through identical logic.
const Pipeline* lighting = aScene.GetLightingPipeline();
if ( !mBenchmarkInitialized )
{
InitBenchmark();
}
// The opt-in per-pass benchmark times only lighting frames: the pass
// boundaries the marks bracket exist only when the lighting path runs,
// so a frame records either the full mark set or none.
mBenchmarkFrameRecorded = mBenchmarkActive && ( lighting != nullptr );
BeginRender ( aWindowId, lighting );
MaybeRecordTimestamp ( aWindowId, 0 );
if ( lighting )
{
// Spot shadow passes: render each spot shadow caster's depth into its
// own layer of the spot shadow map array before the directional pass.
// Spots run BEFORE the directional pass on purpose: every shadow
// depth pass reuses the window's ShadowParams matrix as scratch for
// the depth vertex shader, so the directional pass must write it LAST
// to leave the directional matrix in place for the shading pass.
GpuSpotShadowParams spot_shadow_params{};
const uint32_t spot_caster_count = aScene.GetSpotShadowCasters ( spot_shadow_params, GetSettings().mSpotShadowMapResolution );
SetSpotShadowParams ( aWindowId, spot_shadow_params );
for ( uint32_t slot = 0; slot < spot_caster_count; ++slot )
{
const Matrix4x4 spot_light_view_projection =
spot_shadow_params.spot_light_view_projection[slot];
aScene.BuildRenderQueue ( Frustum ( spot_light_view_projection ) );
BeginSpotShadowPass ( aWindowId, slot, spot_light_view_projection );
SubmitRenderQueue ( aWindowId, aScene, RenderPass::ShadowPass );
EndSpotShadowPass ( aWindowId );
}
// Point shadow passes: each point caster is omnidirectional, so all
// six cube faces are rendered in a single draw -- Vulkan multiview
// (one view per face) or an OpenGL geometry shader -- into the
// caster's six cube-map-array layers.
//
// Each caster's cube map is cached: it is only re-rendered when the
// caster's light (position/radius) or some shadow-casting geometry
// actually changed since it was last drawn. The depth image persists
// between frames, so an unchanged caster reuses its previous map and
// skips the pass -- on a static scene the point shadow maps are
// rendered once and then sampled for free every frame, even while the
// camera moves.
GpuPointShadowParams point_shadow_params{};
const uint32_t point_caster_count = aScene.GetPointShadowCasters ( point_shadow_params, GetSettings().mPointShadowMapResolution );
SetPointShadowParams ( aWindowId, point_shadow_params );
const uint64_t shadow_geometry_signature = aScene.GetShadowGeometrySignature();
auto& point_cache = mPointShadowCache[aWindowId];
for ( uint32_t caster = 0; caster < point_caster_count; ++caster )
{
PointShadowCacheEntry& entry = point_cache[caster];
if ( entry.rendered &&
entry.geometry_signature == shadow_geometry_signature &&
entry.light_position_radius == point_shadow_params.caster_position_radius[caster] )
{
// Nothing this caster sees changed; reuse its cached cube map.
continue;
}
// The whole sphere is rendered in one pass, so cull once to a box
// that bounds the caster's shadow sphere (a point light has no
// single frustum; the axis-aligned box is a conservative superset).
const Vector4& caster_position_radius = point_shadow_params.caster_position_radius[caster];
const float radius = caster_position_radius.GetW();
Matrix4x4 caster_bounds;
caster_bounds.Ortho (
caster_position_radius.GetX() - radius, caster_position_radius.GetX() + radius,
caster_position_radius.GetZ() - radius, caster_position_radius.GetZ() + radius,
caster_position_radius.GetY() - radius, caster_position_radius.GetY() + radius );
aScene.BuildRenderQueue ( Frustum ( caster_bounds ) );
BeginPointShadowPass ( aWindowId, caster );
SubmitRenderQueue ( aWindowId, aScene, RenderPass::ShadowPass );
EndPointShadowPass ( aWindowId );
entry.light_position_radius = point_shadow_params.caster_position_radius[caster];
entry.geometry_signature = shadow_geometry_signature;
entry.rendered = true;
}
// Directional shadow pass: render scene depth from the sun's point of
// view into the shadow map before shading so the fragment stage can
// sample it. The shadow map must contain every caster the light can
// see, NOT just what the camera sees, so the queue is culled to the
// light's orthographic frustum here. Reusing the camera frustum would
// make casters outside the view pop in and out as the camera moves.
Matrix4x4 light_view_projection;
if ( aScene.GetDirectionalShadowMatrix ( light_view_projection, GetProjectionMatrix ( aWindowId ), GetSettings().mDirectionalShadowMapResolution ) )
{
aScene.BuildRenderQueue ( Frustum ( light_view_projection ) );
BeginShadowPass ( aWindowId, light_view_projection );
SubmitRenderQueue ( aWindowId, aScene, RenderPass::ShadowPass );
EndShadowPass ( aWindowId );
}
}
// Collect every camera-visible draw; the queue feeds both the depth
// pre-pass and the shading pass, merging sorted runs into instanced
// draws on submit. Built after the shadow pass, which uses its own
// light-space queue.
aScene.BuildRenderQueue ( GetFrustum ( aWindowId ) );
if ( lighting )
{
// Depth pre-pass: flag clusters containing visible geometry with the
// renderer's marking pipeline before light culling.
MaybeRecordTimestamp ( aWindowId, 1 );
SubmitRenderQueue ( aWindowId, aScene, RenderPass::DepthPrePass );
MaybeRecordTimestamp ( aWindowId, 2 );
EndDepthPrePass ( aWindowId, lighting );
MaybeRecordTimestamp ( aWindowId, 3 );
}
SubmitRenderQueue ( aWindowId, aScene, RenderPass::Shading );
MaybeRecordTimestamp ( aWindowId, 4 );
// Debug geometry shares the scene depth buffer and must precede the
// overlay so the GUI stays on top.
if ( mDebugRendering )
{
SubmitDebugGeometry ( aWindowId, aScene );
}
if ( aGuiOverlay )
{
RenderOverlay ( aWindowId, *aGuiOverlay );
}
MaybeRecordTimestamp ( aWindowId, 5 );
EndRender ( aWindowId );
EndBenchmarkFrame ( aWindowId );
}
void Renderer::InitBenchmark()
{
mBenchmarkInitialized = true;
const char* frames_env = std::getenv ( "AEON_BENCH_FRAMES" );
if ( frames_env == nullptr )
{
return;
}
const long frames = std::strtol ( frames_env, nullptr, 10 );
if ( frames <= 0 )
{
return;
}
mBenchmarkTarget = static_cast<uint32_t> ( frames );
const char* warmup_env = std::getenv ( "AEON_BENCH_WARMUP" );
mBenchmarkWarmup = ( warmup_env != nullptr )
? static_cast<uint32_t> ( std::max ( 0L, std::strtol ( warmup_env, nullptr, 10 ) ) )
: 60u;
mBenchmarkActive = true;
for ( auto& segment : mBenchmarkSegments )
{
segment.reserve ( mBenchmarkTarget );
}
mBenchmarkTotals.reserve ( mBenchmarkTarget );
std::cout << "GPU benchmark armed: " << mBenchmarkTarget
<< " frames after " << mBenchmarkWarmup << " warm-up frames" << std::endl;
}
void Renderer::MaybeRecordTimestamp ( void* aWindowId, uint32_t aSlot )
{
if ( !mBenchmarkFrameRecorded )
{
return;
}
RecordGpuTimestamp ( aWindowId, aSlot );
}
void Renderer::EndBenchmarkFrame ( void* aWindowId )
{
if ( !mBenchmarkFrameRecorded )
{
return;
}
mBenchmarkFrameRecorded = false;
std::array<uint64_t, kGpuTimestampMarks> marks{};
if ( !ReadGpuTimestamps ( aWindowId, marks ) )
{
return;
}
++mBenchmarkFrameCounter;
if ( mBenchmarkFrameCounter <= mBenchmarkWarmup )
{
return;
}
for ( uint32_t i = 0; i + 1 < kGpuTimestampMarks; ++i )
{
const double ms = ( marks[i + 1] >= marks[i] )
? static_cast<double> ( marks[i + 1] - marks[i] ) * 1e-6 : 0.0;
mBenchmarkSegments[i].push_back ( ms );
}
mBenchmarkTotals.push_back ( ( marks[kGpuTimestampMarks - 1] >= marks[0] )
? static_cast<double> ( marks[kGpuTimestampMarks - 1] - marks[0] ) * 1e-6 : 0.0 );
if ( ++mBenchmarkCollected < mBenchmarkTarget )
{
return;
}
// Target reached: summarise each segment and the total, then exit.
static constexpr std::array < const char*, kGpuTimestampMarks - 1 > kSegmentNames =
{
"shadows+setup", "depth prepass", "hiz+lightcull", "shading", "debug+overlay"
};
auto stat = [] ( std::vector<double> v, double& mn, double& med, double& mean, double& p95, double& mx )
{
std::sort ( v.begin(), v.end() );
mn = v.front();
mx = v.back();
med = v[v.size() / 2];
p95 = v[std::min ( v.size() - 1, static_cast<size_t> ( static_cast<double> ( v.size() ) * 0.95 ) )];
double sum = 0.0;
for ( double x : v )
{
sum += x;
}
mean = sum / static_cast<double> ( v.size() );
};
const char* occ_env = std::getenv ( "AEON_HIZ_OCCLUSION" );
const bool occlusion_on = ( occ_env == nullptr ) || ( std::string_view ( occ_env ) != "0" );
std::cout << "\n=== GPU per-pass benchmark (" << mBenchmarkCollected
<< " frames, occlusion=" << ( occlusion_on ? "ON" : "OFF" ) << ") ===\n";
std::cout << std::left << std::setw ( 16 ) << "segment"
<< std::right << std::setw ( 9 ) << "min" << std::setw ( 9 ) << "median"
<< std::setw ( 9 ) << "mean" << std::setw ( 9 ) << "p95"
<< std::setw ( 9 ) << "max" << " (ms)\n";
std::cout << std::fixed << std::setprecision ( 3 );
double mn = 0.0, med = 0.0, mean = 0.0, p95 = 0.0, mx = 0.0;
for ( uint32_t i = 0; i + 1 < kGpuTimestampMarks; ++i )
{
if ( mBenchmarkSegments[i].empty() )
{
continue;
}
stat ( mBenchmarkSegments[i], mn, med, mean, p95, mx );
std::cout << std::left << std::setw ( 16 ) << kSegmentNames[i]
<< std::right << std::setw ( 9 ) << mn << std::setw ( 9 ) << med
<< std::setw ( 9 ) << mean << std::setw ( 9 ) << p95 << std::setw ( 9 ) << mx << "\n";
}
std::cout << std::string ( 61, '-' ) << "\n";
stat ( mBenchmarkTotals, mn, med, mean, p95, mx );
std::cout << std::left << std::setw ( 16 ) << "TOTAL"
<< std::right << std::setw ( 9 ) << mn << std::setw ( 9 ) << med
<< std::setw ( 9 ) << mean << std::setw ( 9 ) << p95 << std::setw ( 9 ) << mx
<< "\n" << std::endl;
std::exit ( 0 );
}
void Renderer::SetDebugRendering ( bool aEnabled )
{
mDebugRendering = aEnabled;
}
bool Renderer::GetDebugRendering() const
{
return mDebugRendering;
}
void Renderer::SetLightTypeEnabled ( LightType aType, bool aEnabled )
{
const uint32_t bit = 1u << static_cast<uint32_t> ( aType );
if ( aEnabled )
{
mLightTypeMask |= bit;
}
else
{
mLightTypeMask &= ~bit;
}
}
bool Renderer::GetLightTypeEnabled ( LightType aType ) const
{
return ( mLightTypeMask & ( 1u << static_cast<uint32_t> ( aType ) ) ) != 0u;
}
void Renderer::ToggleLightType ( LightType aType )
{
mLightTypeMask ^= ( 1u << static_cast<uint32_t> ( aType ) );
}
std::span<const GpuLight> Renderer::FilterLightsByType ( std::span<const GpuLight> aLights ) const
{
// Fast path: every type enabled, no filtering needed.
constexpr uint32_t all_types =
( 1u << static_cast<uint32_t> ( LightType::Point ) ) |
( 1u << static_cast<uint32_t> ( LightType::Spot ) ) |
( 1u << static_cast<uint32_t> ( LightType::Directional ) );
if ( ( mLightTypeMask & all_types ) == all_types )
{
return aLights;
}
mFilteredLights.clear();
for ( const GpuLight& light : aLights )
{
if ( mLightTypeMask & ( 1u << light.type ) )
{
mFilteredLights.push_back ( light );
}
}
return mFilteredLights;
}
void Renderer::SetDebugRenderSettings ( const DebugRenderSettings& aSettings )
{
mDebugSettings = aSettings;
mDebugSettingsDirty = true;
}
const DebugRenderSettings& Renderer::GetDebugRenderSettings() const
{
return mDebugSettings;
}
void Renderer::EnsureDebugAssets()
{
if ( mDebugAssetsLoaded )
{
return;
}
mDebugPipeline = std::make_unique<Pipeline>();
mDebugWireMesh = std::make_unique<Mesh>();
mDebugAABBMaterial = std::make_unique<Material>();
mDebugOctreeMaterial = std::make_unique<Material>();
mDebugFrustumMaterial = std::make_unique<Material>();
mDebugGridPipeline = std::make_unique<Pipeline>();
mDebugGridMesh = std::make_unique<Mesh>();
mDebugGridMaterial = std::make_unique<Material>();
mDebugPipeline->LoadFromFile ( "shaders/solid_color" );
mDebugWireMesh->LoadFromFile ( "meshes/aabb_wire.msh" );
mDebugAABBMaterial->LoadFromFile ( "materials/solidcolor" );
mDebugOctreeMaterial->LoadFromFile ( "materials/solidcolor" );
mDebugFrustumMaterial->LoadFromFile ( "materials/solidcolor" );
mDebugGridPipeline->LoadFromFile ( "shaders/debug_grid" );
mDebugGridMesh->LoadFromFile ( "meshes/fullscreen_triangle.msh" );
mDebugGridMaterial->LoadFromFile ( "materials/debug_grid" );
mDebugAssetsLoaded = true;
}
void Renderer::SubmitDebugGeometry ( void* aWindowId, const Scene& aScene )
{
EnsureDebugAssets();
if ( mDebugSettingsDirty )
{
// Push the tunable grid parameters into the grid material once per
// change rather than every frame.
mDebugGridMaterial->Set ( { "GridColor", mDebugSettings.mGridColor } );
mDebugGridMaterial->Set ( { "GridMajorColor", mDebugSettings.mGridMajorColor } );
mDebugGridMaterial->Set ( { "AxisXColor", mDebugSettings.mAxisXColor } );
mDebugGridMaterial->Set ( { "AxisYColor", mDebugSettings.mAxisYColor } );
mDebugGridMaterial->Set ( { "CellSize", mDebugSettings.mGridCellSize } );
mDebugGridMaterial->Set ( { "MajorInterval", mDebugSettings.mGridMajorInterval } );
mDebugGridMaterial->Set ( { "FadeDistance", mDebugSettings.mGridFadeDistance } );
// Each wireframe category gets its own solid color.
mDebugAABBMaterial->Set ( { "SolidColor", mDebugSettings.mNodeAABBColor } );
mDebugOctreeMaterial->Set ( { "SolidColor", mDebugSettings.mOctreeColor } );
mDebugFrustumMaterial->Set ( { "SolidColor", mDebugSettings.mCameraFrustumColor } );
mDebugSettingsDirty = false;
}
// Infinite ground grid first: a single full-screen triangle the grid
// shader unprojects to the z = 0 plane. It writes true scene depth, so
// the wireframes drawn afterward depth-test against it correctly.
if ( mDebugSettings.mDrawGrid )
{
Render ( aWindowId, Matrix4x4{}, *mDebugGridMesh, *mDebugGridPipeline,
mDebugGridMaterial.get(), Topology::TRIANGLE_LIST );
}
const Frustum& frustum = GetFrustum ( aWindowId );
// Per-node world-space AABB wireframes for everything visible this frame.
// The unit (radii 1) wire cube is scaled and translated to each box by
// its transform; LINE_LIST topology draws the 12 edges.
if ( mDebugSettings.mDrawNodeAABBs )
{
aScene.CullVisible ( frustum, [this, aWindowId] ( const Node & aNode )
{
const AABB world = aNode.GetGlobalTransform() * aNode.GetAABB();
Render ( aWindowId, world.GetTransform(), *mDebugWireMesh, *mDebugPipeline,
mDebugAABBMaterial.get(), Topology::LINE_LIST );
} );
}
// Scene octree grid: one wire cube per allocated cell whose bounds are on
// screen, visualizing the spatial subdivision.
if ( mDebugSettings.mDrawOctree )
{
aScene.ForEachOctreeCell ( frustum, [this, aWindowId] ( const AABB & aBounds, uint32_t /*aDepth*/ )
{
Render ( aWindowId, aBounds.GetTransform(), *mDebugWireMesh, *mDebugPipeline,
mDebugOctreeMaterial.get(), Topology::LINE_LIST );
} );
}
// Camera frustums: the NDC cube ([-1, 1] wire cube) maps to a camera's
// world-space frustum via nodeGlobalTransform * inverse(projection).
// Uses the window's active projection (correct aspect already baked in);
// drawn for every node carrying a Camera component, regardless of
// visibility, so off-screen cameras still show.
if ( mDebugSettings.mDrawCameraFrustums )
{
Matrix4x4 inverse_projection = GetProjectionMatrix ( aWindowId );
inverse_projection.Invert();
aScene.LoopTraverseDFSPreOrder ( [this, aWindowId, &inverse_projection] ( const Node & aNode )
{
if ( aNode.GetComponent ( "Camera"_crc32 ) == nullptr )
{
return;
}
const Matrix4x4 model = Matrix4x4{ aNode.GetGlobalTransform() } * inverse_projection;
Render ( aWindowId, model, *mDebugWireMesh, *mDebugPipeline,
mDebugFrustumMaterial.get(), Topology::LINE_LIST );
} );
}
}
}