Skip to content

Commit 3f5ff74

Browse files
committed
move get_resources template declaration to generated builtin resources header, introduce ADD_CUSTOM_BUILTIN_RESOURCES that creates a target with custom namespace and embed resources as static library. Wrap NBL_ADD_BUILTIN_RESOURCES with ADD_CUSTOM_BUILTIN_RESOURCES. Correct python scripts responsible for builtin resources
1 parent 7b6c357 commit 3f5ff74

File tree

4 files changed

+46
-52
lines changed

4 files changed

+46
-52
lines changed

include/nbl/core/string/StringLiteral.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,4 @@ struct StringLiteral
2323
// for compatibility's sake
2424
#define NBL_CORE_UNIQUE_STRING_LITERAL_TYPE(STRING_LITERAL) nbl::core::StringLiteral(STRING_LITERAL)
2525

26-
namespace nbl::builtin
27-
{
28-
// if you attempt to use this without `NBL_EMBED_BUILTIN_RESOURCES_` CMake option, you will get loads of undefined references
29-
template<nbl::core::StringLiteral Path>
30-
const std::pair<const uint8_t*, size_t> get_resource();
31-
}
32-
3326
#endif // _NBL_CORE_STRING_LITERAL_H_INCLUDED_

src/nbl/builtin/CMakeLists.txt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ set(NBL_RESOURCES_TO_EMBED
238238
"nbl/builtin/glsl/blit/normalization/shared_normalization.glsl"
239239
PARENT_SCOPE)
240240

241-
function(NBL_ADD_BUILTIN_RESOURCES)
241+
function(ADD_CUSTOM_BUILTIN_RESOURCES _TARGET_NAME_ _NAMESPACE_ _CUSTOM_RESOURCES_TO_EMBED_ _OUTPUT_DIRECTORY_)
242242
set(NBL_BUILTIN_HEADER_GEN_PY "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/builtinHeaderGen.py")
243243
set(NBL_BUILTIN_DATA_GEN_PY "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/builtinDataGen.py")
244244

245-
foreach(X IN LISTS NBL_RESOURCES_TO_EMBED)
245+
foreach(X IN LISTS _CUSTOM_RESOURCES_TO_EMBED_)
246246
list(APPEND NBL_DEPENDENCY_FILES "${NBL_ROOT_PATH}/include/${X}")
247247
endforeach()
248248

@@ -251,12 +251,14 @@ function(NBL_ADD_BUILTIN_RESOURCES)
251251

252252
set(NBL_RESOURCES_LIST_FILE "${NBL_ROOT_PATH_BINARY}/include/nbl/builtin/resources.txt")
253253

254-
string(REPLACE ";" "," RESOURCES_ARGS "${NBL_RESOURCES_TO_EMBED}")
254+
string(REPLACE ";" "," RESOURCES_ARGS "${_CUSTOM_RESOURCES_TO_EMBED_}")
255255
file(MAKE_DIRECTORY "${NBL_ROOT_PATH_BINARY}/include/nbl/builtin")
256256
file(WRITE "${NBL_RESOURCES_LIST_FILE}" "${RESOURCES_ARGS}")
257257

258-
set(NBL_BUILTIN_RESOURCES_HEADER "${NBL_ROOT_PATH_BINARY}/include/nbl/builtin/builtinResources.h")
259-
set(NBL_BUILTIN_RESOURCE_DATA_SOURCE "${NBL_ROOT_PATH_BINARY}/include/nbl/builtin/builtinResourceData.cpp")
258+
set(NBL_BS_HEADER_FILENAME "builtinResources.h")
259+
set(NBL_BS_DATA_SOURCE_FILENAME "builtinResourceData.cpp")
260+
set(NBL_BUILTIN_RESOURCES_HEADER "${_OUTPUT_DIRECTORY_}/${NBL_BS_HEADER_FILENAME}")
261+
set(NBL_BUILTIN_RESOURCE_DATA_SOURCE "${_OUTPUT_DIRECTORY_}/${NBL_BS_DATA_SOURCE_FILENAME}")
260262

