2015-07-22 00:38:59 +01:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-04-30 16:34:51 +01:00
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
2017-07-25 20:30:29 +01:00
|
|
|
#include <functional>
|
2023-03-27 12:29:17 +01:00
|
|
|
#include <span>
|
2016-04-30 16:34:51 +01:00
|
|
|
#include <type_traits>
|
2020-01-03 14:00:10 +00:00
|
|
|
#include <boost/serialization/access.hpp>
|
2019-08-08 05:25:24 +01:00
|
|
|
#include <boost/serialization/array.hpp>
|
2019-08-11 00:20:09 +01:00
|
|
|
#include <boost/serialization/base_object.hpp>
|
2019-12-27 21:07:29 +00:00
|
|
|
#include <nihstro/shader_bytecode.h>
|
2016-04-30 16:34:51 +01:00
|
|
|
#include "common/assert.h"
|
2015-07-22 00:38:59 +01:00
|
|
|
#include "common/common_funcs.h"
|
|
|
|
#include "common/common_types.h"
|
2018-04-17 07:47:59 +01:00
|
|
|
#include "common/hash.h"
|
2019-12-27 21:07:29 +00:00
|
|
|
#include "common/vector_math.h"
|
2016-04-30 16:34:51 +01:00
|
|
|
#include "video_core/pica_types.h"
|
2017-01-28 23:12:09 +00:00
|
|
|
#include "video_core/regs_rasterizer.h"
|
|
|
|
#include "video_core/regs_shader.h"
|
2015-07-22 00:38:59 +01:00
|
|
|
|
2018-03-09 17:54:43 +00:00
|
|
|
using nihstro::DestRegister;
|
2015-07-22 00:38:59 +01:00
|
|
|
using nihstro::RegisterType;
|
|
|
|
using nihstro::SourceRegister;
|
|
|
|
|
2019-02-19 08:09:57 +00:00
|
|
|
namespace Pica::Shader {
|
2015-07-22 00:38:59 +01:00
|
|
|
|
2017-05-08 22:37:38 +01:00
|
|
|
constexpr unsigned MAX_PROGRAM_CODE_LENGTH = 4096;
|
|
|
|
constexpr unsigned MAX_SWIZZLE_DATA_LENGTH = 4096;
|
2019-09-07 23:11:09 +01:00
|
|
|
using ProgramCode = std::array<u32, MAX_PROGRAM_CODE_LENGTH>;
|
|
|
|
using SwizzleData = std::array<u32, MAX_SWIZZLE_DATA_LENGTH>;
|
2017-05-08 22:37:38 +01:00
|
|
|
|
2016-12-19 00:42:19 +00:00
|
|
|
struct AttributeBuffer {
|
2019-02-27 03:38:34 +00:00
|
|
|
alignas(16) Common::Vec4<float24> attr[16];
|
2019-08-11 00:20:09 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
template <class Archive>
|
2019-12-27 21:07:29 +00:00
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& attr;
|
2019-08-11 00:20:09 +01:00
|
|
|
}
|
2015-07-22 00:38:59 +01:00
|
|
|
};
|
|
|
|
|
2017-07-25 20:30:29 +01:00
|
|
|
/// Handler type for receiving vertex outputs from vertex shader or geometry shader
|
|
|
|
using VertexHandler = std::function<void(const AttributeBuffer&)>;
|
|
|
|
|
|
|
|
/// Handler type for signaling to invert the vertex order of the next triangle
|
|
|
|
using WindingSetter = std::function<void()>;
|
|
|
|
|
2015-07-22 00:38:59 +01:00
|
|
|
struct OutputVertex {
|
2019-02-27 03:38:34 +00:00
|
|
|
Common::Vec4<float24> pos;
|
|
|
|
Common::Vec4<float24> quat;
|
|
|
|
Common::Vec4<float24> color;
|
|
|
|
Common::Vec2<float24> tc0;
|
|
|
|
Common::Vec2<float24> tc1;
|
2016-04-18 09:51:13 +01:00
|
|
|
float24 tc0_w;
|
|
|
|
INSERT_PADDING_WORDS(1);
|
2019-02-27 03:38:34 +00:00
|
|
|
Common::Vec3<float24> view;
|
2015-09-10 03:39:43 +01:00
|
|
|
INSERT_PADDING_WORDS(1);
|
2019-02-27 03:38:34 +00:00
|
|
|
Common::Vec2<float24> tc2;
|
2015-07-22 00:38:59 +01:00
|
|
|
|
2018-01-02 23:32:33 +00:00
|
|
|
static void ValidateSemantics(const RasterizerRegs& regs);
|
2017-07-25 22:39:43 +01:00
|
|
|
static OutputVertex FromAttributeBuffer(const RasterizerRegs& regs,
|
|
|
|
const AttributeBuffer& output);
|
2019-12-22 16:06:26 +00:00
|
|
|
|
2020-01-03 14:00:10 +00:00
|
|
|
private:
|
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int) {
|
|
|
|
ar& pos;
|
|
|
|
ar& quat;
|
2020-03-28 16:28:07 +00:00
|
|
|
ar& color;
|
2020-01-03 14:00:10 +00:00
|
|
|
ar& tc0;
|
|
|
|
ar& tc1;
|
|
|
|
ar& tc0_w;
|
|
|
|
ar& view;
|
|
|
|
ar& tc2;
|
|
|
|
}
|
|
|
|
friend class boost::serialization::access;
|
2015-07-22 00:38:59 +01:00
|
|
|
};
|
2016-12-19 07:43:37 +00:00
|
|
|
#define ASSERT_POS(var, pos) \
|
|
|
|
static_assert(offsetof(OutputVertex, var) == pos * sizeof(float24), "Semantic at wrong " \
|
|
|
|
"offset.")
|
2017-01-28 04:16:36 +00:00
|
|
|
ASSERT_POS(pos, RasterizerRegs::VSOutputAttributes::POSITION_X);
|
|
|
|
ASSERT_POS(quat, RasterizerRegs::VSOutputAttributes::QUATERNION_X);
|
|
|
|
ASSERT_POS(color, RasterizerRegs::VSOutputAttributes::COLOR_R);
|
|
|
|
ASSERT_POS(tc0, RasterizerRegs::VSOutputAttributes::TEXCOORD0_U);
|
|
|
|
ASSERT_POS(tc1, RasterizerRegs::VSOutputAttributes::TEXCOORD1_U);
|
|
|
|
ASSERT_POS(tc0_w, RasterizerRegs::VSOutputAttributes::TEXCOORD0_W);
|
|
|
|
ASSERT_POS(view, RasterizerRegs::VSOutputAttributes::VIEW_X);
|
|
|
|
ASSERT_POS(tc2, RasterizerRegs::VSOutputAttributes::TEXCOORD2_U);
|
2016-12-19 07:43:37 +00:00
|
|
|
#undef ASSERT_POS
|
2022-09-21 17:36:12 +01:00
|
|
|
static_assert(std::is_trivial_v<OutputVertex>, "Structure is not POD");
|
2016-12-19 07:43:37 +00:00
|
|
|
static_assert(sizeof(OutputVertex) == 24 * sizeof(float), "OutputVertex has invalid size");
|
2015-07-22 00:38:59 +01:00
|
|
|
|
2017-07-25 20:30:29 +01:00
|
|
|
/**
|
|
|
|
* This structure contains state information for primitive emitting in geometry shader.
|
|
|
|
*/
|
|
|
|
struct GSEmitter {
|
2017-12-10 04:30:14 +00:00
|
|
|
std::array<AttributeBuffer, 3> buffer;
|
2017-07-25 20:30:29 +01:00
|
|
|
u8 vertex_id;
|
|
|
|
bool prim_emit;
|
|
|
|
bool winding;
|
|
|
|
u32 output_mask;
|
|
|
|
|
|
|
|
// Function objects are hidden behind a raw pointer to make the structure standard layout type,
|
|
|
|
// for JIT to use offsetof to access other members.
|
|
|
|
struct Handlers {
|
|
|
|
VertexHandler vertex_handler;
|
|
|
|
WindingSetter winding_setter;
|
|
|
|
} * handlers;
|
|
|
|
|
|
|
|
GSEmitter();
|
|
|
|
~GSEmitter();
|
2023-03-27 12:29:17 +01:00
|
|
|
void Emit(std::span<Common::Vec4<float24>, 16> output_regs);
|
2019-08-11 00:20:09 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
template <class Archive>
|
2019-12-27 21:07:29 +00:00
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& buffer;
|
|
|
|
ar& vertex_id;
|
|
|
|
ar& prim_emit;
|
|
|
|
ar& winding;
|
|
|
|
ar& output_mask;
|
2019-08-11 00:20:09 +01:00
|
|
|
// Handlers are ignored because they're constant
|
|
|
|
}
|
2017-07-25 20:30:29 +01:00
|
|
|
};
|
|
|
|
static_assert(std::is_standard_layout<GSEmitter>::value, "GSEmitter is not standard layout type");
|
|
|
|
|
2015-07-22 00:38:59 +01:00
|
|
|
/**
|
|
|
|
* This structure contains the state information that needs to be unique for a shader unit. The 3DS
|
|
|
|
* has four shader units that process shaders in parallel. At the present, Citra only implements a
|
|
|
|
* single shader unit that processes all shaders serially. Putting the state information in a struct
|
|
|
|
* here will make it easier for us to parallelize the shader processing later.
|
|
|
|
*/
|
|
|
|
struct UnitState {
|
2017-07-25 20:30:29 +01:00
|
|
|
explicit UnitState(GSEmitter* emitter = nullptr);
|
2015-08-15 21:51:32 +01:00
|
|
|
struct Registers {
|
|
|
|
// The registers are accessed by the shader JIT using SSE instructions, and are therefore
|
|
|
|
// required to be 16-byte aligned.
|
2023-03-27 12:29:17 +01:00
|
|
|
alignas(16) std::array<Common::Vec4<float24>, 16> input;
|
|
|
|
alignas(16) std::array<Common::Vec4<float24>, 16> temporary;
|
|
|
|
alignas(16) std::array<Common::Vec4<float24>, 16> output;
|
2019-08-11 00:20:09 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
template <class Archive>
|
2019-12-27 21:07:29 +00:00
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& input;
|
|
|
|
ar& temporary;
|
|
|
|
ar& output;
|
2019-08-11 00:20:09 +01:00
|
|
|
}
|
2015-08-15 21:51:32 +01:00
|
|
|
} registers;
|
2022-09-21 17:36:12 +01:00
|
|
|
static_assert(std::is_trivial_v<Registers>, "Structure is not POD");
|
2015-07-22 00:38:59 +01:00
|
|
|
|
|
|
|
bool conditional_code[2];
|
|
|
|
|
|
|
|
// Two Address registers and one loop counter
|
|
|
|
// TODO: How many bits do these actually have?
|
|
|
|
s32 address_registers[3];
|
|
|
|
|
2017-07-25 20:30:29 +01:00
|
|
|
GSEmitter* emitter_ptr;
|
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
static std::size_t InputOffset(const SourceRegister& reg) {
|
2015-07-22 00:38:59 +01:00
|
|
|
switch (reg.GetRegisterType()) {
|
|
|
|
case RegisterType::Input:
|
2016-09-18 01:38:01 +01:00
|
|
|
return offsetof(UnitState, registers.input) +
|
2019-02-27 03:38:34 +00:00
|
|
|
reg.GetIndex() * sizeof(Common::Vec4<float24>);
|
2015-07-22 00:38:59 +01:00
|
|
|
|
|
|
|
case RegisterType::Temporary:
|
2016-09-18 01:38:01 +01:00
|
|
|
return offsetof(UnitState, registers.temporary) +
|
2019-02-27 03:38:34 +00:00
|
|
|
reg.GetIndex() * sizeof(Common::Vec4<float24>);
|
2015-07-22 00:38:59 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
static std::size_t OutputOffset(const DestRegister& reg) {
|
2015-07-22 00:38:59 +01:00
|
|
|
switch (reg.GetRegisterType()) {
|
|
|
|
case RegisterType::Output:
|
2016-12-17 22:38:03 +00:00
|
|
|
return offsetof(UnitState, registers.output) +
|
2019-02-27 03:38:34 +00:00
|
|
|
reg.GetIndex() * sizeof(Common::Vec4<float24>);
|
2015-07-22 00:38:59 +01:00
|
|
|
|
|
|
|
case RegisterType::Temporary:
|
2016-09-18 01:38:01 +01:00
|
|
|
return offsetof(UnitState, registers.temporary) +
|
2019-02-27 03:38:34 +00:00
|
|
|
reg.GetIndex() * sizeof(Common::Vec4<float24>);
|
2015-07-22 00:38:59 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-12-17 05:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the unit state with an input vertex.
|
|
|
|
*
|
2016-12-19 01:25:03 +00:00
|
|
|
* @param config Shader configuration registers corresponding to the unit.
|
|
|
|
* @param input Attribute buffer to load into the input registers.
|
2016-12-17 05:41:38 +00:00
|
|
|
*/
|
2017-01-28 21:03:13 +00:00
|
|
|
void LoadInput(const ShaderRegs& config, const AttributeBuffer& input);
|
2016-12-19 01:58:30 +00:00
|
|
|
|
2017-01-28 21:03:13 +00:00
|
|
|
void WriteOutput(const ShaderRegs& config, AttributeBuffer& output);
|
2019-08-11 00:20:09 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
template <class Archive>
|
2019-12-27 21:07:29 +00:00
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& registers;
|
|
|
|
ar& conditional_code;
|
|
|
|
ar& address_registers;
|
2019-08-13 17:37:04 +01:00
|
|
|
// emitter_ptr is only set by GSUnitState and is serialized there
|
2019-08-11 00:20:09 +01:00
|
|
|
}
|
2015-07-22 00:38:59 +01:00
|
|
|
};
|
|
|
|
|
2017-07-25 20:30:29 +01:00
|
|
|
/**
|
|
|
|
* This is an extended shader unit state that represents the special unit that can run both vertex
|
|
|
|
* shader and geometry shader. It contains an additional primitive emitter and utilities for
|
|
|
|
* geometry shader.
|
|
|
|
*/
|
|
|
|
struct GSUnitState : public UnitState {
|
|
|
|
GSUnitState();
|
|
|
|
void SetVertexHandler(VertexHandler vertex_handler, WindingSetter winding_setter);
|
|
|
|
void ConfigOutput(const ShaderRegs& config);
|
|
|
|
|
|
|
|
GSEmitter emitter;
|
2019-08-11 00:20:09 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
template <class Archive>
|
2019-12-27 21:07:29 +00:00
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& boost::serialization::base_object<UnitState>(*this);
|
|
|
|
ar& emitter;
|
2019-08-11 00:20:09 +01:00
|
|
|
}
|
2017-07-25 20:30:29 +01:00
|
|
|
};
|
|
|
|
|
2018-04-17 07:35:43 +01:00
|
|
|
struct Uniforms {
|
|
|
|
// The float uniforms are accessed by the shader JIT using SSE instructions, and are
|
|
|
|
// therefore required to be 16-byte aligned.
|
2019-02-27 03:38:34 +00:00
|
|
|
alignas(16) Common::Vec4<float24> f[96];
|
2015-07-23 04:25:30 +01:00
|
|
|
|
2018-04-17 07:35:43 +01:00
|
|
|
std::array<bool, 16> b;
|
2019-02-27 03:38:34 +00:00
|
|
|
std::array<Common::Vec4<u8>, 4> i;
|
2015-07-12 00:57:59 +01:00
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
static std::size_t GetFloatUniformOffset(unsigned index) {
|
2019-02-27 03:38:34 +00:00
|
|
|
return offsetof(Uniforms, f) + index * sizeof(Common::Vec4<float24>);
|
2016-12-16 06:35:34 +00:00
|
|
|
}
|
2016-05-13 07:46:14 +01:00
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
static std::size_t GetBoolUniformOffset(unsigned index) {
|
2018-04-17 07:35:43 +01:00
|
|
|
return offsetof(Uniforms, b) + index * sizeof(bool);
|
2016-12-16 06:35:34 +00:00
|
|
|
}
|
2016-05-13 07:46:14 +01:00
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
static std::size_t GetIntUniformOffset(unsigned index) {
|
2019-02-27 03:38:34 +00:00
|
|
|
return offsetof(Uniforms, i) + index * sizeof(Common::Vec4<u8>);
|
2016-05-13 07:46:14 +01:00
|
|
|
}
|
2019-08-08 05:25:24 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class boost::serialization::access;
|
2019-12-27 21:07:29 +00:00
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& f;
|
|
|
|
ar& b;
|
|
|
|
ar& i;
|
2019-08-08 05:25:24 +01:00
|
|
|
}
|
2018-04-17 07:35:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ShaderSetup {
|
|
|
|
Uniforms uniforms;
|
2016-05-13 07:46:14 +01:00
|
|
|
|
2019-09-07 23:11:09 +01:00
|
|
|
ProgramCode program_code;
|
|
|
|
SwizzleData swizzle_data;
|
2016-12-18 00:06:04 +00:00
|
|
|
|
|
|
|
/// Data private to ShaderEngines
|
|
|
|
struct EngineData {
|
2016-12-18 00:16:02 +00:00
|
|
|
unsigned int entry_point;
|
2016-12-18 00:06:04 +00:00
|
|
|
/// Used by the JIT, points to a compiled shader object.
|
|
|
|
const void* cached_shader = nullptr;
|
|
|
|
} engine_data;
|
2018-04-17 07:47:59 +01:00
|
|
|
|
|
|
|
void MarkProgramCodeDirty() {
|
|
|
|
program_code_hash_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkSwizzleDataDirty() {
|
|
|
|
swizzle_data_hash_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetProgramCodeHash() {
|
|
|
|
if (program_code_hash_dirty) {
|
|
|
|
program_code_hash = Common::ComputeHash64(&program_code, sizeof(program_code));
|
|
|
|
program_code_hash_dirty = false;
|
|
|
|
}
|
|
|
|
return program_code_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetSwizzleDataHash() {
|
|
|
|
if (swizzle_data_hash_dirty) {
|
|
|
|
swizzle_data_hash = Common::ComputeHash64(&swizzle_data, sizeof(swizzle_data));
|
|
|
|
swizzle_data_hash_dirty = false;
|
|
|
|
}
|
|
|
|
return swizzle_data_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool program_code_hash_dirty = true;
|
|
|
|
bool swizzle_data_hash_dirty = true;
|
|
|
|
u64 program_code_hash = 0xDEADC0DE;
|
|
|
|
u64 swizzle_data_hash = 0xDEADC0DE;
|
2019-08-08 05:25:24 +01:00
|
|
|
|
|
|
|
friend class boost::serialization::access;
|
2019-12-27 21:07:29 +00:00
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int file_version) {
|
|
|
|
ar& uniforms;
|
|
|
|
ar& program_code;
|
|
|
|
ar& swizzle_data;
|
|
|
|
ar& program_code_hash_dirty;
|
|
|
|
ar& swizzle_data_hash_dirty;
|
|
|
|
ar& program_code_hash;
|
|
|
|
ar& swizzle_data_hash;
|
2019-08-08 05:25:24 +01:00
|
|
|
}
|
2016-12-17 07:21:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ShaderEngine {
|
|
|
|
public:
|
|
|
|
virtual ~ShaderEngine() = default;
|
2016-03-30 01:45:18 +01:00
|
|
|
|
|
|
|
/**
|
2016-09-18 01:38:01 +01:00
|
|
|
* Performs any shader unit setup that only needs to happen once per shader (as opposed to once
|
2016-09-19 02:01:46 +01:00
|
|
|
* per vertex, which would happen within the `Run` function).
|
2016-03-30 01:45:18 +01:00
|
|
|
*/
|
2016-12-18 00:16:02 +00:00
|
|
|
virtual void SetupBatch(ShaderSetup& setup, unsigned int entry_point) = 0;
|
2016-03-30 01:45:18 +01:00
|
|
|
|
|
|
|
/**
|
2016-12-18 00:06:04 +00:00
|
|
|
* Runs the currently setup shader.
|
|
|
|
*
|
|
|
|
* @param setup Shader engine state, must be setup with SetupBatch on each shader change.
|
|
|
|
* @param state Shader unit state, must be setup with input data before each shader invocation.
|
2016-03-30 01:45:18 +01:00
|
|
|
*/
|
2016-12-18 00:16:02 +00:00
|
|
|
virtual void Run(const ShaderSetup& setup, UnitState& state) const = 0;
|
2016-03-30 01:45:18 +01:00
|
|
|
};
|
2015-07-22 00:38:59 +01:00
|
|
|
|
2016-12-17 07:21:26 +00:00
|
|
|
// TODO(yuriks): Remove and make it non-global state somewhere
|
|
|
|
ShaderEngine* GetEngine();
|
2016-12-17 09:21:16 +00:00
|
|
|
void Shutdown();
|
2016-12-17 07:21:26 +00:00
|
|
|
|
2019-02-19 08:09:57 +00:00
|
|
|
} // namespace Pica::Shader
|