2
0
Эх сурвалжийг харах

Take texture out of the compiled geometry

Michael Ragazzon 2 жил өмнө
parent
commit
faae241d78

+ 7 - 10
Backends/RmlUi_Renderer_GL3.cpp

@@ -106,7 +106,6 @@ enum class VertexAttribute { Position, Color0, TexCoord0, Count };
 static const char* const vertex_attribute_names[(size_t)VertexAttribute::Count] = {"inPosition", "inColor0", "inTexCoord0"};
 static const char* const vertex_attribute_names[(size_t)VertexAttribute::Count] = {"inPosition", "inColor0", "inTexCoord0"};
 
 
 struct CompiledGeometryData {
 struct CompiledGeometryData {
-	Rml::TextureHandle texture;
 	GLuint vao;
 	GLuint vao;
 	GLuint vbo;
 	GLuint vbo;
 	GLuint ibo;
 	GLuint ibo;
@@ -449,17 +448,16 @@ void RenderInterface_GL3::Clear()
 void RenderInterface_GL3::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rml::TextureHandle texture,
 void RenderInterface_GL3::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rml::TextureHandle texture,
 	const Rml::Vector2f& translation)
 	const Rml::Vector2f& translation)
 {
 {
-	Rml::CompiledGeometryHandle geometry = CompileGeometry(vertices, num_vertices, indices, num_indices, texture);
+	Rml::CompiledGeometryHandle geometry = CompileGeometry(vertices, num_vertices, indices, num_indices);
 
 
 	if (geometry)
 	if (geometry)
 	{
 	{
-		RenderCompiledGeometry(geometry, translation);
+		RenderCompiledGeometry(geometry, translation, texture);
 		ReleaseCompiledGeometry(geometry);
 		ReleaseCompiledGeometry(geometry);
 	}
 	}
 }
 }
 
 
-Rml::CompiledGeometryHandle RenderInterface_GL3::CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices,
-	Rml::TextureHandle texture)
+Rml::CompiledGeometryHandle RenderInterface_GL3::CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices)
 {
 {
 	constexpr GLenum draw_usage = GL_STATIC_DRAW;
 	constexpr GLenum draw_usage = GL_STATIC_DRAW;
 
 
@@ -496,7 +494,6 @@ Rml::CompiledGeometryHandle RenderInterface_GL3::CompileGeometry(Rml::Vertex* ve
 	Gfx::CheckGLError("CompileGeometry");
 	Gfx::CheckGLError("CompileGeometry");
 
 
 	Gfx::CompiledGeometryData* geometry = new Gfx::CompiledGeometryData;
 	Gfx::CompiledGeometryData* geometry = new Gfx::CompiledGeometryData;
-	geometry->texture = texture;
 	geometry->vao = vao;
 	geometry->vao = vao;
 	geometry->vbo = vbo;
 	geometry->vbo = vbo;
 	geometry->ibo = ibo;
 	geometry->ibo = ibo;
@@ -505,15 +502,15 @@ Rml::CompiledGeometryHandle RenderInterface_GL3::CompileGeometry(Rml::Vertex* ve
 	return (Rml::CompiledGeometryHandle)geometry;
 	return (Rml::CompiledGeometryHandle)geometry;
 }
 }
 
 
-void RenderInterface_GL3::RenderCompiledGeometry(Rml::CompiledGeometryHandle handle, const Rml::Vector2f& translation)
+void RenderInterface_GL3::RenderCompiledGeometry(Rml::CompiledGeometryHandle handle, const Rml::Vector2f& translation, Rml::TextureHandle texture)
 {
 {
 	Gfx::CompiledGeometryData* geometry = (Gfx::CompiledGeometryData*)handle;
 	Gfx::CompiledGeometryData* geometry = (Gfx::CompiledGeometryData*)handle;
 
 
-	if (geometry->texture)
+	if (texture)
 	{
 	{
 		glUseProgram(shaders->program_texture.id);
 		glUseProgram(shaders->program_texture.id);
-		if (geometry->texture != TextureEnableWithoutBinding)
-			glBindTexture(GL_TEXTURE_2D, (GLuint)geometry->texture);
+		if (texture != TextureEnableWithoutBinding)
+			glBindTexture(GL_TEXTURE_2D, (GLuint)texture);
 		SubmitTransformUniform(ProgramId::Texture, shaders->program_texture.uniform_locations[(size_t)Gfx::ProgramUniform::Transform]);
 		SubmitTransformUniform(ProgramId::Texture, shaders->program_texture.uniform_locations[(size_t)Gfx::ProgramUniform::Transform]);
 		glUniform2fv(shaders->program_texture.uniform_locations[(size_t)Gfx::ProgramUniform::Translate], 1, &translation.x);
 		glUniform2fv(shaders->program_texture.uniform_locations[(size_t)Gfx::ProgramUniform::Translate], 1, &translation.x);
 	}
 	}

+ 2 - 3
Backends/RmlUi_Renderer_GL3.h

@@ -59,9 +59,8 @@ public:
 	void RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture,
 	void RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture,
 		const Rml::Vector2f& translation) override;
 		const Rml::Vector2f& translation) override;
 
 
-	Rml::CompiledGeometryHandle CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices,
-		Rml::TextureHandle texture) override;
-	void RenderCompiledGeometry(Rml::CompiledGeometryHandle geometry, const Rml::Vector2f& translation) override;
+	Rml::CompiledGeometryHandle CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices) override;
+	void RenderCompiledGeometry(Rml::CompiledGeometryHandle geometry, const Rml::Vector2f& translation, Rml::TextureHandle texture) override;
 	void ReleaseCompiledGeometry(Rml::CompiledGeometryHandle geometry) override;
 	void ReleaseCompiledGeometry(Rml::CompiledGeometryHandle geometry) override;
 
 
 	void EnableScissorRegion(bool enable) override;
 	void EnableScissorRegion(bool enable) override;

