GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Renderer2D.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_2D_RENDERER_2D_HPP
5#define GREM_GRAPHICS_2D_RENDERER_2D_HPP
6
8
15#include <GREM/core/extents.hpp>
22
23namespace grem::graphics {
24
25class Device; // Forward declaration, to avoid including Device.hpp.
26
38 [[nodiscard]] constexpr bool operator==(const Renderer2DOptions& other) const noexcept = default;
39};
40
47public:
50
53
56
59
73 GREM_API(graphics_2d) explicit Renderer2D(Device& device, const Renderer2DOptions& options = {});
74
76 GREM_API(graphics_2d) ~Renderer2D();
77
79 Renderer2D(const Renderer2D&) = delete;
80
83
85 Renderer2D& operator=(const Renderer2D&) = delete;
86
89
109 template <typename... ExtraBuffers>
110 void drawFrame(RenderPass& renderPass, StridedSpan<const Instances2DView> instanceBatches, const Camera2D& camera, const ExtraBuffers&... extraBuffers) {
111 Array<Pair<BufferLayoutReference, SharedPointer<void>>, sizeof...(ExtraBuffers)> extraBufferHandles{
112 Pair<BufferLayoutReference, SharedPointer<void>>{ExtraBuffers::LAYOUT_REFERENCE, extraBuffers.lock()}...,
113 };
114 drawFrameImplementation(renderPass, instanceBatches, camera, extraBufferHandles);
115 }
116
122 [[nodiscard]] const Texture& getInvisibleTexture2D() {
123 return invisibleTexture2D;
124 }
125
131 [[nodiscard]] const Texture& getInvisibleTexture2DArray() {
132 return invisibleTexture2DArray;
133 }
134
140 [[nodiscard]] const Texture& getInvisibleTextureCube() {
141 return invisibleTextureCube;
142 }
143
149 [[nodiscard]] const Texture& getInvisibleTextureCubeArray() {
150 return invisibleTextureCubeArray;
151 }
152
158 [[nodiscard]] const Texture& getWhiteTexture2D() {
159 return whiteTexture2D;
160 }
161
167 [[nodiscard]] const Texture& getWhiteTextureCube() {
168 return whiteTextureCube;
169 }
170
176 [[nodiscard]] const Texture& getFlatNormalTexture2D() {
177 return flatNormalTexture2D;
178 }
179
185 [[nodiscard]] const Texture& getDefaultDepthTexture2D() {
186 return defaultDepthTexture2D;
187 }
188
194 [[nodiscard]] const Texture& getDefaultDepthTexture2DArray() {
195 return defaultDepthTexture2DArray;
196 }
197
203 [[nodiscard]] const Texture& getDefaultDepthTextureCube() {
204 return defaultDepthTextureCube;
205 }
206
213 return defaultDepthTextureCubeArray;
214 }
215
221 [[nodiscard]] GREM_API(graphics_2d) const Model2D::VertexShader& getDefaultModel2DVertexShader();
222
228 [[nodiscard]] GREM_API(graphics_2d) const PlainModel2DFragmentShader& getPlainModel2DFragmentShader();
229
236 if (!plainModel2DShaderPipeline) {
237 [[unlikely]];
240 }
241 return *plainModel2DShaderPipeline;
242 }
243
249 [[nodiscard]] GREM_API(graphics_2d) const TextModel2DFragmentShader& getTextModel2DFragmentShader();
250
257 if (!textModel2DShaderPipeline) {
258 [[unlikely]];
261 }
262 return *textModel2DShaderPipeline;
263 }
264
270 [[nodiscard]] GREM_API(graphics_2d) const TonemappingModel2DFragmentShader& getTonemappingModel2DFragmentShader();
271
278 if (!tonemappingModel2DShaderPipeline) {
279 [[unlikely]];
281 shaderPipelineOptions.blendState.reset();
283 Model2D::DEFAULT_FRAGMENT_SHADER_CONSTANTS, shaderPipelineOptions);
284 }
285 return *tonemappingModel2DShaderPipeline;
286 }
287
300 [[nodiscard]] const Model2D& getUnitSquareModel2D() const noexcept {
301 return unitSquareModel2D;
302 }
303
314 [[nodiscard]] const Model2D& getUnitRightAngledTriangleModel2D() const noexcept {
315 return unitRightAngledTriangleModel2D;
316 }
317
318private:
319 friend Instances2D;
320
321 static constexpr TextureSamplerOptions DEFAULT_DEPTH_TEXTURE_SAMPLER_OPTIONS{
322 .minificationFilter = TextureFilter::NEAREST,
323 .magnificationFilter = TextureFilter::NEAREST,
324 .mipmapMode = TextureMipmapMode::NONE,
325 .horizontalWrappingMode = TextureWrappingMode::CLAMP_TO_EDGE,
326 .verticalWrappingMode = TextureWrappingMode::CLAMP_TO_EDGE,
327 .maxAnisotropy = 1.0f,
328 .depthComparisonMode = TextureDepthComparisonMode::LESS_OR_EQUAL,
329 };
330
331 GREM_API(graphics_2d) static const Array<Model2D::Vertex, 4> UNIT_SQUARE_MESH_2D_VERTICES;
332
333 template <typename... Buffers>
334 void setTemporaryCombinedBufferHandles(Span<const Pair<BufferLayoutReference, SharedPointer<void>>> extraBufferHandles, const Buffers&... buffers) {
335 temporaryCombinedBufferHandles.clear();
336 (temporaryCombinedBufferHandles.push_back(Pair<BufferLayoutReference, SharedPointer<void>>{Buffers::LAYOUT_REFERENCE, buffers.lock()}), ...);
337 for (const Pair<BufferLayoutReference, SharedPointer<void>>& extraBufferHandle : extraBufferHandles) {
338 temporaryCombinedBufferHandles.push_back(extraBufferHandle);
339 }
340 sortByAscending<&Pair<BufferLayoutReference, SharedPointer<void>>::first>(temporaryCombinedBufferHandles);
341 }
342
343 GREM_API(graphics_2d)
344 void drawFrameImplementation(RenderPass& renderPass, StridedSpan<const Instances2DView> instanceBatches, const Camera2D& camera,
345 Span<const Pair<BufferLayoutReference, SharedPointer<void>>> extraBufferHandles);
346
347 Device& device;
348 Texture invisibleTexture2D =
350 Texture invisibleTexture2DArray = Texture::create(device, TextureType::TEXTURE_2D_ARRAY, TextureFormat::R8G8B8A8_UNORM, Extent3D{1}, 1, ClearValues{.color = Color::INVISIBLE},
352 Texture invisibleTextureCube = Texture::create(device, TextureType::TEXTURE_CUBE, TextureFormat::R8G8B8A8_UNORM, Extent3D{1, 1, 6}, 1, ClearValues{.color = Color::INVISIBLE},
354 Texture invisibleTextureCubeArray = Texture::create(device, TextureType::TEXTURE_CUBE_ARRAY, TextureFormat::R8G8B8A8_UNORM, Extent3D{1, 1, 6}, 1,
356 Texture whiteTexture2D =
358 Texture whiteTextureCube = Texture::create(device, TextureType::TEXTURE_CUBE, TextureFormat::R8G8B8A8_UNORM, Extent3D{1, 1, 6}, 1, ClearValues{.color = Color::WHITE},
360 Texture flatNormalTexture2D = Texture::create(device, TextureType::TEXTURE_2D, TextureFormat::R8G8B8A8_UNORM, Extent2D{1}, 1,
361 ClearValues{.color = Color::fromLinear(0.5f, 0.5f, 1.0f)}, TextureSamplerOptions::UNFILTERED);
362 Texture defaultDepthTexture2D =
363 Texture::create(device, TextureType::TEXTURE_2D, TextureFormat::D16_UNORM, Extent2D{1}, 1, ClearValues{.depth = 1.0f}, DEFAULT_DEPTH_TEXTURE_SAMPLER_OPTIONS);
364 Texture defaultDepthTexture2DArray =
365 Texture::create(device, TextureType::TEXTURE_2D_ARRAY, TextureFormat::D16_UNORM, Extent3D{1}, 1, ClearValues{.depth = 1.0f}, DEFAULT_DEPTH_TEXTURE_SAMPLER_OPTIONS);
366 Texture defaultDepthTextureCube =
367 Texture::create(device, TextureType::TEXTURE_CUBE, TextureFormat::D16_UNORM, Extent3D{1, 1, 6}, 1, ClearValues{.depth = 1.0f}, DEFAULT_DEPTH_TEXTURE_SAMPLER_OPTIONS);
368 Texture defaultDepthTextureCubeArray =
369 Texture::create(device, TextureType::TEXTURE_CUBE_ARRAY, TextureFormat::D16_UNORM, Extent3D{1, 1, 6}, 1, ClearValues{.depth = 1.0f}, DEFAULT_DEPTH_TEXTURE_SAMPLER_OPTIONS);
370 Optional<Model2D::VertexShader> defaultModel2DVertexShader{};
371 Optional<PlainModel2DFragmentShader> plainModel2DFragmentShader{};
372 Optional<Model2D::ShaderPipeline> plainModel2DShaderPipeline{};
373 Optional<TextModel2DFragmentShader> textModel2DFragmentShader{};
374 Optional<Model2D::ShaderPipeline> textModel2DShaderPipeline{};
375 Optional<TonemappingModel2DFragmentShader> tonemappingModel2DFragmentShader{};
376 Optional<Model2D::ShaderPipeline> tonemappingModel2DShaderPipeline{};
377 Model2D unitSquareModel2D{device, *this, UNIT_SQUARE_MESH_2D_VERTICES};
378 Model2D unitRightAngledTriangleModel2D{device, *this, Span{UNIT_SQUARE_MESH_2D_VERTICES}.first<3>()};
379 Text2D temporaryText{};
380 ArrayList<Pair<BufferLayoutReference, SharedPointer<void>>> temporaryCombinedBufferHandles{};
381 DrawCommandBuffer<Model2D::Mesh> drawCommandBuffer2D{device};
382 Model2D::TextureBuffer textureBuffer2D{device};
383};
384
385} // namespace grem::graphics
386
387#endif
static const Color INVISIBLE
Definition Color.hpp:70
static constexpr Color fromLinear(float x, float a=1.0f) noexcept
Create a grayscale color from a linear scalar.
Definition Color.hpp:336
static const Color WHITE
Definition Color.hpp:215
Definition SharedPointer.hpp:65
Definition Span.hpp:146
Definition StridedSpan.hpp:427
Combined view-projection matrix, defining a 2D perspective to render from.
Definition Camera2D.hpp:109
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
Batch of instances to be drawn in a Renderer2D frame.
Definition Instances2D.hpp:586
Container for a set of 2D triangle strip meshes stored on the GPU.
Definition Model2D.hpp:30
UniformBuffer< TextureParameters, "Model2DTexture"> TextureBuffer
Shader buffer for 2D model texture parameters.
Definition Model2D.hpp:97
static constexpr FragmentShaderConstants DEFAULT_FRAGMENT_SHADER_CONSTANTS
Default 2D model fragment shader constants.
Definition Model2D.hpp:176
static constexpr VertexShaderConstants DEFAULT_VERTEX_SHADER_CONSTANTS
Default 2D model vertex shader constants.
Definition Model2D.hpp:171
graphics::ShaderPipeline< Mesh > ShaderPipeline
Shader pipeline for drawing 2D models.
Definition Model2D.hpp:152
static constexpr ShaderPipelineOptions DEFAULT_SHADER_PIPELINE_OPTIONS
Default 2D model graphics pipeline configuration.
Definition Model2D.hpp:181
VertexShaderBase< VertexShaderConstants, VertexShaderOutputs > VertexShader
Default vertex shader for drawing 2D models.
Definition Model2D.hpp:127
FragmentShaderBase< VertexShaderOutputs, FragmentShaderConstants, FragmentShaderOutputs > FragmentShader
Default fragment shader type for drawing 2D models.
Definition Model2D.hpp:147
Backend-specific implementation of RenderPass.
Definition RenderPass.hpp:38
const Model2D::ShaderPipeline & getTonemappingModel2DShaderPipeline()
Get the default tonemapping shader pipeline.
Definition Renderer2D.hpp:277
const Model2D::ShaderPipeline & getTextModel2DShaderPipeline()
Get the default text shader pipeline.
Definition Renderer2D.hpp:256
const Texture & getInvisibleTexture2DArray()
Get the default invisible 2D array texture.
Definition Renderer2D.hpp:131
const Texture & getDefaultDepthTexture2D()
Get the default 2D depth texture.
Definition Renderer2D.hpp:185
const Texture & getDefaultDepthTextureCube()
Get the default cube depth texture.
Definition Renderer2D.hpp:203
const Texture & getInvisibleTexture2D()
Get the default invisible 2D texture.
Definition Renderer2D.hpp:122
Renderer2D & operator=(const Renderer2D &)=delete
Copying a renderer is not allowed.
Model2D::FragmentShader TonemappingModel2DFragmentShader
Default fragment shader type for tonemapping.
Definition Renderer2D.hpp:58
Renderer2D(Renderer2D &&)=delete
Moving a renderer is not allowed.
const Model2D::ShaderPipeline & getPlainModel2DShaderPipeline()
Get the default plain 2D model shader pipeline.
Definition Renderer2D.hpp:235
const Model2D::VertexShader & getDefaultModel2DVertexShader()
Get the default 2D model vertex shader.
Renderer2D & operator=(Renderer2D &&)=delete
Moving a renderer is not allowed.
const Texture & getInvisibleTextureCubeArray()
Get the default invisible cube array texture.
Definition Renderer2D.hpp:149
const Texture & getFlatNormalTexture2D()
Get the default flat normal map 2D texture.
Definition Renderer2D.hpp:176
const Texture & getWhiteTextureCube()
Get the default white cubemap texture.
Definition Renderer2D.hpp:167
Renderer2D(const Renderer2D &)=delete
Copying a renderer is not allowed.
Model2D::FragmentShader PlainModel2DFragmentShader
Default fragment shader type for drawing plain 2D models.
Definition Renderer2D.hpp:52
const Model2D & getUnitSquareModel2D() const noexcept
Get the default 2D unit square model, which holds a single mesh that forms a square using a triangle ...
Definition Renderer2D.hpp:300
Model2D::VertexShader DefaultModel2DVertexShader
Default vertex shader type for drawing 2D models.
Definition Renderer2D.hpp:49
Renderer2D(Device &device, const Renderer2DOptions &options={})
Construct a 2D renderer.
const TonemappingModel2DFragmentShader & getTonemappingModel2DFragmentShader()
Get the default tonemapping fragment shader.
const TextModel2DFragmentShader & getTextModel2DFragmentShader()
Get the default text fragment shader.
const Texture & getInvisibleTextureCube()
Get the default invisible cube texture.
Definition Renderer2D.hpp:140
const Texture & getWhiteTexture2D()
Get the default white 2D texture.
Definition Renderer2D.hpp:158
Model2D::FragmentShader TextModel2DFragmentShader
Default fragment shader type for drawing text.
Definition Renderer2D.hpp:55
const Texture & getDefaultDepthTextureCubeArray()
Get the default cube array depth texture.
Definition Renderer2D.hpp:212
const Model2D & getUnitRightAngledTriangleModel2D() const noexcept
Get the default 2D unit right-angled triangle model, which holds a single mesh that forms a single tr...
Definition Renderer2D.hpp:314
const PlainModel2DFragmentShader & getPlainModel2DFragmentShader()
Get the default plain 2D model fragment shader.
const Texture & getDefaultDepthTexture2DArray()
Get the default 2D array depth texture.
Definition Renderer2D.hpp:194
void drawFrame(RenderPass &renderPass, StridedSpan< const Instances2DView > instanceBatches, const Camera2D &camera, const ExtraBuffers &... extraBuffers)
Push the draw commands of a frame of 2D instance batches to a render pass.
Definition Renderer2D.hpp:110
Storage for multidimensional data, such as 2D images, on the GPU, combined with a sampler configurati...
Definition Texture.hpp:700
static Texture create(Device &device, TextureType type, TextureFormat internalFormat, Extent3D size, uint32_t mipLevelCount, const void *pixels, Optional< TextureSamplerOptions > samplerOptions=TextureSamplerOptions{})
Create a new texture object and allocate GPU memory for storing image data.
Definition buffer_layouts.hpp:20
@ LESS_OR_EQUAL
The depth test passes if and only if the new depth value is less than or equal to the reference depth...
Definition Texture.hpp:515
@ CLAMP_TO_EDGE
Clamp to the edge of the texture when sampling outside the 0-1 range.
Definition Texture.hpp:492
@ D16_UNORM
Raw bitmap where each texel comprises 1 normalized 16-bit unsigned integer component: depth.
Definition Texture.hpp:282
@ R8G8B8A8_UNORM
Raw bitmap where each texel comprises 4 normalized 8-bit unsigned integer components: red,...
Definition Texture.hpp:276
@ NONE
Always use the most detailed mip level (level 0).
Definition Texture.hpp:482
@ NEAREST
Use the nearest neighbor to the sampled texel, which results in a blocky appearance.
Definition Texture.hpp:474
@ TEXTURE_2D_ARRAY
Texture set up to store an array of 2D images.
Definition Texture.hpp:40
@ TEXTURE_CUBE
Texture set up to store a cube of 6 sides of square 2D images.
Definition Texture.hpp:41
@ TEXTURE_CUBE_ARRAY
Texture set up to store an array of cubes of 6 sides of square 2D images.
Definition Texture.hpp:42
@ TEXTURE_2D
Texture set up to store a single 2D image.
Definition Texture.hpp:39
StridedSpan(R &&range) -> StridedSpan< std::remove_reference_t< decltype(*std::data(range))>, DYNAMIC_EXTENT, sizeof(std::remove_reference_t< decltype(*std::data(range))>)>
constexpr void sortByAscending(random_access_range auto &&range)
Definition algorithms.hpp:514
Extent< 2 > Extent2D
Size of a 2D data region.
Definition extents.hpp:31
Span(R &&range) -> Span< std::remove_reference_t< decltype(*std::data(range))> >
Pair(T1, T2) -> Pair< T1, T2 >
Extent< 3 > Extent3D
Size of a 3D data region.
Definition extents.hpp:32
Definition Array.hpp:20
Definition Pair.hpp:53
Configuration options for a Renderer2D.
Definition Renderer2D.hpp:30
constexpr bool operator==(const Renderer2DOptions &other) const noexcept=default
Compare these options to another set of options for equality.
Configuration of a graphics pipeline.
Definition shaders.hpp:428
Optional< BlendState > blendState
How to blend the pixel color with the output while rendering.
Definition shaders.hpp:652
Configuration options for the sampler of a Texture.
Definition Texture.hpp:617
static const TextureSamplerOptions UNFILTERED
A texture sampler configuration that ensures that the texture is sampled as directly as possible,...
Definition Texture.hpp:622