29#include <glm/gtc/type_ptr.hpp>
50 initDefaultTextures();
52 materials_.setDevice(&device_);
53 materials_.setBuiltinDefaults(whiteTexture_, blackTexture_, flatNormalTexture_, ripplesTexture_);
54 materials_.setBuiltinDefaultArrays(whiteArrayTexture_, blackArrayTexture_, flatNormalArrayTexture_);
55 lights_.setDevice(&device_);
65 for (
TextureHandle* tex : {&whiteTexture_, &blackTexture_, &flatNormalTexture_, &ripplesTexture_, &whiteArrayTexture_,
66 &blackArrayTexture_, &flatNormalArrayTexture_}) {
68 device_.deleteTexture(*tex);
74 device_.deleteBuffer(frameUbo_);
78 device_.deleteBuffer(timeUbo_);
82 device_.deleteBuffer(drawParamsFallback_);
85 skinBlocks_.shutdown();
86 probeBlocks_.shutdown();
87 instanceBlocks_.shutdown();
88 planarBlocks_.shutdown();
91 materials_.setResourceManager(
nullptr);
106 const std::vector<u32> texels(layers, rgba);
112constexpr u32 BUILTIN_CONTENT_PROVIDER = 0x4255494C;
113constexpr u32 BUILTIN_RIPPLES = 1;
125 device_.createTexture(desc,
GfxContent::sourced(BUILTIN_CONTENT_PROVIDER, BUILTIN_RIPPLES), pixels.data());
134 device_.forgoContent(owed);
139 device_.restoreContent(owed);
143void RenderContext::initDefaultTextures() {
145 whiteTexture_ = make1x1Texture(0xFFFFFFFF);
146 blackTexture_ = make1x1Texture(0xFF000000);
147 flatNormalTexture_ = make1x1Texture(0xFFFF8080);
148 whiteArrayTexture_ = make1x1Texture(0xFFFFFFFF, 2);
149 blackArrayTexture_ = make1x1Texture(0xFF000000, 2);
150 flatNormalArrayTexture_ = make1x1Texture(0xFFFF8080, 2);
151 ripplesTexture_ = makeRipplesTexture();
152 ES_LOG_DEBUG(
"Default textures created (white {}, black {}, flatNormal {})",
153 static_cast<u32>(whiteTexture_),
static_cast<u32>(blackTexture_),
154 static_cast<u32>(flatNormalTexture_));
157void RenderContext::initFrameUbo() {
158 FrameConstants initial{};
159 frameUbo_ = device_.createBuffer(
168 TimeConstants time{};
169 timeUbo_ = device_.createBuffer(
180 drawParamsFallback_ = device_.createBuffer(
189 skinBlocks_.init(device_,
static_cast<u32>(
sizeof(SkinConstants)));
194 instanceBlocks_.init(device_,
static_cast<u32>(
sizeof(InstanceConstants)));
195 planarBlocks_.init(device_,
static_cast<u32>(
sizeof(PlanarConstants)));
202 ES_LOG_DEBUG(
"FrameConstants UBO created (handle: {})",
static_cast<u32>(frameUbo_));
214 for (
int col = 0; col < 4; ++col) {
252void RenderContext::uploadFrameConstants(
const glm::mat4& upload,
const glm::mat4& engine) {
253 viewProjection_ = engine;
258 device_.
updateBuffer(frameUbo_, 0, &frame,
sizeof(FrameConstants));
270 const f32 dt = (lastElapsed_ > 0.0f && elapsedSec > lastElapsed_) ? elapsedSec - lastElapsed_ : 0.0f;
271 lastElapsed_ = elapsedSec;
272 const f32 w =
static_cast<f32>(viewportW);
273 const f32 h =
static_cast<f32>(viewportH);
275 glm::vec4(elapsedSec, dt, viewExposure_, 0.0f),
276 glm::vec4(w, h, w > 0.0f ? 1.0f / w : 0.0f, h > 0.0f ? 1.0f / h : 0.0f),
279 device_.updateBuffer(timeUbo_, 0, &time,
sizeof(
TimeConstants));
285 device_.resizeBackbuffer(viewportW, viewportH);
Per-draw shader parameter block — the UBO home for what used to be loose uniforms.
Per-frame shader constants shared via a single Uniform Buffer Object.
Abstract graphics device interface.
WHERE one draw's entry is in the frame's per-object record texture, and what an entry says.
Logging utilities for ESEngine.
#define ES_LOG_WARN(...)
Logs a warning message.
#define ES_LOG_INFO(...)
Logs an info message.
#define ES_LOG_DEBUG(...)
The indirect light where ONE draw stands, when a grid was baked for it.
Rendering context containing all renderer state.
The point the frame is drawn relative to: a world far from zero loses precision in single floats,...
The pose one skinned draw is deformed by: a bone matrix per joint.
static constexpr GfxContent retained()
static constexpr GfxContent sourced(u32 provider, u32 key)
Abstract graphics device interface.
void setUniformBuffer(u32 slot, BufferHandle buffer, u32 offsetBytes=0, u32 sizeBytes=0)
Binds a buffer to a uniform binding slot (the block index shaders are linked to).
TextureHandle createTexture(const TextureDesc &desc, GfxContent content, const void *pixels)
Creates a 2D texture; uploads pixels (tightly packed, desc-sized) when non-null.
void updateBuffer(BufferHandle buffer, u32 offsetBytes, const void *data, u32 sizeBytes)
Updates a sub-range of a buffer; must fit within its capacity.
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.
void restoreBuiltinContent()
Builds again what this context made from code alone and the device lost: call once the device is back...
void updateCameraConstants(const glm::mat4 &viewProjection)
Point the frame at what a CAMERA sees.
void bindFrameConstants(BufferHandle ubo)
void init()
Initializes rendering resources.
void setFrameTime(f32 elapsedSec, u32 viewportW, u32 viewportH)
Uploads the frame clock + canvas size into the injected TimeConstants UBO.
glm::mat4 & viewProjection()
Gets the current view-projection matrix.
void updateFrameConstants(const glm::mat4 &viewProjection)
Point the frame at a pass whose projection already suits the device.
void useFrameConstants(BufferHandle ubo, const glm::mat4 &viewProjection)
Frame constants for viewProjection in a buffer the CALLER owns, bound.
void shutdown()
Releases all rendering resources.
constexpr u32 FRAME_CONSTANTS_BINDING
Indexed uniform binding point the FrameConstants block is bound to.
constexpr u32 RIPPLE_NORMALS_SIZE
@ Repeat
Tile the texture.
constexpr u32 PROBE_CONSTANTS_BINDING
Indexed UBO binding point for the per-draw ProbeConstants block.
@ Nearest
No interpolation (pixelated look).
constexpr u32 DRAW_PARAMS_FALLBACK_SIZE
Size of the zeroed fallback UBO RenderContext binds at DRAW_PARAMS_BINDING each frame.
constexpr u32 TIME_CONSTANTS_BINDING
glm::mat4 toClipDepthRange(const glm::mat4 &p, ClipDepthRange range)
The projection p as a device keeping range wants it.
std::uint32_t u32
Unsigned 32-bit integer.
constexpr u32 DRAW_PARAMS_BINDING
Indexed UBO binding point for the per-draw DrawParams block. (0 = FrameConstants, 1 = MaterialConstan...
glm::dvec3 & renderOrigin()
The origin, in world units. A double, as the world positions it is taken from are far from zero.
constexpr u32 PLANAR_CONSTANTS_BINDING
std::vector< u8 > buildRippleNormals()
RGBA8, RIPPLE_NORMALS_SIZE square. A texel's rg (and ba) is the xy of a unit normal whose z is up: th...
constexpr u32 PROBE_BLOCK_BYTES
Bytes the block is declared with: every run, then one vec4 that is not data.
glm::vec4 cameraFromViewProjection(const glm::mat4 &viewProjection)
Where the frame is seen from, read out of the matrix that projects it.
constexpr u32 INSTANCE_CONSTANTS_BINDING
Indexed UBO binding point for the per-draw InstanceConstants block. Nine and not four: slot 4 is DRAW...
float f32
32-bit floating point
CPU mirror of the GLSL FrameConstants block (std140).
A Sourced object that has storage again and is waiting for its contents.
Immutable creation parameters of a 2D texture or 2D texture array.
CPU mirror of the injected TimeConstants block (std140).