tex_inspect_opengl.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. // ImGuiTexInspect, a texture inspector widget for dear imgui
  2. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #endif
  5. #if defined EMSCRIPTEN && defined IMGUI_TEX_INSPECT_FLOAT_READ_ENABLED
  6. #warning "Float texture read back is disabled on Emscripten"
  7. #undef IMGUI_TEX_INSPECT_FLOAT_READ_ENABLED
  8. #endif
  9. #include "imgui_tex_inspect_internal.h"
  10. // ==========================================================================
  11. // This file is largely based on:
  12. // https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_opengl3.cpp
  13. //
  14. // In the following section the ImGui_ImplOpenGL3_Init function has been
  15. // changed to not rewrite global ImGui state. It has also been wrapped in a
  16. // namespace to not clash with the main ImGui version. Aside from that this
  17. // section is identical to the imgui original.
  18. //
  19. // It's reproduced here because none of this code is exposed in the ImGui API
  20. // in a way that be reused (nor should it be).
  21. //
  22. // Search for "END COPIED" to find the end of the copied segment.
  23. // ===========================================================================
  24. // COPIED FROM imgui_impl_opengl3.cp ////////////////////////////////////////
  25. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  26. #define _CRT_SECURE_NO_WARNINGS
  27. #endif
  28. #define IMGUI_IMPL_OPENGL_LOADER_GLEW
  29. #include "imgui.h"
  30. #include <backends/imgui_impl_opengl3.h>
  31. #include <stdio.h>
  32. #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier
  33. #include <stddef.h> // intptr_t
  34. #else
  35. #include <stdint.h> // intptr_t
  36. #endif
  37. // GL includes
  38. #if defined(IMGUI_IMPL_OPENGL_ES2)
  39. #include <GLES2/gl2.h>
  40. #elif defined(IMGUI_IMPL_OPENGL_ES3)
  41. #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV))
  42. #include <OpenGLES/ES3/gl.h> // Use GL ES 3
  43. #else
  44. #include <GLES3/gl3.h> // Use GL ES 3
  45. #endif
  46. #else
  47. // About Desktop OpenGL function loaders:
  48. // Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers.
  49. // Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad).
  50. // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
  51. #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
  52. #include <GL/gl3w.h> // Needs to be initialized with gl3wInit() in user's code
  53. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
  54. #include <GL/glew.h> // Needs to be initialized with glewInit() in user's code.
  55. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
  56. #include <glad/glad.h> // Needs to be initialized with gladLoadGL() in user's code.
  57. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2)
  58. #include <glad/gl.h> // Needs to be initialized with gladLoadGL(...) or gladLoaderLoadGL() in user's code.
  59. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2)
  60. #ifndef GLFW_INCLUDE_NONE
  61. #define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors.
  62. #endif
  63. #include <glbinding/Binding.h> // Needs to be initialized with glbinding::Binding::initialize() in user's code.
  64. #include <glbinding/gl/gl.h>
  65. using namespace gl;
  66. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3)
  67. #ifndef GLFW_INCLUDE_NONE
  68. #define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors.
  69. #endif
  70. #include <glbinding/glbinding.h>// Needs to be initialized with glbinding::initialize() in user's code.
  71. #include <glbinding/gl/gl.h>
  72. using namespace gl;
  73. #elif defined(IMGUI_IMPL_OPENGL_LOADER_EPOXY)
  74. #include <epoxy/gl.h>
  75. #else
  76. #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM
  77. #endif
  78. #endif
  79. // Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have.
  80. #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_2)
  81. #define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
  82. #endif
  83. // Desktop GL 3.3+ has glBindSampler()
  84. #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_3)
  85. #define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
  86. #endif
  87. // Desktop GL 3.1+ has GL_PRIMITIVE_RESTART state
  88. #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_1)
  89. #define IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
  90. #endif
  91. namespace imgui_impl_opengl
  92. {
  93. // OpenGL Data
  94. static GLuint g_GlVersion = 0; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2)
  95. static char g_GlslVersionString[32] = ""; // Specified by user or detected based on compile time GL settings.
  96. static GLuint g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
  97. static GLint g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; // Uniforms location
  98. static GLuint g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0; // Vertex attributes location
  99. static GLint g_UniformLocationForceNearestSampling = 0;
  100. static GLint g_UniformLocationGridWidth = 0;
  101. // Functions
  102. static bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
  103. {
  104. // Query for GL version (e.g. 320 for GL 3.2)
  105. #if !defined(IMGUI_IMPL_OPENGL_ES2)
  106. GLint major = 0;
  107. GLint minor = 0;
  108. glGetIntegerv(GL_MAJOR_VERSION, &major);
  109. glGetIntegerv(GL_MINOR_VERSION, &minor);
  110. if (major == 0 && minor == 0)
  111. {
  112. // Query GL_VERSION in desktop GL 2.x, the string will start with "<major>.<minor>"
  113. const char* gl_version = (const char*)glGetString(GL_VERSION);
  114. sscanf(gl_version, "%d.%d", &major, &minor);
  115. }
  116. g_GlVersion = (GLuint)(major * 100 + minor * 10);
  117. #else
  118. g_GlVersion = 200; // GLES 2
  119. #endif
  120. // Store GLSL version string so we can refer to it later in case we recreate shaders.
  121. // Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure.
  122. #if defined(IMGUI_IMPL_OPENGL_ES2)
  123. if (glsl_version == NULL)
  124. glsl_version = "#version 100";
  125. #elif defined(IMGUI_IMPL_OPENGL_ES3)
  126. if (glsl_version == NULL)
  127. glsl_version = "#version 300 es";
  128. #elif defined(__APPLE__)
  129. if (glsl_version == NULL)
  130. glsl_version = "#version 150";
  131. #else
  132. if (glsl_version == NULL)
  133. glsl_version = "#version 130";
  134. #endif
  135. IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString));
  136. strcpy(g_GlslVersionString, glsl_version);
  137. strcat(g_GlslVersionString, "\n");
  138. // Debugging construct to make it easily visible in the IDE and debugger which GL loader has been selected.
  139. // The code actually never uses the 'gl_loader' variable! It is only here so you can read it!
  140. // If auto-detection fails or doesn't select the same GL loader file as used by your application,
  141. // you are likely to get a crash below.
  142. // You can explicitly select a loader by using '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line.
  143. const char* gl_loader = "Unknown";
  144. IM_UNUSED(gl_loader);
  145. #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
  146. gl_loader = "GL3W";
  147. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
  148. gl_loader = "GLEW";
  149. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
  150. gl_loader = "GLAD";
  151. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2)
  152. gl_loader = "GLAD2";
  153. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2)
  154. gl_loader = "glbinding2";
  155. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3)
  156. gl_loader = "glbinding3";
  157. #elif defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM)
  158. gl_loader = "custom";
  159. #else
  160. gl_loader = "none";
  161. #endif
  162. // Make an arbitrary GL call (we don't actually need the result)
  163. // IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code.
  164. // Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above.
  165. GLint current_texture;
  166. glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
  167. return true;
  168. }
  169. // ===========================================================================
  170. // COPIED FROM A DIFFERENT PART OF imgui_impl_opengl3.cpp
  171. // ===========================================================================
  172. // If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file.
  173. static bool CheckShader(GLuint handle, const char* desc)
  174. {
  175. GLint status = 0, log_length = 0;
  176. glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
  177. glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length);
  178. if ((GLboolean)status == GL_FALSE)
  179. fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s!\n", desc);
  180. if (log_length > 1)
  181. {
  182. ImVector<char> buf;
  183. buf.resize((int)(log_length + 1));
  184. glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
  185. fprintf(stderr, "%s\n", buf.begin());
  186. }
  187. return (GLboolean)status == GL_TRUE;
  188. }
  189. // If you get an error please report on GitHub. You may try different GL context version or GLSL version.
  190. static bool CheckProgram(GLuint handle, const char* desc)
  191. {
  192. GLint status = 0, log_length = 0;
  193. glGetProgramiv(handle, GL_LINK_STATUS, &status);
  194. glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length);
  195. if ((GLboolean)status == GL_FALSE)
  196. fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! (with GLSL '%s')\n", desc, g_GlslVersionString);
  197. if (log_length > 1)
  198. {
  199. ImVector<char> buf;
  200. buf.resize((int)(log_length + 1));
  201. glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
  202. fprintf(stderr, "%s\n", buf.begin());
  203. }
  204. return (GLboolean)status == GL_TRUE;
  205. }
  206. static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height)
  207. {
  208. // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
  209. glEnable(GL_BLEND);
  210. glBlendEquation(GL_FUNC_ADD);
  211. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  212. glDisable(GL_CULL_FACE);
  213. glDisable(GL_DEPTH_TEST);
  214. glDisable(GL_STENCIL_TEST);
  215. glEnable(GL_SCISSOR_TEST);
  216. #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
  217. if (g_GlVersion >= 310)
  218. glDisable(GL_PRIMITIVE_RESTART);
  219. #endif
  220. #ifdef GL_POLYGON_MODE
  221. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  222. #endif
  223. // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT)
  224. #if defined(GL_CLIP_ORIGIN)
  225. bool clip_origin_lower_left = true;
  226. if (g_GlVersion >= 450)
  227. {
  228. GLenum current_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)&current_clip_origin);
  229. if (current_clip_origin == GL_UPPER_LEFT)
  230. clip_origin_lower_left = false;
  231. }
  232. #endif
  233. // Setup viewport, orthographic projection matrix
  234. // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
  235. glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
  236. float L = draw_data->DisplayPos.x;
  237. float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
  238. float T = draw_data->DisplayPos.y;
  239. float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
  240. #if defined(GL_CLIP_ORIGIN)
  241. if (!clip_origin_lower_left) { float tmp = T; T = B; B = tmp; } // Swap top and bottom if origin is upper left
  242. #endif
  243. const float ortho_projection[4][4] =
  244. {
  245. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
  246. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
  247. { 0.0f, 0.0f, -1.0f, 0.0f },
  248. { (R+L)/(L-R), (T+B)/(B-T), 0.0f, 1.0f },
  249. };
  250. glUseProgram(g_ShaderHandle);
  251. glUniform1i(g_AttribLocationTex, 0);
  252. glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
  253. #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
  254. if (g_GlVersion >= 330)
  255. glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.
  256. #endif
  257. glEnableVertexAttribArray(g_AttribLocationVtxPos);
  258. glEnableVertexAttribArray(g_AttribLocationVtxUV);
  259. //glEnableVertexAttribArray(g_AttribLocationVtxColor); //Our shader doesn't use vertex color
  260. glVertexAttribPointer(g_AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos));
  261. glVertexAttribPointer(g_AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv));
  262. //glVertexAttribPointer(g_AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col));
  263. }
  264. // ===========================================================================
  265. // END COPIED FROM imgui_impl_opengl3.cpp
  266. // ---------------------------------------------------------------------------
  267. // Note that a lot of the following code still orginated in
  268. // imgui_impl_opengl3.cpp but there are more changes from here on.
  269. // ===========================================================================
  270. // New uniforms for ImGuiTexInspect fragment shader
  271. static GLint g_UniformLocationTextureSize;
  272. static GLint g_UniformLocationColorTransform;
  273. static GLint g_UniformLocationColorOffset;
  274. static GLint g_UniformLocationBackgroundColor;
  275. static GLint g_UniformLocationPremultiplyAlpha;
  276. static GLint g_UniformLocationDisableFinalAlpha;
  277. static GLint g_UniformLocationGrid;
  278. // Vertex shaders are directly from imgui_impl_opengl3.cpp
  279. const GLchar* vertex_shader_glsl_120 =
  280. "uniform mat4 ProjMtx;\n"
  281. "attribute vec2 Position;\n"
  282. "attribute vec2 UV;\n"
  283. "attribute vec4 Color;\n"
  284. "varying vec2 Frag_UV;\n"
  285. "varying vec4 Frag_Color;\n"
  286. "void main()\n"
  287. "{\n"
  288. " Frag_UV = UV;\n"
  289. " Frag_Color = Color;\n"
  290. " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
  291. "}\n";
  292. const GLchar* vertex_shader_glsl_130 =
  293. "uniform mat4 ProjMtx;\n"
  294. "in vec2 Position;\n"
  295. "in vec2 UV;\n"
  296. "in vec4 Color;\n"
  297. "out vec2 Frag_UV;\n"
  298. "out vec4 Frag_Color;\n"
  299. "void main()\n"
  300. "{\n"
  301. " Frag_UV = UV;\n"
  302. " Frag_Color = Color;\n"
  303. " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
  304. "}\n";
  305. const GLchar* vertex_shader_glsl_300_es =
  306. "precision mediump float;\n"
  307. "layout (location = 0) in vec2 Position;\n"
  308. "layout (location = 1) in vec2 UV;\n"
  309. "layout (location = 2) in vec4 Color;\n"
  310. "uniform mat4 ProjMtx;\n"
  311. "out vec2 Frag_UV;\n"
  312. "out vec4 Frag_Color;\n"
  313. "void main()\n"
  314. "{\n"
  315. " Frag_UV = UV;\n"
  316. " Frag_Color = Color;\n"
  317. " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
  318. "}\n";
  319. const GLchar* vertex_shader_glsl_410_core =
  320. "layout (location = 0) in vec2 Position;\n"
  321. "layout (location = 1) in vec2 UV;\n"
  322. "layout (location = 2) in vec4 Color;\n"
  323. "uniform mat4 ProjMtx;\n"
  324. "out vec2 Frag_UV;\n"
  325. "out vec4 Frag_Color;\n"
  326. "void main()\n"
  327. "{\n"
  328. " Frag_UV = UV;\n"
  329. " Frag_Color = Color;\n"
  330. " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
  331. "}\n";
  332. //-------------------------------------------------------------------------
  333. // [SECTION] IMGUI_TEX_INSPECT FRAGMENT SHADERS
  334. //-------------------------------------------------------------------------
  335. const GLchar *fragment_shader_glsl_120 = "#ifdef GL_ES\n"
  336. " precision mediump float;\n"
  337. "#endif\n"
  338. "uniform sampler2D Texture;\n"
  339. "uniform vec2 TextureSize;\n"
  340. "uniform mat4 ColorTransform;\n"
  341. "uniform vec4 ColorOffset;\n"
  342. "uniform vec3 BackgroundColor;\n"
  343. "uniform float PremultiplyAlpha;\n"
  344. "uniform float DisableFinalAlpha;\n"
  345. "uniform bool ForceNearestSampling;\n"
  346. "uniform vec4 Grid;\n"
  347. "uniform vec2 GridWidth;\n"
  348. "varying vec2 Frag_UV;\n"
  349. "void main()\n"
  350. "{\n"
  351. " vec2 uv;\n"
  352. " vec2 texel = Frag_UV * TextureSize;\n"
  353. " if (ForceNearestSampling)\n"
  354. " uv = (floor(texel) + vec2(0.5,0.5)) / TextureSize;\n"
  355. " else\n"
  356. " uv = Frag_UV;\n"
  357. " vec2 texelEdge = step(mod(texel,vec2(1.0,1.0)),GridWidth);\n"
  358. " float isGrid = max(texelEdge.x, texelEdge.y);\n"
  359. " vec4 ct = ColorTransform * texture2D(Texture, uv) + ColorOffset;\n"
  360. " ct.rgb = ct.rgb * mix(1.0, ct.a, PremultiplyAlpha);\n"
  361. " ct.rgb += BackgroundColor * (1.0-ct.a);\n"
  362. " ct.a = mix(ct.a, 1.0, DisableFinalAlpha);\n"
  363. " ct = mix(ct, vec4(Grid.rgb,1), Grid.a * isGrid);\n"
  364. " gl_FragColor = ct;\n"
  365. "}\n";
  366. const GLchar *fragment_shader_glsl_130 = "uniform sampler2D Texture;\n"
  367. "uniform vec2 TextureSize;\n"
  368. "uniform mat4 ColorTransform;\n"
  369. "uniform vec4 ColorOffset;\n"
  370. "uniform vec3 BackgroundColor;\n"
  371. "uniform float PremultiplyAlpha;\n"
  372. "uniform float DisableFinalAlpha;\n"
  373. "uniform bool ForceNearestSampling;\n"
  374. "uniform vec4 Grid;\n"
  375. "uniform vec2 GridWidth;\n"
  376. "in vec2 Frag_UV;\n"
  377. "out vec4 Out_Color;\n"
  378. "void main()\n"
  379. "{\n"
  380. " vec2 uv;\n"
  381. " vec2 texel = Frag_UV * TextureSize;\n"
  382. " if (ForceNearestSampling)\n"
  383. " uv = (floor(texel) + vec2(0.5,0.5)) / TextureSize;\n"
  384. " else\n"
  385. " uv = Frag_UV;\n"
  386. " vec2 texelEdge = step(mod(texel,vec2(1.0,1.0)),GridWidth);\n"
  387. " float isGrid = max(texelEdge.x, texelEdge.y);\n"
  388. " vec4 ct = ColorTransform * texture(Texture, uv) + ColorOffset;\n"
  389. " ct.rgb = ct.rgb * mix(1.0, ct.a, PremultiplyAlpha);\n"
  390. " ct.rgb += BackgroundColor * (1.0-ct.a);\n"
  391. " ct.a = mix(ct.a, 1.0, DisableFinalAlpha);\n"
  392. " ct = mix(ct, vec4(Grid.rgb,1), Grid.a * isGrid);\n"
  393. " Out_Color = ct;\n"
  394. "}\n";
  395. const GLchar *fragment_shader_glsl_300_es = "precision mediump float;\n"
  396. "uniform sampler2D Texture;\n"
  397. "uniform vec2 TextureSize;\n"
  398. "uniform mat4 ColorTransform;\n"
  399. "uniform vec4 ColorOffset;\n"
  400. "uniform vec3 BackgroundColor;\n"
  401. "uniform float PremultiplyAlpha;\n"
  402. "uniform float DisableFinalAlpha;\n"
  403. "uniform bool ForceNearestSampling;\n"
  404. "uniform vec4 Grid;\n"
  405. "uniform vec2 GridWidth;\n"
  406. "in vec2 Frag_UV;\n"
  407. "layout (location = 0) out vec4 Out_Color;\n"
  408. "void main()\n"
  409. "{\n"
  410. " vec2 uv;\n"
  411. " vec2 texel = Frag_UV * TextureSize;\n"
  412. " if (ForceNearestSampling)\n"
  413. " uv = (floor(texel) + vec2(0.5,0.5)) / TextureSize;\n"
  414. " else\n"
  415. " uv = Frag_UV;\n"
  416. " vec2 texelEdge = step(mod(texel,vec2(1.0,1.0)),GridWidth);\n"
  417. " float isGrid = max(texelEdge.x, texelEdge.y);\n"
  418. " vec4 ct = ColorTransform * texture(Texture, uv) + ColorOffset;\n"
  419. " ct.rgb = ct.rgb * mix(1.0, ct.a, PremultiplyAlpha);\n"
  420. " ct.rgb += BackgroundColor * (1.0-ct.a);\n"
  421. " ct.a = mix(ct.a, 1.0, DisableFinalAlpha);\n"
  422. " ct = mix(ct, vec4(Grid.rgb,1), Grid.a * isGrid);\n"
  423. " Out_Color = ct;\n"
  424. "}\n";
  425. const GLchar *fragment_shader_glsl_410_core = "uniform sampler2D Texture;\n"
  426. "uniform vec2 TextureSize;\n"
  427. "uniform mat4 ColorTransform;\n"
  428. "uniform vec4 ColorOffset;\n"
  429. "uniform vec3 BackgroundColor;\n"
  430. "uniform float PremultiplyAlpha;\n"
  431. "uniform float DisableFinalAlpha;\n"
  432. "uniform bool ForceNearestSampling;\n"
  433. "uniform vec4 Grid;\n"
  434. "uniform vec2 GridWidth;\n"
  435. "in vec2 Frag_UV;\n"
  436. "layout (location = 0) out vec4 Out_Color;\n"
  437. "void main()\n"
  438. "{\n"
  439. " vec2 uv;\n"
  440. " vec2 texel = Frag_UV * TextureSize;\n"
  441. " if (ForceNearestSampling)\n"
  442. " uv = (floor(texel) + vec2(0.5,0.5)) / TextureSize;\n"
  443. " else\n"
  444. " uv = Frag_UV;\n"
  445. " vec2 texelEdge = step(mod(texel,vec2(1.0,1.0)),GridWidth);\n"
  446. " float isGrid = max(texelEdge.x, texelEdge.y);\n"
  447. " vec4 ct = ColorTransform * texture(Texture, uv) + ColorOffset;\n"
  448. " ct.rgb = ct.rgb * mix(1.0, ct.a, PremultiplyAlpha);\n"
  449. " ct.rgb += BackgroundColor * (1.0-ct.a);\n"
  450. " ct.a = mix(ct.a, 1.0, DisableFinalAlpha);\n"
  451. " ct = mix(ct, vec4(Grid.rgb,1), Grid.a * isGrid);\n"
  452. " Out_Color = ct;\n"
  453. "}\n";
  454. /* BuildShader is from imgui_impl_opengl3.cpp. Only change is to query the
  455. * additional uniform locations for the new fragment shader*/
  456. void BuildShader()
  457. {
  458. // Shader selection code based on imgui_impl_opengl3.cpp
  459. // Parse GLSL version string
  460. int glsl_version = 130;
  461. sscanf(g_GlslVersionString, "#version %d", &glsl_version);
  462. // Select shaders matching our GLSL versions
  463. const GLchar *vertex_shader = NULL;
  464. const GLchar *fragment_shader = NULL;
  465. if (glsl_version < 130)
  466. {
  467. vertex_shader = vertex_shader_glsl_120;
  468. fragment_shader = fragment_shader_glsl_120;
  469. }
  470. else if (glsl_version >= 410)
  471. {
  472. vertex_shader = vertex_shader_glsl_410_core;
  473. fragment_shader = fragment_shader_glsl_410_core;
  474. }
  475. else if (glsl_version == 300)
  476. {
  477. vertex_shader = vertex_shader_glsl_300_es;
  478. fragment_shader = fragment_shader_glsl_300_es;
  479. }
  480. else
  481. {
  482. vertex_shader = vertex_shader_glsl_130;
  483. fragment_shader = fragment_shader_glsl_130;
  484. }
  485. if (fragment_shader == NULL)
  486. {
  487. fprintf(stderr, "ERROR: imgui_tex_inspect fragment shader for %s not implemented yet", g_GlslVersionString);
  488. }
  489. else
  490. {
  491. // Create shaders
  492. const GLchar *vertex_shader_with_version[2] = {g_GlslVersionString, vertex_shader};
  493. g_VertHandle = glCreateShader(GL_VERTEX_SHADER);
  494. glShaderSource(g_VertHandle, 2, vertex_shader_with_version, NULL);
  495. glCompileShader(g_VertHandle);
  496. CheckShader(g_VertHandle, "vertex shader");
  497. const GLchar *fragment_shader_with_version[2] = {g_GlslVersionString, fragment_shader};
  498. g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER);
  499. glShaderSource(g_FragHandle, 2, fragment_shader_with_version, NULL);
  500. glCompileShader(g_FragHandle);
  501. CheckShader(g_FragHandle, "fragment shader");
  502. g_ShaderHandle = glCreateProgram();
  503. glAttachShader(g_ShaderHandle, g_VertHandle);
  504. glAttachShader(g_ShaderHandle, g_FragHandle);
  505. glLinkProgram(g_ShaderHandle);
  506. CheckProgram(g_ShaderHandle, "shader program");
  507. g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture");
  508. g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx");
  509. g_AttribLocationVtxPos = (GLuint)glGetAttribLocation(g_ShaderHandle, "Position");
  510. g_AttribLocationVtxUV = (GLuint)glGetAttribLocation(g_ShaderHandle, "UV");
  511. // Change from imgui_impl_opengl3.cpp (Our shader doesn't use vertex color)
  512. //g_AttribLocationVtxColor = (GLuint)glGetAttribLocation(g_ShaderHandle, "Color");
  513. // New uniforms used by imgui_tex_inspect
  514. g_UniformLocationTextureSize = glGetUniformLocation(g_ShaderHandle, "TextureSize");
  515. g_UniformLocationColorTransform = glGetUniformLocation(g_ShaderHandle, "ColorTransform");
  516. g_UniformLocationColorOffset = glGetUniformLocation(g_ShaderHandle, "ColorOffset");
  517. g_UniformLocationBackgroundColor = glGetUniformLocation(g_ShaderHandle, "BackgroundColor");
  518. g_UniformLocationPremultiplyAlpha = glGetUniformLocation(g_ShaderHandle, "PremultiplyAlpha");
  519. g_UniformLocationDisableFinalAlpha = glGetUniformLocation(g_ShaderHandle, "DisableFinalAlpha");
  520. g_UniformLocationGrid = glGetUniformLocation(g_ShaderHandle, "Grid");
  521. g_UniformLocationForceNearestSampling = glGetUniformLocation(g_ShaderHandle, "ForceNearestSampling");
  522. g_UniformLocationGridWidth = glGetUniformLocation(g_ShaderHandle, "GridWidth");
  523. }
  524. }
  525. } // namespace imgui_impl_opengl
  526. namespace ImGuiTexInspect
  527. {
  528. using namespace imgui_impl_opengl;
  529. static GLuint readbackFramebuffer = 0;
  530. //-------------------------------------------------------------------------
  531. // [SECTION] Init and Shutdown
  532. //-------------------------------------------------------------------------
  533. bool ImplOpenGL3_Init(const char *glsl_version)
  534. {
  535. imgui_impl_opengl::ImGui_ImplOpenGL3_Init(glsl_version);
  536. BuildShader();
  537. glGenFramebuffers(1, &readbackFramebuffer);
  538. return true;
  539. }
  540. void ImplOpenGl3_Shutdown()
  541. {
  542. // No need to call ImGui_ImplOpenGL3_Shutdown, it doesn't even
  543. // exist in the imgui_impl_opengl namespace. Our version of
  544. // ImGui_ImplOpenGL3_Init doesn't affect OpenGL state.
  545. glDeleteShader(g_VertHandle);
  546. glDeleteShader(g_FragHandle);
  547. glDeleteProgram(g_ShaderHandle);
  548. g_VertHandle = 0;
  549. g_FragHandle = 0;
  550. g_ShaderHandle = 0;
  551. glDeleteFramebuffers(1, &readbackFramebuffer);
  552. readbackFramebuffer = 0;
  553. }
  554. void GiveNotInitializedWarning()
  555. {
  556. static bool warningGiven = false;
  557. if (!warningGiven)
  558. {
  559. fprintf(stderr, "ERROR: ImGuiTexInspect backend not initialized\n");
  560. warningGiven = true;
  561. }
  562. }
  563. //-------------------------------------------------------------------------
  564. // [SECTION] BackEnd functions declared in imgui_tex_inspect_internal.h
  565. //-------------------------------------------------------------------------
  566. void BackEnd_SetShader(const ImDrawList *, const ImDrawCmd *, const Inspector *inspector)
  567. {
  568. const ShaderOptions *texConversion = &inspector->CachedShaderOptions;
  569. if (g_ShaderHandle)
  570. {
  571. ImDrawData *draw_data = ImGui::GetDrawData();
  572. int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
  573. int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
  574. if (fb_width <= 0 || fb_height <= 0)
  575. return;
  576. // Setup normal ImGui GL state
  577. ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height);
  578. // Setup imgui_tex_inspect specific shader uniforms
  579. glUniformMatrix4fv(g_UniformLocationColorTransform, 1, GL_FALSE, texConversion->ColorTransform);
  580. glUniform2fv(g_UniformLocationTextureSize, 1, &inspector->TextureSize.x);
  581. glUniform4fv(g_UniformLocationColorOffset, 1, texConversion->ColorOffset);
  582. glUniform3fv(g_UniformLocationBackgroundColor, 1, &texConversion->BackgroundColor.x);
  583. glUniform1f(g_UniformLocationPremultiplyAlpha, texConversion->PremultiplyAlpha);
  584. glUniform1f(g_UniformLocationDisableFinalAlpha, texConversion->DisableFinalAlpha);
  585. glUniform1i(g_UniformLocationForceNearestSampling, texConversion->ForceNearestSampling);
  586. glUniform2fv(g_UniformLocationGridWidth, 1, &texConversion->GridWidth.x);
  587. glUniform4fv(g_UniformLocationGrid, 1, &texConversion->GridColor.x);
  588. }
  589. else
  590. {
  591. GiveNotInitializedWarning();
  592. }
  593. }
  594. bool BackEnd_GetData(Inspector *inspector, ImTextureID texture, int /*x*/, int /*y*/, int /*width*/, int /*height*/, BufferDesc *bufferDesc)
  595. {
  596. // Current simple implementation just gets data for whole texture
  597. if (readbackFramebuffer == 0)
  598. {
  599. GiveNotInitializedWarning();
  600. return false;
  601. }
  602. const int numChannels = 4;
  603. glGetError(); // Discard any current error so that check at end of function is useful
  604. void * data;
  605. int texWidth = (int)inspector->TextureSize.x;
  606. int texHeight = (int)inspector->TextureSize.y;
  607. GLuint glTexture = (GLuint)(uintptr_t)texture; //Double cast to avoid warning
  608. #ifdef IMGUI_TEX_INSPECT_FLOAT_READ_ENABLED
  609. size_t bufferSize = sizeof(float) * texWidth * texHeight * numChannels;
  610. bufferDesc->Data_float = (float *)GetBuffer(inspector, bufferSize);
  611. GLuint type = GL_FLOAT;
  612. data = (void *)bufferDesc->Data_float;
  613. #else
  614. size_t bufferSize = sizeof(uint8_t) * texWidth * texHeight * numChannels;
  615. bufferDesc->Data_uint8_t = (uint8_t *)GetBuffer(inspector, bufferSize);
  616. GLuint type = GL_UNSIGNED_BYTE;
  617. data = (void *)bufferDesc->Data_uint8_t;
  618. #endif
  619. if (data == NULL)
  620. return false;
  621. bufferDesc->BufferByteSize = bufferSize;
  622. bufferDesc->Red = 0; // Data is packed such that red channel is first
  623. bufferDesc->Green = 1; // then green, then blue, the alpha. Hence, 0,1,2,3
  624. bufferDesc->Blue = 2; // for these channel order values.
  625. bufferDesc->Alpha = 3;
  626. bufferDesc->ChannelCount = 4; // RGBA
  627. bufferDesc->LineStride = (int)inspector->TextureSize.x * numChannels;
  628. bufferDesc->Stride = 4; // No padding between each RGBA texel
  629. bufferDesc->StartX = 0; // We queried the whole texture
  630. bufferDesc->StartY = 0;
  631. bufferDesc->Width = texWidth;
  632. bufferDesc->Height = texHeight;
  633. // Save current frame buffer so we can restore it
  634. GLuint currentFramebuffer;
  635. glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint *)&currentFramebuffer);
  636. // Read texture data
  637. glBindFramebuffer(GL_FRAMEBUFFER, readbackFramebuffer);
  638. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glTexture, 0);
  639. glReadPixels(0, 0, texWidth, texHeight, GL_RGBA, type, data);
  640. // Restore previous framebuffer
  641. glBindFramebuffer(GL_FRAMEBUFFER, currentFramebuffer);
  642. return glGetError() == GL_NO_ERROR;
  643. }
  644. } // namespace ImGuiTexInspect