ImageViewerMain.cpp 8.5 KB

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