2018-03-19 23:51:43 +01:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-04-05 03:44:35 +02:00
|
|
|
#include <map>
|
2018-10-30 05:03:25 +01:00
|
|
|
#include <optional>
|
2018-04-05 03:44:35 +02:00
|
|
|
#include <set>
|
2018-03-19 23:51:43 +01:00
|
|
|
#include <string>
|
2018-04-20 02:10:40 +02:00
|
|
|
#include <string_view>
|
2018-10-23 03:50:03 +02:00
|
|
|
#include <unordered_set>
|
2018-07-23 23:09:29 +02:00
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2018-03-19 23:51:43 +01:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
2018-04-05 03:44:35 +02:00
|
|
|
#include "video_core/engines/shader_bytecode.h"
|
2018-09-11 03:00:55 +02:00
|
|
|
#include "video_core/engines/shader_header.h"
|
2018-06-10 01:02:05 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_rasterizer.h"
|
2018-03-19 23:51:43 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_decompiler.h"
|
|
|
|
|
2018-08-21 10:18:27 +02:00
|
|
|
namespace OpenGL::GLShader::Decompiler {
|
2018-03-19 23:51:43 +01:00
|
|
|
|
2018-04-08 05:48:38 +02:00
|
|
|
using Tegra::Shader::Attribute;
|
|
|
|
using Tegra::Shader::Instruction;
|
2018-06-17 18:49:34 +02:00
|
|
|
using Tegra::Shader::LogicOperation;
|
2018-04-08 05:48:38 +02:00
|
|
|
using Tegra::Shader::OpCode;
|
|
|
|
using Tegra::Shader::Register;
|
2018-04-10 07:26:15 +02:00
|
|
|
using Tegra::Shader::Sampler;
|
2018-04-10 05:39:44 +02:00
|
|
|
using Tegra::Shader::SubOp;
|
2018-04-08 05:48:38 +02:00
|
|
|
|
2018-03-19 23:51:43 +01:00
|
|
|
constexpr u32 PROGRAM_END = MAX_PROGRAM_CODE_LENGTH;
|
2018-09-11 03:00:55 +02:00
|
|
|
constexpr u32 PROGRAM_HEADER_SIZE = sizeof(Tegra::Shader::Header);
|
2018-03-19 23:51:43 +01:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
constexpr u32 MAX_GEOMETRY_BUFFERS = 6;
|
|
|
|
constexpr u32 MAX_ATTRIBUTES = 0x100; // Size in vec4s, this value is untested
|
|
|
|
|
2018-11-22 02:17:15 +01:00
|
|
|
static const char* INTERNAL_FLAG_NAMES[] = {"zero_flag", "sign_flag", "carry_flag",
|
|
|
|
"overflow_flag"};
|
|
|
|
|
|
|
|
enum class InternalFlag : u64 {
|
|
|
|
ZeroFlag = 0,
|
|
|
|
SignFlag = 1,
|
|
|
|
CarryFlag = 2,
|
|
|
|
OverflowFlag = 3,
|
|
|
|
Amount
|
|
|
|
};
|
|
|
|
|
2018-04-05 03:44:35 +02:00
|
|
|
class DecompileFail : public std::runtime_error {
|
2018-03-19 23:51:43 +01:00
|
|
|
public:
|
2018-04-05 03:44:35 +02:00
|
|
|
using std::runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
/// Translate topology
|
|
|
|
static std::string GetTopologyName(Tegra::Shader::OutputTopology topology) {
|
|
|
|
switch (topology) {
|
|
|
|
case Tegra::Shader::OutputTopology::PointList:
|
|
|
|
return "points";
|
|
|
|
case Tegra::Shader::OutputTopology::LineStrip:
|
|
|
|
return "line_strip";
|
|
|
|
case Tegra::Shader::OutputTopology::TriangleStrip:
|
|
|
|
return "triangle_strip";
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unknown output topology: {}", static_cast<u32>(topology));
|
2018-10-07 04:17:31 +02:00
|
|
|
return "points";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 03:44:35 +02:00
|
|
|
/// Describes the behaviour of code path of a given entry point and a return point.
|
|
|
|
enum class ExitMethod {
|
|
|
|
Undetermined, ///< Internal value. Only occur when analyzing JMP loop.
|
|
|
|
AlwaysReturn, ///< All code paths reach the return point.
|
|
|
|
Conditional, ///< Code path reaches the return point or an END instruction conditionally.
|
|
|
|
AlwaysEnd, ///< All code paths reach a END instruction.
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A subroutine is a range of code refereced by a CALL, IF or LOOP instruction.
|
|
|
|
struct Subroutine {
|
|
|
|
/// Generates a name suitable for GLSL source code.
|
|
|
|
std::string GetName() const {
|
2018-07-13 04:25:03 +02:00
|
|
|
return "sub_" + std::to_string(begin) + '_' + std::to_string(end) + '_' + suffix;
|
2018-04-05 03:44:35 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 04:25:03 +02:00
|
|
|
u32 begin; ///< Entry point of the subroutine.
|
|
|
|
u32 end; ///< Return point of the subroutine.
|
|
|
|
const std::string& suffix; ///< Suffix of the shader, used to make a unique subroutine name
|
|
|
|
ExitMethod exit_method; ///< Exit method of the subroutine.
|
|
|
|
std::set<u32> labels; ///< Addresses refereced by JMP instructions.
|
2018-04-05 03:44:35 +02:00
|
|
|
|
|
|
|
bool operator<(const Subroutine& rhs) const {
|
|
|
|
return std::tie(begin, end) < std::tie(rhs.begin, rhs.end);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Analyzes shader code and produces a set of subroutines.
|
|
|
|
class ControlFlowAnalyzer {
|
|
|
|
public:
|
2018-07-13 04:25:03 +02:00
|
|
|
ControlFlowAnalyzer(const ProgramCode& program_code, u32 main_offset, const std::string& suffix)
|
2018-11-20 20:14:48 +01:00
|
|
|
: program_code(program_code), shader_coverage_begin(main_offset),
|
|
|
|
shader_coverage_end(main_offset + 1) {
|
2018-04-05 03:44:35 +02:00
|
|
|
|
|
|
|
// Recursively finds all subroutines.
|
2018-07-13 04:25:03 +02:00
|
|
|
const Subroutine& program_main = AddSubroutine(main_offset, PROGRAM_END, suffix);
|
2018-04-05 03:44:35 +02:00
|
|
|
if (program_main.exit_method != ExitMethod::AlwaysEnd)
|
|
|
|
throw DecompileFail("Program does not always end");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<Subroutine> GetSubroutines() {
|
|
|
|
return std::move(subroutines);
|
|
|
|
}
|
|
|
|
|
2018-11-20 20:14:48 +01:00
|
|
|
std::size_t GetShaderLength() const {
|
|
|
|
return shader_coverage_end * sizeof(u64);
|
|
|
|
}
|
|
|
|
|
2018-04-05 03:44:35 +02:00
|
|
|
private:
|
|
|
|
const ProgramCode& program_code;
|
|
|
|
std::set<Subroutine> subroutines;
|
|
|
|
std::map<std::pair<u32, u32>, ExitMethod> exit_method_map;
|
2018-11-20 20:14:48 +01:00
|
|
|
u32 shader_coverage_begin;
|
|
|
|
u32 shader_coverage_end;
|
2018-04-05 03:44:35 +02:00
|
|
|
|
|
|
|
/// Adds and analyzes a new subroutine if it is not added yet.
|
2018-07-13 04:25:03 +02:00
|
|
|
const Subroutine& AddSubroutine(u32 begin, u32 end, const std::string& suffix) {
|
2018-07-22 09:28:08 +02:00
|
|
|
Subroutine subroutine{begin, end, suffix, ExitMethod::Undetermined, {}};
|
|
|
|
|
|
|
|
const auto iter = subroutines.find(subroutine);
|
|
|
|
if (iter != subroutines.end()) {
|
2018-04-05 03:44:35 +02:00
|
|
|
return *iter;
|
2018-07-22 09:28:08 +02:00
|
|
|
}
|
2018-04-05 03:44:35 +02:00
|
|
|
|
|
|
|
subroutine.exit_method = Scan(begin, end, subroutine.labels);
|
2018-07-22 09:28:08 +02:00
|
|
|
if (subroutine.exit_method == ExitMethod::Undetermined) {
|
2018-04-05 03:44:35 +02:00
|
|
|
throw DecompileFail("Recursive function detected");
|
2018-07-22 09:28:08 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 03:44:35 +02:00
|
|
|
return *subroutines.insert(std::move(subroutine)).first;
|
|
|
|
}
|
2018-03-19 23:51:43 +01:00
|
|
|
|
2018-06-02 21:45:50 +02:00
|
|
|
/// Merges exit method of two parallel branches.
|
|
|
|
static ExitMethod ParallelExit(ExitMethod a, ExitMethod b) {
|
|
|
|
if (a == ExitMethod::Undetermined) {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
if (b == ExitMethod::Undetermined) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
if (a == b) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return ExitMethod::Conditional;
|
|
|
|
}
|
|
|
|
|
2018-04-05 03:44:35 +02:00
|
|
|
/// Scans a range of code for labels and determines the exit method.
|
|
|
|
ExitMethod Scan(u32 begin, u32 end, std::set<u32>& labels) {
|
2018-09-09 17:51:43 +02:00
|
|
|
const auto [iter, inserted] =
|
2018-04-05 03:44:35 +02:00
|
|
|
exit_method_map.emplace(std::make_pair(begin, end), ExitMethod::Undetermined);
|
|
|
|
ExitMethod& exit_method = iter->second;
|
|
|
|
if (!inserted)
|
|
|
|
return exit_method;
|
|
|
|
|
|
|
|
for (u32 offset = begin; offset != end && offset != PROGRAM_END; ++offset) {
|
2018-11-20 20:14:48 +01:00
|
|
|
shader_coverage_begin = std::min(shader_coverage_begin, offset);
|
|
|
|
shader_coverage_end = std::max(shader_coverage_end, offset + 1);
|
|
|
|
|
2018-06-02 21:45:50 +02:00
|
|
|
const Instruction instr = {program_code[offset]};
|
|
|
|
if (const auto opcode = OpCode::Decode(instr)) {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-04-21 02:49:05 +02:00
|
|
|
case OpCode::Id::EXIT: {
|
2018-06-05 02:14:23 +02:00
|
|
|
// The EXIT instruction can be predicated, which means that the shader can
|
|
|
|
// conditionally end on this instruction. We have to consider the case where the
|
|
|
|
// condition is not met and check the exit method of that other basic block.
|
|
|
|
using Tegra::Shader::Pred;
|
|
|
|
if (instr.pred.pred_index == static_cast<u64>(Pred::UnusedIndex)) {
|
|
|
|
return exit_method = ExitMethod::AlwaysEnd;
|
|
|
|
} else {
|
2018-09-09 17:51:43 +02:00
|
|
|
const ExitMethod not_met = Scan(offset + 1, end, labels);
|
2018-06-05 02:14:23 +02:00
|
|
|
return exit_method = ParallelExit(ExitMethod::AlwaysEnd, not_met);
|
|
|
|
}
|
2018-04-21 02:49:05 +02:00
|
|
|
}
|
2018-06-02 21:45:50 +02:00
|
|
|
case OpCode::Id::BRA: {
|
2018-09-09 17:51:43 +02:00
|
|
|
const u32 target = offset + instr.bra.GetBranchTarget();
|
2018-06-02 21:45:50 +02:00
|
|
|
labels.insert(target);
|
2018-09-09 17:51:43 +02:00
|
|
|
const ExitMethod no_jmp = Scan(offset + 1, end, labels);
|
|
|
|
const ExitMethod jmp = Scan(target, end, labels);
|
2018-06-02 21:45:50 +02:00
|
|
|
return exit_method = ParallelExit(no_jmp, jmp);
|
|
|
|
}
|
2018-10-16 22:11:26 +02:00
|
|
|
case OpCode::Id::SSY:
|
|
|
|
case OpCode::Id::PBK: {
|
|
|
|
// The SSY and PBK use a similar encoding as the BRA instruction.
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
|
|
|
|
"Constant buffer branching is not supported");
|
2018-09-09 17:51:43 +02:00
|
|
|
const u32 target = offset + instr.bra.GetBranchTarget();
|
2018-08-11 22:55:11 +02:00
|
|
|
labels.insert(target);
|
|
|
|
// Continue scanning for an exit method.
|
|
|
|
break;
|
|
|
|
}
|
2018-04-21 02:49:05 +02:00
|
|
|
}
|
2018-04-05 03:44:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return exit_method = ExitMethod::AlwaysReturn;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-28 05:41:06 +01:00
|
|
|
template <typename T>
|
|
|
|
class ShaderScopedScope {
|
|
|
|
public:
|
|
|
|
explicit ShaderScopedScope(T& writer, std::string_view begin_expr, std::string end_expr)
|
|
|
|
: writer(writer), end_expr(std::move(end_expr)) {
|
|
|
|
|
|
|
|
if (begin_expr.empty()) {
|
|
|
|
writer.AddLine('{');
|
|
|
|
} else {
|
|
|
|
writer.AddExpression(begin_expr);
|
|
|
|
writer.AddLine(" {");
|
|
|
|
}
|
|
|
|
++writer.scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderScopedScope(const ShaderScopedScope&) = delete;
|
|
|
|
|
|
|
|
~ShaderScopedScope() {
|
|
|
|
--writer.scope;
|
|
|
|
if (end_expr.empty()) {
|
|
|
|
writer.AddLine('}');
|
|
|
|
} else {
|
|
|
|
writer.AddExpression("} ");
|
|
|
|
writer.AddExpression(end_expr);
|
|
|
|
writer.AddLine(';');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderScopedScope& operator=(const ShaderScopedScope&) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
T& writer;
|
|
|
|
std::string end_expr;
|
|
|
|
};
|
|
|
|
|
2018-04-05 03:44:35 +02:00
|
|
|
class ShaderWriter {
|
|
|
|
public:
|
2018-11-28 05:41:06 +01:00
|
|
|
void AddExpression(std::string_view text) {
|
2018-04-05 03:44:35 +02:00
|
|
|
DEBUG_ASSERT(scope >= 0);
|
|
|
|
if (!text.empty()) {
|
2018-04-20 01:59:20 +02:00
|
|
|
AppendIndentation();
|
2018-04-05 03:44:35 +02:00
|
|
|
}
|
2018-04-20 02:05:42 +02:00
|
|
|
shader_source += text;
|
2018-11-28 05:41:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddLine(std::string_view text) {
|
|
|
|
AddExpression(text);
|
2018-04-20 02:05:42 +02:00
|
|
|
AddNewLine();
|
2018-04-05 03:44:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 02:02:24 +02:00
|
|
|
void AddLine(char character) {
|
|
|
|
DEBUG_ASSERT(scope >= 0);
|
|
|
|
AppendIndentation();
|
|
|
|
shader_source += character;
|
2018-04-20 02:05:42 +02:00
|
|
|
AddNewLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddNewLine() {
|
|
|
|
DEBUG_ASSERT(scope >= 0);
|
2018-04-20 02:02:24 +02:00
|
|
|
shader_source += '\n';
|
2018-04-05 03:44:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetResult() {
|
|
|
|
return std::move(shader_source);
|
|
|
|
}
|
|
|
|
|
2018-11-28 05:41:06 +01:00
|
|
|
ShaderScopedScope<ShaderWriter> Scope(std::string_view begin_expr = {},
|
|
|
|
std::string end_expr = {}) {
|
|
|
|
return ShaderScopedScope(*this, begin_expr, end_expr);
|
|
|
|
}
|
|
|
|
|
2018-04-05 03:44:35 +02:00
|
|
|
int scope = 0;
|
|
|
|
|
|
|
|
private:
|
2018-04-20 01:59:20 +02:00
|
|
|
void AppendIndentation() {
|
2018-09-15 15:21:06 +02:00
|
|
|
shader_source.append(static_cast<std::size_t>(scope) * 4, ' ');
|
2018-04-20 01:59:20 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 03:44:35 +02:00
|
|
|
std::string shader_source;
|
|
|
|
};
|
|
|
|
|
2018-04-26 05:55:21 +02:00
|
|
|
/**
|
|
|
|
* Represents an emulated shader register, used to track the state of that register for emulation
|
|
|
|
* with GLSL. At this time, a register can be used as a float or an integer. This class is used for
|
|
|
|
* bookkeeping within the GLSL program.
|
|
|
|
*/
|
|
|
|
class GLSLRegister {
|
|
|
|
public:
|
2018-04-28 04:24:53 +02:00
|
|
|
enum class Type {
|
|
|
|
Float,
|
|
|
|
Integer,
|
2018-04-29 01:59:30 +02:00
|
|
|
UnsignedInteger,
|
2018-04-28 04:24:53 +02:00
|
|
|
};
|
2018-04-26 05:55:21 +02:00
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
GLSLRegister(std::size_t index, const std::string& suffix) : index{index}, suffix{suffix} {}
|
2018-04-26 05:55:21 +02:00
|
|
|
|
2018-04-29 01:59:30 +02:00
|
|
|
/// Gets the GLSL type string for a register
|
2018-07-22 07:00:44 +02:00
|
|
|
static std::string GetTypeString() {
|
|
|
|
return "float";
|
2018-04-26 05:55:21 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 01:59:30 +02:00
|
|
|
/// Gets the GLSL register prefix string, used for declarations and referencing
|
2018-07-22 07:00:44 +02:00
|
|
|
static std::string GetPrefixString() {
|
|
|
|
return "reg_";
|
2018-04-26 05:55:21 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:24:53 +02:00
|
|
|
/// Returns a GLSL string representing the current state of the register
|
2018-07-22 07:00:44 +02:00
|
|
|
std::string GetString() const {
|
|
|
|
return GetPrefixString() + std::to_string(index) + '_' + suffix;
|
2018-06-04 18:22:26 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:24:53 +02:00
|
|
|
/// Returns the index of the register
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t GetIndex() const {
|
2018-04-28 04:24:53 +02:00
|
|
|
return index;
|
|
|
|
}
|
2018-04-26 05:55:21 +02:00
|
|
|
|
2018-04-28 04:24:53 +02:00
|
|
|
private:
|
2018-09-15 15:21:06 +02:00
|
|
|
const std::size_t index;
|
2018-07-13 04:25:03 +02:00
|
|
|
const std::string& suffix;
|
2018-04-26 05:55:21 +02:00
|
|
|
};
|
|
|
|
|
2018-04-27 04:48:06 +02:00
|
|
|
/**
|
|
|
|
* Used to manage shader registers that are emulated with GLSL. This class keeps track of the state
|
|
|
|
* of all registers (e.g. whether they are currently being used as Floats or Integers), and
|
|
|
|
* generates the necessary GLSL code to perform conversions as needed. This class is used for
|
|
|
|
* bookkeeping within the GLSL program.
|
|
|
|
*/
|
|
|
|
class GLSLRegisterManager {
|
2018-04-05 03:44:35 +02:00
|
|
|
public:
|
2018-04-27 04:48:06 +02:00
|
|
|
GLSLRegisterManager(ShaderWriter& shader, ShaderWriter& declarations,
|
2018-10-07 04:17:31 +02:00
|
|
|
const Maxwell3D::Regs::ShaderStage& stage, const std::string& suffix,
|
|
|
|
const Tegra::Shader::Header& header)
|
2018-10-23 03:50:03 +02:00
|
|
|
: shader{shader}, declarations{declarations}, stage{stage}, suffix{suffix}, header{header},
|
2018-10-20 03:48:21 +02:00
|
|
|
fixed_pipeline_output_attributes_used{}, local_memory_size{0} {
|
2018-04-26 05:55:21 +02:00
|
|
|
BuildRegisterList();
|
2018-09-05 05:09:24 +02:00
|
|
|
BuildInputList();
|
2018-04-05 03:44:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-16 01:40:34 +02:00
|
|
|
/**
|
|
|
|
* Returns code that does an integer size conversion for the specified size.
|
|
|
|
* @param value Value to perform integer size conversion on.
|
|
|
|
* @param size Register size to use for conversion instructions.
|
|
|
|
* @returns GLSL string corresponding to the value converted to the specified size.
|
|
|
|
*/
|
|
|
|
static std::string ConvertIntegerSize(const std::string& value, Register::Size size) {
|
|
|
|
switch (size) {
|
|
|
|
case Register::Size::Byte:
|
|
|
|
return "((" + value + " << 24) >> 24)";
|
|
|
|
case Register::Size::Short:
|
|
|
|
return "((" + value + " << 16) >> 16)";
|
|
|
|
case Register::Size::Word:
|
|
|
|
// Default - do nothing
|
|
|
|
return value;
|
|
|
|
default:
|
2018-11-28 05:41:06 +01:00
|
|
|
UNREACHABLE_MSG("Unimplemented conversion size: {}", static_cast<u32>(size));
|
2018-06-16 01:40:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-29 01:59:30 +02:00
|
|
|
/**
|
|
|
|
* Gets a register as an float.
|
|
|
|
* @param reg The register to get.
|
|
|
|
* @param elem The element to use for the operation.
|
|
|
|
* @returns GLSL string corresponding to the register as a float.
|
|
|
|
*/
|
|
|
|
std::string GetRegisterAsFloat(const Register& reg, unsigned elem = 0) {
|
|
|
|
return GetRegister(reg, elem);
|
|
|
|
}
|
2018-04-27 04:48:06 +02:00
|
|
|
|
2018-04-29 01:59:30 +02:00
|
|
|
/**
|
|
|
|
* Gets a register as an integer.
|
|
|
|
* @param reg The register to get.
|
|
|
|
* @param elem The element to use for the operation.
|
|
|
|
* @param is_signed Whether to get the register as a signed (or unsigned) integer.
|
2018-06-16 01:40:34 +02:00
|
|
|
* @param size Register size to use for conversion instructions.
|
2018-04-29 01:59:30 +02:00
|
|
|
* @returns GLSL string corresponding to the register as an integer.
|
|
|
|
*/
|
2018-06-16 01:40:34 +02:00
|
|
|
std::string GetRegisterAsInteger(const Register& reg, unsigned elem = 0, bool is_signed = true,
|
|
|
|
Register::Size size = Register::Size::Word) {
|
2018-07-22 07:00:44 +02:00
|
|
|
const std::string func{is_signed ? "floatBitsToInt" : "floatBitsToUint"};
|
|
|
|
const std::string value{func + '(' + GetRegister(reg, elem) + ')'};
|
2018-06-16 01:40:34 +02:00
|
|
|
return ConvertIntegerSize(value, size);
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 04:48:06 +02:00
|
|
|
/**
|
2018-04-29 01:59:30 +02:00
|
|
|
* Writes code that does a register assignment to float value operation.
|
2018-04-27 04:48:06 +02:00
|
|
|
* @param reg The destination register to use.
|
|
|
|
* @param elem The element to use for the operation.
|
|
|
|
* @param value The code representing the value to assign.
|
|
|
|
* @param dest_num_components Number of components in the destination.
|
|
|
|
* @param value_num_components Number of components in the value.
|
2018-06-09 08:36:33 +02:00
|
|
|
* @param is_saturated Optional, when True, saturates the provided value.
|
2018-04-27 04:48:06 +02:00
|
|
|
* @param dest_elem Optional, the destination element to use for the operation.
|
|
|
|
*/
|
|
|
|
void SetRegisterToFloat(const Register& reg, u64 elem, const std::string& value,
|
2018-06-09 08:36:33 +02:00
|
|
|
u64 dest_num_components, u64 value_num_components,
|
2018-10-28 16:38:40 +01:00
|
|
|
bool is_saturated = false, u64 dest_elem = 0, bool precise = false) {
|
2018-06-09 08:36:33 +02:00
|
|
|
|
|
|
|
SetRegister(reg, elem, is_saturated ? "clamp(" + value + ", 0.0, 1.0)" : value,
|
2018-10-28 16:38:40 +01:00
|
|
|
dest_num_components, value_num_components, dest_elem, precise);
|
2018-04-29 01:59:30 +02:00
|
|
|
}
|
2018-04-27 04:48:06 +02:00
|
|
|
|
2018-04-29 01:59:30 +02:00
|
|
|
/**
|
|
|
|
* Writes code that does a register assignment to integer value operation.
|
|
|
|
* @param reg The destination register to use.
|
|
|
|
* @param elem The element to use for the operation.
|
|
|
|
* @param value The code representing the value to assign.
|
|
|
|
* @param dest_num_components Number of components in the destination.
|
|
|
|
* @param value_num_components Number of components in the value.
|
2018-06-09 08:36:33 +02:00
|
|
|
* @param is_saturated Optional, when True, saturates the provided value.
|
2018-04-29 01:59:30 +02:00
|
|
|
* @param dest_elem Optional, the destination element to use for the operation.
|
2018-06-16 01:40:34 +02:00
|
|
|
* @param size Register size to use for conversion instructions.
|
2018-04-29 01:59:30 +02:00
|
|
|
*/
|
|
|
|
void SetRegisterToInteger(const Register& reg, bool is_signed, u64 elem,
|
|
|
|
const std::string& value, u64 dest_num_components,
|
2018-06-09 08:36:33 +02:00
|
|
|
u64 value_num_components, bool is_saturated = false,
|
2018-09-18 00:16:06 +02:00
|
|
|
u64 dest_elem = 0, Register::Size size = Register::Size::Word,
|
|
|
|
bool sets_cc = false) {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(is_saturated);
|
2018-06-09 08:36:33 +02:00
|
|
|
|
2018-07-22 07:00:44 +02:00
|
|
|
const std::string func{is_signed ? "intBitsToFloat" : "uintBitsToFloat"};
|
2018-04-27 04:48:06 +02:00
|
|
|
|
2018-06-16 01:40:34 +02:00
|
|
|
SetRegister(reg, elem, func + '(' + ConvertIntegerSize(value, size) + ')',
|
2018-10-28 16:38:40 +01:00
|
|
|
dest_num_components, value_num_components, dest_elem, false);
|
2018-09-18 00:16:06 +02:00
|
|
|
|
|
|
|
if (sets_cc) {
|
|
|
|
const std::string zero_condition = "( " + ConvertIntegerSize(value, size) + " == 0 )";
|
|
|
|
SetInternalFlag(InternalFlag::ZeroFlag, zero_condition);
|
2018-11-22 02:06:01 +01:00
|
|
|
LOG_WARNING(HW_GPU, "Condition codes implementation is incomplete.");
|
2018-09-18 00:16:06 +02:00
|
|
|
}
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 06:58:30 +02:00
|
|
|
/**
|
|
|
|
* Writes code that does a register assignment to a half float value operation.
|
|
|
|
* @param reg The destination register to use.
|
|
|
|
* @param elem The element to use for the operation.
|
|
|
|
* @param value The code representing the value to assign. Type has to be half float.
|
2018-10-20 20:19:28 +02:00
|
|
|
* @param merge Half float kind of assignment.
|
|
|
|
* @param dest_num_components Number of components in the destination.
|
2018-10-15 06:58:30 +02:00
|
|
|
* @param value_num_components Number of components in the value.
|
|
|
|
* @param is_saturated Optional, when True, saturates the provided value.
|
|
|
|
* @param dest_elem Optional, the destination element to use for the operation.
|
|
|
|
*/
|
|
|
|
void SetRegisterToHalfFloat(const Register& reg, u64 elem, const std::string& value,
|
|
|
|
Tegra::Shader::HalfMerge merge, u64 dest_num_components,
|
|
|
|
u64 value_num_components, bool is_saturated = false,
|
|
|
|
u64 dest_elem = 0) {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(is_saturated);
|
2018-10-15 06:58:30 +02:00
|
|
|
|
|
|
|
const std::string result = [&]() {
|
|
|
|
switch (merge) {
|
|
|
|
case Tegra::Shader::HalfMerge::H0_H1:
|
|
|
|
return "uintBitsToFloat(packHalf2x16(" + value + "))";
|
|
|
|
case Tegra::Shader::HalfMerge::F32:
|
|
|
|
// Half float instructions take the first component when doing a float cast.
|
|
|
|
return "float(" + value + ".x)";
|
|
|
|
case Tegra::Shader::HalfMerge::Mrg_H0:
|
|
|
|
// TODO(Rodrigo): I guess Mrg_H0 and Mrg_H1 take their respective component from the
|
|
|
|
// pack. I couldn't test this on hardware but it shouldn't really matter since most
|
|
|
|
// of the time when a Mrg_* flag is used both components will be mirrored. That
|
|
|
|
// being said, it deserves a test.
|
|
|
|
return "((" + GetRegisterAsInteger(reg, 0, false) +
|
|
|
|
" & 0xffff0000) | (packHalf2x16(" + value + ") & 0x0000ffff))";
|
|
|
|
case Tegra::Shader::HalfMerge::Mrg_H1:
|
|
|
|
return "((" + GetRegisterAsInteger(reg, 0, false) +
|
|
|
|
" & 0x0000ffff) | (packHalf2x16(" + value + ") & 0xffff0000))";
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return std::string("0");
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2018-10-28 16:38:40 +01:00
|
|
|
SetRegister(reg, elem, result, dest_num_components, value_num_components, dest_elem, false);
|
2018-10-15 06:58:30 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 04:48:06 +02:00
|
|
|
/**
|
|
|
|
* Writes code that does a register assignment to input attribute operation. Input attributes
|
|
|
|
* are stored as floats, so this may require conversion.
|
|
|
|
* @param reg The destination register to use.
|
|
|
|
* @param elem The element to use for the operation.
|
2018-05-30 17:58:03 +02:00
|
|
|
* @param attribute The input attribute to use as the source value.
|
2018-10-20 20:19:28 +02:00
|
|
|
* @param input_mode The input mode.
|
2018-10-07 04:17:31 +02:00
|
|
|
* @param vertex The register that decides which vertex to read from (used in GS).
|
2018-04-27 04:48:06 +02:00
|
|
|
*/
|
2018-09-05 05:09:24 +02:00
|
|
|
void SetRegisterToInputAttibute(const Register& reg, u64 elem, Attribute::Index attribute,
|
2018-10-07 04:17:31 +02:00
|
|
|
const Tegra::Shader::IpaMode& input_mode,
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<Register> vertex = {}) {
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string dest = GetRegisterAsFloat(reg);
|
2018-10-07 04:17:31 +02:00
|
|
|
const std::string src = GetInputAttribute(attribute, input_mode, vertex) + GetSwizzle(elem);
|
2018-07-22 07:00:44 +02:00
|
|
|
shader.AddLine(dest + " = " + src + ';');
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-20 03:48:21 +02:00
|
|
|
std::string GetLocalMemoryAsFloat(const std::string& index) {
|
|
|
|
return "lmem[" + index + ']';
|
2018-10-20 01:47:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-20 03:48:21 +02:00
|
|
|
std::string GetLocalMemoryAsInteger(const std::string& index, bool is_signed = false) {
|
2018-10-20 01:47:06 +02:00
|
|
|
const std::string func{is_signed ? "floatToIntBits" : "floatBitsToUint"};
|
|
|
|
return func + "(lmem[" + index + "])";
|
|
|
|
}
|
|
|
|
|
2018-10-20 03:48:21 +02:00
|
|
|
void SetLocalMemoryAsFloat(const std::string& index, const std::string& value) {
|
|
|
|
shader.AddLine("lmem[" + index + "] = " + value + ';');
|
2018-10-20 01:47:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-20 03:48:21 +02:00
|
|
|
void SetLocalMemoryAsInteger(const std::string& index, const std::string& value,
|
|
|
|
bool is_signed = false) {
|
2018-10-20 01:47:06 +02:00
|
|
|
const std::string func{is_signed ? "intBitsToFloat" : "uintBitsToFloat"};
|
2018-10-20 03:48:21 +02:00
|
|
|
shader.AddLine("lmem[" + index + "] = " + func + '(' + value + ");");
|
2018-10-20 01:47:06 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 02:06:01 +01:00
|
|
|
std::string GetConditionCode(const Tegra::Shader::ConditionCode cc) const {
|
2018-09-18 00:16:06 +02:00
|
|
|
switch (cc) {
|
2018-11-22 02:06:01 +01:00
|
|
|
case Tegra::Shader::ConditionCode::NEU:
|
2018-09-18 00:16:06 +02:00
|
|
|
return "!(" + GetInternalFlag(InternalFlag::ZeroFlag) + ')';
|
|
|
|
default:
|
2018-11-22 02:06:01 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented condition code: {}", static_cast<u32>(cc));
|
2018-09-18 00:16:06 +02:00
|
|
|
return "false";
|
|
|
|
}
|
2018-09-09 03:54:11 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 02:17:15 +01:00
|
|
|
std::string GetInternalFlag(const InternalFlag flag) const {
|
|
|
|
const auto index = static_cast<u32>(flag);
|
|
|
|
ASSERT(index < static_cast<u32>(InternalFlag::Amount));
|
|
|
|
|
|
|
|
return std::string(INTERNAL_FLAG_NAMES[index]) + '_' + suffix;
|
2018-09-18 00:16:06 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 02:17:15 +01:00
|
|
|
void SetInternalFlag(const InternalFlag flag, const std::string& value) const {
|
|
|
|
shader.AddLine(GetInternalFlag(flag) + " = " + value + ';');
|
2018-09-09 03:54:11 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 04:48:06 +02:00
|
|
|
/**
|
|
|
|
* Writes code that does a output attribute assignment to register operation. Output attributes
|
|
|
|
* are stored as floats, so this may require conversion.
|
|
|
|
* @param attribute The destination output attribute.
|
|
|
|
* @param elem The element to use for the operation.
|
2018-10-07 04:17:31 +02:00
|
|
|
* @param val_reg The register to use as the source value.
|
|
|
|
* @param buf_reg The register that tells which buffer to write to (used in geometry shaders).
|
2018-04-27 04:48:06 +02:00
|
|
|
*/
|
2018-10-07 04:17:31 +02:00
|
|
|
void SetOutputAttributeToRegister(Attribute::Index attribute, u64 elem, const Register& val_reg,
|
|
|
|
const Register& buf_reg) {
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string dest = GetOutputAttribute(attribute);
|
2018-10-07 04:17:31 +02:00
|
|
|
const std::string src = GetRegisterAsFloat(val_reg);
|
2018-11-23 05:57:49 +01:00
|
|
|
if (dest.empty())
|
|
|
|
return;
|
2018-08-10 18:34:51 +02:00
|
|
|
|
2018-11-23 05:57:49 +01:00
|
|
|
// Can happen with unknown/unimplemented output attributes, in which case we ignore the
|
|
|
|
// instruction for now.
|
|
|
|
if (stage == Maxwell3D::Regs::ShaderStage::Geometry) {
|
|
|
|
// TODO(Rodrigo): nouveau sets some attributes after setting emitting a geometry
|
|
|
|
// shader. These instructions use a dirty register as buffer index, to avoid some
|
|
|
|
// drivers from complaining about out of boundary writes, guard them.
|
|
|
|
const std::string buf_index{"((" + GetRegisterAsInteger(buf_reg) + ") % " +
|
|
|
|
std::to_string(MAX_GEOMETRY_BUFFERS) + ')'};
|
|
|
|
shader.AddLine("amem[" + buf_index + "][" +
|
|
|
|
std::to_string(static_cast<u32>(attribute)) + ']' + GetSwizzle(elem) +
|
|
|
|
" = " + src + ';');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (attribute) {
|
|
|
|
case Attribute::Index::ClipDistances0123:
|
|
|
|
case Attribute::Index::ClipDistances4567: {
|
2018-11-27 19:35:26 +01:00
|
|
|
const u64 index = (attribute == Attribute::Index::ClipDistances4567 ? 4 : 0) + elem;
|
2018-11-23 05:57:49 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(
|
|
|
|
((header.vtg.clip_distances >> index) & 1) == 0,
|
|
|
|
"Shader is setting gl_ClipDistance{} without enabling it in the header", index);
|
|
|
|
|
|
|
|
fixed_pipeline_output_attributes_used.insert(attribute);
|
|
|
|
shader.AddLine(dest + '[' + std::to_string(index) + "] = " + src + ';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Attribute::Index::PointSize:
|
|
|
|
fixed_pipeline_output_attributes_used.insert(attribute);
|
|
|
|
shader.AddLine(dest + " = " + src + ';');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
shader.AddLine(dest + GetSwizzle(elem) + " = " + src + ';');
|
|
|
|
break;
|
2018-08-10 18:34:51 +02:00
|
|
|
}
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 18:22:26 +02:00
|
|
|
/// Generates code representing a uniform (C buffer) register, interpreted as the input type.
|
2018-08-15 16:25:02 +02:00
|
|
|
std::string GetUniform(u64 index, u64 offset, GLSLRegister::Type type,
|
|
|
|
Register::Size size = Register::Size::Word) {
|
2018-06-06 04:45:22 +02:00
|
|
|
declr_const_buffers[index].MarkAsUsed(index, offset, stage);
|
2018-06-10 01:02:05 +02:00
|
|
|
std::string value = 'c' + std::to_string(index) + '[' + std::to_string(offset / 4) + "][" +
|
|
|
|
std::to_string(offset % 4) + ']';
|
2018-04-27 04:48:06 +02:00
|
|
|
|
2018-06-04 18:22:26 +02:00
|
|
|
if (type == GLSLRegister::Type::Float) {
|
2018-08-15 16:25:02 +02:00
|
|
|
// Do nothing, default
|
2018-06-04 18:22:26 +02:00
|
|
|
} else if (type == GLSLRegister::Type::Integer) {
|
2018-08-15 16:25:02 +02:00
|
|
|
value = "floatBitsToInt(" + value + ')';
|
2018-08-12 22:51:32 +02:00
|
|
|
} else if (type == GLSLRegister::Type::UnsignedInteger) {
|
2018-08-15 16:25:02 +02:00
|
|
|
value = "floatBitsToUint(" + value + ')';
|
2018-04-27 04:48:06 +02:00
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2018-08-15 16:25:02 +02:00
|
|
|
|
|
|
|
return ConvertIntegerSize(value, size);
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
|
|
|
|
2018-08-15 01:12:45 +02:00
|
|
|
std::string GetUniformIndirect(u64 cbuf_index, s64 offset, const std::string& index_str,
|
2018-06-06 05:34:37 +02:00
|
|
|
GLSLRegister::Type type) {
|
2018-08-15 01:12:45 +02:00
|
|
|
declr_const_buffers[cbuf_index].MarkAsUsedIndirect(cbuf_index, stage);
|
2018-06-10 01:02:05 +02:00
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string final_offset = fmt::format("({} + {})", index_str, offset / 4);
|
|
|
|
const std::string value = 'c' + std::to_string(cbuf_index) + '[' + final_offset + " / 4][" +
|
|
|
|
final_offset + " % 4]";
|
2018-06-06 05:34:37 +02:00
|
|
|
|
|
|
|
if (type == GLSLRegister::Type::Float) {
|
|
|
|
return value;
|
|
|
|
} else if (type == GLSLRegister::Type::Integer) {
|
|
|
|
return "floatBitsToInt(" + value + ')';
|
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
/// Add declarations.
|
2018-07-13 04:25:03 +02:00
|
|
|
void GenerateDeclarations(const std::string& suffix) {
|
2018-10-23 03:50:03 +02:00
|
|
|
GenerateVertex();
|
2018-10-07 04:17:31 +02:00
|
|
|
GenerateRegisters(suffix);
|
2018-10-20 01:47:06 +02:00
|
|
|
GenerateLocalMemory();
|
2018-10-07 04:17:31 +02:00
|
|
|
GenerateInternalFlags();
|
|
|
|
GenerateInputAttrs();
|
|
|
|
GenerateOutputAttrs();
|
|
|
|
GenerateConstBuffers();
|
|
|
|
GenerateSamplers();
|
|
|
|
GenerateGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a list of constant buffer declarations.
|
|
|
|
std::vector<ConstBufferEntry> GetConstBuffersDeclarations() const {
|
|
|
|
std::vector<ConstBufferEntry> result;
|
|
|
|
std::copy_if(declr_const_buffers.begin(), declr_const_buffers.end(),
|
|
|
|
std::back_inserter(result), [](const auto& entry) { return entry.IsUsed(); });
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a list of samplers used in the shader.
|
|
|
|
const std::vector<SamplerEntry>& GetSamplers() const {
|
|
|
|
return used_samplers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the GLSL sampler used for the input shader sampler, and creates a new one if
|
|
|
|
/// necessary.
|
|
|
|
std::string AccessSampler(const Sampler& sampler, Tegra::Shader::TextureType type,
|
|
|
|
bool is_array, bool is_shadow) {
|
|
|
|
const auto offset = static_cast<std::size_t>(sampler.index.Value());
|
|
|
|
|
|
|
|
// If this sampler has already been used, return the existing mapping.
|
|
|
|
const auto itr =
|
|
|
|
std::find_if(used_samplers.begin(), used_samplers.end(),
|
|
|
|
[&](const SamplerEntry& entry) { return entry.GetOffset() == offset; });
|
|
|
|
|
|
|
|
if (itr != used_samplers.end()) {
|
|
|
|
ASSERT(itr->GetType() == type && itr->IsArray() == is_array &&
|
|
|
|
itr->IsShadow() == is_shadow);
|
|
|
|
return itr->GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise create a new mapping for this sampler
|
|
|
|
const std::size_t next_index = used_samplers.size();
|
|
|
|
const SamplerEntry entry{stage, offset, next_index, type, is_array, is_shadow};
|
|
|
|
used_samplers.emplace_back(entry);
|
|
|
|
return entry.GetName();
|
|
|
|
}
|
|
|
|
|
2018-10-20 01:47:06 +02:00
|
|
|
void SetLocalMemory(u64 lmem) {
|
|
|
|
local_memory_size = lmem;
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
private:
|
|
|
|
/// Generates declarations for registers.
|
|
|
|
void GenerateRegisters(const std::string& suffix) {
|
2018-04-27 04:48:06 +02:00
|
|
|
for (const auto& reg : regs) {
|
2018-07-22 07:00:44 +02:00
|
|
|
declarations.AddLine(GLSLRegister::GetTypeString() + ' ' + reg.GetPrefixString() +
|
|
|
|
std::to_string(reg.GetIndex()) + '_' + suffix + " = 0;");
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
|
|
|
declarations.AddNewLine();
|
2018-10-07 04:17:31 +02:00
|
|
|
}
|
2018-04-27 04:48:06 +02:00
|
|
|
|
2018-10-20 01:47:06 +02:00
|
|
|
/// Generates declarations for local memory.
|
|
|
|
void GenerateLocalMemory() {
|
|
|
|
if (local_memory_size > 0) {
|
2018-10-20 03:48:21 +02:00
|
|
|
declarations.AddLine("float lmem[" + std::to_string((local_memory_size - 1 + 4) / 4) +
|
|
|
|
"];");
|
2018-10-20 01:47:06 +02:00
|
|
|
declarations.AddNewLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
/// Generates declarations for internal flags.
|
|
|
|
void GenerateInternalFlags() {
|
2018-11-22 02:17:15 +01:00
|
|
|
for (u32 flag = 0; flag < static_cast<u32>(InternalFlag::Amount); flag++) {
|
|
|
|
const InternalFlag code = static_cast<InternalFlag>(flag);
|
2018-09-18 00:16:06 +02:00
|
|
|
declarations.AddLine("bool " + GetInternalFlag(code) + " = false;");
|
2018-09-09 03:54:11 +02:00
|
|
|
}
|
|
|
|
declarations.AddNewLine();
|
2018-10-07 04:17:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates declarations for input attributes.
|
|
|
|
void GenerateInputAttrs() {
|
2018-09-05 05:09:24 +02:00
|
|
|
for (const auto element : declr_input_attribute) {
|
2018-04-27 04:48:06 +02:00
|
|
|
// TODO(bunnei): Use proper number of elements for these
|
2018-09-05 05:09:24 +02:00
|
|
|
u32 idx =
|
|
|
|
static_cast<u32>(element.first) - static_cast<u32>(Attribute::Index::Attribute_0);
|
2018-10-07 22:27:15 +02:00
|
|
|
if (stage != Maxwell3D::Regs::ShaderStage::Vertex) {
|
|
|
|
// If inputs are varyings, add an offset
|
|
|
|
idx += GENERIC_VARYING_START_LOCATION;
|
|
|
|
}
|
2018-10-07 04:17:31 +02:00
|
|
|
|
|
|
|
std::string attr{GetInputAttribute(element.first, element.second)};
|
|
|
|
if (stage == Maxwell3D::Regs::ShaderStage::Geometry) {
|
|
|
|
attr = "gs_" + attr + "[]";
|
|
|
|
}
|
|
|
|
declarations.AddLine("layout (location = " + std::to_string(idx) + ") " +
|
|
|
|
GetInputFlags(element.first) + "in vec4 " + attr + ';');
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
2018-10-07 04:17:31 +02:00
|
|
|
|
2018-04-27 04:48:06 +02:00
|
|
|
declarations.AddNewLine();
|
2018-10-07 04:17:31 +02:00
|
|
|
}
|
2018-04-27 04:48:06 +02:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
/// Generates declarations for output attributes.
|
|
|
|
void GenerateOutputAttrs() {
|
2018-04-27 04:48:06 +02:00
|
|
|
for (const auto& index : declr_output_attribute) {
|
|
|
|
// TODO(bunnei): Use proper number of elements for these
|
2018-10-07 22:27:15 +02:00
|
|
|
const u32 idx = static_cast<u32>(index) -
|
|
|
|
static_cast<u32>(Attribute::Index::Attribute_0) +
|
|
|
|
GENERIC_VARYING_START_LOCATION;
|
|
|
|
declarations.AddLine("layout (location = " + std::to_string(idx) + ") out vec4 " +
|
|
|
|
GetOutputAttribute(index) + ';');
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
|
|
|
declarations.AddNewLine();
|
2018-10-07 04:17:31 +02:00
|
|
|
}
|
2018-04-27 04:48:06 +02:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
/// Generates declarations for constant buffers.
|
|
|
|
void GenerateConstBuffers() {
|
2018-04-27 04:48:06 +02:00
|
|
|
for (const auto& entry : GetConstBuffersDeclarations()) {
|
2018-10-07 04:17:31 +02:00
|
|
|
declarations.AddLine("layout (std140) uniform " + entry.GetName());
|
2018-04-27 04:48:06 +02:00
|
|
|
declarations.AddLine('{');
|
2018-06-10 01:02:05 +02:00
|
|
|
declarations.AddLine(" vec4 c" + std::to_string(entry.GetIndex()) +
|
|
|
|
"[MAX_CONSTBUFFER_ELEMENTS];");
|
2018-04-27 04:48:06 +02:00
|
|
|
declarations.AddLine("};");
|
|
|
|
declarations.AddNewLine();
|
|
|
|
}
|
|
|
|
declarations.AddNewLine();
|
2018-10-07 04:17:31 +02:00
|
|
|
}
|
2018-06-06 19:58:16 +02:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
/// Generates declarations for samplers.
|
|
|
|
void GenerateSamplers() {
|
2018-09-07 00:45:49 +02:00
|
|
|
const auto& samplers = GetSamplers();
|
|
|
|
for (const auto& sampler : samplers) {
|
|
|
|
declarations.AddLine("uniform " + sampler.GetTypeString() + ' ' + sampler.GetName() +
|
|
|
|
';');
|
2018-06-06 19:58:16 +02:00
|
|
|
}
|
2018-09-07 00:45:49 +02:00
|
|
|
declarations.AddNewLine();
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
/// Generates declarations used for geometry shaders.
|
|
|
|
void GenerateGeometry() {
|
|
|
|
if (stage != Maxwell3D::Regs::ShaderStage::Geometry)
|
|
|
|
return;
|
2018-06-06 19:58:16 +02:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
declarations.AddLine(
|
|
|
|
"layout (" + GetTopologyName(header.common3.output_topology) +
|
|
|
|
", max_vertices = " + std::to_string(header.common4.max_output_vertices) + ") out;");
|
|
|
|
declarations.AddNewLine();
|
2018-06-06 19:58:16 +02:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
declarations.AddLine("vec4 amem[" + std::to_string(MAX_GEOMETRY_BUFFERS) + "][" +
|
|
|
|
std::to_string(MAX_ATTRIBUTES) + "];");
|
|
|
|
declarations.AddNewLine();
|
2018-06-06 19:58:16 +02:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
constexpr char buffer[] = "amem[output_buffer]";
|
|
|
|
declarations.AddLine("void emit_vertex(uint output_buffer) {");
|
|
|
|
++declarations.scope;
|
|
|
|
for (const auto element : declr_output_attribute) {
|
|
|
|
declarations.AddLine(GetOutputAttribute(element) + " = " + buffer + '[' +
|
|
|
|
std::to_string(static_cast<u32>(element)) + "];");
|
|
|
|
}
|
|
|
|
|
|
|
|
declarations.AddLine("position = " + std::string(buffer) + '[' +
|
|
|
|
std::to_string(static_cast<u32>(Attribute::Index::Position)) + "];");
|
|
|
|
|
|
|
|
// If a geometry shader is attached, it will always flip (it's the last stage before
|
|
|
|
// fragment). For more info about flipping, refer to gl_shader_gen.cpp.
|
|
|
|
declarations.AddLine("position.xy *= viewport_flip.xy;");
|
|
|
|
declarations.AddLine("gl_Position = position;");
|
|
|
|
declarations.AddLine("position.w = 1.0;");
|
|
|
|
declarations.AddLine("EmitVertex();");
|
|
|
|
--declarations.scope;
|
|
|
|
declarations.AddLine('}');
|
|
|
|
declarations.AddNewLine();
|
2018-06-06 19:58:16 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 03:50:03 +02:00
|
|
|
void GenerateVertex() {
|
|
|
|
if (stage != Maxwell3D::Regs::ShaderStage::Vertex)
|
|
|
|
return;
|
2018-11-23 05:57:49 +01:00
|
|
|
bool clip_distances_declared = false;
|
|
|
|
|
2018-10-23 03:50:03 +02:00
|
|
|
declarations.AddLine("out gl_PerVertex {");
|
|
|
|
++declarations.scope;
|
|
|
|
declarations.AddLine("vec4 gl_Position;");
|
|
|
|
for (auto& o : fixed_pipeline_output_attributes_used) {
|
|
|
|
if (o == Attribute::Index::PointSize)
|
|
|
|
declarations.AddLine("float gl_PointSize;");
|
2018-11-23 05:57:49 +01:00
|
|
|
if (!clip_distances_declared && (o == Attribute::Index::ClipDistances0123 ||
|
|
|
|
o == Attribute::Index::ClipDistances4567)) {
|
|
|
|
declarations.AddLine("float gl_ClipDistance[];");
|
|
|
|
clip_distances_declared = true;
|
|
|
|
}
|
2018-10-23 03:50:03 +02:00
|
|
|
}
|
|
|
|
--declarations.scope;
|
|
|
|
declarations.AddLine("};");
|
|
|
|
}
|
|
|
|
|
2018-04-29 01:59:30 +02:00
|
|
|
/// Generates code representing a temporary (GPR) register.
|
|
|
|
std::string GetRegister(const Register& reg, unsigned elem) {
|
|
|
|
if (reg == Register::ZeroIndex) {
|
|
|
|
return "0";
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:00:44 +02:00
|
|
|
return regs[reg.GetSwizzledIndex(elem)].GetString();
|
2018-04-29 01:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes code that does a register assignment to value operation.
|
|
|
|
* @param reg The destination register to use.
|
|
|
|
* @param elem The element to use for the operation.
|
|
|
|
* @param value The code representing the value to assign.
|
|
|
|
* @param dest_num_components Number of components in the destination.
|
|
|
|
* @param value_num_components Number of components in the value.
|
|
|
|
* @param dest_elem Optional, the destination element to use for the operation.
|
|
|
|
*/
|
|
|
|
void SetRegister(const Register& reg, u64 elem, const std::string& value,
|
2018-10-28 16:38:40 +01:00
|
|
|
u64 dest_num_components, u64 value_num_components, u64 dest_elem,
|
|
|
|
bool precise) {
|
2018-07-24 01:21:28 +02:00
|
|
|
if (reg == Register::ZeroIndex) {
|
2018-11-21 02:23:10 +01:00
|
|
|
// Setting RZ is a nop in hardware.
|
2018-07-24 01:21:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:39:10 +02:00
|
|
|
std::string dest = GetRegister(reg, static_cast<u32>(dest_elem));
|
2018-04-29 01:59:30 +02:00
|
|
|
if (dest_num_components > 1) {
|
|
|
|
dest += GetSwizzle(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string src = '(' + value + ')';
|
|
|
|
if (value_num_components > 1) {
|
|
|
|
src += GetSwizzle(elem);
|
|
|
|
}
|
|
|
|
|
2018-10-28 16:38:40 +01:00
|
|
|
if (precise && stage != Maxwell3D::Regs::ShaderStage::Fragment) {
|
2018-11-28 06:44:15 +01:00
|
|
|
const auto scope = shader.Scope();
|
|
|
|
|
2018-10-28 16:38:40 +01:00
|
|
|
// This avoids optimizations of constant propagation and keeps the code as the original
|
|
|
|
// Sadly using the precise keyword causes "linking" errors on fragment shaders.
|
|
|
|
shader.AddLine("precise float tmp = " + src + ';');
|
|
|
|
shader.AddLine(dest + " = tmp;");
|
|
|
|
} else {
|
|
|
|
shader.AddLine(dest + " = " + src + ';');
|
|
|
|
}
|
2018-04-29 01:59:30 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 04:48:06 +02:00
|
|
|
/// Build the GLSL register list.
|
2018-04-26 05:55:21 +02:00
|
|
|
void BuildRegisterList() {
|
2018-08-09 23:29:09 +02:00
|
|
|
regs.reserve(Register::NumRegisters);
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t index = 0; index < Register::NumRegisters; ++index) {
|
2018-07-22 07:00:44 +02:00
|
|
|
regs.emplace_back(index, suffix);
|
2018-04-26 05:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 05:09:24 +02:00
|
|
|
void BuildInputList() {
|
|
|
|
const u32 size = static_cast<u32>(Attribute::Index::Attribute_31) -
|
|
|
|
static_cast<u32>(Attribute::Index::Attribute_0) + 1;
|
|
|
|
declr_input_attribute.reserve(size);
|
|
|
|
}
|
|
|
|
|
2018-04-08 05:48:38 +02:00
|
|
|
/// Generates code representing an input attribute register.
|
2018-09-05 05:09:24 +02:00
|
|
|
std::string GetInputAttribute(Attribute::Index attribute,
|
2018-10-07 04:17:31 +02:00
|
|
|
const Tegra::Shader::IpaMode& input_mode,
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<Register> vertex = {}) {
|
2018-10-07 04:17:31 +02:00
|
|
|
auto GeometryPass = [&](const std::string& name) {
|
|
|
|
if (stage == Maxwell3D::Regs::ShaderStage::Geometry && vertex) {
|
2018-11-10 06:41:33 +01:00
|
|
|
// TODO(Rodrigo): Guard geometry inputs against out of bound reads. Some games set
|
|
|
|
// an 0x80000000 index for those and the shader fails to build. Find out why this
|
|
|
|
// happens and what's its intent.
|
|
|
|
return "gs_" + name + '[' + GetRegisterAsInteger(*vertex, 0, false) +
|
|
|
|
" % MAX_VERTEX_INPUT]";
|
2018-10-07 04:17:31 +02:00
|
|
|
}
|
|
|
|
return name;
|
|
|
|
};
|
|
|
|
|
2018-04-16 02:26:45 +02:00
|
|
|
switch (attribute) {
|
|
|
|
case Attribute::Index::Position:
|
2018-09-05 05:09:24 +02:00
|
|
|
if (stage != Maxwell3D::Regs::ShaderStage::Fragment) {
|
2018-10-07 04:17:31 +02:00
|
|
|
return GeometryPass("position");
|
2018-09-05 05:09:24 +02:00
|
|
|
} else {
|
|
|
|
return "vec4(gl_FragCoord.x, gl_FragCoord.y, gl_FragCoord.z, 1.0)";
|
|
|
|
}
|
|
|
|
case Attribute::Index::PointCoord:
|
|
|
|
return "vec4(gl_PointCoord.x, gl_PointCoord.y, 0, 0)";
|
2018-05-30 17:58:03 +02:00
|
|
|
case Attribute::Index::TessCoordInstanceIDVertexID:
|
|
|
|
// TODO(Subv): Find out what the values are for the first two elements when inside a
|
|
|
|
// vertex shader, and what's the value of the fourth element when inside a Tess Eval
|
|
|
|
// shader.
|
|
|
|
ASSERT(stage == Maxwell3D::Regs::ShaderStage::Vertex);
|
2018-11-25 08:37:29 +01:00
|
|
|
// Config pack's first value is instance_id.
|
|
|
|
return "vec4(0, 0, uintBitsToFloat(config_pack[0]), uintBitsToFloat(gl_VertexID))";
|
2018-08-19 07:14:34 +02:00
|
|
|
case Attribute::Index::FrontFacing:
|
|
|
|
// TODO(Subv): Find out what the values are for the other elements.
|
|
|
|
ASSERT(stage == Maxwell3D::Regs::ShaderStage::Fragment);
|
|
|
|
return "vec4(0, 0, 0, uintBitsToFloat(gl_FrontFacing ? 1 : 0))";
|
2018-04-16 02:26:45 +02:00
|
|
|
default:
|
|
|
|
const u32 index{static_cast<u32>(attribute) -
|
|
|
|
static_cast<u32>(Attribute::Index::Attribute_0)};
|
2018-08-10 18:34:51 +02:00
|
|
|
if (attribute >= Attribute::Index::Attribute_0 &&
|
|
|
|
attribute <= Attribute::Index::Attribute_31) {
|
2018-09-05 05:09:24 +02:00
|
|
|
if (declr_input_attribute.count(attribute) == 0) {
|
|
|
|
declr_input_attribute[attribute] = input_mode;
|
|
|
|
} else {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(declr_input_attribute[attribute] != input_mode,
|
|
|
|
"Multiple input modes for the same attribute");
|
2018-09-05 05:09:24 +02:00
|
|
|
}
|
2018-10-07 04:17:31 +02:00
|
|
|
return GeometryPass("input_attribute_" + std::to_string(index));
|
2018-04-16 02:26:45 +02:00
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled input attribute: {}", static_cast<u32>(attribute));
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
2018-08-08 08:25:00 +02:00
|
|
|
|
|
|
|
return "vec4(0, 0, 0, 0)";
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 05:09:24 +02:00
|
|
|
std::string GetInputFlags(const Attribute::Index attribute) {
|
|
|
|
const Tegra::Shader::IpaSampleMode sample_mode =
|
|
|
|
declr_input_attribute[attribute].sampling_mode;
|
|
|
|
const Tegra::Shader::IpaInterpMode interp_mode =
|
|
|
|
declr_input_attribute[attribute].interpolation_mode;
|
|
|
|
std::string out;
|
|
|
|
switch (interp_mode) {
|
|
|
|
case Tegra::Shader::IpaInterpMode::Flat: {
|
|
|
|
out += "flat ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Tegra::Shader::IpaInterpMode::Linear: {
|
|
|
|
out += "noperspective ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Tegra::Shader::IpaInterpMode::Perspective: {
|
|
|
|
// Default, Smooth
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled IPA interp mode: {}", static_cast<u32>(interp_mode));
|
2018-09-05 05:09:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (sample_mode) {
|
2018-11-21 01:20:41 +01:00
|
|
|
case Tegra::Shader::IpaSampleMode::Centroid:
|
|
|
|
// It can be implemented with the "centroid " keyword in glsl
|
|
|
|
UNIMPLEMENTED_MSG("Unimplemented IPA sampler mode centroid");
|
2018-09-05 05:09:24 +02:00
|
|
|
break;
|
2018-11-21 01:20:41 +01:00
|
|
|
case Tegra::Shader::IpaSampleMode::Default:
|
2018-09-05 05:09:24 +02:00
|
|
|
// Default, n/a
|
|
|
|
break;
|
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented IPA sampler mode: {}", static_cast<u32>(sample_mode));
|
|
|
|
break;
|
2018-09-05 05:09:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
/// Generates code representing the declaration name of an output attribute register.
|
2018-04-08 05:48:38 +02:00
|
|
|
std::string GetOutputAttribute(Attribute::Index attribute) {
|
|
|
|
switch (attribute) {
|
2018-10-23 03:50:03 +02:00
|
|
|
case Attribute::Index::PointSize:
|
|
|
|
return "gl_PointSize";
|
2018-04-08 05:48:38 +02:00
|
|
|
case Attribute::Index::Position:
|
2018-04-16 02:26:45 +02:00
|
|
|
return "position";
|
2018-11-23 05:57:49 +01:00
|
|
|
case Attribute::Index::ClipDistances0123:
|
|
|
|
case Attribute::Index::ClipDistances4567: {
|
|
|
|
return "gl_ClipDistance";
|
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
default:
|
|
|
|
const u32 index{static_cast<u32>(attribute) -
|
|
|
|
static_cast<u32>(Attribute::Index::Attribute_0)};
|
|
|
|
if (attribute >= Attribute::Index::Attribute_0) {
|
|
|
|
declr_output_attribute.insert(attribute);
|
|
|
|
return "output_attribute_" + std::to_string(index);
|
|
|
|
}
|
|
|
|
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled output attribute={}", index);
|
2018-08-10 18:34:51 +02:00
|
|
|
return {};
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 04:48:06 +02:00
|
|
|
/// Generates code to use for a swizzle operation.
|
|
|
|
static std::string GetSwizzle(u64 elem) {
|
|
|
|
ASSERT(elem <= 3);
|
|
|
|
std::string swizzle = ".";
|
|
|
|
swizzle += "xyzw"[elem];
|
|
|
|
return swizzle;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderWriter& shader;
|
|
|
|
ShaderWriter& declarations;
|
|
|
|
std::vector<GLSLRegister> regs;
|
2018-09-05 05:09:24 +02:00
|
|
|
std::unordered_map<Attribute::Index, Tegra::Shader::IpaMode> declr_input_attribute;
|
2018-04-27 04:48:06 +02:00
|
|
|
std::set<Attribute::Index> declr_output_attribute;
|
|
|
|
std::array<ConstBufferEntry, Maxwell3D::Regs::MaxConstBuffers> declr_const_buffers;
|
2018-06-06 19:58:16 +02:00
|
|
|
std::vector<SamplerEntry> used_samplers;
|
2018-04-27 04:48:06 +02:00
|
|
|
const Maxwell3D::Regs::ShaderStage& stage;
|
2018-07-13 04:25:03 +02:00
|
|
|
const std::string& suffix;
|
2018-10-07 04:17:31 +02:00
|
|
|
const Tegra::Shader::Header& header;
|
2018-10-23 03:50:03 +02:00
|
|
|
std::unordered_set<Attribute::Index> fixed_pipeline_output_attributes_used;
|
2018-10-20 01:47:06 +02:00
|
|
|
u64 local_memory_size;
|
2018-04-27 04:48:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class GLSLGenerator {
|
|
|
|
public:
|
|
|
|
GLSLGenerator(const std::set<Subroutine>& subroutines, const ProgramCode& program_code,
|
2018-11-20 20:14:48 +01:00
|
|
|
u32 main_offset, Maxwell3D::Regs::ShaderStage stage, const std::string& suffix,
|
|
|
|
std::size_t shader_length)
|
2018-04-27 04:48:06 +02:00
|
|
|
: subroutines(subroutines), program_code(program_code), main_offset(main_offset),
|
2018-11-20 20:14:48 +01:00
|
|
|
stage(stage), suffix(suffix), shader_length(shader_length) {
|
2018-09-11 03:00:55 +02:00
|
|
|
std::memcpy(&header, program_code.data(), sizeof(Tegra::Shader::Header));
|
2018-10-20 03:48:21 +02:00
|
|
|
local_memory_size = header.GetLocalMemorySize();
|
2018-10-20 01:47:06 +02:00
|
|
|
regs.SetLocalMemory(local_memory_size);
|
2018-07-13 04:25:03 +02:00
|
|
|
Generate(suffix);
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetShaderCode() {
|
|
|
|
return declarations.GetResult() + shader.GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns entries in the shader that are useful for external functions
|
|
|
|
ShaderEntries GetEntries() const {
|
2018-11-20 20:14:48 +01:00
|
|
|
return {regs.GetConstBuffersDeclarations(), regs.GetSamplers(), shader_length};
|
2018-04-27 04:48:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Gets the Subroutine object corresponding to the specified address.
|
|
|
|
const Subroutine& GetSubroutine(u32 begin, u32 end) const {
|
2018-09-09 17:51:43 +02:00
|
|
|
const auto iter = subroutines.find(Subroutine{begin, end, suffix});
|
2018-04-27 04:48:06 +02:00
|
|
|
ASSERT(iter != subroutines.end());
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:34:50 +02:00
|
|
|
/// Generates code representing a 19-bit immediate value
|
|
|
|
static std::string GetImmediate19(const Instruction& instr) {
|
2018-08-08 07:27:12 +02:00
|
|
|
return fmt::format("uintBitsToFloat({})", instr.alu.GetImm20_19());
|
2018-04-19 20:34:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates code representing a 32-bit immediate value
|
|
|
|
static std::string GetImmediate32(const Instruction& instr) {
|
2018-08-08 07:27:12 +02:00
|
|
|
return fmt::format("uintBitsToFloat({})", instr.alu.GetImm20_32());
|
2018-04-16 02:45:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 07:07:16 +02:00
|
|
|
/// Generates code representing a vec2 pair unpacked from a half float immediate
|
|
|
|
static std::string UnpackHalfImmediate(const Instruction& instr, bool negate) {
|
|
|
|
const std::string immediate = GetHalfFloat(std::to_string(instr.half_imm.PackImmediates()));
|
|
|
|
if (!negate) {
|
|
|
|
return immediate;
|
|
|
|
}
|
|
|
|
const std::string negate_first = instr.half_imm.first_negate != 0 ? "-" : "";
|
|
|
|
const std::string negate_second = instr.half_imm.second_negate != 0 ? "-" : "";
|
|
|
|
const std::string negate_vec = "vec2(" + negate_first + "1, " + negate_second + "1)";
|
|
|
|
|
|
|
|
return '(' + immediate + " * " + negate_vec + ')';
|
|
|
|
}
|
|
|
|
|
2018-04-10 07:26:15 +02:00
|
|
|
/// Generates code representing a texture sampler.
|
2018-09-19 07:18:20 +02:00
|
|
|
std::string GetSampler(const Sampler& sampler, Tegra::Shader::TextureType type, bool is_array,
|
|
|
|
bool is_shadow) {
|
|
|
|
return regs.AccessSampler(sampler, type, is_array, is_shadow);
|
2018-04-10 07:26:15 +02:00
|
|
|
}
|
|
|
|
|
2018-04-08 05:48:38 +02:00
|
|
|
/**
|
|
|
|
* Adds code that calls a subroutine.
|
|
|
|
* @param subroutine the subroutine to call.
|
|
|
|
*/
|
|
|
|
void CallSubroutine(const Subroutine& subroutine) {
|
|
|
|
if (subroutine.exit_method == ExitMethod::AlwaysEnd) {
|
|
|
|
shader.AddLine(subroutine.GetName() + "();");
|
|
|
|
shader.AddLine("return true;");
|
|
|
|
} else if (subroutine.exit_method == ExitMethod::Conditional) {
|
|
|
|
shader.AddLine("if (" + subroutine.GetName() + "()) { return true; }");
|
|
|
|
} else {
|
|
|
|
shader.AddLine(subroutine.GetName() + "();");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 16:09:50 +02:00
|
|
|
/*
|
|
|
|
* Writes code that assigns a predicate boolean variable.
|
|
|
|
* @param pred The id of the predicate to write to.
|
|
|
|
* @param value The expression value to assign to the predicate.
|
|
|
|
*/
|
|
|
|
void SetPredicate(u64 pred, const std::string& value) {
|
|
|
|
using Tegra::Shader::Pred;
|
|
|
|
// Can't assign to the constant predicate.
|
|
|
|
ASSERT(pred != static_cast<u64>(Pred::UnusedIndex));
|
|
|
|
|
2018-10-20 20:25:12 +02:00
|
|
|
std::string variable = 'p' + std::to_string(pred) + '_' + suffix;
|
2018-04-20 16:09:50 +02:00
|
|
|
shader.AddLine(variable + " = " + value + ';');
|
|
|
|
declr_predicates.insert(std::move(variable));
|
|
|
|
}
|
|
|
|
|
2018-04-20 16:16:55 +02:00
|
|
|
/*
|
|
|
|
* Returns the condition to use in the 'if' for a predicated instruction.
|
|
|
|
* @param instr Instruction to generate the if condition for.
|
|
|
|
* @returns string containing the predicate condition.
|
|
|
|
*/
|
2018-08-09 03:51:09 +02:00
|
|
|
std::string GetPredicateCondition(u64 index, bool negate) {
|
2018-04-20 16:16:55 +02:00
|
|
|
using Tegra::Shader::Pred;
|
2018-05-21 00:53:06 +02:00
|
|
|
std::string variable;
|
2018-04-20 16:16:55 +02:00
|
|
|
|
2018-05-21 00:53:06 +02:00
|
|
|
// Index 7 is used as an 'Always True' condition.
|
2018-08-09 03:51:09 +02:00
|
|
|
if (index == static_cast<u64>(Pred::UnusedIndex)) {
|
2018-05-21 00:53:06 +02:00
|
|
|
variable = "true";
|
2018-08-09 03:51:09 +02:00
|
|
|
} else {
|
2018-07-13 04:25:03 +02:00
|
|
|
variable = 'p' + std::to_string(index) + '_' + suffix;
|
2018-08-09 03:51:09 +02:00
|
|
|
declr_predicates.insert(variable);
|
|
|
|
}
|
2018-05-21 00:53:06 +02:00
|
|
|
if (negate) {
|
2018-04-20 16:16:55 +02:00
|
|
|
return "!(" + variable + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
return variable;
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:22:36 +02:00
|
|
|
/**
|
|
|
|
* Returns the comparison string to use to compare two values in the 'set' family of
|
|
|
|
* instructions.
|
2018-06-30 09:00:39 +02:00
|
|
|
* @param condition The condition used in the 'set'-family instruction.
|
|
|
|
* @param op_a First operand to use for the comparison.
|
|
|
|
* @param op_b Second operand to use for the comparison.
|
2018-05-25 00:22:36 +02:00
|
|
|
* @returns String corresponding to the GLSL operator that matches the desired comparison.
|
|
|
|
*/
|
2018-06-30 09:00:39 +02:00
|
|
|
std::string GetPredicateComparison(Tegra::Shader::PredCondition condition,
|
|
|
|
const std::string& op_a, const std::string& op_b) const {
|
2018-05-25 00:22:36 +02:00
|
|
|
using Tegra::Shader::PredCondition;
|
|
|
|
static const std::unordered_map<PredCondition, const char*> PredicateComparisonStrings = {
|
2018-11-23 17:51:32 +01:00
|
|
|
{PredCondition::LessThan, "<"},
|
|
|
|
{PredCondition::Equal, "=="},
|
|
|
|
{PredCondition::LessEqual, "<="},
|
|
|
|
{PredCondition::GreaterThan, ">"},
|
|
|
|
{PredCondition::NotEqual, "!="},
|
|
|
|
{PredCondition::GreaterEqual, ">="},
|
|
|
|
{PredCondition::LessThanWithNan, "<"},
|
|
|
|
{PredCondition::NotEqualWithNan, "!="},
|
|
|
|
{PredCondition::LessEqualWithNan, "<="},
|
|
|
|
{PredCondition::GreaterThanWithNan, ">"},
|
|
|
|
{PredCondition::GreaterEqualWithNan, ">="}};
|
2018-05-25 00:22:36 +02:00
|
|
|
|
2018-06-30 09:00:39 +02:00
|
|
|
const auto& comparison{PredicateComparisonStrings.find(condition)};
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(comparison == PredicateComparisonStrings.end(),
|
|
|
|
"Unknown predicate comparison operation");
|
2018-06-30 09:00:39 +02:00
|
|
|
|
|
|
|
std::string predicate{'(' + op_a + ") " + comparison->second + " (" + op_b + ')'};
|
2018-07-13 02:03:45 +02:00
|
|
|
if (condition == PredCondition::LessThanWithNan ||
|
2018-08-18 09:49:59 +02:00
|
|
|
condition == PredCondition::NotEqualWithNan ||
|
2018-11-23 17:51:32 +01:00
|
|
|
condition == PredCondition::LessEqualWithNan ||
|
2018-08-31 09:40:18 +02:00
|
|
|
condition == PredCondition::GreaterThanWithNan ||
|
|
|
|
condition == PredCondition::GreaterEqualWithNan) {
|
2018-06-30 09:00:39 +02:00
|
|
|
predicate += " || isnan(" + op_a + ") || isnan(" + op_b + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
return predicate;
|
2018-05-25 00:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the operator string to use to combine two predicates in the 'setp' family of
|
|
|
|
* instructions.
|
|
|
|
* @params operation The operator used in the 'setp'-family instruction.
|
|
|
|
* @returns String corresponding to the GLSL operator that matches the desired operator.
|
|
|
|
*/
|
|
|
|
std::string GetPredicateCombiner(Tegra::Shader::PredOperation operation) const {
|
|
|
|
using Tegra::Shader::PredOperation;
|
|
|
|
static const std::unordered_map<PredOperation, const char*> PredicateOperationStrings = {
|
|
|
|
{PredOperation::And, "&&"},
|
|
|
|
{PredOperation::Or, "||"},
|
2018-05-25 00:28:54 +02:00
|
|
|
{PredOperation::Xor, "^^"},
|
2018-05-25 00:22:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto op = PredicateOperationStrings.find(operation);
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(op == PredicateOperationStrings.end(), "Unknown predicate operation");
|
2018-05-25 00:22:36 +02:00
|
|
|
return op->second;
|
|
|
|
}
|
|
|
|
|
2018-08-16 16:05:16 +02:00
|
|
|
/**
|
|
|
|
* Transforms the input string GLSL operand into one that applies the abs() function and negates
|
|
|
|
* the output if necessary. When both abs and neg are true, the negation will be applied after
|
|
|
|
* taking the absolute value.
|
|
|
|
* @param operand The input operand to take the abs() of, negate, or both.
|
|
|
|
* @param abs Whether to apply the abs() function to the input operand.
|
|
|
|
* @param neg Whether to negate the input operand.
|
|
|
|
* @returns String corresponding to the operand after being transformed by the abs() and
|
|
|
|
* negation operations.
|
|
|
|
*/
|
|
|
|
static std::string GetOperandAbsNeg(const std::string& operand, bool abs, bool neg) {
|
|
|
|
std::string result = operand;
|
|
|
|
|
|
|
|
if (abs) {
|
|
|
|
result = "abs(" + result + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (neg) {
|
|
|
|
result = "-(" + result + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-15 06:58:30 +02:00
|
|
|
/*
|
|
|
|
* Transforms the input string GLSL operand into an unpacked half float pair.
|
|
|
|
* @note This function returns a float type pair instead of a half float pair. This is because
|
2018-10-20 20:19:28 +02:00
|
|
|
* real half floats are not standardized in GLSL but unpackHalf2x16 (which returns a vec2) is.
|
2018-10-15 06:58:30 +02:00
|
|
|
* @param operand Input operand. It has to be an unsigned integer.
|
|
|
|
* @param type How to unpack the unsigned integer to a half float pair.
|
|
|
|
* @param abs Get the absolute value of unpacked half floats.
|
|
|
|
* @param neg Get the negative value of unpacked half floats.
|
|
|
|
* @returns String corresponding to a half float pair.
|
|
|
|
*/
|
|
|
|
static std::string GetHalfFloat(const std::string& operand,
|
|
|
|
Tegra::Shader::HalfType type = Tegra::Shader::HalfType::H0_H1,
|
|
|
|
bool abs = false, bool neg = false) {
|
|
|
|
// "vec2" calls emitted in this function are intended to alias components.
|
|
|
|
const std::string value = [&]() {
|
|
|
|
switch (type) {
|
|
|
|
case Tegra::Shader::HalfType::H0_H1:
|
|
|
|
return "unpackHalf2x16(" + operand + ')';
|
|
|
|
case Tegra::Shader::HalfType::F32:
|
|
|
|
return "vec2(uintBitsToFloat(" + operand + "))";
|
|
|
|
case Tegra::Shader::HalfType::H0_H0:
|
|
|
|
case Tegra::Shader::HalfType::H1_H1: {
|
|
|
|
const bool high = type == Tegra::Shader::HalfType::H1_H1;
|
|
|
|
const char unpack_index = "xy"[high ? 1 : 0];
|
|
|
|
return "vec2(unpackHalf2x16(" + operand + ")." + unpack_index + ')';
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return std::string("vec2(0)");
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
return GetOperandAbsNeg(value, abs, neg);
|
|
|
|
}
|
|
|
|
|
2018-04-20 16:02:28 +02:00
|
|
|
/*
|
|
|
|
* Returns whether the instruction at the specified offset is a 'sched' instruction.
|
|
|
|
* Sched instructions always appear before a sequence of 3 instructions.
|
|
|
|
*/
|
|
|
|
bool IsSchedInstruction(u32 offset) const {
|
|
|
|
// sched instructions appear once every 4 instructions.
|
2018-09-15 15:21:06 +02:00
|
|
|
static constexpr std::size_t SchedPeriod = 4;
|
2018-04-20 16:02:28 +02:00
|
|
|
u32 absolute_offset = offset - main_offset;
|
|
|
|
|
|
|
|
return (absolute_offset % SchedPeriod) == 0;
|
|
|
|
}
|
|
|
|
|
2018-06-17 18:49:34 +02:00
|
|
|
void WriteLogicOperation(Register dest, LogicOperation logic_op, const std::string& op_a,
|
2018-08-18 21:36:37 +02:00
|
|
|
const std::string& op_b,
|
|
|
|
Tegra::Shader::PredicateResultMode predicate_mode,
|
|
|
|
Tegra::Shader::Pred predicate) {
|
|
|
|
std::string result{};
|
2018-06-17 18:49:34 +02:00
|
|
|
switch (logic_op) {
|
|
|
|
case LogicOperation::And: {
|
2018-08-18 21:36:37 +02:00
|
|
|
result = '(' + op_a + " & " + op_b + ')';
|
2018-06-17 18:49:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LogicOperation::Or: {
|
2018-08-18 21:36:37 +02:00
|
|
|
result = '(' + op_a + " | " + op_b + ')';
|
2018-06-17 18:49:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LogicOperation::Xor: {
|
2018-08-18 21:36:37 +02:00
|
|
|
result = '(' + op_a + " ^ " + op_b + ')';
|
2018-06-17 18:49:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LogicOperation::PassB: {
|
2018-08-18 21:36:37 +02:00
|
|
|
result = op_b;
|
2018-06-17 18:49:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented logic operation={}", static_cast<u32>(logic_op));
|
2018-06-17 18:49:34 +02:00
|
|
|
}
|
2018-08-18 21:36:37 +02:00
|
|
|
|
|
|
|
if (dest != Tegra::Shader::Register::ZeroIndex) {
|
|
|
|
regs.SetRegisterToInteger(dest, true, 0, result, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
using Tegra::Shader::PredicateResultMode;
|
|
|
|
// Write the predicate value depending on the predicate mode.
|
|
|
|
switch (predicate_mode) {
|
|
|
|
case PredicateResultMode::None:
|
|
|
|
// Do nothing.
|
|
|
|
return;
|
|
|
|
case PredicateResultMode::NotZero:
|
|
|
|
// Set the predicate to true if the result is not zero.
|
|
|
|
SetPredicate(static_cast<u64>(predicate), '(' + result + ") != 0");
|
|
|
|
break;
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented predicate result mode: {}",
|
|
|
|
static_cast<u32>(predicate_mode));
|
2018-08-18 21:36:37 +02:00
|
|
|
}
|
2018-06-17 18:49:34 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 09:02:18 +02:00
|
|
|
void WriteLop3Instruction(Register dest, const std::string& op_a, const std::string& op_b,
|
|
|
|
const std::string& op_c, const std::string& imm_lut) {
|
2018-08-22 23:01:40 +02:00
|
|
|
if (dest == Tegra::Shader::Register::ZeroIndex) {
|
2018-08-22 20:33:32 +02:00
|
|
|
return;
|
2018-08-22 23:01:40 +02:00
|
|
|
}
|
2018-08-22 20:33:32 +02:00
|
|
|
|
2018-08-22 23:01:40 +02:00
|
|
|
static constexpr std::array<const char*, 32> shift_amounts = {
|
2018-08-22 20:33:32 +02:00
|
|
|
"0", "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"};
|
|
|
|
|
2018-08-22 09:02:18 +02:00
|
|
|
std::string result;
|
|
|
|
result += '(';
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t i = 0; i < shift_amounts.size(); ++i) {
|
2018-08-22 09:02:18 +02:00
|
|
|
if (i)
|
|
|
|
result += '|';
|
2018-08-22 23:01:40 +02:00
|
|
|
result += "(((" + imm_lut + " >> (((" + op_c + " >> " + shift_amounts[i] +
|
|
|
|
") & 1) | ((" + op_b + " >> " + shift_amounts[i] + ") & 1) << 1 | ((" + op_a +
|
|
|
|
" >> " + shift_amounts[i] + ") & 1) << 2)) & 1) << " + shift_amounts[i] + ")";
|
2018-08-22 09:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result += ')';
|
|
|
|
|
2018-08-22 20:33:32 +02:00
|
|
|
regs.SetRegisterToInteger(dest, true, 0, result, 1, 1);
|
2018-08-22 09:02:18 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 06:22:10 +01:00
|
|
|
void WriteTexsInstruction(const Instruction& instr, const std::string& texture) {
|
2018-08-19 21:00:12 +02:00
|
|
|
// TEXS has two destination registers and a swizzle. The first two elements in the swizzle
|
|
|
|
// go into gpr0+0 and gpr0+1, and the rest goes into gpr28+0 and gpr28+1
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t written_components = 0;
|
2018-08-19 21:00:12 +02:00
|
|
|
for (u32 component = 0; component < 4; ++component) {
|
|
|
|
if (!instr.texs.IsComponentEnabled(component)) {
|
|
|
|
continue;
|
2018-07-24 02:02:05 +02:00
|
|
|
}
|
|
|
|
|
2018-08-19 21:00:12 +02:00
|
|
|
if (written_components < 2) {
|
|
|
|
// Write the first two swizzle components to gpr0 and gpr0+1
|
2018-11-28 06:22:10 +01:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, component, texture, 1, 4, false,
|
2018-08-19 21:00:12 +02:00
|
|
|
written_components % 2);
|
|
|
|
} else {
|
|
|
|
ASSERT(instr.texs.HasTwoDestinations());
|
|
|
|
// Write the rest of the swizzle components to gpr28 and gpr28+1
|
2018-11-28 06:22:10 +01:00
|
|
|
regs.SetRegisterToFloat(instr.gpr28, component, texture, 1, 4, false,
|
2018-08-19 21:00:12 +02:00
|
|
|
written_components % 2);
|
2018-07-24 02:02:05 +02:00
|
|
|
}
|
|
|
|
|
2018-08-19 21:00:12 +02:00
|
|
|
++written_components;
|
2018-07-24 02:02:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 07:18:20 +02:00
|
|
|
static u32 TextureCoordinates(Tegra::Shader::TextureType texture_type) {
|
|
|
|
switch (texture_type) {
|
2018-11-21 01:20:41 +01:00
|
|
|
case Tegra::Shader::TextureType::Texture1D:
|
2018-09-19 07:18:20 +02:00
|
|
|
return 1;
|
2018-11-21 01:20:41 +01:00
|
|
|
case Tegra::Shader::TextureType::Texture2D:
|
2018-09-19 07:18:20 +02:00
|
|
|
return 2;
|
2018-10-16 03:04:20 +02:00
|
|
|
case Tegra::Shader::TextureType::Texture3D:
|
2018-11-21 01:20:41 +01:00
|
|
|
case Tegra::Shader::TextureType::TextureCube:
|
2018-09-19 07:18:20 +02:00
|
|
|
return 3;
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled texture type: {}", static_cast<u32>(texture_type));
|
2018-09-19 07:18:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 17:48:12 +02:00
|
|
|
/*
|
2018-10-16 22:11:26 +02:00
|
|
|
* Emits code to push the input target address to the flow address stack, incrementing the stack
|
2018-08-18 17:48:12 +02:00
|
|
|
* top.
|
|
|
|
*/
|
2018-10-16 22:11:26 +02:00
|
|
|
void EmitPushToFlowStack(u32 target) {
|
2018-11-28 06:44:15 +01:00
|
|
|
const auto scope = shader.Scope();
|
|
|
|
|
2018-10-16 22:11:26 +02:00
|
|
|
shader.AddLine("flow_stack[flow_stack_top] = " + std::to_string(target) + "u;");
|
|
|
|
shader.AddLine("flow_stack_top++;");
|
2018-08-18 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-10-16 22:11:26 +02:00
|
|
|
* Emits code to pop an address from the flow address stack, setting the jump address to the
|
2018-08-18 17:48:12 +02:00
|
|
|
* popped address and decrementing the stack top.
|
|
|
|
*/
|
2018-10-16 22:11:26 +02:00
|
|
|
void EmitPopFromFlowStack() {
|
2018-11-28 06:44:15 +01:00
|
|
|
const auto scope = shader.Scope();
|
|
|
|
|
2018-10-16 22:11:26 +02:00
|
|
|
shader.AddLine("flow_stack_top--;");
|
|
|
|
shader.AddLine("jmp_to = flow_stack[flow_stack_top];");
|
2018-08-18 17:48:12 +02:00
|
|
|
shader.AddLine("break;");
|
|
|
|
}
|
|
|
|
|
2018-08-18 23:59:44 +02:00
|
|
|
/// Writes the output values from a fragment shader to the corresponding GLSL output variables.
|
|
|
|
void EmitFragmentOutputsWrite() {
|
|
|
|
ASSERT(stage == Maxwell3D::Regs::ShaderStage::Fragment);
|
|
|
|
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(header.ps.omap.sample_mask != 0, "Samplemask write is unimplemented");
|
2018-08-18 23:59:44 +02:00
|
|
|
|
2018-10-10 22:34:15 +02:00
|
|
|
shader.AddLine("if (alpha_test[0] != 0) {");
|
2018-10-09 05:34:11 +02:00
|
|
|
++shader.scope;
|
2018-10-10 22:34:15 +02:00
|
|
|
// We start on the register containing the alpha value in the first RT.
|
2018-10-09 05:34:11 +02:00
|
|
|
u32 current_reg = 3;
|
|
|
|
for (u32 render_target = 0; render_target < Maxwell3D::Regs::NumRenderTargets;
|
|
|
|
++render_target) {
|
2018-10-10 22:34:15 +02:00
|
|
|
// TODO(Blinkhawk): verify the behavior of alpha testing on hardware when
|
|
|
|
// multiple render targets are used.
|
2018-10-09 05:34:11 +02:00
|
|
|
if (header.ps.IsColorComponentOutputEnabled(render_target, 0) ||
|
|
|
|
header.ps.IsColorComponentOutputEnabled(render_target, 1) ||
|
|
|
|
header.ps.IsColorComponentOutputEnabled(render_target, 2) ||
|
|
|
|
header.ps.IsColorComponentOutputEnabled(render_target, 3)) {
|
2018-10-12 02:29:11 +02:00
|
|
|
shader.AddLine(fmt::format("if (!AlphaFunc({})) discard;",
|
|
|
|
regs.GetRegisterAsFloat(current_reg)));
|
2018-10-09 05:34:11 +02:00
|
|
|
current_reg += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--shader.scope;
|
2018-10-10 15:20:13 +02:00
|
|
|
shader.AddLine('}');
|
2018-10-09 05:34:11 +02:00
|
|
|
|
2018-08-18 23:59:44 +02:00
|
|
|
// Write the color outputs using the data in the shader registers, disabled
|
|
|
|
// rendertargets/components are skipped in the register assignment.
|
2018-10-09 05:34:11 +02:00
|
|
|
current_reg = 0;
|
2018-08-18 23:59:44 +02:00
|
|
|
for (u32 render_target = 0; render_target < Maxwell3D::Regs::NumRenderTargets;
|
|
|
|
++render_target) {
|
|
|
|
// TODO(Subv): Figure out how dual-source blending is configured in the Switch.
|
|
|
|
for (u32 component = 0; component < 4; ++component) {
|
2018-09-11 18:08:06 +02:00
|
|
|
if (header.ps.IsColorComponentOutputEnabled(render_target, component)) {
|
2018-09-11 05:26:56 +02:00
|
|
|
shader.AddLine(fmt::format("FragColor{}[{}] = {};", render_target, component,
|
2018-08-18 23:59:44 +02:00
|
|
|
regs.GetRegisterAsFloat(current_reg)));
|
|
|
|
++current_reg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-21 04:57:56 +02:00
|
|
|
|
2018-09-11 18:08:06 +02:00
|
|
|
if (header.ps.omap.depth) {
|
2018-08-21 04:57:56 +02:00
|
|
|
// The depth output is always 2 registers after the last color output, and current_reg
|
|
|
|
// already contains one past the last color register.
|
2018-09-09 17:51:43 +02:00
|
|
|
|
|
|
|
shader.AddLine(
|
|
|
|
"gl_FragDepth = " +
|
|
|
|
regs.GetRegisterAsFloat(static_cast<Tegra::Shader::Register>(current_reg) + 1) +
|
|
|
|
';');
|
2018-08-21 04:57:56 +02:00
|
|
|
}
|
2018-08-18 23:59:44 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 07:38:58 +02:00
|
|
|
/// Unpacks a video instruction operand (e.g. VMAD).
|
|
|
|
std::string GetVideoOperand(const std::string& op, bool is_chunk, bool is_signed,
|
|
|
|
Tegra::Shader::VideoType type, u64 byte_height) {
|
|
|
|
const std::string value = [&]() {
|
|
|
|
if (!is_chunk) {
|
|
|
|
const auto offset = static_cast<u32>(byte_height * 8);
|
|
|
|
return "((" + op + " >> " + std::to_string(offset) + ") & 0xff)";
|
|
|
|
}
|
|
|
|
const std::string zero = "0";
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Tegra::Shader::VideoType::Size16_Low:
|
|
|
|
return '(' + op + " & 0xffff)";
|
|
|
|
case Tegra::Shader::VideoType::Size16_High:
|
|
|
|
return '(' + op + " >> 16)";
|
|
|
|
case Tegra::Shader::VideoType::Size32:
|
|
|
|
// TODO(Rodrigo): From my hardware tests it becomes a bit "mad" when
|
|
|
|
// this type is used (1 * 1 + 0 == 0x5b800000). Until a better
|
2018-11-21 01:20:41 +01:00
|
|
|
// explanation is found: abort.
|
2018-10-18 07:38:58 +02:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
return zero;
|
|
|
|
case Tegra::Shader::VideoType::Invalid:
|
|
|
|
UNREACHABLE_MSG("Invalid instruction encoding");
|
|
|
|
return zero;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return zero;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
if (is_signed) {
|
|
|
|
return "int(" + value + ')';
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Gets the A operand for a video instruction.
|
|
|
|
std::string GetVideoOperandA(Instruction instr) {
|
|
|
|
return GetVideoOperand(regs.GetRegisterAsInteger(instr.gpr8, 0, false),
|
|
|
|
instr.video.is_byte_chunk_a != 0, instr.video.signed_a,
|
|
|
|
instr.video.type_a, instr.video.byte_height_a);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the B operand for a video instruction.
|
|
|
|
std::string GetVideoOperandB(Instruction instr) {
|
|
|
|
if (instr.video.use_register_b) {
|
|
|
|
return GetVideoOperand(regs.GetRegisterAsInteger(instr.gpr20, 0, false),
|
|
|
|
instr.video.is_byte_chunk_b != 0, instr.video.signed_b,
|
|
|
|
instr.video.type_b, instr.video.byte_height_b);
|
|
|
|
} else {
|
|
|
|
return '(' +
|
|
|
|
std::to_string(instr.video.signed_b ? static_cast<s16>(instr.alu.GetImm20_16())
|
|
|
|
: instr.alu.GetImm20_16()) +
|
|
|
|
')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 05:48:38 +02:00
|
|
|
/**
|
|
|
|
* Compiles a single instruction from Tegra to GLSL.
|
|
|
|
* @param offset the offset of the Tegra shader instruction.
|
|
|
|
* @return the offset of the next instruction to execute. Usually it is the current offset
|
|
|
|
* + 1. If the current instruction always terminates the program, returns PROGRAM_END.
|
|
|
|
*/
|
|
|
|
u32 CompileInstr(u32 offset) {
|
2018-04-20 16:02:28 +02:00
|
|
|
// Ignore sched instructions when generating code.
|
2018-04-21 02:49:05 +02:00
|
|
|
if (IsSchedInstruction(offset)) {
|
2018-04-20 16:02:28 +02:00
|
|
|
return offset + 1;
|
2018-04-21 02:49:05 +02:00
|
|
|
}
|
2018-04-20 16:02:28 +02:00
|
|
|
|
2018-04-08 05:48:38 +02:00
|
|
|
const Instruction instr = {program_code[offset]};
|
2018-04-21 02:49:05 +02:00
|
|
|
const auto opcode = OpCode::Decode(instr);
|
|
|
|
|
|
|
|
// Decoding failure
|
|
|
|
if (!opcode) {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled instruction: {0:x}", instr.value);
|
2018-06-27 07:14:34 +02:00
|
|
|
return offset + 1;
|
2018-04-21 02:49:05 +02:00
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
shader.AddLine(
|
2018-10-30 05:03:25 +01:00
|
|
|
fmt::format("// {}: {} (0x{:016x})", offset, opcode->get().GetName(), instr.value));
|
2018-04-08 05:48:38 +02:00
|
|
|
|
2018-04-20 16:16:55 +02:00
|
|
|
using Tegra::Shader::Pred;
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.pred.full_pred == Pred::NeverExecute,
|
|
|
|
"NeverExecute predicate not implemented");
|
2018-04-20 16:16:55 +02:00
|
|
|
|
2018-08-11 23:00:14 +02:00
|
|
|
// Some instructions (like SSY) don't have a predicate field, they are always
|
|
|
|
// unconditionally executed.
|
2018-10-30 05:03:25 +01:00
|
|
|
bool can_be_predicated = OpCode::IsPredicatedInstruction(opcode->get().GetId());
|
2018-08-11 23:00:14 +02:00
|
|
|
|
|
|
|
if (can_be_predicated && instr.pred.pred_index != static_cast<u64>(Pred::UnusedIndex)) {
|
2018-05-21 00:53:06 +02:00
|
|
|
shader.AddLine("if (" +
|
|
|
|
GetPredicateCondition(instr.pred.pred_index, instr.negate_pred != 0) +
|
|
|
|
')');
|
2018-04-20 16:16:55 +02:00
|
|
|
shader.AddLine('{');
|
|
|
|
++shader.scope;
|
|
|
|
}
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetType()) {
|
2018-04-08 05:48:38 +02:00
|
|
|
case OpCode::Type::Arithmetic: {
|
2018-06-19 02:55:04 +02:00
|
|
|
std::string op_a = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
|
|
|
|
std::string op_b;
|
2018-04-16 02:45:56 +02:00
|
|
|
|
|
|
|
if (instr.is_b_imm) {
|
2018-06-19 02:55:04 +02:00
|
|
|
op_b = GetImmediate19(instr);
|
2018-04-10 05:39:44 +02:00
|
|
|
} else {
|
2018-04-16 02:45:56 +02:00
|
|
|
if (instr.is_b_gpr) {
|
2018-06-19 02:55:04 +02:00
|
|
|
op_b = regs.GetRegisterAsFloat(instr.gpr20);
|
2018-04-16 02:45:56 +02:00
|
|
|
} else {
|
2018-06-19 02:55:04 +02:00
|
|
|
op_b = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Float);
|
2018-04-16 02:45:56 +02:00
|
|
|
}
|
2018-04-10 05:39:44 +02:00
|
|
|
}
|
2018-04-16 02:45:56 +02:00
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-04-29 19:50:52 +02:00
|
|
|
case OpCode::Id::MOV_C:
|
|
|
|
case OpCode::Id::MOV_R: {
|
2018-08-16 16:05:16 +02:00
|
|
|
// MOV does not have neither 'abs' nor 'neg' bits.
|
2018-04-29 19:13:13 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_b, 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-10 05:39:44 +02:00
|
|
|
case OpCode::Id::FMUL_C:
|
2018-04-16 02:45:56 +02:00
|
|
|
case OpCode::Id::FMUL_R:
|
|
|
|
case OpCode::Id::FMUL_IMM: {
|
2018-08-16 16:05:16 +02:00
|
|
|
// FMUL does not have 'abs' bits and only the second operand has a 'neg' bit.
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.fmul.tab5cb8_2 != 0,
|
|
|
|
"FMUL tab5cb8_2({}) is not implemented",
|
|
|
|
instr.fmul.tab5cb8_2.Value());
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.fmul.tab5c68_1 != 0,
|
|
|
|
"FMUL tab5cb8_1({}) is not implemented",
|
|
|
|
instr.fmul.tab5c68_1.Value());
|
|
|
|
UNIMPLEMENTED_IF_MSG(
|
|
|
|
instr.fmul.tab5c68_0 != 1, "FMUL tab5cb8_0({}) is not implemented",
|
|
|
|
instr.fmul.tab5c68_0
|
|
|
|
.Value()); // SMO typical sends 1 here which seems to be the default
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in FMUL is not implemented");
|
2018-09-01 11:05:10 +02:00
|
|
|
|
2018-08-16 16:05:16 +02:00
|
|
|
op_b = GetOperandAbsNeg(op_b, false, instr.fmul.negate_b);
|
2018-10-28 02:09:26 +02:00
|
|
|
|
2018-10-28 16:38:40 +01:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_a + " * " + op_b, 1, 1,
|
|
|
|
instr.alu.saturate_d, 0, true);
|
2018-04-10 05:39:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::FADD_C:
|
2018-04-16 02:45:56 +02:00
|
|
|
case OpCode::Id::FADD_R:
|
|
|
|
case OpCode::Id::FADD_IMM: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in FADD is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-08-16 16:05:16 +02:00
|
|
|
op_a = GetOperandAbsNeg(op_a, instr.alu.abs_a, instr.alu.negate_a);
|
|
|
|
op_b = GetOperandAbsNeg(op_b, instr.alu.abs_b, instr.alu.negate_b);
|
2018-10-28 02:09:26 +02:00
|
|
|
|
2018-10-28 16:38:40 +01:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_a + " + " + op_b, 1, 1,
|
|
|
|
instr.alu.saturate_d, 0, true);
|
2018-04-08 05:48:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-04-10 06:02:12 +02:00
|
|
|
case OpCode::Id::MUFU: {
|
2018-08-16 16:05:16 +02:00
|
|
|
op_a = GetOperandAbsNeg(op_a, instr.alu.abs_a, instr.alu.negate_a);
|
2018-04-10 06:02:12 +02:00
|
|
|
switch (instr.sub_op) {
|
2018-04-16 02:59:37 +02:00
|
|
|
case SubOp::Cos:
|
2018-04-27 04:48:06 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, "cos(" + op_a + ')', 1, 1,
|
2018-10-28 16:38:40 +01:00
|
|
|
instr.alu.saturate_d, 0, true);
|
2018-04-16 02:59:37 +02:00
|
|
|
break;
|
|
|
|
case SubOp::Sin:
|
2018-04-27 04:48:06 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, "sin(" + op_a + ')', 1, 1,
|
2018-10-28 16:38:40 +01:00
|
|
|
instr.alu.saturate_d, 0, true);
|
2018-04-16 02:59:37 +02:00
|
|
|
break;
|
|
|
|
case SubOp::Ex2:
|
2018-04-27 04:48:06 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, "exp2(" + op_a + ')', 1, 1,
|
2018-10-28 16:38:40 +01:00
|
|
|
instr.alu.saturate_d, 0, true);
|
2018-04-16 02:59:37 +02:00
|
|
|
break;
|
|
|
|
case SubOp::Lg2:
|
2018-04-27 04:48:06 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, "log2(" + op_a + ')', 1, 1,
|
2018-10-28 16:38:40 +01:00
|
|
|
instr.alu.saturate_d, 0, true);
|
2018-04-16 02:59:37 +02:00
|
|
|
break;
|
2018-04-10 06:02:12 +02:00
|
|
|
case SubOp::Rcp:
|
2018-06-09 08:36:33 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, "1.0 / " + op_a, 1, 1,
|
2018-10-28 16:38:40 +01:00
|
|
|
instr.alu.saturate_d, 0, true);
|
2018-04-16 02:59:37 +02:00
|
|
|
break;
|
|
|
|
case SubOp::Rsq:
|
2018-04-27 04:48:06 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, "inversesqrt(" + op_a + ')', 1, 1,
|
2018-10-28 16:38:40 +01:00
|
|
|
instr.alu.saturate_d, 0, true);
|
2018-04-16 02:59:37 +02:00
|
|
|
break;
|
2018-07-03 02:48:15 +02:00
|
|
|
case SubOp::Sqrt:
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, "sqrt(" + op_a + ')', 1, 1,
|
2018-10-28 16:38:40 +01:00
|
|
|
instr.alu.saturate_d, 0, true);
|
2018-07-03 02:48:15 +02:00
|
|
|
break;
|
2018-04-10 06:02:12 +02:00
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled MUFU sub op={0:x}",
|
|
|
|
static_cast<unsigned>(instr.sub_op.Value()));
|
2018-04-10 06:02:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-05-25 01:37:18 +02:00
|
|
|
case OpCode::Id::FMNMX_C:
|
|
|
|
case OpCode::Id::FMNMX_R:
|
|
|
|
case OpCode::Id::FMNMX_IMM: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in FMNMX is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-08-16 16:05:16 +02:00
|
|
|
op_a = GetOperandAbsNeg(op_a, instr.alu.abs_a, instr.alu.negate_a);
|
|
|
|
op_b = GetOperandAbsNeg(op_b, instr.alu.abs_b, instr.alu.negate_b);
|
|
|
|
|
2018-05-21 00:53:06 +02:00
|
|
|
std::string condition =
|
|
|
|
GetPredicateCondition(instr.alu.fmnmx.pred, instr.alu.fmnmx.negate_pred != 0);
|
|
|
|
std::string parameters = op_a + ',' + op_b;
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0,
|
|
|
|
'(' + condition + ") ? min(" + parameters + ") : max(" +
|
|
|
|
parameters + ')',
|
2018-10-28 16:38:40 +01:00
|
|
|
1, 1, false, 0, true);
|
2018-05-21 00:53:06 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-06-01 06:03:23 +02:00
|
|
|
case OpCode::Id::RRO_C:
|
|
|
|
case OpCode::Id::RRO_R:
|
|
|
|
case OpCode::Id::RRO_IMM: {
|
|
|
|
// Currently RRO is only implemented as a register move.
|
2018-08-16 16:05:16 +02:00
|
|
|
op_b = GetOperandAbsNeg(op_b, instr.alu.abs_b, instr.alu.negate_b);
|
2018-06-01 06:03:23 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_b, 1, 1);
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_WARNING(HW_GPU, "RRO instruction is incomplete");
|
2018-04-21 04:27:17 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-04-10 05:39:44 +02:00
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled arithmetic instruction: {}", opcode->get().GetName());
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
2018-04-10 05:39:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-19 02:50:35 +02:00
|
|
|
case OpCode::Type::ArithmeticImmediate: {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-06-19 02:50:35 +02:00
|
|
|
case OpCode::Id::MOV32_IMM: {
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, GetImmediate32(instr), 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::FMUL32_IMM: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.op_32.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in FMUL32 is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-10-23 02:17:04 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0,
|
|
|
|
regs.GetRegisterAsFloat(instr.gpr8) + " * " +
|
|
|
|
GetImmediate32(instr),
|
2018-10-28 16:38:40 +01:00
|
|
|
1, 1, instr.fmul32.saturate, 0, true);
|
2018-06-19 02:50:35 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-07-12 19:00:31 +02:00
|
|
|
case OpCode::Id::FADD32I: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.op_32.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in FADD32I is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-07-12 19:00:31 +02:00
|
|
|
std::string op_a = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
std::string op_b = GetImmediate32(instr);
|
|
|
|
|
|
|
|
if (instr.fadd32i.abs_a) {
|
|
|
|
op_a = "abs(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instr.fadd32i.negate_a) {
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instr.fadd32i.abs_b) {
|
|
|
|
op_b = "abs(" + op_b + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instr.fadd32i.negate_b) {
|
|
|
|
op_b = "-(" + op_b + ')';
|
|
|
|
}
|
|
|
|
|
2018-10-28 16:38:40 +01:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_a + " + " + op_b, 1, 1, false, 0, true);
|
2018-07-12 19:00:31 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-06-19 02:50:35 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-07 06:58:12 +02:00
|
|
|
case OpCode::Type::Bfe: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.bfe.negate_b);
|
2018-06-07 06:58:12 +02:00
|
|
|
|
|
|
|
std::string op_a = instr.bfe.negate_a ? "-" : "";
|
|
|
|
op_a += regs.GetRegisterAsInteger(instr.gpr8);
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-06-07 06:58:12 +02:00
|
|
|
case OpCode::Id::BFE_IMM: {
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in BFE is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-06-07 06:58:12 +02:00
|
|
|
std::string inner_shift =
|
|
|
|
'(' + op_a + " << " + std::to_string(instr.bfe.GetLeftShiftValue()) + ')';
|
|
|
|
std::string outer_shift =
|
|
|
|
'(' + inner_shift + " >> " +
|
|
|
|
std::to_string(instr.bfe.GetLeftShiftValue() + instr.bfe.shift_position) + ')';
|
|
|
|
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0, outer_shift, 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled BFE instruction: {}", opcode->get().GetName());
|
2018-06-07 06:58:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-11-21 20:01:35 +01:00
|
|
|
case OpCode::Type::Bfi: {
|
|
|
|
UNIMPLEMENTED_IF(instr.generates_cc);
|
|
|
|
|
|
|
|
const auto [base, packed_shift] = [&]() -> std::tuple<std::string, std::string> {
|
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::BFI_IMM_R:
|
|
|
|
return {regs.GetRegisterAsInteger(instr.gpr39, 0, false),
|
|
|
|
std::to_string(instr.alu.GetSignedImm20_20())};
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
const std::string offset = '(' + packed_shift + " & 0xff)";
|
|
|
|
const std::string bits = "((" + packed_shift + " >> 8) & 0xff)";
|
|
|
|
const std::string insert = regs.GetRegisterAsInteger(instr.gpr8, 0, false);
|
|
|
|
regs.SetRegisterToInteger(
|
|
|
|
instr.gpr0, false, 0,
|
|
|
|
"bitfieldInsert(" + base + ", " + insert + ", " + offset + ", " + bits + ')', 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-06-05 04:15:19 +02:00
|
|
|
case OpCode::Type::Shift: {
|
2018-06-06 00:38:25 +02:00
|
|
|
std::string op_a = regs.GetRegisterAsInteger(instr.gpr8, 0, true);
|
2018-06-05 04:15:19 +02:00
|
|
|
std::string op_b;
|
|
|
|
|
|
|
|
if (instr.is_b_imm) {
|
|
|
|
op_b += '(' + std::to_string(instr.alu.GetSignedImm20_20()) + ')';
|
|
|
|
} else {
|
|
|
|
if (instr.is_b_gpr) {
|
|
|
|
op_b += regs.GetRegisterAsInteger(instr.gpr20);
|
|
|
|
} else {
|
2018-06-06 04:45:22 +02:00
|
|
|
op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Integer);
|
2018-06-05 04:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-06-09 06:01:17 +02:00
|
|
|
case OpCode::Id::SHR_C:
|
|
|
|
case OpCode::Id::SHR_R:
|
|
|
|
case OpCode::Id::SHR_IMM: {
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in SHR is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-06-09 06:01:17 +02:00
|
|
|
if (!instr.shift.is_signed) {
|
|
|
|
// Logical shift right
|
|
|
|
op_a = "uint(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast to int is superfluous for arithmetic shift, it's only for a logical shift
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0, "int(" + op_a + " >> " + op_b + ')',
|
|
|
|
1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-06-05 04:15:19 +02:00
|
|
|
case OpCode::Id::SHL_C:
|
|
|
|
case OpCode::Id::SHL_R:
|
|
|
|
case OpCode::Id::SHL_IMM:
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in SHL is not implemented");
|
2018-06-05 04:15:19 +02:00
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0, op_a + " << " + op_b, 1, 1);
|
|
|
|
break;
|
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled shift instruction: {}", opcode->get().GetName());
|
2018-06-05 04:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-12 18:24:10 +02:00
|
|
|
case OpCode::Type::ArithmeticIntegerImmediate: {
|
|
|
|
std::string op_a = regs.GetRegisterAsInteger(instr.gpr8);
|
2018-06-17 18:49:34 +02:00
|
|
|
std::string op_b = std::to_string(instr.alu.imm20_32.Value());
|
2018-06-12 18:24:10 +02:00
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-06-12 18:24:10 +02:00
|
|
|
case OpCode::Id::IADD32I:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.op_32.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in IADD32I is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-06-17 18:49:34 +02:00
|
|
|
if (instr.iadd32i.negate_a)
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
|
2018-06-12 18:24:10 +02:00
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0, op_a + " + " + op_b, 1, 1,
|
|
|
|
instr.iadd32i.saturate != 0);
|
|
|
|
break;
|
2018-06-17 18:49:34 +02:00
|
|
|
case OpCode::Id::LOP32I: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.op_32.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in LOP32I is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-06-17 18:49:34 +02:00
|
|
|
if (instr.alu.lop32i.invert_a)
|
|
|
|
op_a = "~(" + op_a + ')';
|
|
|
|
|
|
|
|
if (instr.alu.lop32i.invert_b)
|
|
|
|
op_b = "~(" + op_b + ')';
|
|
|
|
|
2018-08-18 21:36:37 +02:00
|
|
|
WriteLogicOperation(instr.gpr0, instr.alu.lop32i.operation, op_a, op_b,
|
|
|
|
Tegra::Shader::PredicateResultMode::None,
|
|
|
|
Tegra::Shader::Pred::UnusedIndex);
|
2018-06-17 18:49:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-06-12 18:24:10 +02:00
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled ArithmeticIntegerImmediate instruction: {}",
|
|
|
|
opcode->get().GetName());
|
2018-06-12 18:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-09 05:25:22 +02:00
|
|
|
case OpCode::Type::ArithmeticInteger: {
|
2018-06-05 02:03:47 +02:00
|
|
|
std::string op_a = regs.GetRegisterAsInteger(instr.gpr8);
|
2018-06-17 19:26:11 +02:00
|
|
|
std::string op_b;
|
2018-06-05 02:03:47 +02:00
|
|
|
if (instr.is_b_imm) {
|
2018-06-05 04:15:19 +02:00
|
|
|
op_b += '(' + std::to_string(instr.alu.GetSignedImm20_20()) + ')';
|
2018-06-05 02:03:47 +02:00
|
|
|
} else {
|
|
|
|
if (instr.is_b_gpr) {
|
|
|
|
op_b += regs.GetRegisterAsInteger(instr.gpr20);
|
|
|
|
} else {
|
2018-06-06 04:45:22 +02:00
|
|
|
op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Integer);
|
2018-06-05 02:03:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-06-09 05:25:22 +02:00
|
|
|
case OpCode::Id::IADD_C:
|
|
|
|
case OpCode::Id::IADD_R:
|
|
|
|
case OpCode::Id::IADD_IMM: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in IADD is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-06-17 19:26:11 +02:00
|
|
|
if (instr.alu_integer.negate_a)
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
|
|
|
|
if (instr.alu_integer.negate_b)
|
|
|
|
op_b = "-(" + op_b + ')';
|
|
|
|
|
2018-06-09 08:36:33 +02:00
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0, op_a + " + " + op_b, 1, 1,
|
|
|
|
instr.alu.saturate_d);
|
2018-06-09 05:25:22 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-24 18:15:43 +02:00
|
|
|
case OpCode::Id::IADD3_C:
|
|
|
|
case OpCode::Id::IADD3_R:
|
|
|
|
case OpCode::Id::IADD3_IMM: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in IADD3 is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-08-24 18:15:43 +02:00
|
|
|
std::string op_c = regs.GetRegisterAsInteger(instr.gpr39);
|
|
|
|
|
|
|
|
auto apply_height = [](auto height, auto& oprand) {
|
|
|
|
switch (height) {
|
|
|
|
case Tegra::Shader::IAdd3Height::None:
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::IAdd3Height::LowerHalfWord:
|
|
|
|
oprand = "((" + oprand + ") & 0xFFFF)";
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::IAdd3Height::UpperHalfWord:
|
|
|
|
oprand = "((" + oprand + ") >> 16)";
|
|
|
|
break;
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled IADD3 height: {}",
|
|
|
|
static_cast<u32>(height.Value()));
|
2018-08-24 18:15:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
if (opcode->get().GetId() == OpCode::Id::IADD3_R) {
|
2018-08-24 18:15:43 +02:00
|
|
|
apply_height(instr.iadd3.height_a, op_a);
|
|
|
|
apply_height(instr.iadd3.height_b, op_b);
|
|
|
|
apply_height(instr.iadd3.height_c, op_c);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instr.iadd3.neg_a)
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
|
|
|
|
if (instr.iadd3.neg_b)
|
|
|
|
op_b = "-(" + op_b + ')';
|
|
|
|
|
|
|
|
if (instr.iadd3.neg_c)
|
|
|
|
op_c = "-(" + op_c + ')';
|
|
|
|
|
|
|
|
std::string result;
|
2018-10-30 05:03:25 +01:00
|
|
|
if (opcode->get().GetId() == OpCode::Id::IADD3_R) {
|
2018-08-24 18:15:43 +02:00
|
|
|
switch (instr.iadd3.mode) {
|
|
|
|
case Tegra::Shader::IAdd3Mode::RightShift:
|
|
|
|
// TODO(tech4me): According to
|
|
|
|
// https://envytools.readthedocs.io/en/latest/hw/graph/maxwell/cuda/int.html?highlight=iadd3
|
|
|
|
// The addition between op_a and op_b should be done in uint33, more
|
|
|
|
// investigation required
|
|
|
|
result = "(((" + op_a + " + " + op_b + ") >> 16) + " + op_c + ')';
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::IAdd3Mode::LeftShift:
|
|
|
|
result = "(((" + op_a + " + " + op_b + ") << 16) + " + op_c + ')';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = '(' + op_a + " + " + op_b + " + " + op_c + ')';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = '(' + op_a + " + " + op_b + " + " + op_c + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0, result, 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-06-09 05:25:22 +02:00
|
|
|
case OpCode::Id::ISCADD_C:
|
|
|
|
case OpCode::Id::ISCADD_R:
|
|
|
|
case OpCode::Id::ISCADD_IMM: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in ISCADD is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-06-17 19:26:11 +02:00
|
|
|
if (instr.alu_integer.negate_a)
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
|
|
|
|
if (instr.alu_integer.negate_b)
|
|
|
|
op_b = "-(" + op_b + ')';
|
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string shift = std::to_string(instr.alu_integer.shift_amount.Value());
|
2018-06-09 05:25:22 +02:00
|
|
|
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0,
|
|
|
|
"((" + op_a + " << " + shift + ") + " + op_b + ')', 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-08-31 03:32:58 +02:00
|
|
|
case OpCode::Id::POPC_C:
|
|
|
|
case OpCode::Id::POPC_R:
|
|
|
|
case OpCode::Id::POPC_IMM: {
|
|
|
|
if (instr.popc.invert) {
|
|
|
|
op_b = "~(" + op_b + ')';
|
|
|
|
}
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0, "bitCount(" + op_b + ')', 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-07-22 06:37:12 +02:00
|
|
|
case OpCode::Id::SEL_C:
|
|
|
|
case OpCode::Id::SEL_R:
|
|
|
|
case OpCode::Id::SEL_IMM: {
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string condition =
|
2018-07-22 06:37:12 +02:00
|
|
|
GetPredicateCondition(instr.sel.pred, instr.sel.neg_pred != 0);
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0,
|
|
|
|
'(' + condition + ") ? " + op_a + " : " + op_b, 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-06-17 19:26:11 +02:00
|
|
|
case OpCode::Id::LOP_C:
|
|
|
|
case OpCode::Id::LOP_R:
|
|
|
|
case OpCode::Id::LOP_IMM: {
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in LOP is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-06-17 19:26:11 +02:00
|
|
|
if (instr.alu.lop.invert_a)
|
|
|
|
op_a = "~(" + op_a + ')';
|
|
|
|
|
|
|
|
if (instr.alu.lop.invert_b)
|
|
|
|
op_b = "~(" + op_b + ')';
|
|
|
|
|
2018-08-18 21:36:37 +02:00
|
|
|
WriteLogicOperation(instr.gpr0, instr.alu.lop.operation, op_a, op_b,
|
|
|
|
instr.alu.lop.pred_result_mode, instr.alu.lop.pred48);
|
2018-06-17 19:26:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-22 09:02:18 +02:00
|
|
|
case OpCode::Id::LOP3_C:
|
|
|
|
case OpCode::Id::LOP3_R:
|
|
|
|
case OpCode::Id::LOP3_IMM: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in LOP3 is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string op_c = regs.GetRegisterAsInteger(instr.gpr39);
|
2018-08-22 09:02:18 +02:00
|
|
|
std::string lut;
|
2018-09-09 17:51:43 +02:00
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
if (opcode->get().GetId() == OpCode::Id::LOP3_R) {
|
2018-08-22 09:02:18 +02:00
|
|
|
lut = '(' + std::to_string(instr.alu.lop3.GetImmLut28()) + ')';
|
|
|
|
} else {
|
|
|
|
lut = '(' + std::to_string(instr.alu.lop3.GetImmLut48()) + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteLop3Instruction(instr.gpr0, op_a, op_b, op_c, lut);
|
|
|
|
break;
|
|
|
|
}
|
2018-07-04 22:25:48 +02:00
|
|
|
case OpCode::Id::IMNMX_C:
|
|
|
|
case OpCode::Id::IMNMX_R:
|
|
|
|
case OpCode::Id::IMNMX_IMM: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.imnmx.exchange != Tegra::Shader::IMinMaxExchange::None);
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
2018-11-22 09:35:00 +01:00
|
|
|
"Condition codes generation in IMNMX is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string condition =
|
2018-07-04 22:25:48 +02:00
|
|
|
GetPredicateCondition(instr.imnmx.pred, instr.imnmx.negate_pred != 0);
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string parameters = op_a + ',' + op_b;
|
2018-07-04 22:25:48 +02:00
|
|
|
regs.SetRegisterToInteger(instr.gpr0, instr.imnmx.is_signed, 0,
|
|
|
|
'(' + condition + ") ? min(" + parameters + ") : max(" +
|
|
|
|
parameters + ')',
|
|
|
|
1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-11 18:27:02 +02:00
|
|
|
case OpCode::Id::LEA_R2:
|
|
|
|
case OpCode::Id::LEA_R1:
|
|
|
|
case OpCode::Id::LEA_IMM:
|
|
|
|
case OpCode::Id::LEA_RZ:
|
|
|
|
case OpCode::Id::LEA_HI: {
|
|
|
|
std::string op_c;
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-09-11 18:27:02 +02:00
|
|
|
case OpCode::Id::LEA_R2: {
|
|
|
|
op_a = regs.GetRegisterAsInteger(instr.gpr20);
|
|
|
|
op_b = regs.GetRegisterAsInteger(instr.gpr39);
|
|
|
|
op_c = std::to_string(instr.lea.r2.entry_a);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OpCode::Id::LEA_R1: {
|
|
|
|
const bool neg = instr.lea.r1.neg != 0;
|
|
|
|
op_a = regs.GetRegisterAsInteger(instr.gpr8);
|
|
|
|
if (neg)
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
op_b = regs.GetRegisterAsInteger(instr.gpr20);
|
|
|
|
op_c = std::to_string(instr.lea.r1.entry_a);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OpCode::Id::LEA_IMM: {
|
|
|
|
const bool neg = instr.lea.imm.neg != 0;
|
|
|
|
op_b = regs.GetRegisterAsInteger(instr.gpr8);
|
|
|
|
if (neg)
|
|
|
|
op_b = "-(" + op_b + ')';
|
|
|
|
op_a = std::to_string(instr.lea.imm.entry_a);
|
|
|
|
op_c = std::to_string(instr.lea.imm.entry_b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OpCode::Id::LEA_RZ: {
|
|
|
|
const bool neg = instr.lea.rz.neg != 0;
|
|
|
|
op_b = regs.GetRegisterAsInteger(instr.gpr8);
|
|
|
|
if (neg)
|
|
|
|
op_b = "-(" + op_b + ')';
|
|
|
|
op_a = regs.GetUniform(instr.lea.rz.cb_index, instr.lea.rz.cb_offset,
|
|
|
|
GLSLRegister::Type::Integer);
|
|
|
|
op_c = std::to_string(instr.lea.rz.entry_a);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OpCode::Id::LEA_HI:
|
|
|
|
default: {
|
|
|
|
op_b = regs.GetRegisterAsInteger(instr.gpr8);
|
|
|
|
op_a = std::to_string(instr.lea.imm.entry_a);
|
|
|
|
op_c = std::to_string(instr.lea.imm.entry_b);
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled LEA subinstruction: {}", opcode->get().GetName());
|
2018-09-11 18:27:02 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.lea.pred48 != static_cast<u64>(Pred::UnusedIndex),
|
|
|
|
"Unhandled LEA Predicate");
|
2018-09-11 18:27:02 +02:00
|
|
|
const std::string value = '(' + op_a + " + (" + op_b + "*(1 << " + op_c + ")))";
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0, value, 1, 1);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-06-09 05:25:22 +02:00
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled ArithmeticInteger instruction: {}",
|
|
|
|
opcode->get().GetName());
|
2018-06-09 05:25:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-05 02:03:47 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-10-15 07:04:31 +02:00
|
|
|
case OpCode::Type::ArithmeticHalf: {
|
2018-10-30 05:03:25 +01:00
|
|
|
if (opcode->get().GetId() == OpCode::Id::HADD2_C ||
|
|
|
|
opcode->get().GetId() == OpCode::Id::HADD2_R) {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.alu_half.ftz != 0);
|
2018-10-15 07:04:31 +02:00
|
|
|
}
|
|
|
|
const bool negate_a =
|
2018-10-30 05:03:25 +01:00
|
|
|
opcode->get().GetId() != OpCode::Id::HMUL2_R && instr.alu_half.negate_a != 0;
|
2018-10-15 07:04:31 +02:00
|
|
|
const bool negate_b =
|
2018-10-30 05:03:25 +01:00
|
|
|
opcode->get().GetId() != OpCode::Id::HMUL2_C && instr.alu_half.negate_b != 0;
|
2018-10-15 07:04:31 +02:00
|
|
|
|
|
|
|
const std::string op_a =
|
|
|
|
GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.alu_half.type_a,
|
|
|
|
instr.alu_half.abs_a != 0, negate_a);
|
|
|
|
|
|
|
|
std::string op_b;
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-10-15 07:04:31 +02:00
|
|
|
case OpCode::Id::HADD2_C:
|
|
|
|
case OpCode::Id::HMUL2_C:
|
|
|
|
op_b = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::UnsignedInteger);
|
|
|
|
break;
|
|
|
|
case OpCode::Id::HADD2_R:
|
|
|
|
case OpCode::Id::HMUL2_R:
|
|
|
|
op_b = regs.GetRegisterAsInteger(instr.gpr20, 0, false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
op_b = "0";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
op_b = GetHalfFloat(op_b, instr.alu_half.type_b, instr.alu_half.abs_b != 0, negate_b);
|
|
|
|
|
|
|
|
const std::string result = [&]() {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-10-15 07:04:31 +02:00
|
|
|
case OpCode::Id::HADD2_C:
|
|
|
|
case OpCode::Id::HADD2_R:
|
|
|
|
return '(' + op_a + " + " + op_b + ')';
|
|
|
|
case OpCode::Id::HMUL2_C:
|
|
|
|
case OpCode::Id::HMUL2_R:
|
|
|
|
return '(' + op_a + " * " + op_b + ')';
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled half float instruction: {}",
|
|
|
|
opcode->get().GetName());
|
2018-10-15 07:04:31 +02:00
|
|
|
return std::string("0");
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
regs.SetRegisterToHalfFloat(instr.gpr0, 0, result, instr.alu_half.merge, 1, 1,
|
|
|
|
instr.alu_half.saturate != 0);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-15 07:07:16 +02:00
|
|
|
case OpCode::Type::ArithmeticHalfImmediate: {
|
2018-10-30 05:03:25 +01:00
|
|
|
if (opcode->get().GetId() == OpCode::Id::HADD2_IMM) {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.alu_half_imm.ftz != 0);
|
2018-10-15 07:07:16 +02:00
|
|
|
} else {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.alu_half_imm.precision !=
|
|
|
|
Tegra::Shader::HalfPrecision::None);
|
2018-10-15 07:07:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string op_a = GetHalfFloat(
|
|
|
|
regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.alu_half_imm.type_a,
|
|
|
|
instr.alu_half_imm.abs_a != 0, instr.alu_half_imm.negate_a != 0);
|
|
|
|
|
|
|
|
const std::string op_b = UnpackHalfImmediate(instr, true);
|
|
|
|
|
|
|
|
const std::string result = [&]() {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-10-15 07:07:16 +02:00
|
|
|
case OpCode::Id::HADD2_IMM:
|
|
|
|
return op_a + " + " + op_b;
|
|
|
|
case OpCode::Id::HMUL2_IMM:
|
|
|
|
return op_a + " * " + op_b;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return std::string("0");
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
regs.SetRegisterToHalfFloat(instr.gpr0, 0, result, instr.alu_half_imm.merge, 1, 1,
|
|
|
|
instr.alu_half_imm.saturate != 0);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-10 05:39:44 +02:00
|
|
|
case OpCode::Type::Ffma: {
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string op_a = regs.GetRegisterAsFloat(instr.gpr8);
|
2018-04-10 05:39:44 +02:00
|
|
|
std::string op_b = instr.ffma.negate_b ? "-" : "";
|
|
|
|
std::string op_c = instr.ffma.negate_c ? "-" : "";
|
|
|
|
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.ffma.cc != 0, "FFMA cc not implemented");
|
|
|
|
UNIMPLEMENTED_IF_MSG(
|
|
|
|
instr.ffma.tab5980_0 != 1, "FFMA tab5980_0({}) not implemented",
|
|
|
|
instr.ffma.tab5980_0.Value()); // Seems to be 1 by default based on SMO
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.ffma.tab5980_1 != 0, "FFMA tab5980_1({}) not implemented",
|
|
|
|
instr.ffma.tab5980_1.Value());
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in FFMA is not implemented");
|
2018-09-01 10:45:14 +02:00
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-04-08 05:48:38 +02:00
|
|
|
case OpCode::Id::FFMA_CR: {
|
2018-06-06 04:45:22 +02:00
|
|
|
op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Float);
|
2018-04-29 01:59:30 +02:00
|
|
|
op_c += regs.GetRegisterAsFloat(instr.gpr39);
|
2018-04-16 02:45:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::FFMA_RR: {
|
2018-04-29 01:59:30 +02:00
|
|
|
op_b += regs.GetRegisterAsFloat(instr.gpr20);
|
|
|
|
op_c += regs.GetRegisterAsFloat(instr.gpr39);
|
2018-04-16 02:45:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::FFMA_RC: {
|
2018-04-29 01:59:30 +02:00
|
|
|
op_b += regs.GetRegisterAsFloat(instr.gpr39);
|
2018-06-06 04:45:22 +02:00
|
|
|
op_c += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Float);
|
2018-04-16 02:45:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::FFMA_IMM: {
|
2018-04-19 20:34:50 +02:00
|
|
|
op_b += GetImmediate19(instr);
|
2018-04-29 01:59:30 +02:00
|
|
|
op_c += regs.GetRegisterAsFloat(instr.gpr39);
|
2018-04-08 05:48:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled FFMA instruction: {}", opcode->get().GetName());
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-16 02:45:56 +02:00
|
|
|
|
2018-10-28 16:38:40 +01:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, "fma(" + op_a + ", " + op_b + ", " + op_c + ')',
|
|
|
|
1, 1, instr.alu.saturate_d, 0, true);
|
2018-04-08 05:48:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-15 07:08:31 +02:00
|
|
|
case OpCode::Type::Hfma2: {
|
2018-10-30 05:03:25 +01:00
|
|
|
if (opcode->get().GetId() == OpCode::Id::HFMA2_RR) {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.hfma2.rr.precision != Tegra::Shader::HalfPrecision::None);
|
2018-10-15 07:08:31 +02:00
|
|
|
} else {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.hfma2.precision != Tegra::Shader::HalfPrecision::None);
|
2018-10-15 07:08:31 +02:00
|
|
|
}
|
2018-10-30 05:03:25 +01:00
|
|
|
const bool saturate = opcode->get().GetId() == OpCode::Id::HFMA2_RR
|
2018-10-15 07:08:31 +02:00
|
|
|
? instr.hfma2.rr.saturate != 0
|
|
|
|
: instr.hfma2.saturate != 0;
|
|
|
|
|
|
|
|
const std::string op_a =
|
|
|
|
GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.hfma2.type_a);
|
|
|
|
std::string op_b, op_c;
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-10-15 07:08:31 +02:00
|
|
|
case OpCode::Id::HFMA2_CR:
|
|
|
|
op_b = GetHalfFloat(regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::UnsignedInteger),
|
|
|
|
instr.hfma2.type_b, false, instr.hfma2.negate_b);
|
|
|
|
op_c = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr39, 0, false),
|
|
|
|
instr.hfma2.type_reg39, false, instr.hfma2.negate_c);
|
|
|
|
break;
|
|
|
|
case OpCode::Id::HFMA2_RC:
|
|
|
|
op_b = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr39, 0, false),
|
|
|
|
instr.hfma2.type_reg39, false, instr.hfma2.negate_b);
|
|
|
|
op_c = GetHalfFloat(regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::UnsignedInteger),
|
|
|
|
instr.hfma2.type_b, false, instr.hfma2.negate_c);
|
|
|
|
break;
|
|
|
|
case OpCode::Id::HFMA2_RR:
|
|
|
|
op_b = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr20, 0, false),
|
|
|
|
instr.hfma2.type_b, false, instr.hfma2.negate_b);
|
|
|
|
op_c = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr39, 0, false),
|
|
|
|
instr.hfma2.rr.type_c, false, instr.hfma2.rr.negate_c);
|
|
|
|
break;
|
|
|
|
case OpCode::Id::HFMA2_IMM_R:
|
|
|
|
op_b = UnpackHalfImmediate(instr, true);
|
|
|
|
op_c = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr39, 0, false),
|
|
|
|
instr.hfma2.type_reg39, false, instr.hfma2.negate_c);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
op_c = op_b = "vec2(0)";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string result = '(' + op_a + " * " + op_b + " + " + op_c + ')';
|
|
|
|
|
|
|
|
regs.SetRegisterToHalfFloat(instr.gpr0, 0, result, instr.hfma2.merge, 1, 1, saturate);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-29 02:01:36 +02:00
|
|
|
case OpCode::Type::Conversion: {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-06-04 18:57:47 +02:00
|
|
|
case OpCode::Id::I2I_R: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.conversion.selector);
|
2018-05-30 05:10:44 +02:00
|
|
|
|
2018-06-16 01:40:34 +02:00
|
|
|
std::string op_a = regs.GetRegisterAsInteger(
|
|
|
|
instr.gpr20, 0, instr.conversion.is_input_signed, instr.conversion.src_size);
|
2018-04-29 02:01:36 +02:00
|
|
|
|
|
|
|
if (instr.conversion.abs_a) {
|
|
|
|
op_a = "abs(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
2018-08-15 16:27:43 +02:00
|
|
|
if (instr.conversion.negate_a) {
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
2018-06-05 01:05:12 +02:00
|
|
|
regs.SetRegisterToInteger(instr.gpr0, instr.conversion.is_output_signed, 0, op_a, 1,
|
2018-09-18 00:16:06 +02:00
|
|
|
1, instr.alu.saturate_d, 0, instr.conversion.dest_size,
|
|
|
|
instr.generates_cc.Value() != 0);
|
2018-04-29 02:01:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-15 16:25:02 +02:00
|
|
|
case OpCode::Id::I2F_R:
|
|
|
|
case OpCode::Id::I2F_C: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.conversion.dest_size != Register::Size::Word);
|
|
|
|
UNIMPLEMENTED_IF(instr.conversion.selector);
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in I2F is not implemented");
|
2018-11-28 06:44:15 +01:00
|
|
|
std::string op_a;
|
2018-08-15 16:25:02 +02:00
|
|
|
|
|
|
|
if (instr.is_b_gpr) {
|
|
|
|
op_a =
|
|
|
|
regs.GetRegisterAsInteger(instr.gpr20, 0, instr.conversion.is_input_signed,
|
|
|
|
instr.conversion.src_size);
|
|
|
|
} else {
|
|
|
|
op_a = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
instr.conversion.is_input_signed
|
|
|
|
? GLSLRegister::Type::Integer
|
|
|
|
: GLSLRegister::Type::UnsignedInteger,
|
|
|
|
instr.conversion.src_size);
|
|
|
|
}
|
2018-06-04 18:57:47 +02:00
|
|
|
|
|
|
|
if (instr.conversion.abs_a) {
|
|
|
|
op_a = "abs(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
2018-08-15 16:18:55 +02:00
|
|
|
if (instr.conversion.negate_a) {
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
2018-06-04 18:57:47 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-05-30 05:10:44 +02:00
|
|
|
case OpCode::Id::F2F_R: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.conversion.dest_size != Register::Size::Word);
|
|
|
|
UNIMPLEMENTED_IF(instr.conversion.src_size != Register::Size::Word);
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in F2F is not implemented");
|
2018-05-30 05:52:54 +02:00
|
|
|
std::string op_a = regs.GetRegisterAsFloat(instr.gpr20);
|
|
|
|
|
2018-08-15 16:27:43 +02:00
|
|
|
if (instr.conversion.abs_a) {
|
|
|
|
op_a = "abs(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instr.conversion.negate_a) {
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
2018-06-07 04:21:29 +02:00
|
|
|
switch (instr.conversion.f2f.rounding) {
|
|
|
|
case Tegra::Shader::F2fRoundingOp::None:
|
|
|
|
break;
|
2018-07-04 22:24:04 +02:00
|
|
|
case Tegra::Shader::F2fRoundingOp::Round:
|
|
|
|
op_a = "roundEven(" + op_a + ')';
|
|
|
|
break;
|
2018-06-07 04:21:29 +02:00
|
|
|
case Tegra::Shader::F2fRoundingOp::Floor:
|
|
|
|
op_a = "floor(" + op_a + ')';
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::F2fRoundingOp::Ceil:
|
|
|
|
op_a = "ceil(" + op_a + ')';
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::F2fRoundingOp::Trunc:
|
|
|
|
op_a = "trunc(" + op_a + ')';
|
|
|
|
break;
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented F2F rounding mode {}",
|
|
|
|
static_cast<u32>(instr.conversion.f2f.rounding.Value()));
|
2018-06-07 04:21:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-06-09 08:36:33 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1, instr.alu.saturate_d);
|
2018-05-30 05:10:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-15 16:16:35 +02:00
|
|
|
case OpCode::Id::F2I_R:
|
|
|
|
case OpCode::Id::F2I_C: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.conversion.src_size != Register::Size::Word);
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in F2I is not implemented");
|
2018-08-15 16:16:35 +02:00
|
|
|
std::string op_a{};
|
|
|
|
|
|
|
|
if (instr.is_b_gpr) {
|
|
|
|
op_a = regs.GetRegisterAsFloat(instr.gpr20);
|
|
|
|
} else {
|
|
|
|
op_a = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Float);
|
|
|
|
}
|
2018-06-05 01:05:12 +02:00
|
|
|
|
|
|
|
if (instr.conversion.abs_a) {
|
|
|
|
op_a = "abs(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
2018-08-15 16:15:55 +02:00
|
|
|
if (instr.conversion.negate_a) {
|
|
|
|
op_a = "-(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
2018-06-05 01:05:12 +02:00
|
|
|
switch (instr.conversion.f2i.rounding) {
|
2018-06-07 04:21:29 +02:00
|
|
|
case Tegra::Shader::F2iRoundingOp::None:
|
2018-06-05 01:05:12 +02:00
|
|
|
break;
|
2018-06-07 04:21:29 +02:00
|
|
|
case Tegra::Shader::F2iRoundingOp::Floor:
|
2018-06-05 01:05:12 +02:00
|
|
|
op_a = "floor(" + op_a + ')';
|
|
|
|
break;
|
2018-06-07 04:21:29 +02:00
|
|
|
case Tegra::Shader::F2iRoundingOp::Ceil:
|
2018-06-05 01:05:12 +02:00
|
|
|
op_a = "ceil(" + op_a + ')';
|
|
|
|
break;
|
2018-06-07 04:21:29 +02:00
|
|
|
case Tegra::Shader::F2iRoundingOp::Trunc:
|
2018-06-05 01:05:12 +02:00
|
|
|
op_a = "trunc(" + op_a + ')';
|
|
|
|
break;
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented F2I rounding mode {}",
|
|
|
|
static_cast<u32>(instr.conversion.f2i.rounding.Value()));
|
2018-06-05 01:05:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instr.conversion.is_output_signed) {
|
|
|
|
op_a = "int(" + op_a + ')';
|
|
|
|
} else {
|
|
|
|
op_a = "uint(" + op_a + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, instr.conversion.is_output_signed, 0, op_a, 1,
|
2018-06-16 01:40:34 +02:00
|
|
|
1, false, 0, instr.conversion.dest_size);
|
2018-06-05 01:05:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-04-29 02:01:36 +02:00
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled conversion instruction: {}", opcode->get().GetName());
|
2018-04-29 02:01:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
case OpCode::Type::Memory: {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-04-08 05:48:38 +02:00
|
|
|
case OpCode::Id::LD_A: {
|
2018-09-05 05:09:24 +02:00
|
|
|
// Note: Shouldn't this be interp mode flat? As in no interpolation made.
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
|
|
|
|
"Indirect attribute loads are not supported");
|
|
|
|
UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
|
|
|
|
"Unaligned attribute loads are not supported");
|
2018-09-05 05:09:24 +02:00
|
|
|
|
|
|
|
Tegra::Shader::IpaMode input_mode{Tegra::Shader::IpaInterpMode::Perspective,
|
|
|
|
Tegra::Shader::IpaSampleMode::Default};
|
2018-09-08 23:13:02 +02:00
|
|
|
|
2018-09-18 01:25:53 +02:00
|
|
|
u64 next_element = instr.attribute.fmt20.element;
|
|
|
|
u64 next_index = static_cast<u64>(instr.attribute.fmt20.index.Value());
|
2018-09-08 23:13:02 +02:00
|
|
|
|
|
|
|
const auto LoadNextElement = [&](u32 reg_offset) {
|
|
|
|
regs.SetRegisterToInputAttibute(instr.gpr0.Value() + reg_offset, next_element,
|
|
|
|
static_cast<Attribute::Index>(next_index),
|
2018-10-07 04:17:31 +02:00
|
|
|
input_mode, instr.gpr39.Value());
|
2018-09-08 23:13:02 +02:00
|
|
|
|
|
|
|
// Load the next attribute element into the following register. If the element
|
|
|
|
// to load goes beyond the vec4 size, load the first element of the next
|
|
|
|
// attribute.
|
|
|
|
next_element = (next_element + 1) % 4;
|
|
|
|
next_index = next_index + (next_element == 0 ? 1 : 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
|
|
|
|
for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
|
|
|
|
LoadNextElement(reg_offset);
|
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-06-06 05:46:23 +02:00
|
|
|
case OpCode::Id::LD_C: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.ld_c.unknown != 0);
|
2018-06-06 05:46:23 +02:00
|
|
|
|
2018-11-28 06:44:15 +01:00
|
|
|
const auto scope = shader.Scope();
|
2018-08-15 01:12:45 +02:00
|
|
|
|
|
|
|
shader.AddLine("uint index = (" + regs.GetRegisterAsInteger(instr.gpr8, 0, false) +
|
|
|
|
" / 4) & (MAX_CONSTBUFFER_ELEMENTS - 1);");
|
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string op_a =
|
2018-08-15 01:12:45 +02:00
|
|
|
regs.GetUniformIndirect(instr.cbuf36.index, instr.cbuf36.offset + 0, "index",
|
2018-06-06 05:46:23 +02:00
|
|
|
GLSLRegister::Type::Float);
|
|
|
|
|
|
|
|
switch (instr.ld_c.type.Value()) {
|
|
|
|
case Tegra::Shader::UniformType::Single:
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1);
|
|
|
|
break;
|
|
|
|
|
2018-08-15 01:12:45 +02:00
|
|
|
case Tegra::Shader::UniformType::Double: {
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string op_b =
|
2018-08-15 01:12:45 +02:00
|
|
|
regs.GetUniformIndirect(instr.cbuf36.index, instr.cbuf36.offset + 4,
|
|
|
|
"index", GLSLRegister::Type::Float);
|
2018-06-06 05:46:23 +02:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1);
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0.Value() + 1, 0, op_b, 1, 1);
|
|
|
|
break;
|
2018-08-15 01:12:45 +02:00
|
|
|
}
|
2018-06-06 05:46:23 +02:00
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled type: {}",
|
|
|
|
static_cast<unsigned>(instr.ld_c.type.Value()));
|
2018-06-06 05:46:23 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-10-20 03:48:21 +02:00
|
|
|
case OpCode::Id::LD_L: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.ld_l.unknown == 1, "LD_L Unhandled mode: {}",
|
|
|
|
static_cast<unsigned>(instr.ld_l.unknown.Value()));
|
|
|
|
|
2018-11-28 06:44:15 +01:00
|
|
|
const auto scope = shader.Scope();
|
2018-10-20 03:48:21 +02:00
|
|
|
|
|
|
|
std::string op = '(' + regs.GetRegisterAsInteger(instr.gpr8, 0, false) + " + " +
|
|
|
|
std::to_string(instr.smem_imm.Value()) + ')';
|
|
|
|
|
|
|
|
shader.AddLine("uint index = (" + op + " / 4);");
|
|
|
|
|
|
|
|
const std::string op_a = regs.GetLocalMemoryAsFloat("index");
|
|
|
|
|
|
|
|
switch (instr.ldst_sl.type.Value()) {
|
|
|
|
case Tegra::Shader::StoreType::Bytes32:
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1);
|
|
|
|
break;
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("LD_L Unhandled type: {}",
|
|
|
|
static_cast<unsigned>(instr.ldst_sl.type.Value()));
|
2018-10-20 03:48:21 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
case OpCode::Id::ST_A: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
|
|
|
|
"Indirect attribute loads are not supported");
|
|
|
|
UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
|
|
|
|
"Unaligned attribute loads are not supported");
|
2018-09-08 23:13:02 +02:00
|
|
|
|
2018-09-18 01:25:53 +02:00
|
|
|
u64 next_element = instr.attribute.fmt20.element;
|
|
|
|
u64 next_index = static_cast<u64>(instr.attribute.fmt20.index.Value());
|
2018-09-08 23:13:02 +02:00
|
|
|
|
|
|
|
const auto StoreNextElement = [&](u32 reg_offset) {
|
|
|
|
regs.SetOutputAttributeToRegister(static_cast<Attribute::Index>(next_index),
|
2018-10-07 04:17:31 +02:00
|
|
|
next_element, instr.gpr0.Value() + reg_offset,
|
|
|
|
instr.gpr39.Value());
|
2018-09-08 23:13:02 +02:00
|
|
|
|
|
|
|
// Load the next attribute element into the following register. If the element
|
|
|
|
// to load goes beyond the vec4 size, load the first element of the next
|
|
|
|
// attribute.
|
|
|
|
next_element = (next_element + 1) % 4;
|
|
|
|
next_index = next_index + (next_element == 0 ? 1 : 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
|
|
|
|
for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
|
|
|
|
StoreNextElement(reg_offset);
|
|
|
|
}
|
|
|
|
|
2018-04-08 05:48:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-20 03:48:21 +02:00
|
|
|
case OpCode::Id::ST_L: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.st_l.unknown == 0, "ST_L Unhandled mode: {}",
|
|
|
|
static_cast<unsigned>(instr.st_l.unknown.Value()));
|
|
|
|
|
2018-11-28 06:44:15 +01:00
|
|
|
const auto scope = shader.Scope();
|
2018-10-20 03:48:21 +02:00
|
|
|
|
|
|
|
std::string op = '(' + regs.GetRegisterAsInteger(instr.gpr8, 0, false) + " + " +
|
|
|
|
std::to_string(instr.smem_imm.Value()) + ')';
|
|
|
|
|
|
|
|
shader.AddLine("uint index = (" + op + " / 4);");
|
|
|
|
|
|
|
|
switch (instr.ldst_sl.type.Value()) {
|
|
|
|
case Tegra::Shader::StoreType::Bytes32:
|
|
|
|
regs.SetLocalMemoryAsFloat("index", regs.GetRegisterAsFloat(instr.gpr0));
|
|
|
|
break;
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("ST_L Unhandled type: {}",
|
|
|
|
static_cast<unsigned>(instr.ldst_sl.type.Value()));
|
2018-10-20 03:48:21 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-01 05:22:21 +02:00
|
|
|
case OpCode::Id::TEX: {
|
2018-09-07 00:45:49 +02:00
|
|
|
Tegra::Shader::TextureType texture_type{instr.tex.texture_type};
|
2018-10-14 00:10:43 +02:00
|
|
|
const bool is_array = instr.tex.array != 0;
|
2018-09-07 00:45:49 +02:00
|
|
|
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tex.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
|
|
|
|
"NODEP is not implemented");
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tex.UsesMiscMode(Tegra::Shader::TextureMiscMode::AOFFI),
|
|
|
|
"AOFFI is not implemented");
|
2018-09-12 22:17:44 +02:00
|
|
|
|
2018-09-19 07:18:20 +02:00
|
|
|
const bool depth_compare =
|
|
|
|
instr.tex.UsesMiscMode(Tegra::Shader::TextureMiscMode::DC);
|
|
|
|
u32 num_coordinates = TextureCoordinates(texture_type);
|
2018-11-22 02:04:43 +01:00
|
|
|
u32 start_index = 0;
|
|
|
|
std::string array_elem;
|
|
|
|
if (is_array) {
|
|
|
|
array_elem = regs.GetRegisterAsInteger(instr.gpr8);
|
|
|
|
start_index = 1;
|
|
|
|
}
|
2018-11-23 13:11:13 +01:00
|
|
|
const auto process_mode = instr.tex.GetTextureProcessMode();
|
2018-11-22 02:04:43 +01:00
|
|
|
u32 start_index_b = 0;
|
|
|
|
std::string lod_value;
|
|
|
|
if (process_mode != Tegra::Shader::TextureProcessMode::LZ &&
|
|
|
|
process_mode != Tegra::Shader::TextureProcessMode::None) {
|
|
|
|
start_index_b = 1;
|
|
|
|
lod_value = regs.GetRegisterAsFloat(instr.gpr20);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string depth_value;
|
|
|
|
if (depth_compare) {
|
|
|
|
depth_value = regs.GetRegisterAsFloat(instr.gpr20.Value() + start_index_b);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool depth_compare_extra = false;
|
2018-09-19 07:18:20 +02:00
|
|
|
|
2018-11-28 06:22:10 +01:00
|
|
|
const auto scope = shader.Scope();
|
|
|
|
|
2018-09-19 07:18:20 +02:00
|
|
|
switch (num_coordinates) {
|
|
|
|
case 1: {
|
2018-11-22 02:04:43 +01:00
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8.Value() + start_index);
|
2018-10-14 00:10:43 +02:00
|
|
|
if (is_array) {
|
2018-11-22 02:04:43 +01:00
|
|
|
if (depth_compare) {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + x + ", " + depth_value + ", " +
|
|
|
|
array_elem + ");");
|
2018-11-22 02:04:43 +01:00
|
|
|
} else {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + array_elem + ");");
|
2018-11-22 02:04:43 +01:00
|
|
|
}
|
2018-10-14 00:10:43 +02:00
|
|
|
} else {
|
2018-11-22 02:04:43 +01:00
|
|
|
if (depth_compare) {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + depth_value + ");");
|
2018-11-22 02:04:43 +01:00
|
|
|
} else {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("float coords = " + x + ';');
|
2018-11-22 02:04:43 +01:00
|
|
|
}
|
2018-10-14 00:10:43 +02:00
|
|
|
}
|
2018-09-07 00:45:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-19 07:18:20 +02:00
|
|
|
case 2: {
|
2018-11-22 02:04:43 +01:00
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8.Value() + start_index);
|
|
|
|
const std::string y =
|
|
|
|
regs.GetRegisterAsFloat(instr.gpr8.Value() + start_index + 1);
|
2018-10-14 00:10:43 +02:00
|
|
|
if (is_array) {
|
2018-11-22 02:04:43 +01:00
|
|
|
if (depth_compare) {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec4 coords = vec4(" + x + ", " + y + ", " +
|
|
|
|
depth_value + ", " + array_elem + ");");
|
2018-11-22 02:04:43 +01:00
|
|
|
} else {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + x + ", " + y + ", " +
|
|
|
|
array_elem + ");");
|
2018-11-22 02:04:43 +01:00
|
|
|
}
|
2018-10-14 00:10:43 +02:00
|
|
|
} else {
|
2018-11-22 02:04:43 +01:00
|
|
|
if (depth_compare) {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + x + ", " + y + ", " +
|
|
|
|
depth_value + ");");
|
2018-11-22 02:04:43 +01:00
|
|
|
} else {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
|
2018-11-22 02:04:43 +01:00
|
|
|
}
|
2018-10-14 00:10:43 +02:00
|
|
|
}
|
2018-09-07 00:45:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-19 07:18:20 +02:00
|
|
|
case 3: {
|
2018-11-23 03:02:18 +01:00
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8.Value() + start_index);
|
2018-11-23 13:11:13 +01:00
|
|
|
const std::string y =
|
|
|
|
regs.GetRegisterAsFloat(instr.gpr8.Value() + start_index + 1);
|
|
|
|
const std::string z =
|
|
|
|
regs.GetRegisterAsFloat(instr.gpr8.Value() + start_index + 2);
|
2018-11-22 02:04:43 +01:00
|
|
|
if (is_array) {
|
|
|
|
depth_compare_extra = depth_compare;
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec4 coords = vec4(" + x + ", " + y + ", " + z + ", " +
|
2018-11-28 06:34:23 +01:00
|
|
|
array_elem + ");");
|
2018-10-14 00:10:43 +02:00
|
|
|
} else {
|
2018-11-22 02:04:43 +01:00
|
|
|
if (depth_compare) {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec4 coords = vec4(" + x + ", " + y + ", " + z + ", " +
|
2018-11-28 06:34:23 +01:00
|
|
|
depth_value + ");");
|
2018-10-14 00:10:43 +02:00
|
|
|
} else {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + x + ", " + y + ", " + z + ");");
|
2018-10-14 00:10:43 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-19 07:18:20 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-07 00:45:49 +02:00
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled coordinates number {}",
|
|
|
|
static_cast<u32>(num_coordinates));
|
2018-09-07 00:45:49 +02:00
|
|
|
|
|
|
|
// Fallback to interpreting as a 2D texture for now
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
|
2018-09-07 00:45:49 +02:00
|
|
|
texture_type = Tegra::Shader::TextureType::Texture2D;
|
|
|
|
}
|
|
|
|
|
2018-09-19 07:18:20 +02:00
|
|
|
const std::string sampler =
|
2018-10-14 00:10:43 +02:00
|
|
|
GetSampler(instr.sampler, texture_type, is_array, depth_compare);
|
2018-06-09 05:24:10 +02:00
|
|
|
// Add an extra scope and declare the texture coords inside to prevent
|
|
|
|
// overwriting them in case they are used as outputs of the texs instruction.
|
2018-09-08 03:09:39 +02:00
|
|
|
|
2018-11-28 06:34:23 +01:00
|
|
|
const std::string texture = [&]() {
|
|
|
|
switch (instr.tex.GetTextureProcessMode()) {
|
|
|
|
case Tegra::Shader::TextureProcessMode::None:
|
|
|
|
if (depth_compare_extra) {
|
|
|
|
return "texture(" + sampler + ", coords, " + depth_value + ')';
|
|
|
|
}
|
|
|
|
return "texture(" + sampler + ", coords)";
|
|
|
|
case Tegra::Shader::TextureProcessMode::LZ:
|
|
|
|
if (depth_compare_extra) {
|
|
|
|
return "texture(" + sampler + ", coords, " + depth_value + ')';
|
|
|
|
}
|
|
|
|
return "textureLod(" + sampler + ", coords, 0.0)";
|
|
|
|
case Tegra::Shader::TextureProcessMode::LB:
|
|
|
|
case Tegra::Shader::TextureProcessMode::LBA:
|
|
|
|
// TODO: Figure if A suffix changes the equation at all.
|
|
|
|
if (depth_compare_extra) {
|
|
|
|
LOG_WARNING(
|
|
|
|
HW_GPU,
|
|
|
|
"OpenGL Limitation: can't set bias value along depth compare");
|
|
|
|
return "texture(" + sampler + ", coords, " + depth_value + ')';
|
|
|
|
}
|
|
|
|
return "texture(" + sampler + ", coords, " + lod_value + ')';
|
|
|
|
case Tegra::Shader::TextureProcessMode::LL:
|
|
|
|
case Tegra::Shader::TextureProcessMode::LLA:
|
|
|
|
// TODO: Figure if A suffix changes the equation at all.
|
|
|
|
if (depth_compare_extra) {
|
|
|
|
LOG_WARNING(
|
|
|
|
HW_GPU,
|
|
|
|
"OpenGL Limitation: can't set lod value along depth compare");
|
|
|
|
return "texture(" + sampler + ", coords, " + depth_value + ')';
|
|
|
|
}
|
|
|
|
return "textureLod(" + sampler + ", coords, " + lod_value + ')';
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unhandled texture process mode {}",
|
|
|
|
static_cast<u32>(instr.tex.GetTextureProcessMode()));
|
|
|
|
if (depth_compare_extra) {
|
|
|
|
return "texture(" + sampler + ", coords, " + depth_value + ')';
|
|
|
|
}
|
|
|
|
return "texture(" + sampler + ", coords)";
|
2018-11-22 02:04:43 +01:00
|
|
|
}
|
2018-11-28 06:34:23 +01:00
|
|
|
}();
|
2018-11-28 06:22:10 +01:00
|
|
|
|
|
|
|
if (depth_compare) {
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, texture, 1, 1, false);
|
|
|
|
} else {
|
2018-09-19 07:18:20 +02:00
|
|
|
std::size_t dest_elem{};
|
|
|
|
for (std::size_t elem = 0; elem < 4; ++elem) {
|
|
|
|
if (!instr.tex.IsComponentEnabled(elem)) {
|
|
|
|
// Skip disabled components
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-28 06:22:10 +01:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, elem, texture, 1, 4, false, dest_elem);
|
2018-09-19 07:18:20 +02:00
|
|
|
++dest_elem;
|
2018-06-01 05:22:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-10 07:26:15 +02:00
|
|
|
case OpCode::Id::TEXS: {
|
2018-09-07 00:45:49 +02:00
|
|
|
Tegra::Shader::TextureType texture_type{instr.texs.GetTextureType()};
|
|
|
|
bool is_array{instr.texs.IsArrayTexture()};
|
2018-06-03 18:08:17 +02:00
|
|
|
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.texs.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
|
|
|
|
"NODEP is not implemented");
|
2018-09-12 22:17:44 +02:00
|
|
|
|
2018-11-28 05:41:06 +01:00
|
|
|
const auto scope = shader.Scope();
|
|
|
|
|
2018-09-19 07:18:20 +02:00
|
|
|
const bool depth_compare =
|
|
|
|
instr.texs.UsesMiscMode(Tegra::Shader::TextureMiscMode::DC);
|
|
|
|
u32 num_coordinates = TextureCoordinates(texture_type);
|
2018-11-23 13:11:13 +01:00
|
|
|
const auto process_mode = instr.texs.GetTextureProcessMode();
|
2018-11-23 03:51:25 +01:00
|
|
|
u32 lod_offset = 0;
|
|
|
|
if (process_mode == Tegra::Shader::TextureProcessMode::LL) {
|
|
|
|
if (num_coordinates > 2) {
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("float lod_value = " +
|
|
|
|
regs.GetRegisterAsFloat(instr.gpr20.Value() + 1) + ';');
|
2018-11-23 03:51:25 +01:00
|
|
|
lod_offset = 2;
|
|
|
|
} else {
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("float lod_value = " + regs.GetRegisterAsFloat(instr.gpr20) +
|
|
|
|
';');
|
2018-11-23 03:51:25 +01:00
|
|
|
lod_offset = 1;
|
|
|
|
}
|
|
|
|
}
|
2018-11-21 06:11:47 +01:00
|
|
|
|
2018-09-19 07:18:20 +02:00
|
|
|
switch (num_coordinates) {
|
2018-11-23 03:51:25 +01:00
|
|
|
case 1: {
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("float coords = " + regs.GetRegisterAsFloat(instr.gpr8) + ';');
|
2018-11-23 03:51:25 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-09-19 07:18:20 +02:00
|
|
|
case 2: {
|
2018-09-07 00:45:49 +02:00
|
|
|
if (is_array) {
|
2018-11-23 13:11:13 +01:00
|
|
|
if (depth_compare) {
|
|
|
|
const std::string index = regs.GetRegisterAsInteger(instr.gpr8);
|
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr20);
|
|
|
|
const std::string z = regs.GetRegisterAsFloat(instr.gpr20.Value() + 1);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("vec4 coords = vec4(" + x + ", " + y + ", " + z + ", " +
|
|
|
|
index + ");");
|
2018-11-23 13:11:13 +01:00
|
|
|
} else {
|
|
|
|
const std::string index = regs.GetRegisterAsInteger(instr.gpr8);
|
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr20);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + x + ", " + y + ", " + index +
|
|
|
|
");");
|
2018-11-23 13:11:13 +01:00
|
|
|
}
|
2018-09-07 00:45:49 +02:00
|
|
|
} else {
|
2018-11-23 03:51:25 +01:00
|
|
|
if (lod_offset != 0) {
|
|
|
|
if (depth_compare) {
|
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
2018-11-23 13:11:13 +01:00
|
|
|
const std::string y =
|
|
|
|
regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
|
|
|
const std::string z =
|
|
|
|
regs.GetRegisterAsFloat(instr.gpr20.Value() + lod_offset);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + x + ", " + y + ", " + z +
|
|
|
|
");");
|
2018-11-23 03:51:25 +01:00
|
|
|
} else {
|
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
2018-11-23 13:11:13 +01:00
|
|
|
const std::string y =
|
|
|
|
regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
|
2018-11-23 03:51:25 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (depth_compare) {
|
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
2018-11-23 13:11:13 +01:00
|
|
|
const std::string y =
|
|
|
|
regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
2018-11-23 03:51:25 +01:00
|
|
|
const std::string z = regs.GetRegisterAsFloat(instr.gpr20);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + x + ", " + y + ", " + z +
|
|
|
|
");");
|
2018-11-23 03:51:25 +01:00
|
|
|
} else {
|
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr20);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
|
2018-11-23 03:51:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-07 00:45:49 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-09-19 07:18:20 +02:00
|
|
|
case 3: {
|
2018-11-23 03:51:25 +01:00
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
|
|
|
const std::string z = regs.GetRegisterAsFloat(instr.gpr20);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + x + ", " + y + ", " + z + ");");
|
2018-09-14 18:18:06 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-07 00:45:49 +02:00
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled coordinates number {}",
|
|
|
|
static_cast<u32>(num_coordinates));
|
2018-09-07 00:45:49 +02:00
|
|
|
|
|
|
|
// Fallback to interpreting as a 2D texture for now
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr20);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
|
2018-09-07 00:45:49 +02:00
|
|
|
texture_type = Tegra::Shader::TextureType::Texture2D;
|
|
|
|
is_array = false;
|
|
|
|
}
|
2018-09-19 07:18:20 +02:00
|
|
|
const std::string sampler =
|
|
|
|
GetSampler(instr.sampler, texture_type, is_array, depth_compare);
|
2018-11-28 06:22:10 +01:00
|
|
|
|
2018-11-28 06:34:23 +01:00
|
|
|
std::string texture = [&]() {
|
|
|
|
switch (process_mode) {
|
|
|
|
case Tegra::Shader::TextureProcessMode::None:
|
|
|
|
return "texture(" + sampler + ", coords)";
|
|
|
|
case Tegra::Shader::TextureProcessMode::LZ:
|
|
|
|
if (depth_compare && is_array) {
|
|
|
|
return "texture(" + sampler + ", coords)";
|
|
|
|
} else {
|
|
|
|
return "textureLod(" + sampler + ", coords, 0.0)";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::TextureProcessMode::LL:
|
|
|
|
return "textureLod(" + sampler + ", coords, lod_value)";
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unhandled texture process mode {}",
|
|
|
|
static_cast<u32>(instr.texs.GetTextureProcessMode()));
|
|
|
|
return "texture(" + sampler + ", coords)";
|
2018-11-08 02:48:45 +01:00
|
|
|
}
|
2018-11-28 06:34:23 +01:00
|
|
|
}();
|
2018-11-28 06:22:10 +01:00
|
|
|
if (depth_compare) {
|
|
|
|
texture = "vec4(" + texture + ')';
|
|
|
|
}
|
2018-11-28 06:34:23 +01:00
|
|
|
|
2018-11-28 06:22:10 +01:00
|
|
|
WriteTexsInstruction(instr, texture);
|
2018-07-24 02:02:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::TLDS: {
|
2018-09-16 09:19:08 +02:00
|
|
|
const Tegra::Shader::TextureType texture_type{instr.tlds.GetTextureType()};
|
|
|
|
const bool is_array{instr.tlds.IsArrayTexture()};
|
2018-09-30 04:35:48 +02:00
|
|
|
|
|
|
|
ASSERT(texture_type == Tegra::Shader::TextureType::Texture2D);
|
|
|
|
ASSERT(is_array == false);
|
2018-09-07 00:45:49 +02:00
|
|
|
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
|
|
|
|
"NODEP is not implemented");
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(Tegra::Shader::TextureMiscMode::AOFFI),
|
|
|
|
"AOFFI is not implemented");
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(Tegra::Shader::TextureMiscMode::MZ),
|
|
|
|
"MZ is not implemented");
|
2018-09-12 22:17:44 +02:00
|
|
|
|
2018-11-21 06:11:47 +01:00
|
|
|
u32 extra_op_offset = 0;
|
|
|
|
|
2018-11-28 05:41:06 +01:00
|
|
|
ShaderScopedScope scope = shader.Scope();
|
2018-10-13 16:35:45 +02:00
|
|
|
|
2018-09-16 09:19:08 +02:00
|
|
|
switch (texture_type) {
|
|
|
|
case Tegra::Shader::TextureType::Texture1D: {
|
|
|
|
const std::string x = regs.GetRegisterAsInteger(instr.gpr8);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("float coords = " + x + ';');
|
2018-09-16 09:19:08 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-07 00:45:49 +02:00
|
|
|
case Tegra::Shader::TextureType::Texture2D: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(is_array, "Unhandled 2d array texture");
|
|
|
|
|
|
|
|
const std::string x = regs.GetRegisterAsInteger(instr.gpr8);
|
|
|
|
const std::string y = regs.GetRegisterAsInteger(instr.gpr20);
|
2018-11-23 13:11:13 +01:00
|
|
|
// shader.AddLine("ivec2 coords = ivec2(" + x + ", " + y + ");");
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("ivec2 coords = ivec2(" + x + ", " + y + ");");
|
2018-11-21 06:11:47 +01:00
|
|
|
extra_op_offset = 1;
|
2018-09-07 00:45:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled texture type {}", static_cast<u32>(texture_type));
|
2018-09-07 00:45:49 +02:00
|
|
|
}
|
2018-09-19 07:18:20 +02:00
|
|
|
const std::string sampler =
|
|
|
|
GetSampler(instr.sampler, texture_type, is_array, false);
|
2018-11-28 06:34:23 +01:00
|
|
|
|
|
|
|
const std::string texture = [&]() {
|
|
|
|
switch (instr.tlds.GetTextureProcessMode()) {
|
|
|
|
case Tegra::Shader::TextureProcessMode::LZ:
|
|
|
|
return "texelFetch(" + sampler + ", coords, 0)";
|
|
|
|
case Tegra::Shader::TextureProcessMode::LL:
|
|
|
|
shader.AddLine(
|
|
|
|
"float lod = " +
|
|
|
|
regs.GetRegisterAsInteger(instr.gpr20.Value() + extra_op_offset) + ';');
|
|
|
|
return "texelFetch(" + sampler + ", coords, lod)";
|
|
|
|
default:
|
|
|
|
UNIMPLEMENTED_MSG("Unhandled texture process mode {}",
|
|
|
|
static_cast<u32>(instr.tlds.GetTextureProcessMode()));
|
|
|
|
return "texelFetch(" + sampler + ", coords, 0)";
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2018-11-28 06:22:10 +01:00
|
|
|
WriteTexsInstruction(instr, texture);
|
2018-04-10 07:26:15 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-19 19:55:50 +02:00
|
|
|
case OpCode::Id::TLD4: {
|
|
|
|
ASSERT(instr.tld4.texture_type == Tegra::Shader::TextureType::Texture2D);
|
|
|
|
ASSERT(instr.tld4.array == 0);
|
|
|
|
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
|
|
|
|
"NODEP is not implemented");
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::AOFFI),
|
|
|
|
"AOFFI is not implemented");
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::NDV),
|
|
|
|
"NDV is not implemented");
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::PTP),
|
|
|
|
"PTP is not implemented");
|
2018-09-19 07:18:20 +02:00
|
|
|
const bool depth_compare =
|
|
|
|
instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::DC);
|
|
|
|
auto texture_type = instr.tld4.texture_type.Value();
|
|
|
|
u32 num_coordinates = TextureCoordinates(texture_type);
|
|
|
|
if (depth_compare)
|
|
|
|
num_coordinates += 1;
|
|
|
|
|
2018-11-28 06:22:10 +01:00
|
|
|
const auto scope = shader.Scope();
|
2018-11-21 06:11:47 +01:00
|
|
|
|
2018-09-19 07:18:20 +02:00
|
|
|
switch (num_coordinates) {
|
|
|
|
case 2: {
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
2018-11-21 06:11:47 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
|
2018-08-19 19:55:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-19 07:18:20 +02:00
|
|
|
case 3: {
|
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
|
|
|
const std::string z = regs.GetRegisterAsFloat(instr.gpr8.Value() + 2);
|
2018-11-21 06:11:47 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + x + ", " + y + ", " + z + ");");
|
2018-09-19 07:18:20 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-19 19:55:50 +02:00
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled coordinates number {}",
|
|
|
|
static_cast<u32>(num_coordinates));
|
2018-09-19 07:18:20 +02:00
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
2018-11-21 06:11:47 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
|
2018-09-19 07:18:20 +02:00
|
|
|
texture_type = Tegra::Shader::TextureType::Texture2D;
|
2018-08-19 19:55:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 00:45:49 +02:00
|
|
|
const std::string sampler =
|
2018-09-19 07:18:20 +02:00
|
|
|
GetSampler(instr.sampler, texture_type, false, depth_compare);
|
2018-11-21 06:11:47 +01:00
|
|
|
|
2018-08-19 19:55:50 +02:00
|
|
|
const std::string texture = "textureGather(" + sampler + ", coords, " +
|
|
|
|
std::to_string(instr.tld4.component) + ')';
|
2018-11-23 13:11:13 +01:00
|
|
|
|
2018-11-28 06:22:10 +01:00
|
|
|
if (depth_compare) {
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, texture, 1, 1, false);
|
|
|
|
} else {
|
2018-11-23 13:11:13 +01:00
|
|
|
shader.AddLine("vec4 texture_tmp = " + texture + ';');
|
2018-09-19 07:18:20 +02:00
|
|
|
std::size_t dest_elem{};
|
|
|
|
for (std::size_t elem = 0; elem < 4; ++elem) {
|
|
|
|
if (!instr.tex.IsComponentEnabled(elem)) {
|
|
|
|
// Skip disabled components
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-23 13:11:13 +01:00
|
|
|
regs.SetRegisterToFloat(instr.gpr0, elem, "texture_tmp", 1, 4, false,
|
|
|
|
dest_elem);
|
2018-09-19 07:18:20 +02:00
|
|
|
++dest_elem;
|
2018-08-19 19:55:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::TLD4S: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(
|
|
|
|
instr.tld4s.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
|
|
|
|
"NODEP is not implemented");
|
|
|
|
UNIMPLEMENTED_IF_MSG(
|
|
|
|
instr.tld4s.UsesMiscMode(Tegra::Shader::TextureMiscMode::AOFFI),
|
|
|
|
"AOFFI is not implemented");
|
2018-09-12 22:17:44 +02:00
|
|
|
|
2018-11-28 05:41:06 +01:00
|
|
|
const auto scope = shader.Scope();
|
2018-11-21 06:11:47 +01:00
|
|
|
|
2018-09-19 07:18:20 +02:00
|
|
|
const bool depth_compare =
|
|
|
|
instr.tld4s.UsesMiscMode(Tegra::Shader::TextureMiscMode::DC);
|
2018-08-19 19:55:50 +02:00
|
|
|
const std::string op_a = regs.GetRegisterAsFloat(instr.gpr8);
|
|
|
|
const std::string op_b = regs.GetRegisterAsFloat(instr.gpr20);
|
|
|
|
// TODO(Subv): Figure out how the sampler type is encoded in the TLD4S instruction.
|
2018-09-19 07:18:20 +02:00
|
|
|
const std::string sampler = GetSampler(
|
|
|
|
instr.sampler, Tegra::Shader::TextureType::Texture2D, false, depth_compare);
|
2018-11-29 01:31:10 +01:00
|
|
|
if (depth_compare) {
|
2018-09-19 07:18:20 +02:00
|
|
|
// Note: TLD4S coordinate encoding works just like TEXS's
|
2018-11-23 13:11:13 +01:00
|
|
|
const std::string op_y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
2018-11-28 05:41:06 +01:00
|
|
|
shader.AddLine("vec3 coords = vec3(" + op_a + ", " + op_y + ", " + op_b + ");");
|
2018-11-29 01:31:10 +01:00
|
|
|
} else {
|
|
|
|
shader.AddLine("vec2 coords = vec2(" + op_a + ", " + op_b + ");");
|
2018-09-19 07:18:20 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 06:22:10 +01:00
|
|
|
std::string texture = "textureGather(" + sampler + ", coords, " +
|
|
|
|
std::to_string(instr.tld4s.component) + ')';
|
|
|
|
if (depth_compare) {
|
|
|
|
texture = "vec4(" + texture + ')';
|
2018-09-19 07:18:20 +02:00
|
|
|
}
|
2018-11-28 06:22:10 +01:00
|
|
|
WriteTexsInstruction(instr, texture);
|
2018-08-19 19:55:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-08 01:53:06 +02:00
|
|
|
case OpCode::Id::TXQ: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.txq.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
|
|
|
|
"NODEP is not implemented");
|
2018-09-12 22:17:44 +02:00
|
|
|
|
2018-11-28 06:22:10 +01:00
|
|
|
const auto scope = shader.Scope();
|
|
|
|
|
|
|
|
// TODO: The new commits on the texture refactor, change the way samplers work.
|
2018-09-08 01:53:06 +02:00
|
|
|
// Sadly, not all texture instructions specify the type of texture their sampler
|
|
|
|
// uses. This must be fixed at a later instance.
|
|
|
|
const std::string sampler =
|
2018-09-19 07:18:20 +02:00
|
|
|
GetSampler(instr.sampler, Tegra::Shader::TextureType::Texture2D, false, false);
|
2018-09-08 01:53:06 +02:00
|
|
|
switch (instr.txq.query_type) {
|
|
|
|
case Tegra::Shader::TextureQueryType::Dimension: {
|
2018-11-21 22:27:17 +01:00
|
|
|
const std::string texture = "textureSize(" + sampler + ", " +
|
|
|
|
regs.GetRegisterAsInteger(instr.gpr8) + ')';
|
|
|
|
const std::string mip_level = "textureQueryLevels(" + sampler + ')';
|
|
|
|
shader.AddLine("ivec2 sizes = " + texture + ';');
|
2018-11-28 06:22:10 +01:00
|
|
|
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0.Value() + 0, true, 0, "sizes.x", 1, 1);
|
2018-11-21 22:27:17 +01:00
|
|
|
regs.SetRegisterToInteger(instr.gpr0.Value() + 1, true, 0, "sizes.y", 1, 1);
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0.Value() + 2, true, 0, "0", 1, 1);
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0.Value() + 3, true, 0, mip_level, 1, 1);
|
2018-09-08 01:53:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled texture query type: {}",
|
|
|
|
static_cast<u32>(instr.txq.query_type.Value()));
|
2018-09-08 01:53:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-09-08 18:40:18 +02:00
|
|
|
case OpCode::Id::TMML: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tmml.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
|
|
|
|
"NODEP is not implemented");
|
|
|
|
UNIMPLEMENTED_IF_MSG(instr.tmml.UsesMiscMode(Tegra::Shader::TextureMiscMode::NDV),
|
|
|
|
"NDV is not implemented");
|
2018-09-12 22:17:44 +02:00
|
|
|
|
2018-10-09 21:44:34 +02:00
|
|
|
const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
|
2018-09-08 18:40:18 +02:00
|
|
|
const bool is_array = instr.tmml.array != 0;
|
|
|
|
auto texture_type = instr.tmml.texture_type.Value();
|
2018-09-19 07:18:20 +02:00
|
|
|
const std::string sampler =
|
|
|
|
GetSampler(instr.sampler, texture_type, is_array, false);
|
2018-09-08 18:40:18 +02:00
|
|
|
|
2018-11-28 06:22:10 +01:00
|
|
|
const auto scope = shader.Scope();
|
|
|
|
|
|
|
|
// TODO: Add coordinates for different samplers once other texture types are
|
2018-09-08 18:40:18 +02:00
|
|
|
// implemented.
|
|
|
|
switch (texture_type) {
|
|
|
|
case Tegra::Shader::TextureType::Texture1D: {
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("float coords = " + x + ';');
|
2018-09-08 18:40:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Tegra::Shader::TextureType::Texture2D: {
|
2018-10-09 21:44:34 +02:00
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
|
2018-09-08 18:40:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled texture type {}", static_cast<u32>(texture_type));
|
2018-09-08 18:40:18 +02:00
|
|
|
|
|
|
|
// Fallback to interpreting as a 2D texture for now
|
2018-10-09 21:44:34 +02:00
|
|
|
const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
|
2018-09-08 18:40:18 +02:00
|
|
|
texture_type = Tegra::Shader::TextureType::Texture2D;
|
|
|
|
}
|
2018-11-28 06:22:10 +01:00
|
|
|
|
2018-09-08 18:40:18 +02:00
|
|
|
const std::string texture = "textureQueryLod(" + sampler + ", coords)";
|
2018-11-28 06:22:10 +01:00
|
|
|
shader.AddLine("vec2 tmp = " + texture + " * vec2(256.0, 256.0);");
|
2018-09-08 18:40:18 +02:00
|
|
|
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, true, 0, "int(tmp.y)", 1, 1);
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0.Value() + 1, false, 0, "uint(tmp.x)", 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled memory instruction: {}", opcode->get().GetName());
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-25 05:42:54 +02:00
|
|
|
case OpCode::Type::FloatSetPredicate: {
|
2018-10-22 02:07:15 +02:00
|
|
|
const std::string op_a =
|
|
|
|
GetOperandAbsNeg(regs.GetRegisterAsFloat(instr.gpr8), instr.fsetp.abs_a != 0,
|
|
|
|
instr.fsetp.neg_a != 0);
|
2018-04-20 16:09:50 +02:00
|
|
|
|
2018-10-22 02:07:15 +02:00
|
|
|
std::string op_b;
|
2018-04-20 16:09:50 +02:00
|
|
|
|
|
|
|
if (instr.is_b_imm) {
|
|
|
|
op_b += '(' + GetImmediate19(instr) + ')';
|
|
|
|
} else {
|
|
|
|
if (instr.is_b_gpr) {
|
2018-04-29 01:59:30 +02:00
|
|
|
op_b += regs.GetRegisterAsFloat(instr.gpr20);
|
2018-04-20 16:09:50 +02:00
|
|
|
} else {
|
2018-06-06 04:45:22 +02:00
|
|
|
op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Float);
|
2018-04-20 16:09:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instr.fsetp.abs_b) {
|
|
|
|
op_b = "abs(" + op_b + ')';
|
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
|
2018-04-20 16:09:50 +02:00
|
|
|
// We can't use the constant predicate as destination.
|
|
|
|
ASSERT(instr.fsetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
|
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string second_pred =
|
2018-05-25 00:22:36 +02:00
|
|
|
GetPredicateCondition(instr.fsetp.pred39, instr.fsetp.neg_pred != 0);
|
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string combiner = GetPredicateCombiner(instr.fsetp.op);
|
2018-05-25 00:22:36 +02:00
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string predicate = GetPredicateComparison(instr.fsetp.cond, op_a, op_b);
|
2018-05-25 00:22:36 +02:00
|
|
|
// Set the primary predicate to the result of Predicate OP SecondPredicate
|
|
|
|
SetPredicate(instr.fsetp.pred3,
|
|
|
|
'(' + predicate + ") " + combiner + " (" + second_pred + ')');
|
|
|
|
|
|
|
|
if (instr.fsetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
|
2018-06-01 06:03:23 +02:00
|
|
|
// Set the secondary predicate to the result of !Predicate OP SecondPredicate,
|
|
|
|
// if enabled
|
2018-05-25 00:22:36 +02:00
|
|
|
SetPredicate(instr.fsetp.pred0,
|
|
|
|
"!(" + predicate + ") " + combiner + " (" + second_pred + ')');
|
2018-04-20 16:09:50 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-04 18:12:03 +02:00
|
|
|
case OpCode::Type::IntegerSetPredicate: {
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string op_a =
|
|
|
|
regs.GetRegisterAsInteger(instr.gpr8, 0, instr.isetp.is_signed);
|
2018-06-06 05:01:51 +02:00
|
|
|
std::string op_b;
|
2018-06-04 18:12:03 +02:00
|
|
|
|
2018-06-06 05:01:51 +02:00
|
|
|
if (instr.is_b_imm) {
|
|
|
|
op_b += '(' + std::to_string(instr.alu.GetSignedImm20_20()) + ')';
|
2018-06-04 18:12:03 +02:00
|
|
|
} else {
|
2018-06-06 05:01:51 +02:00
|
|
|
if (instr.is_b_gpr) {
|
|
|
|
op_b += regs.GetRegisterAsInteger(instr.gpr20, 0, instr.isetp.is_signed);
|
|
|
|
} else {
|
|
|
|
op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Integer);
|
|
|
|
}
|
2018-06-04 18:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We can't use the constant predicate as destination.
|
|
|
|
ASSERT(instr.isetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
|
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string second_pred =
|
2018-06-04 18:12:03 +02:00
|
|
|
GetPredicateCondition(instr.isetp.pred39, instr.isetp.neg_pred != 0);
|
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string combiner = GetPredicateCombiner(instr.isetp.op);
|
2018-06-04 18:12:03 +02:00
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string predicate = GetPredicateComparison(instr.isetp.cond, op_a, op_b);
|
2018-06-04 18:12:03 +02:00
|
|
|
// Set the primary predicate to the result of Predicate OP SecondPredicate
|
|
|
|
SetPredicate(instr.isetp.pred3,
|
|
|
|
'(' + predicate + ") " + combiner + " (" + second_pred + ')');
|
|
|
|
|
|
|
|
if (instr.isetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
|
|
|
|
// Set the secondary predicate to the result of !Predicate OP SecondPredicate,
|
|
|
|
// if enabled
|
|
|
|
SetPredicate(instr.isetp.pred0,
|
|
|
|
"!(" + predicate + ") " + combiner + " (" + second_pred + ')');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-10-15 07:09:40 +02:00
|
|
|
case OpCode::Type::HalfSetPredicate: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.hsetp2.ftz != 0);
|
2018-10-15 07:09:40 +02:00
|
|
|
|
|
|
|
const std::string op_a =
|
|
|
|
GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.hsetp2.type_a,
|
|
|
|
instr.hsetp2.abs_a, instr.hsetp2.negate_a);
|
|
|
|
|
|
|
|
const std::string op_b = [&]() {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-10-15 07:09:40 +02:00
|
|
|
case OpCode::Id::HSETP2_R:
|
|
|
|
return GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr20, 0, false),
|
|
|
|
instr.hsetp2.type_b, instr.hsetp2.abs_a,
|
|
|
|
instr.hsetp2.negate_b);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return std::string("vec2(0)");
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
// We can't use the constant predicate as destination.
|
|
|
|
ASSERT(instr.hsetp2.pred3 != static_cast<u64>(Pred::UnusedIndex));
|
|
|
|
|
|
|
|
const std::string second_pred =
|
|
|
|
GetPredicateCondition(instr.hsetp2.pred39, instr.hsetp2.neg_pred != 0);
|
|
|
|
|
|
|
|
const std::string combiner = GetPredicateCombiner(instr.hsetp2.op);
|
|
|
|
|
|
|
|
const std::string component_combiner = instr.hsetp2.h_and ? "&&" : "||";
|
|
|
|
const std::string predicate =
|
|
|
|
'(' + GetPredicateComparison(instr.hsetp2.cond, op_a + ".x", op_b + ".x") + ' ' +
|
|
|
|
component_combiner + ' ' +
|
|
|
|
GetPredicateComparison(instr.hsetp2.cond, op_a + ".y", op_b + ".y") + ')';
|
|
|
|
|
|
|
|
// Set the primary predicate to the result of Predicate OP SecondPredicate
|
|
|
|
SetPredicate(instr.hsetp2.pred3,
|
|
|
|
'(' + predicate + ") " + combiner + " (" + second_pred + ')');
|
|
|
|
|
|
|
|
if (instr.hsetp2.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
|
|
|
|
// Set the secondary predicate to the result of !Predicate OP SecondPredicate,
|
|
|
|
// if enabled
|
|
|
|
SetPredicate(instr.hsetp2.pred0,
|
|
|
|
"!(" + predicate + ") " + combiner + " (" + second_pred + ')');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-09-11 18:27:02 +02:00
|
|
|
case OpCode::Type::PredicateSetRegister: {
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in PSET is not implemented");
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-09-11 18:27:02 +02:00
|
|
|
const std::string op_a =
|
|
|
|
GetPredicateCondition(instr.pset.pred12, instr.pset.neg_pred12 != 0);
|
|
|
|
const std::string op_b =
|
|
|
|
GetPredicateCondition(instr.pset.pred29, instr.pset.neg_pred29 != 0);
|
|
|
|
|
|
|
|
const std::string second_pred =
|
|
|
|
GetPredicateCondition(instr.pset.pred39, instr.pset.neg_pred39 != 0);
|
|
|
|
|
|
|
|
const std::string combiner = GetPredicateCombiner(instr.pset.op);
|
|
|
|
|
|
|
|
const std::string predicate =
|
|
|
|
'(' + op_a + ") " + GetPredicateCombiner(instr.pset.cond) + " (" + op_b + ')';
|
|
|
|
const std::string result = '(' + predicate + ") " + combiner + " (" + second_pred + ')';
|
|
|
|
if (instr.pset.bf == 0) {
|
|
|
|
const std::string value = '(' + result + ") ? 0xFFFFFFFF : 0";
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, false, 0, value, 1, 1);
|
|
|
|
} else {
|
|
|
|
const std::string value = '(' + result + ") ? 1.0 : 0.0";
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, value, 1, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-07-04 22:15:03 +02:00
|
|
|
case OpCode::Type::PredicateSetPredicate: {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-09-09 04:44:20 +02:00
|
|
|
case OpCode::Id::PSETP: {
|
|
|
|
const std::string op_a =
|
|
|
|
GetPredicateCondition(instr.psetp.pred12, instr.psetp.neg_pred12 != 0);
|
|
|
|
const std::string op_b =
|
|
|
|
GetPredicateCondition(instr.psetp.pred29, instr.psetp.neg_pred29 != 0);
|
2018-07-04 22:15:03 +02:00
|
|
|
|
2018-09-09 04:44:20 +02:00
|
|
|
// We can't use the constant predicate as destination.
|
|
|
|
ASSERT(instr.psetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
|
2018-07-04 22:15:03 +02:00
|
|
|
|
2018-09-09 06:46:19 +02:00
|
|
|
const std::string second_pred =
|
|
|
|
GetPredicateCondition(instr.psetp.pred39, instr.psetp.neg_pred39 != 0);
|
2018-07-04 22:15:03 +02:00
|
|
|
|
2018-09-09 06:46:19 +02:00
|
|
|
const std::string combiner = GetPredicateCombiner(instr.psetp.op);
|
2018-07-04 22:15:03 +02:00
|
|
|
|
2018-09-09 06:46:19 +02:00
|
|
|
const std::string predicate =
|
|
|
|
'(' + op_a + ") " + GetPredicateCombiner(instr.psetp.cond) + " (" + op_b + ')';
|
2018-07-04 22:15:03 +02:00
|
|
|
|
2018-09-09 04:44:20 +02:00
|
|
|
// Set the primary predicate to the result of Predicate OP SecondPredicate
|
|
|
|
SetPredicate(instr.psetp.pred3,
|
|
|
|
'(' + predicate + ") " + combiner + " (" + second_pred + ')');
|
2018-07-04 22:15:03 +02:00
|
|
|
|
2018-09-09 04:44:20 +02:00
|
|
|
if (instr.psetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
|
|
|
|
// Set the secondary predicate to the result of !Predicate OP SecondPredicate,
|
|
|
|
// if enabled
|
|
|
|
SetPredicate(instr.psetp.pred0,
|
|
|
|
"!(" + predicate + ") " + combiner + " (" + second_pred + ')');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::CSETP: {
|
2018-09-09 06:46:19 +02:00
|
|
|
const std::string pred =
|
2018-09-09 04:44:20 +02:00
|
|
|
GetPredicateCondition(instr.csetp.pred39, instr.csetp.neg_pred39 != 0);
|
2018-09-09 06:46:19 +02:00
|
|
|
const std::string combiner = GetPredicateCombiner(instr.csetp.op);
|
2018-11-22 02:06:01 +01:00
|
|
|
const std::string condition_code = regs.GetConditionCode(instr.csetp.cc);
|
2018-09-09 04:44:20 +02:00
|
|
|
if (instr.csetp.pred3 != static_cast<u64>(Pred::UnusedIndex)) {
|
|
|
|
SetPredicate(instr.csetp.pred3,
|
2018-11-22 02:06:01 +01:00
|
|
|
'(' + condition_code + ") " + combiner + " (" + pred + ')');
|
2018-09-09 04:44:20 +02:00
|
|
|
}
|
|
|
|
if (instr.csetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
|
|
|
|
SetPredicate(instr.csetp.pred0,
|
2018-11-22 02:06:01 +01:00
|
|
|
"!(" + condition_code + ") " + combiner + " (" + pred + ')');
|
2018-09-09 04:44:20 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled predicate instruction: {}", opcode->get().GetName());
|
2018-09-09 04:44:20 +02:00
|
|
|
}
|
2018-07-04 22:15:03 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-11-21 08:36:44 +01:00
|
|
|
case OpCode::Type::RegisterSetPredicate: {
|
|
|
|
UNIMPLEMENTED_IF(instr.r2p.mode != Tegra::Shader::R2pMode::Pr);
|
|
|
|
|
|
|
|
const std::string apply_mask = [&]() {
|
|
|
|
switch (opcode->get().GetId()) {
|
|
|
|
case OpCode::Id::R2P_IMM:
|
|
|
|
return std::to_string(instr.r2p.immediate_mask);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
const std::string mask = '(' + regs.GetRegisterAsInteger(instr.gpr8, 0, false) +
|
|
|
|
" >> " + std::to_string(instr.r2p.byte) + ')';
|
|
|
|
|
|
|
|
constexpr u64 programmable_preds = 7;
|
|
|
|
for (u64 pred = 0; pred < programmable_preds; ++pred) {
|
|
|
|
const auto shift = std::to_string(1 << pred);
|
|
|
|
|
|
|
|
shader.AddLine("if ((" + apply_mask + " & " + shift + ") != 0) {");
|
|
|
|
++shader.scope;
|
|
|
|
|
|
|
|
SetPredicate(pred, '(' + mask + " & " + shift + ") != 0");
|
|
|
|
|
|
|
|
--shader.scope;
|
|
|
|
shader.AddLine('}');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-25 18:17:38 +02:00
|
|
|
case OpCode::Type::FloatSet: {
|
2018-10-22 02:07:15 +02:00
|
|
|
const std::string op_a = GetOperandAbsNeg(regs.GetRegisterAsFloat(instr.gpr8),
|
|
|
|
instr.fset.abs_a != 0, instr.fset.neg_a != 0);
|
2018-04-25 18:17:38 +02:00
|
|
|
|
2018-10-22 02:07:15 +02:00
|
|
|
std::string op_b;
|
2018-04-25 18:17:38 +02:00
|
|
|
|
|
|
|
if (instr.is_b_imm) {
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string imm = GetImmediate19(instr);
|
2018-10-22 02:07:15 +02:00
|
|
|
op_b = imm;
|
2018-04-25 18:17:38 +02:00
|
|
|
} else {
|
|
|
|
if (instr.is_b_gpr) {
|
2018-10-22 02:07:15 +02:00
|
|
|
op_b = regs.GetRegisterAsFloat(instr.gpr20);
|
2018-04-25 18:17:38 +02:00
|
|
|
} else {
|
2018-10-22 02:07:15 +02:00
|
|
|
op_b = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Float);
|
2018-04-25 18:17:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 02:07:15 +02:00
|
|
|
op_b = GetOperandAbsNeg(op_b, instr.fset.abs_b != 0, instr.fset.neg_b != 0);
|
2018-04-25 18:17:38 +02:00
|
|
|
|
2018-06-09 23:19:13 +02:00
|
|
|
// The fset instruction sets a register to 1.0 or -1 (depending on the bf bit) if the
|
|
|
|
// condition is true, and to 0 otherwise.
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string second_pred =
|
2018-05-25 00:28:54 +02:00
|
|
|
GetPredicateCondition(instr.fset.pred39, instr.fset.neg_pred != 0);
|
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string combiner = GetPredicateCombiner(instr.fset.op);
|
2018-05-25 00:28:54 +02:00
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string predicate = "((" +
|
|
|
|
GetPredicateComparison(instr.fset.cond, op_a, op_b) +
|
|
|
|
") " + combiner + " (" + second_pred + "))";
|
2018-05-25 00:28:54 +02:00
|
|
|
|
2018-06-04 18:58:29 +02:00
|
|
|
if (instr.fset.bf) {
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, predicate + " ? 1.0 : 0.0", 1, 1);
|
2018-06-09 23:19:13 +02:00
|
|
|
} else {
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, false, 0, predicate + " ? 0xFFFFFFFF : 0", 1,
|
|
|
|
1);
|
|
|
|
}
|
2018-11-16 17:07:22 +01:00
|
|
|
if (instr.generates_cc.Value() != 0) {
|
|
|
|
regs.SetInternalFlag(InternalFlag::ZeroFlag, predicate);
|
|
|
|
LOG_WARNING(HW_GPU, "FSET Condition Code is incomplete");
|
|
|
|
}
|
2018-06-09 23:19:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Type::IntegerSet: {
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string op_a = regs.GetRegisterAsInteger(instr.gpr8, 0, instr.iset.is_signed);
|
2018-06-09 23:19:13 +02:00
|
|
|
|
|
|
|
std::string op_b;
|
|
|
|
|
|
|
|
if (instr.is_b_imm) {
|
|
|
|
op_b = std::to_string(instr.alu.GetSignedImm20_20());
|
|
|
|
} else {
|
|
|
|
if (instr.is_b_gpr) {
|
|
|
|
op_b = regs.GetRegisterAsInteger(instr.gpr20, 0, instr.iset.is_signed);
|
|
|
|
} else {
|
|
|
|
op_b = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
GLSLRegister::Type::Integer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The iset instruction sets a register to 1.0 or -1 (depending on the bf bit) if the
|
|
|
|
// condition is true, and to 0 otherwise.
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string second_pred =
|
2018-06-09 23:19:13 +02:00
|
|
|
GetPredicateCondition(instr.iset.pred39, instr.iset.neg_pred != 0);
|
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string combiner = GetPredicateCombiner(instr.iset.op);
|
2018-06-09 23:19:13 +02:00
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const std::string predicate = "((" +
|
|
|
|
GetPredicateComparison(instr.iset.cond, op_a, op_b) +
|
|
|
|
") " + combiner + " (" + second_pred + "))";
|
2018-06-09 23:19:13 +02:00
|
|
|
|
|
|
|
if (instr.iset.bf) {
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, predicate + " ? 1.0 : 0.0", 1, 1);
|
2018-06-04 18:58:29 +02:00
|
|
|
} else {
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, false, 0, predicate + " ? 0xFFFFFFFF : 0", 1,
|
|
|
|
1);
|
|
|
|
}
|
2018-04-25 18:17:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-15 07:10:15 +02:00
|
|
|
case OpCode::Type::HalfSet: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.hset2.ftz != 0);
|
2018-10-15 07:10:15 +02:00
|
|
|
|
|
|
|
const std::string op_a =
|
|
|
|
GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.hset2.type_a,
|
|
|
|
instr.hset2.abs_a != 0, instr.hset2.negate_a != 0);
|
|
|
|
|
|
|
|
const std::string op_b = [&]() {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-10-15 07:10:15 +02:00
|
|
|
case OpCode::Id::HSET2_R:
|
|
|
|
return GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr20, 0, false),
|
|
|
|
instr.hset2.type_b, instr.hset2.abs_b != 0,
|
|
|
|
instr.hset2.negate_b != 0);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return std::string("vec2(0)");
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
const std::string second_pred =
|
|
|
|
GetPredicateCondition(instr.hset2.pred39, instr.hset2.neg_pred != 0);
|
|
|
|
|
|
|
|
const std::string combiner = GetPredicateCombiner(instr.hset2.op);
|
|
|
|
|
|
|
|
// HSET2 operates on each half float in the pack.
|
|
|
|
std::string result;
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
const std::string float_value = i == 0 ? "0x00003c00" : "0x3c000000";
|
|
|
|
const std::string integer_value = i == 0 ? "0x0000ffff" : "0xffff0000";
|
|
|
|
const std::string value = instr.hset2.bf == 1 ? float_value : integer_value;
|
|
|
|
|
|
|
|
const std::string comp = std::string(".") + "xy"[i];
|
|
|
|
const std::string predicate =
|
|
|
|
"((" + GetPredicateComparison(instr.hset2.cond, op_a + comp, op_b + comp) +
|
|
|
|
") " + combiner + " (" + second_pred + "))";
|
|
|
|
|
|
|
|
result += '(' + predicate + " ? " + value + " : 0)";
|
|
|
|
if (i == 0) {
|
|
|
|
result += " | ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, false, 0, '(' + result + ')', 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-08-12 22:51:32 +02:00
|
|
|
case OpCode::Type::Xmad: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.xmad.sign_a);
|
|
|
|
UNIMPLEMENTED_IF(instr.xmad.sign_b);
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in XMAD is not implemented");
|
2018-08-12 22:51:32 +02:00
|
|
|
|
|
|
|
std::string op_a{regs.GetRegisterAsInteger(instr.gpr8, 0, instr.xmad.sign_a)};
|
|
|
|
std::string op_b;
|
|
|
|
std::string op_c;
|
|
|
|
|
|
|
|
// TODO(bunnei): Needs to be fixed once op_a or op_b is signed
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.xmad.sign_a != instr.xmad.sign_b);
|
2018-08-12 22:51:32 +02:00
|
|
|
const bool is_signed{instr.xmad.sign_a == 1};
|
|
|
|
|
|
|
|
bool is_merge{};
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-08-12 22:51:32 +02:00
|
|
|
case OpCode::Id::XMAD_CR: {
|
|
|
|
is_merge = instr.xmad.merge_56;
|
|
|
|
op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
instr.xmad.sign_b ? GLSLRegister::Type::Integer
|
|
|
|
: GLSLRegister::Type::UnsignedInteger);
|
|
|
|
op_c += regs.GetRegisterAsInteger(instr.gpr39, 0, is_signed);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::XMAD_RR: {
|
|
|
|
is_merge = instr.xmad.merge_37;
|
|
|
|
op_b += regs.GetRegisterAsInteger(instr.gpr20, 0, instr.xmad.sign_b);
|
|
|
|
op_c += regs.GetRegisterAsInteger(instr.gpr39, 0, is_signed);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::XMAD_RC: {
|
|
|
|
op_b += regs.GetRegisterAsInteger(instr.gpr39, 0, instr.xmad.sign_b);
|
|
|
|
op_c += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
|
|
|
|
is_signed ? GLSLRegister::Type::Integer
|
|
|
|
: GLSLRegister::Type::UnsignedInteger);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::XMAD_IMM: {
|
|
|
|
is_merge = instr.xmad.merge_37;
|
|
|
|
op_b += std::to_string(instr.xmad.imm20_16);
|
|
|
|
op_c += regs.GetRegisterAsInteger(instr.gpr39, 0, is_signed);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled XMAD instruction: {}", opcode->get().GetName());
|
2018-08-12 22:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bunnei): Ensure this is right with signed operands
|
|
|
|
if (instr.xmad.high_a) {
|
|
|
|
op_a = "((" + op_a + ") >> 16)";
|
|
|
|
} else {
|
|
|
|
op_a = "((" + op_a + ") & 0xFFFF)";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string src2 = '(' + op_b + ')'; // Preserve original source 2
|
|
|
|
if (instr.xmad.high_b) {
|
|
|
|
op_b = '(' + src2 + " >> 16)";
|
|
|
|
} else {
|
|
|
|
op_b = '(' + src2 + " & 0xFFFF)";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string product = '(' + op_a + " * " + op_b + ')';
|
|
|
|
if (instr.xmad.product_shift_left) {
|
|
|
|
product = '(' + product + " << 16)";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (instr.xmad.mode) {
|
|
|
|
case Tegra::Shader::XmadMode::None:
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::XmadMode::CLo:
|
|
|
|
op_c = "((" + op_c + ") & 0xFFFF)";
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::XmadMode::CHi:
|
|
|
|
op_c = "((" + op_c + ") >> 16)";
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::XmadMode::CBcc:
|
|
|
|
op_c = "((" + op_c + ") + (" + src2 + "<< 16))";
|
|
|
|
break;
|
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled XMAD mode: {}",
|
|
|
|
static_cast<u32>(instr.xmad.mode.Value()));
|
2018-08-12 22:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string sum{'(' + product + " + " + op_c + ')'};
|
|
|
|
if (is_merge) {
|
|
|
|
sum = "((" + sum + " & 0xFFFF) | (" + src2 + "<< 16))";
|
|
|
|
}
|
|
|
|
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, is_signed, 0, sum, 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
default: {
|
2018-10-30 05:03:25 +01:00
|
|
|
switch (opcode->get().GetId()) {
|
2018-04-08 05:48:38 +02:00
|
|
|
case OpCode::Id::EXIT: {
|
2018-11-22 02:06:01 +01:00
|
|
|
const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
|
|
|
|
UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T,
|
|
|
|
"EXIT condition code used: {}", static_cast<u32>(cc));
|
2018-11-21 01:20:41 +01:00
|
|
|
|
2018-04-26 05:10:41 +02:00
|
|
|
if (stage == Maxwell3D::Regs::ShaderStage::Fragment) {
|
2018-08-18 23:59:44 +02:00
|
|
|
EmitFragmentOutputsWrite();
|
2018-04-26 05:10:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:00:37 +02:00
|
|
|
switch (instr.flow.cond) {
|
|
|
|
case Tegra::Shader::FlowCondition::Always:
|
|
|
|
shader.AddLine("return true;");
|
|
|
|
if (instr.pred.pred_index == static_cast<u64>(Pred::UnusedIndex)) {
|
|
|
|
// If this is an unconditional exit then just end processing here,
|
|
|
|
// otherwise we have to account for the possibility of the condition
|
|
|
|
// not being met, so continue processing the next instruction.
|
|
|
|
offset = PROGRAM_END - 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Tegra::Shader::FlowCondition::Fcsm_Tr:
|
|
|
|
// TODO(bunnei): What is this used for? If we assume this conditon is not
|
|
|
|
// satisifed, dual vertex shaders in Farming Simulator make more sense
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Skipping unknown FlowCondition::Fcsm_Tr");
|
2018-07-13 02:00:37 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled flow condition: {}",
|
|
|
|
static_cast<u32>(instr.flow.cond.Value()));
|
2018-06-05 02:18:11 +02:00
|
|
|
}
|
2018-04-08 05:48:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-04-20 16:17:39 +02:00
|
|
|
case OpCode::Id::KIL: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.flow.cond != Tegra::Shader::FlowCondition::Always);
|
|
|
|
|
2018-11-22 02:06:01 +01:00
|
|
|
const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
|
|
|
|
UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T,
|
|
|
|
"KIL condition code used: {}", static_cast<u32>(cc));
|
2018-08-12 06:06:48 +02:00
|
|
|
|
|
|
|
// Enclose "discard" in a conditional, so that GLSL compilation does not complain
|
|
|
|
// about unexecuted instructions that may follow this.
|
|
|
|
shader.AddLine("if (true) {");
|
|
|
|
++shader.scope;
|
2018-04-20 16:17:39 +02:00
|
|
|
shader.AddLine("discard;");
|
2018-08-12 06:06:48 +02:00
|
|
|
--shader.scope;
|
|
|
|
shader.AddLine("}");
|
|
|
|
|
2018-04-20 16:17:39 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-07 04:17:31 +02:00
|
|
|
case OpCode::Id::OUT_R: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.gpr20.Value() != Register::ZeroIndex,
|
|
|
|
"Stream buffer is not supported");
|
2018-10-07 04:17:31 +02:00
|
|
|
ASSERT_MSG(stage == Maxwell3D::Regs::ShaderStage::Geometry,
|
|
|
|
"OUT is expected to be used in a geometry shader.");
|
|
|
|
|
|
|
|
if (instr.out.emit) {
|
|
|
|
// gpr0 is used to store the next address. Hardware returns a pointer but
|
|
|
|
// we just return the next index with a cyclic cap.
|
|
|
|
const std::string current{regs.GetRegisterAsInteger(instr.gpr8, 0, false)};
|
|
|
|
const std::string next = "((" + current + " + 1" + ") % " +
|
|
|
|
std::to_string(MAX_GEOMETRY_BUFFERS) + ')';
|
|
|
|
shader.AddLine("emit_vertex(" + current + ");");
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, false, 0, next, 1, 1);
|
|
|
|
}
|
|
|
|
if (instr.out.cut) {
|
|
|
|
shader.AddLine("EndPrimitive();");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::MOV_SYS: {
|
|
|
|
switch (instr.sys20) {
|
|
|
|
case Tegra::Shader::SystemVariable::InvocationInfo: {
|
|
|
|
LOG_WARNING(HW_GPU, "MOV_SYS instruction with InvocationInfo is incomplete");
|
|
|
|
regs.SetRegisterToInteger(instr.gpr0, false, 0, "0u", 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-11-25 08:37:29 +01:00
|
|
|
case Tegra::Shader::SystemVariable::Ydirection: {
|
|
|
|
// Config pack's third value is Y_NEGATE's state.
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, "uintBitsToFloat(config_pack[2])", 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-07 04:17:31 +02:00
|
|
|
default: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_MSG("Unhandled system move: {}",
|
|
|
|
static_cast<u32>(instr.sys20.Value()));
|
2018-10-07 04:17:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::ISBERD: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF(instr.isberd.o != 0);
|
|
|
|
UNIMPLEMENTED_IF(instr.isberd.skew != 0);
|
|
|
|
UNIMPLEMENTED_IF(instr.isberd.shift != Tegra::Shader::IsberdShift::None);
|
|
|
|
UNIMPLEMENTED_IF(instr.isberd.mode != Tegra::Shader::IsberdMode::None);
|
2018-10-07 04:17:31 +02:00
|
|
|
ASSERT_MSG(stage == Maxwell3D::Regs::ShaderStage::Geometry,
|
|
|
|
"ISBERD is expected to be used in a geometry shader.");
|
|
|
|
LOG_WARNING(HW_GPU, "ISBERD instruction is incomplete");
|
|
|
|
regs.SetRegisterToFloat(instr.gpr0, 0, regs.GetRegisterAsFloat(instr.gpr8), 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
2018-06-02 21:45:50 +02:00
|
|
|
case OpCode::Id::BRA: {
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
|
|
|
|
"BRA with constant buffers are not implemented");
|
|
|
|
|
2018-11-22 02:06:01 +01:00
|
|
|
const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
|
2018-09-09 17:51:43 +02:00
|
|
|
const u32 target = offset + instr.bra.GetBranchTarget();
|
2018-11-16 17:07:22 +01:00
|
|
|
if (cc != Tegra::Shader::ConditionCode::T) {
|
|
|
|
const std::string condition_code = regs.GetConditionCode(cc);
|
|
|
|
shader.AddLine("if (" + condition_code + "){");
|
|
|
|
shader.scope++;
|
|
|
|
shader.AddLine("{ jmp_to = " + std::to_string(target) + "u; break; }");
|
|
|
|
shader.scope--;
|
|
|
|
shader.AddLine('}');
|
|
|
|
} else {
|
|
|
|
shader.AddLine("{ jmp_to = " + std::to_string(target) + "u; break; }");
|
|
|
|
}
|
2018-06-02 21:45:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-04-11 03:37:49 +02:00
|
|
|
case OpCode::Id::IPA: {
|
|
|
|
const auto& attribute = instr.attribute.fmt28;
|
2018-08-29 06:37:29 +02:00
|
|
|
const auto& reg = instr.gpr0;
|
2018-09-06 03:45:38 +02:00
|
|
|
|
2018-09-05 05:09:24 +02:00
|
|
|
Tegra::Shader::IpaMode input_mode{instr.ipa.interp_mode.Value(),
|
|
|
|
instr.ipa.sample_mode.Value()};
|
|
|
|
regs.SetRegisterToInputAttibute(reg, attribute.element, attribute.index,
|
|
|
|
input_mode);
|
2018-09-06 03:45:38 +02:00
|
|
|
|
|
|
|
if (instr.ipa.saturate) {
|
|
|
|
regs.SetRegisterToFloat(reg, 0, regs.GetRegisterAsFloat(reg), 1, 1, true);
|
|
|
|
}
|
2018-04-11 03:37:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-06-09 05:46:10 +02:00
|
|
|
case OpCode::Id::SSY: {
|
2018-08-11 22:55:11 +02:00
|
|
|
// The SSY opcode tells the GPU where to re-converge divergent execution paths, it
|
|
|
|
// sets the target of the jump that the SYNC instruction will make. The SSY opcode
|
|
|
|
// has a similar structure to the BRA opcode.
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
|
|
|
|
"Constant buffer flow is not supported");
|
2018-08-11 22:55:11 +02:00
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const u32 target = offset + instr.bra.GetBranchTarget();
|
2018-10-16 22:11:26 +02:00
|
|
|
EmitPushToFlowStack(target);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::PBK: {
|
|
|
|
// PBK pushes to a stack the address where BRK will jump to. This shares stack with
|
|
|
|
// SSY but using SYNC on a PBK address will kill the shader execution. We don't
|
|
|
|
// emulate this because it's very unlikely a driver will emit such invalid shader.
|
2018-11-21 01:20:41 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
|
|
|
|
"Constant buffer PBK is not supported");
|
2018-10-16 22:11:26 +02:00
|
|
|
|
|
|
|
const u32 target = offset + instr.bra.GetBranchTarget();
|
|
|
|
EmitPushToFlowStack(target);
|
2018-06-09 05:46:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-08-11 22:55:11 +02:00
|
|
|
case OpCode::Id::SYNC: {
|
2018-11-22 02:06:01 +01:00
|
|
|
const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
|
|
|
|
UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T,
|
|
|
|
"SYNC condition code used: {}", static_cast<u32>(cc));
|
2018-11-21 01:20:41 +01:00
|
|
|
|
|
|
|
// The SYNC opcode jumps to the address previously set by the SSY opcode
|
2018-10-16 22:11:26 +02:00
|
|
|
EmitPopFromFlowStack();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpCode::Id::BRK: {
|
|
|
|
// The BRK opcode jumps to the address previously set by the PBK opcode
|
2018-11-22 02:06:01 +01:00
|
|
|
const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
|
|
|
|
UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T,
|
|
|
|
"BRK condition code used: {}", static_cast<u32>(cc));
|
|
|
|
|
2018-10-16 22:11:26 +02:00
|
|
|
EmitPopFromFlowStack();
|
2018-08-11 22:55:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-07-13 02:00:37 +02:00
|
|
|
case OpCode::Id::DEPBAR: {
|
2018-08-11 22:55:11 +02:00
|
|
|
// TODO(Subv): Find out if we actually have to care about this instruction or if
|
2018-07-04 22:29:51 +02:00
|
|
|
// the GLSL compiler takes care of that for us.
|
2018-08-11 22:55:11 +02:00
|
|
|
LOG_WARNING(HW_GPU, "DEPBAR instruction is stubbed");
|
2018-07-04 22:29:51 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-11 08:56:14 +02:00
|
|
|
case OpCode::Id::VMAD: {
|
2018-11-22 09:35:00 +01:00
|
|
|
UNIMPLEMENTED_IF_MSG(instr.generates_cc,
|
|
|
|
"Condition codes generation in VMAD is not implemented");
|
2018-11-22 02:06:01 +01:00
|
|
|
|
2018-10-18 07:38:58 +02:00
|
|
|
const bool result_signed = instr.video.signed_a == 1 || instr.video.signed_b == 1;
|
|
|
|
const std::string op_a = GetVideoOperandA(instr);
|
|
|
|
const std::string op_b = GetVideoOperandB(instr);
|
2018-10-11 08:56:14 +02:00
|
|
|
const std::string op_c = regs.GetRegisterAsInteger(instr.gpr39, 0, result_signed);
|
|
|
|
|
2018-10-18 07:38:58 +02:00
|
|
|
std::string result = '(' + op_a + " * " + op_b + " + " + op_c + ')';
|
2018-10-11 08:56:14 +02:00
|
|
|
|
2018-10-18 07:38:58 +02:00
|
|
|
switch (instr.vmad.shr) {
|
|
|
|
case Tegra::Shader::VmadShr::Shr7:
|
|
|
|
result = '(' + result + " >> 7)";
|
|
|
|
break;
|
|
|
|
case Tegra::Shader::VmadShr::Shr15:
|
|
|
|
result = '(' + result + " >> 15)";
|
|
|
|
break;
|
2018-10-11 08:56:14 +02:00
|
|
|
}
|
2018-10-18 07:38:58 +02:00
|
|
|
|
2018-10-11 08:56:14 +02:00
|
|
|
regs.SetRegisterToInteger(instr.gpr0, result_signed, 1, result, 1, 1,
|
|
|
|
instr.vmad.saturate == 1, 0, Register::Size::Word,
|
|
|
|
instr.vmad.cc);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-18 07:39:15 +02:00
|
|
|
case OpCode::Id::VSETP: {
|
|
|
|
const std::string op_a = GetVideoOperandA(instr);
|
|
|
|
const std::string op_b = GetVideoOperandB(instr);
|
|
|
|
|
|
|
|
// We can't use the constant predicate as destination.
|
|
|
|
ASSERT(instr.vsetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
|
|
|
|
|
|
|
|
const std::string second_pred = GetPredicateCondition(instr.vsetp.pred39, false);
|
|
|
|
|
|
|
|
const std::string combiner = GetPredicateCombiner(instr.vsetp.op);
|
|
|
|
|
|
|
|
const std::string predicate = GetPredicateComparison(instr.vsetp.cond, op_a, op_b);
|
|
|
|
// Set the primary predicate to the result of Predicate OP SecondPredicate
|
|
|
|
SetPredicate(instr.vsetp.pred3,
|
|
|
|
'(' + predicate + ") " + combiner + " (" + second_pred + ')');
|
|
|
|
|
|
|
|
if (instr.vsetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
|
|
|
|
// Set the secondary predicate to the result of !Predicate OP SecondPredicate,
|
|
|
|
// if enabled
|
|
|
|
SetPredicate(instr.vsetp.pred0,
|
|
|
|
"!(" + predicate + ") " + combiner + " (" + second_pred + ')');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-11-21 01:20:41 +01:00
|
|
|
default: { UNIMPLEMENTED_MSG("Unhandled instruction: {}", opcode->get().GetName()); }
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 16:16:55 +02:00
|
|
|
// Close the predicate condition scope.
|
2018-08-11 23:00:14 +02:00
|
|
|
if (can_be_predicated && instr.pred.pred_index != static_cast<u64>(Pred::UnusedIndex)) {
|
2018-04-20 16:16:55 +02:00
|
|
|
--shader.scope;
|
|
|
|
shader.AddLine('}');
|
|
|
|
}
|
|
|
|
|
2018-04-08 05:48:38 +02:00
|
|
|
return offset + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compiles a range of instructions from Tegra to GLSL.
|
|
|
|
* @param begin the offset of the starting instruction.
|
|
|
|
* @param end the offset where the compilation should stop (exclusive).
|
|
|
|
* @return the offset of the next instruction to compile. PROGRAM_END if the program
|
|
|
|
* terminates.
|
|
|
|
*/
|
|
|
|
u32 CompileRange(u32 begin, u32 end) {
|
|
|
|
u32 program_counter;
|
|
|
|
for (program_counter = begin; program_counter < (begin > end ? PROGRAM_END : end);) {
|
|
|
|
program_counter = CompileInstr(program_counter);
|
|
|
|
}
|
|
|
|
return program_counter;
|
|
|
|
}
|
|
|
|
|
2018-07-13 04:25:03 +02:00
|
|
|
void Generate(const std::string& suffix) {
|
2018-04-08 05:48:38 +02:00
|
|
|
// Add declarations for all subroutines
|
|
|
|
for (const auto& subroutine : subroutines) {
|
|
|
|
shader.AddLine("bool " + subroutine.GetName() + "();");
|
|
|
|
}
|
2018-04-20 02:05:42 +02:00
|
|
|
shader.AddNewLine();
|
2018-04-08 05:48:38 +02:00
|
|
|
|
|
|
|
// Add the main entry point
|
2018-07-13 04:25:03 +02:00
|
|
|
shader.AddLine("bool exec_" + suffix + "() {");
|
2018-04-08 05:48:38 +02:00
|
|
|
++shader.scope;
|
|
|
|
CallSubroutine(GetSubroutine(main_offset, PROGRAM_END));
|
|
|
|
--shader.scope;
|
|
|
|
shader.AddLine("}\n");
|
|
|
|
|
|
|
|
// Add definitions for all subroutines
|
|
|
|
for (const auto& subroutine : subroutines) {
|
|
|
|
std::set<u32> labels = subroutine.labels;
|
|
|
|
|
|
|
|
shader.AddLine("bool " + subroutine.GetName() + "() {");
|
|
|
|
++shader.scope;
|
|
|
|
|
|
|
|
if (labels.empty()) {
|
|
|
|
if (CompileRange(subroutine.begin, subroutine.end) != PROGRAM_END) {
|
|
|
|
shader.AddLine("return false;");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
labels.insert(subroutine.begin);
|
|
|
|
shader.AddLine("uint jmp_to = " + std::to_string(subroutine.begin) + "u;");
|
2018-08-18 17:48:12 +02:00
|
|
|
|
2018-10-16 22:11:26 +02:00
|
|
|
// TODO(Subv): Figure out the actual depth of the flow stack, for now it seems
|
|
|
|
// unlikely that shaders will use 20 nested SSYs and PBKs.
|
|
|
|
constexpr u32 FLOW_STACK_SIZE = 20;
|
|
|
|
shader.AddLine("uint flow_stack[" + std::to_string(FLOW_STACK_SIZE) + "];");
|
|
|
|
shader.AddLine("uint flow_stack_top = 0u;");
|
2018-08-18 17:48:12 +02:00
|
|
|
|
2018-04-08 05:48:38 +02:00
|
|
|
shader.AddLine("while (true) {");
|
|
|
|
++shader.scope;
|
|
|
|
|
|
|
|
shader.AddLine("switch (jmp_to) {");
|
|
|
|
|
|
|
|
for (auto label : labels) {
|
|
|
|
shader.AddLine("case " + std::to_string(label) + "u: {");
|
|
|
|
++shader.scope;
|
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const auto next_it = labels.lower_bound(label + 1);
|
|
|
|
const u32 next_label = next_it == labels.end() ? subroutine.end : *next_it;
|
2018-04-08 05:48:38 +02:00
|
|
|
|
2018-09-09 17:51:43 +02:00
|
|
|
const u32 compile_end = CompileRange(label, next_label);
|
2018-04-08 05:48:38 +02:00
|
|
|
if (compile_end > next_label && compile_end != PROGRAM_END) {
|
|
|
|
// This happens only when there is a label inside a IF/LOOP block
|
2018-08-11 22:55:11 +02:00
|
|
|
shader.AddLine(" jmp_to = " + std::to_string(compile_end) + "u; break; }");
|
2018-04-08 05:48:38 +02:00
|
|
|
labels.emplace(compile_end);
|
|
|
|
}
|
|
|
|
|
|
|
|
--shader.scope;
|
2018-04-20 02:02:24 +02:00
|
|
|
shader.AddLine('}');
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shader.AddLine("default: return false;");
|
2018-04-20 02:02:24 +02:00
|
|
|
shader.AddLine('}');
|
2018-04-08 05:48:38 +02:00
|
|
|
|
|
|
|
--shader.scope;
|
2018-04-20 02:02:24 +02:00
|
|
|
shader.AddLine('}');
|
2018-04-08 05:48:38 +02:00
|
|
|
|
|
|
|
shader.AddLine("return false;");
|
|
|
|
}
|
|
|
|
|
|
|
|
--shader.scope;
|
|
|
|
shader.AddLine("}\n");
|
|
|
|
|
|
|
|
DEBUG_ASSERT(shader.scope == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
GenerateDeclarations();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add declarations for registers
|
|
|
|
void GenerateDeclarations() {
|
2018-07-13 04:25:03 +02:00
|
|
|
regs.GenerateDeclarations(suffix);
|
2018-04-08 05:48:38 +02:00
|
|
|
|
2018-04-20 16:09:50 +02:00
|
|
|
for (const auto& pred : declr_predicates) {
|
|
|
|
declarations.AddLine("bool " + pred + " = false;");
|
|
|
|
}
|
|
|
|
declarations.AddNewLine();
|
2018-03-19 23:51:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-05 03:44:35 +02:00
|
|
|
const std::set<Subroutine>& subroutines;
|
|
|
|
const ProgramCode& program_code;
|
2018-09-11 03:00:55 +02:00
|
|
|
Tegra::Shader::Header header;
|
2018-04-05 03:44:35 +02:00
|
|
|
const u32 main_offset;
|
2018-04-10 04:07:30 +02:00
|
|
|
Maxwell3D::Regs::ShaderStage stage;
|
2018-07-13 04:25:03 +02:00
|
|
|
const std::string& suffix;
|
2018-10-20 01:47:06 +02:00
|
|
|
u64 local_memory_size;
|
2018-11-20 20:14:48 +01:00
|
|
|
std::size_t shader_length;
|
2018-04-05 03:44:35 +02:00
|
|
|
|
|
|
|
ShaderWriter shader;
|
2018-04-08 05:48:38 +02:00
|
|
|
ShaderWriter declarations;
|
2018-10-07 04:17:31 +02:00
|
|
|
GLSLRegisterManager regs{shader, declarations, stage, suffix, header};
|
2018-04-05 03:44:35 +02:00
|
|
|
|
2018-04-08 05:48:38 +02:00
|
|
|
// Declarations
|
2018-04-20 16:09:50 +02:00
|
|
|
std::set<std::string> declr_predicates;
|
2018-10-09 05:34:11 +02:00
|
|
|
}; // namespace OpenGL::GLShader::Decompiler
|
2018-04-08 05:48:38 +02:00
|
|
|
|
|
|
|
std::string GetCommonDeclarations() {
|
2018-07-23 23:09:29 +02:00
|
|
|
return fmt::format("#define MAX_CONSTBUFFER_ELEMENTS {}\n",
|
|
|
|
RasterizerOpenGL::MaxConstbufferSize / sizeof(GLvec4));
|
2018-04-08 05:48:38 +02:00
|
|
|
}
|
2018-03-19 23:51:43 +01:00
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<ProgramResult> DecompileProgram(const ProgramCode& program_code, u32 main_offset,
|
|
|
|
Maxwell3D::Regs::ShaderStage stage,
|
|
|
|
const std::string& suffix) {
|
2018-04-05 03:44:35 +02:00
|
|
|
try {
|
2018-11-20 20:14:48 +01:00
|
|
|
ControlFlowAnalyzer analyzer(program_code, main_offset, suffix);
|
|
|
|
const auto subroutines = analyzer.GetSubroutines();
|
|
|
|
GLSLGenerator generator(subroutines, program_code, main_offset, stage, suffix,
|
|
|
|
analyzer.GetShaderLength());
|
2018-04-15 09:32:12 +02:00
|
|
|
return ProgramResult{generator.GetShaderCode(), generator.GetEntries()};
|
2018-04-05 03:44:35 +02:00
|
|
|
} catch (const DecompileFail& exception) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(HW_GPU, "Shader decompilation failed: {}", exception.what());
|
2018-04-05 03:44:35 +02:00
|
|
|
}
|
2018-10-30 05:03:25 +01:00
|
|
|
return {};
|
2018-03-19 23:51:43 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 10:18:27 +02:00
|
|
|
} // namespace OpenGL::GLShader::Decompiler
|