Skip to content

Commit 10f1524

Browse files
committed
DaemonSourceGenerator: make the embedded file generator reusable
1 parent 7c106ed commit 10f1524

File tree

5 files changed

+127
-89
lines changed

5 files changed

+127
-89
lines changed

CMakeLists.txt

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -927,39 +927,8 @@ if (BUILD_CLIENT)
927927
Tests ${CLIENTTESTLIST}
928928
)
929929

930-
# generate glsl include files
931-
set(GLSL_SOURCE_DIR ${ENGINE_DIR}/renderer/glsl_source)
932-
set(EMBED_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/embed_data)
933-
file(MAKE_DIRECTORY ${EMBED_INCLUDE_DIR})
934-
935-
set(SHADERS_CPP_TEXT "// This file is auto-generated by CMakeLists.txt.\n")
936-
string(APPEND SHADERS_CPP_TEXT "#include \"common/Common.h\"\n\n")
937-
set(SHADERMAP_TEXT "")
938-
939-
foreach(res ${GLSLSOURCELIST})
940-
get_filename_component(filename_no_ext ${res} NAME_WE)
941-
set(outpath ${EMBED_INCLUDE_DIR}/${filename_no_ext}.glsl.h)
942-
943-
add_custom_command(
944-
OUTPUT ${outpath}
945-
COMMAND ${CMAKE_COMMAND} "-DINPUT_FILE=${res}" "-DOUTPUT_FILE=${outpath}"
946-
"-DVARIABLE_NAME=${filename_no_ext}_glsl" -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/EmbedText.cmake
947-
MAIN_DEPENDENCY ${res}
948-
)
949-
950-
set_property(TARGET client-objects APPEND PROPERTY SOURCES ${outpath})
951-
952-
string(APPEND SHADERS_CPP_TEXT "#include \"../embed_data/${filename_no_ext}.glsl.h\"\n")
953-
string(APPEND SHADERMAP_TEXT "\t{ \"${filename_no_ext}.glsl\", ")
954-
string(APPEND SHADERMAP_TEXT "std::string(reinterpret_cast<const char *>( ${filename_no_ext}_glsl ), ")
955-
string(APPEND SHADERMAP_TEXT "sizeof( ${filename_no_ext}_glsl )) },\n")
956-
endforeach()
957-
958-
string(APPEND SHADERS_CPP_TEXT "\nextern const std::unordered_map<std::string, std::string> shadermap\n{\n")
959-
string(APPEND SHADERS_CPP_TEXT "${SHADERMAP_TEXT}")
960-
string(APPEND SHADERS_CPP_TEXT "};\n")
961-
962-
daemon_write_generated("shaders.cpp" "${SHADERS_CPP_TEXT}")
930+
# Generate GLSL include files.
931+
daemon_embed_files("EngineShaders" "GLSL" "client-objects")
963932
endif()
964933

965934
if (BUILD_SERVER)

