-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSVBuffer.cpp
68 lines (58 loc) · 1.75 KB
/
SVBuffer.cpp
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
#include "internal_context.h"
#include "SVBuffer.h"
#include "Context.h"
namespace VkInline
{
SVBuffer::SVBuffer(const char* elem_type, size_t size, void* hdata)
{
m_elem_type = elem_type;
m_elem_size = SizeOf(elem_type);
m_size = size;
unsigned alignment = 4;
if (m_elem_size % 8 == 0) alignment = 8;
if (m_elem_size % 16 == 0) alignment = 16;
char line[1024];
sprintf(line, "layout(buffer_reference, scalar, buffer_reference_align = %u) buffer Buf_#hash#\n", alignment);
std::string code = std::string(line) +
"{\n " + elem_type +
" v;\n"
"};\n";
m_name_view_type = std::string("Buf_") + Add_Dynamic_Code(code.c_str());
VkBufferUsageFlags usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
#ifdef _VkInlineEX
usage |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR;
#endif
m_data = new Internal::DeviceBuffer(m_elem_size*m_size, usage);
if (hdata != nullptr)
m_data->upload(hdata);
else
m_data->zero();
}
SVBuffer::~SVBuffer()
{
delete m_data;
}
void SVBuffer::from_host(void* hdata)
{
if (m_size > 0)
m_data->upload(hdata);
}
void SVBuffer::to_host(void* hdata, size_t begin, size_t end) const
{
if (end == (size_t)(-1) || end > m_size) end = m_size;
size_t n = end - begin;
if (n > 0)
m_data->download(hdata, begin*m_elem_size, end*m_elem_size);
}
ViewBuf SVBuffer::view() const
{
ViewBuf buf(sizeof(VkDeviceAddress));
VkDeviceAddress* pview = (VkDeviceAddress*)buf.data();
*pview = m_data->get_device_address();
return buf;
}
void SVBuffer::apply_barriers(const Internal::CommandBuffer& cmdbuf, unsigned dstFlags) const
{
m_data->apply_barrier(cmdbuf, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, dstFlags);
}
}