VideoCore/Shader: Move per-batch ShaderEngine state into ShaderSetup
This commit is contained in:
		| @@ -521,8 +521,9 @@ void GraphicsVertexShaderWidget::Reload(bool replace_vertex_data, void* vertex_d | |||||||
|  |  | ||||||
|     // Generate debug information |     // Generate debug information | ||||||
|     Pica::Shader::InterpreterEngine shader_engine; |     Pica::Shader::InterpreterEngine shader_engine; | ||||||
|     shader_engine.SetupBatch(&shader_setup); |     shader_engine.SetupBatch(shader_setup); | ||||||
|     debug_data = shader_engine.ProduceDebugInfo(input_vertex, num_attributes, entry_point); |     debug_data = | ||||||
|  |         shader_engine.ProduceDebugInfo(shader_setup, 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) { | ||||||
|   | |||||||
| @@ -143,7 +143,7 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) { | |||||||
|                     immediate_attribute_id = 0; |                     immediate_attribute_id = 0; | ||||||
|  |  | ||||||
|                     auto* shader_engine = Shader::GetEngine(); |                     auto* shader_engine = Shader::GetEngine(); | ||||||
|                     shader_engine->SetupBatch(&g_state.vs); |                     shader_engine->SetupBatch(g_state.vs); | ||||||
|  |  | ||||||
|                     // Send to vertex shader |                     // Send to vertex shader | ||||||
|                     if (g_debug_context) |                     if (g_debug_context) | ||||||
| @@ -151,7 +151,7 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) { | |||||||
|                                                  static_cast<void*>(&immediate_input)); |                                                  static_cast<void*>(&immediate_input)); | ||||||
|                     Shader::UnitState shader_unit; |                     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); | ||||||
|                     shader_engine->Run(shader_unit, regs.vs.main_offset); |                     shader_engine->Run(g_state.vs, shader_unit, regs.vs.main_offset); | ||||||
|                     auto output_vertex = Shader::OutputVertex::FromRegisters( |                     auto output_vertex = Shader::OutputVertex::FromRegisters( | ||||||
|                         shader_unit.registers.output, regs, regs.vs.output_mask); |                         shader_unit.registers.output, regs, regs.vs.output_mask); | ||||||
|  |  | ||||||
| @@ -248,7 +248,7 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) { | |||||||
|         auto* shader_engine = Shader::GetEngine(); |         auto* shader_engine = Shader::GetEngine(); | ||||||
|         Shader::UnitState shader_unit; |         Shader::UnitState shader_unit; | ||||||
|  |  | ||||||
|         shader_engine->SetupBatch(&g_state.vs); |         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 | ||||||
| @@ -288,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()); | ||||||
|                 shader_engine->Run(shader_unit, regs.vs.main_offset); |                 shader_engine->Run(g_state.vs, shader_unit, regs.vs.main_offset); | ||||||
|  |  | ||||||
|                 // Retrieve vertex from register data |                 // Retrieve vertex from register data | ||||||
|                 output_vertex = Shader::OutputVertex::FromRegisters(shader_unit.registers.output, |                 output_vertex = Shader::OutputVertex::FromRegisters(shader_unit.registers.output, | ||||||
|   | |||||||
| @@ -167,6 +167,12 @@ 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; | ||||||
|  |  | ||||||
|  |     /// Data private to ShaderEngines | ||||||
|  |     struct EngineData { | ||||||
|  |         /// Used by the JIT, points to a compiled shader object. | ||||||
|  |         const void* cached_shader = nullptr; | ||||||
|  |     } engine_data; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| class ShaderEngine { | class ShaderEngine { | ||||||
| @@ -177,13 +183,16 @@ public: | |||||||
|      * 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). | ||||||
|      */ |      */ | ||||||
|     virtual void SetupBatch(const ShaderSetup* setup) = 0; |     virtual void SetupBatch(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 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. | ||||||
|      */ |      */ | ||||||
|     virtual void Run(UnitState& state, unsigned int entry_point) const = 0; |     virtual void Run(const ShaderSetup& setup, UnitState& state, | ||||||
|  |                      unsigned int entry_point) const = 0; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| // TODO(yuriks): Remove and make it non-global state somewhere | // TODO(yuriks): Remove and make it non-global state somewhere | ||||||
|   | |||||||
| @@ -652,25 +652,23 @@ static void RunInterpreter(const ShaderSetup& setup, UnitState& state, DebugData | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| void InterpreterEngine::SetupBatch(const ShaderSetup* setup_) { | void InterpreterEngine::SetupBatch(ShaderSetup& setup) {} | ||||||
|     setup = setup_; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| MICROPROFILE_DECLARE(GPU_Shader); | MICROPROFILE_DECLARE(GPU_Shader); | ||||||
|  |  | ||||||
| void InterpreterEngine::Run(UnitState& state, unsigned int entry_point) const { | void InterpreterEngine::Run(const ShaderSetup& setup, UnitState& state, | ||||||
|     ASSERT(setup != nullptr); |                             unsigned int entry_point) const { | ||||||
|     ASSERT(entry_point < 1024); |     ASSERT(entry_point < 1024); | ||||||
|  |  | ||||||
|     MICROPROFILE_SCOPE(GPU_Shader); |     MICROPROFILE_SCOPE(GPU_Shader); | ||||||
|  |  | ||||||
|     DebugData<false> dummy_debug_data; |     DebugData<false> dummy_debug_data; | ||||||
|     RunInterpreter(*setup, state, dummy_debug_data, entry_point); |     RunInterpreter(setup, state, dummy_debug_data, entry_point); | ||||||
| } | } | ||||||
|  |  | ||||||
| DebugData<true> InterpreterEngine::ProduceDebugInfo(const InputVertex& input, int num_attributes, | DebugData<true> InterpreterEngine::ProduceDebugInfo(const ShaderSetup& setup, | ||||||
|  |                                                     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; | ||||||
| @@ -679,7 +677,7 @@ DebugData<true> InterpreterEngine::ProduceDebugInfo(const InputVertex& input, in | |||||||
|     // 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(*setup, state, debug_data, entry_point); |     RunInterpreter(setup, state, debug_data, entry_point); | ||||||
|     return debug_data; |     return debug_data; | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -13,8 +13,8 @@ namespace Shader { | |||||||
|  |  | ||||||
| class InterpreterEngine final : public ShaderEngine { | class InterpreterEngine final : public ShaderEngine { | ||||||
| public: | public: | ||||||
|     void SetupBatch(const ShaderSetup* setup) override; |     void SetupBatch(ShaderSetup& setup) override; | ||||||
|     void Run(UnitState& state, unsigned int entry_point) const override; |     void Run(const ShaderSetup& setup, UnitState& state, unsigned int entry_point) const override; | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Produce debug information based on the given shader and input vertex |      * Produce debug information based on the given shader and input vertex | ||||||
| @@ -23,11 +23,8 @@ public: | |||||||
|      * @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, |     DebugData<true> ProduceDebugInfo(const ShaderSetup& setup, const InputVertex& input, | ||||||
|                                      unsigned int entry_point) const; |                                      int num_attributes, unsigned int entry_point) const; | ||||||
|  |  | ||||||
| private: |  | ||||||
|     const ShaderSetup* setup = nullptr; |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| } // namespace | } // namespace | ||||||
|   | |||||||
| @@ -14,37 +14,32 @@ namespace Shader { | |||||||
| JitX64Engine::JitX64Engine() = default; | JitX64Engine::JitX64Engine() = default; | ||||||
| JitX64Engine::~JitX64Engine() = default; | JitX64Engine::~JitX64Engine() = default; | ||||||
|  |  | ||||||
| void JitX64Engine::SetupBatch(const ShaderSetup* setup_) { | void JitX64Engine::SetupBatch(ShaderSetup& setup) { | ||||||
|     cached_shader = nullptr; |     u64 code_hash = Common::ComputeHash64(&setup.program_code, sizeof(setup.program_code)); | ||||||
|     setup = setup_; |     u64 swizzle_hash = Common::ComputeHash64(&setup.swizzle_data, sizeof(setup.swizzle_data)); | ||||||
|     if (setup == nullptr) |  | ||||||
|         return; |  | ||||||
|  |  | ||||||
|     u64 code_hash = Common::ComputeHash64(&setup->program_code, sizeof(setup->program_code)); |  | ||||||
|     u64 swizzle_hash = Common::ComputeHash64(&setup->swizzle_data, sizeof(setup->swizzle_data)); |  | ||||||
|  |  | ||||||
|     u64 cache_key = code_hash ^ swizzle_hash; |     u64 cache_key = code_hash ^ swizzle_hash; | ||||||
|     auto iter = cache.find(cache_key); |     auto iter = cache.find(cache_key); | ||||||
|     if (iter != cache.end()) { |     if (iter != cache.end()) { | ||||||
|         cached_shader = iter->second.get(); |         setup.engine_data.cached_shader = iter->second.get(); | ||||||
|     } else { |     } else { | ||||||
|         auto shader = std::make_unique<JitShader>(); |         auto shader = std::make_unique<JitShader>(); | ||||||
|         shader->Compile(&setup->program_code, &setup->swizzle_data); |         shader->Compile(&setup.program_code, &setup.swizzle_data); | ||||||
|         cached_shader = shader.get(); |         setup.engine_data.cached_shader = shader.get(); | ||||||
|         cache.emplace_hint(iter, cache_key, std::move(shader)); |         cache.emplace_hint(iter, cache_key, std::move(shader)); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| MICROPROFILE_DECLARE(GPU_Shader); | MICROPROFILE_DECLARE(GPU_Shader); | ||||||
|  |  | ||||||
| void JitX64Engine::Run(UnitState& state, unsigned int entry_point) const { | void JitX64Engine::Run(const ShaderSetup& setup, UnitState& state, unsigned int entry_point) const { | ||||||
|     ASSERT(setup != nullptr); |     ASSERT(setup.engine_data.cached_shader != nullptr); | ||||||
|     ASSERT(cached_shader != nullptr); |  | ||||||
|     ASSERT(entry_point < 1024); |     ASSERT(entry_point < 1024); | ||||||
|  |  | ||||||
|     MICROPROFILE_SCOPE(GPU_Shader); |     MICROPROFILE_SCOPE(GPU_Shader); | ||||||
|  |  | ||||||
|     cached_shader->Run(*setup, state, entry_point); |     const JitShader* shader = static_cast<const JitShader*>(setup.engine_data.cached_shader); | ||||||
|  |     shader->Run(setup, state, entry_point); | ||||||
| } | } | ||||||
|  |  | ||||||
| } // namespace Shader | } // namespace Shader | ||||||
|   | |||||||
| @@ -19,14 +19,11 @@ public: | |||||||
|     JitX64Engine(); |     JitX64Engine(); | ||||||
|     ~JitX64Engine() override; |     ~JitX64Engine() override; | ||||||
|  |  | ||||||
|     void SetupBatch(const ShaderSetup* setup) override; |     void SetupBatch(ShaderSetup& setup) override; | ||||||
|     void Run(UnitState& state, unsigned int entry_point) const override; |     void Run(const ShaderSetup& setup, UnitState& state, unsigned int entry_point) const override; | ||||||
|  |  | ||||||
| private: | private: | ||||||
|     const ShaderSetup* setup = nullptr; |  | ||||||
|  |  | ||||||
|     std::unordered_map<u64, std::unique_ptr<JitShader>> cache; |     std::unordered_map<u64, std::unique_ptr<JitShader>> cache; | ||||||
|     const JitShader* cached_shader = nullptr; |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| } // namespace Shader | } // namespace Shader | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user