瀏覽代碼

Vulkan renderer: Fix validation errors (#593)

The problem was in function that used for outside usage, but our case was that we need to use inside in our render pass instance.
wh1t3lord 1 年之前
父節點
當前提交
55d18ed016
共有 3 個文件被更改,包括 42 次插入26 次删除
  1. 1 0
      .gitignore
  2. 36 24
      Backends/RmlUi_Renderer_VK.cpp
  3. 5 2
      Backends/RmlUi_Renderer_VK.h

+ 1 - 0
.gitignore

@@ -29,5 +29,6 @@
 *.dir/
 Win32/
 x64/
+vcpkg/
 CMakeSettings.json
 compile_commands.json

+ 36 - 24
Backends/RmlUi_Renderer_VK.cpp

@@ -358,15 +358,17 @@ void RenderInterface_VK::SetScissorRegion(int x, int y, int width, int height)
 			info_clear_color.depth = 1.0f;
 			info_clear_color.stencil = 0;
 
-			VkImageSubresourceRange info_range{};
-			info_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
-			info_range.baseMipLevel = 0;
-			info_range.baseArrayLayer = 0;
-			info_range.levelCount = 1;
-			info_range.layerCount = 1;
+			VkClearAttachment clear_attachment = {};
+			clear_attachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
+			clear_attachment.clearValue.depthStencil = info_clear_color;
+			clear_attachment.colorAttachment = 1;
 
-			vkCmdClearDepthStencilImage(m_p_current_command_buffer, m_texture_depthstencil.m_p_vk_image,
-				VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, &info_clear_color, 1, &info_range);
+			VkClearRect clear_rect = {};
+			clear_rect.layerCount = 1;
+			clear_rect.rect.extent.width = m_width;
+			clear_rect.rect.extent.height = m_height;
+
+			vkCmdClearAttachments(m_p_current_command_buffer, 1, &clear_attachment, 1, &clear_rect);
 
 			RenderGeometry(vertices, 4, indices, 6, 0, Rml::Vector2f(0.0f, 0.0f));
 
@@ -785,8 +787,18 @@ void RenderInterface_VK::SetViewport(int width, int height)
 	if (window_extent.width == 0 || window_extent.height == 0)
 		return;
 
+#ifdef RMLUI_VK_DEBUG
+	Rml::Log::Message(Rml::Log::Type::LT_DEBUG, "Rml width: %d height: %d | Vulkan width: %d height: %d", m_width, m_height, window_extent.width,
+		window_extent.height);
+#endif
+
+	//  we need to sync the data from Vulkan so we can't use native Rml's data about width and height so be careful otherwise we create framebuffer
+	//  with Rml's width and height but they're different to what Vulkan determines for our window (e.g. device/swapchain)
+	m_width = window_extent.width;
+	m_height = window_extent.height;
+
 	Initialize_Swapchain(window_extent);
-	CreateResourcesDependentOnSize();
+	CreateResourcesDependentOnSize(window_extent);
 }
 
 bool RenderInterface_VK::IsSwapchainValid()
@@ -1202,7 +1214,7 @@ void RenderInterface_VK::Initialize_Allocator() noexcept
 
 	VmaAllocatorCreateInfo info = {};
 
-	info.vulkanApiVersion = VK_API_VERSION_1_0;
+	info.vulkanApiVersion = RMLUI_VK_API_VERSION;
 	info.device = m_p_device;
 	info.instance = m_p_instance;
 	info.physicalDevice = m_p_physical_device;
