Fixing vulkan

This commit is contained in:
Dynamitos
2024-04-23 08:11:44 +02:00
parent 8ea9c34ed4
commit 89571e5298
33 changed files with 217 additions and 926 deletions
+2 -1
View File
@@ -67,6 +67,7 @@ public:
bool operator<(PDescriptorSet other);
constexpr PDescriptorLayout getLayout() const { return layout; }
constexpr const std::string& getName() const { return layout->getName(); }
protected:
PDescriptorLayout layout;
@@ -83,7 +84,7 @@ public:
void addPushConstants(const SePushConstantRange& pushConstants);
constexpr uint32 getHash() const { return layoutHash; }
constexpr const Map<std::string, PDescriptorLayout>& getLayouts() const { return descriptorSetLayouts; }
constexpr uint32 findParameter(const std::string& name) const { return parameterMapping[name]; }
constexpr uint32 findParameter(const std::string& param) const { return parameterMapping[param]; }
void addMapping(Map<std::string, uint32> mapping);
constexpr std::string getName() const {return name;};
+1 -1
View File
@@ -118,9 +118,9 @@ struct ShaderBufferCreateInfo
DECLARE_NAME_REF(Gfx, PipelineLayout)
struct ShaderCreateInfo
{
std::string name; // Debug info
std::string mainModule;
Array<std::string> additionalModules;
std::string name; // Debug info
std::string entryPoint;
Array<Pair<const char*, const char*>> typeParameter;
Map<const char*, const char*> defines;
+1 -1
View File
@@ -124,9 +124,9 @@ void DebugPass::createRenderPass()
pipelineLayout->create();
ShaderCreateInfo createInfo = {
.name = "DebugVertex",
.mainModule = "Debug",
.additionalModules = { "Debug" },
.name = "DebugVertex",
.entryPoint = "vertexMain",
};
vertexShader = graphics->createVertexShader(createInfo);
@@ -133,8 +133,8 @@ void LightCullingPass::publishOutputs()
ShaderCreateInfo createInfo = {
.name = "Culling",
.additionalModules = {"LightCulling"},
.mainModule = "LightCulling",
.additionalModules = {"LightCulling"},
.entryPoint = "cullLights",
.rootSignature = cullingLayout,
};
@@ -226,8 +226,8 @@ void LightCullingPass::setupFrustums()
frustumLayout->create();
ShaderCreateInfo createInfo = {
.name = "Frustum",
.additionalModules = {"ComputeFrustums"},
.mainModule = "ComputeFrustums",
.additionalModules = {"ComputeFrustums"},
.entryPoint = "computeFrustums",
.rootSignature = frustumLayout,
};
@@ -111,9 +111,9 @@ void SkyboxRenderPass::createRenderPass()
});
ShaderCreateInfo createInfo = {
.name = "SkyboxVertex",
.mainModule = "Skybox",
.additionalModules = {"Skybox"},
.name = "SkyboxVertex",
.entryPoint = "vertexMain",
};
vertexShader = graphics->createVertexShader(createInfo);
+2 -2
View File
@@ -139,7 +139,7 @@ void UIPass::createRenderPass()
descriptorSet->updateSampler(1, backgroundSampler);
descriptorSet->writeChanges();
Gfx::OPipelineLayout pipelineLayout = graphics->createPipelineLayout();
pipelineLayout = graphics->createPipelineLayout();
pipelineLayout->addDescriptorLayout(descriptorLayout);
pipelineLayout->create();
@@ -153,7 +153,7 @@ void UIPass::createRenderPass()
pipelineInfo.vertexShader = vertexShader;
pipelineInfo.fragmentShader = fragmentShader;
pipelineInfo.renderPass = renderPass;
pipelineInfo.pipelineLayout = std::move(pipelineLayout);
pipelineInfo.pipelineLayout = pipelineLayout;
pipelineInfo.rasterizationState.cullMode = Gfx::SE_CULL_MODE_NONE;
pipelineInfo.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
+103 -170
View File
@@ -13,57 +13,23 @@ struct PendingBuffer {
bool writeOnly;
};
static Map<Vulkan::Buffer *, PendingBuffer> pendingBuffers;
static Map<Vulkan::Buffer*, PendingBuffer> pendingBuffers;
Buffer::Buffer(PGraphics graphics, uint64 size, VkBufferUsageFlags usage,
Gfx::QueueType &queueType, bool dynamic, std::string name)
: graphics(graphics), currentBuffer(0), size(size), owner(queueType),
name(name) {
Buffer::Buffer(PGraphics graphics, uint64 size, VkBufferUsageFlags usage, Gfx::QueueType& queueType, bool dynamic,
std::string name)
: graphics(graphics), currentBuffer(0), size(size), owner(queueType), usage(usage), name(name) {
if (dynamic) {
numBuffers = Gfx::numFramesBuffered;
} else {
numBuffers = 1;
}
VkBufferCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.size = size,
.usage = usage,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VmaAllocationCreateInfo allocInfo = {
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT |
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
};
for (uint32 i = 0; i < numBuffers; ++i) {
vmaCreateBuffer(graphics->getAllocator(), &info, &allocInfo,
&buffers[i].buffer, &buffers[i].allocation,
&buffers[i].info);
vmaGetAllocationMemoryProperties(graphics->getAllocator(),
buffers[i].allocation,
&buffers[i].properties);
// std::cout << "Create buffer " << std::hex << (uint64)buffers[i].buffer <<
// std::dec;
if (!name.empty()) {
VkDebugUtilsObjectNameInfoEXT nameInfo = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
.pNext = nullptr,
.objectType = VK_OBJECT_TYPE_BUFFER,
.objectHandle = (uint64)buffers[i].buffer,
.pObjectName = this->name.c_str()};
graphics->vkSetDebugUtilsObjectNameEXT(&nameInfo);
// std::cout << ": " << name;
}
// std::cout << std::endl;
}
createBuffer();
}
Buffer::~Buffer() {
for (uint32 i = 0; i < numBuffers; ++i) {
graphics->getDestructionManager()->queueBuffer(
graphics->getQueueCommands(owner)->getCommands(), buffers[i].buffer,
buffers[i].allocation);
graphics->getDestructionManager()->queueBuffer(graphics->getQueueCommands(owner)->getCommands(), buffers[i].buffer,
buffers[i].allocation);
}
}
@@ -112,16 +78,12 @@ void Buffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
dynamicBarriers[i] = barrier;
dynamicBarriers[i].buffer = buffers[i].buffer;
}
vkCmdPipelineBarrier(srcCommand, srcStage, srcStage, 0, 0, nullptr,
numBuffers, dynamicBarriers, 0, nullptr);
vkCmdPipelineBarrier(dstCommand, dstStage, dstStage, 0, 0, nullptr,
numBuffers, dynamicBarriers, 0, nullptr);
vkCmdPipelineBarrier(srcCommand, srcStage, srcStage, 0, 0, nullptr, numBuffers, dynamicBarriers, 0, nullptr);
vkCmdPipelineBarrier(dstCommand, dstStage, dstStage, 0, 0, nullptr, numBuffers, dynamicBarriers, 0, nullptr);
sourcePool->submitCommands();
}
void Buffer::executePipelineBarrier(VkAccessFlags srcAccess,
VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess,
void Buffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, VkAccessFlags dstAccess,
VkPipelineStageFlags dstStage) {
PCommand commandBuffer = graphics->getQueueCommands(owner)->getCommands();
VkBufferMemoryBarrier barrier = {
@@ -139,15 +101,15 @@ void Buffer::executePipelineBarrier(VkAccessFlags srcAccess,
dynamicBarriers[i] = barrier;
dynamicBarriers[i].buffer = buffers[i].buffer;
}
vkCmdPipelineBarrier(commandBuffer->getHandle(), srcStage, dstStage, 0, 0,
nullptr, numBuffers, dynamicBarriers, 0, nullptr);
vkCmdPipelineBarrier(commandBuffer->getHandle(), srcStage, dstStage, 0, 0, nullptr, numBuffers, dynamicBarriers, 0,
nullptr);
}
void *Buffer::map(bool writeOnly) { return mapRegion(0, size, writeOnly); }
void *Buffer::mapRegion(uint64 regionOffset, uint64 regionSize,
bool writeOnly) {
void *data = nullptr;
void* Buffer::map(bool writeOnly) { return mapRegion(0, size, writeOnly); }
void* Buffer::mapRegion(uint64 regionOffset, uint64 regionSize, bool writeOnly) {
void* data = nullptr;
PendingBuffer pending;
pending.writeOnly = writeOnly;
@@ -155,10 +117,8 @@ void *Buffer::mapRegion(uint64 regionOffset, uint64 regionSize,
pending.offset = regionOffset;
pending.size = regionSize;
if (writeOnly) {
if (buffers[currentBuffer].properties &
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
VK_CHECK(vmaMapMemory(graphics->getAllocator(),
buffers[currentBuffer].allocation, &data));
if (buffers[currentBuffer].properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
VK_CHECK(vmaMapMemory(graphics->getAllocator(), buffers[currentBuffer].allocation, &data));
} else {
VkBufferCreateInfo stagingInfo = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
@@ -172,8 +132,7 @@ void *Buffer::mapRegion(uint64 regionOffset, uint64 regionSize,
.usage = VMA_MEMORY_USAGE_AUTO,
.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
};
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo,
&allocInfo, &pending.stagingBuffer,
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &allocInfo, &pending.stagingBuffer,
&pending.allocation, nullptr));
vmaMapMemory(graphics->getAllocator(), pending.allocation, &data);
}
@@ -189,15 +148,12 @@ void *Buffer::mapRegion(uint64 regionOffset, uint64 regionSize,
void Buffer::unmap() {
auto found = pendingBuffers.find(this);
if (found != pendingBuffers.end()) {
PendingBuffer &pending = found->value;
PendingBuffer& pending = found->value;
if (pending.writeOnly) {
if (buffers[currentBuffer].properties &
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
vmaUnmapMemory(graphics->getAllocator(),
buffers[currentBuffer].allocation);
if (buffers[currentBuffer].properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
vmaUnmapMemory(graphics->getAllocator(), buffers[currentBuffer].allocation);
} else {
vmaFlushAllocation(graphics->getAllocator(), pending.allocation, 0,
VK_WHOLE_SIZE);
vmaFlushAllocation(graphics->getAllocator(), pending.allocation, 0, VK_WHOLE_SIZE);
vmaUnmapMemory(graphics->getAllocator(), pending.allocation);
PCommand command = graphics->getQueueCommands(owner)->getCommands();
VkCommandBuffer cmdHandle = command->getHandle();
@@ -218,11 +174,9 @@ void Buffer::unmap() {
.offset = 0,
.size = size,
};
vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 1,
&barrier, 0, nullptr);
vkCmdCopyBuffer(cmdHandle, pending.stagingBuffer,
buffers[currentBuffer].buffer, 1, &region);
vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0,
nullptr, 1, &barrier, 0, nullptr);
vkCmdCopyBuffer(cmdHandle, pending.stagingBuffer, buffers[currentBuffer].buffer, 1, &region);
barrier = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.pNext = nullptr,
@@ -234,27 +188,49 @@ void Buffer::unmap() {
.offset = 0,
.size = size,
};
vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr,
1, &barrier, 0, nullptr);
graphics->getDestructionManager()->queueBuffer(
command, pending.stagingBuffer, pending.allocation);
vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0,
nullptr, 1, &barrier, 0, nullptr);
graphics->getDestructionManager()->queueBuffer(command, pending.stagingBuffer, pending.allocation);
}
}
// requestOwnershipTransfer(pending.prevQueue);
pendingBuffers.erase(this);
}
}
UniformBuffer::UniformBuffer(PGraphics graphics,
const UniformBufferCreateInfo &createInfo)
void Buffer::createBuffer()
{
VkBufferCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.size = size,
.usage = usage,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VmaAllocationCreateInfo allocInfo = {
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT |
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
};
buffers.add();
vmaCreateBuffer(graphics->getAllocator(), &info, &allocInfo, &buffers.back().buffer, &buffers.back().allocation,
&buffers.back().info);
vmaGetAllocationMemoryProperties(graphics->getAllocator(), buffers.back().allocation, &buffers.back().properties);
if (!name.empty()) {
VkDebugUtilsObjectNameInfoEXT nameInfo = { .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
.pNext = nullptr,
.objectType = VK_OBJECT_TYPE_BUFFER,
.objectHandle = (uint64)buffers.back().buffer,
.pObjectName = this->name.c_str() };
graphics->vkSetDebugUtilsObjectNameEXT(&nameInfo);
}
}
UniformBuffer::UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo& createInfo)
: Gfx::UniformBuffer(graphics->getFamilyMapping(), createInfo.sourceData),
Vulkan::Buffer(graphics, createInfo.sourceData.size,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, currentOwner,
Vulkan::Buffer(graphics, createInfo.sourceData.size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, currentOwner,
createInfo.dynamic, createInfo.name) {
if (createInfo.sourceData.data != nullptr) {
void *data = map();
void* data = map();
std::memcpy(data, createInfo.sourceData.data, createInfo.sourceData.size);
unmap();
}
@@ -262,19 +238,17 @@ UniformBuffer::UniformBuffer(PGraphics graphics,
UniformBuffer::~UniformBuffer() {}
bool UniformBuffer::updateContents(const DataSource &sourceData) {
bool UniformBuffer::updateContents(const DataSource& sourceData) {
if (!Gfx::UniformBuffer::updateContents(sourceData)) {
// no update was performed, skip
return false;
}
void *data = map();
void* data = map();
std::memcpy(data, sourceData.data, sourceData.size);
unmap();
return true;
}
void UniformBuffer::beginFrame() { Vulkan::Buffer::advanceBuffer(); }
void UniformBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) {
Gfx::QueueOwnedResource::transferOwnership(newOwner);
}
@@ -283,31 +257,21 @@ void UniformBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
}
void UniformBuffer::executePipelineBarrier(VkAccessFlags srcAccess,
VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess,
VkPipelineStageFlags dstStage) {
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess,
dstStage);
void UniformBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
}
VkAccessFlags UniformBuffer::getSourceAccessMask() {
return VK_ACCESS_MEMORY_WRITE_BIT;
}
VkAccessFlags UniformBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; }
VkAccessFlags UniformBuffer::getDestAccessMask() {
return VK_ACCESS_UNIFORM_READ_BIT;
}
VkAccessFlags UniformBuffer::getDestAccessMask() { return VK_ACCESS_UNIFORM_READ_BIT; }
ShaderBuffer::ShaderBuffer(PGraphics graphics,
const ShaderBufferCreateInfo &sourceData)
: Gfx::ShaderBuffer(graphics->getFamilyMapping(), sourceData.numElements,
sourceData.sourceData),
Vulkan::Buffer(graphics, sourceData.sourceData.size,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, currentOwner,
ShaderBuffer::ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo& sourceData)
: Gfx::ShaderBuffer(graphics->getFamilyMapping(), sourceData.numElements, sourceData.sourceData),
Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, currentOwner,
sourceData.dynamic, sourceData.name) {
if (sourceData.sourceData.data != nullptr) {
void *data = map();
void* data = map();
std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size);
unmap();
}
@@ -315,18 +279,16 @@ ShaderBuffer::ShaderBuffer(PGraphics graphics,
ShaderBuffer::~ShaderBuffer() {}
bool ShaderBuffer::updateContents(const DataSource &sourceData) {
bool ShaderBuffer::updateContents(const DataSource& sourceData) {
assert(sourceData.size <= getSize());
Gfx::ShaderBuffer::updateContents(sourceData);
// We always want to update, as the contents could be different on the GPU
void *data = map();
void* data = map();
std::memcpy(data, sourceData.data, sourceData.size);
unmap();
return true;
}
void ShaderBuffer::beginFrame() { Vulkan::Buffer::advanceBuffer(); }
void ShaderBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) {
Gfx::QueueOwnedResource::transferOwnership(newOwner);
}
@@ -335,31 +297,22 @@ void ShaderBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
}
void ShaderBuffer::executePipelineBarrier(VkAccessFlags srcAccess,
VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess,
VkPipelineStageFlags dstStage) {
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess,
dstStage);
void ShaderBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
}
VkAccessFlags ShaderBuffer::getSourceAccessMask() {
return VK_ACCESS_MEMORY_WRITE_BIT;
}
VkAccessFlags ShaderBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; }
VkAccessFlags ShaderBuffer::getDestAccessMask() {
return VK_ACCESS_MEMORY_READ_BIT;
}
VkAccessFlags ShaderBuffer::getDestAccessMask() { return VK_ACCESS_MEMORY_READ_BIT; }
VertexBuffer::VertexBuffer(PGraphics graphics,
const VertexBufferCreateInfo &sourceData)
: Gfx::VertexBuffer(graphics->getFamilyMapping(), sourceData.numVertices,
sourceData.vertexSize, sourceData.sourceData.owner),
Vulkan::Buffer(graphics, sourceData.sourceData.size,
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, currentOwner, false,
VertexBuffer::VertexBuffer(PGraphics graphics, const VertexBufferCreateInfo& sourceData)
: Gfx::VertexBuffer(graphics->getFamilyMapping(), sourceData.numVertices, sourceData.vertexSize,
sourceData.sourceData.owner),
Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, currentOwner, false,
sourceData.name) {
if (sourceData.sourceData.data != nullptr) {
void *data = map();
void* data = map();
std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size);
unmap();
}
@@ -368,20 +321,18 @@ VertexBuffer::VertexBuffer(PGraphics graphics,
VertexBuffer::~VertexBuffer() {}
void VertexBuffer::updateRegion(DataSource update) {
void *data = mapRegion(update.offset, update.size);
void* data = mapRegion(update.offset, update.size);
std::memcpy(data, update.data, update.size);
unmap();
}
void VertexBuffer::download(Array<uint8> &buffer) {
void *data = map(false);
void VertexBuffer::download(Array<uint8>& buffer) {
void* data = map(false);
buffer.resize(size);
std::memcpy(buffer.data(), data, size);
unmap();
}
void VertexBuffer::beginFrame() { Vulkan::Buffer::advanceBuffer(); }
void VertexBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) {
Gfx::QueueOwnedResource::transferOwnership(newOwner);
}
@@ -390,31 +341,22 @@ void VertexBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
}
void VertexBuffer::executePipelineBarrier(VkAccessFlags srcAccess,
VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess,
VkPipelineStageFlags dstStage) {
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess,
dstStage);
void VertexBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
}
VkAccessFlags VertexBuffer::getSourceAccessMask() {
return VK_ACCESS_MEMORY_WRITE_BIT;
}
VkAccessFlags VertexBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; }
VkAccessFlags VertexBuffer::getDestAccessMask() {
return VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
}
VkAccessFlags VertexBuffer::getDestAccessMask() { return VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; }
IndexBuffer::IndexBuffer(PGraphics graphics,
const IndexBufferCreateInfo &sourceData)
: Gfx::IndexBuffer(graphics->getFamilyMapping(), sourceData.sourceData.size,
sourceData.indexType, sourceData.sourceData.owner),
Vulkan::Buffer(graphics, sourceData.sourceData.size,
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, currentOwner, false,
IndexBuffer::IndexBuffer(PGraphics graphics, const IndexBufferCreateInfo& sourceData)
: Gfx::IndexBuffer(graphics->getFamilyMapping(), sourceData.sourceData.size, sourceData.indexType,
sourceData.sourceData.owner),
Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, currentOwner, false,
sourceData.name) {
if (sourceData.sourceData.data != nullptr) {
void *data = map();
void* data = map();
std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size);
unmap();
}
@@ -422,15 +364,13 @@ IndexBuffer::IndexBuffer(PGraphics graphics,
IndexBuffer::~IndexBuffer() {}
void IndexBuffer::download(Array<uint8> &buffer) {
void *data = map(false);
void IndexBuffer::download(Array<uint8>& buffer) {
void* data = map(false);
buffer.resize(size);
std::memcpy(buffer.data(), data, size);
unmap();
}
void IndexBuffer::beginFrame() { Vulkan::Buffer::advanceBuffer(); }
void IndexBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) {
Gfx::QueueOwnedResource::transferOwnership(newOwner);
}
@@ -439,18 +379,11 @@ void IndexBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
}
void IndexBuffer::executePipelineBarrier(VkAccessFlags srcAccess,
VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess,
VkPipelineStageFlags dstStage) {
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess,
dstStage);
void IndexBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
}
VkAccessFlags IndexBuffer::getSourceAccessMask() {
return VK_ACCESS_MEMORY_WRITE_BIT;
}
VkAccessFlags IndexBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; }
VkAccessFlags IndexBuffer::getDestAccessMask() {
return VK_ACCESS_INDEX_READ_BIT;
}
VkAccessFlags IndexBuffer::getDestAccessMask() { return VK_ACCESS_INDEX_READ_BIT; }
+4
View File
@@ -4,6 +4,8 @@
namespace Seele {
namespace Vulkan {
DECLARE_REF(Command)
DECLARE_REF(Fence)
class Buffer {
public:
Buffer(PGraphics graphics, uint64 size, VkBufferUsageFlags usage,
@@ -28,8 +30,10 @@ protected:
uint64 size;
Gfx::QueueType &owner;
Array<BufferAllocation> buffers;
VkBufferUsageFlags usage;
uint32 numBuffers;
std::string name;
void createBuffer();
void executeOwnershipBarrier(Gfx::QueueType newOwner);
void executePipelineBarrier(VkAccessFlags srcAccess,
+10 -10
View File
@@ -279,7 +279,7 @@ void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet)
descriptor->bind();
VkDescriptorSet setHandle = descriptor->getHandle();
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getLayout(), descriptorSet->getSetIndex(), 1, &setHandle, 0, nullptr);
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getLayout(), pipeline->getPipelineLayout()->findParameter(descriptorSet->getName()), 1, &setHandle, 0, nullptr);
}
void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets)
{
@@ -292,7 +292,7 @@ void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorS
descriptorSet->bind();
boundDescriptors.add(descriptorSet.getHandle());
sets[descriptorSet->getSetIndex()] = descriptorSet->getHandle();
sets[pipeline->getPipelineLayout()->findParameter(descriptorSet->getName())] = descriptorSet->getHandle();
}
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getLayout(), 0, (uint32)descriptorSets.size(), sets, 0, nullptr);
delete[] sets;
@@ -416,7 +416,7 @@ void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet)
descriptor->bind();
VkDescriptorSet setHandle = descriptor->getHandle();
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->getLayout(), descriptorSet->getSetIndex(), 1, &setHandle, 0, nullptr);
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->getLayout(), pipeline->getPipelineLayout()->findParameter(descriptorSet->getName()), 1, &setHandle, 0, nullptr);
}
void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets)
@@ -431,7 +431,7 @@ void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptor
//std::cout << "Binding descriptor " << descriptorSet->getHandle() << " to cmd " << handle << std::endl;
boundDescriptors.add(descriptorSet.getHandle());
sets[descriptorSet->getSetIndex()] = descriptorSet->getHandle();
sets[pipeline->getPipelineLayout()->findParameter(descriptorSet->getName())] = descriptorSet->getHandle();
}
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->getLayout(), 0, (uint32)descriptorSets.size(), sets, 0, nullptr);
delete[] sets;
@@ -469,9 +469,9 @@ CommandPool::CommandPool(PGraphics graphics, PQueue queue)
CommandPool::~CommandPool()
{
vkDeviceWaitIdle(graphics->getDevice());
for (auto& command : allocatedBuffers)
for (auto& cmd : allocatedBuffers)
{
command->checkFence();
cmd->checkFence();
}
allocatedRenderCommands.clear();
allocatedComputeCommands.clear();
@@ -487,17 +487,17 @@ PCommand CommandPool::getCommands()
}
void CommandPool::cacheCommands(Array<ORenderCommand> commands)
{
for(auto&& command : commands)
for(auto&& cmd : commands)
{
allocatedRenderCommands.add(std::move(command));
allocatedRenderCommands.add(std::move(cmd));
}
}
void CommandPool::cacheCommands(Array<OComputeCommand> commands)
{
for(auto&& command : commands)
for(auto&& cmd : commands)
{
allocatedComputeCommands.add(std::move(command));
allocatedComputeCommands.add(std::move(cmd));
}
}
+12 -19
View File
@@ -73,18 +73,13 @@ public:
virtual void setViewport(Gfx::PViewport viewport) override;
virtual void bindPipeline(Gfx::PGraphicsPipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet descriptorSet) override;
virtual void
bindDescriptor(const Array<Gfx::PDescriptorSet> &descriptorSets) override;
virtual void
bindVertexBuffer(const Array<Gfx::PVertexBuffer> &buffers) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets) override;
virtual void bindVertexBuffer(const Array<Gfx::PVertexBuffer>& buffers) override;
virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) override;
virtual void pushConstants(Gfx::PPipelineLayout layout,
Gfx::SeShaderStageFlags stage, uint32 offset,
uint32 size, const void *data) override;
virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex,
uint32 firstInstance) override;
virtual void drawIndexed(uint32 indexCount, uint32 instanceCount,
int32 firstIndex, uint32 vertexOffset,
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size,
const void* data) override;
virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) override;
virtual void drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset,
uint32 firstInstance) override;
virtual void drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) override;
@@ -113,12 +108,10 @@ public:
bool isReady();
virtual void bindPipeline(Gfx::PComputePipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet set) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet> &sets) override;
virtual void pushConstants(Gfx::PPipelineLayout layout,
Gfx::SeShaderStageFlags stage, uint32 offset,
uint32 size, const void *data) override;
virtual void dispatch(uint32 threadX, uint32 threadY,
uint32 threadZ) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets) override;
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size,
const void* data) override;
virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) override;
private:
PComputePipeline pipeline;
@@ -141,8 +134,8 @@ public:
PCommand getCommands();
void cacheCommands(Array<ORenderCommand> commands);
void cacheCommands(Array<OComputeCommand> commands);
ORenderCommand createRenderCommand(const std::string &name);
OComputeCommand createComputeCommand(const std::string &name);
ORenderCommand createRenderCommand(const std::string& name);
OComputeCommand createComputeCommand(const std::string& name);
constexpr VkCommandPool getHandle() const { return commandPool; }
void submitCommands(PSemaphore signalSemaphore = nullptr);
+6 -10
View File
@@ -161,6 +161,7 @@ void DescriptorPool::reset() {
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner)
: Gfx::DescriptorSet(owner->getLayout()), setHandle(VK_NULL_HANDLE), graphics(graphics), owner(owner), bindCount(0),
currentlyInUse(false) {}
DescriptorSet::~DescriptorSet() {}
void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBuffer) {
@@ -315,8 +316,8 @@ void DescriptorSet::writeChanges() {
}
}
PipelineLayout::PipelineLayout(PGraphics graphics, Gfx::PPipelineLayout baseLayout)
: Gfx::PipelineLayout(baseLayout), graphics(graphics), layoutHandle(VK_NULL_HANDLE) {}
PipelineLayout::PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout)
: Gfx::PipelineLayout(name, baseLayout), graphics(graphics), layoutHandle(VK_NULL_HANDLE) {}
PipelineLayout::~PipelineLayout() {}
@@ -324,15 +325,10 @@ Map<uint32, VkPipelineLayout> cachedLayouts;
void PipelineLayout::create() {
vulkanDescriptorLayouts.resize(descriptorSetLayouts.size());
for (size_t i = 0; i < descriptorSetLayouts.size(); ++i) {
// There could be unused descriptor set indices
if (descriptorSetLayouts[i] == nullptr) {
vulkanDescriptorLayouts[i] = VK_NULL_HANDLE;
continue;
}
PDescriptorLayout layout = descriptorSetLayouts[i].cast<DescriptorLayout>();
for (auto [name, desc] : descriptorSetLayouts) {
PDescriptorLayout layout = desc.cast<DescriptorLayout>();
layout->create();
vulkanDescriptorLayouts[i] = layout->getHandle();
vulkanDescriptorLayouts[parameterMapping[layout->getName()]] = layout->getHandle();
}
Array<VkPushConstantRange> vkPushConstants(pushConstants.size());
+1 -1
View File
@@ -84,7 +84,7 @@ DEFINE_REF(DescriptorSet)
class PipelineLayout : public Gfx::PipelineLayout {
public:
PipelineLayout(PGraphics graphics, Gfx::PPipelineLayout baseLayout);
PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout);
virtual ~PipelineLayout();
virtual void create();
constexpr VkPipelineLayout getHandle() const { return layoutHandle; }
-36
View File
@@ -102,24 +102,6 @@ VkShaderStageFlagBits Seele::Vulkan::cast(const Seele::Gfx::SeShaderStageFlagBit
return VK_SHADER_STAGE_ALL_GRAPHICS;
case SE_SHADER_STAGE_ALL:
return VK_SHADER_STAGE_ALL;
#ifdef USE_EXTENSIONS
case SE_SHADER_STAGE_RAYGEN_BIT_NV:
return VK_SHADER_STAGE_RAYGEN_BIT_NV;
case SE_SHADER_STAGE_ANY_HIT_BIT_NV:
return VK_SHADER_STAGE_ANY_HIT_BIT_NV;
case SE_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV;
case SE_SHADER_STAGE_MISS_BIT_NV:
return VK_SHADER_STAGE_MISS_BIT_NV;
case SE_SHADER_STAGE_INTERSECTION_BIT_NV:
return VK_SHADER_STAGE_INTERSECTION_BIT_NV;
case SE_SHADER_STAGE_CALLABLE_BIT_NV:
return VK_SHADER_STAGE_CALLABLE_BIT_NV;
case SE_SHADER_STAGE_TASK_BIT_NV:
return VK_SHADER_STAGE_TASK_BIT_NV;
case SE_SHADER_STAGE_MESH_BIT_NV:
return VK_SHADER_STAGE_MESH_BIT_NV;
#endif
default: throw std::logic_error("Not implemented");
}
@@ -145,24 +127,6 @@ Seele::Gfx::SeShaderStageFlagBits Seele::Vulkan::cast(const VkShaderStageFlagBit
return SE_SHADER_STAGE_ALL_GRAPHICS;
case VK_SHADER_STAGE_ALL:
return SE_SHADER_STAGE_ALL;
#ifdef USE_EXTENSIONS
case VK_SHADER_STAGE_RAYGEN_BIT_NV:
return SE_SHADER_STAGE_RAYGEN_BIT_NV;
case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
return SE_SHADER_STAGE_ANY_HIT_BIT_NV;
case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
return SE_SHADER_STAGE_CLOSEST_HIT_BIT_NV;
case VK_SHADER_STAGE_MISS_BIT_NV:
return SE_SHADER_STAGE_MISS_BIT_NV;
case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
return SE_SHADER_STAGE_INTERSECTION_BIT_NV;
case VK_SHADER_STAGE_CALLABLE_BIT_NV:
return SE_SHADER_STAGE_CALLABLE_BIT_NV;
case VK_SHADER_STAGE_TASK_BIT_NV:
return SE_SHADER_STAGE_TASK_BIT_NV;
case VK_SHADER_STAGE_MESH_BIT_NV:
return SE_SHADER_STAGE_MESH_BIT_NV;
#endif
default: throw std::logic_error("Not implemented");
}
+2 -2
View File
@@ -247,9 +247,9 @@ Gfx::ODescriptorLayout Graphics::createDescriptorLayout(const std::string& name)
return new DescriptorLayout(this, name);
}
Gfx::OPipelineLayout Graphics::createPipelineLayout(Gfx::PPipelineLayout baseLayout)
Gfx::OPipelineLayout Graphics::createPipelineLayout(const std::string& name, Gfx::PPipelineLayout baseLayout)
{
return new PipelineLayout(this, baseLayout);
return new PipelineLayout(this, name, baseLayout);
}
Gfx::OVertexInput Graphics::createVertexInput(VertexInputStateCreateInfo createInfo)
+2 -2
View File
@@ -14,7 +14,7 @@ VertexInput::~VertexInput()
{
}
GraphicsPipeline::GraphicsPipeline(PGraphics graphics, VkPipeline handle, Gfx::OPipelineLayout pipelineLayout)
GraphicsPipeline::GraphicsPipeline(PGraphics graphics, VkPipeline handle, Gfx::PPipelineLayout pipelineLayout)
: Gfx::GraphicsPipeline(std::move(pipelineLayout))
, graphics(graphics)
, pipeline(handle)
@@ -35,7 +35,7 @@ VkPipelineLayout GraphicsPipeline::getLayout() const
return Gfx::PPipelineLayout(layout).cast<PipelineLayout>()->getHandle();
}
ComputePipeline::ComputePipeline(PGraphics graphics, VkPipeline handle, Gfx::OPipelineLayout pipelineLayout)
ComputePipeline::ComputePipeline(PGraphics graphics, VkPipeline handle, Gfx::PPipelineLayout pipelineLayout)
: Gfx::ComputePipeline(std::move(pipelineLayout))
, graphics(graphics)
, pipeline(handle)
+2 -2
View File
@@ -18,7 +18,7 @@ DECLARE_REF(Graphics)
class GraphicsPipeline : public Gfx::GraphicsPipeline
{
public:
GraphicsPipeline(PGraphics graphics, VkPipeline handle, Gfx::OPipelineLayout pipelineLayout);
GraphicsPipeline(PGraphics graphics, VkPipeline handle, Gfx::PPipelineLayout pipelineLayout);
virtual ~GraphicsPipeline();
void bind(VkCommandBuffer handle);
VkPipelineLayout getLayout() const;
@@ -30,7 +30,7 @@ DEFINE_REF(GraphicsPipeline)
class ComputePipeline : public Gfx::ComputePipeline
{
public:
ComputePipeline(PGraphics graphics, VkPipeline handle, Gfx::OPipelineLayout pipelineLayout);
ComputePipeline(PGraphics graphics, VkPipeline handle, Gfx::PPipelineLayout pipelineLayout);
virtual ~ComputePipeline();
void bind(VkCommandBuffer handle);
VkPipelineLayout getLayout() const;
+3 -3
View File
@@ -252,7 +252,7 @@ PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo gf
int64 delta = std::chrono::duration_cast<std::chrono::microseconds>(endTime - beginTime).count();
std::cout << "Gfx creation time: " << delta << std::endl;
OGraphicsPipeline pipeline = new GraphicsPipeline(graphics, pipelineHandle, std::move(gfxInfo.pipelineLayout));
OGraphicsPipeline pipeline = new GraphicsPipeline(graphics, pipelineHandle, gfxInfo.pipelineLayout);
PGraphicsPipeline result = pipeline;
graphicsPipelines[hash] = std::move(pipeline);
return result;
@@ -423,7 +423,7 @@ PGraphicsPipeline PipelineCache::createPipeline(Gfx::MeshPipelineCreateInfo gfxI
int64 delta = std::chrono::duration_cast<std::chrono::microseconds>(endTime - beginTime).count();
std::cout << "Gfx creation time: " << delta << std::endl;
OGraphicsPipeline pipeline = new GraphicsPipeline(graphics, pipelineHandle, std::move(gfxInfo.pipelineLayout));
OGraphicsPipeline pipeline = new GraphicsPipeline(graphics, pipelineHandle, gfxInfo.pipelineLayout);
PGraphicsPipeline result = pipeline;
graphicsPipelines[hash] = std::move(pipeline);
return result;
@@ -462,7 +462,7 @@ PComputePipeline PipelineCache::createPipeline(Gfx::ComputePipelineCreateInfo co
int64 delta = std::chrono::duration_cast<std::chrono::microseconds>(endTime - beginTime).count();
std::cout << "Compute creation time: " << delta << std::endl;
OComputePipeline pipeline = new ComputePipeline(graphics, pipelineHandle, std::move(computeInfo.pipelineLayout));
OComputePipeline pipeline = new ComputePipeline(graphics, pipelineHandle, computeInfo.pipelineLayout);
PComputePipeline result = pipeline;
graphicsPipelines[hash] = std::move(pipeline);
return result;
+3 -1
View File
@@ -30,7 +30,9 @@ uint32 Seele::Vulkan::Shader::getShaderHash() const
void Shader::create(const ShaderCreateInfo& createInfo)
{
Slang::ComPtr<slang::IBlob> kernelBlob = generateShader(createInfo, SLANG_SPIRV);
Map<std::string, uint32> paramMapping;
Slang::ComPtr<slang::IBlob> kernelBlob = generateShader(createInfo, SLANG_SPIRV, paramMapping);
const_cast<Gfx::PipelineLayout*>(createInfo.rootSignature.getHandle())->addMapping(paramMapping);
VkShaderModuleCreateInfo moduleInfo =
{
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
+1
View File
@@ -143,6 +143,7 @@ void Window::endFrame()
VK_CHECK(r);
}
currentSemaphoreIndex = (currentSemaphoreIndex + 1) % Gfx::numFramesBuffered;
graphics->waitDeviceIdle();
}
void Window::onWindowCloseEvent()
-1
View File
@@ -1,5 +1,4 @@
#include "slang-compile.h"
#include <slang-com-ptr.h>
#include <slang.h>
#include "Containers/Array.h"
#include <fmt/core.h>