summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend/spirv/emit_spirv_warp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/backend/spirv/emit_spirv_warp.cpp')
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_warp.cpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_warp.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_warp.cpp
new file mode 100644
index 000000000..44d8a347f
--- /dev/null
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_warp.cpp
@@ -0,0 +1,135 @@
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 "shader_recompiler/backend/spirv/emit_spirv.h"
6
7namespace Shader::Backend::SPIRV {
8namespace {
9Id LargeWarpBallot(EmitContext& ctx, Id ballot) {
10 const Id shift{ctx.Constant(ctx.U32[1], 5)};
11 const Id local_index{ctx.OpLoad(ctx.U32[1], ctx.subgroup_local_invocation_id)};
12 return ctx.OpVectorExtractDynamic(ctx.U32[1], ballot, local_index);
13}
14
15void SetInBoundsFlag(IR::Inst* inst, Id result) {
16 IR::Inst* const in_bounds{inst->GetAssociatedPseudoOperation(IR::Opcode::GetInBoundsFromOp)};
17 if (!in_bounds) {
18 return;
19 }
20 in_bounds->SetDefinition(result);
21 in_bounds->Invalidate();
22}
23
24Id ComputeMinThreadId(EmitContext& ctx, Id thread_id, Id segmentation_mask) {
25 return ctx.OpBitwiseAnd(ctx.U32[1], thread_id, segmentation_mask);
26}
27
28Id ComputeMaxThreadId(EmitContext& ctx, Id min_thread_id, Id clamp, Id not_seg_mask) {
29 return ctx.OpBitwiseOr(ctx.U32[1], min_thread_id,
30 ctx.OpBitwiseAnd(ctx.U32[1], clamp, not_seg_mask));
31}
32
33Id GetMaxThreadId(EmitContext& ctx, Id thread_id, Id clamp, Id segmentation_mask) {
34 const Id not_seg_mask{ctx.OpNot(ctx.U32[1], segmentation_mask)};
35 const Id min_thread_id{ComputeMinThreadId(ctx, thread_id, segmentation_mask)};
36 return ComputeMaxThreadId(ctx, min_thread_id, clamp, not_seg_mask);
37}
38
39Id SelectValue(EmitContext& ctx, Id in_range, Id value, Id src_thread_id) {
40 return ctx.OpSelect(ctx.U32[1], in_range,
41 ctx.OpSubgroupReadInvocationKHR(ctx.U32[1], value, src_thread_id), value);
42}
43} // Anonymous namespace
44
45Id EmitVoteAll(EmitContext& ctx, Id pred) {
46 if (!ctx.profile.warp_size_potentially_larger_than_guest) {
47 return ctx.OpSubgroupAllKHR(ctx.U1, pred);
48 }
49 const Id mask_ballot{ctx.OpSubgroupBallotKHR(ctx.U32[4], ctx.true_value)};
50 const Id active_mask{LargeWarpBallot(ctx, mask_ballot)};
51 const Id ballot{LargeWarpBallot(ctx, ctx.OpSubgroupBallotKHR(ctx.U32[4], pred))};
52 const Id lhs{ctx.OpBitwiseAnd(ctx.U32[1], ballot, active_mask)};
53 return ctx.OpIEqual(ctx.U1, lhs, active_mask);
54}
55
56Id EmitVoteAny(EmitContext& ctx, Id pred) {
57 if (!ctx.profile.warp_size_potentially_larger_than_guest) {
58 return ctx.OpSubgroupAnyKHR(ctx.U1, pred);
59 }
60 const Id mask_ballot{ctx.OpSubgroupBallotKHR(ctx.U32[4], ctx.true_value)};
61 const Id active_mask{LargeWarpBallot(ctx, mask_ballot)};
62 const Id ballot{LargeWarpBallot(ctx, ctx.OpSubgroupBallotKHR(ctx.U32[4], pred))};
63 const Id lhs{ctx.OpBitwiseAnd(ctx.U32[1], ballot, active_mask)};
64 return ctx.OpINotEqual(ctx.U1, lhs, ctx.u32_zero_value);
65}
66
67Id EmitVoteEqual(EmitContext& ctx, Id pred) {
68 if (!ctx.profile.warp_size_potentially_larger_than_guest) {
69 return ctx.OpSubgroupAllEqualKHR(ctx.U1, pred);
70 }
71 const Id mask_ballot{ctx.OpSubgroupBallotKHR(ctx.U32[4], ctx.true_value)};
72 const Id active_mask{LargeWarpBallot(ctx, mask_ballot)};
73 const Id ballot{LargeWarpBallot(ctx, ctx.OpSubgroupBallotKHR(ctx.U32[4], pred))};
74 const Id lhs{ctx.OpBitwiseXor(ctx.U32[1], ballot, active_mask)};
75 return ctx.OpLogicalOr(ctx.U1, ctx.OpIEqual(ctx.U1, lhs, ctx.u32_zero_value),
76 ctx.OpIEqual(ctx.U1, lhs, active_mask));
77}
78
79Id EmitSubgroupBallot(EmitContext& ctx, Id pred) {
80 const Id ballot{ctx.OpSubgroupBallotKHR(ctx.U32[4], pred)};
81 if (!ctx.profile.warp_size_potentially_larger_than_guest) {
82 return ctx.OpCompositeExtract(ctx.U32[1], ballot, 0U);
83 }
84 return LargeWarpBallot(ctx, ballot);
85}
86
87Id EmitShuffleIndex(EmitContext& ctx, IR::Inst* inst, Id value, Id index, Id clamp,
88 Id segmentation_mask) {
89 const Id not_seg_mask{ctx.OpNot(ctx.U32[1], segmentation_mask)};
90 const Id thread_id{ctx.OpLoad(ctx.U32[1], ctx.subgroup_local_invocation_id)};
91 const Id min_thread_id{ComputeMinThreadId(ctx, thread_id, segmentation_mask)};
92 const Id max_thread_id{ComputeMaxThreadId(ctx, min_thread_id, clamp, not_seg_mask)};
93
94 const Id lhs{ctx.OpBitwiseAnd(ctx.U32[1], index, not_seg_mask)};
95 const Id src_thread_id{ctx.OpBitwiseOr(ctx.U32[1], lhs, min_thread_id)};
96 const Id in_range{ctx.OpSLessThanEqual(ctx.U1, src_thread_id, max_thread_id)};
97
98 SetInBoundsFlag(inst, in_range);
99 return SelectValue(ctx, in_range, value, src_thread_id);
100}
101
102Id EmitShuffleUp(EmitContext& ctx, IR::Inst* inst, Id value, Id index, Id clamp,
103 Id segmentation_mask) {
104 const Id thread_id{ctx.OpLoad(ctx.U32[1], ctx.subgroup_local_invocation_id)};
105 const Id max_thread_id{GetMaxThreadId(ctx, thread_id, clamp, segmentation_mask)};
106 const Id src_thread_id{ctx.OpISub(ctx.U32[1], thread_id, index)};
107 const Id in_range{ctx.OpSGreaterThanEqual(ctx.U1, src_thread_id, max_thread_id)};
108
109 SetInBoundsFlag(inst, in_range);
110 return SelectValue(ctx, in_range, value, src_thread_id);
111}
112
113Id EmitShuffleDown(EmitContext& ctx, IR::Inst* inst, Id value, Id index, Id clamp,
114 Id segmentation_mask) {
115 const Id thread_id{ctx.OpLoad(ctx.U32[1], ctx.subgroup_local_invocation_id)};
116 const Id max_thread_id{GetMaxThreadId(ctx, thread_id, clamp, segmentation_mask)};
117 const Id src_thread_id{ctx.OpIAdd(ctx.U32[1], thread_id, index)};
118 const Id in_range{ctx.OpSLessThanEqual(ctx.U1, src_thread_id, max_thread_id)};
119
120 SetInBoundsFlag(inst, in_range);
121 return SelectValue(ctx, in_range, value, src_thread_id);
122}
123
124Id EmitShuffleButterfly(EmitContext& ctx, IR::Inst* inst, Id value, Id index, Id clamp,
125 Id segmentation_mask) {
126 const Id thread_id{ctx.OpLoad(ctx.U32[1], ctx.subgroup_local_invocation_id)};
127 const Id max_thread_id{GetMaxThreadId(ctx, thread_id, clamp, segmentation_mask)};
128 const Id src_thread_id{ctx.OpBitwiseXor(ctx.U32[1], thread_id, index)};
129 const Id in_range{ctx.OpSLessThanEqual(ctx.U1, src_thread_id, max_thread_id)};
130
131 SetInBoundsFlag(inst, in_range);
132 return SelectValue(ctx, in_range, value, src_thread_id);
133}
134
135} // namespace Shader::Backend::SPIRV