Merge pull request #1415 from billhollings/renderpass-testing-fixes

Fix pipeline barriers not working inside self-dependent subpasses on Apple GPUs.
diff --git a/Docs/Whats_New.md b/Docs/Whats_New.md
index 7ea2acb..ea3fd4f 100644
--- a/Docs/Whats_New.md
+++ b/Docs/Whats_New.md
@@ -23,6 +23,7 @@
 - Fix issue with `vkCmdBlitImage()` from compressed textures.
 - Fix incorrect translation of clear color values on Apple Silicon.
 - Fix swizzle of depth and stencil values into RGBA (`float4`) variable in shaders.
+- Fix pipeline barriers not working inside self-dependent subpasses on Apple GPUs.
 - Disable `VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT` for 
   `VK_FORMAT_E5B9G9R9_UFLOAT_PACK32` on macOS Apple Silicon.
 - Support alpha-to-coverage without a color attachment.
diff --git a/MoltenVK/MoltenVK/Commands/MVKCmdPipeline.mm b/MoltenVK/MoltenVK/Commands/MVKCmdPipeline.mm
index bb5124d..a927d00 100644
--- a/MoltenVK/MoltenVK/Commands/MVKCmdPipeline.mm
+++ b/MoltenVK/MoltenVK/Commands/MVKCmdPipeline.mm
@@ -113,6 +113,28 @@
 	}
 #endif
 
+#if MVK_APPLE_SILICON
+	// Apple GPUs do not support renderpass barriers, and do not support rendering/writing
+	// to an attachment and then reading from that attachment within a single renderpass.
+	// So, in the case where we are inside a Metal renderpass, we need to split those activities
+	// into separate Metal renderpasses. Since this is a potentially expensive operation,
+	// verify that at least one attachment is being used both as an input and render attachment
+	// by checking for a VK_IMAGE_LAYOUT_GENERAL layout.
+	if (cmdEncoder->_mtlRenderEncoder) {
+		bool needsRenderpassRestart = false;
+		for (auto& b : _barriers) {
+			if (b.type == MVKPipelineBarrier::Image && b.newLayout == VK_IMAGE_LAYOUT_GENERAL) {
+				needsRenderpassRestart = true;
+				break;
+			}
+		}
+		if (needsRenderpassRestart) {
+			cmdEncoder->encodeStoreActions(true);
+			cmdEncoder->beginMetalRenderPass(true);
+		}
+	}
+#endif
+
 	MVKDevice* mvkDvc = cmdEncoder->getDevice();
 	MVKCommandUse cmdUse = kMVKCommandUsePipelineBarrier;
 
diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm
index 7ef37f5..ebe0114 100644
--- a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm
+++ b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm
@@ -1465,10 +1465,6 @@
 			if (supportsMTLGPUFamily(Apple7)) {
 				_metalFeatures.maxQueryBufferSize = (256 * KIBI);
 			}
-		} else {
-			// Apple devices don't like barriers in render passes.
-			_metalFeatures.memoryBarriers = true;
-			_metalFeatures.textureBarriers = true;
 		}
 	} else
 #endif