Skip to content

Commit 78f1d55

Browse files
committed
[SPIR-V] Use Builder createSelectionMerge directly
1 parent 64d2a80 commit 78f1d55

8 files changed

+182
-175
lines changed

src/xenia/gpu/spirv_builder.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
******************************************************************************
3+
* Xenia : Xbox 360 Emulator Research Project *
4+
******************************************************************************
5+
* Copyright 2023 Ben Vanik. All rights reserved. *
6+
* Released under the BSD license - see LICENSE in the root for more details. *
7+
******************************************************************************
8+
*/
9+
10+
#ifndef XENIA_GPU_SPIRV_BUILDER_H_
11+
#define XENIA_GPU_SPIRV_BUILDER_H_
12+
13+
#include "third_party/glslang/SPIRV/SpvBuilder.h"
14+
15+
namespace xe {
16+
namespace gpu {
17+
18+
// SpvBuilder with extra helpers.
19+
20+
class SpirvBuilder : public spv::Builder {
21+
public:
22+
SpirvBuilder(unsigned int spv_version, unsigned int user_number,
23+
spv::SpvBuildLogger* logger)
24+
: spv::Builder(spv_version, user_number, logger) {}
25+
26+
// Make public rather than protected.
27+
using spv::Builder::createSelectionMerge;
28+
};
29+
30+
} // namespace gpu
31+
} // namespace xe
32+
33+
#endif // XENIA_GPU_SPIRV_BUILDER_H_

