Бранимир Караџић 6 年之前
父节点
当前提交
7f56297766
共有 2 个文件被更改,包括 71 次插入59 次删除
  1. 4 4
      src/renderer_mtl.h
  2. 67 55
      src/renderer_mtl.mm

+ 4 - 4
src/renderer_mtl.h

@@ -140,7 +140,7 @@ namespace bgfx { namespace mtl
 		{
 		{
 			return (uint32_t)m_obj.length;
 			return (uint32_t)m_obj.length;
 		}
 		}
-	
+
 		void setLabel(const char* _label)
 		void setLabel(const char* _label)
 		{
 		{
 			[m_obj setLabel:@(_label)];
 			[m_obj setLabel:@(_label)];
@@ -783,7 +783,7 @@ namespace bgfx { namespace mtl
 		{
 		{
 			MTL_RELEASE(m_ptr);
 			MTL_RELEASE(m_ptr);
 
 
-			if (m_dynamic)
+			if (NULL != m_dynamic)
 			{
 			{
 				BX_DELETE(g_allocator, m_dynamic);
 				BX_DELETE(g_allocator, m_dynamic);
 				m_dynamic = NULL;
 				m_dynamic = NULL;
@@ -794,8 +794,8 @@ namespace bgfx { namespace mtl
 		uint16_t m_flags;
 		uint16_t m_flags;
 		bool     m_vertex;
 		bool     m_vertex;
 
 
-		Buffer 		m_ptr;
-		uint8_t* 	m_dynamic;
+		Buffer   m_ptr;
+		uint8_t* m_dynamic;
 	};
 	};
 
 
 	typedef BufferMtl IndexBufferMtl;
 	typedef BufferMtl IndexBufferMtl;

+ 67 - 55
src/renderer_mtl.mm

@@ -1071,44 +1071,44 @@ namespace bgfx { namespace mtl
 					, _blitter.m_vb->data
 					, _blitter.m_vb->data
 					, true
 					, true
 					);
 					);
-				
+
 				endEncoding();
 				endEncoding();
-				
+
 				uint32_t width  = m_resolution.width;
 				uint32_t width  = m_resolution.width;
 				uint32_t height = m_resolution.height;
 				uint32_t height = m_resolution.height;
-				
+
 				FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
 				FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
-				
+
 				RenderPassDescriptor renderPassDescriptor = newRenderPassDescriptor();
 				RenderPassDescriptor renderPassDescriptor = newRenderPassDescriptor();
-				
+
 				setFrameBuffer(renderPassDescriptor, fbh);
 				setFrameBuffer(renderPassDescriptor, fbh);
-				
+
 				renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionLoad;
 				renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionLoad;
 				renderPassDescriptor.colorAttachments[0].storeAction =
 				renderPassDescriptor.colorAttachments[0].storeAction =
 				NULL != renderPassDescriptor.colorAttachments[0].resolveTexture
 				NULL != renderPassDescriptor.colorAttachments[0].resolveTexture
 				? MTLStoreActionMultisampleResolve
 				? MTLStoreActionMultisampleResolve
 				: MTLStoreActionStore
 				: MTLStoreActionStore
 				;
 				;
-				
+
 				RenderCommandEncoder rce = m_commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor);
 				RenderCommandEncoder rce = m_commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor);
 				m_renderCommandEncoder = rce;
 				m_renderCommandEncoder = rce;
 				m_renderCommandEncoderFrameBufferHandle = fbh;
 				m_renderCommandEncoderFrameBufferHandle = fbh;
 				MTL_RELEASE(renderPassDescriptor);
 				MTL_RELEASE(renderPassDescriptor);
-				
+
 				MTLViewport viewport = { 0.0f, 0.0f, (float)width, (float)height, 0.0f, 1.0f};
 				MTLViewport viewport = { 0.0f, 0.0f, (float)width, (float)height, 0.0f, 1.0f};
 				rce.setViewport(viewport);
 				rce.setViewport(viewport);
 				MTLScissorRect rc = { 0,0,width,height };
 				MTLScissorRect rc = { 0,0,width,height };
 				rce.setScissorRect(rc);
 				rce.setScissorRect(rc);
 				rce.setCullMode(MTLCullModeNone);
 				rce.setCullMode(MTLCullModeNone);
-				
+
 				uint64_t state = 0
 				uint64_t state = 0
 				| BGFX_STATE_WRITE_RGB
 				| BGFX_STATE_WRITE_RGB
 				| BGFX_STATE_WRITE_A
 				| BGFX_STATE_WRITE_A
 				| BGFX_STATE_DEPTH_TEST_ALWAYS
 				| BGFX_STATE_DEPTH_TEST_ALWAYS
 				;
 				;
-				
+
 				setDepthStencilState(state);
 				setDepthStencilState(state);
-				
+
 				PipelineStateMtl* pso = getPipelineState(
 				PipelineStateMtl* pso = getPipelineState(
 														 state
 														 state
 														 , 0
 														 , 0
@@ -1118,31 +1118,31 @@ namespace bgfx { namespace mtl
 														 , 0
 														 , 0
 														 );
 														 );
 				rce.setRenderPipelineState(pso->m_rps);
 				rce.setRenderPipelineState(pso->m_rps);
-				
+
 				const uint32_t vertexUniformBufferSize   = pso->m_vshConstantBufferSize;
 				const uint32_t vertexUniformBufferSize   = pso->m_vshConstantBufferSize;
 				const uint32_t fragmentUniformBufferSize = pso->m_fshConstantBufferSize;
 				const uint32_t fragmentUniformBufferSize = pso->m_fshConstantBufferSize;
-				
+
 				if (vertexUniformBufferSize)
 				if (vertexUniformBufferSize)
 				{
 				{
 					m_uniformBufferVertexOffset = BX_ALIGN_MASK(m_uniformBufferVertexOffset, pso->m_vshConstantBufferAlignmentMask);
 					m_uniformBufferVertexOffset = BX_ALIGN_MASK(m_uniformBufferVertexOffset, pso->m_vshConstantBufferAlignmentMask);
 					rce.setVertexBuffer(m_uniformBuffer, m_uniformBufferVertexOffset, 0);
 					rce.setVertexBuffer(m_uniformBuffer, m_uniformBufferVertexOffset, 0);
 				}
 				}
-				
+
 				m_uniformBufferFragmentOffset = m_uniformBufferVertexOffset + vertexUniformBufferSize;
 				m_uniformBufferFragmentOffset = m_uniformBufferVertexOffset + vertexUniformBufferSize;
-				
+
 				if (0 != fragmentUniformBufferSize)
 				if (0 != fragmentUniformBufferSize)
 				{
 				{
 					m_uniformBufferFragmentOffset = BX_ALIGN_MASK(m_uniformBufferFragmentOffset, pso->m_fshConstantBufferAlignmentMask);
 					m_uniformBufferFragmentOffset = BX_ALIGN_MASK(m_uniformBufferFragmentOffset, pso->m_fshConstantBufferAlignmentMask);
 					rce.setFragmentBuffer(m_uniformBuffer, m_uniformBufferFragmentOffset, 0);
 					rce.setFragmentBuffer(m_uniformBuffer, m_uniformBufferFragmentOffset, 0);
 				}
 				}
-				
+
 				float proj[16];
 				float proj[16];
 				bx::mtxOrtho(proj, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f, 0.0f, false);
 				bx::mtxOrtho(proj, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f, 0.0f, false);
-				
+
 				PredefinedUniform& predefined = pso->m_predefined[0];
 				PredefinedUniform& predefined = pso->m_predefined[0];
 				uint8_t flags = predefined.m_type;
 				uint8_t flags = predefined.m_type;
 				setShaderUniform(flags, predefined.m_loc, proj, 4);
 				setShaderUniform(flags, predefined.m_loc, proj, 4);
-				
+
 				m_textures[_blitter.m_texture.idx].commit(0, false, true);
 				m_textures[_blitter.m_texture.idx].commit(0, false, true);
 
 
 				VertexBufferMtl& vb = m_vertexBuffers[_blitter.m_vb->handle.idx];
 				VertexBufferMtl& vb = m_vertexBuffers[_blitter.m_vb->handle.idx];
@@ -1497,7 +1497,7 @@ namespace bgfx { namespace mtl
 				m_uniformBufferVertexOffset = BX_ALIGN_MASK(m_uniformBufferVertexOffset, pso->m_vshConstantBufferAlignmentMask);
 				m_uniformBufferVertexOffset = BX_ALIGN_MASK(m_uniformBufferVertexOffset, pso->m_vshConstantBufferAlignmentMask);
 				m_renderCommandEncoder.setVertexBuffer(m_uniformBuffer, m_uniformBufferVertexOffset, 0);
 				m_renderCommandEncoder.setVertexBuffer(m_uniformBuffer, m_uniformBufferVertexOffset, 0);
 			}
 			}
-			
+
 			m_uniformBufferFragmentOffset = m_uniformBufferVertexOffset + vertexUniformBufferSize;
 			m_uniformBufferFragmentOffset = m_uniformBufferVertexOffset + vertexUniformBufferSize;
 			if (fragmentUniformBufferSize)
 			if (fragmentUniformBufferSize)
 			{
 			{
@@ -1544,7 +1544,7 @@ namespace bgfx { namespace mtl
 						, mrtClearColor
 						, mrtClearColor
 						, bx::uint32_min(fragmentUniformBufferSize, sizeof(mrtClearColor) )
 						, bx::uint32_min(fragmentUniformBufferSize, sizeof(mrtClearColor) )
 						);
 						);
-			
+
 
 
 			m_uniformBufferFragmentOffset += fragmentUniformBufferSize;
 			m_uniformBufferFragmentOffset += fragmentUniformBufferSize;
 			m_uniformBufferVertexOffset    = m_uniformBufferFragmentOffset;
 			m_uniformBufferVertexOffset    = m_uniformBufferFragmentOffset;
@@ -2201,7 +2201,7 @@ namespace bgfx { namespace mtl
 			if (NULL == m_blitCommandEncoder)
 			if (NULL == m_blitCommandEncoder)
 			{
 			{
 				endEncoding();
 				endEncoding();
-				
+
 				if (NULL == m_commandBuffer)
 				if (NULL == m_commandBuffer)
 				{
 				{
 					m_commandBuffer = m_cmd.alloc();
 					m_commandBuffer = m_cmd.alloc();
@@ -2485,7 +2485,7 @@ namespace bgfx { namespace mtl
 	void BufferMtl::create(uint32_t _size, void* _data, uint16_t _flags, uint16_t _stride, bool _vertex)
 	void BufferMtl::create(uint32_t _size, void* _data, uint16_t _flags, uint16_t _stride, bool _vertex)
 	{
 	{
 		BX_UNUSED(_stride);
 		BX_UNUSED(_stride);
-		
+
 		m_size = _size;
 		m_size = _size;
 		m_flags = _flags;
 		m_flags = _flags;
 		m_vertex = _vertex;
 		m_vertex = _vertex;
@@ -3219,7 +3219,7 @@ namespace bgfx { namespace mtl
 		return m_activeCommandBuffer;
 		return m_activeCommandBuffer;
 	}
 	}
 
 
-	static void commandBufferFinishedCallback(void* _data)
+	inline void commandBufferFinishedCallback(void* _data)
 	{
 	{
 		CommandQueueMtl* queue = (CommandQueueMtl*)_data;
 		CommandQueueMtl* queue = (CommandQueueMtl*)_data;
 
 
@@ -3402,8 +3402,10 @@ namespace bgfx { namespace mtl
 
 
 	void RendererContextMtl::submitBlit(BlitState& _bs, uint16_t _view)
 	void RendererContextMtl::submitBlit(BlitState& _bs, uint16_t _view)
 	{
 	{
-		if (!_bs.hasItem(_view))
+		if (!_bs.hasItem(_view) )
+		{
 			return;
 			return;
+		}
 
 
 		endEncoding();
 		endEncoding();
 
 
@@ -3674,11 +3676,11 @@ namespace bgfx { namespace mtl
 							}
 							}
 
 
 							clearWithRenderPass = true
 							clearWithRenderPass = true
-							&& 0 == viewRect.m_x
-							&& 0      == viewRect.m_y
-							&& width  == viewRect.m_width
-							&& height == viewRect.m_height
-							;
+								&& 0      == viewRect.m_x
+								&& 0      == viewRect.m_y
+								&& width  == viewRect.m_width
+								&& height == viewRect.m_height
+								;
 
 
 							setFrameBuffer(renderPassDescriptor, fbh);
 							setFrameBuffer(renderPassDescriptor, fbh);
 
 
@@ -3728,13 +3730,13 @@ namespace bgfx { namespace mtl
 								{
 								{
 									depthAttachment.clearDepth = clr.m_depth;
 									depthAttachment.clearDepth = clr.m_depth;
 									depthAttachment.loadAction = 0 != (BGFX_CLEAR_DEPTH & clr.m_flags)
 									depthAttachment.loadAction = 0 != (BGFX_CLEAR_DEPTH & clr.m_flags)
-									? MTLLoadActionClear
-									: MTLLoadActionLoad
-									;
-									depthAttachment.storeAction = NULL != m_mainFrameBuffer.m_swapChain->m_backBufferColorMsaa
-									? MTLStoreActionDontCare
-									: MTLStoreActionStore
-									;
+										? MTLLoadActionClear
+										: MTLLoadActionLoad
+										;
+										depthAttachment.storeAction = NULL != m_mainFrameBuffer.m_swapChain->m_backBufferColorMsaa
+										? MTLStoreActionDontCare
+										: MTLStoreActionStore
+										;
 								}
 								}
 
 
 								RenderPassStencilAttachmentDescriptor stencilAttachment = renderPassDescriptor.stencilAttachment;
 								RenderPassStencilAttachmentDescriptor stencilAttachment = renderPassDescriptor.stencilAttachment;
@@ -3743,13 +3745,13 @@ namespace bgfx { namespace mtl
 								{
 								{
 									stencilAttachment.clearStencil = clr.m_stencil;
 									stencilAttachment.clearStencil = clr.m_stencil;
 									stencilAttachment.loadAction   = 0 != (BGFX_CLEAR_STENCIL & clr.m_flags)
 									stencilAttachment.loadAction   = 0 != (BGFX_CLEAR_STENCIL & clr.m_flags)
-									? MTLLoadActionClear
-									: MTLLoadActionLoad
-									;
+										? MTLLoadActionClear
+										: MTLLoadActionLoad
+										;
 									stencilAttachment.storeAction = NULL != m_mainFrameBuffer.m_swapChain->m_backBufferColorMsaa
 									stencilAttachment.storeAction = NULL != m_mainFrameBuffer.m_swapChain->m_backBufferColorMsaa
-									? MTLStoreActionDontCare
-									: MTLStoreActionStore
-									;
+										? MTLStoreActionDontCare
+										: MTLStoreActionStore
+										;
 								}
 								}
 							}
 							}
 							else
 							else
@@ -3863,7 +3865,7 @@ namespace bgfx { namespace mtl
 					if (isValid(currentProgram)
 					if (isValid(currentProgram)
 					&&  NULL != currentPso)
 					&&  NULL != currentPso)
 					{
 					{
-						uint32_t vertexUniformBufferSize   = currentPso->m_vshConstantBufferSize;
+						uint32_t vertexUniformBufferSize = currentPso->m_vshConstantBufferSize;
 
 
 						if (0 != vertexUniformBufferSize)
 						if (0 != vertexUniformBufferSize)
 						{
 						{
@@ -3898,7 +3900,7 @@ namespace bgfx { namespace mtl
 									TextureMtl& texture = m_textures[bind.m_idx];
 									TextureMtl& texture = m_textures[bind.m_idx];
 									m_computeCommandEncoder.setTexture(texture.getTextureMipLevel(bind.m_mip), stage);
 									m_computeCommandEncoder.setTexture(texture.getTextureMipLevel(bind.m_mip), stage);
 								}
 								}
-									break;
+								break;
 
 
 								case Binding::Texture:
 								case Binding::Texture:
 								{
 								{
@@ -3907,13 +3909,13 @@ namespace bgfx { namespace mtl
 
 
 									m_computeCommandEncoder.setTexture(texture.m_ptr, stage);
 									m_computeCommandEncoder.setTexture(texture.m_ptr, stage);
 									m_computeCommandEncoder.setSamplerState(
 									m_computeCommandEncoder.setSamplerState(
-																			0 == (BGFX_SAMPLER_INTERNAL_DEFAULT & flags)
-																			? getSamplerState(flags)
-																			: texture.m_sampler
-																			, stage
-																			);
+										0 == (BGFX_SAMPLER_INTERNAL_DEFAULT & flags)
+										? getSamplerState(flags)
+										: texture.m_sampler
+										, stage
+										);
 								}
 								}
-									break;
+								break;
 
 
 								case Binding::IndexBuffer:
 								case Binding::IndexBuffer:
 								case Binding::VertexBuffer:
 								case Binding::VertexBuffer:
@@ -3924,12 +3926,16 @@ namespace bgfx { namespace mtl
 									;
 									;
 									m_computeCommandEncoder.setBuffer(buffer.m_ptr, 0, stage + 1);
 									m_computeCommandEncoder.setBuffer(buffer.m_ptr, 0, stage + 1);
 								}
 								}
-									break;
+								break;
 							}
 							}
 						}
 						}
 					}
 					}
 
 
-					MTLSize threadsPerGroup = MTLSizeMake(currentPso->m_numThreads[0], currentPso->m_numThreads[1], currentPso->m_numThreads[2]);
+					MTLSize threadsPerGroup = MTLSizeMake(
+						  currentPso->m_numThreads[0]
+						, currentPso->m_numThreads[1]
+						, currentPso->m_numThreads[2]
+						);
 
 
 					if (isValid(compute.m_indirectBuffer) )
 					if (isValid(compute.m_indirectBuffer) )
 					{
 					{
@@ -3943,14 +3949,20 @@ namespace bgfx { namespace mtl
 						uint32_t args = compute.m_startIndirect * BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
 						uint32_t args = compute.m_startIndirect * BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
 						for (uint32_t ii = 0; ii < numDrawIndirect; ++ii)
 						for (uint32_t ii = 0; ii < numDrawIndirect; ++ii)
 						{
 						{
-							m_computeCommandEncoder.dispatchThreadgroupsWithIndirectBuffer(vb.m_ptr, args, threadsPerGroup);
+							m_computeCommandEncoder.dispatchThreadgroupsWithIndirectBuffer(
+								  vb.m_ptr
+								, args
+								, threadsPerGroup
+								);
 							args += BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
 							args += BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
 						}
 						}
 					}
 					}
 					else
 					else
 					{
 					{
-						m_computeCommandEncoder.dispatchThreadgroups(MTLSizeMake(compute.m_numX, compute.m_numY, compute.m_numZ)
-																	 ,threadsPerGroup);
+						m_computeCommandEncoder.dispatchThreadgroups(
+							  MTLSizeMake(compute.m_numX, compute.m_numY, compute.m_numZ)
+							, threadsPerGroup
+							);
 					}
 					}
 					continue;
 					continue;
 				}
 				}