renderer_mtl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Copyright 2011-2015 Attila Kocsis, Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #ifndef BGFX_RENDERER_METAL_H_HEADER_GUARD
  6. #define BGFX_RENDERER_METAL_H_HEADER_GUARD
  7. #include "bgfx_p.h"
  8. #if BGFX_CONFIG_RENDERER_METAL
  9. #import <QuartzCore/CAMetalLayer.h>
  10. #import <Metal/Metal.h>
  11. #if BX_PLATFORM_IOS
  12. # import <UIKit/UIKit.h>
  13. #endif // BX_PLATFORM_*
  14. namespace bgfx { namespace mtl
  15. {
  16. // c++ wrapper
  17. // objects with creation functions starting with 'new' has a refcount 1 after creation, object must be destroyed with release.
  18. // commandBuffer, commandEncoders are autoreleased objects. Needs AutoreleasePool!
  19. #define MTL_CLASS(name) \
  20. class name \
  21. { \
  22. public: \
  23. name(id <MTL##name> _obj = nil) : m_obj(_obj) {} \
  24. operator id <MTL##name>() const { return m_obj; } \
  25. id <MTL##name> m_obj;
  26. #define MTL_CLASS_END };
  27. typedef void (*mtlCallback)(void* userData);
  28. MTL_CLASS(Buffer)
  29. void* contents()
  30. {
  31. return m_obj.contents;
  32. }
  33. uint32_t length()
  34. {
  35. return (uint32_t)m_obj.length;
  36. }
  37. MTL_CLASS_END
  38. MTL_CLASS(CommandBuffer)
  39. // Creating Command Encoders
  40. id<MTLRenderCommandEncoder> renderCommandEncoderWithDescriptor( MTLRenderPassDescriptor* _renderPassDescriptor){
  41. return [m_obj renderCommandEncoderWithDescriptor:_renderPassDescriptor];
  42. }
  43. id<MTLComputeCommandEncoder> computeCommandEncoder()
  44. {
  45. return [m_obj computeCommandEncoder];
  46. }
  47. id<MTLBlitCommandEncoder> blitCommandEncoder()
  48. {
  49. return [m_obj blitCommandEncoder];
  50. }
  51. // Scheduling and Executing Commands
  52. void enqueue()
  53. {
  54. [m_obj enqueue];
  55. }
  56. void commit()
  57. {
  58. [m_obj commit];
  59. }
  60. void addCompletedHandler(mtlCallback _cb, void* _data)
  61. {
  62. [m_obj addCompletedHandler:^(id <MTLCommandBuffer>){ _cb(_data); }];
  63. }
  64. void presentDrawable(id<MTLDrawable> _drawable)
  65. {
  66. [m_obj presentDrawable:_drawable];
  67. }
  68. void waitUntilCompleted()
  69. {
  70. [m_obj waitUntilCompleted];
  71. }
  72. MTL_CLASS_END
  73. MTL_CLASS(CommandQueue)
  74. id<MTLCommandBuffer> commandBuffer()
  75. {
  76. return [m_obj commandBuffer];
  77. }
  78. id<MTLCommandBuffer> commandBufferWithUnretainedReferences()
  79. {
  80. return [m_obj commandBufferWithUnretainedReferences];
  81. }
  82. MTL_CLASS_END
  83. MTL_CLASS(ComputeCommandEncoder)
  84. void setComputePipelineState(id<MTLComputePipelineState> _state)
  85. {
  86. [m_obj setComputePipelineState:_state];
  87. }
  88. void setBuffer(id<MTLBuffer> _buffer, NSUInteger _offset, NSUInteger _index)
  89. {
  90. [m_obj setBuffer:_buffer offset:_offset atIndex:_index];
  91. }
  92. void setTexture(id<MTLTexture> _texture, NSUInteger _index)
  93. {
  94. [m_obj setTexture:_texture atIndex:_index];
  95. }
  96. void setSamplerState(id<MTLSamplerState> _sampler, NSUInteger _index)
  97. {
  98. [m_obj setSamplerState:_sampler atIndex:_index];
  99. }
  100. void endEncoding()
  101. {
  102. [m_obj endEncoding];
  103. }
  104. MTL_CLASS_END
  105. MTL_CLASS(Device)
  106. bool supportsFeatureSet(MTLFeatureSet _featureSet)
  107. {
  108. return [m_obj supportsFeatureSet:_featureSet];
  109. }
  110. id<MTLLibrary> newLibraryWithData(const void* _data)
  111. {
  112. NSError* error;
  113. id<MTLLibrary> lib = [m_obj newLibraryWithData:(dispatch_data_t)_data error:&error];
  114. BX_WARN(NULL == error
  115. , "newLibraryWithData failed: %s"
  116. , [error.localizedDescription cStringUsingEncoding:NSASCIIStringEncoding]
  117. );
  118. return lib;
  119. }
  120. id<MTLLibrary> newLibraryWithSource(const char* _source)
  121. {
  122. NSError* error;
  123. id<MTLLibrary> lib = [m_obj newLibraryWithSource:@(_source) options:nil error:&error];
  124. BX_WARN(NULL == error
  125. , "Shader compilation failed: %s"
  126. , [error.localizedDescription cStringUsingEncoding:NSASCIIStringEncoding]
  127. );
  128. return lib;
  129. }
  130. id<MTLCommandQueue> newCommandQueue()
  131. {
  132. return [m_obj newCommandQueue];
  133. }
  134. id<MTLCommandQueue> newCommandQueueWithMaxCommandBufferCount(NSUInteger _maxCommandBufferCount)
  135. {
  136. return [m_obj newCommandQueueWithMaxCommandBufferCount:_maxCommandBufferCount];
  137. }
  138. // Creating Resources
  139. id<MTLBuffer> newBufferWithLength(unsigned int _length, MTLResourceOptions _options)
  140. {
  141. return [m_obj newBufferWithLength:_length options:_options ];
  142. }
  143. id<MTLBuffer> newBufferWithBytes(const void* _pointer, NSUInteger _length, MTLResourceOptions _options)
  144. {
  145. return [m_obj newBufferWithBytes:_pointer length:_length options:_options];
  146. }
  147. id<MTLTexture> newTextureWithDescriptor(MTLTextureDescriptor* _descriptor)
  148. {
  149. return [m_obj newTextureWithDescriptor:_descriptor];
  150. }
  151. id<MTLSamplerState> newSamplerStateWithDescriptor(MTLSamplerDescriptor* _descriptor)
  152. {
  153. return [m_obj newSamplerStateWithDescriptor:_descriptor];
  154. }
  155. // Creating Command Objects Needed to Render Graphics
  156. id<MTLDepthStencilState> newDepthStencilStateWithDescriptor(MTLDepthStencilDescriptor* _descriptor)
  157. {
  158. return [m_obj newDepthStencilStateWithDescriptor:_descriptor];
  159. }
  160. id <MTLRenderPipelineState> newRenderPipelineStateWithDescriptor(MTLRenderPipelineDescriptor* _descriptor)
  161. {
  162. NSError* error;
  163. id <MTLRenderPipelineState> state = [m_obj newRenderPipelineStateWithDescriptor:_descriptor error:&error];
  164. BX_WARN(NULL == error
  165. , "newRenderPipelineStateWithDescriptor failed: %s"
  166. , [error.localizedDescription cStringUsingEncoding:NSASCIIStringEncoding]
  167. );
  168. return state;
  169. }
  170. id <MTLRenderPipelineState> newRenderPipelineStateWithDescriptor(MTLRenderPipelineDescriptor* _descriptor, MTLPipelineOption _options, MTLRenderPipelineReflection** _reflection)
  171. {
  172. NSError* error;
  173. id <MTLRenderPipelineState> state = [m_obj newRenderPipelineStateWithDescriptor:_descriptor options:_options reflection:_reflection error:&error];
  174. BX_WARN(NULL == error
  175. , "newRenderPipelineStateWithDescriptor failed: %s"
  176. , [error.localizedDescription cStringUsingEncoding:NSASCIIStringEncoding]
  177. );
  178. return state;
  179. }
  180. // Creating Command Objects Needed to Perform Computational Tasks
  181. id <MTLComputePipelineState> newComputePipelineStateWithFunction(id <MTLFunction> _computeFunction)
  182. {
  183. NSError* error;
  184. id <MTLComputePipelineState> state = [m_obj newComputePipelineStateWithFunction:_computeFunction error:&error];
  185. BX_WARN(NULL == error
  186. , "newComputePipelineStateWithFunction failed: %s"
  187. , [error.localizedDescription cStringUsingEncoding:NSASCIIStringEncoding]
  188. );
  189. return state;
  190. }
  191. MTL_CLASS_END
  192. MTL_CLASS(Function)
  193. NSArray* vertexAttributes() { return m_obj.vertexAttributes; }
  194. MTL_CLASS_END
  195. MTL_CLASS(Library)
  196. id <MTLFunction> newFunctionWithName(const char* _functionName) { return [m_obj newFunctionWithName:@(_functionName)]; }
  197. MTL_CLASS_END
  198. MTL_CLASS(RenderCommandEncoder)
  199. // Setting Graphics Rendering State
  200. void setBlendColor(float _red, float _green, float _blue, float _alpha)
  201. {
  202. [m_obj setBlendColorRed:_red green:_green blue:_blue alpha:_alpha];
  203. }
  204. void setCullMode(MTLCullMode _cullMode)
  205. {
  206. [m_obj setCullMode:_cullMode];
  207. }
  208. void setDepthBias(float _depthBias, float _slopeScale, float _clamp)
  209. {
  210. [m_obj setDepthBias:_depthBias slopeScale:_slopeScale clamp:_clamp];
  211. }
  212. void setDepthStencilState(id<MTLDepthStencilState> _depthStencilState)
  213. {
  214. [m_obj setDepthStencilState:_depthStencilState];
  215. }
  216. void setFrontFacingWinding(MTLWinding _frontFacingWinding)
  217. {
  218. [m_obj setFrontFacingWinding:_frontFacingWinding];
  219. }
  220. void setRenderPipelineState(id<MTLRenderPipelineState> _pipelineState)
  221. {
  222. [m_obj setRenderPipelineState:_pipelineState];
  223. }
  224. void setScissorRect(MTLScissorRect _rect)
  225. {
  226. [m_obj setScissorRect:_rect];
  227. }
  228. void setStencilReferenceValue(uint32_t _ref)
  229. {
  230. [m_obj setStencilReferenceValue:_ref];
  231. }
  232. void setTriangleFillMode(MTLTriangleFillMode _fillMode)
  233. {
  234. [m_obj setTriangleFillMode:_fillMode];
  235. }
  236. void setViewport(MTLViewport _viewport)
  237. {
  238. [m_obj setViewport:_viewport];
  239. }
  240. void setVisibilityResultMode(MTLVisibilityResultMode _mode, NSUInteger _offset)
  241. {
  242. [m_obj setVisibilityResultMode:_mode offset:_offset];
  243. }
  244. // Specifying Resources for a Vertex Function
  245. void setVertexBuffer(id<MTLBuffer> _buffer, NSUInteger _offset, NSUInteger _index)
  246. {
  247. [m_obj setVertexBuffer:_buffer offset:_offset atIndex:_index];
  248. }
  249. void setVertexSamplerState(id<MTLSamplerState> _sampler, NSUInteger _index)
  250. {
  251. [m_obj setVertexSamplerState:_sampler atIndex:_index];
  252. }
  253. void setVertexTexture(id<MTLTexture> _texture, NSUInteger _index)
  254. {
  255. [m_obj setVertexTexture:_texture atIndex:_index];
  256. }
  257. // Specifying Resources for a Fragment Function
  258. void setFragmentBuffer(id<MTLBuffer> _buffer, NSUInteger _offset, NSUInteger _index)
  259. {
  260. [m_obj setFragmentBuffer:_buffer offset:_offset atIndex:_index];
  261. }
  262. void setFragmentSamplerState(id<MTLSamplerState> _sampler, NSUInteger _index)
  263. {
  264. [m_obj setFragmentSamplerState:_sampler atIndex:_index];
  265. }
  266. void setFragmentTexture(id<MTLTexture> _texture, NSUInteger _index)
  267. {
  268. [m_obj setFragmentTexture:_texture atIndex:_index];
  269. }
  270. //Drawing Geometric Primitives
  271. //NOTE: not exposing functions without instanceCount, it seems they are just wrappers
  272. void drawIndexedPrimitives(MTLPrimitiveType _primitiveType, NSUInteger _indexCount, MTLIndexType _indexType, id<MTLBuffer> _indexBuffer, NSUInteger _indexBufferOffset, NSUInteger _instanceCount)
  273. {
  274. [m_obj drawIndexedPrimitives:_primitiveType indexCount:_indexCount indexType:_indexType indexBuffer:_indexBuffer indexBufferOffset:_indexBufferOffset instanceCount:_instanceCount];
  275. }
  276. void drawPrimitives(MTLPrimitiveType _primitiveType, NSUInteger _vertexStart, NSUInteger _vertexCount, NSUInteger _instanceCount)
  277. {
  278. [m_obj drawPrimitives:_primitiveType vertexStart:_vertexStart vertexCount:_vertexCount instanceCount:_instanceCount];
  279. }
  280. void insertDebugSignpost(const char* _string)
  281. {
  282. [m_obj insertDebugSignpost:@(_string)];
  283. }
  284. void pushDebugGroup(const char* _string)
  285. {
  286. [m_obj pushDebugGroup:@(_string)];
  287. }
  288. void popDebugGroup()
  289. {
  290. [m_obj popDebugGroup];
  291. }
  292. void endEncoding()
  293. {
  294. [m_obj endEncoding];
  295. }
  296. MTL_CLASS_END
  297. MTL_CLASS(Texture)
  298. // Copying Data into a Texture Image
  299. void replaceRegion(MTLRegion _region, NSUInteger _level, NSUInteger _slice, const void* _pixelBytes, NSUInteger _bytesPerRow, NSUInteger _bytesPerImage)
  300. {
  301. [m_obj replaceRegion:_region mipmapLevel:_level slice:_slice withBytes:_pixelBytes bytesPerRow:_bytesPerRow bytesPerImage:_bytesPerImage];
  302. }
  303. // Copying Data from a Texture Image
  304. void getBytes(void* _pixelBytes, NSUInteger _bytesPerRow, NSUInteger _bytesPerImage, MTLRegion _region, NSUInteger _mipmapLevel, NSUInteger _slice)
  305. {
  306. [m_obj getBytes:_pixelBytes bytesPerRow:_bytesPerRow bytesPerImage:_bytesPerImage fromRegion:_region mipmapLevel:_mipmapLevel slice:_slice];
  307. }
  308. // Creating Textures by Reusing Image Data
  309. id<MTLTexture> newTextureViewWithPixelFormat(MTLPixelFormat _pixelFormat)
  310. {
  311. return [m_obj newTextureViewWithPixelFormat:_pixelFormat];
  312. }
  313. //properties
  314. uint32_t width()
  315. {
  316. return (uint32_t)m_obj.width;
  317. }
  318. uint32_t height()
  319. {
  320. return (uint32_t)m_obj.height;
  321. }
  322. MTLPixelFormat pixelFormat() const
  323. {
  324. return m_obj.pixelFormat;
  325. }
  326. MTL_CLASS_END
  327. typedef id<MTLComputePipelineState> ComputePipelineState;
  328. typedef id<MTLDepthStencilState> DepthStencilState;
  329. typedef id<MTLRenderPipelineState> RenderPipelineState;
  330. typedef id<MTLSamplerState> SamplerState;
  331. //descriptors
  332. //NOTE: [class new] is same as [[class alloc] init]
  333. typedef MTLRenderPipelineDescriptor* RenderPipelineDescriptor;
  334. inline RenderPipelineDescriptor newRenderPipelineDescriptor()
  335. {
  336. return [MTLRenderPipelineDescriptor new];
  337. }
  338. inline void reset(RenderPipelineDescriptor _desc)
  339. {
  340. [_desc reset];
  341. }
  342. typedef MTLRenderPipelineColorAttachmentDescriptor* RenderPipelineColorAttachmentDescriptor;
  343. typedef MTLDepthStencilDescriptor* DepthStencilDescriptor;
  344. inline MTLDepthStencilDescriptor* newDepthStencilDescriptor()
  345. {
  346. return [MTLDepthStencilDescriptor new];
  347. }
  348. typedef MTLStencilDescriptor* StencilDescriptor;
  349. inline MTLStencilDescriptor* newStencilDescriptor()
  350. {
  351. return [MTLStencilDescriptor new];
  352. }
  353. typedef MTLRenderPassColorAttachmentDescriptor* RenderPassColorAttachmentDescriptor;
  354. typedef MTLRenderPassDepthAttachmentDescriptor* RenderPassDepthAttachmentDescriptor;
  355. typedef MTLRenderPassStencilAttachmentDescriptor* RenderPassStencilAttachmentDescriptor;
  356. typedef MTLRenderPassDescriptor* RenderPassDescriptor;
  357. inline MTLRenderPassDescriptor* newRenderPassDescriptor()
  358. {
  359. return [MTLRenderPassDescriptor new];
  360. }
  361. typedef MTLVertexDescriptor* VertexDescriptor;
  362. inline MTLVertexDescriptor* newVertexDescriptor()
  363. {
  364. return [MTLVertexDescriptor new];
  365. }
  366. inline void reset(VertexDescriptor _desc)
  367. {
  368. [_desc reset];
  369. }
  370. typedef MTLSamplerDescriptor* SamplerDescriptor;
  371. inline MTLSamplerDescriptor* newSamplerDescriptor()
  372. {
  373. return [MTLSamplerDescriptor new];
  374. }
  375. typedef MTLTextureDescriptor* TextureDescriptor;
  376. inline MTLTextureDescriptor* newTextureDescriptor()
  377. {
  378. return [MTLTextureDescriptor new];
  379. }
  380. typedef MTLRenderPipelineReflection* RenderPipelineReflection;
  381. //helper functions
  382. inline void release(NSObject* _obj)
  383. {
  384. [_obj release];
  385. }
  386. inline void retain(NSObject* _obj)
  387. {
  388. [_obj retain];
  389. }
  390. inline const char* utf8String(NSString* _str)
  391. {
  392. return [_str UTF8String];
  393. }
  394. #define MTL_RELEASE(_obj) \
  395. BX_MACRO_BLOCK_BEGIN \
  396. [_obj release]; \
  397. _obj = nil; \
  398. BX_MACRO_BLOCK_END
  399. #if BX_PLATFORM_IOS
  400. inline bool OsVersionEqualOrGreater(const char* _version)
  401. {
  402. return ([[[UIDevice currentDevice] systemVersion] compare:@(_version) options:NSNumericSearch] != NSOrderedAscending);
  403. }
  404. //TODO: this could be in bx ?
  405. #endif //
  406. // end of c++ wrapper
  407. template <typename Ty>
  408. class StateCacheT
  409. {
  410. public:
  411. void add(uint64_t _id, Ty _item)
  412. {
  413. invalidate(_id);
  414. m_hashMap.insert(stl::make_pair(_id, _item) );
  415. }
  416. Ty find(uint64_t _id)
  417. {
  418. typename HashMap::iterator it = m_hashMap.find(_id);
  419. if (it != m_hashMap.end() )
  420. {
  421. return it->second;
  422. }
  423. return NULL;
  424. }
  425. void invalidate(uint64_t _id)
  426. {
  427. typename HashMap::iterator it = m_hashMap.find(_id);
  428. if (it != m_hashMap.end() )
  429. {
  430. MTL_RELEASE(it->second);
  431. m_hashMap.erase(it);
  432. }
  433. }
  434. void invalidate()
  435. {
  436. for (typename HashMap::iterator it = m_hashMap.begin(), itEnd = m_hashMap.end(); it != itEnd; ++it)
  437. {
  438. release(it->second);
  439. }
  440. m_hashMap.clear();
  441. }
  442. uint32_t getCount() const
  443. {
  444. return uint32_t(m_hashMap.size() );
  445. }
  446. private:
  447. typedef stl::unordered_map<uint64_t, Ty> HashMap;
  448. HashMap m_hashMap;
  449. };
  450. struct BufferMtl
  451. {
  452. BufferMtl()
  453. : m_buffer(NULL)
  454. , m_flags(BGFX_BUFFER_NONE)
  455. , m_dynamic(false)
  456. {
  457. }
  458. void create(uint32_t _size, void* _data, uint16_t _flags, uint16_t _stride = 0, bool _vertex = false);
  459. void update(uint32_t _offset, uint32_t _size, void* _data, bool _discard = false);
  460. void destroy()
  461. {
  462. if (NULL != m_buffer)
  463. {
  464. [m_buffer release];
  465. m_buffer = NULL;
  466. m_dynamic = false;
  467. }
  468. }
  469. Buffer m_buffer;
  470. uint32_t m_size;
  471. uint16_t m_flags;
  472. bool m_dynamic;
  473. };
  474. typedef BufferMtl IndexBufferMtl;
  475. struct VertexBufferMtl : public BufferMtl
  476. {
  477. VertexBufferMtl()
  478. : BufferMtl()
  479. {
  480. }
  481. void create(uint32_t _size, void* _data, VertexDeclHandle _declHandle, uint16_t _flags);
  482. VertexDeclHandle m_decl;
  483. };
  484. struct ShaderMtl
  485. {
  486. ShaderMtl()
  487. : m_function(NULL)
  488. {
  489. }
  490. void create(const Memory* _mem);
  491. void destroy()
  492. {
  493. MTL_RELEASE(m_function);
  494. }
  495. Function m_function;
  496. };
  497. struct ProgramMtl
  498. {
  499. ProgramMtl()
  500. : m_vsh(NULL)
  501. , m_fsh(NULL)
  502. , m_vshConstantBuffer(NULL)
  503. , m_fshConstantBuffer(NULL)
  504. , m_vshConstantBufferSize(0)
  505. , m_vshConstantBufferAlignmentMask(0)
  506. , m_fshConstantBufferSize(0)
  507. , m_fshConstantBufferAlignmentMask(0)
  508. , m_numPredefined(0)
  509. , m_processedUniforms(false)
  510. {
  511. }
  512. void create(const ShaderMtl* _vsh, const ShaderMtl* _fsh);
  513. void destroy();
  514. RenderPipelineState getRenderPipelineState(uint64_t _state, uint32_t _rgba, FrameBufferHandle _fbHandle, VertexDeclHandle _declHandle, uint16_t _numInstanceData);
  515. StateCacheT<RenderPipelineState> m_renderPipelineStateCache;
  516. uint8_t m_used[Attrib::Count+1]; // dense
  517. uint32_t m_attributes[Attrib::Count]; // sparse
  518. uint32_t m_instanceData[BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT+1];
  519. const ShaderMtl* m_vsh;
  520. const ShaderMtl* m_fsh;
  521. UniformBuffer* m_vshConstantBuffer;
  522. UniformBuffer* m_fshConstantBuffer;
  523. uint32_t m_vshConstantBufferSize;
  524. uint32_t m_vshConstantBufferAlignmentMask;
  525. uint32_t m_fshConstantBufferSize;
  526. uint32_t m_fshConstantBufferAlignmentMask;
  527. PredefinedUniform m_predefined[PredefinedUniform::Count*2];
  528. uint8_t m_numPredefined;
  529. bool m_processedUniforms;
  530. };
  531. struct TextureMtl
  532. {
  533. TextureMtl()
  534. : m_ptr(NULL)
  535. , m_ptrStencil(NULL)
  536. , m_sampler(NULL)
  537. , m_flags(0)
  538. , m_numMips(0)
  539. {
  540. }
  541. void create(const Memory* _mem, uint32_t _flags, uint8_t _skip);
  542. void destroy()
  543. {
  544. MTL_RELEASE(m_ptr);
  545. MTL_RELEASE(m_ptrStencil);
  546. }
  547. void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem);
  548. void commit(uint8_t _stage, uint32_t _flags = BGFX_TEXTURE_INTERNAL_DEFAULT_SAMPLER);
  549. Texture m_ptr;
  550. Texture m_ptrStencil; // for emulating packed depth/stencil formats - only for iOS8...
  551. SamplerState m_sampler;
  552. uint32_t m_flags;
  553. uint8_t m_requestedFormat;
  554. uint8_t m_textureFormat;
  555. uint8_t m_numMips;
  556. };
  557. struct FrameBufferMtl
  558. {
  559. FrameBufferMtl()
  560. : m_denseIdx(UINT16_MAX)
  561. , m_pixelFormatHash(0)
  562. , m_num(0)
  563. {
  564. m_depthHandle.idx = invalidHandle;
  565. }
  566. void create(uint8_t _num, const Attachment* _attachment);
  567. void create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat);
  568. void postReset();
  569. uint16_t destroy();
  570. // SwapChainMtl* m_swapChain;
  571. uint32_t m_width;
  572. uint32_t m_height;
  573. uint16_t m_denseIdx;
  574. uint32_t m_pixelFormatHash;
  575. TextureHandle m_colorHandle[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS-1];
  576. TextureHandle m_depthHandle;
  577. uint8_t m_num; // number of color handles
  578. };
  579. struct OcclusionQueryMTL
  580. {
  581. OcclusionQueryMTL()
  582. : m_control(BX_COUNTOF(m_query) )
  583. {
  584. }
  585. void postReset();
  586. void preReset();
  587. void begin(RenderCommandEncoder& _rce, Frame* _render, OcclusionQueryHandle _handle);
  588. void end(RenderCommandEncoder& _rce);
  589. void resolve(Frame* _render, bool _wait = false);
  590. struct Query
  591. {
  592. OcclusionQueryHandle m_handle;
  593. };
  594. Buffer m_buffer;
  595. Query m_query[BGFX_CONFIG_MAX_OCCUSION_QUERIES];
  596. bx::RingBufferControl m_control;
  597. };
  598. } /* namespace metal */ } // namespace bgfx
  599. #endif // BGFX_CONFIG_RENDERER_METAL
  600. #endif // BGFX_RENDERER_METAL_H_HEADER_GUARD