From 1c59fae6b004370b00a98d5f10e9da6b13d2f014 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 14 May 2021 09:45:37 -0400 Subject: [PATCH] Derivative tests --- src/front/wgsl/conv.rs | 2 +- tests/in/standard.param.ron | 6 ++++++ tests/in/standard.wgsl | 9 +++++++++ tests/out/standard.Fragment.glsl | 15 +++++++++++++++ tests/out/standard.msl | 17 +++++++++++++++++ tests/out/standard.spvasm | 32 ++++++++++++++++++++++++++++++++ tests/snapshots.rs | 1 + 7 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/in/standard.param.ron create mode 100644 tests/in/standard.wgsl create mode 100644 tests/out/standard.Fragment.glsl create mode 100644 tests/out/standard.msl create mode 100644 tests/out/standard.spvasm diff --git a/src/front/wgsl/conv.rs b/src/front/wgsl/conv.rs index 227327ad5..4804f2cfe 100644 --- a/src/front/wgsl/conv.rs +++ b/src/front/wgsl/conv.rs @@ -110,7 +110,7 @@ pub fn map_derivative_axis(word: &str) -> Option { match word { "dpdx" => Some(crate::DerivativeAxis::X), "dpdy" => Some(crate::DerivativeAxis::Y), - "dwidth" => Some(crate::DerivativeAxis::Width), + "fwidth" => Some(crate::DerivativeAxis::Width), _ => None, } } diff --git a/tests/in/standard.param.ron b/tests/in/standard.param.ron new file mode 100644 index 000000000..07559cbff --- /dev/null +++ b/tests/in/standard.param.ron @@ -0,0 +1,6 @@ +( + spv_version: (1, 0), + spv_capabilities: [ Shader ], + spv_debug: false, + spv_adjust_coordinate_space: false, +) diff --git a/tests/in/standard.wgsl b/tests/in/standard.wgsl new file mode 100644 index 000000000..a9db49e78 --- /dev/null +++ b/tests/in/standard.wgsl @@ -0,0 +1,9 @@ +// Standard functions. + +[[stage(fragment)]] +fn derivatives([[builtin(position)]] foo: vec4) -> [[location(0)]] vec4 { + let x = dpdx(foo); + let y = dpdy(foo); + let z = fwidth(foo); + return (x + y) * z; +} diff --git a/tests/out/standard.Fragment.glsl b/tests/out/standard.Fragment.glsl new file mode 100644 index 000000000..d39147bfb --- /dev/null +++ b/tests/out/standard.Fragment.glsl @@ -0,0 +1,15 @@ +#version 310 es + +precision highp float; + +layout(location = 0) out vec4 _fs2p_location0; + +void main() { + vec4 foo = gl_FragCoord; + vec4 _expr1 = dFdx(foo); + vec4 _expr2 = dFdy(foo); + vec4 _expr3 = fwidth(foo); + _fs2p_location0 = ((_expr1 + _expr2) * _expr3); + return; +} + diff --git a/tests/out/standard.msl b/tests/out/standard.msl new file mode 100644 index 000000000..3909bef27 --- /dev/null +++ b/tests/out/standard.msl @@ -0,0 +1,17 @@ +#include +#include + + +struct derivativesInput { +}; +struct derivativesOutput { + metal::float4 member [[color(0)]]; +}; +fragment derivativesOutput derivatives( + metal::float4 foo [[position]] +) { + metal::float4 _e1 = metal::dfdx(foo); + metal::float4 _e2 = metal::dfdy(foo); + metal::float4 _e3 = metal::fwidth(foo); + return derivativesOutput { (_e1 + _e2) * _e3 }; +} diff --git a/tests/out/standard.spvasm b/tests/out/standard.spvasm new file mode 100644 index 000000000..a8c5d0693 --- /dev/null +++ b/tests/out/standard.spvasm @@ -0,0 +1,32 @@ +; SPIR-V +; Version: 1.0 +; Generator: rspirv +; Bound: 19 +OpCapability Shader +%1 = OpExtInstImport "GLSL.std.450" +OpMemoryModel Logical GLSL450 +OpEntryPoint Fragment %11 "derivatives" %6 %9 +OpExecutionMode %11 OriginUpperLeft +OpDecorate %6 BuiltIn FragCoord +OpDecorate %9 Location 0 +%2 = OpTypeVoid +%4 = OpTypeFloat 32 +%3 = OpTypeVector %4 4 +%7 = OpTypePointer Input %3 +%6 = OpVariable %7 Input +%10 = OpTypePointer Output %3 +%9 = OpVariable %10 Output +%12 = OpTypeFunction %2 +%11 = OpFunction %2 None %12 +%5 = OpLabel +%8 = OpLoad %3 %6 +OpBranch %13 +%13 = OpLabel +%14 = OpDPdx %3 %8 +%15 = OpDPdy %3 %8 +%16 = OpFwidth %3 %8 +%17 = OpFAdd %3 %14 %15 +%18 = OpFMul %3 %17 %16 +OpStore %9 %18 +OpReturn +OpFunctionEnd \ No newline at end of file diff --git a/tests/snapshots.rs b/tests/snapshots.rs index 86d228959..622eadd21 100644 --- a/tests/snapshots.rs +++ b/tests/snapshots.rs @@ -243,6 +243,7 @@ fn convert_wgsl() { Targets::SPIRV | Targets::METAL | Targets::GLSL, ), ("access", Targets::SPIRV | Targets::METAL | Targets::WGSL), + ("standard", Targets::SPIRV | Targets::METAL | Targets::GLSL), ]; for &(name, targets) in inputs.iter() {