textureFeatures.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright (C) 2008-2012 Christian Stehno, Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. using namespace core;
  6. namespace
  7. {
  8. //! check miplevels by visual test
  9. bool renderMipLevels(video::E_DRIVER_TYPE driverType)
  10. {
  11. IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
  12. if (!device)
  13. return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
  14. video::IVideoDriver* driver = device->getVideoDriver();
  15. scene::ISceneManager * smgr = device->getSceneManager();
  16. if (!driver->queryFeature(video::EVDF_MIP_MAP))
  17. {
  18. device->closeDevice();
  19. device->run();
  20. device->drop();
  21. return true;
  22. }
  23. stabilizeScreenBackground(driver);
  24. logTestString("Testing driver %ls\n", driver->getName());
  25. scene::ISceneNode* n = smgr->addCubeSceneNode();
  26. scene::ISceneNode* n2 = smgr->addCubeSceneNode(10, 0, -1, core::vector3df(20,0,30), core::vector3df(0,45,0));
  27. // we use a main texture with blue on top and red below
  28. // and mipmap with pink on top and cyan below
  29. if (n && n2)
  30. {
  31. // create the texture and miplevels with distinct colors
  32. u32 texData[16*16];
  33. for (u32 i=0; i<16*16; ++i)
  34. texData[i]=(i<8*16?0xff0000ff:0xffff0000);
  35. video::IImage* image = driver->createImageFromData(video::ECF_A8R8G8B8, core::dimension2du(16,16), texData, false);
  36. u32 mipdata[8*16];
  37. u32 index=0;
  38. for (u32 j=8; j>0; j/=2)
  39. {
  40. for (u32 i=0; i<j; ++i)
  41. {
  42. u32 val=(i<j/2?0xffff00ff:0xff00ffff);
  43. for (u32 k=0; k<j; ++k)
  44. mipdata[index++]=val;
  45. }
  46. }
  47. video::ITexture* tex = driver->addTexture("miptest", image, mipdata);
  48. if (!tex)
  49. // is probably an error in the mipdata handling
  50. return false;
  51. else
  52. {
  53. n->setMaterialFlag(video::EMF_LIGHTING, false);
  54. n->setMaterialTexture(0, tex);
  55. n2->setMaterialFlag(video::EMF_LIGHTING, false);
  56. n2->setMaterialTexture(0, tex);
  57. }
  58. image->drop();
  59. }
  60. (void)smgr->addCameraSceneNode(0, core::vector3df(10,0,-30));
  61. driver->beginScene(true, true, video::SColor(255,100,101,140));
  62. smgr->drawAll();
  63. driver->endScene();
  64. bool result = takeScreenshotAndCompareAgainstReference(driver, "-renderMipmap.png");
  65. if (!result)
  66. logTestString("mipmap render failed.\n", driver->getName());
  67. else
  68. logTestString("Passed\n");
  69. device->closeDevice();
  70. device->run();
  71. device->drop();
  72. return result;
  73. }
  74. //! Tests locking miplevels
  75. bool lockAllMipLevels(video::E_DRIVER_TYPE driverType)
  76. {
  77. IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
  78. if (!device)
  79. return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
  80. video::IVideoDriver* driver = device->getVideoDriver();
  81. scene::ISceneManager * smgr = device->getSceneManager();
  82. if (!driver->queryFeature(video::EVDF_MIP_MAP))
  83. {
  84. device->closeDevice();
  85. device->run();
  86. device->drop();
  87. return true;
  88. }
  89. stabilizeScreenBackground(driver);
  90. logTestString("Testing driver %ls\n", driver->getName());
  91. scene::ISceneNode* n = smgr->addCubeSceneNode();
  92. if (n)
  93. {
  94. // create the texture and miplevels with distinct colors
  95. u32 texData[16*16];
  96. for (u32 i=0; i<16*16; ++i)
  97. texData[i]=0xff0000ff-i;
  98. video::IImage* image = driver->createImageFromData(video::ECF_A8R8G8B8, core::dimension2du(16,16), texData, false);
  99. u32 mipdata[8*16];
  100. u32 index=0;
  101. for (u32 j=8; j>0; j/=2)
  102. {
  103. u32 val=(j==8?0x00ff00ff:(j==4?0x0000ffff:(j==2?0xc2c200ff:0x001212ff)));
  104. for (u32 i=0; i<j; ++i)
  105. {
  106. for (u32 k=0; k<j; ++k)
  107. mipdata[index++]=val-i;
  108. }
  109. }
  110. video::ITexture* tex = driver->addTexture("miptest", image, mipdata);
  111. if (!tex)
  112. // is probably an error in the mipdata handling
  113. return false;
  114. else
  115. n->setMaterialTexture(0, tex);
  116. image->drop();
  117. }
  118. (void)smgr->addCameraSceneNode();
  119. driver->beginScene(true, true, video::SColor(255,100,101,140));
  120. smgr->drawAll();
  121. driver->endScene();
  122. video::ITexture* tex = driver->findTexture("miptest");
  123. video::SColor* bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 0);
  124. bool result = (bits[0].color==0xff0000ff);
  125. tex->unlock();
  126. bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 1);
  127. result &= (bits[0].color==0x00ff00ff);
  128. tex->unlock();
  129. bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 2);
  130. result &= (bits[0].color==0x0000ffff);
  131. tex->unlock();
  132. bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 3);
  133. result &= (bits[0].color==0xc2c200ff);
  134. tex->unlock();
  135. bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 4);
  136. result &= (bits[0].color==0x001212ff);
  137. tex->unlock();
  138. if (!result)
  139. logTestString("mipmap lock after init with driver %ls failed.\n", driver->getName());
  140. // test with updating a lower level, and reading upper and lower
  141. bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 3);
  142. bits[0]=0xff00ff00;
  143. bits[1]=0xff00ff00;
  144. tex->unlock();
  145. bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 4);
  146. result &= (bits[0].color==0x001212ff);
  147. tex->unlock();
  148. bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 3);
  149. result &= ((bits[0].color==0xff00ff00)&&(bits[2].color==0xc2c200fe));
  150. tex->unlock();
  151. if (!result)
  152. logTestString("mipmap lock after mipmap write with driver %ls failed.\n", driver->getName());
  153. // now test locking level 0
  154. bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 0);
  155. bits[0]=0xff00ff00;
  156. bits[1]=0xff00ff00;
  157. tex->unlock();
  158. bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 4);
  159. result &= (bits[0].color==0x001212ff);
  160. tex->unlock();
  161. bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 0);
  162. result &= ((bits[0].color==0xff00ff00)&&(bits[2].color==0xff0000fd));
  163. tex->unlock();
  164. if (!result)
  165. logTestString("mipmap lock at level 0 after mipmap write with driver %ls failed.\n", driver->getName());
  166. else
  167. logTestString("Passed\n");
  168. device->closeDevice();
  169. device->run();
  170. device->drop();
  171. return result;
  172. }
  173. //! Tests locking miplevels after texture was created with auto mipmap update
  174. bool lockWithAutoMipmap(video::E_DRIVER_TYPE driverType)
  175. {
  176. IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
  177. if (!device)
  178. return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
  179. video::IVideoDriver* driver = device->getVideoDriver();
  180. scene::ISceneManager * smgr = device->getSceneManager();
  181. if (!driver->queryFeature(video::EVDF_MIP_MAP))
  182. {
  183. device->closeDevice();
  184. device->run();
  185. device->drop();
  186. return true;
  187. }
  188. stabilizeScreenBackground(driver);
  189. logTestString("Testing driver %ls\n", driver->getName());
  190. scene::ISceneNode* n = smgr->addCubeSceneNode();
  191. if (n)
  192. {
  193. // create the texture
  194. u32 texData[16*16];
  195. for (u32 i=0; i<16*16; ++i)
  196. texData[i]=0xff0000ff-i;
  197. video::IImage* image = driver->createImageFromData(video::ECF_A8R8G8B8, core::dimension2du(16,16), texData, false);
  198. video::ITexture* tex = driver->addTexture("miptest", image);
  199. if (!tex)
  200. return false;
  201. else
  202. n->setMaterialTexture(0, tex);
  203. image->drop();
  204. }
  205. (void)smgr->addCameraSceneNode();
  206. driver->beginScene(true, true, video::SColor(255,100,101,140));
  207. smgr->drawAll();
  208. driver->endScene();
  209. video::ITexture* tex = driver->findTexture("miptest");
  210. video::SColor* bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 0);
  211. bool result = (bits[0].color==0xff0000ff);
  212. tex->unlock();
  213. if (!result)
  214. logTestString("mipmap lock after init with driver %ls failed.\n", driver->getName());
  215. // test with updating a lower level, and reading upper and lower
  216. bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 3);
  217. bits[0]=0xff00ff00;
  218. bits[1]=0xff00ff00;
  219. tex->unlock();
  220. // lock another texture just to invalidate caches in the driver
  221. bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 4);
  222. tex->unlock();
  223. bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 3);
  224. result &= ((bits[0].color==0xff00ff00)&&(bits[2].color!=0xff00ff00));
  225. tex->unlock();
  226. if (!result)
  227. logTestString("mipmap lock after mipmap write with driver %ls failed.\n", driver->getName());
  228. // now test locking level 0
  229. bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 0);
  230. bits[0]=0x00ff00ff;
  231. bits[1]=0x00ff00ff;
  232. tex->unlock();
  233. bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 3);
  234. result &= ((bits[0].color==0xff00ff00)&&(bits[2].color!=0xff00ff00));
  235. tex->unlock();
  236. if (!result)
  237. logTestString("mipmap lock at level 0 after mipmap write with driver %ls failed.\n", driver->getName());
  238. else
  239. logTestString("Passed\n");
  240. device->closeDevice();
  241. device->run();
  242. device->drop();
  243. return result;
  244. }
  245. }
  246. bool textureFeatures(void)
  247. {
  248. bool result = true;
  249. TestWithAllDrivers(renderMipLevels);
  250. TestWithAllDrivers(lockAllMipLevels);
  251. TestWithAllDrivers(lockWithAutoMipmap);
  252. return result;
  253. }