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
198 enum class ResourceStatus {
200 Error,
202 Pending,
204 Ready,
205 };
206
208
209 struct InputLayoutTag {};
210 struct BufferTag {};
211 struct TextureTag {};
212 struct SamplerTag {};
213 struct EffectTag {};
214 struct ShaderTag {};
215 struct RenderPipelineTag {};
216 struct ComputePipelineTag {};
217 struct RenderTargetTag {};
218 struct DepthStencilTag {};
219 struct ConstantBufferBindingTag {};
220 struct VariableBindingTag {};
221 struct TextureBindingTag {};
222 struct SamplerStateBindingTag {};
223 struct BindGroupConstantBufferBindingTag {};
224 struct BindGroupTextureBindingTag {};
225 struct BindGroupSamplerStateBindingTag {};
226 struct BufferBindingTag {};
227 struct BindGroupTag {};
228 struct TextureViewTag {};
229 struct VertexFormatTag {};
230 struct VertexArrayObjectTag {};
231 struct FenceTag {};
232
234
235 typedef Handle_t<struct RasterizerState> RasterizerStateHandle;
236 typedef Handle_t<struct DepthStencilState> DepthStencilStateHandle;
237 typedef Handle_t<struct BlendState> BlendStateHandle;
238
239 typedef Handle_t<InputLayoutTag> InputLayoutHandle;
240
241 typedef Handle_t<BufferTag> BufferHandle;
242 typedef Handle_t<BufferTag, BufferHandle> VertexBufferHandle;
243 typedef Handle_t<BufferTag, BufferHandle> IndexBufferHandle;
244
245 typedef Handle_t<VertexFormatTag> VertexFormatHandle;
246
247 typedef Handle_t<VertexArrayObjectTag> VertexArrayObjectHandle;
248
249 typedef intptr_t TextureNativeHandle;
250 typedef Handle_t<TextureTag> TextureHandle;
251 typedef Handle_t<SamplerTag> SamplerStateHandle;
252
253 typedef Handle_t<EffectTag> EffectHandle;
254 typedef Handle_t<ShaderTag> ShaderHandle;
255 typedef Handle_t<RenderPipelineTag> RenderPipelineHandle;
256 typedef Handle_t<ComputePipelineTag> ComputePipelineHandle;
257
258 typedef Handle_t<RenderTargetTag> RenderTargetHandle;
259 typedef Handle_t<DepthStencilTag> DepthStencilHandle;
260
261 typedef Handle_t<VariableBindingTag> EffectVariableHandle;
262 typedef Handle_t<ConstantBufferBindingTag> ConstantBufferBindingHandle;
263 typedef Handle_t<TextureBindingTag> TextureBindingHandle;
264 typedef Handle_t<SamplerStateBindingTag> SamplerStateBindingHandle;
265 typedef Handle_t<BufferBindingTag> BufferBindingHandle;
266 typedef Handle_t<BindGroupTag> BindGroupHandle;
267 typedef Handle_t<BindGroupConstantBufferBindingTag> BindGroupConstantBufferBindingHandle;
268 typedef Handle_t<BindGroupTextureBindingTag> BindGroupTextureBindingHandle;
269 typedef Handle_t<BindGroupSamplerStateBindingTag> BindGroupSamplerStateBindingHandle;
270
271 typedef Handle_t<TextureViewTag> TextureViewHandle;
272
273 typedef Handle_t<FenceTag> FenceHandle;
274
275 template<typename T>
276 inline bool HandleIsValid(const Handle_t<T> handle)
277 {
278 return (handle != Handle_t<T>::NoHandle && handle != Handle_t<T>::InvalidHandle);
279 }
280
281 template<typename T, typename U>
282 inline bool HandleIsValid(const Handle_t<T, U> handle)
283 {
284 return (handle != Handle_t<T, U>::NoHandle && handle != Handle_t<T, U>::InvalidHandle);
285 }
286
287 inline bool isDebug()
288 {
289#ifdef _DEBUG
290 return true;
291#else
292 return false;
293#endif
294 }
295}
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.
ResourceStatus
Status of an asynchronously loaded resource, such as an effect or a pipeline.
Definition: Common.h:198
@ Pending
The resource is still loading.
@ Error
The resource failed to load.
@ Ready
The resource has loaded successfully and is ready for use.
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