(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
This commit is contained in:
Autechre 2021-01-22 22:20:38 +01:00 committed by GitHub
parent a213c2dcdf
commit a49b02c44b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 132 additions and 110 deletions

View File

@ -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 \

View File

@ -11,6 +11,8 @@
#import <QuartzCore/CAMetalLayer.h>
#import "RendererCommon.h"
#include "../../../retroarch.h"
@interface Texture : NSObject
@property (nonatomic, readonly) id<MTLTexture> texture;
@property (nonatomic, readonly) id<MTLSamplerState> sampler;

View File

@ -6,10 +6,14 @@
// Copyright © 2018 Stuart Carnie. All rights reserved.
//
#include <retro_assert.h>
#import "Context.h"
#import "Filter.h"
#import <QuartzCore/QuartzCore.h>
#import "metal_common.h"
#import "../metal_common.h"
#include "../../verbosity.h"
@interface BufferNode : NSObject
@property (nonatomic, readonly) id<MTLBuffer> src;

View File

@ -4,6 +4,8 @@
#import <Foundation/Foundation.h>
#include "../../gfx_display.h"
@class Context;
@interface MenuDisplay : NSObject

View File

@ -2,13 +2,13 @@
* Created by Stuart Carnie on 6/24/18.
*/
#import <Metal/Metal.h>
#import "Context.h"
#import "MenuDisplay.h"
#import "ShaderTypes.h"
#include "../../../menu/menu_driver.h"
#import <Metal/Metal.h>
/* 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

View File

@ -9,6 +9,8 @@
#import "RendererCommon.h"
#import <Metal/Metal.h>
#include "../../verbosity.h"
NSUInteger RPixelFormatToBPP(RPixelFormat format)
{
switch (format)

View File

@ -2,6 +2,7 @@
// Created by Stuart Carnie on 6/16/18.
//
#import "Context.h"
#import "View.h"
@interface TexturedView : NSObject

View File

@ -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

View File

@ -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

View File

@ -22,6 +22,9 @@
#include "../font_driver.h"
#include "../../configuration.h"
#include "../../verbosity.h"
@interface MetalRaster : NSObject
{
__weak MetalDriver *_driver;

View File

@ -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

View File

@ -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 <Metal/Metal.h>
#import <MetalKit/MetalKit.h>
#endif
#if !defined(HAVE_COCOATOUCH)
#if defined(HAVE_COCOA_METAL) && !defined(HAVE_COCOATOUCH)
@interface WindowListener : NSResponder<NSWindowDelegate>
@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<ApplePlatform> apple_platform;
id<ApplePlatform> apple_platform;
#else
id apple_platform;
#endif
#if defined(HAVE_COCOATOUCH)
@interface RetroArch_iOS : UINavigationController<ApplePlatform, UIApplicationDelegate,
@ -63,30 +69,23 @@ UINavigationControllerDelegate> {
- (void)refreshSystemConfig;
@end
#else
#if defined(HAVE_COCOA_METAL)
@interface RetroArch_OSX : NSObject<ApplePlatform, NSApplicationDelegate> {
#elif (defined(__MACH__) && (defined(__ppc__) || defined(__ppc64__)))
@interface RetroArch_OSX : NSObject {
#else
@interface RetroArch_OSX : NSObject<NSApplicationDelegate> {
#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<NSApplicationDelegate>
#endif
{
NSWindow *_window;
}
#endif
#ifdef OSX
@property(nonatomic, retain) NSWindow IBOutlet *window;
@end

View File

@ -21,11 +21,16 @@
#if defined(HAVE_COCOATOUCH)
#include <UIKit/UIKit.h>
#if TARGET_OS_TV
#import <GameController/GameController.h>
#endif
#else
#include <AppKit/AppKit.h>
#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 <AppKit/AppKit.h>
#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(

View File

@ -16,26 +16,24 @@
#import <AvailabilityMacros.h>
#include <sys/stat.h>
#include <retro_assert.h>
#include "cocoa_common.h"
#include "apple_platform.h"
#include "../ui_cocoa.h"
#include <retro_assert.h>
#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);

View File

@ -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: