2015-04-04 11:57:31 +01:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "common/logging/log.h"
|
2017-05-28 00:31:42 +01:00
|
|
|
#include "core/tracer/recorder.h"
|
2015-04-04 11:57:31 +01:00
|
|
|
|
|
|
|
namespace CiTrace {
|
|
|
|
|
2016-09-19 02:01:46 +01:00
|
|
|
Recorder::Recorder(const InitialState& initial_state) : initial_state(initial_state) {}
|
2015-04-04 11:57:31 +01:00
|
|
|
|
|
|
|
void Recorder::Finish(const std::string& filename) {
|
|
|
|
// Setup CiTrace header
|
|
|
|
CTHeader header;
|
|
|
|
std::memcpy(header.magic, CTHeader::ExpectedMagicWord(), 4);
|
|
|
|
header.version = CTHeader::ExpectedVersion();
|
|
|
|
header.header_size = sizeof(CTHeader);
|
|
|
|
|
|
|
|
// Calculate file offsets
|
|
|
|
auto& initial = header.initial_state_offsets;
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
initial.gpu_registers_size = static_cast<u32>(initial_state.gpu_registers.size());
|
|
|
|
initial.lcd_registers_size = static_cast<u32>(initial_state.lcd_registers.size());
|
|
|
|
initial.pica_registers_size = static_cast<u32>(initial_state.pica_registers.size());
|
2016-04-25 21:10:03 +01:00
|
|
|
initial.default_attributes_size = static_cast<u32>(initial_state.default_attributes.size());
|
2016-09-18 01:38:01 +01:00
|
|
|
initial.vs_program_binary_size = static_cast<u32>(initial_state.vs_program_binary.size());
|
|
|
|
initial.vs_swizzle_data_size = static_cast<u32>(initial_state.vs_swizzle_data.size());
|
|
|
|
initial.vs_float_uniforms_size = static_cast<u32>(initial_state.vs_float_uniforms.size());
|
|
|
|
initial.gs_program_binary_size = static_cast<u32>(initial_state.gs_program_binary.size());
|
|
|
|
initial.gs_swizzle_data_size = static_cast<u32>(initial_state.gs_swizzle_data.size());
|
|
|
|
initial.gs_float_uniforms_size = static_cast<u32>(initial_state.gs_float_uniforms.size());
|
|
|
|
header.stream_size = static_cast<u32>(stream.size());
|
|
|
|
|
|
|
|
initial.gpu_registers = sizeof(header);
|
|
|
|
initial.lcd_registers = initial.gpu_registers + initial.gpu_registers_size * sizeof(u32);
|
|
|
|
initial.pica_registers = initial.lcd_registers + initial.lcd_registers_size * sizeof(u32);
|
|
|
|
;
|
|
|
|
initial.default_attributes = initial.pica_registers + initial.pica_registers_size * sizeof(u32);
|
|
|
|
initial.vs_program_binary =
|
|
|
|
initial.default_attributes + initial.default_attributes_size * sizeof(u32);
|
|
|
|
initial.vs_swizzle_data =
|
|
|
|
initial.vs_program_binary + initial.vs_program_binary_size * sizeof(u32);
|
|
|
|
initial.vs_float_uniforms =
|
|
|
|
initial.vs_swizzle_data + initial.vs_swizzle_data_size * sizeof(u32);
|
|
|
|
initial.gs_program_binary =
|
|
|
|
initial.vs_float_uniforms + initial.vs_float_uniforms_size * sizeof(u32);
|
|
|
|
initial.gs_swizzle_data =
|
|
|
|
initial.gs_program_binary + initial.gs_program_binary_size * sizeof(u32);
|
|
|
|
initial.gs_float_uniforms =
|
|
|
|
initial.gs_swizzle_data + initial.gs_swizzle_data_size * sizeof(u32);
|
|
|
|
header.stream_offset = initial.gs_float_uniforms + initial.gs_float_uniforms_size * sizeof(u32);
|
2015-04-04 11:57:31 +01:00
|
|
|
|
|
|
|
// Iterate through stream elements, update relevant stream element data
|
|
|
|
for (auto& stream_element : stream) {
|
|
|
|
switch (stream_element.data.type) {
|
2016-09-18 01:38:01 +01:00
|
|
|
case MemoryLoad: {
|
2015-04-04 11:57:31 +01:00
|
|
|
auto& file_offset = memory_regions[stream_element.hash];
|
|
|
|
if (!stream_element.uses_existing_data) {
|
|
|
|
file_offset = header.stream_offset;
|
|
|
|
}
|
|
|
|
stream_element.data.memory_load.file_offset = file_offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Other commands don't use any extra data
|
|
|
|
DEBUG_ASSERT(stream_element.extra_data.size() == 0);
|
|
|
|
break;
|
|
|
|
}
|
2016-04-25 21:10:03 +01:00
|
|
|
header.stream_offset += static_cast<u32>(stream_element.extra_data.size());
|
2015-04-04 11:57:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Open file and write header
|
|
|
|
FileUtil::IOFile file(filename, "wb");
|
2018-09-06 21:03:28 +01:00
|
|
|
std::size_t written = file.WriteObject(header);
|
2015-04-04 11:57:31 +01:00
|
|
|
if (written != 1 || file.Tell() != initial.gpu_registers)
|
|
|
|
throw "Failed to write header";
|
|
|
|
|
|
|
|
// Write initial state
|
2016-09-18 01:38:01 +01:00
|
|
|
written =
|
|
|
|
file.WriteArray(initial_state.gpu_registers.data(), initial_state.gpu_registers.size());
|
2015-07-12 02:20:19 +01:00
|
|
|
if (written != initial_state.gpu_registers.size() || file.Tell() != initial.lcd_registers)
|
2015-04-04 11:57:31 +01:00
|
|
|
throw "Failed to write GPU registers";
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written =
|
|
|
|
file.WriteArray(initial_state.lcd_registers.data(), initial_state.lcd_registers.size());
|
2015-07-12 02:20:19 +01:00
|
|
|
if (written != initial_state.lcd_registers.size() || file.Tell() != initial.pica_registers)
|
2015-04-04 11:57:31 +01:00
|
|
|
throw "Failed to write LCD registers";
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written = file.WriteArray(initial_state.pica_registers.data(),
|
|
|
|
initial_state.pica_registers.size());
|
|
|
|
if (written != initial_state.pica_registers.size() ||
|
|
|
|
file.Tell() != initial.default_attributes)
|
2015-04-04 11:57:31 +01:00
|
|
|
throw "Failed to write Pica registers";
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written = file.WriteArray(initial_state.default_attributes.data(),
|
|
|
|
initial_state.default_attributes.size());
|
|
|
|
if (written != initial_state.default_attributes.size() ||
|
|
|
|
file.Tell() != initial.vs_program_binary)
|
2015-05-21 01:51:28 +01:00
|
|
|
throw "Failed to write default vertex attributes";
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written = file.WriteArray(initial_state.vs_program_binary.data(),
|
|
|
|
initial_state.vs_program_binary.size());
|
|
|
|
if (written != initial_state.vs_program_binary.size() ||
|
|
|
|
file.Tell() != initial.vs_swizzle_data)
|
2015-04-04 11:57:31 +01:00
|
|
|
throw "Failed to write vertex shader program binary";
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written = file.WriteArray(initial_state.vs_swizzle_data.data(),
|
|
|
|
initial_state.vs_swizzle_data.size());
|
|
|
|
if (written != initial_state.vs_swizzle_data.size() ||
|
|
|
|
file.Tell() != initial.vs_float_uniforms)
|
2015-04-04 11:57:31 +01:00
|
|
|
throw "Failed to write vertex shader swizzle data";
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written = file.WriteArray(initial_state.vs_float_uniforms.data(),
|
|
|
|
initial_state.vs_float_uniforms.size());
|
|
|
|
if (written != initial_state.vs_float_uniforms.size() ||
|
|
|
|
file.Tell() != initial.gs_program_binary)
|
2015-04-04 11:57:31 +01:00
|
|
|
throw "Failed to write vertex shader float uniforms";
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written = file.WriteArray(initial_state.gs_program_binary.data(),
|
|
|
|
initial_state.gs_program_binary.size());
|
|
|
|
if (written != initial_state.gs_program_binary.size() ||
|
|
|
|
file.Tell() != initial.gs_swizzle_data)
|
2015-04-04 11:57:31 +01:00
|
|
|
throw "Failed to write geomtry shader program binary";
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written = file.WriteArray(initial_state.gs_swizzle_data.data(),
|
|
|
|
initial_state.gs_swizzle_data.size());
|
|
|
|
if (written != initial_state.gs_swizzle_data.size() ||
|
|
|
|
file.Tell() != initial.gs_float_uniforms)
|
2015-04-04 11:57:31 +01:00
|
|
|
throw "Failed to write geometry shader swizzle data";
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written = file.WriteArray(initial_state.gs_float_uniforms.data(),
|
|
|
|
initial_state.gs_float_uniforms.size());
|
|
|
|
if (written != initial_state.gs_float_uniforms.size() ||
|
|
|
|
file.Tell() != initial.gs_float_uniforms + sizeof(u32) * initial.gs_float_uniforms_size)
|
2015-04-04 11:57:31 +01:00
|
|
|
throw "Failed to write geometry shader float uniforms";
|
|
|
|
|
|
|
|
// Iterate through stream elements, write "extra data"
|
|
|
|
for (const auto& stream_element : stream) {
|
|
|
|
if (stream_element.extra_data.size() == 0)
|
|
|
|
continue;
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
written =
|
|
|
|
file.WriteBytes(stream_element.extra_data.data(), stream_element.extra_data.size());
|
2015-04-04 11:57:31 +01:00
|
|
|
if (written != stream_element.extra_data.size())
|
|
|
|
throw "Failed to write extra data";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.Tell() != header.stream_offset)
|
|
|
|
throw "Unexpected end of extra data";
|
|
|
|
|
|
|
|
// Write actual stream elements
|
|
|
|
for (const auto& stream_element : stream) {
|
|
|
|
if (1 != file.WriteObject(stream_element.data))
|
|
|
|
throw "Failed to write stream element";
|
|
|
|
}
|
2016-09-18 01:38:01 +01:00
|
|
|
} catch (const char* str) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_ERROR(HW_GPU, "Writing CiTrace file failed: {}", str);
|
2015-04-04 11:57:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Recorder::FrameFinished() {
|
2016-09-18 01:38:01 +01:00
|
|
|
stream.push_back({{FrameMarker}});
|
2015-04-04 11:57:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Recorder::MemoryAccessed(const u8* data, u32 size, u32 physical_address) {
|
2016-09-18 01:38:01 +01:00
|
|
|
StreamElement element = {{MemoryLoad}};
|
2015-04-04 11:57:31 +01:00
|
|
|
element.data.memory_load.size = size;
|
|
|
|
element.data.memory_load.physical_address = physical_address;
|
|
|
|
|
|
|
|
// Compute hash over given memory region to check if the contents are already stored internally
|
|
|
|
boost::crc_32_type result;
|
|
|
|
result.process_bytes(data, size);
|
|
|
|
element.hash = result.checksum();
|
|
|
|
|
|
|
|
element.uses_existing_data = (memory_regions.find(element.hash) != memory_regions.end());
|
|
|
|
if (!element.uses_existing_data) {
|
|
|
|
element.extra_data.resize(size);
|
|
|
|
memcpy(element.extra_data.data(), data, size);
|
|
|
|
memory_regions.insert({element.hash, 0}); // file offset will be initialized in Finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.push_back(element);
|
|
|
|
}
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
template <typename T>
|
2015-04-04 11:57:31 +01:00
|
|
|
void Recorder::RegisterWritten(u32 physical_address, T value) {
|
2016-09-18 01:38:01 +01:00
|
|
|
StreamElement element = {{RegisterWrite}};
|
|
|
|
element.data.register_write.size =
|
|
|
|
(sizeof(T) == 1) ? CTRegisterWrite::SIZE_8
|
|
|
|
: (sizeof(T) == 2) ? CTRegisterWrite::SIZE_16
|
|
|
|
: (sizeof(T) == 4) ? CTRegisterWrite::SIZE_32
|
|
|
|
: CTRegisterWrite::SIZE_64;
|
2015-04-04 11:57:31 +01:00
|
|
|
element.data.register_write.physical_address = physical_address;
|
|
|
|
element.data.register_write.value = value;
|
|
|
|
|
|
|
|
stream.push_back(element);
|
|
|
|
}
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
template void Recorder::RegisterWritten(u32, u8);
|
|
|
|
template void Recorder::RegisterWritten(u32, u16);
|
|
|
|
template void Recorder::RegisterWritten(u32, u32);
|
|
|
|
template void Recorder::RegisterWritten(u32, u64);
|
2018-03-09 17:54:43 +00:00
|
|
|
} // namespace CiTrace
|