Skip to content

Commit

Permalink
Refactor Geometry and Texture, introduce unique render resources
Browse files Browse the repository at this point in the history
Unique render resources
- Consist of a unique handle and a pointer to its render manger.
- Automatically cleans up its underlying resource when out of scope.
- Ensuring resources are always released in the correct render interface.
- Geometry, CompiledShader, CompiledFilter and CallbackTexture, are now unique render resources.
  - All of which are constructed through the render manager.

RenderManager
- Now the owner of all resources constructed through the render interface.
- Wraps all calls to the render interface, the interface should no longer be called directly.
- Added back ability to use multiple render interfaces for separate contexts.
  - Each render interface is wrapped by a unique render manager.

Geometry
- Geometry is now a unique render resource.
- Geometry is now constructed from a Mesh through the render manager. The mesh cannot be modified after it is submitted. However, it can be released to reuse its buffers, then resubmitted.
  - With this change, we can guarantee pointer stability of submitted vertex and index pointers, until the call to ReleaseCompiledGeometry.
- Move texture out of Geometry, instead, a texture can be provided during the call to render.

Mesh
- Simple data structure, contains indices and vertices defining the mesh.
- Meshes can be constructed directly or using MeshUtilities (previously GeometryUtilities).

Texture
- The render manager now owns and stores file textures.
  - File textures are released when the render manager is destroyed, during Rml::Shutdown.
  - Each render manager has its own texture database to lookup and reuse textures with the same path.
- `Texture` is now simply a non-owning view and can be freely copied.
  - The user is responsible for ensuring validity of the underlying resource's lifetime.
- `CallbackTexture` on the other hand is a unique render resource, automatically released when out of scope.
  - Can make a non-owning reference (Texture).
- Handlers are available for managing texture sources for unknown or multiple render managers, generating references (Texture) as needed.
   - `TextureSource` for file textures and `CallbackTextureSource` for callback textures.

StableVector
- New container for stable indices which remain valid after entries are erased.

Breaking changes
- New procedure for generating geometry and textures (see above for details).
- GeometryUtilities renamed to MeshUtilities.
  • Loading branch information
mikke89 committed Oct 30, 2023
1 parent a076be2 commit a452f26
Show file tree
Hide file tree
Showing 132 changed files with 2,217 additions and 1,821 deletions.
22 changes: 11 additions & 11 deletions Backends/RmlUi_Renderer_GL3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
#include <RmlUi/Core/Core.h>
#include <RmlUi/Core/DecorationTypes.h>
#include <RmlUi/Core/FileInterface.h>
#include <RmlUi/Core/GeometryUtilities.h>
#include <RmlUi/Core/Geometry.h>
#include <RmlUi/Core/MeshUtilities.h>
#include <RmlUi/Core/Log.h>
#include <RmlUi/Core/Platform.h>
#include <RmlUi/Core/SystemInterface.h>
Expand Down Expand Up @@ -780,11 +781,10 @@ RenderInterface_GL3::RenderInterface_GL3()
if (Gfx::CreateShaders(*mut_program_data))
{
program_data = std::move(mut_program_data);

Rml::Vertex vertices[4];
int indices[6];
Rml::GeometryUtilities::GenerateQuad(vertices, indices, Rml::Vector2f(-1), Rml::Vector2f(2), {});
fullscreen_quad_geometry = RenderInterface_GL3::CompileGeometry(vertices, 4, indices, 6);
Rml::Mesh mesh;
Rml::MeshUtilities::GenerateQuad(mesh, Rml::Vector2f(-1), Rml::Vector2f(2), {});
fullscreen_quad_geometry =
RenderInterface_GL3::CompileGeometry(mesh.vertices.data(), (int)mesh.vertices.size(), mesh.indices.data(), (int)mesh.indices.size());
}
}

Expand Down Expand Up @@ -1298,15 +1298,15 @@ void RenderInterface_GL3::DrawFullscreenQuad()

