-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ProgramStateCache.cpp
472 lines (407 loc) · 12.2 KB
/
ProgramStateCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "stdafx.h"
#include "ProgramStateCache.h"
#include "Emu/system_config.h"
#include <stack>
using namespace program_hash_util;
size_t vertex_program_utils::get_vertex_program_ucode_hash(const RSXVertexProgram &program)
{
// 64-bit Fowler/Noll/Vo FNV-1a hash code
size_t hash = 0xCBF29CE484222325ULL;
const qword* instbuffer = reinterpret_cast<const qword*>(program.data.data());
size_t instIndex = 0;
bool end = false;
for (unsigned i = 0; i < program.data.size() / 4; i++)
{
if (program.instruction_mask[i])
{
const qword inst = instbuffer[instIndex];
hash ^= inst.dword[0];
hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) + (hash << 8) + (hash << 40);
hash ^= inst.dword[1];
hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) + (hash << 8) + (hash << 40);
}
instIndex++;
}
return hash;
}
vertex_program_utils::vertex_program_metadata vertex_program_utils::analyse_vertex_program(const u32* data, u32 entry, RSXVertexProgram& dst_prog)
{
vertex_program_utils::vertex_program_metadata result{};
u32 last_instruction_address = 0;
u32 first_instruction_address = entry;
std::stack<u32> call_stack;
std::pair<u32, u32> instruction_range = { UINT32_MAX, 0 };
std::bitset<512> instructions_to_patch;
bool has_branch_instruction = false;
D3 d3;
D2 d2;
D1 d1;
D0 d0;
std::function<void(u32, bool)> walk_function = [&](u32 start, bool fast_exit)
{
u32 current_instruction = start;
std::set<u32> conditional_targets;
while (true)
{
verify(HERE), current_instruction < 512;
if (result.instruction_mask[current_instruction])
{
if (!fast_exit)
{
// This can be harmless if a dangling RET was encountered before
rsx_log.error("vp_analyser: Possible infinite loop detected");
current_instruction++;
continue;
}
else
{
// Block walk, looking for earliest exit
break;
}
}
const qword* instruction = reinterpret_cast<const qword*>(&data[current_instruction * 4]);
d1.HEX = instruction->word[1];
d3.HEX = instruction->word[3];
// Touch current instruction
result.instruction_mask[current_instruction] = true;
instruction_range.first = std::min(current_instruction, instruction_range.first);
instruction_range.second = std::max(current_instruction, instruction_range.second);
// Basic vec op analysis, must be done before flow analysis
switch (d1.vec_opcode)
{
case RSX_VEC_OPCODE_TXL:
{
d2.HEX = instruction->word[2];
result.referenced_textures_mask |= (1 << d2.tex_num);
break;
}
}
bool static_jump = false;
bool function_call = true;
switch (d1.sca_opcode)
{
case RSX_SCA_OPCODE_BRI:
{
d0.HEX = instruction->word[0];
static_jump = (d0.cond == 0x7);
// Fall through
}
case RSX_SCA_OPCODE_BRB:
{
function_call = false;
// Fall through
}
case RSX_SCA_OPCODE_CAL:
case RSX_SCA_OPCODE_CLI:
case RSX_SCA_OPCODE_CLB:
{
// Need to patch the jump address to be consistent wherever the program is located
instructions_to_patch[current_instruction] = true;
has_branch_instruction = true;
d2.HEX = instruction->word[2];
const u32 jump_address = ((d2.iaddrh << 3) | d3.iaddrl);
if (function_call)
{
call_stack.push(current_instruction + 1);
current_instruction = jump_address;
continue;
}
else if (static_jump)
{
// NOTE: This will skip potential jump target blocks between current->target
current_instruction = jump_address;
continue;
}
else
{
// Set possible end address and proceed as usual
conditional_targets.emplace(jump_address);
instruction_range.second = std::max(jump_address, instruction_range.second);
}
break;
}
case RSX_SCA_OPCODE_RET:
{
if (call_stack.empty())
{
rsx_log.error("vp_analyser: RET found outside subroutine call");
}
else
{
current_instruction = call_stack.top();
call_stack.pop();
continue;
}
break;
}
}
if ((d3.end && (fast_exit || current_instruction >= instruction_range.second)) ||
(current_instruction + 1) == 512)
{
break;
}
current_instruction++;
}
for (const u32 target : conditional_targets)
{
if (!result.instruction_mask[target])
{
walk_function(target, true);
}
}
};
if (g_cfg.video.log_programs)
{
fs::file dump(fs::get_cache_dir() + "shaderlog/vp_analyser.bin", fs::rewrite);
dump.write(&entry, 4);
dump.write(data, 512 * 16);
dump.close();
}
walk_function(entry, false);
const u32 instruction_count = (instruction_range.second - instruction_range.first + 1);
result.ucode_length = instruction_count * 16;
dst_prog.base_address = instruction_range.first;
dst_prog.entry = entry;
dst_prog.data.resize(instruction_count * 4);
dst_prog.instruction_mask = (result.instruction_mask >> instruction_range.first);
if (!has_branch_instruction)
{
verify(HERE), instruction_range.first == entry;
std::memcpy(dst_prog.data.data(), data + (instruction_range.first * 4), result.ucode_length);
}
else
{
for (u32 i = instruction_range.first, count = 0; i <= instruction_range.second; ++i, ++count)
{
const qword* instruction = reinterpret_cast<const qword*>(&data[i * 4]);
qword* dst = reinterpret_cast<qword*>(&dst_prog.data[count * 4]);
if (result.instruction_mask[i])
{
dst->dword[0] = instruction->dword[0];
dst->dword[1] = instruction->dword[1];
if (instructions_to_patch[i])
{
d2.HEX = dst->word[2];
d3.HEX = dst->word[3];
u32 address = ((d2.iaddrh << 3) | d3.iaddrl);
address -= instruction_range.first;
d2.iaddrh = (address >> 3);
d3.iaddrl = (address & 0x7);
dst->word[2] = d2.HEX;
dst->word[3] = d3.HEX;
dst_prog.jump_table.emplace(address);
}
}
else
{
dst->dword[0] = 0ull;
dst->dword[1] = 0ull;
}
}
// Verification
for (const u32 target : dst_prog.jump_table)
{
if (!dst_prog.instruction_mask[target])
{
rsx_log.error("vp_analyser: Failed, branch target 0x%x was not resolved", target);
}
}
}
return result;
}
size_t vertex_program_storage_hash::operator()(const RSXVertexProgram &program) const
{
size_t hash = vertex_program_utils::get_vertex_program_ucode_hash(program);
hash ^= program.output_mask;
hash ^= program.texture_dimensions;
return hash;
}
bool vertex_program_compare::operator()(const RSXVertexProgram &binary1, const RSXVertexProgram &binary2) const
{
if (binary1.output_mask != binary2.output_mask)
return false;
if (binary1.texture_dimensions != binary2.texture_dimensions)
return false;
if (binary1.data.size() != binary2.data.size())
return false;
if (binary1.jump_table != binary2.jump_table)
return false;
if (!binary1.skip_vertex_input_check && !binary2.skip_vertex_input_check && binary1.rsx_vertex_inputs != binary2.rsx_vertex_inputs)
return false;
const qword* instBuffer1 = reinterpret_cast<const qword*>(binary1.data.data());
const qword* instBuffer2 = reinterpret_cast<const qword*>(binary2.data.data());
size_t instIndex = 0;
for (unsigned i = 0; i < binary1.data.size() / 4; i++)
{
const auto active = binary1.instruction_mask[instIndex];
if (active != binary2.instruction_mask[instIndex])
{
return false;
}
if (active)
{
const qword& inst1 = instBuffer1[instIndex];
const qword& inst2 = instBuffer2[instIndex];
if (inst1.dword[0] != inst2.dword[0] || inst1.dword[1] != inst2.dword[1])
{
return false;
}
}
instIndex++;
}
return true;
}
bool fragment_program_utils::is_constant(u32 sourceOperand)
{
return ((sourceOperand >> 8) & 0x3) == 2;
}
size_t fragment_program_utils::get_fragment_program_ucode_size(void *ptr)
{
const qword* instBuffer = reinterpret_cast<const qword*>(ptr);
size_t instIndex = 0;
while (true)
{
const qword& inst = instBuffer[instIndex];
bool isSRC0Constant = is_constant(inst.word[1]);
bool isSRC1Constant = is_constant(inst.word[2]);
bool isSRC2Constant = is_constant(inst.word[3]);
bool end = (inst.word[0] >> 8) & 0x1;
if (isSRC0Constant || isSRC1Constant || isSRC2Constant)
{
instIndex += 2;
if (end)
return instIndex * 4 * 4;
continue;
}
instIndex++;
if (end)
return (instIndex)* 4 * 4;
}
}
fragment_program_utils::fragment_program_metadata fragment_program_utils::analyse_fragment_program(void *ptr)
{
const qword* instBuffer = reinterpret_cast<const qword*>(ptr);
s32 index = 0;
s32 program_offset = -1;
u32 ucode_size = 0;
u32 constants_size = 0;
u16 textures_mask = 0;
while (true)
{
const qword& inst = instBuffer[index];
const u32 opcode = (inst.word[0] >> 16) & 0x3F;
if (opcode)
{
if (program_offset < 0)
program_offset = index * 16;
switch(opcode)
{
case RSX_FP_OPCODE_TEX:
case RSX_FP_OPCODE_TEXBEM:
case RSX_FP_OPCODE_TXP:
case RSX_FP_OPCODE_TXPBEM:
case RSX_FP_OPCODE_TXD:
case RSX_FP_OPCODE_TXB:
case RSX_FP_OPCODE_TXL:
{
//Bits 17-20 of word 1, swapped within u16 sections
//Bits 16-23 are swapped into the upper 8 bits (24-31)
const u32 tex_num = (inst.word[0] >> 25) & 15;
textures_mask |= (1 << tex_num);
break;
}
}
if (is_constant(inst.word[1]) || is_constant(inst.word[2]) || is_constant(inst.word[3]))
{
//Instruction references constant, skip one slot occupied by data
index++;
ucode_size += 16;
constants_size += 16;
}
}
if (program_offset >= 0)
{
ucode_size += 16;
}
if ((inst.word[0] >> 8) & 0x1)
{
if (program_offset < 0)
{
program_offset = index * 16;
ucode_size = 16;
}
break;
}
index++;
}
return{ static_cast<u32>(program_offset), ucode_size, constants_size, textures_mask };
}
size_t fragment_program_utils::get_fragment_program_ucode_hash(const RSXFragmentProgram& program)
{
// 64-bit Fowler/Noll/Vo FNV-1a hash code
size_t hash = 0xCBF29CE484222325ULL;
const qword* instbuffer = reinterpret_cast<const qword*>(program.addr);
size_t instIndex = 0;
while (true)
{
const qword& inst = instbuffer[instIndex];
hash ^= inst.dword[0];
hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) + (hash << 8) + (hash << 40);
hash ^= inst.dword[1];
hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) + (hash << 8) + (hash << 40);
instIndex++;
// Skip constants
if (fragment_program_utils::is_constant(inst.word[1]) ||
fragment_program_utils::is_constant(inst.word[2]) ||
fragment_program_utils::is_constant(inst.word[3]))
instIndex++;
bool end = (inst.word[0] >> 8) & 0x1;
if (end)
return hash;
}
return 0;
}
size_t fragment_program_storage_hash::operator()(const RSXFragmentProgram& program) const
{
size_t hash = fragment_program_utils::get_fragment_program_ucode_hash(program);
hash ^= program.ctrl;
hash ^= program.texture_dimensions;
hash ^= program.unnormalized_coords;
hash ^= +program.two_sided_lighting;
hash ^= program.shadow_textures;
hash ^= program.redirected_textures;
return hash;
}
bool fragment_program_compare::operator()(const RSXFragmentProgram& binary1, const RSXFragmentProgram& binary2) const
{
if (binary1.ctrl != binary2.ctrl || binary1.texture_dimensions != binary2.texture_dimensions || binary1.unnormalized_coords != binary2.unnormalized_coords ||
binary1.two_sided_lighting != binary2.two_sided_lighting ||
binary1.shadow_textures != binary2.shadow_textures || binary1.redirected_textures != binary2.redirected_textures)
return false;
for (u8 index = 0; index < 16; ++index)
{
if (binary1.textures_alpha_kill[index] != binary2.textures_alpha_kill[index])
return false;
if (binary1.textures_zfunc[index] != binary2.textures_zfunc[index])
return false;
}
const qword* instBuffer1 = reinterpret_cast<const qword*>(binary1.addr);
const qword* instBuffer2 = reinterpret_cast<const qword*>(binary2.addr);
size_t instIndex = 0;
while (true)
{
const qword& inst1 = instBuffer1[instIndex];
const qword& inst2 = instBuffer2[instIndex];
if (inst1.dword[0] != inst2.dword[0] || inst1.dword[1] != inst2.dword[1])
return false;
instIndex++;
// Skip constants
if (fragment_program_utils::is_constant(inst1.word[1]) ||
fragment_program_utils::is_constant(inst1.word[2]) ||
fragment_program_utils::is_constant(inst1.word[3]))
instIndex++;
bool end = ((inst1.word[0] >> 8) & 0x1) && ((inst2.word[0] >> 8) & 0x1);
if (end)
return true;
}
}