2015-07-23 05:25:30 +02:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
2016-03-18 00:45:09 +01:00
|
|
|
#include <utility>
|
2016-04-13 05:24:34 +02:00
|
|
|
#include <vector>
|
2015-07-23 05:25:30 +02:00
|
|
|
#include <nihstro/shader_bytecode.h>
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "common/bit_set.h"
|
|
|
|
#include "common/common_types.h"
|
2015-08-12 06:00:44 +02:00
|
|
|
#include "common/x64/emitter.h"
|
2015-09-11 13:20:02 +02:00
|
|
|
#include "video_core/shader/shader.h"
|
2015-07-23 05:25:30 +02:00
|
|
|
|
|
|
|
using nihstro::Instruction;
|
|
|
|
using nihstro::OpCode;
|
|
|
|
using nihstro::SwizzlePattern;
|
|
|
|
|
|
|
|
namespace Pica {
|
|
|
|
|
|
|
|
namespace Shader {
|
|
|
|
|
2016-03-20 05:37:05 +01:00
|
|
|
/// Memory allocated for each compiled shader (64Kb)
|
|
|
|
constexpr size_t MAX_SHADER_SIZE = 1024 * 64;
|
2016-03-12 18:15:49 +01:00
|
|
|
|
2015-07-23 05:25:30 +02:00
|
|
|
/**
|
|
|
|
* This class implements the shader JIT compiler. It recompiles a Pica shader program into x86_64
|
|
|
|
* code that can be executed on the host machine directly.
|
|
|
|
*/
|
2016-04-13 05:34:03 +02:00
|
|
|
class JitShader : public Gen::XCodeBlock {
|
2015-07-23 05:25:30 +02:00
|
|
|
public:
|
2016-04-13 05:34:03 +02:00
|
|
|
JitShader();
|
2015-07-23 05:25:30 +02:00
|
|
|
|
2016-05-13 08:46:14 +02:00
|
|
|
void Run(const ShaderSetup& setup, UnitState<false>& state, unsigned offset) const {
|
|
|
|
program(&setup, &state, code_ptr[offset]);
|
2016-03-20 05:37:05 +01:00
|
|
|
}
|
2015-07-23 05:25:30 +02:00
|
|
|
|
2016-03-20 05:37:05 +01:00
|
|
|
void Compile();
|
2015-07-23 05:25:30 +02:00
|
|
|
|
|
|
|
void Compile_ADD(Instruction instr);
|
|
|
|
void Compile_DP3(Instruction instr);
|
|
|
|
void Compile_DP4(Instruction instr);
|
2015-08-21 12:49:21 +02:00
|
|
|
void Compile_DPH(Instruction instr);
|
2015-08-16 17:22:49 +02:00
|
|
|
void Compile_EX2(Instruction instr);
|
|
|
|
void Compile_LG2(Instruction instr);
|
2015-07-23 05:25:30 +02:00
|
|
|
void Compile_MUL(Instruction instr);
|
2015-08-19 14:23:53 +02:00
|
|
|
void Compile_SGE(Instruction instr);
|
|
|
|
void Compile_SLT(Instruction instr);
|
2015-07-23 05:25:30 +02:00
|
|
|
void Compile_FLR(Instruction instr);
|
|
|
|
void Compile_MAX(Instruction instr);
|
|
|
|
void Compile_MIN(Instruction instr);
|
|
|
|
void Compile_RCP(Instruction instr);
|
|
|
|
void Compile_RSQ(Instruction instr);
|
|
|
|
void Compile_MOVA(Instruction instr);
|
|
|
|
void Compile_MOV(Instruction instr);
|
|
|
|
void Compile_NOP(Instruction instr);
|
|
|
|
void Compile_END(Instruction instr);
|
|
|
|
void Compile_CALL(Instruction instr);
|
|
|
|
void Compile_CALLC(Instruction instr);
|
|
|
|
void Compile_CALLU(Instruction instr);
|
|
|
|
void Compile_IF(Instruction instr);
|
|
|
|
void Compile_LOOP(Instruction instr);
|
|
|
|
void Compile_JMP(Instruction instr);
|
|
|
|
void Compile_CMP(Instruction instr);
|
|
|
|
void Compile_MAD(Instruction instr);
|
|
|
|
|
|
|
|
private:
|
2016-01-24 11:10:56 +01:00
|
|
|
void Compile_Block(unsigned end);
|
2016-03-18 00:45:09 +01:00
|
|
|
void Compile_NextInstr();
|
2015-07-23 05:25:30 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
void Compile_SwizzleSrc(Instruction instr, unsigned src_num, SourceRegister src_reg,
|
|
|
|
Gen::X64Reg dest);
|
2015-07-23 05:25:30 +02:00
|
|
|
void Compile_DestEnable(Instruction instr, Gen::X64Reg dest);
|
|
|
|
|
2015-08-24 06:48:15 +02:00
|
|
|
/**
|
|
|
|
* Compiles a `MUL src1, src2` operation, properly handling the PICA semantics when multiplying
|
|
|
|
* zero by inf. Clobbers `src2` and `scratch`.
|
|
|
|
*/
|
|
|
|
void Compile_SanitizedMul(Gen::X64Reg src1, Gen::X64Reg src2, Gen::X64Reg scratch);
|
|
|
|
|
2015-07-23 05:25:30 +02:00
|
|
|
void Compile_EvaluateCondition(Instruction instr);
|
|
|
|
void Compile_UniformCondition(Instruction instr);
|
|
|
|
|
2016-03-18 00:45:09 +01:00
|
|
|
/**
|
|
|
|
* Emits the code to conditionally return from a subroutine envoked by the `CALL` instruction.
|
|
|
|
*/
|
|
|
|
void Compile_Return();
|
|
|
|
|
2015-08-26 09:12:14 +02:00
|
|
|
BitSet32 PersistentCallerSavedRegs();
|
2015-08-19 01:49:45 +02:00
|
|
|
|
2016-04-02 06:02:03 +02:00
|
|
|
/**
|
|
|
|
* Assertion evaluated at compile-time, but only triggered if executed at runtime.
|
|
|
|
* @param msg Message to be logged if the assertion fails.
|
|
|
|
*/
|
2016-04-13 05:35:36 +02:00
|
|
|
void Compile_Assert(bool condition, const char* msg);
|
2016-04-02 06:02:03 +02:00
|
|
|
|
2016-03-18 00:45:09 +01:00
|
|
|
/**
|
|
|
|
* Analyzes the entire shader program for `CALL` instructions before emitting any code,
|
|
|
|
* identifying the locations where a return needs to be inserted.
|
|
|
|
*/
|
|
|
|
void FindReturnOffsets();
|
|
|
|
|
|
|
|
/// Mapping of Pica VS instructions to pointers in the emitted code
|
|
|
|
std::array<const u8*, 1024> code_ptr;
|
|
|
|
|
|
|
|
/// Offsets in code where a return needs to be inserted
|
2016-04-13 05:24:34 +02:00
|
|
|
std::vector<unsigned> return_offsets;
|
2016-03-18 00:45:09 +01:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
unsigned program_counter = 0; ///< Offset of the next instruction to decode
|
|
|
|
bool looping = false; ///< True if compiling a loop, used to check for nested loops
|
2015-07-23 05:25:30 +02:00
|
|
|
|
2016-03-18 00:45:09 +01:00
|
|
|
/// Branches that need to be fixed up once the entire shader program is compiled
|
|
|
|
std::vector<std::pair<Gen::FixupBranch, unsigned>> fixup_branches;
|
2016-03-20 05:37:05 +01:00
|
|
|
|
2016-05-13 08:46:14 +02:00
|
|
|
using CompiledShader = void(const void* setup, void* state, const u8* start_addr);
|
2016-03-20 05:37:05 +01:00
|
|
|
CompiledShader* program = nullptr;
|
2015-07-23 05:25:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // Shader
|
|
|
|
|
|
|
|
} // Pica
|