-
Notifications
You must be signed in to change notification settings - Fork 70
Add HLSL support through DXC #511
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| out | ||
| third_party/cpplint | ||
| third_party/dxc | ||
| third_party/glslang | ||
| third_party/googletest | ||
| third_party/lodepng | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,6 +121,11 @@ The available flags which can be defined are: | |
| cmake -DAMBER_SKIP_TESTS=True -DAMBER_SKIP_SPIRV_TOOLS=True -GNinja ../.. | ||
| ``` | ||
|
|
||
| #### DXC | ||
|
|
||
| DXC can be enabled in Amber by adding the `-DAMBER_USE_DXC=true` flag when | ||
| running cmake. | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should say that this also enables HLSL shader support, and that DXC is used to compile those shaders.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although we would also need updates to the vk_script and amberscript docs. So I'll file an issue for that.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no plans on updating vkscript to support hlsl. This will be AmberScript only. |
||
| ## Build Bots | ||
|
|
||
| There are a number of build bots to verify Amber continues to compile and run | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,11 +51,31 @@ if (${Dawn_FOUND}) | |
| list(APPEND AMBER_SOURCES dawn_engine_config.cc) | ||
| endif() | ||
|
|
||
| if (${AMBER_ENABLE_DXC}) | ||
| list(APPEND AMBER_SOURCES dxc_helper.cc) | ||
| endif() | ||
|
|
||
| add_library(libamber ${AMBER_SOURCES}) | ||
| amber_default_compile_options(libamber) | ||
| target_include_directories(libamber PRIVATE "${CMAKE_BINARY_DIR}") | ||
| set_target_properties(libamber PROPERTIES OUTPUT_NAME "amber") | ||
|
|
||
| if (${AMBER_ENABLE_DXC}) | ||
| target_include_directories(libamber PRIVATE | ||
| "${PROJECT_SOURCE_DIR}/third_party/dxc/include" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, this could be |
||
| "${CMAKE_BINARY_DIR}/third_party/dxc/include" | ||
| ) | ||
|
|
||
| add_dependencies(libamber dxcompiler) | ||
| target_link_libraries(libamber | ||
| dxcompiler | ||
| LLVMDxcSupport | ||
| LLVMOption | ||
| LLVMHLSL | ||
| LLVMScalarOpts | ||
| ) | ||
| endif() | ||
|
|
||
| if (${AMBER_ENABLE_SPIRV_TOOLS}) | ||
| target_link_libraries(libamber SPIRV-Tools) | ||
| endif() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // Copyright 2019 The Amber Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "src/dxc_helper.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <sstream> | ||
|
|
||
| #include "src/platform.h" | ||
|
|
||
| #if AMBER_PLATFORM_WINDOWS | ||
| #pragma warning(push) | ||
| #pragma warning(disable : 4267) | ||
| #pragma warning(disable : 4003) | ||
| #endif // AMBER_PLATFORM_WINDOWS | ||
|
|
||
| // clang-format off | ||
| // The order here matters, so don't reformat. | ||
| #include "third_party/dxc/include/dxc/Support/WinAdapter.h" | ||
| #include "third_party/dxc/include/dxc/Support/WinIncludes.h" | ||
| #include "third_party/dxc/include/dxc/Support/Global.h" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems over-descriptive. In future we should allow DXC's source to be in some configurable location. But that's for the future.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include paths should be from the root src/ directory. Or are you thinking we should allow DXC to live outside amber?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean DXC should be able to live outside of Amber. Like, if I had one giant build tree that included many tools for the Vulkan shader stack. #include "dxc/Support/Global.h" so that I can always find with a -I option on the compiler, where that is controlled from a CMake string expression. |
||
| #include "third_party/dxc/include/dxc/Support/HLSLOptions.h" | ||
| #include "third_party/dxc/include/dxc/Support/dxcapi.use.h" | ||
| #include "third_party/dxc/include/dxc/dxcapi.h" | ||
| // clang-format on | ||
|
|
||
| #if AMBER_PLATFORM_WINDOWS | ||
| #pragma warning(pop) | ||
| #endif // AMBER_PLATFORM_WINDOWS | ||
|
|
||
| namespace amber { | ||
| namespace dxchelper { | ||
| namespace { | ||
|
|
||
| const wchar_t* kDxcFlags[] = { | ||
| L"-spirv", // SPIR-V compilation | ||
| L"-Vd" // Disable validation. | ||
| }; | ||
| const size_t kDxcFlagsCount = sizeof(kDxcFlags) / sizeof(const wchar_t*); | ||
|
|
||
| // Converts an IDxcBlob into a vector of 32-bit unsigned integers which | ||
| // is returned via the 'binaryWords' argument. | ||
| void ConvertIDxcBlobToUint32(IDxcBlob* blob, | ||
| std::vector<uint32_t>* binaryWords) { | ||
| size_t num32BitWords = (blob->GetBufferSize() + 3) / 4; | ||
| std::string binaryStr(static_cast<char*>(blob->GetBufferPointer()), | ||
| blob->GetBufferSize()); | ||
| binaryStr.resize(num32BitWords * 4, 0); | ||
| binaryWords->resize(num32BitWords, 0); | ||
| memcpy(binaryWords->data(), binaryStr.data(), binaryStr.size()); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| Result Compile(const std::string& src, | ||
| const std::string& entry, | ||
| const std::string& profile, | ||
| std::vector<uint32_t>* generated_binary) { | ||
| DxcInitThreadMalloc(); | ||
|
|
||
| if (hlsl::options::initHlslOptTable()) { | ||
| DxcCleanupThreadMalloc(); | ||
dneto0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Result("DXC compile failure: initHlslOptTable"); | ||
| } | ||
|
|
||
| IDxcLibrary* dxc_lib; | ||
| if (DxcCreateInstance(CLSID_DxcLibrary, __uuidof(IDxcLibrary), | ||
| reinterpret_cast<void**>(&dxc_lib)) < 0) { | ||
| DxcCleanupThreadMalloc(); | ||
| return Result("DXCCreateInstance for DXCLibrary failed"); | ||
| } | ||
|
|
||
| IDxcBlobEncoding* source; | ||
| if (dxc_lib->CreateBlobWithEncodingOnHeapCopy( | ||
| src.data(), static_cast<uint32_t>(src.size()), CP_UTF8, &source) < | ||
| 0) { | ||
| DxcCleanupThreadMalloc(); | ||
| return Result("DXC compile failure: CreateBlobFromFile"); | ||
| } | ||
|
|
||
| IDxcIncludeHandler* include_handler; | ||
| if (dxc_lib->CreateIncludeHandler(&include_handler) < 0) { | ||
| DxcCleanupThreadMalloc(); | ||
| return Result("DXC compile failure: CreateIncludeHandler"); | ||
| } | ||
|
|
||
| IDxcCompiler* compiler; | ||
| if (DxcCreateInstance(CLSID_DxcCompiler, __uuidof(IDxcCompiler), | ||
| reinterpret_cast<void**>(&compiler)) < 0) { | ||
| DxcCleanupThreadMalloc(); | ||
| return Result("DXCCreateInstance for DXCCompiler failed"); | ||
| } | ||
|
|
||
| IDxcOperationResult* result; | ||
| std::wstring src_filename = | ||
| L"amber." + std::wstring(profile.begin(), profile.end()); | ||
| if (compiler->Compile(source, /* source text */ | ||
| src_filename.c_str(), /* original file source */ | ||
| std::wstring(entry.begin(), entry.end()) | ||
| .c_str(), /* entry point name */ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to stop for a while, worrying about the lifetime of the temporary. But it dies at the end of the full expression, so that's late enough. Phew.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heh, I didn't think about that at the time. |
||
| std::wstring(profile.begin(), profile.end()) | ||
| .c_str(), /* shader profile to compile */ | ||
| kDxcFlags, /* arguments */ | ||
| kDxcFlagsCount, /* argument count */ | ||
| nullptr, /* defines */ | ||
| 0, /* define count */ | ||
| include_handler, /* handler for #include */ | ||
| &result /* output status */) < 0) { | ||
| DxcCleanupThreadMalloc(); | ||
| return Result("DXC compile failure: Compile"); | ||
| } | ||
|
|
||
| // Compilation is done. We can clean up the HlslOptTable. | ||
| hlsl::options::cleanupHlslOptTable(); | ||
|
|
||
| // Get compilation results. | ||
| HRESULT result_status; | ||
| if (result->GetStatus(&result_status) < 0) { | ||
| DxcCleanupThreadMalloc(); | ||
| return Result("DXC compile failure: GetStatus"); | ||
| } | ||
|
|
||
| // Get diagnostics string. | ||
| IDxcBlobEncoding* error_buffer; | ||
| if (result->GetErrorBuffer(&error_buffer)) { | ||
| DxcCleanupThreadMalloc(); | ||
| return Result("DXC compile failure: GetErrorBuffer"); | ||
| } | ||
|
|
||
| const std::string diagnostics( | ||
| static_cast<char*>(error_buffer->GetBufferPointer()), | ||
| error_buffer->GetBufferSize()); | ||
|
|
||
| bool success = true; | ||
| if (SUCCEEDED(result_status)) { | ||
| IDxcBlob* compiled_blob; | ||
| if (result->GetResult(&compiled_blob) < 0) { | ||
| DxcCleanupThreadMalloc(); | ||
| return Result("DXC compile failure: GetResult"); | ||
| } | ||
| ConvertIDxcBlobToUint32(compiled_blob, generated_binary); | ||
| } else { | ||
| success = false; | ||
| } | ||
|
|
||
| DxcCleanupThreadMalloc(); | ||
| return success ? Result() : Result("DXC compile failure: " + diagnostics); | ||
| } | ||
|
|
||
| } // namespace dxchelper | ||
| } // namespace amber | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright 2019 The Amber Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef SRC_DXC_HELPER_H_ | ||
| #define SRC_DXC_HELPER_H_ | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "amber/result.h" | ||
|
|
||
| namespace amber { | ||
| namespace dxchelper { | ||
|
|
||
| // Passes the HLSL source code to the DXC compiler with SPIR-V CodeGen. | ||
| // Returns the generated SPIR-V binary via |generated_binary| argument. | ||
| Result Compile(const std::string& src_str, | ||
| const std::string& entry_str, | ||
| const std::string& profile_str, | ||
| std::vector<uint32_t>* generated_binary); | ||
|
|
||
| } // namespace dxchelper | ||
| } // namespace amber | ||
|
|
||
| #endif // SRC_DXC_HELPER_H_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For later: We can use
<projectname>_BINARY_DIRto specify this dir, without assuming its source was under third_party.It's kind of weird, but DXC sets the project name to "LLVM", so then it would still look weird!
See https://github.com/microsoft/DirectXShaderCompiler/blob/master/CMakeLists.txt#L28