Texture.cpp 26 KB

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