GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
ConstantDescription.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_CONSTANT_DESCRIPTION_HPP
5#define GREM_GRAPHICS_CONSTANT_DESCRIPTION_HPP
6
8
16#include <GREM/core/math.hpp>
18
19#include <type_traits> // std::remove_cvref_t, std::false_type, std::true_type, std::bool_constant
20#include <utility> // std::declval
21
22namespace grem::graphics {
23
29template <typename T>
35
39enum class ConstantType : uint8_t {
44};
45
46namespace detail {
47
48template <typename T>
49inline constexpr ConstantType CONSTANT_TYPE{};
50
51// clang-format off
52template <> inline constexpr ConstantType CONSTANT_TYPE<bool32_t> = ConstantType::BOOL;
53template <> inline constexpr ConstantType CONSTANT_TYPE<int32_t> = ConstantType::INT;
54template <> inline constexpr ConstantType CONSTANT_TYPE<uint32_t> = ConstantType::UINT;
55template <> inline constexpr ConstantType CONSTANT_TYPE<float> = ConstantType::FLOAT;
56// clang-format on
57
58template <typename T>
59struct is_shader_constant : std::false_type {};
60
61template <constant_value_type T>
62struct is_shader_constant<T> : std::true_type {};
63
64template <typename TupleType>
65inline constexpr bool is_shader_constant_v = is_shader_constant<TupleType>::value;
66
67template <typename TupleType>
68struct is_shader_constants : std::false_type {};
69
70template <typename... Ts>
71struct is_shader_constants<Tuple<Ts...>> : std::bool_constant<(is_shader_constant_v<std::remove_cvref_t<Ts>> && ...)> {};
72
73template <typename TupleType>
74inline constexpr bool is_shader_constants_v = is_shader_constants<TupleType>::value;
75
76} // namespace detail
77
83template <typename T>
85 aggregate<T> && //
88 detail::is_shader_constants_v<decltype(meta::getFields(std::declval<T>()))>;
89
97
98namespace detail {
99
100static_assert(sizeof(float) == sizeof(float32_t));
101static_assert(alignof(float) == alignof(float32_t));
102static_assert(sizeof(bool32_t) == sizeof(float));
103static_assert(sizeof(int32_t) == sizeof(float));
104static_assert(sizeof(uint32_t) == sizeof(float));
105static_assert(alignof(bool32_t) == alignof(float));
106static_assert(alignof(int32_t) == alignof(float));
107static_assert(alignof(uint32_t) == alignof(float));
108
109template <typename ConstantStruct>
110[[nodiscard]] constexpr auto getConstantDescriptions() {
112 meta::forEachIndexedFieldType<ConstantStruct>([&]<typename T>(auto index, meta::Type<T>) -> void {
113 result[index] = {
114 .type = detail::CONSTANT_TYPE<T>,
116 };
117 });
118 return result;
119}
120
121template <typename ConstantStruct>
122inline constexpr auto CONSTANT_DESCRIPTIONS = getConstantDescriptions<ConstantStruct>();
123
124} // namespace detail
125
126} // namespace grem::graphics
127
128#endif
Definition fundamentals.hpp:109
Definition concepts.hpp:100
Concept that checks if a type is a valid value type for a shader constant.
Definition ConstantDescription.hpp:30
Concept that checks if a type is a valid shader constant struct type.
Definition ConstantDescription.hpp:84
Definition concepts.hpp:35
Definition concepts.hpp:109
Definition concepts.hpp:187
@ T
't'
Definition keyboard.hpp:90
Definition buffer_layouts.hpp:20
ConstantType
Specification of the underlying value type of a shader constant.
Definition ConstantDescription.hpp:39
@ UINT
C++: uint32_t, GLSL: uint.
Definition ConstantDescription.hpp:42
@ INT
C++: int32_t, GLSL: int.
Definition ConstantDescription.hpp:41
@ BOOL
C++: bool32_t, GLSL: bool.
Definition ConstantDescription.hpp:40
@ FLOAT
C++: float, GLSL: float.
Definition ConstantDescription.hpp:43
constexpr auto aggregate_field_name_v
Name of the field at a given index of an aggregate type.
Definition metaprogramming.hpp:1568
constexpr void forEachIndexedFieldType(auto fn)
Execute a function for each field type of an aggregate, also including the field index.
Definition metaprogramming.hpp:1160
float float32_t
Definition fundamentals.hpp:155
Tuple(Ts...) -> Tuple< Ts... >
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Definition Array.hpp:20
Description of a constant in a shader constant struct.
Definition ConstantDescription.hpp:93
ConstantType type
Constant value type.
Definition ConstantDescription.hpp:94
CStringView name
Identifier that the constant is accessed by in the shader source code.
Definition ConstantDescription.hpp:95
Type identity wrapper.
Definition metaprogramming.hpp:329