RenderInterfaceCompatibility.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "../../Include/RmlUi/Core/RenderInterfaceCompatibility.h"
  2. #include "../../Include/RmlUi/Core/Math.h"
  3. namespace Rml {
  4. static void UnPremultiplyAlpha(const byte* source, byte* destination)
  5. {
  6. const byte alpha = source[3];
  7. destination[0] = (alpha > 0 ? (source[0] * 255) / alpha : 255);
  8. destination[1] = (alpha > 0 ? (source[1] * 255) / alpha : 255);
  9. destination[2] = (alpha > 0 ? (source[2] * 255) / alpha : 255);
  10. destination[3] = alpha;
  11. }
  12. RenderInterfaceCompatibility::RenderInterfaceCompatibility() : adapter(new RenderInterfaceAdapter(*this)) {}
  13. RenderInterfaceCompatibility::~RenderInterfaceCompatibility() {}
  14. CompiledGeometryHandle RenderInterfaceCompatibility::CompileGeometry(Vertex* /*vertices*/, int /*num_vertices*/, int* /*indices*/,
  15. int /*num_indices*/, TextureHandle /*texture*/)
  16. {
  17. return 0;
  18. }
  19. void RenderInterfaceCompatibility::RenderCompiledGeometry(CompiledGeometryHandle /*geometry*/, const Vector2f& /*translation*/) {}
  20. void RenderInterfaceCompatibility::ReleaseCompiledGeometry(CompiledGeometryHandle /*geometry*/) {}
  21. bool RenderInterfaceCompatibility::LoadTexture(TextureHandle& /*texture_handle*/, Vector2i& /*texture_dimensions*/, const String& /*source*/)
  22. {
  23. return false;
  24. }
  25. bool RenderInterfaceCompatibility::GenerateTexture(TextureHandle& /*texture_handle*/, const byte* /*source*/, const Vector2i& /*source_dimensions*/)
  26. {
  27. return false;
  28. }
  29. void RenderInterfaceCompatibility::ReleaseTexture(TextureHandle /*texture*/) {}
  30. void RenderInterfaceCompatibility::SetTransform(const Matrix4f* /*transform*/) {}
  31. RenderInterface* RenderInterfaceCompatibility::GetAdaptedInterface()
  32. {
  33. return static_cast<RenderInterface*>(adapter.get());
  34. }
  35. RenderInterfaceAdapter::RenderInterfaceAdapter(RenderInterfaceCompatibility& legacy) : legacy(legacy) {}
  36. CompiledGeometryHandle RenderInterfaceAdapter::CompileGeometry(Span<const Vertex> vertices, Span<const int> indices)
  37. {
  38. // Previously, vertex colors were given in unpremultipled alpha, while now they are given in premultiplied alpha. If
  39. // not corrected for, transparent colors may look darker than they should with the legacy renderer. Thus, here we
  40. // make such a conversion.
  41. //
  42. // When upgrading your renderer, it is strongly recommended to convert your pipeline to use premultiplied alpha,
  43. // both to avoid copying vertex data like here and to achieve correct blending results.
  44. //
  45. // Note that, the vertices and indices are now guaranteed to be valid and immutable until the call to
  46. // ReleaseGeometry. Thus, it is possible to avoid copying the data even if you need access to it during the render
  47. // call. However, (1) due to the need to modify the vertices, we need to make a copy of them here. And (2), due to a
  48. // limitation in the legacy render interface, vertices and indices were previously submitted as pointers to mutable
  49. // vertices and indices. They were never intended to be mutable, but to avoid a const_cast we need to copy both of
  50. // them for that reason too.
  51. Vector<Vertex> vertices_unpremultiplied(vertices.begin(), vertices.end());
  52. for (size_t i = 0; i < vertices.size(); i++)
  53. {
  54. UnPremultiplyAlpha(vertices[i].colour, vertices_unpremultiplied[i].colour);
  55. }
  56. Vector<int> indices_copy(indices.begin(), indices.end());
  57. AdaptedGeometry* data = new AdaptedGeometry{std::move(vertices_unpremultiplied), std::move(indices_copy), {}};
  58. return reinterpret_cast<Rml::CompiledGeometryHandle>(data);
  59. }
  60. void RenderInterfaceAdapter::RenderGeometry(CompiledGeometryHandle handle, Vector2f translation, TextureHandle texture)
  61. {
  62. AdaptedGeometry* geometry = reinterpret_cast<AdaptedGeometry*>(handle);
  63. // Textures were previously stored with the compiled geometry, but is now instead submitted during rendering.
  64. LegacyCompiledGeometryHandle& legacy_geometry = geometry->textures[texture];
  65. if (!legacy_geometry)
  66. {
  67. legacy_geometry = legacy.CompileGeometry(geometry->vertices.data(), (int)geometry->vertices.size(), geometry->indices.data(),
  68. (int)geometry->indices.size(), texture);
  69. }
  70. // If the legacy renderer supports compiling, use that, otherwise render the geometry in immediate mode.
  71. if (legacy_geometry)
  72. {
  73. legacy.RenderCompiledGeometry(legacy_geometry, translation);
  74. }
  75. else
  76. {
  77. legacy.RenderGeometry(geometry->vertices.data(), (int)geometry->vertices.size(), geometry->indices.data(), (int)geometry->indices.size(),
  78. texture, translation);
  79. }
  80. }
  81. void RenderInterfaceAdapter::ReleaseGeometry(CompiledGeometryHandle handle)
  82. {
  83. AdaptedGeometry* geometry = reinterpret_cast<AdaptedGeometry*>(handle);
  84. for (auto& pair : geometry->textures)
  85. legacy.ReleaseCompiledGeometry(pair.second);
  86. delete reinterpret_cast<AdaptedGeometry*>(geometry);
  87. }
  88. void RenderInterfaceAdapter::EnableScissorRegion(bool enable)
  89. {
  90. legacy.EnableScissorRegion(enable);
  91. }
  92. void RenderInterfaceAdapter::SetScissorRegion(Rectanglei region)
  93. {
  94. legacy.SetScissorRegion(region.Left(), region.Top(), region.Width(), region.Height());
  95. }
  96. void RenderInterfaceAdapter::EnableClipMask(bool enable)
  97. {
  98. legacy.EnableScissorRegion(enable);
  99. }
  100. void RenderInterfaceAdapter::RenderToClipMask(ClipMaskOperation operation, CompiledGeometryHandle handle, Vector2f translation)
  101. {
  102. switch (operation)
  103. {
  104. case ClipMaskOperation::Set:
  105. case ClipMaskOperation::Intersect:
  106. // Intersect is considered like Set. This typically occurs in nested clipping situations, which never worked
  107. // correctly in legacy.
  108. break;
  109. case ClipMaskOperation::SetInverse:
  110. // Using features not supported in legacy, bail out.
  111. return;
  112. }
  113. // New features can render more complex clip masks, while legacy only supported rectangle scissoring. Find the
  114. // geometry's rectangular coverage.
  115. const AdaptedGeometry* geometry = reinterpret_cast<AdaptedGeometry*>(handle);
  116. Rectanglef rectangle = Rectanglef::FromPosition(geometry->vertices[0].position);
  117. for (const Vertex& vertex : geometry->vertices)
  118. rectangle = rectangle.Join(vertex.position);
  119. const Rectanglei scissor = Rectanglei(rectangle.Translate(translation));
  120. legacy.SetScissorRegion(scissor.Left(), scissor.Top(), scissor.Width(), scissor.Height());
  121. }
  122. TextureHandle RenderInterfaceAdapter::LoadTexture(Vector2i& texture_dimensions, const String& source)
  123. {
  124. TextureHandle texture_handle = {};
  125. if (!legacy.LoadTexture(texture_handle, texture_dimensions, source))
  126. texture_handle = {};
  127. return texture_handle;
  128. }
  129. TextureHandle RenderInterfaceAdapter::GenerateTexture(Span<const byte> source_data, Vector2i source_dimensions)
  130. {
  131. // Previously, textures were given in unpremultiplied alpha format. Since RmlUi 6, they are given in premultiplied
  132. // alpha. For compatibility, convert the texture to unpremultiplied alpha which is expected by legacy render
  133. // interfaces.
  134. const int num_bytes = source_dimensions.x * source_dimensions.y * 4;
  135. std::unique_ptr<byte[]> unpremultiplied_copy(new byte[num_bytes]);
  136. for (int i = 0; i < num_bytes; i += 4)
  137. {
  138. UnPremultiplyAlpha(&source_data[i], unpremultiplied_copy.get() + i);
  139. }
  140. TextureHandle texture_handle = {};
  141. if (!legacy.GenerateTexture(texture_handle, unpremultiplied_copy.get(), source_dimensions))
  142. texture_handle = {};
  143. return texture_handle;
  144. }
  145. void RenderInterfaceAdapter::ReleaseTexture(TextureHandle texture_handle)
  146. {
  147. legacy.ReleaseTexture(texture_handle);
  148. }
  149. void RenderInterfaceAdapter::SetTransform(const Matrix4f* transform)
  150. {
  151. legacy.SetTransform(transform);
  152. }
  153. } // namespace Rml