Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/workflows/.patches/dawn.diff
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
From e41eefd8175a3cd7125699987d452dbfac72fb44 Mon Sep 17 00:00:00 2001
From 25b98595af5e7bcc4cc46d964be139601d3a4a70 Mon Sep 17 00:00:00 2001
From: Bryan Bernhart <bryan.bernhart@intel.com>
Date: Tue, 15 Feb 2022 17:25:29 -0800
Subject: [PATCH] Use GPGMM for D3D12 backend.
Expand Down Expand Up @@ -495,7 +495,7 @@ index 9747b41c0..50db56dbd 100644

AdapterDiscoveryOptions::AdapterDiscoveryOptions()
diff --git a/src/dawn/native/d3d12/DeviceD3D12.cpp b/src/dawn/native/d3d12/DeviceD3D12.cpp
index 90ddc3041..8545685aa 100644
index 90ddc3041..8d94ead35 100644
--- a/src/dawn/native/d3d12/DeviceD3D12.cpp
+++ b/src/dawn/native/d3d12/DeviceD3D12.cpp
@@ -130,8 +130,43 @@ MaybeError Device::Initialize(const DeviceDescriptor* descriptor) {
Expand Down Expand Up @@ -525,19 +525,19 @@ index 90ddc3041..8545685aa 100644
+ allocatorDesc.Flags |= gpgmm::d3d12::ALLOCATOR_FLAG_ALWAYS_IN_BUDGET;
+ }
+
+ if (IsToggleEnabled(Toggle::DumpResourceAllocator)) {
+ residencyDesc.RecordOptions.Flags |= gpgmm::d3d12::EVENT_RECORD_FLAG_ALL_EVENTS;
+ residencyDesc.RecordOptions.EventScope = gpgmm::d3d12::EVENT_RECORD_SCOPE_PER_INSTANCE;
+ residencyDesc.RecordOptions.TraceFile = "dawn_resource_allocator_dump.json";
+ }
+
+ if (IsToggleEnabled(Toggle::UseD3D12ResidencyManagement)) {
+ residencyDesc.VideoMemoryBudget = 0.95; // Use up to 95%
+ DAWN_TRY(CheckHRESULT(gpgmm::d3d12::ResidencyManager::CreateResidencyManager(
+ residencyDesc, &mResidencyManager),
+ "D3D12 create residency manager"));
+ }
+
+ if (IsToggleEnabled(Toggle::DumpResourceAllocator)) {
+ allocatorDesc.RecordOptions.Flags |= gpgmm::d3d12::ALLOCATOR_RECORD_FLAG_ALL_EVENTS;
+ allocatorDesc.RecordOptions.EventScope = gpgmm::d3d12::ALLOCATOR_RECORD_SCOPE_PER_INSTANCE;
+ allocatorDesc.RecordOptions.TraceFile = "dawn_resource_allocator_dump.json";
+ }
+
+ DAWN_TRY(CheckHRESULT(gpgmm::d3d12::ResourceAllocator::CreateAllocator(
+ allocatorDesc, mResidencyManager.Get(), &mResourceAllocator),
+ "D3D12 create resource allocator"));
Expand Down
116 changes: 116 additions & 0 deletions src/gpgmm/d3d12/EventRecordD3D12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2021 The GPGMM 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 GPGMM_D3D12_EVENTRECORDD3D12_H_
#define GPGMM_D3D12_EVENTRECORDD3D12_H_

#include "gpgmm/d3d12/d3d12_platform.h"
#include "gpgmm/utils/Flags.h"

