Cogs.Core
ContextD3D12.h
1#pragma once
2
3#include "../Base/ContextCommon.h"
4
5#include "BuffersD3D12.h"
6#include "TexturesD3D12.h"
7#include "EffectsD3D12.h"
8#include "RenderTargetsD3D12.h"
9
10#include <unordered_map>
11#include <array>
12#include <algorithm>
13
14#include "FrameResourcesD3D12.h"
15#include "ContextStateD3D12.h"
16
17namespace Cogs
18{
20 {
23
24 void initialize(struct GraphicsDeviceD3D12 * graphicsDevice);
25
26 void beginFrame();
27
28 void updateFrameResources();
29
30 void endFrame();
31
32 void executeCommandList();
33 void flush();
34
35 IEffects * getEffects() override;
36
37 void setEffect(EffectHandle handle) override;
38
39 void signal(FenceHandle fenceHandle) override;
40
41 void beginRenderPass(const RenderPassInfo &info) override {}
42 void endRenderPass() override {}
43
44 void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override;
45 void setViewport(const float x, const float y, const float width, const float height) override;
46 void setScissor(const int x, const int y, const int width, const int height) override;
47
48 void clearRenderTarget(const float * color) override;
49 void clearRenderTarget(const float ** colors, const int count) override;
50 void clearDepth(const float depth = 1.0f) override;
51
52 void setDepthStencilState(const DepthStencilStateHandle handle) override;
53 void setRasterizerState(const RasterizerStateHandle handle) override;
54 void setBlendState(const BlendStateHandle handle, const float* constant) override;
55
56 void resolveResource(TextureHandle source, TextureHandle destination) override;
57
58 void setInputLayout(const InputLayoutHandle inputLayoutHandle) override;
59
60 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t count) override;
61 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t count, const uint32_t * strides, const uint32_t * offsets) override;
62 void setIndexBuffer(IndexBufferHandle indexBufferHandle, uint32_t stride, uint32_t offset) override;
63 void setConstantBuffer(const ConstantBufferBindingHandle bufferBinding, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) override;
64
65 void setupConstantBuffers();
66 void setupDrawBuffers();
67
68 void draw(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes) override;
69
70 void drawIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex) override;
71
72 void drawInstanced(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override;
73 void drawInstancedIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override;
74 void dispatchCompute(const unsigned int threadGroupsX, const unsigned int threadGroupsY, const unsigned int threadGroupsZ) override;
75
76 void readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override;
77 void readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override;
78
79 void * map(BufferHandle bufferHandle, MapMode::EMapMode accessMode, uint32_t * stride = 0) override;
80 void unmap(BufferHandle bufferHandle) override;
81
82 void * map(TextureHandle textureHandle, MapMode::EMapMode accessMode, uint32_t * rowPitch, uint32_t * depthPitch) override { return nullptr; }
83 void unmap(TextureHandle textureHandle) {}
84
85 void updateSubTexture(TextureHandle textureHandle, const size_t level, const void * data) override;
86 void updateSubBuffer(BufferHandle bufferHandle, const size_t offset, const size_t size, const void * data) override;
87
88 void copyResource(BufferHandle destinationHandle, BufferHandle sourceHandle) override;
89 void copyResource(TextureHandle destinationHandle, TextureHandle sourceHandle) override;
90
91 void copyTexture(TextureHandle dstHandle, unsigned dstSub, unsigned dstX, unsigned dstY, unsigned dstZ, TextureHandle sourceHandle, unsigned srcSub) override {}
92
93 void clearResource(BufferHandle destinationHandle, uint32_t *Values) override {}
94 void clearResource(BufferHandle destinationHandle, float *Values) override {}
95
96 void setTexture(const TextureBindingHandle textureBindingHandle, const TextureHandle textureHandle) override;
97 void setTexture(const TextureBindingHandle textureBindingHandle, TextureViewHandle textureViewHandle) override;
98
99 void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override;
100
101 void setBuffer(const BufferBindingHandle bufferBindingHandle, BufferHandle bufferHandle) override;
102
103 void setBufferCounter(BufferHandle bufferHandle, uint32_t value);
104 void setBufferCounter(BufferHandle bufferHandle, BufferHandle sourceBufferHandle);
105 uint32_t getBufferCounter(BufferHandle bufferHandle);
106 void getBufferCounter(BufferHandle bufferHandle, BufferHandle destinationBufferHandle);
107
108 struct GraphicsDeviceD3D12 * graphicsDevice;
109
110 void setupPipelineState(PrimitiveType::EPrimitiveType topology);
111 void checkState(PrimitiveType::EPrimitiveType topology);
112
113 void destroy();
114
115 InputAssemblerStateCommon * getIAState() override { return &iaState; }
116 EffectHandle getCurrentEffect() override { return currentEffect; }
117
118 void checkTextureState(TextureD3D12 & texture);
119
120 ResourcePointer<ID3D12DescriptorHeap> descriptorHeapCPU;
121 ResourcePointer<ID3D12DescriptorHeap> descriptorHeap;
122 CD3DX12_CPU_DESCRIPTOR_HANDLE cbvCurrentCpuHandle;
123 CD3DX12_GPU_DESCRIPTOR_HANDLE cbvCurrentGpuHandle;
124
125 CD3DX12_CPU_DESCRIPTOR_HANDLE cbvCurrentCpuHandleCPU;
126
127 CD3DX12_CPU_DESCRIPTOR_HANDLE srvCurrentCpuHandle;
128 CD3DX12_GPU_DESCRIPTOR_HANDLE srvCurrentGpuHandle;
129
130 CD3DX12_CPU_DESCRIPTOR_HANDLE uavCurrentCpuHandle;
131 CD3DX12_GPU_DESCRIPTOR_HANDLE uavCurrentGpuHandle;
132 UINT descriptorSize;
133
134 ResourcePointer<ID3D12DescriptorHeap> samplerDescriptorHeap;
135 CD3DX12_CPU_DESCRIPTOR_HANDLE samplerCurrentCpuHandle;
136 CD3DX12_GPU_DESCRIPTOR_HANDLE samplerCurrentGpuHandle;
137 UINT samplerHandleIncrement;
138
139 ResourcePointer<ID3D12Fence> fence;
140 HANDLE eventHandle;
141
142 std::array<FrameResources, 3> frameResources;
143 size_t frameResourceIndex = 0;
144 FrameResources * currentFrameResources = nullptr;
145
146 uint64_t currentFenceValue = 0;
147
148 ID3D12CommandAllocator * commandAllocator;
149 ID3D12GraphicsCommandList * commandList;
150
151 std::unordered_map<size_t, ResourcePointer<ID3D12PipelineState>> PSOs;
152
153 bool constantBuffersUpdated;
154
155 Direct3D12::InputAssemblerState iaState;
156 Direct3D12::PipelineState state;
157 Direct3D12::ShaderState shaderState;
158
159 EffectHandle currentEffect;
160
161 ResourcePointer<ID3D12Device> device;
162
163 BuffersD3D12 * buffers;
164 TexturesD3D12 * textures;
165 EffectsD3D12 * effects;
166 RenderTargetsD3D12 * renderTargets;
167
168 DepthStencilStateHandle defaultDepthStencilState;
169 RasterizerStateHandle defaultRasterizerState;
170 BlendStateHandle defaultBlendState;
171 SamplerStateHandle defaultSamplerState;
172
173 RenderTargetHandle defaultRenderTarget;
174 DepthStencilHandle defaultDepthStencil;
175 };
176}
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
void setRasterizerState(const RasterizerStateHandle handle) override
Set the current rasterizer state.
void dispatchCompute(const unsigned int threadGroupsX, const unsigned int threadGroupsY, const unsigned int threadGroupsZ) override
Dispatch computing work on the graphics device using the desired thread group count.
void updateSubBuffer(BufferHandle bufferHandle, const size_t offset, const size_t size, const void *data) override
Update a region of data in a buffer.
void clearDepth(const float depth=1.0f) override
Clear the currently set depth/stencil target to the given depth.
void clearRenderTarget(const float *color) override
Clear the currently set render target to the given value (4 component floating point RGBA).
void setBufferCounter(BufferHandle bufferHandle, uint32_t value)
Set the associated counter of a buffer.
void endRenderPass() override
End a render pass.
Definition: ContextD3D12.h:42
void draw(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes) override
Draws non-indexed, non-instanced primitives.
void resolveResource(TextureHandle source, TextureHandle destination) override
Resolves the given source resource target into the given destination texture.
void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override
Sets the sampler state binding given to the given sampler state.
void setDepthStencilState(const DepthStencilStateHandle handle) override
Set the current depth stencil state.
void drawInstanced(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override
Draws non-indexed, instanced primitives.
void readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override
Reads data from the current render target into the given bufferHandle.
void setEffect(EffectHandle handle) override
Set the current effect.
void drawInstancedIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override
Draws indexed, instanced primitives.
void setBlendState(const BlendStateHandle handle, const float *constant) override
Set the current blend state.
void setConstantBuffer(const ConstantBufferBindingHandle bufferBinding, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) override
Sets a constant buffer to the given constant buffer binding.
void setTexture(const TextureBindingHandle textureBindingHandle, const TextureHandle textureHandle) override
Sets the texture given to the binding given by textureBindingHandle.
uint32_t getBufferCounter(BufferHandle bufferHandle)
Get the associated counter of a buffer.
void signal(FenceHandle fenceHandle) override
Insert a fence in the command stream that will signal when all commands before the fence are complete...
void drawIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex) override
Draws indexed, non-instanced primitives.
void * map(TextureHandle textureHandle, MapMode::EMapMode accessMode, uint32_t *rowPitch, uint32_t *depthPitch) override
Create host mapping of a staging texture.
Definition: ContextD3D12.h:82
void setVertexBuffers(const VertexBufferHandle *vertexBufferHandles, const size_t count) override
Overload provided to support transitioning.
void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override
Sets the current render target and an associated depth stencil target.
void unmap(BufferHandle bufferHandle) override
Unmaps the given buffer, applying any synchronization necessary to reflect changes in the mapped memo...
void setIndexBuffer(IndexBufferHandle indexBufferHandle, uint32_t stride, uint32_t offset) override
Sets the current index buffer.
void unmap(TextureHandle textureHandle)
Release a host mapping of a staging texture.
Definition: ContextD3D12.h:83
void beginRenderPass(const RenderPassInfo &info) override
Begin a render pass.
Definition: ContextD3D12.h:41
void setScissor(const int x, const int y, const int width, const int height) override
Sets the current scissor rectangle.
void setViewport(const float x, const float y, const float width, const float height) override
Sets the current viewport to the given location and dimensions.
void readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override
Reads data from the current depth target into the given bufferHandle.
void setInputLayout(const InputLayoutHandle inputLayoutHandle) override
Sets the current input layout.
void setBuffer(const BufferBindingHandle bufferBindingHandle, BufferHandle bufferHandle) override
Sets a buffer to bind to the given binding.
void updateSubTexture(TextureHandle textureHandle, const size_t level, const void *data) override
Update the data of a level in the given texture.
Provides effects and shader management functionality.
Definition: IEffects.h:148
EMapMode
Mapping mode enumeration.
Definition: Flags.h:93
EPrimitiveType
Primitive type enumeration.
Definition: Common.h:114