summaryrefslogtreecommitdiffstats
path: root/src/video_core/shader/shader.cpp
blob: f7459e2ad6c0d30984140e1248e0b90685a60c94 (plain) (blame)
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
// Copyright 2015 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <memory>
#include <unordered_map>

#include "common/hash.h"
#include "common/make_unique.h"
#include "common/profiler.h"

#include "video_core/debug_utils/debug_utils.h"
#include "video_core/pica.h"
#include "video_core/video_core.h"

#include "shader.h"
#include "shader_interpreter.h"

#ifdef ARCHITECTURE_X64
#include "shader_jit_x64.h"
#endif // ARCHITECTURE_X64

namespace Pica {

namespace Shader {

#ifdef ARCHITECTURE_x86_64

static std::unordered_map<u64, CompiledShader*> shader_map;
static JitCompiler jit;
static CompiledShader* jit_shader;

#endif // ARCHITECTURE_x86_64

void Setup(UnitState& state) {
#ifdef ARCHITECTURE_x86_64
    if (VideoCore::g_shader_jit_enabled) {
        u64 cache_key = (Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^
            Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data)) ^
            g_state.regs.vs.main_offset);

        auto iter = shader_map.find(cache_key);
        if (iter != shader_map.end()) {
            jit_shader = iter->second;
        } else {
            jit_shader = jit.Compile();
            shader_map.emplace(cache_key, jit_shader);
        }
#endif // ARCHITECTURE_X64
}

void Shutdown() {
    shader_map.clear();
}

static Common::Profiling::TimingCategory shader_category("Vertex Shader");

OutputVertex Run(UnitState& state, const InputVertex& input, int num_attributes) {
    auto& config = g_state.regs.vs;
    auto& setup = g_state.vs;

    Common::Profiling::ScopeTimer timer(shader_category);

    state.program_counter = config.main_offset;
    state.debug.max_offset = 0;
    state.debug.max_opdesc_id = 0;

    // Setup input register table
    const auto& attribute_register_map = config.input_register_map;

    if (num_attributes > 0) state.input_registers[attribute_register_map.attribute0_register] = input.attr[0];
    if (num_attributes > 1) state.input_registers[attribute_register_map.attribute1_register] = input.attr[1];
    if (num_attributes > 2) state.input_registers[attribute_register_map.attribute2_register] = input.attr[2];
    if (num_attributes > 3) state.input_registers[attribute_register_map.attribute3_register] = input.attr[3];
    if (num_attributes > 4) state.input_registers[attribute_register_map.attribute4_register] = input.attr[4];
    if (num_attributes > 5) state.input_registers[attribute_register_map.attribute5_register] = input.attr[5];
    if (num_attributes > 6) state.input_registers[attribute_register_map.attribute6_register] = input.attr[6];
    if (num_attributes > 7) state.input_registers[attribute_register_map.attribute7_register] = input.attr[7];
    if (num_attributes > 8) state.input_registers[attribute_register_map.attribute8_register] = input.attr[8];
    if (num_attributes > 9) state.input_registers[attribute_register_map.attribute9_register] = input.attr[9];
    if (num_attributes > 10) state.input_registers[attribute_register_map.attribute10_register] = input.attr[10];
    if (num_attributes > 11) state.input_registers[attribute_register_map.attribute11_register] = input.attr[11];
    if (num_attributes > 12) state.input_registers[attribute_register_map.attribute12_register] = input.attr[12];
    if (num_attributes > 13) state.input_registers[attribute_register_map.attribute13_register] = input.attr[13];
    if (num_attributes > 14) state.input_registers[attribute_register_map.attribute14_register] = input.attr[14];
    if (num_attributes > 15) state.input_registers[attribute_register_map.attribute15_register] = input.attr[15];

    state.conditional_code[0] = false;
    state.conditional_code[1] = false;

#ifdef ARCHITECTURE_x86_64
    if (VideoCore::g_shader_jit_enabled)
        jit_shader(&state);
    else
        RunInterpreter(state);
#else
    RunInterpreter(state);
#endif // ARCHITECTURE_X64

#if PICA_DUMP_SHADERS
    DebugUtils::DumpShader(setup.program_code.data(), state.debug.max_offset, setup.swizzle_data.data(),
        state.debug.max_opdesc_id, config.main_offset,
        g_state.regs.vs_output_attributes); // TODO: Don't hardcode VS here
#endif

    // Setup output data
    OutputVertex ret;
    // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to
    // figure out what those circumstances are and enable the remaining outputs then.
    for (int i = 0; i < 7; ++i) {
        const auto& output_register_map = g_state.regs.vs_output_attributes[i]; // TODO: Don't hardcode VS here

        u32 semantics[4] = {
            output_register_map.map_x, output_register_map.map_y,
            output_register_map.map_z, output_register_map.map_w
        };

        for (int comp = 0; comp < 4; ++comp) {
            float24* out = ((float24*)&ret) + semantics[comp];
            if (semantics[comp] != Regs::VSOutputAttributes::INVALID) {
                *out = state.output_registers[i][comp];
            } else {
                // Zero output so that attributes which aren't output won't have denormals in them,
                // which would slow us down later.
                memset(out, 0, sizeof(*out));
            }
        }
    }

    // The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation
    for (int i = 0; i < 4; ++i) {
        ret.color[i] = float24::FromFloat32(
            std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
    }

    LOG_TRACE(Render_Software, "Output vertex: pos (%.2f, %.2f, %.2f, %.2f), col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f)",
        ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(),
        ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(),
        ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32());

    return ret;
}

} // namespace Shader

} // namespace Pica