RAIIfy vulkan

This commit is contained in:
Guillaume Meunier
2024-01-07 11:45:07 +01:00
parent 54fe51b21f
commit cf14f9bd8b
44 changed files with 2711 additions and 2793 deletions
+116
View File
@@ -0,0 +1,116 @@
/*
* WiVRn VR streaming
* Copyright (C) 2023 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2023 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "allocation.h"
#include "application.h"
#include "utils/check.h"
#include <utility>
#include <vk_mem_alloc.h>
buffer_allocation::buffer_allocation(const vk::BufferCreateInfo & buffer_info, const VmaAllocationCreateInfo & alloc_info)
{
VmaAllocator allocator = application::get_allocator();
VkBuffer tmp;
CHECK_VK(vmaCreateBuffer(allocator, &static_cast<const VkBufferCreateInfo&>(buffer_info), &alloc_info, &tmp, &allocation, nullptr));
buffer = vk::raii::Buffer(application::get_device(), tmp);
}
buffer_allocation::~buffer_allocation()
{
VmaAllocator allocator = application::get_allocator();
if (data)
vmaUnmapMemory(allocator, allocation);
vmaDestroyBuffer(allocator, buffer.release(), allocation);
}
buffer_allocation::buffer_allocation(buffer_allocation && other) :
allocation(other.allocation),
buffer(std::move(other.buffer)),
data(other.data)
{
other.allocation = nullptr;
other.data = nullptr;
}
void * buffer_allocation::map()
{
if (data)
return data;
VmaAllocator allocator = application::get_allocator();
CHECK_VK(vmaMapMemory(allocator, allocation, &data));
return data;
}
const buffer_allocation & buffer_allocation::operator=(buffer_allocation && other)
{
std::swap(allocation, other.allocation);
std::swap(buffer, other.buffer);
return *this;
}
image_allocation::image_allocation(const vk::ImageCreateInfo & image_info, const VmaAllocationCreateInfo & alloc_info)
{
VmaAllocator allocator = application::get_allocator();
VkImage tmp;
CHECK_VK(vmaCreateImage(allocator, &static_cast<const VkImageCreateInfo&>(image_info), &alloc_info, &tmp, &allocation, nullptr));
image = vk::raii::Image(application::get_device(), tmp);
}
image_allocation::~image_allocation()
{
VmaAllocator allocator = application::get_allocator();
if (data)
vmaUnmapMemory(allocator, allocation);
vmaDestroyImage(allocator, image.release(), allocation);
}
image_allocation::image_allocation(image_allocation && other) :
allocation(other.allocation),
image(std::move(other.image)),
data(other.data)
{
other.allocation = nullptr;
other.data = nullptr;
}
void * image_allocation::map()
{
if (data)
return data;
VmaAllocator allocator = application::get_allocator();
CHECK_VK(vmaMapMemory(allocator, allocation, &data));
return data;
}
const image_allocation & image_allocation::operator=(image_allocation && other)
{
std::swap(allocation, other.allocation);
std::swap(image, other.image);
return *this;
}
+129
View File
@@ -0,0 +1,129 @@
/*
* WiVRn VR streaming
* Copyright (C) 2023 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2023 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "vk_mem_alloc.h"
#include <vulkan/vulkan_raii.hpp>
class buffer_allocation
{
VmaAllocation allocation = nullptr;
vk::raii::Buffer buffer = nullptr;
void * data = nullptr;
public:
operator VkBuffer() const
{
return *buffer;
}
operator vk::Buffer() const
{
return *buffer;
}
operator const vk::raii::Buffer&() const
{
return buffer;
}
operator vk::raii::Buffer&()
{
return buffer;
}
operator VmaAllocation() const
{
return allocation;
}
vk::raii::Buffer* operator->()
{
return &buffer;
}
const vk::raii::Buffer* operator->() const
{
return &buffer;
}
buffer_allocation() = default;
buffer_allocation(const vk::BufferCreateInfo& buffer_info, const VmaAllocationCreateInfo& alloc_info);
buffer_allocation(const buffer_allocation&) = delete;
buffer_allocation(buffer_allocation&&);
const buffer_allocation& operator=(const buffer_allocation&) = delete;
const buffer_allocation& operator=(buffer_allocation&&);
~buffer_allocation();
void * map();
};
class image_allocation
{
VmaAllocation allocation = nullptr;
vk::raii::Image image = nullptr;
void * data = nullptr;
public:
operator VkImage() const
{
return *image;
}
operator vk::Image() const
{
return *image;
}
operator const vk::raii::Image&() const
{
return image;
}
operator vk::raii::Image&()
{
return image;
}
operator VmaAllocation() const
{
return allocation;
}
vk::raii::Image* operator->()
{
return &image;
}
const vk::raii::Image* operator->() const
{
return &image;
}
image_allocation() = default;
image_allocation(const vk::ImageCreateInfo& image_info, const VmaAllocationCreateInfo& alloc_info);
image_allocation(const image_allocation&) = delete;
image_allocation(image_allocation&&);
const image_allocation& operator=(const image_allocation&) = delete;
const image_allocation& operator=(image_allocation&&);
~image_allocation();
void * map();
};
-33
View File
@@ -1,33 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "buffer.h"
#include "utils/check.h"
vk::buffer::buffer(VkDevice device, const VkBufferCreateInfo & create_info) :
device(device)
{
CHECK_VK(vkCreateBuffer(device, &create_info, nullptr, &id));
}
vk::buffer::~buffer()
{
if (device)
vkDestroyBuffer(device, id, nullptr);
}
-40
View File
@@ -1,40 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "utils/handle.h"
#include <vulkan/vulkan.h>
namespace vk
{
class buffer : public utils::handle<VkBuffer>
{
VkDevice device{};
public:
buffer() = default;
buffer(VkDevice device, const VkBufferCreateInfo & create_info);
buffer(const buffer &) = delete;
buffer(buffer &&) = default;
buffer & operator=(const buffer &) = delete;
buffer & operator=(buffer &&) = default;
~buffer();
};
} // namespace vk
-33
View File
@@ -1,33 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "buffer_view.h"
#include "utils/check.h"
vk::buffer_view::buffer_view(VkDevice device, const VkBufferViewCreateInfo & create_info) :
device(device)
{
CHECK_VK(vkCreateBufferView(device, &create_info, nullptr, &id));
}
vk::buffer_view::~buffer_view()
{
if (device)
vkDestroyBufferView(device, id, nullptr);
}
-40
View File
@@ -1,40 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "utils/handle.h"
#include <vulkan/vulkan.h>
namespace vk
{
class buffer_view : public utils::handle<VkBufferView>
{
VkDevice device{};
public:
buffer_view() = default;
buffer_view(VkDevice device, const VkBufferViewCreateInfo & create_info);
buffer_view(const buffer_view &) = delete;
buffer_view(buffer_view &&) = default;
buffer_view & operator=(const buffer_view &) = delete;
buffer_view & operator=(buffer_view &&) = default;
~buffer_view();
};
} // namespace vk
-84
View File
@@ -1,84 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "command_pool.h"
#include "vk.h"
vk::command_pool::command_pool(VkDevice device, uint32_t queueFamilyIndex) :
device(device), lock(std::make_unique<std::mutex>())
{
VkCommandPoolCreateInfo cmdpool_create_info{};
cmdpool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmdpool_create_info.queueFamilyIndex = queueFamilyIndex;
cmdpool_create_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
CHECK_VK(vkCreateCommandPool(device, &cmdpool_create_info, nullptr, &id));
}
vk::command_pool::~command_pool()
{
if (device)
vkDestroyCommandPool(device, id, nullptr);
}
std::vector<VkCommandBuffer> vk::command_pool::allocate_command_buffers(uint32_t count, VkCommandBufferLevel level)
{
std::unique_lock _{*lock};
VkCommandBufferAllocateInfo alloc_info{};
alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
alloc_info.commandPool = id;
alloc_info.level = level;
alloc_info.commandBufferCount = count;
std::vector<VkCommandBuffer> command_buffers{count};
CHECK_VK(vkAllocateCommandBuffers(device, &alloc_info, command_buffers.data()));
return command_buffers;
}
VkCommandBuffer vk::command_pool::allocate_command_buffer(VkCommandBufferLevel level)
{
std::unique_lock _{*lock};
VkCommandBufferAllocateInfo alloc_info{};
alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
alloc_info.commandPool = id;
alloc_info.level = level;
alloc_info.commandBufferCount = 1;
VkCommandBuffer command_buffer;
CHECK_VK(vkAllocateCommandBuffers(device, &alloc_info, &command_buffer));
return command_buffer;
}
void vk::command_pool::free_command_buffers(const std::vector<VkCommandBuffer> & command_buffers)
{
std::unique_lock _{*lock};
vkFreeCommandBuffers(device, id, command_buffers.size(), command_buffers.data());
}
void vk::command_pool::free_command_buffer(VkCommandBuffer command_buffer)
{
if (command_buffer)
{
std::unique_lock _{*lock};
vkFreeCommandBuffers(device, id, 1, &command_buffer);
}
}
-51
View File
@@ -1,51 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "utils/handle.h"
#include <memory>
#include <mutex>
#include <vector>
#include <vulkan/vulkan.h>
namespace vk
{
class command_pool : public utils::handle<VkCommandPool>
{
VkDevice device{};
std::unique_ptr<std::mutex> lock;
public:
command_pool() = default;
command_pool(VkDevice device, uint32_t queueFamilyIndex);
command_pool(const command_pool &) = delete;
command_pool(command_pool &&) = default;
command_pool & operator=(const command_pool &) = delete;
command_pool & operator=(command_pool &&) = default;
~command_pool();
std::vector<VkCommandBuffer> allocate_command_buffers(uint32_t count, VkCommandBufferLevel level = VK_COMMAND_BUFFER_LEVEL_PRIMARY);
VkCommandBuffer allocate_command_buffer(VkCommandBufferLevel level = VK_COMMAND_BUFFER_LEVEL_PRIMARY);
void free_command_buffers(const std::vector<VkCommandBuffer> & command_buffers);
void free_command_buffer(VkCommandBuffer command_buffer);
};
} // namespace vk
-33
View File
@@ -1,33 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "image.h"
#include "utils/check.h"
vk::image::image(VkDevice device, const VkImageCreateInfo & create_info) :
device(device)
{
CHECK_VK(vkCreateImage(device, &create_info, nullptr, &id));
}
vk::image::~image()
{
if (device)
vkDestroyImage(device, id, nullptr);
}
-40
View File
@@ -1,40 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "utils/handle.h"
#include <vulkan/vulkan.h>
namespace vk
{
class image : public utils::handle<VkImage>
{
VkDevice device{};
public:
image() = default;
image(VkDevice device, const VkImageCreateInfo & create_info);
image(const image &) = delete;
image(image &&) = default;
image & operator=(const image &) = delete;
image & operator=(image &&) = default;
~image();
};
} // namespace vk
-63
View File
@@ -18,66 +18,3 @@
*/
#include "pipeline.h"
#include "vk.h"
vk::pipeline::pipeline(VkDevice device, vk::pipeline::graphics_info & create_info, const VkPipelineLayout layout) :
device(device)
{
for (VkPipelineShaderStageCreateInfo & i: create_info.shader_stages)
{
i.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
}
create_info.InputAssemblyState.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
create_info.RasterizationState.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
create_info.MultisampleState.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
create_info.DepthStencilState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
create_info.ColorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
VkPipelineVertexInputStateCreateInfo VertexInputState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.vertexBindingDescriptionCount = (uint32_t)create_info.vertex_input_bindings.size(),
.pVertexBindingDescriptions = create_info.vertex_input_bindings.data(),
.vertexAttributeDescriptionCount = (uint32_t)create_info.vertex_input_attributes.size(),
.pVertexAttributeDescriptions = create_info.vertex_input_attributes.data(),
};
VkPipelineDynamicStateCreateInfo DynamicState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
.dynamicStateCount = (uint32_t)create_info.dynamic_states.size(),
.pDynamicStates = create_info.dynamic_states.data()};
VkPipelineViewportStateCreateInfo ViewportState{
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.viewportCount = (uint32_t)create_info.viewports.size(),
.pViewports = create_info.viewports.data(),
.scissorCount = (uint32_t)create_info.scissors.size(),
.pScissors = create_info.scissors.data(),
};
VkGraphicsPipelineCreateInfo info{};
info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
info.stageCount = create_info.shader_stages.size();
info.pStages = create_info.shader_stages.data();
info.pVertexInputState = &VertexInputState;
info.pInputAssemblyState = &create_info.InputAssemblyState;
info.pViewportState = &ViewportState;
info.pRasterizationState = &create_info.RasterizationState;
info.pMultisampleState = &create_info.MultisampleState;
info.pColorBlendState = &create_info.ColorBlendState;
info.layout = layout;
info.renderPass = create_info.renderPass;
info.subpass = create_info.subpass;
if (DynamicState.dynamicStateCount > 0)
info.pDynamicState = &DynamicState;
CHECK_VK(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &info, nullptr, &id));
}
vk::pipeline::~pipeline()
{
if (device)
vkDestroyPipeline(device, id, nullptr);
}
+66 -30
View File
@@ -19,42 +19,78 @@
#pragma once
#include "pipeline_layout.h"
#include "utils/handle.h"
#include <optional>
#include <vector>
#include <vulkan/vulkan.h>
#include <vulkan/vulkan.hpp>
namespace vk
{
class pipeline : public utils::handle<VkPipeline>
class pipeline_builder
{
VkDevice device{};
public:
struct graphics_info
{
std::vector<VkPipelineShaderStageCreateInfo> shader_stages;
std::vector<VkVertexInputBindingDescription> vertex_input_bindings;
std::vector<VkVertexInputAttributeDescription> vertex_input_attributes;
VkPipelineInputAssemblyStateCreateInfo InputAssemblyState{};
// VkPipelineTessellationStateCreateInfo TessellationState;
std::vector<VkViewport> viewports;
std::vector<VkRect2D> scissors;
VkPipelineRasterizationStateCreateInfo RasterizationState{};
VkPipelineMultisampleStateCreateInfo MultisampleState{};
VkPipelineDepthStencilStateCreateInfo DepthStencilState{};
VkPipelineColorBlendStateCreateInfo ColorBlendState{};
std::vector<VkDynamicState> dynamic_states;
VkRenderPass renderPass{};
uint32_t subpass{};
};
vk::PipelineCreateFlags flags = {};
pipeline() = default;
pipeline(VkDevice device, graphics_info & create_info, VkPipelineLayout layout);
pipeline(const pipeline &) = delete;
pipeline(pipeline &&) = default;
pipeline & operator=(const pipeline &) = delete;
pipeline & operator=(pipeline &&) = default;
~pipeline();
std::vector<vk::PipelineShaderStageCreateInfo> Stages;
vk::PipelineVertexInputStateCreateInfo VertexInputState;
std::vector<vk::VertexInputBindingDescription> VertexBindingDescriptions;
std::vector<vk::VertexInputAttributeDescription> VertexAttributeDescriptions;
std::optional<vk::PipelineInputAssemblyStateCreateInfo> InputAssemblyState;
std::optional<vk::PipelineTessellationStateCreateInfo> TessellationState;
vk::PipelineViewportStateCreateInfo ViewportState;
std::vector<vk::Viewport> Viewports = {};
std::vector<vk::Rect2D> Scissors = {};
std::optional<vk::PipelineRasterizationStateCreateInfo> RasterizationState;
std::optional<vk::PipelineMultisampleStateCreateInfo> MultisampleState;
std::optional<vk::PipelineDepthStencilStateCreateInfo> DepthStencilState;
vk::PipelineColorBlendStateCreateInfo ColorBlendState;
std::vector<vk::PipelineColorBlendAttachmentState> ColorBlendAttachments;
vk::PipelineDynamicStateCreateInfo DynamicState;
std::vector<vk::DynamicState> DynamicStates;
vk::PipelineLayout layout;
vk::RenderPass renderPass = {};
uint32_t subpass = {};
vk::Pipeline basePipelineHandle;
int32_t basePipelineIndex = {};
operator vk::GraphicsPipelineCreateInfo()
{
VertexInputState.setVertexBindingDescriptions(VertexBindingDescriptions);
VertexInputState.setVertexAttributeDescriptions(VertexAttributeDescriptions);
ViewportState.setViewports(Viewports);
ViewportState.setScissors(Scissors);
ColorBlendState.setAttachments(ColorBlendAttachments);
DynamicState.setDynamicStates(DynamicStates);
return GraphicsPipelineCreateInfo{
.flags = flags,
.stageCount = (uint32_t)Stages.size(),
.pStages = Stages.data(),
.pVertexInputState = &VertexInputState,
.pInputAssemblyState = InputAssemblyState ? &*InputAssemblyState : nullptr,
.pTessellationState = TessellationState ? &*TessellationState : nullptr,
.pViewportState = &ViewportState,
.pRasterizationState = RasterizationState ? &*RasterizationState : nullptr,
.pMultisampleState = MultisampleState ? &*MultisampleState : nullptr,
.pDepthStencilState = DepthStencilState ? &*DepthStencilState : nullptr,
.pColorBlendState = &ColorBlendState,
.pDynamicState = &DynamicState,
.layout = layout,
.renderPass = renderPass,
.subpass = subpass,
.basePipelineHandle = basePipelineHandle,
.basePipelineIndex = basePipelineIndex
};
}
};
} // namespace vk
+5 -13
View File
@@ -19,24 +19,16 @@
#include "shader.h"
#include "vk.h"
vk::shader::shader(VkDevice device, const std::vector<uint32_t> & spirv) :
device(device)
vk::raii::ShaderModule load_shader(vk::raii::Device& device, const std::vector<uint32_t> & spirv)
{
VkShaderModuleCreateInfo create_info{};
create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
vk::ShaderModuleCreateInfo create_info{};
create_info.pCode = data(spirv);
create_info.codeSize = spirv.size() * sizeof(uint32_t);
CHECK_VK(vkCreateShaderModule(device, &create_info, nullptr, &id));
return vk::raii::ShaderModule{device, create_info};
}
vk::shader::shader(VkDevice device, const std::string & name) :
shader(device, shaders.at(name)) {}
vk::shader::~shader()
vk::raii::ShaderModule load_shader(vk::raii::Device& device, const std::string & name)
{
if (device)
vkDestroyShaderModule(device, id, nullptr);
return load_shader(device, shaders.at(name));
}
+3 -18
View File
@@ -19,27 +19,12 @@
#pragma once
#include "utils/handle.h"
#include <map>
#include <string>
#include <vector>
#include <vulkan/vulkan.h>
#include <vulkan/vulkan_raii.hpp>
extern const std::map<std::string, std::vector<uint32_t>> shaders;
namespace vk
{
class shader : public utils::handle<VkShaderModule>
{
VkDevice device;
public:
shader(VkDevice device, const std::vector<uint32_t> & spirv);
shader(VkDevice device, const std::string & name);
shader(shader &&) = default;
shader & operator=(shader &&) = default;
shader(const shader &) = delete;
shader operator=(const shader &) = delete;
~shader();
};
} // namespace vk
vk::raii::ShaderModule load_shader(vk::raii::Device& device, const std::vector<uint32_t> & spirv);
vk::raii::ShaderModule load_shader(vk::raii::Device& device, const std::string & name);
-8
View File
@@ -21,14 +21,6 @@
#include "magic_enum.hpp"
#include "utils/check.h"
#include "vk/buffer.h"
#include "vk/buffer_view.h"
#include "vk/command_pool.h"
#include "vk/device_memory.h"
#include "vk/image.h"
#include "vk/pipeline.h"
#include "vk/renderpass.h"
#include "vk/shader.h"
#include <string>
#include <vulkan/vulkan.h>