D3D12: Initialize DXC in Device::Initialize()

This patch moves the initialization of DXC libraries from
BackendD3D12::GetOrCreateXXX() to Device::Initialize() so
that we don't need to deal with those lazy initializations
in the asynchronous path of Create*PipelineAsync().

BUG=dawn:529

Change-Id: I792847c138738ae8c300d7a1bf3d23fc8fecd746
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/48580
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
This commit is contained in:
Jiawei Shao 2021-04-21 14:35:01 +00:00 committed by Commit Bot service account
parent 672105aa0a
commit d0e8dc0e92
5 changed files with 52 additions and 27 deletions

View File

@ -102,34 +102,49 @@ namespace dawn_native { namespace d3d12 {
return mFactory;
}
ResultOrError<IDxcLibrary*> Backend::GetOrCreateDxcLibrary() {
MaybeError Backend::EnsureDxcLibrary() {
if (mDxcLibrary == nullptr) {
DAWN_TRY(CheckHRESULT(
mFunctions->dxcCreateInstance(CLSID_DxcLibrary, IID_PPV_ARGS(&mDxcLibrary)),
"DXC create library"));
ASSERT(mDxcLibrary != nullptr);
}
return mDxcLibrary.Get();
return {};
}
ResultOrError<IDxcCompiler*> Backend::GetOrCreateDxcCompiler() {
MaybeError Backend::EnsureDxcCompiler() {
if (mDxcCompiler == nullptr) {
DAWN_TRY(CheckHRESULT(
mFunctions->dxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&mDxcCompiler)),
"DXC create compiler"));
ASSERT(mDxcCompiler != nullptr);
}
return mDxcCompiler.Get();
return {};
}
ResultOrError<IDxcValidator*> Backend::GetOrCreateDxcValidator() {
MaybeError Backend::EnsureDxcValidator() {
if (mDxcValidator == nullptr) {
DAWN_TRY(CheckHRESULT(
mFunctions->dxcCreateInstance(CLSID_DxcValidator, IID_PPV_ARGS(&mDxcValidator)),
"DXC create validator"));
ASSERT(mDxcValidator != nullptr);
}
return mDxcValidator.Get();
return {};
}
ComPtr<IDxcLibrary> Backend::GetDxcLibrary() const {
ASSERT(mDxcLibrary != nullptr);
return mDxcLibrary;
}
ComPtr<IDxcCompiler> Backend::GetDxcCompiler() const {
ASSERT(mDxcCompiler != nullptr);
return mDxcCompiler;
}
ComPtr<IDxcValidator> Backend::GetDxcValidator() const {
ASSERT(mDxcValidator != nullptr);
return mDxcValidator;
}
const PlatformFunctions* Backend::GetFunctions() const {

View File

@ -30,9 +30,14 @@ namespace dawn_native { namespace d3d12 {
MaybeError Initialize();
ComPtr<IDXGIFactory4> GetFactory() const;
ResultOrError<IDxcLibrary*> GetOrCreateDxcLibrary();
ResultOrError<IDxcCompiler*> GetOrCreateDxcCompiler();
ResultOrError<IDxcValidator*> GetOrCreateDxcValidator();
MaybeError EnsureDxcLibrary();
MaybeError EnsureDxcCompiler();
MaybeError EnsureDxcValidator();
ComPtr<IDxcLibrary> GetDxcLibrary() const;
ComPtr<IDxcCompiler> GetDxcCompiler() const;
ComPtr<IDxcValidator> GetDxcValidator() const;
const PlatformFunctions* GetFunctions() const;
std::vector<std::unique_ptr<AdapterBase>> DiscoverDefaultAdapters() override;

View File

@ -160,7 +160,7 @@ namespace dawn_native { namespace d3d12 {
// The environment can only use DXC when it's available. Override the decision if it is not
// applicable.
ApplyUseDxcToggle();
DAWN_TRY(ApplyUseDxcToggle());
return {};
}
@ -196,25 +196,33 @@ namespace dawn_native { namespace d3d12 {
return ToBackend(GetAdapter())->GetBackend()->GetFactory();
}
void Device::ApplyUseDxcToggle() {
MaybeError Device::ApplyUseDxcToggle() {
if (!ToBackend(GetAdapter())->GetBackend()->GetFunctions()->IsDXCAvailable()) {
ForceSetToggle(Toggle::UseDXC, false);
} else if (IsExtensionEnabled(Extension::ShaderFloat16)) {
// Currently we can only use DXC to compile HLSL shaders using float16.
ForceSetToggle(Toggle::UseDXC, true);
}
if (IsToggleEnabled(Toggle::UseDXC)) {
DAWN_TRY(ToBackend(GetAdapter())->GetBackend()->EnsureDxcCompiler());
DAWN_TRY(ToBackend(GetAdapter())->GetBackend()->EnsureDxcLibrary());
DAWN_TRY(ToBackend(GetAdapter())->GetBackend()->EnsureDxcValidator());
}
return {};
}
ResultOrError<IDxcLibrary*> Device::GetOrCreateDxcLibrary() const {
return ToBackend(GetAdapter())->GetBackend()->GetOrCreateDxcLibrary();
ComPtr<IDxcLibrary> Device::GetDxcLibrary() const {
return ToBackend(GetAdapter())->GetBackend()->GetDxcLibrary();
}
ResultOrError<IDxcCompiler*> Device::GetOrCreateDxcCompiler() const {
return ToBackend(GetAdapter())->GetBackend()->GetOrCreateDxcCompiler();
ComPtr<IDxcCompiler> Device::GetDxcCompiler() const {
return ToBackend(GetAdapter())->GetBackend()->GetDxcCompiler();
}
ResultOrError<IDxcValidator*> Device::GetOrCreateDxcValidator() const {
return ToBackend(GetAdapter())->GetBackend()->GetOrCreateDxcValidator();
ComPtr<IDxcValidator> Device::GetDxcValidator() const {
return ToBackend(GetAdapter())->GetBackend()->GetDxcValidator();
}
const PlatformFunctions* Device::GetFunctions() const {

View File

@ -65,9 +65,9 @@ namespace dawn_native { namespace d3d12 {
const PlatformFunctions* GetFunctions() const;
ComPtr<IDXGIFactory4> GetFactory() const;
ResultOrError<IDxcLibrary*> GetOrCreateDxcLibrary() const;
ResultOrError<IDxcCompiler*> GetOrCreateDxcCompiler() const;
ResultOrError<IDxcValidator*> GetOrCreateDxcValidator() const;
ComPtr<IDxcLibrary> GetDxcLibrary() const;
ComPtr<IDxcCompiler> GetDxcCompiler() const;
ComPtr<IDxcValidator> GetDxcValidator() const;
ResultOrError<CommandRecordingContext*> GetPendingCommandContext();
@ -177,7 +177,7 @@ namespace dawn_native { namespace d3d12 {
MaybeError CheckDebugLayerAndGenerateErrors();
void ApplyUseDxcToggle();
MaybeError ApplyUseDxcToggle();
ComPtr<ID3D12Fence> mFence;
HANDLE mFenceEvent = nullptr;

View File

@ -97,16 +97,14 @@ namespace dawn_native { namespace d3d12 {
const std::string& hlslSource,
const char* entryPoint,
uint32_t compileFlags) {
IDxcLibrary* dxcLibrary;
DAWN_TRY_ASSIGN(dxcLibrary, device->GetOrCreateDxcLibrary());
ComPtr<IDxcLibrary> dxcLibrary = device->GetDxcLibrary();
ComPtr<IDxcBlobEncoding> sourceBlob;
DAWN_TRY(CheckHRESULT(dxcLibrary->CreateBlobWithEncodingOnHeapCopy(
hlslSource.c_str(), hlslSource.length(), CP_UTF8, &sourceBlob),
"DXC create blob"));
IDxcCompiler* dxcCompiler;
DAWN_TRY_ASSIGN(dxcCompiler, device->GetOrCreateDxcCompiler());
ComPtr<IDxcCompiler> dxcCompiler = device->GetDxcCompiler();
std::wstring entryPointW;
DAWN_TRY_ASSIGN(entryPointW, ConvertStringToWstring(entryPoint));
@ -478,8 +476,7 @@ namespace dawn_native { namespace d3d12 {
}
ResultOrError<uint64_t> ShaderModule::GetDXCompilerVersion() const {
ComPtr<IDxcValidator> dxcValidator;
DAWN_TRY_ASSIGN(dxcValidator, ToBackend(GetDevice())->GetOrCreateDxcValidator());
ComPtr<IDxcValidator> dxcValidator = ToBackend(GetDevice())->GetDxcValidator();
ComPtr<IDxcVersionInfo> versionInfo;
DAWN_TRY(CheckHRESULT(dxcValidator.As(&versionInfo),