+ 33 - 38
Backends/RmlUi_Renderer_VK.cpp

@@ -104,22 +104,19 @@ RenderInterface_VK::~RenderInterface_VK() {}
 void RenderInterface_VK::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture,
 void RenderInterface_VK::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture,
 	const Rml::Vector2f& translation)
 	const Rml::Vector2f& translation)
 {
 {
-	Rml::CompiledGeometryHandle handle = CompileGeometry(vertices, num_vertices, indices, num_indices, texture);
+	Rml::CompiledGeometryHandle handle = CompileGeometry(vertices, num_vertices, indices, num_indices);
 
 
 	if (handle)
 	if (handle)
 	{
 	{
-		RenderCompiledGeometry(handle, translation);
+		RenderCompiledGeometry(handle, translation, texture);
 		ReleaseCompiledGeometry(handle);
 		ReleaseCompiledGeometry(handle);
 	}
 	}
 }
 }
 
 
-Rml::CompiledGeometryHandle RenderInterface_VK::CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices,
-	Rml::TextureHandle texture)
+Rml::CompiledGeometryHandle RenderInterface_VK::CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices)
 {
 {
 	RMLUI_ZoneScopedN("Vulkan - CompileGeometry");
 	RMLUI_ZoneScopedN("Vulkan - CompileGeometry");
 
 
-	texture_data_t* p_texture = reinterpret_cast<texture_data_t*>(texture);
-
 	VkDescriptorSet p_current_descriptor_set = nullptr;
 	VkDescriptorSet p_current_descriptor_set = nullptr;
 	p_current_descriptor_set = m_p_descriptor_set;
 	p_current_descriptor_set = m_p_descriptor_set;
 
 
@@ -129,29 +126,6 @@ Rml::CompiledGeometryHandle RenderInterface_VK::CompileGeometry(Rml::Vertex* ver
 
 
 	auto* p_geometry_handle = new geometry_handle_t{};
 	auto* p_geometry_handle = new geometry_handle_t{};
 
 
-	VkDescriptorImageInfo info_descriptor_image = {};
-	if (p_texture && p_texture->m_p_vk_descriptor_set == nullptr)
-	{
-		VkDescriptorSet p_texture_set = nullptr;
-		m_manager_descriptors.Alloc_Descriptor(m_p_device, &m_p_descriptor_set_layout_texture, &p_texture_set);
-
-		info_descriptor_image.imageView = p_texture->m_p_vk_image_view;
-		info_descriptor_image.sampler = p_texture->m_p_vk_sampler;
-		info_descriptor_image.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
-
-		VkWriteDescriptorSet info_write = {};
-
-		info_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
-		info_write.dstSet = p_texture_set;
-		info_write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
-		info_write.dstBinding = 2;
-		info_write.pImageInfo = &info_descriptor_image;
-		info_write.descriptorCount = 1;
-
-		vkUpdateDescriptorSets(m_p_device, 1, &info_write, 0, nullptr);
-		p_texture->m_p_vk_descriptor_set = p_texture_set;
-	}
-
 	uint32_t* pCopyDataToBuffer = nullptr;
 	uint32_t* pCopyDataToBuffer = nullptr;
 	const void* pData = reinterpret_cast<const void*>(vertices);
 	const void* pData = reinterpret_cast<const void*>(vertices);
 
 
@@ -167,14 +141,12 @@ Rml::CompiledGeometryHandle RenderInterface_VK::CompileGeometry(Rml::Vertex* ver
 
 
 	memcpy(pCopyDataToBuffer, indices, sizeof(int) * num_indices);
 	memcpy(pCopyDataToBuffer, indices, sizeof(int) * num_indices);
 
 
-	p_geometry_handle->m_has_texture = static_cast<bool>(texture);
 	p_geometry_handle->m_num_indices = num_indices;
 	p_geometry_handle->m_num_indices = num_indices;
-	p_geometry_handle->m_p_texture = p_texture;
 
 
 	return Rml::CompiledGeometryHandle(p_geometry_handle);
 	return Rml::CompiledGeometryHandle(p_geometry_handle);
 }
 }
 
 
-void RenderInterface_VK::RenderCompiledGeometry(Rml::CompiledGeometryHandle geometry, const Rml::Vector2f& translation)
+void RenderInterface_VK::RenderCompiledGeometry(Rml::CompiledGeometryHandle geometry, const Rml::Vector2f& translation, Rml::TextureHandle texture)
 {
 {
 	RMLUI_ZoneScopedN("Vulkan - RenderCompiledGeometry");
 	RMLUI_ZoneScopedN("Vulkan - RenderCompiledGeometry");
 
 
@@ -183,6 +155,31 @@ void RenderInterface_VK::RenderCompiledGeometry(Rml::CompiledGeometryHandle geom
 
 
 	RMLUI_VK_ASSERTMSG(m_p_current_command_buffer, "must be valid otherwise you can't render now!!! (can't be)");
 	RMLUI_VK_ASSERTMSG(m_p_current_command_buffer, "must be valid otherwise you can't render now!!! (can't be)");
 
 
+	texture_data_t* p_texture = reinterpret_cast<texture_data_t*>(texture);
+
+	VkDescriptorImageInfo info_descriptor_image = {};
+	if (p_texture && p_texture->m_p_vk_descriptor_set == nullptr)
+	{
+		VkDescriptorSet p_texture_set = nullptr;
+		m_manager_descriptors.Alloc_Descriptor(m_p_device, &m_p_descriptor_set_layout_texture, &p_texture_set);
+
+		info_descriptor_image.imageView = p_texture->m_p_vk_image_view;
+		info_descriptor_image.sampler = p_texture->m_p_vk_sampler;
+		info_descriptor_image.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
+
+		VkWriteDescriptorSet info_write = {};
+
+		info_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
+		info_write.dstSet = p_texture_set;
+		info_write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
+		info_write.dstBinding = 2;
+		info_write.pImageInfo = &info_descriptor_image;
+		info_write.descriptorCount = 1;
+
+		vkUpdateDescriptorSets(m_p_device, 1, &info_write, 0, nullptr);
+		p_texture->m_p_vk_descriptor_set = p_texture_set;
+	}
+
 	geometry_handle_t* p_casted_compiled_geometry = reinterpret_cast<geometry_handle_t*>(geometry);
 	geometry_handle_t* p_casted_compiled_geometry = reinterpret_cast<geometry_handle_t*>(geometry);
 
 
 	m_user_data_for_vertex_shader.m_translate = translation;
 	m_user_data_for_vertex_shader.m_translate = translation;
@@ -236,15 +233,15 @@ void RenderInterface_VK::RenderCompiledGeometry(Rml::CompiledGeometryHandle geom
 
 
 	VkDescriptorSet p_texture_descriptor_set = nullptr;
 	VkDescriptorSet p_texture_descriptor_set = nullptr;
 
 
-	if (p_casted_compiled_geometry->m_p_texture)
+	if (p_texture)
 	{
 	{
-		p_texture_descriptor_set = p_casted_compiled_geometry->m_p_texture->m_p_vk_descriptor_set;
+		p_texture_descriptor_set = p_texture->m_p_vk_descriptor_set;
 	}
 	}
 
 
 	VkDescriptorSet p_sets[] = {p_current_descriptor_set, p_texture_descriptor_set};
 	VkDescriptorSet p_sets[] = {p_current_descriptor_set, p_texture_descriptor_set};
 	int real_size_of_sets = 2;
 	int real_size_of_sets = 2;
 
 
-	if (p_casted_compiled_geometry->m_p_texture == nullptr)
+	if (p_texture == nullptr)
 		real_size_of_sets = 1;
 		real_size_of_sets = 1;
 
 
 	vkCmdBindDescriptorSets(m_p_current_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_p_pipeline_layout, 0, real_size_of_sets, p_sets, 1,
 	vkCmdBindDescriptorSets(m_p_current_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_p_pipeline_layout, 0, real_size_of_sets, p_sets, 1,
@@ -256,7 +253,7 @@ void RenderInterface_VK::RenderCompiledGeometry(Rml::CompiledGeometryHandle geom
 	}
 	}
 	else
 	else
 	{
 	{
-		if (p_casted_compiled_geometry->m_has_texture)
+		if (p_texture)
 		{
 		{
 			if (m_is_apply_to_regular_geometry_stencil)
 			if (m_is_apply_to_regular_geometry_stencil)
 			{
 			{
@@ -3015,8 +3012,6 @@ void RenderInterface_VK::MemoryPool::Free_GeometryHandle(geometry_handle_t* p_va
 	p_valid_geometry_handle->m_p_vertex_allocation = nullptr;
 	p_valid_geometry_handle->m_p_vertex_allocation = nullptr;
 	p_valid_geometry_handle->m_p_shader_allocation = nullptr;
 	p_valid_geometry_handle->m_p_shader_allocation = nullptr;
 	p_valid_geometry_handle->m_p_index_allocation = nullptr;
 	p_valid_geometry_handle->m_p_index_allocation = nullptr;
-	p_valid_geometry_handle->m_p_texture = nullptr;
-	p_valid_geometry_handle->m_has_texture = false;
 	p_valid_geometry_handle->m_num_indices = 0;
 	p_valid_geometry_handle->m_num_indices = 0;
 }
 }
 
 

+ 2 - 6
Backends/RmlUi_Renderer_VK.h

@@ -131,11 +131,10 @@ public:
 		const Rml::Vector2f& translation) override;
 		const Rml::Vector2f& translation) override;
 
 
 	/// Called by RmlUi when it wants to compile geometry it believes will be static for the forseeable future.
 	/// Called by RmlUi when it wants to compile geometry it believes will be static for the forseeable future.
-	Rml::CompiledGeometryHandle CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices,
-		Rml::TextureHandle texture) override;
+	Rml::CompiledGeometryHandle CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices) override;
 
 
 	/// Called by RmlUi when it wants to render application-compiled geometry.
 	/// Called by RmlUi when it wants to render application-compiled geometry.
-	void RenderCompiledGeometry(Rml::CompiledGeometryHandle geometry, const Rml::Vector2f& translation) override;
+	void RenderCompiledGeometry(Rml::CompiledGeometryHandle geometry, const Rml::Vector2f& translation, Rml::TextureHandle texture) override;
 
 
 	/// Called by RmlUi when it wants to release application-compiled geometry.
 	/// Called by RmlUi when it wants to release application-compiled geometry.
 	void ReleaseCompiledGeometry(Rml::CompiledGeometryHandle geometry) override;
 	void ReleaseCompiledGeometry(Rml::CompiledGeometryHandle geometry) override;
@@ -175,11 +174,8 @@ private:
 	};
 	};
 
 
 	struct geometry_handle_t {
 	struct geometry_handle_t {
-		bool m_has_texture;
 		int m_num_indices;
 		int m_num_indices;
 
 
-		texture_data_t* m_p_texture;
-
 		VkDescriptorBufferInfo m_p_vertex;
 		VkDescriptorBufferInfo m_p_vertex;
 		VkDescriptorBufferInfo m_p_index;
 		VkDescriptorBufferInfo m_p_index;
 		VkDescriptorBufferInfo m_p_shader;
 		VkDescriptorBufferInfo m_p_shader;

+ 3 - 3
Include/RmlUi/Core/RenderInterface.h

@@ -80,14 +80,14 @@ public:
 	/// @param[in] num_vertices The number of vertices passed to the function.
 	/// @param[in] num_vertices The number of vertices passed to the function.
 	/// @param[in] indices The geometry's index data.
 	/// @param[in] indices The geometry's index data.
 	/// @param[in] num_indices The number of indices passed to the function. This will always be a multiple of three.
 	/// @param[in] num_indices The number of indices passed to the function. This will always be a multiple of three.
-	/// @param[in] texture The texture to be applied to the geometry. This may be nullptr, in which case the geometry is untextured.
 	/// @return The application-specific compiled geometry. Compiled geometry will be stored and rendered using RenderCompiledGeometry() in future
 	/// @return The application-specific compiled geometry. Compiled geometry will be stored and rendered using RenderCompiledGeometry() in future
 	/// calls, and released with ReleaseCompiledGeometry() when it is no longer needed.
 	/// calls, and released with ReleaseCompiledGeometry() when it is no longer needed.
-	virtual CompiledGeometryHandle CompileGeometry(Vertex* vertices, int num_vertices, int* indices, int num_indices, TextureHandle texture);
+	virtual CompiledGeometryHandle CompileGeometry(Vertex* vertices, int num_vertices, int* indices, int num_indices);
 	/// Called by RmlUi when it wants to render application-compiled geometry.
 	/// Called by RmlUi when it wants to render application-compiled geometry.
 	/// @param[in] geometry The application-specific compiled geometry to render.
 	/// @param[in] geometry The application-specific compiled geometry to render.
 	/// @param[in] translation The translation to apply to the geometry.
 	/// @param[in] translation The translation to apply to the geometry.
-	virtual void RenderCompiledGeometry(CompiledGeometryHandle geometry, const Vector2f& translation);
+	/// @param[in] texture The texture to be applied to the geometry. This may be nullptr, in which case the geometry is untextured.
+	virtual void RenderCompiledGeometry(CompiledGeometryHandle geometry, const Vector2f& translation, TextureHandle texture);
 	/// Called by RmlUi when it wants to release application-compiled geometry.
 	/// Called by RmlUi when it wants to release application-compiled geometry.
 	/// @param[in] geometry The application-specific compiled geometry to release.
 	/// @param[in] geometry The application-specific compiled geometry to release.
 	virtual void ReleaseCompiledGeometry(CompiledGeometryHandle geometry);
 	virtual void ReleaseCompiledGeometry(CompiledGeometryHandle geometry);

+ 3 - 4
Source/Core/Geometry.cpp

@@ -84,7 +84,7 @@ void Geometry::Render(Vector2f translation)
 	if (compiled_geometry)
 	if (compiled_geometry)
 	{
 	{
 		RMLUI_ZoneScopedN("RenderCompiled");
 		RMLUI_ZoneScopedN("RenderCompiled");
-		render_interface->RenderCompiledGeometry(compiled_geometry, translation);
+		render_interface->RenderCompiledGeometry(compiled_geometry, translation, texture ? texture->GetHandle() : 0);
 	}
 	}
 	// Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
 	// Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
 	// immediate mode.
 	// immediate mode.
@@ -98,14 +98,13 @@ void Geometry::Render(Vector2f translation)
 		if (!compile_attempted)
 		if (!compile_attempted)
 		{
 		{
 			compile_attempted = true;
 			compile_attempted = true;
-			compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size(),
-				texture ? texture->GetHandle() : 0);
+			compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size());
 
 
 			// If we managed to compile the geometry, we can clear the local copy of vertices and indices and
 			// If we managed to compile the geometry, we can clear the local copy of vertices and indices and
 			// immediately render the compiled version.
 			// immediately render the compiled version.
 			if (compiled_geometry)
 			if (compiled_geometry)
 			{
 			{
-				render_interface->RenderCompiledGeometry(compiled_geometry, translation);
+				render_interface->RenderCompiledGeometry(compiled_geometry, translation, texture ? texture->GetHandle() : 0);
 				return;
 				return;
 			}
 			}
 		}
 		}

+ 2 - 3
Source/Core/RenderInterface.cpp

@@ -42,13 +42,12 @@ RenderInterface::~RenderInterface()
 		"dereference when releasing the textures. Ensure that the render interface is destroyed *after* the call to Rml::Shutdown.");
 		"dereference when releasing the textures. Ensure that the render interface is destroyed *after* the call to Rml::Shutdown.");
 }
 }
 
 
-CompiledGeometryHandle RenderInterface::CompileGeometry(Vertex* /*vertices*/, int /*num_vertices*/, int* /*indices*/, int /*num_indices*/,
-	TextureHandle /*texture*/)
+CompiledGeometryHandle RenderInterface::CompileGeometry(Vertex* /*vertices*/, int /*num_vertices*/, int* /*indices*/, int /*num_indices*/)
 {
 {
 	return 0;
 	return 0;
 }
 }
 
 
-void RenderInterface::RenderCompiledGeometry(CompiledGeometryHandle /*geometry*/, const Vector2f& /*translation*/) {}
+void RenderInterface::RenderCompiledGeometry(CompiledGeometryHandle /*geometry*/, const Vector2f& /*translation*/, TextureHandle /*texture*/) {}
 
 
 void RenderInterface::ReleaseCompiledGeometry(CompiledGeometryHandle /*geometry*/) {}
 void RenderInterface::ReleaseCompiledGeometry(CompiledGeometryHandle /*geometry*/) {}