Cogs.Core
ContextVK.h
1#pragma once
2
3#include "../Base/ContextCommon.h"
4
5#include "CommonVK.h"
6
7#include "BuffersVK.h"
8#include "TexturesVK.h"
9#include "EffectsVK.h"
10#include "RenderTargetsVK.h"
11
12#include <array>
13
14#include "ContextStateVK.h"
15
16namespace Cogs
17{
18 class ContextVK : public ContextCommon
19 {
20 public:
21 void initialize(class GraphicsDeviceVK * graphicsDevice);
22 void beginFrame();
23 void endFrame();
24
25 void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override;
26 void setViewport(const float x, const float y, const float width, const float height) override;
27 void setScissor(const int x, const int y, const int width, const int height) override;
28
29 void clearRenderTarget(const float * color) override;
30 void clearRenderTarget(const float ** colors, const int count) override;
31 void clearDepth(const float depth = 1.0f) override;
32
33 void setDepthStencilState(const DepthStencilStateHandle handle) override;
34 void setBlendState(const BlendStateHandle handle, const float* constant = nullptr) override;
35 void setRasterizerState(const RasterizerStateHandle handle) override;
36
37 void setEffect(EffectHandle handle) override;
38
39 void setTexture(const TextureBindingHandle textureBindingHandle, const TextureHandle textureHandle) override;
40 void setTexture(const TextureBindingHandle textureBindingHandle, TextureViewHandle textureViewHandle) override;
41
42 void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override;
43
44 void setInputLayout(const InputLayoutHandle inputLayoutHandle) override;
45 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t count, const uint32_t * strides, const uint32_t * offsets) override;
46 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t count) override;
47 void setIndexBuffer(IndexBufferHandle bufferHandle, uint32_t stride = 4, uint32_t offset = 0) override;
48 void setVertexArrayObject(VertexArrayObjectHandle vertexArrayObject) override;
49
50 void setConstantBuffer(const ConstantBufferBindingHandle bufferBindingHandle, const BufferHandle bufferHandle, const uint32_t offset = 0, const uint32_t size = ~0u) override;
51
52 void setBuffer(const BufferBindingHandle bindingHandle, BufferHandle bufferHandle) override;
53
54 void setBufferCounter(BufferHandle bufferHandle, uint32_t value) override;
55 void setBufferCounter(BufferHandle bufferHandle, BufferHandle sourceBufferHandle) override;
56 void getBufferCounter(BufferHandle bufferHandle, BufferHandle destinationBufferHandle) override;
57 uint32_t getBufferCounter(BufferHandle bufferHandle) override;
58
59 void draw(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes) override;
60 void drawIndexed(PrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex) override;
61 void drawInstanced(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override;
62 void drawInstancedIndexed(PrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override;
63 void dispatchCompute(const unsigned int threadGroupsX, const unsigned int threadGroupsY, const unsigned int threadGroupsZ) override;
64
65 void resolveResource(TextureHandle source, TextureHandle destination) override;
66 void readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override;
67 void readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override;
68
69 void * map(BufferHandle bufferHandle, MapMode::EMapMode mapMode, uint32_t * stride = nullptr) override;
70 void unmap(BufferHandle bufferHandle) override;
71
72 void updateBuffer(BufferHandle bufferHandle, const void* data, size_t size) override;
73 void *map(TextureHandle textureHandle, MapMode::EMapMode accessMode, uint32_t * rowPitch, uint32_t * depthPitch) override;
74 void unmap(TextureHandle textureHandle) override;
75 void updateSubTexture(TextureHandle textureHandle, const size_t level, const void * data) override;
76 void updateSubBuffer(BufferHandle bufferHandle, const size_t offset, const size_t size, const void * data) override;
77
78 void copyResource(BufferHandle destinationHandle, BufferHandle sourceHandle) override;
79 void copyResource(TextureHandle destinationHandle, TextureHandle sourceHandle) override;
80
81 void copyTexture(TextureHandle dstHandle, unsigned dstSub, unsigned dstX, unsigned dstY, unsigned dstZ, TextureHandle sourceHandle, unsigned srcSub) override;
82
83 void clearResource(BufferHandle destinationHandle, uint32_t *Values) override {}
84 void clearResource(BufferHandle destinationHandle, float *Values) override {}
85
86 bool updateDescriptorSet();
87
88 bool setupPipeline(PrimitiveType primitiveType);
89
90 void updatePipelineState(PrimitiveType primitiveType);
91
92 void beginRenderPass();
93 void endRenderPass();
94
95 void updateConstantBuffers();
96
97 InputAssemblerStateCommon * getIAState() override { return &iaState; }
98 EffectHandle getCurrentEffect() override { return currentEffect; }
99 IEffects * getEffects() { return effects; }
100
101 TextureHandle defaultTexture;
102 SamplerStateHandle defaultSampler;
103
104 RasterizerStateHandle defaultRasterizerState;
105 DepthStencilStateHandle defaultDepthStencilState;
106 BlendStateHandle defaultBlendState;
107
108 EffectsVK * effects;
109 BuffersVK * buffers;
110 TexturesVK * textures;
111 RenderTargetsVK * renderTargets;
112
113 EffectHandle currentEffect;
114 VkRenderPass renderPass = VK_NULL_HANDLE;
115
118
120 {
121 VkCommandPool commandPool = VK_NULL_HANDLE;
122 VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
123
124 VkFence fence = VK_NULL_HANDLE;
125 VkSemaphore presentSemaphore = VK_NULL_HANDLE;
126
127 std::vector<PoolBufferVK *> orhpanedBuffers;
128 std::vector<TextureHandle> orphanedTextures;
129
130 VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
131 };
132
133 RenderTargetHandle defaultRenderTarget;
134
135 std::vector<VkClearValue> clearValues;
136
137 std::array<FrameResources, 3> frameResources;
138 FrameResources * currentFrameResources = nullptr;
139
140 VkPipelineCache pipelineCache = VK_NULL_HANDLE;
141 std::unordered_map<size_t, VkPipeline> PSOs;
142
143 VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
144 std::vector<VkDescriptorSet> descriptorSets;
145
146 uint64_t currentFrame = 0;
147
149 {
150 EffectSignature effectSignature;
151
152 Binding<BufferVK *, 16> CBVs[ShaderType::NumShaderTypes];
153 Binding<TextureVK *, 16> SRVs[ShaderType::NumShaderTypes];
154 Binding<SamplerVK *, 16> Samplers[ShaderType::NumShaderTypes];
155 Binding<BufferVK *, 16> UAVs[ShaderType::NumShaderTypes];
156 } shaderState;
157
158 private:
159 GraphicsDeviceVK * graphicsDevice;
160 VkDevice device = VK_NULL_HANDLE;
161 };
162}
void setRasterizerState(const RasterizerStateHandle handle) override
Set the current rasterizer state.
Definition: ContextVK.cpp:251
void clearDepth(const float depth=1.0f) override
Clear the currently set depth/stencil target to the given depth.
Definition: ContextVK.cpp:235
void draw(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes) override
Draws non-indexed, non-instanced primitives.
Definition: ContextVK.cpp:433
void endRenderPass()
End a render pass.
Definition: ContextVK.cpp:886
void drawInstanced(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override
Draws non-indexed, instanced primitives.
Definition: ContextVK.cpp:447
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.
Definition: ContextVK.cpp:197
void setBufferCounter(BufferHandle bufferHandle, uint32_t value) override
Set the associated counter of a buffer.
Definition: ContextVK.cpp:416
void clearRenderTarget(const float *color) override
Clear the currently set render target to the given value (4 component floating point RGBA).
Definition: ContextVK.cpp:218
void setBuffer(const BufferBindingHandle bindingHandle, BufferHandle bufferHandle) override
Sets a buffer to bind to the given binding.
Definition: ContextVK.cpp:352
void setInputLayout(const InputLayoutHandle inputLayoutHandle) override
Sets the current input layout.
Definition: ContextVK.cpp:357
void * map(BufferHandle bufferHandle, MapMode::EMapMode mapMode, uint32_t *stride=nullptr) override
Maps the given buffer so it can be accessed.
Definition: ContextVK.cpp:496
void getBufferCounter(BufferHandle bufferHandle, BufferHandle destinationBufferHandle) override
Get the associated counter of a buffer.
Definition: ContextVK.cpp:424
void updateSubTexture(TextureHandle textureHandle, const size_t level, const void *data) override
Update the data of a level in the given texture.
Definition: ContextVK.cpp:569
void updateBuffer(BufferHandle bufferHandle, const void *data, size_t size) override
Replace contents of buffer with new data.
Definition: ContextVK.cpp:554
void setIndexBuffer(IndexBufferHandle bufferHandle, uint32_t stride=4, uint32_t offset=0) override
Sets the current index buffer.
Definition: ContextVK.cpp:384
void unmap(BufferHandle bufferHandle) override
Unmaps the given buffer, applying any synchronization necessary to reflect changes in the mapped memo...
Definition: ContextVK.cpp:545
void setDepthStencilState(const DepthStencilStateHandle handle) override
Set the current depth stencil state.
Definition: ContextVK.cpp:240
void setBlendState(const BlendStateHandle handle, const float *constant=nullptr) override
Set the current blend state.
Definition: ContextVK.cpp:245
void setConstantBuffer(const ConstantBufferBindingHandle bufferBindingHandle, const BufferHandle bufferHandle, const uint32_t offset=0, const uint32_t size=~0u) override
Sets a constant buffer to the given constant buffer binding.
Definition: ContextVK.cpp:401
void setEffect(EffectHandle handle) override
Set the current effect.
Definition: ContextVK.cpp:256
void drawInstancedIndexed(PrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override
Draws indexed, instanced primitives.
Definition: ContextVK.cpp:454
void readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override
Reads data from the current render target into the given bufferHandle.
Definition: ContextVK.cpp:491
void drawIndexed(PrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex) override
Draws indexed, non-instanced primitives.
Definition: ContextVK.cpp:440
void setScissor(const int x, const int y, const int width, const int height) override
Sets the current scissor rectangle.
Definition: ContextVK.cpp:214
void setTexture(const TextureBindingHandle textureBindingHandle, const TextureHandle textureHandle) override
Sets the texture given to the binding given by textureBindingHandle.
Definition: ContextVK.cpp:311
void resolveResource(TextureHandle source, TextureHandle destination) override
Resolves the given source resource target into the given destination texture.
Definition: ContextVK.cpp:462
void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override
Sets the current render target and an associated depth stencil target.
Definition: ContextVK.cpp:167
void setVertexBuffers(const VertexBufferHandle *vertexBufferHandles, const size_t count, const uint32_t *strides, const uint32_t *offsets) override
Sets the current vertex buffers.
Definition: ContextVK.cpp:362
void readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override
Reads data from the current depth target into the given bufferHandle.
Definition: ContextVK.cpp:486
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.
Definition: ContextVK.cpp:458
void setVertexArrayObject(VertexArrayObjectHandle vertexArrayObject) override
Definition: ContextVK.cpp:396
void updateSubBuffer(BufferHandle bufferHandle, const size_t offset, const size_t size, const void *data) override
Update a region of data in a buffer.
Definition: ContextVK.cpp:575
void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override
Sets the sampler state binding given to the given sampler state.
Definition: ContextVK.cpp:338
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Framebuffer
Framebuffers to select from when doing framebuffer operations.
Definition: Common.h:147
PrimitiveType
Primitive types for interpreting vertex data sent to the graphics pipeline.
Definition: Common.h:112
Provides effects and shader management functionality.
Definition: IEffects.h:148
EMapMode
Mapping mode enumeration.
Definition: Flags.h:93