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
|
// Copyright 2015 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "video_core/pica.h"
#include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/gl_shader_gen.h"
using Pica::Regs;
using TevStageConfig = Regs::TevStageConfig;
namespace GLShader {
/// Detects if a TEV stage is configured to be skipped (to avoid generating unnecessary code)
static bool IsPassThroughTevStage(const TevStageConfig& stage) {
return (stage.color_op == TevStageConfig::Operation::Replace &&
stage.alpha_op == TevStageConfig::Operation::Replace &&
stage.color_source1 == TevStageConfig::Source::Previous &&
stage.alpha_source1 == TevStageConfig::Source::Previous &&
stage.color_modifier1 == TevStageConfig::ColorModifier::SourceColor &&
stage.alpha_modifier1 == TevStageConfig::AlphaModifier::SourceAlpha &&
stage.GetColorMultiplier() == 1 &&
stage.GetAlphaMultiplier() == 1);
}
/// Writes the specified TEV stage source component(s)
static void AppendSource(std::string& out, TevStageConfig::Source source,
const std::string& index_name) {
using Source = TevStageConfig::Source;
switch (source) {
case Source::PrimaryColor:
out += "primary_color";
break;
case Source::PrimaryFragmentColor:
out += "primary_fragment_color";
break;
case Source::SecondaryFragmentColor:
// HACK: Until we implement fragment lighting, use zero
out += "vec4(0.0)";
break;
case Source::Texture0:
out += "texture(tex[0], texcoord[0])";
break;
case Source::Texture1:
out += "texture(tex[1], texcoord[1])";
break;
case Source::Texture2:
out += "texture(tex[2], texcoord[2])";
break;
case Source::PreviousBuffer:
out += "combiner_buffer";
break;
case Source::Constant:
((out += "const_color[") += index_name) += ']';
break;
case Source::Previous:
out += "last_tex_env_out";
break;
default:
out += "vec4(0.0)";
LOG_CRITICAL(Render_OpenGL, "Unknown source op %u", source);
break;
}
}
/// Writes the color components to use for the specified TEV stage color modifier
static void AppendColorModifier(std::string& out, TevStageConfig::ColorModifier modifier,
TevStageConfig::Source source, const std::string& index_name) {
using ColorModifier = TevStageConfig::ColorModifier;
switch (modifier) {
case ColorModifier::SourceColor:
AppendSource(out, source, index_name);
out += ".rgb";
break;
case ColorModifier::OneMinusSourceColor:
out += "vec3(1.0) - ";
AppendSource(out, source, index_name);
out += ".rgb";
break;
case ColorModifier::SourceAlpha:
AppendSource(out, source, index_name);
out += ".aaa";
break;
case ColorModifier::OneMinusSourceAlpha:
out += "vec3(1.0) - ";
AppendSource(out, source, index_name);
out += ".aaa";
break;
case ColorModifier::SourceRed:
AppendSource(out, source, index_name);
out += ".rrr";
break;
case ColorModifier::OneMinusSourceRed:
out += "vec3(1.0) - ";
AppendSource(out, source, index_name);
out += ".rrr";
break;
case ColorModifier::SourceGreen:
AppendSource(out, source, index_name);
out += ".ggg";
break;
case ColorModifier::OneMinusSourceGreen:
out += "vec3(1.0) - ";
AppendSource(out, source, index_name);
out += ".ggg";
break;
case ColorModifier::SourceBlue:
AppendSource(out, source, index_name);
out += ".bbb";
break;
case ColorModifier::OneMinusSourceBlue:
out += "vec3(1.0) - ";
AppendSource(out, source, index_name);
out += ".bbb";
break;
default:
out += "vec3(0.0)";
LOG_CRITICAL(Render_OpenGL, "Unknown color modifier op %u", modifier);
break;
}
}
/// Writes the alpha component to use for the specified TEV stage alpha modifier
static void AppendAlphaModifier(std::string& out, TevStageConfig::AlphaModifier modifier,
TevStageConfig::Source source, const std::string& index_name) {
using AlphaModifier = TevStageConfig::AlphaModifier;
switch (modifier) {
case AlphaModifier::SourceAlpha:
AppendSource(out, source, index_name);
out += ".a";
break;
case AlphaModifier::OneMinusSourceAlpha:
out += "1.0 - ";
AppendSource(out, source, index_name);
out += ".a";
break;
case AlphaModifier::SourceRed:
AppendSource(out, source, index_name);
out += ".r";
break;
case AlphaModifier::OneMinusSourceRed:
out += "1.0 - ";
AppendSource(out, source, index_name);
out += ".r";
break;
case AlphaModifier::SourceGreen:
AppendSource(out, source, index_name);
out += ".g";
break;
case AlphaModifier::OneMinusSourceGreen:
out += "1.0 - ";
AppendSource(out, source, index_name);
out += ".g";
break;
case AlphaModifier::SourceBlue:
AppendSource(out, source, index_name);
out += ".b";
break;
case AlphaModifier::OneMinusSourceBlue:
out += "1.0 - ";
AppendSource(out, source, index_name);
out += ".b";
break;
default:
out += "0.0";
LOG_CRITICAL(Render_OpenGL, "Unknown alpha modifier op %u", modifier);
break;
}
}
/// Writes the combiner function for the color components for the specified TEV stage operation
static void AppendColorCombiner(std::string& out, TevStageConfig::Operation operation,
const std::string& variable_name) {
out += "clamp(";
using Operation = TevStageConfig::Operation;
switch (operation) {
case Operation::Replace:
out += variable_name + "[0]";
break;
case Operation::Modulate:
out += variable_name + "[0] * " + variable_name + "[1]";
break;
case Operation::Add:
out += variable_name + "[0] + " + variable_name + "[1]";
break;
case Operation::AddSigned:
out += variable_name + "[0] + " + variable_name + "[1] - vec3(0.5)";
break;
case Operation::Lerp:
// TODO(bunnei): Verify if HW actually does this per-component, otherwise we can just use builtin lerp
out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (vec3(1.0) - " + variable_name + "[2])";
break;
case Operation::Subtract:
out += variable_name + "[0] - " + variable_name + "[1]";
break;
case Operation::MultiplyThenAdd:
out += variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2]";
break;
case Operation::AddThenMultiply:
out += "min(" + variable_name + "[0] + " + variable_name + "[1], vec3(1.0)) * " + variable_name + "[2]";
break;
default:
out += "vec3(0.0)";
LOG_CRITICAL(Render_OpenGL, "Unknown color combiner operation: %u", operation);
break;
}
out += ", vec3(0.0), vec3(1.0))"; // Clamp result to 0.0, 1.0
}
/// Writes the combiner function for the alpha component for the specified TEV stage operation
static void AppendAlphaCombiner(std::string& out, TevStageConfig::Operation operation,
const std::string& variable_name) {
out += "clamp(";
using Operation = TevStageConfig::Operation;
switch (operation) {
case Operation::Replace:
out += variable_name + "[0]";
break;
case Operation::Modulate:
out += variable_name + "[0] * " + variable_name + "[1]";
break;
case Operation::Add:
out += variable_name + "[0] + " + variable_name + "[1]";
break;
case Operation::AddSigned:
out += variable_name + "[0] + " + variable_name + "[1] - 0.5";
break;
case Operation::Lerp:
out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (1.0 - " + variable_name + "[2])";
break;
case Operation::Subtract:
out += variable_name + "[0] - " + variable_name + "[1]";
break;
case Operation::MultiplyThenAdd:
out += variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2]";
break;
case Operation::AddThenMultiply:
out += "min(" + variable_name + "[0] + " + variable_name + "[1], 1.0) * " + variable_name + "[2]";
break;
default:
out += "0.0";
LOG_CRITICAL(Render_OpenGL, "Unknown alpha combiner operation: %u", operation);
break;
}
out += ", 0.0, 1.0)";
}
/// Writes the if-statement condition used to evaluate alpha testing
static void AppendAlphaTestCondition(std::string& out, Regs::CompareFunc func) {
using CompareFunc = Regs::CompareFunc;
switch (func) {
case CompareFunc::Never:
out += "true";
break;
case CompareFunc::Always:
out += "false";
break;
case CompareFunc::Equal:
case CompareFunc::NotEqual:
case CompareFunc::LessThan:
case CompareFunc::LessThanOrEqual:
case CompareFunc::GreaterThan:
case CompareFunc::GreaterThanOrEqual:
{
static const char* op[] = { "!=", "==", ">=", ">", "<=", "<", };
unsigned index = (unsigned)func - (unsigned)CompareFunc::Equal;
out += "int(last_tex_env_out.a * 255.0f) " + std::string(op[index]) + " alphatest_ref";
break;
}
default:
out += "false";
LOG_CRITICAL(Render_OpenGL, "Unknown alpha test condition %u", func);
break;
}
}
/// Writes the code to emulate the specified TEV stage
static void WriteTevStage(std::string& out, const PicaShaderConfig& config, unsigned index) {
auto& stage = config.tev_stages[index];
if (!IsPassThroughTevStage(stage)) {
std::string index_name = std::to_string(index);
out += "vec3 color_results_" + index_name + "[3] = vec3[3](";
AppendColorModifier(out, stage.color_modifier1, stage.color_source1, index_name);
out += ", ";
AppendColorModifier(out, stage.color_modifier2, stage.color_source2, index_name);
out += ", ";
AppendColorModifier(out, stage.color_modifier3, stage.color_source3, index_name);
out += ");\n";
out += "vec3 color_output_" + index_name + " = ";
AppendColorCombiner(out, stage.color_op, "color_results_" + index_name);
out += ";\n";
out += "float alpha_results_" + index_name + "[3] = float[3](";
AppendAlphaModifier(out, stage.alpha_modifier1, stage.alpha_source1, index_name);
out += ", ";
AppendAlphaModifier(out, stage.alpha_modifier2, stage.alpha_source2, index_name);
out += ", ";
AppendAlphaModifier(out, stage.alpha_modifier3, stage.alpha_source3, index_name);
out += ");\n";
out += "float alpha_output_" + index_name + " = ";
AppendAlphaCombiner(out, stage.alpha_op, "alpha_results_" + index_name);
out += ";\n";
out += "last_tex_env_out = vec4("
"clamp(color_output_" + index_name + " * " + std::to_string(stage.GetColorMultiplier()) + ".0, vec3(0.0), vec3(1.0)),"
"clamp(alpha_output_" + index_name + " * " + std::to_string(stage.GetAlphaMultiplier()) + ".0, 0.0, 1.0));\n";
}
out += "combiner_buffer = next_combiner_buffer;\n";
if (config.TevStageUpdatesCombinerBufferColor(index))
out += "next_combiner_buffer.rgb = last_tex_env_out.rgb;\n";
if (config.TevStageUpdatesCombinerBufferAlpha(index))
out += "next_combiner_buffer.a = last_tex_env_out.a;\n";
}
std::string GenerateFragmentShader(const PicaShaderConfig& config) {
std::string out = R"(
#version 330 core
#define NUM_TEV_STAGES 6
#define NUM_LIGHTS 8
#define LIGHTING_LUT_SIZE 256
in vec4 primary_color;
in vec2 texcoord[3];
in vec4 normquat;
in vec3 view;
out vec4 color;
struct LightSrc {
vec3 diffuse;
vec3 ambient;
vec3 position;
};
layout (std140) uniform shader_data {
vec4 const_color[NUM_TEV_STAGES];
vec4 tev_combiner_buffer_color;
int alphatest_ref;
float depth_offset;
vec3 lighting_global_ambient;
LightSrc light_src[NUM_LIGHTS];
vec4 lighting_lut_0[LIGHTING_LUT_SIZE];
vec4 lighting_lut_1[LIGHTING_LUT_SIZE];
vec4 lighting_lut_2[LIGHTING_LUT_SIZE];
vec4 lighting_lut_3[LIGHTING_LUT_SIZE];
vec4 lighting_lut_4[LIGHTING_LUT_SIZE];
vec4 lighting_lut_5[LIGHTING_LUT_SIZE];
};
uniform sampler2D tex[3];
void main() {
vec4 primary_fragment_color = vec4(0.0);
)";
if (config.lighting_enabled) {
out += "vec3 normal = normalize(vec3(\n";
out += " 2.f*(normquat.x*normquat.z + normquat.y*normquat.w),\n";
out += " 2.f*(normquat.y*normquat.z + normquat.x*normquat.w),\n";
out += " 1.f - 2.f*(normquat.x*normquat.x + normquat.y*normquat.y)));\n";
out += "vec4 secondary_color = vec4(0.0);\n";
out += "vec3 diffuse_sum = vec3(0.0);\n";
out += "vec3 fragment_position = -view;\n";
for (unsigned light_index = 0; light_index < config.num_lights; ++light_index) {
unsigned num = config.light_src[light_index].num;
std::string light_src = "light_src[" + std::to_string(num) + "]";
std::string light_vector;
if (config.light_src[light_index].directional)
light_vector = "normalize(-" + light_src + ".position)";
else
light_vector = "normalize(" + light_src + ".position - fragment_position)";
std::string dot_product;
if (config.light_src[light_index].two_sided_diffuse)
dot_product = "abs(dot(" + light_vector + ", normal))";
else
dot_product = "max(dot(" + light_vector + ", normal), 0.0)";
std::string dist_atten = "1.0";
if (config.light_src[light_index].dist_atten_enabled) {
std::string scale = std::to_string(config.light_src[light_index].dist_atten_scale);
std::string bias = std::to_string(config.light_src[light_index].dist_atten_bias);
std::string lut_index = "(" + scale + " * length(fragment_position - " + light_src + ".position) + " + bias + ")";
std::string clamped_lut_index = "((clamp(int(" + lut_index + " * 256.0), 0, 255)))";
unsigned lut_num = ((unsigned)Regs::LightingSampler::DistanceAttenuation + num);
dist_atten = "lighting_lut_" + std::to_string(lut_num /4) + "[" + clamped_lut_index + "][" + std::to_string(lut_num & 3) + "]";
}
out += "diffuse_sum += ((light_src[" + std::to_string(num) + "].diffuse * " + dot_product + ") + light_src[" + std::to_string(num) + "].ambient) * " + dist_atten + ";\n";
}
out += "diffuse_sum += lighting_global_ambient;\n";
out += "primary_fragment_color = vec4(clamp(diffuse_sum, vec3(0.0), vec3(1.0)), 1.0);\n";
}
// Do not do any sort of processing if it's obvious we're not going to pass the alpha test
if (config.alpha_test_func == Regs::CompareFunc::Never) {
out += "discard; }";
return out;
}
out += "vec4 combiner_buffer = vec4(0.0);\n";
out += "vec4 next_combiner_buffer = tev_combiner_buffer_color;\n";
out += "vec4 last_tex_env_out = vec4(0.0);\n";
for (size_t index = 0; index < config.tev_stages.size(); ++index)
WriteTevStage(out, config, (unsigned)index);
if (config.alpha_test_func != Regs::CompareFunc::Always) {
out += "if (";
AppendAlphaTestCondition(out, config.alpha_test_func);
out += ") discard;\n";
}
out += "color = last_tex_env_out;\n";
out += "gl_FragDepth = gl_FragCoord.z + depth_offset;\n}";
return out;
}
std::string GenerateVertexShader() {
std::string out = "#version 330 core\n";
out += "layout(location = " + std::to_string((int)ATTRIBUTE_POSITION) + ") in vec4 vert_position;\n";
out += "layout(location = " + std::to_string((int)ATTRIBUTE_COLOR) + ") in vec4 vert_color;\n";
out += "layout(location = " + std::to_string((int)ATTRIBUTE_TEXCOORD0) + ") in vec2 vert_texcoord0;\n";
out += "layout(location = " + std::to_string((int)ATTRIBUTE_TEXCOORD1) + ") in vec2 vert_texcoord1;\n";
out += "layout(location = " + std::to_string((int)ATTRIBUTE_TEXCOORD2) + ") in vec2 vert_texcoord2;\n";
out += "layout(location = " + std::to_string((int)ATTRIBUTE_NORMQUAT) + ") in vec4 vert_normquat;\n";
out += "layout(location = " + std::to_string((int)ATTRIBUTE_VIEW) + ") in vec3 vert_view;\n";
out += R"(
out vec4 primary_color;
out vec2 texcoord[3];
out vec4 normquat;
out vec3 view;
void main() {
primary_color = vert_color;
texcoord[0] = vert_texcoord0;
texcoord[1] = vert_texcoord1;
texcoord[2] = vert_texcoord2;
normquat = vert_normquat;
view = vert_view;
gl_Position = vec4(vert_position.x, vert_position.y, -vert_position.z, vert_position.w);
}
)";
return out;
}
} // namespace GLShader
|