RmlUi_Renderer_GL2.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "RmlUi_Renderer_GL2.h"
  29. #include <RmlUi/Core/Core.h>
  30. #include <RmlUi/Core/FileInterface.h>
  31. #include <RmlUi/Core/Log.h>
  32. #include <RmlUi/Core/Platform.h>
  33. #include <string.h>
  34. #if defined RMLUI_PLATFORM_WIN32
  35. #include "RmlUi_Include_Windows.h"
  36. #include <gl/Gl.h>
  37. #elif defined RMLUI_PLATFORM_MACOSX
  38. #include <AGL/agl.h>
  39. #include <OpenGL/gl.h>
  40. #include <OpenGL/glext.h>
  41. #elif defined RMLUI_PLATFORM_UNIX
  42. #include "RmlUi_Include_Xlib.h"
  43. #include <GL/gl.h>
  44. #include <GL/glext.h>
  45. #include <GL/glx.h>
  46. #endif
  47. #define GL_CLAMP_TO_EDGE 0x812F
  48. RenderInterface_GL2::RenderInterface_GL2() {}
  49. void RenderInterface_GL2::SetViewport(int in_viewport_width, int in_viewport_height)
  50. {
  51. viewport_width = in_viewport_width;
  52. viewport_height = in_viewport_height;
  53. }
  54. void RenderInterface_GL2::BeginFrame()
  55. {
  56. RMLUI_ASSERT(viewport_width >= 0 && viewport_height >= 0);
  57. glViewport(0, 0, viewport_width, viewport_height);
  58. glEnableClientState(GL_VERTEX_ARRAY);
  59. glEnableClientState(GL_COLOR_ARRAY);
  60. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  61. glEnable(GL_BLEND);
  62. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  63. glEnable(GL_STENCIL_TEST);
  64. glStencilFunc(GL_ALWAYS, 1, GLuint(-1));
  65. glStencilMask(GLuint(-1));
  66. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  67. Rml::Matrix4f projection = Rml::Matrix4f::ProjectOrtho(0, (float)viewport_width, (float)viewport_height, 0, -10000, 10000);
  68. glMatrixMode(GL_PROJECTION);
  69. glLoadMatrixf(projection.data());
  70. glMatrixMode(GL_TEXTURE);
  71. glLoadIdentity();
  72. glMatrixMode(GL_MODELVIEW);
  73. glLoadIdentity();
  74. transform_enabled = false;
  75. }
  76. void RenderInterface_GL2::EndFrame() {}
  77. void RenderInterface_GL2::Clear()
  78. {
  79. glClearStencil(0);
  80. glClearColor(0, 0, 0, 0);
  81. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  82. }
  83. Rml::CompiledGeometryHandle RenderInterface_GL2::CompileGeometry(Rml::Span<const Rml::Vertex> vertices, Rml::Span<const int> indices)
  84. {
  85. GeometryView* data = new GeometryView{vertices, indices};
  86. return reinterpret_cast<Rml::CompiledGeometryHandle>(data);
  87. }
  88. void RenderInterface_GL2::ReleaseGeometry(Rml::CompiledGeometryHandle geometry)
  89. {
  90. delete reinterpret_cast<GeometryView*>(geometry);
  91. }
  92. void RenderInterface_GL2::RenderGeometry(Rml::CompiledGeometryHandle handle, Rml::Vector2f translation, Rml::TextureHandle texture)
  93. {
  94. const GeometryView* geometry = reinterpret_cast<GeometryView*>(handle);
  95. const Rml::Vertex* vertices = geometry->vertices.data();
  96. const int* indices = geometry->indices.data();
  97. const int num_indices = (int)geometry->indices.size();
  98. glPushMatrix();
  99. glTranslatef(translation.x, translation.y, 0);
  100. glVertexPointer(2, GL_FLOAT, sizeof(Rml::Vertex), &vertices[0].position);
  101. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Rml::Vertex), &vertices[0].colour);
  102. if (!texture)
  103. {
  104. glDisable(GL_TEXTURE_2D);
  105. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  106. }
  107. else
  108. {
  109. glEnable(GL_TEXTURE_2D);
  110. if (texture != TextureEnableWithoutBinding)
  111. glBindTexture(GL_TEXTURE_2D, (GLuint)texture);
  112. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  113. glTexCoordPointer(2, GL_FLOAT, sizeof(Rml::Vertex), &vertices[0].tex_coord);
  114. }
  115. glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  116. glPopMatrix();
  117. }
  118. void RenderInterface_GL2::EnableScissorRegion(bool enable)
  119. {
  120. if (enable)
  121. glEnable(GL_SCISSOR_TEST);
  122. else
  123. glDisable(GL_SCISSOR_TEST);
  124. }
  125. void RenderInterface_GL2::SetScissorRegion(Rml::Rectanglei region)
  126. {
  127. glScissor(region.Left(), viewport_height - region.Bottom(), region.Width(), region.Height());
  128. }
  129. void RenderInterface_GL2::EnableClipMask(bool enable)
  130. {
  131. if (enable)
  132. glEnable(GL_STENCIL_TEST);
  133. else
  134. glDisable(GL_STENCIL_TEST);
  135. }
  136. void RenderInterface_GL2::RenderToClipMask(Rml::ClipMaskOperation operation, Rml::CompiledGeometryHandle geometry, Rml::Vector2f translation)
  137. {
  138. RMLUI_ASSERT(glIsEnabled(GL_STENCIL_TEST));
  139. using Rml::ClipMaskOperation;
  140. const bool clear_stencil = (operation == ClipMaskOperation::Set || operation == ClipMaskOperation::SetInverse);
  141. if (clear_stencil)
  142. {
  143. // @performance Increment the reference value instead of clearing each time.
  144. glClear(GL_STENCIL_BUFFER_BIT);
  145. }
  146. GLint stencil_test_value = 0;
  147. glGetIntegerv(GL_STENCIL_REF, &stencil_test_value);
  148. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  149. glStencilFunc(GL_ALWAYS, GLint(1), GLuint(-1));
  150. switch (operation)
  151. {
  152. case ClipMaskOperation::Set:
  153. {
  154. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  155. stencil_test_value = 1;
  156. }
  157. break;
  158. case ClipMaskOperation::SetInverse:
  159. {
  160. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  161. stencil_test_value = 0;
  162. }
  163. break;
  164. case ClipMaskOperation::Intersect:
  165. {
  166. glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
  167. stencil_test_value += 1;
  168. }
  169. break;
  170. }
  171. RenderGeometry(geometry, translation, {});
  172. // Restore state
  173. // @performance Cache state so we don't toggle it unnecessarily.
  174. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  175. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  176. glStencilFunc(GL_EQUAL, stencil_test_value, GLuint(-1));
  177. }
  178. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  179. #pragma pack(1)
  180. struct TGAHeader {
  181. char idLength;
  182. char colourMapType;
  183. char dataType;
  184. short int colourMapOrigin;
  185. short int colourMapLength;
  186. char colourMapDepth;
  187. short int xOrigin;
  188. short int yOrigin;
  189. short int width;
  190. short int height;
  191. char bitsPerPixel;
  192. char imageDescriptor;
  193. };
  194. // Restore packing
  195. #pragma pack()
  196. Rml::TextureHandle RenderInterface_GL2::LoadTexture(Rml::Vector2i& texture_dimensions, const Rml::String& source)
  197. {
  198. Rml::FileInterface* file_interface = Rml::GetFileInterface();
  199. Rml::FileHandle file_handle = file_interface->Open(source);
  200. if (!file_handle)
  201. {
  202. return false;
  203. }
  204. file_interface->Seek(file_handle, 0, SEEK_END);
  205. size_t buffer_size = file_interface->Tell(file_handle);
  206. file_interface->Seek(file_handle, 0, SEEK_SET);
  207. if (buffer_size <= sizeof(TGAHeader))
  208. {
  209. Rml::Log::Message(Rml::Log::LT_ERROR, "Texture file size is smaller than TGAHeader, file is not a valid TGA image.");
  210. file_interface->Close(file_handle);
  211. return false;
  212. }
  213. using Rml::byte;
  214. Rml::UniquePtr<byte[]> buffer(new byte[buffer_size]);
  215. file_interface->Read(buffer.get(), buffer_size, file_handle);
  216. file_interface->Close(file_handle);
  217. TGAHeader header;
  218. memcpy(&header, buffer.get(), sizeof(TGAHeader));
  219. int color_mode = header.bitsPerPixel / 8;
  220. const size_t image_size = header.width * header.height * 4; // We always make 32bit textures
  221. if (header.dataType != 2)
  222. {
  223. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  224. return false;
  225. }
  226. // Ensure we have at least 3 colors
  227. if (color_mode < 3)
  228. {
  229. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24 and 32bit textures are supported.");
  230. return false;
  231. }
  232. const byte* image_src = buffer.get() + sizeof(TGAHeader);
  233. Rml::UniquePtr<byte[]> image_dest_buffer(new byte[image_size]);
  234. byte* image_dest = image_dest_buffer.get();
  235. const bool top_to_bottom_order = ((header.imageDescriptor & 32) != 0);
  236. // Targa is BGR, swap to RGB, flip Y axis as necessary, and convert to premultiplied alpha.
  237. for (long y = 0; y < header.height; y++)
  238. {
  239. long read_index = y * header.width * color_mode;
  240. long write_index = top_to_bottom_order ? (y * header.width * 4) : (header.height - y - 1) * header.width * 4;
  241. for (long x = 0; x < header.width; x++)
  242. {
  243. image_dest[write_index] = image_src[read_index + 2];
  244. image_dest[write_index + 1] = image_src[read_index + 1];
  245. image_dest[write_index + 2] = image_src[read_index];
  246. if (color_mode == 4)
  247. {
  248. const byte alpha = image_src[read_index + 3];
  249. for (size_t j = 0; j < 3; j++)
  250. image_dest[write_index + j] = byte((image_dest[write_index + j] * alpha) / 255);
  251. image_dest[write_index + 3] = alpha;
  252. }
  253. else
  254. image_dest[write_index + 3] = 255;
  255. write_index += 4;
  256. read_index += color_mode;
  257. }
  258. }
  259. texture_dimensions.x = header.width;
  260. texture_dimensions.y = header.height;
  261. return GenerateTexture({image_dest, image_size}, texture_dimensions);
  262. }
  263. Rml::TextureHandle RenderInterface_GL2::GenerateTexture(Rml::Span<const Rml::byte> source, Rml::Vector2i source_dimensions)
  264. {
  265. RMLUI_ASSERT(source.data() && source.size() == size_t(source_dimensions.x * source_dimensions.y * 4));
  266. GLuint texture_id = 0;
  267. glGenTextures(1, &texture_id);
  268. if (texture_id == 0)
  269. {
  270. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to generate texture.");
  271. return {};
  272. }
  273. glBindTexture(GL_TEXTURE_2D, texture_id);
  274. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, source.data());
  275. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  276. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  277. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  278. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  279. return (Rml::TextureHandle)texture_id;
  280. }
  281. void RenderInterface_GL2::ReleaseTexture(Rml::TextureHandle texture_handle)
  282. {
  283. glDeleteTextures(1, (GLuint*)&texture_handle);
  284. }
  285. void RenderInterface_GL2::SetTransform(const Rml::Matrix4f* transform)
  286. {
  287. transform_enabled = (transform != nullptr);
  288. if (transform)
  289. {
  290. if (std::is_same<Rml::Matrix4f, Rml::ColumnMajorMatrix4f>::value)
  291. glLoadMatrixf(transform->data());
  292. else if (std::is_same<Rml::Matrix4f, Rml::RowMajorMatrix4f>::value)
  293. glLoadMatrixf(transform->Transpose().data());
  294. }
  295. else
  296. glLoadIdentity();
  297. }