ImageViewerMain.cpp 8.2 KB

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