namespace gpgmm::d3d12 {

/** \enum EVENT_RECORD_FLAGS
Represents different event categories to record.
*/
enum EVENT_RECORD_FLAGS {

/** \brief Record nothing.
*/
EVENT_RECORD_FLAG_NONE = 0x0,

/** \brief Record lifetimes of API objects created by GPGMM.
*/
EVENT_RECORD_FLAG_API_OBJECTS = 0x1,

/** \brief Record API calls made to GPGMM.
*/
EVENT_RECORD_FLAG_API_CALLS = 0x2,

/** \brief Record duration of GPGMM API calls.
*/
EVENT_RECORD_FLAG_API_TIMINGS = 0x4,

/** \brief Record metrics made to GPGMM API calls.
*/
EVENT_RECORD_FLAG_COUNTERS = 0x8,

/** \brief Record events required for playback.

Bitwise OR'd combination of EVENT_RECORD_FLAG_API_OBJECTS and
EVENT_RECORD_FLAG_API_CALLS.
*/
EVENT_RECORD_FLAG_CAPTURE = 0x3,

/** \brief Record everything.
*/
EVENT_RECORD_FLAG_ALL_EVENTS = 0xFF,
};

using EVENT_RECORD_FLAGS_TYPE = Flags<EVENT_RECORD_FLAGS>;
DEFINE_OPERATORS_FOR_FLAGS(EVENT_RECORD_FLAGS_TYPE)

/** \enum EVENT_RECORD_SCOPE
Represents recording scopes to limit event recording.
*/
enum EVENT_RECORD_SCOPE {

/** \brief Scopes events per process (or multiple instances).
*/
EVENT_RECORD_SCOPE_PER_PROCESS = 0x1,

/** \brief Scopes events per instance.
*/
EVENT_RECORD_SCOPE_PER_INSTANCE = 0x2,
};

/** \struct EVENT_RECORD_OPTIONS
Represents additional controls for recording.
*/
struct EVENT_RECORD_OPTIONS {
/** \brief Flags used to decide what to record.

Optional parameter. By default, nothing is recorded.
*/
EVENT_RECORD_FLAGS_TYPE Flags = EVENT_RECORD_FLAG_NONE;

/** \brief Minimum severity level to record messages.

Messages with lower severity will be ignored.

Optional parameter. By default, the minimum severity level is WARN.
*/
D3D12_MESSAGE_SEVERITY MinMessageLevel = D3D12_MESSAGE_SEVERITY_WARNING;

/** \brief Specifies the scope of the events.

Optional parameter. By default, recording is per process.
*/
EVENT_RECORD_SCOPE EventScope = EVENT_RECORD_SCOPE_PER_PROCESS;

/** \brief Record detailed timing events.

Optional parameter. By default, detailed timing events are disabled.
*/
bool UseDetailedTimingEvents = false;

/** \brief Path to trace file.

Optional parameter. By default, a trace file is created for you.
*/
std::string TraceFile;
};

} // namespace gpgmm::d3d12

#endif // GPGMM_D3D12_EVENTRECORDD3D12_H_
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/JSONSerializerD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace gpgmm::d3d12 {
}

