From 2537efad37109067c08a58fcfa8821f921d54461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 1 Oct 2015 20:26:27 -0700 Subject: [PATCH] Metal: Cleanup. --- scripts/genie.lua | 1 + src/renderer_mtl.h | 56 +++++++++++++++++++++++++++++++-------------- src/renderer_mtl.mm | 40 ++++++++++++++++++++------------ 3 files changed, 66 insertions(+), 31 deletions(-) diff --git a/scripts/genie.lua b/scripts/genie.lua index 90beb31a4..65e3ca4cc 100644 --- a/scripts/genie.lua +++ b/scripts/genie.lua @@ -315,6 +315,7 @@ function exampleProject(_name) linkoptions { "-framework CoreFoundation", "-framework Foundation", + "-framework Metal", "-framework OpenGLES", "-framework UIKit", "-framework QuartzCore", diff --git a/src/renderer_mtl.h b/src/renderer_mtl.h index 1a55c398c..88d7dc8da 100644 --- a/src/renderer_mtl.h +++ b/src/renderer_mtl.h @@ -46,14 +46,37 @@ namespace bgfx { namespace mtl id renderCommandEncoderWithDescriptor( MTLRenderPassDescriptor *_renderPassDescriptor){ return [m_obj renderCommandEncoderWithDescriptor:_renderPassDescriptor]; } - id computeCommandEncoder() { return [m_obj computeCommandEncoder]; } + + id computeCommandEncoder() + { + return [m_obj computeCommandEncoder]; + } // Scheduling and Executing Commands - void enqueue() { [m_obj enqueue]; } - void commit() { [m_obj commit]; } - void addCompletedHandler(mtlCallback _cb, void* _data) { [m_obj addCompletedHandler:^(id cmdb){ _cb(_data); }]; } - void presentDrawable(id _drawable) { [m_obj presentDrawable:_drawable]; } - void waitUntilCompleted() { [m_obj waitUntilCompleted]; } + void enqueue() + { + [m_obj enqueue]; + } + + void commit() + { + [m_obj commit]; + } + + void addCompletedHandler(mtlCallback _cb, void* _data) + { + [m_obj addCompletedHandler:^(id ){ _cb(_data); }]; + } + + void presentDrawable(id _drawable) + { + [m_obj presentDrawable:_drawable]; + } + + void waitUntilCompleted() + { + [m_obj waitUntilCompleted]; + } MTL_CLASS_END MTL_CLASS(CommandQueue) @@ -365,14 +388,14 @@ namespace bgfx { namespace mtl ProgramMtl() : m_vsh(NULL) , m_fsh(NULL) - , m_processedUniforms(false) , m_vshConstantBuffer(NULL) , m_fshConstantBuffer(NULL) - , m_numPredefined(0) , m_vshConstantBufferSize(0) , m_vshConstantBufferAlignmentMask(0) , m_fshConstantBufferSize(0) , m_fshConstantBufferAlignmentMask(0) + , m_numPredefined(0) + , m_processedUniforms(false) { } @@ -383,22 +406,22 @@ namespace bgfx { namespace mtl StateCacheT m_renderPipelineStateCache; - uint8_t m_used[Attrib::Count+1]; // dense + uint8_t m_used[Attrib::Count+1]; // dense uint32_t m_attributes[Attrib::Count]; // sparse uint32_t m_instanceData[BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT+1]; const ShaderMtl* m_vsh; const ShaderMtl* m_fsh; + UniformBuffer* m_vshConstantBuffer; + UniformBuffer* m_fshConstantBuffer; - bool m_processedUniforms; uint32_t m_vshConstantBufferSize; uint32_t m_vshConstantBufferAlignmentMask; uint32_t m_fshConstantBufferSize; uint32_t m_fshConstantBufferAlignmentMask; - UniformBuffer* m_fshConstantBuffer; - UniformBuffer* m_vshConstantBuffer; PredefinedUniform m_predefined[PredefinedUniform::Count*2]; uint8_t m_numPredefined; + bool m_processedUniforms; }; struct TextureMtl @@ -407,8 +430,8 @@ namespace bgfx { namespace mtl : m_ptr(NULL) , m_ptrStencil(NULL) , m_sampler(NULL) - , m_numMips(0) , m_flags(0) + , m_numMips(0) { } @@ -433,10 +456,9 @@ namespace bgfx { namespace mtl struct FrameBufferMtl { FrameBufferMtl() - : //m_swapChain(NULL) //TODO: swapchain - m_denseIdx(UINT16_MAX) - , m_num(0) + : m_denseIdx(UINT16_MAX) , m_pixelFormatHash(0) + , m_num(0) { m_depthHandle.idx = invalidHandle; } @@ -455,7 +477,7 @@ namespace bgfx { namespace mtl TextureHandle m_colorHandle[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS-1]; TextureHandle m_depthHandle; - uint8_t m_num; // number of color handles + uint8_t m_num; // number of color handles }; diff --git a/src/renderer_mtl.mm b/src/renderer_mtl.mm index 3dee97f13..3889433f8 100644 --- a/src/renderer_mtl.mm +++ b/src/renderer_mtl.mm @@ -327,12 +327,12 @@ namespace bgfx { namespace mtl struct RendererContextMtl : public RendererContextI { RendererContextMtl() - : m_numWindows(1), - m_metalLayer(NULL), - m_drawable(NULL), - m_maxAnisotropy(1), - m_rtMsaa(false), - m_backBufferPixelFormatHash(0) + : m_metalLayer(NULL) + , m_backBufferPixelFormatHash(0) + , m_maxAnisotropy(1) + , m_numWindows(1) + , m_rtMsaa(false) + , m_drawable(NULL) { m_fbh.idx = invalidHandle; } @@ -686,7 +686,7 @@ namespace bgfx { namespace mtl uint32_t length = width*height*4; uint8_t* data = (uint8_t*)BX_ALLOC(g_allocator, length); - MTLRegion region = { 0, 0, 0, width, height, 1}; + MTLRegion region = { { 0, 0, 0 }, { width, height, 1 } }; backBuffer.getBytes(data, 4*width, 0, region, 0, 0); @@ -1715,15 +1715,23 @@ namespace bgfx { namespace mtl void BufferMtl::create(uint32_t _size, void* _data, uint16_t _flags, uint16_t _stride, bool _vertex) { + BX_UNUSED(_flags, _stride, _vertex); + m_size = _size; - if ( NULL == _data ) + if (NULL == _data) + { m_buffer = s_renderMtl->m_device.newBufferWithLength(_size, 0); + } else + { m_buffer = s_renderMtl->m_device.newBufferWithBytes(_data, _size, 0); + } } void BufferMtl::update(uint32_t _offset, uint32_t _size, void* _data, bool _discard) { + BX_UNUSED(_discard); + memcpy( (uint8_t*)m_buffer.contents() + _offset, _data, _size); } @@ -1797,11 +1805,11 @@ namespace bgfx { namespace mtl ); - const bool bufferOnly = 0 != (_flags&BGFX_TEXTURE_RT_BUFFER_ONLY); +// const bool bufferOnly = 0 != (_flags&BGFX_TEXTURE_RT_BUFFER_ONLY); const bool computeWrite = 0 != (_flags&BGFX_TEXTURE_COMPUTE_WRITE); - const bool renderTarget = 0 != (_flags&BGFX_TEXTURE_RT_MASK); +// const bool renderTarget = 0 != (_flags&BGFX_TEXTURE_RT_MASK); const bool srgb = 0 != (_flags&BGFX_TEXTURE_SRGB) || imageContainer.m_srgb; - const uint32_t msaaQuality = bx::uint32_satsub( (_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT, 1); +// const uint32_t msaaQuality = bx::uint32_satsub( (_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT, 1); // const DXGI_SAMPLE_DESC& msaa = s_msaa[msaaQuality]; MTLPixelFormat format = MTLPixelFormatInvalid; @@ -1868,7 +1876,7 @@ namespace bgfx { namespace mtl data = temp; } - MTLRegion region = { 0, 0, 0, width, height, depth}; + MTLRegion region = { { 0, 0, 0 }, { width, height, depth } }; uint32_t bytesPerRow; uint32_t bytesPerImage; @@ -1914,7 +1922,7 @@ namespace bgfx { namespace mtl void TextureMtl::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem) { - MTLRegion region = { _rect.m_x, _rect.m_y, _z, _rect.m_width, _rect.m_height, _depth}; + MTLRegion region = { { _rect.m_x, _rect.m_y, _z }, { _rect.m_width, _rect.m_height, _depth } }; const uint32_t bpp = getBitsPerPixel(TextureFormat::Enum(m_textureFormat) ); const uint32_t rectpitch = _rect.m_width*bpp/8; @@ -1990,6 +1998,8 @@ namespace bgfx { namespace mtl void FrameBufferMtl::create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat) { + BX_UNUSED(_denseIdx, _nwh, _width, _height, _depthFormat); + BX_WARN(false, "FrameBufferMtl::create not yet implemented"); } @@ -2012,6 +2022,8 @@ namespace bgfx { namespace mtl void RendererContextMtl::submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter) BX_OVERRIDE { + BX_UNUSED(_clearQuad); + m_commandBuffer = m_commandQueue.commandBuffer(); retain(m_commandBuffer); // keep alive to be useable at 'flip' @@ -2638,7 +2650,7 @@ namespace bgfx { namespace mtl static uint32_t maxGpuLatency = 0; static double maxGpuElapsed = 0.0f; - double elapsedGpuMs = 0.0; +// double elapsedGpuMs = 0.0; //TODO: gputimer /* m_gpuTimer.end();