Skip to content

Commit

Permalink
implement dynamic rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinzii committed Aug 21, 2023
1 parent 01d711f commit 285e937
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 49 deletions.
3 changes: 3 additions & 0 deletions engine/include/graphics/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace gen
void createSurface(const Window & window);
void pickPhysicalDevice();
void createLogicalDevice();
void createCommandPoolAndBuffer();
void createSwapChain(const Window & window);
void createImageViews();

Expand All @@ -75,6 +76,8 @@ namespace gen
vk::UniqueSurfaceKHR m_surface{};
Gpu m_gpu{};
vk::UniqueDevice m_device{};
vk::UniqueCommandPool m_commandPool{};
vk::UniqueCommandBuffer m_commandBuffer{};
vk::Queue m_graphicsQueue{};
SwapChainSupportDetails m_swapChainSupport{};
vk::SwapchainCreateInfoKHR m_swapChainInfo{};
Expand Down
3 changes: 0 additions & 3 deletions engine/include/graphics/pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ namespace gen
static PipelineConfigInfo defaultPipelineConfigInfo(mim::vec2i extent);

private:
void createRenderPass();

static std::vector<char> readFile(const std::string & filePath);

void createGraphicsPipeline();
Expand All @@ -53,7 +51,6 @@ namespace gen
vk::UniqueShaderModule m_vertShaderModule;
vk::UniqueShaderModule m_fragShaderModule;

vk::UniqueRenderPass m_renderPass;
vk::UniquePipelineLayout m_pipelineLayout;
vk::UniquePipeline m_graphicsPipeline;

Expand Down
37 changes: 27 additions & 10 deletions engine/src/graphics/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace gen
createSurface(window);
pickPhysicalDevice();
createLogicalDevice();
createCommandPoolAndBuffer();
createSwapChain(window);
createImageViews();

Expand Down Expand Up @@ -141,23 +142,31 @@ namespace gen
std::vector<vk::DeviceQueueCreateInfo> queueCreateInfos;
std::set<u32> const uniqueQueueFamilies = {m_gpu.queueFamily};

auto enabledFeatures = vk::PhysicalDeviceFeatures{};
auto availableFeatures = m_gpu.physicalDevice.getFeatures();
enabledFeatures.fillModeNonSolid = availableFeatures.fillModeNonSolid;
enabledFeatures.wideLines = availableFeatures.wideLines;
enabledFeatures.samplerAnisotropy = availableFeatures.samplerAnisotropy;
enabledFeatures.sampleRateShading = availableFeatures.sampleRateShading;
enabledFeatures.logicOp = availableFeatures.logicOp;

float const queuePriority = 1.0F;
for (u32 const queueFamily : uniqueQueueFamilies)
{
vk::DeviceQueueCreateInfo const queueCreateInfo({}, queueFamily, 1, &queuePriority);
queueCreateInfos.push_back(queueCreateInfo);
}

vk::DeviceCreateInfo const createInfo(
{},
static_cast<u32>(queueCreateInfos.size()),
queueCreateInfos.data(),
0, // ignored by spec
nullptr, // ignored by spec
static_cast<u32>(deviceExtensions.size()),
deviceExtensions.data(),
nullptr // to be used later
);
auto createInfo = vk::DeviceCreateInfo{};

createInfo.queueCreateInfoCount = static_cast<u32>(queueCreateInfos.size());
createInfo.pQueueCreateInfos = queueCreateInfos.data();
createInfo.enabledExtensionCount = static_cast<u32>(deviceExtensions.size());
createInfo.ppEnabledExtensionNames = deviceExtensions.data();
createInfo.pEnabledFeatures = &enabledFeatures;

auto dynamicRenderingFeature = vk::PhysicalDeviceDynamicRenderingFeatures{true};
createInfo.pNext = &dynamicRenderingFeature;

m_device = m_gpu.physicalDevice.createDeviceUnique(createInfo);

Expand All @@ -174,6 +183,14 @@ namespace gen
m_graphicsQueue = m_device->getQueue(m_gpu.queueFamily, 0);
}

void GraphicsDevice::createCommandPoolAndBuffer()
{
m_commandPool = m_device->createCommandPoolUnique(vk::CommandPoolCreateInfo(vk::CommandPoolCreateFlags(), m_gpu.queueFamily));

m_commandBuffer =
std::move(m_device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(m_commandPool.get(), vk::CommandBufferLevel::ePrimary, 1)).front());
}

void GraphicsDevice::createSwapChain(const Window & window)
{
m_swapChainSupport = querySwapChainSupport(m_gpu.physicalDevice, m_surface.get());
Expand Down
41 changes: 5 additions & 36 deletions engine/src/graphics/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace gen
{
GraphicsPipeline::GraphicsPipeline(GraphicsDevice & device, const PipelineConfigInfo & configInfo) : m_device{device}, m_configInfo{configInfo}
{
createRenderPass();
createGraphicsPipeline();
m_logger.info("Graphics pipeline constructed");
}
Expand Down Expand Up @@ -131,42 +130,12 @@ namespace gen
return buffer;
}

void GraphicsPipeline::createRenderPass()
{
vk::AttachmentDescription colorAttachment{
{},
m_device.getSwapChainInfo().imageFormat,
vk::SampleCountFlagBits::e1,
vk::AttachmentLoadOp::eClear, // Clear framebuffer to black before drawing a new frame.
vk::AttachmentStoreOp::eStore, // Store the framebuffer to memory after drawing a new frame.
vk::AttachmentLoadOp::eDontCare,
vk::AttachmentStoreOp::eDontCare,
vk::ImageLayout::eUndefined,
vk::ImageLayout::ePresentSrcKHR};

vk::AttachmentReference colorAttachmentRef{0, vk::ImageLayout::eColorAttachmentOptimal};

// for the time being we are only doing a single subpass
vk::SubpassDescription subpass{{}, vk::PipelineBindPoint::eGraphics, 0, nullptr, 1, &colorAttachmentRef, nullptr, nullptr, 0, nullptr};

vk::SubpassDependency dependency{
VK_SUBPASS_EXTERNAL,
0,
vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::PipelineStageFlagBits::eColorAttachmentOutput,
{},
vk::AccessFlagBits::eColorAttachmentRead | vk::AccessFlagBits::eColorAttachmentWrite};

vk::RenderPassCreateInfo renderPassInfo{{}, 1, &colorAttachment, 1, &subpass, 1, &dependency};

m_renderPass = m_device.getUniqueDevice()->createRenderPassUnique(renderPassInfo);
}

void GraphicsPipeline::createGraphicsPipeline()
{
// This will be removed later.
auto vertShaderCode = readFile("bin/simplePS.spv");
auto fragShaderCode = readFile("bin/simpleVS.spv");
// This will be removed later and replaced with a better system.
// Just here so we can eventually render a triangle.
auto vertShaderCode = readFile("bin/simpleVS.spv");
auto fragShaderCode = readFile("bin/simplePS.spv");

m_vertShaderModule = vk::util::createShaderModule(m_device.getDevice(), vertShaderCode);
m_fragShaderModule = vk::util::createShaderModule(m_device.getDevice(), fragShaderCode);
Expand Down Expand Up @@ -200,7 +169,7 @@ namespace gen
&m_configInfo.colorBlendInfo,
&dynamicStateCreateInfo,
m_pipelineLayout.get(),
m_renderPass.get(),
nullptr, // render pass is no longer needed with dynamic rendering
m_configInfo.subpass,
nullptr, // optional
-1}; // optional
Expand Down

0 comments on commit 285e937

Please sign in to comment.