Skip to content

Ex builtin archives #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 03_DeviceSelectionAndSharedSources/Testers.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class IntrospectionTesterBase
// if the extension is generic (.glsl or .hlsl) the stage is unknown.
// But it can still be overriden from within the source with a `#pragma shader_stage`
options.stage = shaderStage == IShader::E_SHADER_STAGE::ESS_COMPUTE ? shaderStage : IShader::E_SHADER_STAGE::ESS_VERTEX; // TODO: do smth with it
options.targetSpirvVersion = device->getPhysicalDevice()->getLimits().spirvVersion;
options.preprocessorOptions.targetSpirvVersion = device->getPhysicalDevice()->getLimits().spirvVersion;
// we need to perform an unoptimized compilation with source debug info or we'll lose names of variable sin the introspection
options.spirvOptimizer = nullptr;
options.debugInfoFlags |= IShaderCompiler::E_DEBUG_INFO_FLAGS::EDIF_SOURCE_BIT;
Expand Down
2 changes: 1 addition & 1 deletion 03_DeviceSelectionAndSharedSources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class DeviceSelectionAndSharedSourcesApp final : public application_templates::M
// if the extension is generic (.glsl or .hlsl) the stage is unknown.
// But it can still be overriden from within the source with a `#pragma shader_stage`
options.stage = shaderStage == IShader::E_SHADER_STAGE::ESS_COMPUTE ? shaderStage : IShader::E_SHADER_STAGE::ESS_VERTEX; // TODO: do smth with it
options.targetSpirvVersion = m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.preprocessorOptions.targetSpirvVersion = m_device->getPhysicalDevice()->getLimits().spirvVersion;
// we need to perform an unoptimized compilation with source debug info or we'll lose names of variable sin the introspection
options.spirvOptimizer = nullptr;
options.debugInfoFlags |= IShaderCompiler::E_DEBUG_INFO_FLAGS::EDIF_SOURCE_BIT;
Expand Down
2 changes: 1 addition & 1 deletion 23_Arithmetic2UnitTest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class Workgroup2ScanTestApp final : public application_templates::BasicMultiQueu
auto compiler = make_smart_refctd_ptr<asset::CHLSLCompiler>(smart_refctd_ptr(m_system));
CHLSLCompiler::SOptions options = {};
options.stage = IShader::E_SHADER_STAGE::ESS_COMPUTE;
options.targetSpirvVersion = m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.preprocessorOptions.targetSpirvVersion = m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.spirvOptimizer = nullptr;
#ifndef _NBL_DEBUG
ISPIRVOptimizer::E_OPTIMIZER_PASS optPasses = ISPIRVOptimizer::EOP_STRIP_DEBUG_INFO;
Expand Down
2 changes: 1 addition & 1 deletion 29_Arithmetic2Bench/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ class ArithmeticBenchApp final : public examples::SimpleWindowedApplication, pub
auto compiler = make_smart_refctd_ptr<asset::CHLSLCompiler>(smart_refctd_ptr(m_system));
CHLSLCompiler::SOptions options = {};
options.stage = IShader::E_SHADER_STAGE::ESS_COMPUTE;
options.targetSpirvVersion = m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.preprocessorOptions.targetSpirvVersion = m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.spirvOptimizer = nullptr;
#ifndef _NBL_DEBUG
ISPIRVOptimizer::E_OPTIMIZER_PASS optPasses = ISPIRVOptimizer::EOP_STRIP_DEBUG_INFO;
Expand Down
4 changes: 2 additions & 2 deletions 64_EmulatedFloatTest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class CompatibilityTest final : public MonoDeviceApplication, public BuiltinReso

