Skip to content

Commit a4d7d0a

Browse files
committed
Improved module loading
* Allows checking loading context of a module. (executable or shared library) * Implemented simple asset category for InputAction scripts.
1 parent 7bbc903 commit a4d7d0a

File tree

15 files changed

+197
-78
lines changed

15 files changed

+197
-78
lines changed

source/code/core/modules/private/module_negotiator.cxx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
namespace ice
77
{
88

9+
bool ModuleNegotiatorBase::from_app() const noexcept
10+
{
11+
return negotiator_api->fn_is_app_context(negotiator_context);
12+
}
13+
914
bool ModuleNegotiatorBase::query_apis(
1015
ice::StringID_Arg api_name,
1116
ice::u32 api_version,

source/code/core/modules/private/module_register.cxx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ namespace ice
2929
{
3030
DefaultModuleRegister* module_register;
3131
DefaultModuleEntry current_module;
32+
bool in_app_context;
3233

34+
static bool from_app(ModuleNegotiatorAPIContext*) noexcept;
3335
static bool get_module_api(ModuleNegotiatorAPIContext*, ice::StringID_Hash, ice::u32, ice::ModuleAPI*) noexcept;
3436
static bool get_module_apis(ModuleNegotiatorAPIContext*, ice::StringID_Hash, ice::u32, ice::ModuleAPI*, ice::ucount*) noexcept;
3537
static bool register_module(ModuleNegotiatorAPIContext*, ice::StringID_Hash, FnModuleSelectAPI*) noexcept;
@@ -49,7 +51,8 @@ namespace ice
4951
bool load_module(
5052
ice::Allocator& alloc,
5153
ice::FnModuleLoad* load_fn,
52-
ice::FnModuleUnload* unload_fn
54+
ice::FnModuleUnload* unload_fn,
55+
bool from_shared_library
5356
) noexcept override;
5457

5558
auto api_count(
@@ -117,7 +120,8 @@ namespace ice
117120
load_module(
118121
alloc,
119122
reinterpret_cast<ice::FnModuleLoad*>(load_proc),
120-
reinterpret_cast<ice::FnModuleUnload*>(unload_proc)
123+
reinterpret_cast<ice::FnModuleUnload*>(unload_proc),
124+
/* is_app_context */ false
121125
);
122126

123127
ice::array::push_back(_module_handles, ice::move(module_handle));
@@ -130,7 +134,8 @@ namespace ice
130134
bool DefaultModuleRegister::load_module(
131135
ice::Allocator& alloc,
132136
ice::FnModuleLoad* load_fn,
133-
ice::FnModuleUnload* unload_fn
137+
ice::FnModuleUnload* unload_fn,
138+
bool from_shared_library
134139
) noexcept
135140
{
136141
DefaultModuleEntry module_entry{
@@ -142,9 +147,11 @@ namespace ice
142147
ModuleNegotiatorAPIContext negotiator_context{
143148
.module_register = this,
144149
.current_module = module_entry,
150+
.in_app_context = from_shared_library
145151
};
146152

147153
ModuleNegotiatorAPI negotiator{
154+
.fn_is_app_context = ModuleNegotiatorAPIContext::from_app,
148155
.fn_select_apis = ModuleNegotiatorAPIContext::get_module_apis,
149156
.fn_register_api = ModuleNegotiatorAPIContext::register_module,
150157
};
@@ -220,6 +227,11 @@ namespace ice
220227
return true;
221228
}
222229

230+
bool ModuleNegotiatorAPIContext::from_app(ModuleNegotiatorAPIContext* ctx) noexcept
231+
{
232+
return ctx->in_app_context;
233+
}
234+
223235
bool ModuleNegotiatorAPIContext::get_module_apis(
224236
ice::ModuleNegotiatorAPIContext* ctx,
225237
ice::StringID_Hash api_name,

source/code/core/modules/public/ice/module.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ namespace ice
6363
#else
6464
# define IS_WORKAROUND_MODULE_INITIALIZATION(type)
6565
#endif
66+
#define ICE_WORKAROUND_MODULE_INITIALIZATION(type) IS_WORKAROUND_MODULE_INITIALIZATION(type)
6667

6768
} // namespace ice

source/code/core/modules/public/ice/module_concepts.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace ice::concepts
3232
concept ModuleNegotiator = requires(T const& t, ice::StringID_Arg id, ice::FnModuleSelectAPI* api) {
3333
{ t.query_apis(id, ice::u32{}, (ice::ModuleAPI*) nullptr, (ice::ucount*)nullptr) } -> std::convertible_to<bool>;
3434
{ t.register_api(id, api) } -> std::convertible_to<bool>;
35+
{ t.from_app() } -> std::convertible_to<bool>;
3536
} && requires(T const& t, ice::ProcAPIQuickRegisterFunc<APIConceptStruct> func) {
3637
{ t.register_api(func) } -> std::convertible_to<bool>;
3738
};

source/code/core/modules/public/ice/module_negotiator.hxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ namespace ice
1515
//! \details Allows to register and query APIs independent if the module is dynamically or statically linked.
1616
struct ModuleNegotiatorAPI
1717
{
18+
//! \returns 'true' if the module is loaded from a shared library.
19+
//! Can be used to load system modules only from the executable.
20+
using FnModuleInAppContext = bool(*)(
21+
ice::ModuleNegotiatorAPIContext*
22+
) noexcept;
23+
1824
//! \brief Used to return API pointers into the given array.
1925
//! \note If the array pointer is null, the size pointer will be set to the required size.
2026
//! \note If the array is not large enough, the returned elements will be truncated without a specific order.
@@ -34,6 +40,7 @@ namespace ice
3440
ice::FnModuleSelectAPI* fn_api_selector
3541
) noexcept;
3642

43+
FnModuleInAppContext fn_is_app_context;
3744
FnModuleSelectAPIs fn_select_apis;
3845
FnModuleRegisterAPI fn_register_api;
3946
};
@@ -48,6 +55,9 @@ namespace ice
4855
ice::ModuleNegotiatorAPIContext* negotiator_context
4956
) noexcept;
5057

58+
//! \returns 'true' when the module is loaded in the main application context.
59+
bool from_app() const noexcept;
60+
5161
//! \copy ice::ModuleQuery::query_apis
5262
bool query_apis(
5363
ice::StringID_Arg api_name,

source/code/core/modules/public/ice/module_register.hxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ namespace ice
3838
virtual bool load_module(
3939
ice::Allocator& alloc,
4040
ice::FnModuleLoad* load_fn,
41-
ice::FnModuleUnload* unload_fn
41+
ice::FnModuleUnload* unload_fn,
42+
bool from_shared_library
4243
) noexcept = 0;
4344

4445
//! \brief Loads a module using a module info structure.
4546
virtual bool load_module(ice::Allocator& alloc, ice::ModuleInfo const& module_info) noexcept
4647
{
47-
return this->load_module(alloc, module_info.fn_load, module_info.fn_unload);
48+
return this->load_module(alloc, module_info.fn_load, module_info.fn_unload, true);
4849
}
4950

5051
//! \brief Loads a module using a module type. This is the preferred way of loading modules implicitly.

source/code/modules/shader_tools/private/shader_tools.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace ice
4646

4747
auto compiler_supported_shader_resources(
4848
ice::Span<ice::Shard const> params
49-
) noexcept -> ice::Span<ice::String>
49+
) noexcept -> ice::Span<ice::String const>
5050
{
5151
if constexpr (ice::build::is_windows)
5252
{
@@ -144,6 +144,7 @@ namespace ice
144144
static void v1_compiler_api(ice::api::resource_compiler::v1::ResourceCompilerAPI& api) noexcept
145145
{
146146
#if ISP_WINDOWS || ISP_LINUX || ISP_WEBAPP
147+
api.id_category = "ice/shader-resource"_sid;
147148
api.fn_prepare_context = compiler_context_prepare;
148149
api.fn_cleanup_context = compiler_context_cleanup;
149150
api.fn_supported_resources = compiler_supported_shader_resources;

source/code/modules/vulkan_renderer/private/vk_shader_asset.cxx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace ice::render::vk
5757
) noexcept
5858
{
5959
#if ISP_WINDOWS || ISP_LINUX
60-
static ice::String constexpr extensions[]{ ".asl", ".glsl",".spv" };
60+
static ice::String constexpr extensions[]{ ".asl", ".glsl", ".spv" };
6161
#else
6262
static ice::String constexpr extensions[]{ ".spv" };
6363
#endif
@@ -69,9 +69,22 @@ namespace ice::render::vk
6969
};
7070

7171
#if ISP_WINDOWS || ISP_LINUX
72-
ice::ResourceCompiler compiler{ };
73-
module_query.query_api(compiler);
74-
asset_category_archive.register_category(ice::render::AssetCategory_Shader, definition, &compiler);
72+
static ice::HostAllocator host_alloc;
73+
74+
ice::ResourceCompiler const* selected_compiler = nullptr;
75+
ice::Array<ice::ResourceCompiler> compilers{ host_alloc };
76+
module_query.query_apis(compilers);
77+
78+
for (ice::ResourceCompiler const& compiler : compilers)
79+
{
80+
if (compiler.id_category == "ice/shader-resource"_sid)
81+
{
82+
selected_compiler = ice::addressof(compiler);
83+
break;
84+
}
85+
}
86+
87+
asset_category_archive.register_category(ice::render::AssetCategory_Shader, definition, selected_compiler);
7588
#else
7689
asset_category_archive.register_category(ice::render::AssetCategory_Shader, definition, nullptr);
7790
#endif

source/code/systems/input_action_system/input_action_system.bff

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
.Uses = {
1818
'utils'
1919
'input_system'
20+
'asset_system'
2021
}
2122
]
2223

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,65 @@
11
#include <ice/input_action.hxx>
22

3+
#include <ice/module.hxx>
4+
#include <ice/module_register.hxx>
5+
#include <ice/resource_compiler_api.hxx>
6+
#include <ice/asset_module.hxx>
7+
#include <ice/asset_category_archive.hxx>
8+
#include <ice/log_module.hxx>
9+
310
namespace ice
411
{
512

13+
void asset_category_shader_definition(
14+
ice::AssetCategoryArchive& asset_category_archive,
15+
ice::ModuleQuery const& module_query
16+
) noexcept
17+
{
18+
static ice::String ext[]{ ".ias" };
19+
static ice::AssetCategoryDefinition const definition{
20+
.resource_extensions = ext
21+
};
22+
asset_category_archive.register_category(ice::AssetCategory_InputActionsScript, definition);
23+
}
24+
25+
struct InputActionsModule : public ice::Module<InputActionsModule>
26+
{
27+
28+
#if 0
29+
auto ias_compiler_supported_resources(
30+
ice::Span<ice::Shard const> params
31+
) noexcept -> ice::Span<ice::String const>
32+
{
33+
static ice::String ext[]{ ".ias" };
34+
return ext;
35+
}
36+
37+
static void v1_compiler_api(ice::api::resource_compiler::v1::ResourceCompilerAPI& api) noexcept
38+
{
39+
api.id_category = "ice/ias-script-resource"_sid;
40+
api.fn_supported_resources = ias_compiler_supported_resources;
41+
}
42+
#endif
43+
44+
static void v1_archive_api(ice::detail::asset_system::v1::AssetArchiveAPI& api) noexcept
45+
{
46+
api.fn_register_categories = asset_category_shader_definition;
47+
}
48+
49+
static bool on_load(ice::Allocator& alloc, ice::ModuleNegotiator auto& negotiator) noexcept
50+
{
51+
// Since we are on the 'system' layer, we can be part of multiple dynamic libararies, and prefere to not be loaded from them.
52+
if (negotiator.from_app())
53+
{
54+
#if 0 // Currently we don't really support compiling for InputAction scripts.
55+
negotiator.register_api(v1_compiler_api);
56+
#endif
57+
negotiator.register_api(v1_archive_api);
58+
}
59+
return true;
60+
}
61+
62+
ICE_WORKAROUND_MODULE_INITIALIZATION(InputActionsModule);
63+
};
64+
665
} // namespace ice

0 commit comments

Comments
 (0)