Wrap all uses of SPIRV-Cross in #define.

Put all cross-platform code references to Spirv-Cross behind
ifdef DAWN_USE_SPIRV_CROSS. Hide build dependencies behind
dawn_use_spirv_cross, which itself is set from dawn_enable_opengl,
since it is the only backend which is still using SPIRV-Cross.

Bug: dawn:1036

Change-Id: Id61512230b50a32c63827a16fece40d7899968d8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/61400
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Stephen White 2021-08-10 18:30:55 +00:00 committed by Dawn LUCI CQ
parent ec9b81142f
commit 5d17ed6541
7 changed files with 36 additions and 58 deletions

View File

@ -104,5 +104,9 @@ declare_args() {
dawn_supports_glfw_for_windowing = dawn_supports_glfw_for_windowing =
(is_win && !dawn_is_winuwp) || (is_linux && !is_chromeos) || is_mac (is_win && !dawn_is_winuwp) || (is_linux && !is_chromeos) || is_mac
# Much of the backend code is shared, so define a convenience var. # Much of the GL backend code is shared, so define a convenience var.
dawn_enable_opengl = dawn_enable_opengles || dawn_enable_desktop_gl dawn_enable_opengl = dawn_enable_opengles || dawn_enable_desktop_gl
# The GL backends are the last to use SPIRV-Cross, so only compile it in
# if they are enabled.
dawn_use_spirv_cross = dawn_enable_opengl

View File

@ -85,6 +85,10 @@ config("dawn_internal") {
defines += [ "DAWN_USE_X11" ] defines += [ "DAWN_USE_X11" ]
} }
if (dawn_use_spirv_cross) {
defines += [ "DAWN_USE_SPIRV_CROSS" ]
}
if (dawn_enable_error_injection) { if (dawn_enable_error_injection) {
defines += [ "DAWN_ENABLE_ERROR_INJECTION" ] defines += [ "DAWN_ENABLE_ERROR_INJECTION" ]
} }

View File

