CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Shader.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
12#include "ShaderSource.hpp"
13#include "ShaderType.hpp"
14
17
24
27
28namespace CeresEngine {
29
32 class ShaderSpecializationConstant : private Variant<String, Int64, bool> {
33 private:
34 template<typename U> friend struct std::hash;
36
37 public:
40
43
46
48 CE_EXPLICIT(false) ShaderSpecializationConstant(const bool string) : super(Int64(string)) {}
49
51 CE_EXPLICIT(false) ShaderSpecializationConstant(const UInt8 string) : super(Int64(string)) {}
52
54 CE_EXPLICIT(false) ShaderSpecializationConstant(const UInt16 string) : super(Int64(string)) {}
55
57 CE_EXPLICIT(false) ShaderSpecializationConstant(const UInt32 string) : super(Int64(string)) {}
58
60 CE_EXPLICIT(false) ShaderSpecializationConstant(const Int8 string) : super(Int64(string)) {}
61
63 CE_EXPLICIT(false) ShaderSpecializationConstant(const Int16 string) : super(Int64(string)) {}
64
66 CE_EXPLICIT(false) ShaderSpecializationConstant(const Int32 string) : super(Int64(string)) {}
67
69 CE_EXPLICIT(false) ShaderSpecializationConstant(const Int64 string) : super(Int64(string)) {}
70
73
76
77 public:
82 template<typename T> [[nodiscard]] bool is() const noexcept;
83
85 template<> [[nodiscard]] bool is<String>() const noexcept { return super::is<String>(); }
86
88 template<> [[nodiscard]] bool is<StringView>() const noexcept { return super::is<String>(); }
89
91 template<> [[nodiscard]] bool is<bool>() const noexcept { return super::is<bool>(); }
92
94 template<> [[nodiscard]] bool is<UInt8>() const noexcept { return isConvertibleAs<UInt8>(); }
95
97 template<> [[nodiscard]] bool is<UInt16>() const noexcept { return isConvertibleAs<UInt16>(); }
98
100 template<> [[nodiscard]] bool is<UInt32>() const noexcept { return isConvertibleAs<UInt32>(); }
101
103 template<> [[nodiscard]] bool is<Int8>() const noexcept { return isConvertibleAs<Int8>(); }
104
106 template<> [[nodiscard]] bool is<Int16>() const noexcept { return isConvertibleAs<Int16>(); }
107
109 template<> [[nodiscard]] bool is<Int32>() const noexcept { return isConvertibleAs<Int32>(); }
110
112 template<> [[nodiscard]] bool is<Int64>() const noexcept { return isConvertibleAs<Int64>(); }
113
118 template<typename T> decltype(auto) as() const;
119
121 template<> [[nodiscard]] decltype(auto) as<String>() const { return super::as<String>(); }
122
124 template<> [[nodiscard]] decltype(auto) as<StringView>() const { return StringView(super::as<String>()); }
125
127 template<> [[nodiscard]] decltype(auto) as<bool>() const { return super::as<bool>(); }
128
130 template<> [[nodiscard]] decltype(auto) as<UInt8>() const { return convertAs<UInt8>(); }
131
133 template<> [[nodiscard]] decltype(auto) as<UInt16>() const { return convertAs<UInt16>(); }
134
136 template<> [[nodiscard]] decltype(auto) as<UInt32>() const { return convertAs<UInt32>(); }
137
139 template<> [[nodiscard]] decltype(auto) as<Int8>() const { return convertAs<Int8>(); }
140
142 template<> [[nodiscard]] decltype(auto) as<Int16>() const { return convertAs<Int16>(); }
143
145 template<> [[nodiscard]] decltype(auto) as<Int32>() const { return convertAs<Int32>(); }
146
148 template<> [[nodiscard]] decltype(auto) as<Int64>() const { return convertAs<Int64>(); }
149
150 using super::visit;
151
152 private:
154 template<typename T> [[nodiscard]] bool isConvertibleAs() const noexcept {
155 if(!super::is<Int64>()) {
156 return false;
157 }
158
159 const Int64 value = super::as<Int64>();
160 return value >= std::numeric_limits<T>::min() && value <= std::numeric_limits<T>::max();
161 }
162
164 template<typename T> [[nodiscard]] T convertAs() const noexcept {
165 const Int64 value = super::as<Int64>();
166 CE_ASSERT(value >= std::numeric_limits<T>::min() && value <= std::numeric_limits<T>::max());
167 return T(value);
168 }
169 };
170
173
175 enum class ShadingLanguage {
178 CESL,
179
181 HLSL,
182
184 GLSL,
185
187 SPIRV,
188 };
189
257
259 class CESLShader final : public TResource<CESLShader, Shader> {
261
262 public:
263 using super::super;
264
265 public:
268
271 };
272
274 class HLSLShader final : public TResource<HLSLShader, Shader> {
276
277 public:
278 using super::super;
279
280 public:
283
286 };
287
289 class GLSLShader final : public TResource<GLSLShader, Shader> {
291
292 public:
293 using super::super;
294
295 public:
298
301 };
302
304 class SPIRVShader final : public TResource<SPIRVShader, Shader> {
306
307 public:
308 using super::super;
309
310 public:
313
316 };
317
318} // namespace CeresEngine
319
321template<> struct std::hash<CeresEngine::ShaderSpecializationConstant> {
323 inline size_t operator()(const Type& object) const noexcept { return CeresEngine::hash(static_cast<const Type::super&>(object)); }
324};
325
326
327
328
329
330
331
#define CE_ASSERT(...)
Definition Macros.hpp:323
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
#define CE_META_CLASS_FRIEND(T)
Definition Forward.hpp:48
A shader that must be compiled as a "CeresEngine Shading Language".
Definition Shader.hpp:259
ShadingLanguage getShadingLanguage() const noexcept final
Specifies the shading language of the shader resource.
Definition Shader.hpp:267
Optional< ShaderBinary > compile(ShaderType type, const ShaderSpecializationConstants &specializationConstants={}) const final
Compiles the shader into a binary that can be loaded by the render API.
A shader that must be compiled as a "GLSL Shading Language".
Definition Shader.hpp:289
ShadingLanguage getShadingLanguage() const noexcept final
Specifies the shading language of the shader resource.
Definition Shader.hpp:297
Optional< ShaderBinary > compile(ShaderType type, const ShaderSpecializationConstants &specializationConstants={}) const final
Compiles the shader into a binary that can be loaded by the render API.
A shader that must be compiled as a "HLSL Shading Language".
Definition Shader.hpp:274
Optional< ShaderBinary > compile(ShaderType type, const ShaderSpecializationConstants &specializationConstants={}) const final
Compiles the shader into a binary that can be loaded by the render API.
ShadingLanguage getShadingLanguage() const noexcept final
Specifies the shading language of the shader resource.
Definition Shader.hpp:282
Definition Optional.hpp:17
A shader that is already compiled in the SPIR-V format.
Definition Shader.hpp:304
Optional< ShaderBinary > compile(ShaderType type, const ShaderSpecializationConstants &specializationConstants={}) const final
Compiles the shader into a binary that can be loaded by the render API.
ShadingLanguage getShadingLanguage() const noexcept final
Specifies the shading language of the shader resource.
Definition Shader.hpp:312
A ShaderBinary is an object responsible for wrapping the binary representation of a shader (i....
Definition ShaderSource.hpp:40
A resource that represents a shader that can be compiled and loaded into the runtime.
Definition Shader.hpp:192
String getSourceName() const
Gets the shader source name.
Shader(const ShaderSource &source, const ShaderStages &stage)
void setSource(const ShaderSource &source)
The shader source to compile the final shader to.
virtual Optional< ShaderBinary > compile(ShaderType type, const ShaderSpecializationConstants &specializationConstants={}) const =0
Compiles the shader into a binary that can be loaded by the render API.
virtual ShadingLanguage getShadingLanguage() const noexcept=0
Specifies the shading language of the shader resource.
~Shader() noexcept override=default
ShaderStages mStages
A set of stages supported by the shader.
Definition Shader.hpp:197
ShaderSource mSource
The shader source to compile the final shader to.
Definition Shader.hpp:200
Optional< ShaderBinary > compileShaderInternal(const ShaderSource &sourceCode, ShadingLanguage language, const URI &includeSearchPath) const
Internal method to compile the given shaderSource using the ShaderC compiler.
const ShaderSource & getSource() const noexcept
The shader source to compile the final shader to.
Definition Shader.hpp:209
ShaderSource preProcessShader(const ShaderSource &sourceCode, const ShaderSpecializationConstants &specializationConstants, const URI &includeSearchPath) const
Runs the shader pre-processor on the shader.
Async< Optional< ShaderBinary > > compileAsync(ShaderType type, ShaderSpecializationConstants specializationConstants={})
Asynchronously compiles the shader into a binary that can be loaded by the render API.
A ShaderSource is an object responsible for wrapping the textual representation of a shader (i....
Definition ShaderSource.hpp:112
A variant type that describes a value that can be used as a shader specialization constant.
Definition Shader.hpp:32
ShaderSpecializationConstant & operator=(ShaderSpecializationConstant &&)=default
bool isConvertibleAs() const noexcept
Checks if the value is numeric is a type and convertible to T.
Definition Shader.hpp:154
T convertAs() const noexcept
Converts a numeric type to T.
Definition Shader.hpp:164
decltype(auto) as() const
Gets the shader specialization constant value as type T.
ShaderSpecializationConstant()
Cretaes a new specialization constant with a default value of 0.
Definition Shader.hpp:39
ShaderSpecializationConstant & operator=(const ShaderSpecializationConstant &)=default
ShaderSpecializationConstant(const ShaderSpecializationConstant &)=default
bool is() const noexcept
Checks if the shader specialization constant holds a given type T.
ShaderSpecializationConstant(ShaderSpecializationConstant &&)=default
Utility template class that can be extended by Resources to automatically implement methods that are ...
Definition Resource.hpp:272
A Uniform Resource Identifier (URI) is a unique sequence of characters that identifies a logical or p...
Definition URI.hpp:54
Definition Variant.hpp:15
constexpr decltype(auto) visit(Visitor &&visitor) &
Definition Variant.hpp:37
Definition Application.hpp:19
sfl::small_unordered_flat_map< Key, T, N, KeyEqual, StdAllocator< std::pair< Key, T >, RawAllocator > > SmallUnorderedFlatMap
SmallUnorderedFlatMap is an unordered associative container similar to HashMap.
Definition SmallFlatMap.hpp:69
BasicStringView< char > StringView
Narrow string view used for handling narrow encoded text in UTF-8.
Definition String.hpp:190
cti::continuable< Args... > Async
Defines a non-copyable continuation type which uses the function2 backend for type erasure.
Definition Async.hpp:22
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
std::int32_t Int32
Definition DataTypes.hpp:21
std::uint16_t UInt16
Definition DataTypes.hpp:20
std::uint8_t UInt8
Definition DataTypes.hpp:17
std::int64_t Int64
Definition DataTypes.hpp:24
std::uint32_t UInt32
Definition DataTypes.hpp:23
SmallUnorderedFlatMap< String, ShaderSpecializationConstant, 5 > ShaderSpecializationConstants
A map type that represents a map of shader specialization constants.
Definition Shader.hpp:172
ShadingLanguage
Enumeration that indicates the supported shading languages.
Definition Shader.hpp:175
@ CESL
Specifies that the shader is using the CeresEngine Shading Language, an extension to HLSL with CeresE...
@ GLSL
Specifies that the shader is using the GLSL Shading Language.
@ SPIRV
Specifies that the shader compiled with SPIR-V.
@ HLSL
Specifies that the shader is using the HLSL Shading Language.
std::int8_t Int8
Definition DataTypes.hpp:15
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
std::int16_t Int16
Definition DataTypes.hpp:18
Definition Span.hpp:668
Shader type enumeration.
Definition ShaderType.hpp:59
@ Undefined
Undefined shader type.
Definition ShaderType.hpp:62