Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
RenderContext.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17// =============================================================================
18// Includes
19// =============================================================================
20
21// Project includes
22#include "../../core/Types.hpp"
23#include "../rhi/Buffer.hpp"
24#include "../rhi/GfxEnums.hpp"
30#include "../rhi/Shader.hpp"
31#include "./QualityValues.hpp"
32
33// Third-party
34#include <glm/glm.hpp>
35
36namespace esengine {
37
38// =============================================================================
39// RenderContext Class
40// =============================================================================
41
61class GfxDevice;
62
64public:
65 explicit RenderContext(GfxDevice& device);
66 RenderContext() = delete;
68
69 // Non-copyable
70 RenderContext(const RenderContext&) = delete;
72
73 // =========================================================================
74 // Lifecycle
75 // =========================================================================
76
81 void init();
82
86 void shutdown();
87
92
97 bool isInitialized() const { return initialized_; }
98
99 // =========================================================================
100 // Program readiness
101 // =========================================================================
102
110 u64 programEpoch() const { return program_epoch_; }
111
120 void advanceProgramEpoch() { ++program_epoch_; }
121
122 // =========================================================================
123 // State Access
124 // =========================================================================
125
130 glm::mat4& viewProjection() { return viewProjection_; }
131
136 const glm::mat4& viewProjection() const { return viewProjection_; }
137
138 // =========================================================================
139 // Internal Resources
140 // =========================================================================
141
145 u32 getWhiteTextureId() const { return static_cast<u32>(whiteTexture_); }
146
149 TextureHandle getWhiteTexture() const { return whiteTexture_; }
150
151
157 void updateCameraConstants(const glm::mat4& viewProjection);
165 void updateCameraConstants(const glm::mat4& viewProjection, const glm::vec2& jitterNdc);
166
174 void updateFrameConstants(const glm::mat4& viewProjection);
175
183 void useFrameConstants(BufferHandle ubo, const glm::mat4& viewProjection);
185 void prepareFrameConstants(BufferHandle ubo, const glm::mat4& viewProjection);
187
189 BufferHandle frameUbo() const { return frameUbo_; }
190
192 static u32 frameConstantsSize();
193
195 void setFrameTime(f32 elapsedSec, u32 viewportW, u32 viewportH);
196
202 void setViewExposure(f32 ev) { viewExposure_ = ev; }
203 void setQualityConstants(const glm::vec4& values) { qualityConstants_ = values; }
204 f32 viewExposure() const { return viewExposure_; }
205
206 QualityValues& qualityValues() { return qualityValues_; }
207 const QualityValues& qualityValues() const { return qualityValues_; }
208
215 MaterialStore& materials() { return materials_; }
216 const MaterialStore& materials() const { return materials_; }
217
224 LightStore& lights() { return lights_; }
225 const LightStore& lights() const { return lights_; }
226
229 ProbeStore& probes() { return probes_; }
230 const ProbeStore& probes() const { return probes_; }
231
234 ReflectionStore& reflections() { return reflections_; }
235 const ReflectionStore& reflections() const { return reflections_; }
236
237private:
238 void initDefaultTextures();
239 TextureHandle make1x1Texture(u32 rgba, u32 layers = 1);
240 TextureHandle makeRipplesTexture();
241 void initFrameUbo();
242
243 void uploadFrameConstants(const glm::mat4& upload, const glm::mat4& engine);
244
245 glm::mat4 viewProjection_{1.0f};
246 u64 program_epoch_ = 0;
247
250 TextureHandle whiteArrayTexture_ = TextureHandle::Invalid;
251 TextureHandle blackArrayTexture_ = TextureHandle::Invalid;
252 TextureHandle flatNormalArrayTexture_ = TextureHandle::Invalid;
254 TextureHandle flatNormalTexture_ = TextureHandle::Invalid;
255 TextureHandle ripplesTexture_ = TextureHandle::Invalid;
258 BufferHandle drawParamsFallback_ = BufferHandle::Invalid;
262 PerDrawBlocks skinBlocks_;
263 PerDrawBlocks probeBlocks_;
264 PerDrawBlocks instanceBlocks_;
265 PerDrawBlocks planarBlocks_;
266
267public:
270 return {&skinBlocks_, &probeBlocks_, &instanceBlocks_, &planarBlocks_};
271 }
272
274 skinBlocks_.beginFrame();
275 probeBlocks_.beginFrame();
276 instanceBlocks_.beginFrame();
277 planarBlocks_.beginFrame();
278 }
279
280private:
281 f32 lastElapsed_ = 0.0f;
282 f32 viewExposure_ = 0.0f;
283 glm::vec4 qualityConstants_{0.0f};
284 QualityValues qualityValues_;
285 MaterialStore materials_;
286 LightStore lights_;
287 ProbeStore probes_;
288 ReflectionStore reflections_;
289
290 GfxDevice& device_;
291 bool initialized_ = false;
292};
293
294} // namespace esengine
GPU buffer abstractions for vertex and index data.
Backend-agnostic graphics enumerations.
Engine-side 2D light registry — the per-frame LightConstants UBO and its CPU mirror.
Engine-side material registry — the resolved "how to draw" for a material handle.
A uniform range per draw, so two draws never write into one block.
Which baked grid a point stands in, and what it says there.
Which baked reflection a point stands in — the specular half of the question ProbeStore answers for i...
GPU shader program abstraction.
Core type definitions and utilities for ESEngine.
Abstract graphics device interface.
Definition GfxDevice.hpp:75
Holds the per-frame 2D lights and their GPU UBO. Mirrors MaterialStore's UBO lifecycle (lazy create,...
Maps a material handle to its resolved record. Owned by RenderContext so it is reachable from both th...
The frame's probe volumes, and the trilinear read through them.
The frame's reflection probes, and the lookup that picks one.
const glm::mat4 & viewProjection() const
Gets the current view-projection matrix (const).
ReflectionStore & reflections()
The frame's reflection probes — the same question about the half of the environment a surface MIRRORS...
void advanceProgramEpoch()
Declares that programs which WERE ready have gone cold.
static u32 frameConstantsSize()
Bytes a buffer passed to useFrameConstants must hold.
void prepareFrameConstants(BufferHandle ubo, const glm::mat4 &viewProjection)
Stage a light view before the render passes and switch to it without a mid-pass upload.
u64 programEpoch() const
Which generation of renderer PROGRAM readiness is current.
void setQualityConstants(const glm::vec4 &values)
bool isInitialized() const
Checks if the context is initialized.
void restoreBuiltinContent()
Builds again what this context made from code alone and the device lost: call once the device is back...
const ProbeStore & probes() const
void updateCameraConstants(const glm::mat4 &viewProjection)
Point the frame at what a CAMERA sees.
LightStore & lights()
The engine-side per-frame 2D light registry (binding 2 LightConstants UBO).
void bindFrameConstants(BufferHandle ubo)
const ReflectionStore & reflections() const
void init()
Initializes rendering resources.
void setFrameTime(f32 elapsedSec, u32 viewportW, u32 viewportH)
Uploads the frame clock + canvas size into the injected TimeConstants UBO.
void beginPerDrawBlocks()
Hand every one of them back at the top of a frame.
MaterialStore & materials()
The engine-side material registry (handle -> resolved render state).
glm::mat4 & viewProjection()
Gets the current view-projection matrix.
const MaterialStore & materials() const
BufferHandle frameUbo() const
The buffer the camera's FrameConstants live in, for a compute pass that reads them (it is bound at gr...
PerDrawBlockSet perDrawBlocks()
The blocks the draw loop rewrites before each draw.
void updateFrameConstants(const glm::mat4 &viewProjection)
Point the frame at a pass whose projection already suits the device.
RenderContext(const RenderContext &)=delete
ProbeStore & probes()
The frame's probe volumes. Gathered beside the lights, because it answers the same question at the po...
u32 getWhiteTextureId() const
Gets the white texture handle's raw value (for untextured quads).
TextureHandle getWhiteTexture() const
RenderContext & operator=(const RenderContext &)=delete
const LightStore & lights() const
void useFrameConstants(BufferHandle ubo, const glm::mat4 &viewProjection)
Frame constants for viewProjection in a buffer the CALLER owns, bound.
const QualityValues & qualityValues() const
RenderContext(GfxDevice &device)
QualityValues & qualityValues()
void setViewExposure(f32 ev)
The editor view's exposure, in stops, carried to the output blit in u_time.z. VIEW state: zero (the d...
void shutdown()
Releases all rendering resources.
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
float f32
32-bit floating point
Definition Types.hpp:59
The uniform blocks a draw rewrites immediately before it.