@ -145,11 +145,13 @@ source_set("dawn_native_sources") {
":dawn_native_headers", ":dawn_native_headers",
":dawn_native_utils_gen", ":dawn_native_utils_gen",
"${dawn_root}/src/common", "${dawn_root}/src/common",
"${dawn_root}/third_party/gn/spirv_cross:spirv_cross",
"${dawn_spirv_tools_dir}:spvtools_opt", "${dawn_spirv_tools_dir}:spvtools_opt",
"${dawn_spirv_tools_dir}:spvtools_val", "${dawn_spirv_tools_dir}:spvtools_val",
"${dawn_tint_dir}/src:libtint", "${dawn_tint_dir}/src:libtint",
] ]
if (dawn_use_spirv_cross) {
deps += [ "${dawn_root}/third_party/gn/spirv_cross:spirv_cross" ]
}
defines = [] defines = []
libs = [] libs = []
data_deps = [] data_deps = []
@ -282,8 +284,6 @@ source_set("dawn_native_sources") {
"Sampler.h", "Sampler.h",
"ShaderModule.cpp", "ShaderModule.cpp",
"ShaderModule.h", "ShaderModule.h",
"SpirvUtils.cpp",
"SpirvUtils.h",
"StagingBuffer.cpp", "StagingBuffer.cpp",
"StagingBuffer.h", "StagingBuffer.h",
"Subresource.cpp", "Subresource.cpp",
@ -313,6 +313,13 @@ source_set("dawn_native_sources") {
] ]
} }
if (dawn_use_spirv_cross) {
sources += [
"SpirvUtils.cpp",
"SpirvUtils.h",
]
}
# Only win32 app needs to link with user32.lib # Only win32 app needs to link with user32.lib
# In UWP, all availiable APIs are defined in WindowsApp.lib # In UWP, all availiable APIs are defined in WindowsApp.lib
if (is_win && !dawn_is_winuwp) { if (is_win && !dawn_is_winuwp) {

View File

@ -24,12 +24,16 @@
#include "dawn_native/Pipeline.h" #include "dawn_native/Pipeline.h"
#include "dawn_native/PipelineLayout.h" #include "dawn_native/PipelineLayout.h"
#include "dawn_native/RenderPipeline.h" #include "dawn_native/RenderPipeline.h"
#include "dawn_native/SpirvUtils.h" #if defined(DAWN_USE_SPIRV_CROSS)
# include "dawn_native/SpirvUtils.h"
#endif
#include "dawn_native/TintUtils.h" #include "dawn_native/TintUtils.h"
#include <spirv-tools/libspirv.hpp> #include <spirv-tools/libspirv.hpp>
#include <spirv-tools/optimizer.hpp> #include <spirv-tools/optimizer.hpp>
#include <spirv_cross.hpp> #if defined(DAWN_USE_SPIRV_CROSS)
# include <spirv_cross.hpp>
#endif
// Tint include must be after spirv_cross.hpp, because spirv-cross has its own // Tint include must be after spirv_cross.hpp, because spirv-cross has its own
// version of spirv_headers. We also need to undef SPV_REVISION because SPIRV-Cross // version of spirv_headers. We also need to undef SPV_REVISION because SPIRV-Cross
@ -529,44 +533,6 @@ namespace dawn_native {
return requiredBufferSizes; return requiredBufferSizes;
} }
ResultOrError<std::vector<uint32_t>> RunRobustBufferAccessPass(
const std::vector<uint32_t>& spirv) {
spvtools::Optimizer opt(SPV_ENV_VULKAN_1_1);
std::ostringstream errorStream;
errorStream << "SPIRV Optimizer failure:" << std::endl;
opt.SetMessageConsumer([&errorStream](spv_message_level_t level, const char*,
const spv_position_t& position,
const char* message) {
switch (level) {
case SPV_MSG_FATAL:
case SPV_MSG_INTERNAL_ERROR:
case SPV_MSG_ERROR:
errorStream << "error: line " << position.index << ": " << message
<< std::endl;
break;
case SPV_MSG_WARNING:
errorStream << "warning: line " << position.index << ": " << message
<< std::endl;
break;
case SPV_MSG_INFO:
errorStream << "info: line " << position.index << ": " << message
<< std::endl;
break;
default:
break;
}
});
opt.RegisterPass(spvtools::CreateGraphicsRobustAccessPass());
std::vector<uint32_t> result;
if (!opt.Run(spirv.data(), spirv.size(), &result, spvtools::ValidatorOptions(),
false)) {
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
}
return std::move(result);
}
MaybeError ValidateCompatibilityWithBindGroupLayout(DeviceBase* device, MaybeError ValidateCompatibilityWithBindGroupLayout(DeviceBase* device,
BindGroupIndex group, BindGroupIndex group,
const EntryPointMetadata& entryPoint, const EntryPointMetadata& entryPoint,
@ -713,6 +679,7 @@ namespace dawn_native {
return {}; return {};
} }
#if defined(DAWN_USE_SPIRV_CROSS)
ResultOrError<std::unique_ptr<EntryPointMetadata>> ExtractSpirvInfo( ResultOrError<std::unique_ptr<EntryPointMetadata>> ExtractSpirvInfo(
const DeviceBase* device, const DeviceBase* device,
const spirv_cross::Compiler& compiler, const spirv_cross::Compiler& compiler,
@ -956,6 +923,7 @@ namespace dawn_native {
return {std::move(metadata)}; return {std::move(metadata)};
} }
#endif
ResultOrError<EntryPointMetadataTable> ReflectShaderUsingTint( ResultOrError<EntryPointMetadataTable> ReflectShaderUsingTint(
DeviceBase*, DeviceBase*,
@ -1656,21 +1624,11 @@ namespace dawn_native {
mTintSource = std::move(parseResult->tintSource); mTintSource = std::move(parseResult->tintSource);
mSpirv = std::move(parseResult->spirv); mSpirv = std::move(parseResult->spirv);
if (GetDevice()->IsToggleEnabled(Toggle::UseTintGenerator)) {
DAWN_TRY_ASSIGN(mEntryPoints, ReflectShaderUsingTint(GetDevice(), mTintProgram.get())); DAWN_TRY_ASSIGN(mEntryPoints, ReflectShaderUsingTint(GetDevice(), mTintProgram.get()));
} else {
// If not using Tint to generate backend code, run the robust buffer access pass now
// since all backends will use this SPIR-V. If Tint is used, the robustness pass should
// be run per-backend.
if (GetDevice()->IsRobustnessEnabled()) {
DAWN_TRY_ASSIGN(mSpirv, RunRobustBufferAccessPass(mSpirv));
}
DAWN_TRY_ASSIGN(mEntryPoints, ReflectShaderUsingSPIRVCross(GetDevice(), mSpirv));
}
return {}; return {};
} }
#if defined(DAWN_USE_SPIRV_CROSS)
ResultOrError<EntryPointMetadataTable> ShaderModuleBase::ReflectShaderUsingSPIRVCross( ResultOrError<EntryPointMetadataTable> ShaderModuleBase::ReflectShaderUsingSPIRVCross(
DeviceBase* device, DeviceBase* device,
const std::vector<uint32_t>& spirv) { const std::vector<uint32_t>& spirv) {
@ -1688,6 +1646,7 @@ namespace dawn_native {
} }
return std::move(result); return std::move(result);
} }
#endif
size_t PipelineLayoutEntryPointPairHashFunc::operator()( size_t PipelineLayoutEntryPointPairHashFunc::operator()(
const PipelineLayoutEntryPointPair& pair) const { const PipelineLayoutEntryPointPair& pair) const {

View File

@ -240,9 +240,11 @@ namespace dawn_native {
protected: protected:
MaybeError InitializeBase(ShaderModuleParseResult* parseResult); MaybeError InitializeBase(ShaderModuleParseResult* parseResult);
#if defined(DAWN_USE_SPIRV_CROSS)
static ResultOrError<EntryPointMetadataTable> ReflectShaderUsingSPIRVCross( static ResultOrError<EntryPointMetadataTable> ReflectShaderUsingSPIRVCross(
DeviceBase* device, DeviceBase* device,
const std::vector<uint32_t>& spirv); const std::vector<uint32_t>& spirv);
#endif
private: private:
ShaderModuleBase(DeviceBase* device, ObjectBase::ErrorTag tag); ShaderModuleBase(DeviceBase* device, ObjectBase::ErrorTag tag);

View File

@ -18,6 +18,10 @@
#ifndef DAWNNATIVE_SPIRV_UTILS_H_ #ifndef DAWNNATIVE_SPIRV_UTILS_H_
#define DAWNNATIVE_SPIRV_UTILS_H_ #define DAWNNATIVE_SPIRV_UTILS_H_
#if !defined(DAWN_USE_SPIRV_CROSS)
# error "SpirvCross.h should not be included if dawn_use_spirv_cross is false"
#endif
#include "dawn_native/Format.h" #include "dawn_native/Format.h"
#include "dawn_native/PerStage.h" #include "dawn_native/PerStage.h"
#include "dawn_native/VertexFormat.h" #include "dawn_native/VertexFormat.h"

View File

@ -20,8 +20,6 @@
#include "dawn_native/Instance.h" #include "dawn_native/Instance.h"
#include "dawn_native/Surface.h" #include "dawn_native/Surface.h"
#include <spirv_cross.hpp>
namespace dawn_native { namespace null { namespace dawn_native { namespace null {
// Implementation of pre-Device objects: the null adapter, null backend connection and Connect() // Implementation of pre-Device objects: the null adapter, null backend connection and Connect()