Browse Source

Vulkan: More fixes

BearishSun 9 years ago
parent
commit
9e470e2063

+ 34 - 3
Data/Examples/Example.bsl

@@ -2,11 +2,11 @@
 
 
 Parameters = 
 Parameters = 
 {
 {
-	Sampler2D 	samp;
+	Sampler2D 	samp : alias("tex");
 	Texture2D 	tex;
 	Texture2D 	tex;
 };
 };
 
 
-Technique = 
+Technique : base("Surface") =
 {
 {
 	Language = "HLSL11";
 	Language = "HLSL11";
 	
 	
@@ -33,4 +33,35 @@ Technique =
 			}	
 			}	
 		};
 		};
 	};
 	};
-};
+};
+
+Technique : base("Surface") =
+{
+	Language = "GLSL";
+	
+	Pass =
+	{
+		Fragment =
+		{
+			layout(location = 0) in vec2 uv0;
+			layout(location = 1) in vec3 tangentToWorldZ;
+			layout(location = 2) in vec4 tangentToWorldX;			
+		
+			layout(binding = 4) uniform sampler2D tex;
+
+			void main()
+			{
+				GBufferData gbufferData;
+				gbufferData.albedo = texture(tex, uv0);
+				gbufferData.worldNormal.xyz = tangentToWorldZ;
+				
+				encodeGBuffer(gbufferData, gl_FragData[1], gl_FragData[2]);
+				
+				// TODO - Just returning a simple ambient term, use better environment lighting later
+				gl_FragData[0] = vec4(gbufferData.albedo.rgb, 1.0f) * 0.2f; 
+			}	
+		};
+	};
+};
+
+#include "$ENGINE$\Surface.bslinc"

BIN
Data/Examples/Example.bsl.asset


+ 12 - 4
Source/BansheeCore/Source/BsGpuPipelineParamInfo.cpp

@@ -199,11 +199,19 @@ namespace bs
 			return -1;
 			return -1;
 		}
 		}
 
 
-		if(mSetInfos[set].slotTypes[slot] != type)
+		ParamType slotType = mSetInfos[set].slotTypes[slot];
+		if(slotType != type)
 		{
 		{
-			LOGERR("Requested parameter is not of the valid type. Requested: " + toString((UINT32)type) + ". Actual: " + 
-				toString((UINT32)mSetInfos[set].slotTypes[slot]) + ".");
-			return -1;
+			// Allow sampler states & textures to share the same slot, as some APIs combine them
+			bool potentialCombinedSampler = (slotType == ParamType::SamplerState && type == ParamType::Texture) ||
+				(slotType == ParamType::Texture && type == ParamType::SamplerState);
+
+			if (!potentialCombinedSampler)
+			{
+				LOGERR("Requested parameter is not of the valid type. Requested: " + toString((UINT32)type) + ". Actual: " +
+					   toString((UINT32)mSetInfos[set].slotTypes[slot]) + ".");
+				return -1;
+			}
 		}
 		}
 
 
 #endif
 #endif

+ 9 - 9
Source/BansheeVulkanRenderAPI/Include/BsVulkanFramebuffer.h

@@ -15,16 +15,16 @@ namespace bs
 	struct VULKAN_ATTACHMENT_DESC
 	struct VULKAN_ATTACHMENT_DESC
 	{
 	{
 		/** Image to attach or null if none. */
 		/** Image to attach or null if none. */
-		VulkanImage* image;
+		VulkanImage* image = nullptr;
 
 
 		/** View of the image to attach or VK_NULL_HANDLE if none. */
 		/** View of the image to attach or VK_NULL_HANDLE if none. */
-		VkImageView view;
+		VkImageView view = VK_NULL_HANDLE;
 
 
 		/** Format of the attached image. */
 		/** Format of the attached image. */
-		VkFormat format;
+		VkFormat format = VK_FORMAT_UNDEFINED;
 
 
 		/** Initial layer of the surface as pointed to by the provided image view. */
 		/** Initial layer of the surface as pointed to by the provided image view. */
-		UINT32 baseLayer;
+		UINT32 baseLayer = 0;
 	};
 	};
 
 
 	/** Contains parameters used for initializing VulkanFrameBuffer object. */
 	/** Contains parameters used for initializing VulkanFrameBuffer object. */
@@ -37,19 +37,19 @@ namespace bs
 		VULKAN_ATTACHMENT_DESC depth;
 		VULKAN_ATTACHMENT_DESC depth;
 
 
 		/** Width of the images, in pixels. All images must be the same size. */
 		/** Width of the images, in pixels. All images must be the same size. */
-		UINT32 width;
+		UINT32 width = 0;
 
 
 		/** Height of the images, in pixels. All images must be the same size. */
 		/** Height of the images, in pixels. All images must be the same size. */
-		UINT32 height;
+		UINT32 height = 0;
 
 
 		/** Number of image layers to render to. This value is used for all provided surfaces. */
 		/** Number of image layers to render to. This value is used for all provided surfaces. */
-		UINT32 layers;
+		UINT32 layers = 0;
 
 
 		/** Number of samples in the attached images. All attachments must have the same number of samples. */
 		/** Number of samples in the attached images. All attachments must have the same number of samples. */
-		UINT32 numSamples;
+		UINT32 numSamples = 0;
 
 
 		/** Set to true if framebuffer represents an offscreen surface that will not be presented. */
 		/** Set to true if framebuffer represents an offscreen surface that will not be presented. */
-		bool offscreen;
+		bool offscreen = false;
 	};
 	};
 
 
 	/** Information about a single framebuffer attachment. */
 	/** Information about a single framebuffer attachment. */

+ 29 - 12
Source/BansheeVulkanRenderAPI/Source/BsVulkanCommandBuffer.cpp

@@ -272,6 +272,9 @@ namespace bs
 			imageInfo.currentLayout = imageInfo.requiredLayout;
 			imageInfo.currentLayout = imageInfo.requiredLayout;
 		};
 		};
 
 
