Procházet zdrojové kódy

Fix bugs with scalar block layout

Panagiotis Christopoulos Charitos před 5 roky
rodič
revize
ebeec7c4c3

+ 32 - 29
src/anki/gr/vulkan/GrManagerImpl.cpp

@@ -546,62 +546,65 @@ Error GrManagerImpl::initDevice(const GrManagerInitInfo& init)
 			}
 		}
 
-		// Enable the bindless features required
+		// Enable a few 1.2 features
 		{
-			m_descriptorIndexingFeatures = {};
-			m_descriptorIndexingFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES;
+			m_12Features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
 
 			VkPhysicalDeviceFeatures2 features = {};
 			features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
-			features.pNext = &m_descriptorIndexingFeatures;
+			features.pNext = &m_12Features;
 
 			vkGetPhysicalDeviceFeatures2(m_physicalDevice, &features);
 
-			if(!m_descriptorIndexingFeatures.shaderSampledImageArrayNonUniformIndexing
-			   || !m_descriptorIndexingFeatures.shaderStorageImageArrayNonUniformIndexing)
+			// Descriptor indexing
+			if(!m_12Features.shaderSampledImageArrayNonUniformIndexing
+			   || !m_12Features.shaderStorageImageArrayNonUniformIndexing)
 			{
 				ANKI_VK_LOGE("Non uniform indexing is not supported by the device");
 				return Error::FUNCTION_FAILED;
 			}
 
-			if(!m_descriptorIndexingFeatures.descriptorBindingSampledImageUpdateAfterBind
-			   || !m_descriptorIndexingFeatures.descriptorBindingStorageImageUpdateAfterBind)
+			if(!m_12Features.descriptorBindingSampledImageUpdateAfterBind
+			   || !m_12Features.descriptorBindingStorageImageUpdateAfterBind)
 			{
 				ANKI_VK_LOGE("Update descriptors after bind is not supported by the device");
 				return Error::FUNCTION_FAILED;
 			}
 
-			if(!m_descriptorIndexingFeatures.descriptorBindingUpdateUnusedWhilePending)
+			if(!m_12Features.descriptorBindingUpdateUnusedWhilePending)
 			{
 				ANKI_VK_LOGE("Update descriptors while cmd buffer is pending is not supported by the device");
 				return Error::FUNCTION_FAILED;
 			}
 
-			ci.pNext = &m_descriptorIndexingFeatures;
-		}
-
-		// Enable the buffer address only with ray tracing ATM
-		if(!!(m_extensions & VulkanExtensions::KHR_RAY_TRACING))
-		{
-			m_bufferDeviceAddressFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES;
+			// Buffer address
+			if(!!(m_extensions & VulkanExtensions::KHR_RAY_TRACING))
+			{
+				if(!m_12Features.bufferDeviceAddress)
+				{
+					ANKI_VK_LOGE("Buffer device address is not supported by the device");
+					return Error::FUNCTION_FAILED;
+				}
 
-			VkPhysicalDeviceFeatures2 features{};
-			features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
-			features.pNext = &m_bufferDeviceAddressFeatures;
-			vkGetPhysicalDeviceFeatures2(m_physicalDevice, &features);
+				m_12Features.bufferDeviceAddressCaptureReplay =
+					m_12Features.bufferDeviceAddressCaptureReplay && init.m_config->getBool("gr_debugMarkers");
+				m_12Features.bufferDeviceAddressMultiDevice = false;
+			}
+			else
+			{
+				m_12Features.bufferDeviceAddress = false;
+				m_12Features.bufferDeviceAddressCaptureReplay = false;
+				m_12Features.bufferDeviceAddressMultiDevice = false;
+			}
 
-			if(!m_bufferDeviceAddressFeatures.bufferDeviceAddress)
+			// Scalar block layout
+			if(!m_12Features.scalarBlockLayout)
 			{
-				ANKI_VK_LOGE("Buffer device address is required and not supported");
+				ANKI_VK_LOGE("Scalar block layout is not supported by the device");
 				return Error::FUNCTION_FAILED;
 			}
 
-			m_bufferDeviceAddressFeatures.bufferDeviceAddressCaptureReplay =
-				m_bufferDeviceAddressFeatures.bufferDeviceAddressCaptureReplay
-				&& init.m_config->getBool("gr_debugMarkers");
-			m_bufferDeviceAddressFeatures.bufferDeviceAddressMultiDevice = false;
-
-			m_descriptorIndexingFeatures.pNext = &m_bufferDeviceAddressFeatures;
+			ci.pNext = &m_12Features;
 		}
 
 		// Set RT features
@@ -626,7 +629,7 @@ Error GrManagerImpl::initDevice(const GrManagerInitInfo& init)
 			m_rtFeatures.rayTracing = true;
 			m_rtFeatures.rayQuery = true;
 
-			m_bufferDeviceAddressFeatures.pNext = &m_rtFeatures;
+			m_12Features.pNext = &m_rtFeatures;
 		}
 
 		ANKI_VK_LOGI("Will enable the following device extensions:");

