ocornut_imgui.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 = 255;
  69. ImGuiIO& io = ImGui::GetIO();
  70. io.DisplaySize = ImVec2(1280.0f, 720.0f);
  71. io.DeltaTime = 1.0f / 60.0f;
  72. io.IniFilename = NULL;
  73. // io.PixelCenterOffset = bgfx::RendererType::Direct3D9 == bgfx::getRendererType() ? -0.5f : 0.0f;
  74. const bgfx::Memory* vsmem;
  75. const bgfx::Memory* fsmem;
  76. switch (bgfx::getRendererType())
  77. {
  78. case bgfx::RendererType::Direct3D9:
  79. vsmem = bgfx::makeRef(vs_ocornut_imgui_dx9, sizeof(vs_ocornut_imgui_dx9));
  80. fsmem = bgfx::makeRef(fs_ocornut_imgui_dx9, sizeof(fs_ocornut_imgui_dx9));
  81. break;
  82. case bgfx::RendererType::Direct3D11:
  83. case bgfx::RendererType::Direct3D12:
  84. vsmem = bgfx::makeRef(vs_ocornut_imgui_dx11, sizeof(vs_ocornut_imgui_dx11));
  85. fsmem = bgfx::makeRef(fs_ocornut_imgui_dx11, sizeof(fs_ocornut_imgui_dx11));
  86. break;
  87. default:
  88. vsmem = bgfx::makeRef(vs_ocornut_imgui_glsl, sizeof(vs_ocornut_imgui_glsl));
  89. fsmem = bgfx::makeRef(fs_ocornut_imgui_glsl, sizeof(fs_ocornut_imgui_glsl));
  90. break;
  91. }
  92. bgfx::ShaderHandle vsh = bgfx::createShader(vsmem);
  93. bgfx::ShaderHandle fsh = bgfx::createShader(fsmem);
  94. m_program = bgfx::createProgram(vsh, fsh, true);
  95. m_decl
  96. .begin()
  97. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  98. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  99. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  100. .end();
  101. s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Uniform1i);
  102. uint8_t* data;
  103. int32_t width;
  104. int32_t height;
  105. void* font = ImGui::MemAlloc(_size);
  106. memcpy(font, _data, _size);
  107. io.Fonts->AddFontFromMemoryTTF(font, _size, _fontSize);
  108. io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
  109. m_texture = bgfx::createTexture2D( (uint16_t)width
  110. , (uint16_t)height
  111. , 1
  112. , bgfx::TextureFormat::BGRA8
  113. , BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT
  114. , bgfx::copy(data, width*height*4)
  115. );
  116. ImGuiStyle& style = ImGui::GetStyle();
  117. style.FrameRounding = 4.0f;
  118. io.RenderDrawListsFn = imguiRender;
  119. }
  120. void destroy()
  121. {
  122. bgfx::destroyUniform(s_tex);
  123. bgfx::destroyTexture(m_texture);
  124. bgfx::destroyProgram(m_program);
  125. }
  126. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int _width, int _height, char _inputChar, uint8_t _viewId)
  127. {
  128. m_viewId = _viewId;
  129. ImGuiIO& io = ImGui::GetIO();
  130. io.AddInputCharacter(_inputChar & 0x7f); // ASCII or GTFO! :)
  131. io.DisplaySize = ImVec2((float)_width, (float)_height);
  132. io.DeltaTime = 1.0f / 60.0f;
  133. io.MousePos = ImVec2((float)_mx, (float)_my);
  134. io.MouseDown[0] = 0 != (_button & IMGUI_MBUT_LEFT);
  135. ImGui::NewFrame();
  136. //ImGui::ShowTestWindow(); //Debug only.
  137. }
  138. void endFrame()
  139. {
  140. ImGui::Render();
  141. }
  142. bgfx::VertexDecl m_decl;
  143. bgfx::ProgramHandle m_program;
  144. bgfx::TextureHandle m_texture;
  145. bgfx::UniformHandle s_tex;
  146. uint8_t m_viewId;
  147. };
  148. static OcornutImguiContext s_ctx;
  149. static void imguiRender(ImDrawList** const _lists, int _count)
  150. {
  151. s_ctx.render(_lists, _count);
  152. }
  153. void IMGUI_create(const void* _data, uint32_t _size, float _fontSize)
  154. {
  155. s_ctx.create(_data, _size, _fontSize);
  156. }
  157. void IMGUI_destroy()
  158. {
  159. s_ctx.destroy();
  160. }
  161. void IMGUI_beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int _width, int _height, char _inputChar, uint8_t _viewId)
  162. {
  163. s_ctx.beginFrame(_mx, _my, _button, _width, _height, _inputChar, _viewId);
  164. }
  165. void IMGUI_endFrame()
  166. {
  167. s_ctx.endFrame();
  168. }