Skip to content

Commit

Permalink
GS:MTL: Debug message support
Browse files Browse the repository at this point in the history
  • Loading branch information
TellowKrinkle committed Feb 11, 2022
1 parent a613619 commit c297035
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pcsx2/GS/Renderers/Metal/GSDeviceMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ class GSDeviceMTL final : public GSDevice
MRCOwned<id<MTLCommandBuffer>> m_vertex_upload_cmdbuf;
MRCOwned<id<MTLBlitCommandEncoder>> m_vertex_upload_encoder;

struct DebugEntry
{
enum Op { Push, Insert, Pop } op;
MRCOwned<NSString*> str;
DebugEntry(Op op, MRCOwned<NSString*> str): op(op), str(std::move(str)) {}
};

std::vector<DebugEntry> m_debug_entries;
u32 m_debug_group_level = 0;

GSDeviceMTL();
~GSDeviceMTL() override;

Expand Down Expand Up @@ -358,6 +368,15 @@ class GSDeviceMTL final : public GSDevice
void RenderHW(GSHWDrawConfig& config) override;
void SendHWDraw(GSHWDrawConfig& config, id<MTLRenderCommandEncoder> enc, id<MTLBuffer> buffer, size_t off);

// MARK: Debug

void PushDebugGroup(const char* fmt, ...) override;
void PopDebugGroup() override;
void InsertDebugMessage(DebugMessageCategory category, const char* fmt, ...) override;
void ProcessDebugEntry(id<MTLCommandEncoder> enc, const DebugEntry& entry);
void FlushDebugEntries(id<MTLCommandEncoder> enc);
void EndDebugGroup(id<MTLCommandEncoder> enc);

// MARK: ImGui

void RenderImGui(ImDrawData* data);
Expand Down
83 changes: 83 additions & 0 deletions pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@
{
if (m_current_render.encoder)
{
EndDebugGroup(m_current_render.encoder);
[m_current_render.encoder endEncoding];
m_current_render.encoder = nil;
memset(&m_current_render, 0, offsetof(MainRenderEncoder, depth_sel));
Expand Down Expand Up @@ -992,6 +993,7 @@ static void setFnConstantI(MTLFunctionConstantValues* fc, unsigned int value, GS
else
BeginRenderPass(@"StretchRect", dT, action, nullptr, MTLLoadActionDontCare);

FlushDebugEntries(m_current_render.encoder);
MREClearScissor();
DepthStencilSelector dsel;
dsel.ztst = ZTST_ALWAYS;
Expand Down Expand Up @@ -1497,6 +1499,7 @@ void bitcpy(Dst& dst, const Src& src)

BeginRenderPass(@"RenderHW", rt, MTLLoadActionLoad, config.ds, MTLLoadActionLoad, stencil, MTLLoadActionLoad);
id<MTLRenderCommandEncoder> mtlenc = m_current_render.encoder;
FlushDebugEntries(mtlenc);
MRESetScissor(config.scissor);
MRESetTexture(config.tex, GSMTLTextureIndexTex);
MRESetTexture(config.pal, GSMTLTextureIndexPalette);
Expand Down Expand Up @@ -1620,6 +1623,86 @@ void bitcpy(Dst& dst, const Src& src)
}
}

void GSDeviceMTL::PushDebugGroup(const char* fmt, ...)
{
#if defined(_DEBUG)
va_list va;
va_start(va, fmt);
MRCOwned<NSString*> nsfmt = MRCTransfer([[NSString alloc] initWithUTF8String:fmt]);
m_debug_entries.emplace_back(DebugEntry::Push, MRCTransfer([[NSString alloc] initWithFormat:nsfmt arguments:va]));
va_end(va);
#endif
}

void GSDeviceMTL::PopDebugGroup()
{
#if defined(_DEBUG)
m_debug_entries.emplace_back(DebugEntry::Pop, nullptr);
#endif
}

void GSDeviceMTL::InsertDebugMessage(DebugMessageCategory category, const char* fmt, ...)
{
#if defined(_DEBUG)
va_list va;
va_start(va, fmt);
MRCOwned<NSString*> nsfmt = MRCTransfer([[NSString alloc] initWithUTF8String:fmt]);
m_debug_entries.emplace_back(DebugEntry::Insert, MRCTransfer([[NSString alloc] initWithFormat:nsfmt arguments:va]));
va_end(va);
#endif
}

void GSDeviceMTL::ProcessDebugEntry(id<MTLCommandEncoder> enc, const DebugEntry& entry)
{
switch (entry.op)
{
case DebugEntry::Push:
[enc pushDebugGroup:entry.str];
m_debug_group_level++;
break;
case DebugEntry::Pop:
[enc popDebugGroup];
if (m_debug_group_level > 0)
m_debug_group_level--;
break;
case DebugEntry::Insert:
[enc insertDebugSignpost:entry.str];
break;
}
}

void GSDeviceMTL::FlushDebugEntries(id<MTLCommandEncoder> enc)
{
#if defined(_DEBUG)
if (!m_debug_entries.empty())
{
for (const DebugEntry& entry : m_debug_entries)
{
ProcessDebugEntry(enc, entry);
}
m_debug_entries.clear();
}
#endif
}

void GSDeviceMTL::EndDebugGroup(id<MTLCommandEncoder> enc)
{
#if defined(_DEBUG)
if (!m_debug_entries.empty() && m_debug_group_level)
{
auto begin = m_debug_entries.begin();
auto cur = begin;
auto end = m_debug_entries.end();
while (cur != end && m_debug_group_level)
{
ProcessDebugEntry(enc, *cur);
cur++;
}
m_debug_entries.erase(begin, cur);
}
#endif
}

static simd::float2 ToSimd(const ImVec2& vec)
{
return simd::make_float2(vec.x, vec.y);
Expand Down

0 comments on commit c297035

Please sign in to comment.