RmlUi_Renderer_GL3.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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 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_GL3.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) && !defined(__MINGW32__)
  35. // function call missing argument list
  36. #pragma warning(disable : 4551)
  37. // unreferenced local function has been removed
  38. #pragma warning(disable : 4505)
  39. #endif
  40. #if defined RMLUI_PLATFORM_EMSCRIPTEN
  41. #define RMLUI_SHADER_HEADER "#version 300 es\nprecision highp float;\n"
  42. #include <GLES3/gl3.h>
  43. #else
  44. #define RMLUI_SHADER_HEADER "#version 330\n"
  45. #define GLAD_GL_IMPLEMENTATION
  46. #include "RmlUi_Include_GL3.h"
  47. #endif
  48. static const char* shader_main_vertex = RMLUI_SHADER_HEADER R"(
  49. uniform vec2 _translate;
  50. uniform mat4 _transform;
  51. in vec2 inPosition;
  52. in vec4 inColor0;
  53. in vec2 inTexCoord0;
  54. out vec2 fragTexCoord;
  55. out vec4 fragColor;
  56. void main() {
  57. fragTexCoord = inTexCoord0;
  58. fragColor = inColor0;
  59. vec2 translatedPos = inPosition + _translate.xy;
  60. vec4 outPos = _transform * vec4(translatedPos, 0, 1);
  61. gl_Position = outPos;
  62. }
  63. )";
  64. static const char* shader_main_fragment_texture = RMLUI_SHADER_HEADER R"(
  65. uniform sampler2D _tex;
  66. in vec2 fragTexCoord;
  67. in vec4 fragColor;
  68. out vec4 finalColor;
  69. void main() {
  70. vec4 texColor = texture(_tex, fragTexCoord);
  71. finalColor = fragColor * texColor;
  72. }
  73. )";
  74. static const char* shader_main_fragment_color = RMLUI_SHADER_HEADER R"(
  75. in vec2 fragTexCoord;
  76. in vec4 fragColor;
  77. out vec4 finalColor;
  78. void main() {
  79. finalColor = fragColor;
  80. }
  81. )";
  82. namespace Gfx {
  83. enum class ProgramUniform { Translate, Transform, Tex, Count };
  84. static const char* const program_uniform_names[(size_t)ProgramUniform::Count] = {"_translate", "_transform", "_tex"};
  85. enum class VertexAttribute { Position, Color0, TexCoord0, Count };
  86. static const char* const vertex_attribute_names[(size_t)VertexAttribute::Count] = {"inPosition", "inColor0", "inTexCoord0"};
  87. struct CompiledGeometryData {
  88. GLuint texture;
  89. GLuint vao;
  90. GLuint vbo;
  91. GLuint ibo;
  92. GLsizei draw_count;
  93. };
  94. struct ProgramData {
  95. GLuint id;
  96. GLint uniform_locations[(size_t)ProgramUniform::Count];
  97. };
  98. struct ShadersData {
  99. ProgramData program_color;
  100. ProgramData program_texture;
  101. GLuint shader_main_vertex;
  102. GLuint shader_main_fragment_color;
  103. GLuint shader_main_fragment_texture;
  104. };
  105. static void CheckGLError(const char* operation_name)
  106. {
  107. #ifdef RMLUI_DEBUG
  108. GLenum error_code = glGetError();
  109. if (error_code != GL_NO_ERROR)
  110. {
  111. static const Rml::Pair<GLenum, const char*> error_names[] = {{GL_INVALID_ENUM, "GL_INVALID_ENUM"}, {GL_INVALID_VALUE, "GL_INVALID_VALUE"},
  112. {GL_INVALID_OPERATION, "GL_INVALID_OPERATION"}, {GL_OUT_OF_MEMORY, "GL_OUT_OF_MEMORY"}};
  113. const char* error_str = "''";
  114. for (auto& err : error_names)
  115. {
  116. if (err.first == error_code)
  117. {
  118. error_str = err.second;
  119. break;
  120. }
  121. }
  122. Rml::Log::Message(Rml::Log::LT_ERROR, "OpenGL error during %s. Error code 0x%x (%s).", operation_name, error_code, error_str);
  123. }
  124. #endif
  125. (void)operation_name;
  126. }
  127. // Create the shader, 'shader_type' is either GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
  128. static GLuint CreateShader(GLenum shader_type, const char* code_string)
  129. {
  130. GLuint id = glCreateShader(shader_type);
  131. glShaderSource(id, 1, (const GLchar**)&code_string, NULL);
  132. glCompileShader(id);
  133. GLint status = 0;
  134. glGetShaderiv(id, GL_COMPILE_STATUS, &status);
  135. if (status == GL_FALSE)
  136. {
  137. GLint info_log_length = 0;
  138. glGetShaderiv(id, GL_INFO_LOG_LENGTH, &info_log_length);
  139. char* info_log_string = new char[info_log_length + 1];
  140. glGetShaderInfoLog(id, info_log_length, NULL, info_log_string);
  141. Rml::Log::Message(Rml::Log::LT_ERROR, "Compile failure in OpenGL shader: %s", info_log_string);
  142. delete[] info_log_string;
  143. glDeleteShader(id);
  144. return 0;
  145. }
  146. CheckGLError("CreateShader");
  147. return id;
  148. }
  149. static void BindAttribLocations(GLuint program)
  150. {
  151. for (GLuint i = 0; i < (GLuint)VertexAttribute::Count; i++)
  152. {
  153. glBindAttribLocation(program, i, vertex_attribute_names[i]);
  154. }
  155. CheckGLError("BindAttribLocations");
  156. }
  157. static bool CreateProgram(GLuint vertex_shader, GLuint fragment_shader, ProgramData& out_program)
  158. {
  159. GLuint id = glCreateProgram();
  160. RMLUI_ASSERT(id);
  161. BindAttribLocations(id);
  162. glAttachShader(id, vertex_shader);
  163. glAttachShader(id, fragment_shader);
  164. glLinkProgram(id);
  165. glDetachShader(id, vertex_shader);
  166. glDetachShader(id, fragment_shader);
  167. GLint status = 0;
  168. glGetProgramiv(id, GL_LINK_STATUS, &status);
  169. if (status == GL_FALSE)
  170. {
  171. GLint info_log_length = 0;
  172. glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_length);
  173. char* info_log_string = new char[info_log_length + 1];
  174. glGetProgramInfoLog(id, info_log_length, NULL, info_log_string);
  175. Rml::Log::Message(Rml::Log::LT_ERROR, "OpenGL program linking failure: %s", info_log_string);
  176. delete[] info_log_string;
  177. glDeleteProgram(id);
  178. return false;
  179. }
  180. out_program = {};
  181. out_program.id = id;
  182. // Make a lookup table for the uniform locations.
  183. GLint num_active_uniforms = 0;
  184. glGetProgramiv(id, GL_ACTIVE_UNIFORMS, &num_active_uniforms);
  185. constexpr size_t name_size = 64;
  186. GLchar name_buf[name_size] = "";
  187. for (int unif = 0; unif < num_active_uniforms; ++unif)
  188. {
  189. GLint array_size = 0;
  190. GLenum type = 0;
  191. GLsizei actual_length = 0;
  192. glGetActiveUniform(id, unif, name_size, &actual_length, &array_size, &type, name_buf);
  193. GLint location = glGetUniformLocation(id, name_buf);
  194. // See if we have the name in our pre-defined name list.
  195. ProgramUniform program_uniform = ProgramUniform::Count;
  196. for (int i = 0; i < (int)ProgramUniform::Count; i++)
  197. {
  198. const char* uniform_name = program_uniform_names[i];
  199. if (strcmp(name_buf, uniform_name) == 0)
  200. {
  201. program_uniform = (ProgramUniform)i;
  202. break;
  203. }
  204. }
  205. if ((size_t)program_uniform < (size_t)ProgramUniform::Count)
  206. {
  207. out_program.uniform_locations[(size_t)program_uniform] = location;
  208. }
  209. else
  210. {
  211. Rml::Log::Message(Rml::Log::LT_ERROR, "OpenGL program uses unknown uniform '%s'.", name_buf);
  212. return false;
  213. }
  214. }
  215. CheckGLError("CreateProgram");
  216. return true;
  217. }
  218. static bool CreateShaders(ShadersData& out_shaders)
  219. {
  220. out_shaders = {};
  221. GLuint& main_vertex = out_shaders.shader_main_vertex;
  222. GLuint& main_fragment_color = out_shaders.shader_main_fragment_color;
  223. GLuint& main_fragment_texture = out_shaders.shader_main_fragment_texture;
  224. main_vertex = CreateShader(GL_VERTEX_SHADER, shader_main_vertex);
  225. if (!main_vertex)
  226. {
  227. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not create OpenGL shader: 'shader_main_vertex'.");
  228. return false;
  229. }
  230. main_fragment_color = CreateShader(GL_FRAGMENT_SHADER, shader_main_fragment_color);
  231. if (!main_fragment_color)
  232. {
  233. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not create OpenGL shader: 'shader_main_fragment_color'.");
  234. return false;
  235. }
  236. main_fragment_texture = CreateShader(GL_FRAGMENT_SHADER, shader_main_fragment_texture);
  237. if (!main_fragment_texture)
  238. {
  239. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not create OpenGL shader: 'shader_main_fragment_texture'.");
  240. return false;
  241. }
  242. if (!CreateProgram(main_vertex, main_fragment_color, out_shaders.program_color))
  243. {
  244. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not create OpenGL program: 'program_color'.");
  245. return false;
  246. }
  247. if (!CreateProgram(main_vertex, main_fragment_texture, out_shaders.program_texture))
  248. {
  249. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not create OpenGL program: 'program_texture'.");
  250. return false;
  251. }
  252. return true;
  253. }
  254. static void DestroyShaders(ShadersData& shaders)
  255. {
  256. glDeleteProgram(shaders.program_color.id);
  257. glDeleteProgram(shaders.program_texture.id);
  258. glDeleteShader(shaders.shader_main_vertex);
  259. glDeleteShader(shaders.shader_main_fragment_color);
  260. glDeleteShader(shaders.shader_main_fragment_texture);
  261. shaders = {};
  262. }
  263. } // namespace Gfx
  264. RenderInterface_GL3::RenderInterface_GL3()
  265. {
  266. shaders = Rml::MakeUnique<Gfx::ShadersData>();
  267. if (!Gfx::CreateShaders(*shaders))
  268. shaders.reset();
  269. }
  270. RenderInterface_GL3::~RenderInterface_GL3()
  271. {
  272. if (shaders)
  273. Gfx::DestroyShaders(*shaders);
  274. }
  275. void RenderInterface_GL3::SetViewport(int width, int height)
  276. {
  277. viewport_width = width;
  278. viewport_height = height;
  279. }
  280. void RenderInterface_GL3::BeginFrame()
  281. {
  282. RMLUI_ASSERT(viewport_width > 0 && viewport_height > 0);
  283. glViewport(0, 0, viewport_width, viewport_height);
  284. glClearStencil(0);
  285. glClearColor(0, 0, 0, 1);
  286. glDisable(GL_CULL_FACE);
  287. glEnable(GL_STENCIL_TEST);
  288. glStencilFunc(GL_ALWAYS, 1, GLuint(-1));
  289. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  290. glEnable(GL_BLEND);
  291. glBlendEquation(GL_FUNC_ADD);
  292. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  293. projection = Rml::Matrix4f::ProjectOrtho(0, (float)viewport_width, (float)viewport_height, 0, -10000, 10000);
  294. SetTransform(nullptr);
  295. }
  296. void RenderInterface_GL3::EndFrame() {}
  297. void RenderInterface_GL3::Clear()
  298. {
  299. glClearStencil(0);
  300. glClearColor(0, 0, 0, 1);
  301. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  302. }
  303. void RenderInterface_GL3::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rml::TextureHandle texture,
  304. const Rml::Vector2f& translation)
  305. {
  306. Rml::CompiledGeometryHandle geometry = CompileGeometry(vertices, num_vertices, indices, num_indices, texture);
  307. if (geometry)
  308. {
  309. RenderCompiledGeometry(geometry, translation);
  310. ReleaseCompiledGeometry(geometry);
  311. }
  312. }
  313. Rml::CompiledGeometryHandle RenderInterface_GL3::CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices,
  314. Rml::TextureHandle texture)
  315. {
  316. constexpr GLenum draw_usage = GL_STATIC_DRAW;
  317. GLuint vao = 0;
  318. GLuint vbo = 0;
  319. GLuint ibo = 0;
  320. glGenVertexArrays(1, &vao);
  321. glGenBuffers(1, &vbo);
  322. glGenBuffers(1, &ibo);
  323. glBindVertexArray(vao);
  324. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  325. glBufferData(GL_ARRAY_BUFFER, sizeof(Rml::Vertex) * num_vertices, (const void*)vertices, draw_usage);
  326. glEnableVertexAttribArray((GLuint)Gfx::VertexAttribute::Position);
  327. glVertexAttribPointer((GLuint)Gfx::VertexAttribute::Position, 2, GL_FLOAT, GL_FALSE, sizeof(Rml::Vertex),
  328. (const GLvoid*)(offsetof(Rml::Vertex, position)));
  329. glEnableVertexAttribArray((GLuint)Gfx::VertexAttribute::Color0);
  330. glVertexAttribPointer((GLuint)Gfx::VertexAttribute::Color0, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Rml::Vertex),
  331. (const GLvoid*)(offsetof(Rml::Vertex, colour)));
  332. glEnableVertexAttribArray((GLuint)Gfx::VertexAttribute::TexCoord0);
  333. glVertexAttribPointer((GLuint)Gfx::VertexAttribute::TexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Rml::Vertex),
  334. (const GLvoid*)(offsetof(Rml::Vertex, tex_coord)));
  335. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  336. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * num_indices, (const void*)indices, draw_usage);
  337. glBindVertexArray(0);
  338. Gfx::CheckGLError("CompileGeometry");
  339. Gfx::CompiledGeometryData* geometry = new Gfx::CompiledGeometryData;
  340. geometry->texture = (GLuint)texture;
  341. geometry->vao = vao;
  342. geometry->vbo = vbo;
  343. geometry->ibo = ibo;
  344. geometry->draw_count = num_indices;
  345. return (Rml::CompiledGeometryHandle)geometry;
  346. }
  347. void RenderInterface_GL3::RenderCompiledGeometry(Rml::CompiledGeometryHandle handle, const Rml::Vector2f& translation)
  348. {
  349. Gfx::CompiledGeometryData* geometry = (Gfx::CompiledGeometryData*)handle;
  350. if (geometry->texture)
  351. {
  352. glUseProgram(shaders->program_texture.id);
  353. glBindTexture(GL_TEXTURE_2D, geometry->texture);
  354. SubmitTransformUniform(ProgramId::Texture, shaders->program_texture.uniform_locations[(size_t)Gfx::ProgramUniform::Transform]);
  355. glUniform2fv(shaders->program_texture.uniform_locations[(size_t)Gfx::ProgramUniform::Translate], 1, &translation.x);
  356. }
  357. else
  358. {
  359. glUseProgram(shaders->program_color.id);
  360. glBindTexture(GL_TEXTURE_2D, 0);
  361. SubmitTransformUniform(ProgramId::Color, shaders->program_color.uniform_locations[(size_t)Gfx::ProgramUniform::Transform]);
  362. glUniform2fv(shaders->program_color.uniform_locations[(size_t)Gfx::ProgramUniform::Translate], 1, &translation.x);
  363. }
  364. glBindVertexArray(geometry->vao);
  365. glDrawElements(GL_TRIANGLES, geometry->draw_count, GL_UNSIGNED_INT, (const GLvoid*)0);
  366. Gfx::CheckGLError("RenderCompiledGeometry");
  367. }
  368. void RenderInterface_GL3::ReleaseCompiledGeometry(Rml::CompiledGeometryHandle handle)
  369. {
  370. Gfx::CompiledGeometryData* geometry = (Gfx::CompiledGeometryData*)handle;
  371. glDeleteVertexArrays(1, &geometry->vao);
  372. glDeleteBuffers(1, &geometry->vbo);
  373. glDeleteBuffers(1, &geometry->ibo);
  374. delete geometry;
  375. }
  376. void RenderInterface_GL3::EnableScissorRegion(bool enable)
  377. {
  378. ScissoringState new_state = ScissoringState::Disable;
  379. if (enable)
  380. new_state = (transform_active ? ScissoringState::Stencil : ScissoringState::Scissor);
  381. if (new_state != scissoring_state)
  382. {
  383. // Disable old
  384. if (scissoring_state == ScissoringState::Scissor)
  385. glDisable(GL_SCISSOR_TEST);
  386. else if (scissoring_state == ScissoringState::Stencil)
  387. glStencilFunc(GL_ALWAYS, 1, GLuint(-1));
  388. // Enable new
  389. if (new_state == ScissoringState::Scissor)
  390. glEnable(GL_SCISSOR_TEST);
  391. else if (new_state == ScissoringState::Stencil)
  392. glStencilFunc(GL_EQUAL, 1, GLuint(-1));
  393. scissoring_state = new_state;
  394. }
  395. }
  396. void RenderInterface_GL3::SetScissorRegion(int x, int y, int width, int height)
  397. {
  398. if (transform_active)
  399. {
  400. const float left = float(x);
  401. const float right = float(x + width);
  402. const float top = float(y);
  403. const float bottom = float(y + height);
  404. Rml::Vertex vertices[4];
  405. vertices[0].position = {left, top};
  406. vertices[1].position = {right, top};
  407. vertices[2].position = {right, bottom};
  408. vertices[3].position = {left, bottom};
  409. int indices[6] = {0, 2, 1, 0, 3, 2};
  410. glClear(GL_STENCIL_BUFFER_BIT);
  411. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  412. glStencilFunc(GL_ALWAYS, 1, GLuint(-1));
  413. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  414. RenderGeometry(vertices, 4, indices, 6, 0, Rml::Vector2f(0, 0));
  415. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  416. glStencilFunc(GL_EQUAL, 1, GLuint(-1));
  417. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  418. }
  419. else
  420. {
  421. glScissor(x, viewport_height - (y + height), width, height);
  422. }
  423. }
  424. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  425. #pragma pack(1)
  426. struct TGAHeader {
  427. char idLength;
  428. char colourMapType;
  429. char dataType;
  430. short int colourMapOrigin;
  431. short int colourMapLength;
  432. char colourMapDepth;
  433. short int xOrigin;
  434. short int yOrigin;
  435. short int width;
  436. short int height;
  437. char bitsPerPixel;
  438. char imageDescriptor;
  439. };
  440. // Restore packing
  441. #pragma pack()
  442. bool RenderInterface_GL3::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source)
  443. {
  444. Rml::FileInterface* file_interface = Rml::GetFileInterface();
  445. Rml::FileHandle file_handle = file_interface->Open(source);
  446. if (!file_handle)
  447. {
  448. return false;
  449. }
  450. file_interface->Seek(file_handle, 0, SEEK_END);
  451. size_t buffer_size = file_interface->Tell(file_handle);
  452. file_interface->Seek(file_handle, 0, SEEK_SET);
  453. if (buffer_size <= sizeof(TGAHeader))
  454. {
  455. Rml::Log::Message(Rml::Log::LT_ERROR, "Texture file size is smaller than TGAHeader, file is not a valid TGA image.");
  456. file_interface->Close(file_handle);
  457. return false;
  458. }
  459. using Rml::byte;
  460. byte* buffer = new byte[buffer_size];
  461. file_interface->Read(buffer, buffer_size, file_handle);
  462. file_interface->Close(file_handle);
  463. TGAHeader header;
  464. memcpy(&header, buffer, sizeof(TGAHeader));
  465. int color_mode = header.bitsPerPixel / 8;
  466. int image_size = header.width * header.height * 4; // We always make 32bit textures
  467. if (header.dataType != 2)
  468. {
  469. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  470. delete[] buffer;
  471. return false;
  472. }
  473. // Ensure we have at least 3 colors
  474. if (color_mode < 3)
  475. {
  476. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24 and 32bit textures are supported.");
  477. delete[] buffer;
  478. return false;
  479. }
  480. const byte* image_src = buffer + sizeof(TGAHeader);
  481. byte* image_dest = new byte[image_size];
  482. // Targa is BGR, swap to RGB and flip Y axis
  483. for (long y = 0; y < header.height; y++)
  484. {
  485. long read_index = y * header.width * color_mode;
  486. long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * 4;
  487. for (long x = 0; x < header.width; x++)
  488. {
  489. image_dest[write_index] = image_src[read_index + 2];
  490. image_dest[write_index + 1] = image_src[read_index + 1];
  491. image_dest[write_index + 2] = image_src[read_index];
  492. if (color_mode == 4)
  493. {
  494. const int alpha = image_src[read_index + 3];
  495. #ifdef RMLUI_SRGB_PREMULTIPLIED_ALPHA
  496. image_dest[write_index + 0] = (image_dest[write_index + 0] * alpha) / 255;
  497. image_dest[write_index + 1] = (image_dest[write_index + 1] * alpha) / 255;
  498. image_dest[write_index + 2] = (image_dest[write_index + 2] * alpha) / 255;
  499. #endif
  500. image_dest[write_index + 3] = (byte)alpha;
  501. }
  502. else
  503. {
  504. image_dest[write_index + 3] = 255;
  505. }
  506. write_index += 4;
  507. read_index += color_mode;
  508. }
  509. }
  510. texture_dimensions.x = header.width;
  511. texture_dimensions.y = header.height;
  512. bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  513. delete[] image_dest;
  514. delete[] buffer;
  515. return success;
  516. }
  517. bool RenderInterface_GL3::GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions)
  518. {
  519. GLuint texture_id = 0;
  520. glGenTextures(1, &texture_id);
  521. if (texture_id == 0)
  522. {
  523. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to generate texture.");
  524. return false;
  525. }
  526. glBindTexture(GL_TEXTURE_2D, texture_id);
  527. GLint internal_format = GL_RGBA8;
  528. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, source);
  529. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  530. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  531. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  532. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  533. texture_handle = (Rml::TextureHandle)texture_id;
  534. return true;
  535. }
  536. void RenderInterface_GL3::ReleaseTexture(Rml::TextureHandle texture_handle)
  537. {
  538. glDeleteTextures(1, (GLuint*)&texture_handle);
  539. }
  540. void RenderInterface_GL3::SetTransform(const Rml::Matrix4f* new_transform)
  541. {
  542. transform_active = (new_transform != nullptr);
  543. transform = projection * (new_transform ? *new_transform : Rml::Matrix4f::Identity());
  544. transform_dirty_state = ProgramId::All;
  545. }
  546. void RenderInterface_GL3::SubmitTransformUniform(ProgramId program_id, int uniform_location)
  547. {
  548. if ((int)program_id & (int)transform_dirty_state)
  549. {
  550. glUniformMatrix4fv(uniform_location, 1, false, transform.data());
  551. transform_dirty_state = ProgramId((int)transform_dirty_state & ~(int)program_id);
  552. }
  553. }
  554. bool RmlGL3::Initialize(Rml::String* out_message)
  555. {
  556. #if defined RMLUI_PLATFORM_EMSCRIPTEN
  557. if (out_message)
  558. *out_message = "Started Emscripten WebGL renderer.";
  559. #else
  560. const int gl_version = gladLoaderLoadGL();
  561. if (gl_version == 0)
  562. {
  563. if (out_message)
  564. *out_message = "Failed to initialize OpenGL context.";
  565. return false;
  566. }
  567. if (out_message)
  568. *out_message = Rml::CreateString(128, "Loaded OpenGL %d.%d.", GLAD_VERSION_MAJOR(gl_version), GLAD_VERSION_MINOR(gl_version));
  569. #endif
  570. return true;
  571. }
  572. void RmlGL3::Shutdown()
  573. {
  574. #if !defined RMLUI_PLATFORM_EMSCRIPTEN
  575. gladLoaderUnloadGL();
  576. #endif
  577. }