Texture.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. // ETC1 (OES_compressed_ETC1_RGB8_texture) : All OpenGL ES chipsets
  39. #ifndef ETC1_RGB8
  40. #define ETC1_RGB8 0x8D64
  41. #endif
  42. namespace gameplay
  43. {
  44. static std::vector<Texture*> __textureCache;
  45. static TextureHandle __currentTextureId = 0;
  46. static Texture::Type __currentTextureType = Texture::TEXTURE_2D;
  47. Texture::Texture() : _handle(0), _format(UNKNOWN), _type((Texture::Type)0), _width(0), _height(0), _mipmapped(false), _cached(false), _compressed(false),
  48. _wrapS(Texture::REPEAT), _wrapT(Texture::REPEAT), _wrapR(Texture::REPEAT), _minFilter(Texture::NEAREST_MIPMAP_LINEAR), _magFilter(Texture::LINEAR)
  49. {
  50. }
  51. Texture::~Texture()
  52. {
  53. if (_handle)
  54. {
  55. GL_ASSERT( glDeleteTextures(1, &_handle) );
  56. _handle = 0;
  57. }
  58. // Remove ourself from the texture cache.
  59. if (_cached)
  60. {
  61. std::vector<Texture*>::iterator itr = std::find(__textureCache.begin(), __textureCache.end(), this);
  62. if (itr != __textureCache.end())
  63. {
  64. __textureCache.erase(itr);
  65. }
  66. }
  67. }
  68. Texture* Texture::create(const char* path, bool generateMipmaps)
  69. {
  70. GP_ASSERT( path );
  71. // Search texture cache first.
  72. for (size_t i = 0, count = __textureCache.size(); i < count; ++i)
  73. {
  74. Texture* t = __textureCache[i];
  75. GP_ASSERT( t );
  76. if (t->_path == path)
  77. {
  78. // If 'generateMipmaps' is true, call Texture::generateMipamps() to force the
  79. // texture to generate its mipmap chain if it hasn't already done so.
  80. if (generateMipmaps)
  81. {
  82. t->generateMipmaps();
  83. }
  84. // Found a match.
  85. t->addRef();
  86. return t;
  87. }
  88. }
  89. Texture* texture = NULL;
  90. // Filter loading based on file extension.
  91. const char* ext = strrchr(FileSystem::resolvePath(path), '.');
  92. if (ext)
  93. {
  94. switch (strlen(ext))
  95. {
  96. case 4:
  97. if (tolower(ext[1]) == 'p' && tolower(ext[2]) == 'n' && tolower(ext[3]) == 'g')
  98. {
  99. Image* image = Image::create(path);
  100. if (image)
  101. texture = create(image, generateMipmaps);
  102. SAFE_RELEASE(image);
  103. }
  104. else if (tolower(ext[1]) == 'p' && tolower(ext[2]) == 'v' && tolower(ext[3]) == 'r')
  105. {
  106. // PowerVR Compressed Texture RGBA.
  107. texture = createCompressedPVRTC(path);
  108. }
  109. else if (tolower(ext[1]) == 'd' && tolower(ext[2]) == 'd' && tolower(ext[3]) == 's')
  110. {
  111. // DDS file format (DXT/S3TC) compressed textures
  112. texture = createCompressedDDS(path);
  113. }
  114. break;
  115. }
  116. }
  117. if (texture)
  118. {
  119. texture->_path = path;
  120. texture->_cached = true;
  121. // Add to texture cache.
  122. __textureCache.push_back(texture);
  123. return texture;
  124. }
  125. GP_ERROR("Failed to load texture from file '%s'.", path);
  126. return NULL;
  127. }
  128. Texture* Texture::create(Image* image, bool generateMipmaps)
  129. {
  130. GP_ASSERT( image );
  131. switch (image->getFormat())
  132. {
  133. case Image::RGB:
  134. return create(Texture::RGB, image->getWidth(), image->getHeight(), image->getData(), generateMipmaps);
  135. case Image::RGBA:
  136. return create(Texture::RGBA, image->getWidth(), image->getHeight(), image->getData(), generateMipmaps);
  137. default:
  138. GP_ERROR("Unsupported image format (%d).", image->getFormat());
  139. return NULL;
  140. }
  141. }
  142. Texture* Texture::create(Format format, unsigned int width, unsigned int height, const unsigned char* data, bool generateMipmaps, Texture::Type type)
  143. {
  144. GP_ASSERT( type == Texture::TEXTURE_2D || type == Texture::TEXTURE_CUBE );
  145. GLenum target = (GLenum)type;
  146. // Create the texture.
  147. GLuint textureId;
  148. GL_ASSERT( glGenTextures(1, &textureId) );
  149. GL_ASSERT( glBindTexture(target, textureId) );
  150. GL_ASSERT( glPixelStorei(GL_UNPACK_ALIGNMENT, 1) );
  151. #ifndef OPENGL_ES
  152. // glGenerateMipmap is new in OpenGL 3.0. For OpenGL 2.0 we must fallback to use glTexParameteri
  153. // with GL_GENERATE_MIPMAP prior to actual texture creation (glTexImage2D)
  154. if ( generateMipmaps && !std::addressof(glGenerateMipmap) )
  155. GL_ASSERT( glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE) );
  156. #endif
  157. // Load the texture
  158. if (type == Texture::TEXTURE_2D)
  159. {
  160. // Texture 2D
  161. GL_ASSERT( glTexImage2D(GL_TEXTURE_2D, 0, (GLenum)format, width, height, 0, (GLenum)format, GL_UNSIGNED_BYTE, data) );
  162. }
  163. else
  164. {
  165. // Get texture size
  166. unsigned int textureSize = width * height;
  167. switch (format)
  168. {
  169. case Texture::RGB:
  170. textureSize *= 3;
  171. break;
  172. case Texture::RGBA:
  173. textureSize *= 4;
  174. break;
  175. case Texture::ALPHA:
  176. break;
  177. case Texture::UNKNOWN:
  178. if (data)
  179. {
  180. glDeleteTextures(1, &textureId);
  181. GP_ERROR("Failed to determine texture size because format is UNKNOWN.");
  182. return NULL;
  183. }
  184. break;
  185. }
  186. // Texture Cube
  187. for (unsigned int i = 0; i < 6; i++)
  188. {
  189. const unsigned char* texturePtr = (data == NULL) ? NULL : &data[i * textureSize];
  190. GL_ASSERT( glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, (GLenum)format, width, height, 0, (GLenum)format, GL_UNSIGNED_BYTE, texturePtr) );
  191. }
  192. }
  193. // Set initial minification filter based on whether or not mipmaping was enabled.
  194. Filter minFilter = generateMipmaps ? NEAREST_MIPMAP_LINEAR : LINEAR;
  195. GL_ASSERT( glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter) );
  196. Texture* texture = new Texture();
  197. texture->_handle = textureId;
  198. texture->_format = format;
  199. texture->_type = type;
  200. texture->_width = width;
  201. texture->_height = height;
  202. texture->_minFilter = minFilter;
  203. if (generateMipmaps)
  204. {
  205. texture->generateMipmaps();
  206. }
  207. // Restore the texture id
  208. GL_ASSERT( glBindTexture((GLenum)__currentTextureType, __currentTextureId) );
  209. return texture;
  210. }
  211. Texture* Texture::create(TextureHandle handle, int width, int height, Format format)
  212. {
  213. GP_ASSERT( handle );
  214. Texture* texture = new Texture();
  215. if (glIsTexture(handle))
  216. {
  217. // There is no real way to query for texture type, but an error will be returned if a cube texture is bound to a 2D texture... so check for that
  218. glBindTexture(GL_TEXTURE_CUBE_MAP, handle);
  219. if (glGetError() == GL_NO_ERROR)
  220. {
  221. texture->_type = TEXTURE_CUBE;
  222. }
  223. else
  224. {
  225. // For now, it's either or. But if 3D textures and others are added, it might be useful to simply test a bunch of bindings and seeing which one doesn't error out
  226. texture->_type = TEXTURE_2D;
  227. }
  228. // Restore the texture id
  229. GL_ASSERT( glBindTexture((GLenum)__currentTextureType, __currentTextureId) );
  230. }
  231. texture->_handle = handle;
  232. texture->_format = format;
  233. texture->_width = width;
  234. texture->_height = height;
  235. return texture;
  236. }
  237. void Texture::setData(const unsigned char* data)
  238. {
  239. // Don't work with any compressed or cached textures
  240. GL_ASSERT( data );
  241. GL_ASSERT( (!_compressed) );
  242. GL_ASSERT( (!_cached) );
  243. GL_ASSERT( glBindTexture((GLenum)_type, _handle) );
  244. if (_type == Texture::TEXTURE_2D)
  245. {
  246. GL_ASSERT( glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _width, _height, (GLenum)_format, GL_UNSIGNED_BYTE, data) );
  247. }
  248. else
  249. {
  250. // Get texture size
  251. unsigned int textureSize = _width * _height;
  252. switch (_format)
  253. {
  254. case Texture::RGB:
  255. textureSize *= 3;
  256. break;
  257. case Texture::RGBA:
  258. textureSize *= 4;
  259. break;
  260. case Texture::ALPHA:
  261. break;
  262. }
  263. // Texture Cube
  264. for (unsigned int i = 0; i < 6; i++)
  265. {
  266. GL_ASSERT( glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, _width, _height, (GLenum)_format, GL_UNSIGNED_BYTE, &data[i * textureSize]) );
  267. }
  268. }
  269. if (_mipmapped)
  270. {
  271. generateMipmaps();
  272. }
  273. // Restore the texture id
  274. GL_ASSERT( glBindTexture((GLenum)__currentTextureType, __currentTextureId) );
  275. }
  276. // Computes the size of a PVRTC data chunk for a mipmap level of the given size.
  277. static unsigned int computePVRTCDataSize(int width, int height, int bpp)
  278. {
  279. int blockSize;
  280. int widthBlocks;
  281. int heightBlocks;
  282. if (bpp == 4)
  283. {
  284. blockSize = 4 * 4; // Pixel by pixel block size for 4bpp
  285. widthBlocks = std::max(width >> 2, 2);
  286. heightBlocks = std::max(height >> 2, 2);
  287. }
  288. else
  289. {
  290. blockSize = 8 * 4; // Pixel by pixel block size for 2bpp
  291. widthBlocks = std::max(width >> 3, 2);
  292. heightBlocks = std::max(height >> 2, 2);
  293. }
  294. return widthBlocks * heightBlocks * ((blockSize * bpp) >> 3);
  295. }
  296. Texture* Texture::createCompressedPVRTC(const char* path)
  297. {
  298. std::unique_ptr<Stream> stream(FileSystem::open(path));
  299. if (stream.get() == NULL || !stream->canRead())
  300. {
  301. GP_ERROR("Failed to load file '%s'.", path);
  302. return NULL;
  303. }
  304. // Read first 4 bytes to determine PVRTC format.
  305. size_t read;
  306. unsigned int version;
  307. read = stream->read(&version, sizeof(unsigned int), 1);
  308. if (read != 1)
  309. {
  310. GP_ERROR("Failed to read PVR version.");
  311. return NULL;
  312. }
  313. // Rewind to start of header.
  314. if (stream->seek(0, SEEK_SET) == false)
  315. {
  316. GP_ERROR("Failed to seek backwards to beginning of file after reading PVR version.");
  317. return NULL;
  318. }
  319. // Read texture data.
  320. GLsizei width, height;
  321. GLenum format;
  322. GLubyte* data = NULL;
  323. unsigned int mipMapCount;
  324. unsigned int faceCount;
  325. GLenum faces[6] = { GL_TEXTURE_2D };
  326. if (version == 0x03525650)
  327. {
  328. // Modern PVR file format.
  329. data = readCompressedPVRTC(path, stream.get(), &width, &height, &format, &mipMapCount, &faceCount, faces);
  330. }
  331. else
  332. {
  333. // Legacy PVR file format.
  334. data = readCompressedPVRTCLegacy(path, stream.get(), &width, &height, &format, &mipMapCount, &faceCount, faces);
  335. }
  336. if (data == NULL)
  337. {
  338. GP_ERROR("Failed to read texture data from PVR file '%s'.", path);
  339. return NULL;
  340. }
  341. stream->close();
  342. int bpp = (format == GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG || format == GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG) ? 2 : 4;
  343. // Generate our texture.
  344. GLenum target = faceCount > 1 ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
  345. GLuint textureId;
  346. GL_ASSERT( glGenTextures(1, &textureId) );
  347. GL_ASSERT( glBindTexture(target, textureId) );
  348. Filter minFilter = mipMapCount > 1 ? NEAREST_MIPMAP_LINEAR : LINEAR;
  349. GL_ASSERT( glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter) );
  350. Texture* texture = new Texture();
  351. texture->_handle = textureId;
  352. texture->_type = faceCount > 1 ? TEXTURE_CUBE : TEXTURE_2D;
  353. texture->_width = width;
  354. texture->_height = height;
  355. texture->_mipmapped = mipMapCount > 1;
  356. texture->_compressed = true;
  357. texture->_minFilter = minFilter;
  358. // Load the data for each level.
  359. GLubyte* ptr = data;
  360. for (unsigned int level = 0; level < mipMapCount; ++level)
  361. {
  362. unsigned int dataSize = computePVRTCDataSize(width, height, bpp);
  363. for (unsigned int face = 0; face < faceCount; ++face)
  364. {
  365. // Upload data to GL.
  366. GL_ASSERT(glCompressedTexImage2D(faces[face], level, format, width, height, 0, dataSize, &ptr[face * dataSize]));
  367. }
  368. width = std::max(width >> 1, 1);
  369. height = std::max(height >> 1, 1);
  370. ptr += dataSize * faceCount;
  371. }
  372. // Free data.
  373. SAFE_DELETE_ARRAY(data);
  374. // Restore the texture id
  375. GL_ASSERT( glBindTexture((GLenum)__currentTextureType, __currentTextureId) );
  376. return texture;
  377. }
  378. GLubyte* Texture::readCompressedPVRTC(const char* path, Stream* stream, GLsizei* width, GLsizei* height, GLenum* format, unsigned int* mipMapCount, unsigned int* faceCount, GLenum* faces)
  379. {
  380. GP_ASSERT( stream );
  381. GP_ASSERT( path );
  382. GP_ASSERT( width );
  383. GP_ASSERT( height );
  384. GP_ASSERT( format );
  385. GP_ASSERT( mipMapCount );
  386. GP_ASSERT( faceCount );
  387. GP_ASSERT( faces );
  388. struct pvrtc_file_header
  389. {
  390. unsigned int version;
  391. unsigned int flags;
  392. unsigned int pixelFormat[2];
  393. unsigned int colorSpace;
  394. unsigned int channelType;
  395. unsigned int height;
  396. unsigned int width;
  397. unsigned int depth;
  398. unsigned int surfaceCount;
  399. unsigned int faceCount;
  400. unsigned int mipMapCount;
  401. unsigned int metaDataSize;
  402. };
  403. struct pvrtc_metadata
  404. {
  405. char fourCC[4];
  406. unsigned int key;
  407. unsigned int dataSize;
  408. };
  409. size_t read;
  410. // Read header data.
  411. pvrtc_file_header header;
  412. read = stream->read(&header, sizeof(pvrtc_file_header), 1);
  413. if (read != 1)
  414. {
  415. GP_ERROR("Failed to read PVR header data for file '%s'.", path);
  416. return NULL;
  417. }
  418. if (header.pixelFormat[1] != 0)
  419. {
  420. // Unsupported pixel format.
  421. GP_ERROR("Unsupported pixel format in PVR file '%s'. (MSB == %d != 0)", path, header.pixelFormat[1]);
  422. return NULL;
  423. }
  424. int bpp;
  425. switch (header.pixelFormat[0])
  426. {
  427. case 0:
  428. *format = GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  429. bpp = 2;
  430. break;
  431. case 1:
  432. *format = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  433. bpp = 2;
  434. break;
  435. case 2:
  436. *format = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  437. bpp = 4;
  438. break;
  439. case 3:
  440. *format = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  441. bpp = 4;
  442. break;
  443. default:
  444. // Unsupported format.
  445. GP_ERROR("Unsupported pixel format value (%d) in PVR file '%s'.", header.pixelFormat[0], path);
  446. return NULL;
  447. }
  448. *width = (GLsizei)header.width;
  449. *height = (GLsizei)header.height;
  450. *mipMapCount = header.mipMapCount;
  451. *faceCount = std::min(header.faceCount, 6u);
  452. if ((*faceCount) > 1)
  453. {
  454. // Look for cubemap metadata and setup faces
  455. unsigned int remainingMetadata = header.metaDataSize;
  456. pvrtc_metadata mdHeader;
  457. bool foundTextureCubeMeta = false;
  458. while (remainingMetadata > 0)
  459. {
  460. read = stream->read(&mdHeader, sizeof(pvrtc_metadata), 1);
  461. if (read != 1)
  462. {
  463. GP_ERROR("Failed to read PVR metadata header data for file '%s'.", path);
  464. return NULL;
  465. }
  466. remainingMetadata -= sizeof(pvrtc_metadata) + mdHeader.dataSize;
  467. // Check that it's a known metadata type (specifically, cubemap order), otherwise skip to next metadata
  468. if ((mdHeader.fourCC[0] != 'P') ||
  469. (mdHeader.fourCC[1] != 'V') ||
  470. (mdHeader.fourCC[2] != 'R') ||
  471. (mdHeader.fourCC[3] != 3) ||
  472. (mdHeader.key != 2) || // Everything except cubemap order (cubemap order key is 2)
  473. (mdHeader.dataSize != 6)) // Cubemap order datasize should be 6
  474. {
  475. if (stream->seek(mdHeader.dataSize, SEEK_CUR) == false)
  476. {
  477. GP_ERROR("Failed to seek to next meta data header in PVR file '%s'.", path);
  478. return NULL;
  479. }
  480. continue;
  481. }
  482. // Get cubemap order
  483. foundTextureCubeMeta = true;
  484. char faceOrder[6];
  485. read = stream->read(faceOrder, 1, sizeof(faceOrder));
  486. if (read != sizeof(faceOrder))
  487. {
  488. GP_ERROR("Failed to read cubemap face order meta data for file '%s'.", path);
  489. return NULL;
  490. }
  491. for (unsigned int face = 0; face < (*faceCount); ++face)
  492. {
  493. faces[face] = GL_TEXTURE_CUBE_MAP_POSITIVE_X + (faceOrder[face] <= 'Z' ?
  494. ((faceOrder[face] - 'X') * 2) :
  495. (((faceOrder[face] - 'x') * 2) + 1));
  496. if (faces[face] < GL_TEXTURE_CUBE_MAP_POSITIVE_X)
  497. {
  498. // Just overwrite this face
  499. faces[face] = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  500. }
  501. }
  502. }
  503. if (!foundTextureCubeMeta)
  504. {
  505. // Didn't find cubemap metadata. Just assume it's "in order"
  506. for (unsigned int face = 0; face < (*faceCount); ++face)
  507. {
  508. faces[face] = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
  509. }
  510. }
  511. }
  512. else
  513. {
  514. // Skip meta-data.
  515. if (stream->seek(header.metaDataSize, SEEK_CUR) == false)
  516. {
  517. GP_ERROR("Failed to seek past header meta data in PVR file '%s'.", path);
  518. return NULL;
  519. }
  520. }
  521. // Compute total size of data to be read.
  522. int w = *width;
  523. int h = *height;
  524. size_t dataSize = 0;
  525. for (unsigned int level = 0; level < header.mipMapCount; ++level)
  526. {
  527. dataSize += computePVRTCDataSize(w, h, bpp) * (*faceCount);
  528. w = std::max(w>>1, 1);
  529. h = std::max(h>>1, 1);
  530. }
  531. // Read data.
  532. GLubyte* data = new GLubyte[dataSize];
  533. read = stream->read(data, 1, dataSize);
  534. if (read != dataSize)
  535. {
  536. SAFE_DELETE_ARRAY(data);
  537. GP_ERROR("Failed to read texture data from PVR file '%s'.", path);
  538. return NULL;
  539. }
  540. return data;
  541. }
  542. GLubyte* Texture::readCompressedPVRTCLegacy(const char* path, Stream* stream, GLsizei* width, GLsizei* height, GLenum* format, unsigned int* mipMapCount, unsigned int* faceCount, GLenum* faces)
  543. {
  544. char PVRTCIdentifier[] = "PVR!";
  545. struct pvrtc_file_header_legacy
  546. {
  547. unsigned int size; // size of the structure
  548. unsigned int height; // height of surface to be created
  549. unsigned int width; // width of input surface
  550. unsigned int mipmapCount; // number of mip-map levels requested
  551. unsigned int formatflags; // pixel format flags
  552. unsigned int dataSize; // total size in bytes
  553. unsigned int bpp; // number of bits per pixel
  554. unsigned int redBitMask; // mask for red bit
  555. unsigned int greenBitMask; // mask for green bits
  556. unsigned int blueBitMask; // mask for blue bits
  557. unsigned int alphaBitMask; // mask for alpha channel
  558. unsigned int pvrtcTag; // magic number identifying pvrtc file
  559. unsigned int surfaceCount; // number of surfaces present in the pvrtc
  560. };
  561. // Read the file header.
  562. unsigned int size = sizeof(pvrtc_file_header_legacy);
  563. pvrtc_file_header_legacy header;
  564. unsigned int read = (int)stream->read(&header, 1, size);
  565. if (read != size)
  566. {
  567. GP_ERROR("Failed to read file header for pvrtc file '%s'.", path);
  568. return NULL;
  569. }
  570. // Proper file header identifier.
  571. if (PVRTCIdentifier[0] != (char)((header.pvrtcTag >> 0) & 0xff) ||
  572. PVRTCIdentifier[1] != (char)((header.pvrtcTag >> 8) & 0xff) ||
  573. PVRTCIdentifier[2] != (char)((header.pvrtcTag >> 16) & 0xff) ||
  574. PVRTCIdentifier[3] != (char)((header.pvrtcTag >> 24) & 0xff))
  575. {
  576. GP_ERROR("Failed to load pvrtc file '%s': invalid header.", path);
  577. return NULL;
  578. }
  579. // Format flags for GLenum format.
  580. if (header.bpp == 4)
  581. {
  582. *format = header.alphaBitMask ? GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG : GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  583. }
  584. else if (header.bpp == 2)
  585. {
  586. *format = header.alphaBitMask ? GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG : GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  587. }
  588. else
  589. {
  590. GP_ERROR("Failed to load pvrtc file '%s': invalid pvrtc compressed texture format flags.", path);
  591. return NULL;
  592. }
  593. *width = (GLsizei)header.width;
  594. *height = (GLsizei)header.height;
  595. *mipMapCount = header.mipmapCount + 1; // +1 because mipmapCount does not include the base level
  596. *faceCount = 1;
  597. // Flags (needed legacy documentation on format, pre-PVR Format 3.0)
  598. if ((header.formatflags & 0x1000) != 0)
  599. {
  600. // Texture cube
  601. *faceCount = std::min(header.surfaceCount, 6u);
  602. for (unsigned int face = 0; face < (*faceCount); ++face)
  603. {
  604. faces[face] = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
  605. }
  606. }
  607. else if ((header.formatflags & 0x4000) != 0)
  608. {
  609. // Volume texture
  610. GP_ERROR("Failed to load pvrtc file '%s': volume texture is not supported.", path);
  611. return NULL;
  612. }
  613. unsigned int totalSize = header.dataSize; // Docs say dataSize is the size of the whole surface, or one face of a texture cube. But this does not appear to be the case with the latest PVRTexTool
  614. GLubyte* data = new GLubyte[totalSize];
  615. read = (int)stream->read(data, 1, totalSize);
  616. if (read != totalSize)
  617. {
  618. SAFE_DELETE_ARRAY(data);
  619. GP_ERROR("Failed to load texture data for pvrtc file '%s'.", path);
  620. return NULL;
  621. }
  622. return data;
  623. }
  624. int Texture::getMaskByteIndex(unsigned int mask)
  625. {
  626. switch (mask)
  627. {
  628. case 0xff000000:
  629. return 3;
  630. case 0x00ff0000:
  631. return 2;
  632. case 0x0000ff00:
  633. return 1;
  634. case 0x000000ff:
  635. return 0;
  636. default:
  637. return -1; // no or invalid mask
  638. }
  639. }
  640. Texture* Texture::createCompressedDDS(const char* path)
  641. {
  642. GP_ASSERT( path );
  643. // DDS file structures.
  644. struct dds_pixel_format
  645. {
  646. unsigned int dwSize;
  647. unsigned int dwFlags;
  648. unsigned int dwFourCC;
  649. unsigned int dwRGBBitCount;
  650. unsigned int dwRBitMask;
  651. unsigned int dwGBitMask;
  652. unsigned int dwBBitMask;
  653. unsigned int dwABitMask;
  654. };
  655. struct dds_header
  656. {
  657. unsigned int dwSize;
  658. unsigned int dwFlags;
  659. unsigned int dwHeight;
  660. unsigned int dwWidth;
  661. unsigned int dwPitchOrLinearSize;
  662. unsigned int dwDepth;
  663. unsigned int dwMipMapCount;
  664. unsigned int dwReserved1[11];
  665. dds_pixel_format ddspf;
  666. unsigned int dwCaps;
  667. unsigned int dwCaps2;
  668. unsigned int dwCaps3;
  669. unsigned int dwCaps4;
  670. unsigned int dwReserved2;
  671. };
  672. struct dds_mip_level
  673. {
  674. GLubyte* data;
  675. GLsizei width;
  676. GLsizei height;
  677. GLsizei size;
  678. };
  679. Texture* texture = NULL;
  680. // Read DDS file.
  681. std::unique_ptr<Stream> stream(FileSystem::open(path));
  682. if (stream.get() == NULL || !stream->canRead())
  683. {
  684. GP_ERROR("Failed to open file '%s'.", path);
  685. return NULL;
  686. }
  687. // Validate DDS magic number.
  688. char code[4];
  689. if (stream->read(code, 1, 4) != 4 || strncmp(code, "DDS ", 4) != 0)
  690. {
  691. GP_ERROR("Failed to read DDS file '%s': invalid DDS magic number.", path);
  692. return NULL;
  693. }
  694. // Read DDS header.
  695. dds_header header;
  696. if (stream->read(&header, sizeof(dds_header), 1) != 1)
  697. {
  698. GP_ERROR("Failed to read header for DDS file '%s'.", path);
  699. return NULL;
  700. }
  701. if ((header.dwFlags & 0x20000/*DDSD_MIPMAPCOUNT*/) == 0)
  702. {
  703. // Mipmap count not specified (non-mipmapped texture).
  704. header.dwMipMapCount = 1;
  705. }
  706. // Check type of images. Default is a regular texture
  707. unsigned int facecount = 1;
  708. GLenum faces[6] = { GL_TEXTURE_2D };
  709. GLenum target = GL_TEXTURE_2D;
  710. if ((header.dwCaps2 & 0x200/*DDSCAPS2_CUBEMAP*/) != 0)
  711. {
  712. facecount = 0;
  713. for (unsigned int off = 0, flag = 0x400/*DDSCAPS2_CUBEMAP_POSITIVEX*/; off < 6; ++off, flag <<= 1)
  714. {
  715. if ((header.dwCaps2 & flag) != 0)
  716. {
  717. faces[facecount++] = GL_TEXTURE_CUBE_MAP_POSITIVE_X + off;
  718. }
  719. }
  720. target = GL_TEXTURE_CUBE_MAP;
  721. }
  722. else if ((header.dwCaps2 & 0x200000/*DDSCAPS2_VOLUME*/) != 0)
  723. {
  724. // Volume textures unsupported.
  725. GP_ERROR("Failed to create texture from DDS file '%s': volume textures are unsupported.", path);
  726. return NULL;
  727. }
  728. // Allocate mip level structures.
  729. dds_mip_level* mipLevels = new dds_mip_level[header.dwMipMapCount * facecount];
  730. memset(mipLevels, 0, sizeof(dds_mip_level) * header.dwMipMapCount * facecount);
  731. GLenum format = 0;
  732. GLenum internalFormat = 0;
  733. bool compressed = false;
  734. GLsizei width = header.dwWidth;
  735. GLsizei height = header.dwHeight;
  736. Texture::Format textureFormat = Texture::UNKNOWN;
  737. if (header.ddspf.dwFlags & 0x4/*DDPF_FOURCC*/)
  738. {
  739. compressed = true;
  740. int bytesPerBlock;
  741. // Compressed.
  742. switch (header.ddspf.dwFourCC)
  743. {
  744. case ('D'|('X'<<8)|('T'<<16)|('1'<<24)):
  745. format = internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  746. bytesPerBlock = 8;
  747. break;
  748. case ('D'|('X'<<8)|('T'<<16)|('3'<<24)):
  749. format = internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  750. bytesPerBlock = 16;
  751. break;
  752. case ('D'|('X'<<8)|('T'<<16)|('5'<<24)):
  753. format = internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  754. bytesPerBlock = 16;
  755. break;
  756. case ('A'|('T'<<8)|('C'<<16)|(' '<<24)):
  757. format = internalFormat = ATC_RGB_AMD;
  758. bytesPerBlock = 8;
  759. break;
  760. case ('A'|('T'<<8)|('C'<<16)|('A'<<24)):
  761. format = internalFormat = ATC_RGBA_EXPLICIT_ALPHA_AMD;
  762. bytesPerBlock = 16;
  763. break;
  764. case ('A'|('T'<<8)|('C'<<16)|('I'<<24)):
  765. format = internalFormat = ATC_RGBA_INTERPOLATED_ALPHA_AMD;
  766. bytesPerBlock = 16;
  767. break;
  768. case ('E'|('T'<<8)|('C'<<16)|('1'<<24)):
  769. format = internalFormat = ETC1_RGB8;
  770. bytesPerBlock = 8;
  771. break;
  772. default:
  773. GP_ERROR("Unsupported compressed texture format (%d) for DDS file '%s'.", header.ddspf.dwFourCC, path);
  774. SAFE_DELETE_ARRAY(mipLevels);
  775. return NULL;
  776. }
  777. for (unsigned int face = 0; face < facecount; ++face)
  778. {
  779. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  780. {
  781. dds_mip_level& level = mipLevels[i + face * header.dwMipMapCount];
  782. level.width = width;
  783. level.height = height;
  784. level.size = std::max(1, (width + 3) >> 2) * std::max(1, (height + 3) >> 2) * bytesPerBlock;
  785. level.data = new GLubyte[level.size];
  786. if (stream->read(level.data, 1, level.size) != (unsigned int)level.size)
  787. {
  788. GP_ERROR("Failed to load dds compressed texture bytes for texture: %s", path);
  789. // Cleanup mip data.
  790. for (unsigned int face = 0; face < facecount; ++face)
  791. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  792. SAFE_DELETE_ARRAY(mipLevels[i + face * header.dwMipMapCount].data);
  793. SAFE_DELETE_ARRAY(mipLevels);
  794. return texture;
  795. }
  796. width = std::max(1, width >> 1);
  797. height = std::max(1, height >> 1);
  798. }
  799. width = header.dwWidth;
  800. height = header.dwHeight;
  801. }
  802. }
  803. else if (header.ddspf.dwFlags & 0x40/*DDPF_RGB*/)
  804. {
  805. // RGB/RGBA (uncompressed)
  806. bool colorConvert = false;
  807. unsigned int rmask = header.ddspf.dwRBitMask;
  808. unsigned int gmask = header.ddspf.dwGBitMask;
  809. unsigned int bmask = header.ddspf.dwBBitMask;
  810. unsigned int amask = header.ddspf.dwABitMask;
  811. int ridx = getMaskByteIndex(rmask);
  812. int gidx = getMaskByteIndex(gmask);
  813. int bidx = getMaskByteIndex(bmask);
  814. int aidx = getMaskByteIndex(amask);
  815. if (header.ddspf.dwRGBBitCount == 24)
  816. {
  817. format = internalFormat = GL_RGB;
  818. textureFormat = Texture::RGB;
  819. colorConvert = (ridx != 0) || (gidx != 1) || (bidx != 2);
  820. }
  821. else if (header.ddspf.dwRGBBitCount == 32)
  822. {
  823. format = internalFormat = GL_RGBA;
  824. textureFormat = Texture::RGBA;
  825. if (ridx == 0 && gidx == 1 && bidx == 2)
  826. {
  827. aidx = 3; // XBGR or ABGR
  828. colorConvert = false;
  829. }
  830. else if (ridx == 2 && gidx == 1 && bidx == 0)
  831. {
  832. aidx = 3; // XRGB or ARGB
  833. colorConvert = true;
  834. }
  835. else
  836. {
  837. format = 0; // invalid format
  838. }
  839. }
  840. if (format == 0)
  841. {
  842. GP_ERROR("Failed to create texture from uncompressed DDS file '%s': Unsupported color format (must be one of R8G8B8, A8R8G8B8, A8B8G8R8, X8R8G8B8, X8B8G8R8.", path);
  843. SAFE_DELETE_ARRAY(mipLevels);
  844. return NULL;
  845. }
  846. // Read data.
  847. for (unsigned int face = 0; face < facecount; ++face)
  848. {
  849. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  850. {
  851. dds_mip_level& level = mipLevels[i + face * header.dwMipMapCount];
  852. level.width = width;
  853. level.height = height;
  854. level.size = width * height * (header.ddspf.dwRGBBitCount >> 3);
  855. level.data = new GLubyte[level.size];
  856. if (stream->read(level.data, 1, level.size) != (unsigned int)level.size)
  857. {
  858. GP_ERROR("Failed to load bytes for RGB dds texture: %s", path);
  859. // Cleanup mip data.
  860. for (unsigned int face = 0; face < facecount; ++face)
  861. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  862. SAFE_DELETE_ARRAY(mipLevels[i + face * header.dwMipMapCount].data);
  863. SAFE_DELETE_ARRAY(mipLevels);
  864. return texture;
  865. }
  866. width = std::max(1, width >> 1);
  867. height = std::max(1, height >> 1);
  868. }
  869. width = header.dwWidth;
  870. height = header.dwHeight;
  871. }
  872. // Perform color conversion.
  873. if (colorConvert)
  874. {
  875. // Note: While it's possible to use BGRA_EXT texture formats here and avoid CPU color conversion below,
  876. // there seems to be different flavors of the BGRA extension, with some vendors requiring an internal
  877. // format of RGBA and others requiring an internal format of BGRA.
  878. // We could be smarter here later and skip color conversion in favor of GL_BGRA_EXT (for format
  879. // and/or internal format) based on which GL extensions are available.
  880. // Tip: Using A8B8G8R8 and X8B8G8R8 DDS format maps directly to GL RGBA and requires on no color conversion.
  881. GLubyte *pixel, r, g, b, a;
  882. if (format == GL_RGB)
  883. {
  884. for (unsigned int face = 0; face < facecount; ++face)
  885. {
  886. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  887. {
  888. dds_mip_level& level = mipLevels[i + face * header.dwMipMapCount];
  889. for (int j = 0; j < level.size; j += 3)
  890. {
  891. pixel = &level.data[j];
  892. r = pixel[ridx]; g = pixel[gidx]; b = pixel[bidx];
  893. pixel[0] = r; pixel[1] = g; pixel[2] = b;
  894. }
  895. }
  896. }
  897. }
  898. else if (format == GL_RGBA)
  899. {
  900. for (unsigned int face = 0; face < facecount; ++face)
  901. {
  902. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  903. {
  904. dds_mip_level& level = mipLevels[i + face * header.dwMipMapCount];
  905. for (int j = 0; j < level.size; j += 4)
  906. {
  907. pixel = &level.data[j];
  908. r = pixel[ridx]; g = pixel[gidx]; b = pixel[bidx]; a = pixel[aidx];
  909. pixel[0] = r; pixel[1] = g; pixel[2] = b; pixel[3] = a;
  910. }
  911. }
  912. }
  913. }
  914. }
  915. }
  916. else
  917. {
  918. // Unsupported.
  919. GP_ERROR("Failed to create texture from DDS file '%s': unsupported flags (%d).", path, header.ddspf.dwFlags);
  920. SAFE_DELETE_ARRAY(mipLevels);
  921. return NULL;
  922. }
  923. // Close file.
  924. stream->close();
  925. // Generate GL texture.
  926. GLuint textureId;
  927. GL_ASSERT( glGenTextures(1, &textureId) );
  928. GL_ASSERT( glBindTexture(target, textureId) );
  929. Filter minFilter = header.dwMipMapCount > 1 ? NEAREST_MIPMAP_LINEAR : LINEAR;
  930. GL_ASSERT( glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter ) );
  931. // Create gameplay texture.
  932. texture = new Texture();
  933. texture->_handle = textureId;
  934. texture->_type = (Type)target;
  935. texture->_width = header.dwWidth;
  936. texture->_height = header.dwHeight;
  937. texture->_compressed = compressed;
  938. texture->_mipmapped = header.dwMipMapCount > 1;
  939. texture->_minFilter = minFilter;
  940. // Load texture data.
  941. for (unsigned int face = 0; face < facecount; ++face)
  942. {
  943. GLenum texImageTarget = faces[face];
  944. for (unsigned int i = 0; i < header.dwMipMapCount; ++i)
  945. {
  946. dds_mip_level& level = mipLevels[i + face * header.dwMipMapCount];
  947. if (compressed)
  948. {
  949. GL_ASSERT(glCompressedTexImage2D(texImageTarget, i, format, level.width, level.height, 0, level.size, level.data));
  950. }
  951. else
  952. {
  953. GL_ASSERT(glTexImage2D(texImageTarget, i, internalFormat, level.width, level.height, 0, format, GL_UNSIGNED_BYTE, level.data));
  954. }
  955. // Clean up the texture data.
  956. SAFE_DELETE_ARRAY(level.data);
  957. }
  958. }
  959. // Clean up mip levels structure.
  960. SAFE_DELETE_ARRAY(mipLevels);
  961. // Restore the texture id
  962. GL_ASSERT( glBindTexture((GLenum)__currentTextureType, __currentTextureId) );
  963. return texture;
  964. }
  965. Texture::Format Texture::getFormat() const
  966. {
  967. return _format;
  968. }
  969. Texture::Type Texture::getType() const
  970. {
  971. return _type;
  972. }
  973. const char* Texture::getPath() const
  974. {
  975. return _path.c_str();
  976. }
  977. unsigned int Texture::getWidth() const
  978. {
  979. return _width;
  980. }
  981. unsigned int Texture::getHeight() const
  982. {
  983. return _height;
  984. }
  985. TextureHandle Texture::getHandle() const
  986. {
  987. return _handle;
  988. }
  989. void Texture::generateMipmaps()
  990. {
  991. if (!_mipmapped)
  992. {
  993. GLenum target = (GLenum)_type;
  994. GL_ASSERT( glBindTexture(target, _handle) );
  995. GL_ASSERT( glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST) );
  996. if( std::addressof(glGenerateMipmap) )
  997. GL_ASSERT( glGenerateMipmap(target) );
  998. _mipmapped = true;
  999. // Restore the texture id
  1000. GL_ASSERT( glBindTexture((GLenum)__currentTextureType, __currentTextureId) );
  1001. }
  1002. }
  1003. bool Texture::isMipmapped() const
  1004. {
  1005. return _mipmapped;
  1006. }
  1007. bool Texture::isCompressed() const
  1008. {
  1009. return _compressed;
  1010. }
  1011. Texture::Sampler::Sampler(Texture* texture)
  1012. : _texture(texture), _wrapS(Texture::REPEAT), _wrapT(Texture::REPEAT), _wrapR(Texture::REPEAT)
  1013. {
  1014. GP_ASSERT( texture );
  1015. _minFilter = texture->_minFilter;
  1016. _magFilter = texture->_magFilter;
  1017. }
  1018. Texture::Sampler::~Sampler()
  1019. {
  1020. SAFE_RELEASE(_texture);
  1021. }
  1022. Texture::Sampler* Texture::Sampler::create(Texture* texture)
  1023. {
  1024. GP_ASSERT( texture );
  1025. GP_ASSERT( texture->_type == Texture::TEXTURE_2D || texture->_type == Texture::TEXTURE_CUBE );
  1026. texture->addRef();
  1027. return new Sampler(texture);
  1028. }
  1029. Texture::Sampler* Texture::Sampler::create(const char* path, bool generateMipmaps)
  1030. {
  1031. Texture* texture = Texture::create(path, generateMipmaps);
  1032. return texture ? new Sampler(texture) : NULL;
  1033. }
  1034. void Texture::Sampler::setWrapMode(Wrap wrapS, Wrap wrapT, Wrap wrapR)
  1035. {
  1036. _wrapS = wrapS;
  1037. _wrapT = wrapT;
  1038. _wrapR = wrapR;
  1039. }
  1040. void Texture::Sampler::setFilterMode(Filter minificationFilter, Filter magnificationFilter)
  1041. {
  1042. _minFilter = minificationFilter;
  1043. _magFilter = magnificationFilter;
  1044. }
  1045. Texture* Texture::Sampler::getTexture() const
  1046. {
  1047. return _texture;
  1048. }
  1049. void Texture::Sampler::bind()
  1050. {
  1051. GP_ASSERT( _texture );
  1052. GLenum target = (GLenum)_texture->_type;
  1053. if (__currentTextureId != _texture->_handle)
  1054. {
  1055. GL_ASSERT( glBindTexture(target, _texture->_handle) );
  1056. __currentTextureId = _texture->_handle;
  1057. __currentTextureType = _texture->_type;
  1058. }
  1059. if (_texture->_minFilter != _minFilter)
  1060. {
  1061. _texture->_minFilter = _minFilter;
  1062. GL_ASSERT( glTexParameteri(target, GL_TEXTURE_MIN_FILTER, (GLenum)_minFilter) );
  1063. }
  1064. if (_texture->_magFilter != _magFilter)
  1065. {
  1066. _texture->_magFilter = _magFilter;
  1067. GL_ASSERT( glTexParameteri(target, GL_TEXTURE_MAG_FILTER, (GLenum)_magFilter) );
  1068. }
  1069. if (_texture->_wrapS != _wrapS)
  1070. {
  1071. _texture->_wrapS = _wrapS;
  1072. GL_ASSERT( glTexParameteri(target, GL_TEXTURE_WRAP_S, (GLenum)_wrapS) );
  1073. }
  1074. if (_texture->_wrapT != _wrapT)
  1075. {
  1076. _texture->_wrapT = _wrapT;
  1077. GL_ASSERT( glTexParameteri(target, GL_TEXTURE_WRAP_T, (GLenum)_wrapT) );
  1078. }
  1079. #if defined(GL_TEXTURE_WRAP_R) // OpenGL ES 3.x and up, OpenGL 1.2 and up
  1080. if (_texture->_wrapR != _wrapR)
  1081. {
  1082. _texture->_wrapR = _wrapR;
  1083. if (target == GL_TEXTURE_CUBE_MAP) // We don't want to run this on something that we know will fail
  1084. GL_ASSERT( glTexParameteri(target, GL_TEXTURE_WRAP_R, (GLenum)_wrapR) );
  1085. }
  1086. #endif
  1087. }
  1088. }