2014-05-17 21:34:55 +01:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/log.h"
|
|
|
|
|
|
|
|
#include "core/hle/service/gsp.h"
|
2014-05-18 16:28:30 +01:00
|
|
|
#include "pica.h"
|
2014-05-17 21:34:55 +01:00
|
|
|
|
|
|
|
class GraphicsDebugger
|
|
|
|
{
|
|
|
|
public:
|
2014-05-18 16:28:30 +01:00
|
|
|
// A few utility structs used to expose data
|
|
|
|
// A vector of commands represented by their raw byte sequence
|
|
|
|
struct PicaCommand : public std::vector<u32>
|
|
|
|
{
|
2014-06-11 20:22:26 +01:00
|
|
|
const Pica::CommandHeader& GetHeader() const
|
2014-05-18 16:28:30 +01:00
|
|
|
{
|
2014-06-11 20:22:26 +01:00
|
|
|
const u32& val = at(1);
|
2014-05-18 18:59:36 +01:00
|
|
|
return *(Pica::CommandHeader*)&val;
|
2014-05-18 16:28:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<PicaCommand> PicaCommandList;
|
|
|
|
|
2014-05-17 21:34:55 +01:00
|
|
|
// Base class for all objects which need to be notified about GPU events
|
|
|
|
class DebuggerObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DebuggerObserver() : observed(nullptr) { }
|
|
|
|
|
|
|
|
virtual ~DebuggerObserver()
|
|
|
|
{
|
|
|
|
if (observed)
|
|
|
|
observed->UnregisterObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a GX command has been processed and is ready for being
|
|
|
|
* read via GraphicsDebugger::ReadGXCommandHistory.
|
|
|
|
* @param total_command_count Total number of commands in the GX history
|
|
|
|
* @note All methods in this class are called from the GSP thread
|
|
|
|
*/
|
|
|
|
virtual void GXCommandProcessed(int total_command_count)
|
|
|
|
{
|
2014-07-23 04:26:28 +01:00
|
|
|
const GSP_GPU::Command& cmd = observed->ReadGXCommandHistory(total_command_count-1);
|
2014-07-22 11:41:16 +01:00
|
|
|
ERROR_LOG(GSP, "Received command: id=%x", (int)cmd.id.Value());
|
2014-05-17 21:34:55 +01:00
|
|
|
}
|
|
|
|
|
2014-05-18 16:28:30 +01:00
|
|
|
/**
|
|
|
|
* @param lst command list which triggered this call
|
|
|
|
* @param is_new true if the command list was called for the first time
|
|
|
|
* @todo figure out how to make sure called functions don't keep references around beyond their life time
|
|
|
|
*/
|
2014-05-18 16:52:22 +01:00
|
|
|
virtual void OnCommandListCalled(const PicaCommandList& lst, bool is_new)
|
2014-05-18 16:28:30 +01:00
|
|
|
{
|
|
|
|
ERROR_LOG(GSP, "Command list called: %d", (int)is_new);
|
|
|
|
}
|
|
|
|
|
2014-05-17 21:34:55 +01:00
|
|
|
protected:
|
2014-06-11 20:22:26 +01:00
|
|
|
const GraphicsDebugger* GetDebugger() const
|
2014-05-17 21:34:55 +01:00
|
|
|
{
|
|
|
|
return observed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GraphicsDebugger* observed;
|
|
|
|
bool in_destruction;
|
|
|
|
|
|
|
|
friend class GraphicsDebugger;
|
|
|
|
};
|
|
|
|
|
|
|
|
void GXCommandProcessed(u8* command_data)
|
|
|
|
{
|
2014-06-24 20:27:18 +01:00
|
|
|
if (observers.empty())
|
|
|
|
return;
|
|
|
|
|
2014-07-23 04:26:28 +01:00
|
|
|
gx_command_history.push_back(GSP_GPU::Command());
|
|
|
|
GSP_GPU::Command& cmd = gx_command_history[gx_command_history.size()-1];
|
2014-05-17 21:34:55 +01:00
|
|
|
|
2014-07-23 04:26:28 +01:00
|
|
|
memcpy(&cmd, command_data, sizeof(GSP_GPU::Command));
|
2014-05-17 21:34:55 +01:00
|
|
|
|
|
|
|
ForEachObserver([this](DebuggerObserver* observer) {
|
|
|
|
observer->GXCommandProcessed(this->gx_command_history.size());
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2014-05-18 16:28:30 +01:00
|
|
|
void CommandListCalled(u32 address, u32* command_list, u32 size_in_words)
|
|
|
|
{
|
2014-06-24 20:27:18 +01:00
|
|
|
if (observers.empty())
|
|
|
|
return;
|
|
|
|
|
2014-05-18 16:28:30 +01:00
|
|
|
PicaCommandList cmdlist;
|
2014-05-18 18:59:36 +01:00
|
|
|
for (u32* parse_pointer = command_list; parse_pointer < command_list + size_in_words;)
|
|
|
|
{
|
|
|
|
const Pica::CommandHeader header = static_cast<Pica::CommandHeader>(parse_pointer[1]);
|
|
|
|
|
|
|
|
cmdlist.push_back(PicaCommand());
|
|
|
|
auto& cmd = cmdlist.back();
|
|
|
|
|
|
|
|
size_t size = 2 + header.extra_data_length;
|
2014-05-18 20:18:38 +01:00
|
|
|
size = (size + 1) / 2 * 2; // align to 8 bytes
|
2014-05-18 18:59:36 +01:00
|
|
|
cmd.reserve(size);
|
|
|
|
std::copy(parse_pointer, parse_pointer + size, std::back_inserter(cmd));
|
|
|
|
|
|
|
|
parse_pointer += size;
|
|
|
|
}
|
2014-05-18 16:28:30 +01:00
|
|
|
|
|
|
|
auto obj = std::pair<u32,PicaCommandList>(address, cmdlist);
|
|
|
|
auto it = std::find(command_lists.begin(), command_lists.end(), obj);
|
|
|
|
bool is_new = (it == command_lists.end());
|
|
|
|
if (is_new)
|
|
|
|
command_lists.push_back(obj);
|
|
|
|
|
|
|
|
ForEachObserver([&](DebuggerObserver* observer) {
|
2014-05-18 16:52:22 +01:00
|
|
|
observer->OnCommandListCalled(obj.second, is_new);
|
2014-05-18 16:28:30 +01:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2014-07-23 04:26:28 +01:00
|
|
|
const GSP_GPU::Command& ReadGXCommandHistory(int index) const
|
2014-05-17 21:34:55 +01:00
|
|
|
{
|
|
|
|
// TODO: Is this thread-safe?
|
|
|
|
return gx_command_history[index];
|
|
|
|
}
|
|
|
|
|
2014-05-18 16:28:30 +01:00
|
|
|
const std::vector<std::pair<u32,PicaCommandList>>& GetCommandLists() const
|
|
|
|
{
|
|
|
|
return command_lists;
|
|
|
|
}
|
|
|
|
|
2014-05-17 21:34:55 +01:00
|
|
|
void RegisterObserver(DebuggerObserver* observer)
|
|
|
|
{
|
|
|
|
// TODO: Check for duplicates
|
|
|
|
observers.push_back(observer);
|
|
|
|
observer->observed = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnregisterObserver(DebuggerObserver* observer)
|
|
|
|
{
|
|
|
|
std::remove(observers.begin(), observers.end(), observer);
|
|
|
|
observer->observed = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ForEachObserver(std::function<void (DebuggerObserver*)> func)
|
|
|
|
{
|
|
|
|
std::for_each(observers.begin(),observers.end(), func);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<DebuggerObserver*> observers;
|
|
|
|
|
2014-07-23 04:26:28 +01:00
|
|
|
std::vector<GSP_GPU::Command> gx_command_history;
|
2014-05-18 16:28:30 +01:00
|
|
|
|
|
|
|
// vector of pairs of command lists and their storage address
|
|
|
|
std::vector<std::pair<u32,PicaCommandList>> command_lists;
|
2014-05-17 21:34:55 +01:00
|
|
|
};
|