From 0162968484793e6258fda88a0af0466ca9494042 Mon Sep 17 00:00:00 2001 From: K1ngst0m Date: Thu, 12 Oct 2023 01:34:06 +0800 Subject: [PATCH] code refine --- engine/CMakeLists.txt | 1 + engine/api/vulkan/commandBuffer.cpp | 2 +- engine/api/vulkan/device.cpp | 18 ++++---- engine/api/vulkan/device.h | 4 +- engine/api/vulkan/pipeline.cpp | 13 +++--- engine/api/vulkan/pipeline.h | 9 ++-- engine/renderer/api/vulkan/renderer.cpp | 28 ++++++------ engine/renderer/api/vulkan/renderer.h | 26 ++++------- engine/renderer/api/vulkan/sceneRenderer.cpp | 6 +-- engine/resource/resourceLoader.cpp | 47 +++++++++++--------- examples/base_texture/base_texture.cpp | 10 ++--- examples/triangle_demo/triangle_demo.cpp | 10 ++--- 12 files changed, 84 insertions(+), 90 deletions(-) diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index b38dda11..2905ab57 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -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 diff --git a/engine/api/vulkan/commandBuffer.cpp b/engine/api/vulkan/commandBuffer.cpp index 12a80bfb..2d022489 100644 --- a/engine/api/vulkan/commandBuffer.cpp +++ b/engine/api/vulkan/commandBuffer.cpp @@ -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, diff --git a/engine/api/vulkan/device.cpp b/engine/api/vulkan/device.cpp index 828c91d8..a8b3b454 100644 --- a/engine/api/vulkan/device.cpp +++ b/engine/api/vulkan/device.cpp @@ -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::Create(const DeviceCreateInfo& createInfo) { PhysicalDevice* physicalDevice = createInfo.pPhysicalDevice; @@ -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(new Device(createInfo, physicalDevice, handle)); volkLoadDeviceTable(&device->m_table, handle); device->m_supportedFeatures = supportedFeatures; @@ -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) @@ -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) diff --git a/engine/api/vulkan/device.h b/engine/api/vulkan/device.h index dfd9ae3b..564e46e5 100644 --- a/engine/api/vulkan/device.h +++ b/engine/api/vulkan/device.h @@ -38,8 +38,8 @@ class Device : public ResourceHandle 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 Create(const DeviceCreateInfo& createInfo); + static void Destroy(Device* pDevice); public: VkResult createCubeMap(const std::array, 6>& images, Image** ppImage); diff --git a/engine/api/vulkan/pipeline.cpp b/engine/api/vulkan/pipeline.cpp index c0d1ae42..7a1c0e45 100644 --- a/engine/api/vulkan/pipeline.cpp +++ b/engine/api/vulkan/pipeline.cpp @@ -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; } @@ -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(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(colorAttachmentFormats_.size()), + .pColorAttachmentFormats = colorAttachmentFormats_.data(), .depthAttachmentFormat = depthAttachmentFormat_, .stencilAttachmentFormat = stencilAttachmentFormat_, }; diff --git a/engine/api/vulkan/pipeline.h b/engine/api/vulkan/pipeline.h index ca17d6a8..270bb131 100644 --- a/engine/api/vulkan/pipeline.h +++ b/engine/api/vulkan/pipeline.h @@ -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; @@ -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 colorBlendAttachmentStates_ = {}; + std::vector colorAttachmentFormats_ = {}; VkFormat depthAttachmentFormat_ = VK_FORMAT_UNDEFINED; VkFormat stencilAttachmentFormat_ = VK_FORMAT_UNDEFINED; @@ -214,7 +213,7 @@ class Pipeline : public ResourceHandle ShaderProgram* m_pProgram = {}; VkPipelineBindPoint m_bindPoint = {}; VkPipelineCache m_cache = {}; - RenderPipelineState m_rps = {}; + RenderPipelineState m_rps = {}; }; } // namespace aph::vk diff --git a/engine/renderer/api/vulkan/renderer.cpp b/engine/renderer/api/vulkan/renderer.cpp index f0bcc2fd..8be62f6e 100644 --- a/engine/renderer/api/vulkan/renderer.cpp +++ b/engine/renderer/api/vulkan/renderer.cpp @@ -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 @@ -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(m_pDevice); + m_pSyncPrimitivesPool = std::make_unique(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) @@ -172,7 +174,7 @@ Renderer::Renderer(WSI* wsi, const RenderConfig& config) : IRenderer(wsi, config // init resource loader { - m_pResourceLoader = std::make_unique(ResourceLoaderCreateInfo{.pDevice = m_pDevice}); + m_pResourceLoader = std::make_unique(ResourceLoaderCreateInfo{.pDevice = m_pDevice.get()}); } // init ui @@ -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); }; @@ -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 @@ -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(); } diff --git a/engine/renderer/api/vulkan/renderer.h b/engine/renderer/api/vulkan/renderer.h index 6c9950cc..417d47ff 100644 --- a/engine/renderer/api/vulkan/renderer.h +++ b/engine/renderer/api/vulkan/renderer.h @@ -19,38 +19,28 @@ class Renderer : public IRenderer public: std::unique_ptr m_pResourceLoader; + std::unique_ptr 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 m_queue; std::unordered_map> shaderModuleCaches = {}; diff --git a/engine/renderer/api/vulkan/sceneRenderer.cpp b/engine/renderer/api/vulkan/sceneRenderer.cpp index a3c4e1b9..130d4a7d 100644 --- a/engine/renderer/api/vulkan/sceneRenderer.cpp +++ b/engine/renderer/api/vulkan/sceneRenderer.cpp @@ -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); @@ -118,7 +118,7 @@ void SceneRenderer::cleanup() void SceneRenderer::recordAll() { - auto* queue = getGraphicsQueue(); + auto* queue = getDefaultQueue(QueueType::GRAPHICS); auto* currentCB = cb[m_frameIdx]; { @@ -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])); diff --git a/engine/resource/resourceLoader.cpp b/engine/resource/resourceLoader.cpp index 0288f502..68675b9d 100644 --- a/engine/resource/resourceLoader.cpp +++ b/engine/resource/resourceLoader.cpp @@ -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) @@ -124,28 +146,9 @@ void ResourceLoader::loadImages(ImageLoadInfo& info) { path = {std::get(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) { diff --git a/examples/base_texture/base_texture.cpp b/examples/base_texture/base_texture.cpp index 659e3525..807e25c2 100644 --- a/examples/base_texture/base_texture.cpp +++ b/examples/base_texture/base_texture.cpp @@ -15,7 +15,7 @@ void base_texture::init() }; m_renderer = aph::IRenderer::Create(m_wsi.get(), config); - m_pDevice = m_renderer->getDevice(); + m_pDevice = m_renderer->m_pDevice.get(); // setup quad { @@ -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)); @@ -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(); @@ -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); @@ -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); diff --git a/examples/triangle_demo/triangle_demo.cpp b/examples/triangle_demo/triangle_demo.cpp index 134083e3..b72bbde6 100644 --- a/examples/triangle_demo/triangle_demo.cpp +++ b/examples/triangle_demo/triangle_demo.cpp @@ -15,7 +15,7 @@ void triangle_demo::init() }; m_renderer = aph::IRenderer::Create(m_wsi.get(), config); - m_pDevice = m_renderer->getDevice(); + m_pDevice = m_renderer->m_pDevice.get(); // setup triangle { @@ -70,7 +70,7 @@ void triangle_demo::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)); @@ -85,7 +85,7 @@ void triangle_demo::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(); @@ -96,7 +96,7 @@ void triangle_demo::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); @@ -125,7 +125,7 @@ void triangle_demo::run() void triangle_demo::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);