Skip to content
5 changes: 0 additions & 5 deletions src/core/ModelImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,3 @@ void ModelImpl::createMesh(std::string name) {
void ModelImpl::setMeshTransform(std::string name, glm::mat4x4 transform) {
_meshes[name]->set_transform(transform);
}
Comment thread
Fra1sse marked this conversation as resolved.
Comment thread
Fra1sse marked this conversation as resolved.

/* : _dev { openDevice() }
, _brightness { collectBrightness(_dev) }
, _rgb { collectRGB(_dev) }
{*/
15 changes: 7 additions & 8 deletions src/graphics/vulkan/vk_engine.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "graphics/vulkan/vk_engine.h"
#include "graphics/vulkan/vk_engine.h"

#include <SDL_error.h>
#include <SDL_stdinc.h>
Expand Down Expand Up @@ -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<VulkanSemaphore>(_device, swapchainSemaphore);
_frame._renderSemaphore = std::make_unique<VulkanSemaphore>(_device, renderSemaphore);
}
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<uint32_t>(std::ceil(_drawExtent.width / 16.0)),
static_cast<uint32_t>(std::ceil(_drawExtent.height / 16.0)),
static_cast<uint32_t>(std::ceil(_drawExtent.height / 16.0)),
1);

}

void VulkanEngine::draw_imgui(VkCommandBuffer cmd,
Expand Down
32 changes: 19 additions & 13 deletions src/graphics/vulkan/vk_loader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "graphics/vulkan/vk_loader.h"
#include "graphics/vulkan/vk_loader.h"

#include <array>
#include <cstdint>
Expand All @@ -23,18 +23,18 @@
#include <utility>
#include <variant>

#include "core/Logging.h"
#include "graphics/vulkan/vk_descriptors.h"
#include "graphics/vulkan/vk_engine.h"
#include "graphics/vulkan/vk_types.h"

std::optional<std::vector<std::shared_ptr<MeshAsset>>> 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;

Expand All @@ -56,6 +56,11 @@ std::optional<std::vector<std::shared_ptr<MeshAsset>>> 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);

Expand All @@ -64,8 +69,8 @@ std::optional<std::vector<std::shared_ptr<MeshAsset>>> 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 {};
}

Expand Down Expand Up @@ -202,7 +207,11 @@ VkSamplerMipmapMode extract_mipmap_mode(fastgltf::Filter filter) {

std::optional<std::shared_ptr<LoadedGLTF>> 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<LoadedGLTF>();
scene->creator = engine;
LoadedGLTF& file = *scene;
Expand All @@ -221,8 +230,7 @@ std::optional<std::shared_ptr<LoadedGLTF>> 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) {
Expand All @@ -231,12 +239,11 @@ std::optional<std::shared_ptr<LoadedGLTF>> 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 {};
}

Expand Down Expand Up @@ -314,7 +321,6 @@ std::optional<std::shared_ptr<LoadedGLTF>> 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;
Expand Down
52 changes: 32 additions & 20 deletions src/graphics/vulkan/vk_pipelines.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
#include "graphics/vulkan/vk_pipelines.h"
#include "graphics/vulkan/vk_pipelines.h"

#include <cstdint>
#include <fmt/base.h>
#include <fstream>
#include <spdlog/spdlog.h>
#include "core/Logging.h"
#include <filesystem>

#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;
}

Expand All @@ -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<uint32_t> buffer(static_cast<uint32_t>(fileSize) /
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
};

Expand Down Expand Up @@ -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;
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
Loading