void RenderInterface_GL3::DrawFullscreenQuad(Rml::Vector2f uv_offset, Rml::Vector2f uv_scaling)
{
Rml::Vertex vertices[4];
int indices[6];
Rml::GeometryUtilities::GenerateQuad(vertices, indices, Rml::Vector2f(-1), Rml::Vector2f(2), {});
Rml::Mesh mesh;
Rml::MeshUtilities::GenerateQuad(mesh, Rml::Vector2f(-1), Rml::Vector2f(2), {});
if (uv_offset != Rml::Vector2f() || uv_scaling != Rml::Vector2f(1.f))
{
for (Rml::Vertex& vertex : vertices)
for (Rml::Vertex& vertex : mesh.vertices)
vertex.tex_coord = (vertex.tex_coord * uv_scaling) + uv_offset;
}
RenderGeometry(vertices, 4, indices, 6, RenderInterface_GL3::TexturePostprocess, {});
RenderGeometry(mesh.vertices.data(), (int)mesh.vertices.size(), mesh.indices.data(), (int)mesh.indices.size(),
RenderInterface_GL3::TexturePostprocess, {});
}

static Rml::Colourf ConvertToColorf(Rml::ColourbPremultiplied c0)
Expand Down
17 changes: 11 additions & 6 deletions CMake/FileList.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ set(Core_HDR_FILES
${PROJECT_SOURCE_DIR}/Source/Core/FontEffectShadow.h
${PROJECT_SOURCE_DIR}/Source/Core/GeometryBackgroundBorder.h
${PROJECT_SOURCE_DIR}/Source/Core/GeometryBoxShadow.h
${PROJECT_SOURCE_DIR}/Source/Core/GeometryDatabase.h
${PROJECT_SOURCE_DIR}/Source/Core/IdNameMap.h
${PROJECT_SOURCE_DIR}/Source/Core/Layout/BlockContainer.h
${PROJECT_SOURCE_DIR}/Source/Core/Layout/BlockFormattingContext.h
Expand Down Expand Up @@ -95,6 +94,7 @@ set(Core_HDR_FILES
${PROJECT_SOURCE_DIR}/Source/Core/PropertyParserString.h
${PROJECT_SOURCE_DIR}/Source/Core/PropertyParserTransform.h
${PROJECT_SOURCE_DIR}/Source/Core/PropertyShorthandDefinition.h
${PROJECT_SOURCE_DIR}/Source/Core/RenderManagerAccess.h
${PROJECT_SOURCE_DIR}/Source/Core/ScrollController.h
${PROJECT_SOURCE_DIR}/Source/Core/StreamFile.h
${PROJECT_SOURCE_DIR}/Source/Core/StyleSheetFactory.h
Expand All @@ -108,7 +108,6 @@ set(Core_HDR_FILES
${PROJECT_SOURCE_DIR}/Source/Core/TextureLayoutRectangle.h
${PROJECT_SOURCE_DIR}/Source/Core/TextureLayoutRow.h
${PROJECT_SOURCE_DIR}/Source/Core/TextureLayoutTexture.h
${PROJECT_SOURCE_DIR}/Source/Core/TextureResource.h
${PROJECT_SOURCE_DIR}/Source/Core/TransformState.h
${PROJECT_SOURCE_DIR}/Source/Core/TransformUtilities.h
${PROJECT_SOURCE_DIR}/Source/Core/WidgetScroll.h
Expand All @@ -128,8 +127,10 @@ set(Core_PUB_HDR_FILES
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Animation.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/BaseXMLParser.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Box.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/CallbackTexture.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Colour.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Colour.inl
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/CompiledFilterShader.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/ComputedValues.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Containers/itlib/flat_map.hpp
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Containers/itlib/flat_set.hpp
Expand Down Expand Up @@ -175,14 +176,15 @@ set(Core_PUB_HDR_FILES
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/FontGlyph.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/FontMetrics.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Geometry.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/GeometryUtilities.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Header.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/ID.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Input.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Log.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Math.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Matrix4.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Matrix4.inl
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Mesh.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/MeshUtilities.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/NumericValue.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/ObserverPtr.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Platform.h
Expand All @@ -201,6 +203,7 @@ set(Core_PUB_HDR_FILES
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/ScriptInterface.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/ScrollTypes.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Spritesheet.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/StableVector.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Stream.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/StreamMemory.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/StringUtilities.h
Expand All @@ -218,6 +221,7 @@ set(Core_PUB_HDR_FILES
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/TypeConverter.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/TypeConverter.inl
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Types.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/UniqueRenderResource.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Unit.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/URL.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/Core/Utilities.h
Expand All @@ -237,7 +241,9 @@ set(Core_PUB_HDR_FILES
set(Core_SRC_FILES
${PROJECT_SOURCE_DIR}/Source/Core/BaseXMLParser.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Box.cpp
${PROJECT_SOURCE_DIR}/Source/Core/CallbackTexture.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Clock.cpp
${PROJECT_SOURCE_DIR}/Source/Core/CompiledFilterShader.cpp
${PROJECT_SOURCE_DIR}/Source/Core/ComputedValues.cpp
${PROJECT_SOURCE_DIR}/Source/Core/ComputeProperty.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Context.cpp
Expand Down Expand Up @@ -326,8 +332,6 @@ set(Core_SRC_FILES
${PROJECT_SOURCE_DIR}/Source/Core/Geometry.cpp
${PROJECT_SOURCE_DIR}/Source/Core/GeometryBackgroundBorder.cpp
${PROJECT_SOURCE_DIR}/Source/Core/GeometryBoxShadow.cpp
${PROJECT_SOURCE_DIR}/Source/Core/GeometryDatabase.cpp
${PROJECT_SOURCE_DIR}/Source/Core/GeometryUtilities.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Layout/BlockContainer.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Layout/BlockFormattingContext.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Layout/ContainerBox.cpp
Expand All @@ -348,6 +352,7 @@ set(Core_SRC_FILES
${PROJECT_SOURCE_DIR}/Source/Core/Log.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Math.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Memory.cpp
${PROJECT_SOURCE_DIR}/Source/Core/MeshUtilities.cpp
${PROJECT_SOURCE_DIR}/Source/Core/ObserverPtr.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Plugin.cpp
${PROJECT_SOURCE_DIR}/Source/Core/PluginRegistry.cpp
Expand All @@ -371,6 +376,7 @@ set(Core_SRC_FILES
${PROJECT_SOURCE_DIR}/Source/Core/PropertySpecification.cpp
${PROJECT_SOURCE_DIR}/Source/Core/RenderInterface.cpp
${PROJECT_SOURCE_DIR}/Source/Core/RenderManager.cpp
${PROJECT_SOURCE_DIR}/Source/Core/RenderManagerAccess.cpp
${PROJECT_SOURCE_DIR}/Source/Core/ScrollController.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Spritesheet.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Stream.cpp
Expand All @@ -393,7 +399,6 @@ set(Core_SRC_FILES
${PROJECT_SOURCE_DIR}/Source/Core/TextureLayoutRectangle.cpp
${PROJECT_SOURCE_DIR}/Source/Core/TextureLayoutRow.cpp
${PROJECT_SOURCE_DIR}/Source/Core/TextureLayoutTexture.cpp
${PROJECT_SOURCE_DIR}/Source/Core/TextureResource.cpp
${PROJECT_SOURCE_DIR}/Source/Core/Transform.cpp
${PROJECT_SOURCE_DIR}/Source/Core/TransformPrimitive.cpp
${PROJECT_SOURCE_DIR}/Source/Core/TransformState.cpp
Expand Down
6 changes: 5 additions & 1 deletion Include/RmlUi/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

#include "Core/Animation.h"
#include "Core/Box.h"
#include "Core/CallbackTexture.h"
#include "Core/CompiledFilterShader.h"
#include "Core/ComputedValues.h"
#include "Core/Context.h"
#include "Core/ContextInstancer.h"
Expand Down Expand Up @@ -61,12 +63,13 @@
#include "Core/FontEngineInterface.h"
#include "Core/FontGlyph.h"
#include "Core/Geometry.h"
#include "Core/GeometryUtilities.h"
#include "Core/Header.h"
#include "Core/ID.h"
#include "Core/Input.h"
#include "Core/Log.h"
#include "Core/Math.h"
#include "Core/Mesh.h"
#include "Core/MeshUtilities.h"
#include "Core/NumericValue.h"
#include "Core/Plugin.h"
#include "Core/PropertiesIteratorView.h"
Expand All @@ -91,6 +94,7 @@
#include "Core/Tween.h"
#include "Core/TypeConverter.h"
#include "Core/Types.h"
#include "Core/UniqueRenderResource.h"
#include "Core/Unit.h"
#include "Core/Vertex.h"
#include "Core/XMLNodeHandler.h"
Expand Down
117 changes: 117 additions & 0 deletions Include/RmlUi/Core/CallbackTexture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019-2023 The RmlUi Team, and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#ifndef RMLUI_CORE_CALLBACKTEXTURE_H
#define RMLUI_CORE_CALLBACKTEXTURE_H

#include "Header.h"
#include "Types.h"
#include "UniqueRenderResource.h"

namespace Rml {

class RenderInterface;
class RenderManager;
class CallbackTextureInterface;
class Texture;

/*
Callback function for generating textures on demand.
/// @param[in] texture_interface The interface used to specify the texture.
/// @return True on success.
*/
using CallbackTextureFunction = Function<bool(const CallbackTextureInterface& texture_interface)>;

/**
Callback texture is a unique render resource for generating textures on demand.
It is constructed through the render manager.
*/
class RMLUICORE_API CallbackTexture final : public UniqueRenderResource<CallbackTexture, StableVectorIndex, StableVectorIndex::Invalid> {
public:
CallbackTexture() = default;

operator Texture() const;

void Release();

private:
CallbackTexture(RenderManager* render_manager, StableVectorIndex resource_handle) : UniqueRenderResource(render_manager, resource_handle) {}
friend class RenderManager;
};

/**
Interface handed to the texture callback function, which the client can use to submit a single texture.
*/
class RMLUICORE_API CallbackTextureInterface {
public:
CallbackTextureInterface(RenderManager& render_manager, RenderInterface& render_interface, TextureHandle& texture_handle, Vector2i& dimensions);

/// Generate texture from byte source.
/// @param[in] source Texture data in 8-bit RGBA (premultiplied) format.
/// @param[in] dimensions The width and height of the texture.
/// @return True on success.
bool GenerateTexture(const byte* source, Vector2i dimensions) const;

/// Store the current layer as a texture, so that it can be rendered with geometry later.
/// @param[in] dimensions The dimensions of the resulting texture, which will be copied from the top-left part of the active layer.
void SaveLayerAsTexture(Vector2i dimensions) const;

RenderManager& GetRenderManager() const;

private:
RenderManager& render_manager;
RenderInterface& render_interface;
TextureHandle& texture_handle;
Vector2i& dimensions;
};

/**
Stores a texture callback function, which is used to generate and cache callback textures possibly for multiple render managers.
*/
class RMLUICORE_API CallbackTextureSource {
public:
CallbackTextureSource() = default;
CallbackTextureSource(CallbackTextureFunction&& callback);
~CallbackTextureSource() = default;

CallbackTextureSource(const CallbackTextureSource&) = delete;
CallbackTextureSource& operator=(const CallbackTextureSource&) = delete;

CallbackTextureSource(CallbackTextureSource&& other) noexcept;
CallbackTextureSource& operator=(CallbackTextureSource&& other) noexcept;

Texture GetTexture(RenderManager& render_manager) const;

private:
CallbackTextureFunction callback;
mutable SmallUnorderedMap<RenderManager*, CallbackTexture> textures;
};

} // namespace Rml
#endif
71 changes: 71 additions & 0 deletions Include/RmlUi/Core/CompiledFilterShader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019-2023 The RmlUi Team, and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#ifndef RMLUI_CORE_COMPILEDFILTERSHADER_H
#define RMLUI_CORE_COMPILEDFILTERSHADER_H

#include "Header.h"
#include "UniqueRenderResource.h"

namespace Rml {

class RenderManager;

/**
A compiled filter to be applied during layer pop in its render manager. A unique resource constructed through the render manager.
*/
class RMLUICORE_API CompiledFilter final : public UniqueRenderResource<CompiledFilter, CompiledFilterHandle, CompiledFilterHandle(0)> {
public:
CompiledFilter() = default;

void AddHandleTo(FilterHandleList& list);

void Release();

private:
CompiledFilter(RenderManager* render_manager, CompiledFilterHandle resource_handle) : UniqueRenderResource(render_manager, resource_handle) {}
friend class RenderManager;
};

/**
A compiled shader to be used when rendering geometry. A unique resource constructed through the render manager.
*/
class RMLUICORE_API CompiledShader final : public UniqueRenderResource<CompiledShader, CompiledShaderHandle, CompiledShaderHandle(0)> {
public:
CompiledShader() = default;

void Release();

private:
CompiledShader(RenderManager* render_manager, CompiledShaderHandle resource_handle) : UniqueRenderResource(render_manager, resource_handle) {}
friend class RenderManager;
};

} // namespace Rml

#endif
Loading

0 comments on commit a452f26

Please sign in to comment.