-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSVObjBuffer.cpp
77 lines (66 loc) · 1.95 KB
/
SVObjBuffer.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
68
69
70
71
72
73
74
75
76
77
#include <memory.h>
#include "internal_context.h"
#include "SVObjBuffer.h"
#include "Context.h"
namespace VkInline
{
SVObjBuffer::SVObjBuffer(const std::vector<const ShaderViewable*>& elems)
{
if (elems.size() < 1)
{
printf("SVObjBuffer: cannot create with empty input.\n");
exit(0);
}
m_elem_type = elems[0]->name_view_type();
for (size_t i=1; i<elems.size(); i++)
if (elems[i]->name_view_type() != m_elem_type)
{
printf("SVObjBuffer: input elements must be the same type.\n");
exit(0);
}
m_elems = elems;
m_elem_size = SizeOf(m_elem_type.c_str());
m_size = elems.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 " + m_elem_type +
" v;\n"
"};\n";
m_name_view_type = std::string("Buf_") + Add_Dynamic_Code(code.c_str());
m_data = new Internal::UploadBuffer(m_elem_size*m_size, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
update();
}
SVObjBuffer::~SVObjBuffer()
{
delete m_data;
}
void SVObjBuffer::update()
{
ViewBuf whole(m_elem_size*m_size);
for (size_t i = 0; i < m_elems.size(); i++)
{
ViewBuf elem_view = m_elems[i]->view();
memcpy(whole.data() + m_elem_size * i, elem_view.data(), elem_view.size());
}
m_data->upload(whole.data());
}
ViewBuf SVObjBuffer::view() const
{
ViewBuf buf(sizeof(VkDeviceAddress));
VkDeviceAddress* pview = (VkDeviceAddress*)buf.data();
*pview = m_data->get_device_address();
return buf;
}
void SVObjBuffer::apply_barriers(const Internal::CommandBuffer& cmdbuf, unsigned dstFlags) const
{
for (size_t i = 0; i < m_elems.size(); i++)
{
m_elems[i]->apply_barriers(cmdbuf, dstFlags);
}
m_data->apply_barrier(cmdbuf, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, dstFlags);
}
}