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#include "Resources/EffectDefinition.h"
23
24
25namespace Cogs::Core
26{
27 class Context;
28
29 enum class EnginePermutationFlags
30 {
31 None = 0,
32 DepthOnly = 1 << 0,
33 NoShading = 1 << 1,
34 ReverseDepth = 1 << 2,
35 HasBuiltins = 1 << 3,
36 };
37 ENABLE_ENUM_FLAGS(EnginePermutationFlags);
38
40 {
41 EnginePermutationDefinition* next = nullptr;
42
43 std::string name;
44 std::string inherits;
45
46 std::string vertexShader;
47 std::string geometryShader;
48 std::string pixelShader;
49
50 ShaderVariants variants;
51
52 RenderFlags requiredFlags = RenderFlags::None;
53
54 EnginePermutationFlags flags = EnginePermutationFlags::None;
55
56 MaterialProperties properties;
57 PreprocessorDefinitions definitions;
58
59 ShaderInterfaceDefinition surfaceInterface;
60 ShaderInterfaceDefinition vertexInterface;
62 };
63
64 struct COGSCORE_DLL_API EnginePermutation
65 {
66 void initialize(Context * context, EnginePermutationDefinition * definition);
67
68 const EnginePermutationDefinition * getDefinition() const { return definition; }
69 EnginePermutationDefinition * getDefinition() { return definition; }
70
71 std::span<const ShaderVariantDefinition> getVariants() const { return definition->variants; }
72 std::span<const ShaderVariantSelector> getSelectors() const { return selectors; }
73
74 void setVariant(const StringView & key, const StringView & value);
75 void setVariant(const StringView & key, const char * value) { setVariant(key, StringView(value)); }
76 void setVariant(const StringView & key, bool value);
77 void setVariant(const StringView & key, int value);
78
79 bool hasVariant(const StringView & key);
80
81 void setProperty(const StringView & name, const void * value, int valueSize);
82
83 bool isDirty() const { return dirty; }
84 bool isDepthOnly() const { return (flags & EnginePermutationFlags::DepthOnly) != 0; }
85 bool isShadowPass() const { return isDepthOnly(); }
86 bool isShadingPass() const { return (flags & EnginePermutationFlags::NoShading) == 0; }
87 bool hasBuiltins() const { return (flags & EnginePermutationFlags::HasBuiltins) != 0; }
88 bool isReverseDepth() const { return (flags & EnginePermutationFlags::ReverseDepth) != 0; }
89
90 size_t getCode() const;
91 size_t getNameHash() const { return nameHash; }
92 size_t getIndex() const { return index; }
93
94 const std::string & getName() const { return definition->name; }
95
96 void updateFlags();
97
98 public:
99 RenderItemFlags requiredFlags = RenderItemFlags::None;
100 size_t permutationMask = 0;
101
102 ConstantBuffers constantBuffers;
103 std::vector<Cogs::BufferHandle> bufferHandles;
104
105 private:
106 mutable size_t code = 0; // is updated by getCode if dirty is true.
107 mutable bool dirty = false; // triggers update of code.
108 size_t nameHash = 0;
109 size_t index = 0;
110 EnginePermutationFlags flags = EnginePermutationFlags::None;
111 EnginePermutationDefinition * definition = nullptr;
112 ShaderVariantSelectors selectors;
113
114 friend struct EnginePermutations;
115 };
116
117 struct COGSCORE_DLL_API EnginePermutations
118 {
119 EnginePermutations(Context * context);
120
122
123 void initialize(Context * context);
124
125 EnginePermutation * create(const StringView & name, EnginePermutation * base = nullptr);
126
127 const EnginePermutation * get(size_t index) const;
128 const EnginePermutation * get(const StringView & name) const;
129
130 EnginePermutation * get(size_t index);
131 EnginePermutation * get(const StringView & name);
132
133 EnginePermutation * getInternal(size_t index);
134 EnginePermutation * getInternal(const StringView & name);
135
136 bool exists(const StringView & name) const;
137
138 size_t size() const { return enginePermutations.size(); }
139 size_t getIndex(const StringView & name) const;
140
141 bool load(const StringView & resource, bool isContent = false);
142 EnginePermutationFlags getFlag(const StringView & name);
143
144 protected:
145 std::vector<EnginePermutation> enginePermutations;
147
148 [[nodiscard]] bool inherit(EnginePermutation* enginePermutation, const EnginePermutation* base);
149
150 EnginePermutation* create(EnginePermutationDefinition* definition, EnginePermutation* base = nullptr);
151
152 EnginePermutationDefinition* createDefinition();
153
154 // List of engine permutation definitions to free when engine shuts down.
155 EnginePermutationDefinition* firstDefinition = nullptr;
156
157 Context * mContext;
158 };
159}
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