+ 1 - 2
src/anki/gr/vulkan/GrManagerImpl.h

@@ -254,9 +254,8 @@ private:
 	VkPhysicalDeviceProperties m_devProps = {};
 	VkPhysicalDeviceRayTracingPropertiesKHR m_rtProps = {};
 	VkPhysicalDeviceFeatures m_devFeatures = {};
-	VkPhysicalDeviceDescriptorIndexingFeatures m_descriptorIndexingFeatures = {};
-	VkPhysicalDeviceBufferDeviceAddressFeatures m_bufferDeviceAddressFeatures = {};
 	VkPhysicalDeviceRayTracingFeaturesKHR m_rtFeatures = {};
+	VkPhysicalDeviceVulkan12Features m_12Features = {};
 
 	PFN_vkDebugMarkerSetObjectNameEXT m_pfnDebugMarkerSetObjectNameEXT = nullptr;
 	PFN_vkCmdDebugMarkerBeginEXT m_pfnCmdDebugMarkerBeginEXT = nullptr;

+ 4 - 0
src/anki/shader_compiler/ShaderProgramParser.cpp

@@ -44,6 +44,7 @@ static const char* SHADER_HEADER = R"(#version 460 core
 
 #extension GL_EXT_buffer_reference : enable
 #extension GL_ARB_gpu_shader_int64 : enable
+#extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable
 
 #extension GL_EXT_nonuniform_qualifier : enable
 #extension GL_EXT_scalar_block_layout : enable
@@ -85,6 +86,7 @@ static const char* SHADER_HEADER = R"(#version 460 core
 #define Bool bool
 
 #define U64 uint64_t
+#define U8 uint8_t
 
 #define _ANKI_CONCATENATE(a, b) a##b
 #define ANKI_CONCATENATE(a, b) _ANKI_CONCATENATE(a, b)
@@ -145,6 +147,8 @@ static const char* SHADER_HEADER = R"(#version 460 core
 		type m_value; \
 	}
 
+#define ANKI_PADDING(bytes) U8 _padding_ ## __LINE__[bytes]
+
 layout(std140, row_major) uniform;
 layout(std140, row_major) buffer;
 )";

+ 24 - 108
tests/gr/Gr.cpp

@@ -2207,9 +2207,9 @@ layout(push_constant) uniform pc_
 
 void main()
 {
-	uvec4 val0 = imageLoad(u_bindlessImages[u_texIndices[0]], ivec2(0));
-	uvec4 val1 = texelFetch(usampler2D(u_bindlessTexturesU2d[u_texIndices[1]], u_sampler), ivec2(0), 0);
-	vec4 val2 = texelFetch(sampler2D(u_bindlessTextures2d[u_texIndices[2]], u_sampler), ivec2(0), 0);
+	uvec4 val0 = imageLoad(u_bindlessImages2dU32[u_texIndices[0]], ivec2(0));
+	uvec4 val1 = texelFetch(usampler2D(u_bindlessTextures2dU32[u_texIndices[1]], u_sampler), ivec2(0), 0);
+	vec4 val2 = texelFetch(sampler2D(u_bindlessTextures2dF32[u_texIndices[2]], u_sampler), ivec2(0), 0);
 
 	u_result = val0 + val1 + uvec4(val2);
 })";
@@ -2732,44 +2732,16 @@ ANKI_TEST(Gr, RayGen)
 	if(!useRayTracing)
 	{
 		ANKI_TEST_LOGW("Ray tracing not supported");
+		break;
 	}
 
-	HeapAllocator<U8> alloc(allocAligned, nullptr);
-
-	// Create the raster programs
-	ShaderProgramPtr rasterProg;
-	if(!useRayTracing)
-	{
-		const CString vertSrc = R"(
-layout(push_constant, row_major) uniform b_pc
-{
-	Mat4 u_mvp;
-	Vec4 u_color;
-};
-
-layout(location = 0) in Vec3 in_pos;
-layout(location = 0) out Vec4 out_color;
-
-void main()
-{
-	gl_Position = u_mvp * Vec4(in_pos, 1.0);
-	out_color = u_color;
-}
-)";
-
-		const CString fragSrc = R"(
-layout(location = 0) in Vec4 in_color;
-layout(location = 0) out Vec4 out_color;
+#define MAGIC_MACRO(x) x
+#include "RtTypes.h"
+#undef MAGIC_MACRO
 
-void main()
-{
-	out_color = in_color;
-}
-)";
-
-		rasterProg = createProgram(vertSrc, fragSrc, *gr);
-	}
+	HeapAllocator<U8> alloc(allocAligned, nullptr);
 
+	// Create the ppline
 	ShaderProgramPtr rtProg;
 	constexpr U32 rayGenGroupIdx = 0;
 	constexpr U32 missGroupIdx = 1;
@@ -2778,9 +2750,10 @@ void main()
 	constexpr U32 primitiveChitGroupIdx = 4;
 	constexpr U32 shadowAhitGroupIdx = 5;
 	constexpr U32 hitgroupCount = 6;
