Skip to content

Commit 19d5600

Browse files
committed
[SPIR-V] Wrap NoContraction operations
1 parent 78f1d55 commit 19d5600

File tree

6 files changed

+277
-457
lines changed

6 files changed

+277
-457
lines changed

src/xenia/gpu/spirv_builder.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ class SpirvBuilder : public spv::Builder {
2525

2626
// Make public rather than protected.
2727
using spv::Builder::createSelectionMerge;
28+
29+
spv::Id createNoContractionUnaryOp(spv::Op op_code, spv::Id type_id,
30+
spv::Id operand) {
31+
spv::Id result = createUnaryOp(op_code, type_id, operand);
32+
addDecoration(result, spv::DecorationNoContraction);
33+
return result;
34+
}
35+
36+
spv::Id createNoContractionBinOp(spv::Op op_code, spv::Id type_id,
37+
spv::Id operand1, spv::Id operand2) {
38+
spv::Id result = createBinOp(op_code, type_id, operand1, operand2);
39+
addDecoration(result, spv::DecorationNoContraction);
40+
return result;
41+
}
2842
};
2943

3044
} // namespace gpu

src/xenia/gpu/spirv_shader_translator.cc

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,9 +1490,8 @@ void SpirvShaderTranslator::CompleteVertexOrTessEvalShaderInMain() {
14901490
builder_->makeUintConstant(
14911491
static_cast<unsigned int>(kSysFlag_WNotReciprocal))),
14921492
const_uint_0_);
1493-
spv::Id guest_position_w_inv = builder_->createBinOp(
1493+
spv::Id guest_position_w_inv = builder_->createNoContractionBinOp(
14941494
spv::OpFDiv, type_float_, const_float_1_, position_w);
1495-
builder_->addDecoration(guest_position_w_inv, spv::DecorationNoContraction);
14961495
position_w =
14971496
builder_->createTriOp(spv::OpSelect, type_float_, is_w_not_reciprocal,
14981497
position_w, guest_position_w_inv);
@@ -1516,10 +1515,8 @@ void SpirvShaderTranslator::CompleteVertexOrTessEvalShaderInMain() {
15161515
builder_->makeUintConstant(
15171516
static_cast<unsigned int>(kSysFlag_XYDividedByW))),
15181517
const_uint_0_);
1519-
spv::Id guest_position_xy_mul_w = builder_->createBinOp(
1518+
spv::Id guest_position_xy_mul_w = builder_->createNoContractionBinOp(
15201519
spv::OpVectorTimesScalar, type_float2_, position_xy, position_w);
1521-
builder_->addDecoration(guest_position_xy_mul_w,
1522-
spv::DecorationNoContraction);
15231520
position_xy = builder_->createTriOp(
15241521
spv::OpSelect, type_float2_,
15251522
builder_->smearScalar(spv::NoPrecision, is_xy_divided_by_w,
@@ -1537,10 +1534,8 @@ void SpirvShaderTranslator::CompleteVertexOrTessEvalShaderInMain() {
15371534
builder_->makeUintConstant(
15381535
static_cast<unsigned int>(kSysFlag_ZDividedByW))),
15391536
const_uint_0_);
1540-
spv::Id guest_position_z_mul_w =
1541-
builder_->createBinOp(spv::OpFMul, type_float_, position_z, position_w);
1542-
builder_->addDecoration(guest_position_z_mul_w,
1543-
spv::DecorationNoContraction);
1537+
spv::Id guest_position_z_mul_w = builder_->createNoContractionBinOp(
1538+
spv::OpFMul, type_float_, position_z, position_w);
15441539
position_z =
15451540
builder_->createTriOp(spv::OpSelect, type_float_, is_z_divided_by_w,
15461541
guest_position_z_mul_w, position_z);
@@ -1565,22 +1560,19 @@ void SpirvShaderTranslator::CompleteVertexOrTessEvalShaderInMain() {
15651560
builder_->createAccessChain(spv::StorageClassUniform,
15661561
uniform_system_constants_, id_vector_temp_),
15671562
spv::NoPrecision);
1568-
position_xyz =
1569-
builder_->createBinOp(spv::OpFMul, type_float3_, position_xyz, ndc_scale);
1570-
builder_->addDecoration(position_xyz, spv::DecorationNoContraction);
1563+
position_xyz = builder_->createNoContractionBinOp(spv::OpFMul, type_float3_,
1564+
position_xyz, ndc_scale);
15711565
id_vector_temp_.clear();
15721566
id_vector_temp_.push_back(
15731567
builder_->makeIntConstant(kSystemConstantNdcOffset));
15741568
spv::Id ndc_offset = builder_->createLoad(
15751569
builder_->createAccessChain(spv::StorageClassUniform,
15761570
uniform_system_constants_, id_vector_temp_),
15771571
spv::NoPrecision);
1578-
spv::Id ndc_offset_mul_w = builder_->createBinOp(
1572+
spv::Id ndc_offset_mul_w = builder_->createNoContractionBinOp(
15791573
spv::OpVectorTimesScalar, type_float3_, ndc_offset, position_w);
1580-
builder_->addDecoration(ndc_offset_mul_w, spv::DecorationNoContraction);
1581-
position_xyz = builder_->createBinOp(spv::OpFAdd, type_float3_, position_xyz,
1582-
ndc_offset_mul_w);
1583-
builder_->addDecoration(position_xyz, spv::DecorationNoContraction);
1574+
position_xyz = builder_->createNoContractionBinOp(
1575+
spv::OpFAdd, type_float3_, position_xyz, ndc_offset_mul_w);
15841576

15851577
// Write the point size.
15861578
if (output_point_size_ != spv::NoResult) {
@@ -1666,36 +1658,29 @@ void SpirvShaderTranslator::CompleteVertexOrTessEvalShaderInMain() {
16661658
id_vector_temp_.clear();
16671659
id_vector_temp_.push_back(builder_->makeIntConstant(
16681660
kSystemConstantPointScreenDiameterToNdcRadius));
1669-
spv::Id point_radius = builder_->createBinOp(
1661+
spv::Id point_radius = builder_->createNoContractionBinOp(
16701662
spv::OpFMul, type_float2_, point_guest_diameter,
16711663
builder_->createLoad(builder_->createAccessChain(
16721664
spv::StorageClassUniform,
16731665
uniform_system_constants_, id_vector_temp_),
16741666
spv::NoPrecision));
1675-
builder_->addDecoration(point_radius, spv::DecorationNoContraction);
16761667
// Transform the radius from the normalized device coordinates to the clip
16771668
// space.
1678-
point_radius = builder_->createBinOp(spv::OpVectorTimesScalar, type_float2_,
1679-
point_radius, position_w);
1680-
builder_->addDecoration(point_radius, spv::DecorationNoContraction);
1681-
1682-
// Apply the direction of expansion for the current host vertex.
1683-
spv::Id point_radius_negative =
1684-
builder_->createUnaryOp(spv::OpFNegate, type_float2_, point_radius);
1685-
builder_->addDecoration(point_radius_negative,
1686-
spv::DecorationNoContraction);
1687-
// Expand the point sprite.
1669+
point_radius = builder_->createNoContractionBinOp(
1670+
spv::OpVectorTimesScalar, type_float2_, point_radius, position_w);
1671+
1672+
// Expand the point sprite in the direction for the current host vertex.
16881673
uint_vector_temp_.clear();
16891674
uint_vector_temp_.push_back(0);
16901675
uint_vector_temp_.push_back(1);
1691-
spv::Id point_position_xy = builder_->createBinOp(
1676+
spv::Id point_position_xy = builder_->createNoContractionBinOp(
16921677
spv::OpFAdd, type_float2_,
16931678
builder_->createRvalueSwizzle(spv::NoPrecision, type_float2_,
16941679
position_xyz, uint_vector_temp_),
16951680
builder_->createTriOp(spv::OpSelect, type_float2_,
16961681
point_vertex_positive, point_radius,
1697-
point_radius_negative));
1698-
builder_->addDecoration(point_position_xy, spv::DecorationNoContraction);
1682+
builder_->createNoContractionUnaryOp(
1683+
spv::OpFNegate, type_float2_, point_radius)));
16991684
// Store the position.
17001685
spv::Id position;
17011686
{
@@ -2419,9 +2404,8 @@ spv::Id SpirvShaderTranslator::ApplyOperandModifiers(
24192404
}
24202405
if (original_operand.is_negated != invert_negate) {
24212406
EnsureBuildPointAvailable();
2422-
operand_value =
2423-
builder_->createUnaryOp(spv::OpFNegate, type, operand_value);
2424-
builder_->addDecoration(operand_value, spv::DecorationNoContraction);
2407+
operand_value = builder_->createNoContractionUnaryOp(spv::OpFNegate, type,
2408+
operand_value);
24252409
}
24262410
return operand_value;
24272411
}
@@ -3092,29 +3076,28 @@ spv::Id SpirvShaderTranslator::PWLGammaToLinear(spv::Id gamma,
30923076
spv::Op value_times_scalar_opcode =
30933077
is_vector ? spv::OpVectorTimesScalar : spv::OpFMul;
30943078
// linear = gamma * (255.0f * 1024.0f) * scale + offset
3095-
spv::Id linear =
3096-
builder_->createBinOp(value_times_scalar_opcode, value_type, gamma,
3097-
builder_->makeFloatConstant(255.0f * 1024.0f));
3098-
builder_->addDecoration(linear, spv::DecorationNoContraction);
3099-
linear = builder_->createBinOp(spv::OpFMul, value_type, linear, scale);
3100-
builder_->addDecoration(linear, spv::DecorationNoContraction);
3101-
linear = builder_->createBinOp(spv::OpFAdd, value_type, linear, offset);
3102-
builder_->addDecoration(linear, spv::DecorationNoContraction);
3079+
spv::Id linear = builder_->createNoContractionBinOp(
3080+
spv::OpFAdd, value_type,
3081+
builder_->createNoContractionBinOp(
3082+
spv::OpFMul, value_type,
3083+
builder_->createNoContractionBinOp(
3084+
value_times_scalar_opcode, value_type, gamma,
3085+
builder_->makeFloatConstant(255.0f * 1024.0f)),
3086+
scale),
3087+
offset);
31033088
// linear += trunc(linear * scale)
3104-
spv::Id linear_integer_term =
3105-
builder_->createBinOp(spv::OpFMul, value_type, linear, scale);
3106-
builder_->addDecoration(linear_integer_term, spv::DecorationNoContraction);
3089+
spv::Id linear_integer_term = builder_->createNoContractionBinOp(
3090+
spv::OpFMul, value_type, linear, scale);
31073091
id_vector_temp_.clear();
31083092
id_vector_temp_.push_back(linear_integer_term);
31093093
linear_integer_term = builder_->createBuiltinCall(
31103094
value_type, ext_inst_glsl_std_450_, GLSLstd450Trunc, id_vector_temp_);
3111-
linear = builder_->createBinOp(spv::OpFAdd, value_type, linear,
3112-
linear_integer_term);
3113-
builder_->addDecoration(linear, spv::DecorationNoContraction);
3095+
linear = builder_->createNoContractionBinOp(spv::OpFAdd, value_type, linear,
3096+
linear_integer_term);
31143097
// linear *= 1.0f / 1023.0f
3115-
linear = builder_->createBinOp(value_times_scalar_opcode, value_type, linear,
3116-
builder_->makeFloatConstant(1.0f / 1023.0f));
3117-
builder_->addDecoration(linear, spv::DecorationNoContraction);
3098+
linear = builder_->createNoContractionBinOp(
3099+
value_times_scalar_opcode, value_type, linear,
3100+
builder_->makeFloatConstant(1.0f / 1023.0f));
31183101
return linear;
31193102
}
31203103

@@ -3187,18 +3170,18 @@ spv::Id SpirvShaderTranslator::LinearToPWLGamma(spv::Id linear,
31873170
offset_3_or_2, offset_1_or_0);
31883171

31893172
// gamma = trunc(linear * scale) * (1.0f / 255.0f) + offset
3190-
spv::Id gamma = builder_->createBinOp(spv::OpFMul, value_type, linear, scale);
3191-
builder_->addDecoration(gamma, spv::DecorationNoContraction);
3173+
spv::Id gamma = builder_->createNoContractionBinOp(spv::OpFMul, value_type,
3174+
linear, scale);
31923175
id_vector_temp_.clear();
31933176
id_vector_temp_.push_back(gamma);
31943177
gamma = builder_->createBuiltinCall(value_type, ext_inst_glsl_std_450_,
31953178
GLSLstd450Trunc, id_vector_temp_);
3196-
gamma = builder_->createBinOp(
3197-
is_vector ? spv::OpVectorTimesScalar : spv::OpFMul, value_type, gamma,
3198-
builder_->makeFloatConstant(1.0f / 255.0f));
3199-
builder_->addDecoration(gamma, spv::DecorationNoContraction);
3200-
gamma = builder_->createBinOp(spv::OpFAdd, value_type, gamma, offset);
3201-
builder_->addDecoration(gamma, spv::DecorationNoContraction);
3179+
gamma = builder_->createNoContractionBinOp(
3180+
spv::OpFAdd, value_type,
3181+
builder_->createNoContractionBinOp(
3182+
is_vector ? spv::OpVectorTimesScalar : spv::OpFMul, value_type, gamma,
3183+
builder_->makeFloatConstant(1.0f / 255.0f)),
3184+
offset);
32023185
return gamma;
32033186
}
32043187

0 commit comments

Comments
 (0)