graphicsdriver/metal: Make this compilable on iOS

This change adds #ifs to enable to compile the driver on iOS.

This also removes BOOL, which can be a duplicated definition.
C's _Bool does not work well with Cgo. Use uint8_t instead for
boolean values.
This commit is contained in:
Hajime Hoshi 2019-06-09 05:44:14 +09:00
parent 67230ec499
commit 3af351a2aa
8 changed files with 35 additions and 22 deletions

View File

@ -31,7 +31,8 @@ import (
// Suppress the warnings about availability guard with -Wno-unguarded-availability-new. // Suppress the warnings about availability guard with -Wno-unguarded-availability-new.
// It is because old Xcode (8 or older?) does not accept @available syntax. // It is because old Xcode (8 or older?) does not accept @available syntax.
// #cgo CFLAGS: -mmacosx-version-min=10.11 -Wno-unguarded-availability-new // #cgo CFLAGS: -Wno-unguarded-availability-new
// #cgo !ios CFLAGS: -mmacosx-version-min=10.11
// #cgo LDFLAGS: -framework QuartzCore -framework Foundation -framework CoreGraphics // #cgo LDFLAGS: -framework QuartzCore -framework Foundation -framework CoreGraphics
// //
// #include "ca.h" // #include "ca.h"

View File

@ -14,9 +14,9 @@
// +build darwin // +build darwin
typedef signed char BOOL; #include <stdint.h>
typedef unsigned long uint_t; typedef unsigned long uint_t;
typedef unsigned short uint16_t;
void *MakeMetalLayer(); void *MakeMetalLayer();
@ -26,7 +26,7 @@ const char *MetalLayer_SetPixelFormat(void *metalLayer, uint16_t pixelFormat);
const char *MetalLayer_SetMaximumDrawableCount(void *metalLayer, const char *MetalLayer_SetMaximumDrawableCount(void *metalLayer,
uint_t maximumDrawableCount); uint_t maximumDrawableCount);
void MetalLayer_SetDisplaySyncEnabled(void *metalLayer, void MetalLayer_SetDisplaySyncEnabled(void *metalLayer,
BOOL displaySyncEnabled); uint8_t displaySyncEnabled);
void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height); void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height);
void *MetalLayer_NextDrawable(void *metalLayer); void *MetalLayer_NextDrawable(void *metalLayer);

View File

@ -20,10 +20,13 @@
void *MakeMetalLayer() { void *MakeMetalLayer() {
CAMetalLayer *layer = [[CAMetalLayer alloc] init]; CAMetalLayer *layer = [[CAMetalLayer alloc] init];
// TODO: Expose a function to set color space. // TODO: Expose a function to set color space.
// TODO: Enable colorspace on iOS: this will be available as of iOS 13.0.
#if !TARGET_OS_IPHONE
CGColorSpaceRef colorspace = CGColorSpaceRef colorspace =
CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3); CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);
layer.colorspace = colorspace; layer.colorspace = colorspace;
CGColorSpaceRelease(colorspace); CGColorSpaceRelease(colorspace);
#endif
return layer; return layer;
} }
@ -65,17 +68,19 @@ const char *MetalLayer_SetMaximumDrawableCount(void *metalLayer,
} }
void MetalLayer_SetDisplaySyncEnabled(void *metalLayer, void MetalLayer_SetDisplaySyncEnabled(void *metalLayer,
BOOL displaySyncEnabled) { uint8_t displaySyncEnabled) {
// @available syntax is not available for old Xcode (#781) // @available syntax is not available for old Xcode (#781)
// //
// If possible, we'd want to write the guard like: // If possible, we'd want to write the guard like:
// //
// if (@available(macOS 10.13, *)) { ... // if (@available(macOS 10.13, *)) { ...
#if !TARGET_OS_IPHONE
if ([(CAMetalLayer *)metalLayer if ([(CAMetalLayer *)metalLayer
respondsToSelector:@selector(setDisplaySyncEnabled:)]) { respondsToSelector:@selector(setDisplaySyncEnabled:)]) {
[((CAMetalLayer *)metalLayer) setDisplaySyncEnabled:displaySyncEnabled]; [((CAMetalLayer *)metalLayer) setDisplaySyncEnabled:displaySyncEnabled];
} }
#endif
} }
void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height) { void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height) {

View File

@ -29,7 +29,8 @@ import (
"github.com/hajimehoshi/ebiten/internal/thread" "github.com/hajimehoshi/ebiten/internal/thread"
) )
// #cgo CFLAGS: -x objective-c -mmacosx-version-min=10.11 // #cgo CFLAGS: -x objective-c
// #cgo !ios CFLAGS: -mmacosx-version-min=10.11
// #cgo LDFLAGS: -framework Foundation // #cgo LDFLAGS: -framework Foundation
// //
// #import <Foundation/Foundation.h> // #import <Foundation/Foundation.h>