-	if(useRayTracing)
 	{
-		const CString commonSrc = R"(
+		const CString commonSrcPart = R"(
+%s
+
 struct PayLoad
 {
 	Vec3 m_color;
@@ -2792,41 +2765,14 @@ struct ShadowPayLoad
 	F32 m_shadow;
 };
 
-struct Material
-{
-	Vec3 m_diffuseColor;
-};
-
-struct Mesh
-{
-	U64 m_indexBufferPtr;
-	U64 m_positionBufferPtr;
-};
-
-struct Model
-{
-	Material m_mtl;
-	Mesh m_mesh;
-};
-
-struct Light
-{
-	Vec3 m_min;
-	F32 m_padding0;
-	Vec3 m_max;
-	F32 m_padding1;
-	Vec3 m_intensity;
-	F32 m_padding2;
-};
-
 layout(set = 0, binding = 0, scalar) buffer b_00
 {
 	Model u_models[];
 };
 
-layout(set = 0, binding = 1) buffer b_01
+layout(set = 0, binding = 1, scalar) buffer b_01
 {
-	Light u_lights[];
+	layout(align = 16) Light u_lights[];
 };
 
 #define PAYLOAD_LOCATION 0
@@ -2843,6 +2789,15 @@ void main()
 }
 )";
 
+#define MAGIC_MACRO ANKI_STRINGIZE
+		const CString rtTypesStr =
+#include "RtTypes.h"
+			;
+#undef MAGIC_MACRO
+
+		StringAuto commonSrc(alloc);
+		commonSrc.sprintf(commonSrcPart, rtTypesStr.cstr());
+
 		const CString chit1Src = R"(
 layout(location = PAYLOAD_LOCATION) rayPayloadInEXT PayLoad s_payLoad;
 
@@ -3073,7 +3028,6 @@ void main()
 	constexpr U32 modelCount = 4;
 	constexpr U8 opaqueMask = 0b10;
 	constexpr U8 lightMask = 0b01;
-	if(useRayTracing)
 	{
 		// Small box
 		AccelerationStructureInitInfo inf;
@@ -3139,7 +3093,6 @@ void main()
 
 	// Create the SBT
 	BufferPtr sbt;
-	if(useRayTracing)
 	{
 		const U32 recordCount = 1 + 2 + modelCount * 2;
 
@@ -3206,25 +3159,7 @@ void main()
 
 	// Create model info
 	BufferPtr modelBuffer;
-	if(useRayTracing)
 	{
-		struct Material
-		{
-			Vec3 m_diffuseColor;
-		};
-
-		struct Mesh
-		{
-			U64 m_indexBufferPtr;
-			U64 m_positionBufferPtr;
-		};
-
-		struct Model
-		{
-			Material m_mtl;
-			Mesh m_mesh;
-		};
-
 		BufferInitInfo inf;
 		inf.m_mapAccess = BufferMapAccessBit::WRITE;
 		inf.m_usage = BufferUsageBit::ALL_STORAGE;
@@ -3244,19 +3179,7 @@ void main()
 	// Create lights
 	BufferPtr lightBuffer;
 	constexpr U32 lightCount = 1;
-	if(useRayTracing)
 	{
-		class Light
-		{
-		public:
-			Vec3 m_min;
-			F32 m_padding0;
-			Vec3 m_max;
-			F32 m_padding1;
-			Vec3 m_intensity;
-			F32 m_padding2;
-		};
-
 		BufferInitInfo inf;
 		inf.m_mapAccess = BufferMapAccessBit::WRITE;
 		inf.m_usage = BufferUsageBit::ALL_STORAGE;
@@ -3340,14 +3263,7 @@ void main()
 
 		cmdb->bindShaderProgram(rtProg);
 
-		struct PC
-		{
-			Mat4 m_vp;
-			Vec3 m_cameraPos;
-			U32 m_lightCount;
-			UVec3 m_padding0;
-			U32 m_frame;
-		} pc;
+		PushConstants pc;
 		pc.m_vp = projMat * viewMat;
 		pc.m_cameraPos = Vec3(278.0f, 278.0f, -800.0f);
 		pc.m_lightCount = lightCount;

+ 29 - 0
tests/gr/RtTypes.h

@@ -0,0 +1,29 @@
+MAGIC_MACRO(
+	struct Material {
+		Vec3 m_diffuseColor;
+		Vec3 m_emissiveColor;
+	};
+
+	struct Mesh {
+		U64 m_indexBufferPtr;
+		U64 m_positionBufferPtr;
+	};
+
+	struct Model {
+		Material m_mtl;
+		Mesh m_mesh;
+	};
+
+	struct Light {
+		Vec3 m_min;
+		Vec3 m_max;
+		Vec3 m_intensity;
+	};
+
+	struct PushConstants {
+		Mat4 m_vp;
+		Vec3 m_cameraPos;
+		U32 m_lightCount;
+		UVec3 m_padding0;
+		U32 m_frame;
+	};)