nbl::asset::IShaderCompiler::SCompilerOptions options = {};
options.stage = ESS_COMPUTE;
options.targetSpirvVersion = base.m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.preprocessorOptions.targetSpirvVersion = base.m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.spirvOptimizer = nullptr;
options.debugInfoFlags |= IShaderCompiler::E_DEBUG_INFO_FLAGS::EDIF_SOURCE_BIT;
options.preprocessorOptions.sourceIdentifier = source->getFilepathHint();
Expand Down Expand Up @@ -946,7 +946,7 @@ class CompatibilityTest final : public MonoDeviceApplication, public BuiltinReso

IShaderCompiler::SCompilerOptions options = {};
options.stage = ESS_COMPUTE;
options.targetSpirvVersion = base.m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.preprocessorOptions.targetSpirvVersion = base.m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.spirvOptimizer = nullptr;
options.debugInfoFlags |= IShaderCompiler::E_DEBUG_INFO_FLAGS::EDIF_SOURCE_BIT;
options.preprocessorOptions.sourceIdentifier = source->getFilepathHint();
Expand Down
2 changes: 1 addition & 1 deletion 70_FLIPFluids/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ class FLIPFluidsApp final : public SimpleWindowedApplication, public BuiltinReso
options.stage = shaderStage;
if (!(options.stage == IShader::E_SHADER_STAGE::ESS_COMPUTE || options.stage == IShader::E_SHADER_STAGE::ESS_FRAGMENT))
options.stage = IShader::E_SHADER_STAGE::ESS_VERTEX;
options.targetSpirvVersion = m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.preprocessorOptions.targetSpirvVersion = m_device->getPhysicalDevice()->getLimits().spirvVersion;
options.spirvOptimizer = nullptr;
#ifndef _NBL_DEBUG
ISPIRVOptimizer::E_OPTIMIZER_PASS optPasses = ISPIRVOptimizer::EOP_STRIP_DEBUG_INFO;
Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ if(NBL_BUILD_EXAMPLES)
target_link_libraries(${T} PUBLIC ${NBL_EXAMPLES_API_TARGET})
target_include_directories(${T} PUBLIC $<TARGET_PROPERTY:${NBL_EXAMPLES_API_TARGET},INCLUDE_DIRECTORIES>)
target_precompile_headers(${T} REUSE_FROM "${NBL_EXAMPLES_API_TARGET}")

# TODO: make them all INTERFACE if not NBL_EMBED_BUILTIN_RESOURCES and link in loop
if(NBL_EMBED_BUILTIN_RESOURCES)
LINK_BUILTIN_RESOURCES_TO_TARGET(${T} NblExtExamplesAPIBuiltinsSource)
LINK_BUILTIN_RESOURCES_TO_TARGET(${T} NblExtExamplesAPIBuiltinsInclude)
LINK_BUILTIN_RESOURCES_TO_TARGET(${T} NblExtExamplesAPIBuiltinsSPIRV)
else()
target_link_libraries(${T} PUBLIC NblExtExamplesAPIBuiltinsSPIRV)
endif()
endforeach()

