GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
buffer_layouts.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_BUFFER_LAYOUTS_HPP
5#define GREM_GRAPHICS_BUFFER_LAYOUTS_HPP
6
8
17
18#include <compare> // std::strong_ordering
19
20namespace grem::graphics {
21
27
33
34struct BufferLayoutReference; // Forward declaration.
35
41
43struct BufferLayoutReference : Variant<UniformBufferLayoutReference, StorageBufferLayoutReference, BufferSetLayoutReference> {
45
51 constexpr BufferLayoutReference(UniformBufferLayoutReference uniformBufferLayout) noexcept
52 : Variant(uniformBufferLayout)
53 , nameCRC32(uniformBufferLayout.name) {}
54
60 constexpr BufferLayoutReference(StorageBufferLayoutReference storageBufferLayout) noexcept
61 : Variant(storageBufferLayout)
62 , nameCRC32(storageBufferLayout.name) {}
63
69 constexpr BufferLayoutReference(BufferSetLayoutReference bufferSetLayout) noexcept
70 : Variant(bufferSetLayout)
71 , nameCRC32(bufferSetLayout.name) {}
72
81 [[nodiscard]] constexpr bool operator==(const BufferLayoutReference& other) const noexcept {
82 return nameCRC32 == other.nameCRC32;
83 }
84
92 [[nodiscard]] constexpr std::strong_ordering operator<=>(const BufferLayoutReference& other) const noexcept {
93 return static_cast<uint32_t>(nameCRC32) <=> static_cast<uint32_t>(other.nameCRC32);
94 }
95};
96
105template <typename ParameterStruct, ConstantString Name>
112 constexpr operator UniformBufferLayoutReference() const noexcept {
113 return {.name = Name, .parameterDescriptions = detail::PARAMETER_DESCRIPTIONS<ParameterStruct>};
114 }
115};
116
124template <typename FieldStruct, ConstantString Name>
131 constexpr operator StorageBufferLayoutReference() const noexcept {
132 return {.name = Name, .fieldDescriptions = detail::FIELD_DESCRIPTIONS<FieldStruct>};
133 }
134};
135
136namespace detail {
137
138template <typename... Buffers>
139inline constexpr ConstantString BUFFER_SET_NAME = [] {
140 ConstantString<char, ((Buffers::NAME.size() + 2) + ...)> result{};
141 size_t i = 0;
142 result[i++] = '{';
143 for (const CStringView name : {Buffers::NAME...}) {
144 for (const char ch : name) {
145 result[i++] = ch;
146 }
147 if (i + 2 < result.size()) {
148 result[i++] = ',';
149 result[i++] = ' ';
150 }
151 }
152 result[i++] = '}';
153 GREM_ASSERT(i == result.size());
154 return result;
155}();
156
157template <typename... Buffers>
158inline constexpr Array<BufferLayoutReference, sizeof...(Buffers)> BUFFER_LAYOUT_REFERENCES{Buffers::LAYOUT_REFERENCE...};
159
160} // namespace detail
161
167template <typename... Buffers>
174 constexpr operator BufferSetLayoutReference() const noexcept {
175 return {
176 .name = detail::BUFFER_SET_NAME<Buffers...>,
177 .bufferLayouts = detail::BUFFER_LAYOUT_REFERENCES<Buffers...>,
178 };
179 }
180};
181
182} // namespace grem::graphics
183
184#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition CRC32.hpp:19
Definition ConstantString.hpp:40
constexpr size_type size() const noexcept
Definition ConstantString.hpp:127
Definition Span.hpp:146
Definition buffer_layouts.hpp:20
Array(T, Us...) -> Array< T, 1+sizeof...(Us)>
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Generic type-erased read-only reference to a buffer or buffer set layout.
Definition buffer_layouts.hpp:43
constexpr BufferLayoutReference(StorageBufferLayoutReference storageBufferLayout) noexcept
Construct a reference to a storage buffer layout.
Definition buffer_layouts.hpp:60
constexpr BufferLayoutReference(BufferSetLayoutReference bufferSetLayout) noexcept
Construct a reference to a buffer set layout.
Definition buffer_layouts.hpp:69
constexpr bool operator==(const BufferLayoutReference &other) const noexcept
Compare this buffer layout reference to another for equality.
Definition buffer_layouts.hpp:81
constexpr BufferLayoutReference(UniformBufferLayoutReference uniformBufferLayout) noexcept
Construct a reference to a uniform buffer layout.
Definition buffer_layouts.hpp:51
constexpr std::strong_ordering operator<=>(const BufferLayoutReference &other) const noexcept
Compare this buffer layout reference to another.
Definition buffer_layouts.hpp:92
CRC32 nameCRC32
CRC32 of the buffer name, used as a unique ID for the buffer type.
Definition buffer_layouts.hpp:44
Type-erased read-only reference to a BufferSetLayout.
Definition buffer_layouts.hpp:37
CStringView name
Unique name of the buffer set.
Definition buffer_layouts.hpp:38
Span< const BufferLayoutReference > bufferLayouts
Read-only view over the buffer layout references of the buffers in the set.
Definition buffer_layouts.hpp:39
Layout description of a BufferSet.
Definition buffer_layouts.hpp:168
Type-erased read-only reference to a StorageBufferLayout.
Definition buffer_layouts.hpp:29
CStringView name
Unique name of the storage buffer.
Definition buffer_layouts.hpp:30
Span< const FieldDescription > fieldDescriptions
Read-only view over the field descriptions of the field struct stored by the buffer.
Definition buffer_layouts.hpp:31
Layout description of a StorageBuffer.
Definition buffer_layouts.hpp:125
Type-erased read-only reference to a UniformBufferLayout.
Definition buffer_layouts.hpp:23
CStringView name
Unique name of the uniform buffer.
Definition buffer_layouts.hpp:24
Span< const ParameterDescription > parameterDescriptions
Read-only view over the parameter descriptions of the parameter struct stored by the buffer.
Definition buffer_layouts.hpp:25
Layout description of a UniformBuffer.
Definition buffer_layouts.hpp:106