diff --git a/src/renderer_mtl.h b/src/renderer_mtl.h index 0ee1468ab..41ba78725 100644 --- a/src/renderer_mtl.h +++ b/src/renderer_mtl.h @@ -12,16 +12,17 @@ #import #import + #ifdef BX_PLATFORM_IOS -#import -#endif +# import +#endif // BX_PLATFORM_IOS namespace bgfx { namespace mtl { //c++ wrapper // objects with creation functions starting with 'new' has a refcount 1 after creation, object must be destroyed with release. // commandBuffer, commandEncoders are autoreleased objects. Needs AutoreleasePool! - + #define MTL_CLASS(name) \ class name \ { \ @@ -29,23 +30,24 @@ namespace bgfx { namespace mtl name(id _obj = nil) : m_obj(_obj) {} \ operator id () const { return m_obj; } \ id m_obj; + #define MTL_CLASS_END }; - + typedef void (*mtlCallback)(void* userData); - + //TODO: ??MTLBlitCommandEncoder?? MTL_CLASS(Buffer) void* contents() { return m_obj.contents; } uint32_t length() { return (uint32_t)m_obj.length; } MTL_CLASS_END - + MTL_CLASS(CommandBuffer) // Creating Command Encoders id renderCommandEncoderWithDescriptor( MTLRenderPassDescriptor *_renderPassDescriptor){ return [m_obj renderCommandEncoderWithDescriptor:_renderPassDescriptor]; } id computeCommandEncoder() { return [m_obj computeCommandEncoder]; } - + // Scheduling and Executing Commands void enqueue() { [m_obj enqueue]; } void commit() { [m_obj commit]; } @@ -53,27 +55,27 @@ namespace bgfx { namespace mtl void presentDrawable(id _drawable) { [m_obj presentDrawable:_drawable]; } void waitUntilCompleted() { [m_obj waitUntilCompleted]; } MTL_CLASS_END - + MTL_CLASS(CommandQueue) id commandBuffer() { return [m_obj commandBuffer]; } id commandBufferWithUnretainedReferences() { return [m_obj commandBufferWithUnretainedReferences]; } MTL_CLASS_END - + MTL_CLASS(ComputeCommandEncoder) void setComputePipelineState(id _state) { [m_obj setComputePipelineState:_state]; } - + void setBuffer(id _buffer, NSUInteger _offset, NSUInteger _index) { [m_obj setBuffer:_buffer offset:_offset atIndex:_index]; } - + void setTexture(id _texture, NSUInteger _index) { [m_obj setTexture:_texture atIndex:_index]; } - + void setSamplerState(id _sampler, NSUInteger _index) { [m_obj setSamplerState:_sampler atIndex:_index]; } - + void endEncoding() {[m_obj endEncoding] ;} MTL_CLASS_END - + MTL_CLASS(Device) bool supportsFeatureSet(MTLFeatureSet _featureSet) { return [m_obj supportsFeatureSet:_featureSet]; } - + id newLibraryWithData(const void* _data) { NSError* error; id lib = [m_obj newLibraryWithData:(dispatch_data_t)_data error:&error]; @@ -88,20 +90,20 @@ namespace bgfx { namespace mtl BX_WARN(error==NULL,"Shader compilation failed:%s", [error.localizedDescription UTF8String]); return lib; } - + id newCommandQueue() { return [m_obj newCommandQueue]; } id newCommandQueueWithMaxCommandBufferCount(NSUInteger _maxCommandBufferCount) { return [m_obj newCommandQueueWithMaxCommandBufferCount:_maxCommandBufferCount]; } - + // Creating Resources id newBufferWithLength(unsigned int _length, MTLResourceOptions _options) { return [m_obj newBufferWithLength:_length options:_options ]; } id newBufferWithBytes(const void *_pointer, NSUInteger _length, MTLResourceOptions _options) { return [m_obj newBufferWithBytes:_pointer length:_length options:_options]; } - + id newTextureWithDescriptor(MTLTextureDescriptor * _descriptor) { return [m_obj newTextureWithDescriptor:_descriptor]; } id newSamplerStateWithDescriptor(MTLSamplerDescriptor * _descriptor) { return [m_obj newSamplerStateWithDescriptor:_descriptor]; } - + // Creating Command Objects Needed to Render Graphics id newDepthStencilStateWithDescriptor(MTLDepthStencilDescriptor * _descriptor) { return [m_obj newDepthStencilStateWithDescriptor:_descriptor]; } - + id newRenderPipelineStateWithDescriptor(MTLRenderPipelineDescriptor *_descriptor) { NSError *error; @@ -109,7 +111,7 @@ namespace bgfx { namespace mtl BX_WARN(error==NULL,"newRenderPipelineStateWithDescriptor failed:%s", [error.localizedDescription UTF8String] ); return state; } - + id newRenderPipelineStateWithDescriptor(MTLRenderPipelineDescriptor *_descriptor, MTLPipelineOption _options, MTLRenderPipelineReflection** _reflection) { NSError *error; @@ -118,7 +120,7 @@ namespace bgfx { namespace mtl BX_WARN(error==NULL,"newRenderPipelineStateWithDescriptor failed:%s", [error.localizedDescription UTF8String] ); return state; } - + // Creating Command Objects Needed to Perform Computational Tasks id newComputePipelineStateWithFunction(id _computeFunction) { @@ -128,15 +130,15 @@ namespace bgfx { namespace mtl return state; } MTL_CLASS_END - + MTL_CLASS(Function) NSArray *vertexAttributes() { return m_obj.vertexAttributes; } MTL_CLASS_END - + MTL_CLASS(Library) id newFunctionWithName(const char* _functionName) { return [m_obj newFunctionWithName:@(_functionName)]; } MTL_CLASS_END - + MTL_CLASS(RenderCommandEncoder) // Setting Graphics Rendering State void setBlendColor(float _red, float _green, float _blue, float _alpha) { [m_obj setBlendColorRed:_red green:_green blue:_blue alpha:_alpha]; } @@ -150,41 +152,41 @@ namespace bgfx { namespace mtl void setTriangleFillMode(MTLTriangleFillMode _fillMode) { [m_obj setTriangleFillMode:_fillMode]; } void setViewport(MTLViewport _viewport) { [m_obj setViewport:_viewport]; } void setVisibilityResultMode(MTLVisibilityResultMode _mode, NSUInteger _offset) { [m_obj setVisibilityResultMode:_mode offset:_offset]; } - + // Specifying Resources for a Vertex Function void setVertexBuffer(id _buffer, NSUInteger _offset, NSUInteger _index) { [m_obj setVertexBuffer:_buffer offset:_offset atIndex:_index]; } void setVertexSamplerState(id _sampler, NSUInteger _index) { [m_obj setVertexSamplerState:_sampler atIndex:_index]; } void setVertexTexture(id _texture, NSUInteger _index) { [m_obj setVertexTexture:_texture atIndex:_index]; } - + // Specifying Resources for a Fragment Function void setFragmentBuffer(id _buffer, NSUInteger _offset, NSUInteger _index) { [m_obj setFragmentBuffer:_buffer offset:_offset atIndex:_index]; } void setFragmentSamplerState(id _sampler, NSUInteger _index) { [m_obj setFragmentSamplerState:_sampler atIndex:_index]; } void setFragmentTexture(id _texture, NSUInteger _index) { [m_obj setFragmentTexture:_texture atIndex:_index]; } - + //Drawing Geometric Primitives //NOTE: not exposing functions without instanceCount, it seems they are just wrappers void drawIndexedPrimitives(MTLPrimitiveType _primitiveType, NSUInteger _indexCount, MTLIndexType _indexType, id _indexBuffer, NSUInteger _indexBufferOffset, NSUInteger _instanceCount) { [m_obj drawIndexedPrimitives:_primitiveType indexCount:_indexCount indexType:_indexType indexBuffer:_indexBuffer indexBufferOffset:_indexBufferOffset instanceCount:_instanceCount]; } - + void drawPrimitives(MTLPrimitiveType _primitiveType, NSUInteger _vertexStart, NSUInteger _vertexCount, NSUInteger _instanceCount) { [m_obj drawPrimitives:_primitiveType vertexStart:_vertexStart vertexCount:_vertexCount instanceCount:_instanceCount];} void insertDebugSignpost(const char* _string) { [m_obj insertDebugSignpost:@(_string)]; } void pushDebugGroup(const char* _string) { [m_obj pushDebugGroup:@(_string)]; } void popDebugGroup() { [m_obj popDebugGroup]; } - + void endEncoding() { [m_obj endEncoding]; } MTL_CLASS_END - + MTL_CLASS(Texture) // Copying Data into a Texture Image void replaceRegion(MTLRegion _region, NSUInteger _level, NSUInteger _slice, const void *_pixelBytes, NSUInteger _bytesPerRow, NSUInteger _bytesPerImage) { [m_obj replaceRegion:_region mipmapLevel:_level slice:_slice withBytes:_pixelBytes bytesPerRow:_bytesPerRow bytesPerImage:_bytesPerImage]; } - + // Copying Data from a Texture Image void getBytes(void *_pixelBytes, NSUInteger _bytesPerRow, NSUInteger _bytesPerImage, MTLRegion _region, NSUInteger _mipmapLevel, NSUInteger _slice) { [m_obj getBytes:_pixelBytes bytesPerRow:_bytesPerRow bytesPerImage:_bytesPerImage fromRegion:_region mipmapLevel:_mipmapLevel slice:_slice]; } - + // Creating Textures by Reusing Image Data id newTextureViewWithPixelFormat(MTLPixelFormat _pixelFormat) { return [m_obj newTextureViewWithPixelFormat:_pixelFormat]; } //properties @@ -192,30 +194,30 @@ namespace bgfx { namespace mtl uint32_t height() { return (uint32_t)m_obj.height; } MTLPixelFormat pixelFormat() const { return m_obj.pixelFormat; } MTL_CLASS_END - + typedef id ComputePipelineState; typedef id DepthStencilState; typedef id RenderPipelineState; typedef id SamplerState; - + //descriptors //NOTE: [class new] is same as [[class alloc] init] typedef MTLRenderPipelineDescriptor* RenderPipelineDescriptor; RenderPipelineDescriptor newRenderPipelineDescriptor() { return [MTLRenderPipelineDescriptor new]; } void reset(RenderPipelineDescriptor _desc) { [_desc reset]; }; - + typedef MTLRenderPipelineColorAttachmentDescriptor* RenderPipelineColorAttachmentDescriptor; - + typedef MTLDepthStencilDescriptor* DepthStencilDescriptor; MTLDepthStencilDescriptor* newDepthStencilDescriptor() { return [MTLDepthStencilDescriptor new]; } typedef MTLStencilDescriptor* StencilDescriptor; MTLStencilDescriptor* newStencilDescriptor() { return [MTLStencilDescriptor new]; } - + typedef MTLRenderPassColorAttachmentDescriptor* RenderPassColorAttachmentDescriptor; typedef MTLRenderPassDepthAttachmentDescriptor* RenderPassDepthAttachmentDescriptor; typedef MTLRenderPassStencilAttachmentDescriptor* RenderPassStencilAttachmentDescriptor; - + typedef MTLRenderPassDescriptor* RenderPassDescriptor; MTLRenderPassDescriptor* newRenderPassDescriptor() { return [MTLRenderPassDescriptor new]; } @@ -228,7 +230,7 @@ namespace bgfx { namespace mtl typedef MTLTextureDescriptor* TextureDescriptor; MTLTextureDescriptor* newTextureDescriptor() { return [MTLTextureDescriptor new]; } - + typedef MTLRenderPipelineReflection* RenderPipelineReflection; //helper functions @@ -236,17 +238,16 @@ namespace bgfx { namespace mtl void retain(NSObject* _obj) { [_obj retain]; } const char* utf8String(NSString* _str) { return [_str UTF8String]; } - #define MTL_RELEASE(_obj) \ [_obj release]; \ _obj = nil; - + #ifdef BX_PLATFORM_IOS bool OsVersionEqualOrGreater(const char* _version) { return ([[[UIDevice currentDevice] systemVersion] compare:@(_version) options:NSNumericSearch] != NSOrderedAscending); } //TODO: this could be in bx ? #endif // end of c++ wrapper - + template class StateCacheT { @@ -256,7 +257,7 @@ namespace bgfx { namespace mtl invalidate(_id); m_hashMap.insert(stl::make_pair(_id, _item) ); } - + Ty find(uint64_t _id) { typename HashMap::iterator it = m_hashMap.find(_id); @@ -264,54 +265,52 @@ namespace bgfx { namespace mtl { return it->second; } - + return NULL; } - + void invalidate(uint64_t _id) { typename HashMap::iterator it = m_hashMap.find(_id); if (it != m_hashMap.end() ) { - //DX_RELEASE_WARNONLY(it->second, 0); MTL_RELEASE(it->second); m_hashMap.erase(it); } } - + void invalidate() { for (typename HashMap::iterator it = m_hashMap.begin(), itEnd = m_hashMap.end(); it != itEnd; ++it) { - //DX_CHECK_REFCOUNT(it->second, 1); release(it->second); } - + m_hashMap.clear(); } - + uint32_t getCount() const { return uint32_t(m_hashMap.size() ); } - + private: typedef stl::unordered_map HashMap; HashMap m_hashMap; }; - + struct BufferMtl { BufferMtl() - : m_buffer(NULL) - , m_flags(BGFX_BUFFER_NONE) - , m_dynamic(false) + : m_buffer(NULL) + , m_flags(BGFX_BUFFER_NONE) + , m_dynamic(false) { } - + void create(uint32_t _size, void* _data, uint16_t _flags, uint16_t _stride = 0, bool _vertex = false); void update(uint32_t _offset, uint32_t _size, void* _data, bool _discard = false); - + void destroy() { if (NULL != m_buffer) @@ -321,76 +320,76 @@ namespace bgfx { namespace mtl m_dynamic = false; } } - + Buffer m_buffer; uint32_t m_size; uint16_t m_flags; bool m_dynamic; }; - + typedef BufferMtl IndexBufferMtl; - + struct VertexBufferMtl : public BufferMtl { VertexBufferMtl() - : BufferMtl() + : BufferMtl() { } - + void create(uint32_t _size, void* _data, VertexDeclHandle _declHandle, uint16_t _flags); - + VertexDeclHandle m_decl; }; - - + + struct ShaderMtl { ShaderMtl() - : m_function(NULL) + : m_function(NULL) { } - + void create(const Memory* _mem); void destroy() { MTL_RELEASE(m_function); - + } - + Function m_function; }; - - + + struct ProgramMtl { 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_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) { } - + void create(const ShaderMtl* _vsh, const ShaderMtl* _fsh); void destroy(); - + RenderPipelineState getRenderPipelineState(uint64_t _state, uint32_t _rgba, FrameBufferHandle _fbHandle, VertexDeclHandle _declHandle, uint16_t _numInstanceData); - + StateCacheT m_renderPipelineStateCache; 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; - + bool m_processedUniforms; uint32_t m_vshConstantBufferSize; uint32_t m_vshConstantBufferAlignmentMask; @@ -401,18 +400,18 @@ namespace bgfx { namespace mtl PredefinedUniform m_predefined[PredefinedUniform::Count*2]; uint8_t m_numPredefined; }; - + struct TextureMtl { TextureMtl() - : m_ptr(NULL) - , m_ptrStencil(NULL) - , m_sampler(NULL) - , m_numMips(0) - , m_flags(0) + : m_ptr(NULL) + , m_ptrStencil(NULL) + , m_sampler(NULL) + , m_numMips(0) + , m_flags(0) { } - + void create(const Memory* _mem, uint32_t _flags, uint8_t _skip); void destroy() { @@ -421,7 +420,7 @@ namespace bgfx { namespace mtl } void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem); void commit(uint8_t _stage, uint32_t _flags = BGFX_SAMPLER_DEFAULT_FLAGS); - + Texture m_ptr; Texture m_ptrStencil; // for emulating packed depth/stencil formats - only for iOS8... SamplerState m_sampler; @@ -430,39 +429,38 @@ namespace bgfx { namespace mtl uint8_t m_textureFormat; uint8_t m_numMips; }; - + struct FrameBufferMtl { FrameBufferMtl() - : //m_swapChain(NULL) //TODO: swapchain - m_denseIdx(UINT16_MAX) - , m_num(0) - , m_pixelFormatHash(0) + : //m_swapChain(NULL) //TODO: swapchain + m_denseIdx(UINT16_MAX) + , m_num(0) + , m_pixelFormatHash(0) { m_depthHandle.idx = invalidHandle; } - + void create(uint8_t _num, const TextureHandle* _handles); void create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat); void postReset(); uint16_t destroy(); - + // SwapChainMtl* m_swapChain; uint32_t m_width; uint32_t m_height; uint16_t m_denseIdx; - + uint32_t m_pixelFormatHash; TextureHandle m_colorHandle[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS-1]; TextureHandle m_depthHandle; uint8_t m_num; // number of color handles }; - - + + #endif - + } /* namespace metal */ } // namespace bgfx #endif // BGFX_RENDERER_METAL_H_HEADER_GUARD - diff --git a/src/renderer_mtl.mm b/src/renderer_mtl.mm index 8597e0e97..fb4eaa835 100644 --- a/src/renderer_mtl.mm +++ b/src/renderer_mtl.mm @@ -5,41 +5,12 @@ #include "bgfx_p.h" - -/* -Known issues / TODOs: -- 15-shadowmaps-simple (modified shaderc and example needs modification too, mtxCrop znew = z * 0.5 + 0.5 is not needed ) could be hacked in shader too -- 19-oit ( hacked shaderc to support MRT output) -- 21-deferred ( hacked shaderc to support MRT output and fs_deferred_light needed modification for metal (similar to BGFX_SHADER_LANGUAGE_HLSL ) - -07-callback, saveScreenshot should be implemented with one frame latency (using saveScreenshotBegin and End) - -16-shadowmaps, //problem with essl -> metal: SAMPLER2D(u_shadowMap0, 4); sampler index is lost. Shadowmap is set to slot 4, but - metal shader uses sampler/texture slot 0. this could require changes outside of renderer_mtl? - Otherwise it works with hacking the slot. - -24-nbody - cannot generate compute shaders for metal - -20-nanonvg - TODO: remove sampler/texture hack - -- caps - -- optimization... - -create binary shader representation - - 13-stencil and 16-shadowmaps are very inefficient. every view stores/loads backbuffer data - - BGFX_RESET_FLIP_AFTER_RENDER on low level renderers should be true? (crashes even with BGFX_RESET_FLIP_AFTER_RENDER because there is - one rendering frame before reset). Do I have absolutely need to send result to View at flip or can I do it in submit? - - */ - #if BGFX_CONFIG_RENDERER_METAL #include "renderer_mtl.h" #include "renderer.h" #include "bx/bx.h" + #if BX_PLATFORM_OSX # include #endif @@ -48,10 +19,39 @@ create binary shader representation #define UNIFORM_BUFFER_SIZE (1024*1024) +/* +Known issues / TODOs: +- 15-shadowmaps-simple (modified shaderc and example needs modification too, mtxCrop znew = z * 0.5 + 0.5 is not needed ) could be hacked in shader too +- 19-oit ( hacked shaderc to support MRT output) +- 21-deferred ( hacked shaderc to support MRT output and fs_deferred_light needed modification for metal (similar to BGFX_SHADER_LANGUAGE_HLSL ) + +07-callback, saveScreenshot should be implemented with one frame latency (using saveScreenshotBegin and End) + +16-shadowmaps, //problem with essl -> metal: SAMPLER2D(u_shadowMap0, 4); sampler index is lost. Shadowmap is set to slot 4, but + metal shader uses sampler/texture slot 0. this could require changes outside of renderer_mtl? + Otherwise it works with hacking the slot. + +24-nbody - cannot generate compute shaders for metal + +20-nanonvg - TODO: remove sampler/texture hack + +- caps + +- optimization... + +create binary shader representation + + 13-stencil and 16-shadowmaps are very inefficient. every view stores/loads backbuffer data + + BGFX_RESET_FLIP_AFTER_RENDER on low level renderers should be true? (crashes even with BGFX_RESET_FLIP_AFTER_RENDER because there is + one rendering frame before reset). Do I have absolutely need to send result to View at flip or can I do it in submit? + + */ + namespace bgfx { namespace mtl { static char s_viewName[BGFX_CONFIG_MAX_VIEWS][BGFX_CONFIG_MAX_VIEW_NAME]; - + struct PrimInfo { MTLPrimitiveType m_type; @@ -59,16 +59,16 @@ namespace bgfx { namespace mtl uint32_t m_div; uint32_t m_sub; }; - + static const PrimInfo s_primInfo[] = { - { MTLPrimitiveTypeTriangle, 3, 3, 0 }, - { MTLPrimitiveTypeTriangleStrip, 3, 1, 2 }, - { MTLPrimitiveTypeLine, 2, 2, 0 }, - { MTLPrimitiveTypeLineStrip, 2, 1, 1 }, - { MTLPrimitiveTypePoint, 1, 1, 0 }, + { MTLPrimitiveTypeTriangle, 3, 3, 0 }, + { MTLPrimitiveTypeTriangleStrip, 3, 1, 2 }, + { MTLPrimitiveTypeLine, 2, 2, 0 }, + { MTLPrimitiveTypeLineStrip, 2, 1, 1 }, + { MTLPrimitiveTypePoint, 1, 1, 0 }, }; - + static const char* s_primName[] = { "TriList", @@ -78,7 +78,7 @@ namespace bgfx { namespace mtl "Point", }; BX_STATIC_ASSERT(BX_COUNTOF(s_primInfo) == BX_COUNTOF(s_primName)); - + static const char* s_attribName[] = { "a_position", @@ -99,7 +99,7 @@ namespace bgfx { namespace mtl "a_texcoord7", }; BX_STATIC_ASSERT(Attrib::Count == BX_COUNTOF(s_attribName) ); - + static const char* s_instanceDataName[] = { "i_data0", @@ -109,7 +109,7 @@ namespace bgfx { namespace mtl "i_data4", }; BX_STATIC_ASSERT(BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT == BX_COUNTOF(s_instanceDataName) ); - + static const MTLVertexFormat s_attribType[][4][2] = //type, count, normalized { // Uint8 @@ -121,14 +121,14 @@ namespace bgfx { namespace mtl }, //Uint10 - //TODO: normalized only + //TODO: normalized only { { MTLVertexFormatInvalid, MTLVertexFormatUInt1010102Normalized }, { MTLVertexFormatInvalid, MTLVertexFormatUInt1010102Normalized }, { MTLVertexFormatInvalid, MTLVertexFormatUInt1010102Normalized }, { MTLVertexFormatInvalid, MTLVertexFormatUInt1010102Normalized } }, - + //Int16 { { MTLVertexFormatShort2, MTLVertexFormatShort2Normalized }, @@ -136,7 +136,7 @@ namespace bgfx { namespace mtl { MTLVertexFormatShort3, MTLVertexFormatShort3Normalized }, { MTLVertexFormatShort4, MTLVertexFormatShort4Normalized } }, - + //Half { { MTLVertexFormatHalf2, MTLVertexFormatHalf2 }, @@ -147,14 +147,14 @@ namespace bgfx { namespace mtl //Float { - { MTLVertexFormatFloat, MTLVertexFormatFloat }, + { MTLVertexFormatFloat, MTLVertexFormatFloat }, { MTLVertexFormatFloat2, MTLVertexFormatFloat2 }, { MTLVertexFormatFloat3, MTLVertexFormatFloat3 }, { MTLVertexFormatFloat4, MTLVertexFormatFloat4 } }, }; BX_STATIC_ASSERT(AttribType::Count == BX_COUNTOF(s_attribType) ); - + static const MTLCullMode s_cullMode[] = { MTLCullModeNone, @@ -162,25 +162,25 @@ namespace bgfx { namespace mtl MTLCullModeBack, MTLCullModeNone }; - + static const MTLBlendFactor s_blendFactor[][2] = { - { (MTLBlendFactor)0, (MTLBlendFactor)0 }, // ignored - { MTLBlendFactorZero, MTLBlendFactorZero }, // ZERO - { MTLBlendFactorOne, MTLBlendFactorOne }, // ONE - { MTLBlendFactorSourceColor, MTLBlendFactorSourceAlpha }, // SRC_COLOR - { MTLBlendFactorOneMinusSourceColor, MTLBlendFactorOneMinusSourceAlpha }, // INV_SRC_COLOR - { MTLBlendFactorSourceAlpha, MTLBlendFactorSourceAlpha }, // SRC_ALPHA - { MTLBlendFactorOneMinusSourceAlpha, MTLBlendFactorOneMinusSourceAlpha }, // INV_SRC_ALPHA - { MTLBlendFactorDestinationAlpha, MTLBlendFactorDestinationAlpha }, // DST_ALPHA - { MTLBlendFactorOneMinusDestinationAlpha, MTLBlendFactorOneMinusDestinationAlpha }, // INV_DST_ALPHA - { MTLBlendFactorDestinationColor, MTLBlendFactorDestinationAlpha }, // DST_COLOR - { MTLBlendFactorOneMinusDestinationColor, MTLBlendFactorOneMinusDestinationAlpha }, // INV_DST_COLOR - { MTLBlendFactorSourceAlphaSaturated, MTLBlendFactorOne }, // SRC_ALPHA_SAT - { MTLBlendFactorBlendColor, MTLBlendFactorBlendColor }, // FACTOR - { MTLBlendFactorOneMinusBlendColor, MTLBlendFactorOneMinusBlendColor }, // INV_FACTOR + { (MTLBlendFactor)0, (MTLBlendFactor)0 }, // ignored + { MTLBlendFactorZero, MTLBlendFactorZero }, // ZERO + { MTLBlendFactorOne, MTLBlendFactorOne }, // ONE + { MTLBlendFactorSourceColor, MTLBlendFactorSourceAlpha }, // SRC_COLOR + { MTLBlendFactorOneMinusSourceColor, MTLBlendFactorOneMinusSourceAlpha }, // INV_SRC_COLOR + { MTLBlendFactorSourceAlpha, MTLBlendFactorSourceAlpha }, // SRC_ALPHA + { MTLBlendFactorOneMinusSourceAlpha, MTLBlendFactorOneMinusSourceAlpha }, // INV_SRC_ALPHA + { MTLBlendFactorDestinationAlpha, MTLBlendFactorDestinationAlpha }, // DST_ALPHA + { MTLBlendFactorOneMinusDestinationAlpha, MTLBlendFactorOneMinusDestinationAlpha }, // INV_DST_ALPHA + { MTLBlendFactorDestinationColor, MTLBlendFactorDestinationAlpha }, // DST_COLOR + { MTLBlendFactorOneMinusDestinationColor, MTLBlendFactorOneMinusDestinationAlpha }, // INV_DST_COLOR + { MTLBlendFactorSourceAlphaSaturated, MTLBlendFactorOne }, // SRC_ALPHA_SAT + { MTLBlendFactorBlendColor, MTLBlendFactorBlendColor }, // FACTOR + { MTLBlendFactorOneMinusBlendColor, MTLBlendFactorOneMinusBlendColor }, // INV_FACTOR }; - + static const MTLBlendOperation s_blendEquation[] = { MTLBlendOperationAdd, @@ -189,10 +189,10 @@ namespace bgfx { namespace mtl MTLBlendOperationMin, MTLBlendOperationMax, }; - + static const MTLCompareFunction s_cmpFunc[] = { - MTLCompareFunctionAlways, //TODO: depth disable? + MTLCompareFunctionAlways, //TODO: depth disable? MTLCompareFunctionLess, MTLCompareFunctionLessEqual, MTLCompareFunctionEqual, @@ -202,7 +202,7 @@ namespace bgfx { namespace mtl MTLCompareFunctionNever, MTLCompareFunctionAlways }; - + static const MTLStencilOperation s_stencilOp[] = { MTLStencilOperationZero, @@ -214,14 +214,14 @@ namespace bgfx { namespace mtl MTLStencilOperationDecrementClamp, MTLStencilOperationInvert }; - + static const MTLSamplerAddressMode s_textureAddress[] = { MTLSamplerAddressModeRepeat, MTLSamplerAddressModeMirrorRepeat, MTLSamplerAddressModeClampToEdge, }; - + static const MTLSamplerMinMagFilter s_textureFilterMinMag[] = { MTLSamplerMinMagFilterLinear, @@ -234,76 +234,72 @@ namespace bgfx { namespace mtl MTLSamplerMipFilterLinear, MTLSamplerMipFilterNearest, }; - + struct TextureFormatInfo { MTLPixelFormat m_fmt; MTLPixelFormat m_fmtSrgb; }; - - //TODO: add new ios/osx formats - //TODO: add caps for format support + + //TODO: add new ios/osx formats + //TODO: add caps for format support static TextureFormatInfo s_textureFormat[] = { - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC1 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC2 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC3 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC4 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC5 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC6H - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC7 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ETC1 - { MTLPixelFormatETC2_RGB8, MTLPixelFormatETC2_RGB8_sRGB }, // ETC2 - { MTLPixelFormatEAC_RGBA8, MTLPixelFormatEAC_RGBA8_sRGB }, // ETC2A - { MTLPixelFormatETC2_RGB8A1, MTLPixelFormatETC2_RGB8A1_sRGB }, // ETC2A1 - { MTLPixelFormatPVRTC_RGB_2BPP, MTLPixelFormatPVRTC_RGB_2BPP_sRGB }, // PTC12 - { MTLPixelFormatPVRTC_RGB_4BPP, MTLPixelFormatPVRTC_RGB_4BPP_sRGB }, // PTC14 - { MTLPixelFormatPVRTC_RGBA_2BPP, MTLPixelFormatPVRTC_RGBA_2BPP_sRGB}, // PTC12A - { MTLPixelFormatPVRTC_RGBA_4BPP, MTLPixelFormatPVRTC_RGBA_4BPP_sRGB}, // PTC14A - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // PTC22 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // PTC24 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // Unknown - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // R1 - { MTLPixelFormatR8Unorm, MTLPixelFormatR8Unorm_sRGB }, // R8 -//ASK: R8 uses unorm R16 uint is this ok? From d3d11 - { MTLPixelFormatR16Uint, MTLPixelFormatInvalid }, // R16 - { MTLPixelFormatR16Float, MTLPixelFormatInvalid }, // R16F - { MTLPixelFormatR32Uint, MTLPixelFormatInvalid }, // R32 - { MTLPixelFormatR32Float, MTLPixelFormatInvalid }, // R32F - { MTLPixelFormatRG8Unorm, MTLPixelFormatRG8Unorm_sRGB }, // RG8 - { MTLPixelFormatRG16Unorm, MTLPixelFormatInvalid }, // RG16 - { MTLPixelFormatRG16Float, MTLPixelFormatInvalid }, // RG16F - { MTLPixelFormatRG32Uint, MTLPixelFormatInvalid }, // RG32 - { MTLPixelFormatRG32Float, MTLPixelFormatInvalid }, // RG32F - { MTLPixelFormatBGRA8Unorm, MTLPixelFormatBGRA8Unorm_sRGB }, // BGRA8 - { MTLPixelFormatRGBA8Unorm, MTLPixelFormatRGBA8Unorm_sRGB }, // RGBA8 - { MTLPixelFormatRGBA16Unorm, MTLPixelFormatInvalid }, // RGBA16 - { MTLPixelFormatRGBA16Float, MTLPixelFormatInvalid }, // RGBA16F - { MTLPixelFormatRGBA32Uint, MTLPixelFormatInvalid }, // RGBA32 - { MTLPixelFormatRGBA32Float, MTLPixelFormatInvalid }, // RGBA32F - //only have abgr (not rgba) - { MTLPixelFormatB5G6R5Unorm, MTLPixelFormatInvalid }, // R5G6B5 - { MTLPixelFormatABGR4Unorm, MTLPixelFormatInvalid }, // RGBA4 - { MTLPixelFormatA1BGR5Unorm, MTLPixelFormatInvalid }, // RGB5A1 - { MTLPixelFormatRGB10A2Unorm, MTLPixelFormatInvalid }, // RGB10A2 - { MTLPixelFormatRG11B10Float, MTLPixelFormatInvalid }, // R11G11B10F - - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // UnknownDepth - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D16 - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D24 - { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // D24S8 - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D32 - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D16F - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D24F - { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D32F - { MTLPixelFormatStencil8, MTLPixelFormatInvalid }, // D0S8 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC1 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC2 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC3 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC4 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC5 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC6H + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // BC7 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ETC1 + { MTLPixelFormatETC2_RGB8, MTLPixelFormatETC2_RGB8_sRGB }, // ETC2 + { MTLPixelFormatEAC_RGBA8, MTLPixelFormatEAC_RGBA8_sRGB }, // ETC2A + { MTLPixelFormatETC2_RGB8A1, MTLPixelFormatETC2_RGB8A1_sRGB }, // ETC2A1 + { MTLPixelFormatPVRTC_RGB_2BPP, MTLPixelFormatPVRTC_RGB_2BPP_sRGB }, // PTC12 + { MTLPixelFormatPVRTC_RGB_4BPP, MTLPixelFormatPVRTC_RGB_4BPP_sRGB }, // PTC14 + { MTLPixelFormatPVRTC_RGBA_2BPP, MTLPixelFormatPVRTC_RGBA_2BPP_sRGB }, // PTC12A + { MTLPixelFormatPVRTC_RGBA_4BPP, MTLPixelFormatPVRTC_RGBA_4BPP_sRGB }, // PTC14A + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // PTC22 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // PTC24 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // Unknown + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // R1 + { MTLPixelFormatR8Unorm, MTLPixelFormatR8Unorm_sRGB }, // R8 + { MTLPixelFormatR16Uint, MTLPixelFormatInvalid }, // R16 + { MTLPixelFormatR16Float, MTLPixelFormatInvalid }, // R16F + { MTLPixelFormatR32Uint, MTLPixelFormatInvalid }, // R32 + { MTLPixelFormatR32Float, MTLPixelFormatInvalid }, // R32F + { MTLPixelFormatRG8Unorm, MTLPixelFormatRG8Unorm_sRGB }, // RG8 + { MTLPixelFormatRG16Unorm, MTLPixelFormatInvalid }, // RG16 + { MTLPixelFormatRG16Float, MTLPixelFormatInvalid }, // RG16F + { MTLPixelFormatRG32Uint, MTLPixelFormatInvalid }, // RG32 + { MTLPixelFormatRG32Float, MTLPixelFormatInvalid }, // RG32F + { MTLPixelFormatBGRA8Unorm, MTLPixelFormatBGRA8Unorm_sRGB }, // BGRA8 + { MTLPixelFormatRGBA8Unorm, MTLPixelFormatRGBA8Unorm_sRGB }, // RGBA8 + { MTLPixelFormatRGBA16Unorm, MTLPixelFormatInvalid }, // RGBA16 + { MTLPixelFormatRGBA16Float, MTLPixelFormatInvalid }, // RGBA16F + { MTLPixelFormatRGBA32Uint, MTLPixelFormatInvalid }, // RGBA32 + { MTLPixelFormatRGBA32Float, MTLPixelFormatInvalid }, // RGBA32F + { MTLPixelFormatB5G6R5Unorm, MTLPixelFormatInvalid }, // R5G6B5 + { MTLPixelFormatABGR4Unorm, MTLPixelFormatInvalid }, // RGBA4 + { MTLPixelFormatA1BGR5Unorm, MTLPixelFormatInvalid }, // RGB5A1 + { MTLPixelFormatRGB10A2Unorm, MTLPixelFormatInvalid }, // RGB10A2 + { MTLPixelFormatRG11B10Float, MTLPixelFormatInvalid }, // R11G11B10F + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // UnknownDepth + { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D16 + { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D24 + { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // D24S8 + { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D32 + { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D16F + { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D24F + { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D32F + { MTLPixelFormatStencil8, MTLPixelFormatInvalid }, // D0S8 }; BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormat) ); - - + #define SHADER_FUNCTION_NAME ("xlatMtlMain") #define SHADER_UNIFORM_NAME ("_mtl_u") - + struct RendererContextMtl : public RendererContextI { RendererContextMtl() @@ -316,11 +312,11 @@ namespace bgfx { namespace mtl { m_fbh.idx = invalidHandle; } - + ~RendererContextMtl() { } - + bool init() { if ( NSClassFromString(@"CAMetalLayer") != nil) @@ -337,20 +333,20 @@ namespace bgfx { namespace mtl #elif BX_PLATFORM_OSX // create and set metalLayer NSWindow* nsWindow = (NSWindow*)g_platformData.nwh; - + [nsWindow.contentView setWantsLayer:YES]; m_metalLayer = [CAMetalLayer layer]; [nsWindow.contentView setLayer:m_metalLayer]; -#endif +#endif // BX_PLATFORM_* m_device = (id)g_platformData.context; - + if (NULL == m_device) { m_device = MTLCreateSystemDefaultDevice(); } } - + if (m_device==NULL || m_metalLayer==nil) { BX_WARN(NULL != m_device, "Unable to create Metal device."); @@ -358,10 +354,10 @@ namespace bgfx { namespace mtl } m_metalLayer.device = m_device; - + m_commandQueue = m_device.newCommandQueue(); BGFX_FATAL(NULL != m_commandQueue, Fatal::UnableToInitialize, "Unable to create Metal device."); - + m_renderPipelineDescriptor = newRenderPipelineDescriptor(); m_depthStencilDescriptor = newDepthStencilDescriptor(); m_frontFaceStencilDescriptor = newStencilDescriptor(); @@ -369,78 +365,74 @@ namespace bgfx { namespace mtl m_vertexDescriptor = newVertexDescriptor(); m_textureDescriptor = newTextureDescriptor(); m_samplerDescriptor = newSamplerDescriptor(); - + m_uniformBuffer = m_device.newBufferWithLength(UNIFORM_BUFFER_SIZE, 0); m_uniformBufferVertexOffset = 0; m_uniformBufferFragmentOffset = 0; memset(m_uniforms, 0, sizeof(m_uniforms) ); - + g_caps.supported |= (0 | BGFX_CAPS_TEXTURE_COMPARE_LEQUAL | BGFX_CAPS_TEXTURE_3D | BGFX_CAPS_INSTANCING | BGFX_CAPS_VERTEX_ATTRIB_HALF - //| BGFX_CAPS_FRAGMENT_DEPTH //TODO: is this supported? +// | BGFX_CAPS_FRAGMENT_DEPTH //TODO: is this supported? | BGFX_CAPS_BLEND_INDEPENDENT | BGFX_CAPS_COMPUTE | BGFX_CAPS_INDEX32 | BGFX_CAPS_DRAW_INDIRECT ); - + g_caps.maxTextureSize = 2048; //ASK: real caps width/height: 4096, but max depth(3D) size is only: 2048 //TODO: OSX #if BX_PLATFORM_IOS g_caps.maxFBAttachments = uint8_t(bx::uint32_min(m_device.supportsFeatureSet(MTLFeatureSet_iOS_GPUFamily2_v1) ? 8 :4, BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS)); -#endif - +#endif // BX_PLATFORM_* + //todo: vendor id, device id, gpu enum //todo: texture format caps //add texture formats/caps/etc that are available only on new sdk/devices #if BX_PLATFORM_IOS - #ifdef __IPHONE_8_0 +# ifdef __IPHONE_8_0 if (OsVersionEqualOrGreater("8.0.0")) { s_textureFormat[TextureFormat::D24S8].m_fmt = MTLPixelFormatDepth32Float; } - #endif -#endif +# endif // __IPHONE_8_0 +#endif // BX_PLATFORM_* -#if BX_PLATFORM_OSX - -#endif - for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii) { uint8_t support = (s_textureFormat[ii].m_fmt != MTLPixelFormatInvalid) ? BGFX_CAPS_FORMAT_TEXTURE_COLOR : BGFX_CAPS_FORMAT_TEXTURE_NONE; - + support |= (s_textureFormat[ii].m_fmtSrgb != MTLPixelFormatInvalid) ? BGFX_CAPS_FORMAT_TEXTURE_COLOR_SRGB : BGFX_CAPS_FORMAT_TEXTURE_NONE; //TODO: additional caps flags // support |= BGFX_CAPS_FORMAT_TEXTURE_VERTEX : BGFX_CAPS_FORMAT_TEXTURE_NONE; // support |= BGFX_CAPS_FORMAT_TEXTURE_IMAGE : BGFX_CAPS_FORMAT_TEXTURE_NONE; // support |= BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER : BGFX_CAPS_FORMAT_TEXTURE_NONE; - + g_caps.formats[ii] = support; } - + // Init reserved part of view name. for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii) { bx::snprintf(s_viewName[ii], BGFX_CONFIG_MAX_VIEW_NAME_RESERVED+1, "%3d ", ii); } - + return true; } - + void shutdown() { for (uint32_t ii = 0; ii < BX_COUNTOF(m_shaders); ++ii) { m_shaders[ii].destroy(); } - + for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii) { m_textures[ii].destroy(); @@ -453,60 +445,59 @@ namespace bgfx { namespace mtl MTL_RELEASE(m_vertexDescriptor); MTL_RELEASE(m_textureDescriptor); MTL_RELEASE(m_samplerDescriptor); - + MTL_RELEASE(m_backBufferDepth); #if BX_PLATFORM_IOS MTL_RELEASE(m_backBufferStencil); -#endif +#endif // BX_PLATFORM_* MTL_RELEASE(m_uniformBuffer); MTL_RELEASE(m_commandQueue); MTL_RELEASE(m_device); } - - + RendererType::Enum getRendererType() const BX_OVERRIDE { return RendererType::Metal; } - + const char* getRendererName() const BX_OVERRIDE { return BGFX_RENDERER_METAL_NAME; } - + void createIndexBuffer(IndexBufferHandle _handle, Memory* _mem, uint16_t _flags) BX_OVERRIDE { m_indexBuffers[_handle.idx].create(_mem->size, _mem->data, _flags); } - + void destroyIndexBuffer(IndexBufferHandle _handle) BX_OVERRIDE { m_indexBuffers[_handle.idx].destroy(); } - + void createVertexDecl(VertexDeclHandle _handle, const VertexDecl& _decl) BX_OVERRIDE { VertexDecl& decl = m_vertexDecls[_handle.idx]; memcpy(&decl, &_decl, sizeof(VertexDecl) ); dump(decl); } - + void destroyVertexDecl(VertexDeclHandle /*_handle*/) BX_OVERRIDE { } - + void createVertexBuffer(VertexBufferHandle _handle, Memory* _mem, VertexDeclHandle _declHandle, uint16_t _flags) BX_OVERRIDE { m_vertexBuffers[_handle.idx].create(_mem->size, _mem->data, _declHandle, _flags); } - + void destroyVertexBuffer(VertexBufferHandle _handle) BX_OVERRIDE { m_vertexBuffers[_handle.idx].destroy(); } - + void createDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _size, uint16_t _flags) BX_OVERRIDE { m_indexBuffers[_handle.idx].create(_size, NULL, _flags); @@ -542,22 +533,22 @@ namespace bgfx { namespace mtl { m_shaders[_handle.idx].create(_mem); } - + void destroyShader(ShaderHandle _handle) BX_OVERRIDE { m_shaders[_handle.idx].destroy(); } - + void createProgram(ProgramHandle _handle, ShaderHandle _vsh, ShaderHandle _fsh) BX_OVERRIDE { m_program[_handle.idx].create(&m_shaders[_vsh.idx], &m_shaders[_fsh.idx]); } - + void destroyProgram(ProgramHandle _handle) BX_OVERRIDE { m_program[_handle.idx].destroy(); } - + void createTexture(TextureHandle _handle, Memory* _mem, uint32_t _flags, uint8_t _skip) BX_OVERRIDE { m_textures[_handle.idx].create(_mem, _flags, _skip); @@ -566,7 +557,7 @@ namespace bgfx { namespace mtl void updateTextureBegin(TextureHandle /*_handle*/, uint8_t /*_side*/, uint8_t /*_mip*/) BX_OVERRIDE { } - + void updateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem) BX_OVERRIDE { m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _pitch, _mem); @@ -781,29 +772,28 @@ namespace bgfx { namespace mtl { m_indexBuffers [_blitter.m_ib->handle.idx].update(0, _numIndices*2, _blitter.m_ib->data); m_vertexBuffers[_blitter.m_vb->handle.idx].update(0, numVertices*_blitter.m_decl.m_stride, _blitter.m_vb->data, true); - + m_renderCommandEncoder.drawIndexedPrimitives(MTLPrimitiveTypeTriangle, _numIndices, MTLIndexTypeUInt16, m_indexBuffers[_blitter.m_ib->handle.idx].m_buffer, 0, 1); } } - void flip(HMD& /*_hmd*/) BX_OVERRIDE { if ( m_drawable == nil || m_commandBuffer == nil) //there was no draw call, cannot flip return; - + // Present and commit the command buffer m_commandBuffer.presentDrawable(m_drawable); MTL_RELEASE(m_drawable); - + m_commandBuffer.commit(); - + // using heavy syncing now // TODO: refactor it with double/triple buffering frame data m_commandBuffer.waitUntilCompleted(); - + MTL_RELEASE(m_commandBuffer); - + //TODO: support multiple windows on OSX /* if (m_flip) @@ -812,7 +802,7 @@ namespace bgfx { namespace mtl { m_glctx.swap(m_frameBuffers[m_windows[ii].idx].m_swapChain); } - + if (!m_ovr.swap(_hmd) ) { m_glctx.swap(); @@ -831,25 +821,27 @@ namespace bgfx { namespace mtl { m_maxAnisotropy = 1; } - + //TODO: _resolution has wrong dimensions, usng m_drawable.texture size now - + if ( NULL == m_drawable.texture ) return; - + uint32_t width = (uint32_t)m_drawable.texture.width; uint32_t height = (uint32_t)m_drawable.texture.height; - + //TODO: there should be a way to specify if backbuffer needs stencil/depth. //TODO: support msaa if ( NULL == m_backBufferDepth || width!=m_backBufferDepth.width() || height!=m_backBufferDepth.height()) { m_textureDescriptor.textureType = MTLTextureType2D; + #if BX_PLATFORM_IOS m_textureDescriptor.pixelFormat = MTLPixelFormatDepth32Float; #else m_textureDescriptor.pixelFormat = MTLPixelFormatDepth32Float_Stencil8; -#endif +#endif // BX_PLATFORM_* + m_textureDescriptor.width = width; m_textureDescriptor.height = height; m_textureDescriptor.depth = 1; @@ -857,18 +849,18 @@ namespace bgfx { namespace mtl m_textureDescriptor.sampleCount = 1; m_textureDescriptor.arrayLength = 1; //m_textureDescriptor.resourceOptions = 0; - + if ( NULL != m_backBufferDepth ) { release(m_backBufferDepth); } m_backBufferDepth = m_device.newTextureWithDescriptor(m_textureDescriptor); - + #if BX_PLATFORM_IOS m_textureDescriptor.pixelFormat = MTLPixelFormatStencil8; m_textureDescriptor.width = width; m_textureDescriptor.height = height; - + if ( NULL != m_backBufferStencil ) { release(m_backBufferStencil); @@ -876,8 +868,8 @@ namespace bgfx { namespace mtl m_backBufferStencil = m_device.newTextureWithDescriptor(m_textureDescriptor); #else m_backBufferStencil = m_backBufferDepth; -#endif - +#endif // BX_PLATFORM_* + bx::HashMurmur2A murmur; murmur.begin(); murmur.add(1); @@ -885,12 +877,12 @@ namespace bgfx { namespace mtl murmur.add((uint32_t)m_backBufferDepth.pixelFormat()); murmur.add((uint32_t)m_backBufferStencil.pixelFormat()); m_backBufferPixelFormatHash = murmur.end(); - + for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii) { m_frameBuffers[ii].postReset(); } - + m_textVideoMem.resize(false, width, height); m_textVideoMem.clear(); } @@ -921,22 +913,22 @@ namespace bgfx { namespace mtl void commit(ConstantBuffer& _constantBuffer) { _constantBuffer.reset(); - + for (;;) { uint32_t opcode = _constantBuffer.read(); - + if (UniformType::End == opcode) { break; } - + UniformType::Enum type; uint16_t loc; uint16_t num; uint16_t copy; ConstantBuffer::decodeOpcode(opcode, type, loc, num, copy); - + const char* data; if (copy) { @@ -948,15 +940,15 @@ namespace bgfx { namespace mtl memcpy(&handle, _constantBuffer.read(sizeof(UniformHandle) ), sizeof(UniformHandle) ); data = (const char*)m_uniforms[handle.idx]; } - + #define CASE_IMPLEMENT_UNIFORM(_uniform, _dxsuffix, _type) \ -case UniformType::_uniform: \ -case UniformType::_uniform|BGFX_UNIFORM_FRAGMENTBIT: \ -{ \ -setShaderUniform(uint8_t(type), loc, data, num); \ -} \ -break; - + case UniformType::_uniform: \ + case UniformType::_uniform|BGFX_UNIFORM_FRAGMENTBIT: \ + { \ + setShaderUniform(uint8_t(type), loc, data, num); \ + } \ + break; + switch ( (uint32_t)type) { case UniformType::Mat3: @@ -982,21 +974,21 @@ break; } } break; - + CASE_IMPLEMENT_UNIFORM(Int1, I, int); CASE_IMPLEMENT_UNIFORM(Vec4, F, float); CASE_IMPLEMENT_UNIFORM(Mat4, F, float); - + case UniformType::End: break; - + default: BX_TRACE("%4d: INVALID 0x%08x, t %d, l %d, n %d, c %d", _constantBuffer.getPos(), opcode, type, loc, num, copy); break; } - + #undef CASE_IMPLEMENT_UNIFORM - + } } @@ -1007,13 +999,13 @@ break; renderPassDescriptor.colorAttachments[0].texture = m_drawable.texture; renderPassDescriptor.depthAttachment.texture = m_backBufferDepth; renderPassDescriptor.stencilAttachment.texture = m_backBufferStencil; - + //todo: set resolve textures } else { FrameBufferMtl& frameBuffer = m_frameBuffers[_fbh.idx]; - + for (uint32_t ii = 0; ii < frameBuffer.m_num; ++ii) { const TextureMtl& texture = m_textures[frameBuffer.m_colorHandle[ii].idx]; @@ -1027,10 +1019,10 @@ break; renderPassDescriptor.stencilAttachment.texture = texture.m_ptrStencil; //TODO: stencilAttachment should be the same if packed/depth stencil format is used } - + //todo: set resolve textures } - + m_fbh = _fbh; m_rtMsaa = _msaa; } @@ -1041,13 +1033,13 @@ break; uint32_t fstencil = unpackStencil(0, _stencil); uint32_t ref = (fstencil&BGFX_STENCIL_FUNC_REF_MASK)>>BGFX_STENCIL_FUNC_REF_SHIFT; _stencil &= packStencil(~BGFX_STENCIL_FUNC_REF_MASK, BGFX_STENCIL_MASK); - + bx::HashMurmur2A murmur; murmur.begin(); murmur.add(_state); murmur.add(_stencil); uint32_t hash = murmur.end(); - + DepthStencilState dss = m_depthStencilStateCache.find(hash); if (NULL == dss) { @@ -1055,11 +1047,11 @@ break; uint32_t func = (_state&BGFX_STATE_DEPTH_TEST_MASK)>>BGFX_STATE_DEPTH_TEST_SHIFT; desc.depthWriteEnabled = !!(BGFX_STATE_DEPTH_WRITE & _state); desc.depthCompareFunction = s_cmpFunc[func]; - + uint32_t bstencil = unpackStencil(1, _stencil); uint32_t frontAndBack = bstencil != BGFX_STENCIL_NONE && bstencil != fstencil; bstencil = frontAndBack ? bstencil : fstencil; - + if ( 0 != _stencil) { StencilDescriptor frontFaceDesc = m_frontFaceStencilDescriptor; @@ -1067,14 +1059,14 @@ break; uint32_t readMask = (fstencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT; uint32_t writeMask = 0xff; - + frontFaceDesc.stencilFailureOperation = s_stencilOp[(fstencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT]; frontFaceDesc.depthFailureOperation = s_stencilOp[(fstencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT]; frontFaceDesc.depthStencilPassOperation = s_stencilOp[(fstencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT]; frontFaceDesc.stencilCompareFunction = s_cmpFunc[(fstencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT]; frontFaceDesc.readMask = readMask; frontFaceDesc.writeMask = writeMask; - + backfaceDesc.stencilFailureOperation = s_stencilOp[(bstencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT]; backfaceDesc.depthFailureOperation = s_stencilOp[(bstencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT]; backfaceDesc.depthStencilPassOperation = s_stencilOp[(bstencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT]; @@ -1092,10 +1084,10 @@ break; } dss = m_device.newDepthStencilStateWithDescriptor(desc); - + m_depthStencilStateCache.add(hash, dss); } - + m_renderCommandEncoder.setDepthStencilState(dss); m_renderCommandEncoder.setStencilReferenceValue(ref); } @@ -1106,7 +1098,7 @@ break; SamplerState sampler = m_samplerStateCache.find(_flags); if (NULL == sampler) { - + m_samplerDescriptor.sAddressMode = s_textureAddress[(_flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT]; m_samplerDescriptor.tAddressMode = s_textureAddress[(_flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT]; m_samplerDescriptor.rAddressMode = s_textureAddress[(_flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT]; @@ -1127,7 +1119,7 @@ break; sampler = m_device.newSamplerStateWithDescriptor(m_samplerDescriptor); m_samplerStateCache.add(_flags, sampler); } - + return sampler; } @@ -1143,48 +1135,47 @@ break; - Device m_device; - CommandQueue m_commandQueue; - CAMetalLayer* m_metalLayer; - Texture m_backBufferDepth; - Texture m_backBufferStencil; - uint32_t m_backBufferPixelFormatHash; - uint32_t m_maxAnisotropy; + Device m_device; + CommandQueue m_commandQueue; + CAMetalLayer* m_metalLayer; + Texture m_backBufferDepth; + Texture m_backBufferStencil; + uint32_t m_backBufferPixelFormatHash; + uint32_t m_maxAnisotropy; - Buffer m_uniformBuffer; //todo: use a pool of this - uint32_t m_uniformBufferVertexOffset; - uint32_t m_uniformBufferFragmentOffset; + Buffer m_uniformBuffer; //todo: use a pool of this + uint32_t m_uniformBufferVertexOffset; + uint32_t m_uniformBufferFragmentOffset; - uint16_t m_numWindows; - FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS]; + uint16_t m_numWindows; + FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS]; - IndexBufferMtl m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS]; + IndexBufferMtl m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS]; VertexBufferMtl m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS]; - ShaderMtl m_shaders[BGFX_CONFIG_MAX_SHADERS]; - ProgramMtl m_program[BGFX_CONFIG_MAX_PROGRAMS]; - TextureMtl m_textures[BGFX_CONFIG_MAX_TEXTURES]; - FrameBufferMtl m_frameBuffers[BGFX_CONFIG_MAX_FRAME_BUFFERS]; - VertexDecl m_vertexDecls[BGFX_CONFIG_MAX_VERTEX_DECLS]; + ShaderMtl m_shaders[BGFX_CONFIG_MAX_SHADERS]; + ProgramMtl m_program[BGFX_CONFIG_MAX_PROGRAMS]; + TextureMtl m_textures[BGFX_CONFIG_MAX_TEXTURES]; + FrameBufferMtl m_frameBuffers[BGFX_CONFIG_MAX_FRAME_BUFFERS]; + VertexDecl m_vertexDecls[BGFX_CONFIG_MAX_VERTEX_DECLS]; UniformRegistry m_uniformReg; - void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS]; + void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS]; - StateCacheT m_depthStencilStateCache; - StateCacheT m_samplerStateCache; + StateCacheT m_depthStencilStateCache; + StateCacheT m_samplerStateCache; - TextVideoMem m_textVideoMem; + TextVideoMem m_textVideoMem; FrameBufferHandle m_fbh; bool m_rtMsaa; - // descriptors - RenderPipelineDescriptor m_renderPipelineDescriptor; - DepthStencilDescriptor m_depthStencilDescriptor; - StencilDescriptor m_frontFaceStencilDescriptor; - StencilDescriptor m_backFaceStencilDescriptor; - VertexDescriptor m_vertexDescriptor; - TextureDescriptor m_textureDescriptor; - SamplerDescriptor m_samplerDescriptor; + RenderPipelineDescriptor m_renderPipelineDescriptor; + DepthStencilDescriptor m_depthStencilDescriptor; + StencilDescriptor m_frontFaceStencilDescriptor; + StencilDescriptor m_backFaceStencilDescriptor; + VertexDescriptor m_vertexDescriptor; + TextureDescriptor m_textureDescriptor; + SamplerDescriptor m_samplerDescriptor; // currently active objects data id m_drawable; @@ -1220,68 +1211,68 @@ break; void ShaderMtl::create(const Memory* _mem) { bx::MemoryReader reader(_mem->data, _mem->size); - + uint32_t magic; bx::read(&reader, magic); - + switch (magic) { case BGFX_CHUNK_MAGIC_CSH: case BGFX_CHUNK_MAGIC_FSH: case BGFX_CHUNK_MAGIC_VSH: break; - + default: BGFX_FATAL(false, Fatal::InvalidShader, "Unknown shader format %x.", magic); break; } - + //bool fragment = BGFX_CHUNK_MAGIC_FSH == magic; - + uint32_t iohash; bx::read(&reader, iohash); - + uint16_t count; bx::read(&reader, count); - + BX_TRACE("%s Shader consts %d" , BGFX_CHUNK_MAGIC_FSH == magic ? "Fragment" : BGFX_CHUNK_MAGIC_VSH == magic ? "Vertex" : "Compute" , count ); - + for (uint32_t ii = 0; ii < count; ++ii) { uint8_t nameSize; bx::read(&reader, nameSize); - + char name[256]; bx::read(&reader, &name, nameSize); name[nameSize] = '\0'; - + uint8_t type; bx::read(&reader, type); - + uint8_t num; bx::read(&reader, num); - + uint16_t regIndex; bx::read(&reader, regIndex); - + uint16_t regCount; bx::read(&reader, regCount); } - + uint32_t shaderSize; bx::read(&reader, shaderSize); - + const char* code = (const char*)reader.getDataPtr(); bx::skip(&reader, shaderSize+1); - + int32_t codeLen = (int32_t)strlen(code); int32_t tempLen = codeLen + (4<<10); char* temp = (char*)alloca(tempLen); bx::StaticMemoryBlockWriter writer(temp, tempLen); - + //TODO: remove this hack. some shaders have problem with half<->float conversion writeString(&writer , "#define half float\n" @@ -1301,7 +1292,7 @@ break; { m_function = lib.newFunctionWithName(SHADER_FUNCTION_NAME); } - + BGFX_FATAL(NULL != m_function, bgfx::Fatal::InvalidShader, "Failed to create %s shader." , BGFX_CHUNK_MAGIC_FSH == magic ? "Fragment" : BGFX_CHUNK_MAGIC_VSH == magic ? "Vertex" : "Compute"); } @@ -1310,7 +1301,7 @@ break; { BX_CHECK(NULL != _vsh->m_function.m_obj, "Vertex shader doesn't exist."); m_vsh = _vsh; - + if (NULL != _fsh) { BX_CHECK(NULL != _fsh->m_function.m_obj, "Fragment shader doesn't exist."); @@ -1330,7 +1321,7 @@ break; const char* name = utf8String(attrib.name); uint32_t loc = (uint32_t)attrib.attributeIndex; BX_TRACE("attr %s: %d", name, loc); - + for (uint8_t ii = 0; ii < Attrib::Count; ++ii) { if (!strcmp(s_attribName[ii],name)) @@ -1340,7 +1331,7 @@ break; break; } } - + for (uint32_t ii = 0; ii < BX_COUNTOF(s_instanceDataName); ++ii) { if (!strcmp(s_instanceDataName[ii],name)) @@ -1348,7 +1339,7 @@ break; m_instanceData[instUsed++] = loc; } } - + } } } @@ -1360,7 +1351,7 @@ break; { m_vsh = NULL; m_fsh = NULL; - + if (NULL != m_vshConstantBuffer) { ConstantBuffer::destroy(m_vshConstantBuffer); @@ -1372,7 +1363,7 @@ break; ConstantBuffer::destroy(m_fshConstantBuffer); m_fshConstantBuffer = NULL; } - + m_vshConstantBufferSize = 0; m_vshConstantBufferAlignmentMask = 0; m_fshConstantBufferSize = 0; @@ -1389,21 +1380,21 @@ break; case MTLDataTypeUInt: case MTLDataTypeInt: return UniformType::Int1; - + case MTLDataTypeFloat: case MTLDataTypeFloat2: case MTLDataTypeFloat3: case MTLDataTypeFloat4: return UniformType::Vec4; - + case MTLDataTypeFloat3x3: return UniformType::Mat3; - + case MTLDataTypeFloat4x4: return UniformType::Mat4; - + }; - + BX_CHECK(false, "Unrecognized Mtl Data type 0x%04x.", _type); return UniformType::End; } @@ -1411,9 +1402,9 @@ break; RenderPipelineState ProgramMtl::getRenderPipelineState(uint64_t _state, uint32_t _rgba, FrameBufferHandle _fbHandle, VertexDeclHandle _declHandle, uint16_t _numInstanceData) { _state &= (BGFX_STATE_BLEND_MASK|BGFX_STATE_BLEND_EQUATION_MASK|BGFX_STATE_ALPHA_WRITE|BGFX_STATE_RGB_WRITE|BGFX_STATE_BLEND_INDEPENDENT|BGFX_STATE_MSAA); - + bool independentBlendEnable = !!(BGFX_STATE_BLEND_INDEPENDENT & _state); - + bx::HashMurmur2A murmur; murmur.begin(); murmur.add(_state); @@ -1428,14 +1419,14 @@ break; murmur.add(_declHandle.idx); murmur.add(_numInstanceData); uint32_t hash = murmur.end(); - + RenderPipelineState rps = m_renderPipelineStateCache.find(hash); if ( NULL == rps ) { RenderPipelineDescriptor& pipelineDesc = s_renderMtl->m_renderPipelineDescriptor; reset(pipelineDesc); uint32_t frameBufferAttachment = 1; - + if (!isValid(_fbHandle) ) { pipelineDesc.colorAttachments[0].pixelFormat = s_renderMtl->m_drawable.texture.pixelFormat; @@ -1446,13 +1437,13 @@ break; { FrameBufferMtl& frameBuffer = s_renderMtl->m_frameBuffers[_fbHandle.idx]; frameBufferAttachment = frameBuffer.m_num; - + for (uint32_t ii = 0; ii < frameBuffer.m_num; ++ii) { const TextureMtl& texture = s_renderMtl->m_textures[frameBuffer.m_colorHandle[ii].idx]; pipelineDesc.colorAttachments[ii].pixelFormat = texture.m_ptr.m_obj.pixelFormat; } - + if (isValid(frameBuffer.m_depthHandle)) { const TextureMtl& texture = s_renderMtl->m_textures[frameBuffer.m_depthHandle.idx]; @@ -1462,90 +1453,90 @@ break; //todo: stencil attachment should be the same as depth for packed depth/stencil } } - + // TODO: BGFX_STATE_MSAA using _fbHandle texture msaa values const uint32_t blend = uint32_t( (_state&BGFX_STATE_BLEND_MASK)>>BGFX_STATE_BLEND_SHIFT); const uint32_t equation = uint32_t( (_state&BGFX_STATE_BLEND_EQUATION_MASK)>>BGFX_STATE_BLEND_EQUATION_SHIFT); - + const uint32_t srcRGB = (blend )&0xf; const uint32_t dstRGB = (blend>> 4)&0xf; const uint32_t srcA = (blend>> 8)&0xf; const uint32_t dstA = (blend>>12)&0xf; - + const uint32_t equRGB = (equation )&0x7; const uint32_t equA = (equation>>3)&0x7; - + uint8_t writeMask = (_state&BGFX_STATE_ALPHA_WRITE) ? MTLColorWriteMaskAlpha : 0; writeMask |= (_state&BGFX_STATE_RGB_WRITE) ? MTLColorWriteMaskRed|MTLColorWriteMaskGreen|MTLColorWriteMaskBlue : 0; - + for (uint32_t ii = 0; ii < (independentBlendEnable ? 1 : frameBufferAttachment); ++ii) { RenderPipelineColorAttachmentDescriptor drt = pipelineDesc.colorAttachments[ii]; - + drt.blendingEnabled = !!(BGFX_STATE_BLEND_MASK & _state); - + drt.sourceRGBBlendFactor = s_blendFactor[srcRGB][0]; drt.destinationRGBBlendFactor = s_blendFactor[dstRGB][0]; drt.rgbBlendOperation = s_blendEquation[equRGB]; - + drt.sourceAlphaBlendFactor = s_blendFactor[srcA][1]; drt.destinationAlphaBlendFactor = s_blendFactor[dstA][1]; drt.alphaBlendOperation = s_blendEquation[equA]; - + drt.writeMask = writeMask; } - + if (independentBlendEnable) { for (uint32_t ii = 1, rgba = _rgba; ii < frameBufferAttachment; ++ii, rgba >>= 11) { RenderPipelineColorAttachmentDescriptor drt = pipelineDesc.colorAttachments[ii]; - + drt.blendingEnabled = 0 != (rgba&0x7ff); - + const uint32_t src = (rgba )&0xf; const uint32_t dst = (rgba>>4)&0xf; const uint32_t equationIndex = (rgba>>8)&0x7; - + drt.sourceRGBBlendFactor = s_blendFactor[src][0]; drt.destinationRGBBlendFactor = s_blendFactor[dst][0]; drt.rgbBlendOperation = s_blendEquation[equationIndex]; - + drt.sourceAlphaBlendFactor = s_blendFactor[src][1]; drt.destinationAlphaBlendFactor = s_blendFactor[dst][1]; drt.alphaBlendOperation = s_blendEquation[equationIndex]; - + drt.writeMask = writeMask; } } - + pipelineDesc.vertexFunction = m_vsh->m_function; pipelineDesc.fragmentFunction = m_fsh->m_function; - + if (isValid(_declHandle)) { VertexDescriptor vertexDesc = s_renderMtl->m_vertexDescriptor; reset(vertexDesc); - + VertexDecl &vertexDecl = s_renderMtl->m_vertexDecls[_declHandle.idx]; for (uint32_t ii = 0; Attrib::Count != m_used[ii]; ++ii) { Attrib::Enum attr = Attrib::Enum(m_used[ii]); uint32_t loc = m_attributes[attr]; - + uint8_t num; AttribType::Enum type; bool normalized; bool asInt; vertexDecl.decode(attr, num, type, normalized, asInt); BX_CHECK(num<=4, "num must be <=4"); - + if (0xff != vertexDecl.m_attributes[attr]) { vertexDesc.attributes[loc].format = s_attribType[type][num-1][normalized?1:0]; vertexDesc.attributes[loc].bufferIndex = 1; vertexDesc.attributes[loc].offset = vertexDecl.m_offset[attr]; - + BX_TRACE("attrib:%s format: %d offset:%d", s_attribName[attr], (int)vertexDesc.attributes[loc].format, (int)vertexDesc.attributes[loc].offset); } else @@ -1555,12 +1546,12 @@ break; vertexDesc.attributes[loc].offset = 0; } } - + vertexDesc.layouts[1].stride = vertexDecl.getStride(); vertexDesc.layouts[1].stepFunction = MTLVertexStepFunctionPerVertex; - + BX_TRACE("stride: %d", (int)vertexDesc.layouts[1].stride); - + if (_numInstanceData > 0) { for (uint32_t ii = 0; 0xffff != m_instanceData[ii]; ++ii) @@ -1570,12 +1561,12 @@ break; vertexDesc.attributes[loc].bufferIndex = 2; vertexDesc.attributes[loc].offset = ii*16; } - + vertexDesc.layouts[2].stride = _numInstanceData * 16; vertexDesc.layouts[2].stepFunction = MTLVertexStepFunctionPerInstance; vertexDesc.layouts[2].stepRate = 1; } - + pipelineDesc.vertexDescriptor = vertexDesc; } @@ -1595,7 +1586,7 @@ break; { ConstantBuffer*& constantBuffer = (type==0?m_vshConstantBuffer : m_fshConstantBuffer); uint8_t fragmentBit = (1 == type ? BGFX_UNIFORM_FRAGMENTBIT : 0); - + for( MTLArgument* arg in (type==0?reflection.vertexArguments:reflection.fragmentArguments)) { BX_TRACE("arg: %s type:%d", utf8String(arg.name), arg.type); @@ -1605,7 +1596,7 @@ break; { BX_CHECK( arg.index == 0, "Uniform buffer must be in the buffer slot 0."); BX_CHECK( MTLDataTypeStruct == arg.bufferDataType, "%s's type must be a struct",SHADER_UNIFORM_NAME ); - + if ( MTLDataTypeStruct == arg.bufferDataType ) { if ( type == 0) @@ -1618,21 +1609,21 @@ break; m_fshConstantBufferSize = (uint32_t)arg.bufferDataSize; m_fshConstantBufferAlignmentMask = (uint32_t)arg.bufferAlignment - 1; } - + for( MTLStructMember* uniform in arg.bufferStructType.members ) { const char* name = utf8String(uniform.name); BX_TRACE("uniform: %s type:%d", name, uniform.dataType); - + MTLDataType dataType = uniform.dataType; uint32_t num = 1; - + if (dataType==MTLDataTypeArray) { dataType = uniform.arrayType.elementType; num = (uint32_t)uniform.arrayType.arrayLength; } - + switch (dataType) { case MTLDataTypeFloat4 : num *= 1; @@ -1647,7 +1638,7 @@ break; BX_WARN(0, "Unsupported uniform MTLDataType: %d", uniform.dataType); break; } - + PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name); if (PredefinedUniform::Count != predefined) { @@ -1665,13 +1656,13 @@ break; { constantBuffer = ConstantBuffer::create(1024); } - + UniformType::Enum type = convertMtlType(dataType); constantBuffer->writeUniformHandle((UniformType::Enum)(type|fragmentBit), uint32_t(uniform.offset), info->m_handle, uint16_t(num) ); BX_TRACE("store %s %d offset:%d", name, info->m_handle, uint32_t(uniform.offset)); } } - + } } } else if (arg.type == MTLArgumentTypeTexture) @@ -1691,10 +1682,10 @@ break; } m_processedUniforms = true; } - + m_renderPipelineStateCache.add(hash, rps); } - + return rps; } @@ -1719,16 +1710,16 @@ break; ? s_renderMtl->m_vertexDecls[_declHandle.idx].m_stride : 0 ; - + BufferMtl::create(_size, _data, _flags, stride, true); } void TextureMtl::create(const Memory* _mem, uint32_t _flags, uint8_t _skip) { m_sampler = s_renderMtl->getSamplerState(_flags); - + ImageContainer imageContainer; - + if (imageParse(imageContainer, _mem->data, _mem->size) ) { uint8_t numMips = imageContainer.m_numMips; @@ -1737,23 +1728,23 @@ break; const ImageBlockInfo& blockInfo = getBlockInfo(TextureFormat::Enum(imageContainer.m_format) ); const uint32_t textureWidth = bx::uint32_max(blockInfo.blockWidth, imageContainer.m_width >>startLod); const uint32_t textureHeight = bx::uint32_max(blockInfo.blockHeight, imageContainer.m_height>>startLod); - + m_flags = _flags; m_requestedFormat = (uint8_t)imageContainer.m_format; m_textureFormat = (uint8_t)imageContainer.m_format; - + const TextureFormatInfo& tfi = s_textureFormat[m_requestedFormat]; const bool convert = MTLPixelFormatInvalid == tfi.m_fmt; - + uint8_t bpp = getBitsPerPixel(TextureFormat::Enum(m_textureFormat) ); if (convert) { m_textureFormat = (uint8_t)TextureFormat::RGBA8; bpp = 32; } - + TextureDescriptor desc = s_renderMtl->m_textureDescriptor; - + if (imageContainer.m_cubeMap) { desc.textureType = MTLTextureTypeCube; @@ -1766,11 +1757,11 @@ break; { desc.textureType = MTLTextureType2D; } - + m_numMips = numMips; - + const bool compressed = isCompressed(TextureFormat::Enum(m_textureFormat) ); - + BX_TRACE("Texture %3d: %s (requested: %s), %dx%d%s%s." , this - s_renderMtl->m_textures , getName( (TextureFormat::Enum)m_textureFormat) @@ -1780,67 +1771,67 @@ break; , imageContainer.m_cubeMap ? "x6" : "" , 0 != (_flags&BGFX_TEXTURE_RT_MASK) ? " (render target)" : "" ); - - + + 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 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 DXGI_SAMPLE_DESC& msaa = s_msaa[msaaQuality]; - + MTLPixelFormat format = MTLPixelFormatInvalid; if (srgb) { format = s_textureFormat[m_textureFormat].m_fmtSrgb; BX_WARN(format != MTLPixelFormatInvalid, "sRGB not supported for texture format %d", m_textureFormat); } - + if (format == MTLPixelFormatInvalid) { // not swizzled and not sRGB, or sRGB unsupported format = s_textureFormat[m_textureFormat].m_fmt; } - + desc.pixelFormat = format; desc.width = textureWidth; desc.height = textureHeight; desc.depth = bx::uint32_max(1,imageContainer.m_depth); desc.mipmapLevelCount = imageContainer.m_numMips; desc.sampleCount = 1; //TODO: set samplecount - If textureType is not MTLTextureType2DMultisample, the value must be 1. - + //TODO: set resource flags depending on usage(renderTarget/computeWrite/etc) on iOS9/OSX - + m_ptr = s_renderMtl->m_device.newTextureWithDescriptor(desc); if ( m_requestedFormat == TextureFormat::D24S8 && desc.pixelFormat == MTLPixelFormatDepth32Float ) { desc.pixelFormat = MTLPixelFormatStencil8; m_ptrStencil = s_renderMtl->m_device.newTextureWithDescriptor(desc); } - + uint8_t* temp = NULL; if (convert) { temp = (uint8_t*)BX_ALLOC(g_allocator, textureWidth*textureHeight*4); } - + for (uint8_t side = 0, numSides = imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side) { uint32_t width = textureWidth; uint32_t height = textureHeight; uint32_t depth = imageContainer.m_depth; - + for (uint8_t lod = 0, num = numMips; lod < num; ++lod) { width = bx::uint32_max(1, width); height = bx::uint32_max(1, height); depth = bx::uint32_max(1, depth); - + ImageMip mip; if (imageGetRawData(imageContainer, side, lod+startLod, _mem->data, _mem->size, mip) ) { const uint8_t* data = mip.m_data; - + if (convert) { imageDecodeToRgba8(temp @@ -1852,12 +1843,12 @@ break; ); data = temp; } - + MTLRegion region = { 0, 0, 0, width, height, depth}; uint32_t bytesPerRow; uint32_t bytesPerImage; - + if ( compressed && !convert) { if ( format >= MTLPixelFormatPVRTC_RGB_2BPP && format <= MTLPixelFormatPVRTC_RGBA_4BPP_sRGB) @@ -1876,20 +1867,20 @@ break; bytesPerRow = width * bpp / 8; bytesPerImage = (desc.textureType == MTLTextureType3D) ? width * height * bpp / 8 : 0; } - + m_ptr.replaceRegion(region, lod, side, data, bytesPerRow, bytesPerImage); } else if (!computeWrite) { //TODO: do we need to clear to zero?? } - + width >>= 1; height >>= 1; depth >>= 1; } } - + if (NULL != temp) { BX_FREE(g_allocator, temp); @@ -1900,25 +1891,25 @@ break; 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}; - + const uint32_t bpp = getBitsPerPixel(TextureFormat::Enum(m_textureFormat) ); const uint32_t rectpitch = _rect.m_width*bpp/8; const uint32_t srcpitch = UINT16_MAX == _pitch ? rectpitch : _pitch; - + const bool convert = m_textureFormat != m_requestedFormat; - + uint8_t* data = _mem->data; uint8_t* temp = NULL; - + if (convert) { temp = (uint8_t*)BX_ALLOC(g_allocator, rectpitch*_rect.m_height); imageDecodeToBgra8(temp, data, _rect.m_width, _rect.m_height, srcpitch, m_requestedFormat); data = temp; } - + m_ptr.replaceRegion(region, _mip, _side, data, srcpitch, srcpitch * _rect.m_height); - + if (NULL != temp) { BX_FREE(g_allocator, temp); @@ -1943,7 +1934,7 @@ break; if (isValid(handle) ) { const TextureMtl& texture = s_renderMtl->m_textures[handle.idx]; - + //TODO: separate stencil buffer? or just use packed depth/stencil (which is not available on iOS8) if (isDepth( (TextureFormat::Enum)texture.m_textureFormat) ) { @@ -1969,7 +1960,7 @@ break; const TextureMtl& depthTexture = s_renderMtl->m_textures[m_depthHandle.idx]; murmur.add((uint32_t)depthTexture.m_ptr.pixelFormat()); murmur.add((uint32_t)MTLPixelFormatInvalid); //stencil - + m_pixelFormatHash = murmur.end(); } @@ -1988,10 +1979,10 @@ break; { m_num = 0; m_depthHandle.idx = invalidHandle; - + uint16_t denseIdx = m_denseIdx; m_denseIdx = UINT16_MAX; - + return denseIdx; } @@ -2090,20 +2081,20 @@ break; uint8_t eye = 0; uint8_t restartState = 0; viewState.m_rect = _render->m_rect[0]; - + int32_t numItems = _render->m_num; for (int32_t item = 0, restartItem = numItems; item < numItems || restartItem < numItems;) { const bool isCompute = key.decode(_render->m_sortKeys[item], _render->m_viewRemap); statsKeyType[isCompute]++; - + const bool viewChanged = 0 || key.m_view != view || item == numItems ; const RenderItem& renderItem = _render->m_renderItem[_render->m_sortValues[item] ]; ++item; - + if (viewChanged) { if (1 == restartState) @@ -2114,11 +2105,11 @@ break; view = 0xff; continue; } - + view = key.m_view; programIdx = invalidHandle; - - + + viewRestart = ( (BGFX_VIEW_STEREO == (_render->m_viewFlags[view] & BGFX_VIEW_STEREO) ) ); viewRestart &= hmdEnabled; if (viewRestart) @@ -2128,7 +2119,7 @@ break; restartState = 1; restartItem = item - 1; } - + eye = (restartState - 1) & 1; restartState &= 1; } @@ -2136,32 +2127,32 @@ break; { eye = 0; } - + viewState.m_rect = _render->m_rect[view]; if (viewRestart) { viewState.m_rect.m_x = eye * (viewState.m_rect.m_width+1)/2; viewState.m_rect.m_width /= 2; } - + const Rect& scissorRect = _render->m_scissor[view]; viewHasScissor = !scissorRect.isZero(); viewScissorRect = viewHasScissor ? scissorRect : viewState.m_rect; Clear& clr = _render->m_clear[view]; - + mtl::RenderPassDescriptor renderPassDescriptor = newRenderPassDescriptor(); - + //todo: check FB size uint32_t width = getBufferWidth(); uint32_t height = getBufferHeight(); Rect viewRect = viewState.m_rect; bool fullscreenRect = (0 == viewRect.m_x && 0 == viewRect.m_y && width == viewRect.m_width && height == viewRect.m_height); - + //TODO/OPTIMIZATION: merge views with same target framebuffer into one renderPass - + fbh = _render->m_fb[view]; setFrameBuffer(renderPassDescriptor, fbh); - + RenderPassColorAttachmentDescriptor colorAttachment0 = renderPassDescriptor.colorAttachments[0]; if (BGFX_CLEAR_COLOR & clr.m_flags) { @@ -2183,12 +2174,12 @@ break; float aa = clr.m_index[3]*1.0f/255.0f; colorAttachment0.clearColor = MTLClearColorMake(rr,gg,bb,aa); } - + colorAttachment0.loadAction = MTLLoadActionClear; } else colorAttachment0.loadAction = MTLLoadActionLoad; - + //TODO: optimize store actions use discard flag RenderPassDepthAttachmentDescriptor depthAttachment = renderPassDescriptor.depthAttachment; if ( NULL != depthAttachment.texture) @@ -2197,7 +2188,7 @@ break; depthAttachment.loadAction = (BGFX_CLEAR_DEPTH & clr.m_flags) ? MTLLoadActionClear : MTLLoadActionLoad; depthAttachment.storeAction = MTLStoreActionStore; } - + RenderPassStencilAttachmentDescriptor stencilAttachment = renderPassDescriptor.stencilAttachment; if ( NULL != stencilAttachment.texture ) { @@ -2205,7 +2196,7 @@ break; stencilAttachment.loadAction = (BGFX_CLEAR_STENCIL & clr.m_flags) ? MTLLoadActionClear : MTLLoadActionLoad; stencilAttachment.storeAction = MTLStoreActionStore; } - + if ( 0 != m_renderCommandEncoder) { m_renderCommandEncoder.endEncoding(); @@ -2213,22 +2204,22 @@ break; rce = m_commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor); m_renderCommandEncoder = rce; MTL_RELEASE(renderPassDescriptor); - + //TODO: REMOVE THIS!!!! // TERRIBLE HACK TO SUPPRESS DEBUG LAYER WARNING ABOUT MISSING TEXTURE/SAMPLER AT 0 in 20-nanovg m_renderCommandEncoder.setFragmentTexture(zeroTexture, 0); m_renderCommandEncoder.setFragmentSamplerState(getSamplerState(0), 0); - + rce.setTriangleFillMode(wireframe? MTLTriangleFillModeLines : MTLTriangleFillModeFill); - + if (BX_ENABLED(BGFX_CONFIG_DEBUG_MTL) ) { if ( item != 1) //ASK: better check ? I don't get the whole restart thing rce.popDebugGroup(); - + rce.pushDebugGroup(s_viewName[view]); } - + MTLViewport vp; vp.originX = viewState.m_rect.m_x; vp.originY = viewState.m_rect.m_y; @@ -2237,38 +2228,38 @@ break; vp.znear = 0.0f; vp.zfar = 1.0f; rce.setViewport(vp); - + if (BGFX_CLEAR_NONE != (clr.m_flags & BGFX_CLEAR_MASK) && !fullscreenRect) { //TODO: fallback to clear with quad //clearQuad(_clearQuad, viewState.m_rect, clr, _render->m_clearColor); } } - + //TODO: iscompute - + bool resetState = viewChanged || wasCompute; - + if (wasCompute) { wasCompute = false; - + programIdx = invalidHandle; currentProgram = NULL; - + //TODO //invalidateCompute(); } - + const RenderDraw& draw = renderItem.draw; - + const uint64_t newFlags = draw.m_flags; uint64_t changedFlags = currentState.m_flags ^ draw.m_flags; currentState.m_flags = newFlags; - + const uint64_t newStencil = draw.m_stencil; uint64_t changedStencil = currentState.m_stencil ^ draw.m_stencil; currentState.m_stencil = newStencil; - + if (resetState) { currentState.clear(); @@ -2277,24 +2268,24 @@ break; changedStencil = packStencil(BGFX_STENCIL_MASK, BGFX_STENCIL_MASK); currentState.m_flags = newFlags; currentState.m_stencil = newStencil; - + programIdx = invalidHandle; setDepthStencilState(newFlags, packStencil(BGFX_STENCIL_DEFAULT, BGFX_STENCIL_DEFAULT)); - + const uint64_t pt = newFlags&BGFX_STATE_PT_MASK; primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT); } - + if (prim.m_type != s_primInfo[primIndex].m_type) { prim = s_primInfo[primIndex]; } - + uint16_t scissor = draw.m_scissor; if (currentState.m_scissor != scissor) { currentState.m_scissor = scissor; - + MTLScissorRect rc; if (UINT16_MAX == scissor) { @@ -2324,13 +2315,13 @@ break; } rce.setScissorRect(rc); } - + if ( (BGFX_STATE_DEPTH_WRITE|BGFX_STATE_DEPTH_TEST_MASK) & changedFlags || 0 != changedStencil) { setDepthStencilState(newFlags,newStencil); } - + if ( (0 | BGFX_STATE_CULL_MASK | BGFX_STATE_ALPHA_REF_MASK @@ -2344,13 +2335,13 @@ break; uint8_t cullIndex = uint8_t(pt>>BGFX_STATE_CULL_SHIFT); rce.setCullMode(s_cullMode[cullIndex]); } - + if (BGFX_STATE_ALPHA_REF_MASK & changedFlags) { uint32_t ref = (newFlags&BGFX_STATE_ALPHA_REF_MASK)>>BGFX_STATE_ALPHA_REF_SHIFT; viewState.m_alphaRef = ref/255.0f; } - + const uint64_t pt = newFlags&BGFX_STATE_PT_MASK; primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT); if (prim.m_type != s_primInfo[primIndex].m_type) @@ -2358,7 +2349,7 @@ break; prim = s_primInfo[primIndex]; } } - + if ( (blendFactor != draw.m_rgba) && !(newFlags & BGFX_STATE_BLEND_INDEPENDENT) ) { const uint32_t rgba = draw.m_rgba; @@ -2367,14 +2358,14 @@ break; float bb = ( (rgba>> 8)&0xff)/255.0f; float aa = ( (rgba )&0xff)/255.0f; rce.setBlendColor(rr,gg,bb,aa); - + blendFactor = draw.m_rgba; } - + bool programChanged = false; bool constantsChanged = draw.m_constBegin < draw.m_constEnd; rendererUpdateUniforms(this, _render->m_constantBuffer, draw.m_constBegin, draw.m_constEnd); - + if (key.m_program != programIdx || (BGFX_STATE_BLEND_MASK|BGFX_STATE_BLEND_EQUATION_MASK|BGFX_STATE_ALPHA_WRITE|BGFX_STATE_RGB_WRITE|BGFX_STATE_BLEND_INDEPENDENT|BGFX_STATE_MSAA) & changedFlags || currentState.m_vertexBuffer.idx != draw.m_vertexBuffer.idx || @@ -2385,7 +2376,7 @@ break; programIdx = key.m_program; currentState.m_vertexDecl = draw.m_vertexDecl; currentState.m_instanceDataStride = draw.m_instanceDataStride; - + if (invalidHandle == programIdx) { currentProgram = NULL; @@ -2395,12 +2386,12 @@ break; { ProgramMtl& program = m_program[programIdx]; currentProgram = &program; - + uint16_t handle = draw.m_vertexBuffer.idx; const VertexBufferMtl& vb = m_vertexBuffers[handle]; VertexDeclHandle decl; decl.idx = !isValid(vb.m_decl) ? draw.m_vertexDecl.idx : vb.m_decl.idx; - + RenderPipelineState pipelineState = program.getRenderPipelineState(newFlags, draw.m_rgba, fbh, decl, draw.m_instanceDataStride/16); if ( NULL == pipelineState ) { //call with invalid program @@ -2408,36 +2399,36 @@ break; programIdx = invalidHandle; continue; } - + rce.setRenderPipelineState(pipelineState); } - + programChanged = constantsChanged = true; } - + if (invalidHandle != programIdx) { ProgramMtl& program = m_program[programIdx]; - + uint32_t vertexUniformBufferSize = program.m_vshConstantBufferSize; uint32_t fragmentUniformBufferSize = program.m_fshConstantBufferSize; - + if ( vertexUniformBufferSize ) { m_uniformBufferVertexOffset = BX_ALIGN_MASK(m_uniformBufferVertexOffset, program.m_vshConstantBufferAlignmentMask); rce.setVertexBuffer(m_uniformBuffer, m_uniformBufferVertexOffset, 0); } - + m_uniformBufferFragmentOffset = m_uniformBufferVertexOffset + vertexUniformBufferSize; if ( fragmentUniformBufferSize ) { m_uniformBufferFragmentOffset = BX_ALIGN_MASK(m_uniformBufferFragmentOffset, program.m_fshConstantBufferAlignmentMask); rce.setFragmentBuffer(m_uniformBuffer, m_uniformBufferFragmentOffset, 0); } - + //TODO: create new UniformBuffer when not enough place for next buffer - + if (constantsChanged) { ConstantBuffer* vcb = program.m_vshConstantBuffer; @@ -2445,20 +2436,20 @@ break; { commit(*vcb); } - + ConstantBuffer* fcb = program.m_fshConstantBuffer; if (NULL != fcb) { commit(*fcb); } } - + viewState.setPredefined<4>(this, view, eye, program, _render, draw); - + m_uniformBufferFragmentOffset += fragmentUniformBufferSize; m_uniformBufferVertexOffset = m_uniformBufferFragmentOffset; } - + { for (uint8_t stage = 0; stage < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++stage) { @@ -2474,11 +2465,11 @@ break; texture.commit(stage, sampler.m_un.m_draw.m_flags); } } - + current = sampler; } } - + if (currentState.m_vertexBuffer.idx != draw.m_vertexBuffer.idx || currentState.m_startVertex != draw.m_startVertex || currentState.m_instanceDataBuffer.idx != draw.m_instanceDataBuffer.idx @@ -2489,18 +2480,18 @@ break; currentState.m_startVertex = draw.m_startVertex; currentState.m_instanceDataBuffer.idx = draw.m_instanceDataBuffer.idx; currentState.m_instanceDataOffset = draw.m_instanceDataOffset; - + uint16_t handle = draw.m_vertexBuffer.idx; if (invalidHandle != handle) { const VertexBufferMtl& vb = m_vertexBuffers[handle]; - + uint16_t decl = !isValid(vb.m_decl) ? draw.m_vertexDecl.idx : vb.m_decl.idx; const VertexDecl& vertexDecl = m_vertexDecls[decl]; uint32_t offset = draw.m_startVertex * vertexDecl.getStride(); - + rce.setVertexBuffer(vb.m_buffer, offset, 1); - + if (isValid(draw.m_instanceDataBuffer) ) { const VertexBufferMtl& inst = m_vertexBuffers[draw.m_instanceDataBuffer.idx]; @@ -2508,7 +2499,7 @@ break; } } } - + if (isValid(currentState.m_vertexBuffer) ) { uint32_t numVertices = draw.m_numVertices; @@ -2519,13 +2510,13 @@ break; const VertexDecl& vertexDecl = m_vertexDecls[decl]; numVertices = vb.m_size/vertexDecl.m_stride; } - + uint32_t numIndices = 0; uint32_t numPrimsSubmitted = 0; uint32_t numInstances = 0; uint32_t numPrimsRendered = 0; uint32_t numDrawIndirect = 0; - + if (isValid(draw.m_indirectBuffer) ) { // TODO: indirect draw @@ -2536,7 +2527,7 @@ break; { const IndexBufferMtl& ib = m_indexBuffers[draw.m_indexBuffer.idx]; MTLIndexType indexType = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32; - + if (UINT32_MAX == draw.m_numIndices) { const uint32_t indexSize = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? 2 : 4; @@ -2544,7 +2535,7 @@ break; numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub; numInstances = draw.m_numInstances; numPrimsRendered = numPrimsSubmitted*draw.m_numInstances; - + rce.drawIndexedPrimitives(prim.m_type, numIndices, indexType, ib.m_buffer, 0, draw.m_numInstances); } else if (prim.m_min <= draw.m_numIndices) @@ -2554,7 +2545,7 @@ break; numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub; numInstances = draw.m_numInstances; numPrimsRendered = numPrimsSubmitted*draw.m_numInstances; - + rce.drawIndexedPrimitives(prim.m_type, numIndices, indexType, ib.m_buffer, draw.m_startIndex * indexSize,numInstances); } } @@ -2563,11 +2554,11 @@ break; numPrimsSubmitted = numVertices/prim.m_div - prim.m_sub; numInstances = draw.m_numInstances; numPrimsRendered = numPrimsSubmitted*draw.m_numInstances; - + rce.drawPrimitives(prim.m_type, 0, draw.m_numVertices, draw.m_numInstances); } } - + statsNumPrimsSubmitted[primIndex] += numPrimsSubmitted; statsNumPrimsRendered[primIndex] += numPrimsRendered; statsNumInstances[primIndex] += numInstances; @@ -2575,13 +2566,13 @@ break; statsNumIndices += numIndices; } } - + if (wasCompute) { //TODO //invalidateCompute(); } - + if (0 < _render->m_num) { //ASK: we now using one commandBuffer that is commited in flush. Should we implement this? @@ -2590,7 +2581,7 @@ break; // ???? //deviceCtx->Flush(); } - + captureElapsed = -bx::getHPCounter(); //TODO //capture(); @@ -2624,10 +2615,10 @@ break; static uint32_t maxGpuLatency = 0; static double maxGpuElapsed = 0.0f; double elapsedGpuMs = 0.0; - + //TODO: gputimer /* m_gpuTimer.end(); - + while (m_gpuTimer.get() ) { double toGpuMs = 1000.0 / double(m_gpuTimer.m_frequency); @@ -2637,25 +2628,25 @@ break; maxGpuLatency = bx::uint32_imax(maxGpuLatency, m_gpuTimer.m_control.available()-1); */ TextVideoMem& tvm = m_textVideoMem; - + static int64_t next = now; - + if (now >= next) { next = now + bx::getHPFrequency(); double freq = double(bx::getHPFrequency() ); double toMs = 1000.0/freq; - + tvm.clear(); uint16_t pos = 0; tvm.printf(0, pos++, BGFX_CONFIG_DEBUG ? 0x89 : 0x8f, " %s / " BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME " " , getRendererName() ); - + //const D3DADAPTER_IDENTIFIER9& identifier = m_identifier; //tvm.printf(0, pos++, 0x0f, " Device: %s (%s)", identifier.Description, identifier.Driver); - + pos = 10; tvm.printf(10, pos++, 0x8e, " Frame: %7.3f, % 7.3f \x1f, % 7.3f \x1e [ms] / % 6.2f FPS " , double(frameTime)*toMs @@ -2663,8 +2654,8 @@ break; , double(max)*toMs , freq/frameTime ); - - /* + + /* const uint32_t msaa = (m_resolution.m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT; tvm.printf(10, pos++, 0x8e, " Reset flags: [%c] vsync, [%c] MSAAx%d, [%c] MaxAnisotropy " , !!(m_resolution.m_flags&BGFX_RESET_VSYNC) ? '\xfe' : ' ' @@ -2685,7 +2676,7 @@ break; ); maxGpuLatency = 0; maxGpuElapsed = 0.0; - + for (uint32_t ii = 0; ii < BX_COUNTOF(s_primName); ++ii) { tvm.printf(10, pos++, 0x8e, " %9s: %7d (#inst: %5d), submitted: %7d" @@ -2695,34 +2686,34 @@ break; , statsNumPrimsSubmitted[ii] ); } - + tvm.printf(10, pos++, 0x8e, " Indices: %7d", statsNumIndices); tvm.printf(10, pos++, 0x8e, " DVB size: %7d", _render->m_vboffset); tvm.printf(10, pos++, 0x8e, " DIB size: %7d", _render->m_iboffset); - + double captureMs = double(captureElapsed)*toMs; tvm.printf(10, pos++, 0x8e, " Capture: %3.4f [ms]", captureMs); - + uint8_t attr[2] = { 0x89, 0x8a }; uint8_t attrIndex = _render->m_waitSubmit < _render->m_waitRender; - + tvm.printf(10, pos++, attr[attrIndex&1], " Submit wait: %3.4f [ms]", _render->m_waitSubmit*toMs); tvm.printf(10, pos++, attr[(attrIndex+1)&1], " Render wait: %3.4f [ms]", _render->m_waitRender*toMs); - + min = frameTime; max = frameTime; } - + blit(this, _textVideoMemBlitter, tvm); - + rce.popDebugGroup(); } else if (_render->m_debug & BGFX_DEBUG_TEXT) { rce.pushDebugGroup("debugtext"); - + blit(this, _textVideoMemBlitter, _render->m_textVideoMem); - + rce.popDebugGroup(); } @@ -2733,7 +2724,6 @@ break; m_renderCommandEncoder = 0; } - } /* namespace mtl */ } // namespace bgfx #else @@ -2744,10 +2734,10 @@ namespace bgfx { namespace mtl { return NULL; } - + void rendererDestroy() { } } /* namespace mtl */ } // namespace bgfx -#endif // BGFX_CONFIG_RENDERER_NULL \ No newline at end of file +#endif // BGFX_CONFIG_RENDERER_METAL