2014-08-17 14:06:58 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-08-14 14:30:38 +02:00
|
|
|
#include <algorithm>
|
2014-10-25 18:02:26 +02:00
|
|
|
#include <condition_variable>
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <cstdint>
|
2015-08-16 14:43:23 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
2014-08-16 14:06:40 +02:00
|
|
|
#include <map>
|
2014-08-14 19:21:55 +02:00
|
|
|
#include <mutex>
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <stdexcept>
|
2014-08-17 14:06:58 +02:00
|
|
|
#include <string>
|
|
|
|
|
2014-08-16 14:06:40 +02:00
|
|
|
#ifdef HAVE_PNG
|
|
|
|
#include <png.h>
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <setjmp.h>
|
2014-08-16 14:06:40 +02:00
|
|
|
#endif
|
|
|
|
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <nihstro/bit_field.h>
|
2015-05-28 00:31:26 +02:00
|
|
|
#include <nihstro/float24.h>
|
2014-12-16 01:12:29 +01:00
|
|
|
#include <nihstro/shader_binary.h>
|
2015-01-21 02:16:47 +01:00
|
|
|
#include "common/assert.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "common/bit_field.h"
|
2015-05-24 21:20:31 +02:00
|
|
|
#include "common/color.h"
|
2015-08-11 23:32:39 +02:00
|
|
|
#include "common/common_types.h"
|
2014-08-16 14:06:40 +02:00
|
|
|
#include "common/file_util.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "common/logging/log.h"
|
2014-12-31 14:48:19 +01:00
|
|
|
#include "common/math_util.h"
|
2015-05-24 21:16:22 +02:00
|
|
|
#include "common/vector_math.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/debug_utils/debug_utils.h"
|
2014-08-17 14:06:58 +02:00
|
|
|
#include "video_core/pica.h"
|
2016-03-03 04:16:38 +01:00
|
|
|
#include "video_core/pica_state.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/pica_types.h"
|
|
|
|
#include "video_core/rasterizer_interface.h"
|
2015-06-21 15:58:59 +02:00
|
|
|
#include "video_core/renderer_base.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/shader/shader.h"
|
2017-01-05 23:11:23 +01:00
|
|
|
#include "video_core/texture/texture_decode.h"
|
2015-02-24 00:24:35 +01:00
|
|
|
#include "video_core/utils.h"
|
2015-05-19 06:21:33 +02:00
|
|
|
#include "video_core/video_core.h"
|
2014-08-17 14:06:58 +02:00
|
|
|
|
2014-12-16 01:12:29 +01:00
|
|
|
using nihstro::DVLBHeader;
|
|
|
|
using nihstro::DVLEHeader;
|
|
|
|
using nihstro::DVLPHeader;
|
|
|
|
|
2014-08-17 14:06:58 +02:00
|
|
|
namespace Pica {
|
|
|
|
|
2016-04-24 14:19:49 +02:00
|
|
|
void DebugContext::DoOnEvent(Event event, void* data) {
|
2014-10-25 18:02:26 +02:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(breakpoint_mutex);
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
// Commit the rasterizer's caches so framebuffers, render targets, etc. will show on debug
|
|
|
|
// widgets
|
2016-04-17 00:57:57 +02:00
|
|
|
VideoCore::g_renderer->Rasterizer()->FlushAll();
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2014-10-25 18:02:26 +02:00
|
|
|
// TODO: Should stop the CPU thread here once we multithread emulation.
|
|
|
|
|
|
|
|
active_breakpoint = event;
|
|
|
|
at_breakpoint = true;
|
|
|
|
|
|
|
|
// Tell all observers that we hit a breakpoint
|
|
|
|
for (auto& breakpoint_observer : breakpoint_observers) {
|
|
|
|
breakpoint_observer->OnPicaBreakPointHit(event, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until another thread tells us to Resume()
|
2016-09-18 02:38:01 +02:00
|
|
|
resume_from_breakpoint.wait(lock, [&] { return !at_breakpoint; });
|
2014-10-25 18:02:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DebugContext::Resume() {
|
|
|
|
{
|
2015-07-26 15:56:45 +02:00
|
|
|
std::lock_guard<std::mutex> lock(breakpoint_mutex);
|
2014-10-25 18:02:26 +02:00
|
|
|
|
|
|
|
// Tell all observers that we are about to resume
|
|
|
|
for (auto& breakpoint_observer : breakpoint_observers) {
|
|
|
|
breakpoint_observer->OnPicaResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resume the waiting thread (i.e. OnEvent())
|
|
|
|
at_breakpoint = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
resume_from_breakpoint.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<DebugContext> g_debug_context; // TODO: Get rid of this global
|
|
|
|
|
2014-08-17 14:06:58 +02:00
|
|
|
namespace DebugUtils {
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
void DumpShader(const std::string& filename, const Regs::ShaderConfig& config,
|
|
|
|
const Shader::ShaderSetup& setup,
|
2017-01-28 05:16:36 +01:00
|
|
|
const RasterizerRegs::VSOutputAttributes* output_attributes) {
|
2014-08-14 14:30:38 +02:00
|
|
|
struct StuffToWrite {
|
2016-03-17 06:51:09 +01:00
|
|
|
const u8* pointer;
|
2014-08-14 14:30:38 +02:00
|
|
|
u32 size;
|
|
|
|
};
|
|
|
|
std::vector<StuffToWrite> writing_queue;
|
|
|
|
u32 write_offset = 0;
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
auto QueueForWriting = [&writing_queue, &write_offset](const u8* pointer, u32 size) {
|
2014-08-14 14:30:38 +02:00
|
|
|
writing_queue.push_back({pointer, size});
|
|
|
|
u32 old_write_offset = write_offset;
|
|
|
|
write_offset += size;
|
|
|
|
return old_write_offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
// First off, try to translate Pica state (one enum for output attribute type and component)
|
|
|
|
// into shbin format (separate type and component mask).
|
|
|
|
union OutputRegisterInfo {
|
|
|
|
enum Type : u64 {
|
2016-09-18 02:38:01 +02:00
|
|
|
POSITION = 0,
|
2015-05-27 16:20:46 +02:00
|
|
|
QUATERNION = 1,
|
2016-09-18 02:38:01 +02:00
|
|
|
COLOR = 2,
|
|
|
|
TEXCOORD0 = 3,
|
|
|
|
TEXCOORD1 = 5,
|
|
|
|
TEXCOORD2 = 6,
|
2015-05-27 16:20:46 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
VIEW = 8,
|
2014-08-14 14:30:38 +02:00
|
|
|
};
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
BitField<0, 64, u64> hex;
|
2014-08-14 14:30:38 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
BitField<0, 16, Type> type;
|
2014-08-14 14:30:38 +02:00
|
|
|
BitField<16, 16, u64> id;
|
2016-09-18 02:38:01 +02:00
|
|
|
BitField<32, 4, u64> component_mask;
|
2014-08-14 14:30:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is put into a try-catch block to make sure we notice unknown configurations.
|
|
|
|
std::vector<OutputRegisterInfo> output_info_table;
|
2016-09-18 02:38:01 +02:00
|
|
|
for (unsigned i = 0; i < 7; ++i) {
|
2017-01-28 05:16:36 +01:00
|
|
|
using OutputAttributes = Pica::RasterizerRegs::VSOutputAttributes;
|
2016-09-18 02:38:01 +02:00
|
|
|
|
|
|
|
// TODO: It's still unclear how the attribute components map to the register!
|
|
|
|
// Once we know that, this code probably will not make much sense anymore.
|
|
|
|
std::map<OutputAttributes::Semantic, std::pair<OutputRegisterInfo::Type, u32>> map = {
|
|
|
|
{OutputAttributes::POSITION_X, {OutputRegisterInfo::POSITION, 1}},
|
|
|
|
{OutputAttributes::POSITION_Y, {OutputRegisterInfo::POSITION, 2}},
|
|
|
|
{OutputAttributes::POSITION_Z, {OutputRegisterInfo::POSITION, 4}},
|
|
|
|
{OutputAttributes::POSITION_W, {OutputRegisterInfo::POSITION, 8}},
|
|
|
|
{OutputAttributes::QUATERNION_X, {OutputRegisterInfo::QUATERNION, 1}},
|
|
|
|
{OutputAttributes::QUATERNION_Y, {OutputRegisterInfo::QUATERNION, 2}},
|
|
|
|
{OutputAttributes::QUATERNION_Z, {OutputRegisterInfo::QUATERNION, 4}},
|
|
|
|
{OutputAttributes::QUATERNION_W, {OutputRegisterInfo::QUATERNION, 8}},
|
|
|
|
{OutputAttributes::COLOR_R, {OutputRegisterInfo::COLOR, 1}},
|
|
|
|
{OutputAttributes::COLOR_G, {OutputRegisterInfo::COLOR, 2}},
|
|
|
|
{OutputAttributes::COLOR_B, {OutputRegisterInfo::COLOR, 4}},
|
|
|
|
{OutputAttributes::COLOR_A, {OutputRegisterInfo::COLOR, 8}},
|
|
|
|
{OutputAttributes::TEXCOORD0_U, {OutputRegisterInfo::TEXCOORD0, 1}},
|
|
|
|
{OutputAttributes::TEXCOORD0_V, {OutputRegisterInfo::TEXCOORD0, 2}},
|
|
|
|
{OutputAttributes::TEXCOORD1_U, {OutputRegisterInfo::TEXCOORD1, 1}},
|
|
|
|
{OutputAttributes::TEXCOORD1_V, {OutputRegisterInfo::TEXCOORD1, 2}},
|
|
|
|
{OutputAttributes::TEXCOORD2_U, {OutputRegisterInfo::TEXCOORD2, 1}},
|
|
|
|
{OutputAttributes::TEXCOORD2_V, {OutputRegisterInfo::TEXCOORD2, 2}},
|
|
|
|
{OutputAttributes::VIEW_X, {OutputRegisterInfo::VIEW, 1}},
|
|
|
|
{OutputAttributes::VIEW_Y, {OutputRegisterInfo::VIEW, 2}},
|
2016-09-19 03:01:46 +02:00
|
|
|
{OutputAttributes::VIEW_Z, {OutputRegisterInfo::VIEW, 4}},
|
|
|
|
};
|
2016-09-18 02:38:01 +02:00
|
|
|
|
|
|
|
for (const auto& semantic : std::vector<OutputAttributes::Semantic>{
|
|
|
|
output_attributes[i].map_x, output_attributes[i].map_y, output_attributes[i].map_z,
|
|
|
|
output_attributes[i].map_w}) {
|
|
|
|
if (semantic == OutputAttributes::INVALID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
OutputRegisterInfo::Type type = map.at(semantic).first;
|
|
|
|
u32 component_mask = map.at(semantic).second;
|
|
|
|
|
|
|
|
auto it = std::find_if(output_info_table.begin(), output_info_table.end(),
|
|
|
|
[&i, &type](const OutputRegisterInfo& info) {
|
|
|
|
return info.id == i && info.type == type;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (it == output_info_table.end()) {
|
|
|
|
output_info_table.emplace_back();
|
|
|
|
output_info_table.back().type.Assign(type);
|
|
|
|
output_info_table.back().component_mask.Assign(component_mask);
|
|
|
|
output_info_table.back().id.Assign(i);
|
|
|
|
} else {
|
|
|
|
it->component_mask.Assign(it->component_mask | component_mask);
|
2014-08-14 14:30:38 +02:00
|
|
|
}
|
2016-09-18 02:38:01 +02:00
|
|
|
} catch (const std::out_of_range&) {
|
|
|
|
DEBUG_ASSERT_MSG(false, "Unknown output attribute mapping");
|
|
|
|
LOG_ERROR(HW_GPU, "Unknown output attribute mapping: %03x, %03x, %03x, %03x",
|
|
|
|
(int)output_attributes[i].map_x.Value(),
|
|
|
|
(int)output_attributes[i].map_y.Value(),
|
|
|
|
(int)output_attributes[i].map_z.Value(),
|
|
|
|
(int)output_attributes[i].map_w.Value());
|
2014-08-14 14:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2014-08-14 14:30:38 +02:00
|
|
|
|
|
|
|
struct {
|
|
|
|
DVLBHeader header;
|
|
|
|
u32 dvle_offset;
|
2016-09-18 02:38:01 +02:00
|
|
|
} dvlb{{DVLBHeader::MAGIC_WORD, 1}}; // 1 DVLE
|
2014-08-14 14:30:38 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
DVLPHeader dvlp{DVLPHeader::MAGIC_WORD};
|
|
|
|
DVLEHeader dvle{DVLEHeader::MAGIC_WORD};
|
2014-08-14 14:30:38 +02:00
|
|
|
|
2016-03-17 06:51:09 +01:00
|
|
|
QueueForWriting(reinterpret_cast<const u8*>(&dvlb), sizeof(dvlb));
|
|
|
|
u32 dvlp_offset = QueueForWriting(reinterpret_cast<const u8*>(&dvlp), sizeof(dvlp));
|
|
|
|
dvlb.dvle_offset = QueueForWriting(reinterpret_cast<const u8*>(&dvle), sizeof(dvle));
|
2014-08-14 14:30:38 +02:00
|
|
|
|
|
|
|
// TODO: Reduce the amount of binary code written to relevant portions
|
|
|
|
dvlp.binary_offset = write_offset - dvlp_offset;
|
2016-04-25 22:10:03 +02:00
|
|
|
dvlp.binary_size_words = static_cast<uint32_t>(setup.program_code.size());
|
|
|
|
QueueForWriting(reinterpret_cast<const u8*>(setup.program_code.data()),
|
|
|
|
static_cast<u32>(setup.program_code.size()) * sizeof(u32));
|
2014-08-14 14:30:38 +02:00
|
|
|
|
2014-12-16 01:12:29 +01:00
|
|
|
dvlp.swizzle_info_offset = write_offset - dvlp_offset;
|
2016-04-25 22:10:03 +02:00
|
|
|
dvlp.swizzle_info_num_entries = static_cast<uint32_t>(setup.swizzle_data.size());
|
2014-08-14 14:30:38 +02:00
|
|
|
u32 dummy = 0;
|
2015-05-28 00:31:26 +02:00
|
|
|
for (unsigned int i = 0; i < setup.swizzle_data.size(); ++i) {
|
2016-09-18 02:38:01 +02:00
|
|
|
QueueForWriting(reinterpret_cast<const u8*>(&setup.swizzle_data[i]),
|
|
|
|
sizeof(setup.swizzle_data[i]));
|
2016-03-17 06:51:09 +01:00
|
|
|
QueueForWriting(reinterpret_cast<const u8*>(&dummy), sizeof(dummy));
|
2014-08-14 14:30:38 +02:00
|
|
|
}
|
|
|
|
|
2015-05-28 00:31:26 +02:00
|
|
|
dvle.main_offset_words = config.main_offset;
|
2014-08-14 14:30:38 +02:00
|
|
|
dvle.output_register_table_offset = write_offset - dvlb.dvle_offset;
|
2015-08-11 23:32:39 +02:00
|
|
|
dvle.output_register_table_size = static_cast<u32>(output_info_table.size());
|
2016-09-18 02:38:01 +02:00
|
|
|
QueueForWriting(reinterpret_cast<const u8*>(output_info_table.data()),
|
|
|
|
static_cast<u32>(output_info_table.size() * sizeof(OutputRegisterInfo)));
|
2014-08-14 14:30:38 +02:00
|
|
|
|
|
|
|
// TODO: Create a label table for "main"
|
|
|
|
|
2015-05-28 00:31:26 +02:00
|
|
|
std::vector<nihstro::ConstantInfo> constant_table;
|
|
|
|
for (unsigned i = 0; i < setup.uniforms.b.size(); ++i) {
|
|
|
|
nihstro::ConstantInfo constant;
|
|
|
|
memset(&constant, 0, sizeof(constant));
|
|
|
|
constant.type = nihstro::ConstantInfo::Bool;
|
|
|
|
constant.regid = i;
|
|
|
|
constant.b = setup.uniforms.b[i];
|
|
|
|
constant_table.emplace_back(constant);
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < setup.uniforms.i.size(); ++i) {
|
|
|
|
nihstro::ConstantInfo constant;
|
|
|
|
memset(&constant, 0, sizeof(constant));
|
|
|
|
constant.type = nihstro::ConstantInfo::Int;
|
|
|
|
constant.regid = i;
|
|
|
|
constant.i.x = setup.uniforms.i[i].x;
|
|
|
|
constant.i.y = setup.uniforms.i[i].y;
|
|
|
|
constant.i.z = setup.uniforms.i[i].z;
|
|
|
|
constant.i.w = setup.uniforms.i[i].w;
|
|
|
|
constant_table.emplace_back(constant);
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < sizeof(setup.uniforms.f) / sizeof(setup.uniforms.f[0]); ++i) {
|
|
|
|
nihstro::ConstantInfo constant;
|
|
|
|
memset(&constant, 0, sizeof(constant));
|
|
|
|
constant.type = nihstro::ConstantInfo::Float;
|
|
|
|
constant.regid = i;
|
|
|
|
constant.f.x = nihstro::to_float24(setup.uniforms.f[i].x.ToFloat32());
|
|
|
|
constant.f.y = nihstro::to_float24(setup.uniforms.f[i].y.ToFloat32());
|
|
|
|
constant.f.z = nihstro::to_float24(setup.uniforms.f[i].z.ToFloat32());
|
|
|
|
constant.f.w = nihstro::to_float24(setup.uniforms.f[i].w.ToFloat32());
|
|
|
|
|
|
|
|
// Store constant if it's different from zero..
|
2016-09-18 02:38:01 +02:00
|
|
|
if (setup.uniforms.f[i].x.ToFloat32() != 0.0 || setup.uniforms.f[i].y.ToFloat32() != 0.0 ||
|
|
|
|
setup.uniforms.f[i].z.ToFloat32() != 0.0 || setup.uniforms.f[i].w.ToFloat32() != 0.0)
|
2015-05-28 00:31:26 +02:00
|
|
|
constant_table.emplace_back(constant);
|
|
|
|
}
|
|
|
|
dvle.constant_table_offset = write_offset - dvlb.dvle_offset;
|
2016-04-25 22:10:03 +02:00
|
|
|
dvle.constant_table_size = static_cast<uint32_t>(constant_table.size());
|
2015-05-28 00:31:26 +02:00
|
|
|
for (const auto& constant : constant_table) {
|
2016-03-17 06:51:09 +01:00
|
|
|
QueueForWriting(reinterpret_cast<const u8*>(&constant), sizeof(constant));
|
2015-05-28 00:31:26 +02:00
|
|
|
}
|
2014-08-14 14:30:38 +02:00
|
|
|
|
|
|
|
// Write data to file
|
|
|
|
std::ofstream file(filename, std::ios_base::out | std::ios_base::binary);
|
|
|
|
|
2016-03-17 06:51:09 +01:00
|
|
|
for (const auto& chunk : writing_queue) {
|
|
|
|
file.write(reinterpret_cast<const char*>(chunk.pointer), chunk.size);
|
2014-08-14 14:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 19:21:55 +02:00
|
|
|
static std::unique_ptr<PicaTrace> pica_trace;
|
|
|
|
static std::mutex pica_trace_mutex;
|
2016-12-15 07:01:24 +01:00
|
|
|
bool g_is_pica_tracing = false;
|
2014-08-14 19:21:55 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
void StartPicaTracing() {
|
2016-12-15 07:01:24 +01:00
|
|
|
if (g_is_pica_tracing) {
|
2014-12-06 02:53:49 +01:00
|
|
|
LOG_WARNING(HW_GPU, "StartPicaTracing called even though tracing already running!");
|
2014-08-14 19:21:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:56:45 +02:00
|
|
|
std::lock_guard<std::mutex> lock(pica_trace_mutex);
|
2016-04-15 04:04:46 +02:00
|
|
|
pica_trace = std::make_unique<PicaTrace>();
|
2014-08-14 19:21:55 +02:00
|
|
|
|
2016-12-15 07:01:24 +01:00
|
|
|
g_is_pica_tracing = true;
|
2014-08-14 19:21:55 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
void OnPicaRegWrite(PicaTrace::Write write) {
|
2015-07-26 15:56:45 +02:00
|
|
|
std::lock_guard<std::mutex> lock(pica_trace_mutex);
|
2014-08-14 19:21:55 +02:00
|
|
|
|
2016-12-15 07:01:24 +01:00
|
|
|
if (!g_is_pica_tracing)
|
2014-08-14 19:21:55 +02:00
|
|
|
return;
|
|
|
|
|
2015-07-25 22:00:40 +02:00
|
|
|
pica_trace->writes.push_back(write);
|
2014-08-14 19:21:55 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
std::unique_ptr<PicaTrace> FinishPicaTracing() {
|
2016-12-15 07:01:24 +01:00
|
|
|
if (!g_is_pica_tracing) {
|
2014-12-06 02:53:49 +01:00
|
|
|
LOG_WARNING(HW_GPU, "FinishPicaTracing called even though tracing isn't running!");
|
2014-08-14 19:21:55 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// signalize that no further tracing should be performed
|
2016-12-15 07:01:24 +01:00
|
|
|
g_is_pica_tracing = false;
|
2014-08-14 19:21:55 +02:00
|
|
|
|
|
|
|
// Wait until running tracing is finished
|
2015-07-26 15:56:45 +02:00
|
|
|
std::lock_guard<std::mutex> lock(pica_trace_mutex);
|
2014-08-14 19:21:55 +02:00
|
|
|
std::unique_ptr<PicaTrace> ret(std::move(pica_trace));
|
2015-07-26 15:56:45 +02:00
|
|
|
|
2016-06-25 20:26:21 +02:00
|
|
|
return ret;
|
2014-08-14 19:21:55 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 01:29:16 +02:00
|
|
|
#ifdef HAVE_PNG
|
|
|
|
// Adapter functions to libpng to write/flush to File::IOFile instances.
|
|
|
|
static void WriteIOFile(png_structp png_ptr, png_bytep data, png_size_t length) {
|
|
|
|
auto* fp = static_cast<FileUtil::IOFile*>(png_get_io_ptr(png_ptr));
|
|
|
|
if (!fp->WriteBytes(data, length))
|
2016-09-18 02:38:01 +02:00
|
|
|
png_error(png_ptr, "Failed to write to output PNG file.");
|
2016-04-14 01:29:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void FlushIOFile(png_structp png_ptr) {
|
|
|
|
auto* fp = static_cast<FileUtil::IOFile*>(png_get_io_ptr(png_ptr));
|
|
|
|
if (!fp->Flush())
|
2016-09-18 02:38:01 +02:00
|
|
|
png_error(png_ptr, "Failed to flush to output PNG file.");
|
2016-04-14 01:29:16 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
void DumpTexture(const TexturingRegs::TextureConfig& texture_config, u8* data) {
|
2014-08-16 14:06:40 +02:00
|
|
|
#ifndef HAVE_PNG
|
2014-11-19 10:02:05 +01:00
|
|
|
return;
|
2014-08-16 14:06:40 +02:00
|
|
|
#else
|
2014-11-19 10:02:05 +01:00
|
|
|
if (!data)
|
2014-08-16 14:06:40 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Write data to file
|
|
|
|
static int dump_index = 0;
|
2016-09-18 02:38:01 +02:00
|
|
|
std::string filename =
|
|
|
|
std::string("texture_dump") + std::to_string(++dump_index) + std::string(".png");
|
2014-08-16 14:06:40 +02:00
|
|
|
u32 row_stride = texture_config.width * 3;
|
|
|
|
|
|
|
|
u8* buf;
|
|
|
|
|
|
|
|
char title[] = "Citra texture dump";
|
|
|
|
char title_key[] = "Title";
|
|
|
|
png_structp png_ptr = nullptr;
|
|
|
|
png_infop info_ptr = nullptr;
|
|
|
|
|
|
|
|
// Open file for writing (binary mode)
|
2014-09-12 00:17:15 +02:00
|
|
|
FileUtil::IOFile fp(filename, "wb");
|
2014-08-16 14:06:40 +02:00
|
|
|
|
|
|
|
// Initialize write structure
|
|
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
|
|
|
if (png_ptr == nullptr) {
|
2015-10-09 23:13:54 +02:00
|
|
|
LOG_ERROR(Debug_GPU, "Could not allocate write struct");
|
2014-08-16 14:06:40 +02:00
|
|
|
goto finalise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize info structure
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if (info_ptr == nullptr) {
|
2015-10-09 23:13:54 +02:00
|
|
|
LOG_ERROR(Debug_GPU, "Could not allocate info struct");
|
2014-08-16 14:06:40 +02:00
|
|
|
goto finalise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Exception handling
|
|
|
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
2015-10-09 23:13:54 +02:00
|
|
|
LOG_ERROR(Debug_GPU, "Error during png creation");
|
2014-08-16 14:06:40 +02:00
|
|
|
goto finalise;
|
|
|
|
}
|
|
|
|
|
2016-04-14 01:29:16 +02:00
|
|
|
png_set_write_fn(png_ptr, static_cast<void*>(&fp), WriteIOFile, FlushIOFile);
|
2014-08-16 14:06:40 +02:00
|
|
|
|
2015-04-05 08:36:43 +02:00
|
|
|
// Write header (8 bit color depth)
|
2016-09-18 02:38:01 +02:00
|
|
|
png_set_IHDR(png_ptr, info_ptr, texture_config.width, texture_config.height, 8,
|
|
|
|
PNG_COLOR_TYPE_RGB /*_ALPHA*/, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
|
|
|
|
PNG_FILTER_TYPE_BASE);
|
2014-08-16 14:06:40 +02:00
|
|
|
|
|
|
|
png_text title_text;
|
|
|
|
title_text.compression = PNG_TEXT_COMPRESSION_NONE;
|
|
|
|
title_text.key = title_key;
|
|
|
|
title_text.text = title;
|
|
|
|
png_set_text(png_ptr, info_ptr, &title_text, 1);
|
|
|
|
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
buf = new u8[row_stride * texture_config.height];
|
2014-11-30 08:44:30 +01:00
|
|
|
for (unsigned y = 0; y < texture_config.height; ++y) {
|
|
|
|
for (unsigned x = 0; x < texture_config.width; ++x) {
|
2017-01-05 23:11:23 +01:00
|
|
|
Pica::Texture::TextureInfo info;
|
2014-08-24 14:39:52 +02:00
|
|
|
info.width = texture_config.width;
|
|
|
|
info.height = texture_config.height;
|
|
|
|
info.stride = row_stride;
|
2017-01-28 05:51:59 +01:00
|
|
|
info.format = g_state.regs.texturing.texture0_format;
|
2017-01-05 23:11:23 +01:00
|
|
|
Math::Vec4<u8> texture_color = Pica::Texture::LookupTexture(data, x, y, info);
|
2016-09-18 02:38:01 +02:00
|
|
|
buf[3 * x + y * row_stride] = texture_color.r();
|
2014-08-24 14:39:52 +02:00
|
|
|
buf[3 * x + y * row_stride + 1] = texture_color.g();
|
|
|
|
buf[3 * x + y * row_stride + 2] = texture_color.b();
|
2014-08-16 14:06:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write image data
|
2016-09-18 02:38:01 +02:00
|
|
|
for (unsigned y = 0; y < texture_config.height; ++y) {
|
2014-08-16 14:06:40 +02:00
|
|
|
u8* row_ptr = (u8*)buf + y * row_stride;
|
|
|
|
png_write_row(png_ptr, row_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] buf;
|
|
|
|
|
|
|
|
// End write
|
|
|
|
png_write_end(png_ptr, nullptr);
|
|
|
|
|
|
|
|
finalise:
|
2016-09-18 02:38:01 +02:00
|
|
|
if (info_ptr != nullptr)
|
|
|
|
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
|
|
|
if (png_ptr != nullptr)
|
|
|
|
png_destroy_write_struct(&png_ptr, (png_infopp) nullptr);
|
2014-08-16 14:06:40 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
static std::string ReplacePattern(const std::string& input, const std::string& pattern,
|
|
|
|
const std::string& replacement) {
|
2016-05-17 11:13:25 +02:00
|
|
|
size_t start = input.find(pattern);
|
|
|
|
if (start == std::string::npos)
|
|
|
|
return input;
|
|
|
|
|
|
|
|
std::string ret = input;
|
|
|
|
ret.replace(start, pattern.length(), replacement);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
static std::string GetTevStageConfigSourceString(
|
|
|
|
const TexturingRegs::TevStageConfig::Source& source) {
|
|
|
|
|
|
|
|
using Source = TexturingRegs::TevStageConfig::Source;
|
2016-05-17 11:13:25 +02:00
|
|
|
static const std::map<Source, std::string> source_map = {
|
2016-09-18 02:38:01 +02:00
|
|
|
{Source::PrimaryColor, "PrimaryColor"},
|
|
|
|
{Source::PrimaryFragmentColor, "PrimaryFragmentColor"},
|
|
|
|
{Source::SecondaryFragmentColor, "SecondaryFragmentColor"},
|
|
|
|
{Source::Texture0, "Texture0"},
|
|
|
|
{Source::Texture1, "Texture1"},
|
|
|
|
{Source::Texture2, "Texture2"},
|
|
|
|
{Source::Texture3, "Texture3"},
|
|
|
|
{Source::PreviousBuffer, "PreviousBuffer"},
|
|
|
|
{Source::Constant, "Constant"},
|
|
|
|
{Source::Previous, "Previous"},
|
2016-05-17 11:13:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto src_it = source_map.find(source);
|
|
|
|
if (src_it == source_map.end())
|
|
|
|
return "Unknown";
|
|
|
|
|
|
|
|
return src_it->second;
|
|
|
|
}
|
|
|
|
|
2016-09-19 03:01:46 +02:00
|
|
|
static std::string GetTevStageConfigColorSourceString(
|
2017-01-28 05:51:59 +01:00
|
|
|
const TexturingRegs::TevStageConfig::Source& source,
|
|
|
|
const TexturingRegs::TevStageConfig::ColorModifier modifier) {
|
|
|
|
|
|
|
|
using ColorModifier = TexturingRegs::TevStageConfig::ColorModifier;
|
2016-05-17 11:13:25 +02:00
|
|
|
static const std::map<ColorModifier, std::string> color_modifier_map = {
|
2016-09-18 02:38:01 +02:00
|
|
|
{ColorModifier::SourceColor, "%source.rgb"},
|
|
|
|
{ColorModifier::OneMinusSourceColor, "(1.0 - %source.rgb)"},
|
|
|
|
{ColorModifier::SourceAlpha, "%source.aaa"},
|
|
|
|
{ColorModifier::OneMinusSourceAlpha, "(1.0 - %source.aaa)"},
|
|
|
|
{ColorModifier::SourceRed, "%source.rrr"},
|
|
|
|
{ColorModifier::OneMinusSourceRed, "(1.0 - %source.rrr)"},
|
|
|
|
{ColorModifier::SourceGreen, "%source.ggg"},
|
|
|
|
{ColorModifier::OneMinusSourceGreen, "(1.0 - %source.ggg)"},
|
|
|
|
{ColorModifier::SourceBlue, "%source.bbb"},
|
|
|
|
{ColorModifier::OneMinusSourceBlue, "(1.0 - %source.bbb)"},
|
2016-05-17 11:13:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto src_str = GetTevStageConfigSourceString(source);
|
|
|
|
auto modifier_it = color_modifier_map.find(modifier);
|
|
|
|
std::string modifier_str = "%source.????";
|
|
|
|
if (modifier_it != color_modifier_map.end())
|
|
|
|
modifier_str = modifier_it->second;
|
|
|
|
|
|
|
|
return ReplacePattern(modifier_str, "%source", src_str);
|
|
|
|
}
|
|
|
|
|
2016-09-19 03:01:46 +02:00
|
|
|
static std::string GetTevStageConfigAlphaSourceString(
|
2017-01-28 05:51:59 +01:00
|
|
|
const TexturingRegs::TevStageConfig::Source& source,
|
|
|
|
const TexturingRegs::TevStageConfig::AlphaModifier modifier) {
|
|
|
|
|
|
|
|
using AlphaModifier = TexturingRegs::TevStageConfig::AlphaModifier;
|
2016-05-17 11:13:25 +02:00
|
|
|
static const std::map<AlphaModifier, std::string> alpha_modifier_map = {
|
2016-09-18 02:38:01 +02:00
|
|
|
{AlphaModifier::SourceAlpha, "%source.a"},
|
|
|
|
{AlphaModifier::OneMinusSourceAlpha, "(1.0 - %source.a)"},
|
|
|
|
{AlphaModifier::SourceRed, "%source.r"},
|
|
|
|
{AlphaModifier::OneMinusSourceRed, "(1.0 - %source.r)"},
|
|
|
|
{AlphaModifier::SourceGreen, "%source.g"},
|
|
|
|
{AlphaModifier::OneMinusSourceGreen, "(1.0 - %source.g)"},
|
|
|
|
{AlphaModifier::SourceBlue, "%source.b"},
|
|
|
|
{AlphaModifier::OneMinusSourceBlue, "(1.0 - %source.b)"},
|
2016-05-17 11:13:25 +02:00
|
|
|
};
|
2014-08-17 12:17:32 +02:00
|
|
|
|
2016-05-17 11:13:25 +02:00
|
|
|
auto src_str = GetTevStageConfigSourceString(source);
|
|
|
|
auto modifier_it = alpha_modifier_map.find(modifier);
|
|
|
|
std::string modifier_str = "%source.????";
|
|
|
|
if (modifier_it != alpha_modifier_map.end())
|
|
|
|
modifier_str = modifier_it->second;
|
2014-08-17 12:17:32 +02:00
|
|
|
|
2016-05-17 11:13:25 +02:00
|
|
|
return ReplacePattern(modifier_str, "%source", src_str);
|
|
|
|
}
|
2014-08-17 12:17:32 +02:00
|
|
|
|
2016-09-19 03:01:46 +02:00
|
|
|
static std::string GetTevStageConfigOperationString(
|
2017-01-28 05:51:59 +01:00
|
|
|
const TexturingRegs::TevStageConfig::Operation& operation) {
|
|
|
|
|
|
|
|
using Operation = TexturingRegs::TevStageConfig::Operation;
|
2016-05-17 11:13:25 +02:00
|
|
|
static const std::map<Operation, std::string> combiner_map = {
|
2016-09-18 02:38:01 +02:00
|
|
|
{Operation::Replace, "%source1"},
|
|
|
|
{Operation::Modulate, "(%source1 * %source2)"},
|
|
|
|
{Operation::Add, "(%source1 + %source2)"},
|
|
|
|
{Operation::AddSigned, "(%source1 + %source2) - 0.5"},
|
|
|
|
{Operation::Lerp, "lerp(%source1, %source2, %source3)"},
|
|
|
|
{Operation::Subtract, "(%source1 - %source2)"},
|
|
|
|
{Operation::Dot3_RGB, "dot(%source1, %source2)"},
|
|
|
|
{Operation::MultiplyThenAdd, "((%source1 * %source2) + %source3)"},
|
|
|
|
{Operation::AddThenMultiply, "((%source1 + %source2) * %source3)"},
|
2016-05-17 11:13:25 +02:00
|
|
|
};
|
2016-04-16 01:00:09 +02:00
|
|
|
|
2016-05-17 11:13:25 +02:00
|
|
|
const auto op_it = combiner_map.find(operation);
|
|
|
|
if (op_it == combiner_map.end())
|
|
|
|
return "Unknown op (%source1, %source2, %source3)";
|
2014-08-17 12:17:32 +02:00
|
|
|
|
2016-05-17 11:13:25 +02:00
|
|
|
return op_it->second;
|
|
|
|
}
|
2014-08-17 12:17:32 +02:00
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
std::string GetTevStageConfigColorCombinerString(const TexturingRegs::TevStageConfig& tev_stage) {
|
2016-05-17 11:13:25 +02:00
|
|
|
auto op_str = GetTevStageConfigOperationString(tev_stage.color_op);
|
2016-09-18 02:38:01 +02:00
|
|
|
op_str = ReplacePattern(
|
|
|
|
op_str, "%source1",
|
|
|
|
GetTevStageConfigColorSourceString(tev_stage.color_source1, tev_stage.color_modifier1));
|
|
|
|
op_str = ReplacePattern(
|
|
|
|
op_str, "%source2",
|
|
|
|
GetTevStageConfigColorSourceString(tev_stage.color_source2, tev_stage.color_modifier2));
|
|
|
|
return ReplacePattern(
|
|
|
|
op_str, "%source3",
|
|
|
|
GetTevStageConfigColorSourceString(tev_stage.color_source3, tev_stage.color_modifier3));
|
2016-05-17 11:13:25 +02:00
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
std::string GetTevStageConfigAlphaCombinerString(const TexturingRegs::TevStageConfig& tev_stage) {
|
2016-05-17 11:13:25 +02:00
|
|
|
auto op_str = GetTevStageConfigOperationString(tev_stage.alpha_op);
|
2016-09-18 02:38:01 +02:00
|
|
|
op_str = ReplacePattern(
|
|
|
|
op_str, "%source1",
|
|
|
|
GetTevStageConfigAlphaSourceString(tev_stage.alpha_source1, tev_stage.alpha_modifier1));
|
|
|
|
op_str = ReplacePattern(
|
|
|
|
op_str, "%source2",
|
|
|
|
GetTevStageConfigAlphaSourceString(tev_stage.alpha_source2, tev_stage.alpha_modifier2));
|
|
|
|
return ReplacePattern(
|
|
|
|
op_str, "%source3",
|
|
|
|
GetTevStageConfigAlphaSourceString(tev_stage.alpha_source3, tev_stage.alpha_modifier3));
|
2016-05-17 11:13:25 +02:00
|
|
|
}
|
2014-08-17 12:17:32 +02:00
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
void DumpTevStageConfig(const std::array<TexturingRegs::TevStageConfig, 6>& stages) {
|
2016-05-17 11:13:25 +02:00
|
|
|
std::string stage_info = "Tev setup:\n";
|
|
|
|
for (size_t index = 0; index < stages.size(); ++index) {
|
|
|
|
const auto& tev_stage = stages[index];
|
2016-09-18 02:38:01 +02:00
|
|
|
stage_info += "Stage " + std::to_string(index) + ": " +
|
|
|
|
GetTevStageConfigColorCombinerString(tev_stage) + " " +
|
|
|
|
GetTevStageConfigAlphaCombinerString(tev_stage) + "\n";
|
2016-05-17 11:13:25 +02:00
|
|
|
}
|
2014-12-06 02:53:49 +01:00
|
|
|
LOG_TRACE(HW_GPU, "%s", stage_info.c_str());
|
2014-08-17 12:17:32 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 14:06:58 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
} // namespace
|