1
// Copyright 2013 Dolphin Emulator Project
2
// Licensed under GPLv2
3
// Refer to the license.txt file included.
8
#include "BPMemLoader.h"
9
#include "TextureSampler.h"
10
#include "SWVideoConfig.h"
11
#include "EfbInterface.h"
12
#include "SWStatistics.h"
13
#include "HwRasterizer.h"
14
#include "StringUtil.h"
15
#include "SWCommandProcessor.h"
16
#include "ImageWrite.h"
23
bool drawingHwTriangles = false;
25
enum { NumObjectBuffers = 40};
27
u32 ObjectBuffer[NumObjectBuffers][EFB_WIDTH*EFB_HEIGHT];
28
u32 TempBuffer[NumObjectBuffers];
30
bool DrawnToBuffer[NumObjectBuffers];
31
const char* ObjectBufferName[NumObjectBuffers];
32
int BufferBase[NumObjectBuffers];
36
for (int i = 0; i < NumObjectBuffers; i++)
38
memset(ObjectBuffer[i], 0, sizeof(ObjectBuffer[i]));
39
DrawnToBuffer[i] = false;
40
ObjectBufferName[i] = 0;
45
void SaveTexture(const char* filename, u32 texmap, s32 mip)
47
FourTexUnits& texUnit = bpmem.tex[(texmap >> 2) & 1];
48
u8 subTexmap = texmap & 3;
50
TexImage0& ti0 = texUnit.texImage0[subTexmap];
52
u32 width = ti0.width + 1;
53
u32 height = ti0.height + 1;
55
u8 *data = new u8[width * height * 4];
57
GetTextureBGRA(data, texmap, mip, width, height);
59
(void)SaveTGA(filename, width, height, data);
64
void GetTextureBGRA(u8 *dst, u32 texmap, s32 mip, u32 width, u32 height)
68
for (u32 y = 0; y < height; y++)
70
for (u32 x = 0; x < width; x++)
72
TextureSampler::SampleMip(x << 7, y << 7, mip, false, texmap, sample);
83
s32 GetMaxTextureLod(u32 texmap)
85
FourTexUnits& texUnit = bpmem.tex[(texmap >> 2) & 1];
86
u8 subTexmap = texmap & 3;
88
u8 maxLod = texUnit.texMode1[subTexmap].max_lod;
90
u8 fract = maxLod & 0xf;
98
void DumpActiveTextures()
100
for (unsigned int stageNum = 0; stageNum < bpmem.genMode.numindstages; stageNum++)
102
u32 texmap = bpmem.tevindref.getTexMap(stageNum);
104
s32 maxLod = GetMaxTextureLod(texmap);
105
for (s32 mip = 0; mip <= maxLod; ++mip)
107
SaveTexture(StringFromFormat("%star%i_ind%i_map%i_mip%i.tga",
108
File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
109
swstats.thisFrame.numDrawnObjects, stageNum, texmap, mip).c_str(), texmap, mip);
113
for (unsigned int stageNum = 0; stageNum <= bpmem.genMode.numtevstages; stageNum++)
115
int stageNum2 = stageNum >> 1;
116
int stageOdd = stageNum&1;
117
TwoTevStageOrders &order = bpmem.tevorders[stageNum2];
119
int texmap = order.getTexMap(stageOdd);
121
s32 maxLod = GetMaxTextureLod(texmap);
122
for (s32 mip = 0; mip <= maxLod; ++mip)
124
SaveTexture(StringFromFormat("%star%i_stage%i_map%i_mip%i.tga",
125
File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
126
swstats.thisFrame.numDrawnObjects, stageNum, texmap, mip).c_str(), texmap, mip);
131
void DumpEfb(const char* filename)
133
u8 *data = new u8[EFB_WIDTH * EFB_HEIGHT * 4];
137
for (int y = 0; y < EFB_HEIGHT; y++)
139
for (int x = 0; x < EFB_WIDTH; x++)
141
EfbInterface::GetColor(x, y, sample);
143
*(writePtr++) = sample[1];
144
*(writePtr++) = sample[2];
145
*(writePtr++) = sample[3];
146
*(writePtr++) = sample[0];
150
(void)SaveTGA(filename, EFB_WIDTH, EFB_HEIGHT, data);
155
void DumpDepth(const char* filename)
157
u8 *data = new u8[EFB_WIDTH * EFB_HEIGHT * 4];
160
for (int y = 0; y < EFB_HEIGHT; y++)
162
for (int x = 0; x < EFB_WIDTH; x++)
164
u32 depth = EfbInterface::GetDepth(x, y);
166
*(writePtr++) = (depth >> 16) & 0xff;
167
*(writePtr++) = (depth >> 8) & 0xff;
168
*(writePtr++) = depth & 0xff;
173
(void)SaveTGA(filename, EFB_WIDTH, EFB_HEIGHT, data);
178
void DrawObjectBuffer(s16 x, s16 y, u8 *color, int bufferBase, int subBuffer, const char *name)
180
int buffer = bufferBase + subBuffer;
182
u32 offset = (x + y * EFB_WIDTH) * 4;
183
u8 *dst = (u8*)&ObjectBuffer[buffer][offset];
189
DrawnToBuffer[buffer] = true;
190
ObjectBufferName[buffer] = name;
191
BufferBase[buffer] = bufferBase;
194
void DrawTempBuffer(u8 *color, int buffer)
196
u8 *dst = (u8*)&TempBuffer[buffer];
203
void CopyTempBuffer(s16 x, s16 y, int bufferBase, int subBuffer, const char *name)
205
int buffer = bufferBase + subBuffer;
207
u32 offset = (x + y * EFB_WIDTH);
208
ObjectBuffer[buffer][offset] = TempBuffer[buffer];
210
DrawnToBuffer[buffer] = true;
211
ObjectBufferName[buffer] = name;
212
BufferBase[buffer] = bufferBase;
217
if (!g_bSkipCurrentFrame)
219
if (g_SWVideoConfig.bDumpTextures && swstats.thisFrame.numDrawnObjects >= g_SWVideoConfig.drawStart && swstats.thisFrame.numDrawnObjects < g_SWVideoConfig.drawEnd)
220
DumpActiveTextures();
222
if (g_SWVideoConfig.bHwRasterizer)
224
HwRasterizer::BeginTriangles();
225
drawingHwTriangles = true;
232
if (!g_bSkipCurrentFrame)
234
if (g_SWVideoConfig.bDumpObjects && swstats.thisFrame.numDrawnObjects >= g_SWVideoConfig.drawStart && swstats.thisFrame.numDrawnObjects < g_SWVideoConfig.drawEnd)
235
DumpEfb(StringFromFormat("%sobject%i.tga",
236
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
237
swstats.thisFrame.numDrawnObjects).c_str());
239
if (g_SWVideoConfig.bHwRasterizer || drawingHwTriangles)
241
HwRasterizer::EndTriangles();
242
drawingHwTriangles = false;
245
for (int i = 0; i < NumObjectBuffers; i++)
247
if (DrawnToBuffer[i])
249
DrawnToBuffer[i] = false;
250
(void)SaveTGA(StringFromFormat("%sobject%i_%s(%i).tga",
251
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
252
swstats.thisFrame.numDrawnObjects, ObjectBufferName[i], i - BufferBase[i]).c_str(),
253
EFB_WIDTH, EFB_HEIGHT, ObjectBuffer[i]);
254
memset(ObjectBuffer[i], 0, sizeof(ObjectBuffer[i]));
258
swstats.thisFrame.numDrawnObjects++;
264
if (!g_bSkipCurrentFrame)
266
if (g_SWVideoConfig.bDumpFrames)
268
DumpEfb(StringFromFormat("%sframe%i_color.tga",
269
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), swstats.frameCount).c_str());
270
DumpDepth(StringFromFormat("%sframe%i_depth.tga",
271
File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), swstats.frameCount).c_str());