ImageViewerMain.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/AnKi.h>
  6. using namespace anki;
  7. class TextureViewerUiNode : public SceneNode
  8. {
  9. public:
  10. ImageResourcePtr m_imageResource;
  11. TextureViewerUiNode(CString name)
  12. : SceneNode(name)
  13. {
  14. UiComponent* uic = newComponent<UiComponent>();
  15. uic->init(
  16. [](CanvasPtr& canvas, void* ud) {
  17. static_cast<TextureViewerUiNode*>(ud)->draw(canvas);
  18. },
  19. this);
  20. ANKI_CHECK_AND_IGNORE(UiManager::getSingleton().newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf", Array<U32, 1>{16}));
  21. ANKI_CHECK_AND_IGNORE(ResourceManager::getSingleton().loadResource("ShaderBinaries/UiVisualizeImage.ankiprogbin", m_imageProgram));
  22. }
  23. Error frameUpdate([[maybe_unused]] Second prevUpdateTime, [[maybe_unused]] Second crntTime) override
  24. {
  25. if(!m_textureView.isCreated())
  26. {
  27. m_textureView.reset(&m_imageResource->getTextureView());
  28. }
  29. return Error::kNone;
  30. }
  31. private:
  32. FontPtr m_font;
  33. ShaderProgramResourcePtr m_imageProgram;
  34. ShaderProgramPtr m_imageGrProgram;
  35. TextureViewPtr m_textureView;
  36. UiImageIdExtra m_imageIdExtra;
  37. U32 m_crntMip = 0;
  38. F32 m_zoom = 1.0f;
  39. F32 m_depth = 0.0f;
  40. Bool m_pointSampling = true;
  41. Array<Bool, 4> m_colorChannel = {true, true, true, true};
  42. F32 m_maxColorValue = 1.0f;
  43. void draw(CanvasPtr& canvas)
  44. {
  45. const Texture& grTex = m_imageResource->getTexture();
  46. const U32 colorComponentCount = getFormatInfo(grTex.getFormat()).m_componentCount;
  47. ANKI_ASSERT(grTex.getTextureType() == TextureType::k2D || grTex.getTextureType() == TextureType::k3D);
  48. if(!m_imageGrProgram.isCreated())
  49. {
  50. ShaderProgramResourceVariantInitInfo variantInit(m_imageProgram);
  51. variantInit.addMutation("TEXTURE_TYPE", (grTex.getTextureType() == TextureType::k2D) ? 0 : 1);
  52. const ShaderProgramResourceVariant* variant;
  53. m_imageProgram->getOrCreateVariant(variantInit, variant);
  54. m_imageGrProgram.reset(&variant->getProgram());
  55. }
  56. ImGui::Begin("Console", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove);
  57. canvas->pushFont(m_font, 16);
  58. ImGui::SetWindowPos(Vec2(0.0f, 0.0f));
  59. ImGui::SetWindowSize(Vec2(F32(canvas->getWidth()), F32(canvas->getHeight())));
  60. ImGui::BeginChild("Tools", Vec2(-1.0f, 30.0f), false, ImGuiWindowFlags_AlwaysAutoResize);
  61. // Zoom
  62. if(ImGui::Button("-"))
  63. {
  64. m_zoom -= 0.1f;
  65. }
  66. ImGui::SameLine();
  67. ImGui::DragFloat("##Zoom", &m_zoom, 0.01f, 0.1f, 20.0f, "Zoom %.3f");
  68. ImGui::SameLine();
  69. if(ImGui::Button("+"))
  70. {
  71. m_zoom += 0.1f;
  72. }
  73. ImGui::SameLine();
  74. ImGui::Spacing();
  75. ImGui::SameLine();
  76. // Sampling
  77. ImGui::Checkbox("Point sampling", &m_pointSampling);
  78. ImGui::SameLine();
  79. ImGui::Spacing();
  80. ImGui::SameLine();
  81. // Colors
  82. ImGui::Checkbox("Red", &m_colorChannel[0]);
  83. ImGui::SameLine();
  84. ImGui::Checkbox("Green", &m_colorChannel[1]);
  85. ImGui::SameLine();
  86. ImGui::Checkbox("Blue", &m_colorChannel[2]);
  87. ImGui::SameLine();
  88. if(colorComponentCount == 4)
  89. {
  90. ImGui::Checkbox("Alpha", &m_colorChannel[3]);
  91. ImGui::SameLine();
  92. }
  93. ImGui::Spacing();
  94. ImGui::SameLine();
  95. // Mips combo
  96. {
  97. UiStringList mipLabels;
  98. for(U32 mip = 0; mip < grTex.getMipmapCount(); ++mip)
  99. {
  100. mipLabels.pushBackSprintf("Mip %u (%u x %u)", mip, grTex.getWidth() >> mip, grTex.getHeight() >> mip);
  101. }
  102. const U32 lastCrntMip = m_crntMip;
  103. if(ImGui::BeginCombo("##Mipmap", (mipLabels.getBegin() + m_crntMip)->cstr(), ImGuiComboFlags_HeightLarge))
  104. {
  105. for(U32 mip = 0; mip < grTex.getMipmapCount(); ++mip)
  106. {
  107. const Bool isSelected = (m_crntMip == mip);
  108. if(ImGui::Selectable((mipLabels.getBegin() + mip)->cstr(), isSelected))
  109. {
  110. m_crntMip = mip;
  111. }
  112. if(isSelected)
  113. {
  114. ImGui::SetItemDefaultFocus();
  115. }
  116. }
  117. ImGui::EndCombo();
  118. }
  119. if(lastCrntMip != m_crntMip)
  120. {
  121. // Re-create the image view
  122. TextureViewInitInfo viewInitInf(&m_imageResource->getTexture());
  123. viewInitInf.m_firstMipmap = m_crntMip;
  124. viewInitInf.m_mipmapCount = 1;
  125. m_textureView = GrManager::getSingleton().newTextureView(viewInitInf);
  126. }
  127. ImGui::SameLine();
  128. }
  129. // Depth
  130. if(grTex.getTextureType() == TextureType::k3D)
  131. {
  132. UiStringList labels;
  133. for(U32 d = 0; d < grTex.getDepth(); ++d)
  134. {
  135. labels.pushBackSprintf("Depth %u", d);
  136. }
  137. if(ImGui::BeginCombo("##Depth", (labels.getBegin() + U32(m_depth))->cstr(), ImGuiComboFlags_HeightLarge))
  138. {
  139. for(U32 d = 0; d < grTex.getDepth(); ++d)
  140. {
  141. const Bool isSelected = (m_depth == F32(d));
  142. if(ImGui::Selectable((labels.getBegin() + d)->cstr(), isSelected))
  143. {
  144. m_depth = F32(d);
  145. }
  146. if(isSelected)
  147. {
  148. ImGui::SetItemDefaultFocus();
  149. }
  150. }
  151. ImGui::EndCombo();
  152. }
  153. ImGui::SameLine();
  154. }
  155. // Max color slider
  156. ImGui::SliderFloat("##Max color", &m_maxColorValue, 0.0f, 5.0f, "Max color = %.3f");
  157. ImGui::SameLine();
  158. // Next
  159. ImGui::EndChild();
  160. ImGui::BeginChild("Image", Vec2(-1.0f, -1.0f), false, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_HorizontalScrollbar);
  161. // Image
  162. {
  163. // Center image
  164. const Vec2 imageSize = Vec2(F32(grTex.getWidth()), F32(grTex.getHeight())) * m_zoom;
  165. class ExtraPushConstants
  166. {
  167. public:
  168. Vec4 m_colorScale;
  169. Vec4 m_depth;
  170. } pc;
  171. pc.m_colorScale.x() = F32(m_colorChannel[0]) / m_maxColorValue;
  172. pc.m_colorScale.y() = F32(m_colorChannel[1]) / m_maxColorValue;
  173. pc.m_colorScale.z() = F32(m_colorChannel[2]) / m_maxColorValue;
  174. pc.m_colorScale.w() = F32(m_colorChannel[3]);
  175. pc.m_depth = Vec4((m_depth + 0.5f) / F32(grTex.getDepth()));
  176. m_imageIdExtra.m_customProgram = m_imageGrProgram;
  177. m_imageIdExtra.m_textureView = m_textureView;
  178. m_imageIdExtra.m_extraPushConstantsSize = U8(sizeof(pc));
  179. m_imageIdExtra.setExtraPushConstants(&pc, sizeof(pc));
  180. ImGui::Image(UiImageId(&m_imageIdExtra, m_pointSampling), imageSize, Vec2(0.0f, 1.0f), Vec2(1.0f, 0.0f), Vec4(1.0f),
  181. Vec4(0.0f, 0.0f, 0.0f, 1.0f));
  182. if(ImGui::IsItemHovered())
  183. {
  184. if(ImGui::GetIO().KeyCtrl)
  185. {
  186. // Zoom
  187. const F32 zoomSpeed = 0.05f;
  188. if(ImGui::GetIO().MouseWheel > 0.0f)
  189. {
  190. m_zoom *= 1.0f + zoomSpeed;
  191. }
  192. else if(ImGui::GetIO().MouseWheel < 0.0f)
  193. {
  194. m_zoom *= 1.0f - zoomSpeed;
  195. }
  196. // Pan
  197. if(ImGui::GetIO().MouseDown[0])
  198. {
  199. const Vec2 velocity = toAnki(ImGui::GetIO().MouseDelta);
  200. if(velocity.x() != 0.0f)
  201. {
  202. ImGui::SetScrollX(ImGui::GetScrollX() - velocity.x());
  203. }
  204. if(velocity.y() != 0.0f)
  205. {
  206. ImGui::SetScrollY(ImGui::GetScrollY() - velocity.y());
  207. }
  208. }
  209. }
  210. }
  211. }
  212. ImGui::EndChild();
  213. canvas->popFont();
  214. ImGui::End();
  215. }
  216. };
  217. class MyApp : public App
  218. {
  219. public:
  220. MyApp(AllocAlignedCallback allocCb, void* allocCbUserData)
  221. : App(allocCb, allocCbUserData)
  222. {
  223. }
  224. Error init(int argc, char** argv, [[maybe_unused]] CString appName)
  225. {
  226. if(argc < 2)
  227. {
  228. ANKI_LOGE("Wrong number of arguments");
  229. return Error::kUserData;
  230. }
  231. g_windowFullscreenCVar.set(0);
  232. g_dataPathsCVar.set(ANKI_SOURCE_DIRECTORY);
  233. ANKI_CHECK(CVarSet::getSingleton().setFromCommandLineArguments(argc - 2, argv + 2));
  234. ANKI_CHECK(App::init());
  235. // Load the texture
  236. ImageResourcePtr image;
  237. ANKI_CHECK(ResourceManager::getSingleton().loadResource(argv[1], image, false));
  238. // Change window name
  239. String title;
  240. title.sprintf("%s %u x %u Mips %u Format %s", argv[1], image->getWidth(), image->getHeight(), image->getTexture().getMipmapCount(),
  241. getFormatInfo(image->getTexture().getFormat()).m_name);
  242. NativeWindow::getSingleton().setWindowTitle(title);
  243. // Create the node
  244. TextureViewerUiNode* node;
  245. ANKI_CHECK(SceneGraph::getSingleton().newSceneNode("TextureViewer", node));
  246. node->m_imageResource = std::move(image);
  247. return Error::kNone;
  248. }
  249. Error userMainLoop(Bool& quit, [[maybe_unused]] Second elapsedTime) override
  250. {
  251. Input& input = Input::getSingleton();
  252. if(input.getKey(KeyCode::kEscape))
  253. {
  254. quit = true;
  255. }
  256. return Error::kNone;
  257. }
  258. };
  259. ANKI_MAIN_FUNCTION(myMain)
  260. int myMain(int argc, char* argv[])
  261. {
  262. Error err = Error::kNone;
  263. MyApp* app = new MyApp(allocAligned, nullptr);
  264. err = app->init(argc, argv, "Texture Viewer");
  265. if(!err)
  266. {
  267. err = app->mainLoop();
  268. }
  269. delete app;
  270. if(err)
  271. {
  272. ANKI_LOGE("Error reported. Bye!!");
  273. }
  274. else
  275. {
  276. ANKI_LOGI("Bye!!");
  277. }
  278. return 0;
  279. }