Cogs.Core
Factory.cpp
1#include "Factory.h"
2
3#ifdef COGS_DIRECT3D12
4#include "Direct3D12/GraphicsDeviceD3D12.h"
5#endif
6
7#ifdef COGS_DIRECT3D11
8#include "Direct3D11/GraphicsDeviceD3D11.h"
9#endif
10
11#ifdef COGS_OPENGL20
12#include "OpenGL20/GraphicsDeviceGL20.h"
13#endif
14
15#ifdef COGS_OPENGLES30
16#include "OpenGLES30/GraphicsDeviceGLES30.h"
17#endif
18
19#ifdef COGS_VULKAN
20#include "Vulkan/GraphicsDeviceVK.h"
21#endif
22
23#ifdef COGS_WEBGPU
24#include "WebGPU/GraphicsDeviceWebGPU.h"
25#endif
26
27#ifdef COGS_NULL
28#include "Null/GraphicsDeviceNull.h"
29#endif
30
31#include "Foundation/Collections/SmallVector.h"
32
33namespace
34{
35 using namespace Cogs;
36
37 struct DeviceInfo
38 {
39 IGraphicsDevice * device;
40 RenderingAllocatorInfo allocatorInfo;
41 };
42
44}
45
47{
48#ifdef COGS_DIRECT3D11
49 return createDevice(GraphicsDeviceType::Direct3D11, allocatorInfo);
50#elif COGS_OPENGL20
51 return createDevice(GraphicsDeviceType::OpenGL20, allocatorInfo);
52#elif COGS_OPENGLES30
53 return createDevice(GraphicsDeviceType::OpenGLES30, allocatorInfo);
54#elif COGS_DIRECT3D12
55 return createDevice(GraphicsDeviceType::Direct3D12, allocatorInfo);
56#elif COGS_VULKAN
57 return createDevice(GraphicsDeviceType::Vulkan, allocatorInfo);
58#elif COGS_WEBGPU
59 return createDevice(GraphicsDeviceType::WebGPU, allocatorInfo);
60#elif COGS_NULL
61 return createDevice(GraphicsDeviceType::Null, allocatorInfo);
62#else
63 return nullptr;
64#endif
65}
66
68{
69 if (name == "Direct3D12" || name == "DirectX12") {
70 return createDevice(GraphicsDeviceType::Direct3D12);
71 } if (name == "Direct3D11" || name == "DirectX11") {
72 return createDevice(GraphicsDeviceType::Direct3D11);
73 } else if (name == "OpenGL20" || name == "OpenGL") {
74 return createDevice(GraphicsDeviceType::OpenGL20);
75 } else if (name == "OpenGLES30") {
76 return createDevice(GraphicsDeviceType::OpenGLES30);
77 } else if (name == "Vulkan") {
78 return createDevice(GraphicsDeviceType::Vulkan);
79 } else if (name == "Dawn" || name == "WebGPU") {
80 return createDevice(GraphicsDeviceType::WebGPU);
81 } else if (name == "Null") {
82 return createDevice(GraphicsDeviceType::Null);
83 }
84
85 return nullptr;
86}
87
88namespace
89{
90 using namespace Cogs;
91
92 template<typename DeviceType>
93 DeviceType * create(RenderingAllocatorInfo * allocatorInfo)
94 {
95 auto device = Memory::create<DeviceType>(allocatorInfo->baseAllocator, allocatorInfo);
96
97 devices.push_back({ device, *allocatorInfo });
98
99 return device;
100 }
101}
102
104{
105 RenderingAllocatorInfo defaultAllocatorInfo = {
108 };
109
110 auto allocatorInfo = externalAllocatorInfo ? externalAllocatorInfo : &defaultAllocatorInfo;
111
112 switch (type) {
114 default:
115 return createDevice(allocatorInfo);
116#ifdef COGS_DIRECT3D12
118 return create<GraphicsDeviceD3D12>(allocatorInfo);
119#endif
120#ifdef COGS_DIRECT3D11
122 return create<GraphicsDeviceD3D11>(allocatorInfo);
123#endif
124#ifdef COGS_OPENGL20
126 return create<GraphicsDeviceGL20>(allocatorInfo);
127#endif
128#ifdef COGS_OPENGLES30
130 return create<GraphicsDeviceGLES30>(allocatorInfo);
131#endif
132#ifdef COGS_VULKAN
134 return create<GraphicsDeviceVK>(allocatorInfo);
135#endif
136#ifdef COGS_WEBGPU
138 return create<GraphicsDeviceWebGPU>(allocatorInfo);
139#endif
140#ifdef COGS_NULL
142 return create<GraphicsDeviceNull>(allocatorInfo);
143#endif
144 }
145}
146
148{
149 size_t index = 0;
150 for (size_t i = 0; i < devices.size(); ++i) {
151 if (devices[i].device == device) {
152 index = i;
153 break;
154 }
155 }
156
157 size_t deviceSize = 0;
158 switch (device->getType())
159 {
160#ifdef COGS_DIRECT3D12
162 deviceSize = sizeof(GraphicsDeviceD3D12);
163 break;
164#endif
165#ifdef COGS_DIRECT3D11
167 deviceSize = sizeof(GraphicsDeviceD3D11);
168 break;
169#endif
170#ifdef COGS_OPENGL20
172 deviceSize = sizeof(GraphicsDeviceGL20);
173 break;
174#endif
175#ifdef COGS_OPENGLES30
177 deviceSize = sizeof(GraphicsDeviceGLES30);
178 break;
179#endif
180#ifdef COGS_VULKAN
182 deviceSize = sizeof(GraphicsDeviceVK);
183 break;
184#endif
185#ifdef COGS_WEBGPU
187 deviceSize = sizeof(GraphicsDeviceWebGPU);
188 break;
189#endif
190#ifdef COGS_NULL
192 deviceSize = sizeof(GraphicsDeviceNull);
193 break;
194#endif
195 default:
196 deviceSize = 0;
197 break;
198 }
199
200 device->~IGraphicsDevice();
201 devices[index].allocatorInfo.baseAllocator->deallocate(device, deviceSize);
202 devices.erase(devices.begin() + index);
203}
Represents a graphics device used to manage graphics resources and issue drawing commands.
virtual ~IGraphicsDevice()=default
Destructor.
virtual GraphicsDeviceType getType() const
Get the type of the graphics device.
static Allocator * defaultAllocator()
Gets the system default allocator.
Definition: Allocator.cpp:17
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
GraphicsDeviceType
Contains types of graphics devices that may be supported.
Definition: Base.h:48
@ Direct3D12
Graphics device using the Direct3D 12 API.
@ OpenGLES30
Graphics device using the OpenGLES 3.0 API.
@ Vulkan
Graphics device using the Vulkan API.
@ Direct3D11
Graphics device using the Direct3D 11 API.
@ Unknown
Unknown type of graphics device.
@ OpenGL20
Graphics device using OpenGL, supporting at least OpenGL 2.0.
@ Null
Null device that implements no rendering functionality.
@ WebGPU
Graphics device using the WebGPU API Backend.
static void releaseDevice(class IGraphicsDevice *device)
Release the given graphics device, freeing all associated resources.
Definition: Factory.cpp:147
static class IGraphicsDevice * createDevice(RenderingAllocatorInfo *allocatorInfo=nullptr)
Creates a graphics device.
Definition: Factory.cpp:46
Allocation information.
Definition: Base.h:35
Memory::Allocator * baseAllocator
Base allocator. Used for misc. internal memory allocations.
Definition: Base.h:37