Cogs.Core
Common.h
1#pragma once
2
3#include "Flags.h"
4#include "Foundation/HashFunctions.h"
5
6// Use to ensure arrays used for mapping enumeration values are of correct size.
7#define STATIC_ASSERT_MAPPING(mappingArray, sourceEnum) static_assert(sizeof(mappingArray) / sizeof(mappingArray[0]) == (int)sourceEnum, "Array size of " #mappingArray " must match number of enumerations in mapping array.");
8
9namespace Cogs
10{
12
14 struct NoConversion {};
15
17
21 template<typename Type, typename Convertible = NoConversion>
22 struct Handle_t
23 {
25 typedef int64_t handle_type;
26
30 Handle_t() : handle(-1) {}
31
35 Handle_t(const Handle_t & other) : handle(other.handle) {}
36
42 explicit Handle_t(const handle_type& handle) : handle(handle) {}
43
49 Handle_t(const Convertible & other) : handle(other.handle) {}
50
54 Handle_t & operator=(const Handle_t & other) { this->handle = other.handle; return *this; }
55
62 [[nodiscard]] bool operator==(const Handle_t & other) const { return this->handle == other.handle; }
63
65 explicit operator bool() const { return handle != 0 && handle != -1; }
66
72 operator Convertible() { return Convertible(this->handle); };
73
75 handle_type handle;
76
78 const static Handle_t NoHandle;
79
81 const static Handle_t InvalidHandle;
82 };
83
84 template<typename T, typename U>
86
87 template<typename T, typename U>
89}
90
91namespace std
92{
96 template<typename T, typename U>
97 struct hash<Cogs::Handle_t<T, U>>
98 {
99 std::size_t operator()(const Cogs::Handle_t<T, U> & handle) const
100 {
101 return std::hash<typename Cogs::Handle_t<T, U>::handle_type>()(handle.handle);
102 }
103 };
104}
105
106namespace Cogs
107{
111 enum class PrimitiveType
112 {
114 TriangleList = 0,
118 LineList,
120 LineStrip,
122 PointList,
131
132 ControlPoint1PatchList,
133 ControlPoint2PatchList,
134 ControlPoint3PatchList,
135 ControlPoint4PatchList,
136
137 // Provided for size comparisons.
138 PrimitiveType_Size,
139
140 None = -1,
141 };
142
146 enum class Framebuffer
147 {
149 Front = 0,
151 Back
152 };
153
154 enum class BlendFlags
155 {
156 None = 0,
157 AlphaToCoverage = 1 << 0,
158 IndependentBlend = 1 << 1
159 };
160 COGS_RENDERING_ENABLE_ENUM_FLAGS(BlendFlags);
161
165 enum class PresentFlags
166 {
168 None = 0,
170 Default = None,
172 NoSwap = 0x0001L,
173 };
174 COGS_RENDERING_ENABLE_ENUM_FLAGS(PresentFlags);
175
179 enum class Axis {
180 PositiveX,
181 NegativeX,
182 PositiveY,
183 NegativeY,
184 PositiveZ,
185 NegativeZ,
186
187 Count,
188 Unknown = -1,
189 };
190
192
193 struct InputLayoutTag {};
194 struct BufferTag {};
195 struct TextureTag {};
196 struct SamplerTag {};
197 struct EffectTag {};
198 struct ShaderTag {};
199 struct RenderPipelineTag {};
200 struct ComputePipelineTag {};
201 struct RenderTargetTag {};
202 struct DepthStencilTag {};
203 struct ConstantBufferBindingTag {};
204 struct VariableBindingTag {};
205 struct TextureBindingTag {};
206 struct SamplerStateBindingTag {};
207 struct BufferBindingTag {};
208 struct TextureViewTag {};
209 struct VertexFormatTag {};
210 struct VertexArrayObjectTag {};
211 struct FenceTag {};
212
214
215 typedef Handle_t<struct RasterizerState> RasterizerStateHandle;
216 typedef Handle_t<struct DepthStencilState> DepthStencilStateHandle;
217 typedef Handle_t<struct BlendState> BlendStateHandle;
218
219 typedef Handle_t<InputLayoutTag> InputLayoutHandle;
220
221 typedef Handle_t<BufferTag> BufferHandle;
222 typedef Handle_t<BufferTag, BufferHandle> VertexBufferHandle;
223 typedef Handle_t<BufferTag, BufferHandle> IndexBufferHandle;
224
225 typedef Handle_t<VertexFormatTag> VertexFormatHandle;
226
227 typedef Handle_t<VertexArrayObjectTag> VertexArrayObjectHandle;
228
229 typedef intptr_t TextureNativeHandle;
230 typedef Handle_t<TextureTag> TextureHandle;
231 typedef Handle_t<SamplerTag> SamplerStateHandle;
232
233 typedef Handle_t<EffectTag> EffectHandle;
234 typedef Handle_t<ShaderTag> ShaderHandle;
235 typedef Handle_t<RenderPipelineTag> RenderPipelineHandle;
236 typedef Handle_t<ComputePipelineTag> ComputePipelineHandle;
237
238 typedef Handle_t<RenderTargetTag> RenderTargetHandle;
239 typedef Handle_t<DepthStencilTag> DepthStencilHandle;
240
241 typedef Handle_t<VariableBindingTag> EffectVariableHandle;
242 typedef Handle_t<ConstantBufferBindingTag> ConstantBufferBindingHandle;
243 typedef Handle_t<TextureBindingTag> TextureBindingHandle;
244 typedef Handle_t<SamplerStateBindingTag> SamplerStateBindingHandle;
245 typedef Handle_t<BufferBindingTag> BufferBindingHandle;
246
247 typedef Handle_t<TextureViewTag> TextureViewHandle;
248
249 typedef Handle_t<FenceTag> FenceHandle;
250
251 template<typename T>
252 inline bool HandleIsValid(const Handle_t<T> handle)
253 {
254 return (handle != Handle_t<T>::NoHandle && handle != Handle_t<T>::InvalidHandle);
255 }
256
257 template<typename T, typename U>
258 inline bool HandleIsValid(const Handle_t<T, U> handle)
259 {
260 return (handle != Handle_t<T, U>::NoHandle && handle != Handle_t<T, U>::InvalidHandle);
261 }
262
263 inline bool isDebug()
264 {
265#ifdef _DEBUG
266 return true;
267#else
268 return false;
269#endif
270 }
271}
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
@ Default
Default type of graphics device, same as unknown.
Axis
Axis definitions.
Definition: Common.h:179
Framebuffer
Framebuffers to select from when doing framebuffer operations.
Definition: Common.h:147
@ Back
Back buffer.
@ Front
Front buffer.
PresentFlags
Flags controlling presentation.
Definition: Common.h:166
@ NoSwap
Disables frame buffer swapping. This will leave the currently presented buffer as is.
PrimitiveType
Primitive types for interpreting vertex data sent to the graphics pipeline.
Definition: Common.h:112
@ PointList
List of points.
@ TriangleStrip
Triangle strip.
@ LineList
List of lines.
@ TriangleStripAdjacency
Triangle strip with adjacency.
@ LineStripAdjacency
Line strip with adjacency.
@ LineListAdjacency
List of lines with adjacency.
@ TriangleListAdjacency
List of triangles with adjacency.
@ LineStrip
Line strip.
@ TriangleList
List of triangles.
@ None
Default flags.
STL namespace.
Handle template class used to provide opaque, non-converting handles.
Definition: Common.h:23
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:78
Handle_t(const Handle_t &other)
Copy constructor.
Definition: Common.h:35
Handle_t(const handle_type &handle)
Construct a handle with an explicit internal resource handle.
Definition: Common.h:42
Handle_t(const Convertible &other)
Conversion constructor from Convertible type.
Definition: Common.h:49
handle_type handle
Internal resource handle.
Definition: Common.h:75
Handle_t()
Construct a handle.
Definition: Common.h:30
int64_t handle_type
Internal handle storage type.
Definition: Common.h:25
static const Handle_t InvalidHandle
Represents an invalid handle.
Definition: Common.h:81
bool operator==(const Handle_t &other) const
Equality operator.
Definition: Common.h:62
Handle_t & operator=(const Handle_t &other)
Assignment operator.
Definition: Common.h:54