libdonut  2.3.2
Application framework for cross-platform game development in C++20
RenderPass.hpp
Go to the documentation of this file.
1 #ifndef DONUT_GRAPHICS_RENDER_PASS_HPP
2 #define DONUT_GRAPHICS_RENDER_PASS_HPP
3 
4 #include <donut/Color.hpp>
6 #include <donut/LinearBuffer.hpp>
11 #include <donut/graphics/Text.hpp>
14 #include <donut/math.hpp>
15 
16 #include <cstddef> // std::byte
17 #include <span> // std::span
18 #include <string_view> // std::string_view, std::u8string_view
19 #include <vector> // std::vector
20 
21 namespace donut::graphics {
22 
32 struct ModelInstance {
40 
47  const Model* model;
48 
56  const Texture* diffuseMapOverride = nullptr;
57 
65  const Texture* specularMapOverride = nullptr;
66 
74  const Texture* normalMapOverride = nullptr;
75 
83  const Texture* emissiveMapOverride = nullptr;
84 
89  mat4 transformation = identity<mat4>();
90 
98  vec2 textureOffset{0.0f, 0.0f};
99 
107  vec2 textureScale{1.0f, 1.0f};
108 
118 
126  vec3 specularFactor{1.0f, 1.0f, 1.0f};
127 
135  vec3 emissiveFactor{1.0f, 1.0f, 1.0f};
136 };
137 
149 struct QuadInstance {
157 
165 
169  mat3 transformation = identity<mat3>();
170 
178  vec2 textureOffset{0.0f, 0.0f};
179 
187  vec2 textureScale{1.0f, 1.0f};
188 
200 };
201 
224 
231  const Texture* texture;
232 
237  vec2 position{0.0f, 0.0f};
238 
245  vec2 scale{1.0f, 1.0f};
246 
251  float angle = 0.0f;
252 
258  vec2 origin{0.0f, 0.0f};
259 
267  vec2 textureOffset{0.0f, 0.0f};
268 
276  vec2 textureScale{1.0f, 1.0f};
277 
287 };
288 
308 
316 
321  vec2 position{0.0f, 0.0f};
322 
326  vec2 size{1.0f, 1.0f};
327 
332  float angle = 0.0f;
333 
339  vec2 origin{0.0f, 0.0f};
340 
348  vec2 textureOffset{0.0f, 0.0f};
349 
357  vec2 textureScale{1.0f, 1.0f};
358 
371 };
372 
396 
404 
413 
418  vec2 position{0.0f, 0.0f};
419 
426  vec2 scale{1.0f, 1.0f};
427 
432  float angle = 0.0f;
433 
439  vec2 origin{0.0f, 0.0f};
440 
450 };
451 
466 struct TextInstance {
475 
483  const Text* text;
484 
489  vec2 position{0.0f, 0.0f};
490 
495 };
496 
523 
531  const Text* text;
532 
537  vec2 position{0.0f, 0.0f};
538 
543 };
544 
569 
577 
582 
587  vec2 position{0.0f, 0.0f};
588 
600  vec2 scale{1.0f, 1.0f};
601 
609  vec2 origin{0.0f, 0.0f};
610 
615 
626  std::u8string_view string;
627 };
628 
653 
661 
666 
671  vec2 position{0.0f, 0.0f};
672 
684  vec2 scale{1.0f, 1.0f};
685 
693  vec2 origin{0.0f, 0.0f};
694 
699 
712  std::string_view string;
713 };
714 
718 class RenderPass {
719 public:
723  RenderPass() noexcept = default;
724 
734  RenderPass(std::span<std::byte> initialMemory) noexcept
735  : memoryResource(initialMemory) {}
736 
746  RenderPass& draw(const ModelInstance& model);
747 
757  RenderPass& draw(const QuadInstance& quad);
758 
768  RenderPass& draw(const TextureInstance& texture);
769 
779  RenderPass& draw(const RectangleInstance& rectangle);
780 
790  RenderPass& draw(const SpriteInstance& sprite);
791 
801  RenderPass& draw(const TextInstance& text);
802 
813 
825 
837 
838 private:
839  friend Renderer;
840 
841  struct CommandUseShader3D {
842  Shader3D* shader;
843  };
844 
845  struct CommandUseShader2D {
846  Shader2D* shader;
847  };
848 
849  struct CommandUseModel {
850  const Model* model;
851  const Texture* diffuseMapOverride;
852  const Texture* specularMapOverride;
853  const Texture* normalMapOverride;
854  const Texture* emissiveMapOverride;
855  };
856 
857  struct CommandUseTexture {
858  const Texture* texture;
859  };
860 
861  struct CommandUseSpriteAtlas {
862  const SpriteAtlas* atlas;
863  };
864 
865  struct CommandUseFont {
866  Font* font;
867  };
868 
869  struct CommandDrawModelInstance {
870  mat4 transformation;
871  Color tintColor;
872  vec2 textureOffset;
873  vec2 textureScale;
874  vec3 specularFactor;
875  vec3 emissiveFactor;
876  };
877 
878  struct CommandDrawQuadInstance {
879  mat3 transformation;
880  Color tintColor;
881  vec2 textureOffset;
882  vec2 textureScale;
883  };
884 
885  struct CommandDrawTextureInstance {
886  Color tintColor;
887  vec2 position;
888  vec2 scale;
889  vec2 origin;
890  vec2 textureOffset;
891  vec2 textureScale;
892  float angle;
893  };
894 
895  struct CommandDrawRectangleInstance {
896  Color tintColor;
897  vec2 position;
898  vec2 size;
899  vec2 origin;
900  vec2 textureOffset;
901  vec2 textureScale;
902  float angle;
903  };
904 
905  struct CommandDrawSpriteInstance {
906  Color tintColor;
907  vec2 position;
908  vec2 scale;
909  vec2 origin;
910  float angle;
911  SpriteAtlas::SpriteId id;
912  };
913 
914  struct CommandDrawTextInstance {
915  Color color;
916  const Text* text;
917  vec2 position;
918  };
919 
920  struct CommandDrawTextCopyInstance {
921  Color color;
922  std::span<const Text::ShapedGlyph> shapedGlyphs;
923  vec2 position;
924  };
925 
926  struct CommandDrawTextStringInstance {
927  Color color;
928  std::string_view string;
929  vec2 position;
930  vec2 scale;
931  vec2 origin;
932  u32 characterSize;
933  };
934 
935  LinearMemoryResource memoryResource{};
936  LinearBuffer< //
937  CommandUseShader3D, //
938  CommandUseShader2D, //
939  CommandUseModel, //
940  CommandUseTexture, //
941  CommandUseSpriteAtlas, //
942  CommandUseFont, //
943  CommandDrawModelInstance, //
944  CommandDrawQuadInstance, //
945  CommandDrawTextureInstance, //
946  CommandDrawRectangleInstance, //
947  CommandDrawSpriteInstance, //
948  CommandDrawTextInstance, //
949  CommandDrawTextCopyInstance, //
950  CommandDrawTextStringInstance, //
951  Text::ShapedGlyph[], //
952  char[]>
953  commandBuffer2D{&memoryResource, memoryResource.getRemainingCapacity()};
954  std::vector<Font*, LinearAllocator<Font*>> fonts{&memoryResource};
955  Shader3D* previousShader3D = nullptr;
956  Shader2D* previousShader2D = nullptr;
957  const Model* previousModel = nullptr;
958  const Texture* previousDiffuseMapOverride = nullptr;
959  const Texture* previousSpecularMapOverride = nullptr;
960  const Texture* previousNormalMapOverride = nullptr;
961  const Texture* previousEmissiveMapOverride = nullptr;
962  const Texture* previousTexture = nullptr;
963  const SpriteAtlas* previousSpriteAtlas = nullptr;
964  Font* previousFont = nullptr;
965 };
966 
967 } // namespace donut::graphics
968 
969 #endif
Normalized floating-point RGBA color type with 32 bits per component.
Definition: Color.hpp:11
static const Color WHITE
Definition: Color.hpp:158
std::size_t getRemainingCapacity() const noexcept
Definition: LinearAllocator.hpp:49
Typeface describing an assortment of character glyphs that may be rendered on-demand into an expandin...
Definition: Font.hpp:42
Graphics drawing queue for batch rendering using a Renderer.
Definition: RenderPass.hpp:718
RenderPass & draw(const RectangleInstance &rectangle)
Enqueue a RectangleInstance to be drawn when the render pass is rendered.
RenderPass & draw(const TextCopyInstance &text)
Enqueue a TextCopyInstance to be drawn when the render pass is rendered.
RenderPass & draw(const TextureInstance &texture)
Enqueue a TextureInstance to be drawn when the render pass is rendered.
RenderPass() noexcept=default
Construct an empty RenderPass.
RenderPass & draw(const SpriteInstance &sprite)
Enqueue a SpriteInstance to be drawn when the render pass is rendered.
RenderPass & draw(const TextInstance &text)
Enqueue a TextInstance to be drawn when the render pass is rendered.
RenderPass & draw(const ModelInstance &model)
Enqueue a ModelInstance to be drawn when the render pass is rendered.
RenderPass & draw(const QuadInstance &quad)
Enqueue a QuadInstance to be drawn when the render pass is rendered.
RenderPass & draw(const TextStringInstance &text)
Enqueue a TextStringInstance to be drawn when the render pass is rendered.
RenderPass & draw(const TextUTF8StringInstance &text)
Enqueue a TextUTF8StringInstance to be drawn when the render pass is rendered.
Persistent system for rendering the batched draw commands of a RenderPass onto a Framebuffer,...
Definition: Renderer.hpp:38
ShaderProgram specialized for rendering TexturedQuad instances in 2D.
Definition: Shader2D.hpp:29
static Shader2D *const PLAIN
Pointer to the statically allocated storage for the built-in plain shader.
Definition: Shader2D.hpp:58
static Shader2D *const ALPHA
Pointer to the statically allocated storage for the built-in alpha shader.
Definition: Shader2D.hpp:68
ShaderProgram specialized for rendering Model instances in 3D.
Definition: Shader3D.hpp:25
static Shader3D *const BLINN_PHONG
Pointer to the statically allocated storage for the built-in blinn-phong shader.
Definition: Shader3D.hpp:74
Expandable texture atlas for packing 2D images into a spritesheet to enable batch rendering.
Definition: SpriteAtlas.hpp:34
Facility for shaping text, according to a Font, into renderable glyphs.
Definition: Text.hpp:19
Storage for multidimensional data, such as 2D images, on the GPU, combined with a sampler configurati...
Definition: Texture.hpp:75
static const Texture *const WHITE
Pointer to the statically allocated storage for the built-in white 2D texture.
Definition: Texture.hpp:105
Definition: Buffer.hpp:7
Configuration of a 3D Model instance, for drawing as part of a RenderPass.
Definition: RenderPass.hpp:32
const Model * model
Non-owning pointer to the model to be drawn.
Definition: RenderPass.hpp:47
const Texture * diffuseMapOverride
Non-owning pointer to the texture to use for the base color, or nullptr to use the original textures ...
Definition: RenderPass.hpp:56
vec3 specularFactor
Specular factor to use in the shader.
Definition: RenderPass.hpp:126
const Texture * normalMapOverride
Non-owning pointer to the texture to use for normal mapping, or nullptr to use the original textures ...
Definition: RenderPass.hpp:74
vec2 textureScale
Coefficients to scale the texture coordinates by before sampling textures.
Definition: RenderPass.hpp:107
Color tintColor
Tint color to use in the shader.
Definition: RenderPass.hpp:117
const Texture * specularMapOverride
Non-owning pointer to the texture to use for specular highlights, or nullptr to use the original text...
Definition: RenderPass.hpp:65
vec3 emissiveFactor
Emissive factor to use in the shader.
Definition: RenderPass.hpp:135
Shader3D * shader
Non-owning pointer the shader to use when rendering this model.
Definition: RenderPass.hpp:39
mat4 transformation
Transformation matrix to apply to every vertex position of the model, in world space.
Definition: RenderPass.hpp:89
const Texture * emissiveMapOverride
Non-owning pointer to the texture to use for emissive mapping, or nullptr to use the original texture...
Definition: RenderPass.hpp:83
vec2 textureOffset
Offset, in texture coordinates, to apply to the texture coordinates before sampling textures.
Definition: RenderPass.hpp:98
Container for a set of 3D triangle meshes stored on the GPU, combined with associated materials.
Definition: Model.hpp:20
Configuration of an arbitrarily shaded/transformed 2D quad instance, optionally textured,...
Definition: RenderPass.hpp:149
Color tintColor
Tint color to use in the shader.
Definition: RenderPass.hpp:199
mat3 transformation
Transformation matrix to apply to every corner of the quad.
Definition: RenderPass.hpp:169
Shader2D * shader
Non-owning pointer to the shader to use when rendering this quad.
Definition: RenderPass.hpp:156
vec2 textureScale
Coefficients to scale the texture coordinates by before sampling the texture.
Definition: RenderPass.hpp:187
vec2 textureOffset
Offset, in texture coordinates, to apply to the texture coordinates before sampling the texture.
Definition: RenderPass.hpp:178
const Texture * texture
Non-owning pointer to a texture to apply to the quad.
Definition: RenderPass.hpp:164
Configuration of a 2D rectangle instance, optionally textured, for drawing as part of a RenderPass.
Definition: RenderPass.hpp:300
const Texture * texture
Non-owning pointer to a texture to apply to the rectangle.
Definition: RenderPass.hpp:315
Color tintColor
Tint color to use in the shader.
Definition: RenderPass.hpp:370
vec2 position
Position, in world coordinates, to render the rectangle at, with respect to its RectangleInstance::or...
Definition: RenderPass.hpp:321
vec2 origin
Offset, in texture coordinates, specifying the origin relative to the bottom left of the rectangle.
Definition: RenderPass.hpp:339
vec2 textureScale
Coefficients to scale the texture coordinates by before sampling the texture.
Definition: RenderPass.hpp:357
Shader2D * shader
Non-owning pointer to the shader to use when rendering this rectangle.
Definition: RenderPass.hpp:307
vec2 textureOffset
Offset, in texture coordinates, to apply to the texture coordinates before sampling the texture.
Definition: RenderPass.hpp:348
vec2 size
Size of the rectangle, in world coordinates.
Definition: RenderPass.hpp:326
float angle
Angle, in radians, to rotate the rectangle by, around its RectangleInstance::origin.
Definition: RenderPass.hpp:332
Identifier for a specific image in the spritesheet.
Definition: SpriteAtlas.hpp:53
Configuration of a 2D sprite instance from a SpriteAtlas, for drawing as part of a RenderPass.
Definition: RenderPass.hpp:388
float angle
Angle, in radians, to rotate the sprite by, around its SpriteInstance::origin.
Definition: RenderPass.hpp:432
const SpriteAtlas * atlas
Non-owning pointer to the texture atlas in which the sprite resides.
Definition: RenderPass.hpp:403
vec2 position
Position, in world coordinates, to render the sprite at, with respect to its SpriteInstance::origin.
Definition: RenderPass.hpp:418
vec2 scale
Coefficients to scale the size of the sprite by.
Definition: RenderPass.hpp:426
vec2 origin
Offset, in texture coordinates, specifying the origin relative to the bottom left of the sprite.
Definition: RenderPass.hpp:439
Color tintColor
Tint color to use in the shader.
Definition: RenderPass.hpp:449
SpriteAtlas::SpriteId id
Identifier of the specific sprite in the SpriteInstance::atlas that is to be drawn.
Definition: RenderPass.hpp:412
Shader2D * shader
Non-owning pointer to the shader to use when rendering this sprite.
Definition: RenderPass.hpp:395
Configuration of a copied 2D instance of Text shaped from a Font, for drawing as part of a RenderPass...
Definition: RenderPass.hpp:514
Shader2D * shader
Non-owning pointer to the shader to use when rendering the glyphs of this text.
Definition: RenderPass.hpp:522
Color color
Base text color.
Definition: RenderPass.hpp:542
vec2 position
Starting position, in world coordinates, to render the text at.
Definition: RenderPass.hpp:537
const Text * text
Non-owning read-only pointer to the shaped text to copy, and later draw.
Definition: RenderPass.hpp:531
Configuration of a 2D instance of Text shaped from a Font, for drawing as part of a RenderPass.
Definition: RenderPass.hpp:466
Color color
Base text color.
Definition: RenderPass.hpp:494
const Text * text
Non-owning read-only pointer to the shaped text to draw.
Definition: RenderPass.hpp:483
vec2 position
Starting position, in world coordinates, to render the text at.
Definition: RenderPass.hpp:489
Shader2D * shader
Non-owning pointer to the shader to use when rendering the glyphs of this text.
Definition: RenderPass.hpp:474
Configuration of a 2D instance of a string of text with a Font, for drawing as part of a RenderPass.
Definition: RenderPass.hpp:644
vec2 scale
Scaling to apply to the size of the shaped glyphs.
Definition: RenderPass.hpp:684
Shader2D * shader
Non-owning pointer to the shader to use when rendering the glyphs of this text.
Definition: RenderPass.hpp:652
Font * font
Non-owning pointer to the font from which to shape the text.
Definition: RenderPass.hpp:660
vec2 origin
Offset, in normalized coordinates, specifying the origin relative to the baseline of the first line o...
Definition: RenderPass.hpp:693
vec2 position
Starting position, in world coordinates, to render the text at.
Definition: RenderPass.hpp:671
u32 characterSize
Character size to shape the glyphs at.
Definition: RenderPass.hpp:665
std::string_view string
String to shape the text from.
Definition: RenderPass.hpp:712
Color color
Base text color.
Definition: RenderPass.hpp:698
Configuration of a 2D instance of a UTF-8 string of text with a Font, for drawing as part of a Render...
Definition: RenderPass.hpp:560
Color color
Base text color.
Definition: RenderPass.hpp:614
vec2 origin
Offset, in normalized coordinates, specifying the origin relative to the baseline of the first line o...
Definition: RenderPass.hpp:609
std::u8string_view string
UTF8-encoded string to shape the text from.
Definition: RenderPass.hpp:626
vec2 position
Starting position, in world coordinates, to render the text at.
Definition: RenderPass.hpp:587
vec2 scale
Scaling to apply to the size of the shaped glyphs.
Definition: RenderPass.hpp:600
u32 characterSize
Character size to shape the glyphs at.
Definition: RenderPass.hpp:581
Font * font
Non-owning pointer to the font from which to shape the text.
Definition: RenderPass.hpp:576
Shader2D * shader
Non-owning pointer to the shader to use when rendering the glyphs of this text.
Definition: RenderPass.hpp:568
Configuration of a 2D textured quad instance, for drawing as part of a RenderPass.
Definition: RenderPass.hpp:216
vec2 textureOffset
Offset, in texture coordinates, to apply to the texture coordinates before sampling the texture.
Definition: RenderPass.hpp:267
vec2 scale
Coefficients to scale the size of the texture by.
Definition: RenderPass.hpp:245
Color tintColor
Tint color to use in the shader.
Definition: RenderPass.hpp:286
vec2 textureScale
Coefficients to scale the texture coordinates by before sampling the texture.
Definition: RenderPass.hpp:276
float angle
Angle, in radians, to rotate the texture by, around its TextureInstance::origin.
Definition: RenderPass.hpp:251
Shader2D * shader
Non-owning pointer to the shader to use when rendering this texture.
Definition: RenderPass.hpp:223
vec2 position
Position, in world coordinates, to render the texture at, with respect to its TextureInstance::origin...
Definition: RenderPass.hpp:237
const Texture * texture
Non-owning pointer to the texture to be drawn.
Definition: RenderPass.hpp:231
vec2 origin
Offset, in texture coordinates, specifying the origin relative to the bottom left of the texture.
Definition: RenderPass.hpp:258