Skip to content

Commit

Permalink
Count frames and trim per instance
Browse files Browse the repository at this point in the history
  • Loading branch information
panos-lunarg committed Feb 4, 2025
1 parent 13e850b commit 66cfb10
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 16 deletions.
74 changes: 61 additions & 13 deletions framework/encode/capture_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/

#include "encode/capture_settings.h"
#include <cstdint>
#include <limits>
#include <string>
#include PROJECT_VERSION_HEADER_FILE

Expand Down Expand Up @@ -67,14 +69,16 @@ CommonCaptureManager::CommonCaptureManager() :
memory_tracking_mode_(CaptureSettings::MemoryTrackingMode::kPageGuard), page_guard_align_buffer_sizes_(false),
page_guard_track_ahb_memory_(false), page_guard_unblock_sigsegv_(false), page_guard_signal_handler_watcher_(false),
page_guard_memory_mode_(kMemoryModeShadowInternal), page_guard_external_memory_(false), trim_enabled_(false),
trim_boundary_(CaptureSettings::TrimBoundary::kUnknown), trim_current_range_(0), current_frame_(kFirstFrame),
trim_boundary_(CaptureSettings::TrimBoundary::kUnknown), trim_current_range_(0), global_frame_counter_(kFirstFrame),
queue_submit_count_(0), capture_mode_(kModeWrite), previous_hotkey_state_(false),
previous_runtime_trigger_state_(CaptureSettings::RuntimeTriggerState::kNotUsed), debug_layer_(false),
debug_device_lost_(false), screenshot_prefix_(""), screenshots_enabled_(false), disable_dxr_(false),
accel_struct_padding_(0), iunknown_wrapping_(false), force_command_serialization_(false), queue_zero_only_(false),
allow_pipeline_compile_required_(false), quit_after_frame_ranges_(false), use_asset_file_(false), block_index_(0),
write_assets_(false), previous_write_assets_(false)
{}
{
instance_frame_counters.push_back(kFirstFrame);
}

