Browse Source

metal: fix a freeze issue with the main uniform buffer

Alex Szpakowski 5 years ago
parent
commit
426798ab10
1 changed files with 25 additions and 6 deletions
  1. 25 6
      src/modules/graphics/metal/Graphics.mm

+ 25 - 6
src/modules/graphics/metal/Graphics.mm

@@ -156,7 +156,6 @@ Graphics::Graphics()
 	initCapabilities();
 	initCapabilities();
 
 
 	uniformBuffer = CreateStreamBuffer(device, BUFFER_UNIFORM, 1024 * 1024 * 1);
 	uniformBuffer = CreateStreamBuffer(device, BUFFER_UNIFORM, 1024 * 1024 * 1);
-	uniformBufferData = uniformBuffer->map(uniformBuffer->getSize());
 
 
 	float defaultAttributes[4] = {0.0f, 0.0f, 0.0f, 1.0f};
 	float defaultAttributes[4] = {0.0f, 0.0f, 0.0f, 1.0f};
 	defaultAttributesBuffer = newBuffer(sizeof(float) * 4, defaultAttributes, BUFFER_VERTEX, vertex::USAGE_STATIC, 0);
 	defaultAttributesBuffer = newBuffer(sizeof(float) * 4, defaultAttributes, BUFFER_VERTEX, vertex::USAGE_STATIC, 0);
@@ -603,9 +602,11 @@ void Graphics::applyRenderState(id<MTLRenderCommandEncoder> encoder, const verte
 		id<MTLDepthStencilState> mtlstate = getCachedDepthStencilState(depth, stencil);
 		id<MTLDepthStencilState> mtlstate = getCachedDepthStencilState(depth, stencil);
 
 
 		[encoder setDepthStencilState:mtlstate];
 		[encoder setDepthStencilState:mtlstate];
-		[encoder setStencilReferenceValue:state.stencil.value];
 	}
 	}
 
 
+	if (dirtyState & STATEBIT_STENCIL)
+		[encoder setStencilReferenceValue:state.stencil.value];
+
 	dirtyRenderState = 0;
 	dirtyRenderState = 0;
 }
 }
 
 
@@ -651,15 +652,18 @@ void Graphics::applyShaderUniforms(id<MTLRenderCommandEncoder> renderEncoder, lo
 	data.constantColor = getColor();
 	data.constantColor = getColor();
 	gammaCorrectColor(data.constantColor);
 	gammaCorrectColor(data.constantColor);
 
 
-	if (uniformBufferData.size < uniformBufferOffset + size)
+	if (uniformBuffer->getSize() < uniformBufferOffset + size)
 	{
 	{
 		size_t newsize = uniformBuffer->getSize() * 2;
 		size_t newsize = uniformBuffer->getSize() * 2;
 		delete uniformBuffer;
 		delete uniformBuffer;
 		uniformBuffer = CreateStreamBuffer(device, BUFFER_UNIFORM, newsize);
 		uniformBuffer = CreateStreamBuffer(device, BUFFER_UNIFORM, newsize);
-		uniformBufferData = uniformBuffer->map(uniformBuffer->getSize());
+		uniformBufferData = {};
 		uniformBufferOffset = 0;
 		uniformBufferOffset = 0;
 	}
 	}
 
 
+	if (uniformBufferData.data == nullptr)
+		uniformBufferData = uniformBuffer->map(uniformBuffer->getSize());
+
 	memcpy(uniformBufferData.data + uniformBufferOffset, &data, sizeof(Shader::BuiltinUniformData));
 	memcpy(uniformBufferData.data + uniformBufferOffset, &data, sizeof(Shader::BuiltinUniformData));
 
 
 	id<MTLBuffer> buffer = (__bridge id<MTLBuffer>)(void *)uniformBuffer->getHandle();
 	id<MTLBuffer> buffer = (__bridge id<MTLBuffer>)(void *)uniformBuffer->getHandle();
@@ -1073,7 +1077,7 @@ void Graphics::present(void *screenshotCallbackData)
 	batchedDrawState.indexBuffer->nextFrame();
 	batchedDrawState.indexBuffer->nextFrame();
 
 
 	uniformBuffer->nextFrame();
 	uniformBuffer->nextFrame();
-	uniformBufferData = uniformBuffer->map(uniformBuffer->getSize());
+	uniformBufferData = {};
 	uniformBufferOffset = 0;
 	uniformBufferOffset = 0;
 
 
 	id<MTLCommandBuffer> cmd = getCommandBuffer();
 	id<MTLCommandBuffer> cmd = getCommandBuffer();
@@ -1183,11 +1187,26 @@ void Graphics::stopDrawToStencilBuffer()
 void Graphics::setStencilTest(CompareMode compare, int value)
 void Graphics::setStencilTest(CompareMode compare, int value)
 {
 {
 	// TODO
 	// TODO
+	DisplayState &state = states.back();
+	if (state.stencil.compare != compare || state.stencil.value != value)
+	{
+		state.stencil.compare = compare;
+		state.stencil.value = value;
+		dirtyRenderState |= STATEBIT_STENCIL;
+	}
 }
 }
 
 
 void Graphics::setDepthMode(CompareMode compare, bool write)
 void Graphics::setDepthMode(CompareMode compare, bool write)
 {
 {
-	// TODO
+	DisplayState &state = states.back();
+
+	if (state.depthTest != compare || state.depthWrite != write)
+	{
+		flushBatchedDraws();
+		state.depthTest = compare;
+		state.depthWrite = write;
+		dirtyRenderState |= STATEBIT_DEPTH;
+	}
 }
 }
 
 
 void Graphics::setFrontFaceWinding(vertex::Winding winding)
 void Graphics::setFrontFaceWinding(vertex::Winding winding)