// static
JSONDict JSONSerializer::Serialize(const ALLOCATOR_RECORD_OPTIONS& desc) {
JSONDict JSONSerializer::Serialize(const EVENT_RECORD_OPTIONS& desc) {
JSONDict dict;
dict.AddItem("Flags", desc.Flags);
dict.AddItem("MinMessageLevel", desc.MinMessageLevel);
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/d3d12/JSONSerializerD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace gpgmm::d3d12 {
// Forward declare backend types.
struct ALLOCATION_DESC;
struct ALLOCATOR_DESC;
struct ALLOCATOR_RECORD_OPTIONS;
struct EVENT_RECORD_OPTIONS;
struct HEAP_DESC;
struct HEAP_INFO;
struct RESOURCE_ALLOCATION_DESC;
Expand Down Expand Up @@ -62,7 +62,7 @@ namespace gpgmm::d3d12 {
static JSONDict Serialize(const RESIDENCY_DESC& desc);

private:
static JSONDict Serialize(const ALLOCATOR_RECORD_OPTIONS& desc);
static JSONDict Serialize(const EVENT_RECORD_OPTIONS& desc);
static JSONDict Serialize(const D3D12_DEPTH_STENCIL_VALUE& depthStencilValue);
static JSONDict Serialize(const FLOAT rgba[4]);
static JSONDict Serialize(const D3D12_CLEAR_VALUE* clearValue);
Expand Down
19 changes: 18 additions & 1 deletion src/gpgmm/d3d12/ResidencyManagerD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "gpgmm/d3d12/HeapD3D12.h"
#include "gpgmm/d3d12/JSONSerializerD3D12.h"
#include "gpgmm/d3d12/ResidencySetD3D12.h"
#include "gpgmm/d3d12/UtilsD3D12.h"
#include "gpgmm/utils/Math.h"

#include <algorithm>
Expand All @@ -32,7 +33,7 @@
namespace gpgmm::d3d12 {

static constexpr uint32_t kDefaultEvictBatchSize = GPGMM_MB_TO_BYTES(50);
static constexpr float kDefaultVideoMemoryBudget = 0.95f; // 95%
static constexpr float kDefaultVideoMemoryBudget = 0.95f; // 95%

// Creates a long-lived task to recieve and process OS budget change events.
class BudgetUpdateTask : public VoidCallback {
Expand Down Expand Up @@ -161,6 +162,16 @@ namespace gpgmm::d3d12 {
<< "Video memory budget was ignored since a budget was already specified.";
}

if (descriptor.RecordOptions.Flags != EVENT_RECORD_FLAG_NONE) {
StartupEventTrace(descriptor.RecordOptions.TraceFile,
static_cast<TraceEventPhase>(~descriptor.RecordOptions.Flags | 0),
descriptor.RecordOptions.EventScope & EVENT_RECORD_SCOPE_PER_PROCESS);

SetEventMessageLevel(GetLogSeverity(descriptor.RecordOptions.MinMessageLevel));
}

SetLogMessageLevel(GetLogSeverity(descriptor.MinLogLevel));

std::unique_ptr<ResidencyManager> residencyManager = std::unique_ptr<ResidencyManager>(
new ResidencyManager(descriptor, std::move(residencyFence)));

Expand Down Expand Up @@ -211,6 +222,8 @@ namespace gpgmm::d3d12 {
: descriptor.EvictBatchSize),
mIsUMA(descriptor.IsUMA),
mIsBudgetChangeEventsDisabled(descriptor.UpdateBudgetByPolling),
mShutdownEventTrace(descriptor.RecordOptions.EventScope &
EVENT_RECORD_SCOPE_PER_INSTANCE),
mThreadPool(ThreadPool::Create()) {
GPGMM_TRACE_EVENT_OBJECT_NEW(this);

Expand All @@ -222,6 +235,10 @@ namespace gpgmm::d3d12 {
ResidencyManager::~ResidencyManager() {
GPGMM_TRACE_EVENT_OBJECT_DESTROY(this);
StopBudgetNotificationUpdates();

if (mShutdownEventTrace) {
ShutdownEventTrace();
}
}

const char* ResidencyManager::GetTypename() const {
Expand Down
18 changes: 18 additions & 0 deletions src/gpgmm/d3d12/ResidencyManagerD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef GPGMM_D3D12_RESIDENCYMANAGERD3D12_H_
#define GPGMM_D3D12_RESIDENCYMANAGERD3D12_H_

#include "gpgmm/d3d12/EventRecordD3D12.h"
#include "gpgmm/d3d12/IUnknownImplD3D12.h"
#include "gpgmm/utils/LinkedList.h"
#include "include/gpgmm_export.h"
Expand Down Expand Up @@ -59,6 +60,22 @@ namespace gpgmm::d3d12 {
*/
bool IsUMA;

/** \brief Minimum severity level to log messages to console.

Messages with lower severity will be ignored.

Optional parameter. By default, will log only corruption messages.
*/
D3D12_MESSAGE_SEVERITY MinLogLevel = D3D12_MESSAGE_SEVERITY_WARNING;

/** \brief Specifies recording options.

For example, what events to record, and where to record them.

Optional parameter. By default, no options are specified for recording.
*/
EVENT_RECORD_OPTIONS RecordOptions;

/** \brief Total budget of video memory, expressed as a percentage.

Optional parameter. When 0 is specified, the API will automatically set the video
Expand Down Expand Up @@ -271,6 +288,7 @@ namespace gpgmm::d3d12 {
const uint64_t mEvictBatchSize;
const bool mIsUMA;
const bool mIsBudgetChangeEventsDisabled;
const bool mShutdownEventTrace;

VideoMemorySegment mLocalVideoMemorySegment;
VideoMemorySegment mNonLocalVideoMemorySegment;
Expand Down
31 changes: 10 additions & 21 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,6 @@ namespace gpgmm::d3d12 {
}
}

LogSeverity GetLogSeverity(D3D12_MESSAGE_SEVERITY messageSeverity) {
switch (messageSeverity) {
case D3D12_MESSAGE_SEVERITY_CORRUPTION:
case D3D12_MESSAGE_SEVERITY_ERROR:
return LogSeverity::Error;
case D3D12_MESSAGE_SEVERITY_WARNING:
return LogSeverity::Warning;
case D3D12_MESSAGE_SEVERITY_INFO:
return LogSeverity::Info;
case D3D12_MESSAGE_SEVERITY_MESSAGE:
return LogSeverity::Debug;
default:
UNREACHABLE();
return LogSeverity::Debug;
}
}

// https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_heap_flags
uint64_t GetHeapAlignment(D3D12_HEAP_FLAGS heapFlags, bool allowMSAA) {
const D3D12_HEAP_FLAGS denyAllTexturesFlags =
Expand Down Expand Up @@ -300,6 +283,8 @@ namespace gpgmm::d3d12 {
RESIDENCY_DESC residencyDesc = {};
residencyDesc.Device = allocatorDescriptor.Device;
residencyDesc.IsUMA = allocatorDescriptor.IsUMA;
residencyDesc.MinLogLevel = allocatorDescriptor.MinLogLevel;
residencyDesc.RecordOptions = allocatorDescriptor.RecordOptions;
ReturnIfFailed(allocatorDescriptor.Adapter.As(&residencyDesc.Adapter));

ReturnIfFailed(
Expand Down Expand Up @@ -355,16 +340,20 @@ namespace gpgmm::d3d12 {
return E_INVALIDARG;
}

if (newDescriptor.RecordOptions.Flags != ALLOCATOR_RECORD_FLAG_NONE) {
if (!IsEventTraceEnabled() && newDescriptor.RecordOptions.Flags != EVENT_RECORD_FLAG_NONE) {
StartupEventTrace(
allocatorDescriptor.RecordOptions.TraceFile,
static_cast<TraceEventPhase>(~newDescriptor.RecordOptions.Flags | 0),
allocatorDescriptor.RecordOptions.EventScope & ALLOCATOR_RECORD_SCOPE_PER_PROCESS);
allocatorDescriptor.RecordOptions.EventScope & EVENT_RECORD_SCOPE_PER_PROCESS);

SetEventMessageLevel(GetLogSeverity(newDescriptor.RecordOptions.MinMessageLevel));
}

SetLogMessageLevel(GetLogSeverity(newDescriptor.MinLogLevel));
// Do not override the default min. log level specified by the residency manager.
// Only if this allocator is without residency, does the min. log level have affect.
if (pResidencyManager == nullptr) {
SetLogMessageLevel(GetLogSeverity(newDescriptor.MinLogLevel));
}

#if defined(GPGMM_ENABLE_DEVICE_CHECKS)
ComPtr<ID3D12InfoQueue> leakMessageQueue;
Expand Down Expand Up @@ -403,7 +392,7 @@ namespace gpgmm::d3d12 {
mIsAlwaysInBudget(descriptor.Flags & ALLOCATOR_FLAG_ALWAYS_IN_BUDGET),
mMaxResourceHeapSize(descriptor.MaxResourceHeapSize),
mShutdownEventTrace(descriptor.RecordOptions.EventScope &
ALLOCATOR_RECORD_SCOPE_PER_INSTANCE),
EVENT_RECORD_SCOPE_PER_INSTANCE),
mUseDetailedTimingEvents(descriptor.RecordOptions.UseDetailedTimingEvents) {
GPGMM_TRACE_EVENT_OBJECT_NEW(this);

Expand Down
Loading