-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathTransferTask.h
More file actions
127 lines (92 loc) · 5.83 KB
/
TransferTask.h
File metadata and controls
127 lines (92 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma once
/* <editor-fold desc="MIT License">
Copyright(c) 2022 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/io/Logger.h>
#include <vsg/state/ImageInfo.h>
#include <vsg/vk/CommandBuffer.h>
#include <vsg/vk/ResourceRequirements.h>
#include <vsg/vk/Semaphore.h>
#include <vsg/vk/Fence.h>
namespace vsg
{
/// TransferTask manages a collection of dynamically updated vsg::Data associated with GPU memory of vsg::BufferInfo and vsg::ImageInfo.
/// During the viewer.compile(..) traversal the collection of dynamic data that has dataVariance of DYNAMIC_DATA* is assigned to the appropriate TransferTask
/// and then each new frame that collection of data is checked to see if the modification count has changed, if it has that data is copied to the associated BufferInfo/ImageInfo.
/// vsg::Data that are orphaned so the TransferTask has the only remaining reference to them are automatically removed.
class VSG_DECLSPEC TransferTask : public Inherit<Object, TransferTask>
{
public:
explicit TransferTask(Device* in_device, uint32_t numBuffers = 3);
struct TransferResult
{
VkResult result = VK_SUCCESS;
ref_ptr<Semaphore> dataTransferredSemaphore;
};
enum TransferMask
{
TRANSFER_NONE = 0,
TRANSFER_BEFORE_RECORD_TRAVERSAL = 1 << 0,
TRANSFER_AFTER_RECORD_TRAVERSAL = 1 << 1,
TRANSFER_ALL = TRANSFER_BEFORE_RECORD_TRAVERSAL | TRANSFER_AFTER_RECORD_TRAVERSAL
};
/// transfer any vsg::Data entries that have been updated to the associated GPU memory.
virtual TransferResult transferData(TransferMask transferMask);
virtual bool containsDataToTransfer(TransferMask transferMask) const;
ref_ptr<Device> device;
void assign(const ResourceRequirements::DynamicData& dynamicData);
void assign(const BufferInfoList& bufferInfoList);
void assign(const ImageInfoList& imageInfoList);
ref_ptr<Queue> transferQueue;
/// minimum size to use when allocating staging buffers.
VkDeviceSize minimumStagingBufferSize = 16 * 1024 * 1024;
/// hook for assigning Instrumentation to enable profiling of record traversal.
ref_ptr<Instrumentation> instrumentation;
/// control for the level of debug information emitted by the TransferTask
Logger::Level level = Logger::LOGGER_DEBUG;
void assignTransferConsumedCompletedSemaphore(TransferMask transferMask, ref_ptr<Semaphore> semaphore);
protected:
using OffsetBufferInfoMap = std::map<VkDeviceSize, ref_ptr<BufferInfo>>;
using BufferMap = std::map<ref_ptr<Buffer>, OffsetBufferInfoMap>;
mutable std::mutex _mutex;
struct TransferBlock : public Inherit<Object, TransferBlock>
{
ref_ptr<CommandBuffer> transferCommandBuffer;
ref_ptr<Fence> fence;
ref_ptr<Buffer> staging;
void* buffer_data = nullptr;
std::vector<VkBufferCopy> copyRegions;
bool waitOnFence = false;
};
struct DataToCopy
{
std::string name;
BufferMap dataMap;
std::set<ref_ptr<ImageInfo>> imageInfoSet;
uint32_t frameIndex = 0;
std::vector<ref_ptr<TransferBlock>> frames;
VkDeviceSize dataTotalRegions = 0;
VkDeviceSize dataTotalSize = 0;
VkDeviceSize imageTotalSize = 0;
ref_ptr<Fence> transferCompleteFence;
bool waitOnTransferFence = false;
ref_ptr<Semaphore> transferCompleteSemaphore;
ref_ptr<Semaphore> transferConsumerCompletedSemaphore;
bool requiresCopy(uint32_t deviceID) const;
bool containsDataToTransfer() const { return !dataMap.empty() || !imageInfoSet.empty(); }
};
DataToCopy _earlyDataToCopy;
DataToCopy _lateDataToCopy;
size_t _bufferCount;
TransferResult _transferData(DataToCopy& dataToCopy);
void _transferBufferInfos(DataToCopy& dataToCopy, VkCommandBuffer vk_commandBuffer, TransferBlock& frame, VkDeviceSize& offset);
void _transferImageInfos(DataToCopy& dataToCopy, VkCommandBuffer vk_commandBuffer, TransferBlock& frame, VkDeviceSize& offset);
void _transferImageInfo(VkCommandBuffer vk_commandBuffer, TransferBlock& frame, VkDeviceSize& offset, ImageInfo& imageInfo);
};
VSG_type_name(vsg::TransferTask);
/// convenience function that uploads staging buffer data to device including mipmaps.
extern VSG_DECLSPEC void transferImageData(ref_ptr<ImageView> imageView, VkImageLayout targetImageLayout, Data::Properties properties, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipLevels, ref_ptr<Buffer> stagingBuffer, VkDeviceSize stagingBufferOffset, VkCommandBuffer vk_commandBuffer, vsg::Device* device);
} // namespace vsg