macOS API sync

This commit is contained in:
Jack Andersen
2018-10-15 17:13:57 -10:00
parent 592ffa1372
commit 65c99ad769
8 changed files with 177 additions and 425 deletions

View File

@@ -3,7 +3,6 @@
#include "boo/IApplication.hpp"
#include "boo/graphicsdev/Metal.hpp"
#include "boo/graphicsdev/GL.hpp"
#include "CocoaCommon.hpp"
#include "../Common.hpp"
@@ -32,8 +31,7 @@ namespace boo
{
static logvisor::Module Log("boo::ApplicationCocoa");
std::shared_ptr<IWindow> _WindowCocoaNew(SystemStringView title, NSOpenGLContext* lastGLCtx,
MetalContext* metalCtx, GLContext* glCtx);
std::shared_ptr<IWindow> _WindowCocoaNew(SystemStringView title, MetalContext* metalCtx);
class ApplicationCocoa : public IApplication
{
@@ -52,7 +50,9 @@ private:
std::unordered_map<uintptr_t, std::weak_ptr<IWindow>> m_windows;
MetalContext m_metalCtx;
#if 0
GLContext m_glCtx;
#endif
void _deletedWindow(IWindow* window)
{
@@ -78,9 +78,11 @@ public:
m_metalCtx.m_sampleCount = samples;
m_metalCtx.m_anisotropy = anisotropy;
m_metalCtx.m_pixelFormat = deepColor ? MTLPixelFormatRGBA16Float : MTLPixelFormatBGRA8Unorm;
#if 0
m_glCtx.m_sampleCount = samples;
m_glCtx.m_anisotropy = anisotropy;
m_glCtx.m_deepColor = deepColor;
#endif
[[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyRegular];
@@ -104,38 +106,13 @@ public:
action:nil keyEquivalent:@""] setSubmenu:appMenu];
[[NSApplication sharedApplication] setMainMenu:rootMenu];
/* Determine which graphics API to use */
#if BOO_HAS_METAL
bool useGL = false;
if (!gfxApi.compare("OpenGL"))
useGL = true;
for (const SystemString& arg : args)
{
if (!arg.compare("--gl"))
{
useGL = true;
break;
}
else if (!arg.compare("--metal"))
{
useGL = false;
break;
}
}
if (!useGL)
m_metalCtx.m_dev = MTLCreateSystemDefaultDevice();
if (m_metalCtx.m_dev)
{
m_metalCtx.m_q = [m_metalCtx.m_dev newCommandQueue];
while (![m_metalCtx.m_dev supportsTextureSampleCount:m_metalCtx.m_sampleCount])
m_metalCtx.m_sampleCount = flp2(m_metalCtx.m_sampleCount - 1);
Log.report(logvisor::Info, "using Metal renderer");
}
else
Log.report(logvisor::Info, "using OpenGL renderer");
#else
Log.report(logvisor::Info, "using OpenGL renderer");
#endif
m_metalCtx.m_dev = MTLCreateSystemDefaultDevice();
if (!m_metalCtx.m_dev)
Log.report(logvisor::Fatal, "Unable to create metal device");
m_metalCtx.m_q = [m_metalCtx.m_dev newCommandQueue];
while (![m_metalCtx.m_dev supportsTextureSampleCount:m_metalCtx.m_sampleCount])
m_metalCtx.m_sampleCount = flp2(m_metalCtx.m_sampleCount - 1);
Log.report(logvisor::Info, "using Metal renderer");
}
EPlatformType getPlatformType() const
@@ -214,19 +191,23 @@ public:
std::shared_ptr<IWindow> newWindow(std::string_view title)
{
auto newWindow = _WindowCocoaNew(title, m_lastGLCtx, &m_metalCtx, &m_glCtx);
auto newWindow = _WindowCocoaNew(title, &m_metalCtx);
m_windows[newWindow->getPlatformHandle()] = newWindow;
return newWindow;
}
/* Last GL context */
#if 0
NSOpenGLContext* m_lastGLCtx = nullptr;
#endif
};
#if 0
void _CocoaUpdateLastGLCtx(NSOpenGLContext* lastGLCtx)
{
static_cast<ApplicationCocoa*>(APP)->m_lastGLCtx = lastGLCtx;
}
#endif
IApplication* APP = nullptr;
int ApplicationRun(IApplication::EPlatformType platform,

View File

@@ -1,5 +1,3 @@
#include "boo/graphicsdev/GL.hpp"
#include "boo/graphicsdev/glew.h"
#include "boo/graphicsdev/Metal.hpp"
#include "CocoaCommon.hpp"
#import <AppKit/AppKit.h>
@@ -26,6 +24,7 @@ namespace boo {class WindowCocoa; class GraphicsContextCocoa;}
- (void)setTouchBarProvider:(id)provider;
@end
#if 0
/* AppKit applies OpenGL much differently than other platforms
* the NSOpenGLView class composes together all necessary
* OGL context members and provides the necessary event hooks
@@ -95,6 +94,7 @@ static const NSOpenGLPixelFormatAttribute* PF_TABLE[] =
PF_RGBAF32_ATTRS,
PF_RGBAF32_Z24_ATTRS
};
#endif
@interface BooCocoaResponder : NSResponder <NSTextInputClient>
{
@@ -155,10 +155,13 @@ public:
}
virtual BooCocoaResponder* responder() const=0;
};
#if 0
class GraphicsContextCocoaGL;
#endif
class GraphicsContextCocoaMetal;
}
#if 0
@interface GraphicsContextCocoaGLInternal : NSOpenGLView
{
@public
@@ -166,6 +169,7 @@ class GraphicsContextCocoaMetal;
}
- (id)initWithBooContext:(boo::GraphicsContextCocoaGL*)bctx;
@end
#endif
@interface GraphicsContextCocoaMetalInternal : NSView
{
@@ -181,11 +185,14 @@ class GraphicsContextCocoaMetal;
namespace boo
{
static logvisor::Module Log("boo::WindowCocoa");
#if 0
std::unique_ptr<IGraphicsCommandQueue> _NewGLCommandQueue(IGraphicsContext* parent, GLContext* glCtx);
std::unique_ptr<IGraphicsDataFactory> _NewGLDataFactory(IGraphicsContext* parent, GLContext* glCtx);
#endif
std::unique_ptr<IGraphicsCommandQueue> _NewMetalCommandQueue(MetalContext* ctx, IWindow* parentWindow,
IGraphicsContext* parent);
std::unique_ptr<IGraphicsDataFactory> _NewMetalDataFactory(IGraphicsContext* parent, MetalContext* ctx);
#if 0
void _CocoaUpdateLastGLCtx(NSOpenGLContext* lastGLCtx);
class GraphicsContextCocoaGL : public GraphicsContextCocoa
@@ -349,6 +356,7 @@ IGraphicsContext* _GraphicsContextCocoaGLNew(IGraphicsContext::EGraphicsAPI api,
return new GraphicsContextCocoaGL(api, parentWindow, lastGLCtx, glCtx);
}
#endif
#if BOO_HAS_METAL
class GraphicsContextCocoaMetal : public GraphicsContextCocoa
@@ -1147,6 +1155,7 @@ static boo::ESpecialKey translateKeycode(short code)
@end
#if 0
@implementation GraphicsContextCocoaGLInternal
- (id)initWithBooContext:(boo::GraphicsContextCocoaGL*)bctx
{
@@ -1203,6 +1212,7 @@ static boo::ESpecialKey translateKeycode(short code)
}
@end
#endif
#if BOO_HAS_METAL
@implementation GraphicsContextCocoaMetalInternal
@@ -1299,23 +1309,16 @@ class WindowCocoa : public IWindow
public:
void setup(std::string_view title, NSOpenGLContext* lastGLCtx, MetalContext* metalCtx, GLContext* glCtx)
void setup(std::string_view title, MetalContext* metalCtx)
{
dispatch_sync(dispatch_get_main_queue(),
^{
std::shared_ptr<boo::WindowCocoa> windowPtr =
std::static_pointer_cast<boo::WindowCocoa>(shared_from_this());
m_nsWindow = [[WindowCocoaInternal alloc] initWithBooWindow:windowPtr title:title];
#if BOO_HAS_METAL
if (metalCtx->m_dev)
m_gfxCtx = static_cast<GraphicsContextCocoa*>(
_GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI::Metal,
this, metalCtx));
else
#endif
m_gfxCtx = static_cast<GraphicsContextCocoa*>(
_GraphicsContextCocoaGLNew(IGraphicsContext::EGraphicsAPI::OpenGL3_3,
this, lastGLCtx, glCtx));
m_gfxCtx = static_cast<GraphicsContextCocoa*>(
_GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI::Metal,
this, metalCtx));
m_gfxCtx->initializeContext(nullptr);
});
m_gfxCtx->getMainContextDataFactory();
@@ -1587,11 +1590,10 @@ public:
};
std::shared_ptr<IWindow> _WindowCocoaNew(SystemStringView title, NSOpenGLContext* lastGLCtx,
MetalContext* metalCtx, GLContext* glCtx)
std::shared_ptr<IWindow> _WindowCocoaNew(SystemStringView title, MetalContext* metalCtx)
{
auto ret = std::make_shared<WindowCocoa>();
ret->setup(title, lastGLCtx, metalCtx, glCtx);
ret->setup(title, metalCtx);
return ret;
}