summaryrefslogtreecommitdiff
path: root/src/video_core/shader/shader.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2015-07-21 19:38:59 -0400
committerGravatar bunnei2015-08-15 17:33:44 -0400
commit3f69c2039de1c3d084ac2c9eb0aa9315490346bf (patch)
tree743f6bae0c3f1d475eabb083335ad7d6377bb97e /src/video_core/shader/shader.cpp
parentShader: Move shader code to its own subdirectory, "shader". (diff)
downloadyuzu-3f69c2039de1c3d084ac2c9eb0aa9315490346bf.tar.gz
yuzu-3f69c2039de1c3d084ac2c9eb0aa9315490346bf.tar.xz
yuzu-3f69c2039de1c3d084ac2c9eb0aa9315490346bf.zip
Shader: Define a common interface for running vertex shader programs.
Diffstat (limited to 'src/video_core/shader/shader.cpp')
-rw-r--r--src/video_core/shader/shader.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/video_core/shader/shader.cpp b/src/video_core/shader/shader.cpp
new file mode 100644
index 000000000..e397e8e03
--- /dev/null
+++ b/src/video_core/shader/shader.cpp
@@ -0,0 +1,105 @@
1// Copyright 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/logging/log.h"
6#include "common/profiler.h"
7
8#include "video_core/debug_utils/debug_utils.h"
9#include "video_core/pica.h"
10
11#include "shader.h"
12#include "shader_interpreter.h"
13
14namespace Pica {
15
16namespace Shader {
17
18void Setup(UnitState& state) {
19 // TODO(bunnei): This will be used by the JIT in a subsequent commit
20}
21
22static Common::Profiling::TimingCategory shader_category("Vertex Shader");
23
24OutputVertex Run(UnitState& state, const InputVertex& input, int num_attributes) {
25 auto& config = g_state.regs.vs;
26 auto& setup = g_state.vs;
27
28 Common::Profiling::ScopeTimer timer(shader_category);
29
30 state.program_counter = config.main_offset;
31 state.debug.max_offset = 0;
32 state.debug.max_opdesc_id = 0;
33
34 // Setup input register table
35 const auto& attribute_register_map = config.input_register_map;
36
37 if (num_attributes > 0) state.input_registers[attribute_register_map.attribute0_register] = input.attr[0];
38 if (num_attributes > 1) state.input_registers[attribute_register_map.attribute1_register] = input.attr[1];
39 if (num_attributes > 2) state.input_registers[attribute_register_map.attribute2_register] = input.attr[2];
40 if (num_attributes > 3) state.input_registers[attribute_register_map.attribute3_register] = input.attr[3];
41 if (num_attributes > 4) state.input_registers[attribute_register_map.attribute4_register] = input.attr[4];
42 if (num_attributes > 5) state.input_registers[attribute_register_map.attribute5_register] = input.attr[5];
43 if (num_attributes > 6) state.input_registers[attribute_register_map.attribute6_register] = input.attr[6];
44 if (num_attributes > 7) state.input_registers[attribute_register_map.attribute7_register] = input.attr[7];
45 if (num_attributes > 8) state.input_registers[attribute_register_map.attribute8_register] = input.attr[8];
46 if (num_attributes > 9) state.input_registers[attribute_register_map.attribute9_register] = input.attr[9];
47 if (num_attributes > 10) state.input_registers[attribute_register_map.attribute10_register] = input.attr[10];
48 if (num_attributes > 11) state.input_registers[attribute_register_map.attribute11_register] = input.attr[11];
49 if (num_attributes > 12) state.input_registers[attribute_register_map.attribute12_register] = input.attr[12];
50 if (num_attributes > 13) state.input_registers[attribute_register_map.attribute13_register] = input.attr[13];
51 if (num_attributes > 14) state.input_registers[attribute_register_map.attribute14_register] = input.attr[14];
52 if (num_attributes > 15) state.input_registers[attribute_register_map.attribute15_register] = input.attr[15];
53
54 state.conditional_code[0] = false;
55 state.conditional_code[1] = false;
56
57 RunInterpreter(state);
58
59#if PICA_DUMP_SHADERS
60 DebugUtils::DumpShader(setup.program_code.data(), state.debug.max_offset, setup.swizzle_data.data(),
61 state.debug.max_opdesc_id, config.main_offset,
62 g_state.regs.vs_output_attributes); // TODO: Don't hardcode VS here
63#endif
64
65 // Setup output data
66 OutputVertex ret;
67 // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to
68 // figure out what those circumstances are and enable the remaining outputs then.
69 for (int i = 0; i < 7; ++i) {
70 const auto& output_register_map = g_state.regs.vs_output_attributes[i]; // TODO: Don't hardcode VS here
71
72 u32 semantics[4] = {
73 output_register_map.map_x, output_register_map.map_y,
74 output_register_map.map_z, output_register_map.map_w
75 };
76
77 for (int comp = 0; comp < 4; ++comp) {
78 float24* out = ((float24*)&ret) + semantics[comp];
79 if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
80 *out = state.output_registers[i][comp];
81 } else {
82 // Zero output so that attributes which aren't output won't have denormals in them,
83 // which would slow us down later.
84 memset(out, 0, sizeof(*out));
85 }
86 }
87 }
88
89 // The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation
90 for (int i = 0; i < 4; ++i) {
91 ret.color[i] = float24::FromFloat32(
92 std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
93 }
94
95 LOG_TRACE(Render_Software, "Output vertex: pos (%.2f, %.2f, %.2f, %.2f), col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f)",
96 ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
97 ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
98 ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32());
99
100 return ret;
101}
102
103} // namespace Shader
104
105} // namespace Pica