Vulkan: Add uploader for BufferSetSubData
This commit is contained in:
parent
a8ec80ba57
commit
eee5171c39
|
@ -289,6 +289,8 @@ if (NXT_ENABLE_VULKAN)
|
||||||
|
|
||||||
list(APPEND BACKEND_SOURCES
|
list(APPEND BACKEND_SOURCES
|
||||||
${VULKAN_DIR}/vulkan_platform.h
|
${VULKAN_DIR}/vulkan_platform.h
|
||||||
|
${VULKAN_DIR}/BufferUploader.cpp
|
||||||
|
${VULKAN_DIR}/BufferUploader.h
|
||||||
${VULKAN_DIR}/BufferVk.cpp
|
${VULKAN_DIR}/BufferVk.cpp
|
||||||
${VULKAN_DIR}/BufferVk.h
|
${VULKAN_DIR}/BufferVk.h
|
||||||
${VULKAN_DIR}/MemoryAllocator.cpp
|
${VULKAN_DIR}/MemoryAllocator.cpp
|
||||||
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
// Copyright 2017 The NXT Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include "backend/vulkan/BufferUploader.h"
|
||||||
|
|
||||||
|
#include "backend/vulkan/MemoryAllocator.h"
|
||||||
|
#include "backend/vulkan/VulkanBackend.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace backend {
|
||||||
|
namespace vulkan {
|
||||||
|
|
||||||
|
BufferUploader::BufferUploader(Device* device)
|
||||||
|
: device(device) {
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferUploader::~BufferUploader() {
|
||||||
|
ASSERT(buffersToDelete.Empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void BufferUploader::BufferSubData(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size, const void* data) {
|
||||||
|
// TODO(cwallez@chromium.org): this is soooooo bad. We should use some sort of ring buffer for this.
|
||||||
|
|
||||||
|
// Create a staging buffer
|
||||||
|
VkBufferCreateInfo createInfo;
|
||||||
|
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||||
|
createInfo.pNext = nullptr;
|
||||||
|
createInfo.flags = 0;
|
||||||
|
createInfo.size = size;
|
||||||
|
createInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||||
|
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
createInfo.queueFamilyIndexCount = 0;
|
||||||
|
createInfo.pQueueFamilyIndices = 0;
|
||||||
|
|
||||||
|
VkBuffer stagingBuffer = VK_NULL_HANDLE;
|
||||||
|
if (device->fn.CreateBuffer(device->GetVkDevice(), &createInfo, nullptr, &stagingBuffer) != VK_SUCCESS) {
|
||||||
|
ASSERT(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
VkMemoryRequirements requirements;
|
||||||
|
device->fn.GetBufferMemoryRequirements(device->GetVkDevice(), stagingBuffer, &requirements);
|
||||||
|
|
||||||
|
DeviceMemoryAllocation allocation;
|
||||||
|
if (!device->GetMemoryAllocator()->Allocate(requirements, true, &allocation)) {
|
||||||
|
ASSERT(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device->fn.BindBufferMemory(device->GetVkDevice(), stagingBuffer, allocation.GetMemory(),
|
||||||
|
allocation.GetMemoryOffset()) != VK_SUCCESS) {
|
||||||
|
ASSERT(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write to the staging buffer
|
||||||
|
ASSERT(allocation.GetMappedPointer() != nullptr);
|
||||||
|
memcpy(allocation.GetMappedPointer(), data, size);
|
||||||
|
|
||||||
|
// Enqueue host write -> transfer src barrier and copy command
|
||||||
|
VkCommandBuffer commands = device->GetPendingCommandBuffer();
|
||||||
|
|
||||||
|
VkMemoryBarrier barrier;
|
||||||
|
barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
|
||||||
|
barrier.pNext = nullptr;
|
||||||
|
barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
|
||||||
|
barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
|
||||||
|
|
||||||
|
device->fn.CmdPipelineBarrier(commands,
|
||||||
|
VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
|
||||||
|
1, &barrier,
|
||||||
|
0, nullptr,
|
||||||
|
0, nullptr);
|
||||||
|
|
||||||
|
VkBufferCopy copy;
|
||||||
|
copy.srcOffset = 0;
|
||||||
|
copy.dstOffset = offset;
|
||||||
|
copy.size = size;
|
||||||
|
device->fn.CmdCopyBuffer(commands, stagingBuffer, buffer, 1, ©);
|
||||||
|
|
||||||
|
// TODO(cwallez@chromium.org): Buffers must be deleted before the memory.
|
||||||
|
// This happens to work for now, but is fragile.
|
||||||
|
device->GetMemoryAllocator()->Free(&allocation);
|
||||||
|
buffersToDelete.Enqueue(stagingBuffer, device->GetSerial());
|
||||||
|
}
|
||||||
|
|
||||||
|
void BufferUploader::Tick(Serial completedSerial) {
|
||||||
|
for (VkBuffer buffer : buffersToDelete.IterateUpTo(completedSerial)) {
|
||||||
|
device->fn.DestroyBuffer(device->GetVkDevice(), buffer, nullptr);
|
||||||
|
}
|
||||||
|
buffersToDelete.ClearUpTo(completedSerial);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
// Copyright 2017 The NXT Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#ifndef BACKEND_VULKAN_BUFFERUPLOADER_H_
|
||||||
|
#define BACKEND_VULKAN_BUFFERUPLOADER_H_
|
||||||
|
|
||||||
|
#include "backend/vulkan/vulkan_platform.h"
|
||||||
|
#include "common/SerialQueue.h"
|
||||||
|
|
||||||
|
namespace backend {
|
||||||
|
namespace vulkan {
|
||||||
|
|
||||||
|
class Device;
|
||||||
|
|
||||||
|
class BufferUploader {
|
||||||
|
public:
|
||||||
|
BufferUploader(Device* device);
|
||||||
|
~BufferUploader();
|
||||||
|
|
||||||
|
void BufferSubData(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size, const void* data);
|
||||||
|
|
||||||
|
void Tick(Serial completedSerial);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Device* device = nullptr;
|
||||||
|
SerialQueue<VkBuffer> buffersToDelete;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // BACKEND_VULKAN_BUFFERUPLOADER_H_
|
|
@ -13,6 +13,8 @@
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#include "backend/vulkan/BufferVk.h"
|
#include "backend/vulkan/BufferVk.h"
|
||||||
|
|
||||||
|
#include "backend/vulkan/BufferUploader.h"
|
||||||
#include "backend/vulkan/VulkanBackend.h"
|
#include "backend/vulkan/VulkanBackend.h"
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
@ -97,14 +99,8 @@ namespace vulkan {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) {
|
void Buffer::SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) {
|
||||||
// TODO(cwallez@chromium.org): Make a proper resource uploader. Not all resources
|
BufferUploader* uploader = ToBackend(GetDevice())->GetBufferUploader();
|
||||||
// can be directly mapped.
|
uploader->BufferSubData(handle, start * sizeof(uint32_t), count * sizeof(uint32_t), data);
|
||||||
uint8_t* memory = memoryAllocation.GetMappedPointer();
|
|
||||||
ASSERT(memory != nullptr);
|
|
||||||
|
|
||||||
memcpy(memory + start * sizeof(uint32_t), data, count * sizeof(uint32_t));
|
|
||||||
|
|
||||||
ToBackend(GetDevice())->GetPendingCommandBuffer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t /*count*/) {
|
void Buffer::MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t /*count*/) {
|
||||||
|
@ -113,8 +109,6 @@ namespace vulkan {
|
||||||
|
|
||||||
MapReadRequestTracker* tracker = ToBackend(GetDevice())->GetMapReadRequestTracker();
|
MapReadRequestTracker* tracker = ToBackend(GetDevice())->GetMapReadRequestTracker();
|
||||||
tracker->Track(this, serial, memory + start);
|
tracker->Track(this, serial, memory + start);
|
||||||
|
|
||||||
ToBackend(GetDevice())->GetPendingCommandBuffer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::UnmapImpl() {
|
void Buffer::UnmapImpl() {
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
#include "backend/Commands.h"
|
#include "backend/Commands.h"
|
||||||
#include "backend/vulkan/BufferVk.h"
|
#include "backend/vulkan/BufferVk.h"
|
||||||
|
#include "backend/vulkan/BufferUploader.h"
|
||||||
#include "common/Platform.h"
|
#include "common/Platform.h"
|
||||||
|
|
||||||
#include <spirv-cross/spirv_cross.hpp>
|
#include <spirv-cross/spirv_cross.hpp>
|
||||||
|
@ -109,6 +110,7 @@ namespace vulkan {
|
||||||
|
|
||||||
mapReadRequestTracker = new MapReadRequestTracker(this);
|
mapReadRequestTracker = new MapReadRequestTracker(this);
|
||||||
memoryAllocator = new MemoryAllocator(this);
|
memoryAllocator = new MemoryAllocator(this);
|
||||||
|
bufferUploader = new BufferUploader(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Device::~Device() {
|
Device::~Device() {
|
||||||
|
@ -138,6 +140,11 @@ namespace vulkan {
|
||||||
}
|
}
|
||||||
unusedFences.clear();
|
unusedFences.clear();
|
||||||
|
|
||||||
|
if (bufferUploader) {
|
||||||
|
delete bufferUploader;
|
||||||
|
bufferUploader = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
if (memoryAllocator) {
|
if (memoryAllocator) {
|
||||||
delete memoryAllocator;
|
delete memoryAllocator;
|
||||||
memoryAllocator = nullptr;
|
memoryAllocator = nullptr;
|
||||||
|
@ -234,6 +241,7 @@ namespace vulkan {
|
||||||
RecycleCompletedCommands();
|
RecycleCompletedCommands();
|
||||||
|
|
||||||
mapReadRequestTracker->Tick(completedSerial);
|
mapReadRequestTracker->Tick(completedSerial);
|
||||||
|
bufferUploader->Tick(completedSerial);
|
||||||
memoryAllocator->Tick(completedSerial);
|
memoryAllocator->Tick(completedSerial);
|
||||||
|
|
||||||
if (pendingCommands.pool != VK_NULL_HANDLE) {
|
if (pendingCommands.pool != VK_NULL_HANDLE) {
|
||||||
|
@ -253,6 +261,10 @@ namespace vulkan {
|
||||||
return memoryAllocator;
|
return memoryAllocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BufferUploader* Device::GetBufferUploader() const {
|
||||||
|
return bufferUploader;
|
||||||
|
}
|
||||||
|
|
||||||
Serial Device::GetSerial() const {
|
Serial Device::GetSerial() const {
|
||||||
return nextSerial;
|
return nextSerial;
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,6 +69,7 @@ namespace vulkan {
|
||||||
|
|
||||||
class MapReadRequestTracker;
|
class MapReadRequestTracker;
|
||||||
class MemoryAllocator;
|
class MemoryAllocator;
|
||||||
|
class BufferUploader;
|
||||||
|
|
||||||
struct VulkanBackendTraits {
|
struct VulkanBackendTraits {
|
||||||
using BindGroupType = BindGroup;
|
using BindGroupType = BindGroup;
|
||||||
|
@ -128,6 +129,7 @@ namespace vulkan {
|
||||||
const VulkanDeviceInfo& GetDeviceInfo() const;
|
const VulkanDeviceInfo& GetDeviceInfo() const;
|
||||||
MapReadRequestTracker* GetMapReadRequestTracker() const;
|
MapReadRequestTracker* GetMapReadRequestTracker() const;
|
||||||
MemoryAllocator* GetMemoryAllocator() const;
|
MemoryAllocator* GetMemoryAllocator() const;
|
||||||
|
BufferUploader* GetBufferUploader() const;
|
||||||
|
|
||||||
Serial GetSerial() const;
|
Serial GetSerial() const;
|
||||||
|
|
||||||
|
@ -175,6 +177,7 @@ namespace vulkan {
|
||||||
|
|
||||||
MapReadRequestTracker* mapReadRequestTracker = nullptr;
|
MapReadRequestTracker* mapReadRequestTracker = nullptr;
|
||||||
MemoryAllocator* memoryAllocator = nullptr;
|
MemoryAllocator* memoryAllocator = nullptr;
|
||||||
|
BufferUploader* bufferUploader = nullptr;
|
||||||
|
|
||||||
VkFence GetUnusedFence();
|
VkFence GetUnusedFence();
|
||||||
void CheckPassedFences();
|
void CheckPassedFences();
|
||||||
|
|
Loading…
Reference in New Issue