NBL_ADJUST_FOLDERS(examples)
Expand Down
65 changes: 61 additions & 4 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,61 @@ nbl_create_ext_library_project(ExamplesAPI "" "${CMAKE_CURRENT_SOURCE_DIR}/src/n
set_target_properties(${LIB_NAME} PROPERTIES DISABLE_PRECOMPILE_HEADERS OFF)
target_precompile_headers(${LIB_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/nbl/examples/PCH.hpp")

set(COMMON_INCLUDE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include")

function(INTERFACE_TO_BUILTINS TARGET)
#[[
even though builtin target is static library its still valid to reuse
common PCH to boost its build speed to not preprocess entire Nabla again
]]
set_target_properties(${TARGET} PROPERTIES DISABLE_PRECOMPILE_HEADERS OFF)
target_precompile_headers(${TARGET} REUSE_FROM "${LIB_NAME}")

target_include_directories(${TARGET} PUBLIC "${COMMON_INCLUDE_DIRECTORY}")
target_link_libraries(${TARGET} INTERFACE ${LIB_NAME})
endfunction()

function(REGISTER_COMMON_BUILTINS)
cmake_parse_arguments(EX "" "TARGET;ARCHIVE_ABS_ENTRY;ARCHIVE_NAMESPACE" "GLOB_RGX" ${ARGN})

get_filename_component(INPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE)
get_filename_component(OUTPUT_SRC "${CMAKE_CURRENT_BINARY_DIR}/builtin/${EX_TARGET}/src" ABSOLUTE)
get_filename_component(OUTPUT_INCLUDE "${CMAKE_CURRENT_BINARY_DIR}/builtin/${EX_TARGET}/include" ABSOLUTE)

set(KEYS_ENTRY "${INPUT_DIRECTORY}/${EX_ARCHIVE_ABS_ENTRY}")
list(TRANSFORM EX_GLOB_RGX PREPEND "${KEYS_ENTRY}/")
file(GLOB_RECURSE KEYS RELATIVE "${KEYS_ENTRY}" CONFIGURE_DEPENDS ${EX_GLOB_RGX})

#[[
note we do force you to specify full globbing expressions relative to keys entry which we do not filter
because if runtime outputs .spv compilation artifacts/shader cache preprocessed.hlsl(s) to source you will hit CMake
reconfiguration each time the file content or timestampts change and it could lead to embeding intermediate trash
]]

unset(EXAMPLES_RESOURCES_TO_EMBED)
foreach(KEY IN LISTS KEYS)
LIST_BUILTIN_RESOURCE(EXAMPLES_RESOURCES_TO_EMBED "${KEY}")
endforeach()

ADD_CUSTOM_BUILTIN_RESOURCES(${EX_TARGET} EXAMPLES_RESOURCES_TO_EMBED "${INPUT_DIRECTORY}" "${EX_ARCHIVE_ABS_ENTRY}" "${EX_ARCHIVE_NAMESPACE}" "${OUTPUT_INCLUDE}" "${OUTPUT_SRC}")
INTERFACE_TO_BUILTINS(${EX_TARGET})
endfunction()

#! common example API builtins as static library targets linked to each example
if(NBL_EMBED_BUILTIN_RESOURCES)
REGISTER_COMMON_BUILTINS(TARGET NblExtExamplesAPIBuiltinsSource
ARCHIVE_ABS_ENTRY src/nbl/examples
ARCHIVE_NAMESPACE nbl::builtin::examples::src
GLOB_RGX *.hlsl *.txt
)

REGISTER_COMMON_BUILTINS(TARGET NblExtExamplesAPIBuiltinsInclude
ARCHIVE_ABS_ENTRY include/nbl/examples
ARCHIVE_NAMESPACE nbl::builtin::examples::include
GLOB_RGX *.hpp *.h *.hlsl *.txt
)
endif()

#! Examples API common libraries
#[[
The rule is to avoid creating additional libraries as part of the examples' common
Expand All @@ -24,18 +79,18 @@ target_precompile_headers(${LIB_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/includ
but If you have a good reason to create library because you cannot make it header only
AND you *can REUSE* the examples' PCH then go ahead anyway and put it under `src/nbl/examples`,
otherwise keep it header only - a good example would be to use our embedded-whatever-you-want tool
which does create library but can reuse example's PCH
which does create library but can reuse example's PCH (see NblExtExamplesAPIBuiltinsSource
and NblExtExamplesAPIBuiltinsInclude targets)
]]

#! NOTE: as I write it we don't have any targets there yet
add_subdirectory("src/nbl/examples" EXCLUDE_FROM_ALL)

NBL_GET_ALL_TARGETS(TARGETS)
list(REMOVE_ITEM TARGETS ${LIB_NAME})

# the Examples API proxy library CMake target name
#[[
this one gets linked to each executable automatically
this one gets linked to each executable automatically with its interface libraries
]]
set(NBL_EXAMPLES_API_TARGET ${LIB_NAME} PARENT_SCOPE)

