mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-20 14:31:08 +01:00
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, using the negation operator, then bitcasting the result back to signed. As HLSL's bitcast functions asint() and asuint() only work for 32-bit types, we only use this workaround in such cases. Division and Modulo avoid undefined behaviour 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 on operands of mixed signedness by using the equation from the WGSL spec, using division, subtraction and multiplication, rather than HLSL's modulus operator.
73 lines
2.2 KiB
HLSL
73 lines
2.2 KiB
HLSL
struct NagaConstants {
|
|
int first_vertex;
|
|
int first_instance;
|
|
uint other;
|
|
};
|
|
ConstantBuffer<NagaConstants> _NagaConstants: register(b1);
|
|
|
|
struct VertexOutput {
|
|
float4 position : SV_Position;
|
|
float3 uv : LOC0;
|
|
};
|
|
|
|
struct Data {
|
|
row_major float4x4 proj_inv;
|
|
row_major float4x4 view;
|
|
};
|
|
|
|
cbuffer r_data : register(b0) { Data r_data; }
|
|
TextureCube<float4> r_texture : register(t0);
|
|
SamplerState nagaSamplerHeap[2048]: register(s0, space0);
|
|
SamplerComparisonState nagaComparisonSamplerHeap[2048]: register(s0, space1);
|
|
StructuredBuffer<uint> nagaGroup0SamplerIndexArray : register(t0, space2);
|
|
static const SamplerState r_sampler = nagaSamplerHeap[nagaGroup0SamplerIndexArray[0]];
|
|
|
|
struct VertexOutput_vs_main {
|
|
float3 uv : LOC0;
|
|
float4 position : SV_Position;
|
|
};
|
|
|
|
struct FragmentInput_fs_main {
|
|
float3 uv_1 : LOC0;
|
|
float4 position_1 : SV_Position;
|
|
};
|
|
|
|
int naga_div(int lhs, int rhs) {
|
|
return lhs / (((lhs == -2147483648 & rhs == -1) | (rhs == 0)) ? 1 : rhs);
|
|
}
|
|
|
|
VertexOutput ConstructVertexOutput(float4 arg0, float3 arg1) {
|
|
VertexOutput ret = (VertexOutput)0;
|
|
ret.position = arg0;
|
|
ret.uv = arg1;
|
|
return ret;
|
|
}
|
|
|
|
VertexOutput_vs_main vs_main(uint vertex_index : SV_VertexID)
|
|
{
|
|
int tmp1_ = (int)0;
|
|
int tmp2_ = (int)0;
|
|
|
|
tmp1_ = naga_div(int((_NagaConstants.first_vertex + vertex_index)), int(2));
|
|
tmp2_ = (int((_NagaConstants.first_vertex + vertex_index)) & int(1));
|
|
int _e9 = tmp1_;
|
|
int _e15 = tmp2_;
|
|
float4 pos = float4(((float(_e9) * 4.0) - 1.0), ((float(_e15) * 4.0) - 1.0), 0.0, 1.0);
|
|
float4 _e27 = r_data.view[0];
|
|
float4 _e32 = r_data.view[1];
|
|
float4 _e37 = r_data.view[2];
|
|
float3x3 inv_model_view = transpose(float3x3(_e27.xyz, _e32.xyz, _e37.xyz));
|
|
float4x4 _e43 = r_data.proj_inv;
|
|
float4 unprojected = mul(pos, _e43);
|
|
const VertexOutput vertexoutput = ConstructVertexOutput(pos, mul(unprojected.xyz, inv_model_view));
|
|
const VertexOutput_vs_main vertexoutput_1 = { vertexoutput.uv, vertexoutput.position };
|
|
return vertexoutput_1;
|
|
}
|
|
|
|
float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
|
|
{
|
|
VertexOutput in_ = { fragmentinput_fs_main.position_1, fragmentinput_fs_main.uv_1 };
|
|
float4 _e4 = r_texture.Sample(r_sampler, in_.uv);
|
|
return _e4;
|
|
}
|