Vulkan: Plumb the debug context down to SpirvShader

SpirvShader doesn't actually do anything with this (yet).
Extracts the dull plumbing from the meat of the SpirvShader debugger implementation.

Bug: b/145351270
Change-Id: I1a648d8e463c428a232f711d0cc5dd0ecf94ad1e
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/38914
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Ben Clayton <bclayton@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Pipeline/SpirvShader.cpp b/src/Pipeline/SpirvShader.cpp
index 2350195..0ee4bd4 100644
--- a/src/Pipeline/SpirvShader.cpp
+++ b/src/Pipeline/SpirvShader.cpp
@@ -29,7 +29,8 @@
     InsnStore const &insns,
     const vk::RenderPass *renderPass,
     uint32_t subpassIndex,
-    bool robustBufferAccess)
+    bool robustBufferAccess,
+    const std::shared_ptr<vk::dbg::Context> &dbgctx)
     : insns{ insns }
     , inputs{ MAX_INTERFACE_COMPONENTS }
     , outputs{ MAX_INTERFACE_COMPONENTS }
diff --git a/src/Pipeline/SpirvShader.hpp b/src/Pipeline/SpirvShader.hpp
index f32cc63..b7c3eeb 100644
--- a/src/Pipeline/SpirvShader.hpp
+++ b/src/Pipeline/SpirvShader.hpp
@@ -50,6 +50,10 @@
 class RenderPass;
 struct SampledImageDescriptor;
 
+namespace dbg {
+class Context;
+}  // namespace dbg
+
 }  // namespace vk
 
 namespace sw {
@@ -473,7 +477,8 @@
 	            InsnStore const &insns,
 	            const vk::RenderPass *renderPass,
 	            uint32_t subpassIndex,
-	            bool robustBufferAccess);
+	            bool robustBufferAccess,
+	            const std::shared_ptr<vk::dbg::Context> &dbgctx);
 
 	struct Modes
 	{
diff --git a/src/Vulkan/VkDevice.hpp b/src/Vulkan/VkDevice.hpp
index d83e8e8..0a19f50 100644
--- a/src/Vulkan/VkDevice.hpp
+++ b/src/Vulkan/VkDevice.hpp
@@ -98,12 +98,14 @@
 	rr::Routine *findInConstCache(const SamplingRoutineCache::Key &key) const;
 	void updateSamplingRoutineConstCache();
 
-#ifdef ENABLE_VK_DEBUGGER
 	std::shared_ptr<vk::dbg::Context> getDebuggerContext() const
 	{
+#ifdef ENABLE_VK_DEBUGGER
 		return debugger.context;
-	}
+#else
+		return nullptr;
 #endif  // ENABLE_VK_DEBUGGER
+	}
 
 private:
 	PhysicalDevice *const physicalDevice = nullptr;
diff --git a/src/Vulkan/VkPipeline.cpp b/src/Vulkan/VkPipeline.cpp
index a57867b..71ad391 100644
--- a/src/Vulkan/VkPipeline.cpp
+++ b/src/Vulkan/VkPipeline.cpp
@@ -212,7 +212,11 @@
 	return optimized;
 }
 
-std::shared_ptr<sw::SpirvShader> createShader(const vk::PipelineCache::SpirvShaderKey &key, const vk::ShaderModule *module, bool robustBufferAccess)
+std::shared_ptr<sw::SpirvShader> createShader(
+    const vk::PipelineCache::SpirvShaderKey &key,
+    const vk::ShaderModule *module,
+    bool robustBufferAccess,
+    const std::shared_ptr<vk::dbg::Context> &dbgctx)
 {
 	auto code = preprocessSpirv(key.getInsns(), key.getSpecializationInfo());
 	ASSERT(code.size() > 0);
@@ -223,7 +227,7 @@
 
 	// TODO(b/119409619): use allocator.
 	return std::make_shared<sw::SpirvShader>(codeSerialID, key.getPipelineStage(), key.getEntryPointName().c_str(),
-	                                         code, key.getRenderPass(), key.getSubpassIndex(), robustBufferAccess);
+	                                         code, key.getRenderPass(), key.getSubpassIndex(), robustBufferAccess, dbgctx);
 }
 
 std::shared_ptr<sw::ComputeProgram> createProgram(const vk::PipelineCache::ComputeProgramKey &key)
@@ -244,6 +248,7 @@
 
 Pipeline::Pipeline(PipelineLayout const *layout, const Device *device)
     : layout(layout)
+    , device(device)
     , robustBufferAccess(device->getEnabledFeatures().robustBufferAccess)
 {
 }
@@ -550,7 +555,7 @@
 				const std::shared_ptr<sw::SpirvShader> *spirvShader = pipelineCache[key];
 				if(!spirvShader)
 				{
-					auto shader = createShader(key, module, robustBufferAccess);
+					auto shader = createShader(key, module, robustBufferAccess, device->getDebuggerContext());
 					setShader(pipelineStage, shader);
 					pipelineCache.insert(key, getShader(pipelineStage));
 				}
@@ -562,7 +567,7 @@
 		}
 		else
 		{
-			auto shader = createShader(key, module, robustBufferAccess);
+			auto shader = createShader(key, module, robustBufferAccess, device->getDebuggerContext());
 			setShader(pipelineStage, shader);
 		}
 	}
@@ -650,7 +655,7 @@
 			const std::shared_ptr<sw::SpirvShader> *spirvShader = pipelineCache[shaderKey];
 			if(!spirvShader)
 			{
-				shader = createShader(shaderKey, module, robustBufferAccess);
+				shader = createShader(shaderKey, module, robustBufferAccess, device->getDebuggerContext());
 				pipelineCache.insert(shaderKey, shader);
 			}
 			else
@@ -676,7 +681,7 @@
 	}
 	else
 	{
-		shader = createShader(shaderKey, module, robustBufferAccess);
+		shader = createShader(shaderKey, module, robustBufferAccess, device->getDebuggerContext());
 		const PipelineCache::ComputeProgramKey programKey(shader.get(), layout);
 		program = createProgram(programKey);
 	}
diff --git a/src/Vulkan/VkPipeline.hpp b/src/Vulkan/VkPipeline.hpp
index ad52c8e..c2cb295 100644
--- a/src/Vulkan/VkPipeline.hpp
+++ b/src/Vulkan/VkPipeline.hpp
@@ -30,6 +30,10 @@
 
 namespace vk {
 
+namespace dbg {
+class Context;
+}  // namespace dbg
+
 class PipelineCache;
 class PipelineLayout;
 class ShaderModule;
@@ -68,6 +72,7 @@
 
 protected:
 	PipelineLayout const *layout = nullptr;
+	Device const *const device;
 
 	const bool robustBufferAccess = true;
 };
@@ -75,7 +80,9 @@
 class GraphicsPipeline : public Pipeline, public ObjectBase<GraphicsPipeline, VkPipeline>
 {
 public:
-	GraphicsPipeline(const VkGraphicsPipelineCreateInfo *pCreateInfo, void *mem, const Device *device);
+	GraphicsPipeline(const VkGraphicsPipelineCreateInfo *pCreateInfo,
+	                 void *mem,
+	                 const Device *device);
 	virtual ~GraphicsPipeline() = default;
 
 	void destroyPipeline(const VkAllocationCallbacks *pAllocator) override;