diff --git a/src/core/ModelImpl.cpp b/src/core/ModelImpl.cpp index 73b5d57f..f8e28caa 100644 --- a/src/core/ModelImpl.cpp +++ b/src/core/ModelImpl.cpp @@ -34,8 +34,3 @@ void ModelImpl::createMesh(std::string name) { void ModelImpl::setMeshTransform(std::string name, glm::mat4x4 transform) { _meshes[name]->set_transform(transform); } - -/* : _dev { openDevice() } - , _brightness { collectBrightness(_dev) } - , _rgb { collectRGB(_dev) } -{*/ \ No newline at end of file diff --git a/src/graphics/vulkan/vk_engine.cpp b/src/graphics/vulkan/vk_engine.cpp index fc360907..da8e1659 100644 --- a/src/graphics/vulkan/vk_engine.cpp +++ b/src/graphics/vulkan/vk_engine.cpp @@ -1,4 +1,4 @@ -#include "graphics/vulkan/vk_engine.h" +#include "graphics/vulkan/vk_engine.h" #include #include @@ -480,7 +480,7 @@ void VulkanEngine::init_sync_structures() { VkSemaphore swapchainSemaphore, renderSemaphore; VK_CHECK(vkCreateSemaphore(_device, &semaphoreCreateInfo, nullptr, &swapchainSemaphore)); VK_CHECK(vkCreateSemaphore(_device, &semaphoreCreateInfo, nullptr, &renderSemaphore)); - + _frame._swapchainSemaphore = std::make_unique(_device, swapchainSemaphore); _frame._renderSemaphore = std::make_unique(_device, renderSemaphore); } @@ -575,8 +575,8 @@ void VulkanEngine::init_swapchain() { VkFormat depthFormat = VK_FORMAT_D32_SFLOAT; VkImageCreateInfo dimg_info = vkinit::image_create_info( - depthFormat, - VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, + depthFormat, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, depthImageExtent); VmaAllocationCreateInfo dimg_allocinfo = {}; @@ -627,7 +627,7 @@ void VulkanEngine::cleanup() { if (_frame._commandPool) { vkDestroyCommandPool(_device, _frame._commandPool->get(), nullptr); } - + // Destroy frame descriptors manually _frame._frameDescriptors.destroy_pools(_device); } @@ -751,11 +751,10 @@ void VulkanEngine::draw_background(VkCommandBuffer cmd) const { pipelines.gradientPipeline->bindDescriptorSets(cmd, &_drawImageDescriptors, 1); // dispatch the compute shader - pipelines.gradientPipeline->dispatch(cmd, + pipelines.gradientPipeline->dispatch(cmd, static_cast(std::ceil(_drawExtent.width / 16.0)), - static_cast(std::ceil(_drawExtent.height / 16.0)), + static_cast(std::ceil(_drawExtent.height / 16.0)), 1); - } void VulkanEngine::draw_imgui(VkCommandBuffer cmd, diff --git a/src/graphics/vulkan/vk_loader.cpp b/src/graphics/vulkan/vk_loader.cpp index d33c8ffc..b631ced4 100644 --- a/src/graphics/vulkan/vk_loader.cpp +++ b/src/graphics/vulkan/vk_loader.cpp @@ -1,4 +1,4 @@ -#include "graphics/vulkan/vk_loader.h" +#include "graphics/vulkan/vk_loader.h" #include #include @@ -23,6 +23,7 @@ #include #include +#include "core/Logging.h" #include "graphics/vulkan/vk_descriptors.h" #include "graphics/vulkan/vk_engine.h" #include "graphics/vulkan/vk_types.h" @@ -30,11 +31,10 @@ std::optional>> loadGltfMeshes( VulkanEngine* engine, const std::filesystem::path& filePath) { if (!std::filesystem::exists(filePath)) { - std::cout << "Failed to find " << filePath << '\n'; + LOGW("Failed to find file: {}", filePath.string()); return {}; } - - std::cout << "Loading " << filePath << '\n'; + LOGI("Loading: {}", filePath.string()); fastgltf::Asset gltf; @@ -56,6 +56,11 @@ std::optional>> loadGltfMeshes( fastgltf::Options::LoadExternalImages | fastgltf::Options::GenerateMeshIndices; + if (!std::filesystem::exists(path)) { + LOGW("Failed to find file: {}", path.string()); + return {}; + } + fastgltf::GltfDataBuffer data; data.loadFromFile(path); @@ -64,8 +69,8 @@ std::optional>> loadGltfMeshes( if (asset) { gltf = std::move(asset.get()); } else { - fmt::print("Failed to load glTF: {} \n", - fastgltf::to_underlying(asset.error())); + LOGE("Failed to load glTF: {}", + fastgltf::to_underlying(asset.error())); return {}; } @@ -202,7 +207,11 @@ VkSamplerMipmapMode extract_mipmap_mode(fastgltf::Filter filter) { std::optional> loadGltf(VulkanEngine* engine, std::string_view filePath) { - fmt::print("Loading GLTF: {}", filePath); + LOGI("Loading GLTF: {}", filePath); + + if (!std::filesystem::exists(filePath)) { + LOGW("File does not exist: {}", filePath); + } auto scene = std::make_shared(); scene->creator = engine; LoadedGLTF& file = *scene; @@ -221,8 +230,7 @@ std::optional> loadGltf(VulkanEngine* engine, if (load) { gltf = std::move(load.get()); } else { - std::cerr << "Failed to load glTF: " - << fastgltf::to_underlying(load.error()) << std::endl; + LOGE("Failed to load glTF: {} ", fastgltf::to_underlying(load.error())); return {}; } } else if (type == fastgltf::GltfType::GLB) { @@ -231,12 +239,11 @@ std::optional> loadGltf(VulkanEngine* engine, if (load) { gltf = std::move(load.get()); } else { - std::cerr << "Failed to load glTF: " - << fastgltf::to_underlying(load.error()) << std::endl; + LOGE("Failed to load glTF: {} ", fastgltf::to_underlying(load.error())); return {}; } } else { - std::cerr << "Failed to determine glTF container" << std::endl; + LOGE("Failed to determine glTF container"); return {}; } @@ -314,7 +321,6 @@ std::optional> loadGltf(VulkanEngine* engine, GLTFMetallic_Roughness::MaterialResources materialResources; materialResources.colorImage = engine->_whiteImage->get(); - materialResources.colorSampler = engine->_defaultSamplerLinear; materialResources.metalRoughImage = engine->_whiteImage->get(); materialResources.metalRoughSampler = engine->_defaultSamplerLinear; diff --git a/src/graphics/vulkan/vk_pipelines.cpp b/src/graphics/vulkan/vk_pipelines.cpp index 945c8749..40438f0b 100644 --- a/src/graphics/vulkan/vk_pipelines.cpp +++ b/src/graphics/vulkan/vk_pipelines.cpp @@ -1,18 +1,27 @@ -#include "graphics/vulkan/vk_pipelines.h" +#include "graphics/vulkan/vk_pipelines.h" #include #include #include #include #include "core/Logging.h" +#include + #include "graphics/vulkan/vk_initializers.h" bool vkutil::load_shader_module(const char* filePath, VkDevice device, VkShaderModule* outShaderModule) { // open the file. With cursor at the end + if (!std::filesystem::exists(filePath)) { + LOGE("Shader file does not exist: {}", filePath); + return false; + } + + // Open the file in binary mode, with the cursor at the end std::ifstream file(filePath, std::ios::ate | std::ios::binary); if (!file.is_open()) { + LOGE("Failed to open shader file: {}", filePath); return false; } @@ -22,9 +31,10 @@ bool vkutil::load_shader_module(const char* filePath, VkDevice device, const auto fileSize = file.tellg(); if (fileSize == -1) { - spdlog::error("Failed to open file {}", filePath); + LOGE("Failed to open file {}", filePath); return false; } + // spirv expects the buffer to be on uint32, so make sure to reserve a int // vector big enough for the entire file std::vector buffer(static_cast(fileSize) / @@ -51,11 +61,14 @@ bool vkutil::load_shader_module(const char* filePath, VkDevice device, // check that the creation goes well. VkShaderModule shaderModule; - if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != - VK_SUCCESS) { + VkResult result = + vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule); + if (result != VK_SUCCESS) { + LOGE("Error: Failed to create shader module."); return false; } *outShaderModule = shaderModule; + LOGI("Shader module created successfully."); return true; } @@ -85,7 +98,7 @@ void PipelineBuilder::clear() { // Initialize color blend attachment with defaults _colorBlendAttachment = { .blendEnable = VK_FALSE, - .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | + .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT }; @@ -129,22 +142,22 @@ VkPipeline PipelineBuilder::build_pipeline(VkDevice device) const { VkPipelineRasterizationStateCreateInfo rasterizer = _rasterizer; VkPipelineMultisampleStateCreateInfo multisampling = _multisampling; VkPipelineInputAssemblyStateCreateInfo inputAssembly = _inputAssembly; - + // Fix any uninitialized values if (renderInfo.depthAttachmentFormat == 0) { renderInfo.depthAttachmentFormat = VK_FORMAT_UNDEFINED; } - + // Ensure rasterizer line width is valid if (rasterizer.lineWidth <= 0.0f) { rasterizer.lineWidth = 1.0f; } - + // Ensure multisampling uses a valid sample count if (multisampling.rasterizationSamples == 0) { multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; } - + // Avoid POINT_LIST topology without PointSize in shader if (inputAssembly.topology == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; @@ -156,7 +169,6 @@ VkPipeline PipelineBuilder::build_pipeline(VkDevice device) const { viewportState.pNext = nullptr; viewportState.viewportCount = 1; viewportState.scissorCount = 1; - // Use dynamic viewport and scissor VkDynamicState dynamicStates[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; VkPipelineDynamicStateCreateInfo dynamicState = {}; @@ -200,11 +212,11 @@ VkPipeline PipelineBuilder::build_pipeline(VkDevice device) const { VkPipeline newPipeline; if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &newPipeline) != VK_SUCCESS) { - + LOGE("Failed to create graphics pipeline in build_pipeline."); return VK_NULL_HANDLE; } - + return newPipeline; } @@ -221,13 +233,13 @@ void PipelineBuilder::set_shaders(VkShaderModule vertexShader, void PipelineBuilder::set_input_topology(VkPrimitiveTopology topology) { _inputAssembly.topology = topology; - + // Avoid POINT_LIST topology which requires special shader setup if (topology == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { // Fall back to triangle list if point list is requested _inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; } - + _inputAssembly.primitiveRestartEnable = VK_FALSE; } @@ -271,15 +283,15 @@ void PipelineBuilder::set_color_attachment_format(VkFormat format) { void PipelineBuilder::set_depth_format(VkFormat format) { // Ensure format is valid or undefined - if (format != VK_FORMAT_D16_UNORM && - format != VK_FORMAT_D32_SFLOAT && - format != VK_FORMAT_D16_UNORM_S8_UINT && - format != VK_FORMAT_D24_UNORM_S8_UINT && - format != VK_FORMAT_D32_SFLOAT_S8_UINT && + if (format != VK_FORMAT_D16_UNORM && + format != VK_FORMAT_D32_SFLOAT && + format != VK_FORMAT_D16_UNORM_S8_UINT && + format != VK_FORMAT_D24_UNORM_S8_UINT && + format != VK_FORMAT_D32_SFLOAT_S8_UINT && format != VK_FORMAT_UNDEFINED) { format = VK_FORMAT_UNDEFINED; } - + _renderInfo.depthAttachmentFormat = format; }