diff --git a/ds4_metal.m b/ds4_metal.m index 7e3f8bd5c..75d2e4ad9 100644 --- a/ds4_metal.m +++ b/ds4_metal.m @@ -1,5 +1,6 @@ #import #import +#include #include #include @@ -3066,6 +3067,20 @@ static int ds4_gpu_model_map_log_enabled(void) { @[@"DS4_METAL_SET_ROWS_SOURCE", @"metal/set_rows.metal"], ]; + /* Shader sources resolve against the current directory first, then against + * the directory containing the executable, so the binary can be launched + * from any working directory (e.g. inside another project). */ + NSString *exe_dir = nil; + { + char exe_path[4096]; + uint32_t exe_len = sizeof(exe_path); + if (_NSGetExecutablePath(exe_path, &exe_len) == 0) { + NSString *resolved = [[NSString stringWithUTF8String:exe_path] + stringByResolvingSymlinksInPath]; + exe_dir = [resolved stringByDeletingLastPathComponent]; + } + } + NSMutableString *source = [NSMutableString stringWithString:base]; for (NSArray *spec in required_sources) { const char *override_path = getenv([spec[0] UTF8String]); @@ -3075,6 +3090,8 @@ static int ds4_gpu_model_map_log_enabled(void) { } [paths addObject:spec[1]]; [paths addObject:[@"./" stringByAppendingString:spec[1]]]; + if (exe_dir) + [paths addObject:[exe_dir stringByAppendingPathComponent:spec[1]]]; NSString *loaded = nil; NSString *loaded_path = nil;