Commit 37a21837 authored by Florian Oetke's avatar Florian Oetke
Browse files

Merge branch 'feature/native_msvc_resource_embedding' into develop

parents f572239c 6a0697cc
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
if(MSVC)
find_program(MIRRAGE_AUX_ASSEMBLER as)
if(MIRRAGE_AUX_ASSEMBLER)
message(STATUS "Using MIRRAGE_AUX_ASSEMBLER for asset embedding: ${MIRRAGE_AUX_ASSEMBLER}")
else()
message(FATAL_ERROR "Can't find a binary for MIRRAGE_AUX_ASSEMBLER. The asset embedding mechanism of mirrage currently doesn't work natively with MSVC. To support MSVC despite this, MinGWs assembler is required to create embedded asset OBJs. However, this binary wasn't found using find_program().")
endif()
endif()
macro(mirrage_embed_asset)
set(options)
set(oneValueArgs TARGET EXPORT)
set(multiValueArgs SOURCES DEPENDS)
cmake_parse_arguments(EMBED_ASSET "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(target ${EMBED_ASSET_TARGET})
#optional: generated files to depend on
macro(mirrage_embed_asset target src_files)
string (REPLACE ";" "$<SEMICOLON>" src_files_str "${src_files}")
string (REPLACE ";" "$<SEMICOLON>" src_files_str "${EMBED_ASSET_SOURCES}")
set(ID "mirrage_embedded_asset_${target}")
if(MSVC)
set(EMBED_SRC_FILE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets_${target}.rc")
set(EMBED_MODE "MSVC")
elseif(APPLE)
set(EMBED_SRC_FILE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
set(EMBED_MODE "APPLE")
else()
set(EMBED_SRC_FILE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
set(EMBED_MODE "ASM")
endif()
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s"
COMMAND ${CMAKE_COMMAND} -DMIRRAGE_ROOT_DIR=${MIRRAGE_ROOT_DIR} -DID=${ID} -DSRC_FILES=${src_files_str} -DDST_DIR=${CMAKE_CURRENT_BINARY_DIR} -P ${MIRRAGE_ROOT_DIR}/embed_recursive_into_asm.cmake
DEPENDS ${ARGN}
add_custom_command(OUTPUT ${EMBED_SRC_FILE}
COMMAND ${CMAKE_COMMAND} -DMIRRAGE_ROOT_DIR=${MIRRAGE_ROOT_DIR} -DEMBED_MODE=${EMBED_MODE} -DID=${ID} -DSRC_FILES=${src_files_str} -DDST_DIR=${CMAKE_CURRENT_BINARY_DIR} -DEMBED_SRC_FILE=${EMBED_SRC_FILE} -P ${MIRRAGE_ROOT_DIR}/embed_recursive_into_asm.cmake
DEPENDS ${EMBED_ASSET_DEPENDS}
VERBATIM
)
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.cpp" CONTENT
"#include <mirrage/asset/embedded_asset.hpp>
set(PLATFORM_EMBED_SRC "")
extern \"C\" const char ${ID}[];
extern \"C\" int ${ID}_size;
if(MSVC)
string(TOUPPER "${ID}" RES_ID)
set(PLATFORM_EMBED_SRC
"#include <windows.h>
#include <stdexcept>
static auto create_asset() -> mirrage::asset::Embedded_asset {
auto handle = GetModuleHandle(NULL);
auto res = FindResource(handle, \"${RES_ID}\", RT_RCDATA);
if(!res){
throw std::runtime_error(\"Couldn't find ressource ${RES_ID}.\");
}
return mirrage::asset::Embedded_asset(\"${target}\",
gsl::span<const gsl::byte>{reinterpret_cast<const gsl::byte*>(LockResource(LoadResource(handle, res))),
static_cast<int>(SizeofResource(handle, res))});
}
static auto mirrage_asset_reg = create_asset();
")
else()
set(PLATFORM_EMBED_SRC
"extern \"C\" const char ${ID}[];
extern \"C\" const int ${ID}_size;
static auto mirrage_asset_reg = mirrage::asset::Embedded_asset(\"${target}\",
gsl::span<const gsl::byte>{reinterpret_cast<const gsl::byte*>(&*${ID}), ${ID}_size});
")
endif()
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.cpp" CONTENT
"#include <mirrage/asset/embedded_asset.hpp>
${PLATFORM_EMBED_SRC}
void ref_embedded_assets_${target}() {
volatile mirrage::asset::Embedded_asset* x = &mirrage_asset_reg;
......@@ -38,17 +74,21 @@ void ref_embedded_assets_${target}() {
}
")
if(MSVC)
add_custom_command( OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s.obj"
COMMAND ${MIRRAGE_AUX_ASSEMBLER} "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s" -o "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s.obj"
MAIN_DEPENDENCY "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s"
COMMENT "Running MIRRAGE_AUX_ASSEMBLER (${MIRRAGE_AUX_ASSEMBLER}) for embedded assets of target ${target}."
VERBATIM)
target_link_libraries(${target} PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s.obj")
if(NOT MSVC)
target_sources(${target} PRIVATE ${EMBED_SRC_FILE})
endif()
target_sources(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
target_sources(${target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.cpp")
add_custom_target(mirrage_embedded_assets_${target} DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
add_custom_target(mirrage_embedded_assets_${target} DEPENDS ${EMBED_SRC_FILE})
add_dependencies(${target} mirrage_embedded_assets_${target})
if(MSVC)
add_library(mirrage_embedded_assets_obj_${target} OBJECT "${EMBED_SRC_FILE}")
target_link_libraries(${target} INTERFACE $<TARGET_OBJECTS:mirrage_embedded_assets_obj_${target}>)
add_dependencies(mirrage_embedded_assets_obj_${target} mirrage_embedded_assets_${target})
if(EMBED_ASSET_EXPORT)
install(TARGETS mirrage_embedded_assets_obj_${target}
EXPORT ${EMBED_ASSET_EXPORT}
OBJECTS DESTINATION lib)
endif()
endif()
endmacro()
......@@ -31,6 +31,16 @@ execute_process(COMMAND ${CMAKE_COMMAND} -E tar "cfv" "${DST_DIR}/embedded_asset
OUTPUT_QUIET
)
configure_file(${MIRRAGE_ROOT_DIR}/embedded_assets.s.in "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
execute_process(COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s" OUTPUT_QUIET)
if("${EMBED_MODE} " STREQUAL "MSVC ")
string(TOUPPER "${ID}" RES_ID)
configure_file(${MIRRAGE_ROOT_DIR}/embedded_assets.rc.in "${EMBED_SRC_FILE}")
execute_process(COMMAND ${CMAKE_COMMAND} -E touch "${EMBED_SRC_FILE}" OUTPUT_QUIET)
else()
if("${EMBED_MODE} " STREQUAL "APPLE ")
set(SECTION ".const_data")
else()
set(SECTION ".section .rodata")
endif()
configure_file(${MIRRAGE_ROOT_DIR}/embedded_assets.s.in "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s")
execute_process(COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_CURRENT_BINARY_DIR}/embedded_assets.s" OUTPUT_QUIET)
endif()
${RES_ID} RCDATA "${DST_DIR}/embedded_assets.zip"
.global ${ID}
.global ${ID}_size
.section .rodata
${SECTION}
${ID}:
.incbin "${DST_DIR}/embedded_assets.zip"
1:
......
......@@ -32,7 +32,10 @@ cfg:gui = settings/gui.json
loc: = loc/
pl_cache: = pipeline_caches
")
mirrage_embed_asset(mirrage "${CMAKE_CURRENT_BINARY_DIR}/assets_mirrage_core.map")
mirrage_embed_asset(TARGET mirrage
EXPORT mirrage_targets
SOURCES "${CMAKE_CURRENT_BINARY_DIR}/assets_mirrage_core.map"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/assets_mirrage_core.map")
set(MIRRAGE_DEFAULT_COMPILER_ARGS ${MIRRAGE_DEFAULT_COMPILER_ARGS})
target_compile_options(mirrage PRIVATE ${MIRRAGE_DEFAULT_COMPILER_ARGS})
......
......@@ -25,8 +25,13 @@ cfg:console_history = console_history.txt
font: = fonts/
font:default_font = fonts/default_font.ttf
")
mirrage_embed_asset(mirrage_gui "${CMAKE_CURRENT_BINARY_DIR}/assets_mirrage_gui.map;${CMAKE_CURRENT_SOURCE_DIR}/assets/fonts"
"${CMAKE_CURRENT_BINARY_DIR}/assets_mirrage_gui.map")
mirrage_embed_asset(TARGET mirrage_gui
EXPORT mirrage_gui_targets
SOURCES
"${CMAKE_CURRENT_BINARY_DIR}/assets_mirrage_gui.map"
"${CMAKE_CURRENT_SOURCE_DIR}/assets/fonts"
DEPENDS
"${CMAKE_CURRENT_BINARY_DIR}/assets_mirrage_gui.map")
set(MIRRAGE_DEFAULT_COMPILER_ARGS ${MIRRAGE_DEFAULT_COMPILER_ARGS})
target_compile_options(mirrage_gui PRIVATE ${MIRRAGE_DEFAULT_COMPILER_ARGS})
......
......@@ -75,13 +75,17 @@ add_library(mirrage_renderer STATIC
add_library(mirrage::renderer ALIAS mirrage_renderer)
target_compile_features(mirrage_renderer PUBLIC cxx_std_17)
set(EMBEDDED_ASSETS
"${CMAKE_CURRENT_BINARY_DIR}/shader"
"${CMAKE_CURRENT_SOURCE_DIR}/assets/textures"
"${CMAKE_CURRENT_SOURCE_DIR}/assets/assets_mirrage_renderer_shader.map"
"${CMAKE_CURRENT_SOURCE_DIR}/assets/assets_mirrage_renderer_textures.map"
)
mirrage_embed_asset(mirrage_renderer "${EMBEDDED_ASSETS}" ${SPIRV_BINARY_FILES})
mirrage_embed_asset(TARGET mirrage_renderer
EXPORT mirrage_renderer_targets
SOURCES
"${CMAKE_CURRENT_BINARY_DIR}/shader"
"${CMAKE_CURRENT_SOURCE_DIR}/assets/textures"
"${CMAKE_CURRENT_SOURCE_DIR}/assets/assets_mirrage_renderer_shader.map"
"${CMAKE_CURRENT_SOURCE_DIR}/assets/assets_mirrage_renderer_textures.map"
DEPENDS
${SPIRV_BINARY_FILES}
"${CMAKE_CURRENT_SOURCE_DIR}/assets/assets_mirrage_renderer_shader.map"
"${CMAKE_CURRENT_SOURCE_DIR}/assets/assets_mirrage_renderer_textures.map")
option(MIRRAGE_HISTOGRAM_DEBUG_VIEW "Enable debug view for tone mapping pass" OFF)
if(MIRRAGE_HISTOGRAM_DEBUG_VIEW)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment