Browse Source

Vulkan: Fix crash on shutdown due to unreleased resources

BearishSun 8 years ago
parent
commit
2f1caaa044

+ 9 - 0
Source/BansheeEngine/Include/BsIBLUtility.h

@@ -187,6 +187,12 @@ namespace bs { namespace ct
 	class BS_EXPORT IBLUtility
 	{
 	public:
+		/** Sets up any resources requires for operation. Must be called before any other methods in this class. */
+		static void startUp();
+
+		/** Cleans up any resources allocated during startUp(). */
+		static void shutDown();
+
 		/**
 		 * Performs filtering on the cubemap, populating its mip-maps with filtered values that can be used for
 		 * evaluating specular reflections.
@@ -230,6 +236,9 @@ namespace bs { namespace ct
 		 * @param[in]   dstMip			Determines which mip level of the destination texture to scale.
 		 */
 		static void downsampleCubemap(const SPtr<Texture>& src, UINT32 srcMip, const SPtr<Texture>& dst, UINT32 dstMip);
+
+		struct Members;
+		static Members* m;
 	};
 
 	/** @} */

+ 2 - 2
Source/BansheeEngine/Include/BsRendererMaterialManager.h

@@ -2,9 +2,8 @@
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #pragma once
 
-#include "BsCorePrerequisites.h"
+#include "BsPrerequisites.h"
 #include "BsModule.h"
-#include "BsRendererMaterial.h"
 
 namespace bs
 {
@@ -45,6 +44,7 @@ namespace bs
 	private:
 		template<class T>
 		friend class RendererMaterial;
+		friend class ct::RendererMaterialBase;
 
 		/**	Initializes all materials on the core thread. */
 		static void initOnCore(const Vector<SPtr<ct::Shader>>& shaders);

+ 1 - 0
Source/BansheeEngine/Source/BsBuiltinResourcesHelper.cpp

@@ -7,6 +7,7 @@
 #include "BsShaderImportOptions.h"
 #include "BsTextureImportOptions.h"
 #include "BsRendererMaterialManager.h"
+#include "BsRendererMaterial.h"
 #include "BsFontImportOptions.h"
 #include "BsSpriteTexture.h"
 #include "BsTexture.h"

+ 27 - 16
Source/BansheeEngine/Source/BsIBLUtility.cpp

@@ -8,6 +8,28 @@
 
 namespace bs { namespace ct
 {
+	struct IBLUtility::Members
+	{
+		ReflectionCubeDownsampleMat downsampleMat;
+		ReflectionCubeImportanceSampleMat importanceSampleMat;
+
+		IrradianceComputeSHMat shCompute;
+		IrradianceReduceSHMat shReduce;
+		IrradianceProjectSHMat shProject;
+	};
+
+	IBLUtility::Members* IBLUtility::m = nullptr;
+
+	void IBLUtility::startUp()
+	{
+		m = bs_new<Members>();
+	}
+
+	void IBLUtility::shutDown()
+	{
+		bs_delete(m);
+	}
+
 	ReflectionCubeDownsampleParamDef gReflectionCubeDownsampleParamDef;
 
 	ReflectionCubeDownsampleMat::ReflectionCubeDownsampleMat()
@@ -231,9 +253,6 @@ namespace bs { namespace ct
 
 	void IBLUtility::filterCubemapForSpecular(const SPtr<Texture>& cubemap, const SPtr<Texture>& scratch)
 	{
-		static ReflectionCubeDownsampleMat downsampleMat;
-		static ReflectionCubeImportanceSampleMat importanceSampleMat;
-
 		auto& props = cubemap->getProperties();
 
 		SPtr<Texture> scratchCubemap = scratch;
@@ -285,7 +304,7 @@ namespace bs { namespace ct
 
 				SPtr<RenderTarget> target = RenderTexture::create(cubeFaceRTDesc);
 
-				importanceSampleMat.execute(scratchCubemap, face, mip, target);
+				m->importanceSampleMat.execute(scratchCubemap, face, mip, target);
 			}
 		}
 
@@ -295,17 +314,13 @@ namespace bs { namespace ct
 
 	void IBLUtility::filterCubemapForIrradiance(const SPtr<Texture>& cubemap, const SPtr<Texture>& output)
 	{
-		static IrradianceComputeSHMat shCompute;
-		static IrradianceReduceSHMat shReduce;
-		static IrradianceProjectSHMat shProject;
-
 		UINT32 numCoeffSets;
 		SPtr<GpuBuffer> coeffSetBuffer = IrradianceComputeSHMat::createOutputBuffer(cubemap, numCoeffSets);
 		for (UINT32 face = 0; face < 6; face++)
-			shCompute.execute(cubemap, face, coeffSetBuffer);
+			m->shCompute.execute(cubemap, face, coeffSetBuffer);
 
 		SPtr<GpuBuffer> coeffBuffer = IrradianceReduceSHMat::createOutputBuffer();
-		shReduce.execute(coeffSetBuffer, numCoeffSets, coeffBuffer);
+		m->shReduce.execute(coeffSetBuffer, numCoeffSets, coeffBuffer);
 
 		for (UINT32 face = 0; face < 6; face++)
 		{
@@ -316,14 +331,12 @@ namespace bs { namespace ct
 			cubeFaceRTDesc.colorSurfaces[0].mipLevel = 0;
 
 			SPtr<RenderTarget> target = RenderTexture::create(cubeFaceRTDesc);
-			shProject.execute(coeffBuffer, face, target);
+			m->shProject.execute(coeffBuffer, face, target);
 		}
 	}
 
 	void IBLUtility::scaleCubemap(const SPtr<Texture>& src, UINT32 srcMip, const SPtr<Texture>& dst, UINT32 dstMip)
 	{
-		static ReflectionCubeDownsampleMat downsampleMat;
-
 		auto& srcProps = src->getProperties();
 		auto& dstProps = dst->getProperties();
 
@@ -369,8 +382,6 @@ namespace bs { namespace ct
 
 	void IBLUtility::downsampleCubemap(const SPtr<Texture>& src, UINT32 srcMip, const SPtr<Texture>& dst, UINT32 dstMip)
 	{
-		static ReflectionCubeDownsampleMat downsampleMat;
-
 		for (UINT32 face = 0; face < 6; face++)
 		{
 			RENDER_TEXTURE_DESC cubeFaceRTDesc;
@@ -382,7 +393,7 @@ namespace bs { namespace ct
 			SPtr<RenderTarget> target = RenderTexture::create(cubeFaceRTDesc);
 
 			TextureSurface sourceSurface(srcMip, 1, 0, 6);
-			downsampleMat.execute(src, face, sourceSurface, target);
+			m->downsampleMat.execute(src, face, sourceSurface, target);
 		}
 	}
 }}

+ 2 - 3
Source/BansheeEngine/Source/BsRendererMaterial.cpp

@@ -1,9 +1,8 @@
 //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #include "BsRendererMaterial.h"
-#include "BsMaterial.h"
 
-namespace bs
+namespace bs { namespace ct
 {
 
-}
+}}

+ 1 - 0
Source/BansheeEngine/Source/BsRendererMaterialManager.cpp

@@ -1,6 +1,7 @@
 //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #include "BsRendererMaterialManager.h"
+#include "BsRendererMaterial.h"
 #include "BsBuiltinResources.h"
 #include "BsCoreThread.h"
 #include "BsShader.h"

+ 5 - 7
Source/BansheeEngine/Source/BsRendererUtility.cpp

@@ -7,15 +7,11 @@
 #include "BsMaterial.h"
 #include "BsGpuParamsSet.h"
 #include "BsPass.h"
-#include "BsBlendState.h"
-#include "BsDepthStencilState.h"
-#include "BsRasterizerState.h"
 #include "BsGpuParams.h"
-#include "BsGpuParamDesc.h"
-#include "BsGpuParamBlockBuffer.h"
 #include "BsShapeMeshes3D.h"
 #include "BsLight.h"
 #include "BsShader.h"
+#include "BsIBLUtility.h"
 
 namespace bs { namespace ct
 {
@@ -130,11 +126,13 @@ namespace bs { namespace ct
 		// TODO - When I add proper preprocessor support, merge these into a single material
 		mResolveMat = bs_shared_ptr_new<ResolveMat>();
 		mBlitMat = bs_shared_ptr_new<BlitMat>();
+
+		IBLUtility::startUp();
 	}
 
 	RendererUtility::~RendererUtility()
 	{
-
+		IBLUtility::shutDown();
 	}
 
 	void RendererUtility::setPass(const SPtr<Material>& material, UINT32 passIdx, UINT32 techniqueIdx)
@@ -397,4 +395,4 @@ namespace bs { namespace ct
 
 		mMaterial->updateParamsSet(mParamsSet);
 	}
-}}
+}}

+ 1 - 1
Source/BansheeVulkanRenderAPI/Source/BsVulkanOcclusionQuery.cpp

@@ -127,7 +127,7 @@ namespace bs { namespace ct
 			bool ready = true;
 			for (auto& query : mQueries)
 			{
-				UINT64 numSamples;
+				UINT64 numSamples = 0;
 				ready &= !query->isBound() && query->getResult(numSamples);
 
 				totalNumSamples += numSamples;

+ 2 - 1
Source/BansheeVulkanRenderAPI/Source/BsVulkanTimerQuery.cpp

@@ -149,7 +149,8 @@ namespace bs { namespace ct
 			bool ready = true;
 			for (auto& entry : mQueries)
 			{
-				UINT64 timeBegin, timeEnd;
+				UINT64 timeBegin = 0;
+				UINT64 timeEnd = 0;
 				ready &= !entry.first->isBound() && entry.first->getResult(timeBegin);
 				ready &= !entry.second->isBound() && entry.second->getResult(timeEnd);