2017-10-15 14:36:41 +02:00
|
|
|
#include "dxvk_buffer.h"
|
2017-12-16 13:21:11 +01:00
|
|
|
#include "dxvk_device.h"
|
2017-10-15 14:36:41 +02:00
|
|
|
|
|
|
|
namespace dxvk {
|
|
|
|
|
2017-12-16 13:21:11 +01:00
|
|
|
DxvkBuffer::DxvkBuffer(
|
|
|
|
DxvkDevice* device,
|
|
|
|
const DxvkBufferCreateInfo& createInfo,
|
|
|
|
VkMemoryPropertyFlags memoryType)
|
2018-01-19 00:20:05 +01:00
|
|
|
: m_device (device),
|
|
|
|
m_info (createInfo),
|
|
|
|
m_memFlags (memoryType) {
|
2018-01-29 00:01:00 +01:00
|
|
|
// Align physical buffer slices to 256 bytes, which guarantees
|
|
|
|
// that we don't violate any Vulkan alignment requirements
|
|
|
|
m_physSliceLength = createInfo.size;
|
|
|
|
m_physSliceStride = align(createInfo.size, 256);
|
|
|
|
|
2018-01-19 00:20:05 +01:00
|
|
|
// Initialize a single backing bufer with one slice
|
|
|
|
m_physBuffers[0] = this->allocPhysicalBuffer(1);
|
|
|
|
m_physSlice = this->allocPhysicalSlice();
|
2017-12-20 02:58:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 13:54:28 +01:00
|
|
|
void DxvkBuffer::rename(const DxvkPhysicalBufferSlice& slice) {
|
2018-01-19 00:20:05 +01:00
|
|
|
m_physSlice = slice;
|
2018-03-07 13:54:28 +01:00
|
|
|
m_revision += 1;
|
2018-01-19 00:20:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkPhysicalBufferSlice DxvkBuffer::allocPhysicalSlice() {
|
2018-01-29 00:01:00 +01:00
|
|
|
if (m_physSliceId >= m_physSliceCount) {
|
2018-01-19 00:20:05 +01:00
|
|
|
m_physBufferId = (m_physBufferId + 1) % m_physBuffers.size();
|
|
|
|
m_physSliceId = 0;
|
|
|
|
|
2018-01-29 00:01:00 +01:00
|
|
|
if (m_physBuffers[m_physBufferId] == nullptr) {
|
2018-01-19 00:20:05 +01:00
|
|
|
// Make sure that all buffers have the same size. If we don't do this,
|
|
|
|
// one of the physical buffers may grow indefinitely while the others
|
|
|
|
// remain small, depending on the usage pattern of the application.
|
|
|
|
m_physBuffers[m_physBufferId] = this->allocPhysicalBuffer(m_physSliceCount);
|
|
|
|
} else if (m_physBuffers[m_physBufferId]->isInUse()) {
|
|
|
|
// Allocate a new physical buffer if the current one is still in use.
|
|
|
|
// This also indicates that the buffer gets updated frequently, so we
|
|
|
|
// will double the size of the physical buffers to accomodate for it.
|
2018-01-29 00:01:00 +01:00
|
|
|
if (m_physBufferId == 0) {
|
|
|
|
std::fill(m_physBuffers.begin(), m_physBuffers.end(), nullptr);
|
2018-01-19 00:20:05 +01:00
|
|
|
m_physSliceCount *= 2;
|
2018-01-29 00:01:00 +01:00
|
|
|
}
|
2018-01-19 00:20:05 +01:00
|
|
|
|
|
|
|
m_physBuffers[m_physBufferId] = this->allocPhysicalBuffer(m_physSliceCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:01:00 +01:00
|
|
|
return m_physBuffers[m_physBufferId]->slice(
|
|
|
|
m_physSliceStride * m_physSliceId++,
|
|
|
|
m_physSliceLength);
|
2017-12-16 13:21:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-19 00:20:05 +01:00
|
|
|
Rc<DxvkPhysicalBuffer> DxvkBuffer::allocPhysicalBuffer(VkDeviceSize sliceCount) const {
|
2018-01-29 11:44:02 +01:00
|
|
|
DxvkBufferCreateInfo createInfo = m_info;
|
2018-01-29 00:01:00 +01:00
|
|
|
createInfo.size = sliceCount * m_physSliceStride;
|
|
|
|
|
|
|
|
return m_device->allocPhysicalBuffer(createInfo, m_memFlags);
|
2017-12-16 13:21:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-21 19:50:57 +01:00
|
|
|
DxvkBufferView::DxvkBufferView(
|
|
|
|
const Rc<vk::DeviceFn>& vkd,
|
|
|
|
const Rc<DxvkBuffer>& buffer,
|
|
|
|
const DxvkBufferViewCreateInfo& info)
|
2018-03-07 13:54:28 +01:00
|
|
|
: m_vkd(vkd), m_info(info), m_buffer(buffer),
|
|
|
|
m_physView(this->createView()),
|
|
|
|
m_revision(m_buffer->m_revision) {
|
|
|
|
|
2018-01-18 18:33:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkBufferView::~DxvkBufferView() {
|
2018-03-07 13:54:28 +01:00
|
|
|
|
2018-01-18 18:33:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 13:54:28 +01:00
|
|
|
void DxvkBufferView::updateView() {
|
|
|
|
if (m_revision != m_buffer->m_revision) {
|
|
|
|
m_physView = this->createView();
|
|
|
|
m_revision = m_buffer->m_revision;
|
|
|
|
}
|
2017-11-21 19:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 13:54:28 +01:00
|
|
|
Rc<DxvkPhysicalBufferView> DxvkBufferView::createView() {
|
|
|
|
return new DxvkPhysicalBufferView(
|
|
|
|
m_vkd, m_buffer->slice(), m_info);
|
2017-11-21 19:50:57 +01:00
|
|
|
}
|
|
|
|
|
2017-10-15 14:36:41 +02:00
|
|
|
}
|