CommonCaptureManager::~CommonCaptureManager()
{
Expand Down Expand Up @@ -173,6 +177,7 @@ bool CommonCaptureManager::LockedCreateInstance(ApiCaptureManager* api
}

++instance_count_;
instance_frame_counters.push_back(kFirstFrame);

GFXRECON_LOG_DEBUG("CommonCaptureManager::CreateInstance(): Current instance count is %u", instance_count_);

Expand Down Expand Up @@ -201,6 +206,7 @@ void CommonCaptureManager::DestroyInstance(ApiCaptureManager* api_capture_manage
}
--instance_count_;
GFXRECON_LOG_DEBUG("CommonCaptureManager::DestroyInstance(): Current instance count is %u", instance_count_);
instance_frame_counters.pop_back();

if (instance_count_ == 0)
{
Expand Down Expand Up @@ -281,6 +287,13 @@ bool CommonCaptureManager::Initialize(format::ApiFamilyId api_
force_fifo_present_mode_ = trace_settings.force_fifo_present_mode;
use_asset_file_ = trace_settings.use_asset_file;

count_frames_per_instance_ = trace_settings.trim_instance_index != std::numeric_limits<uint32_t>::max();
trim_instance_index_ = trace_settings.trim_instance_index;

// GFXRECON_WRITE_CONSOLE("CommonCaptureManager::%s()", __func__)
// GFXRECON_WRITE_CONSOLE("count_frames_per_instance_: %u", count_frames_per_instance_)
// GFXRECON_WRITE_CONSOLE("trim_instance_index_: %u", trim_instance_index_)

rv_annotation_info_.gpuva_mask = trace_settings.rv_anotation_info.gpuva_mask;
rv_annotation_info_.descriptor_mask = trace_settings.rv_anotation_info.descriptor_mask;

Expand Down Expand Up @@ -368,11 +381,13 @@ bool CommonCaptureManager::Initialize(format::ApiFamilyId api_

trim_ranges_ = trace_settings.trim_ranges;

uint32_t current_frame = CurrentFrame();

// Determine if trim starts at the first frame
if ((trim_boundary_ == CaptureSettings::TrimBoundary::kFrames) && (trim_ranges_[0].first == current_frame_))
if ((trim_boundary_ == CaptureSettings::TrimBoundary::kFrames) && (trim_ranges_[0].first == current_frame))
{
// When capturing from the first frame, state tracking only needs to be enabled if there is more than
// one capture range.
// When capturing from the first frame, state tracking only needs to be enabled if there is more
// than one capture range.
if (trim_ranges_.size() > 1)
{
capture_mode_ = kModeWriteAndTrack;
Expand Down Expand Up @@ -400,8 +415,9 @@ bool CommonCaptureManager::Initialize(format::ApiFamilyId api_
if (IsTrimHotkeyPressed() ||
trace_settings.runtime_capture_trigger == CaptureSettings::RuntimeTriggerState::kEnabled)
{
capture_mode_ = kModeWriteAndTrack;
trim_key_first_frame_ = current_frame_;
capture_mode_ = kModeWriteAndTrack;
trim_key_first_frame_ =
count_frames_per_instance_ ? instance_frame_counters[instance_count_] : global_frame_counter_;

success = CreateCaptureFile(api_family,
util::filepath::InsertFilenamePostfix(base_filename_, "_trim_trigger"));
Expand Down Expand Up @@ -852,7 +868,7 @@ bool CommonCaptureManager::ShouldTriggerScreenshot()
uint32_t target_frame = screenshot_indices_.back();

// If this is a frame of interest, take a screenshot
if (target_frame == current_frame_)
if (target_frame == global_frame_counter_)
{
triger_screenshot = true;

Expand All @@ -879,31 +895,58 @@ void CommonCaptureManager::WriteFrameMarker(format::MarkerType marker_type)
marker_cmd.header.size = sizeof(marker_cmd.marker_type) + sizeof(marker_cmd.frame_number);
marker_cmd.header.type = format::BlockType::kFrameMarkerBlock;
marker_cmd.marker_type = marker_type;
marker_cmd.frame_number = current_frame_;
marker_cmd.frame_number = global_frame_counter_;
WriteToFile(&marker_cmd, sizeof(marker_cmd));
}
}

uint32_t CommonCaptureManager::CurrentFrame() const
{
// GFXRECON_WRITE_CONSOLE("%s()", __func__)
// GFXRECON_WRITE_CONSOLE(" instance_count_: %u", instance_count_)

uint32_t current_frame;

if (!count_frames_per_instance_)
{
current_frame = global_frame_counter_;
}
else
{
uint32_t current_instance = instance_count_ ? instance_count_ - 1 : 0;
current_frame =
trim_instance_index_ == current_instance ? instance_frame_counters[current_instance] : std::numeric_limits<uint32_t>::max();
}

// GFXRECON_WRITE_CONSOLE(" current_frame: %u", current_frame)

return current_frame;
}

void CommonCaptureManager::EndFrame(format::ApiFamilyId api_family, std::shared_lock<ApiCallMutexT>& current_lock)
{
// Write an end-of-frame marker to the capture file.
WriteFrameMarker(format::MarkerType::kEndMarker);

++current_frame_;
++global_frame_counter_;
GFXRECON_ASSERT(instance_count_);
++instance_frame_counters[instance_count_ - 1];

uint32_t current_frame = CurrentFrame();

if (trim_enabled_ && (trim_boundary_ == CaptureSettings::TrimBoundary::kFrames))
{
if ((capture_mode_ & kModeWrite) == kModeWrite)
{
// Currently capturing a frame range.
// Check for end of range or hotkey trigger to stop capture.
CheckContinueCaptureForWriteMode(api_family, current_frame_, current_lock);
CheckContinueCaptureForWriteMode(api_family, current_frame, current_lock);
}
else if ((capture_mode_ & kModeTrack) == kModeTrack)
{
// Capture is not active.
// Check for start of capture frame range or hotkey trigger to start capture
CheckStartCaptureForTrackMode(api_family, current_frame_, current_lock);
CheckStartCaptureForTrackMode(api_family, current_frame, current_lock);
}
}

Expand Down Expand Up @@ -1056,6 +1099,10 @@ bool CommonCaptureManager::CreateCaptureFile(format::ApiFamilyId api_family, con
capture_filename_ = util::filepath::GenerateTimestampedFilename(capture_filename_);
}

// GFXRECON_WRITE_CONSOLE("CommonCaptureManager::%s()", __func__)
// GFXRECON_WRITE_CONSOLE(" file_stream_: %p", file_stream_.get())
// GFXRECON_WRITE_CONSOLE(" capture_filename_: %s", capture_filename_.c_str())

file_stream_ = std::make_unique<CaptureFileOutputStream>(this, capture_filename_, kFileStreamBufferSize);

if (file_stream_->IsValid())
Expand Down Expand Up @@ -1629,7 +1676,8 @@ CaptureFileOutputStream::CaptureFileOutputStream(CommonCaptureManager* capture_m
const std::string& filename,
size_t buffer_size,
bool append) :
FileOutputStream(filename, buffer_size, append), capture_manager_(capture_manager)
FileOutputStream(filename, buffer_size, append),
capture_manager_(capture_manager)
{}

bool CaptureFileOutputStream::Write(const void* data, size_t len)
Expand Down
12 changes: 9 additions & 3 deletions framework/encode/capture_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class CommonCaptureManager
PageGuardMemoryMode GetPageGuardMemoryMode() const { return page_guard_memory_mode_; }
const std::string& GetTrimKey() const { return trim_key_; }
bool IsTrimEnabled() const { return trim_enabled_; }
uint32_t GetCurrentFrame() const { return current_frame_; }
uint32_t GetCurrentFrame() const { return global_frame_counter_; }
CaptureMode GetCaptureMode() const { return capture_mode_; }
void SetCaptureMode(CaptureMode new_mode) { capture_mode_ = new_mode; }
bool GetDebugLayerSetting() const { return debug_layer_; }
Expand Down Expand Up @@ -290,7 +290,7 @@ class CommonCaptureManager

template <size_t N>
void CombineAndWriteToFile(const std::pair<const void*, size_t> (&buffers)[N],
util::FileOutputStream* file_stream = nullptr)
util::FileOutputStream* file_stream = nullptr)
{
file_stream ? file_stream->CombineAndWrite<N>(buffers, GetThreadData()->GetScratchBuffer())
: file_stream_->CombineAndWrite<N>(buffers, GetThreadData()->GetScratchBuffer());
Expand All @@ -313,6 +313,8 @@ class CommonCaptureManager
uint32_t n_blocks,
int64_t offset);

uint32_t CurrentFrame() const;

protected:
std::unique_ptr<util::Compressor> compressor_;
std::mutex mapped_memory_lock_;
Expand Down Expand Up @@ -369,7 +371,7 @@ class CommonCaptureManager
uint32_t trim_key_frames_;
uint32_t trim_key_first_frame_;
size_t trim_current_range_;
uint32_t current_frame_;
uint32_t global_frame_counter_;
uint32_t queue_submit_count_;
CaptureMode capture_mode_;
bool previous_hotkey_state_;
Expand All @@ -391,6 +393,10 @@ class CommonCaptureManager
bool previous_write_assets_;
bool write_state_files_;

std::vector<uint32_t> instance_frame_counters;
bool count_frames_per_instance_;
uint32_t trim_instance_index_;

struct
{
bool rv_annotation{ false };
Expand Down
10 changes: 10 additions & 0 deletions framework/encode/capture_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ GFXRECON_BEGIN_NAMESPACE(encode)
#define RV_ANNOTATION_DESCRIPTOR_UPPER "RV_ANNOTATION_DESCRIPTOR"
#define FORCE_FIFO_PRESENT_MODE_LOWER "force_fifo_present_mode"
#define FORCE_FIFO_PRESENT_MODE_UPPER "FORCE_FIFO_PRESENT_MODE"
#define TRIM_INSTANCE_INDEX_UPPER "TRIM_INSTANCE_INDEX"
#define TRIM_INSTANCE_INDEX_LOWER "trim_instance_index"

#if defined(__ANDROID__)
// Android Properties
Expand Down Expand Up @@ -200,6 +202,7 @@ const char kAnnotationRandEnvVar[] = GFXRECON_ENV_VAR_
const char kAnnotationGPUVAEnvVar[] = GFXRECON_ENV_VAR_PREFIX RV_ANNOTATION_GPUVA_LOWER;
const char kAnnotationDescriptorEnvVar[] = GFXRECON_ENV_VAR_PREFIX RV_ANNOTATION_DESCRIPTOR_LOWER;
const char kForceFifoPresentModeEnvVar[] = GFXRECON_ENV_VAR_PREFIX FORCE_FIFO_PRESENT_MODE_LOWER;
const char kTrimInstanceIndexEnvVar[] = GFXRECON_ENV_VAR_PREFIX TRIM_INSTANCE_INDEX_LOWER;

#else
// Desktop environment settings
Expand Down Expand Up @@ -255,6 +258,7 @@ const char kAnnotationRandEnvVar[] = GFXRECON_ENV_VAR_
const char kAnnotationGPUVAEnvVar[] = GFXRECON_ENV_VAR_PREFIX RV_ANNOTATION_GPUVA_UPPER;
const char kAnnotationDescriptorEnvVar[] = GFXRECON_ENV_VAR_PREFIX RV_ANNOTATION_DESCRIPTOR_UPPER;
const char kForceFifoPresentModeEnvVar[] = GFXRECON_ENV_VAR_PREFIX FORCE_FIFO_PRESENT_MODE_UPPER;
const char kTrimInstanceIndexEnvVar[] = GFXRECON_ENV_VAR_PREFIX TRIM_INSTANCE_INDEX_UPPER;

#endif

Expand Down Expand Up @@ -309,6 +313,7 @@ const std::string kOptionKeyAnnotationRand = std::stri
const std::string kOptionKeyAnnotationGPUVA = std::string(kSettingsFilter) + std::string(RV_ANNOTATION_GPUVA_LOWER);
const std::string kOptionKeyAnnotationDescriptor = std::string(kSettingsFilter) + std::string(RV_ANNOTATION_DESCRIPTOR_LOWER);
const std::string kOptionForceFifoPresentModeEnvVar = std::string(kSettingsFilter) + std::string(FORCE_FIFO_PRESENT_MODE_LOWER);
const std::string kOptionTrimInstanceIndexEnvVar = std::string(kSettingsFilter) + std::string(TRIM_INSTANCE_INDEX_LOWER);

#if defined(GFXRECON_ENABLE_LZ4_COMPRESSION)
const format::CompressionType kDefaultCompressionType = format::CompressionType::kLz4;
Expand Down Expand Up @@ -478,6 +483,8 @@ void CaptureSettings::LoadOptionsEnvVar(OptionsMap* options)
LoadSingleOptionEnvVar(options, kAnnotationGPUVAEnvVar, kOptionKeyAnnotationGPUVA);
LoadSingleOptionEnvVar(options, kAnnotationDescriptorEnvVar, kOptionKeyAnnotationDescriptor);
LoadSingleOptionEnvVar(options, kForceFifoPresentModeEnvVar, kOptionForceFifoPresentModeEnvVar);

LoadSingleOptionEnvVar(options, kTrimInstanceIndexEnvVar, kOptionTrimInstanceIndexEnvVar);
}

void CaptureSettings::LoadOptionsFile(OptionsMap* options)
Expand Down Expand Up @@ -691,6 +698,9 @@ void CaptureSettings::ProcessOptions(OptionsMap* options, CaptureSettings* setti
settings->trace_settings_.rv_anotation_info.descriptor_mask);
settings->trace_settings_.force_fifo_present_mode = ParseBoolString(
FindOption(options, kOptionForceFifoPresentModeEnvVar), settings->trace_settings_.force_fifo_present_mode);

settings->trace_settings_.trim_instance_index = ParseIntegerString(
FindOption(options, kOptionTrimInstanceIndexEnvVar), settings->trace_settings_.trim_instance_index);
}

void CaptureSettings::ProcessLogOptions(OptionsMap* options, CaptureSettings* settings)
Expand Down
2 changes: 2 additions & 0 deletions framework/encode/capture_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "util/page_guard_manager.h"
#include "util/options.h"

#include <limits>
#include <string>
#include <unordered_map>
#include <vector>
Expand Down Expand Up @@ -132,6 +133,7 @@ class CaptureSettings
bool quit_after_frame_ranges{ false };
bool force_fifo_present_mode{ true };
bool use_asset_file{ false };
uint32_t trim_instance_index{ std::numeric_limits<uint32_t>::max() };

// An optimization for the page_guard memory tracking mode that eliminates the need for shadow memory by
// overriding vkAllocateMemory so that all host visible allocations use the external memory extension with a
Expand Down
10 changes: 10 additions & 0 deletions framework/encode/vulkan_capture_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,16 @@ VkResult VulkanCaptureManager::OverrideCreateInstance(const VkInstanceCreateInfo
{
VkResult result = VK_ERROR_INITIALIZATION_FAILED;

GFXRECON_WRITE_CONSOLE("%s()", __func__)
if (pCreateInfo->pApplicationInfo)
{
if (pCreateInfo->pApplicationInfo->pApplicationName)
GFXRECON_WRITE_CONSOLE(" app: %s", pCreateInfo->pApplicationInfo->pApplicationName)

if (pCreateInfo->pApplicationInfo->pEngineName)
GFXRECON_WRITE_CONSOLE(" engine: %s", pCreateInfo->pApplicationInfo->pEngineName)
}

if (CreateInstance())
{
if (singleton_->IsPageGuardMemoryModeExternal())
Expand Down

0 comments on commit 66cfb10

Please sign in to comment.