GLSL Struct 1.4.0
glslstruct
Loading...
Searching...
No Matches
writer.hpp
1/*
2 * glslstruct - a C++ library designed to easily represent GLSL's Uniform Buffer Objects (UBOs) and Shader Storage Buffer Objects (SSBOs) in C++.
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/glslstruct/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 */
9
10#pragma once
11#ifndef _GLSL_STRUCT_GLSL_WRITER_HPP_
12 #define _GLSL_STRUCT_GLSL_WRITER_HPP_
13
14 #include <glslstruct/config.hpp>
15
17_GLSL_STRUCT_ERROR(
18 "This is only available for c++17 and greater and when types are not disabled with defined GLSL_STRUCT_DISABLE_TYPES!"
19);
20 #else
21
22 #include <glslstruct/type/containers/struct_type.hpp>
23 #include <glslstruct/utils/assert.hpp>
24
25namespace glslstruct::utils {
26 /**
27 * @brief type visitor which returns variable type
28 * @ingroup utils
29 */
31 private:
32 /// @brief result
33 std::string _result;
34 /// @brief defined structs names
36
37 public:
38 /// @brief constructor with defined structs names
39 explicit glsl_var_type_getter(const mstd::ordered_map<struct_type, std::string>& structsNames);
40
41 /// @brief type visitor
42 template<class Type>
43 void visit(Type&& varType) {
44 using T = std::decay_t<Type>;
47 else {
48 glsl_struct_assert(_structsNames.contains(varType), "Struct not defined!!");
50 }
51 }
52
53 /// @brief returns result
54 [[nodiscard]] const std::string& get_result() const noexcept;
55 };
56
57 /**
58 * @ingroup utils
59 * @brief returns variable type
60 * @param varType variable of which type is returned
61 * @param structsNames defined structs names
62 */
63 [[nodiscard]] std::string get_glsl_var_type_string(const base_type_handle& varType,
64 const mstd::ordered_map<struct_type, std::string>& structsNames);
65
66 /**
67 * @ingroup utils
68 * @brief type visitor which gets array and sub arrays counts in format [0][1]...
69 */
71 private:
72 /// @brief check for variable size arrays
74 /// @brief result
75 std::string _result;
76
77 public:
78 explicit glsl_array_count_getter(bool canBeVariableSize) noexcept;
79
80 /// @brief visitor function
81 template<class Type>
82 void visit(Type&& varType) {
83 using T = std::decay_t<Type>;
86 _result = fmt::format("{}[]", _result);
87 _canBeVariableSize = false;
88 }
89 else { _result = fmt::format("{}[{}]", _result, varType.get_count()); }
90 }
91 }
92
93 /// @brief returns result
94 [[nodiscard]] const std::string& get_result() const noexcept;
95 };
96
97 /**
98 * @ingroup utils
99 * @brief returns array counts string
100 * @param varType variable for which we get array counts
101 * @param canBeVariableSize check if array size can be variable size
102 */
103 [[nodiscard]] std::string get_glsl_array_count_string(const base_type_handle& varType, bool canBeVariableSize);
104
105 /**
106 * @ingroup utils
107 * @brief returns variable line `type name[0][1]...`
108 * @param name name of variable
109 * @param varType type of variable
110 * @param canBeVariableSize check if array size can be variable size
111 * @param structsNames defined structs names
112 */
113 [[nodiscard]] std::string get_glsl_variable_string(std::string_view name, const base_type_handle& varType,
114 bool canBeVariableSize, const mstd::ordered_map<struct_type, std::string>& structsNames);
115
116 /**
117 * @ingroup utils
118 * @brief general glsl writer
119 */
121 private:
122 /// @brief result
123 std::string _result;
124
125 /// @brief struct name generator idx
126 size_t _structIdx = 0;
127 /// @brief struct name generator idx
129
130 /// @brief unique names of all strucs, UBOs and SSBOs
132
133 /// @brief returns layout string
134 [[nodiscard]] static std::string _get_layout(std::string_view layoutData);
135
136 /// @brief appends struct body to result
137 void _append_struct_body(std::string_view structBody);
138
139 /// @brief returns struct body string
140 [[nodiscard]] std::string _get_struct_body(std::string_view type, std::string_view name, bool canHaveVariableSizeArray,
141 const mstd::ordered_map<std::string, var_data>& variables);
142
143 /// @brief appends buffer body to result
144 void _append_buffer_body(std::string_view layoutData, std::string_view name, std::string_view bufferType,
145 bool canHaveVariableSizeArray, const mstd::ordered_map<std::string, var_data>& variables,
146 std::string_view varName = "");
147
148 /// @brief appends shader storage buffer body (SSBO)
149 void _append_shader_storage_buffer_body(std::string_view layoutData, std::string_view name,
150 const mstd::ordered_map<std::string, var_data>& variables, std::string_view varName = "",
151 std::string_view qualifiers = "");
152
153 /// @brief returns true if struct, UBO or SSBO with given name already was appended
154 [[nodiscard]] bool _contains_name(std::string_view name);
155
156 public:
157 /// @brief default constructor
159
160 /// @brief type visitor
161 template<class Type>
162 void visit(Type&& varType) {
163 using T = std::decay_t<Type>;
165 if (_uniqueStructs.contains(varType)) { return; }
166
168 }
169 }
170
171 /// @brief appends struct with given name
172 void append_struct(std::string_view name, const struct_type& structType);
173
174 /// @brief appends struct with generated name
175 void append_struct(const struct_type& structType);
176
177 /// @brief appends uniform buffer
178 void append_uniform_buffer(std::string_view layoutData, std::string_view name,
179 const mstd::ordered_map<std::string, var_data>& variables, std::string_view varName = "");
180
181 /// @brief appends shader storage buffer
182 void append_shader_storage_buffer(std::string_view layoutData, std::string_view name,
183 const mstd::ordered_map<std::string, var_data>& variables, std::string_view varName = "",
184 std::string_view qualifiers = "");
185
186 /// @brief returns result string
187 [[nodiscard]] const std::string& to_string() const noexcept;
188 };
189} // namespace glslstruct::utils
190
191 #include <glslstruct/writer/glsl/opengl/writer.hpp>
192 #include <glslstruct/writer/glsl/vulkan/writer.hpp>
193
194 #endif
195#endif
type visitor which gets array and sub arrays counts in format [0][1]...
Definition writer.hpp:70
bool _canBeVariableSize
check for variable size arrays
Definition writer.hpp:73
glsl_array_count_getter(bool canBeVariableSize) noexcept
Definition writer.cpp:35
const std::string & get_result() const noexcept
returns result
Definition writer.cpp:37
std::string _result
result
Definition writer.hpp:75
void visit(Type &&varType)
visitor function
Definition writer.hpp:82
type visitor which returns variable type
Definition writer.hpp:30
void visit(Type &&varType)
type visitor
Definition writer.hpp:43
glsl_var_type_getter(const mstd::ordered_map< struct_type, std::string > &structsNames)
constructor with defined structs names
Definition writer.cpp:23
std::string _result
result
Definition writer.hpp:33
const std::string & get_result() const noexcept
returns result
Definition writer.cpp:26
mstd::ordered_map< struct_type, std::string > _structsNames
defined structs names
Definition writer.hpp:35
general glsl writer
Definition writer.hpp:120
std::unordered_set< std::string > _uniqueNames
unique names of all strucs, UBOs and SSBOs
Definition writer.hpp:131
mstd::ordered_map< struct_type, std::string > _uniqueStructs
struct name generator idx
Definition writer.hpp:128
static std::string _get_layout(std::string_view layoutData)
returns layout string
Definition writer.cpp:51
void visit(Type &&varType)
type visitor
Definition writer.hpp:162
void append_struct(std::string_view name, const struct_type &structType)
appends struct with given name
Definition writer.cpp:110
glsl_writer()
default constructor
void _append_shader_storage_buffer_body(std::string_view layoutData, std::string_view name, const mstd::ordered_map< std::string, var_data > &variables, std::string_view varName="", std::string_view qualifiers="")
appends shader storage buffer body (SSBO)
Definition writer.cpp:92
size_t _structIdx
struct name generator idx
Definition writer.hpp:126
void _append_struct_body(std::string_view structBody)
appends struct body to result
Definition writer.cpp:53
const std::string & to_string() const noexcept
returns result string
Definition writer.cpp:149
void append_struct(const struct_type &structType)
appends struct with generated name
Definition writer.cpp:128
std::string _get_struct_body(std::string_view type, std::string_view name, bool canHaveVariableSizeArray, const mstd::ordered_map< std::string, var_data > &variables)
returns struct body string
Definition writer.cpp:58
void append_shader_storage_buffer(std::string_view layoutData, std::string_view name, const mstd::ordered_map< std::string, var_data > &variables, std::string_view varName="", std::string_view qualifiers="")
appends shader storage buffer
Definition writer.cpp:144
std::string _result
result
Definition writer.hpp:123
void _append_buffer_body(std::string_view layoutData, std::string_view name, std::string_view bufferType, bool canHaveVariableSizeArray, const mstd::ordered_map< std::string, var_data > &variables, std::string_view varName="")
appends buffer body to result
Definition writer.cpp:76
bool _contains_name(std::string_view name)
returns true if struct, UBO or SSBO with given name already was appended
Definition writer.cpp:100
void append_uniform_buffer(std::string_view layoutData, std::string_view name, const mstd::ordered_map< std::string, var_data > &variables, std::string_view varName="")
appends uniform buffer
Definition writer.cpp:139
std::string get_glsl_variable_string(std::string_view name, const base_type_handle &varType, bool canBeVariableSize, const mstd::ordered_map< struct_type, std::string > &structsNames)
returns variable line type name[0][1]...
Definition writer.cpp:45
#define glsl_struct_assert(expression,...)
glslstruct assert
Definition assert.hpp:33
std::string get_glsl_array_count_string(const base_type_handle &varType, bool canBeVariableSize)
returns array counts string
Definition writer.cpp:39
#define _GLSL_STRUCT_HAS_TYPES
check if user not disabled type containers using GLSL_STRUCT_DISABLE_TYPES
Definition config.hpp:162
std::string get_glsl_var_type_string(const base_type_handle &varType, const mstd::ordered_map< struct_type, std::string > &structsNames)
returns variable type
Definition writer.cpp:28
#define _GLSL_STRUCT_CONSTEXPR17
constexpr for c++17 and higher
Definition config.hpp:196
Definition writer.hpp:25
Main namespace of glslstruct library.
Definition scalar_layout_traits.hpp:23