Plasma Engine  2.0
Loading...
Searching...
No Matches
Device.h
1
2#pragma once
3
4#include <Foundation/Containers/HashTable.h>
5#include <Foundation/Containers/IdTable.h>
6#include <Foundation/Memory/CommonAllocators.h>
7#include <Foundation/Strings/HashedString.h>
8#include <RendererFoundation/Descriptors/Descriptors.h>
9#include <RendererFoundation/Device/DeviceCapabilities.h>
10#include <RendererFoundation/RendererFoundationDLL.h>
11
12class plColor;
13
18class PL_RENDERERFOUNDATION_DLL plGALDevice
19{
20public:
22
23 // Init & shutdown functions
24
25 plResult Init();
26 plResult Shutdown();
27 plStringView GetRenderer();
28
29 // Commands functions
30
32 plGALCommandEncoder* BeginCommands(const char* szName);
35 void EndCommands(plGALCommandEncoder* pCommandEncoder);
36
37 // State creation functions
38
39 plGALBlendStateHandle CreateBlendState(const plGALBlendStateCreationDescription& description);
40 void DestroyBlendState(plGALBlendStateHandle hBlendState);
41
42 plGALDepthStencilStateHandle CreateDepthStencilState(const plGALDepthStencilStateCreationDescription& description);
43 void DestroyDepthStencilState(plGALDepthStencilStateHandle hDepthStencilState);
44
45 plGALRasterizerStateHandle CreateRasterizerState(const plGALRasterizerStateCreationDescription& description);
46 void DestroyRasterizerState(plGALRasterizerStateHandle hRasterizerState);
47
48 plGALSamplerStateHandle CreateSamplerState(const plGALSamplerStateCreationDescription& description);
49 void DestroySamplerState(plGALSamplerStateHandle hSamplerState);
50
51 // Resource creation functions
52
53 plGALShaderHandle CreateShader(const plGALShaderCreationDescription& description);
54 void DestroyShader(plGALShaderHandle hShader);
55
57 void DestroyBuffer(plGALBufferHandle hBuffer);
58
59 // Helper functions for buffers (for common, simple use cases)
60
61 plGALBufferHandle CreateVertexBuffer(plUInt32 uiVertexSize, plUInt32 uiVertexCount, plArrayPtr<const plUInt8> initialData = plArrayPtr<const plUInt8>(), bool bDataIsMutable = false);
62 plGALBufferHandle CreateIndexBuffer(plGALIndexType::Enum indexType, plUInt32 uiIndexCount, plArrayPtr<const plUInt8> initialData = plArrayPtr<const plUInt8>(), bool bDataIsMutable = false);
63 plGALBufferHandle CreateConstantBuffer(plUInt32 uiBufferSize);
64
66 void DestroyTexture(plGALTextureHandle hTexture);
67
68 plGALTextureHandle CreateProxyTexture(plGALTextureHandle hParentTexture, plUInt32 uiSlice);
69 void DestroyProxyTexture(plGALTextureHandle hProxyTexture);
70
71 plGALTextureHandle CreateSharedTexture(const plGALTextureCreationDescription& description, plArrayPtr<plGALSystemMemoryDescription> initialData = {});
72 plGALTextureHandle OpenSharedTexture(const plGALTextureCreationDescription& description, plGALPlatformSharedHandle hSharedHandle);
73 void DestroySharedTexture(plGALTextureHandle hTexture);
74
75 // Resource views
76 plGALTextureResourceViewHandle GetDefaultResourceView(plGALTextureHandle hTexture);
77 plGALBufferResourceViewHandle GetDefaultResourceView(plGALBufferHandle hBuffer);
78
80 void DestroyResourceView(plGALTextureResourceViewHandle hResourceView);
81
83 void DestroyResourceView(plGALBufferResourceViewHandle hResourceView);
84
85 // Render target views
86 plGALRenderTargetViewHandle GetDefaultRenderTargetView(plGALTextureHandle hTexture);
87
88 plGALRenderTargetViewHandle CreateRenderTargetView(const plGALRenderTargetViewCreationDescription& description);
89 void DestroyRenderTargetView(plGALRenderTargetViewHandle hRenderTargetView);
90
91 // Unordered access views
93 void DestroyUnorderedAccessView(plGALTextureUnorderedAccessViewHandle hUnorderedAccessView);
94
96 void DestroyUnorderedAccessView(plGALBufferUnorderedAccessViewHandle hUnorderedAccessView);
97
98 // Other rendering creation functions
99
101 plGALSwapChainHandle CreateSwapChain(const SwapChainFactoryFunction& func);
102 plResult UpdateSwapChain(plGALSwapChainHandle hSwapChain, plEnum<plGALPresentMode> newPresentMode);
103 void DestroySwapChain(plGALSwapChainHandle hSwapChain);
104
105 plGALVertexDeclarationHandle CreateVertexDeclaration(const plGALVertexDeclarationCreationDescription& description);
106 void DestroyVertexDeclaration(plGALVertexDeclarationHandle hVertexDeclaration);
107
108 // GPU -> CPU query functions
109
116 plEnum<plGALAsyncResult> GetTimestampResult(plGALTimestampHandle hTimestamp, plTime& out_result);
117
124 plEnum<plGALAsyncResult> GetOcclusionQueryResult(plGALOcclusionHandle hOcclusion, plUInt64& out_uiResult);
125
132 plEnum<plGALAsyncResult> GetFenceResult(plGALFenceHandle hFence, plTime timeout = plTime::MakeZero());
133
135
136 // Swap chain functions
137
138 plGALTextureHandle GetBackBufferTextureFromSwapChain(plGALSwapChainHandle hSwapChain);
139
140
141 // Misc functions
142
146 void EnqueueFrameSwapChain(plGALSwapChainHandle hSwapChain);
147
150 void BeginFrame(const plUInt64 uiAppFrame = 0);
151
153 void EndFrame();
154
158 plUInt64 GetCurrentFrame() const;
162 plUInt64 GetSafeFrame() const;
163
164
165 const plGALDeviceCreationDescription* GetDescription() const;
166
167 const plGALSwapChain* GetSwapChain(plGALSwapChainHandle hSwapChain) const;
168 template <typename T>
169 const T* GetSwapChain(plGALSwapChainHandle hSwapChain) const
170 {
171 return static_cast<const T*>(GetSwapChainInternal(hSwapChain, plGetStaticRTTI<T>()));
172 }
173
174 const plGALShader* GetShader(plGALShaderHandle hShader) const;
175 const plGALTexture* GetTexture(plGALTextureHandle hTexture) const;
176 virtual const plGALSharedTexture* GetSharedTexture(plGALTextureHandle hTexture) const = 0;
177 const plGALBuffer* GetBuffer(plGALBufferHandle hBuffer) const;
178 const plGALDepthStencilState* GetDepthStencilState(plGALDepthStencilStateHandle hDepthStencilState) const;
179 const plGALBlendState* GetBlendState(plGALBlendStateHandle hBlendState) const;
180 const plGALRasterizerState* GetRasterizerState(plGALRasterizerStateHandle hRasterizerState) const;
181 const plGALVertexDeclaration* GetVertexDeclaration(plGALVertexDeclarationHandle hVertexDeclaration) const;
182 const plGALSamplerState* GetSamplerState(plGALSamplerStateHandle hSamplerState) const;
183 const plGALTextureResourceView* GetResourceView(plGALTextureResourceViewHandle hResourceView) const;
184 const plGALBufferResourceView* GetResourceView(plGALBufferResourceViewHandle hResourceView) const;
185 const plGALRenderTargetView* GetRenderTargetView(plGALRenderTargetViewHandle hRenderTargetView) const;
186 const plGALTextureUnorderedAccessView* GetUnorderedAccessView(plGALTextureUnorderedAccessViewHandle hUnorderedAccessView) const;
187 const plGALBufferUnorderedAccessView* GetUnorderedAccessView(plGALBufferUnorderedAccessViewHandle hUnorderedAccessView) const;
188
189 const plGALDeviceCapabilities& GetCapabilities() const;
190
191 virtual plUInt64 GetMemoryConsumptionForTexture(const plGALTextureCreationDescription& description) const;
192 virtual plUInt64 GetMemoryConsumptionForBuffer(const plGALBufferCreationDescription& description) const;
193
194 static void SetDefaultDevice(plGALDevice* pDefaultDevice);
195 static plGALDevice* GetDefaultDevice();
196 static bool HasDefaultDevice();
197
198 // \brief Sends the queued up commands to the GPU.
199 // Same as plCommandEncoder:Flush.
200 void Flush();
201
203 void WaitIdle();
204
205 // public in case someone external needs to lock multiple operations
206 mutable plMutex m_Mutex;
207
209 plAllocator* GetAllocator();
210
211private:
212 static plGALDevice* s_pDefaultDevice;
213
214protected:
216
217 virtual ~plGALDevice();
218
219 template <typename IdTableType, typename ReturnType>
220 ReturnType* Get(typename IdTableType::TypeOfId hHandle, const IdTableType& IdTable) const;
221
222 void DestroyViews(plGALTexture* pResource);
223 void DestroyViews(plGALBuffer* pResource);
224
225 template <typename HandleType>
226 void AddDeadObject(plUInt32 uiType, HandleType handle);
227
228 template <typename HandleType>
229 void ReviveDeadObject(plUInt32 uiType, HandleType handle);
230
231 void DestroyDeadObjects();
232
234 void VerifyMultithreadedAccess() const;
235
236 const plGALSwapChain* GetSwapChainInternal(plGALSwapChainHandle hSwapChain, const plRTTI* pRequestedType) const;
237
238 plGALTextureHandle FinalizeTextureInternal(const plGALTextureCreationDescription& desc, plGALTexture* pTexture);
239 plGALBufferHandle FinalizeBufferInternal(const plGALBufferCreationDescription& desc, plGALBuffer* pBuffer);
240
241 plProxyAllocator m_Allocator;
242 plLocalAllocatorWrapper m_AllocatorWrapper;
243
258
259 ShaderTable m_Shaders;
260 BlendStateTable m_BlendStates;
261 DepthStencilStateTable m_DepthStencilStates;
262 RasterizerStateTable m_RasterizerStates;
263 BufferTable m_Buffers;
264 TextureTable m_Textures;
265 TextureResourceViewTable m_TextureResourceViews;
266 BufferResourceViewTable m_BufferResourceViews;
267 SamplerStateTable m_SamplerStates;
268 RenderTargetViewTable m_RenderTargetViews;
269 TextureUnorderedAccessViewTable m_TextureUnorderedAccessViews;
270 BufferUnorderedAccessViewTable m_BufferUnorderedAccessViews;
271 SwapChainTable m_SwapChains;
272 VertexDeclarationTable m_VertexDeclarations;
273
274
275 // Hash tables used to prevent state object duplication
281
283 {
284 PL_DECLARE_POD_TYPE();
285
286 plUInt32 m_uiType;
287 plUInt32 m_uiHandle;
288 };
289
291
292 plGALDeviceCreationDescription m_Description;
293
294 plGALDeviceCapabilities m_Capabilities;
295
296 // Deactivate Doxygen document generation for the following block. (API abstraction only)
298
299 // These functions need to be implemented by a render API abstraction
300protected:
301 friend class plMemoryUtils;
302
303 // Init & shutdown functions
304
305 virtual plResult InitPlatform() = 0;
306 virtual plResult ShutdownPlatform() = 0;
307 virtual plStringView GetRendererPlatform() = 0;
308
309 // Pipeline & Pass functions
310
311
312
313 // Command Encoder
314
315 virtual plGALCommandEncoder* BeginCommandsPlatform(const char* szName) = 0;
316 virtual void EndCommandsPlatform(plGALCommandEncoder* pPass) = 0;
317
318 // State creation functions
319
320 virtual plGALBlendState* CreateBlendStatePlatform(const plGALBlendStateCreationDescription& Description) = 0;
321 virtual void DestroyBlendStatePlatform(plGALBlendState* pBlendState) = 0;
322
323 virtual plGALDepthStencilState* CreateDepthStencilStatePlatform(const plGALDepthStencilStateCreationDescription& Description) = 0;
324 virtual void DestroyDepthStencilStatePlatform(plGALDepthStencilState* pDepthStencilState) = 0;
325
326 virtual plGALRasterizerState* CreateRasterizerStatePlatform(const plGALRasterizerStateCreationDescription& Description) = 0;
327 virtual void DestroyRasterizerStatePlatform(plGALRasterizerState* pRasterizerState) = 0;
328
329 virtual plGALSamplerState* CreateSamplerStatePlatform(const plGALSamplerStateCreationDescription& Description) = 0;
330 virtual void DestroySamplerStatePlatform(plGALSamplerState* pSamplerState) = 0;
331
332 // Resource creation functions
333
334 virtual plGALShader* CreateShaderPlatform(const plGALShaderCreationDescription& Description) = 0;
335 virtual void DestroyShaderPlatform(plGALShader* pShader) = 0;
336
337 virtual plGALBuffer* CreateBufferPlatform(const plGALBufferCreationDescription& Description, plArrayPtr<const plUInt8> pInitialData) = 0;
338 virtual void DestroyBufferPlatform(plGALBuffer* pBuffer) = 0;
339
340 virtual plGALTexture* CreateTexturePlatform(const plGALTextureCreationDescription& Description, plArrayPtr<plGALSystemMemoryDescription> pInitialData) = 0;
341 virtual void DestroyTexturePlatform(plGALTexture* pTexture) = 0;
342
343 virtual plGALTexture* CreateSharedTexturePlatform(const plGALTextureCreationDescription& Description, plArrayPtr<plGALSystemMemoryDescription> pInitialData, plEnum<plGALSharedTextureType> sharedType, plGALPlatformSharedHandle handle) = 0;
344 virtual void DestroySharedTexturePlatform(plGALTexture* pTexture) = 0;
345
346 virtual plGALTextureResourceView* CreateResourceViewPlatform(plGALTexture* pResource, const plGALTextureResourceViewCreationDescription& Description) = 0;
347 virtual void DestroyResourceViewPlatform(plGALTextureResourceView* pResourceView) = 0;
348
349 virtual plGALBufferResourceView* CreateResourceViewPlatform(plGALBuffer* pResource, const plGALBufferResourceViewCreationDescription& Description) = 0;
350 virtual void DestroyResourceViewPlatform(plGALBufferResourceView* pResourceView) = 0;
351
352 virtual plGALRenderTargetView* CreateRenderTargetViewPlatform(plGALTexture* pTexture, const plGALRenderTargetViewCreationDescription& Description) = 0;
353 virtual void DestroyRenderTargetViewPlatform(plGALRenderTargetView* pRenderTargetView) = 0;
354
355 virtual plGALTextureUnorderedAccessView* CreateUnorderedAccessViewPlatform(plGALTexture* pResource, const plGALTextureUnorderedAccessViewCreationDescription& Description) = 0;
356 virtual void DestroyUnorderedAccessViewPlatform(plGALTextureUnorderedAccessView* pUnorderedAccessView) = 0;
357
358 virtual plGALBufferUnorderedAccessView* CreateUnorderedAccessViewPlatform(plGALBuffer* pResource, const plGALBufferUnorderedAccessViewCreationDescription& Description) = 0;
359 virtual void DestroyUnorderedAccessViewPlatform(plGALBufferUnorderedAccessView* pUnorderedAccessView) = 0;
360
361 // Other rendering creation functions
362
363 virtual plGALVertexDeclaration* CreateVertexDeclarationPlatform(const plGALVertexDeclarationCreationDescription& Description) = 0;
364 virtual void DestroyVertexDeclarationPlatform(plGALVertexDeclaration* pVertexDeclaration) = 0;
365
366 // GPU -> CPU query functions
367
368 virtual plEnum<plGALAsyncResult> GetTimestampResultPlatform(plGALTimestampHandle hTimestamp, plTime& out_result) = 0;
369 virtual plEnum<plGALAsyncResult> GetOcclusionResultPlatform(plGALOcclusionHandle hOcclusion, plUInt64& out_uiResult) = 0;
370 virtual plEnum<plGALAsyncResult> GetFenceResultPlatform(plGALFenceHandle hFence, plTime timeout) = 0;
371
372 // Misc functions
373
374 virtual void BeginFramePlatform(plArrayPtr<plGALSwapChain*> swapchains, const plUInt64 uiAppFrame) = 0;
375 virtual void EndFramePlatform(plArrayPtr<plGALSwapChain*> swapchains) = 0;
376
377 virtual plUInt64 GetCurrentFramePlatform() const = 0;
378 virtual plUInt64 GetSafeFramePlatform() const = 0;
379
380 virtual void FillCapabilitiesPlatform() = 0;
381
382 virtual void FlushPlatform() = 0;
383 virtual void WaitIdlePlatform() = 0;
384
385
387
388private:
389 bool m_bBeginFrameCalled = false;
390 plHybridArray<plGALSwapChain*, 8> m_FrameSwapChains;
391 bool m_bBeginPipelineCalled = false;
392 bool m_bBeginCommandsCalled = false;
393};
394
395#include <RendererFoundation/Device/Implementation/Device_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
Definition DynamicArray.h:81
Definition Event.h:177
Definition RendererFoundationDLL.h:467
Definition State.h:7
Definition RendererFoundationDLL.h:418
Definition Buffer.h:8
Definition RendererFoundationDLL.h:432
Definition ResourceView.h:30
Definition RendererFoundationDLL.h:446
Definition UnorderedAccesView.h:28
Definition CommandEncoder.h:11
Definition RendererFoundationDLL.h:460
Definition State.h:20
The plRenderDevice class is the primary interface for interactions with rendering APIs It contains a ...
Definition Device.h:19
Definition RendererFoundationDLL.h:474
Definition State.h:33
Definition RendererFoundationDLL.h:453
Definition RenderTargetView.h:8
Definition RendererFoundationDLL.h:481
Definition State.h:46
Definition RendererFoundationDLL.h:404
Definition Shader.h:7
Optional interface for plGALTexture if it was created via plGALDevice::CreateSharedTexture....
Definition Texture.h:33
Definition RendererFoundationDLL.h:397
Definition SwapChain.h:11
Definition RendererFoundationDLL.h:411
Definition Texture.h:8
Definition RendererFoundationDLL.h:425
Definition ResourceView.h:11
Definition RendererFoundationDLL.h:439
Definition UnorderedAccesView.h:11
Definition RendererFoundationDLL.h:488
Definition VertexDeclaration.h:8
Definition HashTable.h:333
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
This class provides functions to work on raw memory.
Definition MemoryUtils.h:26
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Definition Descriptors.h:70
Definition Descriptors.h:169
Definition Descriptors.h:211
Definition Device.h:283
This struct holds information about the rendering device capabilities (e.g. what shader stages are su...
Definition DeviceCapabilities.h:42
Definition Descriptors.h:39
Definition Descriptors.h:277
Describes the settings for a new rasterizer state. See plGALDevice::CreateRasterizerState.
Definition Descriptors.h:105
Definition Descriptors.h:220
Definition Descriptors.h:118
Definition Descriptors.h:44
Definition Descriptors.h:177
Definition Descriptors.h:153
A generic id class that holds an id combined of an instance index and a generation counter.
Definition Id.h:52
Definition AllocatorWrapper.h:30
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
PL_ALWAYS_INLINE static constexpr plTime MakeZero()
Creates an instance of plTime that was initialized with zero.
Definition Time.h:42