Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
RenderStateBlock.cpp
Go to the documentation of this file.
1
2
4
5namespace Divide {
6
7namespace TypeUtil
8{
9 const char* ComparisonFunctionToString(const ComparisonFunction func) noexcept
10 {
12 }
13
14 const char* StencilOperationToString(const StencilOperation op) noexcept
15 {
17 }
18
19 const char* FillModeToString(const FillMode mode) noexcept
20 {
21 return Names::fillMode[to_base(mode)];
22 }
23
24 const char* CullModeToString(const CullMode mode) noexcept
25 {
26 return Names::cullModes[to_base(mode)];
27 }
28
30 {
31 for (U8 i = 0; i < to_U8(ComparisonFunction::COUNT); ++i)
32 {
33 if (strcmp(name, Names::compFunctionNames[i]) == 0)
34 {
35 return static_cast<ComparisonFunction>(i);
36 }
37 }
38
40 }
41
42 StencilOperation StringToStencilOperation(const char* name) noexcept
43 {
44 for (U8 i = 0; i < to_U8(StencilOperation::COUNT); ++i)
45 {
46 if (strcmp(name, Names::stencilOpNames[i]) == 0)
47 {
48 return static_cast<StencilOperation>(i);
49 }
50 }
51
53 }
54
55 FillMode StringToFillMode(const char* name) noexcept
56 {
57 for (U8 i = 0; i < to_U8(FillMode::COUNT); ++i)
58 {
59 if (strcmp(name, Names::fillMode[i]) == 0)
60 {
61 return static_cast<FillMode>(i);
62 }
63 }
64
65 return FillMode::COUNT;
66 }
67
68 CullMode StringToCullMode(const char* name) noexcept
69 {
70 for (U8 i = 0; i < to_U8(CullMode::COUNT); ++i)
71 {
72 if (strcmp(name, Names::cullModes[i]) == 0)
73 {
74 return static_cast<CullMode>(i);
75 }
76 }
77
78 return CullMode::COUNT;
79 }
80};
81
82size_t GetHash( const RenderStateBlock& block )
83{
84 // Avoid small float rounding errors offsetting the general hash value
85 const U32 zBias = to_U32(std::floor( block._zBias * 1000.0f + 0.5f));
86 const U32 zUnits = to_U32(std::floor( block._zUnits * 1000.0f + 0.5f));
87
88 size_t hash = 59;
90 to_U32( block._cullMode),
93 to_U32(block._zFunc),
94 zBias,
95 zUnits,
97 block._stencilEnabled,
98 block._stencilRef,
99 block._stencilMask,
100 block._stencilWriteMask,
101 block._frontFaceCCW,
102 to_U32(block._stencilFailOp),
103 to_U32(block._stencilZFailOp),
104 to_U32(block._stencilPassOp),
105 to_U32(block._stencilFunc),
106 to_U32(block._fillMode),
107 block._tessControlPoints,
109 block._rasterizationEnabled );
110
111 return hash;
112}
113
114void SaveToXML(const RenderStateBlock& block, const std::string& entryName, boost::property_tree::ptree& pt)
115{
116
117 pt.put(entryName + ".colourWrite.<xmlattr>.r", block._colourWrite.b[0] == 1);
118 pt.put(entryName + ".colourWrite.<xmlattr>.g", block._colourWrite.b[1] == 1);
119 pt.put(entryName + ".colourWrite.<xmlattr>.b", block._colourWrite.b[2] == 1);
120 pt.put(entryName + ".colourWrite.<xmlattr>.a", block._colourWrite.b[3] == 1);
121
122 pt.put(entryName + ".zBias", block._zBias);
123 pt.put(entryName + ".zUnits", block._zUnits);
124
125 pt.put(entryName + ".zFunc", TypeUtil::ComparisonFunctionToString(block._zFunc));
126 pt.put(entryName + ".tessControlPoints", block._tessControlPoints);
127 pt.put(entryName + ".cullMode", TypeUtil::CullModeToString(block._cullMode));
128 pt.put(entryName + ".fillMode", TypeUtil::FillModeToString(block._fillMode));
129
130 pt.put(entryName + ".frontFaceCCW", block._frontFaceCCW);
131 pt.put(entryName + ".scissorTestEnabled", block._scissorTestEnabled);
132 pt.put(entryName + ".depthTestEnabled", block._depthTestEnabled);
133 pt.put(entryName + ".depthWriteEnabled", block._depthWriteEnabled);
134
135 pt.put(entryName + ".stencilEnable", block._stencilEnabled);
136 pt.put(entryName + ".stencilFailOp", TypeUtil::StencilOperationToString(block._stencilFailOp));
137 pt.put(entryName + ".stencilPassOp", TypeUtil::StencilOperationToString(block._stencilPassOp));
138 pt.put(entryName + ".stencilZFailOp", TypeUtil::StencilOperationToString(block._stencilZFailOp));
139 pt.put(entryName + ".stencilFunc", TypeUtil::ComparisonFunctionToString(block._stencilFunc));
140 pt.put(entryName + ".stencilRef", block._stencilRef);
141 pt.put(entryName + ".stencilMask", block._stencilMask);
142 pt.put(entryName + ".stencilWriteMask", block._stencilWriteMask);
143}
144
145void LoadFromXML(const std::string& entryName, const boost::property_tree::ptree& pt, RenderStateBlock& blockInOut)
146{
147 blockInOut._colourWrite.b[0] = pt.get(entryName + ".colourWrite.<xmlattr>.r", blockInOut._colourWrite.b[0]);
148 blockInOut._colourWrite.b[1] = pt.get(entryName + ".colourWrite.<xmlattr>.g", blockInOut._colourWrite.b[1]);
149 blockInOut._colourWrite.b[2] = pt.get(entryName + ".colourWrite.<xmlattr>.b", blockInOut._colourWrite.b[2]);
150 blockInOut._colourWrite.b[3] = pt.get(entryName + ".colourWrite.<xmlattr>.a", blockInOut._colourWrite.b[3]);
151 blockInOut._zBias = pt.get(entryName + ".zBias", blockInOut._zBias);
152 blockInOut._zUnits = pt.get(entryName + ".zUnits", blockInOut._zUnits);
153 blockInOut._zFunc = TypeUtil::StringToComparisonFunction(pt.get(entryName + ".zFunc", TypeUtil::ComparisonFunctionToString( blockInOut._zFunc)).c_str());
154 blockInOut._tessControlPoints = pt.get(entryName + ".tessControlPoints", blockInOut._tessControlPoints);
155 blockInOut._cullMode = TypeUtil::StringToCullMode(pt.get(entryName + ".cullMode", TypeUtil::CullModeToString(blockInOut._cullMode)).c_str());
156 blockInOut._fillMode = TypeUtil::StringToFillMode(pt.get(entryName + ".fillMode", TypeUtil::FillModeToString(blockInOut._fillMode)).c_str());
157 blockInOut._frontFaceCCW = pt.get(entryName + ".frontFaceCCW", blockInOut._frontFaceCCW);
158 blockInOut._scissorTestEnabled = pt.get(entryName + ".scissorTestEnabled", blockInOut._scissorTestEnabled);
159 blockInOut._depthTestEnabled = pt.get(entryName + ".depthTestEnabled", blockInOut._depthTestEnabled);
160 blockInOut._depthWriteEnabled = pt.get(entryName + ".depthWriteEnabled", blockInOut._depthWriteEnabled);
161 blockInOut._stencilEnabled = pt.get(entryName + ".stencilEnable", blockInOut._stencilEnabled);
162 blockInOut._stencilRef = pt.get(entryName + ".stencilRef", blockInOut._stencilRef);
163 blockInOut._stencilFailOp = TypeUtil::StringToStencilOperation(pt.get(entryName + ".stencilFailOp", TypeUtil::StencilOperationToString(blockInOut._stencilFailOp)).c_str());
164 blockInOut._stencilZFailOp = TypeUtil::StringToStencilOperation(pt.get(entryName + ".stencilPassOp", TypeUtil::StencilOperationToString(blockInOut._stencilPassOp)).c_str());
165 blockInOut._stencilPassOp = TypeUtil::StringToStencilOperation(pt.get(entryName + ".stencilZFailOp",TypeUtil::StencilOperationToString(blockInOut._stencilZFailOp)).c_str());
166 blockInOut._stencilFunc = TypeUtil::StringToComparisonFunction(pt.get(entryName + ".stencilFunc", TypeUtil::ComparisonFunctionToString(blockInOut._stencilFunc)).c_str());
167 blockInOut._stencilMask = pt.get(entryName + ".stencilMask", blockInOut._stencilMask);
168 blockInOut._stencilWriteMask = pt.get(entryName + ".stencilWriteMask", blockInOut._stencilWriteMask);
169}
170
171};
static constexpr const char * cullModes[]
static constexpr const char * fillMode[]
static constexpr const char * compFunctionNames[]
static constexpr const char * stencilOpNames[]
FillMode StringToFillMode(const char *name) noexcept
const char * StencilOperationToString(StencilOperation op) noexcept
const char * CullModeToString(CullMode mode) noexcept
const char * ComparisonFunctionToString(ComparisonFunction func) noexcept
const char * FillModeToString(FillMode mode) noexcept
CullMode StringToCullMode(const char *name) noexcept
ComparisonFunction StringToComparisonFunction(const char *name) noexcept
StencilOperation StringToStencilOperation(const char *name) noexcept
void Hash_combine(size_t &seed, const T &v, const Rest &... rest) noexcept
a la Boost
Definition: MathHelper.inl:799
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U32 to_U32(const T value)
uint8_t U8
StencilOperation
Valid front and back stencil test actions.
@ COUNT
Place all properties above this.
size_t GetHash(const PropertyDescriptor< T > &descriptor) noexcept
Definition: Resource.inl:40
void SaveToXML(const TerrainDescriptor &descriptor, boost::property_tree::ptree &pt)
@ COUNT
Place all properties above this.
bool LoadFromXML(TerrainDescriptor &descriptor, const boost::property_tree::ptree &pt, std::string_view name)
FillMode
Defines all available fill modes for primitives.
@ COUNT
Place all properties above this.
constexpr U8 to_U8(const T value)
CullMode
Specifies whether front- or back-facing facets are candidates for culling.
@ COUNT
Place all properties above this.
uint32_t U32
constexpr auto to_base(const Type value) -> Type
StencilOperation _stencilZFailOp
StencilOperation _stencilFailOp
ComparisonFunction _stencilFunc
StencilOperation _stencilPassOp
ComparisonFunction _zFunc