GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
RenderPass.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_RENDER_PASS_HPP
5#define GREM_GRAPHICS_RENDER_PASS_HPP
6
8
18#include <GREM/core/extents.hpp>
25
26#include <utility> // std::move
27
28namespace grem::graphics {
29
30class Device; // Forward declaration, to avoid a circular include of Device.hpp.
31
32struct RenderPassImplementation;
33
39public:
43 struct Statistics {
49
58
63
69
78 [[nodiscard]] constexpr bool operator==(const Statistics& other) const noexcept = default;
79
87 constexpr Statistics& operator+=(const Statistics& other) noexcept {
88 totalDrawnVertexCount += other.totalDrawnVertexCount;
89 totalDrawnIndexCount += other.totalDrawnIndexCount;
90 totalDrawnInstanceCount += other.totalDrawnInstanceCount;
91 totalDrawCallCount += other.totalDrawCallCount;
92 return *this;
93 }
94
103 constexpr Statistics& operator-=(const Statistics& other) noexcept {
104 totalDrawnVertexCount -= other.totalDrawnVertexCount;
105 totalDrawnIndexCount -= other.totalDrawnIndexCount;
106 totalDrawnInstanceCount -= other.totalDrawnInstanceCount;
107 totalDrawCallCount -= other.totalDrawCallCount;
108 return *this;
109 }
110
119 [[nodiscard]] friend constexpr Statistics operator+(const Statistics& a, const Statistics& b) noexcept {
120 Statistics result = a;
121 result += b;
122 return result;
123 }
124
134 [[nodiscard]] friend constexpr Statistics operator-(const Statistics& a, const Statistics& b) noexcept {
135 Statistics result = a;
136 result -= b;
137 return result;
138 }
139 };
140
174 GREM_API(graphics) RenderPass(Device& device, Span<const TextureSubresourceReference> renderTargets, const ClearMode& clearMode, Optional<Viewport> viewport = {});
175
205 GREM_ALWAYS_INLINE RenderPass(Device& device, TextureSubresourceReference renderTarget, const ClearMode& clearMode, Optional<Viewport> viewport = {})
206 : RenderPass(device, Span{&renderTarget, 1}, clearMode, viewport) {}
207
244 GREM_API(graphics)
245 RenderPass(Device& device, TextureSubresourceReference resolveTarget, const ResolveMode& resolveMode, Span<const TextureSubresourceReference> renderTargets,
246 const ClearMode& clearMode, Optional<Viewport> viewport = {});
247
282 const ClearMode& clearMode, Optional<Viewport> viewport = {})
283 : RenderPass(device, resolveTarget, resolveMode, Span{&renderTarget, 1}, clearMode, viewport) {}
284
287
289 RenderPass(const RenderPass&) = delete;
290
293
295 RenderPass& operator=(const RenderPass&) = delete;
296
299
313 GREM_API(graphics) RenderPass& setViewport(const Viewport& viewport);
314
334
343 [[nodiscard]] GREM_API(graphics) const Viewport& getViewport();
344
351 [[nodiscard]] GREM_API(graphics) Extent2D getFramebufferSize();
352
368 GREM_API(graphics) RenderPass& fill(const Region2D& targetRegion, const ClearValues& values);
369
382 GREM_API(graphics) RenderPass& fill(const ClearValues& values) {
383 return fill(Region2D{.size = getFramebufferSize()}, values);
384 }
385
409 template <typename Vertex, typename Index, typename Parameters, typename Instance, typename... Buffers>
411 const Buffers&... buffers) {
412 Array<Pair<BufferLayoutReference, SharedPointer<void>>, sizeof...(Buffers)> bufferHandles{
413 Pair<BufferLayoutReference, SharedPointer<void>>{Buffers::LAYOUT_REFERENCE, buffers.lock()}...,
414 };
415 sortBufferHandles(bufferHandles);
416 return draw(drawCommandBuffer.lock(), instanceBuffer.lock(), bufferHandles);
417 }
418
438 template <typename Vertex, typename Index, typename Parameters, typename... Buffers>
439 RenderPass& draw(const DrawCommandBuffer<Mesh<Vertex, Index, Parameters, NoInstance>>& drawCommandBuffer, const Buffers&... buffers) {
440 Array<Pair<BufferLayoutReference, SharedPointer<void>>, sizeof...(Buffers)> bufferHandles{
441 Pair<BufferLayoutReference, SharedPointer<void>>{Buffers::LAYOUT_REFERENCE, buffers.lock()}...,
442 };
443 sortBufferHandles(bufferHandles);
444 return draw(drawCommandBuffer.lock(), SharedPointer<InstanceBufferImplementation>{}, bufferHandles);
445 }
446
476 Span<const Pair<BufferLayoutReference, SharedPointer<void>>> bufferHandles) {
477 return drawShaded({}, std::move(drawCommandBufferHandle), std::move(instanceBufferHandle), bufferHandles);
478 }
479
504 template <typename Vertex, typename Index, typename Parameters, typename Instance, typename... Buffers>
506 const DrawCommandBuffer<Mesh<Vertex, Index, Parameters, Instance>>& drawCommandBuffer, const InstanceBuffer<Instance>& instanceBuffer, const Buffers&... buffers) {
507 Array<Pair<BufferLayoutReference, SharedPointer<void>>, sizeof...(Buffers)> bufferHandles{
508 Pair<BufferLayoutReference, SharedPointer<void>>{Buffers::LAYOUT_REFERENCE, buffers.lock()}...,
509 };
510 sortBufferHandles(bufferHandles);
511 return drawShaded(shaderPipelineOverride.lock(), drawCommandBuffer.lock(), instanceBuffer.lock(), bufferHandles);
512 }
513
534 template <typename Vertex, typename Index, typename Parameters, typename... Buffers>
536 const DrawCommandBuffer<Mesh<Vertex, Index, Parameters, NoInstance>>& drawCommandBuffer, const Buffers&... buffers) {
537 Array<Pair<BufferLayoutReference, SharedPointer<void>>, sizeof...(Buffers)> bufferHandles{
538 Pair<BufferLayoutReference, SharedPointer<void>>{Buffers::LAYOUT_REFERENCE, buffers.lock()}...,
539 };
540 sortBufferHandles(bufferHandles);
541 return drawShaded(shaderPipelineOverride.lock(), drawCommandBuffer.lock(), SharedPointer<InstanceBufferImplementation>{}, bufferHandles);
542 }
543
575 GREM_API(graphics)
576 RenderPass& drawShaded(SharedPointer<ShaderPipelineImplementation> shaderPipelineOverrideHandle, SharedPointer<DrawCommandBufferImplementation> drawCommandBufferHandle,
577 SharedPointer<InstanceBufferImplementation> instanceBufferHandle, Span<const Pair<BufferLayoutReference, SharedPointer<void>>> bufferHandles);
578
602 template <typename Vertex, typename Index, typename Parameters, typename Instance, typename... Buffers>
603 RenderPass& drawUnordered(const UnorderedDrawCommandBuffer<Mesh<Vertex, Index, Parameters, Instance>>& drawCommandBuffer, const InstanceBuffer<Instance>& instanceBuffer,
604 const Buffers&... buffers) {
605 Array<Pair<BufferLayoutReference, SharedPointer<void>>, sizeof...(Buffers)> bufferHandles{
606 Pair<BufferLayoutReference, SharedPointer<void>>{&Buffers::LAYOUT_REFERENCE, buffers.lock()}...,
607 };
608 sortBufferHandles(bufferHandles);
609 return drawUnordered(drawCommandBuffer.lock(), instanceBuffer.lock(), bufferHandles);
610 }
611
631 template <typename Vertex, typename Index, typename Parameters, typename... Buffers>
633 Array<Pair<BufferLayoutReference, SharedPointer<void>>, sizeof...(Buffers)> bufferHandles{
634 Pair<BufferLayoutReference, SharedPointer<void>>{&Buffers::LAYOUT_REFERENCE, buffers.lock()}...,
635 };
636 sortBufferHandles(bufferHandles);
637 return drawUnordered(drawCommandBuffer.lock(), SharedPointer<InstanceBufferImplementation>{}, bufferHandles);
638 }
639
669 Span<const Pair<BufferLayoutReference, SharedPointer<void>>> bufferHandles) {
670 return drawShadedUnordered({}, std::move(drawCommandBufferHandle), std::move(instanceBufferHandle), bufferHandles);
671 }
672
697 template <typename Vertex, typename Index, typename Parameters, typename Instance, typename... Buffers>
699 const UnorderedDrawCommandBuffer<Mesh<Vertex, Index, Parameters, Instance>>& drawCommandBuffer, const InstanceBuffer<Instance>& instanceBuffer, const Buffers&... buffers) {
700 Array<Pair<BufferLayoutReference, SharedPointer<void>>, sizeof...(Buffers)> bufferHandles{
701 Pair<BufferLayoutReference, SharedPointer<void>>{Buffers::LAYOUT_REFERENCE, buffers.lock()}...,
702 };
703 sortBufferHandles(bufferHandles);
704 return drawShadedUnordered(shaderPipelineOverride.lock(), drawCommandBuffer.lock(), instanceBuffer.lock(), bufferHandles);
705 }
706
727 template <typename Vertex, typename Index, typename Parameters, typename... Buffers>
729 const UnorderedDrawCommandBuffer<Mesh<Vertex, Index, Parameters, NoInstance>>& drawCommandBuffer, const Buffers&... buffers) {
730 Array<Pair<BufferLayoutReference, SharedPointer<void>>, sizeof...(Buffers)> bufferHandles{
731 Pair<BufferLayoutReference, SharedPointer<void>>{Buffers::LAYOUT_REFERENCE, buffers.lock()}...,
732 };
733 sortBufferHandles(bufferHandles);
734 return drawShadedUnordered(shaderPipelineOverride.lock(), drawCommandBuffer.lock(), SharedPointer<InstanceBufferImplementation>{}, bufferHandles);
735 }
736
768 GREM_API(graphics)
769 RenderPass& drawShadedUnordered(SharedPointer<ShaderPipelineImplementation> shaderPipelineOverrideHandle,
770 SharedPointer<UnorderedDrawCommandBufferImplementation> drawCommandBufferHandle, SharedPointer<InstanceBufferImplementation> instanceBufferHandle,
771 Span<const Pair<BufferLayoutReference, SharedPointer<void>>> bufferHandles);
772
776 [[nodiscard]] GREM_API(graphics) Statistics getStatistics() const;
777
786 [[nodiscard]] SharedPointer<RenderPassImplementation> lock() const {
787 return implementation;
788 }
789
798 [[nodiscard]] RenderPassImplementation* get() noexcept { // NOLINT(readability-make-member-function-const)
799 return implementation.get();
800 }
801
810 [[nodiscard]] const RenderPassImplementation* get() const noexcept {
811 return implementation.get();
812 }
813
814private:
815 static void sortBufferHandles(Span<Pair<BufferLayoutReference, SharedPointer<void>>> bufferHandles) {
817 }
818
820};
821
822} // namespace grem::graphics
823
824#endif
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Definition SharedPointer.hpp:65
Definition Span.hpp:146
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
Resource handle with shared ownership of a GPU memory buffer with copy-on-write value semantics conta...
Definition buffers.hpp:883
Resource handle with shared ownership of a GPU memory buffer with copy-on-write value semantics conta...
Definition buffers.hpp:825
Generic abstraction of a GPU vertex array object and its associated buffers.
Definition Mesh.hpp:209
Backend-specific implementation of RenderPass.
Definition RenderPass.hpp:38
GREM_ALWAYS_INLINE RenderPass(Device &device, TextureSubresourceReference renderTarget, const ClearMode &clearMode, Optional< Viewport > viewport={})
Construct a render pass.
Definition RenderPass.hpp:205
RenderPass & fill(const ClearValues &values)
Record a command to fill the full contents of the render targets.
Definition RenderPass.hpp:382
RenderPass & drawShaded(const ShaderPipeline< Mesh< Vertex, Index, Parameters, Instance > > &shaderPipelineOverride, const DrawCommandBuffer< Mesh< Vertex, Index, Parameters, Instance > > &drawCommandBuffer, const InstanceBuffer< Instance > &instanceBuffer, const Buffers &... buffers)
Record the commands to draw the contents of a draw command buffer with specific shaders using a set o...
Definition RenderPass.hpp:505
RenderPass(RenderPass &&)=delete
Moving a render pass is not allowed.
RenderPass & draw(SharedPointer< DrawCommandBufferImplementation > drawCommandBufferHandle, SharedPointer< InstanceBufferImplementation > instanceBufferHandle, Span< const Pair< BufferLayoutReference, SharedPointer< void > > > bufferHandles)
Record the commands to draw the contents of a draw command buffer using a set of shader buffers to th...
Definition RenderPass.hpp:475
RenderPass & operator=(const RenderPass &)=delete
Copying a render pass is not allowed.
Statistics getStatistics() const
Get the current statistics of the render pass.
RenderPass & drawUnordered(const UnorderedDrawCommandBuffer< Mesh< Vertex, Index, Parameters, Instance > > &drawCommandBuffer, const InstanceBuffer< Instance > &instanceBuffer, const Buffers &... buffers)
Record the commands to draw the contents of an unordered draw command buffer using a set of shader bu...
Definition RenderPass.hpp:603
RenderPass & operator=(RenderPass &&)=delete
Moving a render pass is not allowed.
RenderPass & draw(const DrawCommandBuffer< Mesh< Vertex, Index, Parameters, NoInstance > > &drawCommandBuffer, const Buffers &... buffers)
Record the commands to draw the contents of a draw command buffer using a set of shader buffers to th...
Definition RenderPass.hpp:439
RenderPass & drawShadedUnordered(const ShaderPipeline< Mesh< Vertex, Index, Parameters, Instance > > &shaderPipelineOverride, const UnorderedDrawCommandBuffer< Mesh< Vertex, Index, Parameters, Instance > > &drawCommandBuffer, const InstanceBuffer< Instance > &instanceBuffer, const Buffers &... buffers)
Record the commands to draw the contents of an unordered draw command buffer with specific shaders us...
Definition RenderPass.hpp:698
RenderPass & setViewport(const Viewport &viewport)
Set the viewport and scissor area to use for subsequently recorded draw commands.
const RenderPassImplementation * get() const noexcept
Get a pointer to the underlying resource implementation.
Definition RenderPass.hpp:810
RenderPass & drawShaded(const ShaderPipeline< Mesh< Vertex, Index, Parameters, NoInstance > > &shaderPipelineOverride, const DrawCommandBuffer< Mesh< Vertex, Index, Parameters, NoInstance > > &drawCommandBuffer, const Buffers &... buffers)
Record the commands to draw the contents of a draw command buffer with specific shaders using a set o...
Definition RenderPass.hpp:535
Extent2D getFramebufferSize()
Get the size of the framebuffer that the render pass will be rendering through.
RenderPass & draw(const DrawCommandBuffer< Mesh< Vertex, Index, Parameters, Instance > > &drawCommandBuffer, const InstanceBuffer< Instance > &instanceBuffer, const Buffers &... buffers)
Record the commands to draw the contents of a draw command buffer using a set of shader buffers to th...
Definition RenderPass.hpp:410
RenderPass & fill(const Region2D &targetRegion, const ClearValues &values)
Record a command to fill the contents of a rectangular region of the render targets.
GREM_ALWAYS_INLINE RenderPass(Device &device, TextureSubresourceReference resolveTarget, const ResolveMode &resolveMode, TextureSubresourceReference renderTarget, const ClearMode &clearMode, Optional< Viewport > viewport={})
Construct a multisampled render pass that resolves to a resolve target at the end of the render.
Definition RenderPass.hpp:281
SharedPointer< RenderPassImplementation > lock() const
Get a lock for the underlying resource implementation.
Definition RenderPass.hpp:786
const Viewport & getViewport()
Get the viewport that will be used for subsequently recorded draw commands.
RenderPassImplementation * get() noexcept
Get a pointer to the underlying resource implementation.
Definition RenderPass.hpp:798
RenderPass(Device &device, Span< const TextureSubresourceReference > renderTargets, const ClearMode &clearMode, Optional< Viewport > viewport={})
Construct a render pass.
RenderPass & setViewportScissor(const Optional< Region2D > &scissor)
Set the scissor area to use for subsequently recoreded draw commands.
RenderPass(const RenderPass &)=delete
Copying a render pass is not allowed.
RenderPass & drawShadedUnordered(const ShaderPipeline< Mesh< Vertex, Index, Parameters, NoInstance > > &shaderPipelineOverride, const UnorderedDrawCommandBuffer< Mesh< Vertex, Index, Parameters, NoInstance > > &drawCommandBuffer, const Buffers &... buffers)
Record the commands to draw the contents of an unordered draw command buffer with specific shaders us...
Definition RenderPass.hpp:728
RenderPass & drawUnordered(const UnorderedDrawCommandBuffer< Mesh< Vertex, Index, Parameters, NoInstance > > &drawCommandBuffer, const Buffers &... buffers)
Record the commands to draw the contents of an unordered draw command buffer using a set of shader bu...
Definition RenderPass.hpp:632
RenderPass & drawUnordered(SharedPointer< UnorderedDrawCommandBufferImplementation > drawCommandBufferHandle, SharedPointer< InstanceBufferImplementation > instanceBufferHandle, Span< const Pair< BufferLayoutReference, SharedPointer< void > > > bufferHandles)
Record the commands to draw the contents of an unordered draw command buffer using a set of shader bu...
Definition RenderPass.hpp:668
Linked GPU shader pipeline.
Definition shaders.hpp:1508
Resource handle with shared ownership of a GPU memory buffer with copy-on-write value semantics conta...
Definition buffers.hpp:1176
Region of a render target.
Definition Viewport.hpp:18
Definition buffer_layouts.hpp:20
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
Region< 2 > Region2D
Rectangular region of 2D data.
Definition extents.hpp:819
Definition Array.hpp:20
Definition Pair.hpp:53
Generic type-erased read-only reference to a buffer or buffer set layout.
Definition buffer_layouts.hpp:43
Specification of how a texture should either be left untouched, be cleared to specific values or be c...
Definition Texture.hpp:444
Specification of values that a texture should be cleared to.
Definition Texture.hpp:426
Performance-related statistics of a render pass.
Definition RenderPass.hpp:43
size_t totalDrawCallCount
Total number of draw calls to the underlying graphics API required by the render pass for the current...
Definition RenderPass.hpp:68
friend constexpr Statistics operator+(const Statistics &a, const Statistics &b) noexcept
Add two sets of render pass statistics together.
Definition RenderPass.hpp:119
constexpr Statistics & operator+=(const Statistics &other) noexcept
Add another set of render pass statistics to this one.
Definition RenderPass.hpp:87
size_t totalDrawnIndexCount
Total number of vertex indices processed in the render pass, including the duplicated indices produce...
Definition RenderPass.hpp:57
size_t totalDrawnVertexCount
Total number of vertices processed in the render pass, including the duplicated vertices produced by ...
Definition RenderPass.hpp:48
size_t totalDrawnInstanceCount
Total number of mesh instances processed in the render pass.
Definition RenderPass.hpp:62
friend constexpr Statistics operator-(const Statistics &a, const Statistics &b) noexcept
Subtract two sets of render pass statistics to produce the difference between them,...
Definition RenderPass.hpp:134
constexpr Statistics & operator-=(const Statistics &other) noexcept
Subtract another set of render pass statistics from this one, using unsigned modular arithmetic.
Definition RenderPass.hpp:103
constexpr bool operator==(const Statistics &other) const noexcept=default
Compare this set of render pass statistics against another for equality.
Specification of how an intermediate render texture should either store or discard its values after b...
Definition Texture.hpp:466
Reference to a subresource of a texture.
Definition Texture.hpp:372