diff options
| author | 2015-08-15 18:26:12 -0400 | |
|---|---|---|
| committer | 2015-08-15 18:26:12 -0400 | |
| commit | d852c4ecc71f800b552978362947a5eb5a524694 (patch) | |
| tree | 9ea9a86da4027126914e69b12c24d2849fdb2c2d /src/video_core/shader/shader.cpp | |
| parent | Merge pull request #1027 from lioncash/debugger (diff) | |
| parent | Shader: Use a POD struct for registers. (diff) | |
| download | yuzu-d852c4ecc71f800b552978362947a5eb5a524694.tar.gz yuzu-d852c4ecc71f800b552978362947a5eb5a524694.tar.xz yuzu-d852c4ecc71f800b552978362947a5eb5a524694.zip | |
Merge pull request #1002 from bunnei/shader-jit
Vertex Shader JIT for X86-64
Diffstat (limited to 'src/video_core/shader/shader.cpp')
| -rw-r--r-- | src/video_core/shader/shader.cpp | 145 |
1 files changed, 145 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..6a27a8015 --- /dev/null +++ b/src/video_core/shader/shader.cpp | |||
| @@ -0,0 +1,145 @@ | |||
| 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 <memory> | ||
| 6 | #include <unordered_map> | ||
| 7 | |||
| 8 | #include "common/hash.h" | ||
| 9 | #include "common/make_unique.h" | ||
| 10 | #include "common/profiler.h" | ||
| 11 | |||
| 12 | #include "video_core/debug_utils/debug_utils.h" | ||
| 13 | #include "video_core/pica.h" | ||
| 14 | #include "video_core/video_core.h" | ||
| 15 | |||
| 16 | #include "shader.h" | ||
| 17 | #include "shader_interpreter.h" | ||
| 18 | |||
| 19 | #ifdef ARCHITECTURE_x86_64 | ||
| 20 | #include "shader_jit_x64.h" | ||
| 21 | #endif // ARCHITECTURE_x86_64 | ||
| 22 | |||
| 23 | namespace Pica { | ||
| 24 | |||
| 25 | namespace Shader { | ||
| 26 | |||
| 27 | #ifdef ARCHITECTURE_x86_64 | ||
| 28 | static std::unordered_map<u64, CompiledShader*> shader_map; | ||
| 29 | static JitCompiler jit; | ||
| 30 | static CompiledShader* jit_shader; | ||
| 31 | #endif // ARCHITECTURE_x86_64 | ||
| 32 | |||
| 33 | void Setup(UnitState& state) { | ||
| 34 | #ifdef ARCHITECTURE_x86_64 | ||
| 35 | if (VideoCore::g_shader_jit_enabled) { | ||
| 36 | u64 cache_key = (Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^ | ||
| 37 | Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data)) ^ | ||
| 38 | g_state.regs.vs.main_offset); | ||
| 39 | |||
| 40 | auto iter = shader_map.find(cache_key); | ||
| 41 | if (iter != shader_map.end()) { | ||
| 42 | jit_shader = iter->second; | ||
| 43 | } else { | ||
| 44 | jit_shader = jit.Compile(); | ||
| 45 | shader_map.emplace(cache_key, jit_shader); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | #endif // ARCHITECTURE_x86_64 | ||
| 49 | } | ||
| 50 | |||
| 51 | void Shutdown() { | ||
| 52 | shader_map.clear(); | ||
| 53 | } | ||
| 54 | |||
| 55 | static Common::Profiling::TimingCategory shader_category("Vertex Shader"); | ||
| 56 | |||
| 57 | OutputVertex Run(UnitState& state, const InputVertex& input, int num_attributes) { | ||
| 58 | auto& config = g_state.regs.vs; | ||
| 59 | auto& setup = g_state.vs; | ||
| 60 | |||
| 61 | Common::Profiling::ScopeTimer timer(shader_category); | ||
| 62 | |||
| 63 | state.program_counter = config.main_offset; | ||
| 64 | state.debug.max_offset = 0; | ||
| 65 | state.debug.max_opdesc_id = 0; | ||
| 66 | |||
| 67 | // Setup input register table | ||
| 68 | const auto& attribute_register_map = config.input_register_map; | ||
| 69 | |||
| 70 | if (num_attributes > 0) state.registers.input[attribute_register_map.attribute0_register] = input.attr[0]; | ||
| 71 | if (num_attributes > 1) state.registers.input[attribute_register_map.attribute1_register] = input.attr[1]; | ||
| 72 | if (num_attributes > 2) state.registers.input[attribute_register_map.attribute2_register] = input.attr[2]; | ||
| 73 | if (num_attributes > 3) state.registers.input[attribute_register_map.attribute3_register] = input.attr[3]; | ||
| 74 | if (num_attributes > 4) state.registers.input[attribute_register_map.attribute4_register] = input.attr[4]; | ||
| 75 | if (num_attributes > 5) state.registers.input[attribute_register_map.attribute5_register] = input.attr[5]; | ||
| 76 | if (num_attributes > 6) state.registers.input[attribute_register_map.attribute6_register] = input.attr[6]; | ||
| 77 | if (num_attributes > 7) state.registers.input[attribute_register_map.attribute7_register] = input.attr[7]; | ||
| 78 | if (num_attributes > 8) state.registers.input[attribute_register_map.attribute8_register] = input.attr[8]; | ||
| 79 | if (num_attributes > 9) state.registers.input[attribute_register_map.attribute9_register] = input.attr[9]; | ||
| 80 | if (num_attributes > 10) state.registers.input[attribute_register_map.attribute10_register] = input.attr[10]; | ||
| 81 | if (num_attributes > 11) state.registers.input[attribute_register_map.attribute11_register] = input.attr[11]; | ||
| 82 | if (num_attributes > 12) state.registers.input[attribute_register_map.attribute12_register] = input.attr[12]; | ||
| 83 | if (num_attributes > 13) state.registers.input[attribute_register_map.attribute13_register] = input.attr[13]; | ||
| 84 | if (num_attributes > 14) state.registers.input[attribute_register_map.attribute14_register] = input.attr[14]; | ||
| 85 | if (num_attributes > 15) state.registers.input[attribute_register_map.attribute15_register] = input.attr[15]; | ||
| 86 | |||
| 87 | state.conditional_code[0] = false; | ||
| 88 | state.conditional_code[1] = false; | ||
| 89 | |||
| 90 | #ifdef ARCHITECTURE_x86_64 | ||
| 91 | if (VideoCore::g_shader_jit_enabled) | ||
| 92 | jit_shader(&state.registers); | ||
| 93 | else | ||
| 94 | RunInterpreter(state); | ||
| 95 | #else | ||
| 96 | RunInterpreter(state); | ||
| 97 | #endif // ARCHITECTURE_x86_64 | ||
| 98 | |||
| 99 | #if PICA_DUMP_SHADERS | ||
| 100 | DebugUtils::DumpShader(setup.program_code.data(), state.debug.max_offset, setup.swizzle_data.data(), | ||
| 101 | state.debug.max_opdesc_id, config.main_offset, | ||
| 102 | g_state.regs.vs_output_attributes); // TODO: Don't hardcode VS here | ||
| 103 | #endif | ||
| 104 | |||
| 105 | // Setup output data | ||
| 106 | OutputVertex ret; | ||
| 107 | // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to | ||
| 108 | // figure out what those circumstances are and enable the remaining outputs then. | ||
| 109 | for (int i = 0; i < 7; ++i) { | ||
| 110 | const auto& output_register_map = g_state.regs.vs_output_attributes[i]; // TODO: Don't hardcode VS here | ||
| 111 | |||
| 112 | u32 semantics[4] = { | ||
| 113 | output_register_map.map_x, output_register_map.map_y, | ||
| 114 | output_register_map.map_z, output_register_map.map_w | ||
| 115 | }; | ||
| 116 | |||
| 117 | for (int comp = 0; comp < 4; ++comp) { | ||
| 118 | float24* out = ((float24*)&ret) + semantics[comp]; | ||
| 119 | if (semantics[comp] != Regs::VSOutputAttributes::INVALID) { | ||
| 120 | *out = state.registers.output[i][comp]; | ||
| 121 | } else { | ||
| 122 | // Zero output so that attributes which aren't output won't have denormals in them, | ||
| 123 | // which would slow us down later. | ||
| 124 | memset(out, 0, sizeof(*out)); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | // The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation | ||
| 130 | for (int i = 0; i < 4; ++i) { | ||
| 131 | ret.color[i] = float24::FromFloat32( | ||
| 132 | std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f)); | ||
| 133 | } | ||
| 134 | |||
| 135 | LOG_TRACE(Render_Software, "Output vertex: pos (%.2f, %.2f, %.2f, %.2f), col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f)", | ||
| 136 | ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(), | ||
| 137 | ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(), | ||
| 138 | ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32()); | ||
| 139 | |||
| 140 | return ret; | ||
| 141 | } | ||
| 142 | |||
| 143 | } // namespace Shader | ||
| 144 | |||
| 145 | } // namespace Pica | ||