ocornut_imgui.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2014-2015 Daniel Collin. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bgfx.h>
  6. #include <bx/fpumath.h>
  7. #include <ocornut-imgui/imgui.h>
  8. #include "imgui.h"
  9. #include "ocornut_imgui.h"
  10. #include <stb/stb_image.c>
  11. #include "vs_ocornut_imgui.bin.h"
  12. #include "fs_ocornut_imgui.bin.h"
  13. static void imguiRender(ImDrawList** const _lists, int cmd_lists_count);
  14. struct OcornutImguiContext
  15. {
  16. void render(ImDrawList** const _lists, int _count)
  17. {
  18. const float width = ImGui::GetIO().DisplaySize.x;
  19. const float height = ImGui::GetIO().DisplaySize.y;
  20. float ortho[16];
  21. bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, -1.0f, 1.0f);
  22. bgfx::setViewTransform(m_viewId, NULL, ortho);
  23. // Render command lists
  24. for (int32_t ii = 0; ii < _count; ++ii)
  25. {
  26. bgfx::TransientVertexBuffer tvb;
  27. uint32_t vtx_size = 0;
  28. const ImDrawList* cmd_list = _lists[ii];
  29. const ImDrawVert* vtx_buffer = cmd_list->vtx_buffer.begin();
  30. const ImDrawCmd* pcmd_begin = cmd_list->commands.begin();
  31. const ImDrawCmd* pcmd_end = cmd_list->commands.end();
  32. for (const ImDrawCmd* pcmd = pcmd_begin; pcmd != pcmd_end; pcmd++)
  33. {
  34. vtx_size += (uint32_t)pcmd->vtx_count;
  35. }
  36. if (!bgfx::checkAvailTransientVertexBuffer(vtx_size, m_decl))
  37. {
  38. // not enough space in transient buffer just quit drawing the rest...
  39. break;
  40. }
  41. bgfx::allocTransientVertexBuffer(&tvb, vtx_size, m_decl);
  42. ImDrawVert* verts = (ImDrawVert*)tvb.data;
  43. memcpy(verts, vtx_buffer, vtx_size * sizeof(ImDrawVert));
  44. uint32_t vtx_offset = 0;
  45. for (const ImDrawCmd* pcmd = pcmd_begin; pcmd != pcmd_end; pcmd++)
  46. {
  47. bgfx::setState(0
  48. | BGFX_STATE_RGB_WRITE
  49. | BGFX_STATE_ALPHA_WRITE
  50. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  51. | BGFX_STATE_MSAA
  52. );
  53. bgfx::setScissor(uint16_t(pcmd->clip_rect.x)
  54. , uint16_t(pcmd->clip_rect.y)
  55. , uint16_t(pcmd->clip_rect.z-pcmd->clip_rect.x)
  56. , uint16_t(pcmd->clip_rect.w-pcmd->clip_rect.y)
  57. );
  58. bgfx::setTexture(0, s_tex, m_texture);
  59. bgfx::setVertexBuffer(&tvb, vtx_offset, pcmd->vtx_count);
  60. bgfx::setProgram(m_program);
  61. bgfx::submit(m_viewId);
  62. vtx_offset += pcmd->vtx_count;
  63. }
  64. }
  65. }
  66. void create(const void* _data, uint32_t _size, float _fontSize)
  67. {
  68. m_viewId = 31;
  69. ImGuiIO& io = ImGui::GetIO();
  70. io.DisplaySize = ImVec2(1280.0f, 720.0f);
  71. io.DeltaTime = 1.0f / 60.0f;
  72. // io.PixelCenterOffset = bgfx::RendererType::Direct3D9 == bgfx::getRendererType() ? -0.5f : 0.0f;
  73. const bgfx::Memory* vsmem;
  74. const bgfx::Memory* fsmem;
  75. switch (bgfx::getRendererType())
  76. {
  77. case bgfx::RendererType::Direct3D9:
  78. vsmem = bgfx::makeRef(vs_ocornut_imgui_dx9, sizeof(vs_ocornut_imgui_dx9));
  79. fsmem = bgfx::makeRef(fs_ocornut_imgui_dx9, sizeof(fs_ocornut_imgui_dx9));
  80. break;
  81. case bgfx::RendererType::Direct3D11:
  82. vsmem = bgfx::makeRef(vs_ocornut_imgui_dx11, sizeof(vs_ocornut_imgui_dx11));
  83. fsmem = bgfx::makeRef(fs_ocornut_imgui_dx11, sizeof(fs_ocornut_imgui_dx11));
  84. break;
  85. default:
  86. vsmem = bgfx::makeRef(vs_ocornut_imgui_glsl, sizeof(vs_ocornut_imgui_glsl));
  87. fsmem = bgfx::makeRef(fs_ocornut_imgui_glsl, sizeof(fs_ocornut_imgui_glsl));
  88. break;
  89. }
  90. bgfx::ShaderHandle vsh = bgfx::createShader(vsmem);
  91. bgfx::ShaderHandle fsh = bgfx::createShader(fsmem);
  92. m_program = bgfx::createProgram(vsh, fsh, true);
  93. m_decl
  94. .begin()
  95. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  96. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  97. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  98. .end();
  99. s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Uniform1i);
  100. uint8_t* data;
  101. int32_t width;
  102. int32_t height;
  103. void* font = malloc(_size);
  104. memcpy(font, _data, _size);
  105. io.Fonts->AddFontFromMemoryTTF(const_cast<void*>(font), _size, _fontSize);
  106. io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
  107. m_texture = bgfx::createTexture2D( (uint16_t)width
  108. , (uint16_t)height
  109. , 1
  110. , bgfx::TextureFormat::BGRA8
  111. , BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT
  112. , bgfx::copy(data, width*height*4)
  113. );
  114. io.RenderDrawListsFn = imguiRender;
  115. }
  116. void destroy()
  117. {
  118. bgfx::destroyUniform(s_tex);
  119. bgfx::destroyTexture(m_texture);
  120. bgfx::destroyProgram(m_program);
  121. }
  122. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int _width, int _height, char _inputChar, uint8_t _viewId)
  123. {
  124. m_viewId = _viewId;
  125. ImGuiIO& io = ImGui::GetIO();
  126. io.AddInputCharacter(_inputChar & 0x7f); // ASCII or GTFO! :)
  127. io.DisplaySize = ImVec2((float)_width, (float)_height);
  128. io.DeltaTime = 1.0f / 60.0f;
  129. io.MousePos = ImVec2((float)_mx, (float)_my);
  130. io.MouseDown[0] = 0 != (_button & IMGUI_MBUT_LEFT);
  131. ImGui::NewFrame();
  132. }
  133. void endFrame()
  134. {
  135. ImGui::Render();
  136. }
  137. bgfx::VertexDecl m_decl;
  138. bgfx::ProgramHandle m_program;
  139. bgfx::TextureHandle m_texture;
  140. bgfx::UniformHandle s_tex;
  141. uint8_t m_viewId;
  142. };
  143. static OcornutImguiContext s_ctx;
  144. static void imguiRender(ImDrawList** const _lists, int _count)
  145. {
  146. s_ctx.render(_lists, _count);
  147. }
  148. void IMGUI_create(const void* _data, uint32_t _size, float _fontSize)
  149. {
  150. s_ctx.create(_data, _size, _fontSize);
  151. }
  152. void IMGUI_destroy()
  153. {
  154. s_ctx.destroy();
  155. }
  156. void IMGUI_beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int _width, int _height, char _inputChar, uint8_t _viewId)
  157. {
  158. s_ctx.beginFrame(_mx, _my, _button, _width, _height, _inputChar, _viewId);
  159. }
  160. void IMGUI_endFrame()
  161. {
  162. s_ctx.endFrame();
  163. }