Skip to content

Commit

Permalink
Complete the rendering sorting, we can render the transparent object …
Browse files Browse the repository at this point in the history
…correctly now
  • Loading branch information
VirFunc committed Sep 18, 2019
1 parent 3ad1c12 commit 4beb427
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 497 deletions.
61 changes: 61 additions & 0 deletions Mx/Vulkan/MxVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "Pipeline/MxVkVertexInput.h"
#include "../Definitions/MxSystemInfo.h"
#include "MxVkUtils.h"
#include "Image/MxVkImage.h"
#include "FrameBuffer/MxVkFramebuffer.h"

namespace Mix {
namespace Vulkan {
Expand Down Expand Up @@ -65,6 +67,8 @@ namespace Mix {
createSwapchain();
createCommandPool();
createAllocator();
createRenderPass();
createFrameBuffer();

mGraphicsCommandBuffers.reserve(mSwapchain->imageCount());
for (size_t i = 0; i < mSwapchain->imageCount(); ++i) {
Expand All @@ -80,9 +84,20 @@ namespace Mix {
mCurrCmd->wait();
mSwapchain->acquireNextImage();
mCurrCmd->begin();

std::vector<vk::ClearValue> clearValues(2);
clearValues[0].color = std::array<float, 4>{0.0f, 0.75f, 1.0f, 1.0f};
clearValues[1].depthStencil = vk::ClearDepthStencilValue(1.0f, 0);

mRenderPass->beginRenderPass(mCurrCmd->get(),
mFrameBuffers[mCurrFrame].get(),
clearValues,
mSwapchain->extent());
}

void VulkanAPI::endRender() {
mRenderPass->endRenderPass(mCurrCmd->get());

mCurrCmd->end();
mCurrCmd->submit({ mSwapchain->presentFinishedSph() }, // wait for image
{ vk::PipelineStageFlagBits::eColorAttachmentOutput },
Expand Down Expand Up @@ -181,5 +196,51 @@ namespace Mix {
void VulkanAPI::createAllocator() {
mAllocator = std::make_shared<DeviceAllocator>(mDevice);
}

void VulkanAPI::createRenderPass() {
mDepthStencil = Image::CreateDepthStencil(getAllocator(),
mSwapchain->extent(),
vk::SampleCountFlagBits::e1);
mDepthStencilView = Image::CreateVkImageView2D(mDevice->get(),
mDepthStencil->get(),
mDepthStencil->format(),
vk::ImageAspectFlagBits::eDepth |
vk::ImageAspectFlagBits::eStencil);

// RenderPass
mRenderPass = std::make_shared<RenderPass>(getLogicalDevice());

mRenderPass->addAttachment({
{0, Attachment::Type::PRESENT, mSwapchain->surfaceFormat().format},
{1, Attachment::Type::DEPTH_STENCIL, mDepthStencil->format()}
});

Subpass subpass(0);
subpass.addRef({
{AttachmentRef::Type::COLOR, 0},
{AttachmentRef::Type::DEPTH_STENCIL, 1}
});

mRenderPass->addSubpass(subpass);

mRenderPass->addDependency({
VK_SUBPASS_EXTERNAL,
0,
vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::AccessFlags(),
vk::AccessFlagBits::eColorAttachmentRead |
vk::AccessFlagBits::eColorAttachmentWrite
});
mRenderPass->create();
}

void VulkanAPI::createFrameBuffer() {
for (size_t i = 0; i < mSwapchain->imageCount(); ++i) {
mFrameBuffers.emplace_back(mRenderPass, getSwapchain()->extent());
mFrameBuffers.back().addAttachments({ mSwapchain->getImageViews()[i], mDepthStencilView });
mFrameBuffers.back().create();
}
}
}
}
202 changes: 107 additions & 95 deletions Mx/Vulkan/MxVulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,137 +5,149 @@
#include "../Window/MxWindow.h"
#include "../Object/MxObject.h"

#include "../Definitions/MxDefinitions.h"
#include "Core/MxVkDef.h"
#include "CommandBuffer/MxVkCommanddBufferHandle.h"
#include "../Definitions/MxVersion.h"
#include "Pipeline/MxVkRenderPass.h"
#include "FrameBuffer/MxVkFramebuffer.h"

namespace Mix {
class Camera;
class Camera;

namespace Vulkan {
class Instance;
class Device;
class PhysicalDevice;
class DeviceAllocator;
class CommandPool;
class DebugUtils;
class Swapchain;
class PipelineMgr;
class RenderPass;
class DescriptorPool;
class DescriptorSetLayout;
class FrameBuffer;
class Image;
class Buffer;
class DynamicUniformBuffer;
class ShaderBase;
class VertexInputManager;
namespace Vulkan {
class Instance;
class Device;
class PhysicalDevice;
class DeviceAllocator;
class CommandPool;
class DebugUtils;
class Swapchain;
class PipelineMgr;
class RenderPass;
class DescriptorPool;
class DescriptorSetLayout;
class FrameBuffer;
class Image;
class Buffer;
class DynamicUniformBuffer;
class ShaderBase;
class VertexInputManager;

struct VulkanSettings {
struct {
Version appVersion;
std::string appName;
}appInfo;
bool debugMode;
std::vector<const char*> instanceExts;
std::vector<const char*> deviceExts;
std::vector<const char*> validationLayers;
uint32_t physicalDeviceIndex;
vk::PhysicalDeviceFeatures enabledFeatures;
};
struct VulkanSettings {
struct {
Version appVersion;
std::string appName;
}appInfo;
bool debugMode;
std::vector<const char*> instanceExts;
std::vector<const char*> deviceExts;
std::vector<const char*> validationLayers;
uint32_t physicalDeviceIndex;
vk::PhysicalDeviceFeatures enabledFeatures;
};

class VulkanAPI {
public:
void init();
class VulkanAPI {
public:
void init();

static std::vector<vk::ExtensionProperties> GetAllSupportedInstanceExts();
static std::vector<vk::ExtensionProperties> GetAllSupportedInstanceExts();

static std::vector<vk::LayerProperties> GetAllSupportedLayers();
static std::vector<vk::LayerProperties> GetAllSupportedLayers();

const std::vector<PhysicalDeviceInfo>& getAllPhysicalDeviceInfo() const { return *mPhysicalDeviceInfos; }
const std::vector<PhysicalDeviceInfo>& getAllPhysicalDeviceInfo() const { return *mPhysicalDeviceInfos; }

void setTargetWindow(Window* _window) {
mWindow = _window;
}
void setTargetWindow(Window* _window) {
mWindow = _window;
}

void build(const VulkanSettings& _settings);
void build(const VulkanSettings& _settings);

const std::shared_ptr<Device>& getLogicalDevice() const {
return mDevice;
}
const std::shared_ptr<Device>& getLogicalDevice() const {
return mDevice;
}

const std::shared_ptr<PhysicalDevice>& getPhysicalDevice() const {
return mPhysicalDevice;
}
const std::shared_ptr<PhysicalDevice>& getPhysicalDevice() const {
return mPhysicalDevice;
}

const std::shared_ptr<DeviceAllocator>& getAllocator() const {
return mAllocator;
}
const std::shared_ptr<DeviceAllocator>& getAllocator() const {
return mAllocator;
}

const std::shared_ptr<Swapchain>& getSwapchain() const {
return mSwapchain;
}
const std::shared_ptr<Swapchain>& getSwapchain() const {
return mSwapchain;
}

const std::shared_ptr<CommandPool>& getTransferCommandPool() const { return mTransferCommandPool; }
const std::shared_ptr<CommandPool>& getTransferCommandPool() const { return mTransferCommandPool; }

const std::shared_ptr<CommandPool>& getGraphicsCommandPool() const { return mGraphicsCommandPool; }
const std::shared_ptr<CommandPool>& getGraphicsCommandPool() const { return mGraphicsCommandPool; }

const std::shared_ptr<DescriptorPool>& getDescriptorPool() const { return mDescriptorPool; }
const std::shared_ptr<DescriptorPool>& getDescriptorPool() const { return mDescriptorPool; }

void beginRender();
void beginRender();

void endRender();
void endRender();

uint32_t getCurrFrame() const { return mCurrFrame; }
uint32_t getCurrFrame() const { return mCurrFrame; }

CommandBufferHandle& getCurrDrawCmd()const { return *mCurrCmd; }
CommandBufferHandle& getCurrDrawCmd()const { return *mCurrCmd; }

void waitDeviceIdle();
const std::shared_ptr<RenderPass>& getRenderPass() { return mRenderPass; }

~VulkanAPI();
const FrameBuffer& getCurrFrameBuffer() const { return mFrameBuffers[mCurrFrame]; }

// Test managers
VertexInputManager& getVertexInputManager() {return *mVertexInputManager;};
void waitDeviceIdle();

private:
void build();
void createInstance();
void pickPhysicalDevice();
void createDevice();
void createDebugUtils();
void createDescriptorPool();
void createSwapchain();
void createCommandPool();
void createAllocator();
~VulkanAPI();

// Test managers
VertexInputManager& getVertexInputManager() { return *mVertexInputManager; }

std::shared_ptr<VulkanSettings> mSettings;
private:
void build();
void createInstance();
void pickPhysicalDevice();
void createDevice();
void createDebugUtils();
void createDescriptorPool();
void createSwapchain();
void createCommandPool();
void createAllocator();

std::shared_ptr<std::vector<PhysicalDeviceInfo>> mPhysicalDeviceInfos;
void createRenderPass();
void createFrameBuffer();

Window* mWindow = nullptr;
vk::SurfaceKHR mSurface;
std::shared_ptr<Instance> mInstance;
std::shared_ptr<Device> mDevice;
std::shared_ptr<PhysicalDevice> mPhysicalDevice;
std::shared_ptr<DebugUtils> mDebugUtils;
std::shared_ptr<DeviceAllocator> mAllocator;
std::shared_ptr<Swapchain> mSwapchain;
std::shared_ptr<DescriptorPool> mDescriptorPool;
std::shared_ptr<VulkanSettings> mSettings;

std::shared_ptr<CommandPool> mTransferCommandPool;
std::shared_ptr<CommandPool> mGraphicsCommandPool;
std::shared_ptr<std::vector<PhysicalDeviceInfo>> mPhysicalDeviceInfos;

// Test managers
std::shared_ptr<VertexInputManager> mVertexInputManager;
Window* mWindow = nullptr;
vk::SurfaceKHR mSurface;
std::shared_ptr<Instance> mInstance;
std::shared_ptr<Device> mDevice;
std::shared_ptr<PhysicalDevice> mPhysicalDevice;
std::shared_ptr<DebugUtils> mDebugUtils;
std::shared_ptr<DeviceAllocator> mAllocator;
std::shared_ptr<Swapchain> mSwapchain;
std::shared_ptr<DescriptorPool> mDescriptorPool;

std::vector<std::shared_ptr<CommandBufferHandle>> mGraphicsCommandBuffers;
std::shared_ptr<RenderPass> mRenderPass;
std::vector<FrameBuffer> mFrameBuffers;
std::shared_ptr<Image> mDepthStencil;
vk::ImageView mDepthStencilView;

uint32_t mCurrFrame = 0;
CommandBufferHandle* mCurrCmd = nullptr;
};
}
std::shared_ptr<CommandPool> mTransferCommandPool;
std::shared_ptr<CommandPool> mGraphicsCommandPool;

// Test managers
std::shared_ptr<VertexInputManager> mVertexInputManager;

std::vector<std::shared_ptr<CommandBufferHandle>> mGraphicsCommandBuffers;

uint32_t mCurrFrame = 0;
CommandBufferHandle* mCurrCmd = nullptr;
};
}
}

#endif // !MX_VK_GRAPHICS_H_
29 changes: 23 additions & 6 deletions Mx/Vulkan/Pipeline/MxVkGraphicsPipelineState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,14 @@ namespace Mix {
std::shared_ptr<Pipeline> GraphicsPipelineState::getPipeline(const std::shared_ptr<RenderPass>& _renderPass,
uint32_t _subpassIndex,
const std::shared_ptr<VertexInput>& _vertexInput,
MeshTopology _drawMode) {
// todo Find a nother way to generate the id of RenderPass
MeshTopology _drawMode,
bool _depthTest,
bool _depthWrite,
bool _stencilTest) {
// todo Find a nother way to generate the id of RenderPass
auto renderPassKey = static_cast<uint32_t>(reinterpret_cast<intptr_t>(_renderPass.get()));

PipelineKey key{ renderPassKey,_subpassIndex,_drawMode,_vertexInput->getId() };
PipelineKey key{ renderPassKey,_subpassIndex,_drawMode,_vertexInput->getId(),_depthTest,_depthWrite,_stencilTest };

// A suitable graphice pipeline exists
auto it = mPipelineMap.find(key);
Expand Down Expand Up @@ -221,9 +224,15 @@ namespace Mix {
std::shared_ptr<Pipeline> GraphicsPipelineState::createPipeline(const std::shared_ptr<RenderPass>& _renderPass,
uint32_t _subpassIndex,
MeshTopology _drawMode,
const std::shared_ptr<VertexInput>& _vertexInput) {
// todo Add more other common options
const std::shared_ptr<VertexInput>& _vertexInput,
bool _depthTest,
bool _depthWrite,
bool _stencilTest) {
// todo Add more other common options
mPipelineStateData.inputAssemblyInfo.topology = VulkanUtils::GetTopology(_drawMode);
mPipelineStateData.depthStencilInfo.depthTestEnable = _depthTest;
mPipelineStateData.depthStencilInfo.depthWriteEnable = _depthWrite;
mPipelineStateData.depthStencilInfo.stencilTestEnable = _stencilTest;
mPipelineStateData.pipelineCreateInfo.renderPass = _renderPass->get();
mPipelineStateData.pipelineCreateInfo.subpass = _subpassIndex;
mPipelineStateData.pipelineCreateInfo.pVertexInputState = &_vertexInput->getVertexInputStateInfo();
Expand All @@ -239,6 +248,8 @@ namespace Mix {
Utils::HashCombine(hash, _v.subpass);
Utils::HashCombine(hash, _v.drawMode);
Utils::HashCombine(hash, _v.vertexInputId);
uint32_t flag = (_v.depthTest << 2 | _v.depthWrite << 1 | _v.stencilTest);
Utils::HashCombine(hash, flag);
return hash;
}

Expand All @@ -247,7 +258,13 @@ namespace Mix {
}

bool GraphicsPipelineState::PipelineKey::operator==(const PipelineKey& _other) const {
return renderPassId == _other.renderPassId && subpass == _other.subpass && drawMode == _other.drawMode && vertexInputId == _other.vertexInputId;
return renderPassId == _other.renderPassId &&
subpass == _other.subpass &&
drawMode == _other.drawMode &&
vertexInputId == _other.vertexInputId &&
depthTest == _other.depthTest &&
depthWrite == _other.depthWrite &&
stencilTest == _other.stencilTest;
}

bool GraphicsPipelineState::PipelineKey::operator!=(const PipelineKey& _other) const {
Expand Down
Loading

0 comments on commit 4beb427

Please sign in to comment.