Parcourir la source

Some vulkan fixes (#1051)

Brian Harris il y a 8 ans
Parent
commit
ae566e2bb6
5 fichiers modifiés avec 78 ajouts et 34 suppressions
  1. 2 0
      examples/makefile
  2. 4 4
      src/bgfx_shader.sh
  3. 1 1
      src/makefile
  4. 20 23
      src/renderer_vk.cpp
  5. 51 6
      tools/shaderc/shaderc_spirv.cpp

+ 2 - 0
examples/makefile

@@ -31,6 +31,8 @@ rebuild:
 #	@make -s --no-print-directory rebuild -C 26-occlusion
 	@make -s --no-print-directory rebuild -C 27-terrain
 	@make -s --no-print-directory rebuild -C 28-wireframe
+	@make -s --no-print-directory rebuild -C common/debugdraw
 	@make -s --no-print-directory rebuild -C common/font
 	@make -s --no-print-directory rebuild -C common/imgui
 	@make -s --no-print-directory rebuild -C common/nanovg
+	@make -s --no-print-directory rebuild -C common/ps

+ 4 - 4
src/bgfx_shader.sh

@@ -53,12 +53,12 @@
 #			define dFdyFine(_y)   ddy_fine(-_y)
 #		endif // BGFX_SHADER_LANGUAGE_HLSL > 4
 
-#	if BGFX_SHADER_LANGUAGE_HLSL
+#		if BGFX_SHADER_LANGUAGE_HLSL
 float intBitsToFloat(int   _x) { return asfloat(_x); }
 vec2  intBitsToFloat(uint2 _x) { return asfloat(_x); }
 vec3  intBitsToFloat(uint3 _x) { return asfloat(_x); }
 vec4  intBitsToFloat(uint4 _x) { return asfloat(_x); }
-#	endif // BGFX_SHADER_LANGUAGE_HLSL
+#		endif // BGFX_SHADER_LANGUAGE_HLSL
 
 float uintBitsToFloat(uint  _x) { return asfloat(_x); }
 vec2  uintBitsToFloat(uint2 _x) { return asfloat(_x); }
@@ -80,7 +80,7 @@ uint2 bitfieldReverse(uint2 _x) { return reversebits(_x); }
 uint3 bitfieldReverse(uint3 _x) { return reversebits(_x); }
 uint4 bitfieldReverse(uint4 _x) { return reversebits(_x); }
 
-#if !BGFX_SHADER_LANGUAGE_SPIRV
+#		if !BGFX_SHADER_LANGUAGE_SPIRV
 uint packHalf2x16(vec2 _x)
 {
 	return (f32tof16(_x.x)<<16) | f32tof16(_x.y);
@@ -90,7 +90,7 @@ vec2 unpackHalf2x16(uint _x)
 {
 	return vec2(f16tof32(_x >> 16), f16tof32(_x) );
 }
-#endif // !BGFX_SHADER_LANGUAGE_SPIRV
+#		endif // !BGFX_SHADER_LANGUAGE_SPIRV
 
 struct BgfxSampler2D
 {

+ 1 - 1
src/makefile

@@ -27,7 +27,7 @@ define shader-embedded
 	@echo [$(<)]
 	 $(SILENT) $(SHADERC) --type $(1) --platform linux                 -f $(<) -o $(SHADER_TMP) --bin2c $(basename $(<))_glsl
 	 @cat $(SHADER_TMP) > $(@)
-	-$(SILENT) $(SHADERC) --type $(1) --platform windows -p spirv      -f $(<) -o $(SHADER_TMP) --bin2c $(basename $(<))_spv
+	-$(SILENT) $(SHADERC) --type $(1) --platform linux   -p spirv      -f $(<) -o $(SHADER_TMP) --bin2c $(basename $(<))_spv
 	-@cat $(SHADER_TMP) >> $(@)
 	-$(SILENT) $(SHADERC) --type $(1) --platform windows -p $(2)  -O 3 -f $(<) -o $(SHADER_TMP) --bin2c $(basename $(<))_dx9
 	-@cat $(SHADER_TMP) >> $(@)

+ 20 - 23
src/renderer_vk.cpp

@@ -928,7 +928,7 @@ VK_IMPORT_INSTANCE
 					vkGetPhysicalDeviceMemoryProperties(physicalDevices[ii], &pdmp);
 
 					BX_TRACE("\tMemory type count: %d", pdmp.memoryTypeCount);
-					for (uint32_t jj = 0; jj < pdmp.memoryHeapCount; ++jj)
+					for (uint32_t jj = 0; jj < pdmp.memoryTypeCount; ++jj)
 					{
 						BX_TRACE("\t%3d: flags 0x%08x, index %d"
 								, jj
@@ -937,7 +937,7 @@ VK_IMPORT_INSTANCE
 								);
 					}
 
-					BX_TRACE("\tMemory type count: %d", pdmp.memoryHeapCount);
+					BX_TRACE("\tMemory heap count: %d", pdmp.memoryHeapCount);
 					for (uint32_t jj = 0; jj < pdmp.memoryHeapCount; ++jj)
 					{
 						char size[16];
@@ -1029,20 +1029,6 @@ VK_IMPORT_INSTANCE
 				}
 
 				vkGetPhysicalDeviceMemoryProperties(m_physicalDevice, &m_memoryProperties);
-
-				for (uint32_t ii = 0, num = m_memoryProperties.memoryTypeCount; ii < num; ++ii)
-				{
-					const VkMemoryType& memoryType = m_memoryProperties.memoryTypes[ii];
-					if (0 != (memoryType.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) )
-					{
-						m_memHostVisibleIdx = ii;
-					}
-
-					if (0 != (memoryType.propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) )
-					{
-						m_memLocalVisibleIdx = ii;
-					}
-				}
 			}
 
 			{
@@ -1462,7 +1448,7 @@ VK_IMPORT_DEVICE
 				ma.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
 				ma.pNext = NULL;
 				ma.allocationSize  = mr.size;
-				ma.memoryTypeIndex = m_memLocalVisibleIdx;
+				ma.memoryTypeIndex = selectMemoryType(mr.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
 				result = vkAllocateMemory(m_device
 					, &ma
 					, m_allocatorCb
@@ -2968,6 +2954,19 @@ VK_IMPORT_DEVICE
 //			VK_CHECK(vkWaitForFences(m_device, 1, &m_fence, true, INT64_MAX) );
 		}
 
+		uint32_t selectMemoryType(uint32_t memoryTypeBits, uint32_t propertyFlags)
+		{
+			for (uint32_t ii = 0; ii < m_memoryProperties.memoryTypeCount; ++ii)
+			{
+				if ((((1<<ii) & memoryTypeBits) != 0) && ((m_memoryProperties.memoryTypes[ii].propertyFlags & propertyFlags) == propertyFlags))
+				{
+					return ii;
+				}
+			}
+			BX_TRACE("failed to find memory that supports flags 0x%08x", propertyFlags);
+			return 0;
+		}
+
 		VkAllocationCallbacks* m_allocatorCb;
 		VkDebugReportCallbackEXT m_debugReportCallback;
 		VkInstance       m_instance;
@@ -2975,8 +2974,6 @@ VK_IMPORT_DEVICE
 
 		VkPhysicalDeviceProperties m_deviceProperties;
 		VkPhysicalDeviceMemoryProperties m_memoryProperties;
-		uint32_t m_memHostVisibleIdx;
-		uint32_t m_memLocalVisibleIdx;
 
 		VkSwapchainCreateInfoKHR m_sci;
 		VkSurfaceKHR     m_surface;
@@ -3121,7 +3118,7 @@ VK_DESTROY
 		ma.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
 		ma.pNext = NULL;
 		ma.allocationSize  = mr.size;
-		ma.memoryTypeIndex = s_renderVK->m_memHostVisibleIdx;
+		ma.memoryTypeIndex = s_renderVK->selectMemoryType(mr.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
 		VK_CHECK(vkAllocateMemory(device
 			, &ma
 			, allocatorCb
@@ -3230,7 +3227,7 @@ VK_DESTROY
 		ma.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
 		ma.pNext = NULL;
 		ma.allocationSize  = mr.size;
-		ma.memoryTypeIndex = s_renderVK->m_memLocalVisibleIdx;
+		ma.memoryTypeIndex = s_renderVK->selectMemoryType(mr.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
 		result = vkAllocateMemory(device
 			, &ma
 			, allocatorCb
@@ -3333,7 +3330,7 @@ VK_DESTROY
 		ma.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
 		ma.pNext = NULL;
 		ma.allocationSize  = mr.size;
-		ma.memoryTypeIndex = s_renderVK->m_memHostVisibleIdx;
+		ma.memoryTypeIndex = s_renderVK->selectMemoryType(mr.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
 		VK_CHECK(vkAllocateMemory(device
 			, &ma
 			, allocatorCb
@@ -3488,7 +3485,7 @@ VK_DESTROY
 			}
 		}
 
-		uint32_t shaderSize;
+		uint16_t shaderSize;
 		bx::read(&reader, shaderSize);
 
 #if 1

+ 51 - 6
tools/shaderc/shaderc_spirv.cpp

@@ -645,12 +645,57 @@ namespace bgfx { namespace spirv
 			}
 			else
 			{
-//				program->buildReflection();
-//				fprintf(stderr, "attributes %d, uniforms %d\n"
-//					, program->getNumLiveAttributes()
-//					, program->getNumLiveUniformVariables()
-//					);
-//				program->dumpReflection();
+				program->buildReflection();
+				{
+					uint16_t count = (uint16_t)program->getNumLiveUniformVariables();
+					bx::write(_writer, count);
+
+					uint32_t fragmentBit = profile[0] == 'p' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
+					for (uint16_t ii = 0; ii < count; ++ii)
+					{
+						Uniform un;
+						un.name = program->getUniformName(ii);
+						switch (program->getUniformType(ii))
+						{
+						case 0x1404: // GL_INT:
+							un.type = UniformType::Int1;
+							break;
+						case 0x8B52: // GL_FLOAT_VEC4:
+							un.type = UniformType::Vec4;
+							break;
+						case 0x8B5B: // GL_FLOAT_MAT3:
+							un.type = UniformType::Mat3;
+							break;
+						case 0x8B5C: // GL_FLOAT_MAT4:
+							un.type = UniformType::Mat4;
+							break;
+						default:
+							un.type = UniformType::End;
+							break;
+						}
+						un.num = program->getUniformArraySize(ii);
+						un.regIndex = 0;
+						un.regCount = un.num;
+
+						uint8_t nameSize = (uint8_t)un.name.size();
+						bx::write(_writer, nameSize);
+						bx::write(_writer, un.name.c_str(), nameSize);
+						uint8_t type = un.type | fragmentBit;
+						bx::write(_writer, type);
+						bx::write(_writer, un.num);
+						bx::write(_writer, un.regIndex);
+						bx::write(_writer, un.regCount);
+
+						BX_TRACE("%s, %s, %d, %d, %d"
+							, un.name.c_str()
+							, getUniformTypeName(un.type)
+							, un.num
+							, un.regIndex
+							, un.regCount
+						);
+					}
+				}
+				program->dumpReflection();
 
 				BX_UNUSED(spv::MemorySemanticsAllMemory);