-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move VulkanPushBuffer into VulkanMemory.h
- Loading branch information
Showing
11 changed files
with
166 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2016- PPSSPP Project. | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, version 2.0 or later versions. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License 2.0 for more details. | ||
|
||
// A copy of the GPL 2.0 should have been included with the program. | ||
// If not, see http://www.gnu.org/licenses/ | ||
|
||
// Official git repository and contact information can be found at | ||
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. | ||
|
||
// Additionally, Common/Vulkan/* , including this file, are also licensed | ||
// under the public domain. | ||
|
||
#include "Common/Vulkan/VulkanMemory.h" | ||
|
||
VulkanPushBuffer::VulkanPushBuffer(VulkanContext *vulkan, size_t size) : offset_(0), size_(size), writePtr_(nullptr), deviceMemory_(0) { | ||
VkDevice device = vulkan->GetDevice(); | ||
|
||
VkBufferCreateInfo b = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; | ||
b.size = size; | ||
b.flags = 0; | ||
b.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT; | ||
b.sharingMode = VK_SHARING_MODE_EXCLUSIVE; | ||
b.queueFamilyIndexCount = 0; | ||
b.pQueueFamilyIndices = nullptr; | ||
VkResult res = vkCreateBuffer(device, &b, nullptr, &buffer_); | ||
assert(VK_SUCCESS == res); | ||
|
||
// Okay, that's the buffer. Now let's allocate some memory for it. | ||
VkMemoryAllocateInfo alloc = {}; | ||
alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; | ||
alloc.pNext = nullptr; | ||
vulkan->MemoryTypeFromProperties(0xFFFFFFFF, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &alloc.memoryTypeIndex); | ||
alloc.allocationSize = size; | ||
|
||
res = vkAllocateMemory(device, &alloc, nullptr, &deviceMemory_); | ||
assert(VK_SUCCESS == res); | ||
res = vkBindBufferMemory(device, buffer_, deviceMemory_, 0); | ||
assert(VK_SUCCESS == res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#pragma once | ||
|
||
#include "Common/Vulkan/VulkanContext.h" | ||
|
||
// VulkanMemory | ||
// | ||
// Vulkan memory management utils. | ||
|
||
// VulkanPushBuffer | ||
// Simple incrementing allocator. | ||
// Use these to push vertex, index and uniform data. Generally you'll have two of these | ||
// and alternate on each frame. Make sure not to reset until the fence from the last time you used it | ||
// has completed. | ||
// | ||
// TODO: Make it possible to suballocate pushbuffers from a large DeviceMemory block. | ||
// TODO: Make this auto-grow and shrink. Need to be careful about returning and using the new | ||
// buffer handle on overflow. | ||
class VulkanPushBuffer { | ||
public: | ||
VulkanPushBuffer(VulkanContext *vulkan, size_t size); | ||
|
||
~VulkanPushBuffer() { | ||
assert(buffer_ == VK_NULL_HANDLE); | ||
assert(deviceMemory_ == VK_NULL_HANDLE); | ||
} | ||
|
||
void Destroy(VulkanContext *vulkan) { | ||
vulkan->Delete().QueueDeleteBuffer(buffer_); | ||
vulkan->Delete().QueueDeleteDeviceMemory(deviceMemory_); | ||
buffer_ = VK_NULL_HANDLE; | ||
deviceMemory_ = VK_NULL_HANDLE; | ||
} | ||
|
||
void Reset() { offset_ = 0; } | ||
|
||
void Begin(VkDevice device) { | ||
offset_ = 0; | ||
VkResult res = vkMapMemory(device, deviceMemory_, 0, size_, 0, (void **)(&writePtr_)); | ||
assert(VK_SUCCESS == res); | ||
} | ||
|
||
void End(VkDevice device) { | ||
vkUnmapMemory(device, deviceMemory_); | ||
writePtr_ = nullptr; | ||
} | ||
|
||
size_t Allocate(size_t numBytes) { | ||
size_t out = offset_; | ||
offset_ += (numBytes + 3) & ~3; // Round up to 4 bytes. | ||
if (offset_ >= size_) { | ||
// TODO: Allocate a second buffer, then combine them on the next frame. | ||
#ifdef _WIN32 | ||
DebugBreak(); | ||
#endif | ||
} | ||
return out; | ||
} | ||
|
||
// TODO: Add alignment support? | ||
// Returns the offset that should be used when binding this buffer to get this data. | ||
size_t Push(const void *data, size_t size) { | ||
size_t off = Allocate(size); | ||
memcpy(writePtr_ + off, data, size); | ||
return off; | ||
} | ||
|
||
uint32_t PushAligned(const void *data, size_t size, int align) { | ||
offset_ = (offset_ + align - 1) & ~(align - 1); | ||
size_t off = Allocate(size); | ||
memcpy(writePtr_ + off, data, size); | ||
return (uint32_t)off; | ||
} | ||
|
||
size_t GetOffset() const { | ||
return offset_; | ||
} | ||
|
||
// "Zero-copy" variant - you can write the data directly as you compute it. | ||
void *Push(size_t size, size_t *bindOffset) { | ||
size_t off = Allocate(size); | ||
*bindOffset = off; | ||
return writePtr_ + off; | ||
} | ||
|
||
VkBuffer GetVkBuffer() const { return buffer_; } | ||
|
||
private: | ||
VkDeviceMemory deviceMemory_; | ||
VkBuffer buffer_; | ||
size_t offset_; | ||
size_t size_; | ||
uint8_t *writePtr_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters