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.
// 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
//
// #include "ca.h"

View File

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

View File

@ -20,10 +20,13 @@
void *MakeMetalLayer() {
CAMetalLayer *layer = [[CAMetalLayer alloc] init];
// 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 =
CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);
CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);
layer.colorspace = colorspace;
CGColorSpaceRelease(colorspace);
#endif
return layer;
}
@ -65,17 +68,19 @@ const char *MetalLayer_SetMaximumDrawableCount(void *metalLayer,
}
void MetalLayer_SetDisplaySyncEnabled(void *metalLayer,
BOOL displaySyncEnabled) {
uint8_t displaySyncEnabled) {
// @available syntax is not available for old Xcode (#781)
//
// If possible, we'd want to write the guard like:
//
// if (@available(macOS 10.13, *)) { ...
#if !TARGET_OS_IPHONE
if ([(CAMetalLayer *)metalLayer
respondsToSelector:@selector(setDisplaySyncEnabled:)]) {
[((CAMetalLayer *)metalLayer) setDisplaySyncEnabled:displaySyncEnabled];
}
#endif
}
void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height) {

View File

@ -29,7 +29,8 @@ import (
"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
//
// #import <Foundation/Foundation.h>

View File

@ -30,7 +30,7 @@ import (
"unsafe"
)
// #cgo CFLAGS: -mmacosx-version-min=10.11
// #cgo !ios CFLAGS: -mmacosx-version-min=10.11
// #cgo LDFLAGS: -framework Metal -framework Foundation
//
// #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.
func (d Device) MakeRenderPipelineState(rpd RenderPipelineDescriptor) (RenderPipelineState, error) {
blendingEnabled := C.BOOL(0)
blendingEnabled := 0
if rpd.ColorAttachments[0].BlendingEnabled {
blendingEnabled = C.BOOL(1)
blendingEnabled = 1
}
c := &rpd.ColorAttachments[0]
descriptor := C.struct_RenderPipelineDescriptor{
VertexFunction: rpd.VertexFunction.function,
FragmentFunction: rpd.FragmentFunction.function,
ColorAttachment0PixelFormat: C.uint16_t(c.PixelFormat),
ColorAttachment0BlendingEnabled: C.BOOL(blendingEnabled),
ColorAttachment0BlendingEnabled: C.uint8_t(blendingEnabled),
ColorAttachment0DestinationAlphaBlendFactor: C.uint8_t(c.DestinationAlphaBlendFactor),
ColorAttachment0DestinationRGBBlendFactor: C.uint8_t(c.DestinationRGBBlendFactor),
ColorAttachment0SourceAlphaBlendFactor: C.uint8_t(c.SourceAlphaBlendFactor),

View File

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

View File

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

View File

@ -27,7 +27,7 @@ import (
"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"
import "C"