Cogs.Core
BuffersVK.h
1#pragma once
2
3#include "../Base/BuffersCommon.h"
4
5#include "CommonVK.h"
6
7#include "../Base/ResourcePolicies.h"
8
9#include "Foundation/Collections/ConfigurablePagedPool.h"
10
11namespace Cogs
12{
14 {
15 PoolBufferVK * next;
16 uint8_t * mappedRegion;
17 VkBuffer buffer;
18 size_t offset;
19 size_t size;
20 struct BufferPoolVK * pool;
21 };
22
24 {
25 class GraphicsDeviceVK * graphicsDevice;
26 VkDevice device;
27 uint32_t elementSize;
28 struct BufferPoolVK * pool;
29 };
30
32 {
33 BufferPageVK(BufferContextVK & context, size_t pageSize);
34
35 PoolBufferVK * getHead() { return reinterpret_cast<PoolBufferVK *>(storage.data()); }
36
37 VkBuffer buffer;
38 VkDeviceMemory deviceMemory;
39
40 private:
41 std::vector<uint8_t> storage;
42 uint8_t * mappedRegion;
43 };
44
45 struct BufferPoolVK : public Collections::ConfigurablePagedPool<BufferContextVK, BufferPageVK, PoolBufferVK>
46 {
47 void initialize(GraphicsDeviceVK * graphicsDevice, VkDevice device, size_t capacity, uint32_t elementSize, uint32_t pageSize = 128)
48 {
49 BufferContextVK context;
50 context.graphicsDevice = graphicsDevice;
51 context.device = device;
52 context.elementSize = elementSize;
53 context.pool = this;
54
55 ConfigurablePagedPool::initialize(context, capacity, pageSize);
56 }
57 };
58
59 struct BufferVK
60 {
61 PoolBufferVK * poolBuffer = nullptr;
62
63 VkBuffer buffer;
64 VkDeviceMemory deviceMemory;
65
66 VkBufferView bufferView;
67 VkDescriptorBufferInfo bufferInfo;
68
69 size_t size;
70
71 BindFlags::EBindFlags bindFlags;
72
73 union
74 {
75 VertexFormat * vertexFormat;
76 size_t indexSize;
77 };
78 };
79
81 {
82 VkPipelineVertexInputStateCreateInfo vertexInputState;
83 std::vector<VkVertexInputBindingDescription> vertexInputBinding;
84 std::vector<VkVertexInputAttributeDescription> vertexInputAttribs;
85 };
86
87 class BuffersVK : public BuffersCommon
88 {
89 public:
90 void initialize(class GraphicsDeviceVK * graphicsDevice);
91
92 class GraphicsDeviceVK * graphicsDevice;
93
94 VertexBufferHandle loadVertexBuffer(const void * vertexData, const size_t count, const VertexFormat & vertexFormat) override;
95 VertexBufferHandle loadVertexBuffer(const void * vertexData, const size_t count, VertexFormatHandle vertexFormatHandle) override;
96 void releaseVertexBuffer(VertexBufferHandle vertexBufferHandle) override;
97 IndexBufferHandle loadIndexBuffer(const void * indexData, const size_t count, const size_t indexSize) override;
98 void releaseIndexBuffer(IndexBufferHandle indexBufferHandle) override;
99
100 InputLayoutHandle loadInputLayout(const VertexFormatHandle * vertexFormats, const size_t count, EffectHandle effectHandle) override;
101 void releaseInputLayout(InputLayoutHandle inputLayoutHandle) override;
102 BufferHandle loadBuffer(const void * data, const size_t size, Usage::EUsage usage, uint32_t accessMode, uint32_t bindFlags, uint32_t stride = 0) override;
103
104 void retrieveSubBuffer(void * data, BufferHandle source, const size_t offset, const size_t size) override;
105 void releaseBuffer(BufferHandle bufferHandle) override;
106 void * getNativeHandle(BufferHandle bufferHandle) override;
107 void releaseResources() override;
108
111
112 std::unordered_map<size_t, VertexFormat> vertexFormats;
113
114 BufferPoolVK pool256;
115 BufferPoolVK pool1024;
116 };
117}
void releaseVertexBuffer(VertexBufferHandle vertexBufferHandle) override
Release the vertex buffer with the given handle.
Definition: BuffersVK.cpp:109
BufferHandle loadBuffer(const void *data, const size_t size, Usage::EUsage usage, uint32_t accessMode, uint32_t bindFlags, uint32_t stride=0) override
Loads a new buffer using the given data to populate the buffer.
Definition: BuffersVK.cpp:236
void releaseBuffer(BufferHandle bufferHandle) override
Releases the buffer with the given bufferHandle.
Definition: BuffersVK.cpp:331
VertexBufferHandle loadVertexBuffer(const void *vertexData, const size_t count, const VertexFormat &vertexFormat) override
Loads a new vertex buffer and populates it with the given data.
Definition: BuffersVK.cpp:80
void releaseInputLayout(InputLayoutHandle inputLayoutHandle) override
Releases the input layout with the given inputLayoutHandle.
Definition: BuffersVK.cpp:231
IndexBufferHandle loadIndexBuffer(const void *indexData, const size_t count, const size_t indexSize) override
Loads a new index buffer and populates it with the given indexData.
Definition: BuffersVK.cpp:114
void releaseIndexBuffer(IndexBufferHandle indexBufferHandle) override
Releases the index buffer with the given handle.
Definition: BuffersVK.cpp:126
void retrieveSubBuffer(void *data, BufferHandle source, const size_t offset, const size_t size) override
Retrieves the contents of a buffer.
Definition: BuffersVK.cpp:326
void * getNativeHandle(BufferHandle bufferHandle) override
Get the device-specific handle (D3D buffer pointer, OpenGL buffer ID etc) associated with the given b...
Definition: BuffersVK.cpp:336
void releaseResources() override
Releases all allocated buffer resources.
Definition: BuffersVK.cpp:341
InputLayoutHandle loadInputLayout(const VertexFormatHandle *vertexFormats, const size_t count, EffectHandle effectHandle) override
Loads a new input layout to map vertex flow between vertex buffers with the given vertexFormats to ef...
Definition: BuffersVK.cpp:145
Provides a configurable pool implementation usable as base implementation for several pool-like stora...
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
EBindFlags
Bind flags enumeration.
Definition: Flags.h:64
EUsage
Usage enumeration.
Definition: Flags.h:24
Vertex format structure used to describe a single vertex for the input assembler.
Definition: VertexFormat.h:60