summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/ir_opt
diff options
context:
space:
mode:
authorGravatar FernandoS272021-04-19 01:03:38 +0200
committerGravatar ameerj2021-07-22 21:51:29 -0400
commitb541f5e5e333a8ec8c3569e02d67e59ad14217c2 (patch)
tree5b19bdd92d74ce8e32be75cfa2c27a4612a663de /src/shader_recompiler/ir_opt
parentshader: Implement delegation of Exit to dispatcher on CFG (diff)
downloadyuzu-b541f5e5e333a8ec8c3569e02d67e59ad14217c2.tar.gz
yuzu-b541f5e5e333a8ec8c3569e02d67e59ad14217c2.tar.xz
yuzu-b541f5e5e333a8ec8c3569e02d67e59ad14217c2.zip
shader: Implement VertexA stage
Diffstat (limited to 'src/shader_recompiler/ir_opt')
-rw-r--r--src/shader_recompiler/ir_opt/dual_vertex_pass.cpp74
-rw-r--r--src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp26
-rw-r--r--src/shader_recompiler/ir_opt/passes.h7
-rw-r--r--src/shader_recompiler/ir_opt/texture_pass.cpp21
4 files changed, 128 insertions, 0 deletions
diff --git a/src/shader_recompiler/ir_opt/dual_vertex_pass.cpp b/src/shader_recompiler/ir_opt/dual_vertex_pass.cpp
new file mode 100644
index 000000000..f35c6478a
--- /dev/null
+++ b/src/shader_recompiler/ir_opt/dual_vertex_pass.cpp
@@ -0,0 +1,74 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <ranges>
7#include <tuple>
8#include <type_traits>
9
10#include "common/bit_cast.h"
11#include "common/bit_util.h"
12#include "shader_recompiler/exception.h"
13#include "shader_recompiler/frontend/ir/ir_emitter.h"
14#include "shader_recompiler/ir_opt/passes.h"
15
16namespace Shader::Optimization {
17
18void VertexATransformPass(IR::Program& program) {
19 bool replaced_join{};
20 bool eliminated_epilogue{};
21 for (IR::Block* const block : program.post_order_blocks) {
22 for (IR::Inst& inst : block->Instructions()) {
23 switch (inst.GetOpcode()) {
24 case IR::Opcode::Return:
25 inst.ReplaceOpcode(IR::Opcode::Join);
26 replaced_join = true;
27 break;
28 case IR::Opcode::Epilogue:
29 inst.Invalidate();
30 eliminated_epilogue = true;
31 break;
32 default:
33 break;
34 }
35 if (replaced_join && eliminated_epilogue) {
36 return;
37 }
38 }
39 }
40}
41
42void VertexBTransformPass(IR::Program& program) {
43 for (IR::Block* const block : program.post_order_blocks | std::views::reverse) {
44 for (IR::Inst& inst : block->Instructions()) {
45 if (inst.GetOpcode() == IR::Opcode::Prologue) {
46 return inst.Invalidate();
47 }
48 }
49 }
50}
51
52void DualVertexJoinPass(IR::Program& program) {
53 const auto& blocks = program.blocks;
54 s64 s = static_cast<s64>(blocks.size()) - 1;
55 if (s < 1) {
56 throw NotImplementedException("Dual Vertex Join pass failed, expected atleast 2 blocks!");
57 }
58 for (s64 index = 0; index < s; index++) {
59 IR::Block* const current_block = blocks[index];
60 IR::Block* const next_block = blocks[index + 1];
61 for (IR::Inst& inst : current_block->Instructions()) {
62 if (inst.GetOpcode() == IR::Opcode::Join) {
63 IR::IREmitter ir{*current_block, IR::Block::InstructionList::s_iterator_to(inst)};
64 ir.Branch(next_block);
65 inst.Invalidate();
66 // only 1 join should exist
67 return;
68 }
69 }
70 }
71 throw NotImplementedException("Dual Vertex Join pass failed, no join present!");
72}
73
74} // namespace Shader::Optimization
diff --git a/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp b/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp
index 87eca2a0d..1d11a00d8 100644
--- a/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp
+++ b/src/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp
@@ -499,4 +499,30 @@ void GlobalMemoryToStorageBufferPass(IR::Program& program) {
499 } 499 }
500} 500}
501 501
502template <typename Descriptors, typename Descriptor, typename Func>
503static u32 Add(Descriptors& descriptors, const Descriptor& desc, Func&& pred) {
504 // TODO: Handle arrays
505 const auto it{std::ranges::find_if(descriptors, pred)};
506 if (it != descriptors.end()) {
507 return static_cast<u32>(std::distance(descriptors.begin(), it));
508 }
509 descriptors.push_back(desc);
510 return static_cast<u32>(descriptors.size()) - 1;
511}
512
513void JoinStorageInfo(Info& base, Info& source) {
514 auto& descriptors = base.storage_buffers_descriptors;
515 for (auto& desc : source.storage_buffers_descriptors) {
516 auto it{std::ranges::find_if(descriptors, [&desc](const auto& existing) {
517 return desc.cbuf_index == existing.cbuf_index &&
518 desc.cbuf_offset == existing.cbuf_offset && desc.count == existing.count;
519 })};
520 if (it != descriptors.end()) {
521 it->is_written |= desc.is_written;
522 continue;
523 }
524 descriptors.push_back(desc);
525 }
526}
527
502} // namespace Shader::Optimization 528} // namespace Shader::Optimization
diff --git a/src/shader_recompiler/ir_opt/passes.h b/src/shader_recompiler/ir_opt/passes.h
index 186104713..e9cb8546a 100644
--- a/src/shader_recompiler/ir_opt/passes.h
+++ b/src/shader_recompiler/ir_opt/passes.h
@@ -22,4 +22,11 @@ void SsaRewritePass(IR::Program& program);
22void TexturePass(Environment& env, IR::Program& program); 22void TexturePass(Environment& env, IR::Program& program);
23void VerificationPass(const IR::Program& program); 23void VerificationPass(const IR::Program& program);
24 24
25// Dual Vertex
26void VertexATransformPass(IR::Program& program);
27void VertexBTransformPass(IR::Program& program);
28void DualVertexJoinPass(IR::Program& program);
29void JoinTextureInfo(Info& base, Info& source);
30void JoinStorageInfo(Info& base, Info& source);
31
25} // namespace Shader::Optimization 32} // namespace Shader::Optimization
diff --git a/src/shader_recompiler/ir_opt/texture_pass.cpp b/src/shader_recompiler/ir_opt/texture_pass.cpp
index cfa6b34b9..2b38bcf42 100644
--- a/src/shader_recompiler/ir_opt/texture_pass.cpp
+++ b/src/shader_recompiler/ir_opt/texture_pass.cpp
@@ -426,4 +426,25 @@ void TexturePass(Environment& env, IR::Program& program) {
426 } 426 }
427} 427}
428 428
429void JoinTextureInfo(Info& base, Info& source) {
430 Descriptors descriptors{
431 base.texture_buffer_descriptors,
432 base.image_buffer_descriptors,
433 base.texture_descriptors,
434 base.image_descriptors,
435 };
436 for (auto& desc : source.texture_buffer_descriptors) {
437 descriptors.Add(desc);
438 }
439 for (auto& desc : source.image_buffer_descriptors) {
440 descriptors.Add(desc);
441 }
442 for (auto& desc : source.texture_descriptors) {
443 descriptors.Add(desc);
444 }
445 for (auto& desc : source.image_descriptors) {
446 descriptors.Add(desc);
447 }
448}
449
429} // namespace Shader::Optimization 450} // namespace Shader::Optimization