Run spirv-opt when compiling shaders
This commit is contained in:
@@ -32,6 +32,7 @@ option(WIVRN_BUILD_WIVRNCTL "Build wivrnctl" ON)
|
|||||||
option(WIVRN_BUILD_DISSECTOR "Build Wireshark dissector" OFF)
|
option(WIVRN_BUILD_DISSECTOR "Build Wireshark dissector" OFF)
|
||||||
option(WIVRN_BUILD_TEST "Build test binaries" OFF)
|
option(WIVRN_BUILD_TEST "Build test binaries" OFF)
|
||||||
option(WIVRN_WERROR "Treat warnings as errors" OFF)
|
option(WIVRN_WERROR "Treat warnings as errors" OFF)
|
||||||
|
option(WIVRN_OPTIMIZE_SHADERS "Run siprv-opt on compiled shaders" ON)
|
||||||
|
|
||||||
option(WIVRN_USE_NVENC "Enable nvenc (Nvidia) hardware encoder" ON)
|
option(WIVRN_USE_NVENC "Enable nvenc (Nvidia) hardware encoder" ON)
|
||||||
option(WIVRN_USE_VAAPI "Enable vaapi (AMD/Intel) hardware encoder" ON)
|
option(WIVRN_USE_VAAPI "Enable vaapi (AMD/Intel) hardware encoder" ON)
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ file(GLOB LOCAL_SOURCE CONFIGURE_DEPENDS
|
|||||||
|
|
||||||
file(GLOB_RECURSE VULKAN_SHADERS CONFIGURE_DEPENDS "*.glsl")
|
file(GLOB_RECURSE VULKAN_SHADERS CONFIGURE_DEPENDS "*.glsl")
|
||||||
target_sources(wivrn PRIVATE ${LOCAL_SOURCE} ${VULKAN_SHADERS})
|
target_sources(wivrn PRIVATE ${LOCAL_SOURCE} ${VULKAN_SHADERS})
|
||||||
wivrn_compile_glsl(wivrn ${VULKAN_SHADERS})
|
wivrn_compile_glsl(wivrn SHADERS ${VULKAN_SHADERS})
|
||||||
|
|
||||||
target_link_libraries(wivrn
|
target_link_libraries(wivrn
|
||||||
Boost::locale
|
Boost::locale
|
||||||
|
|||||||
@@ -35,7 +35,6 @@
|
|||||||
#include <glm/gtc/matrix_access.hpp>
|
#include <glm/gtc/matrix_access.hpp>
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
#include <glm/gtx/quaternion.hpp>
|
#include <glm/gtx/quaternion.hpp>
|
||||||
#include <map>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
@@ -43,8 +42,6 @@
|
|||||||
#include <vulkan/vulkan_enums.hpp>
|
#include <vulkan/vulkan_enums.hpp>
|
||||||
#include <vulkan/vulkan_structs.hpp>
|
#include <vulkan/vulkan_structs.hpp>
|
||||||
|
|
||||||
extern const std::map<std::string, std::vector<uint32_t>> shaders;
|
|
||||||
|
|
||||||
// TODO move in lobby?
|
// TODO move in lobby?
|
||||||
vk::Format scene_renderer::find_usable_image_format(
|
vk::Format scene_renderer::find_usable_image_format(
|
||||||
vk::raii::PhysicalDevice physical_device,
|
vk::raii::PhysicalDevice physical_device,
|
||||||
|
|||||||
@@ -18,12 +18,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
#include "wivrn_shaders.h"
|
||||||
|
|
||||||
vk::raii::ShaderModule load_shader(vk::raii::Device & device, const std::vector<uint32_t> & spirv)
|
vk::raii::ShaderModule load_shader(vk::raii::Device & device, const std::vector<uint32_t> & spirv)
|
||||||
{
|
{
|
||||||
vk::ShaderModuleCreateInfo create_info{
|
vk::ShaderModuleCreateInfo create_info{
|
||||||
.codeSize = spirv.size() * sizeof(uint32_t),
|
.codeSize = spirv.size() * sizeof(uint32_t),
|
||||||
.pCode = data(spirv),
|
.pCode = spirv.data(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return vk::raii::ShaderModule{device, create_info};
|
return vk::raii::ShaderModule{device, create_info};
|
||||||
|
|||||||
@@ -19,12 +19,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <vulkan/vulkan_raii.hpp>
|
#include <vulkan/vulkan_raii.hpp>
|
||||||
|
|
||||||
extern const std::map<std::string, std::vector<uint32_t>> shaders;
|
|
||||||
|
|
||||||
vk::raii::ShaderModule load_shader(vk::raii::Device & device, const std::vector<uint32_t> & spirv);
|
vk::raii::ShaderModule load_shader(vk::raii::Device & device, const std::vector<uint32_t> & spirv);
|
||||||
vk::raii::ShaderModule load_shader(vk::raii::Device & device, const std::string & name);
|
vk::raii::ShaderModule load_shader(vk::raii::Device & device, const std::string & name);
|
||||||
|
|||||||
+54
-9
@@ -1,5 +1,8 @@
|
|||||||
|
set(_THIS_MODULE_BASE_DIR "${CMAKE_CURRENT_LIST_DIR}")
|
||||||
|
|
||||||
function(compile_glsl_aux shader_stage shader_name glsl_filename output)
|
find_program(HEXDUMP hexdump REQUIRED)
|
||||||
|
|
||||||
|
function(compile_glsl_aux shader_stage shader_name glsl_filename output target_env)
|
||||||
|
|
||||||
string(TOUPPER ${shader_stage} shader_stage_upper)
|
string(TOUPPER ${shader_stage} shader_stage_upper)
|
||||||
cmake_path(REPLACE_FILENAME output "${shader_name}.spv" OUTPUT_VARIABLE SPV_FILE)
|
cmake_path(REPLACE_FILENAME output "${shader_name}.spv" OUTPUT_VARIABLE SPV_FILE)
|
||||||
@@ -11,11 +14,29 @@ function(compile_glsl_aux shader_stage shader_name glsl_filename output)
|
|||||||
")
|
")
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT "${SPV_FILE}"
|
OUTPUT "${SPV_FILE}-nopt"
|
||||||
COMMAND Vulkan::glslangValidator -V -S ${shader_stage} -D${shader_stage_upper}_SHADER ${in_file} -x -o "${SPV_FILE}"
|
COMMAND Vulkan::glslangValidator -V --target-env ${target_env} -S ${shader_stage} -D${shader_stage_upper}_SHADER ${in_file} -o "${SPV_FILE}-nopt"
|
||||||
DEPENDS "${glsl_filename}"
|
DEPENDS "${glsl_filename}"
|
||||||
VERBATIM
|
VERBATIM
|
||||||
)
|
)
|
||||||
|
if (WIVRN_OPTIMIZE_SHADERS)
|
||||||
|
add_custom_command(
|
||||||
|
OUTPUT "${SPV_FILE}-opt"
|
||||||
|
COMMAND spirv-opt --target-env=${target_env} -O "${SPV_FILE}-nopt" -o "${SPV_FILE}-opt"
|
||||||
|
DEPENDS "${SPV_FILE}-nopt"
|
||||||
|
VERBATIM
|
||||||
|
)
|
||||||
|
set(_spv "${SPV_FILE}-opt")
|
||||||
|
else()
|
||||||
|
set(_spv "${SPV_FILE}-nopt")
|
||||||
|
endif()
|
||||||
|
set(_format "/4 \"0x%02X, \"")
|
||||||
|
add_custom_command(
|
||||||
|
OUTPUT "${SPV_FILE}"
|
||||||
|
COMMAND "${HEXDUMP}" -ve "${_format}" ${_spv} > "${SPV_FILE}"
|
||||||
|
DEPENDS "${_spv}"
|
||||||
|
VERBATIM
|
||||||
|
)
|
||||||
|
|
||||||
get_source_file_property(deps "${output}" OBJECT_DEPENDS)
|
get_source_file_property(deps "${output}" OBJECT_DEPENDS)
|
||||||
if (deps)
|
if (deps)
|
||||||
@@ -30,33 +51,57 @@ endfunction()
|
|||||||
|
|
||||||
|
|
||||||
function(wivrn_compile_glsl target)
|
function(wivrn_compile_glsl target)
|
||||||
|
set(options)
|
||||||
|
set(oneValueArgs TARGET_ENV NAMESPACE)
|
||||||
|
set(multiValueArgs SHADERS)
|
||||||
|
cmake_parse_arguments(_args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
||||||
|
|
||||||
|
if(NOT _args_TARGET_ENV)
|
||||||
|
set(_args_TARGET_ENV vulkan1.0)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (_args_NAMESPACE)
|
||||||
|
set(_namespace_begin "namespace ${_args_NAMESPACE} {")
|
||||||
|
set(_namespace_end "}")
|
||||||
|
endif()
|
||||||
|
|
||||||
cmake_path(APPEND OUTPUT "${CMAKE_CURRENT_BINARY_DIR}" "${target}_shaders.cpp")
|
cmake_path(APPEND OUTPUT "${CMAKE_CURRENT_BINARY_DIR}" "${target}_shaders.cpp")
|
||||||
|
|
||||||
|
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${target}_shaders.h" "\
|
||||||
|
#include <cstdint>
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
${_namespace_begin}
|
||||||
|
extern const std::map<std::string, std::vector<uint32_t>> shaders;
|
||||||
|
${_namespace_end}")
|
||||||
|
|
||||||
file(WRITE ${OUTPUT} "\
|
file(WRITE ${OUTPUT} "\
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
${_namespace_begin}
|
||||||
extern const std::map<std::string, std::vector<uint32_t>> shaders = {
|
extern const std::map<std::string, std::vector<uint32_t>> shaders = {
|
||||||
")
|
")
|
||||||
|
|
||||||
foreach(in_file IN LISTS ARGN)
|
foreach(in_file IN LISTS _args_SHADERS)
|
||||||
if (in_file MATCHES "\.\(vert|frag|tesc|tese|geom|comp\)\.glsl$")
|
if (in_file MATCHES "\.\(vert|frag|tesc|tese|geom|comp\)\(\.glsl\)?$")
|
||||||
set(shader_stage ${CMAKE_MATCH_1})
|
set(shader_stage ${CMAKE_MATCH_1})
|
||||||
cmake_path(GET in_file STEM LAST_ONLY shader_name)
|
cmake_path(GET in_file STEM LAST_ONLY shader_name)
|
||||||
compile_glsl_aux(${shader_stage} ${shader_name} ${in_file} ${OUTPUT})
|
compile_glsl_aux(${shader_stage} ${shader_name} ${in_file} ${OUTPUT} ${_args_TARGET_ENV})
|
||||||
else()
|
else()
|
||||||
cmake_path(GET in_file STEM LAST_ONLY shader_name)
|
cmake_path(GET in_file STEM LAST_ONLY shader_name)
|
||||||
compile_glsl_aux(vert ${shader_name}.vert ${in_file} ${OUTPUT})
|
compile_glsl_aux(vert ${shader_name}.vert ${in_file} ${OUTPUT} ${_args_TARGET_ENV})
|
||||||
compile_glsl_aux(frag ${shader_name}.frag ${in_file} ${OUTPUT})
|
compile_glsl_aux(frag ${shader_name}.frag ${in_file} ${OUTPUT} ${_args_TARGET_ENV})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
file(APPEND ${OUTPUT} "};")
|
file(APPEND ${OUTPUT} "};${_namespace_end}")
|
||||||
|
|
||||||
target_sources(${target} PRIVATE ${OUTPUT})
|
target_sources(${target} PRIVATE ${OUTPUT})
|
||||||
|
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
|
||||||
|
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|||||||
@@ -21,6 +21,9 @@
|
|||||||
pkgs.libpng
|
pkgs.libpng
|
||||||
pkgs.libarchive
|
pkgs.libarchive
|
||||||
];
|
];
|
||||||
|
extraNativeBuildInputs = [
|
||||||
|
pkgs.util-linux
|
||||||
|
];
|
||||||
|
|
||||||
package = pkgs.enableDebugging (pkgs.wivrn.overrideAttrs (finalAttrs: oldAttrs: {
|
package = pkgs.enableDebugging (pkgs.wivrn.overrideAttrs (finalAttrs: oldAttrs: {
|
||||||
src = ./.;
|
src = ./.;
|
||||||
@@ -46,6 +49,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
buildInputs = oldAttrs.buildInputs ++ extraBuildInputs;
|
buildInputs = oldAttrs.buildInputs ++ extraBuildInputs;
|
||||||
|
nativeBuildInputs = oldAttrs.nativeBuildInputs ++ extraNativeBuildInputs;
|
||||||
|
|
||||||
dontWrapQtApps = true;
|
dontWrapQtApps = true;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user