2015-07-21 23:38:59 +00: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 15:34:51 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <nihstro/shader_bytecode.h>
|
|
|
|
#include "common/assert.h"
|
2015-07-21 23:38:59 +00:00
|
|
|
#include "common/common_funcs.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/vector_math.h"
|
|
|
|
#include "video_core/pica.h"
|
2016-04-30 15:34:51 +00:00
|
|
|
#include "video_core/pica_types.h"
|
2015-07-21 23:38:59 +00:00
|
|
|
|
|
|
|
using nihstro::RegisterType;
|
|
|
|
using nihstro::SourceRegister;
|
|
|
|
using nihstro::DestRegister;
|
|
|
|
|
|
|
|
namespace Pica {
|
|
|
|
|
|
|
|
namespace Shader {
|
|
|
|
|
|
|
|
struct InputVertex {
|
2016-04-28 17:01:47 +00:00
|
|
|
alignas(16) Math::Vec4<float24> attr[16];
|
2015-07-21 23:38:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct OutputVertex {
|
|
|
|
OutputVertex() = default;
|
|
|
|
|
|
|
|
// VS output attributes
|
|
|
|
Math::Vec4<float24> pos;
|
2015-08-16 13:12:43 +00:00
|
|
|
Math::Vec4<float24> quat;
|
2015-07-21 23:38:59 +00:00
|
|
|
Math::Vec4<float24> color;
|
|
|
|
Math::Vec2<float24> tc0;
|
|
|
|
Math::Vec2<float24> tc1;
|
2016-04-18 08:51:13 +00:00
|
|
|
float24 tc0_w;
|
|
|
|
INSERT_PADDING_WORDS(1);
|
2015-09-10 02:39:43 +00:00
|
|
|
Math::Vec3<float24> view;
|
|
|
|
INSERT_PADDING_WORDS(1);
|
2015-07-21 23:38:59 +00:00
|
|
|
Math::Vec2<float24> tc2;
|
|
|
|
|
|
|
|
// Padding for optimal alignment
|
2015-09-10 02:39:43 +00:00
|
|
|
INSERT_PADDING_WORDS(4);
|
2015-07-21 23:38:59 +00:00
|
|
|
|
|
|
|
// Attributes used to store intermediate results
|
|
|
|
|
|
|
|
// position after perspective divide
|
|
|
|
Math::Vec3<float24> screenpos;
|
2015-09-10 02:39:43 +00:00
|
|
|
INSERT_PADDING_WORDS(1);
|
2015-07-21 23:38:59 +00:00
|
|
|
|
|
|
|
// Linear interpolation
|
|
|
|
// factor: 0=this, 1=vtx
|
|
|
|
void Lerp(float24 factor, const OutputVertex& vtx) {
|
|
|
|
pos = pos * factor + vtx.pos * (float24::FromFloat32(1) - factor);
|
|
|
|
|
|
|
|
// TODO: Should perform perspective correct interpolation here...
|
|
|
|
tc0 = tc0 * factor + vtx.tc0 * (float24::FromFloat32(1) - factor);
|
|
|
|
tc1 = tc1 * factor + vtx.tc1 * (float24::FromFloat32(1) - factor);
|
|
|
|
tc2 = tc2 * factor + vtx.tc2 * (float24::FromFloat32(1) - factor);
|
|
|
|
|
|
|
|
screenpos = screenpos * factor + vtx.screenpos * (float24::FromFloat32(1) - factor);
|
|
|
|
|
|
|
|
color = color * factor + vtx.color * (float24::FromFloat32(1) - factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linear interpolation
|
|
|
|
// factor: 0=v0, 1=v1
|
|
|
|
static OutputVertex Lerp(float24 factor, const OutputVertex& v0, const OutputVertex& v1) {
|
|
|
|
OutputVertex ret = v0;
|
|
|
|
ret.Lerp(factor, v1);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-12-17 22:38:03 +00:00
|
|
|
|
|
|
|
static OutputVertex FromRegisters(Math::Vec4<float24> output_regs[16], const Regs& regs,
|
|
|
|
u32 output_mask);
|
2015-07-21 23:38:59 +00:00
|
|
|
};
|
|
|
|
static_assert(std::is_pod<OutputVertex>::value, "Structure is not POD");
|
|
|
|
static_assert(sizeof(OutputVertex) == 32 * sizeof(float), "OutputVertex has invalid size");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2015-08-15 20:51:32 +00:00
|
|
|
struct Registers {
|
|
|
|
// The registers are accessed by the shader JIT using SSE instructions, and are therefore
|
|
|
|
// required to be 16-byte aligned.
|
2016-03-09 06:28:26 +00:00
|
|
|
alignas(16) Math::Vec4<float24> input[16];
|
|
|
|
alignas(16) Math::Vec4<float24> temporary[16];
|
2016-12-17 22:38:03 +00:00
|
|
|
alignas(16) Math::Vec4<float24> output[16];
|
2015-08-15 20:51:32 +00:00
|
|
|
} registers;
|
|
|
|
static_assert(std::is_pod<Registers>::value, "Structure is not POD");
|
2015-07-21 23:38:59 +00:00
|
|
|
|
|
|
|
bool conditional_code[2];
|
|
|
|
|
|
|
|
// Two Address registers and one loop counter
|
|
|
|
// TODO: How many bits do these actually have?
|
|
|
|
s32 address_registers[3];
|
|
|
|
|
2015-09-07 05:49:57 +00:00
|
|
|
static size_t InputOffset(const SourceRegister& reg) {
|
2015-07-21 23:38:59 +00:00
|
|
|
switch (reg.GetRegisterType()) {
|
|
|
|
case RegisterType::Input:
|
2016-09-18 00:38:01 +00:00
|
|
|
return offsetof(UnitState, registers.input) +
|
|
|
|
reg.GetIndex() * sizeof(Math::Vec4<float24>);
|
2015-07-21 23:38:59 +00:00
|
|
|
|
|
|
|
case RegisterType::Temporary:
|
2016-09-18 00:38:01 +00:00
|
|
|
return offsetof(UnitState, registers.temporary) +
|
|
|
|
reg.GetIndex() * sizeof(Math::Vec4<float24>);
|
2015-07-21 23:38:59 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 05:49:57 +00:00
|
|
|
static size_t OutputOffset(const DestRegister& reg) {
|
2015-07-21 23:38:59 +00:00
|
|
|
switch (reg.GetRegisterType()) {
|
|
|
|
case RegisterType::Output:
|
2016-12-17 22:38:03 +00:00
|
|
|
return offsetof(UnitState, registers.output) +
|
2016-09-18 00:38:01 +00:00
|
|
|
reg.GetIndex() * sizeof(Math::Vec4<float24>);
|
2015-07-21 23:38:59 +00:00
|
|
|
|
|
|
|
case RegisterType::Temporary:
|
2016-09-18 00:38:01 +00:00
|
|
|
return offsetof(UnitState, registers.temporary) +
|
|
|
|
reg.GetIndex() * sizeof(Math::Vec4<float24>);
|
2015-07-21 23:38:59 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-12-17 05:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the unit state with an input vertex.
|
|
|
|
*
|
|
|
|
* @param input Input vertex into the shader
|
|
|
|
* @param num_attributes The number of vertex shader attributes to load
|
|
|
|
*/
|
|
|
|
void LoadInputVertex(const InputVertex& input, int num_attributes);
|
2015-07-21 23:38:59 +00:00
|
|
|
};
|
|
|
|
|
2016-03-30 00:45:18 +00:00
|
|
|
struct ShaderSetup {
|
|
|
|
struct {
|
|
|
|
// The float uniforms are accessed by the shader JIT using SSE instructions, and are
|
|
|
|
// therefore required to be 16-byte aligned.
|
|
|
|
alignas(16) Math::Vec4<float24> f[96];
|
2015-07-23 03:25:30 +00:00
|
|
|
|
2016-03-30 00:45:18 +00:00
|
|
|
std::array<bool, 16> b;
|
|
|
|
std::array<Math::Vec4<u8>, 4> i;
|
|
|
|
} uniforms;
|
2015-07-11 23:57:59 +00:00
|
|
|
|
2016-12-16 06:35:34 +00:00
|
|
|
static size_t GetFloatUniformOffset(unsigned index) {
|
|
|
|
return offsetof(ShaderSetup, uniforms.f) + index * sizeof(Math::Vec4<float24>);
|
|
|
|
}
|
2016-05-13 06:46:14 +00:00
|
|
|
|
2016-12-16 06:35:34 +00:00
|
|
|
static size_t GetBoolUniformOffset(unsigned index) {
|
|
|
|
return offsetof(ShaderSetup, uniforms.b) + index * sizeof(bool);
|
|
|
|
}
|
2016-05-13 06:46:14 +00:00
|
|
|
|
2016-12-16 06:35:34 +00:00
|
|
|
static size_t GetIntUniformOffset(unsigned index) {
|
|
|
|
return offsetof(ShaderSetup, uniforms.i) + index * sizeof(Math::Vec4<u8>);
|
2016-05-13 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 00:45:18 +00:00
|
|
|
std::array<u32, 1024> program_code;
|
|
|
|
std::array<u32, 1024> swizzle_data;
|
2016-12-18 00:06:04 +00:00
|
|
|
|
|
|
|
/// Data private to ShaderEngines
|
|
|
|
struct EngineData {
|
|
|
|
/// Used by the JIT, points to a compiled shader object.
|
|
|
|
const void* cached_shader = nullptr;
|
|
|
|
} engine_data;
|
2016-12-17 07:21:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ShaderEngine {
|
|
|
|
public:
|
|
|
|
virtual ~ShaderEngine() = default;
|
2016-03-30 00:45:18 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-18 00:38:01 +00:00
|
|
|
* Performs any shader unit setup that only needs to happen once per shader (as opposed to once
|
2016-09-19 01:01:46 +00:00
|
|
|
* per vertex, which would happen within the `Run` function).
|
2016-03-30 00:45:18 +00:00
|
|
|
*/
|
2016-12-18 00:06:04 +00:00
|
|
|
virtual void SetupBatch(ShaderSetup& setup) = 0;
|
2016-03-30 00:45:18 +00: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 00:45:18 +00:00
|
|
|
*/
|
2016-12-18 00:06:04 +00:00
|
|
|
virtual void Run(const ShaderSetup& setup, UnitState& state,
|
|
|
|
unsigned int entry_point) const = 0;
|
2016-03-30 00:45:18 +00:00
|
|
|
};
|
2015-07-21 23:38:59 +00: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
|
|
|
|
2015-07-21 23:38:59 +00:00
|
|
|
} // namespace Shader
|
|
|
|
|
|
|
|
} // namespace Pica
|