mirror of https://github.com/AxioDL/boo.git
macOS fixes
This commit is contained in:
parent
93c787dcd4
commit
5f48359cc7
|
@ -1,4 +1,5 @@
|
|||
#include "Common.hpp"
|
||||
#include <cmath>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
|
|
@ -1321,8 +1321,6 @@ struct GLCommandQueue : IGraphicsCommandQueue
|
|||
{
|
||||
std::unique_lock<std::mutex> lk(self->m_initmt);
|
||||
self->m_parent->makeCurrent();
|
||||
//if (glewInit() != GLEW_OK)
|
||||
// Log.report(logvisor::Fatal, "unable to init glew");
|
||||
const GLubyte* version = glGetString(GL_VERSION);
|
||||
Log.report(logvisor::Info, "OpenGL Version: %s", version);
|
||||
self->m_parent->postInit();
|
||||
|
|
|
@ -17,6 +17,50 @@
|
|||
#define MAX_UNIFORM_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
|
||||
{
|
||||
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;
|
||||
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:
|
||||
std::unordered_map<uint64_t, uint64_t> m_sourceToBinary;
|
||||
char m_libfile[MAXPATHLEN];
|
||||
|
@ -201,6 +282,16 @@ public:
|
|||
}
|
||||
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
|
||||
|
@ -296,6 +387,11 @@ class MetalTextureS : public GraphicsDataNode<ITextureS>
|
|||
ppitchNum = 1;
|
||||
bytesPerRow = width * ppitchNum;
|
||||
break;
|
||||
case TextureFormat::I16:
|
||||
pfmt = MTLPixelFormatR16Unorm;
|
||||
ppitchNum = 2;
|
||||
bytesPerRow = width * ppitchNum;
|
||||
break;
|
||||
case TextureFormat::DXT1:
|
||||
pfmt = MTLPixelFormatBC1_RGBA;
|
||||
ppitchNum = 1;
|
||||
|
@ -352,6 +448,10 @@ class MetalTextureSA : public GraphicsDataNode<ITextureSA>
|
|||
pfmt = MTLPixelFormatR8Unorm;
|
||||
ppitch = 1;
|
||||
break;
|
||||
case TextureFormat::I16:
|
||||
pfmt = MTLPixelFormatR16Unorm;
|
||||
ppitch = 2;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
@ -417,6 +517,10 @@ class MetalTextureD : public GraphicsDataNode<ITextureD>
|
|||
format = MTLPixelFormatR8Unorm;
|
||||
m_pxPitch = 1;
|
||||
break;
|
||||
case TextureFormat::I16:
|
||||
format = MTLPixelFormatR16Unorm;
|
||||
m_pxPitch = 2;
|
||||
break;
|
||||
default:
|
||||
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()
|
||||
{
|
||||
m_running = false;
|
||||
|
@ -1212,14 +1321,9 @@ struct MetalCommandQueue : IGraphicsCommandQueue
|
|||
baseInstance:m_boundData->m_baseInst];
|
||||
}
|
||||
|
||||
void resolveBindTexture(const ObjToken<ITextureR>& texture, const SWindowRect& rect, bool tlOrigin,
|
||||
int bindIdx, bool color, bool depth, bool clearDepth)
|
||||
void _resolveBindTexture(MetalTextureR* tex, const SWindowRect& rect, bool tlOrigin,
|
||||
int bindIdx, bool color, bool depth)
|
||||
{
|
||||
MetalTextureR* tex = texture.cast<MetalTextureR>();
|
||||
@autoreleasepool
|
||||
{
|
||||
[m_enc endEncoding];
|
||||
|
||||
if (tex->samples() > 1)
|
||||
{
|
||||
if (color && tex->m_colorBindTex[bindIdx])
|
||||
|
@ -1263,6 +1367,17 @@ struct MetalCommandQueue : IGraphicsCommandQueue
|
|||
|
||||
[blitEnc endEncoding];
|
||||
}
|
||||
}
|
||||
|
||||
void resolveBindTexture(const ObjToken<ITextureR>& texture, const SWindowRect& rect, bool tlOrigin,
|
||||
int bindIdx, bool color, bool depth, bool clearDepth)
|
||||
{
|
||||
MetalTextureR* tex = texture.cast<MetalTextureR>();
|
||||
@autoreleasepool
|
||||
{
|
||||
[m_enc endEncoding];
|
||||
|
||||
_resolveBindTexture(tex, rect, tlOrigin, bindIdx, color, depth);
|
||||
|
||||
m_enc = [m_cmdBuf renderCommandEncoderWithDescriptor:clearDepth ? tex->m_clearDepthPassDesc : tex->m_passDesc];
|
||||
[m_enc setFrontFacingWinding:MTLWindingCounterClockwise];
|
||||
|
@ -1282,6 +1397,7 @@ struct MetalCommandQueue : IGraphicsCommandQueue
|
|||
|
||||
bool m_inProgress = false;
|
||||
std::unordered_map<uintptr_t, MTLRenderPassDescriptor*> m_resolvePasses;
|
||||
std::unordered_map<uintptr_t, MTLRenderPassDescriptor*> m_gammaPasses;
|
||||
void execute()
|
||||
{
|
||||
if (!m_running)
|
||||
|
@ -1356,6 +1472,32 @@ struct MetalCommandQueue : IGraphicsCommandQueue
|
|||
id<MTLTexture> dest = drawable.texture;
|
||||
if (src->m_colorTex.width == dest.width &&
|
||||
src->m_colorTex.height == dest.height)
|
||||
{
|
||||
if (gfxF->m_gamma != 1.f)
|
||||
{
|
||||
SWindowRect rect(0, 0, src->m_width, src->m_height);
|
||||
_resolveBindTexture(src, rect, true, 0, true, false);
|
||||
|
||||
uintptr_t key = uintptr_t(dest);
|
||||
auto passSearch = m_gammaPasses.find(key);
|
||||
if (passSearch == m_gammaPasses.end())
|
||||
{
|
||||
MTLRenderPassDescriptor* desc = [MTLRenderPassDescriptor renderPassDescriptor];
|
||||
desc.colorAttachments[0].texture = dest;
|
||||
desc.colorAttachments[0].loadAction = MTLLoadActionLoad;
|
||||
desc.colorAttachments[0].storeAction = MTLStoreActionStore;
|
||||
passSearch = m_gammaPasses.insert(std::make_pair(key, desc)).first;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
if (src->samples() > 1)
|
||||
{
|
||||
|
@ -1386,6 +1528,7 @@ struct MetalCommandQueue : IGraphicsCommandQueue
|
|||
destinationOrigin:MTLOriginMake(0, 0, 0)];
|
||||
[blitEnc endEncoding];
|
||||
}
|
||||
}
|
||||
[m_cmdBuf presentDrawable:drawable];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -266,6 +266,7 @@ public:
|
|||
CVDisplayLinkSetOutputCallback(m_dispLink, (CVDisplayLinkOutputCallback)DLCallback, this);
|
||||
CVDisplayLinkStart(m_dispLink);
|
||||
m_commandQueue = _NewGLCommandQueue(this, m_glCtx);
|
||||
m_commandQueue->startRenderer();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -351,6 +352,8 @@ IGraphicsContext* _GraphicsContextCocoaGLNew(IGraphicsContext::EGraphicsAPI api,
|
|||
major = glVersion[0] - '0';
|
||||
minor = glVersion[2] - '0';
|
||||
}
|
||||
if (glewInit() != GLEW_OK)
|
||||
Log.report(logvisor::Fatal, "glewInit failed");
|
||||
[NSOpenGLContext clearCurrentContext];
|
||||
if (!glVersion)
|
||||
return NULL;
|
||||
|
@ -428,6 +431,7 @@ public:
|
|||
CVDisplayLinkSetOutputCallback(m_dispLink, (CVDisplayLinkOutputCallback)DLCallback, this);
|
||||
CVDisplayLinkStart(m_dispLink);
|
||||
m_commandQueue = _NewMetalCommandQueue(m_metalCtx, m_parentWindow, this);
|
||||
m_commandQueue->startRenderer();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -1244,7 +1248,6 @@ static boo::ESpecialKey translateKeycode(short code)
|
|||
CAMetalLayer* layer = [CAMetalLayer new];
|
||||
layer.device = m_ctx->m_dev;
|
||||
layer.pixelFormat = m_ctx->m_pixelFormat;
|
||||
layer.colorspace = CGColorSpaceCreateDeviceRGB();
|
||||
layer.framebufferOnly = NO;
|
||||
return layer;
|
||||
}
|
||||
|
|
|
@ -463,7 +463,7 @@ struct TestApplicationCallback : IApplicationCallback
|
|||
"using namespace metal;\n"
|
||||
"struct VertToFrag {float4 out_pos [[ position ]]; float2 out_uv;};\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"
|
||||
"{\n"
|
||||
" return tex.sample(samp, d.out_uv);\n"
|
||||
|
|
Loading…
Reference in New Issue