@@ -1573,7 +1585,7 @@ void RenderInterface_VK::Destroy_ReportDebugCallback() noexcept
 
 uint32_t RenderInterface_VK::GetUserAPIVersion() const noexcept
 {
-	uint32_t result = VK_API_VERSION_1_0;
+	uint32_t result = RMLUI_VK_API_VERSION;
 
 #if defined VK_VERSION_1_1
 	VkResult status = vkEnumerateInstanceVersion(&result);
@@ -1585,7 +1597,7 @@ uint32_t RenderInterface_VK::GetUserAPIVersion() const noexcept
 
 uint32_t RenderInterface_VK::GetRequiredVersionAndValidateMachine() noexcept
 {
-	constexpr uint32_t kRequiredVersion = VK_API_VERSION_1_0;
+	constexpr uint32_t kRequiredVersion = RMLUI_VK_API_VERSION;
 	const uint32_t user_version = GetUserAPIVersion();
 
 	RMLUI_VK_ASSERTMSG(kRequiredVersion <= user_version, "Your machine doesn't support Vulkan");
@@ -2177,7 +2189,7 @@ void RenderInterface_VK::Create_Pipelines() noexcept
 #endif
 }
 
-void RenderInterface_VK::CreateSwapchainFrameBuffers() noexcept
+void RenderInterface_VK::CreateSwapchainFrameBuffers(const VkExtent2D& real_render_image_size) noexcept
 {
 	RMLUI_VK_ASSERTMSG(m_p_render_pass, "you must create a VkRenderPass before calling this method");
 	RMLUI_VK_ASSERTMSG(m_p_device, "you must have a valid VkDevice here");
@@ -2196,8 +2208,8 @@ void RenderInterface_VK::CreateSwapchainFrameBuffers() noexcept
 	info.renderPass = m_p_render_pass;
 	info.attachmentCount = static_cast<uint32_t>(attachments.size());
 	info.pAttachments = attachments.data();
-	info.width = m_width;
-	info.height = m_height;
+	info.width = real_render_image_size.width;
+	info.height = real_render_image_size.height;
 	info.layers = 1;
 
 	int index = 0;
@@ -2285,7 +2297,7 @@ void RenderInterface_VK::Create_DepthStencilImage() noexcept
 	info.arrayLayers = 1;
 	info.samples = VK_SAMPLE_COUNT_1_BIT;
 	info.tiling = VK_IMAGE_TILING_OPTIMAL;
-	info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
+	info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
 
 	VmaAllocation p_allocation = {};
 	VkImage p_image = {};
@@ -2337,17 +2349,17 @@ void RenderInterface_VK::Create_DepthStencilImageViews() noexcept
 	m_texture_depthstencil.m_p_vk_image_view = p_image_view;
 }
 
-void RenderInterface_VK::CreateResourcesDependentOnSize() noexcept
+void RenderInterface_VK::CreateResourcesDependentOnSize(const VkExtent2D& real_render_image_size) noexcept
 {
-	m_viewport.height = static_cast<float>(m_height);
-	m_viewport.width = static_cast<float>(m_width);
+	m_viewport.height = static_cast<float>(real_render_image_size.height);
+	m_viewport.width = static_cast<float>(real_render_image_size.width);
 	m_viewport.minDepth = 0.0f;
 	m_viewport.maxDepth = 1.0f;
 	m_viewport.x = 0.0f;
 	m_viewport.y = 0.0f;
 
-	m_scissor.extent.width = m_width;
-	m_scissor.extent.height = m_height;
+	m_scissor.extent.width = real_render_image_size.width;
+	m_scissor.extent.height = real_render_image_size.height;
 	m_scissor.offset.x = 0;
 	m_scissor.offset.y = 0;
 
@@ -2365,7 +2377,7 @@ void RenderInterface_VK::CreateResourcesDependentOnSize() noexcept
 	SetTransform(nullptr);
 
 	CreateRenderPass();
-	CreateSwapchainFrameBuffers();
+	CreateSwapchainFrameBuffers(real_render_image_size);
 	Create_Pipelines();
 }
 
@@ -2539,7 +2551,7 @@ void RenderInterface_VK::CreateRenderPass() noexcept
 	attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
 	attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
 	attachments[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
-	attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
+	attachments[1].finalLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
 
 	RMLUI_VK_ASSERTMSG(attachments[1].format != VkFormat::VK_FORMAT_UNDEFINED,
 		"can't obtain depth format, your device doesn't support depth/stencil operations");
@@ -2552,7 +2564,7 @@ void RenderInterface_VK::CreateRenderPass() noexcept
 
 	// depth stencil
 	color_references[1].attachment = 1;
-	color_references[1].layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
+	color_references[1].layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
 
 	VkSubpassDescription subpass = {};
 

+ 5 - 2
Backends/RmlUi_Renderer_VK.h

@@ -85,6 +85,9 @@ RMLUI_DISABLE_ALL_COMPILER_WARNINGS_POP
 	#define RMLUI_VK_ASSERTMSG(statement, msg) static_cast<void>(statement)
 #endif
 
+// your specified api version, but in future it will be dynamic ^_^
+#define RMLUI_VK_API_VERSION VK_API_VERSION_1_0
+
 /**
  * Vulkan render interface for RmlUi
  *
@@ -545,7 +548,7 @@ private:
 	void Create_Pipelines() noexcept;
 	void CreateRenderPass() noexcept;
 
-	void CreateSwapchainFrameBuffers() noexcept;
+	void CreateSwapchainFrameBuffers(const VkExtent2D& real_render_image_size) noexcept;
 
 	// This method is called in Views, so don't call it manually
 	void CreateSwapchainImages() noexcept;
@@ -554,7 +557,7 @@ private:
 	void Create_DepthStencilImage() noexcept;
 	void Create_DepthStencilImageViews() noexcept;
 
-	void CreateResourcesDependentOnSize() noexcept;
+	void CreateResourcesDependentOnSize(const VkExtent2D& real_render_image_size) noexcept;
 
 	buffer_data_t CreateResource_StagingBuffer(VkDeviceSize size, VkBufferUsageFlags flags) noexcept;
 	void DestroyResource_StagingBuffer(const buffer_data_t& data) noexcept;