From d9b0e82be7160f378810df4dbea6db452829cb23 Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Mon, 1 Jul 2024 21:22:40 +0200 Subject: [PATCH] Improve HLSL compile. --- add_shader.cmake | 5 ++++- samples/01_triangle/triangle.cpp | 18 ++++-------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/add_shader.cmake b/add_shader.cmake index 4084d8c..a935ea2 100644 --- a/add_shader.cmake +++ b/add_shader.cmake @@ -2,6 +2,9 @@ function(add_shader TARGET SHADER) find_package(Vulkan REQUIRED COMPONENTS dxc) get_filename_component(shader-ext ${SHADER} LAST_EXT) + get_filename_component(shader-inner ${SHADER} NAME_WLE) + get_filename_component(shader-type ${shader-inner} LAST_EXT) + string(REPLACE "." "" shader-type ${shader-type}) set(current-shader-path ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER}) set(current-output-path ${CMAKE_CURRENT_BINARY_DIR}/${SHADER}.spv) @@ -13,7 +16,7 @@ function(add_shader TARGET SHADER) message("Marked as hlsl file. ${current-output-path}") add_custom_command( OUTPUT ${current-output-path} - COMMAND Vulkan::dxc_exe -spirv -T vs_6_0 -E main ${current-shader-path} -Fo ${current-output-path} + COMMAND Vulkan::dxc_exe -spirv -T "${shader-type}_6_0" -E main ${current-shader-path} -Fo ${current-output-path} DEPENDS ${current-shader-path} IMPLICIT_DEPENDS CXX ${current-shader-path} VERBATIM) diff --git a/samples/01_triangle/triangle.cpp b/samples/01_triangle/triangle.cpp index aa82da8..afc632e 100644 --- a/samples/01_triangle/triangle.cpp +++ b/samples/01_triangle/triangle.cpp @@ -14,7 +14,6 @@ #include "aster/swapchain.h" #include -#include constexpr u32 MAX_FRAMES_IN_FLIGHT = 3; constexpr auto VERTEX_SHADER_FILE = "shader/triangle.vs.hlsl.spv"; @@ -23,7 +22,7 @@ constexpr auto FRAGMENT_SHADER_FILE = "shader/white.frag.glsl.spv"; bool IsSuitableDevice(const PhysicalDevice *physicalDevice); PhysicalDevice FindSuitableDevice(const PhysicalDevices &physicalDevices); QueueAllocation FindAppropriateQueueAllocation(const PhysicalDevice *physicalDevice); -eastl::optional> ReadFile(cstr fileName); +eastl::vector ReadFile(cstr fileName); vk::ShaderModule CreateShader(const Device *device, cstr shaderFile); struct Frame @@ -296,15 +295,14 @@ FindAppropriateQueueAllocation(const PhysicalDevice *physicalDevice) THEN_ABORT(vk::Result::eErrorUnknown); } -eastl::optional> +eastl::vector ReadFile(cstr fileName) { FILE *filePtr = fopen(fileName, "rb"); if (!filePtr) { - ERROR("Invalid read of {}", fileName); - return eastl::nullopt; + ERROR("Invalid read of {}", fileName) THEN_ABORT(-1); } eastl::vector outputVec; @@ -490,15 +488,7 @@ CreatePipeline(const Device *device, const Swapchain *swapchain, cstr vertexShad vk::ShaderModule CreateShader(const Device *device, cstr shaderFile) { - eastl::vector shaderCode; - if (auto res = ReadFile(shaderFile)) - { - shaderCode = res.value(); - } - else - { - ERROR("Could not open {}.", shaderFile) THEN_ABORT(-1); - } + eastl::vector shaderCode = ReadFile(shaderFile); const vk::ShaderModuleCreateInfo shaderModuleCreateInfo = { .codeSize = shaderCode.size() * sizeof(u32),