+		// Note: These layout transitions will contain transitions for offscreen framebuffer attachments (while they 
+		// transition to shader read-only layout). This can be avoided, since they're immediately used by the render pass
+		// as color attachments, making the layout change redundant.
 		for (auto& entry : mQueuedLayoutTransitions)
 		for (auto& entry : mQueuedLayoutTransitions)
 		{
 		{
 			UINT32 imageInfoIdx = entry.second;
 			UINT32 imageInfoIdx = entry.second;
@@ -639,7 +642,7 @@ namespace bs
 	void VulkanCmdBuffer::setRenderTarget(const SPtr<RenderTargetCore>& rt, bool readOnlyDepthStencil, 
 	void VulkanCmdBuffer::setRenderTarget(const SPtr<RenderTargetCore>& rt, bool readOnlyDepthStencil, 
 		RenderSurfaceMask loadMask)
 		RenderSurfaceMask loadMask)
 	{
 	{
-		assert(mState != State::RecordingRenderPass && mState != State::Submitted);
+		assert(mState != State::Submitted);
 
 
 		VulkanFramebuffer* oldFramebuffer = mFramebuffer;
 		VulkanFramebuffer* oldFramebuffer = mFramebuffer;
 
 
@@ -731,7 +734,6 @@ namespace bs
 					baseLayer = curBaseLayer;
 					baseLayer = curBaseLayer;
 				else
 				else
 				{
 				{
-					
 					if(baseLayer != curBaseLayer)
 					if(baseLayer != curBaseLayer)
 					{
 					{
 						// Note: This could be supported relatively easily: we would need to issue multiple separate
 						// Note: This could be supported relatively easily: we would need to issue multiple separate
@@ -749,15 +751,17 @@ namespace bs
 		{
 		{
 			if (mFramebuffer->hasDepthAttachment())
 			if (mFramebuffer->hasDepthAttachment())
 			{
 			{
+				attachments[attachmentIdx].aspectMask = 0;
+
 				if ((buffers & FBT_DEPTH) != 0)
 				if ((buffers & FBT_DEPTH) != 0)
 				{
 				{
-					attachments[attachmentIdx].aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
+					attachments[attachmentIdx].aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
 					attachments[attachmentIdx].clearValue.depthStencil.depth = depth;
 					attachments[attachmentIdx].clearValue.depthStencil.depth = depth;
 				}
 				}
 
 
 				if ((buffers & FBT_STENCIL) != 0)
 				if ((buffers & FBT_STENCIL) != 0)
 				{
 				{
-					attachments[attachmentIdx].aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
+					attachments[attachmentIdx].aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
 					attachments[attachmentIdx].clearValue.depthStencil.stencil = stencil;
 					attachments[attachmentIdx].clearValue.depthStencil.stencil = stencil;
 				}
 				}
 
 
@@ -768,7 +772,6 @@ namespace bs
 					baseLayer = curBaseLayer;
 					baseLayer = curBaseLayer;
 				else
 				else
 				{
 				{
-
 					if (baseLayer != curBaseLayer)
 					if (baseLayer != curBaseLayer)
 					{
 					{
 						// Note: This could be supported relatively easily: we would need to issue multiple separate
 						// Note: This could be supported relatively easily: we would need to issue multiple separate
@@ -782,6 +785,13 @@ namespace bs
 			}
 			}
 		}
 		}
 
 
+		UINT32 numAttachments = attachmentIdx;
+		if (numAttachments == 0)
+			return;
+		
+		if (!isInRenderPass())
+			beginRenderPass();
+
 		VkClearRect clearRect;
 		VkClearRect clearRect;
 		clearRect.baseArrayLayer = baseLayer;
 		clearRect.baseArrayLayer = baseLayer;
 		clearRect.layerCount = mFramebuffer->getNumLayers();
 		clearRect.layerCount = mFramebuffer->getNumLayers();
@@ -789,8 +799,7 @@ namespace bs
 		clearRect.rect.offset.y = area.y;
 		clearRect.rect.offset.y = area.y;
 		clearRect.rect.extent.width = area.width;
 		clearRect.rect.extent.width = area.width;
 		clearRect.rect.extent.height = area.height;
 		clearRect.rect.extent.height = area.height;
-
-		UINT32 numAttachments = attachmentIdx;
+				
 		vkCmdClearAttachments(mCmdBuffer, numAttachments, attachments, 1, &clearRect);
 		vkCmdClearAttachments(mCmdBuffer, numAttachments, attachments, 1, &clearRect);
 	}
 	}
 
 
@@ -1312,16 +1321,24 @@ namespace bs
 		for (UINT32 i = 0; i < numColorAttachments; i++)
 		for (UINT32 i = 0; i < numColorAttachments; i++)
 		{
 		{
 			const VulkanFramebufferAttachment& attachment = res->getColorAttachment(i);
 			const VulkanFramebufferAttachment& attachment = res->getColorAttachment(i);
-			registerResource(attachment.image, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
-							 attachment.image->getLayout(), attachment.finalLayout, VulkanUseFlag::Write, true);
+
+			VkAccessFlags accessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
+			if (attachment.finalLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
+				accessMask |= VK_ACCESS_SHADER_READ_BIT;
+			else
+				accessMask |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
+
+			registerResource(attachment.image, accessMask, attachment.image->getLayout(), attachment.finalLayout, 
+				VulkanUseFlag::Write, true);
 		}
 		}
 
 
 		if(res->hasDepthAttachment())
 		if(res->hasDepthAttachment())
 		{
 		{
 			const VulkanFramebufferAttachment& attachment = res->getDepthStencilAttachment();
 			const VulkanFramebufferAttachment& attachment = res->getDepthStencilAttachment();
-			registerResource(attachment.image,
-							 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
-							 attachment.image->getLayout(), attachment.finalLayout, VulkanUseFlag::Write, true);
+
+			VkAccessFlags accessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
+			registerResource(attachment.image, accessMask, attachment.image->getLayout(), attachment.finalLayout, 
+				VulkanUseFlag::Write, true);
 		}
 		}
 	}
 	}
 
 

+ 26 - 10
Source/BansheeVulkanRenderAPI/Source/BsVulkanGpuPipelineParamInfo.cpp

@@ -103,12 +103,19 @@ namespace bs
 			{
 			{
 				for (auto& entry : params)
 				for (auto& entry : params)
 				{
 				{
-					assert(false); // Accesing the slot below is wrong
-
-					VkDescriptorSetLayoutBinding& binding = mLayoutInfos[entry.second.set].bindings[entry.second.slot];
-					binding.descriptorCount = 1;
-					binding.stageFlags |= stageFlagsLookup[i];
-					binding.descriptorType = descType;
+					LayoutInfo& layoutInfo = mLayoutInfos[entry.second.set];
+					for(UINT32 j = 0; j < layoutInfo.numBindings; j++)
+					{
+						if(layoutInfo.bindings[j].binding == entry.second.slot)
+						{
+							VkDescriptorSetLayoutBinding& binding = layoutInfo.bindings[j];
+							binding.descriptorCount = 1;
+							binding.stageFlags |= stageFlagsLookup[i];
+							binding.descriptorType = descType;
+
+							break;
+						}
+					}
 				}
 				}
 			};
 			};
 
 
@@ -124,10 +131,19 @@ namespace bs
 				bool isLoadStore = entry.second.type != GPOT_BYTE_BUFFER &&
 				bool isLoadStore = entry.second.type != GPOT_BYTE_BUFFER &&
 					entry.second.type != GPOT_STRUCTURED_BUFFER;
 					entry.second.type != GPOT_STRUCTURED_BUFFER;
 
 
-				VkDescriptorSetLayoutBinding& binding = mLayoutInfos[entry.second.set].bindings[entry.second.slot];
-				binding.descriptorCount = 1;
-				binding.stageFlags |= stageFlagsLookup[i];
-				binding.descriptorType = isLoadStore ? VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER : VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
+				LayoutInfo& layoutInfo = mLayoutInfos[entry.second.set];
+				for (UINT32 j = 0; j < layoutInfo.numBindings; j++)
+				{
+					if (layoutInfo.bindings[j].binding == entry.second.slot)
+					{
+						VkDescriptorSetLayoutBinding& binding = layoutInfo.bindings[j];
+						binding.descriptorCount = 1;
+						binding.stageFlags |= stageFlagsLookup[i];
+						binding.descriptorType = isLoadStore ? VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER : VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
+
+						break;
+					}
+				}
 			}
 			}
 		}
 		}
 
 

+ 0 - 1
Source/BansheeVulkanRenderAPI/Source/BsVulkanSwapChain.cpp

@@ -194,7 +194,6 @@ namespace bs
 		for (UINT32 i = 0; i < numFramebuffers; i++)
 		for (UINT32 i = 0; i < numFramebuffers; i++)
 		{
 		{
 			VULKAN_FRAMEBUFFER_DESC& desc = mSurfaces[i].framebufferDesc;
 			VULKAN_FRAMEBUFFER_DESC& desc = mSurfaces[i].framebufferDesc;
-
 			desc.width = getWidth();
 			desc.width = getWidth();
 			desc.height = getHeight();
 			desc.height = getHeight();
 			desc.layers = 1;
 			desc.layers = 1;

+ 3 - 0
Source/BansheeVulkanRenderAPI/Source/BsVulkanVertexInputManager.cpp

@@ -48,7 +48,9 @@ namespace bs
 		{
 		{
 			auto firstElem = mVertexInputMap.begin();
 			auto firstElem = mVertexInputMap.begin();
 
 
+			firstElem->second->vertexInput.~SPtr<VulkanVertexInput>();
 			bs_free(firstElem->second);
 			bs_free(firstElem->second);
+
 			mVertexInputMap.erase(firstElem);
 			mVertexInputMap.erase(firstElem);
 		}
 		}
 	}
 	}
@@ -111,6 +113,7 @@ namespace bs
 
 
 		UINT8* data = (UINT8*)bs_alloc(totalBytes);
 		UINT8* data = (UINT8*)bs_alloc(totalBytes);
 		VertexInputEntry* newEntry = (VertexInputEntry*)data;
 		VertexInputEntry* newEntry = (VertexInputEntry*)data;
+		new (&newEntry->vertexInput) SPtr<VulkanVertexInput>();
 		data += sizeof(VertexInputEntry);
 		data += sizeof(VertexInputEntry);
 
 
 		newEntry->attributes = (VkVertexInputAttributeDescription*)data;
 		newEntry->attributes = (VkVertexInputAttributeDescription*)data;