ImageViewerMain.cpp 8.6 KB

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