View File

@ -30,7 +30,7 @@ import (
"unsafe" "unsafe"
) )
// #cgo CFLAGS: -mmacosx-version-min=10.11 // #cgo !ios CFLAGS: -mmacosx-version-min=10.11
// #cgo LDFLAGS: -framework Metal -framework Foundation // #cgo LDFLAGS: -framework Metal -framework Foundation
// //
// #include <stdlib.h> // #include <stdlib.h>
@ -437,16 +437,16 @@ func (d Device) MakeLibrary(source string, opt CompileOptions) (Library, error)
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433369-makerenderpipelinestate. // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433369-makerenderpipelinestate.
func (d Device) MakeRenderPipelineState(rpd RenderPipelineDescriptor) (RenderPipelineState, error) { func (d Device) MakeRenderPipelineState(rpd RenderPipelineDescriptor) (RenderPipelineState, error) {
blendingEnabled := C.BOOL(0) blendingEnabled := 0
if rpd.ColorAttachments[0].BlendingEnabled { if rpd.ColorAttachments[0].BlendingEnabled {
blendingEnabled = C.BOOL(1) blendingEnabled = 1
} }
c := &rpd.ColorAttachments[0] c := &rpd.ColorAttachments[0]
descriptor := C.struct_RenderPipelineDescriptor{ descriptor := C.struct_RenderPipelineDescriptor{
VertexFunction: rpd.VertexFunction.function, VertexFunction: rpd.VertexFunction.function,
FragmentFunction: rpd.FragmentFunction.function, FragmentFunction: rpd.FragmentFunction.function,
ColorAttachment0PixelFormat: C.uint16_t(c.PixelFormat), ColorAttachment0PixelFormat: C.uint16_t(c.PixelFormat),
ColorAttachment0BlendingEnabled: C.BOOL(blendingEnabled), ColorAttachment0BlendingEnabled: C.uint8_t(blendingEnabled),
ColorAttachment0DestinationAlphaBlendFactor: C.uint8_t(c.DestinationAlphaBlendFactor), ColorAttachment0DestinationAlphaBlendFactor: C.uint8_t(c.DestinationAlphaBlendFactor),
ColorAttachment0DestinationRGBBlendFactor: C.uint8_t(c.DestinationRGBBlendFactor), ColorAttachment0DestinationRGBBlendFactor: C.uint8_t(c.DestinationRGBBlendFactor),
ColorAttachment0SourceAlphaBlendFactor: C.uint8_t(c.SourceAlphaBlendFactor), ColorAttachment0SourceAlphaBlendFactor: C.uint8_t(c.SourceAlphaBlendFactor),

View File

@ -15,18 +15,15 @@
// +build darwin // +build darwin
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
typedef signed char BOOL;
typedef unsigned long uint_t; typedef unsigned long uint_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long long uint64_t;
struct Device { struct Device {
void *Device; void *Device;
BOOL Headless; uint8_t Headless;
BOOL LowPower; uint8_t LowPower;
BOOL Removable; uint8_t Removable;
uint64_t RegistryID; uint64_t RegistryID;
const char *Name; const char *Name;
}; };
@ -45,7 +42,7 @@ struct RenderPipelineDescriptor {
void *VertexFunction; void *VertexFunction;
void *FragmentFunction; void *FragmentFunction;
uint16_t ColorAttachment0PixelFormat; uint16_t ColorAttachment0PixelFormat;
BOOL ColorAttachment0BlendingEnabled; uint8_t ColorAttachment0BlendingEnabled;
uint8_t ColorAttachment0DestinationAlphaBlendFactor; uint8_t ColorAttachment0DestinationAlphaBlendFactor;
uint8_t ColorAttachment0DestinationRGBBlendFactor; uint8_t ColorAttachment0DestinationRGBBlendFactor;
uint8_t ColorAttachment0SourceAlphaBlendFactor; uint8_t ColorAttachment0SourceAlphaBlendFactor;
@ -108,7 +105,7 @@ struct Viewport {
struct Device CreateSystemDefaultDevice(); struct Device CreateSystemDefaultDevice();
struct Devices CopyAllDevices(); struct Devices CopyAllDevices();
BOOL Device_SupportsFeatureSet(void *device, uint16_t featureSet); uint8_t Device_SupportsFeatureSet(void *device, uint16_t featureSet);
void *Device_MakeCommandQueue(void *device); void *Device_MakeCommandQueue(void *device);
struct Library Device_MakeLibrary(void *device, const char *source, struct Library Device_MakeLibrary(void *device, const char *source,
size_t sourceLength); size_t sourceLength);

View File

@ -28,13 +28,18 @@ struct Device CreateSystemDefaultDevice() {
struct Device d; struct Device d;
d.Device = device; d.Device = device;
#if !TARGET_OS_IPHONE
d.Headless = device.headless; d.Headless = device.headless;
d.LowPower = device.lowPower; d.LowPower = device.lowPower;
#else
d.Headless = 0;
d.LowPower = 0;
#endif
d.Name = device.name.UTF8String; d.Name = device.name.UTF8String;
return d; return d;
} }
BOOL Device_SupportsFeatureSet(void *device, uint16_t featureSet) { uint8_t Device_SupportsFeatureSet(void *device, uint16_t featureSet) {
return [(id<MTLDevice>)device supportsFeatureSet:featureSet]; return [(id<MTLDevice>)device supportsFeatureSet:featureSet];
} }
@ -257,17 +262,21 @@ void RenderCommandEncoder_DrawIndexedPrimitives(
} }
void BlitCommandEncoder_Synchronize(void *blitCommandEncoder, void *resource) { void BlitCommandEncoder_Synchronize(void *blitCommandEncoder, void *resource) {
#if !TARGET_OS_IPHONE
[(id<MTLBlitCommandEncoder>)blitCommandEncoder [(id<MTLBlitCommandEncoder>)blitCommandEncoder
synchronizeResource:(id<MTLResource>)resource]; synchronizeResource:(id<MTLResource>)resource];
#endif
} }
void BlitCommandEncoder_SynchronizeTexture(void *blitCommandEncoder, void BlitCommandEncoder_SynchronizeTexture(void *blitCommandEncoder,
void *texture, uint_t slice, void *texture, uint_t slice,
uint_t level) { uint_t level) {
#if !TARGET_OS_IPHONE
[(id<MTLBlitCommandEncoder>)blitCommandEncoder [(id<MTLBlitCommandEncoder>)blitCommandEncoder
synchronizeTexture:(id<MTLTexture>)texture synchronizeTexture:(id<MTLTexture>)texture
slice:(NSUInteger)slice slice:(NSUInteger)slice
level:(NSUInteger)level]; level:(NSUInteger)level];
#endif
} }
void *Library_MakeFunction(void *library, const char *name) { void *Library_MakeFunction(void *library, const char *name) {

View File

@ -27,7 +27,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal/ca" "github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal/ca"
) )
// #cgo CFLAGS: -mmacosx-version-min=10.11 // #cgo !ios CFLAGS: -mmacosx-version-min=10.11
// //
// #include "ns.h" // #include "ns.h"
import "C" import "C"