Skip to content

Commit

Permalink
Add GPU timestamps for RGB-YUV encode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themaister committed Nov 18, 2023
1 parent 4f609a5 commit cc338ed
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions threading/thread_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ void ThreadGroup::start(unsigned num_threads_foreground,
fg.thread_group.resize(num_threads_foreground);
bg.thread_group.resize(num_threads_background);

#ifndef GRANITE_SHIPPING
if (const char *env = getenv("GRANITE_TIMELINE_TRACE"))
{
LOGI("Enabling JSON timeline tracing to %s.\n", env);
timeline_trace_file = std::make_unique<Util::TimelineTraceFile>(env);
}
#endif

refresh_global_timeline_trace_file();
set_main_thread_name();
Expand Down
24 changes: 22 additions & 2 deletions util/timeline_trace_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,36 @@ TimelineTraceFile::~TimelineTraceFile()
thr.join();
}

TimelineTraceFile::ScopedEvent::ScopedEvent(TimelineTraceFile *file_, const char *tag)
TimelineTraceFile::ScopedEvent::ScopedEvent(TimelineTraceFile *file_, const char *tag, uint32_t pid)
: file(file_)
{
if (file && tag && *tag != '\0')
event = file->begin_event(tag);
event = file->begin_event(tag, pid);
}

TimelineTraceFile::ScopedEvent::~ScopedEvent()
{
if (event)
file->end_event(event);
}

TimelineTraceFile::ScopedEvent &
TimelineTraceFile::ScopedEvent::operator=(TimelineTraceFile::ScopedEvent &&other) noexcept
{
if (this != &other)
{
if (event)
file->end_event(event);
event = other.event;
file = other.file;
other.event = nullptr;
other.file = nullptr;
}
return *this;
}

TimelineTraceFile::ScopedEvent::ScopedEvent(TimelineTraceFile::ScopedEvent &&other) noexcept
{
*this = std::move(other);
}
}
5 changes: 4 additions & 1 deletion util/timeline_trace_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ class TimelineTraceFile

struct ScopedEvent
{
ScopedEvent(TimelineTraceFile *file, const char *tag);
ScopedEvent(TimelineTraceFile *file, const char *tag, uint32_t pid = 0);
ScopedEvent() = default;
~ScopedEvent();
void operator=(const ScopedEvent &) = delete;
ScopedEvent(const ScopedEvent &) = delete;
ScopedEvent(ScopedEvent &&other) noexcept;
ScopedEvent &operator=(ScopedEvent &&other) noexcept;
TimelineTraceFile *file = nullptr;
Event *event = nullptr;
};
Expand Down
8 changes: 8 additions & 0 deletions video/ffmpeg_encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,10 @@ void VideoEncoder::process_rgb(Vulkan::CommandBuffer &cmd, YCbCrPipeline &pipeli
push.base_v = pipeline.constants.base_uv_luma[1];
push.dither_strength = pipeline.constants.dither_strength;
cmd.push_constants(&push, 0, sizeof(push));

auto start_yuv_ts = cmd.write_timestamp(VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
cmd.dispatch(pipeline.constants.luma_dispatch[0], pipeline.constants.luma_dispatch[1], 1);
auto end_yuv_ts = cmd.write_timestamp(VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);

if (pipeline.luma)
{
Expand All @@ -1516,7 +1519,9 @@ void VideoEncoder::process_rgb(Vulkan::CommandBuffer &cmd, YCbCrPipeline &pipeli
push.base_u = pipeline.constants.base_uv_chroma[0];
push.base_v = pipeline.constants.base_uv_chroma[1];
cmd.push_constants(&push, 0, sizeof(push));
auto start_chroma_ts = cmd.write_timestamp(VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
cmd.dispatch(pipeline.constants.chroma_dispatch[0], pipeline.constants.chroma_dispatch[1], 1);
auto end_chroma_ts = cmd.write_timestamp(VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);

if (pipeline.chroma)
{
Expand All @@ -1537,6 +1542,9 @@ void VideoEncoder::process_rgb(Vulkan::CommandBuffer &cmd, YCbCrPipeline &pipeli
cmd.barrier(VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_HOST_READ_BIT);
}

cmd.get_device().register_time_interval("GPU", std::move(start_yuv_ts), std::move(end_yuv_ts), "rgb-to-yuv");
cmd.get_device().register_time_interval("GPU", std::move(start_chroma_ts), std::move(end_chroma_ts), "chroma-downsample");
}

bool VideoEncoder::encode_frame(YCbCrPipeline &pipeline_ptr, int64_t pts, int compensate_audio_us)
Expand Down

0 comments on commit cc338ed

Please sign in to comment.