Expand All @@ -45,4 +100,6 @@ set(NBL_EXAMPLES_API_TARGET ${LIB_NAME} PARENT_SCOPE)
then you must target_link_libraries() the lib you want as we
don't link all those libraries to each executable automatically
]]
set(NBL_EXAMPLES_API_LIBRARIES ${TARGETS} PARENT_SCOPE)
set(NBL_EXAMPLES_API_LIBRARIES ${TARGETS} PARENT_SCOPE)

NBL_ADJUST_FOLDERS(common)
43 changes: 27 additions & 16 deletions common/include/nbl/examples/common/BuiltinResourcesApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
#ifndef _NBL_EXAMPLES_BUILTIN_RESOURCE_APPLICATION_HPP_INCLUDED_
#define _NBL_EXAMPLES_BUILTIN_RESOURCE_APPLICATION_HPP_INCLUDED_


// we need a system, logger and an asset manager
#include "nbl/application_templates/MonoAssetManagerApplication.hpp"

#ifdef NBL_EMBED_BUILTIN_RESOURCES
// TODO: the include/header `nbl/examples` archive
// TODO: the source `nbl/examples` archive
// TODO: the build `nbl/examples` archive
#if __has_include("nbl/this_example/builtin/CArchive.h")
#include "nbl/this_example/builtin/CArchive.h"
#endif
#include "nbl/builtin/examples/include/CArchive.h"
#include "nbl/builtin/examples/src/CArchive.h"
#include "nbl/builtin/examples/build/CArchive.h"
#if __has_include("nbl/this_example/builtin/CArchive.h")
#include "nbl/this_example/builtin/CArchive.h"
#endif
// TODO: (**) there should be also 5th arch "nbl/this_example/builtin/build/CArchive.h"
/*
#if __has_include("nbl/this_example/builtin/build/CArchive.h")
#include "nbl/this_example/builtin/build/CArchive.h"
#endif
*/
//! this ain't meant to be the same as this_example ordinary archive
#endif


namespace nbl::examples
{

Expand All @@ -40,29 +45,35 @@ class BuiltinResourcesApplication : public virtual application_templates::MonoAs

using namespace core;

smart_refctd_ptr<system::IFileArchive> examplesHeaderArch,examplesSourceArch,examplesBuildArch,thisExampleArch;
#ifdef NBL_EMBED_BUILTIN_RESOURCES
// TODO: the 3 examples archives
smart_refctd_ptr<system::IFileArchive> examplesHeaderArch,examplesSourceArch,examplesBuildSpirvArch,thisExampleArch;
#ifdef NBL_EMBED_BUILTIN_RESOURCES
examplesHeaderArch = core::make_smart_refctd_ptr<nbl::builtin::examples::include::CArchive>(smart_refctd_ptr(m_logger));
examplesSourceArch = core::make_smart_refctd_ptr<nbl::builtin::examples::src::CArchive>(smart_refctd_ptr(m_logger));
examplesBuildSpirvArch = core::make_smart_refctd_ptr<nbl::builtin::examples::build::CArchive>(smart_refctd_ptr(m_logger));

#ifdef _NBL_THIS_EXAMPLE_BUILTIN_C_ARCHIVE_H_
thisExampleArch = make_smart_refctd_ptr<nbl::this_example::builtin::CArchive>(smart_refctd_ptr(m_logger));
#endif
#else
// TODO: (**)
#else
examplesHeaderArch = make_smart_refctd_ptr<system::CMountDirectoryArchive>(localInputCWD/"../common/include/nbl/examples",smart_refctd_ptr(m_logger),m_system.get());
examplesSourceArch = make_smart_refctd_ptr<system::CMountDirectoryArchive>(localInputCWD/"../common/src/nbl/examples",smart_refctd_ptr(m_logger),m_system.get());
// TODO: examplesBuildArch =
examplesBuildSpirvArch = make_smart_refctd_ptr<system::CMountDirectoryArchive>(NBL_EXAMPLES_BUILD_MOUNT_POINT, smart_refctd_ptr(m_logger), m_system.get());
thisExampleArch = make_smart_refctd_ptr<system::CMountDirectoryArchive>(localInputCWD/"app_resources",smart_refctd_ptr(m_logger),m_system.get());
#endif
// TODO: (**)
#endif
// yes all 3 aliases are meant to be the same
m_system->mount(std::move(examplesHeaderArch),"nbl/examples");
m_system->mount(std::move(examplesSourceArch),"nbl/examples");
// m_system->mount(std::move(examplesBuildArch),"nbl/examples");
m_system->mount(std::move(examplesBuildSpirvArch),"nbl/examples");
if (thisExampleArch)
m_system->mount(std::move(thisExampleArch),"app_resources");
// TODO: (**)

return true;
}
};

}

