macOS fixes

This commit is contained in:
Jack Andersen 2018-01-21 13:07:34 -10:00
parent 93c787dcd4
commit 5f48359cc7
5 changed files with 212 additions and 67 deletions

View File

@ -1,4 +1,5 @@
#include "Common.hpp" #include "Common.hpp"
#include <cmath>
namespace boo namespace boo
{ {

View File

@ -1321,8 +1321,6 @@ struct GLCommandQueue : IGraphicsCommandQueue
{ {
std::unique_lock<std::mutex> lk(self->m_initmt); std::unique_lock<std::mutex> lk(self->m_initmt);
self->m_parent->makeCurrent(); self->m_parent->makeCurrent();
//if (glewInit() != GLEW_OK)
// Log.report(logvisor::Fatal, "unable to init glew");
const GLubyte* version = glGetString(GL_VERSION); const GLubyte* version = glGetString(GL_VERSION);
Log.report(logvisor::Info, "OpenGL Version: %s", version); Log.report(logvisor::Info, "OpenGL Version: %s", version);
self->m_parent->postInit(); self->m_parent->postInit();

View File

@ -17,6 +17,50 @@
#define MAX_UNIFORM_COUNT 8 #define MAX_UNIFORM_COUNT 8
#define MAX_TEXTURE_COUNT 8 #define MAX_TEXTURE_COUNT 8
static const char* GammaVS =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"struct VertData\n"
"{\n"
" float4 posIn [[ attribute(0) ]];\n"
" float4 uvIn [[ attribute(1) ]];\n"
"};\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 pos [[ position ]];\n"
" float2 uv;\n"
"};\n"
"\n"
"vertex VertToFrag vmain(VertData v [[ stage_in ]])\n"
"{\n"
" VertToFrag vtf;\n"
" vtf.uv = v.uvIn.xy;\n"
" vtf.pos = v.posIn;\n"
" return vtf;\n"
"}\n";
static const char* GammaFS =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"struct VertToFrag\n"
"{\n"
" float4 pos [[ position ]];\n"
" float2 uv;\n"
"};\n"
"\n"
"fragment float4 fmain(VertToFrag vtf [[ stage_in ]],\n"
" sampler clampSamp [[ sampler(2) ]],\n"
" texture2d<float> screenTex [[ texture(0) ]],\n"
" texture2d<float> gammaLUT [[ texture(1) ]])\n"
"{\n"
" uint4 tex = uint4(saturate(screenTex.sample(clampSamp, vtf.uv)) * float4(65535.0));\n"
" float4 colorOut;\n"
" for (int i=0 ; i<3 ; ++i)\n"
" colorOut[i] = gammaLUT.read(uint2(tex[i] % 256, tex[i] / 256)).r;\n"
" return colorOut;\n"
"}\n";
namespace boo namespace boo
{ {
static logvisor::Module Log("boo::Metal"); static logvisor::Module Log("boo::Metal");
@ -38,6 +82,43 @@ class MetalDataFactoryImpl : public MetalDataFactory, public GraphicsDataFactory
std::unordered_map<uint64_t, std::unique_ptr<MetalShareableShader>> m_sharedShaders; std::unordered_map<uint64_t, std::unique_ptr<MetalShareableShader>> m_sharedShaders;
struct MetalContext* m_ctx; struct MetalContext* m_ctx;
float m_gamma = 1.f;
ObjToken<IShaderPipeline> m_gammaShader;
ObjToken<ITextureD> m_gammaLUT;
ObjToken<IGraphicsBufferS> m_gammaVBO;
ObjToken<IVertexFormat> m_gammaVFMT;
ObjToken<IShaderDataBinding> m_gammaBinding;
void SetupGammaResources()
{
commitTransaction([this](IGraphicsDataFactory::Context& ctx)
{
const VertexElementDescriptor vfmt[] = {
{nullptr, nullptr, VertexSemantic::Position4},
{nullptr, nullptr, VertexSemantic::UV4}
};
m_gammaVFMT = ctx.newVertexFormat(2, vfmt);
m_gammaShader = static_cast<Context&>(ctx).newShaderPipeline(GammaVS, GammaFS,
nullptr, nullptr, m_gammaVFMT, BlendFactor::One, BlendFactor::Zero,
Primitive::TriStrips, ZTest::None, false, true, false, CullMode::None);
m_gammaLUT = ctx.newDynamicTexture(256, 256, TextureFormat::I16, TextureClampMode::ClampToEdge);
setDisplayGamma(1.f);
const struct Vert {
float pos[4];
float uv[4];
} verts[4] = {
{{-1.f, -1.f, 0.f, 1.f}, {0.f, 0.f, 0.f, 0.f}},
{{ 1.f, -1.f, 0.f, 1.f}, {1.f, 0.f, 0.f, 0.f}},
{{-1.f, 1.f, 0.f, 1.f}, {0.f, 1.f, 0.f, 0.f}},
{{ 1.f, 1.f, 0.f, 1.f}, {1.f, 1.f, 0.f, 0.f}}
};
m_gammaVBO = ctx.newStaticBuffer(BufferUse::Vertex, verts, 32, 4);
ObjToken<ITexture> texs[] = {{}, m_gammaLUT.get()};
m_gammaBinding = ctx.newShaderDataBinding(m_gammaShader, m_gammaVFMT, m_gammaVBO.get(), {}, {},
0, nullptr, nullptr, 2, texs, nullptr, nullptr);
return true;
});
}
public: public:
std::unordered_map<uint64_t, uint64_t> m_sourceToBinary; std::unordered_map<uint64_t, uint64_t> m_sourceToBinary;
char m_libfile[MAXPATHLEN]; char m_libfile[MAXPATHLEN];
@ -201,6 +282,16 @@ public:
} }
return 0; return 0;
} }
void setDisplayGamma(float gamma)
{
if (m_ctx->m_pixelFormat == MTLPixelFormatRGBA16Float)
m_gamma = gamma * 2.2f;
else
m_gamma = gamma;
if (m_gamma != 1.f)
UpdateGammaLUT(m_gammaLUT.get(), m_gamma);
}
}; };
#define MTL_STATIC MTLResourceCPUCacheModeWriteCombined|MTLResourceStorageModeManaged #define MTL_STATIC MTLResourceCPUCacheModeWriteCombined|MTLResourceStorageModeManaged
@ -296,6 +387,11 @@ class MetalTextureS : public GraphicsDataNode<ITextureS>
ppitchNum = 1; ppitchNum = 1;
bytesPerRow = width * ppitchNum; bytesPerRow = width * ppitchNum;
break; break;
case TextureFormat::I16:
pfmt = MTLPixelFormatR16Unorm;
ppitchNum = 2;
bytesPerRow = width * ppitchNum;
break;
case TextureFormat::DXT1: case TextureFormat::DXT1:
pfmt = MTLPixelFormatBC1_RGBA; pfmt = MTLPixelFormatBC1_RGBA;
ppitchNum = 1; ppitchNum = 1;
@ -352,6 +448,10 @@ class MetalTextureSA : public GraphicsDataNode<ITextureSA>
pfmt = MTLPixelFormatR8Unorm; pfmt = MTLPixelFormatR8Unorm;
ppitch = 1; ppitch = 1;
break; break;
case TextureFormat::I16:
pfmt = MTLPixelFormatR16Unorm;
ppitch = 2;
break;
default: break; default: break;
} }
@ -417,6 +517,10 @@ class MetalTextureD : public GraphicsDataNode<ITextureD>
format = MTLPixelFormatR8Unorm; format = MTLPixelFormatR8Unorm;
m_pxPitch = 1; m_pxPitch = 1;
break; break;
case TextureFormat::I16:
format = MTLPixelFormatR16Unorm;
m_pxPitch = 2;
break;
default: default:
Log.report(logvisor::Fatal, "unsupported tex format"); Log.report(logvisor::Fatal, "unsupported tex format");
} }
@ -1061,6 +1165,11 @@ struct MetalCommandQueue : IGraphicsCommandQueue
} }
} }
void startRenderer()
{
static_cast<MetalDataFactoryImpl*>(m_parent->getDataFactory())->SetupGammaResources();
}
void stopRenderer() void stopRenderer()
{ {
m_running = false; m_running = false;
@ -1212,6 +1321,54 @@ struct MetalCommandQueue : IGraphicsCommandQueue
baseInstance:m_boundData->m_baseInst]; baseInstance:m_boundData->m_baseInst];
} }
void _resolveBindTexture(MetalTextureR* tex, const SWindowRect& rect, bool tlOrigin,
int bindIdx, bool color, bool depth)
{
if (tex->samples() > 1)
{
if (color && tex->m_colorBindTex[bindIdx])
[[m_cmdBuf renderCommandEncoderWithDescriptor:tex->m_blitColor[bindIdx]] endEncoding];
if (depth && tex->m_depthBindTex[bindIdx])
[[m_cmdBuf renderCommandEncoderWithDescriptor:tex->m_blitDepth[bindIdx]] endEncoding];
}
else
{
SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height));
NSUInteger y = tlOrigin ? intersectRect.location[1] : int(tex->m_height) -
intersectRect.location[1] - intersectRect.size[1];
MTLOrigin origin = {NSUInteger(intersectRect.location[0]), y, 0};
id<MTLBlitCommandEncoder> blitEnc = [m_cmdBuf blitCommandEncoder];
if (color && tex->m_colorBindTex[bindIdx])
{
[blitEnc copyFromTexture:tex->m_colorTex
sourceSlice:0
sourceLevel:0
sourceOrigin:origin
sourceSize:MTLSizeMake(intersectRect.size[0], intersectRect.size[1], 1)
toTexture:tex->m_colorBindTex[bindIdx]
destinationSlice:0
destinationLevel:0
destinationOrigin:origin];
}
if (depth && tex->m_depthBindTex[bindIdx])
{
[blitEnc copyFromTexture:tex->m_depthTex
sourceSlice:0
sourceLevel:0
sourceOrigin:origin
sourceSize:MTLSizeMake(intersectRect.size[0], intersectRect.size[1], 1)
toTexture:tex->m_depthBindTex[bindIdx]
destinationSlice:0
destinationLevel:0
destinationOrigin:origin];
}
[blitEnc endEncoding];
}
}
void resolveBindTexture(const ObjToken<ITextureR>& texture, const SWindowRect& rect, bool tlOrigin, void resolveBindTexture(const ObjToken<ITextureR>& texture, const SWindowRect& rect, bool tlOrigin,
int bindIdx, bool color, bool depth, bool clearDepth) int bindIdx, bool color, bool depth, bool clearDepth)
{ {
@ -1220,49 +1377,7 @@ struct MetalCommandQueue : IGraphicsCommandQueue
{ {
[m_enc endEncoding]; [m_enc endEncoding];
if (tex->samples() > 1) _resolveBindTexture(tex, rect, tlOrigin, bindIdx, color, depth);
{
if (color && tex->m_colorBindTex[bindIdx])
[[m_cmdBuf renderCommandEncoderWithDescriptor:tex->m_blitColor[bindIdx]] endEncoding];
if (depth && tex->m_depthBindTex[bindIdx])
[[m_cmdBuf renderCommandEncoderWithDescriptor:tex->m_blitDepth[bindIdx]] endEncoding];
}
else
{
SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height));
NSUInteger y = tlOrigin ? intersectRect.location[1] : int(tex->m_height) -
intersectRect.location[1] - intersectRect.size[1];
MTLOrigin origin = {NSUInteger(intersectRect.location[0]), y, 0};
id<MTLBlitCommandEncoder> blitEnc = [m_cmdBuf blitCommandEncoder];
if (color && tex->m_colorBindTex[bindIdx])
{
[blitEnc copyFromTexture:tex->m_colorTex
sourceSlice:0
sourceLevel:0
sourceOrigin:origin
sourceSize:MTLSizeMake(intersectRect.size[0], intersectRect.size[1], 1)
toTexture:tex->m_colorBindTex[bindIdx]
destinationSlice:0
destinationLevel:0
destinationOrigin:origin];
}
if (depth && tex->m_depthBindTex[bindIdx])
{
[blitEnc copyFromTexture:tex->m_depthTex
sourceSlice:0
sourceLevel:0
sourceOrigin:origin
sourceSize:MTLSizeMake(intersectRect.size[0], intersectRect.size[1], 1)
toTexture:tex->m_depthBindTex[bindIdx]
destinationSlice:0
destinationLevel:0
destinationOrigin:origin];
}
[blitEnc endEncoding];
}
m_enc = [m_cmdBuf renderCommandEncoderWithDescriptor:clearDepth ? tex->m_clearDepthPassDesc : tex->m_passDesc]; m_enc = [m_cmdBuf renderCommandEncoderWithDescriptor:clearDepth ? tex->m_clearDepthPassDesc : tex->m_passDesc];
[m_enc setFrontFacingWinding:MTLWindingCounterClockwise]; [m_enc setFrontFacingWinding:MTLWindingCounterClockwise];
@ -1282,6 +1397,7 @@ struct MetalCommandQueue : IGraphicsCommandQueue
bool m_inProgress = false; bool m_inProgress = false;
std::unordered_map<uintptr_t, MTLRenderPassDescriptor*> m_resolvePasses; std::unordered_map<uintptr_t, MTLRenderPassDescriptor*> m_resolvePasses;
std::unordered_map<uintptr_t, MTLRenderPassDescriptor*> m_gammaPasses;
void execute() void execute()
{ {
if (!m_running) if (!m_running)
@ -1357,34 +1473,61 @@ struct MetalCommandQueue : IGraphicsCommandQueue
if (src->m_colorTex.width == dest.width && if (src->m_colorTex.width == dest.width &&
src->m_colorTex.height == dest.height) src->m_colorTex.height == dest.height)
{ {
if (src->samples() > 1) if (gfxF->m_gamma != 1.f)
{ {
uintptr_t key = uintptr_t(src->m_colorTex) ^ uintptr_t(dest); SWindowRect rect(0, 0, src->m_width, src->m_height);
auto passSearch = m_resolvePasses.find(key); _resolveBindTexture(src, rect, true, 0, true, false);
if (passSearch == m_resolvePasses.end())
uintptr_t key = uintptr_t(dest);
auto passSearch = m_gammaPasses.find(key);
if (passSearch == m_gammaPasses.end())
{ {
MTLRenderPassDescriptor* desc = [MTLRenderPassDescriptor renderPassDescriptor]; MTLRenderPassDescriptor* desc = [MTLRenderPassDescriptor renderPassDescriptor];
desc.colorAttachments[0].texture = src->m_colorTex; desc.colorAttachments[0].texture = dest;
desc.colorAttachments[0].loadAction = MTLLoadActionLoad; desc.colorAttachments[0].loadAction = MTLLoadActionLoad;
desc.colorAttachments[0].storeAction = MTLStoreActionMultisampleResolve; desc.colorAttachments[0].storeAction = MTLStoreActionStore;
desc.colorAttachments[0].resolveTexture = dest; passSearch = m_gammaPasses.insert(std::make_pair(key, desc)).first;
passSearch = m_resolvePasses.insert(std::make_pair(key, desc)).first;
} }
[[m_cmdBuf renderCommandEncoderWithDescriptor:passSearch->second] endEncoding];
id<MTLRenderCommandEncoder> enc = [m_cmdBuf renderCommandEncoderWithDescriptor:passSearch->second];
MetalShaderDataBinding* gammaBinding = gfxF->m_gammaBinding.cast<MetalShaderDataBinding>();
gammaBinding->m_texs[0].tex = m_needsDisplay.get();
gammaBinding->bind(enc, m_drawBuf);
[enc drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
gammaBinding->m_texs[0].tex.reset();
[enc endEncoding];
} }
else else
{ {
id<MTLBlitCommandEncoder> blitEnc = [m_cmdBuf blitCommandEncoder]; if (src->samples() > 1)
[blitEnc copyFromTexture:src->m_colorTex {
sourceSlice:0 uintptr_t key = uintptr_t(src->m_colorTex) ^ uintptr_t(dest);
sourceLevel:0 auto passSearch = m_resolvePasses.find(key);
sourceOrigin:MTLOriginMake(0, 0, 0) if (passSearch == m_resolvePasses.end())
sourceSize:MTLSizeMake(dest.width, dest.height, 1) {
toTexture:dest MTLRenderPassDescriptor* desc = [MTLRenderPassDescriptor renderPassDescriptor];
destinationSlice:0 desc.colorAttachments[0].texture = src->m_colorTex;
destinationLevel:0 desc.colorAttachments[0].loadAction = MTLLoadActionLoad;
destinationOrigin:MTLOriginMake(0, 0, 0)]; desc.colorAttachments[0].storeAction = MTLStoreActionMultisampleResolve;
[blitEnc endEncoding]; desc.colorAttachments[0].resolveTexture = dest;
passSearch = m_resolvePasses.insert(std::make_pair(key, desc)).first;
}
[[m_cmdBuf renderCommandEncoderWithDescriptor:passSearch->second] endEncoding];
}
else
{
id<MTLBlitCommandEncoder> blitEnc = [m_cmdBuf blitCommandEncoder];
[blitEnc copyFromTexture:src->m_colorTex
sourceSlice:0
sourceLevel:0
sourceOrigin:MTLOriginMake(0, 0, 0)
sourceSize:MTLSizeMake(dest.width, dest.height, 1)
toTexture:dest
destinationSlice:0
destinationLevel:0
destinationOrigin:MTLOriginMake(0, 0, 0)];
[blitEnc endEncoding];
}
} }
[m_cmdBuf presentDrawable:drawable]; [m_cmdBuf presentDrawable:drawable];
} }

View File

@ -266,6 +266,7 @@ public:
CVDisplayLinkSetOutputCallback(m_dispLink, (CVDisplayLinkOutputCallback)DLCallback, this); CVDisplayLinkSetOutputCallback(m_dispLink, (CVDisplayLinkOutputCallback)DLCallback, this);
CVDisplayLinkStart(m_dispLink); CVDisplayLinkStart(m_dispLink);
m_commandQueue = _NewGLCommandQueue(this, m_glCtx); m_commandQueue = _NewGLCommandQueue(this, m_glCtx);
m_commandQueue->startRenderer();
return true; return true;
} }
@ -351,6 +352,8 @@ IGraphicsContext* _GraphicsContextCocoaGLNew(IGraphicsContext::EGraphicsAPI api,
major = glVersion[0] - '0'; major = glVersion[0] - '0';
minor = glVersion[2] - '0'; minor = glVersion[2] - '0';
} }
if (glewInit() != GLEW_OK)
Log.report(logvisor::Fatal, "glewInit failed");
[NSOpenGLContext clearCurrentContext]; [NSOpenGLContext clearCurrentContext];
if (!glVersion) if (!glVersion)
return NULL; return NULL;
@ -428,6 +431,7 @@ public:
CVDisplayLinkSetOutputCallback(m_dispLink, (CVDisplayLinkOutputCallback)DLCallback, this); CVDisplayLinkSetOutputCallback(m_dispLink, (CVDisplayLinkOutputCallback)DLCallback, this);
CVDisplayLinkStart(m_dispLink); CVDisplayLinkStart(m_dispLink);
m_commandQueue = _NewMetalCommandQueue(m_metalCtx, m_parentWindow, this); m_commandQueue = _NewMetalCommandQueue(m_metalCtx, m_parentWindow, this);
m_commandQueue->startRenderer();
return true; return true;
} }
@ -1244,7 +1248,6 @@ static boo::ESpecialKey translateKeycode(short code)
CAMetalLayer* layer = [CAMetalLayer new]; CAMetalLayer* layer = [CAMetalLayer new];
layer.device = m_ctx->m_dev; layer.device = m_ctx->m_dev;
layer.pixelFormat = m_ctx->m_pixelFormat; layer.pixelFormat = m_ctx->m_pixelFormat;
layer.colorspace = CGColorSpaceCreateDeviceRGB();
layer.framebufferOnly = NO; layer.framebufferOnly = NO;
return layer; return layer;
} }

View File

@ -463,7 +463,7 @@ struct TestApplicationCallback : IApplicationCallback
"using namespace metal;\n" "using namespace metal;\n"
"struct VertToFrag {float4 out_pos [[ position ]]; float2 out_uv;};\n" "struct VertToFrag {float4 out_pos [[ position ]]; float2 out_uv;};\n"
"fragment float4 fmain(VertToFrag d [[ stage_in ]],\n" "fragment float4 fmain(VertToFrag d [[ stage_in ]],\n"
" sampler samp [[ sampler(0) ]],\n" " sampler samp [[ sampler(2) ]],\n"
" texture2d<float> tex [[ texture(0) ]])\n" " texture2d<float> tex [[ texture(0) ]])\n"
"{\n" "{\n"
" return tex.sample(samp, d.out_uv);\n" " return tex.sample(samp, d.out_uv);\n"