shader: Address feedback

This commit is contained in:
FernandoS27 2021-05-01 14:56:25 +02:00 committed by ameerj
parent b541f5e5e3
commit c49d56c931
5 changed files with 42 additions and 44 deletions

View file

@ -209,9 +209,9 @@ CFG::CFG(Environment& env_, ObjectPool<Block>& block_pool_, Location start_addre
} }
} }
if (exits_to_dispatcher) { if (exits_to_dispatcher) {
const auto it = functions[0].blocks.rbegin(); const auto last_block{functions[0].blocks.rbegin()};
dispatch_block->begin = it->end + 1; dispatch_block->begin = last_block->end + 1;
dispatch_block->end = it->end + 1; dispatch_block->end = last_block->end + 1;
functions[0].blocks.insert(*dispatch_block); functions[0].blocks.insert(*dispatch_block);
} }
} }
@ -481,7 +481,7 @@ CFG::AnalysisState CFG::AnalyzeEXIT(Block* block, FunctionId function_id, Locati
return AnalysisState::Continue; return AnalysisState::Continue;
} }
if (exits_to_dispatcher && function_id != 0) { if (exits_to_dispatcher && function_id != 0) {
throw NotImplementedException("Dispatch EXIT on external function."); throw NotImplementedException("Dispatch EXIT on external function");
} }
if (pred != Predicate{true} || flow_test != IR::FlowTest::T) { if (pred != Predicate{true} || flow_test != IR::FlowTest::T) {
if (block->stack.Peek(Token::PEXIT).has_value()) { if (block->stack.Peek(Token::PEXIT).has_value()) {
@ -490,9 +490,9 @@ CFG::AnalysisState CFG::AnalyzeEXIT(Block* block, FunctionId function_id, Locati
const IR::Condition cond{flow_test, static_cast<IR::Pred>(pred.index), pred.negated}; const IR::Condition cond{flow_test, static_cast<IR::Pred>(pred.index), pred.negated};
if (exits_to_dispatcher) { if (exits_to_dispatcher) {
block->end = pc; block->end = pc;
block->branch_true = dispatch_block;
block->end_class = EndClass::Branch; block->end_class = EndClass::Branch;
block->cond = cond; block->cond = cond;
block->branch_true = dispatch_block;
block->branch_false = AddLabel(block, block->stack, pc + 1, function_id); block->branch_false = AddLabel(block, block->stack, pc + 1, function_id);
return AnalysisState::Branch; return AnalysisState::Branch;
} }

View file

@ -151,31 +151,30 @@ IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Blo
} }
IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b, IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b,
Environment& env2) { Environment& env_vertex_b) {
IR::Program program{}; IR::Program result{};
Optimization::VertexATransformPass(vertex_a); Optimization::VertexATransformPass(vertex_a);
Optimization::VertexBTransformPass(vertex_b); Optimization::VertexBTransformPass(vertex_b);
program.blocks.swap(vertex_a.blocks); std::swap(result.blocks, vertex_a.blocks);
for (IR::Block* block : vertex_b.blocks) { for (IR::Block* block : vertex_b.blocks) {
program.blocks.push_back(block); result.blocks.push_back(block);
} }
program.stage = Stage::VertexB; result.stage = Stage::VertexB;
program.info = vertex_a.info; result.info = vertex_a.info;
program.local_memory_size = std::max(vertex_a.local_memory_size, vertex_b.local_memory_size); result.local_memory_size = std::max(vertex_a.local_memory_size, vertex_b.local_memory_size);
for (size_t index = 0; index < 32; index++) { for (size_t index = 0; index < 32; ++index) {
program.info.input_generics[index].used |= vertex_b.info.input_generics[index].used; result.info.input_generics[index].used |= vertex_b.info.input_generics[index].used;
program.info.stores_generics[index] |= vertex_b.info.stores_generics[index]; result.info.stores_generics[index] |= vertex_b.info.stores_generics[index];
} }
Optimization::JoinTextureInfo(program.info, vertex_b.info); Optimization::JoinTextureInfo(result.info, vertex_b.info);
Optimization::JoinStorageInfo(program.info, vertex_b.info); Optimization::JoinStorageInfo(result.info, vertex_b.info);
Optimization::DualVertexJoinPass(program); Optimization::DualVertexJoinPass(result);
program.post_order_blocks = PostOrder(program.blocks); result.post_order_blocks = PostOrder(result.blocks);
Optimization::DeadCodeEliminationPass(program); Optimization::DeadCodeEliminationPass(result);
Optimization::IdentityRemovalPass(program); Optimization::VerificationPass(result);
Optimization::VerificationPass(program); Optimization::CollectShaderInfoPass(env_vertex_b, result);
Optimization::CollectShaderInfoPass(env2, program); return result;
return program;
} }
} // namespace Shader::Maxwell } // namespace Shader::Maxwell

View file

@ -23,4 +23,5 @@ namespace Shader::Maxwell {
[[nodiscard]] IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b, [[nodiscard]] IR::Program MergeDualVertexPrograms(IR::Program& vertex_a, IR::Program& vertex_b,
Environment& env_vertex_b); Environment& env_vertex_b);
} // namespace Shader::Maxwell } // namespace Shader::Maxwell

View file

