Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set CPU&GPU memory usage by command line option #1248

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions USAGE_desktop_D3D12.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ Usage:
[-m <mode> | --memory-translation <mode>]
[--fw <width,height> | --force-windowed <width,height>]
[--log-level <level>] [--log-file <file>] [--log-debugview]
[--batching-memory-usage <pct>]
[--api <api>] <file>

Required arguments:
Expand Down Expand Up @@ -327,6 +328,11 @@ D3D12-only:
--dx12-override-object-names Generates unique names for all ID3D12Objects and
assigns each object the generated name.
This is intended to assist replay debugging.
--batching-memory-usage <pct>
Max amount of memory consumption while loading a trimmed capture file.
JerryAMD marked this conversation as resolved.
Show resolved Hide resolved
Acceptable values range from 0 to 100 (default: 80)
0 means no batching at all
100 means use all available system and GPU memory
```


Expand Down
4 changes: 2 additions & 2 deletions framework/decode/dx12_replay_consumer_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ void Dx12ReplayConsumerBase::ProcessInitSubresourceCommand(const format::InitSub
// If no entry exists in resource_init_infos_, this is the first subresource of a new resource.
GFXRECON_ASSERT(command_header.subresource == 0);

const double max_cpu_mem_usage = 15.0 / 16.0;
const double max_mem_usage = static_cast<double>(options_.memory_usage) / 100.0;
if (!graphics::dx12::IsMemoryAvailable(
total_size_in_bytes, extra_device_info->adapter3, max_cpu_mem_usage, extra_device_info->is_uma))
total_size_in_bytes, extra_device_info->adapter3, max_mem_usage, extra_device_info->is_uma))
{
// If neither system memory or GPU memory are able to accommodate next resource,
// execute the Copy() calls and release temp buffer to free memory
Expand Down
3 changes: 3 additions & 0 deletions framework/decode/dx_replay_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(decode)

static constexpr uint32_t kDefaultBatchingMemoryUsage = 80;

struct DxReplayOptions : public ReplayOptions
{
bool enable_d3d12{ true };
Expand All @@ -50,6 +52,7 @@ struct DxReplayOptions : public ReplayOptions
std::string screenshot_dir;
std::string screenshot_file_prefix{ kDefaultScreenshotFilePrefix };
std::string replace_dir;
int32_t memory_usage{ kDefaultBatchingMemoryUsage };
};

GFXRECON_END_NAMESPACE(decode)
Expand Down
4 changes: 2 additions & 2 deletions framework/encode/dx12_state_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,11 @@ void Dx12StateWriter::WriteResourceSnapshots(
uint64_t size_in_bytes = resource_info.get()->size_in_bytes;
auto device_info = device_wrapper->GetObjectInfo();

const double max_cpu_mem_usage = 7.0 / 8.0;
const double max_mem_usage = 7.0 / 8.0;

const bool is_uma = device_wrapper->GetObjectInfo()->is_uma;
if (!graphics::dx12::IsMemoryAvailable(
size_in_bytes, device_info.get()->adapter3, max_cpu_mem_usage, is_uma))
size_in_bytes, device_info.get()->adapter3, max_mem_usage, is_uma))
{
// If neither system memory or GPU memory are able to accommodate next resource,
// execute the existing Copy() calls and release temp buffer to free memory
Expand Down
14 changes: 7 additions & 7 deletions framework/graphics/dx12_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ bool IsUma(ID3D12Device* device)
return isUma;
}

uint64_t GetAvailableGpuAdapterMemory(IDXGIAdapter3* adapter, const bool is_uma)
uint64_t GetAvailableGpuAdapterMemory(IDXGIAdapter3* adapter, double memory_usage, const bool is_uma)
{
uint64_t available_mem = 0;

Expand All @@ -992,9 +992,10 @@ uint64_t GetAvailableGpuAdapterMemory(IDXGIAdapter3* adapter, const bool is_uma)
}
if (SUCCEEDED(adapter->QueryVideoMemoryInfo(0, memory_segment, &video_memory_info)))
{
if (video_memory_info.Budget > video_memory_info.CurrentUsage)
uint64_t total_memory = video_memory_info.Budget * memory_usage;
JerryAMD marked this conversation as resolved.
Show resolved Hide resolved
if (total_memory > video_memory_info.CurrentUsage)
{
available_mem = video_memory_info.Budget - video_memory_info.CurrentUsage;
available_mem = total_memory - video_memory_info.CurrentUsage;
}
else
{
Expand Down Expand Up @@ -1023,7 +1024,6 @@ uint64_t GetAvailableCpuMemory(double max_usage)
GFXRECON_LOG_ERROR("Failed to get available virtual memory");
}

// Only limit by available physical memory if max_usage <= 1.0.
uint64_t avail_phys = std::numeric_limits<uint64_t>::max();
JerryAMD marked this conversation as resolved.
Show resolved Hide resolved
if (max_usage <= 1.0)
{
Expand All @@ -1035,15 +1035,15 @@ uint64_t GetAvailableCpuMemory(double max_usage)
return std::min(avail_phys, mem_info.ullAvailVirtual);
}

bool IsMemoryAvailable(uint64_t required_memory, IDXGIAdapter3* adapter, double max_cpu_mem_usage, const bool is_uma)
bool IsMemoryAvailable(uint64_t required_memory, IDXGIAdapter3* adapter, double max_mem_usage, const bool is_uma)
{
bool available = false;
#ifdef _WIN64
// For 32bit, only upload one buffer at one time, to save memory usage.
if (adapter != nullptr)
{
uint64_t total_available_gpu_adapter_memory = GetAvailableGpuAdapterMemory(adapter, is_uma);
uint64_t total_available_cpu_memory = GetAvailableCpuMemory(max_cpu_mem_usage);
uint64_t total_available_gpu_adapter_memory = GetAvailableGpuAdapterMemory(adapter, max_mem_usage, is_uma);
uint64_t total_available_cpu_memory = GetAvailableCpuMemory(max_mem_usage);
uint64_t total_required_memory = static_cast<uint64_t>(required_memory * kMemoryTolerance);
if ((total_required_memory < total_available_gpu_adapter_memory) &&
(total_required_memory < total_available_cpu_memory))
Expand Down
4 changes: 2 additions & 2 deletions framework/graphics/dx12_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ bool IsUma(ID3D12Device* device);

// This function is used to get available GPU virtual memory.
// The input is current adapter which created current device.
uint64_t GetAvailableGpuAdapterMemory(IDXGIAdapter3* adapter, bool is_uma);
uint64_t GetAvailableGpuAdapterMemory(IDXGIAdapter3* adapter, double max_usage, bool is_uma);

// This function is used to get available CPU memory.
uint64_t GetAvailableCpuMemory(double max_usage);

// Give require memory size to check if there are enough CPU&GPU memory to allocate the resource. If max_cpu_mem_usage
// > 1.0, the result is not limited by available physical memory.
bool IsMemoryAvailable(uint64_t requried_memory, IDXGIAdapter3* adapter, double max_cpu_mem_usage, bool is_uma);
bool IsMemoryAvailable(uint64_t requried_memory, IDXGIAdapter3* adapter, double max_mem_usage, bool is_uma);

// Get GPU memory usage by resource desc
uint64_t GetResourceSizeInBytes(ID3D12Device* device, const D3D12_RESOURCE_DESC* desc);
Expand Down
8 changes: 7 additions & 1 deletion tools/replay/replay_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const char kArguments[] =
"--log-level,--log-file,--gpu,--gpu-group,--pause-frame,--wsi,--surface-index,-m|--memory-translation,"
"--replace-shaders,--screenshots,--denied-messages,--allowed-messages,--screenshot-format,--"
"screenshot-dir,--screenshot-prefix,--screenshot-size,--screenshot-scale,--mfr|--measurement-frame-range,--fw|--"
"force-windowed";
"force-windowed,--batching-memory-usage";

static void PrintUsage(const char* exe_name)
{
Expand Down Expand Up @@ -66,6 +66,7 @@ static void PrintUsage(const char* exe_name)
GFXRECON_WRITE_CONSOLE("\t\t\t[--fw <width,height> | --force-windowed <width,height>]");
#if defined(WIN32)
GFXRECON_WRITE_CONSOLE("\t\t\t[--log-level <level>] [--log-file <file>] [--log-debugview]");
GFXRECON_WRITE_CONSOLE("\t\t\t[--batching-memory-usage <pct>]");
#if defined(_DEBUG)
GFXRECON_WRITE_CONSOLE("\t\t\t[--api <api>] [--no-debug-popup] <file>\n");
#else
Expand Down Expand Up @@ -225,6 +226,11 @@ static void PrintUsage(const char* exe_name)
GFXRECON_WRITE_CONSOLE(" --dx12-override-object-names Generates unique names for all ID3D12Objects and");
GFXRECON_WRITE_CONSOLE(" assigns each object the generated name.");
GFXRECON_WRITE_CONSOLE(" This is intended to assist replay debugging.");
GFXRECON_WRITE_CONSOLE(" --batching-memory-usage <pct>");
GFXRECON_WRITE_CONSOLE(" \t\tMax amount of memory consumption while loading a trimmed capture file.");
GFXRECON_WRITE_CONSOLE(" \t\tAcceptable values range from 0 to 100 (default: 80)");
GFXRECON_WRITE_CONSOLE(" \t\t0 means no batching at all");
GFXRECON_WRITE_CONSOLE(" \t\t100 means use all available system and GPU memory");

#endif

Expand Down
7 changes: 7 additions & 0 deletions tools/tool_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const char kFilePerFrameOption[] = "--file-per-frame";
const char kApiFamilyOption[] = "--api";
const char kDxTwoPassReplay[] = "--dx12-two-pass-replay";
const char kDxOverrideObjectNames[] = "--dx12-override-object-names";
const char kBatchingMemoryUsageArgument[] = "--batching-memory-usage";
#endif

enum class WsiPlatform
Expand Down Expand Up @@ -880,6 +881,12 @@ static gfxrecon::decode::DxReplayOptions GetDxReplayOptions(const gfxrecon::util
replay_options.override_object_names = true;
}

const std::string& memory_usage = arg_parser.GetArgumentValue(kBatchingMemoryUsageArgument);
if (!memory_usage.empty())
{
replay_options.memory_usage = std::stoi(memory_usage);
JerryAMD marked this conversation as resolved.
Show resolved Hide resolved
}

replay_options.screenshot_ranges = GetScreenshotRanges(arg_parser);
replay_options.screenshot_format = GetScreenshotFormat(arg_parser);
replay_options.screenshot_dir = GetScreenshotDir(arg_parser);
Expand Down