Files
wgpu/naga/tests/out/msl/skybox.msl
T
Jamie Nicol 8807c83363 [naga msl-out] Avoid undefined behaviour for integer division, modulo, negation, and abs
Adds infrastructure to the MSL backend for emitting helper functions,
based upon the existing HLSL backend equivalent. Emit helper functions
for MathFunction::Abs and UnaryOperator::Negate with a signed integer
scalar or vector operand. And for BinaryOperator::Divide and
BinaryOperator::Modulo with signed or unsigned integer scalar or
vector operands.

Abs and Negate are written to avoid signed integer overflow when the
operand equals INT_MIN. This is achieved by bitcasting the value to
unsigned and using the negation operator, then bitcasting the result
back to signed.

Division and Modulo avoid undefined bevaviour for INT_MIN / -1 and
divide-by-zero by using 1 for the divisor instead. Additionally we
avoid undefined behaviour when using the modulo operator when either
or both operands are negative by using the equation from the WGSL
spec, using division, subtraction and multiplication, rather than
MSL's modulus operator.

Lastly, as the usage of the wrapper function for unary integer
negation makes the negation_avoids_prefix_decrement() testcase less
interesting, we extend it to additionally test negating floats.
2025-02-14 15:17:40 -08:00

71 lines
2.1 KiB
Plaintext

// language: metal2.1
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct VertexOutput {
metal::float4 position;
metal::float3 uv;
};
struct Data {
metal::float4x4 proj_inv;
metal::float4x4 view;
};
int naga_div(int lhs, int rhs) {
return lhs / metal::select(rhs, 1, (lhs == -2147483648 & rhs == -1) | (rhs == 0));
}
struct vs_mainInput {
};
struct vs_mainOutput {
metal::float4 position [[position]];
metal::float3 uv [[user(loc0), center_perspective]];
};
vertex vs_mainOutput vs_main(
uint vertex_index [[vertex_id]]
, constant Data& r_data [[buffer(0)]]
) {
int tmp1_ = {};
int tmp2_ = {};
tmp1_ = naga_div(static_cast<int>(vertex_index), 2);
tmp2_ = static_cast<int>(vertex_index) & 1;
int _e9 = tmp1_;
int _e15 = tmp2_;
metal::float4 pos = metal::float4((static_cast<float>(_e9) * 4.0) - 1.0, (static_cast<float>(_e15) * 4.0) - 1.0, 0.0, 1.0);
metal::float4 _e27 = r_data.view[0];
metal::float4 _e32 = r_data.view[1];
metal::float4 _e37 = r_data.view[2];
metal::float3x3 inv_model_view = metal::transpose(metal::float3x3(_e27.xyz, _e32.xyz, _e37.xyz));
metal::float4x4 _e43 = r_data.proj_inv;
metal::float4 unprojected = _e43 * pos;
const auto _tmp = VertexOutput {pos, inv_model_view * unprojected.xyz};
return vs_mainOutput { _tmp.position, _tmp.uv };
}
struct fs_mainInput {
metal::float3 uv [[user(loc0), center_perspective]];
};
struct fs_mainOutput {
metal::float4 member_1 [[color(0)]];
};
fragment fs_mainOutput fs_main(
fs_mainInput varyings_1 [[stage_in]]
, metal::float4 position [[position]]
, metal::texturecube<float, metal::access::sample> r_texture [[texture(0)]]
) {
constexpr metal::sampler r_sampler(
metal::s_address::clamp_to_edge,
metal::t_address::clamp_to_edge,
metal::r_address::clamp_to_edge,
metal::mag_filter::linear,
metal::min_filter::linear,
metal::coord::normalized
);
const VertexOutput in = { position, varyings_1.uv };
metal::float4 _e4 = r_texture.sample(r_sampler, in.uv);
return fs_mainOutput { _e4 };
}