From a49b02c44b868233f886b32b05544448fe40e066 Mon Sep 17 00:00:00 2001 From: Autechre Date: Fri, 22 Jan 2021 22:20:38 +0100 Subject: [PATCH] (Mac) Makefile changes for supporting Metal build (#11929) * (QB) Add rule for Metal - define HAVE_COCOA_METAL for now when building for Metal, and HAVE_COCOA for non-Metal build * Make necessary changes to Makefile.common and qb/config.libs.sh: - Disable OpenGL for now for Metal build - Take Metal conditional out of OpenGL block in Makefile.common * (Metal) Header fixes for compiling without Xcode/Griffin * (Mac) Some buildfixes for non-Xcode building * (cocoa_common.h) Restructure to no longer use -DOSX * (Apple) More buildfixes * (Apple) Cleanups --- Makefile.common | 39 ++++++++++++------- gfx/common/metal/Context.h | 2 + gfx/common/metal/Context.m | 6 ++- gfx/common/metal/MenuDisplay.h | 2 + gfx/common/metal/MenuDisplay.m | 56 ++++++++++++++-------------- gfx/common/metal/RendererCommon.m | 2 + gfx/common/metal/TexturedView.h | 1 + gfx/common/metal_common.m | 15 +++----- gfx/drivers/metal.m | 2 + gfx/drivers_font/metal_raster_font.m | 3 ++ qb/config.libs.sh | 12 +++++- ui/drivers/cocoa/apple_platform.h | 37 +++++++++--------- ui/drivers/cocoa/cocoa_common.h | 47 ++++++++++------------- ui/drivers/cocoa/cocoa_common.m | 16 ++++---- ui/drivers/ui_cocoa.m | 2 +- 15 files changed, 132 insertions(+), 110 deletions(-) diff --git a/Makefile.common b/Makefile.common index 0eb2f34225..365fab1041 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1475,19 +1475,6 @@ ifeq ($(HAVE_GL_CONTEXT), 1) endif endif - ifeq ($(HAVE_METAL), 1) - DEFINES += -DHAVE_METAL - OBJ += gfx/common/metal/Context.o \ - gfx/common/metal/Filter.o \ - gfx/common/metal/RendererCommon.o \ - gfx/common/metal/View.o \ - gfx/common/metal/TexturedView.o \ - gfx/common/metal/MenuDisplay.o \ - gfx/common/metal_common.o \ - gfx/drivers/metal.o \ - gfx/drivers_font/metal_raster_font.o \ - gfx/drivers_display/gfx_display_metal.o - endif ifeq ($(HAVE_MPV), 1) OBJ += cores/libretro-mpv/mpv-libretro.o @@ -1521,6 +1508,23 @@ ifeq ($(HAVE_GL_CONTEXT), 1) endif +ifeq ($(HAVE_METAL), 1) + DEFINES += -DHAVE_METAL + LIBS += -framework Metal -framework MetalKit + # Metal code relies on ARC (Automatic Reference Counting), enable it + DEF_FLAGS += -fobjc-arc + OBJ += gfx/common/metal/Context.o \ + gfx/common/metal/Filter.o \ + gfx/common/metal/RendererCommon.o \ + gfx/common/metal/View.o \ + gfx/common/metal/TexturedView.o \ + gfx/common/metal/MenuDisplay.o \ + gfx/common/metal_common.o \ + gfx/drivers/metal.o \ + gfx/drivers_font/metal_raster_font.o \ + gfx/drivers_display/gfx_display_metal.o +endif + ifeq ($(HAVE_EGL), 1) DEFINES += -DHAVE_EGL DEF_FLAGS += $(EGL_CFLAGS) @@ -2252,7 +2256,14 @@ ifeq ($(HAVE_COMPRESSION), 1) OBJ += tasks/task_decompress.o endif -ifeq ($(HAVE_COCOA),1) +ifeq ($(HAVE_COCOA), 1) + HAVE_COCOA_COMMON = 1 +endif +ifeq ($(HAVE_COCOA_METAL), 1) + HAVE_COCOA_COMMON = 1 +endif + +ifeq ($(HAVE_COCOA_COMMON),1) DEFINES += -DHAVE_MAIN -DOSX OBJ += input/drivers/cocoa_input.o \ ui/drivers/ui_cocoa.o \ diff --git a/gfx/common/metal/Context.h b/gfx/common/metal/Context.h index 770fa3d55c..0a6e9b2148 100644 --- a/gfx/common/metal/Context.h +++ b/gfx/common/metal/Context.h @@ -11,6 +11,8 @@ #import #import "RendererCommon.h" +#include "../../../retroarch.h" + @interface Texture : NSObject @property (nonatomic, readonly) id texture; @property (nonatomic, readonly) id sampler; diff --git a/gfx/common/metal/Context.m b/gfx/common/metal/Context.m index 5519ff251c..033bb219a1 100644 --- a/gfx/common/metal/Context.m +++ b/gfx/common/metal/Context.m @@ -6,10 +6,14 @@ // Copyright © 2018 Stuart Carnie. All rights reserved. // +#include + #import "Context.h" #import "Filter.h" #import -#import "metal_common.h" +#import "../metal_common.h" + +#include "../../verbosity.h" @interface BufferNode : NSObject @property (nonatomic, readonly) id src; diff --git a/gfx/common/metal/MenuDisplay.h b/gfx/common/metal/MenuDisplay.h index 102657d8cd..c2a86d41f6 100644 --- a/gfx/common/metal/MenuDisplay.h +++ b/gfx/common/metal/MenuDisplay.h @@ -4,6 +4,8 @@ #import +#include "../../gfx_display.h" + @class Context; @interface MenuDisplay : NSObject diff --git a/gfx/common/metal/MenuDisplay.m b/gfx/common/metal/MenuDisplay.m index cc5024c5a8..29d2b4e479 100644 --- a/gfx/common/metal/MenuDisplay.m +++ b/gfx/common/metal/MenuDisplay.m @@ -2,13 +2,13 @@ * Created by Stuart Carnie on 6/24/18. */ +#import + #import "Context.h" #import "MenuDisplay.h" #import "ShaderTypes.h" + #include "../../../menu/menu_driver.h" -#import -/* TODO(sgc): this dependency is incorrect */ -#import "../metal_common.h" @implementation MenuDisplay { @@ -24,17 +24,17 @@ { if (self = [super init]) { - _context = context; - _clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); + _context = context; + _clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); _uniforms.projectionMatrix = matrix_proj_ortho(0, 1, 0, 1); - _useScissorRect = NO; + _useScissorRect = NO; } return self; } + (const float *)defaultVertices { - static float dummy[] = { + static float dummy[8] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, @@ -45,7 +45,7 @@ + (const float *)defaultTexCoords { - static float dummy[] = { + static float dummy[8] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, @@ -56,7 +56,7 @@ + (const float *)defaultColor { - static float dummy[] = { + static float dummy[16] = { 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, @@ -67,7 +67,7 @@ - (void)setClearColor:(MTLClearColor)clearColor { - _clearColor = clearColor; + _clearColor = clearColor; _clearNextRender = YES; } @@ -95,11 +95,12 @@ case GFX_DISPLAY_PRIM_TRIANGLESTRIP: return MTLPrimitiveTypeTriangleStrip; case GFX_DISPLAY_PRIM_TRIANGLES: - return MTLPrimitiveTypeTriangle; default: - RARCH_LOG("unexpected primitive type %d\n", prim); - return MTLPrimitiveTypeTriangle; + /* Unexpected primitive type, defaulting to triangle */ + break; } + + return MTLPrimitiveTypeTriangle; } - (void)drawPipeline:(gfx_display_ctx_draw_t *)draw @@ -145,21 +146,21 @@ - (void)draw:(gfx_display_ctx_draw_t *)draw { - const float *vertex = draw->coords->vertex ?: MenuDisplay.defaultVertices; - const float *tex_coord = draw->coords->tex_coord ?: MenuDisplay.defaultTexCoords; - const float *color = draw->coords->color ?: MenuDisplay.defaultColor; - - NSUInteger needed = draw->coords->vertices * sizeof(SpriteVertex); + unsigned i; BufferRange range; + NSUInteger vertex_count; + SpriteVertex *pv; + const float *vertex = draw->coords->vertex ?: MenuDisplay.defaultVertices; + const float *tex_coord = draw->coords->tex_coord ?: MenuDisplay.defaultTexCoords; + const float *color = draw->coords->color ?: MenuDisplay.defaultColor; + NSUInteger needed = draw->coords->vertices * sizeof(SpriteVertex); if (![_context allocRange:&range length:needed]) - { - RARCH_ERR("[Metal]: MenuDisplay unable to allocate buffer of %d bytes", needed); return; - } - NSUInteger vertexCount = draw->coords->vertices; - SpriteVertex *pv = (SpriteVertex *)range.data; - for (unsigned i = 0; i < draw->coords->vertices; i++, pv++) + vertex_count = draw->coords->vertices; + pv = (SpriteVertex *)range.data; + + for (i = 0; i < draw->coords->vertices; i++, pv++) { pv->position = simd_make_float2(vertex[0], 1.0f - vertex[1]); vertex += 2; @@ -197,9 +198,8 @@ }; [rce setViewport:vp]; - if (_useScissorRect) { + if (_useScissorRect) [rce setScissorRect:_scissorRect]; - } switch (draw->pipeline_id) { @@ -214,7 +214,7 @@ [rce setVertexBytes:draw->backend_data length:draw->backend_data_size atIndex:BufferIndexUniforms]; [rce setVertexBuffer:range.buffer offset:range.offset atIndex:BufferIndexPositions]; [rce setFragmentBytes:draw->backend_data length:draw->backend_data_size atIndex:BufferIndexUniforms]; - [rce drawPrimitives:[self _toPrimitiveType:draw->prim_type] vertexStart:0 vertexCount:vertexCount]; + [rce drawPrimitives:[self _toPrimitiveType:draw->prim_type] vertexStart:0 vertexCount:vertex_count]; return; #endif default: @@ -235,6 +235,6 @@ [rce setVertexBuffer:range.buffer offset:range.offset atIndex:BufferIndexPositions]; [rce setFragmentTexture:tex.texture atIndex:TextureIndexColor]; [rce setFragmentSamplerState:tex.sampler atIndex:SamplerIndexDraw]; - [rce drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:vertexCount]; + [rce drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:vertex_count]; } @end diff --git a/gfx/common/metal/RendererCommon.m b/gfx/common/metal/RendererCommon.m index 573c08b16d..d43f5b498a 100644 --- a/gfx/common/metal/RendererCommon.m +++ b/gfx/common/metal/RendererCommon.m @@ -9,6 +9,8 @@ #import "RendererCommon.h" #import +#include "../../verbosity.h" + NSUInteger RPixelFormatToBPP(RPixelFormat format) { switch (format) diff --git a/gfx/common/metal/TexturedView.h b/gfx/common/metal/TexturedView.h index 750dd6d7e5..9c512c7ad9 100644 --- a/gfx/common/metal/TexturedView.h +++ b/gfx/common/metal/TexturedView.h @@ -2,6 +2,7 @@ // Created by Stuart Carnie on 6/16/18. // +#import "Context.h" #import "View.h" @interface TexturedView : NSObject diff --git a/gfx/common/metal_common.m b/gfx/common/metal_common.m index e8637fbf32..ff6182a19b 100644 --- a/gfx/common/metal_common.m +++ b/gfx/common/metal_common.m @@ -27,6 +27,7 @@ #import "metal_common.h" #include "metal/Context.h" +#include "../../ui/drivers/cocoa/apple_platform.h" #include "../../ui/drivers/cocoa/cocoa_common.h" #ifdef HAVE_REWIND @@ -39,6 +40,9 @@ #include "../gfx_widgets.h" #endif +#include "../../configuration.h" +#include "../../verbosity.h" + #define STRUCT_ASSIGN(x, y) \ { \ NSObject * __y = y; \ @@ -192,7 +196,6 @@ - (void)dealloc { - RARCH_LOG("[MetalDriver]: destroyed\n"); if (_viewport) { free(_viewport); @@ -264,10 +267,6 @@ - (void)setViewportWidth:(unsigned)width height:(unsigned)height forceFull:(BOOL)forceFull allowRotate:(BOOL)allowRotate { -#if 0 - RARCH_LOG("[Metal]: setViewportWidth size %dx%d\n", width, height); -#endif - _viewport->full_width = width; _viewport->full_height = height; video_driver_set_size(_viewport->full_width, _viewport->full_height); @@ -856,8 +855,6 @@ typedef struct MTLALIGN(16) - (bool)readViewport:(uint8_t *)buffer isIdle:(bool)isIdle { - RARCH_LOG("[Metal]: readViewport is_idle = %s\n", isIdle ? "YES" : "NO"); - bool enabled = _context.captureEnabled; if (!enabled) _context.captureEnabled = YES; @@ -1149,7 +1146,7 @@ typedef struct MTLALIGN(16) height = _viewport->height; } - RARCH_LOG("[Metal]: Updating framebuffer size %u x %u.\n", width, height); + /* Updating framebuffer size */ MTLPixelFormat fmt = SelectOptimalPixelFormat(glslang_format_to_metal(_engine.pass[i].semantics.format)); if ((i != (_shader->passes - 1)) || @@ -1375,7 +1372,7 @@ typedef struct MTLALIGN(16) NSError *err = nil; NSString *basePath = [[NSString stringWithUTF8String:shader->pass[i].source.path] stringByDeletingPathExtension]; - RARCH_LOG("[Metal]: saving metal shader files to %s\n", basePath.UTF8String); + /* Saving Metal shader files... */ [vs_src writeToFile:[basePath stringByAppendingPathExtension:@"vs.metal"] atomically:NO diff --git a/gfx/drivers/metal.m b/gfx/drivers/metal.m index bfe7c93224..2e7f789419 100644 --- a/gfx/drivers/metal.m +++ b/gfx/drivers/metal.m @@ -54,6 +54,8 @@ #import "../video_coord_array.h" +#include "../../ui/drivers/cocoa/apple_platform.h" + static uint32_t metal_get_flags(void *data); #pragma mark Graphics Context for Metal diff --git a/gfx/drivers_font/metal_raster_font.m b/gfx/drivers_font/metal_raster_font.m index 2783461f68..fb93a5c27f 100644 --- a/gfx/drivers_font/metal_raster_font.m +++ b/gfx/drivers_font/metal_raster_font.m @@ -22,6 +22,9 @@ #include "../font_driver.h" +#include "../../configuration.h" +#include "../../verbosity.h" + @interface MetalRaster : NSObject { __weak MetalDriver *_driver; diff --git a/qb/config.libs.sh b/qb/config.libs.sh index 0f1c85a5f6..1e16cdd0d7 100644 --- a/qb/config.libs.sh +++ b/qb/config.libs.sh @@ -243,7 +243,17 @@ check_platform Darwin METAL 'Metal is' true if [ "$OS" = 'Darwin' ]; then check_lib '' COREAUDIO "-framework AudioUnit" AudioUnitInitialize check_lib '' CORETEXT "-framework CoreText" CTFontCreateWithName - check_lib '' COCOA "-framework AppKit" NSApplicationMain + + if [ "$HAVE_METAL" = yes ]; then + check_lib '' COCOA_METAL "-framework AppKit" NSApplicationMain + add_opt OPENGL no + add_opt OPENGL1 no + add_opt OPENGL_CORE no + die : 'Notice: Metal cannot coexist with OpenGL (yet), so disabling OpenGL.' + else + check_lib '' COCOA "-framework AppKit" NSApplicationMain + fi + check_lib '' AVFOUNDATION "-framework AVFoundation" check_lib '' CORELOCATION "-framework CoreLocation" check_lib '' IOHIDMANAGER "-framework IOKit" IOHIDManagerCreate diff --git a/ui/drivers/cocoa/apple_platform.h b/ui/drivers/cocoa/apple_platform.h index 282d47cd67..221ce68d88 100644 --- a/ui/drivers/cocoa/apple_platform.h +++ b/ui/drivers/cocoa/apple_platform.h @@ -1,14 +1,12 @@ #ifndef COCOA_APPLE_PLATFORM_H #define COCOA_APPLE_PLATFORM_H -#if defined(HAVE_COCOA_METAL) || defined(HAVE_COCOATOUCH) - -#ifdef HAVE_COCOA_METAL +#ifdef HAVE_METAL #import #import #endif -#if !defined(HAVE_COCOATOUCH) +#if defined(HAVE_COCOA_METAL) && !defined(HAVE_COCOATOUCH) @interface WindowListener : NSResponder @end @@ -23,6 +21,8 @@ @end #endif + +#if defined(HAVE_COCOA_METAL) || defined(HAVE_COCOATOUCH) @protocol ApplePlatform /*! @brief renderView returns the current render view based on the viewType */ @@ -41,9 +41,15 @@ - (bool)setDisableDisplaySleep:(bool)disable; @end +#endif + +#if defined(HAVE_COCOA_METAL) || defined(HAVE_COCOATOUCH) extern id apple_platform; id apple_platform; +#else +id apple_platform; +#endif #if defined(HAVE_COCOATOUCH) @interface RetroArch_iOS : UINavigationController { - (void)refreshSystemConfig; @end - #else +#if defined(HAVE_COCOA_METAL) @interface RetroArch_OSX : NSObject { +#elif (defined(__MACH__) && (defined(__ppc__) || defined(__ppc64__))) +@interface RetroArch_OSX : NSObject { +#else +@interface RetroArch_OSX : NSObject { +#endif NSWindow *_window; apple_view_type_t _vt; NSView *_renderView; id _sleepActivity; +#if defined(HAVE_COCOA_METAL) WindowListener *_listener; +#endif } -#endif -#elif defined(HAVE_COCOA) -id apple_platform; -#if (defined(__MACH__) && (defined(__ppc__) || defined(__ppc64__))) -@interface RetroArch_OSX : NSObject -#else -@interface RetroArch_OSX : NSObject -#endif -{ - NSWindow *_window; -} -#endif - -#ifdef OSX @property(nonatomic, retain) NSWindow IBOutlet *window; @end diff --git a/ui/drivers/cocoa/cocoa_common.h b/ui/drivers/cocoa/cocoa_common.h index 63af13f812..3749c26c23 100644 --- a/ui/drivers/cocoa/cocoa_common.h +++ b/ui/drivers/cocoa/cocoa_common.h @@ -21,11 +21,16 @@ #if defined(HAVE_COCOATOUCH) #include - #if TARGET_OS_TV #import #endif +#else +#include +#endif +#include "../../../retroarch.h" + +#if defined(HAVE_COCOATOUCH) #define RAScreen UIScreen #ifndef UIUserInterfaceIdiomTV @@ -35,18 +40,6 @@ #ifndef UIUserInterfaceIdiomCarPlay #define UIUserInterfaceIdiomCarPlay 3 #endif -#else - -#define RAScreen NSScreen -#endif - -#if defined(OSX) -#include -#endif - -#include "../../../retroarch.h" - -#if defined(HAVE_COCOATOUCH) #if TARGET_OS_IOS @interface CocoaView : UIViewController @@ -57,7 +50,17 @@ @end void get_ios_version(int *major, int *minor); +#else +#define RAScreen NSScreen +@interface CocoaView : NSView + ++ (CocoaView*)get; +#if !defined(HAVE_COCOA) && !defined(HAVE_COCOA_METAL) +- (void)display; +#endif + +@end #endif typedef struct @@ -68,18 +71,6 @@ typedef struct } apple_frontend_settings_t; extern apple_frontend_settings_t apple_frontend_settings; -#if defined(OSX) -@interface CocoaView : NSView - -+ (CocoaView*)get; -#if !defined(HAVE_COCOA) && !defined(HAVE_COCOA_METAL) -- (void)display; -#endif - -@end - -#endif - #define BOXSTRING(x) [NSString stringWithUTF8String:x] #define BOXINT(x) [NSNumber numberWithInt:x] #define BOXUINT(x) [NSNumber numberWithUnsignedInt:x] @@ -116,11 +107,11 @@ void cocoa_show_mouse(void *data, bool state); void *cocoa_screen_get_chosen(void); -#ifdef OSX +#ifdef HAVE_COCOATOUCH +float cocoa_screen_get_native_scale(void); +#else float cocoa_screen_get_backing_scale_factor(void); void cocoa_update_title(void *data); -#else -float cocoa_screen_get_native_scale(void); #endif bool cocoa_get_metrics( diff --git a/ui/drivers/cocoa/cocoa_common.m b/ui/drivers/cocoa/cocoa_common.m index a33bba05a8..08d074790f 100644 --- a/ui/drivers/cocoa/cocoa_common.m +++ b/ui/drivers/cocoa/cocoa_common.m @@ -16,26 +16,24 @@ #import #include + +#include + #include "cocoa_common.h" #include "apple_platform.h" #include "../ui_cocoa.h" -#include +#ifdef HAVE_COCOATOUCH +#import "../../../pkg/apple/WebServer/GCDWebUploader/GCDWebUploader.h" +#import "WebServer.h" +#endif #include "../../../configuration.h" #include "../../../retroarch.h" #include "../../../verbosity.h" -#ifdef HAVE_COCOATOUCH -#import "GCDWebUploader.h" -#import "WebServer.h" -#include "apple_platform.h" -#endif - static CocoaView* g_instance; - - #ifdef HAVE_COCOATOUCH void *glkitview_init(void); diff --git a/ui/drivers/ui_cocoa.m b/ui/drivers/ui_cocoa.m index e96bb1cdea..28f96395b4 100644 --- a/ui/drivers/ui_cocoa.m +++ b/ui/drivers/ui_cocoa.m @@ -121,7 +121,7 @@ static char **waiting_argv; old_flags = new_flags; apple_input_keyboard_event(down, keycode, - 0, new_flags, RETRO_DEVICE_KEYBOARD); + 0, (uint32_t)new_flags, RETRO_DEVICE_KEYBOARD); } break; case NSEventTypeMouseMoved: