Skip to content

Commit

Permalink
code refine
Browse files Browse the repository at this point in the history
  • Loading branch information
K1ngst0m committed Oct 11, 2023
1 parent d5eaa0f commit 0162968
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 90 deletions.
1 change: 1 addition & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ file(GLOB base_src
)

add_library(engine STATIC ${base_src})
aph_compiler_options(engine)

target_include_directories(engine PUBLIC
../external/glm
Expand Down
2 changes: 1 addition & 1 deletion engine/api/vulkan/commandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ void CommandBuffer::flushGraphicsCommand()
aph::utils::forEachBitRange(m_commandState.vertexBinding.dirty, [&](uint32_t binding, uint32_t bindingCount) {
#ifdef APH_DEBUG
for(unsigned i = binding; i < binding + bindingCount; i++)
VK_ASSERT(m_commandState.vertexBinding.buffers[i] != VK_NULL_HANDLE);
APH_ASSERT(m_commandState.vertexBinding.buffers[i] != VK_NULL_HANDLE);
#endif
m_pDeviceTable->vkCmdBindVertexBuffers(m_handle, binding, bindingCount,
m_commandState.vertexBinding.buffers + binding,
Expand Down
18 changes: 9 additions & 9 deletions engine/api/vulkan/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Device::Device(const DeviceCreateInfo& createInfo, PhysicalDevice* pPhysicalDevi
getCreateInfo() = createInfo;
}

VkResult Device::Create(const DeviceCreateInfo& createInfo, Device** ppDevice)
std::unique_ptr<Device> Device::Create(const DeviceCreateInfo& createInfo)
{
PhysicalDevice* physicalDevice = createInfo.pPhysicalDevice;

Expand Down Expand Up @@ -108,10 +108,15 @@ VkResult Device::Create(const DeviceCreateInfo& createInfo, Device** ppDevice)
};

VkDevice handle = VK_NULL_HANDLE;
_VR(vkCreateDevice(physicalDevice->getHandle(), &deviceCreateInfo, nullptr, &handle));
auto result = vkCreateDevice(physicalDevice->getHandle(), &deviceCreateInfo, nullptr, &handle);
if(result != VK_SUCCESS)
{
VK_LOG_ERR("Failed to create device: %s.", vk::utils::errorString(result));
return {};
}

// Initialize Device class.
auto* device = new Device(createInfo, physicalDevice, handle);
auto device = std::unique_ptr<Device>(new Device(createInfo, physicalDevice, handle));
volkLoadDeviceTable(&device->m_table, handle);
device->m_supportedFeatures = supportedFeatures;

Expand All @@ -129,11 +134,8 @@ VkResult Device::Create(const DeviceCreateInfo& createInfo, Device** ppDevice)
}
}

// Copy address of object instance.
*ppDevice = device;

// Return success.
return VK_SUCCESS;
return device;
}

void Device::Destroy(Device* pDevice)
Expand All @@ -151,8 +153,6 @@ void Device::Destroy(Device* pDevice)
{
pDevice->m_table.vkDestroyDevice(pDevice->m_handle, nullptr);
}
delete pDevice;
pDevice = nullptr;
}

VkResult Device::createCommandPool(const CommandPoolCreateInfo& createInfo, VkCommandPool* ppPool)
Expand Down
4 changes: 2 additions & 2 deletions engine/api/vulkan/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Device : public ResourceHandle<VkDevice, DeviceCreateInfo>
Device(const DeviceCreateInfo& createInfo, PhysicalDevice* pPhysicalDevice, VkDevice handle);

public:
static VkResult Create(const DeviceCreateInfo& createInfo, Device** ppDevice);
static void Destroy(Device* pDevice);
static std::unique_ptr<Device> Create(const DeviceCreateInfo& createInfo);
static void Destroy(Device* pDevice);

