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 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ 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}")
LINK_BUILTIN_RESOURCES_TO_TARGET(${T} NblExtExamplesAPIBuiltinsSource)
LINK_BUILTIN_RESOURCES_TO_TARGET(${T} NblExtExamplesAPIBuiltinsInclude)
LINK_BUILTIN_RESOURCES_TO_TARGET(${T} NblExtExamplesAPIBuiltinsSPIRV)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why SPIR-V, could be anything in the build dir really

endforeach()

NBL_ADJUST_FOLDERS(examples)
Expand Down
64 changes: 61 additions & 3 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 "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/include")
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,7 +79,8 @@ 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
Expand All @@ -35,7 +91,7 @@ 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 +101,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/spirv/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/spirv/CArchive.h"
/*
#if __has_include("nbl/this_example/builtin/build/spirv/CArchive.h")
#include "nbl/this_example/builtin/build/spirv/CArchive.h"
#endif
*/
//! this ain't meant to be the same as this_example ordinary archive
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need 3 (include,src,build) to keep symmetry with rest, then

#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::spirv::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_SPIRV_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_
20 changes: 16 additions & 4 deletions common/include/nbl/examples/geometry/CSimpleDebugRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,26 @@ 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",{});
// TODO & NOTE: tmp, maybe I will turn it into CMake option
#define NBL_USE_PRECOMPILED_SPIRV

#ifdef NBL_USE_PRECOMPILED_SPIRV
constexpr std::string_view key = "nbl/examples/shaders/geometry/unified.hlsl.spv";
#else
constexpr std::string_view key = "nbl/examples/shaders/geometry/unified.hlsl";
#endif // NBL_USE_PRECOMPILED_SPIRV

const auto bundle = assMan->getAsset(key.data(), {});

//const auto bundle = assMan->getAsset("nbl/examples/shaders/geometry/unified.hlsl.spv",{});
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()});

#ifndef NBL_USE_PRECOMPILED_SPIRV
shader = device->compileShader({ .source = shader.get() });
#endif // NBL_USE_PRECOMPILED_SPIRV
if (!shader)
return nullptr;
}
Expand Down
23 changes: 20 additions & 3 deletions common/src/nbl/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# 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}"
)

NBL_REGISTER_SPIRV_SHADERS(
MOUNT_POINT_DEFINE
NBL_EXAMPLES_BUILD_SPIRV_MOUNT_POINT

ARCHIVE
TARGET NblExtExamplesAPIBuiltinsSPIRV
INPUT_DIRECTORY .
NAMESPACE nbl::builtin::examples::build::spirv

INPUTS
KEY shaders/geometry/unified.hlsl COMPILE_OPTIONS ${COMMON_OPTIONS} -T lib_${SPIRV_TARGET_V}
# KEY <xyz> COMPILE_OPTIONS ${COMMON_OPTIONS} -T <target>_${SPIRV_TARGET_V}
)

INTERFACE_TO_BUILTINS(NblExtExamplesAPIBuiltinsSPIRV)
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.