Member rename: src/backend/vulkan

This commit is contained in:
Corentin Wallez 2017-11-23 11:35:40 -08:00 committed by Corentin Wallez
parent 7ee1610f38
commit 8d75e5b4ae
8 changed files with 177 additions and 179 deletions

View File

@ -23,11 +23,11 @@ namespace backend {
namespace vulkan { namespace vulkan {
BufferUploader::BufferUploader(Device* device) BufferUploader::BufferUploader(Device* device)
: device(device) { : mDevice(device) {
} }
BufferUploader::~BufferUploader() { BufferUploader::~BufferUploader() {
ASSERT(buffersToDelete.Empty()); ASSERT(mBuffersToDelete.Empty());
} }
void BufferUploader::BufferSubData(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size, const void* data) { void BufferUploader::BufferSubData(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size, const void* data) {
@ -45,19 +45,19 @@ namespace vulkan {
createInfo.pQueueFamilyIndices = 0; createInfo.pQueueFamilyIndices = 0;
VkBuffer stagingBuffer = VK_NULL_HANDLE; VkBuffer stagingBuffer = VK_NULL_HANDLE;
if (device->fn.CreateBuffer(device->GetVkDevice(), &createInfo, nullptr, &stagingBuffer) != VK_SUCCESS) { if (mDevice->fn.CreateBuffer(mDevice->GetVkDevice(), &createInfo, nullptr, &stagingBuffer) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
VkMemoryRequirements requirements; VkMemoryRequirements requirements;
device->fn.GetBufferMemoryRequirements(device->GetVkDevice(), stagingBuffer, &requirements); mDevice->fn.GetBufferMemoryRequirements(mDevice->GetVkDevice(), stagingBuffer, &requirements);
DeviceMemoryAllocation allocation; DeviceMemoryAllocation allocation;
if (!device->GetMemoryAllocator()->Allocate(requirements, true, &allocation)) { if (!mDevice->GetMemoryAllocator()->Allocate(requirements, true, &allocation)) {
ASSERT(false); ASSERT(false);
} }
if (device->fn.BindBufferMemory(device->GetVkDevice(), stagingBuffer, allocation.GetMemory(), if (mDevice->fn.BindBufferMemory(mDevice->GetVkDevice(), stagingBuffer, allocation.GetMemory(),
allocation.GetMemoryOffset()) != VK_SUCCESS) { allocation.GetMemoryOffset()) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
@ -67,7 +67,7 @@ namespace vulkan {
memcpy(allocation.GetMappedPointer(), data, size); memcpy(allocation.GetMappedPointer(), data, size);
// Enqueue host write -> transfer src barrier and copy command // Enqueue host write -> transfer src barrier and copy command
VkCommandBuffer commands = device->GetPendingCommandBuffer(); VkCommandBuffer commands = mDevice->GetPendingCommandBuffer();
VkMemoryBarrier barrier; VkMemoryBarrier barrier;
barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER; barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
@ -75,7 +75,7 @@ namespace vulkan {
barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
device->fn.CmdPipelineBarrier(commands, mDevice->fn.CmdPipelineBarrier(commands,
VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
1, &barrier, 1, &barrier,
0, nullptr, 0, nullptr,
@ -85,19 +85,19 @@ namespace vulkan {
copy.srcOffset = 0; copy.srcOffset = 0;
copy.dstOffset = offset; copy.dstOffset = offset;
copy.size = size; copy.size = size;
device->fn.CmdCopyBuffer(commands, stagingBuffer, buffer, 1, &copy); mDevice->fn.CmdCopyBuffer(commands, stagingBuffer, buffer, 1, &copy);
// TODO(cwallez@chromium.org): Buffers must be deleted before the memory. // TODO(cwallez@chromium.org): Buffers must be deleted before the memory.
// This happens to work for now, but is fragile. // This happens to work for now, but is fragile.
device->GetMemoryAllocator()->Free(&allocation); mDevice->GetMemoryAllocator()->Free(&allocation);
buffersToDelete.Enqueue(stagingBuffer, device->GetSerial()); mBuffersToDelete.Enqueue(stagingBuffer, mDevice->GetSerial());
} }
void BufferUploader::Tick(Serial completedSerial) { void BufferUploader::Tick(Serial completedSerial) {
for (VkBuffer buffer : buffersToDelete.IterateUpTo(completedSerial)) { for (VkBuffer buffer : mBuffersToDelete.IterateUpTo(completedSerial)) {
device->fn.DestroyBuffer(device->GetVkDevice(), buffer, nullptr); mDevice->fn.DestroyBuffer(mDevice->GetVkDevice(), buffer, nullptr);
} }
buffersToDelete.ClearUpTo(completedSerial); mBuffersToDelete.ClearUpTo(completedSerial);
} }
} }

View File

@ -33,8 +33,8 @@ namespace vulkan {
void Tick(Serial completedSerial); void Tick(Serial completedSerial);
private: private:
Device* device = nullptr; Device* mDevice = nullptr;
SerialQueue<VkBuffer> buffersToDelete; SerialQueue<VkBuffer> mBuffersToDelete;
}; };
} }

View File

@ -65,20 +65,20 @@ namespace vulkan {
createInfo.queueFamilyIndexCount = 0; createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = 0; createInfo.pQueueFamilyIndices = 0;
if (device->fn.CreateBuffer(device->GetVkDevice(), &createInfo, nullptr, &handle) != VK_SUCCESS) { if (device->fn.CreateBuffer(device->GetVkDevice(), &createInfo, nullptr, &mHandle) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
VkMemoryRequirements requirements; VkMemoryRequirements requirements;
device->fn.GetBufferMemoryRequirements(device->GetVkDevice(), handle, &requirements); device->fn.GetBufferMemoryRequirements(device->GetVkDevice(), mHandle, &requirements);
bool requestMappable = (GetAllowedUsage() & (nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::MapWrite)) != 0; bool requestMappable = (GetAllowedUsage() & (nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::MapWrite)) != 0;
if (!device->GetMemoryAllocator()->Allocate(requirements, requestMappable, &memoryAllocation)) { if (!device->GetMemoryAllocator()->Allocate(requirements, requestMappable, &mMemoryAllocation)) {
ASSERT(false); ASSERT(false);
} }
if (device->fn.BindBufferMemory(device->GetVkDevice(), handle, memoryAllocation.GetMemory(), if (device->fn.BindBufferMemory(device->GetVkDevice(), mHandle, mMemoryAllocation.GetMemory(),
memoryAllocation.GetMemoryOffset()) != VK_SUCCESS) { mMemoryAllocation.GetMemoryOffset()) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
} }
@ -86,11 +86,11 @@ namespace vulkan {
Buffer::~Buffer() { Buffer::~Buffer() {
Device* device = ToBackend(GetDevice()); Device* device = ToBackend(GetDevice());
device->GetMemoryAllocator()->Free(&memoryAllocation); device->GetMemoryAllocator()->Free(&mMemoryAllocation);
if (handle != VK_NULL_HANDLE) { if (mHandle != VK_NULL_HANDLE) {
device->fn.DestroyBuffer(device->GetVkDevice(), handle, nullptr); device->fn.DestroyBuffer(device->GetVkDevice(), mHandle, nullptr);
handle = VK_NULL_HANDLE; mHandle = VK_NULL_HANDLE;
} }
} }
@ -100,11 +100,11 @@ 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) {
BufferUploader* uploader = ToBackend(GetDevice())->GetBufferUploader(); BufferUploader* uploader = ToBackend(GetDevice())->GetBufferUploader();
uploader->BufferSubData(handle, start * sizeof(uint32_t), count * sizeof(uint32_t), data); uploader->BufferSubData(mHandle, start * sizeof(uint32_t), count * sizeof(uint32_t), data);
} }
void Buffer::MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t /*count*/) { void Buffer::MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t /*count*/) {
const uint8_t* memory = memoryAllocation.GetMappedPointer(); const uint8_t* memory = mMemoryAllocation.GetMappedPointer();
ASSERT(memory != nullptr); ASSERT(memory != nullptr);
MapReadRequestTracker* tracker = ToBackend(GetDevice())->GetMapReadRequestTracker(); MapReadRequestTracker* tracker = ToBackend(GetDevice())->GetMapReadRequestTracker();
@ -119,11 +119,11 @@ namespace vulkan {
} }
MapReadRequestTracker::MapReadRequestTracker(Device* device) MapReadRequestTracker::MapReadRequestTracker(Device* device)
: device(device) { : mDevice(device) {
} }
MapReadRequestTracker::~MapReadRequestTracker() { MapReadRequestTracker::~MapReadRequestTracker() {
ASSERT(inflightRequests.Empty()); ASSERT(mInflightRequests.Empty());
} }
void MapReadRequestTracker::Track(Buffer* buffer, uint32_t mapSerial, const void* data) { void MapReadRequestTracker::Track(Buffer* buffer, uint32_t mapSerial, const void* data) {
@ -132,14 +132,14 @@ namespace vulkan {
request.mapSerial = mapSerial; request.mapSerial = mapSerial;
request.data = data; request.data = data;
inflightRequests.Enqueue(std::move(request), device->GetSerial()); mInflightRequests.Enqueue(std::move(request), mDevice->GetSerial());
} }
void MapReadRequestTracker::Tick(Serial finishedSerial) { void MapReadRequestTracker::Tick(Serial finishedSerial) {
for (auto& request : inflightRequests.IterateUpTo(finishedSerial)) { for (auto& request : mInflightRequests.IterateUpTo(finishedSerial)) {
request.buffer->OnMapReadCommandSerialFinished(request.mapSerial, request.data); request.buffer->OnMapReadCommandSerialFinished(request.mapSerial, request.data);
} }
inflightRequests.ClearUpTo(finishedSerial); mInflightRequests.ClearUpTo(finishedSerial);
} }
} }

View File

@ -39,8 +39,8 @@ namespace vulkan {
void UnmapImpl() override; void UnmapImpl() override;
void TransitionUsageImpl(nxt::BufferUsageBit currentUsage, nxt::BufferUsageBit targetUsage) override; void TransitionUsageImpl(nxt::BufferUsageBit currentUsage, nxt::BufferUsageBit targetUsage) override;
VkBuffer handle = VK_NULL_HANDLE; VkBuffer mHandle = VK_NULL_HANDLE;
DeviceMemoryAllocation memoryAllocation; DeviceMemoryAllocation mMemoryAllocation;
}; };
class MapReadRequestTracker { class MapReadRequestTracker {
@ -52,14 +52,14 @@ namespace vulkan {
void Tick(Serial finishedSerial); void Tick(Serial finishedSerial);
private: private:
Device* device; Device* mDevice;
struct Request { struct Request {
Ref<Buffer> buffer; Ref<Buffer> buffer;
uint32_t mapSerial; uint32_t mapSerial;
const void* data; const void* data;
}; };
SerialQueue<Request> inflightRequests; SerialQueue<Request> mInflightRequests;
}; };
} }

View File

@ -19,31 +19,31 @@ namespace backend {
namespace vulkan { namespace vulkan {
DeviceMemoryAllocation::~DeviceMemoryAllocation() { DeviceMemoryAllocation::~DeviceMemoryAllocation() {
ASSERT(memory == VK_NULL_HANDLE); ASSERT(mMemory == VK_NULL_HANDLE);
} }
VkDeviceMemory DeviceMemoryAllocation::GetMemory() const { VkDeviceMemory DeviceMemoryAllocation::GetMemory() const {
return memory; return mMemory;
} }
size_t DeviceMemoryAllocation::GetMemoryOffset() const { size_t DeviceMemoryAllocation::GetMemoryOffset() const {
return offset; return mOffset;
} }
uint8_t* DeviceMemoryAllocation::GetMappedPointer() const { uint8_t* DeviceMemoryAllocation::GetMappedPointer() const {
return mappedPointer; return mMappedPointer;
} }
MemoryAllocator::MemoryAllocator(Device* device) MemoryAllocator::MemoryAllocator(Device* device)
:device(device) { :mDevice(device) {
} }
MemoryAllocator::~MemoryAllocator() { MemoryAllocator::~MemoryAllocator() {
ASSERT(releasedMemory.Empty()); ASSERT(mReleasedMemory.Empty());
} }
bool MemoryAllocator::Allocate(VkMemoryRequirements requirements, bool mappable, DeviceMemoryAllocation* allocation) { bool MemoryAllocator::Allocate(VkMemoryRequirements requirements, bool mappable, DeviceMemoryAllocation* allocation) {
const VulkanDeviceInfo& info = device->GetDeviceInfo(); const VulkanDeviceInfo& info = mDevice->GetDeviceInfo();
// Find a suitable memory type for this allocation // Find a suitable memory type for this allocation
int bestType = -1; int bestType = -1;
@ -95,37 +95,37 @@ namespace vulkan {
allocateInfo.memoryTypeIndex = static_cast<uint32_t>(bestType); allocateInfo.memoryTypeIndex = static_cast<uint32_t>(bestType);
VkDeviceMemory allocatedMemory = VK_NULL_HANDLE; VkDeviceMemory allocatedMemory = VK_NULL_HANDLE;
if (device->fn.AllocateMemory(device->GetVkDevice(), &allocateInfo, nullptr, &allocatedMemory) != VK_SUCCESS) { if (mDevice->fn.AllocateMemory(mDevice->GetVkDevice(), &allocateInfo, nullptr, &allocatedMemory) != VK_SUCCESS) {
return false; return false;
} }
void* mappedPointer = nullptr; void* mappedPointer = nullptr;
if (mappable) { if (mappable) {
if (device->fn.MapMemory(device->GetVkDevice(), allocatedMemory, 0, requirements.size, 0, if (mDevice->fn.MapMemory(mDevice->GetVkDevice(), allocatedMemory, 0, requirements.size, 0,
&mappedPointer) != VK_SUCCESS) { &mappedPointer) != VK_SUCCESS) {
return false; return false;
} }
} }
allocation->memory = allocatedMemory; allocation->mMemory = allocatedMemory;
allocation->offset = 0; allocation->mOffset = 0;
allocation->mappedPointer = reinterpret_cast<uint8_t*>(mappedPointer); allocation->mMappedPointer = reinterpret_cast<uint8_t*>(mappedPointer);
return true; return true;
} }
void MemoryAllocator::Free(DeviceMemoryAllocation* allocation) { void MemoryAllocator::Free(DeviceMemoryAllocation* allocation) {
releasedMemory.Enqueue(allocation->memory, device->GetSerial()); mReleasedMemory.Enqueue(allocation->mMemory, mDevice->GetSerial());
allocation->memory = VK_NULL_HANDLE; allocation->mMemory = VK_NULL_HANDLE;
allocation->offset = 0; allocation->mOffset = 0;
allocation->mappedPointer = nullptr; allocation->mMappedPointer = nullptr;
} }
void MemoryAllocator::Tick(Serial finishedSerial) { void MemoryAllocator::Tick(Serial finishedSerial) {
for (auto memory : releasedMemory.IterateUpTo(finishedSerial)) { for (auto memory : mReleasedMemory.IterateUpTo(finishedSerial)) {
device->fn.FreeMemory(device->GetVkDevice(), memory, nullptr); mDevice->fn.FreeMemory(mDevice->GetVkDevice(), memory, nullptr);
} }
releasedMemory.ClearUpTo(finishedSerial); mReleasedMemory.ClearUpTo(finishedSerial);
} }
} }
} }

View File

@ -33,9 +33,9 @@ namespace vulkan {
private: private:
friend class MemoryAllocator; friend class MemoryAllocator;
VkDeviceMemory memory = VK_NULL_HANDLE; VkDeviceMemory mMemory = VK_NULL_HANDLE;
size_t offset = 0; size_t mOffset = 0;
uint8_t* mappedPointer = nullptr; uint8_t* mMappedPointer = nullptr;
}; };
class MemoryAllocator { class MemoryAllocator {
@ -49,8 +49,8 @@ namespace vulkan {
void Tick(Serial finishedSerial); void Tick(Serial finishedSerial);
private: private:
Device* device = nullptr; Device* mDevice = nullptr;
SerialQueue<VkDeviceMemory> releasedMemory; SerialQueue<VkDeviceMemory> mReleasedMemory;
}; };
} }

View File

@ -45,19 +45,19 @@ namespace vulkan {
// Device // Device
Device::Device() { Device::Device() {
if (!vulkanLib.Open(kVulkanLibName)) { if (!mVulkanLib.Open(kVulkanLibName)) {
ASSERT(false); ASSERT(false);
return; return;
} }
VulkanFunctions* functions = GetMutableFunctions(); VulkanFunctions* functions = GetMutableFunctions();
if (!functions->LoadGlobalProcs(vulkanLib)) { if (!functions->LoadGlobalProcs(mVulkanLib)) {
ASSERT(false); ASSERT(false);
return; return;
} }
if (!GatherGlobalInfo(*this, &globalInfo)) { if (!GatherGlobalInfo(*this, &mGlobalInfo)) {
ASSERT(false); ASSERT(false);
return; return;
} }
@ -67,9 +67,9 @@ namespace vulkan {
ASSERT(false); ASSERT(false);
return; return;
} }
*static_cast<VulkanGlobalKnobs*>(&globalInfo) = usedGlobalKnobs; *static_cast<VulkanGlobalKnobs*>(&mGlobalInfo) = usedGlobalKnobs;
if (!functions->LoadInstanceProcs(instance, usedGlobalKnobs)) { if (!functions->LoadInstanceProcs(mInstance, usedGlobalKnobs)) {
ASSERT(false); ASSERT(false);
return; return;
} }
@ -87,9 +87,9 @@ namespace vulkan {
return; return;
} }
// TODO(cwallez@chromium.org): Choose the physical device based on ??? // TODO(cwallez@chromium.org): Choose the physical device based on ???
physicalDevice = physicalDevices[0]; mPhysicalDevice = physicalDevices[0];
if (!GatherDeviceInfo(*this, physicalDevice, &deviceInfo)) { if (!GatherDeviceInfo(*this, mPhysicalDevice, &mDeviceInfo)) {
ASSERT(false); ASSERT(false);
return; return;
} }
@ -99,77 +99,77 @@ namespace vulkan {
ASSERT(false); ASSERT(false);
return; return;
} }
*static_cast<VulkanDeviceKnobs*>(&deviceInfo) = usedDeviceKnobs; *static_cast<VulkanDeviceKnobs*>(&mDeviceInfo) = usedDeviceKnobs;
if (!functions->LoadDeviceProcs(vkDevice, usedDeviceKnobs)) { if (!functions->LoadDeviceProcs(mVkDevice, usedDeviceKnobs)) {
ASSERT(false); ASSERT(false);
return; return;
} }
GatherQueueFromDevice(); GatherQueueFromDevice();
mapReadRequestTracker = new MapReadRequestTracker(this); mMapReadRequestTracker = new MapReadRequestTracker(this);
memoryAllocator = new MemoryAllocator(this); mMemoryAllocator = new MemoryAllocator(this);
bufferUploader = new BufferUploader(this); mBufferUploader = new BufferUploader(this);
} }
Device::~Device() { Device::~Device() {
// Immediately forget about all pending commands so we don't try to submit them in Tick // Immediately forget about all pending commands so we don't try to submit them in Tick
FreeCommands(&pendingCommands); FreeCommands(&mPendingCommands);
if (fn.QueueWaitIdle(queue) != VK_SUCCESS) { if (fn.QueueWaitIdle(mQueue) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
CheckPassedFences(); CheckPassedFences();
ASSERT(fencesInFlight.empty()); ASSERT(mFencesInFlight.empty());
// Some operations might have been started since the last submit and waiting // Some operations might have been started since the last submit and waiting
// on a serial that doesn't have a corresponding fence enqueued. Force all // on a serial that doesn't have a corresponding fence enqueued. Force all
// operations to look as if they were completed (because they were). // operations to look as if they were completed (because they were).
completedSerial = nextSerial; mCompletedSerial = mNextSerial;
Tick(); Tick();
ASSERT(commandsInFlight.Empty()); ASSERT(mCommandsInFlight.Empty());
for (auto& commands : unusedCommands) { for (auto& commands : mUnusedCommands) {
FreeCommands(&commands); FreeCommands(&commands);
} }
unusedCommands.clear(); mUnusedCommands.clear();
for (VkFence fence : unusedFences) { for (VkFence fence : mUnusedFences) {
fn.DestroyFence(vkDevice, fence, nullptr); fn.DestroyFence(mVkDevice, fence, nullptr);
} }
unusedFences.clear(); mUnusedFences.clear();
if (bufferUploader) { if (mBufferUploader) {
delete bufferUploader; delete mBufferUploader;
bufferUploader = nullptr; mBufferUploader = nullptr;
} }
if (memoryAllocator) { if (mMemoryAllocator) {
delete memoryAllocator; delete mMemoryAllocator;
memoryAllocator = nullptr; mMemoryAllocator = nullptr;
} }
if (mapReadRequestTracker) { if (mMapReadRequestTracker) {
delete mapReadRequestTracker; delete mMapReadRequestTracker;
mapReadRequestTracker = nullptr; mMapReadRequestTracker = nullptr;
} }
// VkQueues are destroyed when the VkDevice is destroyed // VkQueues are destroyed when the VkDevice is destroyed
if (vkDevice != VK_NULL_HANDLE) { if (mVkDevice != VK_NULL_HANDLE) {
fn.DestroyDevice(vkDevice, nullptr); fn.DestroyDevice(mVkDevice, nullptr);
vkDevice = VK_NULL_HANDLE; mVkDevice = VK_NULL_HANDLE;
} }
if (debugReportCallback != VK_NULL_HANDLE) { if (mDebugReportCallback != VK_NULL_HANDLE) {
fn.DestroyDebugReportCallbackEXT(instance, debugReportCallback, nullptr); fn.DestroyDebugReportCallbackEXT(mInstance, mDebugReportCallback, nullptr);
debugReportCallback = VK_NULL_HANDLE; mDebugReportCallback = VK_NULL_HANDLE;
} }
// VkPhysicalDevices are destroyed when the VkInstance is destroyed // VkPhysicalDevices are destroyed when the VkInstance is destroyed
if (instance != VK_NULL_HANDLE) { if (mInstance != VK_NULL_HANDLE) {
fn.DestroyInstance(instance, nullptr); fn.DestroyInstance(mInstance, nullptr);
instance = VK_NULL_HANDLE; mInstance = VK_NULL_HANDLE;
} }
} }
@ -240,38 +240,38 @@ namespace vulkan {
CheckPassedFences(); CheckPassedFences();
RecycleCompletedCommands(); RecycleCompletedCommands();
mapReadRequestTracker->Tick(completedSerial); mMapReadRequestTracker->Tick(mCompletedSerial);
bufferUploader->Tick(completedSerial); mBufferUploader->Tick(mCompletedSerial);
memoryAllocator->Tick(completedSerial); mMemoryAllocator->Tick(mCompletedSerial);
if (pendingCommands.pool != VK_NULL_HANDLE) { if (mPendingCommands.pool != VK_NULL_HANDLE) {
SubmitPendingCommands(); SubmitPendingCommands();
} }
} }
const VulkanDeviceInfo& Device::GetDeviceInfo() const { const VulkanDeviceInfo& Device::GetDeviceInfo() const {
return deviceInfo; return mDeviceInfo;
} }
MapReadRequestTracker* Device::GetMapReadRequestTracker() const { MapReadRequestTracker* Device::GetMapReadRequestTracker() const {
return mapReadRequestTracker; return mMapReadRequestTracker;
} }
MemoryAllocator* Device::GetMemoryAllocator() const { MemoryAllocator* Device::GetMemoryAllocator() const {
return memoryAllocator; return mMemoryAllocator;
} }
BufferUploader* Device::GetBufferUploader() const { BufferUploader* Device::GetBufferUploader() const {
return bufferUploader; return mBufferUploader;
} }
Serial Device::GetSerial() const { Serial Device::GetSerial() const {
return nextSerial; return mNextSerial;
} }
VkCommandBuffer Device::GetPendingCommandBuffer() { VkCommandBuffer Device::GetPendingCommandBuffer() {
if (pendingCommands.pool == VK_NULL_HANDLE) { if (mPendingCommands.pool == VK_NULL_HANDLE) {
pendingCommands = GetUnusedCommands(); mPendingCommands = GetUnusedCommands();
VkCommandBufferBeginInfo beginInfo; VkCommandBufferBeginInfo beginInfo;
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
@ -279,20 +279,20 @@ namespace vulkan {
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
beginInfo.pInheritanceInfo = nullptr; beginInfo.pInheritanceInfo = nullptr;
if (fn.BeginCommandBuffer(pendingCommands.commandBuffer, &beginInfo) != VK_SUCCESS) { if (fn.BeginCommandBuffer(mPendingCommands.commandBuffer, &beginInfo) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
} }
return pendingCommands.commandBuffer; return mPendingCommands.commandBuffer;
} }
void Device::SubmitPendingCommands() { void Device::SubmitPendingCommands() {
if (pendingCommands.pool == VK_NULL_HANDLE) { if (mPendingCommands.pool == VK_NULL_HANDLE) {
return; return;
} }
if (fn.EndCommandBuffer(pendingCommands.commandBuffer) != VK_SUCCESS) { if (fn.EndCommandBuffer(mPendingCommands.commandBuffer) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
@ -303,27 +303,27 @@ namespace vulkan {
submitInfo.pWaitSemaphores = nullptr; submitInfo.pWaitSemaphores = nullptr;
submitInfo.pWaitDstStageMask = 0; submitInfo.pWaitDstStageMask = 0;
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &pendingCommands.commandBuffer; submitInfo.pCommandBuffers = &mPendingCommands.commandBuffer;
submitInfo.signalSemaphoreCount = 0; submitInfo.signalSemaphoreCount = 0;
submitInfo.pSignalSemaphores = 0; submitInfo.pSignalSemaphores = 0;
VkFence fence = GetUnusedFence(); VkFence fence = GetUnusedFence();
if (fn.QueueSubmit(queue, 1, &submitInfo, fence) != VK_SUCCESS) { if (fn.QueueSubmit(mQueue, 1, &submitInfo, fence) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
commandsInFlight.Enqueue(pendingCommands, nextSerial); mCommandsInFlight.Enqueue(mPendingCommands, mNextSerial);
pendingCommands = CommandPoolAndBuffer(); mPendingCommands = CommandPoolAndBuffer();
fencesInFlight.emplace(fence, nextSerial); mFencesInFlight.emplace(fence, mNextSerial);
nextSerial++; mNextSerial++;
} }
VkInstance Device::GetInstance() const { VkInstance Device::GetInstance() const {
return instance; return mInstance;
} }
VkDevice Device::GetVkDevice() const { VkDevice Device::GetVkDevice() const {
return vkDevice; return mVkDevice;
} }
bool Device::CreateInstance(VulkanGlobalKnobs* usedKnobs) { bool Device::CreateInstance(VulkanGlobalKnobs* usedKnobs) {
@ -331,11 +331,11 @@ namespace vulkan {
std::vector<const char*> extensionsToRequest; std::vector<const char*> extensionsToRequest;
#if defined(NXT_ENABLE_ASSERTS) #if defined(NXT_ENABLE_ASSERTS)
if (globalInfo.standardValidation) { if (mGlobalInfo.standardValidation) {
layersToRequest.push_back(kLayerNameLunargStandardValidation); layersToRequest.push_back(kLayerNameLunargStandardValidation);
usedKnobs->standardValidation = true; usedKnobs->standardValidation = true;
} }
if (globalInfo.debugReport) { if (mGlobalInfo.debugReport) {
extensionsToRequest.push_back(kExtensionNameExtDebugReport); extensionsToRequest.push_back(kExtensionNameExtDebugReport);
usedKnobs->debugReport = true; usedKnobs->debugReport = true;
} }
@ -360,7 +360,7 @@ namespace vulkan {
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensionsToRequest.size()); createInfo.enabledExtensionCount = static_cast<uint32_t>(extensionsToRequest.size());
createInfo.ppEnabledExtensionNames = extensionsToRequest.data(); createInfo.ppEnabledExtensionNames = extensionsToRequest.data();
if (fn.CreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) { if (fn.CreateInstance(&createInfo, nullptr, &mInstance) != VK_SUCCESS) {
return false; return false;
} }
@ -373,7 +373,7 @@ namespace vulkan {
std::vector<const char*> extensionsToRequest; std::vector<const char*> extensionsToRequest;
std::vector<VkDeviceQueueCreateInfo> queuesToRequest; std::vector<VkDeviceQueueCreateInfo> queuesToRequest;
if (deviceInfo.swapchain) { if (mDeviceInfo.swapchain) {
extensionsToRequest.push_back(kExtensionNameKhrSwapchain); extensionsToRequest.push_back(kExtensionNameKhrSwapchain);
usedKnobs->swapchain = true; usedKnobs->swapchain = true;
} }
@ -382,8 +382,8 @@ namespace vulkan {
{ {
constexpr uint32_t kUniversalFlags = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT; constexpr uint32_t kUniversalFlags = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT;
int universalQueueFamily = -1; int universalQueueFamily = -1;
for (unsigned int i = 0; i < deviceInfo.queueFamilies.size(); ++i) { for (unsigned int i = 0; i < mDeviceInfo.queueFamilies.size(); ++i) {
if ((deviceInfo.queueFamilies[i].queueFlags & kUniversalFlags) == kUniversalFlags) { if ((mDeviceInfo.queueFamilies[i].queueFlags & kUniversalFlags) == kUniversalFlags) {
universalQueueFamily = i; universalQueueFamily = i;
break; break;
} }
@ -392,7 +392,7 @@ namespace vulkan {
if (universalQueueFamily == -1) { if (universalQueueFamily == -1) {
return false; return false;
} }
queueFamily = static_cast<uint32_t>(universalQueueFamily); mQueueFamily = static_cast<uint32_t>(universalQueueFamily);
} }
// Choose to create a single universal queue // Choose to create a single universal queue
@ -401,7 +401,7 @@ namespace vulkan {
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.pNext = nullptr; queueCreateInfo.pNext = nullptr;
queueCreateInfo.flags = 0; queueCreateInfo.flags = 0;
queueCreateInfo.queueFamilyIndex = static_cast<uint32_t>(queueFamily); queueCreateInfo.queueFamilyIndex = static_cast<uint32_t>(mQueueFamily);
queueCreateInfo.queueCount = 1; queueCreateInfo.queueCount = 1;
queueCreateInfo.pQueuePriorities = &zero; queueCreateInfo.pQueuePriorities = &zero;
@ -420,7 +420,7 @@ namespace vulkan {
createInfo.ppEnabledExtensionNames = extensionsToRequest.data(); createInfo.ppEnabledExtensionNames = extensionsToRequest.data();
createInfo.pEnabledFeatures = &usedKnobs->features; createInfo.pEnabledFeatures = &usedKnobs->features;
if (fn.CreateDevice(physicalDevice, &createInfo, nullptr, &vkDevice) != VK_SUCCESS) { if (fn.CreateDevice(mPhysicalDevice, &createInfo, nullptr, &mVkDevice) != VK_SUCCESS) {
return false; return false;
} }
@ -428,7 +428,7 @@ namespace vulkan {
} }
void Device::GatherQueueFromDevice() { void Device::GatherQueueFromDevice() {
fn.GetDeviceQueue(vkDevice, queueFamily, 0, &queue); fn.GetDeviceQueue(mVkDevice, mQueueFamily, 0, &mQueue);
} }
bool Device::RegisterDebugReport() { bool Device::RegisterDebugReport() {
@ -439,7 +439,7 @@ namespace vulkan {
createInfo.pfnCallback = Device::OnDebugReportCallback; createInfo.pfnCallback = Device::OnDebugReportCallback;
createInfo.pUserData = this; createInfo.pUserData = this;
if (fn.CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, &debugReportCallback) != VK_SUCCESS) { if (fn.CreateDebugReportCallbackEXT(mInstance, &createInfo, nullptr, &mDebugReportCallback) != VK_SUCCESS) {
return false; return false;
} }
@ -465,9 +465,9 @@ namespace vulkan {
} }
VkFence Device::GetUnusedFence() { VkFence Device::GetUnusedFence() {
if (!unusedFences.empty()) { if (!mUnusedFences.empty()) {
VkFence fence = unusedFences.back(); VkFence fence = mUnusedFences.back();
unusedFences.pop_back(); mUnusedFences.pop_back();
return fence; return fence;
} }
@ -477,7 +477,7 @@ namespace vulkan {
createInfo.flags = 0; createInfo.flags = 0;
VkFence fence = VK_NULL_HANDLE; VkFence fence = VK_NULL_HANDLE;
if (fn.CreateFence(vkDevice, &createInfo, nullptr, &fence) != VK_SUCCESS) { if (fn.CreateFence(mVkDevice, &createInfo, nullptr, &fence) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
@ -485,11 +485,11 @@ namespace vulkan {
} }
void Device::CheckPassedFences() { void Device::CheckPassedFences() {
while (!fencesInFlight.empty()) { while (!mFencesInFlight.empty()) {
VkFence fence = fencesInFlight.front().first; VkFence fence = mFencesInFlight.front().first;
Serial fenceSerial = fencesInFlight.front().second; Serial fenceSerial = mFencesInFlight.front().second;
VkResult result = fn.GetFenceStatus(vkDevice, fence); VkResult result = fn.GetFenceStatus(mVkDevice, fence);
ASSERT(result == VK_SUCCESS || result == VK_NOT_READY); ASSERT(result == VK_SUCCESS || result == VK_NOT_READY);
// Fence are added in order, so we can stop searching as soon // Fence are added in order, so we can stop searching as soon
@ -498,22 +498,22 @@ namespace vulkan {
return; return;
} }
if (fn.ResetFences(vkDevice, 1, &fence) != VK_SUCCESS) { if (fn.ResetFences(mVkDevice, 1, &fence) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
unusedFences.push_back(fence); mUnusedFences.push_back(fence);
fencesInFlight.pop(); mFencesInFlight.pop();
ASSERT(fenceSerial > completedSerial); ASSERT(fenceSerial > mCompletedSerial);
completedSerial = fenceSerial; mCompletedSerial = fenceSerial;
} }
} }
Device::CommandPoolAndBuffer Device::GetUnusedCommands() { Device::CommandPoolAndBuffer Device::GetUnusedCommands() {
if (!unusedCommands.empty()) { if (!mUnusedCommands.empty()) {
CommandPoolAndBuffer commands = unusedCommands.back(); CommandPoolAndBuffer commands = mUnusedCommands.back();
unusedCommands.pop_back(); mUnusedCommands.pop_back();
return commands; return commands;
} }
@ -523,9 +523,9 @@ namespace vulkan {
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
createInfo.pNext = nullptr; createInfo.pNext = nullptr;
createInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT; createInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
createInfo.queueFamilyIndex = queueFamily; createInfo.queueFamilyIndex = mQueueFamily;
if (fn.CreateCommandPool(vkDevice, &createInfo, nullptr, &commands.pool) != VK_SUCCESS) { if (fn.CreateCommandPool(mVkDevice, &createInfo, nullptr, &commands.pool) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
@ -536,7 +536,7 @@ namespace vulkan {
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandBufferCount = 1; allocateInfo.commandBufferCount = 1;
if (fn.AllocateCommandBuffers(vkDevice, &allocateInfo, &commands.commandBuffer) != VK_SUCCESS) { if (fn.AllocateCommandBuffers(mVkDevice, &allocateInfo, &commands.commandBuffer) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
@ -544,18 +544,18 @@ namespace vulkan {
} }
void Device::RecycleCompletedCommands() { void Device::RecycleCompletedCommands() {
for (auto& commands : commandsInFlight.IterateUpTo(completedSerial)) { for (auto& commands : mCommandsInFlight.IterateUpTo(mCompletedSerial)) {
if (fn.ResetCommandPool(vkDevice, commands.pool, 0) != VK_SUCCESS) { if (fn.ResetCommandPool(mVkDevice, commands.pool, 0) != VK_SUCCESS) {
ASSERT(false); ASSERT(false);
} }
unusedCommands.push_back(commands); mUnusedCommands.push_back(commands);
} }
commandsInFlight.ClearUpTo(completedSerial); mCommandsInFlight.ClearUpTo(mCompletedSerial);
} }
void Device::FreeCommands(CommandPoolAndBuffer* commands) { void Device::FreeCommands(CommandPoolAndBuffer* commands) {
if (commands->pool != VK_NULL_HANDLE) { if (commands->pool != VK_NULL_HANDLE) {
fn.DestroyCommandPool(vkDevice, commands->pool, nullptr); fn.DestroyCommandPool(mVkDevice, commands->pool, nullptr);
commands->pool = VK_NULL_HANDLE; commands->pool = VK_NULL_HANDLE;
} }

View File

@ -142,8 +142,6 @@ namespace vulkan {
VkInstance GetInstance() const; VkInstance GetInstance() const;
VkDevice GetVkDevice() const; VkDevice GetVkDevice() const;
void FakeSubmit();
private: private:
bool CreateInstance(VulkanGlobalKnobs* usedKnobs); bool CreateInstance(VulkanGlobalKnobs* usedKnobs);
bool CreateDevice(VulkanDeviceKnobs* usedKnobs); bool CreateDevice(VulkanDeviceKnobs* usedKnobs);
@ -163,21 +161,21 @@ namespace vulkan {
// the Device is allowed to mutate them through these private methods. // the Device is allowed to mutate them through these private methods.
VulkanFunctions* GetMutableFunctions(); VulkanFunctions* GetMutableFunctions();
VulkanGlobalInfo globalInfo; VulkanGlobalInfo mGlobalInfo;
VulkanDeviceInfo deviceInfo; VulkanDeviceInfo mDeviceInfo;
DynamicLib vulkanLib; DynamicLib mVulkanLib;
VkInstance instance = VK_NULL_HANDLE; VkInstance mInstance = VK_NULL_HANDLE;
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
VkDevice vkDevice = VK_NULL_HANDLE; VkDevice mVkDevice = VK_NULL_HANDLE;
uint32_t queueFamily = 0; uint32_t mQueueFamily = 0;
VkQueue queue = VK_NULL_HANDLE; VkQueue mQueue = VK_NULL_HANDLE;
VkDebugReportCallbackEXT debugReportCallback = VK_NULL_HANDLE; VkDebugReportCallbackEXT mDebugReportCallback = VK_NULL_HANDLE;
MapReadRequestTracker* mapReadRequestTracker = nullptr; MapReadRequestTracker* mMapReadRequestTracker = nullptr;
MemoryAllocator* memoryAllocator = nullptr; MemoryAllocator* mMemoryAllocator = nullptr;
BufferUploader* bufferUploader = nullptr; BufferUploader* mBufferUploader = nullptr;
VkFence GetUnusedFence(); VkFence GetUnusedFence();
void CheckPassedFences(); void CheckPassedFences();
@ -186,10 +184,10 @@ namespace vulkan {
// This works only because we have a single queue. Each submit to a queue is associated // This works only because we have a single queue. Each submit to a queue is associated
// to a serial and a fence, such that when the fence is "ready" we know the operations // to a serial and a fence, such that when the fence is "ready" we know the operations
// have finished. // have finished.
std::queue<std::pair<VkFence, Serial>> fencesInFlight; std::queue<std::pair<VkFence, Serial>> mFencesInFlight;
std::vector<VkFence> unusedFences; std::vector<VkFence> mUnusedFences;
Serial nextSerial = 1; Serial mNextSerial = 1;
Serial completedSerial = 0; Serial mCompletedSerial = 0;
struct CommandPoolAndBuffer { struct CommandPoolAndBuffer {
VkCommandPool pool = VK_NULL_HANDLE; VkCommandPool pool = VK_NULL_HANDLE;
@ -200,9 +198,9 @@ namespace vulkan {
void RecycleCompletedCommands(); void RecycleCompletedCommands();
void FreeCommands(CommandPoolAndBuffer* commands); void FreeCommands(CommandPoolAndBuffer* commands);
SerialQueue<CommandPoolAndBuffer> commandsInFlight; SerialQueue<CommandPoolAndBuffer> mCommandsInFlight;
std::vector<CommandPoolAndBuffer> unusedCommands; std::vector<CommandPoolAndBuffer> mUnusedCommands;
CommandPoolAndBuffer pendingCommands; CommandPoolAndBuffer mPendingCommands;
}; };
class Queue : public QueueBase { class Queue : public QueueBase {