public:
VkResult createCubeMap(const std::array<std::shared_ptr<ImageInfo>, 6>& images, Image** ppImage);
Expand Down
13 changes: 6 additions & 7 deletions engine/api/vulkan/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,13 @@ VulkanPipelineBuilder& VulkanPipelineBuilder::colorAttachments(const VkPipelineC
{
APH_ASSERT(states);
APH_ASSERT(formats);
APH_ASSERT(numColorAttachments <= LVK_ARRAY_NUM_ELEMENTS(colorBlendAttachmentStates_));
APH_ASSERT(numColorAttachments <= LVK_ARRAY_NUM_ELEMENTS(colorAttachmentFormats_));
colorBlendAttachmentStates_.resize(numColorAttachments);
colorAttachmentFormats_.resize(numColorAttachments);
for(uint32_t i = 0; i != numColorAttachments; i++)
{
colorBlendAttachmentStates_[i] = states[i];
colorAttachmentFormats_[i] = formats[i];
}
numColorAttachments_ = numColorAttachments;
return *this;
}

Expand Down Expand Up @@ -254,14 +253,14 @@ VkResult VulkanPipelineBuilder::build(VkDevice device, VkPipelineCache pipelineC
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.logicOpEnable = VK_FALSE,
.logicOp = VK_LOGIC_OP_COPY,
.attachmentCount = numColorAttachments_,
.pAttachments = colorBlendAttachmentStates_,
.attachmentCount = static_cast<uint32_t>(colorBlendAttachmentStates_.size()),
.pAttachments = colorBlendAttachmentStates_.data(),
};
const VkPipelineRenderingCreateInfo renderingInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
.pNext = nullptr,
.colorAttachmentCount = numColorAttachments_,
.pColorAttachmentFormats = colorAttachmentFormats_,
.colorAttachmentCount = static_cast<uint32_t>(colorAttachmentFormats_.size()),
.pColorAttachmentFormats = colorAttachmentFormats_.data(),
.depthAttachmentFormat = depthAttachmentFormat_,
.stencilAttachmentFormat = stencilAttachmentFormat_,
};
Expand Down
9 changes: 4 additions & 5 deletions engine/api/vulkan/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct RenderPipelineDynamicState final
struct GraphicsPipelineCreateInfo
{
RenderPipelineDynamicState dynamicState = {};
VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;

VertexInput vertexInput;

Expand Down Expand Up @@ -190,9 +190,8 @@ class VulkanPipelineBuilder final
VkPipelineMultisampleStateCreateInfo multisampleState_;
VkPipelineDepthStencilStateCreateInfo depthStencilState_;

uint32_t numColorAttachments_ = 0;
VkPipelineColorBlendAttachmentState colorBlendAttachmentStates_[APH_MAX_COLOR_ATTACHMENTS] = {};
VkFormat colorAttachmentFormats_[APH_MAX_COLOR_ATTACHMENTS] = {};
std::vector<VkPipelineColorBlendAttachmentState> colorBlendAttachmentStates_ = {};
std::vector<VkFormat> colorAttachmentFormats_ = {};

VkFormat depthAttachmentFormat_ = VK_FORMAT_UNDEFINED;
VkFormat stencilAttachmentFormat_ = VK_FORMAT_UNDEFINED;
Expand All @@ -214,7 +213,7 @@ class Pipeline : public ResourceHandle<VkPipeline>
ShaderProgram* m_pProgram = {};
VkPipelineBindPoint m_bindPoint = {};
VkPipelineCache m_cache = {};
RenderPipelineState m_rps = {};
RenderPipelineState m_rps = {};
};

} // namespace aph::vk
Expand Down
28 changes: 15 additions & 13 deletions engine/renderer/api/vulkan/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,21 @@ Renderer::Renderer(WSI* wsi, const RenderConfig& config) : IRenderer(wsi, config
.pPhysicalDevice = m_pInstance->getPhysicalDevices(0),
};

VK_CHECK_RESULT(Device::Create(createInfo, &m_pDevice));
m_pDevice = Device::Create(createInfo);
APH_ASSERT(m_pDevice != nullptr);

// get 3 type queue
m_queue.graphics = m_pDevice->getQueueByFlags(QueueType::GRAPHICS);
m_queue.compute = m_pDevice->getQueueByFlags(QueueType::COMPUTE);
m_queue.transfer = m_pDevice->getQueueByFlags(QueueType::TRANSFER);
if(!m_queue.compute)
m_queue[QueueType::GRAPHICS] = m_pDevice->getQueueByFlags(QueueType::GRAPHICS);
m_queue[QueueType::COMPUTE] = m_pDevice->getQueueByFlags(QueueType::COMPUTE);
m_queue[QueueType::TRANSFER] = m_pDevice->getQueueByFlags(QueueType::TRANSFER);

if(!m_queue[QueueType::COMPUTE])
{
m_queue.compute = m_queue.graphics;
m_queue[QueueType::COMPUTE] = m_queue[QueueType::GRAPHICS];
}
if(!m_queue.transfer)
if(!m_queue[QueueType::TRANSFER])
{
m_queue.transfer = m_queue.compute;
m_queue[QueueType::TRANSFER] = m_queue[QueueType::COMPUTE];
}

