Search code examples
iosswiftshaderarkitmetal

no matching function for call to 'dot' and 'reflect'


I am trying to run ARBrush iOS ARKit app available in this GitHub repository. which is a really cool 3D drawing app! Unfortunately, I am getting 2 errors. I don't know why I am getting these errors and how to solve them. the first error is " no matching function for call to 'dot' " for line 104 in shaders.metal:

 float diffuseFactor = max(0.0,dot(interpolated.normal, light.direction)); // 1

The second one is " no matching function for call to 'reflect' " in line 109 in the same file (shaders.metal)

float3 reflection = reflect(light.direction, interpolated.normal); // 2

Below is my complete error report

CompileMetalFile /Users/Myname/Documents/Xcode/ARBrush/ARBrush/Shaders.metal (in target: ARBrush)
    cd /Users/Myname/Documents/Xcode/ARBrush
    export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/metal -arch air64 -emit-llvm -c -gline-tables-only -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk -ffast-math -serialize-diagnostics /Users/Myname/Library/Developer/Xcode/DerivedData/ARBrush-amgfmkvulxeadccjphanytgyilwe/Build/Intermediates.noindex/ARBrush.build/Debug-iphoneos/ARBrush.build/Metal/Shaders.dia -o /Users/Myname/Library/Developer/Xcode/DerivedData/ARBrush-amgfmkvulxeadccjphanytgyilwe/Build/Intermediates.noindex/ARBrush.build/Debug-iphoneos/ARBrush.build/Metal/Shaders.air -mios-version-min=11.0 "" -MMD -MT dependencies -MF /Users/Myname/Library/Developer/Xcode/DerivedData/ARBrush-amgfmkvulxeadccjphanytgyilwe/Build/Intermediates.noindex/ARBrush.build/Debug-iphoneos/ARBrush.build/Metal/Shaders.dat /Users/Myname/Documents/Xcode/ARBrush/ARBrush/Shaders.metal

/Users/Myname/Documents/Xcode/ARBrush/ARBrush/Shaders.metal:104:35: error: no matching function for call to 'dot'
    float diffuseFactor = max(0.0,dot(interpolated.normal, light.direction)); // 1
                                  ^~~
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/lib/clang/902.1/include/metal/metal_geometric:171:30: note: candidate template ignored: requirement '_geometric_binary_func_enable<float __attribute__((ext_vector_type(3))), float __attribute__((packed_vector_type(3)))>::value' was not satisfied [with T = float __attribute__((ext_vector_type(3))), U = float __attribute__((packed_vector_type(3)))]
METAL_FUNC make_scalar_t<_O> dot(T x, U y)
                             ^
/Users/Myname/Documents/Xcode/ARBrush/ARBrush/Shaders.metal:109:25: error: no matching function for call to 'reflect'
    float3 reflection = reflect(light.direction, interpolated.normal); // 2
                        ^~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/lib/clang/902.1/include/metal/metal_geometric:354:15: note: candidate template ignored: requirement '_geometric_binary_func_enable<float __attribute__((packed_vector_type(3))), float __attribute__((ext_vector_type(3)))>::value' was not satisfied [with T = float __attribute__((packed_vector_type(3))), U = float __attribute__((ext_vector_type(3)))]
METAL_FUNC _O reflect(T i, U n)
              ^
2 errors generated.

I am running this app using swift 4 on Xcode 10.1 and my development target is iOS 11. I will be so thankful if anyone can address my problems.

Thank you


Solution

  • I believe this issue occurs because the functions in the Metal standard library don't accept packed vector types. You should be able to fix this with an inline type conversion at the call site:

    float diffuseFactor = max(0.0,dot(interpolated.normal, float3(light.direction))); // 1
    
    float3 reflection = reflect(float3(light.direction), interpolated.normal); // 2
    

    or simply create a local variable of non-packed type

    float3 lightDir(light.direction);
    

    and use that in each call instead of passing the struct member directly.