Common.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. // Include ImGUI
  7. #include <AnKi/Ui/ImGuiConfig.h>
  8. #include <ImGui/imgui.h>
  9. #include <AnKi/Util/Allocator.h>
  10. #include <AnKi/Util/Ptr.h>
  11. #include <AnKi/Gr/TextureView.h>
  12. namespace anki {
  13. // Forward
  14. class UiManager;
  15. /// @addtogroup ui
  16. /// @{
  17. #define ANKI_UI_LOGI(...) ANKI_LOG("UI ", NORMAL, __VA_ARGS__)
  18. #define ANKI_UI_LOGE(...) ANKI_LOG("UI ", ERROR, __VA_ARGS__)
  19. #define ANKI_UI_LOGW(...) ANKI_LOG("UI ", WARNING, __VA_ARGS__)
  20. #define ANKI_UI_LOGF(...) ANKI_LOG("UI ", FATAL, __VA_ARGS__)
  21. using UiAllocator = HeapAllocator<U8>;
  22. #define ANKI_UI_OBJECT_FW(name_) \
  23. class name_; \
  24. using name_##Ptr = IntrusivePtr<name_>;
  25. ANKI_UI_OBJECT_FW(Font)
  26. ANKI_UI_OBJECT_FW(Canvas)
  27. ANKI_UI_OBJECT_FW(UiImmediateModeBuilder)
  28. #undef ANKI_UI_OBJECT
  29. inline Vec2 toAnki(const ImVec2& v)
  30. {
  31. return Vec2(v.x, v.y);
  32. }
  33. /// This is extra data required by UiImageId.
  34. /// Since UiImageId needs to map to ImTextureID, UiImageId can only be a single pointer. Thus extra data required for
  35. /// custom drawing of that image need a different structure.
  36. class UiImageIdExtra
  37. {
  38. public:
  39. TextureViewPtr m_textureView;
  40. ShaderProgramPtr m_customProgram;
  41. U8 m_extraPushConstantsSize = 0;
  42. Array<U8, 64> m_extraPushConstants;
  43. void setExtraPushConstants(const void* ptr, PtrSize pushConstantSize)
  44. {
  45. ANKI_ASSERT(ptr);
  46. ANKI_ASSERT(pushConstantSize > 0 && pushConstantSize < sizeof(m_extraPushConstants));
  47. m_extraPushConstantsSize = U8(pushConstantSize);
  48. memcpy(m_extraPushConstants.getBegin(), ptr, pushConstantSize);
  49. }
  50. };
  51. /// This is what someone should push to ImGui::Image() function.
  52. class UiImageId
  53. {
  54. friend class Canvas;
  55. public:
  56. /// Construct a simple UiImageId that only points to a texture view.
  57. UiImageId(TextureViewPtr textureView, Bool pointSampling = false)
  58. {
  59. m_bits.m_textureViewPtrOrComplex = ptrToNumber(textureView.get()) & 0x3FFFFFFFFFFFFFFFllu;
  60. m_bits.m_pointSampling = pointSampling;
  61. m_bits.m_extra = false;
  62. }
  63. /// Construct a complex UiImageId that points to an UiImageIdExtra structure.
  64. UiImageId(const UiImageIdExtra* extra, Bool pointSampling = false)
  65. {
  66. m_bits.m_textureViewPtrOrComplex = ptrToNumber(extra) & 0x3FFFFFFFFFFFFFFFllu;
  67. m_bits.m_pointSampling = pointSampling;
  68. m_bits.m_extra = true;
  69. }
  70. operator void*() const
  71. {
  72. return numberToPtr<void*>(m_allBits);
  73. }
  74. private:
  75. class Bits
  76. {
  77. public:
  78. U64 m_textureViewPtrOrComplex : 62;
  79. U64 m_pointSampling : 1;
  80. U64 m_extra : 1;
  81. };
  82. union
  83. {
  84. Bits m_bits;
  85. U64 m_allBits;
  86. };
  87. UiImageId(void* ptr)
  88. : m_allBits(ptrToNumber(ptr))
  89. {
  90. ANKI_ASSERT(ptr);
  91. }
  92. };
  93. static_assert(sizeof(UiImageId) == sizeof(void*), "See file");
  94. /// @}
  95. } // end namespace anki