@ -4,8 +4,6 @@
#include <algorithm> #include <algorithm>
#include <ranges> #include <ranges>
#include <tuple>
#include <type_traits>
#include "common/bit_cast.h" #include "common/bit_cast.h"
#include "common/bit_util.h" #include "common/bit_util.h"
@ -40,7 +38,7 @@ void VertexATransformPass(IR::Program& program) {
} }
void VertexBTransformPass(IR::Program& program) { void VertexBTransformPass(IR::Program& program) {
for (IR::Block* const block : program.post_order_blocks | std::views::reverse) { for (IR::Block* const block : program.blocks) {
for (IR::Inst& inst : block->Instructions()) { for (IR::Inst& inst : block->Instructions()) {
if (inst.GetOpcode() == IR::Opcode::Prologue) { if (inst.GetOpcode() == IR::Opcode::Prologue) {
return inst.Invalidate(); return inst.Invalidate();
@ -51,24 +49,24 @@ void VertexBTransformPass(IR::Program& program) {
void DualVertexJoinPass(IR::Program& program) { void DualVertexJoinPass(IR::Program& program) {
const auto& blocks = program.blocks; const auto& blocks = program.blocks;
s64 s = static_cast<s64>(blocks.size()) - 1; const s64 sub_size = static_cast<s64>(blocks.size()) - 1;
if (s < 1) { if (sub_size < 1) {
throw NotImplementedException("Dual Vertex Join pass failed, expected atleast 2 blocks!"); throw LogicError("Dual Vertex Join pass failed, expected atleast 2 blocks");
} }
for (s64 index = 0; index < s; index++) { for (s64 index = 0; index < sub_size; ++index) {
IR::Block* const current_block = blocks[index]; IR::Block* const current_block{blocks[index]};
IR::Block* const next_block = blocks[index + 1]; IR::Block* const next_block{blocks[index + 1]};
for (IR::Inst& inst : current_block->Instructions()) { for (IR::Inst& inst : current_block->Instructions()) {
if (inst.GetOpcode() == IR::Opcode::Join) { if (inst.GetOpcode() == IR::Opcode::Join) {
IR::IREmitter ir{*current_block, IR::Block::InstructionList::s_iterator_to(inst)}; IR::IREmitter ir{*current_block, IR::Block::InstructionList::s_iterator_to(inst)};
ir.Branch(next_block); ir.Branch(next_block);
inst.Invalidate(); inst.Invalidate();
// only 1 join should exist // Only 1 join should exist
return; return;
} }
} }
} }
throw NotImplementedException("Dual Vertex Join pass failed, no join present!"); throw LogicError("Dual Vertex Join pass failed, no join present");
} }
} // namespace Shader::Optimization } // namespace Shader::Optimization

View file

@ -288,32 +288,32 @@ std::unique_ptr<GraphicsPipeline> PipelineCache::CreateGraphicsPipeline(
LOG_INFO(Render_Vulkan, "0x{:016x}", key.Hash()); LOG_INFO(Render_Vulkan, "0x{:016x}", key.Hash());
size_t env_index{0}; size_t env_index{0};
std::array<Shader::IR::Program, Maxwell::MaxShaderProgram> programs; std::array<Shader::IR::Program, Maxwell::MaxShaderProgram> programs;
bool uses_vertex_a{}; const bool uses_vertex_a{key.unique_hashes[0] != 0};
std::size_t start_value_processing{}; const bool uses_vertex_b{key.unique_hashes[1] != 0};
for (size_t index = 0; index < Maxwell::MaxShaderProgram; ++index) { for (size_t index = 0; index < Maxwell::MaxShaderProgram; ++index) {
if (key.unique_hashes[index] == 0) { if (key.unique_hashes[index] == 0) {
continue; continue;
} }
uses_vertex_a |= index == 0;
Shader::Environment& env{*envs[env_index]}; Shader::Environment& env{*envs[env_index]};
++env_index; ++env_index;
const u32 cfg_offset{static_cast<u32>(env.StartAddress() + sizeof(Shader::ProgramHeader))}; const u32 cfg_offset{static_cast<u32>(env.StartAddress() + sizeof(Shader::ProgramHeader))};
Shader::Maxwell::Flow::CFG cfg(env, pools.flow_block, cfg_offset, index == 0); Shader::Maxwell::Flow::CFG cfg(env, pools.flow_block, cfg_offset, index == 0);
if (!uses_vertex_a || index != 1) { if (!uses_vertex_a || index != 1) {
// Normal path
programs[index] = TranslateProgram(pools.inst, pools.block, env, cfg); programs[index] = TranslateProgram(pools.inst, pools.block, env, cfg);
continue; } else {
} // VertexB path when VertexA is present.
Shader::IR::Program& program_va{programs[0]}; Shader::IR::Program& program_va{programs[0]};
Shader::IR::Program program_vb{TranslateProgram(pools.inst, pools.block, env, cfg)}; Shader::IR::Program program_vb{TranslateProgram(pools.inst, pools.block, env, cfg)};
programs[index] = MergeDualVertexPrograms(program_va, program_vb, env); programs[index] = MergeDualVertexPrograms(program_va, program_vb, env);
start_value_processing = 1; }
} }
std::array<const Shader::Info*, Maxwell::MaxShaderStage> infos{}; std::array<const Shader::Info*, Maxwell::MaxShaderStage> infos{};
std::array<vk::ShaderModule, Maxwell::MaxShaderStage> modules; std::array<vk::ShaderModule, Maxwell::MaxShaderStage> modules;
u32 binding{0}; u32 binding{0};
for (size_t index = start_value_processing; index < Maxwell::MaxShaderProgram; ++index) { for (size_t index = uses_vertex_a && uses_vertex_b ? 1 : 0; index < Maxwell::MaxShaderProgram; ++index) {
if (key.unique_hashes[index] == 0) { if (key.unique_hashes[index] == 0) {
continue; continue;
} }