Texture.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. #include "Base.h"
  2. #include "Image.h"
  3. #include "Texture.h"
  4. #include "FileSystem.h"
  5. // PVRTC (GL_IMG_texture_compression_pvrtc) : Imagination based gpus
  6. #ifndef GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG
  7. #define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  8. #endif
  9. #ifndef GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
  10. #define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  11. #endif
  12. #ifndef GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG
  13. #define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  14. #endif
  15. #ifndef GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
  16. #define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  17. #endif
  18. // S3TC/DXT (GL_EXT_texture_compression_s3tc) : Most desktop/console gpus
  19. #ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
  20. #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  21. #endif
  22. #ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
  23. #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  24. #endif
  25. #ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
  26. #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  27. #endif
  28. // ATC (GL_AMD_compressed_ATC_texture) : Qualcomm/Adreno based gpus
  29. #ifndef ATC_RGB_AMD
  30. #define ATC_RGB_AMD 0x8C92
  31. #endif
  32. #ifndef ATC_RGBA_EXPLICIT_ALPHA_AMD
  33. #define ATC_RGBA_EXPLICIT_ALPHA_AMD 0x8C93
  34. #endif
  35. #ifndef ATC_RGBA_INTERPOLATED_ALPHA_AMD
  36. #define ATC_RGBA_INTERPOLATED_ALPHA_AMD 0x87EE
  37. #endif
  38. namespace gameplay
  39. {
  40. static std::vector<Texture*> __textureCache;
  41. Texture::Texture() : _handle(0), _format(RGBA), _width(0), _height(0), _mipmapped(false), _cached(false), _compressed(false)
  42. {
  43. }
  44. Texture::Texture(const Texture& copy)
  45. {
  46. }
  47. Texture::~Texture()
  48. {
  49. if (_handle)
  50. {
  51. GL_ASSERT( glDeleteTextures(1, &_handle) );
  52. _handle = 0;
  53. }
  54. // Remove ourself from the texture cache.
  55. if (_cached)
  56. {
  57. std::vector<Texture*>::iterator itr = std::find(__textureCache.begin(), __textureCache.end(), this);
  58. if (itr != __textureCache.end())
  59. {
  60. __textureCache.erase(itr);
  61. }
  62. }
  63. }
  64. Texture* Texture::create(const char* path, bool generateMipmaps)
  65. {
  66. GP_ASSERT(path);
  67. // Search texture cache first.
  68. for (unsigned int i = 0, count = __textureCache.size(); i < count; ++i)
  69. {
  70. Texture* t = __textureCache[i];
  71. GP_ASSERT(t);
  72. if (t->_path == path)
  73. {
  74. // If 'generateMipmaps' is true, call Texture::generateMipamps() to force the
  75. // texture to generate its mipmap chain if it hasn't already done so.
  76. if (generateMipmaps)
  77. {
  78. t->generateMipmaps();
  79. }
  80. // Found a match.
  81. t->addRef();
  82. return t;
  83. }
  84. }
  85. Texture* texture = NULL;
  86. // Filter loading based on file extension.
  87. const char* ext = strrchr(FileSystem::resolvePath(path), '.');
  88. if (ext)
  89. {
  90. switch (strlen(ext))
  91. {
  92. case 4:
  93. if (tolower(ext[1]) == 'p' && tolower(ext[2]) == 'n' && tolower(ext[3]) == 'g')
  94. {
  95. Image* image = Image::create(path);
  96. if (image)
  97. texture = create(image, generateMipmaps);
  98. SAFE_RELEASE(image);
  99. }
  100. else if (tolower(ext[1]) == 'p' && tolower(ext[2]) == 'v' && tolower(ext[3]) == 'r')
  101. {
  102. // PowerVR Compressed Texture RGBA.
  103. texture = createCompressedPVRTC(path);
  104. }
  105. else if (tolower(ext[1]) == 'd' && tolower(ext[2]) == 'd' && tolower(ext[3]) == 's')
  106. {
  107. // DDS file format (DXT/S3TC) compressed textures
  108. texture = createCompressedDDS(path);
  109. }
  110. break;
  111. }
  112. }
  113. if (texture)
  114. {
  115. texture->_path = path;
  116. texture->_cached = true;
  117. // Add to texture cache.
  118. __textureCache.push_back(texture);
  119. return texture;
  120. }
  121. GP_ERROR("Failed to load texture from file '%s'.", path);
  122. return NULL;
  123. }
  124. Texture* Texture::create(Image* image, bool generateMipmaps)
  125. {
  126. GP_ASSERT(image);
  127. switch (image->getFormat())
  128. {
  129. case Image::RGB:
  130. return create(Texture::RGB, image->getWidth(), image->getHeight(), image->getData(), generateMipmaps);
  131. case Image::RGBA:
  132. return create(Texture::RGBA, image->getWidth(), image->getHeight(), image->getData(), generateMipmaps);
  133. default:
  134. GP_ERROR("Unsupported image format (%d).", image->getFormat());
  135. return NULL;
  136. }
  137. }
  138. Texture* Texture::create(Format format, unsigned int width, unsigned int height, unsigned char* data, bool generateMipmaps)
  139. {
  140. // Create and load the texture.
  141. GLuint textureId;
  142. GL_ASSERT( glGenTextures(1, &textureId) );
  143. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, textureId) );
  144. GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, width, height, 0, (GLenum)format, GL_UNSIGNED_BYTE, data) );
  145. // Set initial minification filter based on whether or not mipmaping was enabled.
  146. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, generateMipmaps ? GL_NEAREST_MIPMAP_LINEAR : GL_LINEAR) );
  147. Texture* texture = new Texture();
  148. texture->_handle = textureId;
  149. texture->_format = format;
  150. texture->_width = width;
  151. texture->_height = height;
  152. if (generateMipmaps)
  153. {
  154. texture->generateMipmaps();
  155. texture->_mipmapped = true;
  156. }
  157. return texture;
  158. }
  159. // Computes the size of a PVRTC data chunk for a mipmap level of the given size.
  160. unsigned int computePVRTCDataSize(int width, int height, int bpp)
  161. {
  162. int blockSize;
  163. int widthBlocks;
  164. int heightBlocks;
  165. if (bpp == 4)
  166. {
  167. blockSize = 4 * 4; // Pixel by pixel block size for 4bpp
  168. widthBlocks = std::max(width >> 2, 2);
  169. heightBlocks = std::max(height >> 2, 2);
  170. }
  171. else
  172. {
  173. blockSize = 8 * 4; // Pixel by pixel block size for 2bpp
  174. widthBlocks = std::max(width >> 3, 2);
  175. heightBlocks = std::max(height >> 2, 2);
  176. }
  177. return widthBlocks * heightBlocks * ((blockSize * bpp) >> 3);
  178. }
  179. Texture* Texture::createCompressedPVRTC(const char* path)
  180. {
  181. FILE* file = FileSystem::openFile(path, "rb");
  182. if (file == NULL)
  183. {
  184. GP_ERROR("Failed to load file '%s'.", path);
  185. return NULL;
  186. }
  187. // Read first 4 bytes to determine PVRTC format.
  188. unsigned int read;
  189. unsigned int version;
  190. read = fread(&version, sizeof(unsigned int), 1, file);
  191. if (read != 1)
  192. {
  193. GP_ERROR("Failed to read PVR version.");
  194. if (fclose(file) != 0)
  195. {
  196. GP_ERROR("Failed to close PVR file '%s'.", path);
  197. return NULL;
  198. }
  199. return NULL;
  200. }
  201. // Rewind to start of header.
  202. if (fseek(file, 0, SEEK_SET) != 0)
  203. {
  204. GP_ERROR("Failed to seek backwards to beginning of file after reading PVR version.");
  205. if (fclose(file) != 0)
  206. {
  207. GP_ERROR("Failed to close PVR file '%s'.", path);
  208. return NULL;
  209. }
  210. return NULL;
  211. }
  212. // Read texture data.
  213. GLsizei width, height;
  214. GLenum format;
  215. GLubyte* data = NULL;
  216. unsigned int mipMapCount;
  217. if (version == 0x03525650)
  218. {
  219. // Modern PVR file format.
  220. data = readCompressedPVRTC(path, file, &width, &height, &format, &mipMapCount);
  221. }
  222. else
  223. {
  224. // Legacy PVR file format.
  225. data = readCompressedPVRTCLegacy(path, file, &width, &height, &format, &mipMapCount);
  226. }
  227. if (data == NULL)
  228. {
  229. GP_ERROR("Failed to read texture data from PVR file '%s'.", path);
  230. if (fclose(file) != 0)
  231. {
  232. GP_ERROR("Failed to close PVR file '%s'.", path);
  233. return NULL;
  234. }
  235. return NULL;
  236. }
  237. if (fclose(file) != 0)
  238. {
  239. GP_ERROR("Failed to close PVR file '%s'.", path);
  240. return NULL;
  241. }
  242. int bpp = (format == GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format == GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG) ? 2 : 4;
  243. // Generate our texture.
  244. GLuint textureId;
  245. GL_ASSERT( glGenTextures(1, &textureId) );
  246. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, textureId) );
  247. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipMapCount > 1 ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR) );
  248. Texture* texture = new Texture();
  249. texture->_handle = textureId;
  250. texture->_width = width;
  251. texture->_height = height;
  252. texture->_mipmapped = mipMapCount > 1;
  253. texture->_compressed = true;
  254. // Load the data for each level.
  255. GLubyte* ptr = data;
  256. for (unsigned int level = 0; level < mipMapCount; ++level)
  257. {
  258. unsigned int dataSize = computePVRTCDataSize(width, height, bpp);
  259. // Upload data to GL.
  260. GL_ASSERT( glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height, 0, dataSize, ptr) );
  261. width = std::max(width >> 1, 1);
  262. height = std::max(height >> 1, 1);
  263. ptr += dataSize;
  264. }
  265. // Free data.
  266. SAFE_DELETE_ARRAY(data);
  267. return texture;
  268. }
  269. GLubyte* Texture::readCompressedPVRTC(const char* path, FILE* file, GLsizei* width, GLsizei* height, GLenum* format, unsigned int* mipMapCount)
  270. {
  271. GP_ASSERT(file);
  272. GP_ASSERT(path);
  273. GP_ASSERT(width);
  274. GP_ASSERT(height);
  275. GP_ASSERT(format);
  276. GP_ASSERT(mipMapCount);
  277. struct pvrtc_file_header
  278. {
  279. unsigned int version;
  280. unsigned int flags;
  281. unsigned int pixelFormat[2];
  282. unsigned int colorSpace;
  283. unsigned int channelType;
  284. unsigned int height;
  285. unsigned int width;
  286. unsigned int depth;
  287. unsigned int surfaceCount;
  288. unsigned int faceCount;
  289. unsigned int mipMapCount;
  290. unsigned int metaDataSize;
  291. };
  292. unsigned int read;
  293. // Read header data.
  294. pvrtc_file_header header;
  295. read = fread(&header, sizeof(pvrtc_file_header), 1, file);
  296. if (read != 1)
  297. {
  298. GP_ERROR("Failed to read PVR header data for file '%s'.", path);
  299. return NULL;
  300. }
  301. if (header.pixelFormat[1] != 0)
  302. {
  303. // Unsupported pixel format.
  304. GP_ERROR("Unsupported pixel format in PVR file '%s'. (MSB == %d != 0)", path, header.pixelFormat[1]);
  305. return NULL;
  306. }
  307. int bpp;
  308. switch (header.pixelFormat[0])
  309. {
  310. case 0:
  311. *format = GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  312. bpp = 2;
  313. break;
  314. case 1:
  315. *format = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  316. bpp = 2;
  317. break;
  318. case 2:
  319. *format = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  320. bpp = 4;
  321. break;
  322. case 3:
  323. *format = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  324. bpp = 4;
  325. break;
  326. default:
  327. // Unsupported format.
  328. GP_ERROR("Unsupported pixel format value (%d) in PVR file '%s'.", header.pixelFormat[0], path);
  329. return NULL;
  330. }
  331. *width = (GLsizei)header.width;
  332. *height = (GLsizei)header.height;
  333. *mipMapCount = header.mipMapCount;
  334. // Skip meta-data.
  335. if (fseek(file, header.metaDataSize, SEEK_CUR) != 0)
  336. {
  337. GP_ERROR("Failed to seek past header meta data in PVR file '%s'.", path);
  338. return NULL;
  339. }
  340. // Compute total size of data to be read.
  341. int w = *width;
  342. int h = *height;
  343. unsigned int dataSize = 0;
  344. for (unsigned int level = 0; level < header.mipMapCount; ++level)
  345. {
  346. dataSize += computePVRTCDataSize(w, h, bpp);
  347. w = std::max(w>>1, 1);
  348. h = std::max(h>>1, 1);
  349. }
  350. // Read data.
  351. GLubyte* data = new GLubyte[dataSize];
  352. read = fread(data, 1, dataSize, file);
  353. if (read != dataSize)
  354. {
  355. GP_ERROR("Failed to read texture data from PVR file '%s'.", path);
  356. return NULL;
  357. }
  358. return data;
  359. }
  360. GLubyte* Texture::readCompressedPVRTCLegacy(const char* path, FILE* file, GLsizei* width, GLsizei* height, GLenum* format, unsigned int* mipMapCount)
  361. {
  362. char PVRTCIdentifier[] = "PVR!";
  363. struct pvrtc_file_header_legacy
  364. {
  365. unsigned int size; // size of the structure
  366. unsigned int height; // height of surface to be created
  367. unsigned int width; // width of input surface
  368. unsigned int mipmapCount; // number of mip-map levels requested
  369. unsigned int formatflags; // pixel format flags
  370. unsigned int dataSize; // total size in bytes
  371. unsigned int bpp; // number of bits per pixel
  372. unsigned int redBitMask; // mask for red bit
  373. unsigned int greenBitMask; // mask for green bits
  374. unsigned int blueBitMask; // mask for blue bits
  375. unsigned int alphaBitMask; // mask for alpha channel
  376. unsigned int pvrtcTag; // magic number identifying pvrtc file
  377. unsigned int surfaceCount; // number of surfaces present in the pvrtc
  378. };
  379. // Read the file header.
  380. unsigned int size = sizeof(pvrtc_file_header_legacy);
  381. pvrtc_file_header_legacy header;
  382. unsigned int read = (int)fread(&header, 1, size, file);
  383. if (read != size)
  384. {
  385. GP_ERROR("Failed to read file header for pvrtc file '%s'.", path);
  386. if (fclose(file) != 0)
  387. {
  388. GP_ERROR("Failed to close file '%s'.", path);
  389. }
  390. return NULL;
  391. }
  392. // Proper file header identifier.
  393. if (PVRTCIdentifier[0] != (char)((header.pvrtcTag >> 0) & 0xff) ||
  394. PVRTCIdentifier[1] != (char)((header.pvrtcTag >> 8) & 0xff) ||
  395. PVRTCIdentifier[2] != (char)((header.pvrtcTag >> 16) & 0xff) ||
  396. PVRTCIdentifier[3] != (char)((header.pvrtcTag >> 24) & 0xff))
  397. {
  398. GP_ERROR("Failed to load pvrtc file '%s': invalid header.", path);
  399. if (fclose(file) != 0)
  400. {
  401. GP_ERROR("Failed to close file '%s'.", path);
  402. }
  403. return NULL;
  404. }
  405. // Format flags for GLenum format.
  406. if (header.bpp == 4)
  407. {
  408. *format = header.alphaBitMask ? GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG : GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  409. }
  410. else if (header.bpp == 2)
  411. {
  412. *format = header.alphaBitMask ? GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG : GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  413. }
  414. else
  415. {
  416. GP_ERROR("Failed to load pvrtc file '%s': invalid pvrtc compressed texture format flags.", path);
  417. if (fclose(file) != 0)
  418. {
  419. GP_ERROR("Failed to close file '%s'.", path);
  420. }
  421. return NULL;
  422. }
  423. *width = (GLsizei)header.width;
  424. *height = (GLsizei)header.height;
  425. *mipMapCount = header.mipmapCount + 1; // +1 because mipmapCount does not include the base level
  426. GLubyte* data = new GLubyte[header.dataSize];
  427. read = (int)fread(data, 1, header.dataSize, file);
  428. if (read != header.dataSize)
  429. {
  430. GP_ERROR("Failed to load texture data for pvrtc file '%s'.", path);
  431. if (fclose(file) != 0)
  432. {
  433. GP_ERROR("Failed to close file '%s'.", path);
  434. }
  435. SAFE_DELETE_ARRAY(data);
  436. return NULL;
  437. }
  438. return data;
  439. }
  440. Texture* Texture::createCompressedDDS(const char* path)
  441. {
  442. GP_ASSERT(path);
  443. // DDS file structures.
  444. struct dds_pixel_format
  445. {
  446. unsigned int dwSize;
  447. unsigned int dwFlags;
  448. unsigned int dwFourCC;
  449. unsigned int dwRGBBitCount;
  450. unsigned int dwRBitMask;
  451. unsigned int dwGBitMask;
  452. unsigned int dwBBitMask;
  453. unsigned int dwABitMask;
  454. };
  455. struct dds_header
  456. {
  457. unsigned int dwSize;
  458. unsigned int dwFlags;
  459. unsigned int dwHeight;
  460. unsigned int dwWidth;
  461. unsigned int dwPitchOrLinearSize;
  462. unsigned int dwDepth;
  463. unsigned int dwMipMapCount;
  464. unsigned int dwReserved1[11];
  465. dds_pixel_format ddspf;
  466. unsigned int dwCaps;
  467. unsigned int dwCaps2;
  468. unsigned int dwCaps3;
  469. unsigned int dwCaps4;
  470. unsigned int dwReserved2;
  471. };
  472. struct dds_mip_level
  473. {
  474. GLubyte* data;
  475. GLsizei width;
  476. GLsizei height;
  477. GLsizei size;
  478. };
  479. Texture* texture = NULL;
  480. // Read DDS file.
  481. FILE* fp = FileSystem::openFile(path, "rb");
  482. if (fp == NULL)
  483. {
  484. GP_ERROR("Failed to open file '%s'.", path);
  485. return NULL;
  486. }
  487. // Validate DDS magic number.
  488. char code[4];
  489. if (fread(code, 1, 4, fp) != 4 || strncmp(code, "DDS ", 4) != 0)
  490. {
  491. GP_ERROR("Failed to read DDS file '%s': invalid DDS magic number.", path);
  492. if (fclose(fp) != 0)
  493. {
  494. GP_ERROR("Failed to close file '%s'.", path);
  495. }
  496. return NULL;
  497. }
  498. // Read DDS header.
  499. dds_header header;
  500. if (fread(&header, sizeof(dds_header), 1, fp) != 1)
  501. {
  502. GP_ERROR("Failed to read header for DDS file '%s'.", path);
  503. if (fclose(fp) != 0)
  504. {
  505. GP_ERROR("Failed to close file '%s'.", path);
  506. }
  507. return NULL;
  508. }
  509. if ((header.dwFlags & 0x20000/*DDSD_MIPMAPCOUNT*/) == 0)
  510. {
  511. // Mipmap count not specified (non-mipmapped texture).
  512. header.dwMipMapCount = 1;
  513. }
  514. // Allocate mip level structures.
  515. dds_mip_level* mipLevels = new dds_mip_level[header.dwMipMapCount];
  516. memset(mipLevels, 0, sizeof(dds_mip_level) * header.dwMipMapCount);
  517. GLenum format, internalFormat;
  518. bool compressed = false;
  519. GLsizei width = header.dwWidth;
  520. GLsizei height = header.dwHeight;
  521. int bytesPerBlock;
  522. if (header.ddspf.dwFlags & 0x4/*DDPF_FOURCC*/)
  523. {
  524. compressed = true;
  525. // Compressed.
  526. switch (header.ddspf.dwFourCC)
  527. {
  528. case ('D'|('X'<<8)|('T'<<16)|('1'<<24)):
  529. format = internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  530. bytesPerBlock = 8;
  531. break;
  532. case ('D'|('X'<<8)|('T'<<16)|('3'<<24)):
  533. format = internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  534. bytesPerBlock = 16;
  535. break;
  536. case ('D'|('X'<<8)|('T'<<16)|('5'<<24)):
  537. format = internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  538. bytesPerBlock = 16;
  539. break;
  540. case ('A'|('T'<<8)|('C'<<16)|(' '<<24)):
  541. format = internalFormat = ATC_RGB_AMD;
  542. bytesPerBlock = 8;
  543. break;
  544. case ('A'|('T'<<8)|('C'<<16)|('A'<<24)):
  545. format = internalFormat = ATC_RGBA_EXPLICIT_ALPHA_AMD;
  546. bytesPerBlock = 16;
  547. break;
  548. case ('A'|('T'<<8)|('C'<<16)|('I'<<24)):
  549. format = internalFormat = ATC_RGBA_INTERPOLATED_ALPHA_AMD;
  550. bytesPerBlock = 16;
  551. break;
  552. default:
  553. GP_ERROR("Unsupported compressed texture format (%d) for DDS file '%s'.", header.ddspf.dwFourCC, path);
  554. if (fclose(fp) != 0)
  555. {
  556. GP_ERROR("Failed to close file '%s'.", path);
  557. }
  558. SAFE_DELETE_ARRAY(mipLevels);
  559. return NULL;
  560. }
  561. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  562. {
  563. mipLevels[i].width = width;
  564. mipLevels[i].height = height;
  565. mipLevels[i].size = std::max(1, (width+3) >> 2) * std::max(1, (height+3) >> 2) * bytesPerBlock;
  566. mipLevels[i].data = new GLubyte[mipLevels[i].size];
  567. if (fread(mipLevels[i].data, 1, mipLevels[i].size, fp) != (unsigned int)mipLevels[i].size)
  568. {
  569. GP_ERROR("Failed to load dds compressed texture bytes for texture: %s", path);
  570. // Cleanup mip data.
  571. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  572. SAFE_DELETE_ARRAY(mipLevels[i].data);
  573. SAFE_DELETE_ARRAY(mipLevels);
  574. if (fclose(fp) != 0)
  575. {
  576. GP_ERROR("Failed to close file '%s'.", path);
  577. }
  578. return texture;
  579. }
  580. width = std::max(1, width >> 1);
  581. height = std::max(1, height >> 1);
  582. }
  583. }
  584. else if (header.ddspf.dwFlags == 0x40/*DDPF_RGB*/)
  585. {
  586. // RGB (uncompressed)
  587. // Note: Use GL_BGR as internal format to flip bytes.
  588. GP_ERROR("Failed to create texture from DDS file '%s': uncompressed RGB format is not supported.", path);
  589. if (fclose(fp) != 0)
  590. {
  591. GP_ERROR("Failed to close file '%s'.", path);
  592. }
  593. SAFE_DELETE_ARRAY(mipLevels);
  594. return NULL;
  595. }
  596. else if (header.ddspf.dwFlags == 0x41/*DDPF_RGB|DDPF_ALPHAPIXELS*/)
  597. {
  598. // RGBA (uncompressed)
  599. // Note: Use GL_BGRA as internal format to flip bytes.
  600. GP_ERROR("Failed to create texture from DDS file '%s': uncompressed RGBA format is not supported.", path);
  601. if (fclose(fp) != 0)
  602. {
  603. GP_ERROR("Failed to close file '%s'.", path);
  604. }
  605. SAFE_DELETE_ARRAY(mipLevels);
  606. return NULL;
  607. }
  608. else
  609. {
  610. // Unsupported.
  611. GP_ERROR("Failed to create texture from DDS file '%s': unsupported flags (%d).", path, header.ddspf.dwFlags);
  612. if (fclose(fp) != 0)
  613. {
  614. GP_ERROR("Failed to close file '%s'.", path);
  615. }
  616. SAFE_DELETE_ARRAY(mipLevels);
  617. return NULL;
  618. }
  619. // Close file.
  620. if (fclose(fp) != 0)
  621. {
  622. GP_ERROR("Failed to close file '%s'.", path);
  623. }
  624. // Generate GL texture.
  625. GLuint textureId;
  626. GL_ASSERT( glGenTextures(1, &textureId) );
  627. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, textureId) );
  628. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, header.dwMipMapCount > 1 ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR ) );
  629. // Create gameplay texture.
  630. texture = new Texture();
  631. texture->_handle = textureId;
  632. texture->_width = header.dwWidth;
  633. texture->_height = header.dwHeight;
  634. texture->_compressed = compressed;
  635. texture->_mipmapped = header.dwMipMapCount > 1;
  636. // Load texture data.
  637. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  638. {
  639. if (compressed)
  640. {
  641. GL_ASSERT( glCompressedTexImage2D(GL_TEXTURE_2D, i, format, mipLevels[i].width, mipLevels[i].height, 0, mipLevels[i].size, mipLevels[i].data) );
  642. }
  643. else
  644. {
  645. GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, i, internalFormat, mipLevels[i].width, mipLevels[i].height, 0, format, GL_UNSIGNED_INT, mipLevels[i].data) );
  646. }
  647. // Clean up the texture data.
  648. SAFE_DELETE_ARRAY(mipLevels[i].data);
  649. }
  650. // Clean up mip levels structure.
  651. SAFE_DELETE_ARRAY(mipLevels);
  652. return texture;
  653. }
  654. unsigned int Texture::getWidth() const
  655. {
  656. return _width;
  657. }
  658. unsigned int Texture::getHeight() const
  659. {
  660. return _height;
  661. }
  662. TextureHandle Texture::getHandle() const
  663. {
  664. return _handle;
  665. }
  666. void Texture::setWrapMode(Wrap wrapS, Wrap wrapT)
  667. {
  668. GLint currentTextureId;
  669. GL_ASSERT( glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId) );
  670. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _handle) );
  671. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)wrapS) );
  672. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)wrapT) );
  673. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, (GLuint)currentTextureId) );
  674. }
  675. void Texture::setFilterMode(Filter minificationFilter, Filter magnificationFilter)
  676. {
  677. GLint currentTextureId;
  678. GL_ASSERT( glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId) );
  679. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _handle) );
  680. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)minificationFilter) );
  681. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)magnificationFilter) );
  682. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, (GLuint)currentTextureId) );
  683. }
  684. void Texture::generateMipmaps()
  685. {
  686. if (!_mipmapped)
  687. {
  688. GLint currentTextureId;
  689. GL_ASSERT( glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentTextureId) );
  690. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _handle) );
  691. GL_ASSERT( glGenerateMipmap(GL_TEXTURE_2D) );
  692. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, (GLuint)currentTextureId) );
  693. _mipmapped = true;
  694. }
  695. }
  696. bool Texture::isMipmapped() const
  697. {
  698. return _mipmapped;
  699. }
  700. bool Texture::isCompressed() const
  701. {
  702. return _compressed;
  703. }
  704. Texture::Sampler::Sampler(Texture* texture)
  705. : _texture(texture), _wrapS(Texture::REPEAT), _wrapT(Texture::REPEAT), _magFilter(Texture::LINEAR)
  706. {
  707. GP_ASSERT(texture);
  708. _minFilter = texture->isMipmapped() ? Texture::NEAREST_MIPMAP_LINEAR : Texture::LINEAR;
  709. }
  710. Texture::Sampler::~Sampler()
  711. {
  712. SAFE_RELEASE(_texture);
  713. }
  714. Texture::Sampler* Texture::Sampler::create(Texture* texture)
  715. {
  716. GP_ASSERT(texture);
  717. texture->addRef();
  718. return new Sampler(texture);
  719. }
  720. Texture::Sampler* Texture::Sampler::create(const char* path, bool generateMipmaps)
  721. {
  722. Texture* texture = Texture::create(path, generateMipmaps);
  723. return texture ? new Sampler(texture) : NULL;
  724. }
  725. void Texture::Sampler::setWrapMode(Wrap wrapS, Wrap wrapT)
  726. {
  727. _wrapS = wrapS;
  728. _wrapT = wrapT;
  729. }
  730. void Texture::Sampler::setFilterMode(Filter minificationFilter, Filter magnificationFilter)
  731. {
  732. _minFilter = minificationFilter;
  733. _magFilter = magnificationFilter;
  734. }
  735. Texture* Texture::Sampler::getTexture() const
  736. {
  737. return _texture;
  738. }
  739. void Texture::Sampler::bind()
  740. {
  741. GP_ASSERT(_texture);
  742. GL_ASSERT( glBindTexture(GL_TEXTURE_2D, _texture->_handle) );
  743. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (GLenum)_wrapS) );
  744. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (GLenum)_wrapT) );
  745. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLenum)_minFilter) );
  746. GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)_magFilter) );
  747. }
  748. }