// check sample count support
Expand Down Expand Up @@ -153,7 +155,7 @@ Renderer::Renderer(WSI* wsi, const RenderConfig& config) : IRenderer(wsi, config
m_frameFence.resize(m_config.maxFrames);

{
m_pSyncPrimitivesPool = std::make_unique<SyncPrimitivesPool>(m_pDevice);
m_pSyncPrimitivesPool = std::make_unique<SyncPrimitivesPool>(m_pDevice.get());
m_pSyncPrimitivesPool->acquireSemaphore(m_presentSemaphore.size(), m_presentSemaphore.data());
m_pSyncPrimitivesPool->acquireSemaphore(m_renderSemaphore.size(), m_renderSemaphore.data());
for(auto& fence : m_frameFence)
Expand All @@ -172,7 +174,7 @@ Renderer::Renderer(WSI* wsi, const RenderConfig& config) : IRenderer(wsi, config

// init resource loader
{
m_pResourceLoader = std::make_unique<ResourceLoader>(ResourceLoaderCreateInfo{.pDevice = m_pDevice});
m_pResourceLoader = std::make_unique<ResourceLoader>(ResourceLoaderCreateInfo{.pDevice = m_pDevice.get()});
}

// init ui
Expand Down Expand Up @@ -334,7 +336,7 @@ Renderer::~Renderer()

m_pDevice->destroySwapchain(m_pSwapChain);
vkDestroySurfaceKHR(m_pInstance->getHandle(), m_surface, nullptr);
Device::Destroy(m_pDevice);
Device::Destroy(m_pDevice.get());
Instance::Destroy(m_pInstance);
};

Expand All @@ -351,7 +353,7 @@ void Renderer::beginFrame()

void Renderer::endFrame()
{
auto* queue = getGraphicsQueue();
auto* queue = getDefaultQueue(QueueType::GRAPHICS);
VK_CHECK_RESULT(m_pSwapChain->presentImage(queue, {m_presentSemaphore[m_frameIdx]}));

// clean the frame data
Expand Down Expand Up @@ -382,7 +384,7 @@ Shader* Renderer::getShaders(const std::filesystem::path& path)
{
if(!shaderModuleCaches.count(path))
{
shaderModuleCaches[path] = Shader::Create(m_pDevice, path);
shaderModuleCaches[path] = Shader::Create(m_pDevice.get(), path);
}
return shaderModuleCaches[path].get();
}
Expand Down
26 changes: 8 additions & 18 deletions engine/renderer/api/vulkan/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,28 @@ class Renderer : public IRenderer

public:
std::unique_ptr<ResourceLoader> m_pResourceLoader;
std::unique_ptr<Device> m_pDevice = {};
SwapChain* m_pSwapChain = {};

public:
SwapChain* getSwapChain() const { return m_pSwapChain; }
Device* getDevice() const { return m_pDevice; }
Shader* getShaders(const std::filesystem::path& path);

Queue* getGraphicsQueue() const { return m_queue.graphics; }
Queue* getComputeQueue() const { return m_queue.compute; }
Queue* getTransferQueue() const { return m_queue.transfer; }
Shader* getShaders(const std::filesystem::path& path);
Queue* getDefaultQueue(QueueType type) const { return m_queue.at(type); }

VkSemaphore getRenderSemaphore() { return m_renderSemaphore[m_frameIdx]; }
VkSemaphore getPresentSemaphore() { return m_presentSemaphore[m_frameIdx]; }
VkFence getFrameFence() { return m_frameFence[m_frameIdx]; }
VkFence getFrameFence() { return m_frameFence[m_frameIdx]; }

CommandBuffer* acquireFrameCommandBuffer(Queue * queue);
CommandBuffer* acquireFrameCommandBuffer(Queue* queue);

protected:
VkSampleCountFlagBits m_sampleCount = {VK_SAMPLE_COUNT_1_BIT};

Instance* m_pInstance = {};
Device* m_pDevice = {};
SwapChain* m_pSwapChain = {};
Instance* m_pInstance = {};

VkSurfaceKHR m_surface = {};
VkPipelineCache m_pipelineCache = {};

struct
{
Queue* graphics = {};
Queue* compute = {};
Queue* transfer = {};
} m_queue;
std::unordered_map<QueueType, Queue*> m_queue;

std::unordered_map<std::string, std::unique_ptr<Shader>> shaderModuleCaches = {};

Expand Down
6 changes: 3 additions & 3 deletions engine/renderer/api/vulkan/sceneRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void SceneRenderer::load(Scene* scene)

_initSet();

auto* queue = getGraphicsQueue();
auto* queue = getDefaultQueue(QueueType::GRAPHICS);
for(auto idx = 0; idx < m_config.maxFrames; idx++)
{
m_pDevice->allocateThreadCommandBuffers(COMMAND_BUFFER_MAX, cb[idx], queue);
Expand Down Expand Up @@ -118,7 +118,7 @@ void SceneRenderer::cleanup()

void SceneRenderer::recordAll()
{
auto* queue = getGraphicsQueue();
auto* queue = getDefaultQueue(QueueType::GRAPHICS);
auto* currentCB = cb[m_frameIdx];

{
Expand Down Expand Up @@ -500,7 +500,7 @@ void SceneRenderer::_initGbuffer()

GraphicsPipelineCreateInfo createInfo{
.pProgram = program,
.color = {{.format = getSwapChain()->getFormat()}},
.color = {{.format = m_pSwapChain->getFormat()}},
};

VK_CHECK_RESULT(m_pDevice->createGraphicsPipeline(createInfo, &m_pipelines[PIPELINE_GRAPHICS_LIGHTING]));
Expand Down
47 changes: 25 additions & 22 deletions engine/resource/resourceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ inline bool loadPNGJPG(const std::filesystem::path& path, aph::vk::ImageCreateIn
namespace aph
{

ImageContainerType GetImageContainerType(const std::filesystem::path& path)
{
if(path.extension() == ".ktx")
{
return ImageContainerType::Ktx;
}

if(path.extension() == ".png")
{
return ImageContainerType::Png;
}

if(path.extension() == ".jpg")
{
return ImageContainerType::Jpg;
}

CM_LOG_ERR("Unsupported image format.");

return ImageContainerType::Default;
}

ResourceLoader::ResourceLoader(const ResourceLoaderCreateInfo& createInfo) :
m_createInfo(createInfo),
m_pDevice(createInfo.pDevice)
Expand All @@ -124,28 +146,9 @@ void ResourceLoader::loadImages(ImageLoadInfo& info)
{
path = {std::get<std::string>(info.data)};

auto containerType = info.containerType;

if(info.containerType == ImageContainerType::Default)
{
if(path.extension() == ".ktx")
{
containerType = ImageContainerType::Ktx;
}
else if(path.extension() == ".png")
{
containerType = ImageContainerType::Png;
}
else if(path.extension() == ".jpg")
{
containerType = ImageContainerType::Jpg;
}
else
{
CM_LOG_ERR("Unsupported image format.");
APH_ASSERT(false);
}
}
auto containerType = info.containerType == ImageContainerType::Default
? GetImageContainerType(path)
: info.containerType;

switch(containerType)
{
Expand Down
10 changes: 5 additions & 5 deletions examples/base_texture/base_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void base_texture::init()
};

m_renderer = aph::IRenderer::Create<aph::vk::Renderer>(m_wsi.get(), config);
m_pDevice = m_renderer->getDevice();
m_pDevice = m_renderer->m_pDevice.get();

// setup quad
{
Expand Down Expand Up @@ -101,7 +101,7 @@ void base_texture::init()
aph::vk::GraphicsPipelineCreateInfo createInfo{
.vertexInput = vdesc,
.pProgram = m_pShaderProgram,
.color = {{.format = m_renderer->getSwapChain()->getFormat()}},
.color = {{.format = m_renderer->m_pSwapChain->getFormat()}},
};

VK_CHECK_RESULT(m_pDevice->createGraphicsPipeline(createInfo, &m_pPipeline));
Expand Down Expand Up @@ -132,7 +132,7 @@ void base_texture::run()
static float deltaTime = {};
auto timer = aph::Timer(deltaTime);

auto* queue = m_renderer->getGraphicsQueue();
auto* queue = m_renderer->getDefaultQueue(aph::vk::QueueType::GRAPHICS);

// draw and submit
m_renderer->beginFrame();
Expand All @@ -143,7 +143,7 @@ void base_texture::run()
.height = m_renderer->getWindowHeight(),
};

aph::vk::Image* presentImage = m_renderer->getSwapChain()->getImage();
aph::vk::Image* presentImage = m_renderer->m_pSwapChain->getImage();

cb->begin();
cb->setViewport(extent);
Expand Down Expand Up @@ -173,7 +173,7 @@ void base_texture::run()

void base_texture::finish()
{
m_renderer->getDevice()->waitIdle();
m_renderer->m_pDevice->waitIdle();
m_pDevice->destroyBuffer(m_pVB);
m_pDevice->destroyBuffer(m_pIB);
m_pDevice->destroyPipeline(m_pPipeline);
Expand Down
Loading

0 comments on commit 0162968

Please sign in to comment.