Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
RenderTarget.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#include "./RenderTarget.hpp"
4#include "./GfxDevice.hpp"
5
6namespace esengine {
7
8void RenderTarget::init(GfxDevice& device, u32 width, u32 height, bool depth, bool linearFilter, bool floatColor) {
9 width_ = width;
10 height_ = height;
11 has_depth_ = depth;
12 linear_filter_ = linearFilter;
13
14 FramebufferSpec spec;
15 spec.width = width;
16 spec.height = height;
17 spec.depthStencil = depth;
18 spec.linearFilter = linearFilter;
19 if (floatColor && device.supportsFloatTargets()) spec.colorFormat = GfxPixelFormat::RGBA16F;
20
21 framebuffer_ = Framebuffer::create(device, spec);
22 if (!framebuffer_ && floatColor) {
24 framebuffer_ = Framebuffer::create(device, spec);
25 }
26 device_ = &device;
27}
28
30 framebuffer_.reset();
31 width_ = 0;
32 height_ = 0;
33}
34
36 if (framebuffer_) {
37 framebuffer_->bind();
38 }
39}
40
42 if (framebuffer_) {
43 framebuffer_->unbind();
44 }
45}
46
47void RenderTarget::resize(u32 width, u32 height) {
48 if (width == width_ && height == height_) return;
49
50 width_ = width;
51 height_ = height;
52
53 if (framebuffer_) {
54 framebuffer_->resize(width, height);
55 }
56}
57
59 return framebuffer_ ? framebuffer_->getColorAttachment() : TextureHandle::Invalid;
60}
61
63 return framebuffer_ ? framebuffer_->getDepthAttachment() : TextureHandle::Invalid;
64}
65
67 return framebuffer_ ? framebuffer_->handle() : FramebufferHandle::Default;
68}
69
70RenderTargetManager::Handle RenderTargetManager::create(u32 width, u32 height, bool depth, bool linearFilter, bool floatColor) {
71 Handle handle;
72
73 if (!free_list_.empty()) {
74 handle = free_list_.back();
75 free_list_.pop_back();
76 } else {
77 handle = next_handle_++;
78 targets_.resize(handle);
79 }
80
81 auto target = makeUnique<RenderTarget>();
82 target->init(*device_, width, height, depth, linearFilter, floatColor);
83
84 usize index = handle - 1;
85 if (index < targets_.size()) {
86 targets_[index] = std::move(target);
87 }
88
89 return handle;
90}
91
93 if (handle == INVALID_HANDLE || handle > targets_.size()) {
94 return nullptr;
95 }
96 return targets_[handle - 1].get();
97}
98
100 if (handle == INVALID_HANDLE || handle > targets_.size()) {
101 return;
102 }
103
104 auto& target = targets_[handle - 1];
105 if (target) {
106 target->shutdown();
107 target.reset();
108 free_list_.push_back(handle);
109 }
110}
111
113 if (handle == INVALID_HANDLE || handle > targets_.size()) {
114 return false;
115 }
116 return targets_[handle - 1] != nullptr;
117}
118
119} // namespace esengine
Abstract graphics device interface.
static Unique< Framebuffer > create(GfxDevice &device, const FramebufferSpec &spec)
Creates a framebuffer from specification.
Abstract graphics device interface.
Definition GfxDevice.hpp:75
virtual bool supportsFloatTargets()=0
Whether RGBA16F color attachments are renderable.
Handle create(u32 width, u32 height, bool depth=true, bool linearFilter=false, bool floatColor=false)
static constexpr Handle INVALID_HANDLE
void release(Handle handle)
RenderTarget * get(Handle handle)
bool isValid(Handle handle) const
TextureHandle getDepthTexture() const
TextureHandle getColorTexture() const
void init(GfxDevice &device, u32 width, u32 height, bool depth=true, bool linearFilter=false, bool floatColor=false)
void resize(u32 width, u32 height)
FramebufferHandle getFramebuffer() const
FramebufferHandle
Handle 0 is the default framebuffer (backbuffer); it cannot be created or deleted.
Definition GfxEnums.hpp:41
constexpr Unique< T > makeUnique(Args &&... args)
Creates a Unique pointer with the given arguments.
Definition Types.hpp:99
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
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 depthStencil
Whether to create depth/stencil attachment.
bool linearFilter
Texture filter mode: false=GL_NEAREST, true=GL_LINEAR.
u32 height
Framebuffer height in pixels.
u32 width
Framebuffer width in pixels.