cmake/DaemonSourceGenerator.cmake

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ set(DAEMON_GENERATED_SUBDIR "GeneratedSource")
22
set(DAEMON_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/${DAEMON_GENERATED_SUBDIR}")
33

44
set(DAEMON_BUILDINFO_SUBDIR "DaemonBuildInfo")
5+
set(DAEMON_EMBEDDED_SUBDIR "DaemonEmbeddedFiles")
6+
57
set(DAEMON_BUILDINFO_DIR "${DAEMON_GENERATED_DIR}/${DAEMON_BUILDINFO_SUBDIR}")
8+
set(DAEMON_EMBEDDED_DIR "${DAEMON_GENERATED_DIR}/${DAEMON_EMBEDDED_SUBDIR}")
69

710
file(MAKE_DIRECTORY "${DAEMON_GENERATED_DIR}")
811
include_directories("${DAEMON_GENERATED_DIR}")
912

1013
file(MAKE_DIRECTORY "${DAEMON_BUILDINFO_DIR}")
14+
file(MAKE_DIRECTORY "${DAEMON_EMBEDDED_DIR}")
1115

1216
set(DAEMON_GENERATED_HEADER "// Automatically generated, do not modify!\n")
1317
set(DAEMON_GENERATED_CPP_EXT ".cpp")
@@ -46,3 +50,67 @@ macro(daemon_write_buildinfo NAME)
4650
list(APPEND BUILDINFOLIST "${DAEMON_GENERATED_FILE}")
4751
endforeach()
4852
endmacro()
53+
54+
macro(daemon_embed_files BASENAME SLUG FORMAT TARGETNAME)
55+
set(EMBED_SOURCE_DIR "${SLUG}_EMBED_DIR")
56+
set(EMBED_SOURCE_LIST "${SLUG}_EMBED_LIST")
57+
58+
set(EMBED_SUBDIR "${DAEMON_EMBEDDED_SUBDIR}/${BASENAME}")
59+
set(EMBED_DIR "${DAEMON_GENERATED_DIR}/${EMBED_SUBDIR}")
60+
61+
foreach(kind CPP H)
62+
set(EMBED_${kind}_FILE "${DAEMON_EMBEDDED_SUBDIR}/${BASENAME}${DAEMON_GENERATED_${kind}_EXT}")
63+
set(EMBED_${kind}_TEXT "${DAEMON_GENERATED_HEADER}")
64+
endforeach()
65+
66+
string(APPEND EMBED_CPP_TEXT "#include \"${EMBED_H_FILE}\"\n\n")
67+
68+
set(EMBED_MAP_TEXT "")
69+
70+
foreach(filename ${${EMBED_SOURCE_LIST}})
71+
string(REGEX REPLACE "[^A-Za-z0-9]" "_" filename_symbol "${filename}")
72+
73+
set(inpath "${${EMBED_SOURCE_DIR}}/${filename}")
74+
set(outpath "${EMBED_DIR}/${filename_symbol}${DAEMON_GENERATED_H_EXT}")
75+
76+
add_custom_command(
77+
OUTPUT "${outpath}"
78+
COMMAND ${CMAKE_COMMAND}
79+
"-DINPUT_FILE=${inpath}"
80+
"-DOUTPUT_FILE=${outpath}"
81+
"-DVARIABLE_NAME=${filename_symbol}"
82+
-P "${CMAKE_CURRENT_SOURCE_DIR}/cmake/EmbedText.cmake"
83+
MAIN_DEPENDENCY ${inpath}
84+
)
85+
86+
set_property(TARGET "${TARGETNAME}" APPEND PROPERTY SOURCES "${outpath}")
87+
88+
string(APPEND EMBED_CPP_TEXT
89+
"#include \"${BASENAME}/${filename_symbol}.h\"\n")
90+
string(APPEND EMBED_MAP_TEXT
91+
"\t{ \"${filename}\", "
92+
"std::string(reinterpret_cast<const char *>( ${filename_symbol} ), "
93+
"sizeof( ${filename_symbol} )) },\n")
94+
endforeach()
95+
96+
string(APPEND EMBED_CPP_TEXT
97+
"\n"
98+
"namespace ${BASENAME} {\n"
99+
"const std::unordered_map<std::string, std::string> FileMap\n{\n"
100+
"${EMBED_MAP_TEXT}"
101+
"};\n"
102+
"}"
103+
)
104+
105+
string(APPEND EMBED_H_TEXT
106+
"#include \"common/Common.h\"\n"
107+
"\n"
108+
"namespace ${BASENAME} {\n"
109+
"extern const std::unordered_map<std::string, std::string> FileMap;\n"
110+
"};\n"
111+
)
112+
113+
foreach(kind CPP H)
114+
daemon_write_generated("${EMBED_${kind}_FILE}" "${EMBED_${kind}_TEXT}")
115+
endforeach()
116+
endmacro()

src.cmake

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -86,68 +86,68 @@ else()
8686
include (${ENGINE_DIR}/renderer/src.cmake)
8787
endif()
8888

89-
set(GLSLSOURCELIST
89+
set(GLSL_EMBED_DIR "${ENGINE_DIR}/renderer/glsl_source")
90+
set(GLSL_EMBED_LIST
9091
# Common shader libraries
91-
${ENGINE_DIR}/renderer/glsl_source/common.glsl
92-
${ENGINE_DIR}/renderer/glsl_source/common_cp.glsl
93-
${ENGINE_DIR}/renderer/glsl_source/fogEquation_fp.glsl
94-
${ENGINE_DIR}/renderer/glsl_source/shaderProfiler_vp.glsl
95-
${ENGINE_DIR}/renderer/glsl_source/shaderProfiler_fp.glsl
96-
92+
common.glsl
93+
common_cp.glsl
94+
fogEquation_fp.glsl
95+
shaderProfiler_vp.glsl
96+
shaderProfiler_fp.glsl
9797
# Material system shaders
98-
${ENGINE_DIR}/renderer/glsl_source/material_cp.glsl
99-
${ENGINE_DIR}/renderer/glsl_source/material_vp.glsl
100-
${ENGINE_DIR}/renderer/glsl_source/material_fp.glsl
101-
${ENGINE_DIR}/renderer/glsl_source/clearSurfaces_cp.glsl
102-
${ENGINE_DIR}/renderer/glsl_source/cull_cp.glsl
103-
${ENGINE_DIR}/renderer/glsl_source/depthReduction_cp.glsl
104-
${ENGINE_DIR}/renderer/glsl_source/processSurfaces_cp.glsl
98+
material_cp.glsl
99+
material_vp.glsl
100+
material_fp.glsl
101+
clearSurfaces_cp.glsl
102+
cull_cp.glsl
103+
depthReduction_cp.glsl
104+
processSurfaces_cp.glsl
105105

106106
# Screen-space shaders
107-
${ENGINE_DIR}/renderer/glsl_source/screenSpace_vp.glsl
108-
${ENGINE_DIR}/renderer/glsl_source/blur_fp.glsl
109-
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_fp.glsl
110-
${ENGINE_DIR}/renderer/glsl_source/contrast_fp.glsl
111-
${ENGINE_DIR}/renderer/glsl_source/fogGlobal_fp.glsl
112-
${ENGINE_DIR}/renderer/glsl_source/fxaa_fp.glsl
113-
${ENGINE_DIR}/renderer/glsl_source/fxaa3_11_fp.glsl
114-
${ENGINE_DIR}/renderer/glsl_source/motionblur_fp.glsl
115-
${ENGINE_DIR}/renderer/glsl_source/ssao_fp.glsl
107+
screenSpace_vp.glsl
108+
blur_fp.glsl
109+
cameraEffects_fp.glsl
110+
contrast_fp.glsl
111+
fogGlobal_fp.glsl
112+
fxaa_fp.glsl
113+
fxaa3_11_fp.glsl
114+
motionblur_fp.glsl
115+
ssao_fp.glsl
116116

117117
# Lighting shaders
118-
${ENGINE_DIR}/renderer/glsl_source/depthtile1_vp.glsl
119-
${ENGINE_DIR}/renderer/glsl_source/depthtile1_fp.glsl
120-
${ENGINE_DIR}/renderer/glsl_source/depthtile2_fp.glsl
121-
${ENGINE_DIR}/renderer/glsl_source/lighttile_vp.glsl
122-
${ENGINE_DIR}/renderer/glsl_source/lighttile_fp.glsl
123-
${ENGINE_DIR}/renderer/glsl_source/computeLight_fp.glsl
124-
${ENGINE_DIR}/renderer/glsl_source/reliefMapping_fp.glsl
118+
depthtile1_vp.glsl
119+
depthtile1_fp.glsl
120+
depthtile2_fp.glsl
121+
lighttile_vp.glsl
122+
lighttile_fp.glsl
123+
computeLight_fp.glsl
124+
reliefMapping_fp.glsl
125125

126126
# Common vertex shader libraries
127-
${ENGINE_DIR}/renderer/glsl_source/deformVertexes_vp.glsl
128-
${ENGINE_DIR}/renderer/glsl_source/vertexAnimation_vp.glsl
129-
${ENGINE_DIR}/renderer/glsl_source/vertexSimple_vp.glsl
130-
${ENGINE_DIR}/renderer/glsl_source/vertexSkinning_vp.glsl
127+
deformVertexes_vp.glsl
128+
vertexAnimation_vp.glsl
129+
vertexSimple_vp.glsl
130+
vertexSkinning_vp.glsl
131131

132132
# Regular shaders
133-
${ENGINE_DIR}/renderer/glsl_source/fogQuake3_vp.glsl
134-
${ENGINE_DIR}/renderer/glsl_source/fogQuake3_fp.glsl
135-
${ENGINE_DIR}/renderer/glsl_source/generic_vp.glsl
136-
${ENGINE_DIR}/renderer/glsl_source/generic_fp.glsl
137-
${ENGINE_DIR}/renderer/glsl_source/heatHaze_vp.glsl
138-
${ENGINE_DIR}/renderer/glsl_source/heatHaze_fp.glsl
139-
${ENGINE_DIR}/renderer/glsl_source/lightMapping_vp.glsl
140-
${ENGINE_DIR}/renderer/glsl_source/lightMapping_fp.glsl
141-
${ENGINE_DIR}/renderer/glsl_source/liquid_vp.glsl
142-
${ENGINE_DIR}/renderer/glsl_source/liquid_fp.glsl
143-
${ENGINE_DIR}/renderer/glsl_source/portal_vp.glsl
144-
${ENGINE_DIR}/renderer/glsl_source/portal_fp.glsl
145-
${ENGINE_DIR}/renderer/glsl_source/reflection_CB_vp.glsl
146-
${ENGINE_DIR}/renderer/glsl_source/reflection_CB_fp.glsl
147-
${ENGINE_DIR}/renderer/glsl_source/screen_vp.glsl
148-
${ENGINE_DIR}/renderer/glsl_source/screen_fp.glsl
149-
${ENGINE_DIR}/renderer/glsl_source/skybox_vp.glsl
150-
${ENGINE_DIR}/renderer/glsl_source/skybox_fp.glsl
133+
fogQuake3_vp.glsl
134+
fogQuake3_fp.glsl
135+
generic_vp.glsl
136+
generic_fp.glsl
137+
heatHaze_vp.glsl
138+
heatHaze_fp.glsl
139+
lightMapping_vp.glsl
140+
lightMapping_fp.glsl
141+
liquid_vp.glsl
142+
liquid_fp.glsl
143+
portal_vp.glsl
144+
portal_fp.glsl
145+
reflection_CB_vp.glsl
146+
reflection_CB_fp.glsl
147+
screen_vp.glsl
148+
screen_fp.glsl
149+
skybox_vp.glsl
150+
skybox_fp.glsl
151151
)
152152

153153
set(SERVERLIST

src/engine/renderer/gl_shader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2424
#include <common/FileSystem.h>
2525
#include "gl_shader.h"
2626
#include "Material.h"
27+
#include "DaemonEmbeddedFiles/EngineShaders.h"
2728

2829
// We currently write GLBinaryHeader to a file and memcpy all over it.
2930
// Make sure it's a pod, so we don't put a std::string in it or something
@@ -41,7 +42,6 @@ static Cvar::Cvar<bool> r_logUnmarkedGLSLBuilds(
4142
"r_logUnmarkedGLSLBuilds", "Log building information for GLSL shaders that are built after the map is loaded",
4243
Cvar::NONE, true );
4344

44-
extern const std::unordered_map<std::string, std::string> shadermap;
4545
// shaderKind's value will be determined later based on command line setting or absence of.
4646
ShaderKind shaderKind = ShaderKind::Unknown;
4747

@@ -92,8 +92,8 @@ namespace // Implementation details
9292

9393
const char* GetInternalShader(Str::StringRef filename)
9494
{
95-
auto it = shadermap.find(filename);
96-
if (it != shadermap.end())
95+
auto it = EngineShaders::FileMap.find(filename);
96+
if (it != EngineShaders::FileMap.end())
9797
return it->second.c_str();
9898
return nullptr;
9999
}

src/engine/renderer/src.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
set(RENDERERLIST
3-
${DAEMON_GENERATED_DIR}/shaders.cpp
3+
${DAEMON_EMBEDDED_DIR}/EngineShaders.cpp
4+
${DAEMON_EMBEDDED_DIR}/EngineShaders.h
45
${ENGINE_DIR}/renderer/BufferBind.h
56
${ENGINE_DIR}/renderer/DetectGLVendors.cpp
67
${ENGINE_DIR}/renderer/DetectGLVendors.h

0 commit comments

Comments
 (0)