ocornut_imgui.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright 2014-2015 Daniel Collin. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bx/allocator.h>
  7. #include <bx/fpumath.h>
  8. #include <bx/timer.h>
  9. #include <ocornut-imgui/imgui.h>
  10. #include "imgui.h"
  11. #include "ocornut_imgui.h"
  12. #include <stb/stb_image.c>
  13. #if defined(SCI_NAMESPACE)
  14. # include "../entry/input.h"
  15. # include "scintilla.h"
  16. #endif // defined(SCI_NAMESPACE)
  17. #include "vs_ocornut_imgui.bin.h"
  18. #include "fs_ocornut_imgui.bin.h"
  19. struct OcornutImguiContext
  20. {
  21. static void* memAlloc(size_t _size);
  22. static void memFree(void* _ptr);
  23. static void renderDrawLists(ImDrawData* draw_data);
  24. void render(ImDrawData* draw_data)
  25. {
  26. const float width = ImGui::GetIO().DisplaySize.x;
  27. const float height = ImGui::GetIO().DisplaySize.y;
  28. float ortho[16];
  29. bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, -1.0f, 1.0f);
  30. bgfx::setViewTransform(m_viewId, NULL, ortho);
  31. // Render command lists
  32. for (int32_t ii = 0; ii < draw_data->CmdListsCount; ++ii)
  33. {
  34. bgfx::TransientVertexBuffer tvb;
  35. bgfx::TransientIndexBuffer tib;
  36. const ImDrawList* cmd_list = draw_data->CmdLists[ii];
  37. uint32_t vtx_size = (uint32_t)cmd_list->VtxBuffer.size();
  38. uint32_t idx_size = (uint32_t)cmd_list->IdxBuffer.size();
  39. if (!bgfx::checkAvailTransientVertexBuffer(vtx_size, m_decl) || !bgfx::checkAvailTransientIndexBuffer(idx_size) )
  40. {
  41. // not enough space in transient buffer just quit drawing the rest...
  42. break;
  43. }
  44. bgfx::allocTransientVertexBuffer(&tvb, vtx_size, m_decl);
  45. bgfx::allocTransientIndexBuffer(&tib, idx_size);
  46. ImDrawVert* verts = (ImDrawVert*)tvb.data;
  47. memcpy(verts, cmd_list->VtxBuffer.begin(), vtx_size * sizeof(ImDrawVert) );
  48. ImDrawIdx* indices = (ImDrawIdx*)tib.data;
  49. memcpy(indices, cmd_list->IdxBuffer.begin(), idx_size * sizeof(ImDrawIdx) );
  50. uint32_t elem_offset = 0;
  51. const ImDrawCmd* pcmd_begin = cmd_list->CmdBuffer.begin();
  52. const ImDrawCmd* pcmd_end = cmd_list->CmdBuffer.end();
  53. for (const ImDrawCmd* pcmd = pcmd_begin; pcmd != pcmd_end; pcmd++)
  54. {
  55. if (pcmd->UserCallback)
  56. {
  57. pcmd->UserCallback(cmd_list, pcmd);
  58. elem_offset += pcmd->ElemCount;
  59. continue;
  60. }
  61. if (0 == pcmd->ElemCount)
  62. {
  63. continue;
  64. }
  65. bgfx::setState(0
  66. | BGFX_STATE_RGB_WRITE
  67. | BGFX_STATE_ALPHA_WRITE
  68. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  69. | BGFX_STATE_MSAA
  70. );
  71. bgfx::setScissor(uint16_t(bx::fmax(pcmd->ClipRect.x, 0.0f) )
  72. , uint16_t(bx::fmax(pcmd->ClipRect.y, 0.0f) )
  73. , uint16_t(bx::fmin(pcmd->ClipRect.z, 65535.0f)-bx::fmax(pcmd->ClipRect.x, 0.0f) )
  74. , uint16_t(bx::fmin(pcmd->ClipRect.w, 65535.0f)-bx::fmax(pcmd->ClipRect.y, 0.0f) )
  75. );
  76. union { void* ptr; bgfx::TextureHandle handle; } texture = { pcmd->TextureId };
  77. bgfx::setTexture(0, s_tex, 0 != texture.handle.idx
  78. ? texture.handle
  79. : m_texture
  80. );
  81. bgfx::setVertexBuffer(&tvb, 0, vtx_size);
  82. bgfx::setIndexBuffer(&tib, elem_offset, pcmd->ElemCount);
  83. bgfx::submit(m_viewId, m_program);
  84. elem_offset += pcmd->ElemCount;
  85. }
  86. }
  87. }
  88. void create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
  89. {
  90. m_viewId = 255;
  91. m_allocator = _allocator;
  92. m_lastScroll = 0;
  93. m_last = bx::getHPCounter();
  94. ImGuiIO& io = ImGui::GetIO();
  95. io.RenderDrawListsFn = renderDrawLists;
  96. if (NULL != m_allocator)
  97. {
  98. io.MemAllocFn = memAlloc;
  99. io.MemFreeFn = memFree;
  100. }
  101. io.DisplaySize = ImVec2(1280.0f, 720.0f);
  102. io.DeltaTime = 1.0f / 60.0f;
  103. io.IniFilename = NULL;
  104. #if defined(SCI_NAMESPACE)
  105. io.KeyMap[ImGuiKey_Tab] = (int)entry::Key::Tab;
  106. io.KeyMap[ImGuiKey_LeftArrow] = (int)entry::Key::Left;
  107. io.KeyMap[ImGuiKey_RightArrow] = (int)entry::Key::Right;
  108. io.KeyMap[ImGuiKey_UpArrow] = (int)entry::Key::Up;
  109. io.KeyMap[ImGuiKey_DownArrow] = (int)entry::Key::Down;
  110. io.KeyMap[ImGuiKey_Home] = (int)entry::Key::Home;
  111. io.KeyMap[ImGuiKey_End] = (int)entry::Key::End;
  112. io.KeyMap[ImGuiKey_Delete] = (int)entry::Key::Delete;
  113. io.KeyMap[ImGuiKey_Backspace] = (int)entry::Key::Backspace;
  114. io.KeyMap[ImGuiKey_Enter] = (int)entry::Key::Return;
  115. io.KeyMap[ImGuiKey_Escape] = (int)entry::Key::Esc;
  116. io.KeyMap[ImGuiKey_A] = (int)entry::Key::KeyA;
  117. io.KeyMap[ImGuiKey_C] = (int)entry::Key::KeyC;
  118. io.KeyMap[ImGuiKey_V] = (int)entry::Key::KeyV;
  119. io.KeyMap[ImGuiKey_X] = (int)entry::Key::KeyX;
  120. io.KeyMap[ImGuiKey_Y] = (int)entry::Key::KeyY;
  121. io.KeyMap[ImGuiKey_Z] = (int)entry::Key::KeyZ;
  122. #endif // defined(SCI_NAMESPACE)
  123. const bgfx::Memory* vsmem;
  124. const bgfx::Memory* fsmem;
  125. switch (bgfx::getRendererType() )
  126. {
  127. case bgfx::RendererType::Direct3D9:
  128. vsmem = bgfx::makeRef(vs_ocornut_imgui_dx9, sizeof(vs_ocornut_imgui_dx9) );
  129. fsmem = bgfx::makeRef(fs_ocornut_imgui_dx9, sizeof(fs_ocornut_imgui_dx9) );
  130. break;
  131. case bgfx::RendererType::Direct3D11:
  132. case bgfx::RendererType::Direct3D12:
  133. vsmem = bgfx::makeRef(vs_ocornut_imgui_dx11, sizeof(vs_ocornut_imgui_dx11) );
  134. fsmem = bgfx::makeRef(fs_ocornut_imgui_dx11, sizeof(fs_ocornut_imgui_dx11) );
  135. break;
  136. case bgfx::RendererType::Metal:
  137. vsmem = bgfx::makeRef(vs_ocornut_imgui_mtl, sizeof(vs_ocornut_imgui_mtl) );
  138. fsmem = bgfx::makeRef(fs_ocornut_imgui_mtl, sizeof(fs_ocornut_imgui_mtl) );
  139. break;
  140. default:
  141. vsmem = bgfx::makeRef(vs_ocornut_imgui_glsl, sizeof(vs_ocornut_imgui_glsl) );
  142. fsmem = bgfx::makeRef(fs_ocornut_imgui_glsl, sizeof(fs_ocornut_imgui_glsl) );
  143. break;
  144. }
  145. bgfx::ShaderHandle vsh = bgfx::createShader(vsmem);
  146. bgfx::ShaderHandle fsh = bgfx::createShader(fsmem);
  147. m_program = bgfx::createProgram(vsh, fsh, true);
  148. m_decl
  149. .begin()
  150. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  151. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  152. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  153. .end();
  154. s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Int1);
  155. uint8_t* data;
  156. int32_t width;
  157. int32_t height;
  158. void* font = ImGui::MemAlloc(_size);
  159. memcpy(font, _data, _size);
  160. io.Fonts->AddFontFromMemoryTTF(font, _size, _fontSize);
  161. io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
  162. m_texture = bgfx::createTexture2D( (uint16_t)width
  163. , (uint16_t)height
  164. , 1
  165. , bgfx::TextureFormat::BGRA8
  166. , 0
  167. , bgfx::copy(data, width*height*4)
  168. );
  169. ImGuiStyle& style = ImGui::GetStyle();
  170. style.FrameRounding = 4.0f;
  171. }
  172. void destroy()
  173. {
  174. ImGui::Shutdown();
  175. bgfx::destroyUniform(s_tex);
  176. bgfx::destroyTexture(m_texture);
  177. bgfx::destroyProgram(m_program);
  178. m_allocator = NULL;
  179. }
  180. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
  181. {
  182. m_viewId = _viewId;
  183. ImGuiIO& io = ImGui::GetIO();
  184. if (_inputChar < 0x7f)
  185. {
  186. io.AddInputCharacter(_inputChar); // ASCII or GTFO! :(
  187. }
  188. io.DisplaySize = ImVec2( (float)_width, (float)_height);
  189. const int64_t now = bx::getHPCounter();
  190. const int64_t frameTime = now - m_last;
  191. m_last = now;
  192. const double freq = double(bx::getHPFrequency() );
  193. io.DeltaTime = float(frameTime/freq);
  194. io.MousePos = ImVec2( (float)_mx, (float)_my);
  195. io.MouseDown[0] = 0 != (_button & IMGUI_MBUT_LEFT);
  196. io.MouseDown[1] = 0 != (_button & IMGUI_MBUT_RIGHT);
  197. io.MouseDown[2] = 0 != (_button & IMGUI_MBUT_MIDDLE);
  198. io.MouseWheel = (float)(_scroll - m_lastScroll);
  199. m_lastScroll = _scroll;
  200. #if defined(SCI_NAMESPACE)
  201. uint8_t modifiers = inputGetModifiersState();
  202. io.KeyShift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
  203. io.KeyCtrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
  204. io.KeyAlt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
  205. for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
  206. {
  207. io.KeysDown[ii] = inputGetKeyState(entry::Key::Enum(ii) );
  208. }
  209. #endif // defined(SCI_NAMESPACE)
  210. ImGui::NewFrame();
  211. #if 0
  212. ImGui::ShowTestWindow(); //Debug only.
  213. #endif // 0
  214. #if 0
  215. extern void ShowExampleAppCustomNodeGraph(bool* opened);
  216. bool opened = true;
  217. ShowExampleAppCustomNodeGraph(&opened);
  218. #endif // 0
  219. #if defined(SCI_NAMESPACE) && 0
  220. bool opened = true;
  221. ImGuiScintilla("Scintilla Editor", &opened, ImVec2(640.0f, 480.0f) );
  222. #endif // 0
  223. }
  224. void endFrame()
  225. {
  226. ImGui::Render();
  227. }
  228. bx::AllocatorI* m_allocator;
  229. bgfx::VertexDecl m_decl;
  230. bgfx::ProgramHandle m_program;
  231. bgfx::TextureHandle m_texture;
  232. bgfx::UniformHandle s_tex;
  233. int64_t m_last;
  234. int32_t m_lastScroll;
  235. uint8_t m_viewId;
  236. };
  237. static OcornutImguiContext s_ctx;
  238. void* OcornutImguiContext::memAlloc(size_t _size)
  239. {
  240. return BX_ALLOC(s_ctx.m_allocator, _size);
  241. }
  242. void OcornutImguiContext::memFree(void* _ptr)
  243. {
  244. BX_FREE(s_ctx.m_allocator, _ptr);
  245. }
  246. void OcornutImguiContext::renderDrawLists(ImDrawData* draw_data)
  247. {
  248. s_ctx.render(draw_data);
  249. }
  250. void IMGUI_create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
  251. {
  252. s_ctx.create(_data, _size, _fontSize, _allocator);
  253. }
  254. void IMGUI_destroy()
  255. {
  256. s_ctx.destroy();
  257. }
  258. void IMGUI_beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
  259. {
  260. s_ctx.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _viewId);
  261. }
  262. void IMGUI_endFrame()
  263. {
  264. s_ctx.endFrame();
  265. }