forked from RPCS3/rpcs3
/
VKFragmentProgram.cpp
473 lines (396 loc) · 12.8 KB
/
VKFragmentProgram.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
473
#include "stdafx.h"
#include "Emu/Memory/vm.h"
#include "Emu/System.h"
#include "VKFragmentProgram.h"
#include "VKCommonDecompiler.h"
#include "VKHelpers.h"
#include "../GCM.h"
std::string VKFragmentDecompilerThread::getFloatTypeName(size_t elementCount)
{
return glsl::getFloatTypeNameImpl(elementCount);
}
std::string VKFragmentDecompilerThread::getHalfTypeName(size_t elementCount)
{
return glsl::getHalfTypeNameImpl(elementCount);
}
std::string VKFragmentDecompilerThread::getFunction(FUNCTION f)
{
return glsl::getFunctionImpl(f);
}
std::string VKFragmentDecompilerThread::compareFunction(COMPARE f, const std::string &Op0, const std::string &Op1)
{
return glsl::compareFunctionImpl(f, Op0, Op1);
}
void VKFragmentDecompilerThread::insertHeader(std::stringstream & OS)
{
if (device_props.has_native_half_support)
{
OS << "#version 450\n";
OS << "#extension GL_KHX_shader_explicit_arithmetic_types_float16: enable\n";
}
else
{
OS << "#version 420\n";
}
OS << "#extension GL_ARB_separate_shader_objects: enable\n\n";
}
void VKFragmentDecompilerThread::insertInputs(std::stringstream & OS)
{
//It is possible for the two_sided_enabled flag to be set without actual 2-sided outputs
bool two_sided_enabled = m_prog.front_back_color_enabled && (m_prog.back_color_diffuse_output || m_prog.back_color_specular_output);
for (const ParamType& PT : m_parr.params[PF_PARAM_IN])
{
for (const ParamItem& PI : PT.items)
{
//ssa is defined in the program body and is not a varying type
if (PI.name == "ssa") continue;
const vk::varying_register_t ® = vk::get_varying_register(PI.name);
std::string var_name = PI.name;
if (two_sided_enabled)
{
if (m_prog.back_color_diffuse_output && var_name == "diff_color")
var_name = "back_diff_color";
if (m_prog.back_color_specular_output && var_name == "spec_color")
var_name = "back_spec_color";
}
if (var_name == "fogc")
var_name = "fog_c";
OS << "layout(location=" << reg.reg_location << ") in " << PT.type << " " << var_name << ";\n";
}
}
if (two_sided_enabled)
{
//Only include the front counterparts if the default output is for back only and exists.
if (m_prog.front_color_diffuse_output && m_prog.back_color_diffuse_output)
{
const vk::varying_register_t ® = vk::get_varying_register("front_diff_color");
OS << "layout(location=" << reg.reg_location << ") in vec4 front_diff_color;\n";
}
if (m_prog.front_color_specular_output && m_prog.back_color_specular_output)
{
const vk::varying_register_t ® = vk::get_varying_register("front_spec_color");
OS << "layout(location=" << reg.reg_location << ") in vec4 front_spec_color;\n";
}
}
}
void VKFragmentDecompilerThread::insertOutputs(std::stringstream & OS)
{
const std::pair<std::string, std::string> table[] =
{
{ "ocol0", m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS ? "r0" : "h0" },
{ "ocol1", m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS ? "r2" : "h4" },
{ "ocol2", m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS ? "r3" : "h6" },
{ "ocol3", m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS ? "r4" : "h8" },
};
//NOTE: We do not skip outputs, the only possible combinations are a(0), b(0), ab(0,1), abc(0,1,2), abcd(0,1,2,3)
u8 output_index = 0;
const bool float_type = (m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS) || !device_props.has_native_half_support;
const auto reg_type = float_type ? "vec4" : getHalfTypeName(4);
for (int i = 0; i < std::size(table); ++i)
{
if (m_parr.HasParam(PF_PARAM_NONE, reg_type, table[i].second))
{
OS << "layout(location=" << std::to_string(output_index++) << ") " << "out vec4 " << table[i].first << ";\n";
vk_prog->output_color_masks[i] = UINT32_MAX;
}
}
}
void VKFragmentDecompilerThread::insertConstants(std::stringstream & OS)
{
u32 location = TEXTURES_FIRST_BIND_SLOT;
for (const ParamType& PT : m_parr.params[PF_PARAM_UNIFORM])
{
if (PT.type != "sampler1D" &&
PT.type != "sampler2D" &&
PT.type != "sampler3D" &&
PT.type != "samplerCube")
continue;
for (const ParamItem& PI : PT.items)
{
std::string samplerType = PT.type;
int index = atoi(&PI.name.data()[3]);
const auto mask = (1 << index);
if (!device_props.emulate_depth_compare && m_prog.shadow_textures & mask)
{
if (m_shadow_sampled_textures & mask)
{
if (m_2d_sampled_textures & mask)
LOG_ERROR(RSX, "Texture unit %d is sampled as both a shadow texture and a depth texture", index);
else
samplerType = "sampler2DShadow";
}
}
vk::glsl::program_input in;
in.location = location;
in.domain = glsl::glsl_fragment_program;
in.name = PI.name;
in.type = vk::glsl::input_type_texture;
inputs.push_back(in);
OS << "layout(set=0, binding=" << location++ << ") uniform " << samplerType << " " << PI.name << ";\n";
if (m_prog.redirected_textures & mask)
{
// Insert stencil mirror declaration
in.name += "_stencil";
in.location = location;
inputs.push_back(in);
OS << "layout(set=0, binding=" << location++ << ") uniform u" << PT.type << " " << in.name << ";\n";
}
}
}
// Some drivers (macOS) do not support more than 16 texture descriptors per stage
// TODO: If any application requires more than this, the layout can be restructured a bit
verify("Too many sampler descriptors!" HERE), location <= VERTEX_TEXTURES_FIRST_BIND_SLOT;
std::string constants_block;
for (const ParamType& PT : m_parr.params[PF_PARAM_UNIFORM])
{
if (PT.type == "sampler1D" ||
PT.type == "sampler2D" ||
PT.type == "sampler3D" ||
PT.type == "samplerCube")
continue;
for (const ParamItem& PI : PT.items)
{
constants_block += " " + PT.type + " " + PI.name + ";\n";
}
}
if (!constants_block.empty())
{
OS << "layout(std140, set = 0, binding = 3) uniform FragmentConstantsBuffer\n";
OS << "{\n";
OS << constants_block;
OS << "};\n\n";
}
OS << "layout(std140, set = 0, binding = 4) uniform FragmentStateBuffer\n";
OS << "{\n";
OS << " float fog_param0;\n";
OS << " float fog_param1;\n";
OS << " uint rop_control;\n";
OS << " float alpha_ref;\n";
OS << " uint reserved;\n";
OS << " uint fog_mode;\n";
OS << " float wpos_scale;\n";
OS << " float wpos_bias;\n";
OS << "};\n\n";
OS << "layout(std140, set = 0, binding = 5) uniform TextureParametersBuffer\n";
OS << "{\n";
OS << " vec4 texture_parameters[16];\n";
OS << "};\n\n";
vk::glsl::program_input in;
in.location = FRAGMENT_CONSTANT_BUFFERS_BIND_SLOT;
in.domain = glsl::glsl_fragment_program;
in.name = "FragmentConstantsBuffer";
in.type = vk::glsl::input_type_uniform_buffer;
inputs.push_back(in);
in.location = FRAGMENT_STATE_BIND_SLOT;
in.name = "FragmentStateBuffer";
inputs.push_back(in);
in.location = FRAGMENT_TEXTURE_PARAMS_BIND_SLOT;
in.name = "TextureParametersBuffer";
inputs.push_back(in);
}
void VKFragmentDecompilerThread::insertGlobalFunctions(std::stringstream &OS)
{
glsl::shader_properties properties2;
properties2.domain = glsl::glsl_fragment_program;
properties2.require_lit_emulation = properties.has_lit_op;
properties2.require_depth_conversion = m_prog.redirected_textures != 0;
properties2.require_wpos = properties.has_wpos_input;
properties2.require_texture_ops = properties.has_tex_op;
properties2.emulate_shadow_compare = device_props.emulate_depth_compare;
properties2.low_precision_tests = vk::get_driver_vendor() == vk::driver_vendor::NVIDIA;
glsl::insert_glsl_legacy_function(OS, properties2);
}
void VKFragmentDecompilerThread::insertMainStart(std::stringstream & OS)
{
//TODO: Generate input mask during parse stage to avoid this
for (const ParamType& PT : m_parr.params[PF_PARAM_IN])
{
for (const ParamItem& PI : PT.items)
{
if (PI.name == "fogc")
{
glsl::insert_fog_declaration(OS);
break;
}
}
}
const std::set<std::string> output_values =
{
"r0", "r1", "r2", "r3", "r4",
"h0", "h2", "h4", "h6", "h8"
};
std::string parameters = "";
const auto half4 = getHalfTypeName(4);
for (auto ®_name : output_values)
{
const auto type = (reg_name[0] == 'r' || !device_props.has_native_half_support)? "vec4" : half4;
if (m_parr.HasParam(PF_PARAM_NONE, type, reg_name))
{
if (parameters.length())
parameters += ", ";
parameters += "inout " + type + " " + reg_name;
}
}
OS << "void fs_main(" << parameters << ")\n";
OS << "{\n";
for (const ParamType& PT : m_parr.params[PF_PARAM_NONE])
{
for (const ParamItem& PI : PT.items)
{
if (output_values.find(PI.name) != output_values.end())
continue;
OS << " " << PT.type << " " << PI.name;
if (!PI.value.empty())
OS << " = " << PI.value;
OS << ";\n";
}
}
if (m_parr.HasParam(PF_PARAM_IN, "vec4", "ssa"))
OS << " vec4 ssa = gl_FrontFacing ? vec4(1.) : vec4(-1.);\n";
if (properties.has_wpos_input)
OS << " vec4 wpos = get_wpos();\n";
bool two_sided_enabled = m_prog.front_back_color_enabled && (m_prog.back_color_diffuse_output || m_prog.back_color_specular_output);
//Some registers require redirection
for (const ParamType& PT : m_parr.params[PF_PARAM_IN])
{
for (const ParamItem& PI : PT.items)
{
if (two_sided_enabled)
{
if (PI.name == "spec_color")
{
//Only redirect/rename variables if the back_color exists
if (m_prog.back_color_specular_output)
{
if (m_prog.back_color_specular_output && m_prog.front_color_specular_output)
{
OS << " vec4 spec_color = gl_FrontFacing ? front_spec_color : back_spec_color;\n";
}
else
{
OS << " vec4 spec_color = back_spec_color;\n";
}
}
continue;
}
else if (PI.name == "diff_color")
{
//Only redirect/rename variables if the back_color exists
if (m_prog.back_color_diffuse_output)
{
if (m_prog.back_color_diffuse_output && m_prog.front_color_diffuse_output)
{
OS << " vec4 diff_color = gl_FrontFacing ? front_diff_color : back_diff_color;\n";
}
else
{
OS << " vec4 diff_color = back_diff_color;\n";
}
}
continue;
}
}
if (PI.name == "fogc")
{
OS << " vec4 fogc = fetch_fog_value(fog_mode);\n";
continue;
}
}
}
}
void VKFragmentDecompilerThread::insertMainEnd(std::stringstream & OS)
{
const std::set<std::string> output_values =
{
"r0", "r1", "r2", "r3", "r4",
"h0", "h2", "h4", "h6", "h8"
};
OS << "}\n\n";
OS << "void main()\n";
OS << "{\n";
std::string parameters = "";
const auto half4 = getHalfTypeName(4);
for (auto ®_name : output_values)
{
const std::string type = (reg_name[0] == 'r' || !device_props.has_native_half_support)? "vec4" : half4;
if (m_parr.HasParam(PF_PARAM_NONE, type, reg_name))
{
if (parameters.length())
parameters += ", ";
parameters += reg_name;
OS << " " << type << " " << reg_name << " = " << type << "(0.);\n";
}
}
OS << "\n" << " fs_main(" + parameters + ");\n\n";
glsl::insert_rop(OS, !!(m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS), device_props.has_native_half_support);
if (m_ctrl & CELL_GCM_SHADER_CONTROL_DEPTH_EXPORT)
{
if (m_parr.HasParam(PF_PARAM_NONE, "vec4", "r1"))
{
//Depth writes are always from a fp32 register. See issues section on nvidia's NV_fragment_program spec
//https://www.khronos.org/registry/OpenGL/extensions/NV/NV_fragment_program.txt
OS << " gl_FragDepth = r1.z;\n";
}
else
{
//Input not declared. Leave commented to assist in debugging the shader
OS << " //gl_FragDepth = r1.z;\n";
}
}
OS << "}\n";
}
void VKFragmentDecompilerThread::Task()
{
m_shader = Decompile();
vk_prog->SetInputs(inputs);
}
VKFragmentProgram::VKFragmentProgram()
{
}
VKFragmentProgram::~VKFragmentProgram()
{
Delete();
}
void VKFragmentProgram::Decompile(const RSXFragmentProgram& prog)
{
u32 size;
std::string source;
VKFragmentDecompilerThread decompiler(source, parr, prog, size, *this);
const auto pdev = vk::get_current_renderer();
if (!g_cfg.video.disable_native_float16)
{
decompiler.device_props.has_native_half_support = pdev->get_shader_types_support().allow_float16;
}
decompiler.device_props.emulate_depth_compare = !pdev->get_formats_support().d24_unorm_s8;
decompiler.Task();
shader.create(::glsl::program_domain::glsl_fragment_program, source);
for (const ParamType& PT : decompiler.m_parr.params[PF_PARAM_UNIFORM])
{
for (const ParamItem& PI : PT.items)
{
if (PT.type == "sampler1D" ||
PT.type == "sampler2D" ||
PT.type == "sampler3D" ||
PT.type == "samplerCube")
continue;
size_t offset = atoi(PI.name.c_str() + 2);
FragmentConstantOffsetCache.push_back(offset);
}
}
}
void VKFragmentProgram::Compile()
{
fs::file(fs::get_cache_dir() + "shaderlog/FragmentProgram" + std::to_string(id) + ".spirv", fs::rewrite).write(shader.get_source());
handle = shader.compile();
}
void VKFragmentProgram::Delete()
{
shader.destroy();
}
void VKFragmentProgram::SetInputs(std::vector<vk::glsl::program_input>& inputs)
{
for (auto &it : inputs)
{
uniforms.push_back(it);
}
}