Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Pipeline.cpp
1/*
2Copyright (C) 2016-2019,2021,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include <unordered_map>
18#include "aeongames/Pipeline.hpp"
20#include "aeongames/ProtoBufHelpers.hpp"
21#ifdef _MSC_VER
22#pragma warning( push )
23#pragma warning( disable : PROTOBUF_WARNINGS )
24#endif
25#include "pipeline.pb.h"
26#include "property.pb.h"
27#ifdef _MSC_VER
28#pragma warning( pop )
29#endif
30
31namespace AeonGames
32{
33#if 0
34 static const std::unordered_map<PipelineMsg_Topology, Topology> TopologyMap
35 {
36 {PipelineMsg_Topology::PipelineMsg_Topology_POINT_LIST, POINT_LIST},
37 {PipelineMsg_Topology::PipelineMsg_Topology_LINE_STRIP, LINE_STRIP},
38 {PipelineMsg_Topology::PipelineMsg_Topology_LINE_LIST, LINE_LIST},
39 {PipelineMsg_Topology::PipelineMsg_Topology_TRIANGLE_STRIP, TRIANGLE_STRIP},
40 {PipelineMsg_Topology::PipelineMsg_Topology_TRIANGLE_FAN, TRIANGLE_FAN},
41 {PipelineMsg_Topology::PipelineMsg_Topology_TRIANGLE_LIST, TRIANGLE_LIST},
42 {PipelineMsg_Topology::PipelineMsg_Topology_LINE_LIST_WITH_ADJACENCY, LINE_LIST_WITH_ADJACENCY},
43 {PipelineMsg_Topology::PipelineMsg_Topology_LINE_STRIP_WITH_ADJACENCY, LINE_STRIP_WITH_ADJACENCY},
44 {PipelineMsg_Topology::PipelineMsg_Topology_TRIANGLE_LIST_WITH_ADJACENCY, TRIANGLE_LIST_WITH_ADJACENCY},
45 {PipelineMsg_Topology::PipelineMsg_Topology_TRIANGLE_STRIP_WITH_ADJACENCY, TRIANGLE_STRIP_WITH_ADJACENCY},
46 {PipelineMsg_Topology::PipelineMsg_Topology_PATCH_LIST, PATCH_LIST},
47 };
48
49 static const std::unordered_map<UniformDescriptorMsg_Type, UniformType> UniformTypeMap
50 {
51 {UniformDescriptorMsg_Type::UniformDescriptorMsg_Type_SCALAR_FLOAT, SCALAR_FLOAT},
52 {UniformDescriptorMsg_Type::UniformDescriptorMsg_Type_SCALAR_UINT, SCALAR_UINT},
53 {UniformDescriptorMsg_Type::UniformDescriptorMsg_Type_SCALAR_INT, SCALAR_INT},
54 {UniformDescriptorMsg_Type::UniformDescriptorMsg_Type_VECTOR_FLOAT_2, VECTOR_FLOAT_2},
55 {UniformDescriptorMsg_Type::UniformDescriptorMsg_Type_VECTOR_FLOAT_3, VECTOR_FLOAT_3},
56 {UniformDescriptorMsg_Type::UniformDescriptorMsg_Type_VECTOR_FLOAT_4, VECTOR_FLOAT_4},
57 };
58#endif
59 Pipeline::Pipeline() = default;
60
62 = default;
64 {
65 return mTopologyClass;
66 }
67
68#if 0
69 const std::string& Pipeline::GetVertexShaderCode() const
70 {
71 return mVertexShaderCode;
72 }
73 const std::string& Pipeline::GetFragmentShaderCode() const
74 {
75 return mFragmentShaderCode;
76 }
77
78 const std::vector<std::tuple<UniformType, std::string >> & Pipeline::GetUniformDescriptors() const
79 {
80 return mUniformDescriptors;
81 }
82
83 const std::vector<std::string>& Pipeline::GetSamplerDescriptors() const
84 {
85 return mSamplerDescriptors;
86 }
87
88 std::string Pipeline::GetProperties () const
89 {
90 std::string properties{};
91 for ( auto& i : mUniformDescriptors )
92 {
93 switch ( std::get<0> ( i ) )
94 {
95 case SCALAR_FLOAT:
96 properties += "float " + std::get<1> ( i ) + ";\n";
97 break;
98 case SCALAR_UINT:
99 properties += "uint " + std::get<1> ( i ) + ";\n";
100 break;
101 case SCALAR_INT:
102 properties += "int " + std::get<1> ( i ) + ";\n";
103 break;
104 case VECTOR_FLOAT_2:
105 properties += "vec2 " + std::get<1> ( i ) + ";\n";
106 break;
107 case VECTOR_FLOAT_3:
108 properties += "vec3 " + std::get<1> ( i ) + ";\n";
109 break;
110 case VECTOR_FLOAT_4:
111 properties += "vec4 " + std::get<1> ( i ) + ";\n";
112 break;
113 default:
114 std::cout << LogLevel::Error << "Unknown Type." << std::endl;
115 throw std::runtime_error ( "Unknown Type." );
116 }
117 }
118 return properties;
119 }
120
121 static const std::array<const char*, 8> AttributeStrings
122 {
123 {
124 "VertexPosition", // 0
125 "VertexNormal", // 1
126 "VertexTangent", // 2
127 "VertexBitangent",// 3
128 "VertexUV", // 4
129 "VertexWeightIndices", // 5
130 "VertexWeights", // 6
131 "VertexColor" // 7
132 }
133 };
134
135 static const std::array<const char*, 8> AttributeTypes
136 {
137 {
138 "vec3",
139 "vec3",
140 "vec3",
141 "vec3",
142 "vec2",
143 "uvec4",
144 "vec4",
145 "vec3"
146 }
147 };
148
149 uint32_t Pipeline::GetAttributeBitmap() const
150 {
151 static const std::regex AttributeRegex
152 {
153 "\\bVertexPosition\\b|"
154 "\\bVertexNormal\\b|"
155 "\\bVertexTangent\\b|"
156 "\\bVertexBitangent\\b|"
157 "\\bVertexUV\\b|"
158 "\\bVertexWeightIndices\\b|"
159 "\\bVertexWeights\\b|"
160 "\\bVertexColor\\b" };
161
162 std::smatch attribute_matches;
163 uint32_t attributes{};
164 std::string code{mVertexShaderCode};
165 while ( std::regex_search ( code, attribute_matches, AttributeRegex ) )
166 {
167 for ( uint32_t i = 0; i < AttributeStrings.size(); ++i )
168 {
169 if ( attribute_matches.str().substr ( 6 ) == AttributeStrings[i] + 6 )
170 {
171 if ( ! ( attributes & ( 1 << i ) ) )
172 {
173 attributes |= ( 1 << i );
174 }
175 break;
176 }
177 }
178 code = attribute_matches.suffix();
179 }
180 return attributes;
181 }
182
183 std::string Pipeline::GetAttributes() const
184 {
185 std::string attribute_code{};
186 uint32_t attributes{ GetAttributeBitmap() };
187 for ( uint32_t i = 0; i < AttributeStrings.size(); ++i )
188 {
189 if ( attributes & ( 1 << i ) )
190 {
191 attribute_code.append ( "layout(location = " );
192 attribute_code.append ( std::to_string ( i ) );
193 attribute_code.append ( ") in " );
194 attribute_code.append ( AttributeTypes[i] );
195 attribute_code.append ( " " );
196 attribute_code.append ( AttributeStrings[i] );
197 attribute_code.append ( ";\n" );
198 }
199 }
200 return attribute_code;
201 }
202#endif
203
204 const std::string_view Pipeline::GetShaderCode ( ShaderType aType ) const
205 {
206 return mShaderCode[aType];
207 }
208
209 void Pipeline::LoadFromMemory ( const void* aBuffer, size_t aBufferSize )
210 {
211 LoadFromProtoBufObject<Pipeline, PipelineMsg, "AEONPLN"_mgk> ( *this, aBuffer, aBufferSize );
212 }
213
214 void Pipeline::LoadFromPBMsg ( const PipelineMsg& aPipelineMsg )
215 {
216 mShaderCode[VERT] = aPipelineMsg.vert();
217 mShaderCode[FRAG] = aPipelineMsg.frag();
218 mShaderCode[COMP] = aPipelineMsg.comp();
219 mShaderCode[TESC] = aPipelineMsg.tesc();
220 mShaderCode[TESE] = aPipelineMsg.tese();
221 mShaderCode[GEOM] = aPipelineMsg.geom();
222 if ( aPipelineMsg.has_topology_class() )
223 {
224 mTopologyClass = static_cast<uint32_t> ( aPipelineMsg.topology_class() );
225 mTopologyClass = mTopologyClass == 0 ? TOPOLOGY_CLASS_TRIANGLE : mTopologyClass;
226 }
227 else
228 {
229 mTopologyClass = TOPOLOGY_CLASS_TRIANGLE;
230 }
231 }
232
234 {
235 for ( auto& i : mShaderCode )
236 {
237 i.clear();
238 }
239 }
240}
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
DLL void Unload() final
Release all pipeline resources.
Definition Pipeline.cpp:233
static const uint32_t TOPOLOGY_CLASS_TRIANGLE
Triangle topology class bitmask.
Definition Pipeline.hpp:126
DLL const std::string_view GetShaderCode(ShaderType aType) const
Get the shader source code for the given shader stage.
Definition Pipeline.cpp:204
virtual DLL ~Pipeline()
Virtual destructor.
DLL void LoadFromMemory(const void *aBuffer, size_t aBufferSize) final
Load pipeline data from a memory buffer.
Definition Pipeline.cpp:209
DLL void LoadFromPBMsg(const PipelineMsg &aPipelineMsg)
Load pipeline configuration from a protobuf message.
Definition Pipeline.cpp:214
DLL uint32_t GetTopologyClass() const
Get the topology class bitmask for this pipeline.
Definition Pipeline.cpp:63
DLL Pipeline()
Default constructor.
<- This is here just for the literals
Definition AABB.hpp:31
void LoadFromProtoBufObject(T &aTarget, const void *aBuffer, size_t aBufferSize)
Loads a Protocol Buffer message from a buffer and populates a target object.
ShaderType
Shader stage types.
Definition Pipeline.hpp:96
@ VERT
Vertex shader.
Definition Pipeline.hpp:97
@ FRAG
Fragment shader.
Definition Pipeline.hpp:98
@ GEOM
Geometry shader.
Definition Pipeline.hpp:102
@ TESC
Tessellation control shader.
Definition Pipeline.hpp:100
@ TESE
Tessellation evaluation shader.
Definition Pipeline.hpp:101
@ COMP
Compute shader.
Definition Pipeline.hpp:99
@ TRIANGLE_STRIP
Connected triangle strip.
Definition Pipeline.hpp:43
@ PATCH_LIST
Patch list for tessellation.
Definition Pipeline.hpp:50
@ LINE_LIST_WITH_ADJACENCY
Line list with adjacency information.
Definition Pipeline.hpp:46
@ TRIANGLE_LIST_WITH_ADJACENCY
Triangle list with adjacency information.
Definition Pipeline.hpp:48
@ POINT_LIST
List of individual points.
Definition Pipeline.hpp:40
@ LINE_STRIP
Connected line segments.
Definition Pipeline.hpp:41
@ LINE_LIST
Pairs of vertices forming individual lines.
Definition Pipeline.hpp:42
@ TRIANGLE_LIST
Independent triangles.
Definition Pipeline.hpp:45
@ TRIANGLE_STRIP_WITH_ADJACENCY
Triangle strip with adjacency information.
Definition Pipeline.hpp:49
@ TRIANGLE_FAN
Triangles sharing a common vertex.
Definition Pipeline.hpp:44
@ LINE_STRIP_WITH_ADJACENCY
Line strip with adjacency information.
Definition Pipeline.hpp:47
@ Error
Error conditions.
Definition LogLevel.hpp:33