Cogs.Core
EnginePermutations.h
1#pragma once
2
3#include "Base.h"
4
5#include "Resources/VariantDefinition.h"
6#include "Resources/MaterialPropertyBuffer.h"
7#include "Resources/MaterialProperty.h"
8#include "Resources/MaterialDefinition.h"
9#include "Resources/ConstantBuffers.h"
10
11#include "Components/Core/RenderComponent.h"
12
13#include "Utilities/Parsing.h"
14
15#include "Rendering/IEffects.h"
16
17#include "Foundation/StringView.h"
18#include "Foundation/Collections/Pool.h"
19
20#include "Renderer/RenderList.h"
21
22
23namespace Cogs::Core
24{
25 class Context;
26
27 enum class EnginePermutationFlags
28 {
29 None = 0,
30 DepthOnly = 1 << 0,
31 NoShading = 1 << 1,
32 ReverseDepth = 1 << 2,
33 HasBuiltins = 1 << 3,
34 };
35 ENABLE_ENUM_FLAGS(EnginePermutationFlags);
36
38 {
39 EnginePermutationDefinition* next = nullptr;
40
41 std::string name;
42 std::string inherits;
43
44 std::string vertexShader;
45 std::string geometryShader;
46 std::string pixelShader;
47
48 ShaderVariants variants;
49
50 RenderFlags requiredFlags = RenderFlags::None;
51
52 EnginePermutationFlags flags = EnginePermutationFlags::None;
53
54 MaterialProperties properties;
55 PreprocessorDefinitions definitions;
56 };
57
58 struct COGSCORE_DLL_API EnginePermutation
59 {
60 void initialize(Context * context, EnginePermutationDefinition * definition);
61
62 const EnginePermutationDefinition * getDefinition() const { return definition; }
63 EnginePermutationDefinition * getDefinition() { return definition; }
64
65 std::span<const ShaderVariantDefinition> getVariants() const { return definition->variants; }
66 std::span<const ShaderVariantSelector> getSelectors() const { return selectors; }
67
68 void setVariant(const StringView & key, const StringView & value);
69 void setVariant(const StringView & key, const char * value) { setVariant(key, StringView(value)); }
70 void setVariant(const StringView & key, bool value);
71 void setVariant(const StringView & key, int value);
72
73 bool hasVariant(const StringView & key);
74
75 void setProperty(const StringView & name, const void * value, int valueSize);
76
77 bool isDirty() const { return dirty; }
78 bool isDepthOnly() const { return (flags & EnginePermutationFlags::DepthOnly) != 0; }
79 bool isShadowPass() const { return isDepthOnly(); }
80 bool isShadingPass() const { return (flags & EnginePermutationFlags::NoShading) == 0; }
81 bool hasBuiltins() const { return (flags & EnginePermutationFlags::HasBuiltins) != 0; }
82 bool isReverseDepth() const { return (flags & EnginePermutationFlags::ReverseDepth) != 0; }
83
84 size_t getCode() const;
85 size_t getNameHash() const { return nameHash; }
86 size_t getIndex() const { return index; }
87
88 const std::string & getName() const { return definition->name; }
89
90 void updateFlags();
91
92 public:
93 RenderItemFlags requiredFlags = RenderItemFlags::None;
94 size_t permutationMask = 0;
95
96 ConstantBuffers constantBuffers;
97 std::vector<Cogs::BufferHandle> bufferHandles;
98
99 private:
100 mutable size_t code = 0; // is updated by getCode if dirty is true.
101 mutable bool dirty = false; // triggers update of code.
102 size_t nameHash = 0;
103 size_t index = 0;
104 EnginePermutationFlags flags = EnginePermutationFlags::None;
105 EnginePermutationDefinition * definition = nullptr;
106 ShaderVariantSelectors selectors;
107
108 friend struct EnginePermutations;
109 };
110
111 struct COGSCORE_DLL_API EnginePermutations
112 {
113 EnginePermutations(Context * context);
114
116
117 void initialize(Context * context);
118
119 EnginePermutation * create(const StringView & name, EnginePermutation * base = nullptr);
120
121 const EnginePermutation * get(size_t index) const;
122 const EnginePermutation * get(const StringView & name) const;
123
124 EnginePermutation * get(size_t index);
125 EnginePermutation * get(const StringView & name);
126
127 EnginePermutation * getInternal(size_t index);
128 EnginePermutation * getInternal(const StringView & name);
129
130 bool exists(const StringView & name) const;
131
132 size_t size() const { return enginePermutations.size(); }
133 size_t getIndex(const StringView & name) const;
134
135 bool load(const StringView & resource, bool isContent = false);
136 EnginePermutationFlags getFlag(const StringView & name);
137
138 protected:
139 std::vector<EnginePermutation> enginePermutations;
141
142 [[nodiscard]] bool inherit(EnginePermutation* enginePermutation, const EnginePermutation* base);
143
144 EnginePermutation* create(EnginePermutationDefinition* definition, EnginePermutation* base = nullptr);
145
146 EnginePermutationDefinition* createDefinition();
147
148 // List of engine permutation definitions to free when engine shuts down.
149 EnginePermutationDefinition* firstDefinition = nullptr;
150
151 Context * mContext;
152 };
153}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
RenderFlags
Contains flags to describe rendering behavior.
@ None
No render flags set.
std::vector< PreprocessorDefinition > PreprocessorDefinitions
A set of preprocessor definitions.
Definition: IEffects.h:13
Pool used to store elements of ElementType.
Definition: Pool.h:17