From 3d987b6dc9e2c06072a5a09832e85bbd4279c2a4 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Mon, 15 Jan 2018 20:29:43 -1000 Subject: [PATCH] Add deep color arg to ApplicationRun --- include/boo/IApplication.hpp | 4 +++- include/boo/IGraphicsContext.hpp | 7 ++++--- include/boo/graphicsdev/GL.hpp | 1 + lib/graphicsdev/GL.cpp | 26 +++++++++++++++----------- lib/graphicsdev/Metal.mm | 6 +++--- lib/mac/ApplicationCocoa.mm | 9 +++++++-- lib/mac/CocoaCommon.hpp | 1 + lib/mac/WindowCocoa.mm | 21 ++++++++++++++++++--- lib/win/ApplicationWin32.cpp | 1 + lib/x11/ApplicationUnix.cpp | 1 + test/main.cpp | 16 +++++++++++++--- 11 files changed, 67 insertions(+), 26 deletions(-) diff --git a/include/boo/IApplication.hpp b/include/boo/IApplication.hpp index 4e95e2d..e12ba52 100644 --- a/include/boo/IApplication.hpp +++ b/include/boo/IApplication.hpp @@ -64,6 +64,7 @@ ApplicationRun(IApplication::EPlatformType platform, std::string_view gfxApi = {}, uint32_t samples = 1, uint32_t anisotropy = 1, + bool deepColor = false, bool singleInstance=true); extern IApplication* APP; @@ -76,6 +77,7 @@ ApplicationRun(IApplication::EPlatformType platform, std::string_view gfxApi = {}, uint32_t samples = 1, uint32_t anisotropy = 1, + bool deepColor = false, bool singleInstance=true) { if (APP) @@ -84,7 +86,7 @@ ApplicationRun(IApplication::EPlatformType platform, for (int i=1 ; i size_t m_width = 0; size_t m_height = 0; size_t m_samples = 0; + GLenum m_colorFormat; size_t m_colorBindCount; size_t m_depthBindCount; - GLTextureR(const ObjToken& parent, GLCommandQueue* q, size_t width, size_t height, size_t samples, - TextureClampMode clampMode, size_t colorBindCount, size_t depthBindCount); + GLTextureR(const ObjToken& parent, GLCommandQueue* q, size_t width, size_t height, + size_t samples, GLenum colorFormat, TextureClampMode clampMode, + size_t colorBindCount, size_t depthBindCount); public: ~GLTextureR() { @@ -486,14 +488,14 @@ public: if (m_samples > 1) { glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]); - glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_RGBA, width, height, GL_FALSE); + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, m_colorFormat, width, height, GL_FALSE); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_DEPTH_COMPONENT24, width, height, GL_FALSE); } else { glBindTexture(GL_TEXTURE_2D, m_texs[0]); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); + glTexImage2D(GL_TEXTURE_2D, 0, m_colorFormat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); glBindTexture(GL_TEXTURE_2D, m_texs[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr); @@ -507,7 +509,7 @@ public: if (m_bindTexs[0][i]) { glBindTexture(GL_TEXTURE_2D, m_bindTexs[0][i]); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); + glTexImage2D(GL_TEXTURE_2D, 0, m_colorFormat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); } } @@ -1639,9 +1641,10 @@ GLDataFactory::Context::newDynamicTexture(size_t width, size_t height, TextureFo } GLTextureR::GLTextureR(const ObjToken& parent, GLCommandQueue* q, size_t width, size_t height, - size_t samples, TextureClampMode clampMode, size_t colorBindingCount, size_t depthBindingCount) + size_t samples, GLenum colorFormat, TextureClampMode clampMode, + size_t colorBindingCount, size_t depthBindingCount) : GraphicsDataNode(parent), m_q(q), m_width(width), m_height(height), m_samples(samples), - m_colorBindCount(colorBindingCount), m_depthBindCount(depthBindingCount) + m_colorFormat(colorFormat), m_colorBindCount(colorBindingCount), m_depthBindCount(depthBindingCount) { glGenTextures(2, m_texs); if (colorBindingCount) @@ -1660,14 +1663,14 @@ GLTextureR::GLTextureR(const ObjToken& parent, GLCommandQueue* if (samples > 1) { glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]); - glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, width, height, GL_FALSE); + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, colorFormat, width, height, GL_FALSE); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_DEPTH_COMPONENT24, width, height, GL_FALSE); } else { glBindTexture(GL_TEXTURE_2D, m_texs[0]); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); + glTexImage2D(GL_TEXTURE_2D, 0, colorFormat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); glBindTexture(GL_TEXTURE_2D, m_texs[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr); } @@ -1675,7 +1678,7 @@ GLTextureR::GLTextureR(const ObjToken& parent, GLCommandQueue* for (int i=0 ; i(m_parent); GLCommandQueue* q = static_cast(factory.m_parent->getCommandQueue()); - ObjToken retval(new GLTextureR(m_data, q, width, height, factory.m_glCtx->m_sampleCount, clampMode, + ObjToken retval(new GLTextureR(m_data, q, width, height, factory.m_glCtx->m_sampleCount, + factory.m_glCtx->m_deepColor ? GL_RGBA16 : GL_RGBA8, clampMode, colorBindingCount, depthBindingCount)); q->resizeRenderTexture(retval, width, height); return retval; diff --git a/lib/graphicsdev/Metal.mm b/lib/graphicsdev/Metal.mm index cb6eddc..7864340 100644 --- a/lib/graphicsdev/Metal.mm +++ b/lib/graphicsdev/Metal.mm @@ -490,7 +490,7 @@ class MetalTextureR : public GraphicsDataNode @autoreleasepool { MTLTextureDescriptor* desc = - [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm + [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:ctx->m_pixelFormat width:m_width height:m_height mipmapped:NO]; desc.storageMode = MTLStorageModePrivate; @@ -521,7 +521,7 @@ class MetalTextureR : public GraphicsDataNode desc.usage = MTLTextureUsageShaderRead; if (m_colorBindCount) { - desc.pixelFormat = MTLPixelFormatBGRA8Unorm; + desc.pixelFormat = ctx->m_pixelFormat; for (int i=0 ; im_dev newTextureWithDescriptor:desc]; @@ -799,7 +799,7 @@ class MetalShaderPipeline : public GraphicsDataNode desc.fragmentFunction = m_frag.get().m_shader; desc.vertexDescriptor = vtxFmt.cast()->m_vdesc; desc.sampleCount = targetSamples; - desc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm; + desc.colorAttachments[0].pixelFormat = ctx->m_pixelFormat; desc.colorAttachments[0].writeMask = (colorWrite ? COLOR_WRITE_MASK : 0) | (alphaWrite ? MTLColorWriteMaskAlpha : 0); desc.colorAttachments[0].blendingEnabled = dstFac != BlendFactor::Zero; diff --git a/lib/mac/ApplicationCocoa.mm b/lib/mac/ApplicationCocoa.mm index 758ae44..3cba24e 100644 --- a/lib/mac/ApplicationCocoa.mm +++ b/lib/mac/ApplicationCocoa.mm @@ -67,7 +67,8 @@ public: const std::vector& args, std::string_view gfxApi, uint32_t samples, - uint32_t anisotropy) + uint32_t anisotropy, + bool deepColor) : m_callback(callback), m_uniqueName(uniqueName), m_friendlyName(friendlyName), @@ -76,8 +77,10 @@ public: { m_metalCtx.m_sampleCount = samples; m_metalCtx.m_anisotropy = anisotropy; + m_metalCtx.m_pixelFormat = deepColor ? MTLPixelFormatRGBA16Float : MTLPixelFormatBGRA8Unorm; m_glCtx.m_sampleCount = samples; m_glCtx.m_anisotropy = anisotropy; + m_glCtx.m_deepColor = deepColor; [[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyRegular]; @@ -228,6 +231,7 @@ int ApplicationRun(IApplication::EPlatformType platform, std::string_view gfxApi, uint32_t samples, uint32_t anisotropy, + bool deepColor, bool singleInstance) { std::string thrName = std::string(friendlyName) + " Main Thread"; @@ -240,7 +244,8 @@ int ApplicationRun(IApplication::EPlatformType platform, platform != IApplication::EPlatformType::Auto) return 1; /* Never deallocated to ensure window destructors have access */ - APP = new ApplicationCocoa(cb, uniqueName, friendlyName, pname, args, gfxApi, samples, anisotropy); + APP = new ApplicationCocoa(cb, uniqueName, friendlyName, pname, args, + gfxApi, samples, anisotropy, deepColor); } [NSApp run]; ApplicationCocoa* appCocoa = static_cast(APP); diff --git a/lib/mac/CocoaCommon.hpp b/lib/mac/CocoaCommon.hpp index e62a143..9338f13 100644 --- a/lib/mac/CocoaCommon.hpp +++ b/lib/mac/CocoaCommon.hpp @@ -30,6 +30,7 @@ struct MetalContext std::unordered_map m_windows; uint32_t m_sampleCount = 1; uint32_t m_anisotropy = 1; + MTLPixelFormat m_pixelFormat = MTLPixelFormatBGRA8Unorm; }; } diff --git a/lib/mac/WindowCocoa.mm b/lib/mac/WindowCocoa.mm index e4d460c..81dbcea 100644 --- a/lib/mac/WindowCocoa.mm +++ b/lib/mac/WindowCocoa.mm @@ -42,6 +42,16 @@ static const NSOpenGLPixelFormatAttribute PF_RGBA8_ATTRS[] = 0, 0 }; +static const NSOpenGLPixelFormatAttribute PF_RGBA16_ATTRS[] = +{ + NSOpenGLPFAAccelerated, + NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core, + NSOpenGLPFADoubleBuffer, + NSOpenGLPFAColorSize, 48, + NSOpenGLPFAAlphaSize, 16, + 0, 0 +}; + static const NSOpenGLPixelFormatAttribute PF_RGBA8_Z24_ATTRS[] = { NSOpenGLPFAAccelerated, @@ -80,6 +90,7 @@ static const NSOpenGLPixelFormatAttribute* PF_TABLE[] = { NULL, PF_RGBA8_ATTRS, + PF_RGBA16_ATTRS, PF_RGBA8_Z24_ATTRS, PF_RGBAF32_ATTRS, PF_RGBAF32_Z24_ATTRS @@ -207,7 +218,8 @@ public: GraphicsContextCocoaGL(EGraphicsAPI api, IWindow* parentWindow, NSOpenGLContext* lastGLCtx, GLContext* glCtx) - : GraphicsContextCocoa(api, EPixelFormat::RGBA8, parentWindow), + : GraphicsContextCocoa(api, glCtx->m_deepColor ? + EPixelFormat::RGBA16 : EPixelFormat::RGBA8, parentWindow), m_lastCtx(lastGLCtx), m_glCtx(glCtx) { m_dataFactory = _NewGLDataFactory(this, glCtx); @@ -365,7 +377,9 @@ public: MetalContext* m_metalCtx; GraphicsContextCocoaMetal(EGraphicsAPI api, IWindow* parentWindow, MetalContext* metalCtx) - : GraphicsContextCocoa(api, EPixelFormat::RGBA8, parentWindow), + : GraphicsContextCocoa(api, + (metalCtx->m_pixelFormat == MTLPixelFormatRGBA16Float) ? + EPixelFormat::RGBA16 : EPixelFormat::RGBA8, parentWindow), m_parentWindow(parentWindow), m_metalCtx(metalCtx) { m_dataFactory = _NewMetalDataFactory(this, metalCtx); @@ -1229,7 +1243,8 @@ static boo::ESpecialKey translateKeycode(short code) { CAMetalLayer* layer = [CAMetalLayer new]; layer.device = m_ctx->m_dev; - layer.pixelFormat = MTLPixelFormatBGRA8Unorm; + layer.pixelFormat = m_ctx->m_pixelFormat; + layer.colorspace = CGColorSpaceCreateDeviceRGB(); layer.framebufferOnly = NO; return layer; } diff --git a/lib/win/ApplicationWin32.cpp b/lib/win/ApplicationWin32.cpp index bc5fd81..66890a1 100644 --- a/lib/win/ApplicationWin32.cpp +++ b/lib/win/ApplicationWin32.cpp @@ -569,6 +569,7 @@ int ApplicationRun(IApplication::EPlatformType platform, std::string_view gfxApi, uint32_t samples, uint32_t anisotropy, + bool deepColor, bool singleInstance) { std::string thrName = WCSTMBS(friendlyName.data()) + " Main Thread"; diff --git a/lib/x11/ApplicationUnix.cpp b/lib/x11/ApplicationUnix.cpp index 8d7008a..1b30312 100644 --- a/lib/x11/ApplicationUnix.cpp +++ b/lib/x11/ApplicationUnix.cpp @@ -61,6 +61,7 @@ int ApplicationRun(IApplication::EPlatformType platform, std::string_view gfxApi, uint32_t samples, uint32_t anisotropy, + bool deepColor, bool singleInstance) { std::string thrName = std::string(friendlyName) + " Main Thread"; diff --git a/test/main.cpp b/test/main.cpp index 9e6a1be..b87674b 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -302,12 +302,21 @@ struct TestApplicationCallback : IApplicationCallback float pos[3]; float uv[2]; }; + /* static const Vert quad[4] = { {{0.5,0.5},{1.0,1.0}}, {{-0.5,0.5},{0.0,1.0}}, {{0.5,-0.5},{1.0,0.0}}, {{-0.5,-0.5},{0.0,0.0}} + }; + */ + static const Vert quad[4] = + { + {{1.0,1.0},{1.0,1.0}}, + {{-1.0,1.0},{0.0,1.0}}, + {{1.0,-1.0},{1.0,0.0}}, + {{-1.0,-1.0},{0.0,0.0}} }; auto vbo = ctx.newStaticBuffer(BufferUse::Vertex, quad, sizeof(Vert), 4); @@ -361,7 +370,8 @@ struct TestApplicationCallback : IApplicationCallback "in vec2 out_uv;\n" "void main()\n" "{\n" - " out_frag = texture(tex, out_uv);\n" + " //out_frag = texture(tex, out_uv);\n" + " out_frag = vec4(out_uv.xy, 0.0, 1.0);\n" "}\n"; static const char* texName = "tex"; @@ -541,7 +551,7 @@ struct TestApplicationCallback : IApplicationCallback gfxQ->setViewport(r); gfxQ->setScissor(r); float rgba[] = {std::max(0.f, sinf(frameIdx / 60.0)), std::max(0.f, cosf(frameIdx / 60.0)), 0.0, 1.0}; - gfxQ->setClearColor(rgba); + //gfxQ->setClearColor(rgba); gfxQ->clearTarget(); gfxQ->setShaderDataBinding(m_binding); @@ -596,7 +606,7 @@ int main(int argc, const boo::SystemChar** argv) logvisor::RegisterConsoleLogger(); boo::TestApplicationCallback appCb; int ret = ApplicationRun(boo::IApplication::EPlatformType::Auto, - appCb, _S("boo"), _S("boo"), argc, argv); + appCb, _S("boo"), _S("boo"), argc, argv, {}, 1, 1, false); printf("IM DYING!!\n"); return ret; }