Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Framebuffer.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15
16#include "./Framebuffer.hpp"
17#include "./GfxDevice.hpp"
18#include "../../core/Log.hpp"
19
20#include <algorithm>
21
22namespace esengine {
23
24// =============================================================================
25// Constructor / Destructor
26// =============================================================================
27
29 cleanup();
30}
31
33 : device_(other.device_),
34 spec_(other.spec_),
35 handle_(other.handle_),
36 colorAttachment_(other.colorAttachment_),
37 depthAttachment_(other.depthAttachment_),
38 resolveColor_(other.resolveColor_),
39 resolveDepth_(other.resolveDepth_),
40 colorAttachment1_(other.colorAttachment1_),
41 resolveColor1_(other.resolveColor1_),
42 colorAttachment2_(other.colorAttachment2_),
43 resolveColor2_(other.resolveColor2_) {
44 other.colorAttachment1_ = TextureHandle::Invalid;
45 other.resolveColor1_ = TextureHandle::Invalid;
46 other.colorAttachment2_ = TextureHandle::Invalid;
47 other.resolveColor2_ = TextureHandle::Invalid;
48 other.handle_ = FramebufferHandle::Default;
49 other.colorAttachment_ = TextureHandle::Invalid;
50 other.depthAttachment_ = TextureHandle::Invalid;
51 other.resolveColor_ = TextureHandle::Invalid;
52 other.resolveDepth_ = TextureHandle::Invalid;
53}
54
56 if (this != &other) {
57 cleanup();
58 device_ = other.device_;
59 spec_ = other.spec_;
60 handle_ = other.handle_;
61 colorAttachment_ = other.colorAttachment_;
62 depthAttachment_ = other.depthAttachment_;
63 resolveColor_ = other.resolveColor_;
64 resolveDepth_ = other.resolveDepth_;
65 colorAttachment1_ = other.colorAttachment1_;
66 resolveColor1_ = other.resolveColor1_;
67 colorAttachment2_ = other.colorAttachment2_;
68 resolveColor2_ = other.resolveColor2_;
69 other.colorAttachment1_ = TextureHandle::Invalid;
70 other.resolveColor1_ = TextureHandle::Invalid;
71 other.colorAttachment2_ = TextureHandle::Invalid;
72 other.resolveColor2_ = TextureHandle::Invalid;
73 other.handle_ = FramebufferHandle::Default;
74 other.colorAttachment_ = TextureHandle::Invalid;
75 other.depthAttachment_ = TextureHandle::Invalid;
76 other.resolveColor_ = TextureHandle::Invalid;
77 other.resolveDepth_ = TextureHandle::Invalid;
78 }
79 return *this;
80}
81
82// =============================================================================
83// Creation
84// =============================================================================
85
87 auto framebuffer = makeUnique<Framebuffer>();
88 framebuffer->device_ = &device;
89 framebuffer->spec_ = spec;
90
91 if (!framebuffer->initialize()) {
92 ES_LOG_ERROR("Failed to create framebuffer");
93 return nullptr;
94 }
95
96 return framebuffer;
97}
98
99// =============================================================================
100// Operations
101// =============================================================================
102
103void Framebuffer::bind() const {
104 if (device_) device_->beginRenderPass({handle_});
105}
106
108 if (device_) device_->endRenderPass();
109}
110
111void Framebuffer::resize(u32 width, u32 height) {
112 if (width == 0 || height == 0 || width > 8192 || height > 8192) {
113 ES_LOG_WARN("Invalid framebuffer size: {}x{}", width, height);
114 return;
115 }
116
117 spec_.width = width;
118 spec_.height = height;
119
120 cleanup();
121 if (!initialize()) {
122 ES_LOG_ERROR("Framebuffer resize failed: {}x{}", width, height);
123 }
124}
125
126// =============================================================================
127// Private Methods
128// =============================================================================
129
130bool Framebuffer::initialize() {
132 // The backend's ceiling, not the caller's wish: asking a backend with no MSAA
133 // for samples would leave a resolve reading an attachment nothing multisampled.
134 const u32 samples = std::min(std::max(spec_.samples, 1u), device_->maxSamples());
135
136 TextureDesc colorDesc;
137 colorDesc.width = spec_.width;
138 colorDesc.height = spec_.height;
139 colorDesc.format = spec_.colorFormat;
140 colorDesc.minFilter = filter;
141 colorDesc.magFilter = filter;
142 colorDesc.samples = samples;
143 colorDesc.mipmaps = spec_.mipmaps && samples == 1;
144 colorDesc.asyncShareable = true;
145 colorDesc.storage = spec_.storage && samples == 1 && device_->supportsCompute();
146 colorAttachment_ = device_->createTexture(colorDesc, GfxContent::transient(), nullptr);
147
148 // The surface's own record: 8 bits a channel is what an octahedral normal, a roughness and a metalness
149 // need, and a float target would spend four times the bandwidth for precision none of them uses.
150 if (spec_.gbuffer) {
151 TextureDesc gDesc = colorDesc;
153 // Nearest: a normal or a roughness blended with its neighbour's is neither's.
156 colorAttachment1_ = device_->createTexture(gDesc, GfxContent::transient(), nullptr);
157 // Beside it, the surface's diffuse colour: light an effect adds after the frame lit it (bounce
158 // light) is coloured by what it lands on, and the lit picture cannot say that where the frame left it dark.
159 colorAttachment2_ = device_->createTexture(gDesc, GfxContent::transient(), nullptr);
160 if (samples > 1) {
161 gDesc.samples = 1;
162 resolveColor1_ = device_->createTexture(gDesc, GfxContent::transient(), nullptr);
163 resolveColor2_ = device_->createTexture(gDesc, GfxContent::transient(), nullptr);
164 }
165 }
166
167 TextureDesc depthDesc;
168 depthDesc.width = spec_.width;
169 depthDesc.height = spec_.height;
170 depthDesc.format = GfxPixelFormat::Depth24Stencil8;
171 depthDesc.minFilter = TextureFilter::Nearest;
172 depthDesc.magFilter = TextureFilter::Nearest;
173 if (spec_.depthStencil) {
174 depthDesc.samples = samples;
175 depthAttachment_ = device_->createTexture(depthDesc, GfxContent::transient(), nullptr);
176 }
177
178 // The single-sample twins the multisampled attachments resolve into. They
179 // belong to this target: it is what makes a multisampled target sampleable,
180 // and no pass or effect has to know it happened.
181 if (samples > 1) {
182 colorDesc.samples = 1;
183 resolveColor_ = device_->createTexture(colorDesc, GfxContent::transient(), nullptr);
184 // Depth resolves only where the backend can: WebGPU has no depth resolve,
185 // and a twin nothing ever writes would hand an effect an empty depth
186 // buffer that looks like a working one.
187 if (spec_.depthStencil && device_->resolvesDepth()) {
188 depthDesc.samples = 1;
189 resolveDepth_ = device_->createTexture(depthDesc, GfxContent::transient(), nullptr);
190 }
191 }
192
193 FramebufferDesc fb;
194 fb.color0 = colorAttachment_;
195 fb.depthStencil = depthAttachment_;
196 fb.resolveColor0 = resolveColor_;
197 fb.resolveDepthStencil = resolveDepth_;
198 fb.color1 = colorAttachment1_;
199 fb.resolveColor1 = resolveColor1_;
200 fb.color2 = colorAttachment2_;
201 fb.resolveColor2 = resolveColor2_;
202 fb.width = spec_.width;
203 fb.height = spec_.height;
204 handle_ = device_->createFramebuffer(fb);
205 if (handle_ == FramebufferHandle::Default) {
206 ES_LOG_ERROR("Framebuffer is incomplete! (size: {}x{}, GL error 0x{:X})",
207 spec_.width, spec_.height, device_->getError());
208 cleanup();
209 return false;
210 }
211 return true;
212}
213
214void Framebuffer::cleanup() {
215 if (!device_) return;
216
217 if (resolveColor_ != TextureHandle::Invalid) {
218 device_->deleteTexture(resolveColor_);
219 resolveColor_ = TextureHandle::Invalid;
220 }
221 if (resolveColor1_ != TextureHandle::Invalid) {
222 device_->deleteTexture(resolveColor1_);
223 resolveColor1_ = TextureHandle::Invalid;
224 }
225 if (colorAttachment1_ != TextureHandle::Invalid) {
226 device_->deleteTexture(colorAttachment1_);
227 colorAttachment1_ = TextureHandle::Invalid;
228 }
229 if (resolveColor2_ != TextureHandle::Invalid) {
230 device_->deleteTexture(resolveColor2_);
231 resolveColor2_ = TextureHandle::Invalid;
232 }
233 if (colorAttachment2_ != TextureHandle::Invalid) {
234 device_->deleteTexture(colorAttachment2_);
235 colorAttachment2_ = TextureHandle::Invalid;
236 }
237 if (resolveDepth_ != TextureHandle::Invalid) {
238 device_->deleteTexture(resolveDepth_);
239 resolveDepth_ = TextureHandle::Invalid;
240 }
241 if (colorAttachment_ != TextureHandle::Invalid) {
242 device_->deleteTexture(colorAttachment_);
243 colorAttachment_ = TextureHandle::Invalid;
244 }
245
246 if (depthAttachment_ != TextureHandle::Invalid) {
247 device_->deleteTexture(depthAttachment_);
248 depthAttachment_ = TextureHandle::Invalid;
249 }
250
251 if (handle_ != FramebufferHandle::Default) {
252 device_->deleteFramebuffer(handle_);
254 }
255}
256
257} // namespace esengine
GPU framebuffer abstraction for render-to-texture.
Abstract graphics device interface.
Logging utilities for ESEngine.
#define ES_LOG_WARN(...)
Logs a warning message.
Definition Log.hpp:294
#define ES_LOG_ERROR(...)
Logs an error message.
Definition Log.hpp:296
Framebuffer & operator=(const Framebuffer &)=delete
void bind() const
Binds the framebuffer for rendering.
static Unique< Framebuffer > create(GfxDevice &device, const FramebufferSpec &spec)
Creates a framebuffer from specification.
void unbind() const
Unbinds the framebuffer.
void resize(u32 width, u32 height)
Resizes the framebuffer.
static constexpr GfxContent transient()
Abstract graphics device interface.
Definition GfxDevice.hpp:75
virtual u32 maxSamples()=0
Most samples an offscreen colour attachment may carry; 1 = none.
TextureHandle createTexture(const TextureDesc &desc, GfxContent content, const void *pixels)
Creates a 2D texture; uploads pixels (tightly packed, desc-sized) when non-null.
virtual bool supportsCompute() const
Whether this device runs compute shaders.
constexpr Unique< T > makeUnique(Args &&... args)
Creates a Unique pointer with the given arguments.
Definition Types.hpp:99
@ Linear
Bilinear interpolation (smooth).
Definition GfxEnums.hpp:59
@ Nearest
No interpolation (pixelated look).
Definition GfxEnums.hpp:58
std::unique_ptr< T > Unique
Unique ownership smart pointer.
Definition Types.hpp:75
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
Framebuffer creation parameters.
GfxPixelFormat colorFormat
Color attachment format. RGBA16F needs GfxDevice::supportsFloatTargets.
bool gbuffer
A second colour attachment (RGBA8) drawn in the same pass: the surface's material,...
bool linearFilter
Texture filter mode: false=GL_NEAREST, true=GL_LINEAR.
u32 samples
Number of samples for MSAA (1 = no multisampling).
u32 height
Framebuffer height in pixels.
bool storage
The colour attachment can also be written by a compute shader (GfxDevice::supportsCompute).
u32 width
Framebuffer width in pixels.
Immutable creation parameters of a 2D texture or 2D texture array.
Definition GfxEnums.hpp:414
bool asyncShareable
Allow a native compute queue to share this single-sample colour target.
Definition GfxEnums.hpp:441
TextureFilter minFilter
Definition GfxEnums.hpp:422
TextureFilter magFilter
Definition GfxEnums.hpp:423
GfxPixelFormat format
Definition GfxEnums.hpp:421