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; return mFactory;
} }
ResultOrError<IDxcLibrary*> Backend::GetOrCreateDxcLibrary() { MaybeError Backend::EnsureDxcLibrary() {
if (mDxcLibrary == nullptr) { if (mDxcLibrary == nullptr) {
DAWN_TRY(CheckHRESULT( DAWN_TRY(CheckHRESULT(
mFunctions->dxcCreateInstance(CLSID_DxcLibrary, IID_PPV_ARGS(&mDxcLibrary)), mFunctions->dxcCreateInstance(CLSID_DxcLibrary, IID_PPV_ARGS(&mDxcLibrary)),
"DXC create library")); "DXC create library"));
ASSERT(mDxcLibrary != nullptr); ASSERT(mDxcLibrary != nullptr);
} }
return mDxcLibrary.Get(); return {};
} }
ResultOrError<IDxcCompiler*> Backend::GetOrCreateDxcCompiler() { MaybeError Backend::EnsureDxcCompiler() {
if (mDxcCompiler == nullptr) { if (mDxcCompiler == nullptr) {
DAWN_TRY(CheckHRESULT( DAWN_TRY(CheckHRESULT(
mFunctions->dxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&mDxcCompiler)), mFunctions->dxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&mDxcCompiler)),
"DXC create compiler")); "DXC create compiler"));
ASSERT(mDxcCompiler != nullptr); ASSERT(mDxcCompiler != nullptr);
} }
return mDxcCompiler.Get(); return {};
} }
ResultOrError<IDxcValidator*> Backend::GetOrCreateDxcValidator() { MaybeError Backend::EnsureDxcValidator() {
if (mDxcValidator == nullptr) { if (mDxcValidator == nullptr) {
DAWN_TRY(CheckHRESULT( DAWN_TRY(CheckHRESULT(
mFunctions->dxcCreateInstance(CLSID_DxcValidator, IID_PPV_ARGS(&mDxcValidator)), mFunctions->dxcCreateInstance(CLSID_DxcValidator, IID_PPV_ARGS(&mDxcValidator)),
"DXC create validator")); "DXC create validator"));
ASSERT(mDxcValidator != nullptr); 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 { const PlatformFunctions* Backend::GetFunctions() const {

View File

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

View File

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

View File

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