VideoCore/Shader: Split shader uniform state and shader engine

Currently there's only a single dummy implementation, which will be
split in a following commit.
This commit is contained in:
Yuri Kunde Schlesner 2016-12-16 23:21:26 -08:00
parent bd82cffd0b
commit dd4a1672a7
5 changed files with 57 additions and 22 deletions

View file

@ -518,7 +518,9 @@ void GraphicsVertexShaderWidget::Reload(bool replace_vertex_data, void* vertex_d
info.labels.insert({entry_point, "main"}); info.labels.insert({entry_point, "main"});
// Generate debug information // Generate debug information
debug_data = shader_setup.ProduceDebugInfo(input_vertex, num_attributes, entry_point); auto* shader_engine = Pica::Shader::GetEngine();
shader_engine->SetupBatch(&shader_setup);
debug_data = shader_engine->ProduceDebugInfo(input_vertex, num_attributes, entry_point);
// Reload widget state // Reload widget state
for (int attr = 0; attr < num_attributes; ++attr) { for (int attr = 0; attr < num_attributes; ++attr) {

View file

@ -142,15 +142,16 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) {
MICROPROFILE_SCOPE(GPU_Drawing); MICROPROFILE_SCOPE(GPU_Drawing);
immediate_attribute_id = 0; immediate_attribute_id = 0;
Shader::UnitState shader_unit; auto* shader_engine = Shader::GetEngine();
g_state.vs.Setup(); shader_engine->SetupBatch(&g_state.vs);
// Send to vertex shader // Send to vertex shader
if (g_debug_context) if (g_debug_context)
g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation, g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
static_cast<void*>(&immediate_input)); static_cast<void*>(&immediate_input));
Shader::UnitState shader_unit;
shader_unit.LoadInputVertex(immediate_input, regs.vs.num_input_attributes + 1); shader_unit.LoadInputVertex(immediate_input, regs.vs.num_input_attributes + 1);
g_state.vs.Run(shader_unit, regs.vs.main_offset); shader_engine->Run(shader_unit, regs.vs.main_offset);
Shader::OutputVertex output_vertex = Shader::OutputVertex output_vertex =
shader_unit.output_registers.ToVertex(regs.vs); shader_unit.output_registers.ToVertex(regs.vs);
@ -244,8 +245,10 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) {
unsigned int vertex_cache_pos = 0; unsigned int vertex_cache_pos = 0;
vertex_cache_ids.fill(-1); vertex_cache_ids.fill(-1);
auto* shader_engine = Shader::GetEngine();
Shader::UnitState shader_unit; Shader::UnitState shader_unit;
g_state.vs.Setup();
shader_engine->SetupBatch(&g_state.vs);
for (unsigned int index = 0; index < regs.num_vertices; ++index) { for (unsigned int index = 0; index < regs.num_vertices; ++index) {
// Indexed rendering doesn't use the start offset // Indexed rendering doesn't use the start offset
@ -285,7 +288,7 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) {
g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation, g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
(void*)&input); (void*)&input);
shader_unit.LoadInputVertex(input, loader.GetNumTotalAttributes()); shader_unit.LoadInputVertex(input, loader.GetNumTotalAttributes());
g_state.vs.Run(shader_unit, regs.vs.main_offset); shader_engine->Run(shader_unit, regs.vs.main_offset);
// Retrieve vertex from register data // Retrieve vertex from register data
output_vertex = shader_unit.output_registers.ToVertex(regs.vs); output_vertex = shader_unit.output_registers.ToVertex(regs.vs);

View file

@ -87,6 +87,17 @@ void UnitState::LoadInputVertex(const InputVertex& input, int num_attributes) {
conditional_code[1] = false; conditional_code[1] = false;
} }
class MergedShaderEngine : public ShaderEngine {
public:
void SetupBatch(const ShaderSetup* setup) override;
void Run(UnitState& state, unsigned int entry_point) const override;
DebugData<true> ProduceDebugInfo(const InputVertex& input, int num_attributes,
unsigned int entry_point) const override;
private:
const ShaderSetup* setup = nullptr;
};
#ifdef ARCHITECTURE_x86_64 #ifdef ARCHITECTURE_x86_64
static std::unordered_map<u64, std::unique_ptr<JitShader>> shader_map; static std::unordered_map<u64, std::unique_ptr<JitShader>> shader_map;
static const JitShader* jit_shader; static const JitShader* jit_shader;
@ -98,13 +109,17 @@ void ClearCache() {
#endif // ARCHITECTURE_x86_64 #endif // ARCHITECTURE_x86_64
} }
void ShaderSetup::Setup() { void MergedShaderEngine::SetupBatch(const ShaderSetup* setup_) {
setup = setup_;
if (setup == nullptr)
return;
#ifdef ARCHITECTURE_x86_64 #ifdef ARCHITECTURE_x86_64
if (VideoCore::g_shader_jit_enabled) { if (VideoCore::g_shader_jit_enabled) {
u64 cache_key = u64 code_hash = Common::ComputeHash64(&setup->program_code, sizeof(setup->program_code));
Common::ComputeHash64(&program_code, sizeof(program_code)) ^ u64 swizzle_hash = Common::ComputeHash64(&setup->swizzle_data, sizeof(setup->swizzle_data));
Common::ComputeHash64(&swizzle_data, sizeof(swizzle_data));
u64 cache_key = code_hash ^ swizzle_hash;
auto iter = shader_map.find(cache_key); auto iter = shader_map.find(cache_key);
if (iter != shader_map.end()) { if (iter != shader_map.end()) {
jit_shader = iter->second.get(); jit_shader = iter->second.get();
@ -120,26 +135,28 @@ void ShaderSetup::Setup() {
MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240)); MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240));
void ShaderSetup::Run(UnitState& state, unsigned int entry_point) const { void MergedShaderEngine::Run(UnitState& state, unsigned int entry_point) const {
ASSERT(setup != nullptr);
ASSERT(entry_point < 1024); ASSERT(entry_point < 1024);
MICROPROFILE_SCOPE(GPU_Shader); MICROPROFILE_SCOPE(GPU_Shader);
#ifdef ARCHITECTURE_x86_64 #ifdef ARCHITECTURE_x86_64
if (VideoCore::g_shader_jit_enabled) { if (VideoCore::g_shader_jit_enabled) {
jit_shader->Run(*this, state, entry_point); jit_shader->Run(*setup, state, entry_point);
} else { } else {
DebugData<false> dummy_debug_data; DebugData<false> dummy_debug_data;
RunInterpreter(*this, state, dummy_debug_data, entry_point); RunInterpreter(*setup, state, dummy_debug_data, entry_point);
} }
#else #else
DebugData<false> dummy_debug_data; DebugData<false> dummy_debug_data;
RunInterpreter(*this, state, dummy_debug_data, entry_point); RunInterpreter(*setup, state, dummy_debug_data, entry_point);
#endif // ARCHITECTURE_x86_64 #endif // ARCHITECTURE_x86_64
} }
DebugData<true> ShaderSetup::ProduceDebugInfo(const InputVertex& input, int num_attributes, DebugData<true> MergedShaderEngine::ProduceDebugInfo(const InputVertex& input, int num_attributes,
unsigned int entry_point) const { unsigned int entry_point) const {
ASSERT(setup != nullptr);
ASSERT(entry_point < 1024); ASSERT(entry_point < 1024);
UnitState state; UnitState state;
@ -148,10 +165,15 @@ DebugData<true> ShaderSetup::ProduceDebugInfo(const InputVertex& input, int num_
// Setup input register table // Setup input register table
boost::fill(state.registers.input, Math::Vec4<float24>::AssignToAll(float24::Zero())); boost::fill(state.registers.input, Math::Vec4<float24>::AssignToAll(float24::Zero()));
state.LoadInputVertex(input, num_attributes); state.LoadInputVertex(input, num_attributes);
RunInterpreter(*this, state, debug_data, entry_point); RunInterpreter(*setup, state, debug_data, entry_point);
return debug_data; return debug_data;
} }
ShaderEngine* GetEngine() {
static MergedShaderEngine merged_engine;
return &merged_engine;
}
} // namespace Shader } // namespace Shader
} // namespace Pica } // namespace Pica

View file

@ -156,7 +156,6 @@ struct UnitState {
void ClearCache(); void ClearCache();
struct ShaderSetup { struct ShaderSetup {
struct { struct {
// The float uniforms are accessed by the shader JIT using SSE instructions, and are // The float uniforms are accessed by the shader JIT using SSE instructions, and are
// therefore required to be 16-byte aligned. // therefore required to be 16-byte aligned.
@ -180,18 +179,23 @@ struct ShaderSetup {
std::array<u32, 1024> program_code; std::array<u32, 1024> program_code;
std::array<u32, 1024> swizzle_data; std::array<u32, 1024> swizzle_data;
};
class ShaderEngine {
public:
virtual ~ShaderEngine() = default;
/** /**
* Performs any shader unit setup that only needs to happen once per shader (as opposed to once * Performs any shader unit setup that only needs to happen once per shader (as opposed to once
* per vertex, which would happen within the `Run` function). * per vertex, which would happen within the `Run` function).
*/ */
void Setup(); virtual void SetupBatch(const ShaderSetup* setup) = 0;
/** /**
* Runs the currently setup shader * Runs the currently setup shader
* @param state Shader unit state, must be setup per shader and per shader unit * @param state Shader unit state, must be setup per shader and per shader unit
*/ */
void Run(UnitState& state, unsigned int entry_point) const; virtual void Run(UnitState& state, unsigned int entry_point) const = 0;
/** /**
* Produce debug information based on the given shader and input vertex * Produce debug information based on the given shader and input vertex
@ -200,10 +204,13 @@ struct ShaderSetup {
* @param config Configuration object for the shader pipeline * @param config Configuration object for the shader pipeline
* @return Debug information for this shader with regards to the given vertex * @return Debug information for this shader with regards to the given vertex
*/ */
DebugData<true> ProduceDebugInfo(const InputVertex& input, int num_attributes, virtual DebugData<true> ProduceDebugInfo(const InputVertex& input, int num_attributes,
unsigned int entry_point) const; unsigned int entry_point) const = 0;
}; };
// TODO(yuriks): Remove and make it non-global state somewhere
ShaderEngine* GetEngine();
} // namespace Shader } // namespace Shader
} // namespace Pica } // namespace Pica

View file

@ -8,6 +8,7 @@ namespace Pica {
namespace Shader { namespace Shader {
struct ShaderSetup;
struct UnitState; struct UnitState;
template <bool Debug> template <bool Debug>