-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
186 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function(compile_shaders target shaders output) | ||
if (CMAKE_CROSSCOMPILING AND IOS_OR_TVOS) | ||
set(shader_compiler_cli "${CMAKE_BINARY_DIR}/macOS/bin/ShaderCompilerCLI") | ||
else() | ||
set(shader_compiler_cli "$<TARGET_FILE:ShaderCompilerCLI>") | ||
endif() | ||
set(output_dir "${CMAKE_BINARY_DIR}/gen/${target}/") | ||
foreach(full_shader_path ${shaders}) | ||
get_filename_component(shader_name ${full_shader_path} NAME_WE) | ||
set(spirv ${output_dir}/${shader_name}.spirv) | ||
set(dxil ${output_dir}/${shader_name}.dxil) | ||
get_property(entrypoint SOURCE ${full_shader_path} PROPERTY SHADER_ENTRYPOINT) | ||
get_property(type SOURCE ${full_shader_path} PROPERTY SHADER_TYPE) | ||
get_property(model SOURCE ${full_shader_path} PROPERTY SHADER_MODEL) | ||
add_custom_command(OUTPUT ${spirv} ${dxil} | ||
COMMAND ${CMAKE_COMMAND} -E echo ${shader_name} ${full_shader_path} ${entrypoint} ${type} ${model} ${output_dir} | ||
COMMAND ${CMAKE_COMMAND} -E make_directory ${output_dir} | ||
COMMAND ${shader_compiler_cli} ${shader_name} ${full_shader_path} ${entrypoint} ${type} ${model} ${output_dir} | ||
DEPENDS ShaderCompilerCLI | ||
MAIN_DEPENDENCY "${full_shader_path}" | ||
) | ||
set(compiled_shaders ${compiled_shaders} ${spirv}) | ||
set(compiled_shaders ${compiled_shaders} ${dxil}) | ||
endforeach() | ||
set(${output} ${compiled_shaders} PARENT_SCOPE) | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
if (BUILD_SAMPLES) | ||
add_subdirectory(Apps) | ||
add_subdirectory(Modules) | ||
add_subdirectory(Tools) | ||
endif() | ||
|
||
add_subdirectory(FlyCube) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(ShaderCompilerCLI) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
if (CMAKE_CROSSCOMPILING) | ||
return() | ||
endif() | ||
|
||
add_executable(ShaderCompilerCLI | ||
${project_root}/src/FlyCube/HLSLCompiler/Compiler.cpp | ||
${project_root}/src/FlyCube/HLSLCompiler/DXCLoader.cpp | ||
${project_root}/src/FlyCube/Utilities/SystemUtils.cpp | ||
main.cpp | ||
) | ||
|
||
target_link_libraries(ShaderCompilerCLI | ||
dxc | ||
gli | ||
glm | ||
nowide | ||
) | ||
|
||
target_include_directories(ShaderCompilerCLI | ||
PUBLIC | ||
"${project_root}/src/FlyCube" | ||
) | ||
|
||
set_target_properties(ShaderCompilerCLI PROPERTIES FOLDER "Tools") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include "HLSLCompiler/Compiler.h" | ||
#include "Instance/BaseTypes.h" | ||
|
||
#include <cassert> | ||
#include <fstream> | ||
|
||
namespace { | ||
|
||
ShaderType GetShaderType(const std::string& target) | ||
{ | ||
if (target == "Pixel") { | ||
return ShaderType::kPixel; | ||
} else if (target == "Vertex") { | ||
return ShaderType::kVertex; | ||
} else if (target == "Compute") { | ||
return ShaderType::kCompute; | ||
} else if (target == "Geometry") { | ||
return ShaderType::kGeometry; | ||
} else if (target == "Amplification") { | ||
return ShaderType::kAmplification; | ||
} else if (target == "Mesh") { | ||
return ShaderType::kMesh; | ||
} else if (target == "Library") { | ||
return ShaderType::kLibrary; | ||
} | ||
assert(false); | ||
return ShaderType::kUnknown; | ||
} | ||
|
||
std::string GetShaderExtension(ShaderBlobType shader_type) | ||
{ | ||
switch (shader_type) { | ||
case ShaderBlobType::kDXIL: | ||
return ".dxil"; | ||
case ShaderBlobType::kSPIRV: | ||
return ".spirv"; | ||
default: | ||
assert(false); | ||
return ".blob"; | ||
} | ||
} | ||
|
||
void CompileShader(const std::string& shader_name, | ||
const ShaderDesc& desc, | ||
const std::string& output_path, | ||
ShaderBlobType shader_type) | ||
{ | ||
std::string path = output_path + "/" + shader_name + GetShaderExtension(shader_type); | ||
std::vector<uint8_t> blob = Compile(desc, shader_type); | ||
std::fstream file(path, std::ios::out | std::ios::binary); | ||
file.write(reinterpret_cast<char*>(blob.data()), blob.size()); | ||
} | ||
|
||
} // namespace | ||
|
||
class ParseCmd { | ||
public: | ||
ParseCmd(int argc, char* argv[]) | ||
{ | ||
size_t arg_index = 0; | ||
auto get_next_arg = [&] { | ||
++arg_index; | ||
assert(arg_index < argc); | ||
return argv[arg_index]; | ||
}; | ||
m_shader_name = get_next_arg(); | ||
m_desc.shader_path = get_next_arg(); | ||
m_desc.entrypoint = get_next_arg(); | ||
m_desc.type = GetShaderType(get_next_arg()); | ||
m_desc.model = get_next_arg(); | ||
m_output_dir = get_next_arg(); | ||
} | ||
|
||
const std::string& GetShaderName() const | ||
{ | ||
return m_shader_name; | ||
} | ||
|
||
const ShaderDesc& GetShaderDesc() const | ||
{ | ||
return m_desc; | ||
} | ||
|
||
const std::string& GetOutputDir() const | ||
{ | ||
return m_output_dir; | ||
} | ||
|
||
private: | ||
std::string m_shader_name; | ||
ShaderDesc m_desc; | ||
std::string m_output_dir; | ||
}; | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
ParseCmd cmd(argc, argv); | ||
CompileShader(cmd.GetShaderName(), cmd.GetShaderDesc(), cmd.GetOutputDir(), ShaderBlobType::kDXIL); | ||
CompileShader(cmd.GetShaderName(), cmd.GetShaderDesc(), cmd.GetOutputDir(), ShaderBlobType::kSPIRV); | ||
return 0; | ||
} |