2019-10-10 18:06:58 +00:00
|
|
|
// Copyright 2019 The Dawn 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 "dawn_native/d3d12/CommandRecordingContext.h"
|
|
|
|
#include "dawn_native/d3d12/CommandAllocatorManager.h"
|
2019-10-16 09:26:54 +00:00
|
|
|
#include "dawn_native/d3d12/D3D12Error.h"
|
2020-03-12 13:23:22 +00:00
|
|
|
#include "dawn_native/d3d12/HeapD3D12.h"
|
2019-10-10 18:06:58 +00:00
|
|
|
|
|
|
|
namespace dawn_native { namespace d3d12 {
|
|
|
|
|
2019-10-18 00:37:42 +00:00
|
|
|
void CommandRecordingContext::AddToSharedTextureList(Texture* texture) {
|
|
|
|
ASSERT(IsOpen());
|
|
|
|
mSharedTextures.insert(texture);
|
|
|
|
}
|
|
|
|
|
2019-10-10 18:06:58 +00:00
|
|
|
MaybeError CommandRecordingContext::Open(ID3D12Device* d3d12Device,
|
|
|
|
CommandAllocatorManager* commandAllocationManager) {
|
|
|
|
ASSERT(!IsOpen());
|
2019-10-16 09:26:54 +00:00
|
|
|
ID3D12CommandAllocator* commandAllocator;
|
|
|
|
DAWN_TRY_ASSIGN(commandAllocator, commandAllocationManager->ReserveCommandAllocator());
|
2019-10-10 18:06:58 +00:00
|
|
|
if (mD3d12CommandList != nullptr) {
|
2019-10-16 09:26:54 +00:00
|
|
|
MaybeError error = CheckHRESULT(mD3d12CommandList->Reset(commandAllocator, nullptr),
|
|
|
|
"D3D12 resetting command list");
|
|
|
|
if (error.IsError()) {
|
2019-10-10 18:06:58 +00:00
|
|
|
mD3d12CommandList.Reset();
|
2019-10-16 09:26:54 +00:00
|
|
|
DAWN_TRY(std::move(error));
|
2019-10-10 18:06:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ComPtr<ID3D12GraphicsCommandList> d3d12GraphicsCommandList;
|
2019-10-16 09:26:54 +00:00
|
|
|
DAWN_TRY(CheckHRESULT(
|
|
|
|
d3d12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, commandAllocator,
|
|
|
|
nullptr, IID_PPV_ARGS(&d3d12GraphicsCommandList)),
|
|
|
|
"D3D12 creating direct command list"));
|
2019-10-10 18:06:58 +00:00
|
|
|
mD3d12CommandList = std::move(d3d12GraphicsCommandList);
|
2019-11-12 18:14:21 +00:00
|
|
|
// Store a cast to ID3D12GraphicsCommandList4. This is required to use the D3D12 render
|
|
|
|
// pass APIs introduced in Windows build 1809.
|
|
|
|
mD3d12CommandList.As(&mD3d12CommandList4);
|
2019-10-10 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mIsOpen = true;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-10-18 00:37:42 +00:00
|
|
|
MaybeError CommandRecordingContext::ExecuteCommandList(ID3D12CommandQueue* d3d12CommandQueue) {
|
|
|
|
if (IsOpen()) {
|
|
|
|
// Shared textures must be transitioned to common state after the last usage in order
|
|
|
|
// for them to be used by other APIs like D3D11. We ensure this by transitioning to the
|
|
|
|
// common state right before command list submission. TransitionUsageNow itself ensures
|
|
|
|
// no unnecessary transitions happen if the resources is already in the common state.
|
|
|
|
for (Texture* texture : mSharedTextures) {
|
2020-03-12 13:23:22 +00:00
|
|
|
texture->TrackUsageAndTransitionNow(this, D3D12_RESOURCE_STATE_COMMON);
|
2019-10-18 00:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MaybeError error =
|
|
|
|
CheckHRESULT(mD3d12CommandList->Close(), "D3D12 closing pending command list");
|
|
|
|
if (error.IsError()) {
|
|
|
|
Release();
|
|
|
|
DAWN_TRY(std::move(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
ID3D12CommandList* d3d12CommandList = GetCommandList();
|
|
|
|
d3d12CommandQueue->ExecuteCommandLists(1, &d3d12CommandList);
|
|
|
|
|
|
|
|
mIsOpen = false;
|
|
|
|
mSharedTextures.clear();
|
2020-03-12 13:23:22 +00:00
|
|
|
mHeapsPendingUsage.clear();
|
2019-10-10 18:06:58 +00:00
|
|
|
}
|
2019-10-18 00:37:42 +00:00
|
|
|
return {};
|
2019-10-10 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:23:22 +00:00
|
|
|
void CommandRecordingContext::TrackHeapUsage(Heap* heap, Serial serial) {
|
|
|
|
// Before tracking the heap, check the last serial it was recorded on to ensure we aren't
|
|
|
|
// tracking it more than once.
|
|
|
|
if (heap->GetLastUsage() < serial) {
|
|
|
|
heap->SetLastUsage(serial);
|
|
|
|
mHeapsPendingUsage.push_back(heap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 18:06:58 +00:00
|
|
|
ID3D12GraphicsCommandList* CommandRecordingContext::GetCommandList() const {
|
|
|
|
ASSERT(mD3d12CommandList != nullptr);
|
|
|
|
ASSERT(IsOpen());
|
|
|
|
return mD3d12CommandList.Get();
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:14:21 +00:00
|
|
|
// This function will fail on Windows versions prior to 1809. Support must be queried through
|
|
|
|
// the device before calling.
|
|
|
|
ID3D12GraphicsCommandList4* CommandRecordingContext::GetCommandList4() const {
|
|
|
|
ASSERT(IsOpen());
|
|
|
|
ASSERT(mD3d12CommandList.Get() != nullptr);
|
|
|
|
return mD3d12CommandList4.Get();
|
|
|
|
}
|
|
|
|
|
2019-10-10 18:06:58 +00:00
|
|
|
void CommandRecordingContext::Release() {
|
|
|
|
mD3d12CommandList.Reset();
|
2019-11-12 18:14:21 +00:00
|
|
|
mD3d12CommandList4.Reset();
|
2019-10-10 18:06:58 +00:00
|
|
|
mIsOpen = false;
|
2019-10-18 00:37:42 +00:00
|
|
|
mSharedTextures.clear();
|
2020-03-12 13:23:22 +00:00
|
|
|
mHeapsPendingUsage.clear();
|
2019-10-10 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandRecordingContext::IsOpen() const {
|
|
|
|
return mIsOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
}} // namespace dawn_native::d3d12
|