#endif // _CAMERA_IMPL_
#endif // _NBL_EXAMPLES_BUILTIN_RESOURCE_APPLICATION_HPP_INCLUDED_
8 changes: 4 additions & 4 deletions common/include/nbl/examples/geometry/CSimpleDebugRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
// load shader
smart_refctd_ptr<IShader> shader;
{
const auto bundle = assMan->getAsset("nbl/examples/geometry/shaders/unified.hlsl",{});
// TODO: Arek
//const auto bundle = assMan->getAsset("nbl/examples/geometry/shaders/unified.spv",{});
constexpr std::string_view key = "nbl/examples/shaders/geometry/unified.hlsl.spv";
const auto bundle = assMan->getAsset(key.data(), {});

const auto contents = bundle.getContents();
if (contents.empty() || bundle.getAssetType()!=IAsset::ET_SHADER)
return nullptr;
shader = IAsset::castDown<IShader>(contents[0]);
shader = device->compileShader({.source=shader.get()});

if (!shader)
return nullptr;
}
Expand Down
35 changes: 32 additions & 3 deletions common/src/nbl/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
# TODO builtin SPIR-V shaders
# add_subdirectory(geometry EXCLUDE_FROM_ALL)
set(SPIRV_TARGET_V 6_8)

# TODO: make docs once I get n4ce embed SPIRV tool to build system and then use the tool with Matts new shader
set(COMMON_OPTIONS
-I "${COMMON_INCLUDE_DIRECTORY}"
)

set(OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/auto-gen" )
file(WRITE "${OUTPUT_DIRECTORY}/dummy.txt" "dummy, test")

NBL_REGISTER_BUILD_MOUNT_POINT(
ARCHIVE
TARGET NblExtExamplesAPIBuiltinsSPIRV
INPUT_DIRECTORY .
OUTPUT_DIRECTORY "${OUTPUT_DIRECTORY}"
NAMESPACE nbl::builtin::examples::build
MOUNT_POINT_DEFINE NBL_EXAMPLES_BUILD_MOUNT_POINT

BUILTINS
dummy.txt # relative to ARCHIVE.OUTPUT_DIRECTORY

SHADERS
KEY shaders/geometry/unified.hlsl
COMPILE_OPTIONS ${COMMON_OPTIONS} -T lib_${SPIRV_TARGET_V}
# DEPENDS <>

# KEY <xyz>
# COMPILE_OPTIONS ${COMMON_OPTIONS} -T <target>_${SPIRV_TARGET_V}
# DEPENDS <>
)

if(NBL_EMBED_BUILTIN_RESOURCES)
INTERFACE_TO_BUILTINS(NblExtExamplesAPIBuiltinsSPIRV)
endif()
11 changes: 0 additions & 11 deletions common/src/nbl/examples/geometry/shaders/grid.vertex.hlsl

This file was deleted.

43 changes: 0 additions & 43 deletions common/src/nbl/examples/geometry/shaders/template/grid.common.hlsl

This file was deleted.