261263
set(_NBL_SOURCES_
262264
"${NBL_BUILTIN_RESOURCES_HEADER}"
@@ -265,23 +267,27 @@ function(NBL_ADD_BUILTIN_RESOURCES)
265267

266268
add_custom_command(
267269
OUTPUT "${NBL_BUILTIN_RESOURCES_HEADER}" "${NBL_BUILTIN_RESOURCE_DATA_SOURCE}"
268-
COMMAND "${PYTHON_EXECUTABLE}" "${NBL_BUILTIN_HEADER_GEN_PY}" "${NBL_BUILTIN_RESOURCES_HEADER}" "${NBL_ROOT_PATH}/include" "${NBL_RESOURCES_LIST_FILE}"
269-
COMMAND "${PYTHON_EXECUTABLE}" "${NBL_BUILTIN_DATA_GEN_PY}" "${NBL_BUILTIN_RESOURCE_DATA_SOURCE}" "${NBL_ROOT_PATH}/include" "${NBL_RESOURCES_LIST_FILE}"
270+
COMMAND "${PYTHON_EXECUTABLE}" "${NBL_BUILTIN_HEADER_GEN_PY}" "${NBL_BUILTIN_RESOURCES_HEADER}" "${NBL_ROOT_PATH}/include" "${NBL_RESOURCES_LIST_FILE}" "${_NAMESPACE_}"
271+
COMMAND "${PYTHON_EXECUTABLE}" "${NBL_BUILTIN_DATA_GEN_PY}" "${NBL_BUILTIN_RESOURCE_DATA_SOURCE}" "${NBL_ROOT_PATH}/include" "${NBL_RESOURCES_LIST_FILE}" "${_NAMESPACE_}" "${NBL_BS_HEADER_FILENAME}"
270272
COMMENT "Generating built-in resources"
271273
DEPENDS ${NBL_DEPENDENCY_FILES}
272274
VERBATIM
273275
)
274276

275-
add_library(nblBuiltinResourceData STATIC
277+
add_library(${_TARGET_NAME_} STATIC
276278
${_NBL_SOURCES_}
277279
)
278280

279-
target_include_directories(nblBuiltinResourceData PUBLIC "${NBL_ROOT_PATH}/include")
281+
target_include_directories(${_TARGET_NAME_} PUBLIC "${NBL_ROOT_PATH}/include")
280282

281283
if(NBL_DYNAMIC_MSVC_RUNTIME)
282-
set_property(TARGET nblBuiltinResourceData PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
284+
set_property(TARGET ${_TARGET_NAME_} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
283285
else()
284-
set_property(TARGET nblBuiltinResourceData PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
286+
set_property(TARGET ${_TARGET_NAME_} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
285287
endif()
286288
endfunction()
287289

290+
function(NBL_ADD_BUILTIN_RESOURCES)
291+
ADD_CUSTOM_BUILTIN_RESOURCES(nblBuiltinResourceData "nbl::builtin" "${NBL_RESOURCES_TO_EMBED}" "${NBL_ROOT_PATH_BINARY}/include/nbl/builtin")
292+
endfunction()
293+

src/nbl/builtin/builtinDataGen.py

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,27 @@
1212
if len(sys.argv) < 4 :
1313
print(sys.argv[0] + " - Incorrect argument count")
1414
else:
15-
1615
#arguments
1716
outputFilename = sys.argv[1]
1817
cmakeSourceDir = sys.argv[2]
1918
resourcesFile = sys.argv[3]
19+
resourcesNamespace = sys.argv[4]
20+
correspondingHeaderFile = sys.argv[5]
2021

2122
with open(resourcesFile, "r") as f:
2223
resourcePaths = f.read().rstrip().split(',')
2324

2425
#opening a file
2526
outp = open(outputFilename,"w+")
2627

27-
outp.write("#include <stdlib.h>\n")
28-
outp.write("#include <cstdint>\n")
29-
outp.write("#include <string>\n")
30-
outp.write("#include <unordered_map>\n")
31-
outp.write("#include <utility>\n")
32-
outp.write("#include <nbl/core/string/StringLiteral.h>\n")
33-
outp.write("using namespace nbl;\n")
34-
outp.write("using namespace nbl::builtin;\n\n")
35-
outp.write("namespace nbl {\n")
36-
outp.write("\tnamespace builtin {\n\n")
28+
outp.write("#include \"" + correspondingHeaderFile + "\"\n")
29+
outp.write("\tnamespace " + resourcesNamespace + " {\n")
3730

3831
# writing binary data of all files in a loop
3932
for x in resourcePaths:
40-
outp.write('\n\t\ttemplate<> const std::pair<const uint8_t*, size_t> get_resource<typename NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>()' % x)
41-
outp.write('\n\t\t{')
42-
outp.write('\n\t\t\tstatic const uint8_t data[] = {\n\t\t\t')
33+
outp.write('\n\ttemplate<> const std::pair<const uint8_t*, size_t> get_resource<typename NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>()' % x)
34+
outp.write('\n\t{')
35+
outp.write('\n\t\tstatic const uint8_t data[] = {\n\t\t\t')
4336
try:
4437
with open(cmakeSourceDir+'/'+x, "rb") as f:
4538
index = 0
@@ -57,30 +50,29 @@
5750
print('Error: BuiltinResources - file with the following path not found: ' + x)
5851
outp.write('\n\n Error: BuiltinResources - file with the following path not found: %s' % x )
5952

60-
outp.write('\n\t\t\t};')
61-
outp.write('\n\t\t\treturn { data, sizeof(data) };')
62-
outp.write('\n\t\t}')
63-
outp.write('\n\t\ttemplate const std::pair<const uint8_t*, size_t> get_resource<typename NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>();\n\n\n'%x)
53+
outp.write('\n\t\t};')
54+
outp.write('\n\t\treturn { data, sizeof(data) };')
55+
outp.write('\n\t}')
56+
outp.write('\n\ttemplate const std::pair<const uint8_t*, size_t> get_resource<typename NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>();\n\n\n'%x)
6457

6558

66-
outp.write("\t\tstd::pair<const uint8_t*, size_t> get_resource_runtime(const std::string& filename) {\n")
67-
outp.write("\t\t\tstatic std::unordered_map<std::string, int> resourcesByFilename( {\n")
59+
outp.write("\tstd::pair<const uint8_t*, size_t> get_resource_runtime(const std::string& filename) {\n")
60+
outp.write("\t\tstatic std::unordered_map<std::string, int> resourcesByFilename( {\n")
6861
counter = 1
6962
for x in resourcePaths:
70-
outp.write("\t\t\t\t{\"%s\", %d},\n" % (x,counter))
63+
outp.write("\t\t\t{\"%s\", %d},\n" % (x,counter))
7164
counter+= 1
72-
outp.write("\t\t\t});\n\n")
73-
outp.write("\t\t\tauto resource = resourcesByFilename.find(filename);\n")
74-
outp.write("\t\t\tif(resource == resourcesByFilename.end()) return { nullptr,0ull };\n")
75-
outp.write("\t\t\tswitch (resource->second) \n\t\t\t{\n")
65+
outp.write("\t\t});\n\n")
66+
outp.write("\t\tauto resource = resourcesByFilename.find(filename);\n")
67+
outp.write("\t\tif(resource == resourcesByFilename.end()) return { nullptr,0ull };\n")
68+
outp.write("\t\tswitch (resource->second) \n\t\t\t{\n")
7669
counter = 1
7770
for x in resourcePaths:
78-
outp.write("\t\t\t\tcase %d:\n\t\t\t\t\treturn get_resource<typename NBL_CORE_UNIQUE_STRING_LITERAL_TYPE(\"%s\")>();\n" % (counter,x))
71+
outp.write("\t\t\tcase %d:\n\t\t\t\t\treturn get_resource<typename NBL_CORE_UNIQUE_STRING_LITERAL_TYPE(\"%s\")>();\n" % (counter,x))
7972
counter+= 1
8073

81-
outp.write("\t\t\t\tdefault:\n")
82-
outp.write("\t\t\t\t\treturn { nullptr,0ull };\n")
83-
outp.write("\t\t\t}\n\t\t}")
84-
outp.write("\n\t}")
74+
outp.write("\t\t\tdefault:\n")
75+
outp.write("\t\t\t\treturn { nullptr,0ull };\n")
76+
outp.write("\t\t}\n\t}")
8577
outp.write("\n}")
8678
outp.close()

src/nbl/builtin/builtinHeaderGen.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
outputFilename = sys.argv[1]
1717
cmakeSourceDir = sys.argv[2]
1818
resourcesFile = sys.argv[3]
19+
resourcesNamespace = sys.argv[4]
1920

2021
with open(resourcesFile, "r") as f:
2122
resourcePaths = f.read().rstrip().split(',')
@@ -29,14 +30,16 @@
2930
outp.write("#include <cstdint>\n")
3031
outp.write("#include <string>\n")
3132
outp.write("#include <unordered_map>\n")
32-
outp.write("#include <utility>\n#include <nbl/core/string/StringLiteral.h>\n")
33-
outp.write("namespace nbl { \n\tnamespace builtin { \n")
34-
33+
outp.write("#include <utility>\n#include <nbl/core/string/StringLiteral.h>\n\n")
34+
outp.write("namespace " + resourcesNamespace + " { \n")
35+
outp.write("\t\ttemplate<nbl::core::StringLiteral Path>\n")
36+
outp.write("\t\tconst std::pair<const uint8_t*, size_t> get_resource();\n")
37+
3538
#Iterating through input list
3639
for x in resourcePaths:
3740
outp.write('\n\t\textern template const std::pair<const uint8_t*, size_t> get_resource<typename NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("%s")>();' % x)
3841

39-
outp.write("\n\t}\n}")
42+
outp.write("\n\t}")
4043
outp.write("\n#endif")
4144

4245
outp.close()

0 commit comments

Comments
 (0)