src/xenia/gpu/spirv_shader_translator.cc

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ uint32_t SpirvShaderTranslator::GetModificationRegisterCount() const {
161161

162162
void SpirvShaderTranslator::StartTranslation() {
163163
// TODO(Triang3l): Logger.
164-
builder_ = std::make_unique<spv::Builder>(
164+
builder_ = std::make_unique<SpirvBuilder>(
165165
features_.spirv_version, (kSpirvMagicToolId << 16) | 1, nullptr);
166166

167167
builder_->addCapability(IsSpirvTessEvalShader() ? spv::CapabilityTessellation
@@ -591,8 +591,8 @@ void SpirvShaderTranslator::StartTranslation() {
591591
main_switch_header_ = builder_->getBuildPoint();
592592
main_switch_merge_ =
593593
new spv::Block(builder_->getUniqueId(), *function_main_);
594-
SpirvCreateSelectionMerge(main_switch_merge_->getId(),
595-
spv::SelectionControlDontFlattenMask);
594+
builder_->createSelectionMerge(main_switch_merge_,
595+
spv::SelectionControlDontFlattenMask);
596596
main_switch_op_ = std::make_unique<spv::Instruction>(spv::OpSwitch);
597597
main_switch_op_->addIdOperand(main_loop_pc_current);
598598
main_switch_op_->addIdOperand(main_switch_merge_->getId());
@@ -914,7 +914,7 @@ void SpirvShaderTranslator::ProcessLoopStartInstruction(
914914
spv::OpIEqual, type_bool_, loop_count_new, const_uint_0_);
915915
spv::Block& skip_block = builder_->makeNewBlock();
916916
spv::Block& body_block = builder_->makeNewBlock();
917-
SpirvCreateSelectionMerge(body_block.getId());
917+
builder_->createSelectionMerge(&body_block, spv::SelectionControlMaskNone);
918918
{
919919
std::unique_ptr<spv::Instruction> branch_conditional_op =
920920
std::make_unique<spv::Instruction>(spv::OpBranchConditional);
@@ -976,7 +976,7 @@ void SpirvShaderTranslator::ProcessLoopEndInstruction(
976976
spv::Block& body_block = *builder_->getBuildPoint();
977977
spv::Block& continue_block = builder_->makeNewBlock();
978978
spv::Block& break_block = builder_->makeNewBlock();
979-
SpirvCreateSelectionMerge(break_block.getId());
979+
builder_->createSelectionMerge(&break_block, spv::SelectionControlMaskNone);
980980
{
981981
std::unique_ptr<spv::Instruction> branch_conditional_op =
982982
std::make_unique<spv::Instruction>(spv::OpBranchConditional);
@@ -1293,8 +1293,8 @@ void SpirvShaderTranslator::StartVertexOrTessEvalShaderInMain() {
12931293
spv::Block& block_load_vertex_index_pre = *builder_->getBuildPoint();
12941294
spv::Block& block_load_vertex_index_start = builder_->makeNewBlock();
12951295
spv::Block& block_load_vertex_index_merge = builder_->makeNewBlock();
1296-
SpirvCreateSelectionMerge(block_load_vertex_index_merge.getId(),
1297-
spv::SelectionControlDontFlattenMask);
1296+
builder_->createSelectionMerge(&block_load_vertex_index_merge,
1297+
spv::SelectionControlDontFlattenMask);
12981298
builder_->createConditionalBranch(load_vertex_index,
12991299
&block_load_vertex_index_start,
13001300
&block_load_vertex_index_merge);
@@ -1389,8 +1389,8 @@ void SpirvShaderTranslator::StartVertexOrTessEvalShaderInMain() {
13891389
spv::Block& block_load_vertex_index_pre = *builder_->getBuildPoint();
13901390
spv::Block& block_load_vertex_index_start = builder_->makeNewBlock();
13911391
spv::Block& block_load_vertex_index_merge = builder_->makeNewBlock();
1392-
SpirvCreateSelectionMerge(block_load_vertex_index_merge.getId(),
1393-
spv::SelectionControlDontFlattenMask);
1392+
builder_->createSelectionMerge(&block_load_vertex_index_merge,
1393+
spv::SelectionControlDontFlattenMask);
13941394
builder_->createConditionalBranch(load_vertex_index,
13951395
&block_load_vertex_index_start,
13961396
&block_load_vertex_index_merge);
@@ -1992,8 +1992,8 @@ void SpirvShaderTranslator::StartFragmentShaderInMain() {
19921992
builder_->makeNewBlock();
19931993
main_fsi_early_depth_stencil_execute_quad_merge_ =
19941994
&builder_->makeNewBlock();
1995-
SpirvCreateSelectionMerge(
1996-
main_fsi_early_depth_stencil_execute_quad_merge_->getId(),
1995+
builder_->createSelectionMerge(
1996+
main_fsi_early_depth_stencil_execute_quad_merge_,
19971997
spv::SelectionControlDontFlattenMask);
19981998
builder_->createConditionalBranch(
19991999
quad_needs_execution, &main_fsi_early_depth_stencil_execute_quad,
@@ -2244,7 +2244,8 @@ void SpirvShaderTranslator::UpdateExecConditionals(
22442244
cf_exec_condition_ = condition;
22452245
cf_exec_conditional_merge_ = new spv::Block(
22462246
builder_->getUniqueId(), builder_->getBuildPoint()->getParent());
2247-
SpirvCreateSelectionMerge(cf_exec_conditional_merge_->getId());
2247+
builder_->createSelectionMerge(cf_exec_conditional_merge_,
2248+
spv::SelectionControlDontFlattenMask);
22482249
spv::Block& inner_block = builder_->makeNewBlock();
22492250
builder_->createConditionalBranch(
22502251
condition_id, condition ? &inner_block : cf_exec_conditional_merge_,
@@ -2284,7 +2285,8 @@ void SpirvShaderTranslator::UpdateInstructionPredication(bool predicated,
22842285
spv::Block& predicated_block = builder_->makeNewBlock();
22852286
cf_instruction_predicate_merge_ = new spv::Block(
22862287
builder_->getUniqueId(), builder_->getBuildPoint()->getParent());
2287-
SpirvCreateSelectionMerge(cf_instruction_predicate_merge_->getId());
2288+
builder_->createSelectionMerge(cf_instruction_predicate_merge_,
2289+
spv::SelectionControlMaskNone);
22882290
builder_->createConditionalBranch(
22892291
predicate_id,
22902292
condition ? &predicated_block : cf_instruction_predicate_merge_,
@@ -2870,7 +2872,8 @@ spv::Id SpirvShaderTranslator::EndianSwap32Uint(spv::Id value, spv::Id endian) {
28702872
assert_false(block_pre_8in16.isTerminated());
28712873
spv::Block& block_8in16 = builder_->makeNewBlock();
28722874
spv::Block& block_8in16_merge = builder_->makeNewBlock();
2873-
SpirvCreateSelectionMerge(block_8in16_merge.getId());
2875+
builder_->createSelectionMerge(&block_8in16_merge,
2876+
spv::SelectionControlMaskNone);
28742877
builder_->createConditionalBranch(is_8in16_or_8in32, &block_8in16,
28752878
&block_8in16_merge);
28762879
builder_->setBuildPoint(&block_8in16);
@@ -2910,7 +2913,8 @@ spv::Id SpirvShaderTranslator::EndianSwap32Uint(spv::Id value, spv::Id endian) {
29102913
spv::Block& block_pre_16in32 = *builder_->getBuildPoint();
29112914
spv::Block& block_16in32 = builder_->makeNewBlock();
29122915
spv::Block& block_16in32_merge = builder_->makeNewBlock();
2913-
SpirvCreateSelectionMerge(block_16in32_merge.getId());
2916+
builder_->createSelectionMerge(&block_16in32_merge,
2917+
spv::SelectionControlMaskNone);
29142918
builder_->createConditionalBranch(is_8in32_or_16in32, &block_16in32,
29152919
&block_16in32_merge);
29162920
builder_->setBuildPoint(&block_16in32);
@@ -2982,8 +2986,8 @@ spv::Id SpirvShaderTranslator::LoadUint32FromSharedMemory(
29822986
std::unique_ptr<spv::Instruction> value_phi_op =
29832987
std::make_unique<spv::Instruction>(value_phi_result, type_uint_,
29842988
spv::OpPhi);
2985-
SpirvCreateSelectionMerge(switch_merge_block.getId(),
2986-
spv::SelectionControlDontFlattenMask);
2989+
builder_->createSelectionMerge(&switch_merge_block,
2990+
spv::SelectionControlDontFlattenMask);
29872991
{
29882992
std::unique_ptr<spv::Instruction> switch_op =
29892993
std::make_unique<spv::Instruction>(spv::OpSwitch);

src/xenia/gpu/spirv_shader_translator.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include <utility>
1818
#include <vector>
1919

20-
#include "third_party/glslang/SPIRV/SpvBuilder.h"
2120
#include "xenia/gpu/shader_translator.h"
21+
#include "xenia/gpu/spirv_builder.h"
2222
#include "xenia/gpu/xenos.h"
2323
#include "xenia/ui/vulkan/vulkan_provider.h"
2424

@@ -374,30 +374,30 @@ class SpirvShaderTranslator : public ShaderTranslator {
374374

375375
// Converts the color value externally clamped to [0, 31.875] to 7e3 floating
376376
// point, with zeros in bits 10:31, rounding to the nearest even.
377-
static spv::Id PreClampedFloat32To7e3(spv::Builder& builder,
377+
static spv::Id PreClampedFloat32To7e3(SpirvBuilder& builder,
378378
spv::Id f32_scalar,
379379
spv::Id ext_inst_glsl_std_450);
380380
// Same as PreClampedFloat32To7e3, but clamps the input to [0, 31.875].
381-
static spv::Id UnclampedFloat32To7e3(spv::Builder& builder,
381+
static spv::Id UnclampedFloat32To7e3(SpirvBuilder& builder,
382382
spv::Id f32_scalar,
383383
spv::Id ext_inst_glsl_std_450);
384384
// Converts the 7e3 number in bits [f10_shift, f10_shift + 10) to a 32-bit
385385
// float.
386-
static spv::Id Float7e3To32(spv::Builder& builder, spv::Id f10_uint_scalar,
386+
static spv::Id Float7e3To32(SpirvBuilder& builder, spv::Id f10_uint_scalar,
387387
uint32_t f10_shift, bool result_as_uint,
388388
spv::Id ext_inst_glsl_std_450);
389389
// Converts the depth value externally clamped to the representable [0, 2)
390390
// range to 20e4 floating point, with zeros in bits 24:31, rounding to the
391391
// nearest even or towards zero. If remap_from_0_to_0_5 is true, it's assumed
392392
// that 0...1 is pre-remapped to 0...0.5 in the input.
393-
static spv::Id PreClampedDepthTo20e4(spv::Builder& builder,
393+
static spv::Id PreClampedDepthTo20e4(SpirvBuilder& builder,
394394
spv::Id f32_scalar,
395395
bool round_to_nearest_even,
396396
bool remap_from_0_to_0_5,
397397
spv::Id ext_inst_glsl_std_450);
398398
// Converts the 20e4 number in bits [f24_shift, f24_shift + 10) to a 32-bit
399399
// float.
400-
static spv::Id Depth20e4To32(spv::Builder& builder, spv::Id f24_uint_scalar,
400+
static spv::Id Depth20e4To32(SpirvBuilder& builder, spv::Id f24_uint_scalar,
401401
uint32_t f24_shift, bool remap_to_0_to_0_5,
402402
bool result_as_uint,
403403
spv::Id ext_inst_glsl_std_450);
@@ -451,15 +451,6 @@ class SpirvShaderTranslator : public ShaderTranslator {
451451

452452
// Builder helpers.
453453
spv::Id SpirvSmearScalarResultOrConstant(spv::Id scalar, spv::Id vector_type);
454-
void SpirvCreateSelectionMerge(
455-
spv::Id merge_block_id, spv::SelectionControlMask selection_control_mask =
456-
spv::SelectionControlMaskNone) {
457-
std::unique_ptr<spv::Instruction> selection_merge_op =
458-
std::make_unique<spv::Instruction>(spv::OpSelectionMerge);
459-
selection_merge_op->addIdOperand(merge_block_id);
460-
selection_merge_op->addImmediateOperand(selection_control_mask);
461-
builder_->getBuildPoint()->addInstruction(std::move(selection_merge_op));
462-
}
463454

464455
Modification GetSpirvShaderModification() const {
465456
return Modification(current_translation().modification());
@@ -689,7 +680,7 @@ class SpirvShaderTranslator : public ShaderTranslator {
689680
// and stencil testing with fragment shader interlock.
690681
bool is_depth_only_fragment_shader_ = false;
691682

692-
std::unique_ptr<spv::Builder> builder_;
683+
std::unique_ptr<SpirvBuilder> builder_;
693684

694685
std::vector<spv::Id> id_vector_temp_;
695686
// For helper functions like operand loading, so they don't conflict with

src/xenia/gpu/spirv_shader_translator_alu.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void SpirvShaderTranslator::KillPixel(spv::Id condition) {
6262
builder_->createBranch(merge_block);
6363

6464
builder_->setBuildPoint(&header_block);
65-
SpirvCreateSelectionMerge(merge_block->getId());
65+
builder_->createSelectionMerge(merge_block, spv::SelectionControlMaskNone);
6666
builder_->createConditionalBranch(condition, kill_block, merge_block);
6767

6868
function.addBlock(merge_block);

src/xenia/gpu/spirv_shader_translator_fetch.cc

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,8 +1178,8 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
11781178
spv::Block& block_dimension_head = *builder_->getBuildPoint();
11791179
spv::Block& block_dimension_merge = builder_->makeNewBlock();
11801180
spv::Block& block_dimension_3d = builder_->makeNewBlock();
1181-
SpirvCreateSelectionMerge(block_dimension_merge.getId(),
1182-
spv::SelectionControlDontFlattenMask);
1181+
builder_->createSelectionMerge(&block_dimension_merge,
1182+
spv::SelectionControlDontFlattenMask);
11831183
assert_true(data_is_3d != spv::NoResult);
11841184
builder_->createConditionalBranch(data_is_3d, &block_dimension_3d,
11851185
&block_dimension_merge);
@@ -1209,8 +1209,8 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
12091209
spv::Block* block_dimension_3d =
12101210
z_offset != spv::NoResult ? &builder_->makeNewBlock() : nullptr;
12111211
spv::Block& block_dimension_stacked = builder_->makeNewBlock();
1212-
SpirvCreateSelectionMerge(block_dimension_merge.getId(),
1213-
spv::SelectionControlDontFlattenMask);
1212+
builder_->createSelectionMerge(&block_dimension_merge,
1213+
spv::SelectionControlDontFlattenMask);
12141214
assert_true(data_is_3d != spv::NoResult);
12151215
builder_->createConditionalBranch(
12161216
data_is_3d,
@@ -1318,7 +1318,8 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
13181318
spv::Block& block_ma_y = builder_->makeNewBlock();
13191319
spv::Block& block_ma_z = builder_->makeNewBlock();
13201320
spv::Block& block_ma_merge = builder_->makeNewBlock();
1321-
SpirvCreateSelectionMerge(block_ma_merge.getId());
1321+
builder_->createSelectionMerge(&block_ma_merge,
1322+
spv::SelectionControlMaskNone);
13221323
{
13231324
std::unique_ptr<spv::Instruction> ma_switch_op =
13241325
std::make_unique<spv::Instruction>(spv::OpSwitch);
@@ -1441,8 +1442,8 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
14411442
spv::Block& block_dimension_3d_start = builder_->makeNewBlock();
14421443
spv::Block& block_dimension_stacked_start = builder_->makeNewBlock();
14431444
spv::Block& block_dimension_merge = builder_->makeNewBlock();
1444-
SpirvCreateSelectionMerge(block_dimension_merge.getId(),
1445-
spv::SelectionControlDontFlattenMask);
1445+
builder_->createSelectionMerge(&block_dimension_merge,
1446+
spv::SelectionControlDontFlattenMask);
14461447
assert_true(data_is_3d != spv::NoResult);
14471448
builder_->createConditionalBranch(data_is_3d,
14481449
&block_dimension_3d_start,
@@ -1843,8 +1844,8 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
18431844
spv::Block& block_dimension_3d_start = builder_->makeNewBlock();
18441845
spv::Block& block_dimension_stacked_start = builder_->makeNewBlock();
18451846
spv::Block& block_dimension_merge = builder_->makeNewBlock();
1846-
SpirvCreateSelectionMerge(block_dimension_merge.getId(),
1847-
spv::SelectionControlDontFlattenMask);
1847+
builder_->createSelectionMerge(&block_dimension_merge,
1848+
spv::SelectionControlDontFlattenMask);
18481849
assert_true(data_is_3d != spv::NoResult);
18491850
builder_->createConditionalBranch(data_is_3d,
18501851
&block_dimension_3d_start,
@@ -2021,8 +2022,8 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
20212022
? builder_->makeNewBlock()
20222023
: block_z_head;
20232024
if (vol_filter_is_linear != spv::NoResult) {
2024-
SpirvCreateSelectionMerge(block_z_merge.getId(),
2025-
spv::SelectionControlDontFlattenMask);
2025+
builder_->createSelectionMerge(
2026+
&block_z_merge, spv::SelectionControlDontFlattenMask);
20262027
builder_->createConditionalBranch(
20272028
vol_filter_is_linear, &block_z_linear, &block_z_merge);
20282029
builder_->setBuildPoint(&block_z_linear);
@@ -2187,8 +2188,8 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
21872188
spv::Block& block_swizzle_constant = builder_->makeNewBlock();
21882189
spv::Block& block_swizzle_component = builder_->makeNewBlock();
21892190
spv::Block& block_swizzle_merge = builder_->makeNewBlock();
2190-
SpirvCreateSelectionMerge(block_swizzle_merge.getId(),
2191-
spv::SelectionControlDontFlattenMask);
2191+
builder_->createSelectionMerge(
2192+
&block_swizzle_merge, spv::SelectionControlDontFlattenMask);
21922193
builder_->createConditionalBranch(swizzle_bit_2,
21932194
&block_swizzle_constant,
21942195
&block_swizzle_component);
@@ -2279,8 +2280,8 @@ void SpirvShaderTranslator::ProcessTextureFetchInstruction(
22792280
spv::Block& block_sign_unsigned_biased = builder_->makeNewBlock();
22802281
spv::Block& block_sign_gamma_start = builder_->makeNewBlock();
22812282
spv::Block& block_sign_merge = builder_->makeNewBlock();
2282-
SpirvCreateSelectionMerge(block_sign_merge.getId(),
2283-
spv::SelectionControlDontFlattenMask);
2283+
builder_->createSelectionMerge(
2284+
&block_sign_merge, spv::SelectionControlDontFlattenMask);
22842285
{
22852286
std::unique_ptr<spv::Instruction> sign_switch_op =
22862287
std::make_unique<spv::Instruction>(spv::OpSwitch);
@@ -2543,8 +2544,8 @@ void SpirvShaderTranslator::SampleTexture(
25432544
spv::Block& block_sign_head = *builder_->getBuildPoint();
25442545
spv::Block& block_sign = builder_->makeNewBlock();
25452546
spv::Block& block_sign_merge = builder_->makeNewBlock();
2546-
SpirvCreateSelectionMerge(block_sign_merge.getId(),
2547-
spv::SelectionControlDontFlattenMask);
2547+
builder_->createSelectionMerge(&block_sign_merge,
2548+
spv::SelectionControlDontFlattenMask);
25482549
// Unsigned (i == 0) - if there are any non-signed components.
25492550
// Signed (i == 1) - if there are any signed components.
25502551
builder_->createConditionalBranch(i ? is_any_signed : is_all_signed,
@@ -2601,8 +2602,8 @@ spv::Id SpirvShaderTranslator::QueryTextureLod(
26012602
spv::Block& block_sign_signed = builder_->makeNewBlock();
26022603
spv::Block& block_sign_unsigned = builder_->makeNewBlock();
26032604
spv::Block& block_sign_merge = builder_->makeNewBlock();
2604-
SpirvCreateSelectionMerge(block_sign_merge.getId(),
2605-
spv::SelectionControlDontFlattenMask);
2605+
builder_->createSelectionMerge(&block_sign_merge,
2606+
spv::SelectionControlDontFlattenMask);
26062607
builder_->createConditionalBranch(is_all_signed, &block_sign_signed,
26072608
&block_sign_unsigned);
26082609
builder_->setBuildPoint(&block_sign_signed);

0 commit comments

Comments
 (0)