PageRenderTime 148ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 1ms

/GT-I5700/gles20/src/glTex.cpp

https://github.com/DE-NISkA/cm_android_vendor_spica
C++ | 5533 lines | 3760 code | 1017 blank | 756 comment | 1205 complexity | 18e6b01492ad12521b320a8745f6d4c6 MD5 | raw file
  1. /*
  2. *******************************************************************************
  3. *
  4. * SAMSUNG INDIA SOFTWARE OPERATIONS
  5. * Copyright(C) 2006
  6. * ALL RIGHTS RESERVED
  7. *
  8. * This program is proprietary to Samsung India Software Operations Pvt. Ltd.,
  9. * and is protected under International Copyright Act as an unpublished work.Its
  10. * use and disclosure is limited by the terms and conditions of a license agree-
  11. * -ment. It may not be copied or otherwise reproduced or disclosed to persons
  12. * outside the licensee's organization except in accordance with the terms and
  13. * conditions of such an agreement. All copies and reproductions shall be the
  14. * property of Samsung India Software Operations Pvt. Ltd. and must bear this
  15. * notice in its entirety.
  16. *
  17. *******************************************************************************
  18. */
  19. /*
  20. ***************************************************************************//*!
  21. *
  22. * \file glTex.cpp
  23. * \author Prashant Singh (prashant@samsung.com),
  24. * Anurag Ved (anuragv@samsung.com)
  25. * \brief Texture handling
  26. *
  27. *//*---------------------------------------------------------------------------
  28. * NOTES:
  29. *
  30. *//*---------------------------------------------------------------------------
  31. * HISTORY:
  32. *
  33. * 31.07.2006 Anurag Ved Initial draft implementation
  34. *
  35. * 3.08.2006 Prashant Singh Functions with new state and texture object
  36. *
  37. * 22.08.2006 Prashant Singh Compilable version with isComplete, Compile,
  38. * etc. Complete image specification, tex copy
  39. * and compressed image api is missing
  40. *
  41. * 24.08.2006 Prashant Singh Priliminary testing on host completed
  42. *
  43. *******************************************************************************
  44. */
  45. /*
  46. *******************************************************************************
  47. * Includes
  48. *******************************************************************************
  49. */
  50. #include "gl.h"
  51. #include "platform.h"
  52. #include "glState.h"
  53. #include "glTex.h"
  54. #include "buffers.h"
  55. #include "texfglstate.h"
  56. #include "glFimg.h"
  57. //#include "pixel.h"
  58. #include "ustl.h"
  59. /*
  60. *******************************************************************************
  61. * Macro definitions and enumerations
  62. *******************************************************************************
  63. */
  64. #define TEX_2D_DEFAULT (MAX_TEXTURE_OBJECTS)
  65. #define TEX_3D_DEFAULT (TEX_2D_DEFAULT + 1)
  66. #define TEX_CUBE_MAP_DEFAULT (TEX_3D_DEFAULT + 1)
  67. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  68. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  69. /*
  70. *******************************************************************************
  71. * Type, Structure & Class Definitions
  72. *******************************************************************************
  73. */
  74. /*
  75. *******************************************************************************
  76. * Global Variables
  77. *******************************************************************************
  78. */
  79. /*
  80. *******************************************************************************
  81. * Local Function Declarations
  82. *******************************************************************************
  83. */
  84. //static GLint GetTextureObject (OGLState* pState, GLenum target);
  85. static GLenum checkTextureFormat (OGLState* pState, GLenum internalFormat, GLenum format);
  86. static inline GLboolean IsPowerOf2 (GLuint n);
  87. static bool s3tcCompressedTex(GLenum& texformat,GLenum internalformat,GLsizei width,GLsizei height,GLsizei imageSize);
  88. static GLboolean CheckFormatConversion(GLenum dstFormat, GLenum srcFormat, GLenum* dstType);
  89. /*
  90. *******************************************************************************
  91. * Function Definitions
  92. *******************************************************************************
  93. */
  94. static void decodePalette4(const void *data, int level, int width, int height,
  95. void *surface, int stride, int format)
  96. {
  97. int indexBits = 8;
  98. int entrySize = 0;
  99. switch (format) {
  100. case GL_PALETTE4_RGB8_OES:
  101. indexBits = 4;
  102. /* FALLTHROUGH */
  103. case GL_PALETTE8_RGB8_OES:
  104. entrySize = 3;
  105. break;
  106. case GL_PALETTE4_RGBA8_OES:
  107. indexBits = 4;
  108. /* FALLTHROUGH */
  109. case GL_PALETTE8_RGBA8_OES:
  110. entrySize = 4;
  111. break;
  112. case GL_PALETTE4_R5_G6_B5_OES:
  113. case GL_PALETTE4_RGBA4_OES:
  114. case GL_PALETTE4_RGB5_A1_OES:
  115. indexBits = 4;
  116. /* FALLTHROUGH */
  117. case GL_PALETTE8_R5_G6_B5_OES:
  118. case GL_PALETTE8_RGBA4_OES:
  119. case GL_PALETTE8_RGB5_A1_OES:
  120. entrySize = 2;
  121. break;
  122. }
  123. const int paletteSize = (1 << indexBits) * entrySize;
  124. unsigned char const* pixels = (unsigned char *)data + paletteSize;
  125. for (int i=0 ; i<level ; i++) {
  126. int w = (width >> i) ? : 1;
  127. int h = (height >> i) ? : 1;
  128. pixels += h * ((w * indexBits) / 8);
  129. }
  130. width = (width >> level) ? : 1;
  131. height = (height >> level) ? : 1;
  132. if (entrySize == 2) {
  133. unsigned char const* const palette = (unsigned char*)data;
  134. for (int y=0 ; y<height ; y++) {
  135. unsigned char* p = (unsigned char*)surface + y*stride*2;
  136. if (indexBits == 8) {
  137. for (int x=0 ; x<width ; x++) {
  138. int index = 2 * (*pixels++);
  139. *p++ = palette[index + 0];
  140. *p++ = palette[index + 1];
  141. }
  142. } else {
  143. for (int x=0 ; x<width ; x+=2) {
  144. int v = *pixels++;
  145. int index = 2 * (v >> 4);
  146. *p++ = palette[index + 0];
  147. *p++ = palette[index + 1];
  148. if (x+1 < width) {
  149. index = 2 * (v & 0xF);
  150. *p++ = palette[index + 0];
  151. *p++ = palette[index + 1];
  152. }
  153. }
  154. }
  155. }
  156. } else if (entrySize == 3) {
  157. unsigned char const* const palette = (unsigned char*)data;
  158. for (int y=0 ; y<height ; y++) {
  159. unsigned char* p = (unsigned char*)surface + y*stride*3;
  160. if (indexBits == 8) {
  161. for (int x=0 ; x<width ; x++) {
  162. int index = 3 * (*pixels++);
  163. *p++ = palette[index + 0];
  164. *p++ = palette[index + 1];
  165. *p++ = palette[index + 2];
  166. }
  167. } else {
  168. for (int x=0 ; x<width ; x+=2) {
  169. int v = *pixels++;
  170. int index = 3 * (v >> 4);
  171. *p++ = palette[index + 0];
  172. *p++ = palette[index + 1];
  173. *p++ = palette[index + 2];
  174. if (x+1 < width) {
  175. index = 3 * (v & 0xF);
  176. *p++ = palette[index + 0];
  177. *p++ = palette[index + 1];
  178. *p++ = palette[index + 2];
  179. }
  180. }
  181. }
  182. }
  183. } else if (entrySize == 4) {
  184. unsigned char const* const palette = (unsigned char*)data;
  185. for (int y=0 ; y<height ; y++) {
  186. unsigned char* p = (unsigned char*)surface + y*stride*4;
  187. if (indexBits == 8) {
  188. for (int x=0 ; x<width ; x++) {
  189. int index = 4 * (*pixels++);
  190. *p++ = palette[index + 0];
  191. *p++ = palette[index + 1];
  192. *p++ = palette[index + 2];
  193. *p++ = palette[index + 3];
  194. }
  195. } else {
  196. for (int x=0 ; x<width ; x+=2) {
  197. int v = *pixels++;
  198. int index = 4 * (v >> 4);
  199. *p++ = palette[index + 0];
  200. *p++ = palette[index + 1];
  201. *p++ = palette[index + 2];
  202. *p++ = palette[index + 3];
  203. if (x+1 < width) {
  204. index = 4 * (v & 0xF);
  205. *p++ = palette[index + 0];
  206. *p++ = palette[index + 1];
  207. *p++ = palette[index + 2];
  208. *p++ = palette[index + 3];
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. #if 1
  216. GLboolean getTextureNames( SharedTextureState* pSharedTexState ,GLsizei n, GLuint *textures)
  217. {
  218. GLuint lastValue = 1;
  219. GLsizei toAssign = n;
  220. int (* unusedValueRange)[2]=NULL; // The first element will store the starting unused name (not present in the set) and the second element will store the range
  221. unusedValueRange = (int(*)[2])Plat::malloc ( 2*sizeof(int));
  222. int index = 0 ;
  223. //right from the beginning to the end of the set there might be unused value , to assign these value first
  224. TexNameSet::iterator it = pSharedTexState->usedTexNames.begin();
  225. while(it != pSharedTexState->usedTexNames.end()){
  226. //if the values are not equal it means atleast 1 value is missing
  227. if(*it == lastValue){
  228. it++;
  229. lastValue++;
  230. }
  231. else{ //storing the unused name and the range
  232. GLsizei noOfConsecUnusedValue = *it - lastValue; //the no of unused name(not present in the set)
  233. unusedValueRange[index][0] = lastValue;
  234. unusedValueRange[index][1] = noOfConsecUnusedValue;
  235. index++;
  236. unusedValueRange = (int(*)[2])Plat::realloc(unusedValueRange, 2*(index+1)*sizeof(int));
  237. lastValue = lastValue + noOfConsecUnusedValue;
  238. }
  239. }
  240. //inserting the unused name in the set
  241. for(GLsizei var = 0; var < index; var++)
  242. {
  243. for (GLsizei count = 0; count < unusedValueRange[var][1] ; count++)
  244. {
  245. *textures++ = unusedValueRange[var][0];
  246. toAssign--;
  247. pSharedTexState->usedTexNames.insert(unusedValueRange[var][0]);
  248. unusedValueRange[var][0]++;
  249. if (toAssign == 0)
  250. {
  251. free(unusedValueRange);
  252. return GL_TRUE;
  253. }
  254. }
  255. }
  256. //some texture might be left to be assigned
  257. for( ; toAssign > 0 ; toAssign--){
  258. *textures++ = lastValue;
  259. pSharedTexState->usedTexNames.insert(lastValue);
  260. lastValue++;
  261. }
  262. Plat::safe_free(unusedValueRange);
  263. return GL_TRUE;
  264. }
  265. #else
  266. GLboolean getTextureNames( SharedTextureState* pSharedTexState ,GLsizei n, GLuint *textures)
  267. {
  268. GLuint lastValue = 1;
  269. GLsizei toAssign = n;
  270. TexNameSet::iterator it = pSharedTexState->usedTexNames.begin();
  271. //right from the beginning to the end of the set there might be unused value , to assign these value first
  272. while(it != pSharedTexState->usedTexNames.end()){
  273. //if the values are not equal it means atleast 1 value is missing
  274. if(*it == lastValue){
  275. lastValue ++ ;
  276. it++;
  277. }else{
  278. GLsizei noOfValue = *it - lastValue; //the no of unused name(not present in the set)
  279. for( GLsizei count = 0; count < noOfValue ; count++){
  280. *textures++ = lastValue;
  281. toAssign --;
  282. pSharedTexState->usedTexNames.insert(lastValue);
  283. lastValue++;
  284. if(toAssign == 0) return GL_TRUE;
  285. }
  286. }
  287. }
  288. //some texture might be left to be assigned
  289. for( ; toAssign > 0 ; toAssign--){
  290. *textures++ = lastValue;
  291. pSharedTexState->usedTexNames.insert(lastValue);
  292. lastValue++;
  293. }
  294. return GL_TRUE;
  295. }
  296. #endif
  297. static
  298. void deleteTexImage(TextureObject* pTexObj )
  299. {
  300. Image *pImgObj = NULL;
  301. switch(pTexObj->texType)
  302. {
  303. case GL_TEXTURE_2D:
  304. for(int i = 0; i < MAX_MIPMAP_LEVELS; i++)
  305. {
  306. pImgObj = &(pTexObj->images.tex2D[i]);
  307. if(pImgObj->hImgChunk != NULL){
  308. pCA->Free(pImgObj->hImgChunk);
  309. pImgObj->hImgChunk = NULL;
  310. pImgObj->imagedataLocation = NONE;
  311. }
  312. //Resetting the values of the image structure.
  313. pImgObj->imgSize = 0;
  314. pImgObj->isUsed = GL_FALSE;
  315. pImgObj->width = 0;
  316. pImgObj->height = 0;
  317. pImgObj->depth = 0;
  318. //not needed added just to be sure
  319. pImgObj->internalFormat = GLenum(-1);
  320. pImgObj->PixType = GLenum(-1);
  321. pImgObj->nativeFormat = E_INVALID_PIXEL_FORMAT;
  322. pImgObj->isCompressed = GL_FALSE;
  323. }
  324. break;
  325. case GL_TEXTURE_CUBE_MAP:
  326. for(int j = 0 ; j < 6; j++)
  327. for(int i = 0; i < MAX_MIPMAP_LEVELS; i++)
  328. {
  329. pImgObj = &(pTexObj->images.cubeMap[j][i]);
  330. if(pImgObj->hImgChunk != NULL){
  331. pCA->Free(pImgObj->hImgChunk);
  332. pImgObj->hImgChunk = NULL;
  333. pImgObj->imagedataLocation = NONE;
  334. }
  335. //Resetting the values of the image structure.
  336. pImgObj->imgSize = 0;
  337. pImgObj->isUsed = GL_FALSE;
  338. pImgObj->width = 0;
  339. pImgObj->height = 0;
  340. pImgObj->depth = 0;
  341. //not needed added just to be sure
  342. pImgObj->internalFormat = GLenum(-1);
  343. pImgObj->PixType = GLenum(-1);
  344. pImgObj->nativeFormat = E_INVALID_PIXEL_FORMAT;
  345. pImgObj->isCompressed = GL_FALSE;
  346. }
  347. break;
  348. case GL_TEXTURE_3D:
  349. for(int i = 0; i < MAX_MIPMAP_LEVELS; i++)
  350. {
  351. pImgObj = &(pTexObj->images.tex3D[i]);
  352. if(pImgObj->hImgChunk != NULL){
  353. pCA->Free(pImgObj->hImgChunk);
  354. pImgObj->hImgChunk = NULL;
  355. pImgObj->imagedataLocation = NONE;
  356. }
  357. //Resetting the values of the image structure.
  358. pImgObj->imgSize = 0;
  359. pImgObj->isUsed = GL_FALSE;
  360. pImgObj->width = 0;
  361. pImgObj->height = 0;
  362. pImgObj->depth = 0;
  363. //not needed added just to be sure
  364. pImgObj->internalFormat = GLenum(-1);
  365. pImgObj->PixType = GLenum(-1);
  366. pImgObj->nativeFormat = E_INVALID_PIXEL_FORMAT;
  367. pImgObj->isCompressed = GL_FALSE;
  368. }
  369. break;
  370. }
  371. }
  372. inline
  373. void transferImageFromTexChunckToImageChunck(TextureObject* pTexObj)
  374. {
  375. Image *pImgObj = NULL;
  376. GLubyte* pImgBuf = NULL;
  377. if(pTexObj->texType == GL_TEXTURE_2D){
  378. int bpp =0;
  379. if(pTexObj->images.tex2D[0].isCompressed == false) bpp = pixelSize(pTexObj->nativeFormat); //TO DO ONCE SURE HOW THE MIPMAPPED COMPRESSED IMAGE ARE DEALT
  380. GLubyte* texObjImagedata = (GLubyte*) pTexObj->hChunk->GetVirtAddr();
  381. for(int level = 0; level < pTexObj->levels; level++) {
  382. pImgObj = &(pTexObj->images.tex2D[level]);
  383. pImgObj->hImgChunk = pCA->New(pImgObj->imgSize);
  384. if(pImgObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImgObj->hImgChunk->GetVirtAddr()) == NULL)
  385. {
  386. //LOGMSG("OUT OF MEMORY \n");
  387. return ;
  388. }
  389. Plat::memcpy( pImgBuf, texObjImagedata+ pTexObj->Offsets[level] * bpp, pImgObj->imgSize );
  390. pImgObj->imagedataLocation = IMAGECHUNK;
  391. pTexObj->Offsets[level] = 0;
  392. }
  393. pCA->Free(pTexObj->hChunk);
  394. pTexObj->hChunk = NULL;
  395. pTexObj->levels = 0;
  396. texObjImagedata = NULL;
  397. }else if(pTexObj->texType == GL_TEXTURE_3D){
  398. int bpp = pixelSize(pTexObj->nativeFormat);
  399. GLubyte* texObjImagedata = (GLubyte*) pTexObj->hChunk->GetVirtAddr();
  400. for(int level = 0; level < pTexObj->levels; level++) {
  401. Image* pImageObj= &(pTexObj->images.tex3D[level]);
  402. pImageObj->hImgChunk = pCA->New( pImageObj->imgSize);
  403. if(pImageObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImageObj->hImgChunk->GetVirtAddr()) == NULL)
  404. {
  405. //LOGMSG("OUT OF MEMORY \n");
  406. return ;
  407. }
  408. Plat::memcpy( pImgBuf, texObjImagedata+ pTexObj->Offsets[level] * bpp, pImgObj->imgSize );
  409. pTexObj->Offsets[level] = 0;
  410. pImgObj->imagedataLocation = IMAGECHUNK;
  411. pImageObj = NULL;
  412. }
  413. pCA->Free(pTexObj->hChunk);
  414. pTexObj->hChunk = NULL;
  415. pTexObj->levels = 0;
  416. texObjImagedata = NULL;
  417. }else if(pTexObj->texType == GL_TEXTURE_CUBE_MAP){
  418. int faceOffset = pTexObj->Offsets[pTexObj->levels - 1] + 1 * 1 ;
  419. int bpp = pixelSize(pTexObj->nativeFormat);
  420. GLubyte* texObjImagedata = (GLubyte*) pTexObj->hChunk->GetVirtAddr();
  421. for(int faceNo = 0; faceNo < 6; faceNo++){
  422. for(int level = 0; level < pTexObj->levels; level++){
  423. Image* pImageObj= &(pTexObj->images.cubeMap[faceNo][level]);
  424. pImageObj->hImgChunk = pCA->New(pImageObj->imgSize);
  425. if(pImageObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImageObj->hImgChunk->GetVirtAddr()) == NULL)
  426. {
  427. //LOGMSG("OUT OF MEMORY \n");
  428. return ;
  429. }
  430. Plat::memcpy( pImgBuf, texObjImagedata + (faceOffset * faceNo + pTexObj->Offsets[level]) * bpp, pImageObj->imgSize );
  431. pTexObj->Offsets[level] = 0;
  432. pImgObj->imagedataLocation = IMAGECHUNK;
  433. pImageObj = NULL;
  434. }
  435. }
  436. pCA->Free(pTexObj->hChunk);
  437. pTexObj->hChunk = NULL;
  438. pTexObj->levels = 0;
  439. texObjImagedata = NULL;
  440. }
  441. }
  442. //called by glCopyTex* and glTex* function. If needed memory can be allocated
  443. GLubyte* getImageDataLocation(TextureObject* pTexObj,GLint level , GLint faceNo, GLint width , GLint height ,int depth ,GLenum format, GLenum type, GLint imgSize , GLint isExt)
  444. {
  445. Image *pImgObj = NULL;
  446. GLubyte* pImgBuf = NULL;
  447. int newDepth =1;
  448. if(depth != 0) newDepth =depth; //for 3D
  449. //get the pointer to the image structure
  450. if(pTexObj->texType == GL_TEXTURE_2D){
  451. pImgObj = &(pTexObj->images.tex2D[level]);
  452. }else if(pTexObj->texType == GL_TEXTURE_3D){
  453. pImgObj = &(pTexObj->images.tex3D[level]);
  454. }else if(pTexObj->texType == GL_TEXTURE_CUBE_MAP){
  455. pImgObj = &(pTexObj->images.cubeMap[faceNo][level]);
  456. }else{
  457. gAssert(false && "undetermined texture format in function getImageDataLocation");
  458. return NULL;
  459. }
  460. //if previous was externally managed texture
  461. if(pTexObj->isExtTex == GL_TRUE){
  462. pTexObj->releaseMem();
  463. pTexObj->images.tex2D[0].imagedataLocation = NONE;
  464. }
  465. //if external managed tex image: then delete all the previous mipmap level
  466. if(isExt == GL_TRUE){
  467. pTexObj->releaseMem();
  468. deleteTexImage(pTexObj);
  469. return NULL;
  470. }
  471. //if the image data is NULL(i.e. not present in imagechunk or tex object chunck)
  472. if(pImgObj->imagedataLocation == NONE){
  473. pImgObj->hImgChunk = pCA->New(imgSize);
  474. if(pImgObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImgObj->hImgChunk->GetVirtAddr()) == NULL)
  475. {
  476. // SET_ERR(pState, GL_OUT_OF_MEMORY, "glTexImage2D");
  477. return NULL;
  478. }else{
  479. pImgObj->imagedataLocation = IMAGECHUNK;
  480. pTexObj->reCompile = GL_TRUE;
  481. return pImgBuf;
  482. }
  483. }
  484. //if the image is present in the texture chunk .
  485. if(pImgObj->imagedataLocation == TEXOBJCHUNK){
  486. //if texture format, width , heigth , depth are same then we can use the texture chunk memory itself
  487. if((pImgObj->internalFormat == format) && (pImgObj->PixType == type) &&
  488. (pImgObj->width == width) && (pImgObj->height == height) && (pImgObj->depth == depth)){
  489. if(pImgObj->isCompressed == false){
  490. int bpp = GetPixelSize(format, type); //of the format specified for the current gldrawCall
  491. unsigned int faceOffset = (pTexObj->Offsets[pTexObj->levels - 1]) + 1 * 1;
  492. return(((GLubyte*)( pTexObj->hChunk->GetVirtAddr())) +(faceNo* faceOffset * bpp) + (pTexObj->Offsets[level] * bpp));
  493. }else{
  494. //TO DO ONCE SURE HOW THE MIPMAPPED COMPRESSED IMAGE ARE DEALT
  495. return(GLubyte*)( pTexObj->hChunk->GetVirtAddr()) ;
  496. }
  497. }else{
  498. //spill back the texture image from texture chunk to the image chunk
  499. transferImageFromTexChunckToImageChunck(pTexObj);
  500. }
  501. }
  502. //if the image is present in the image chunk .This can even happen when the image has been transfered from texture chunk to image chunck ( above)
  503. if(pImgObj->imagedataLocation == IMAGECHUNK){
  504. //if image size are same we can use the same image chunk memory location //TO DO WRONG
  505. if( pImgObj->imgSize == imgSize){
  506. pTexObj->reCompile = GL_TRUE;
  507. return ((GLubyte* )pImgObj->hImgChunk->GetVirtAddr());
  508. }else{
  509. //free the previous image data and then allocate the new one
  510. pCA->Free(pImgObj->hImgChunk);
  511. pImgObj->hImgChunk = NULL;
  512. pImgObj->hImgChunk = pCA->New(imgSize);
  513. if(pImgObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImgObj->hImgChunk->GetVirtAddr()) == NULL) {
  514. //LOGMSG("OUT OF MEMORY \n");
  515. return NULL;
  516. }else{
  517. pTexObj->reCompile = GL_TRUE;
  518. return pImgBuf;
  519. }
  520. }
  521. }
  522. //LOGMSG(" should not have reached the end of the function :getImageDataLocation \n") ;
  523. return NULL;
  524. }
  525. //called by glTexSub* glCopyTexSub* function.As the memry has to be already allocated need to return the pointer
  526. GLubyte* getImageDataLocation(TextureObject* pTexObj, Image *pImgObj , GLint level , GLint faceNo)
  527. {
  528. if(pImgObj->imagedataLocation == NONE){
  529. gAssert(false && "subimage called but memory not allocated : an errror \n");
  530. }else if(pImgObj->imagedataLocation == TEXOBJCHUNK){
  531. if(pImgObj->isCompressed == false){
  532. int bpp = pixelSize(pTexObj->nativeFormat );
  533. unsigned int faceOffset = (pTexObj->Offsets[pTexObj->levels - 1]) + 1 * 1;
  534. return(((GLubyte*)( pTexObj->hChunk->GetVirtAddr())) +(faceNo* faceOffset * bpp) + (pTexObj->Offsets[level] * bpp));
  535. }else{
  536. //TO DO ONCE SURE HOW THE MIPMAPPED COMPRESSED IMAGE ARE DEALT
  537. return(GLubyte*)( pTexObj->hChunk->GetVirtAddr()) ;
  538. }
  539. }else if(pImgObj->imagedataLocation == IMAGECHUNK){
  540. return ((GLubyte* )pImgObj->hImgChunk->GetVirtAddr());
  541. }
  542. return NULL;
  543. }
  544. GLboolean isMipmapFilter(GLenum minFilter)
  545. {
  546. if((minFilter == GL_LINEAR) || (minFilter == GL_NEAREST)){
  547. return GL_FALSE;
  548. }else if((minFilter == GL_NEAREST_MIPMAP_NEAREST) ||(minFilter == GL_LINEAR_MIPMAP_NEAREST)
  549. || (minFilter == GL_NEAREST_MIPMAP_LINEAR ) || (minFilter == GL_LINEAR_MIPMAP_LINEAR)){
  550. return GL_TRUE;
  551. }else{
  552. gAssert(false && "undetermined mimfilter in function isMipmapFilter \n");
  553. return GL_TRUE;
  554. }
  555. }
  556. /*
  557. *******************************************************************************
  558. * OpenGL API functions
  559. *******************************************************************************
  560. */
  561. /*-----------------------------------------------------------------------*//*!
  562. * OpenGL active texture function.
  563. *
  564. * This function sets the active texture unit in texture state. The steps
  565. * performed are:
  566. * - Sanity checks and Set error.
  567. * - Store the index to active texture unit.
  568. *
  569. *//*------------------------------------------------------------------------*/
  570. GL_API void GL_APIENTRY
  571. glActiveTexture (GLenum texture)
  572. {
  573. OGLState* pState = GET_OGL_STATE();
  574. TextureState* pTexState = &(pState->texState);
  575. // Sanity checks and error reporting
  576. if(texture < GL_TEXTURE0 || texture >= (GL_TEXTURE0 + MAX_TEXTURE_UNITS)){
  577. SET_ERR(pState, GL_INVALID_OPERATION, "glActiveTexture");
  578. return;
  579. }
  580. // Store the index to texture unit
  581. pTexState->activeTexUnit = texture - GL_TEXTURE0;
  582. }
  583. /*-----------------------------------------------------------------------*//*!
  584. * OpenGL generate textures function.
  585. *
  586. * This function finds the free texture names and sets it in the pointer passed.
  587. * The steps performed are:
  588. * - Search the object array for free texture names and store as required.
  589. * - Record error if we don't find enough free texture names.
  590. *
  591. *//*------------------------------------------------------------------------*/
  592. GL_API void GL_APIENTRY
  593. glGenTextures (GLsizei n, GLuint *textures)
  594. {
  595. OGLState* pState = GET_OGL_STATE();
  596. SharedTextureState* psharedTexState = &(pState->sharedState->sharedTexState);
  597. GLuint *texID = textures;
  598. // Validate parameters
  599. if(n < 0 || textures == NULL){
  600. SET_ERR(pState, GL_INVALID_VALUE, "glGenTextures");
  601. return;
  602. }
  603. // Search in the object array for free textures
  604. // Texture 0 is default texture. Skip it.
  605. #if 1
  606. lockGLSharedTextureState(pState);
  607. if(getTextureNames(psharedTexState,n,textures) != GL_TRUE)
  608. {
  609. //this case is not expected to be reached
  610. SET_ERR(pState, GL_OUT_OF_MEMORY, "glGenTextures");
  611. }
  612. unlockGLSharedTextureState( pState);
  613. #else
  614. for(i = 1; i < MAX_TEXTURE_OBJECTS; i++) {
  615. if((pTexState->texObjects[i]).isUsed == GL_FALSE) {
  616. *(textures + j) = i;
  617. j++;
  618. if(j >= n) return; // We got all we wanted
  619. }
  620. }
  621. // We couldn't find the required no of free textures
  622. SET_ERR(pState, GL_OUT_OF_MEMORY, "glGenTextures");
  623. #endif
  624. while(n--)
  625. {
  626. while(psharedTexState->texObjects.count(*texID) > 0)
  627. psharedTexState->texObjects.erase(*texID);
  628. texID++;
  629. }
  630. return;
  631. }
  632. /*-----------------------------------------------------------------------*//*!
  633. * OpenGL bind texture function.
  634. *
  635. * This function creates or re-use texture objects and binds them to current
  636. * texture. The steps followed are:
  637. * - Validate input parameters.
  638. * - Check if texture object referenced is already in use. Create new object
  639. * if required.
  640. * - Check type if object already exists and raise error on mismatch. If the
  641. * texture referenced is default texture then type change will re-initialize
  642. * the default texture.
  643. *
  644. *//*------------------------------------------------------------------------*/
  645. GL_API void GL_APIENTRY
  646. glBindTexture (GLenum target, GLuint texture)
  647. {
  648. OGLState* pState = GET_OGL_STATE();
  649. TextureState* pTexState = &(pState->texState);
  650. SharedTextureState* pSharedTexState = &(pState->sharedState->sharedTexState);
  651. GLint texRef;
  652. GLuint currentUnit = pTexState->activeTexUnit;//sanvd added this
  653. TextureObject* lastTexObj = NULL;
  654. TextureObject *pTexObj;
  655. CurrentTexture *currTex = &pTexState->texUnitBinding[currentUnit];
  656. // -----------------------------------------------------------------------
  657. // Validate input parameters
  658. // -----------------------------------------------------------------------
  659. if(!(target == GL_TEXTURE_2D || target == GL_TEXTURE_3D || target == GL_TEXTURE_CUBE_MAP)){
  660. SET_ERR(pState, GL_INVALID_ENUM, "glBindTexture");
  661. return;
  662. }
  663. // -----------------------------------------------------------------------
  664. // Handle texture 0 special case
  665. // -----------------------------------------------------------------------
  666. lockGLSharedTextureState(pState);
  667. if(texture == 0) {
  668. // Texture zero has 3 different objects associated with it. One each
  669. // for different targets.
  670. switch(target) {
  671. case GL_TEXTURE_2D:
  672. //texRef = TEX_2D_DEFAULT;
  673. if(currTex->texture2D != 0){
  674. lastTexObj = GetTextureObject( target, false , 0);
  675. lastTexObj->release(pState);
  676. lastTexObj = NULL;
  677. }
  678. currTex->texture2D = texture;
  679. #if STORE_TEX_OBJ_POINTER == ENABLE
  680. pTexState->ptexUnitBinding[currentUnit].texture2D = &(pTexState->defaultTexObjects[TEX_2D_DEFAULT -TEX_2D_DEFAULT]);
  681. #endif
  682. break;
  683. case GL_TEXTURE_3D:
  684. //texRef = TEX_3D_DEFAULT;
  685. if(currTex->texture3D != 0){
  686. lastTexObj = GetTextureObject( target, false , 0);
  687. lastTexObj->release(pState);
  688. lastTexObj = NULL;
  689. }
  690. currTex->texture3D = texture;
  691. #if STORE_TEX_OBJ_POINTER == ENABLE
  692. pTexState->ptexUnitBinding[currentUnit].texture3D = &(pTexState->defaultTexObjects[TEX_3D_DEFAULT -TEX_2D_DEFAULT]);
  693. #endif
  694. break;
  695. case GL_TEXTURE_CUBE_MAP:
  696. //texRef = TEX_CUBE_MAP_DEFAULT;
  697. if(currTex->cubeMap != 0){
  698. lastTexObj = GetTextureObject( target, false , 0);
  699. lastTexObj->release(pState);
  700. lastTexObj = NULL;
  701. }
  702. currTex->cubeMap = texture;
  703. #if STORE_TEX_OBJ_POINTER == ENABLE
  704. pTexState->ptexUnitBinding[currentUnit].cubeMap = &(pTexState->defaultTexObjects[TEX_CUBE_MAP_DEFAULT -TEX_2D_DEFAULT]);
  705. #endif
  706. break;
  707. }
  708. //default textures are not shared
  709. // -----------------------------------------------------------------------
  710. // Handle other textures
  711. // -----------------------------------------------------------------------
  712. }
  713. else
  714. {
  715. texRef = GetTexNameArrayIndex(texture,false);
  716. if(texRef ==-1){
  717. SET_ERR(pState, GL_INVALID_VALUE, "glBindTexture");
  718. unlockGLSharedTextureState(pState);
  719. return;
  720. }
  721. // New texture object
  722. pTexObj = pSharedTexState->texObjects[texRef];
  723. if(pTexObj != NULL)
  724. {
  725. if(pTexObj->isUsed != GL_TRUE) {
  726. // Initialize, mark used
  727. pTexObj->Init(texture, target);
  728. //reaching inside the loop means that this is the first time the texture has been called by glBindTexture
  729. // so it might have been called using the glGenTexture or without it
  730. //this if condition can be removed as set contain only 1 copy of the value
  731. if(pSharedTexState->usedTexNames.count(texture) == 0){
  732. pSharedTexState->usedTexNames.insert(texture);
  733. }
  734. // Texture object reuse
  735. } else if(pTexObj->texType != target){
  736. // Raise error if type mismatches
  737. SET_ERR(pState, GL_INVALID_OPERATION, "glBindTexture");
  738. unlockGLSharedTextureState(pState);
  739. return;
  740. }
  741. // for reference count of the texture objects
  742. // & Store current texture value
  743. switch(target) {
  744. case GL_TEXTURE_2D:
  745. if(currTex->texture2D != texture)
  746. {
  747. if(currTex->texture2D != 0){
  748. lastTexObj = GetTextureObject( target, false , 0);
  749. lastTexObj->release(pState);
  750. lastTexObj = NULL;
  751. }
  752. pTexObj->acquire(pState);
  753. currTex->texture2D = texture;
  754. #if STORE_TEX_OBJ_POINTER == ENABLE
  755. pTexState->ptexUnitBinding[currentUnit].texture2D = (pSharedTexState->texObjects[texRef]);
  756. #endif
  757. }
  758. break;
  759. case GL_TEXTURE_3D:
  760. if(currTex->texture3D != texture){
  761. if(currTex->texture3D != 0){
  762. lastTexObj = GetTextureObject( target, false , 0);
  763. lastTexObj->release(pState);
  764. lastTexObj = NULL;
  765. }
  766. pTexObj->acquire(pState);
  767. currTex->texture3D = texture;
  768. #if STORE_TEX_OBJ_POINTER == ENABLE
  769. pTexState->ptexUnitBinding[currentUnit].texture3D = (pSharedTexState->texObjects[texRef]);
  770. #endif
  771. }
  772. break;
  773. case GL_TEXTURE_CUBE_MAP:
  774. if(currTex->cubeMap != texture){
  775. if(currTex->cubeMap != 0){
  776. lastTexObj = GetTextureObject( target, false , 0);
  777. lastTexObj->release(pState);
  778. lastTexObj = NULL;
  779. }
  780. pTexObj->acquire(pState);
  781. currTex->cubeMap = texture;
  782. #if STORE_TEX_OBJ_POINTER == ENABLE
  783. pTexState->ptexUnitBinding[currentUnit].cubeMap = (pSharedTexState->texObjects[texRef]);
  784. #endif
  785. }
  786. break;
  787. };
  788. }
  789. }
  790. unlockGLSharedTextureState(pState);
  791. #if 0
  792. // -----------------------------------------------------------------------
  793. // Store current texture value
  794. // -----------------------------------------------------------------------
  795. switch(target) {
  796. case GL_TEXTURE_2D:
  797. pTexState->texUnitBinding[currentUnit].texture2D = texture;
  798. break;
  799. case GL_TEXTURE_3D:
  800. pTexState->texUnitBinding[currentUnit].texture3D = texture;
  801. break;
  802. case GL_TEXTURE_CUBE_MAP:
  803. pTexState->texUnitBinding[currentUnit].cubeMap = texture;
  804. break;
  805. };
  806. #endif
  807. }
  808. /*-----------------------------------------------------------------------*//*!
  809. * OpenGL delete texture function.
  810. *//*------------------------------------------------------------------------*/
  811. GL_API void GL_APIENTRY
  812. glDeleteTextures (GLsizei n, const GLuint *textures)
  813. {
  814. OGLState* pState = GET_OGL_STATE();
  815. TextureState* pTexState = &(pState->texState);
  816. GLuint currentUnit = pTexState->activeTexUnit;
  817. int i = 0 ; //for count of the no of texture unit
  818. // Validate parameters
  819. if(n < 0 || textures == NULL){
  820. SET_ERR(pState, GL_INVALID_VALUE, "glDeleteTextures");
  821. return;
  822. }
  823. lockGLSharedTextureState(pState);
  824. while(n--){
  825. if(*textures == 0){
  826. continue; //silently ignore 0
  827. }
  828. GLint texRef = GetTexNameArrayIndex(*textures,false);//only delete userdefined tex objects..operation is heavy.....O( n )
  829. SharedTextureState *pSTState = &(pState->sharedState->sharedTexState);
  830. TextureObject* pTexObj = pSTState->texObjects[texRef];
  831. if((texRef == -1) || (pTexObj->isUsed == GL_FALSE))//it means that texture is not found in the list and that textureObject array is fulluy populated...
  832. //if((texRef == -1) || (pState->sharedState->sharedTexState.texObjects[texRef]->isUsed == GL_FALSE))//it means that texture is not found in the list and that textureObject array is fulluy populated...
  833. {
  834. //Sandeep K. spec says silently ignore 0 and invalid texture names
  835. //SET_ERR(pState, GL_INVALID_VALUE, "glDeleteTexture");
  836. // unlockGLSharedTextureState(pState);
  837. //return;
  838. continue; //it should try tio delete the other texture object
  839. }
  840. //check the reference count before deleting
  841. //the reference count need to be decreased for all the texture unit that is using the f=given texture object
  842. pTexObj->deleteTexObj = GL_TRUE;
  843. switch(pTexObj->texType){
  844. case GL_TEXTURE_2D:
  845. for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
  846. if ((pTexObj->id == pTexState->texUnitBinding[i].texture2D)
  847. && (pTexObj->id !=0)){
  848. pTexState->texUnitBinding[i].texture2D = 0;
  849. #if STORE_TEX_OBJ_POINTER == ENABLE
  850. pTexState->ptexUnitBinding[currentUnit].texture2D =
  851. &(pTexState->defaultTexObjects[TEX_2D_DEFAULT
  852. -TEX_2D_DEFAULT]);
  853. #endif
  854. int id = pTexObj->id;
  855. pSTState->ReleaseTexObj(pState, pTexObj->id) ;
  856. }
  857. }
  858. //if the object is not bounded to any texture object
  859. if( pTexObj->id != 0 && pSTState->texObjects.count(texRef)){
  860. int id = pTexObj->id;
  861. pSTState->ReleaseTexObj(pState, pTexObj->id);
  862. }
  863. pTexObj = NULL;
  864. //pSTState->texObjects[texRef] = NULL;
  865. break;
  866. case GL_TEXTURE_3D:
  867. for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
  868. if ((pTexObj->id == pTexState->texUnitBinding[i].texture3D)
  869. && (pTexObj->id !=0)){
  870. pTexState->texUnitBinding[i].texture3D = 0;
  871. #if STORE_TEX_OBJ_POINTER == ENABLE
  872. pTexState->ptexUnitBinding[currentUnit].texture3D =
  873. &(pTexState->defaultTexObjects[TEX_3D_DEFAULT
  874. -TEX_2D_DEFAULT]);
  875. #endif
  876. pSTState->ReleaseTexObj(pState, pTexObj->id);
  877. }
  878. }
  879. if( pTexObj->id != 0 && pSTState->texObjects.count(texRef)){
  880. pSTState->ReleaseTexObj(pState, pTexObj->id);
  881. }
  882. pTexObj = NULL;
  883. //pSTState->texObjects[texRef] = NULL;
  884. break;
  885. case GL_TEXTURE_CUBE_MAP:
  886. for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
  887. if( (pTexObj->id == pTexState->texUnitBinding[i].cubeMap)
  888. && (pTexObj->id !=0)){
  889. pTexState->texUnitBinding[i].cubeMap= 0;
  890. #if STORE_TEX_OBJ_POINTER == ENABLE
  891. pTexState->ptexUnitBinding[currentUnit].cubeMap =
  892. &(pTexState->defaultTexObjects[TEX_CUBE_MAP_DEFAULT
  893. -TEX_2D_DEFAULT]);
  894. #endif
  895. pSTState->ReleaseTexObj(pState, pTexObj->id);
  896. }
  897. }
  898. if( pTexObj->id != 0 && pSTState->texObjects.count(texRef)){
  899. pSTState->ReleaseTexObj(pState, pTexObj->id);
  900. }
  901. pTexObj = NULL;
  902. //pSTState->texObjects[texRef] = NULL;
  903. break;
  904. }
  905. #if 0
  906. //need to unbound from all texture unit
  907. switch(pTexState->psharedTexObjects->ptexObjects[texRef].texType ){
  908. case GL_TEXTURE_2D:
  909. for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
  910. if (*textures == pTexState->texUnitBinding[i].texture2D){
  911. pTexState->texUnitBinding[i].texture2D = 0;
  912. }
  913. }
  914. break;
  915. case GL_TEXTURE_3D:
  916. for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
  917. if (*textures == pTexState->texUnitBinding[i].texture3D){
  918. pTexState->texUnitBinding[i].texture3D = 0;
  919. }
  920. }
  921. break;
  922. case GL_TEXTURE_CUBE_MAP:
  923. for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
  924. if (*textures == pTexState->texUnitBinding[i].cubeMap){
  925. pTexState->texUnitBinding[i].cubeMap= 0;
  926. }
  927. }
  928. break;
  929. }
  930. if(*textures == pTexState->texUnitBinding[currentUnit].texture2D)
  931. pTexState->texUnitBinding[currentUnit].texture2D = 0;
  932. if(*textures == pTexState->texUnitBinding[currentUnit].texture3D)
  933. pTexState->texUnitBinding[currentUnit].texture3D = 0;
  934. if(*textures == pTexState->texUnitBinding[currentUnit].cubeMap)
  935. pTexState->texUnitBinding[currentUnit].cubeMap= 0;
  936. // Delete texture object
  937. if(*textures != 0 && pTexState->psharedTexObjects->ptexObjects[texRef].isUsed == GL_TRUE){
  938. pTexState->psharedTexObjects->ptexObjects[texRef].Delete();
  939. }
  940. //to remove form the used texture name set
  941. if(pTexState->psharedTexObjects->usedTexNames.count(*textures) != 0){
  942. pTexState->psharedTexObjects->usedTexNames.erase(*textures);
  943. }
  944. #endif
  945. //Sandeep K.
  946. //FBO spec says that is a texture is bound to the currently bound fbo it must be detached from all attachment points
  947. // it is attached to.
  948. pState->framebuffState.detachTexture(*textures);
  949. textures++; // Next object
  950. }
  951. unlockGLSharedTextureState(pState);
  952. }
  953. /*-----------------------------------------------------------------------*//*!
  954. * OpenGL IsTexture function.
  955. *//*------------------------------------------------------------------------*/
  956. GL_API GLboolean GL_APIENTRY
  957. glIsTexture (GLuint texture)
  958. {
  959. OGLState* pState = GET_OGL_STATE();
  960. // Validate parameters
  961. if(texture >= MAX_TEXTURE_OBJECTS){
  962. SET_ERR(pState, GL_INVALID_VALUE, "glIsTexture");
  963. return GL_FALSE;
  964. }
  965. GLint texRef = GetTexNameArrayIndex(texture,false);//only checks the non-default tex objs//operation is heavy.....O( n )
  966. if(texRef == -1) //it means that texture is not found in the list and that textureObject array is fulluy populated...
  967. return GL_FALSE;
  968. if(pState->sharedState->sharedTexState.texObjects[texRef]->isUsed == GL_TRUE)
  969. return GL_TRUE;
  970. else
  971. return GL_FALSE;
  972. }
  973. /*-----------------------------------------------------------------------*//*!
  974. * OpenGL texture parameter function (int).
  975. *//*------------------------------------------------------------------------*/
  976. GL_API void GL_APIENTRY
  977. glTexParameteri (GLenum target, GLenum pname, GLint param)
  978. {
  979. OGLState* pState = GET_OGL_STATE();
  980. TextureObject* pTexObj = NULL;
  981. //GLuint texRef;
  982. //Sandeep
  983. GLenum eParam = (GLenum)(param);
  984. // Select object reference according to target
  985. // Return error on invalid enum
  986. if(target == GL_TEXTURE_2D || target == GL_TEXTURE_3D || target == GL_TEXTURE_CUBE_MAP){
  987. pTexObj = GetTextureObject(target,false,0);
  988. } else {
  989. SET_ERR(pState, GL_INVALID_ENUM, "glTexParameter*");
  990. return;
  991. }
  992. GLboolean isMipmapFilterPrevious = isMipmapFilter(pTexObj->minFilter);
  993. // Store the parameter passed in texture state
  994. // Return error on invalid enum or value
  995. switch(pname) {
  996. case GL_TEXTURE_MIN_FILTER:
  997. if((eParam == GL_LINEAR) || (eParam == GL_NEAREST) || (eParam == GL_LINEAR_MIPMAP_LINEAR)
  998. || (eParam == GL_LINEAR_MIPMAP_NEAREST) || (eParam == GL_NEAREST_MIPMAP_LINEAR) ||
  999. (eParam == GL_NEAREST_MIPMAP_NEAREST)) {
  1000. pTexObj->minFilter = eParam;
  1001. break;
  1002. } else {
  1003. SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
  1004. return;
  1005. }
  1006. case GL_TEXTURE_MAG_FILTER:
  1007. if((eParam == GL_LINEAR) || (eParam == GL_NEAREST)){
  1008. pTexObj->magFilter = eParam;
  1009. break;
  1010. } else {
  1011. SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
  1012. return;
  1013. }
  1014. case GL_TEXTURE_WRAP_S:
  1015. if((eParam == GL_REPEAT) || (eParam == GL_CLAMP_TO_EDGE) || (eParam == GL_MIRRORED_REPEAT)) {
  1016. pTexObj->wrapS = eParam;
  1017. break;
  1018. } else {
  1019. SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
  1020. return;
  1021. }
  1022. case GL_TEXTURE_WRAP_T:
  1023. if((eParam == GL_REPEAT) || (eParam == GL_CLAMP_TO_EDGE) || (eParam == GL_MIRRORED_REPEAT)) {
  1024. pTexObj->wrapT = eParam;
  1025. break;
  1026. } else {
  1027. SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
  1028. return;
  1029. }
  1030. case GL_TEXTURE_WRAP_R:
  1031. if(target != GL_TEXTURE_3D) {
  1032. SET_ERR(pState, GL_INVALID_ENUM, "glTexParameter*");
  1033. return;
  1034. }
  1035. if((eParam == GL_REPEAT) || (eParam == GL_CLAMP_TO_EDGE) || (eParam == GL_MIRRORED_REPEAT)) {
  1036. pTexObj->wrapR = eParam;
  1037. break;
  1038. } else {
  1039. SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
  1040. return;
  1041. }
  1042. default:
  1043. SET_ERR(pState, GL_INVALID_ENUM, "glTexParameter*");
  1044. return;
  1045. };
  1046. GLboolean isMipmapFilterPresent = isMipmapFilter(pTexObj->minFilter);
  1047. if(isMipmapFilterPresent != isMipmapFilterPrevious){
  1048. pTexObj->reCompile = GL_TRUE;
  1049. }
  1050. pTexObj->isDirtyState = GL_TRUE;
  1051. }
  1052. /*-----------------------------------------------------------------------*//*!
  1053. * OpenGL texture parameter function (intv).
  1054. *
  1055. * See glTexParameteri() for details regarding glTexParameter* functions.
  1056. *
  1057. *//*------------------------------------------------------------------------*/
  1058. GL_API void GL_APIENTRY
  1059. glTexParameteriv (GLenum target, GLenum pname, const GLint *params)
  1060. {
  1061. OGLState* pState = GET_OGL_STATE();
  1062. if(params == NULL) {
  1063. SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
  1064. } else {
  1065. //Texture OES API - 2009.05.20
  1066. //#ifndef GL_OES_draw_texture
  1067. // glTexParameteri(EXPCTX_LOCAL target, pname, *params);
  1068. //#else
  1069. if ((target == GL_TEXTURE_2D) && (pname == GL_TEXTURE_CROP_RECT_OES)){
  1070. TextureObject* pTexObj = NULL;
  1071. pTexObj = GetTextureObject(target,false,0);
  1072. // Plat::printf ("\nGL_TEXTURE_CROP_RECT_OES is called.\n");
  1073. pTexObj->crop_rect[0] = params[0];
  1074. pTexObj->crop_rect[1] = params[1];
  1075. pTexObj->crop_rect[2] = params[2];
  1076. pTexObj->crop_rect[3] = params[3];
  1077. }else{
  1078. glTexParameteri( target, pname, *params);
  1079. }
  1080. //#endif //GL_OES_draw_texture
  1081. }
  1082. }
  1083. /*-----------------------------------------------------------------------*//*!
  1084. * OpenGL texture parameter function (float).
  1085. *
  1086. * See glTexParameteri() for details regarding glTexParameter* functions.
  1087. *
  1088. *//*------------------------------------------------------------------------*/
  1089. GL_API void GL_APIENTRY
  1090. glTexParameterf (GLenum target, GLenum pname, GLfloat param)
  1091. {
  1092. glTexParameteri(target, pname, (GLint)param);
  1093. }
  1094. /*-----------------------------------------------------------------------*//*!
  1095. * OpenGL texture parameter function (floatv).
  1096. *
  1097. * See glTexParameteri() for details regarding glTexParameter* functions.
  1098. *
  1099. *//*------------------------------------------------------------------------*/
  1100. GL_API void GL_APIENTRY
  1101. glTexParameterfv (GLenum target, GLenum pname, const GLfloat *params)
  1102. {
  1103. OGLState* pState = GET_OGL_STATE();
  1104. if(params == NULL) {
  1105. SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
  1106. } else {
  1107. //Texture OES API - 2009.05.20
  1108. //#ifndef GL_OES_draw_texture
  1109. // glTexParameteri(EXPCTX_LOCAL target, pname, *params);
  1110. //#else
  1111. if ((target == GL_TEXTURE_2D) && (pname == GL_TEXTURE_CROP_RECT_OES)){
  1112. TextureObject* pTexObj = NULL;
  1113. pTexObj = GetTextureObject(target,false,0);
  1114. // Plat::printf ("\nGL_TEXTURE_CROP_RECT_OES is called.\n");
  1115. pTexObj->crop_rect[0] = params[0];
  1116. pTexObj->crop_rect[1] = params[1];
  1117. pTexObj->crop_rect[2] = params[2];
  1118. pTexObj->crop_rect[3] = params[3];
  1119. }else{
  1120. glTexParameteri( target, pname, *params);
  1121. }
  1122. //#endif //GL_OES_draw_texture
  1123. }
  1124. }
  1125. /* the function will determine the TextureFormat depeneding on the value of
  1126. internalformat & type.this value will be used internal by the library
  1127. the user TextureFormat is given by the srcTextFormat &
  1128. the final format in which the texture data will be used is returned */
  1129. PxFmt DetermineTextureFormat(GLenum pFormat, GLenum pType, PxFmt* srcTextFormat)
  1130. {
  1131. switch(pType)
  1132. {
  1133. case GL_UNSIGNED_BYTE:
  1134. {
  1135. switch(pFormat)
  1136. {
  1137. case GL_LUMINANCE:
  1138. *srcTextFormat = E_LUMINANCE8;
  1139. return E_LUMINANCE_ALPHA80;
  1140. case GL_ALPHA:
  1141. *srcTextFormat = E_ALPHA8;
  1142. return E_LUMINANCE_ALPHA08;
  1143. case GL_LUMINANCE_ALPHA:
  1144. *srcTextFormat = E_LUMINANCE_ALPHA88;
  1145. return E_LUMINANCE_ALPHA88; //TE_AI88;
  1146. case GL_RGB:
  1147. *srcTextFormat = E_BGR8;
  1148. return E_ARGB0888 ; //E_ARGB8; //TODO: or should this be E_ARGB0888
  1149. case GL_RGBA:
  1150. *srcTextFormat = E_ABGR8;
  1151. return E_ARGB8 ; //TE_ARGB8888;
  1152. case GL_BGRA:
  1153. *srcTextFormat = E_ARGB8;
  1154. return E_ARGB8 ; //TE_ARGB8888;
  1155. }
  1156. }
  1157. break;
  1158. case GL_UNSIGNED_SHORT_4_4_4_4:
  1159. *srcTextFormat = E_RGBA4;
  1160. return E_ARGB4; // changed as alpha location value is 0
  1161. case GL_UNSIGNED_SHORT_5_6_5:
  1162. *srcTextFormat = E_RGB565;
  1163. return E_RGB565; //TE_RGB565;
  1164. case GL_UNSIGNED_SHORT_5_5_5_1:
  1165. *srcTextFormat = E_RGBA5551;
  1166. return E_ARGB1555; // changed as alpha location value is 0
  1167. case GL_PALETTE4_RGB8_OES:
  1168. case GL_PALETTE8_RGB8_OES:
  1169. *srcTextFormat = E_BGR8;
  1170. return E_ARGB8;
  1171. case GL_PALETTE4_RGBA8_OES:
  1172. case GL_PALETTE8_RGBA8_OES:
  1173. *srcTextFormat = E_ABGR8;
  1174. return E_ARGB8;
  1175. case GL_PALETTE4_RGBA4_OES:
  1176. case GL_PALETTE8_RGBA4_OES:
  1177. *srcTextFormat = E_RGBA4;
  1178. return E_ARGB8;
  1179. case GL_PALETTE4_R5_G6_B5_OES:
  1180. case GL_PALETTE8_R5_G6_B5_OES:
  1181. *srcTextFormat = E_RGB565;
  1182. return E_ARGB8;
  1183. case GL_PALETTE8_RGB5_A1_OES:
  1184. case GL_PALETTE4_RGB5_A1_OES:
  1185. *srcTextFormat = E_RGBA5551;
  1186. return E_ARGB8;
  1187. default:
  1188. return E_INVALID_PIXEL_FORMAT;
  1189. }
  1190. return E_INVALID_PIXEL_FORMAT; // added to remove warning
  1191. }//DetermineTextureFormat()
  1192. /*cheks whether the current texture is alpha texture , Luminance texture or RGB texture. Only support for GLES1.1. implemented for
  1193. GL_TEXTURE_2D only.*/
  1194. extern "C" GLboolean isAlphaorLuminanceRGBTextureEXP (GLenum texture, GLboolean alpha)
  1195. {
  1196. OGLState* pState = GET_OGL_STATE();
  1197. TextureObject* pTexObj =NULL;
  1198. Image *pImgObj = NULL;
  1199. GLint texUnit = 0;
  1200. if(texture < GL_TEXTURE0 || texture >= (GL_TEXTURE0 + MAX_TEXTURE_UNITS)){
  1201. SET_ERR(pState, GL_INVALID_OPERATION, "glIsAlphaTextureEXP");
  1202. return false;
  1203. }
  1204. texUnit = texture - GL_TEXTURE0;
  1205. pTexObj = GetTextureObject(GL_TEXTURE_2D,true,texUnit);
  1206. pImgObj = &(pTexObj->images.tex2D[0]); // for level = 0
  1207. if (alpha == 1){
  1208. if (pImgObj->internalFormat != GL_ALPHA){
  1209. return false;
  1210. }else{
  1211. // Plat::printf("\n %s ::Current bound texture is alpha texture \n", __FUNCTION__);
  1212. return true;
  1213. }
  1214. }
  1215. else{
  1216. if (pImgObj->internalFormat == GL_LUMINANCE || pImgObj->internalFormat == GL_RGB){
  1217. return true;
  1218. }else{
  1219. return false;
  1220. }
  1221. }
  1222. }
  1223. void convertPixelsTexure( PxFmt dstFmt, void* dstPixels, GLsizei width, GLsizei height,
  1224. PxFmt srcFmt,const void* srcPixels);
  1225. /*-----------------------------------------------------------------------*//*!
  1226. * OpenGL specify 2D texture function.
  1227. *//*------------------------------------------------------------------------*/
  1228. #ifdef USE_3D_PM
  1229. void PM_glTexImage2D (GLenum target, GLint level, GLenum internalformat, GLsizei width,
  1230. GLsizei height, GLint border, GLenum format, GLenum type,
  1231. const void *pixels);
  1232. GL_API void GL_APIENTRY
  1233. glTexImage2D (GLenum target, GLint level, GLenum internalformat, GLsizei width,
  1234. GLsizei height, GLint border, GLenum format, GLenum type,
  1235. const void *pixels)
  1236. {
  1237. lock3DCriticalSection();
  1238. PM_glTexImage2D ( target, level, internalformat, width, height, border, format, type, pixels);
  1239. unlock3DCriticalSection();
  1240. }
  1241. void PM_glTexImage2D (GLenum target, GLint level, GLenum internalformat, GLsizei width,
  1242. GLsizei height, GLint border, GLenum format, GLenum type,
  1243. const void *pixels)
  1244. #else
  1245. GL_API void GL_APIENTRY
  1246. glTexImage2D (GLenum target, GLint level, GLenum internalformat, GLsizei width,
  1247. GLsizei height, GLint border, GLenum format, GLenum type,
  1248. const void *pixels)
  1249. #endif
  1250. {
  1251. OGLState* pState = GET_OGL_STATE();
  1252. //TextureState* pTexState = &(pState->texState);
  1253. //GLuint texRef; // Reference to texture object
  1254. GLenum texFormatError; // Internal texture format
  1255. GLsizei pixSize; // Bytes per pixel
  1256. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  1257. Image *pImgObj = NULL; // Pointer to image object
  1258. GLuint cubeFace = 0; // Index of cube face refered
  1259. PxFmt DstTextureFormat ; /* the texture format in which the data will be send */
  1260. PxFmt srcTextureFormat ; /* source texture format */
  1261. //GLenum tmpPixelSize ; /*temporary to check if value is same or not*/
  1262. //GLubyte *pTempImgBuf = NULL;
  1263. //GLuint isImagedata = 1;
  1264. TextureObject* pTexObj =NULL;
  1265. //-------------------------------------------------------------------------
  1266. // Get texture object and VALIDATE INPUT PARAMETERS
  1267. //-------------------------------------------------------------------------
  1268. // Check MipMap level
  1269. if(level < 0 || level >= MAX_MIPMAP_LEVELS) {
  1270. SET_ERR(pState, GL_INVALID_VALUE, "glTexImage2D");
  1271. return;
  1272. };
  1273. // Select object reference according to target
  1274. // Return error on invalid enum
  1275. if(target == GL_TEXTURE_2D){
  1276. pTexObj = GetTextureObject(GL_TEXTURE_2D,false,0);
  1277. pImgObj = &(pTexObj->images.tex2D[level]);
  1278. } else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
  1279. pTexObj = GetTextureObject( GL_TEXTURE_CUBE_MAP,false,0);
  1280. cubeFace = (target - GL_TEXTURE_CUBE_MAP_POSITIVE_X);
  1281. pImgObj = &(pTexObj->images.cubeMap[cubeFace][level]);
  1282. } else {
  1283. SET_ERR(pState, GL_INVALID_ENUM, "glTexImage2D*");
  1284. return;
  1285. }
  1286. // Check width and height should be less than zero or greater than GL_MAX_TEXTURE_SIZE????
  1287. if(width > (1 << (MAX_MIPMAP_LEVELS-1)) || height > (1 << (MAX_MIPMAP_LEVELS-1))
  1288. || width < 0 || height < 0 ) {
  1289. SET_ERR(pState, GL_INVALID_VALUE, "glTexImage2D");
  1290. return;
  1291. }
  1292. //in case of cubemap width and height should be same
  1293. if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z){
  1294. if(width != height) {
  1295. SET_ERR(pState, GL_INVALID_VALUE, "glTexImage2D");
  1296. return;
  1297. }
  1298. }
  1299. // Border not supported in ES
  1300. if(border != 0) {
  1301. SET_ERR(pState, GL_INVALID_VALUE, "glTexImage2D");
  1302. return;
  1303. }
  1304. //check the texture format and internal format ?????TO DO
  1305. texFormatError = checkTextureFormat(pState, internalformat, format);
  1306. if(texFormatError != GL_NO_ERROR) {
  1307. SET_ERR(pState, texFormatError, "glTexImage2D");
  1308. return;
  1309. }
  1310. // check the type and format combination supported
  1311. if(CheckFormatTypeCombination(internalformat,type) == 1){
  1312. SET_ERR(pState, GL_INVALID_OPERATION, "glTexImage2D");
  1313. return;
  1314. }
  1315. DstTextureFormat = DetermineTextureFormat(internalformat, type, &srcTextureFormat);
  1316. pImgObj->nativeFormat = DstTextureFormat;
  1317. //TO DO WHEN WITDTH 0 AND HEIGHT IS 0
  1318. //FLUSHING THE CACHE
  1319. FimgFinish(pState);
  1320. //-------------------------------------------------------------------------
  1321. // COPY TEXTURE DATA
  1322. //-------------------------------------------------------------------------
  1323. pixSize = pixelSize(DstTextureFormat);
  1324. if(pixSize <= 0) {
  1325. return;
  1326. }
  1327. pImgBuf = getImageDataLocation( pTexObj, level , cubeFace, width , height , 0 ,internalformat, type, width * height * pixSize, 0);
  1328. if(pImgBuf == NULL){
  1329. gAssert(false && " unbale to get the memory location \n");
  1330. return;
  1331. }
  1332. // we need to copy only if pixel data from main memory is not null...
  1333. if(pixels != NULL)
  1334. {
  1335. /*convertPixels(DstTextureFormat,(void*)pImgBuf, width, height,0,0,0,0,
  1336. srcTextureFormat, (void*) pixels, width, height,0,0,0,0,
  1337. width,height,0,0,0,0);*/
  1338. convertPixelsTexure(DstTextureFormat,(void*)pImgBuf, width, height,
  1339. srcTextureFormat, (void*) pixels);
  1340. }
  1341. #ifdef CACHE_MEM
  1342. pCA->cache_clean_invalid(pImgBuf, width * height * pixSize);
  1343. #endif
  1344. //-------------------------------------------------------------------------
  1345. //SET IMAGE PARAMETERS
  1346. //-------------------------------------------------------------------------
  1347. pImgObj->isUsed = GL_TRUE;
  1348. pImgObj->width = width;
  1349. pImgObj->height = height;
  1350. pImgObj->internalFormat = internalformat;
  1351. pImgObj->PixType = type;
  1352. pImgObj->imgSize = width * height * pixSize;
  1353. pImgObj->isCompressed = false;
  1354. pTexObj->isDirtyState = GL_TRUE; // Set dirty flag
  1355. }
  1356. #ifdef USE_3D_PM
  1357. void PM_glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
  1358. GL_API void GL_APIENTRY glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
  1359. {
  1360. lock3DCriticalSection();
  1361. PM_glTexSubImage2D ( target, level, xoffset, yoffset, width, height, format, type, pixels);
  1362. unlock3DCriticalSection();
  1363. }
  1364. void PM_glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
  1365. #else
  1366. GL_API void GL_APIENTRY glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)
  1367. #endif
  1368. {
  1369. OGLState* pState = GET_OGL_STATE();
  1370. // LOGE("!!!!!!!!!!!!!!!!!!!!glTexSubImage2D!!!!!!!!!!!!!!!!!!!!");
  1371. //GLuint texRef; // Reference to texture object
  1372. //GLenum texFormat; // Internal texture format
  1373. //GLsizei pixelSize; // Bytes per pixel
  1374. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  1375. Image *pImgObj = NULL; // Pointer to image object
  1376. GLuint cubeFace = 0; // Index of cube face refered
  1377. TextureObject* pTexObj;
  1378. //GLuint isImagedata = 1;
  1379. PxFmt DstTextureFormat ; // the texture format in which the data will be send
  1380. PxFmt srcTextureFormat ; // source texture format
  1381. //-------------------------------------------------------------------------
  1382. // VALIDATE INPUT PARAMETERS
  1383. //-------------------------------------------------------------------------
  1384. if(pixels == NULL) {
  1385. //SET_ERR(pState, GL_INVALID_VALUE, "glTexSubImage2D"); no need for a error...Just return without modifying the original texture object
  1386. return;
  1387. }
  1388. // Check MipMap level
  1389. if( level < 0 || level >= MAX_MIPMAP_LEVELS )
  1390. {
  1391. SET_ERR(pState, GL_INVALID_VALUE, "glTexSubImage2D");
  1392. return;
  1393. }
  1394. if(target == GL_TEXTURE_2D){
  1395. pTexObj = GetTextureObject( target,false,0);
  1396. pImgObj = &(pTexObj->images.tex2D[level]);
  1397. } else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
  1398. pTexObj = GetTextureObject( GL_TEXTURE_CUBE_MAP,false,0);
  1399. cubeFace = (target - GL_TEXTURE_CUBE_MAP_POSITIVE_X);
  1400. pImgObj = &(pTexObj->images.cubeMap[cubeFace][level]);
  1401. } else {
  1402. SET_ERR(pState, GL_INVALID_ENUM, "glTexSubImage2D*");
  1403. return;
  1404. }
  1405. //if texsubimage is called before calling glteximage function .
  1406. if(pImgObj->imagedataLocation == NONE){
  1407. SET_ERR(pState, GL_INVALID_OPERATION, "glTexSubImage2D");
  1408. return;
  1409. }
  1410. if( xoffset < 0 || yoffset < 0 || (xoffset + width) > pImgObj->width
  1411. ||
  1412. (yoffset + height) > pImgObj->height || width < 0 || height < 0
  1413. )
  1414. {
  1415. SET_ERR(pState, GL_INVALID_VALUE, "glTexSubImage2D");
  1416. return;
  1417. }
  1418. if(format != pImgObj->internalFormat || type != pImgObj->PixType) //format of the image should be identical to the internal format of the texture object created...
  1419. {
  1420. SET_ERR(pState, GL_INVALID_ENUM, "glTexSubImage2D");
  1421. return;
  1422. }
  1423. DstTextureFormat = DetermineTextureFormat(format,type, &srcTextureFormat );
  1424. //FLUSHING THE CACHE
  1425. FimgFinish(pState);
  1426. //-------------------------------------------------------------------------
  1427. // COPY TEXTURE DATA
  1428. //-------------------------------------------------------------------------
  1429. pImgBuf = getImageDataLocation( pTexObj, pImgObj, level , cubeFace);
  1430. if(pImgBuf == NULL){
  1431. gAssert(false && " unbale to get the memory location \n");
  1432. return;
  1433. }
  1434. #if 0 //liyue 090527
  1435. convertPixels(DstTextureFormat,(void*)pImgBuf, pImgObj->width, pImgObj->height,0,xoffset,yoffset,0,
  1436. srcTextureFormat, (void*) pixels, width, height,0,0,0,0,
  1437. width,height,0,0,0,0);
  1438. #else
  1439. convertPixelsSubTexture(DstTextureFormat, (void*)pImgBuf,pImgObj->width,pImgObj->height, xoffset, yoffset,srcTextureFormat,(void*) pixels, width, height);
  1440. #endif
  1441. #ifdef CACHE_MEM
  1442. pCA->cache_clean_invalid(pImgBuf, pImgObj->imgSize);
  1443. #endif
  1444. pImgObj->isUsed = GL_TRUE; //do we really need these?
  1445. pTexObj->isDirtyState = GL_TRUE;
  1446. }
  1447. #ifdef USE_3D_PM
  1448. void PM_glCopyTexImage2D (GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border);
  1449. GL_API void GL_APIENTRY glCopyTexImage2D (GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
  1450. {
  1451. lock3DCriticalSection();
  1452. PM_glCopyTexImage2D ( target, level, internalformat, x, y, width, height, border);
  1453. unlock3DCriticalSection();
  1454. }
  1455. void PM_glCopyTexImage2D (GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
  1456. #else
  1457. GL_API void GL_APIENTRY glCopyTexImage2D (GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
  1458. #endif
  1459. {
  1460. OGLState* pState = GET_OGL_STATE();
  1461. //GLuint texRef; // Reference to texture object
  1462. GLenum texFormatError; // Internal texture format
  1463. GLsizei pixSize; // Bytes per pixel
  1464. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  1465. Image *pImgObj = NULL; // Pointer to image object
  1466. GLuint cubeFace = 0; // Index of cube face refered
  1467. //GLubyte *pTempImgBuf = NULL;
  1468. //GLuint isImagedata = 1;
  1469. TextureObject* pTexObj =NULL;
  1470. //PxFmt srcTextureFormat ; // the texture format in which the data will be send
  1471. //-------------------------------------------------------------------------
  1472. // VALIDATE INPUT PARAMETERS
  1473. //-------------------------------------------------------------------------
  1474. texFormatError = checkTextureFormat(pState, internalformat, internalformat);
  1475. if(texFormatError != GL_NO_ERROR || border != 0 || level < 0 || level >= MAX_MIPMAP_LEVELS
  1476. ||
  1477. width > (1 << (MAX_MIPMAP_LEVELS-1)) || height > (1 << (MAX_MIPMAP_LEVELS-1)) ||
  1478. width < 0 || height < 0 )
  1479. {
  1480. SET_ERR(pState, GL_INVALID_VALUE, "glCopyTexImage2D");
  1481. return;
  1482. };
  1483. if(target == GL_TEXTURE_2D){
  1484. pTexObj = GetTextureObject(target,false,0);
  1485. pImgObj = &(pTexObj->images.tex2D[level]);
  1486. } else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
  1487. pTexObj = GetTextureObject(GL_TEXTURE_CUBE_MAP,false,0);
  1488. cubeFace = (target - GL_TEXTURE_CUBE_MAP_POSITIVE_X);
  1489. pImgObj = &(pTexObj->images.cubeMap[cubeFace][level]);
  1490. } else {
  1491. SET_ERR(pState, GL_INVALID_ENUM, "glCopyTexImage2D*");
  1492. return;
  1493. }
  1494. //Check if FB is complete Sandeep K. //TO DO ADD WHETHER THE FB ADDRESS IS NULL OR NOT
  1495. if(!isFBrenderable(pState))
  1496. {
  1497. set_err(GL_INVALID_FRAMEBUFFER_OPERATION);
  1498. return;
  1499. }
  1500. //Get info from FrameBuffer...
  1501. FramebufferData fbData = getFBData();
  1502. // to chekc whether this case is sufficient or needs to have the above case also TO DO
  1503. if(fbData.colorAddr.vaddr == 0){
  1504. set_err(GL_INVALID_FRAMEBUFFER_OPERATION);
  1505. return;
  1506. }
  1507. PxFmt FrameBufFormat = fbData.nativeColorFormat;
  1508. void* FramePixels = (unsigned char*)fbData.colorAddr.vaddr;
  1509. unsigned int FrameWidth = fbData.width;
  1510. unsigned int FrameHeight = fbData.height;
  1511. GLenum FrameBufFormatEnum = translateToGLenum(FrameBufFormat);
  1512. GLenum dstType;
  1513. if(!CheckFormatConversion( internalformat , FrameBufFormatEnum, &dstType) )
  1514. {
  1515. // Do a comparison of internalFormat and framebuftype...and throw error if GL specs are violated....
  1516. SET_ERR(pState, GL_INVALID_OPERATION, "glCopyTexImage2D*");
  1517. gAssert(false && "glCopyTexImage2D" && "Unsupproted format Conversion");
  1518. return;
  1519. }
  1520. //PxFmt fmtFB = translateGLSizedInternal(FrameBufFormat);
  1521. PxFmt fmtFB = FrameBufFormat;
  1522. PxFmt fmtData = translateGLInternal(internalformat,dstType);
  1523. //Wait till FIMG has finished rendering and all FB data is flushed from FIMG caches to RAM.
  1524. FimgFinish(pState);
  1525. //-------------------------------------------------------------------------
  1526. // COPY IMAGE DATA
  1527. //-------------------------------------------------------------------------
  1528. pixSize = GetPixSize(fmtData); //the value is going to be stored in the texture format
  1529. if(pixSize <= 0) {
  1530. return;
  1531. }
  1532. pImgBuf = getImageDataLocation( pTexObj, level , cubeFace, width, height , 0 ,internalformat, dstType, width * height * pixSize , 0);
  1533. if(pImgBuf == NULL){
  1534. gAssert(false && " unbale to get the memory location \n");
  1535. return;
  1536. }
  1537. GLint dir = 1;
  1538. if(fbData.flipped == 0)
  1539. dir = -1;//y = FrameHeight - y - height + 1*(!(!y));//-1 so as to read in reverse direction...
  1540. else dir = 1;
  1541. convertPixels( fmtData, (void*)pImgBuf, width, height, 0,
  1542. 0, 0, 0,
  1543. fmtFB, FramePixels, FrameWidth, FrameHeight, 0,
  1544. x, y, 0,
  1545. width, height, 0 , 0 , 0,dir);
  1546. #ifdef CACHE_MEM
  1547. pCA->cache_clean_invalid(pImgBuf, width * height * pixSize);
  1548. #endif
  1549. //-------------------------------------------------------------------------
  1550. // SET IMAGE PARAMETERS
  1551. //-------------------------------------------------------------------------
  1552. pImgObj->isUsed = GL_TRUE;
  1553. pImgObj->width = width;
  1554. pImgObj->height = height;
  1555. pImgObj->internalFormat = internalformat;
  1556. pImgObj->PixType = dstType;
  1557. pImgObj->nativeFormat = fmtData;
  1558. pImgObj->imgSize = width * height * pixSize;
  1559. pImgObj->isCompressed = false;
  1560. pTexObj->isDirtyState = GL_TRUE;
  1561. }
  1562. #ifdef USE_3D_PM
  1563. void PM_glCopyTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height);
  1564. GL_API void GL_APIENTRY glCopyTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
  1565. {
  1566. lock3DCriticalSection();
  1567. PM_glCopyTexSubImage2D ( target, level, xoffset, yoffset, x, y, width, height);
  1568. unlock3DCriticalSection();
  1569. }
  1570. void PM_glCopyTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
  1571. #else
  1572. GL_API void GL_APIENTRY glCopyTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
  1573. #endif
  1574. {
  1575. OGLState* pState = GET_OGL_STATE();
  1576. //GLuint texRef; // Reference to texture object
  1577. //GLsizei pixelSize; // Bytes per pixel
  1578. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  1579. Image *pImgObj = NULL; // Pointer to image object
  1580. GLuint cubeFace = 0; // Index of cube face refered
  1581. PxFmt textureFormat;
  1582. PxFmt frameFormat;
  1583. TextureObject *pTexObj =NULL;
  1584. //GLuint isImagedata = 1;
  1585. //-------------------------------------------------------------------------
  1586. // VALIDATE IMAGE DATA
  1587. //-------------------------------------------------------------------------
  1588. // Check MipMap level
  1589. if(level < 0 || level >= MAX_MIPMAP_LEVELS )
  1590. {
  1591. SET_ERR(pState, GL_INVALID_VALUE, "glCopyTexSubImage2D");
  1592. return;
  1593. };
  1594. if(target == GL_TEXTURE_2D){
  1595. pTexObj = GetTextureObject(target,false,0);
  1596. pImgObj = &(pTexObj->images.tex2D[level]);
  1597. } else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
  1598. pTexObj = GetTextureObject(GL_TEXTURE_CUBE_MAP,false,0);
  1599. cubeFace = (target - GL_TEXTURE_CUBE_MAP_POSITIVE_X);
  1600. pImgObj = &(pTexObj->images.cubeMap[cubeFace][level]);
  1601. } else {
  1602. SET_ERR(pState, GL_INVALID_ENUM, "glCopyTexSubImage2D*");
  1603. return;
  1604. }
  1605. if( x < 0 || y < 0 || xoffset < 0 || yoffset < 0 || (xoffset + width) > pImgObj->width
  1606. ||
  1607. (yoffset + height) > pImgObj->height || width < 0 || height < 0
  1608. )
  1609. {
  1610. SET_ERR(pState, GL_INVALID_VALUE, "glCopyTexSubImage2D");
  1611. return;
  1612. }
  1613. //if texsubimage is called before calling glteximage function .
  1614. if(pImgObj->imagedataLocation == NONE){
  1615. SET_ERR(pState, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
  1616. return;
  1617. }
  1618. //Check if FB is complete Sandeep K.
  1619. if(!isFBrenderable(pState))
  1620. {
  1621. set_err(GL_INVALID_FRAMEBUFFER_OPERATION);
  1622. return;
  1623. }
  1624. //Get info from FrameBuffer...
  1625. FramebufferData fbData = getFBData();
  1626. // to chekc whether this case is sufficient or needs to have the above case also TO DO
  1627. if(fbData.colorAddr.vaddr == 0){
  1628. set_err(GL_INVALID_FRAMEBUFFER_OPERATION);
  1629. return;
  1630. }
  1631. GLenum FrameBufFormat = translateToGLenum(fbData.nativeColorFormat); //GL_RGB; //GLenum(fbData.colorFormat);
  1632. //need to convert to GL format
  1633. //GLenum type = GL_UNSIGNED_SHORT_5_6_5; //GLenum(fbData.colorType);NOT USED?
  1634. GLenum dstType;//not req here...
  1635. // Do a comparison of internalFormat and framebuftype...and throw error if GL specs are violated....
  1636. if(!CheckFormatConversion( pImgObj->internalFormat , FrameBufFormat , &dstType) )
  1637. {
  1638. SET_ERR(pState, GL_INVALID_OPERATION, "glCopyTexSubImage2D*");
  1639. return;
  1640. }
  1641. unsigned char* FramePixels = (unsigned char*)fbData.colorAddr.vaddr;
  1642. unsigned int FrameWidth = fbData.width;
  1643. unsigned int FrameHeight = fbData.height;
  1644. textureFormat = translateGLInternal(pImgObj->internalFormat,pImgObj->PixType);
  1645. frameFormat = fbData.nativeColorFormat; //to check whether need to be hard coded
  1646. //TO DO CHECK WHICH WILL MAKE SURE THAT TEXTURE SUB IMAGE IS NOT CALLED BEFORE TEXIMAGE
  1647. //pImgObj->isUsed = GL_TRUE;
  1648. //Wait till FIMG has finished rendering and all FB data is flushed from FIMG caches to RAM.
  1649. FimgFinish(pState);
  1650. //-------------------------------------------------------------------------
  1651. // COPY TEXTURE DATA
  1652. //-------------------------------------------------------------------------
  1653. pImgBuf = getImageDataLocation( pTexObj, pImgObj, level , cubeFace);
  1654. if(pImgBuf == NULL){
  1655. gAssert(false && " unbale to get the memory location \n");
  1656. return;
  1657. }
  1658. convertPixels( textureFormat, (void*)pImgBuf, pImgObj->width, pImgObj->height, 0,
  1659. xoffset, yoffset, 0,
  1660. frameFormat, FramePixels, FrameWidth, FrameHeight, 0,
  1661. x, y, 0,
  1662. width, height, 0 , 0 , 0,fbData.flipped ? 1 : -1);
  1663. #ifdef CACHE_MEM
  1664. pCA->cache_clean_invalid(pImgBuf, pImgObj->imgSize);
  1665. #endif
  1666. pImgObj->isUsed = GL_TRUE;
  1667. pTexObj->isDirtyState = GL_TRUE;
  1668. }
  1669. /*******************************************************************************
  1670. *******************************************************************************/
  1671. #ifdef USE_3D_PM
  1672. void PM_glCompressedTexImage2D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const void *pixels);
  1673. GL_API void GL_APIENTRY glCompressedTexImage2D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const void *pixels)
  1674. {
  1675. lock3DCriticalSection();
  1676. PM_glCompressedTexImage2D ( target, level, internalformat, width, height, border, imageSize, pixels);
  1677. unlock3DCriticalSection();
  1678. }
  1679. void PM_glCompressedTexImage2D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const void *pixels)
  1680. #else
  1681. GL_API void GL_APIENTRY glCompressedTexImage2D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const void *pixels)
  1682. #endif
  1683. {
  1684. OGLState* pState = GET_OGL_STATE();
  1685. //GLuint texRef; // Reference to texture object
  1686. PxFmt texFormat; // Internal texture format
  1687. //GLsizei pixelSize; // Bytes per pixel
  1688. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  1689. Image *pImgObj = NULL; // Pointer to image object
  1690. GLuint cubeFace = 0; // Index of cube face refered
  1691. //PtrChunkH pTmpChunk = NULL; /* pointer to the ChunkHandle of new image data */
  1692. //GLubyte* newPixels = NULL; /* pointer to the new image data */
  1693. GLuint newImageSize = 0;
  1694. PxFmt DstTextureFormat ; // the texture format in which the data will be send
  1695. PxFmt srcTextureFormat ; // source texture format
  1696. //GLenum tmpPixelSize ; //temporary to check if value is same or not
  1697. //GLubyte *pTempImgBuf = NULL;
  1698. unsigned int NoOfpaletteEntry = 0; //determine the palette entry of table
  1699. unsigned int OldpaletteSize = 0; // the size of the 1 palette entry in byte
  1700. unsigned int NewpaletteSize = 0; // the size of the 1 palette entry in byte after convertPixel
  1701. TextureObject* pTexObj = NULL;
  1702. //-------------------------------------------------------------------------
  1703. // Get texture object and VALIDATE INPUT PARAMETERS
  1704. //-------------------------------------------------------------------------
  1705. //TO DO LEVEL HAS TIO BE GREATER THAN 0 EXCEPT FOR PALETTE ONE WHERE NEGATIVE
  1706. if(level > 0 || abs(level) >=MAX_MIPMAP_LEVELS) {
  1707. SET_ERR(pState, GL_INVALID_VALUE, "glCompressedTexImage2D");
  1708. return;
  1709. }
  1710. // Select object reference according to target
  1711. // Return error on invalid enum
  1712. if(target == GL_TEXTURE_2D){
  1713. pTexObj = GetTextureObject(target,false,0);
  1714. pImgObj = &(pTexObj->images.tex2D[0]);
  1715. //pTexObj->levels = abs(level) + 1;
  1716. } else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
  1717. pTexObj = GetTextureObject(GL_TEXTURE_CUBE_MAP,false,0);
  1718. cubeFace = (target - GL_TEXTURE_CUBE_MAP_POSITIVE_X);
  1719. pImgObj = &(pTexObj->images.cubeMap[cubeFace][0]);
  1720. //pTexObj->levels = abs(level) + 1;
  1721. } else {
  1722. SET_ERR(pState, GL_INVALID_ENUM, "glCompressedTexImage2D*");
  1723. return;
  1724. }
  1725. // Check width and height
  1726. if(width > (1 << (MAX_MIPMAP_LEVELS-1)) || height > (1 << (MAX_MIPMAP_LEVELS-1))) {
  1727. SET_ERR(pState, GL_INVALID_VALUE, "glCompressedTexImage2D");
  1728. return;
  1729. }
  1730. // Border not supported in ES
  1731. if(border != 0) {
  1732. SET_ERR(pState, GL_INVALID_VALUE, "glCompressedTexImage2D");
  1733. return;
  1734. }
  1735. //-------------------------------------------------------------------------
  1736. // Calculate internal format and space requirements
  1737. //-------------------------------------------------------------------------
  1738. void* tmpImgSrc = const_cast<void*>(pixels); //this variable will point to the user data and will be changed for palette case.
  1739. GLenum bFormat = GLenum(-1);
  1740. GLenum bType = GLenum(-1);
  1741. switch(internalformat)
  1742. {
  1743. case GL_RGB_S3TC_OES:
  1744. {
  1745. GLenum format;
  1746. bool flag = s3tcCompressedTex(format,internalformat,width, height, imageSize);
  1747. if(!flag) return;
  1748. newImageSize = imageSize;
  1749. texFormat = E_RGB_S3TC_OES;
  1750. break;
  1751. }
  1752. case GL_RGBA_S3TC_OES:
  1753. {
  1754. GLenum format;
  1755. bool flag = s3tcCompressedTex(format,internalformat,width, height, imageSize);
  1756. if(!flag) return;
  1757. newImageSize = imageSize;
  1758. texFormat = E_RGBA_S3TC_OES;
  1759. break;
  1760. }
  1761. case GL_PALETTE4_RGB8_OES: //24
  1762. texFormat = E_PALETTE4_RGBA8_OES;
  1763. NoOfpaletteEntry = 16;
  1764. OldpaletteSize = 3;
  1765. NewpaletteSize = 4;
  1766. bFormat = GL_RGB;
  1767. bType = GL_UNSIGNED_BYTE;
  1768. break;
  1769. case GL_PALETTE8_RGB8_OES: //24
  1770. texFormat = E_PALETTE8_RGBA8_OES;
  1771. NoOfpaletteEntry = 256;
  1772. OldpaletteSize = 3;
  1773. NewpaletteSize = 4;
  1774. bFormat = GL_RGB;
  1775. bType = GL_UNSIGNED_BYTE;
  1776. break;
  1777. case GL_PALETTE4_RGBA8_OES: //32
  1778. texFormat = E_PALETTE4_RGBA8_OES;
  1779. NoOfpaletteEntry = 16;
  1780. OldpaletteSize = 4;
  1781. NewpaletteSize = 4;
  1782. bFormat = GL_RGBA;
  1783. bType = GL_UNSIGNED_BYTE;
  1784. break;
  1785. case GL_PALETTE8_RGBA8_OES: //32
  1786. texFormat = E_PALETTE8_RGBA8_OES;
  1787. NoOfpaletteEntry = 256;
  1788. OldpaletteSize = 4;
  1789. NewpaletteSize = 4;
  1790. bFormat = GL_RGBA;
  1791. bType = GL_UNSIGNED_BYTE;
  1792. break;
  1793. case GL_PALETTE4_RGBA4_OES: //16
  1794. texFormat = E_PALETTE4_RGBA8_OES;
  1795. NoOfpaletteEntry = 16;
  1796. OldpaletteSize = 2;
  1797. NewpaletteSize = 4;
  1798. bFormat = GL_RGBA;
  1799. bType = GL_UNSIGNED_SHORT_4_4_4_4;
  1800. break;
  1801. case GL_PALETTE8_RGBA4_OES:
  1802. texFormat = E_PALETTE8_RGBA8_OES;
  1803. NoOfpaletteEntry = 256;
  1804. OldpaletteSize = 2;
  1805. NewpaletteSize = 4;
  1806. bFormat = GL_RGBA;
  1807. bType = GL_UNSIGNED_SHORT_4_4_4_4;
  1808. break;
  1809. case GL_PALETTE4_RGB5_A1_OES: //16
  1810. texFormat = E_PALETTE4_RGBA8_OES;
  1811. NoOfpaletteEntry = 16;
  1812. OldpaletteSize = 2;
  1813. NewpaletteSize = 4;
  1814. bFormat = GL_RGBA;
  1815. bType = GL_UNSIGNED_SHORT_5_5_5_1;
  1816. break;
  1817. case GL_PALETTE8_RGB5_A1_OES://16
  1818. texFormat = E_PALETTE8_RGBA8_OES;
  1819. NoOfpaletteEntry = 256;
  1820. OldpaletteSize = 2;
  1821. NewpaletteSize = 4;
  1822. bFormat = GL_RGBA;
  1823. bType = GL_UNSIGNED_SHORT_5_5_5_1;
  1824. break;
  1825. case GL_PALETTE8_R5_G6_B5_OES: //16
  1826. texFormat = E_PALETTE8_RGBA8_OES;
  1827. NoOfpaletteEntry = 256;
  1828. OldpaletteSize = 2;
  1829. NewpaletteSize = 4;
  1830. bFormat = GL_RGB;
  1831. bType = GL_UNSIGNED_SHORT_5_6_5;
  1832. break;
  1833. case GL_PALETTE4_R5_G6_B5_OES: //16
  1834. texFormat = E_PALETTE4_RGBA8_OES;
  1835. NoOfpaletteEntry = 16;
  1836. OldpaletteSize = 2;
  1837. NewpaletteSize = 4;
  1838. bFormat = GL_RGB;
  1839. bType = GL_UNSIGNED_SHORT_5_6_5;
  1840. break;
  1841. default:
  1842. return;
  1843. }
  1844. if((internalformat != GL_RGB_S3TC_OES)&& (internalformat != GL_RGBA_S3TC_OES))
  1845. {
  1846. if(imageSize==0)
  1847. imageSize = (NoOfpaletteEntry*OldpaletteSize)+(width*height);
  1848. newImageSize = imageSize + NoOfpaletteEntry * (NewpaletteSize -OldpaletteSize) ;
  1849. }
  1850. //FLUSHING THE CACHE
  1851. FimgFinish(pState);
  1852. #ifdef PALETTE_SW_WORKAROUND
  1853. if ((internalformat >= GL_PALETTE4_RGB8_OES) && ( internalformat <= GL_PALETTE8_RGB5_A1_OES)) {
  1854. for (int count =0 ; count <= abs(level) ; count++) {
  1855. int lodWidth = (width >> count) ? : 1;
  1856. int lodHeight = (height >> count) ? : 1;
  1857. GLubyte * pixValBuf = (GLubyte *)Plat::malloc(lodWidth * lodHeight * OldpaletteSize) ;
  1858. decodePalette4(pixels, count, width, height, pixValBuf, lodWidth, internalformat);
  1859. #ifdef USE_3D_PM
  1860. PM_glTexImage2D ( target, count, bFormat, lodWidth, lodHeight, border, bFormat, bType , pixValBuf);
  1861. #else
  1862. glTexImage2D ( target, count, bFormat, lodWidth, lodHeight, border, bFormat, bType , pixValBuf);
  1863. #endif
  1864. Plat::safe_free(pixValBuf);
  1865. return ;
  1866. }
  1867. }
  1868. #endif
  1869. //-------------------------------------------------------------------------
  1870. // COPY IMAGE DATA
  1871. //-------------------------------------------------------------------------
  1872. pImgBuf = getImageDataLocation( pTexObj, abs(level) , cubeFace, width, height , 0 ,internalformat,internalformat, newImageSize, 0);
  1873. if(pImgBuf == NULL){
  1874. gAssert(false && " unbale to get the memory location \n");
  1875. return;
  1876. }
  1877. if((internalformat == GL_RGB_S3TC_OES)||(internalformat == GL_RGBA_S3TC_OES))
  1878. {
  1879. if (pixels)
  1880. Plat::memcpy(pImgBuf,(GLubyte*)tmpImgSrc,newImageSize);
  1881. }
  1882. else
  1883. {
  1884. GLenum type = internalformat;
  1885. GLubyte* tempPixel = (GLubyte*)pixels;
  1886. DstTextureFormat = DetermineTextureFormat(internalformat, type, &srcTextureFormat);
  1887. newImageSize = imageSize + NoOfpaletteEntry * (NewpaletteSize -OldpaletteSize) ;
  1888. if (pixels)
  1889. {
  1890. convertPixels(DstTextureFormat,(void*)pImgBuf, NoOfpaletteEntry, 1,0,0,0,0,
  1891. srcTextureFormat, (void*) pixels, NoOfpaletteEntry, 1,0,0,0,0,
  1892. NoOfpaletteEntry,1,0,0,0,1); //paletteSize is the size of the table to be converted
  1893. Plat::memcpy((pImgBuf+NoOfpaletteEntry * NewpaletteSize) ,
  1894. (tempPixel+NoOfpaletteEntry * OldpaletteSize) ,
  1895. imageSize -NoOfpaletteEntry * OldpaletteSize );
  1896. }
  1897. }
  1898. #ifdef CACHE_MEM
  1899. pCA->cache_clean_invalid(pImgBuf, newImageSize);
  1900. #endif
  1901. pImgObj->isUsed = GL_TRUE;
  1902. pImgObj->width = width;
  1903. pImgObj->height = height;
  1904. pImgObj->internalFormat = internalformat;
  1905. pImgObj->PixType = internalformat;
  1906. pImgObj->imgSize = newImageSize;
  1907. pImgObj->isCompressed = true;
  1908. pImgObj->nativeFormat = texFormat;
  1909. pTexObj->isDirtyState = GL_TRUE;
  1910. tmpImgSrc = NULL;
  1911. //fprintf(stderr, " line = %d , func =%s \n", __LINE__ , __FUNCTION__);
  1912. }
  1913. #ifdef USE_3D_PM
  1914. void PM_glCompressedTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *data);
  1915. GL_API void GL_APIENTRY glCompressedTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *data)
  1916. {
  1917. lock3DCriticalSection();
  1918. PM_glCompressedTexSubImage2D ( target, level, xoffset, yoffset, width, height, format, imageSize, data);
  1919. unlock3DCriticalSection();
  1920. }
  1921. void PM_glCompressedTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *data)
  1922. #else
  1923. GL_API void GL_APIENTRY glCompressedTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *data)
  1924. #endif
  1925. {
  1926. OGLState* pState = GET_OGL_STATE();
  1927. //GLuint texRef; // Reference to texture object
  1928. GLenum texFormat; // Internal texture format
  1929. //GLsizei pixelSize; // Bytes per pixel
  1930. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  1931. Image *pImgObj = NULL; // Pointer to image object
  1932. GLuint cubeFace = 0; // Index of cube face refered
  1933. TextureObject* pTexObj = NULL;
  1934. //-------------------------------------------------------------------------
  1935. // VALIDATE INPUT PARAMETERS
  1936. //-------------------------------------------------------------------------
  1937. // Check image pointer
  1938. if(data == NULL) {
  1939. return;
  1940. }
  1941. if(level < 0 || level >= MAX_MIPMAP_LEVELS) {
  1942. SET_ERR(pState, GL_INVALID_VALUE, "glCompressedTexSubImage2D");
  1943. return;
  1944. };
  1945. if(target == GL_TEXTURE_2D){
  1946. pTexObj = GetTextureObject(target,false,0);
  1947. pImgObj = &(pTexObj->images.tex2D[level]);
  1948. } else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
  1949. pTexObj = GetTextureObject(GL_TEXTURE_CUBE_MAP,false,0);
  1950. cubeFace = (target - GL_TEXTURE_CUBE_MAP_POSITIVE_X);
  1951. pImgObj = &(pTexObj->images.cubeMap[cubeFace][level]);
  1952. } else {
  1953. SET_ERR(pState, GL_INVALID_ENUM, "glCompressedTexSubImage2D*");
  1954. return;
  1955. }
  1956. // Check MipMap level
  1957. if( xoffset < 0 || yoffset < 0 || (xoffset + width) > pImgObj->width
  1958. ||
  1959. (yoffset + height) > pImgObj->height || width < 0 || height < 0
  1960. )
  1961. {
  1962. SET_ERR(pState, GL_INVALID_VALUE, "glCompressedTexSubImage2D");
  1963. return;
  1964. }
  1965. #if 0
  1966. if( pImgObj->hImgChunk == NULL /*pImgObj->data == NULL*/ ){ //probably this will take care if texSubImage is called before texImage2D......
  1967. SET_ERR(pState, GL_INVALID_OPERATION, "glCompressedTexSubImage2D");
  1968. }
  1969. #else
  1970. //TO DO MAKE SURE THAT SUB IMAGE IS NOT CALLED BEFORE IMAGE
  1971. #endif
  1972. int offset = 0;
  1973. //int sizeToCopy =0;
  1974. int ww =0;
  1975. int hh =0;
  1976. int BPG =0; //bytes per group
  1977. int srcW = 0;
  1978. int srcH = 0;
  1979. switch(format)
  1980. {
  1981. case GL_RGB_S3TC_OES:
  1982. case GL_RGBA_S3TC_OES:
  1983. {
  1984. ///in S3TC 4x4 block is taking 8 bytes
  1985. ww = (xoffset+width)/4;
  1986. hh = (yoffset+height)/4;
  1987. xoffset = xoffset/4;
  1988. yoffset = yoffset/4;
  1989. offset = (xoffset + yoffset * ww)*8 ; // 8 is number of bytes for 16 pixels.
  1990. BPG = 8;
  1991. srcW = pImgObj->width/4;
  1992. srcH = pImgObj->height/4;
  1993. bool flag = s3tcCompressedTex(texFormat,format,width, height, imageSize);
  1994. if(!flag) return; // if flag is false
  1995. break;
  1996. }
  1997. //TO DO INVALID_OPERATION is generated when the given formats are present as internalformat
  1998. case GL_PALETTE4_RGB8_OES:
  1999. case GL_PALETTE4_RGBA8_OES:
  2000. case GL_PALETTE4_R5_G6_B5_OES:
  2001. case GL_PALETTE4_RGBA4_OES:
  2002. case GL_PALETTE4_RGB5_A1_OES:
  2003. case GL_PALETTE8_RGB8_OES:
  2004. case GL_PALETTE8_RGBA8_OES:
  2005. case GL_PALETTE8_R5_G6_B5_OES:
  2006. case GL_PALETTE8_RGBA4_OES:
  2007. case GL_PALETTE8_RGB5_A1_OES:
  2008. #if 1
  2009. //INVALID_OPERATION is generated CompressedTexSubImage2D if internalformat is PALETTE
  2010. SET_ERR(pState, GL_INVALID_OPERATION, "glCompressedTexSubImage2D");
  2011. return ;
  2012. #else
  2013. //paletteCompressedTex(); //TODO:stuff related to palette based comp goes here
  2014. ww = width;
  2015. hh = height;
  2016. BPG = 1;
  2017. srcW = pImgObj->width;
  2018. srcH = pImgObj->height;
  2019. break;
  2020. #endif
  2021. default:
  2022. return;
  2023. }
  2024. //FLUSHING THE CACHE
  2025. FimgFinish(pState);
  2026. //-------------------------------------------------------------------------
  2027. // COPY TEXTURE DATA
  2028. //-------------------------------------------------------------------------
  2029. pImgBuf = getImageDataLocation( pTexObj, level , cubeFace, pImgObj->width, pImgObj->height , 0 , pImgObj->internalFormat, pImgObj->PixType, pImgObj->imgSize, 0);
  2030. if(pImgBuf == NULL){
  2031. gAssert(false && " unbale to get the memory location \n");
  2032. return;
  2033. }
  2034. #ifdef CACHE_MEM
  2035. pCA->cache_clean_invalid(pImgBuf, pImgObj->imgSize);
  2036. #endif
  2037. //TODO: Copy of subsection of the texel array for compressed case is to be implemented....
  2038. GLubyte* src = (GLubyte*)data;
  2039. GLubyte* dst = pImgBuf + offset;
  2040. for(int j = 0; j < hh; ++j)
  2041. for(int i = 0; i < ww ; ++i)
  2042. {
  2043. for(int b = 0; b < BPG; ++b)
  2044. {
  2045. *dst = *src;
  2046. dst++;
  2047. src++;
  2048. }
  2049. dst += (srcW - ww)*BPG;
  2050. }
  2051. pImgObj->isUsed = GL_TRUE;
  2052. // Set recompile flag
  2053. pTexObj->reCompile = GL_TRUE;
  2054. pTexObj->isDirtyState = GL_TRUE;
  2055. }
  2056. #ifdef USE_3D_PM
  2057. void PM_glTexImage3D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *pixels);
  2058. GL_API void GL_APIENTRY glTexImage3D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *pixels)
  2059. {
  2060. lock3DCriticalSection();
  2061. PM_glTexImage3D ( target, level, internalformat, width, height, depth, border, format, type, pixels);
  2062. unlock3DCriticalSection();
  2063. }
  2064. void PM_glTexImage3D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *pixels)
  2065. #else
  2066. GL_API void GL_APIENTRY glTexImage3D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *pixels)
  2067. #endif
  2068. {
  2069. OGLState* pState = GET_OGL_STATE();
  2070. //TextureState* pTexState = &(pState->texState);
  2071. //GLuint texRef; // Reference to texture object
  2072. GLenum texFormatError; // Internal texture format
  2073. GLsizei pixSize; // Bytes per pixel
  2074. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  2075. Image *pImgObj = NULL; // Pointer to image object
  2076. PxFmt DstTextureFormat ; /* the texture format in which the data will be send */
  2077. PxFmt srcTextureFormat ; /* source texture format */
  2078. //GLenum tmpPixelSize ; /*temporary to check if value is same or not*/
  2079. //GLubyte *pTempImgBuf = NULL;
  2080. //GLuint isImagedata = 1;
  2081. TextureObject* pTexObj = NULL;
  2082. //-------------------------------------------------------------------------
  2083. // Get texture object and VALIDATE INPUT PARAMETER
  2084. //-------------------------------------------------------------------------
  2085. // Check MipMap level and border
  2086. if(level < 0 || level >= MAX_MIPMAP_LEVELS || border!=0) {
  2087. SET_ERR(pState, GL_INVALID_VALUE, "glTexImage3D");
  2088. return;
  2089. };
  2090. // Select object reference according to target
  2091. // Return error on invalid enum
  2092. if(target == GL_TEXTURE_3D){
  2093. pTexObj = GetTextureObject(target,false,0);
  2094. pImgObj = &(pTexObj->images.tex3D[level]);
  2095. }
  2096. else {
  2097. SET_ERR(pState, GL_INVALID_ENUM, "glTexImage3D*");
  2098. return;
  2099. }
  2100. // Check width and height
  2101. if(width > (1 << (MAX_MIPMAP_LEVELS-1)) || height > (1 << (MAX_MIPMAP_LEVELS-1)) || depth > (1 << (MAX_MIPMAP_LEVELS-1)) ||
  2102. width < 0 || height < 0 || depth < 0) {
  2103. SET_ERR(pState, GL_INVALID_VALUE, "glTexImage3D");
  2104. return;
  2105. }
  2106. //check the Texture format
  2107. texFormatError = checkTextureFormat(pState, internalformat, format);
  2108. if(texFormatError != GL_NO_ERROR) {
  2109. SET_ERR(pState, texFormatError, "glTexImage3D");
  2110. return;
  2111. }
  2112. // check the type and format combination supported
  2113. if(CheckFormatTypeCombination(internalformat,type) == 1){
  2114. SET_ERR(pState, GL_INVALID_OPERATION, "glTexImage3D");
  2115. return;
  2116. }
  2117. DstTextureFormat = DetermineTextureFormat(internalformat, type, &srcTextureFormat);
  2118. pImgObj->nativeFormat = DstTextureFormat;
  2119. //FLUSHING THE CACHE
  2120. FimgFinish(pState);
  2121. //-------------------------------------------------------------------------
  2122. // COPY TEXTURE DATA
  2123. //-------------------------------------------------------------------------
  2124. // Per-pixel size
  2125. pixSize = pixelSize(DstTextureFormat);
  2126. if(pixSize <= 0) {
  2127. return;
  2128. }
  2129. pImgBuf = getImageDataLocation( pTexObj, level , 0, width , height , depth ,internalformat,type, width * height * depth * pixSize , 0);
  2130. if(pImgBuf == NULL){
  2131. gAssert(false && " unbale to get the memory location \n");
  2132. return;
  2133. }
  2134. if(pixels != NULL)
  2135. convertPixels(DstTextureFormat,(void*)pImgBuf, width, height,depth,0,0,0,
  2136. srcTextureFormat, (void*) pixels, width, height,depth,0,0,0,
  2137. width,height,depth,0,0,0);
  2138. #ifdef CACHE_MEM
  2139. pCA->cache_clean_invalid(pImgBuf, width * height * pixSize);
  2140. #endif
  2141. //-------------------------------------------------------------------------
  2142. // SET IMAGE PARAMETERS
  2143. //-------------------------------------------------------------------------
  2144. pImgObj->isUsed = GL_TRUE;
  2145. pImgObj->width = width;
  2146. pImgObj->height = height;
  2147. pImgObj->depth = depth;
  2148. pImgObj->internalFormat = internalformat;
  2149. pImgObj->PixType = type;
  2150. pImgObj->imgSize = width * height * depth* pixSize;
  2151. pImgObj->isCompressed = false;
  2152. pTexObj->isDirtyState = GL_TRUE;
  2153. }
  2154. #ifdef USE_3D_PM
  2155. void PM_glTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels);
  2156. GL_API void GL_APIENTRY glTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels)
  2157. {
  2158. lock3DCriticalSection();
  2159. PM_glTexSubImage3D (target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels);
  2160. unlock3DCriticalSection();
  2161. }
  2162. void PM_glTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels)
  2163. #else
  2164. GL_API void GL_APIENTRY glTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels)
  2165. #endif
  2166. {
  2167. OGLState* pState = GET_OGL_STATE();
  2168. //TextureState* pTexState = &(pState->texState);
  2169. //GLuint texRef; // Reference to texture object
  2170. //GLenum texFormat; // Internal texture format
  2171. //GLsizei pixelSize; // Bytes per pixel
  2172. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  2173. Image *pImgObj = NULL; // Pointer to image object
  2174. //TextureObject* pTexObj;
  2175. //GLuint isImagedata = 0;
  2176. PxFmt DstTextureFormat ; // the texture format in which the data will be send
  2177. PxFmt srcTextureFormat ; // source texture format
  2178. TextureObject* pTexObj = NULL;
  2179. //-------------------------------------------------------------------------
  2180. // VALIDATE INPUT PARAMETERS
  2181. //-------------------------------------------------------------------------
  2182. if(pixels == NULL) {
  2183. //SET_ERR(pState, GL_INVALID_VALUE, "glTexSubImage2D"); no need for a error...Just return without modifying the original texture object
  2184. return;
  2185. }
  2186. // Check MipMap level
  2187. if( level < 0 || level >= MAX_MIPMAP_LEVELS )
  2188. {
  2189. SET_ERR(pState, GL_INVALID_VALUE, "glTexSubImage2D");
  2190. return;
  2191. }
  2192. if(target == GL_TEXTURE_3D){
  2193. pTexObj = GetTextureObject(target,false,0);
  2194. pImgObj = &(pTexObj->images.tex3D[level]);
  2195. } else {
  2196. SET_ERR(pState, GL_INVALID_ENUM, "glTexSubImage3D*");
  2197. return;
  2198. }
  2199. if( xoffset < 0 || yoffset < 0 || zoffset < 0 ||(xoffset + width) > pImgObj->width
  2200. ||
  2201. (yoffset + height) > pImgObj->height || (zoffset + depth) > pImgObj->depth
  2202. ||
  2203. width < 0 || height < 0 || depth < 0
  2204. )
  2205. {
  2206. SET_ERR(pState, GL_INVALID_VALUE, "glTexSubImage3D");
  2207. return;
  2208. }
  2209. if(format != pImgObj->internalFormat || type != pImgObj->PixType) //format of the image should be identical to the internal format of the texture object created...
  2210. {
  2211. SET_ERR(pState, GL_INVALID_ENUM, "glTexSubImage3D");
  2212. return;
  2213. }
  2214. //if texsubimage is called before calling glteximage function .
  2215. if(pImgObj->imagedataLocation == NONE){
  2216. SET_ERR(pState, GL_INVALID_OPERATION, "glTexSubImage3D");
  2217. return;
  2218. }
  2219. //FLUSHING THE CACHE
  2220. FimgFinish(pState);
  2221. //-------------------------------------------------------------------------
  2222. // COPY TEXTURE DATA
  2223. //-------------------------------------------------------------------------
  2224. pImgBuf = getImageDataLocation( pTexObj, level , 0, pImgObj->width , pImgObj->height , pImgObj->depth ,pImgObj->internalFormat, pImgObj->PixType, pImgObj->imgSize, 0);
  2225. if(pImgBuf == NULL){
  2226. gAssert(false && " unbale to get the memory location \n");
  2227. return;
  2228. }
  2229. DstTextureFormat = DetermineTextureFormat(format,type, &srcTextureFormat );
  2230. convertPixels(DstTextureFormat,(void*)pImgBuf, pImgObj->width, pImgObj->height, pImgObj->depth,xoffset,yoffset,zoffset,
  2231. srcTextureFormat, (void*) pixels, width, height,depth,0,0,0,
  2232. width,height,depth,0,0,0);
  2233. #ifdef CACHE_MEM
  2234. pCA->cache_clean_invalid(pImgBuf, pImgObj->imgSize);
  2235. #endif
  2236. pTexObj->isDirtyState = GL_TRUE;
  2237. pImgObj->isUsed = GL_TRUE;
  2238. }
  2239. #ifdef USE_3D_PM
  2240. void PM_glCopyTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height);
  2241. GL_API void GL_APIENTRY glCopyTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height)
  2242. {
  2243. lock3DCriticalSection();
  2244. PM_glCopyTexSubImage3D ( target, level, xoffset, yoffset, zoffset, x, y, width, height);
  2245. unlock3DCriticalSection();
  2246. }
  2247. void PM_glCopyTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height)
  2248. #else
  2249. GL_API void GL_APIENTRY glCopyTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height)
  2250. #endif
  2251. {
  2252. OGLState* pState = GET_OGL_STATE();
  2253. //TextureState* pTexState = &(pState->texState);
  2254. //GLuint texRef; // Reference to texture object
  2255. //GLsizei pixelSize; // Bytes per pixel
  2256. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  2257. Image *pImgObj = NULL; // Pointer to image object
  2258. //GLuint cubeFace = 0; // Index of cube face refered
  2259. PxFmt textureFormat;
  2260. PxFmt frameFormat;
  2261. //GLuint isImagedata;
  2262. TextureObject* pTexObj =NULL;
  2263. //-------------------------------------------------------------------------
  2264. // VALIDATE INPUT PARAMETERS
  2265. //-------------------------------------------------------------------------
  2266. // Check MipMap level
  2267. if(level < 0 || level >= MAX_MIPMAP_LEVELS )
  2268. {
  2269. SET_ERR(pState, GL_INVALID_VALUE, "glCopyTexSubImage3D");
  2270. return;
  2271. };
  2272. if(target == GL_TEXTURE_3D){
  2273. pTexObj = GetTextureObject(target,false,0);
  2274. pImgObj = &(pTexObj->images.tex3D[level]);
  2275. } else {
  2276. SET_ERR(pState, GL_INVALID_ENUM, "glCopyTexSubImage3D*");
  2277. return;
  2278. }
  2279. if( x < 0 || y < 0 || xoffset < 0 || yoffset < 0 || zoffset < 0 || (xoffset + width) > pImgObj->width
  2280. ||
  2281. (yoffset + height) > pImgObj->height || zoffset > pImgObj->depth
  2282. ||
  2283. width < 0 || height < 0
  2284. )
  2285. {
  2286. SET_ERR(pState, GL_INVALID_VALUE, "glCopyTexSubImage3D");
  2287. return;
  2288. }
  2289. //if texsubimage is called before calling glteximage function .
  2290. if(pImgObj->imagedataLocation == NONE){
  2291. SET_ERR(pState, GL_INVALID_OPERATION, "glCopyTexSubImage3D");
  2292. return;
  2293. }
  2294. //TO DO CHECK WHETHER FB is complete CONDITION NEED TO BE TESTED OR NOT
  2295. if(!isFBrenderable(pState))
  2296. {
  2297. set_err(GL_INVALID_FRAMEBUFFER_OPERATION);
  2298. return;
  2299. }
  2300. //Get info from FrameBuffer...
  2301. FramebufferData fbData = getFBData();
  2302. // to chekc whether this case is sufficient or needs to have the above case also TO DO
  2303. if(fbData.colorAddr.vaddr == 0){
  2304. set_err(GL_INVALID_FRAMEBUFFER_OPERATION);
  2305. return;
  2306. }
  2307. GLenum FrameBufFormat = translateToGLenum(fbData.nativeColorFormat);
  2308. //GLenum type = GLenum(0);//GLenum(fbData.colorType);
  2309. GLenum dstType;//not req here...
  2310. // Do a comparison of internalFormat and framebuftype...and throw error if GL specs are violated....
  2311. if(!CheckFormatConversion( pImgObj->internalFormat , FrameBufFormat , &dstType) )
  2312. {
  2313. SET_ERR(pState, GL_INVALID_OPERATION, "glCopyTexSubImage3D*");
  2314. return;
  2315. }
  2316. pImgBuf = (GLubyte *)pImgObj->hImgChunk->GetVirtAddr();//pImgObj->data;
  2317. unsigned char* FramePixels = (unsigned char*)fbData.colorAddr.vaddr;
  2318. unsigned int FrameWidth = fbData.width;
  2319. unsigned int FrameHeight = fbData.height;
  2320. textureFormat = translateGLInternal(pImgObj->internalFormat,pImgObj->PixType);
  2321. frameFormat = fbData.nativeColorFormat; //to check whether need to be hard coded
  2322. //Wait till FIMG has finished rendering and all FB data is flushed from FIMG caches to RAM.
  2323. FimgFinish(pState);
  2324. //-------------------------------------------------------------------------
  2325. // COPY TEXTURE DATA
  2326. //-------------------------------------------------------------------------
  2327. pImgBuf = getImageDataLocation( pTexObj, pImgObj, level , 0);
  2328. if(pImgBuf == NULL){
  2329. gAssert(false && " unbale to get the memory location \n");
  2330. return;
  2331. }
  2332. if(fbData.flipped == 0)
  2333. y = FrameHeight - y - height + 1*(!(!y));
  2334. convertPixels( textureFormat, (void*)pImgBuf, pImgObj->width, pImgObj->height, pImgObj->depth,
  2335. xoffset, yoffset, zoffset,
  2336. frameFormat, FramePixels, FrameWidth, FrameHeight, 0,
  2337. x, y, 0,
  2338. width, height, 0 , 0 , 0,1);
  2339. #ifdef CACHE_MEM
  2340. pCA->cache_clean_invalid(pImgBuf, pImgObj->imgSize);
  2341. #endif
  2342. pImgObj->isUsed = GL_TRUE;
  2343. pTexObj->isDirtyState = GL_TRUE;
  2344. }
  2345. #ifdef USE_3D_PM
  2346. void PM_glCompressedTexImage3D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void *data);
  2347. GL_API void GL_APIENTRY glCompressedTexImage3D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void *data)
  2348. {
  2349. lock3DCriticalSection();
  2350. PM_glCompressedTexImage3D ( target, level, internalformat, width, height, depth, border, imageSize, data);
  2351. unlock3DCriticalSection();
  2352. }
  2353. void PM_glCompressedTexImage3D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void *data)
  2354. #else
  2355. GL_API void GL_APIENTRY glCompressedTexImage3D (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void *data)
  2356. #endif
  2357. {
  2358. OGLState* pState = GET_OGL_STATE();
  2359. //TextureState* pTexState = &(pState->texState);
  2360. //GLuint texRef; // Reference to texture object
  2361. PxFmt texFormat; // Internal texture format
  2362. //GLsizei pixelSize; // Bytes per pixel
  2363. GLubyte *pImgBuf = NULL; // Pointer to image buffer
  2364. Image *pImgObj = NULL; // Pointer to image object
  2365. //GLuint cubeFace = 0; // Index of cube face refered
  2366. //PtrChunkH pTmpChunk = NULL; /* pointer to the ChunkHandle of new image data */
  2367. //GLubyte* newPixels = NULL; /* pointer to the new image data */
  2368. GLuint newImageSize = 0;
  2369. PxFmt DstTextureFormat ; // the texture format in which the data will be send
  2370. PxFmt srcTextureFormat ; // source texture format
  2371. //GLenum tmpPixelSize ; //temporary to check if value is same or not
  2372. //GLubyte *pTempImgBuf = NULL;
  2373. unsigned int NoOfpaletteEntry = 0; //determine the palette entry of table
  2374. unsigned int OldpaletteSize; // the size of the 1 palette entry in byte
  2375. unsigned int NewpaletteSize; // the size of the 1 palette entry in byte after convertPixel
  2376. TextureObject* pTexObj =NULL;
  2377. //-------------------------------------------------------------------------
  2378. // Get texture object and VALIDATE INPUT PARAMETER
  2379. //-------------------------------------------------------------------------
  2380. //however FIMG support 3D texture level 0 only. TO DO LEVELS
  2381. if(level > 0 || abs(level) >=MAX_MIPMAP_LEVELS) {
  2382. SET_ERR(pState, GL_INVALID_VALUE, "glCompressedTexImage2D");
  2383. return;
  2384. }
  2385. // Select object reference according to target
  2386. // Return error on invalid enum
  2387. if(target == GL_TEXTURE_3D){
  2388. pTexObj = GetTextureObject(target,false,0);
  2389. pImgObj = &(pTexObj->images.tex3D[0]);
  2390. pTexObj->levels = abs(level) + 1;
  2391. } else{
  2392. SET_ERR(pState, GL_INVALID_ENUM, "glCompressedTexImage3D*");
  2393. return;
  2394. }
  2395. // Check width and height
  2396. if(width > (1 << (MAX_MIPMAP_LEVELS-1)) || height > (1 << (MAX_MIPMAP_LEVELS-1))
  2397. || depth > (1 << (MAX_MIPMAP_LEVELS-1)) || (width < 0) || (height < 0) || (depth < 0)) {
  2398. SET_ERR(pState, GL_INVALID_VALUE, "glCompressedTexImage3D");
  2399. return;
  2400. }
  2401. // Border not supported in ES, INVALID_OPERATION should be set as error spec of palette
  2402. if(border != 0) {
  2403. SET_ERR(pState, GL_INVALID_OPERATION, "glCompressedTexImage3D");
  2404. return;
  2405. }
  2406. //-------------------------------------------------------------------------
  2407. // Calculate internal format and space requirements
  2408. //-------------------------------------------------------------------------
  2409. switch(internalformat)
  2410. {
  2411. //EXT_texture_compression_s3tc says 3D s3TC texture can't be specified
  2412. case GL_RGB_S3TC_OES:
  2413. case GL_RGBA_S3TC_OES:
  2414. SET_ERR(pState, GL_INVALID_ENUM , "glCompressedTexImage3D");
  2415. return ;
  2416. case GL_PALETTE4_RGB8_OES: //24
  2417. texFormat = E_PALETTE4_RGBA8_OES;
  2418. NoOfpaletteEntry = 16;
  2419. OldpaletteSize = 3;
  2420. NewpaletteSize = 4;
  2421. break;
  2422. case GL_PALETTE8_RGB8_OES: //24
  2423. texFormat = E_PALETTE8_RGBA8_OES;
  2424. NoOfpaletteEntry = 256;
  2425. OldpaletteSize = 3;
  2426. NewpaletteSize = 4;
  2427. break;
  2428. case GL_PALETTE4_RGBA8_OES: //32
  2429. texFormat = E_PALETTE4_RGBA8_OES;
  2430. NoOfpaletteEntry = 16;
  2431. OldpaletteSize = 4;
  2432. NewpaletteSize = 4;
  2433. break;
  2434. case GL_PALETTE8_RGBA8_OES: //32
  2435. texFormat = E_PALETTE8_RGBA8_OES;
  2436. NoOfpaletteEntry = 256;
  2437. OldpaletteSize = 4;
  2438. NewpaletteSize = 4;
  2439. break;
  2440. case GL_PALETTE4_RGBA4_OES: //16
  2441. texFormat = E_PALETTE4_RGBA8_OES;
  2442. NoOfpaletteEntry = 16;
  2443. OldpaletteSize = 2;
  2444. NewpaletteSize = 4;
  2445. break;
  2446. case GL_PALETTE8_RGBA4_OES:
  2447. texFormat = E_PALETTE8_RGBA8_OES;
  2448. NoOfpaletteEntry = 256;
  2449. OldpaletteSize = 2;
  2450. NewpaletteSize = 4;
  2451. break;
  2452. case GL_PALETTE4_RGB5_A1_OES: //16
  2453. texFormat = E_PALETTE4_RGBA8_OES;
  2454. NoOfpaletteEntry = 16;
  2455. OldpaletteSize = 2;
  2456. NewpaletteSize = 4;
  2457. break;
  2458. case GL_PALETTE4_R5_G6_B5_OES: //16
  2459. texFormat = E_PALETTE4_RGBA8_OES;
  2460. NoOfpaletteEntry = 16;
  2461. OldpaletteSize = 2;
  2462. NewpaletteSize = 4;
  2463. break;
  2464. case GL_PALETTE8_R5_G6_B5_OES: //16
  2465. texFormat = E_PALETTE8_RGBA8_OES;
  2466. NoOfpaletteEntry = 256;
  2467. OldpaletteSize = 2;
  2468. NewpaletteSize = 4;
  2469. break;
  2470. case GL_PALETTE8_RGB5_A1_OES://16
  2471. texFormat = E_PALETTE8_RGBA8_OES;
  2472. NoOfpaletteEntry = 256;
  2473. OldpaletteSize = 2;
  2474. NewpaletteSize = 4;
  2475. break;
  2476. default:
  2477. //SET_ERR(pState, GL_INVALID_ENUM , "glCompressedTexImage3D");
  2478. return ;
  2479. }
  2480. //-------------------------------------------------------------------------
  2481. // COPY TEXTURE DATA
  2482. //-------------------------------------------------------------------------
  2483. GLenum type = internalformat;
  2484. DstTextureFormat = DetermineTextureFormat(internalformat, type, &srcTextureFormat);
  2485. newImageSize = imageSize + NoOfpaletteEntry * (NewpaletteSize -OldpaletteSize) ;
  2486. //FLUSHING THE CACHE
  2487. FimgFinish(pState);
  2488. pImgBuf = getImageDataLocation( pTexObj, level , 0, width, height , depth ,internalformat,internalformat, newImageSize, 0);
  2489. if(pImgBuf == NULL){
  2490. gAssert(false && " unbale to get the memory location \n");
  2491. return;
  2492. }
  2493. GLubyte* tempPixel = (GLubyte*)data;
  2494. convertPixels(DstTextureFormat,(void*)pImgBuf, NoOfpaletteEntry, 1,0,0,0,0,
  2495. srcTextureFormat, (void*) tempPixel, NoOfpaletteEntry, 1,0,0,0,0,
  2496. NoOfpaletteEntry,1,0,0,0,1); //paletteSize is the size of the table to be converted
  2497. Plat::memcpy((pImgBuf+NoOfpaletteEntry * NewpaletteSize) ,
  2498. (tempPixel+NoOfpaletteEntry * OldpaletteSize) ,
  2499. imageSize -NoOfpaletteEntry * OldpaletteSize );
  2500. #ifdef CACHE_MEM
  2501. pCA->cache_clean_invalid(pImgBuf, newImageSize);
  2502. #endif
  2503. tempPixel = NULL;
  2504. //-------------------------------------------------------------------------
  2505. // SET IMAGE PARAMETERS
  2506. //-------------------------------------------------------------------------
  2507. pImgObj->isUsed = GL_TRUE;
  2508. pImgObj->width = width;
  2509. pImgObj->height = height;
  2510. pImgObj->depth = depth;
  2511. pImgObj->internalFormat = internalformat;
  2512. pImgObj->PixType = internalformat;
  2513. pImgObj->imgSize = newImageSize;
  2514. pImgObj->isCompressed = true;
  2515. pImgObj->nativeFormat = texFormat;
  2516. // Set recompile flag
  2517. pTexObj->reCompile = GL_TRUE;
  2518. pTexObj->isDirtyState = GL_TRUE;
  2519. //fprintf(stderr,"\n ### leaving 3D COMPRESSED TEXTURE ###");
  2520. }
  2521. /* no need as the palette texture sub image can't be specified and the
  2522. S3TC image can't be used with 3D texture image*/
  2523. GL_API void GL_APIENTRY glCompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const void *data)
  2524. {
  2525. OGLState* pState = GET_OGL_STATE();
  2526. //TextureState* pTexState = &(pState->texState);
  2527. switch(format){
  2528. case GL_PALETTE4_RGB8_OES:
  2529. case GL_PALETTE4_RGBA8_OES:
  2530. case GL_PALETTE4_R5_G6_B5_OES:
  2531. case GL_PALETTE4_RGBA4_OES:
  2532. case GL_PALETTE4_RGB5_A1_OES:
  2533. case GL_PALETTE8_RGB8_OES:
  2534. case GL_PALETTE8_RGBA8_OES:
  2535. case GL_PALETTE8_R5_G6_B5_OES:
  2536. case GL_PALETTE8_RGBA4_OES:
  2537. case GL_PALETTE8_RGB5_A1_OES:
  2538. SET_ERR(pState, GL_INVALID_OPERATION, "glCompressedTexSubImage3D");
  2539. break;
  2540. default:
  2541. //to check what error to be set
  2542. SET_ERR(pState, GL_INVALID_ENUM , "glCompressedTexSubImage3D");
  2543. break;
  2544. }
  2545. }
  2546. GL_API void GL_APIENTRY glGenerateMipmap(GLenum target)
  2547. {
  2548. OGLState* pState = GET_OGL_STATE();
  2549. //TextureState* pTexState = &(pState->texState);
  2550. //GLuint texRef; // Reference to texture object
  2551. //GLenum texFormat; // Internal texture format
  2552. GLsizei pixelSize; // Bytes per pixel
  2553. GLubyte *pImgBufBase = NULL; // Pointer to image buffer
  2554. Image *pImgObj = NULL; // Pointer to image object
  2555. GLuint cubeFace = 0; // Index of cube face refered
  2556. TextureObject* pTexObj = NULL;
  2557. if(target == GL_TEXTURE_2D){
  2558. pTexObj = GetTextureObject(target,false,0);
  2559. } else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
  2560. pTexObj = GetTextureObject(GL_TEXTURE_CUBE_MAP,false,0);
  2561. cubeFace = (target - GL_TEXTURE_CUBE_MAP_POSITIVE_X);
  2562. } else {
  2563. SET_ERR(pState, GL_INVALID_ENUM, "glGenerateMipMap*");
  2564. return;
  2565. }
  2566. if(target == GL_TEXTURE_2D){
  2567. pImgObj = &(pTexObj->images.tex2D[0]); //0 = base mipmap level....For this api base MipMap level should be defined.
  2568. } else { // Cubemap
  2569. pImgObj = &(pTexObj->images.cubeMap[cubeFace][0]);
  2570. }
  2571. if(pImgObj->isUsed != GL_TRUE /*&& pImgObj->data != NULL */) //in case base level is not defined...before calling this api
  2572. {
  2573. SET_ERR(pState, GL_INVALID_OPERATION, "glGenerateMipMap");
  2574. return ;
  2575. }
  2576. if ( (pImgBufBase = (GLubyte*)pImgObj[0].hImgChunk->GetPhyAddr()) == NULL) //base level data
  2577. {
  2578. SET_ERR(pState, GL_OUT_OF_MEMORY, "glGenerateMipMap");
  2579. return;
  2580. }
  2581. pixelSize = GetPixelSize(pImgObj[0].internalFormat,pImgObj[0].PixType);
  2582. if(pixelSize <= 0) {
  2583. return;
  2584. }
  2585. int maxDim = MAX(pImgObj->width,pImgObj->height);
  2586. //int levels = log(maxDim)/log(2.0) + 1;
  2587. int levels = 0; // levels will be log(maxDim)
  2588. while (maxDim >>= 1) // unroll for more speed...
  2589. levels++;
  2590. void *pImgBuf = NULL;
  2591. void* pImgPrevLev = NULL;
  2592. PxFmt fmt = translateGLInternal(pImgObj[0].internalFormat,pImgObj[0].PixType);
  2593. for(int i = 1; i <= levels; ++i)
  2594. {
  2595. pImgObj[i].isUsed = GL_TRUE;
  2596. pImgObj[i].width = MAX(1,pImgObj[0].width >> i);
  2597. pImgObj[i].height = MAX(1,pImgObj[0].height >> i);
  2598. pImgObj[i].internalFormat = pImgObj[0].internalFormat;
  2599. pImgObj[i].PixType = pImgObj[0].PixType;
  2600. pImgObj[i].imgSize = pImgObj[i].width * pImgObj[i].height * pixelSize; //what about compressed format??.....
  2601. pImgObj[i].isCompressed = pImgObj[0].isCompressed;
  2602. pImgObj[i].hImgChunk = pCA->New(pImgObj[i].imgSize);
  2603. if(pImgObj[i].hImgChunk == NULL || (pImgBuf = pImgObj[i].hImgChunk->GetVirtAddr()) == NULL)
  2604. {
  2605. SET_ERR(pState, GL_OUT_OF_MEMORY, "glGenerateMipMap");
  2606. return;
  2607. }
  2608. if( (pImgPrevLev = pImgObj[i -1].hImgChunk->GetVirtAddr()) == NULL)
  2609. {
  2610. SET_ERR(pState, GL_OUT_OF_MEMORY, "glGenerateMipMap");
  2611. return;
  2612. }
  2613. // Batch ConvertTO RGBA();
  2614. // Take AVG
  2615. // ConvertFromRGBA
  2616. genMipMaps(fmt, pImgPrevLev, pImgBuf, pImgObj[i].width, pImgObj[i].height,pImgObj[i-1].width, pImgObj[i-1].height);
  2617. SwapRB((GLubyte*)pImgBuf,pImgObj[i].internalFormat,pImgObj[i].PixType,pImgObj[i].width,pImgObj[i].height,0,0,0,0,pImgObj[i].width, pImgObj[i].height,0,pixelSize );
  2618. }
  2619. }
  2620. GL_API void GL_APIENTRY glGetTexParameteriv (GLenum target, GLenum pname, GLint *params)
  2621. {
  2622. GET_GL_STATE(ctx);
  2623. //GLuint texRef;
  2624. TextureObject* pTexObj = NULL;
  2625. if(target == GL_TEXTURE_2D || target == GL_TEXTURE_3D || target == GL_TEXTURE_CUBE_MAP){
  2626. pTexObj = GetTextureObject(target,false,0);
  2627. } else {
  2628. SET_ERR(ctx, GL_INVALID_ENUM, "glGetTexParameter");
  2629. return;
  2630. }
  2631. switch(pname)
  2632. {
  2633. case GL_TEXTURE_MIN_FILTER:
  2634. (*params) =pTexObj->minFilter;
  2635. break;
  2636. case GL_TEXTURE_MAG_FILTER:
  2637. (*params) =pTexObj->magFilter;
  2638. break;
  2639. case GL_TEXTURE_WRAP_S:
  2640. (*params) = pTexObj->wrapS;
  2641. break;
  2642. case GL_TEXTURE_WRAP_T:
  2643. (*params) = pTexObj->wrapT;
  2644. break;
  2645. case GL_TEXTURE_WRAP_R:
  2646. (*params) = pTexObj->wrapR;
  2647. break;
  2648. //Texture OES API - 2009.05.20
  2649. case GL_TEXTURE_CROP_RECT_OES:
  2650. params[0] = pTexObj->crop_rect[0];
  2651. params[1] = pTexObj->crop_rect[1];
  2652. params[2] = pTexObj->crop_rect[2];
  2653. params[3] = pTexObj->crop_rect[3];
  2654. break;
  2655. default:
  2656. SET_ERR(ctx, GL_INVALID_ENUM, "glGetTexParameter");
  2657. return;
  2658. }
  2659. }
  2660. GL_API void GL_APIENTRY glGetTexParameterfv (GLenum target, GLenum pname, GLfloat *params)
  2661. {
  2662. glGetTexParameteriv(target,pname,(GLint*)params);
  2663. }
  2664. //Texture OES API - 2009.05.20
  2665. //#ifdef GL_OES_draw_texture
  2666. GL_API void GL_APIENTRY glGetTexLevelParameteriv(GLenum target, GLint level, GLenum pname, GLint *params)
  2667. {
  2668. GET_GL_STATE(ctx);
  2669. TextureObject* pTexObj = NULL;
  2670. Image *pImgObj = NULL;
  2671. if(target == GL_TEXTURE_2D){
  2672. pTexObj = GetTextureObject(target,false,0);
  2673. } else {
  2674. SET_ERR(ctx, GL_INVALID_ENUM, "glGetTexLevelParameteriv");
  2675. return;
  2676. }
  2677. //TODO validate the level value and check whether pTexObj->levels is updated or not
  2678. // Ask shariq
  2679. /*if (level >= pTexObj->levels)
  2680. {
  2681. SET_ERR(ctx, GL_INVALID_VALUE, "glGetTexLevelParameteriv");
  2682. return;
  2683. }*/
  2684. switch (pname)
  2685. {
  2686. case GL_TEXTURE_WIDTH:
  2687. *params = pTexObj->images.tex2D[level].width;
  2688. break;
  2689. case GL_TEXTURE_HEIGHT:
  2690. *params = pTexObj->images.tex2D[level].height;
  2691. break;
  2692. case GL_TEXTURE_INTERNAL_FORMAT:
  2693. *params = pTexObj->images.tex2D[level].internalFormat;
  2694. break;
  2695. default:
  2696. SET_ERR(ctx, GL_INVALID_ENUM, "glGetTexLevelParameteriv");
  2697. return;
  2698. }
  2699. }
  2700. GL_API void GL_APIENTRY glGetTexLevelParameterfv(GLenum target, GLint level, GLenum pname, GLfloat *params)
  2701. {
  2702. }
  2703. //#endif //GL_OES_draw_texture
  2704. /*
  2705. *******************************************************************************
  2706. * Non-GL API functions
  2707. *******************************************************************************
  2708. */
  2709. /*-----------------------------------------------------------------------*//*!
  2710. *
  2711. *//*------------------------------------------------------------------------*/
  2712. void
  2713. InitLocalTextureState (OGLState* pState)
  2714. {
  2715. int i;
  2716. TextureState* pTexState = &(pState->texState);
  2717. for(i = 0; i < 3 ; i++) {
  2718. pTexState->defaultTexObjects[i].reset();
  2719. }
  2720. //TO DO added as memset might add 0 to the set
  2721. //pTexState->defaultTexObjectses.insert(0);
  2722. pTexState->defaultTexObjects[TEX_2D_DEFAULT - TEX_2D_DEFAULT].Init(TEX_2D_DEFAULT,GL_TEXTURE_2D);
  2723. pTexState->defaultTexObjects[TEX_3D_DEFAULT - TEX_2D_DEFAULT].Init(TEX_3D_DEFAULT,GL_TEXTURE_3D);
  2724. pTexState->defaultTexObjects[TEX_CUBE_MAP_DEFAULT - TEX_2D_DEFAULT].Init(TEX_CUBE_MAP_DEFAULT,GL_TEXTURE_CUBE_MAP);
  2725. // Current selection
  2726. pTexState->activeTexUnit = 0;
  2727. // Texture units
  2728. for(i = 0; i < MAX_TEXTURE_UNITS; i++) {
  2729. pTexState->texUnitBinding[i].texture2D = 0;
  2730. pTexState->ptexUnitBinding[i].texture2D = &(pTexState->defaultTexObjects[TEX_2D_DEFAULT - TEX_2D_DEFAULT]);
  2731. pTexState->texUnitBinding[i].texture3D = 0;
  2732. pTexState->ptexUnitBinding[i].texture3D = &(pTexState->defaultTexObjects[TEX_3D_DEFAULT - TEX_2D_DEFAULT]);
  2733. pTexState->texUnitBinding[i].cubeMap = 0;
  2734. pTexState->ptexUnitBinding[i].cubeMap = &(pTexState->defaultTexObjects[TEX_CUBE_MAP_DEFAULT - TEX_2D_DEFAULT]);
  2735. //added for dirty state tracking
  2736. pTexState->prevTexUnitBinding[i] = 0;
  2737. pTexState->pTexObjectToConfigFimg[i].id = 0;
  2738. pTexState->pTexObjectToConfigFimg[i].pTexFGLState = NULL;
  2739. }
  2740. }
  2741. void
  2742. DeInitLocalTextureState(OGLState* pState)
  2743. {
  2744. releaseBoundTextures(pState);
  2745. unsigned int i =0;
  2746. //default texture object
  2747. pState->texState.defaultTexObjects[TEX_2D_DEFAULT - TEX_2D_DEFAULT].Delete();
  2748. pState->texState.defaultTexObjects[TEX_3D_DEFAULT - TEX_2D_DEFAULT].Delete();
  2749. pState->texState.defaultTexObjects[TEX_CUBE_MAP_DEFAULT - TEX_2D_DEFAULT].Delete();
  2750. // Texture units
  2751. pState->texState.activeTexUnit = 0;
  2752. for(i = 0; i < MAX_TEXTURE_UNITS; i++) {
  2753. pState->texState.texUnitBinding[i].texture2D = 0;
  2754. pState->texState.ptexUnitBinding[i].texture2D = &(pState->texState.defaultTexObjects[TEX_2D_DEFAULT - TEX_2D_DEFAULT]);
  2755. pState->texState.texUnitBinding[i].texture3D = 0;
  2756. pState->texState.ptexUnitBinding[i].texture3D = &(pState->texState.defaultTexObjects[TEX_3D_DEFAULT - TEX_2D_DEFAULT]);
  2757. pState->texState.texUnitBinding[i].cubeMap = 0;
  2758. pState->texState.ptexUnitBinding[i].cubeMap = &(pState->texState.defaultTexObjects[TEX_CUBE_MAP_DEFAULT - TEX_2D_DEFAULT]);
  2759. pState->texState.prevTexUnitBinding[i] = 0;
  2760. pState->texState.pTexObjectToConfigFimg[i].id= 0;
  2761. pState->texState.pTexObjectToConfigFimg[i].pTexFGLState = NULL;
  2762. }
  2763. }
  2764. /*
  2765. *******************************************************************************
  2766. * Texture object member functions
  2767. *******************************************************************************
  2768. */
  2769. /*-----------------------------------------------------------------------*//*!
  2770. *
  2771. *//*------------------------------------------------------------------------*/
  2772. inline void
  2773. TextureObject::releaseMem()
  2774. {
  2775. if(isExtTex == GL_TRUE){
  2776. pExtTexchunk.paddr = NULL;
  2777. pExtTexchunk.vaddr = NULL;
  2778. isExtTex = GL_FALSE; // to check
  2779. }else if(hChunk != NULL){
  2780. pCA->Free(hChunk);
  2781. hChunk = NULL;
  2782. }
  2783. }
  2784. //called during initialization of the state
  2785. void
  2786. TextureObject::reset()
  2787. {
  2788. id = 0;
  2789. texType = GLenum(0);
  2790. isUsed = GL_FALSE;
  2791. reCompile = GL_FALSE;
  2792. minFilter = GLenum(0);
  2793. magFilter = GLenum(0);
  2794. wrapS = GLenum(0);
  2795. wrapT = GLenum(0);
  2796. wrapR = GLenum(0);
  2797. internalFormat = GLenum(0);
  2798. nativeFormat = E_INVALID_PIXEL_FORMAT;
  2799. isDirtyState = GL_FALSE;
  2800. for(int j = 0 ; j < MAX_MIPMAP_LEVELS; ++j)
  2801. Offsets[j] = 0;
  2802. Plat::memset(texFGLState.texSFRtate, 0, sizeof(texFGLState.texSFRtate));
  2803. hChunk = NULL;
  2804. width = 0;
  2805. height = 0;
  2806. depth =0;
  2807. texFGLState.palEntryFGLState = NULL;
  2808. texFGLState.noOfpalette = 0;
  2809. Plat::memset(&(images), 0, sizeof(images));
  2810. texObjrefCount = 0;
  2811. deleteTexObj = GL_FALSE;
  2812. pExtTexchunk.paddr = NULL;
  2813. pExtTexchunk.vaddr = NULL;
  2814. isExtTex = GL_FALSE;
  2815. }
  2816. //called when texture object is created
  2817. void
  2818. TextureObject::Init(GLint tex, GLenum type)
  2819. {
  2820. id = tex;
  2821. texType = type;
  2822. isUsed = GL_TRUE;
  2823. reCompile = GL_TRUE;
  2824. // minFilter = GL_NEAREST_MIPMAP_LINEAR; // default value according to spec
  2825. minFilter = GL_LINEAR; // for Android
  2826. magFilter = GL_LINEAR;
  2827. wrapS = GL_REPEAT;
  2828. wrapT = GL_REPEAT;
  2829. wrapR = GL_REPEAT;
  2830. internalFormat = GLenum(-1);
  2831. nativeFormat = E_INVALID_PIXEL_FORMAT;
  2832. isDirtyState = GL_TRUE;
  2833. texFGLState.fglStateDirty = GL_TRUE;
  2834. texFGLState.noOfpalette = 0;
  2835. texFGLState.palEntryFGLState =NULL;
  2836. isExtTex = GL_FALSE;
  2837. pExtTexchunk.paddr = NULL;
  2838. pExtTexchunk.vaddr = NULL;
  2839. for(int i = 0 ; i < MAX_MIPMAP_LEVELS; ++i)
  2840. Offsets[i] = 0;
  2841. deleteTexObj = GL_FALSE;
  2842. switch(type)
  2843. {
  2844. case GL_TEXTURE_2D:
  2845. for(int i = 0; i < MAX_MIPMAP_LEVELS; i++)
  2846. {
  2847. images.tex2D[i].imagedataLocation = NONE;
  2848. }
  2849. break;
  2850. case GL_TEXTURE_CUBE_MAP:
  2851. for(int j = 0 ; j < 6; j++)
  2852. for(int i = 0; i < MAX_MIPMAP_LEVELS; i++)
  2853. {
  2854. images.cubeMap[j][i].imagedataLocation = NONE;
  2855. }
  2856. break;
  2857. case GL_TEXTURE_3D:
  2858. for(int i = 0; i < MAX_MIPMAP_LEVELS; i++)
  2859. {
  2860. images.tex3D[i].imagedataLocation = NONE;
  2861. }
  2862. break;
  2863. }
  2864. #ifdef SHARED_CONTEXT_DEBUG
  2865. //LOGMSG(" id being init is = % d \n" , id);
  2866. #endif
  2867. }
  2868. /*-----------------------------------------------------------------------*//*!
  2869. *
  2870. *//*------------------------------------------------------------------------*/
  2871. //called when the texture object is deleted or deinitailization of the state
  2872. //TO DO: Now no need to reset teh vlaue only need to release any memory used
  2873. void
  2874. TextureObject::Delete()
  2875. {
  2876. #if 0
  2877. if(!(texObjrefCount == 0) ||(texObjrefCount == -1))
  2878. return;
  2879. #endif
  2880. isUsed = GL_FALSE;
  2881. reCompile = GL_TRUE;
  2882. isDirtyState = GL_TRUE;
  2883. // resetting the size
  2884. width = 0;
  2885. height = 0;
  2886. depth = 0;
  2887. #ifdef SHARED_CONTEXT_DEBUG
  2888. //LOGMSG("\n id being deleted is = % d \n" , id);
  2889. #endif
  2890. //not needed added just to be sure
  2891. minFilter = GL_NEAREST_MIPMAP_LINEAR;
  2892. magFilter = GL_LINEAR;
  2893. wrapS = GL_REPEAT;
  2894. wrapT = GL_REPEAT;
  2895. wrapR = GL_REPEAT;
  2896. internalFormat = GLenum(-1);
  2897. nativeFormat = E_INVALID_PIXEL_FORMAT;
  2898. if (texFGLState.palEntryFGLState != NULL){
  2899. Plat::safe_free(texFGLState.palEntryFGLState);
  2900. }
  2901. //Plat::memset((void *)texFGLState.palEntryFGLState, 0, sizeof(texFGLState.palEntryFGLState));
  2902. #if 0
  2903. //freeing the texture memory
  2904. if(hChunk != NULL)
  2905. {
  2906. pCA->Free(hChunk);
  2907. hChunk = NULL;
  2908. }
  2909. pExtTexchunk.paddr = NULL;
  2910. pExtTexchunk.vaddr = NULL;
  2911. #else
  2912. releaseMem();
  2913. #endif
  2914. isExtTex = GL_FALSE;
  2915. switch(texType)
  2916. {
  2917. case GL_TEXTURE_2D:
  2918. for(int i = 0; i < MAX_TEXTURE_UNITS; i++) // or it should be isUsed as uncompiled texture may also be deleted. TO DO
  2919. {
  2920. if(images.tex2D[i].hImgChunk != NULL){
  2921. pCA->Free(images.tex2D[i].hImgChunk);
  2922. images.tex2D[i].hImgChunk = NULL;
  2923. }
  2924. //Resetting the values of the image structure.
  2925. images.tex2D[i].imgSize = 0;
  2926. images.tex2D[i].isUsed = GL_FALSE;
  2927. images.tex2D[i].width = 0;
  2928. images.tex2D[i].height = 0;
  2929. images.tex2D[i].depth = 0;
  2930. //not needed added just to be sure
  2931. images.tex2D[i].internalFormat = GLenum(-1);
  2932. images.tex2D[i].PixType = GLenum(-1);
  2933. images.tex2D[i].nativeFormat = E_INVALID_PIXEL_FORMAT;
  2934. images.tex2D[i].isCompressed = GL_FALSE;
  2935. }
  2936. break;
  2937. case GL_TEXTURE_CUBE_MAP:
  2938. for(int j = 0 ; j < 6; j++)
  2939. for(int i = 0; i < MAX_TEXTURE_UNITS; i++)
  2940. {
  2941. if(images.cubeMap[j][i].hImgChunk != NULL){
  2942. pCA->Free(images.cubeMap[j][i].hImgChunk);
  2943. images.cubeMap[j][i].hImgChunk = NULL;
  2944. }
  2945. //Resetting the values of the image structure.
  2946. images.cubeMap[j][i].imgSize = 0;
  2947. images.cubeMap[j][i].isUsed = GL_FALSE;
  2948. images.cubeMap[j][i].width = 0;
  2949. images.cubeMap[j][i].height = 0;
  2950. images.cubeMap[j][i].depth = 0;
  2951. //not needed added just to be sure
  2952. images.cubeMap[j][i].internalFormat = GLenum(-1);
  2953. images.cubeMap[j][i].PixType = GLenum(-1);
  2954. images.cubeMap[j][i].nativeFormat = E_INVALID_PIXEL_FORMAT;
  2955. images.cubeMap[j][i].isCompressed = GL_FALSE;
  2956. }
  2957. break;
  2958. case GL_TEXTURE_3D:
  2959. for(int i = 0; i < MAX_TEXTURE_UNITS; i++)
  2960. {
  2961. if(images.tex3D[i].hImgChunk != NULL){
  2962. pCA->Free(images.tex3D[i].hImgChunk);
  2963. images.tex3D[i].hImgChunk = NULL;
  2964. }
  2965. //Resetting the values of the image structure.
  2966. images.tex3D[i].imgSize = 0;
  2967. images.tex3D[i].isUsed = GL_FALSE;
  2968. images.tex3D[i].width = 0;
  2969. images.tex3D[i].height = 0;
  2970. images.tex3D[i].depth = 0;
  2971. //not needed added just to be sure
  2972. images.tex3D[i].internalFormat = GLenum(-1);
  2973. images.tex3D[i].PixType = GLenum(-1);
  2974. images.tex3D[i].nativeFormat = E_INVALID_PIXEL_FORMAT;
  2975. images.tex3D[i].isCompressed = GL_FALSE;
  2976. }
  2977. break;
  2978. }
  2979. // Free image pointer
  2980. // Free each individual image
  2981. deleteTexObj = GL_FALSE;
  2982. levels = 0;
  2983. texType = GLenum(-1);
  2984. }
  2985. void
  2986. inline TextureObject::acquire(OGLState* pState)
  2987. {
  2988. if(id == 0){
  2989. //LOGMSG(" zero can not be released . should have have entered the function : %s \n" , __FUNCTION__);
  2990. return;
  2991. }
  2992. if(deleteTexObj == GL_TRUE){
  2993. //LOGMSG(" \n trying to acqire a texture object marked for deletion . ISSUE UNRESOLVED ");
  2994. }
  2995. gAssert(pState->sharedState->sharedStateMutexAcquired && "mutex should have been acquired");
  2996. texObjrefCount++;
  2997. #ifdef SHARED_CONTEXT_DEBUG
  2998. //LOGMSG(" refCount of %d is % d : %s \n" , id , texObjrefCount , __FUNCTION__);
  2999. #endif
  3000. }
  3001. GLboolean
  3002. inline TextureObject::release(OGLState* pState)
  3003. {
  3004. if(id == 0){
  3005. //LOGMSG(" zero can not be released . should have have entered the function : %s \n" , __FUNCTION__);
  3006. return GL_FALSE;
  3007. }
  3008. gAssert(pState->sharedState->sharedStateMutexAcquired && "mutex should have been acquired");
  3009. texObjrefCount--;
  3010. //gAssert(texObjrefCount >= -1 && " ref count cannot be less than zero. " );
  3011. #ifdef SHARED_CONTEXT_DEBUG
  3012. //LOGMSG(" refCount of %d is % d : %s \n" , id , texObjrefCount , __FUNCTION__);
  3013. #endif
  3014. if((texObjrefCount < 0) && ( deleteTexObj == GL_TRUE))
  3015. {
  3016. // Delete texture object
  3017. if(isUsed == GL_TRUE){
  3018. Delete();
  3019. }
  3020. return GL_TRUE;
  3021. }
  3022. return GL_FALSE;
  3023. }
  3024. /*-----------------------------------------------------------------------*//*!
  3025. *
  3026. *//*------------------------------------------------------------------------*/
  3027. GLboolean
  3028. TextureObject::IsComplete(int* levels)
  3029. {
  3030. GLint currWidth = 1, currHeight = 1, currDepth = 1;
  3031. int i =0;
  3032. //-------------------------------------------------------------------------
  3033. // Check texture object state
  3034. //-------------------------------------------------------------------------
  3035. if(isUsed != GL_TRUE)
  3036. return GL_FALSE;
  3037. // Texture type
  3038. if(!(texType == GL_TEXTURE_2D || texType == GL_TEXTURE_3D || texType == GL_TEXTURE_CUBE_MAP))
  3039. return GL_FALSE;
  3040. // Minification and Magnification filters
  3041. if(!((minFilter == GL_LINEAR) || (minFilter == GL_NEAREST) || (minFilter == GL_LINEAR_MIPMAP_LINEAR)
  3042. || (minFilter == GL_LINEAR_MIPMAP_NEAREST) || (minFilter == GL_NEAREST_MIPMAP_LINEAR) ||
  3043. (minFilter == GL_NEAREST_MIPMAP_NEAREST)))
  3044. return GL_FALSE;
  3045. if(!((magFilter == GL_LINEAR) || (magFilter == GL_NEAREST)))
  3046. return GL_FALSE;
  3047. // Wrap modes
  3048. if(!((wrapS == GL_REPEAT) || (wrapS == GL_CLAMP_TO_EDGE) || (wrapS == GL_MIRRORED_REPEAT)))
  3049. return GL_FALSE;
  3050. if(!((wrapT == GL_REPEAT) || (wrapT == GL_CLAMP_TO_EDGE) || (wrapT == GL_MIRRORED_REPEAT)))
  3051. return GL_FALSE;
  3052. if(texType == GL_TEXTURE_3D && !((wrapR == GL_REPEAT) || (wrapR == GL_CLAMP_TO_EDGE) || (wrapR == GL_MIRRORED_REPEAT)))
  3053. return GL_FALSE;
  3054. //-------------------------------------------------------------------------
  3055. // Check image format and specifications
  3056. //-------------------------------------------------------------------------
  3057. switch (texType) {
  3058. case GL_TEXTURE_2D:
  3059. if(images.tex2D[0].isUsed != GL_TRUE)
  3060. return GL_FALSE;
  3061. currWidth = images.tex2D[0].width;
  3062. currHeight = images.tex2D[0].height;
  3063. break;
  3064. case GL_TEXTURE_3D:
  3065. if(images.tex3D[0].isUsed != GL_TRUE)
  3066. return GL_FALSE;
  3067. currWidth = images.tex3D[0].width;
  3068. currHeight = images.tex3D[0].height;
  3069. currDepth = images.tex3D[0].depth;
  3070. break;
  3071. case GL_TEXTURE_CUBE_MAP:
  3072. if(images.cubeMap[0][0].isUsed != GL_TRUE)
  3073. return GL_FALSE;
  3074. // Check base level cube-map images
  3075. if(images.cubeMap[0][0].width != images.cubeMap[0][0].height)
  3076. return GL_FALSE;
  3077. for(i = 0; i < 6; i++) {
  3078. if((images.cubeMap[i][0].internalFormat != images.cubeMap[0][0].internalFormat) ||
  3079. (images.cubeMap[i][0].width != images.cubeMap[0][0].width) ||
  3080. (images.cubeMap[i][0].height != images.cubeMap[0][0].height)) {
  3081. return GL_FALSE;
  3082. }
  3083. }
  3084. currWidth = images.cubeMap[0][0].width;
  3085. currHeight = images.cubeMap[0][0].height;
  3086. break;
  3087. }
  3088. #if(OES_TEXTURE_NPOT == DISABLE)
  3089. //-------------------------------------------------------------------------
  3090. // Check Power of Two dimensions
  3091. //-------------------------------------------------------------------------
  3092. if(IsPowerOf2(currWidth) != GL_TRUE || IsPowerOf2(currHeight) != GL_TRUE ||
  3093. (texType == GL_TEXTURE_3D && IsPowerOf2(currDepth) != GL_TRUE)) {
  3094. // Minification: Nearest of Linear
  3095. if(!(minFilter == GL_NEAREST || minFilter == GL_LINEAR))
  3096. return GL_FALSE;
  3097. // Wrap: Clamp to edge
  3098. if(wrapS != GL_CLAMP_TO_EDGE || wrapT != GL_CLAMP_TO_EDGE ||
  3099. (texType == GL_TEXTURE_3D && wrapR != GL_CLAMP_TO_EDGE))
  3100. return GL_FALSE;
  3101. }
  3102. #endif
  3103. //-------------------------------------------------------------------------
  3104. // Check dimensions of each mip-map level
  3105. //-------------------------------------------------------------------------
  3106. int level =1; //will determine the level to be used
  3107. if((minFilter == GL_LINEAR_MIPMAP_LINEAR) || (minFilter == GL_LINEAR_MIPMAP_NEAREST) ||
  3108. (minFilter == GL_NEAREST_MIPMAP_LINEAR) || (minFilter == GL_NEAREST_MIPMAP_NEAREST)) {
  3109. for( ; level < MAX_MIPMAP_LEVELS; level++) {
  3110. // Check complete if we reach the level-max
  3111. if(texType == GL_TEXTURE_3D){
  3112. if(currWidth == 1 && currHeight == 1 && currDepth == 1)
  3113. break;
  3114. } else {
  3115. if(currWidth == 1 && currHeight == 1)
  3116. break;
  3117. }
  3118. // Current dimension (Divide by 2). Clamp to a lower limit of 1
  3119. currWidth = ((currWidth / 2) > 1) ? (currWidth / 2) : 1;
  3120. currHeight = ((currHeight / 2) > 1) ? (currHeight / 2) : 1;
  3121. currDepth = ((currDepth / 2) > 1) ? (currDepth / 2) : 1;
  3122. switch (texType) {
  3123. case GL_TEXTURE_2D:
  3124. if(images.tex2D[level].isUsed != GL_TRUE || images.tex2D[level].internalFormat != images.tex2D[0].internalFormat ||
  3125. images.tex2D[level].width != currWidth || images.tex2D[level].height != currHeight) {
  3126. return GL_FALSE;
  3127. }
  3128. break;
  3129. case GL_TEXTURE_3D:
  3130. if(images.tex3D[level].isUsed != GL_TRUE || images.tex3D[level].internalFormat != images.tex3D[0].internalFormat ||
  3131. images.tex3D[level].width != currWidth || images.tex3D[level].height != currHeight || images.tex3D[level].depth != currDepth) {
  3132. return GL_FALSE;
  3133. }
  3134. break;
  3135. case GL_TEXTURE_CUBE_MAP:
  3136. if(images.cubeMap[0][level].isUsed != GL_TRUE || images.cubeMap[0][level].internalFormat != images.cubeMap[0][0].internalFormat ||
  3137. images.cubeMap[0][level].width != currWidth || images.cubeMap[0][level].height != currHeight) {
  3138. return GL_FALSE;
  3139. }
  3140. break;
  3141. }
  3142. }
  3143. // We were finished with all mipmap levels before reaching the smallest image (1 x 1 x 1)
  3144. if(level > MAX_MIPMAP_LEVELS){
  3145. return GL_FALSE;
  3146. }
  3147. }
  3148. // All tests are complete without failure
  3149. *levels = level;
  3150. return GL_TRUE;
  3151. }
  3152. /*-----------------------------------------------------------------------*//*!
  3153. *
  3154. *//*------------------------------------------------------------------------*/
  3155. GLboolean
  3156. TextureObject::Compile ()
  3157. {
  3158. GLubyte *pImgBuf = NULL; // Image buffer
  3159. GLuint imgSize, pixSize; // Image size, Pixel size
  3160. int i = 0, j = 0; // Temporary counter
  3161. int level = 0;
  3162. // Return if texture object state has not changed.
  3163. if(isDirtyState == GL_FALSE) { //fi dirty state is false the recompile will be false
  3164. return GL_TRUE;
  3165. }
  3166. // As the object state has changed check the texture completeness
  3167. if(IsComplete(&level) != GL_TRUE){
  3168. return GL_FALSE;
  3169. }
  3170. //-------------------------------------------------------------------------
  3171. // Image buffer handling
  3172. //-------------------------------------------------------------------------
  3173. if(reCompile == GL_TRUE){
  3174. if( hChunk != NULL){
  3175. transferImageFromTexChunckToImageChunck(this);
  3176. }
  3177. //-------------------------------------------------------------------------
  3178. // Compute texture parameters and compile various images into a single image
  3179. //-------------------------------------------------------------------------
  3180. switch (texType){
  3181. case GL_TEXTURE_2D:
  3182. // parameters
  3183. width = images.tex2D[0].width;
  3184. height = images.tex2D[0].height;
  3185. internalFormat = images.tex2D[0].internalFormat;
  3186. nativeFormat = images.tex2D[0].nativeFormat;
  3187. levels = level;
  3188. // *Pra* If loop completes without a break .. It is an error! .. But this should never happen in
  3189. // normal case of isComplete()
  3190. if(!images.tex2D[0].isCompressed)
  3191. {
  3192. //TO DO IF LEVEL IS 1 THEN NEED TO USE THAT TEXTURE IMAGE ONLY SAME FOR 3D
  3193. if( levels == 1){
  3194. hChunk = images.tex2D[0].hImgChunk;
  3195. images.tex2D[0].hImgChunk = NULL;
  3196. images.tex2D[0].imagedataLocation = TEXOBJCHUNK;
  3197. }else{
  3198. // Pixel size
  3199. pixSize = pixelSize(nativeFormat);
  3200. // Allocate memory chunk for the compiled image
  3201. hChunk = pCA->New(CompiledSize());
  3202. if(hChunk == NULL || (pImgBuf = (GLubyte*) hChunk->GetVirtAddr()) == NULL) {
  3203. //LOGMSG("Unable to get new chunk\n");
  3204. return GL_FALSE;
  3205. }
  3206. //offset
  3207. for(int j=1; j < levels; j++){
  3208. Offsets[j]=Offsets[j-1]+ (images.tex2D[j-1].width * images.tex2D[j-1].height);
  3209. }
  3210. // Copy images together
  3211. for(i = 0; i < levels; i++) {
  3212. imgSize = images.tex2D[i].width * images.tex2D[i].height * pixSize;
  3213. Plat::memcpy( pImgBuf , images.tex2D[i].hImgChunk->GetVirtAddr(), imgSize );
  3214. pCA->Free(images.tex2D[i].hImgChunk);
  3215. // Increment pointer
  3216. pImgBuf += imgSize;
  3217. images.tex2D[i].hImgChunk = NULL;
  3218. images.tex2D[i].imagedataLocation = TEXOBJCHUNK;
  3219. }
  3220. }
  3221. }
  3222. else
  3223. {
  3224. //compressed images...all the mipmap levels are in the data..it at all they are there. So no compiling is required.??????
  3225. // parameters
  3226. //HOW TO SET THE OFFSET TO DO
  3227. hChunk = pCA->New(images.tex2D[0].imgSize);
  3228. if(hChunk == NULL || (pImgBuf = (GLubyte*) hChunk->GetVirtAddr()) == NULL) {
  3229. //LOGMSG("Unable to get new chunk\n");
  3230. return GL_FALSE;
  3231. }
  3232. Plat::memcpy( pImgBuf , images.tex2D[0].hImgChunk->GetVirtAddr(), images.tex2D[0].imgSize);
  3233. pCA->Free(images.tex2D[0].hImgChunk);
  3234. images.tex2D[0].hImgChunk = NULL;
  3235. images.tex2D[0].imagedataLocation = TEXOBJCHUNK;
  3236. //images.tex2D[0].isCompressed = false;
  3237. }
  3238. break;
  3239. case GL_TEXTURE_CUBE_MAP:
  3240. // parameters
  3241. width = images.cubeMap[0][0].width;
  3242. height = images.cubeMap[0][0].height;
  3243. internalFormat = images.cubeMap[0][0].internalFormat;
  3244. nativeFormat = images.cubeMap[0][0].nativeFormat; //added
  3245. levels = level;
  3246. if(!images.cubeMap[0][0].isCompressed)
  3247. {
  3248. // Pixel size
  3249. pixSize = pixelSize(nativeFormat);
  3250. // Allocate memory chunk for the compiled imagep
  3251. hChunk = pCA->New(CompiledSize());
  3252. if(hChunk == NULL || (pImgBuf = (GLubyte*) hChunk->GetVirtAddr()) == NULL) {
  3253. //LOGMSG("Unable to get new chunk\n");
  3254. return GL_FALSE;
  3255. }
  3256. GLuint offSetSize =0;
  3257. //offset
  3258. for(i = 1; i < levels; i++) {
  3259. Offsets[i] = Offsets[i-1]+ (images.cubeMap[0][0].width >> (i-1) )* (images.cubeMap[0][0].height >> (i-1));
  3260. //LOGMSG("Offsets[%d] = %d \n", i, Offsets[i]);
  3261. }
  3262. //calculation for mipmap offset for storing Image data as In case of cubemap
  3263. // mipmap should be enabled. And for each face data should be stored in mipmap aligned memory not
  3264. // in continous order.
  3265. GLubyte *pfaceStartBuf = pImgBuf;
  3266. GLuint twidth = width;
  3267. GLuint toffset = 0;
  3268. for( ; twidth >= 1 ; twidth>>=1 )
  3269. {
  3270. toffset += twidth * twidth * pixSize;
  3271. }
  3272. // Copy images together
  3273. for(j = 0; j < 6; j++) {
  3274. for(i = 0; i < levels; i++) {
  3275. imgSize = images.cubeMap[j][i].width * images.cubeMap[j][i].height * pixSize;
  3276. offSetSize = (images.cubeMap[j][0].width >> i) * (images.cubeMap[j][0].height >> i) * pixSize;
  3277. //LOGMSG("ImageSize[Level = %d] = %d \n",i,imgSize);
  3278. //LOGMSG("offSetSize[Level = %d] = %d \n",i,offSetSize);
  3279. //LOGMSG("pImgBuf = %p\n",pImgBuf);
  3280. Plat::memcpy( pImgBuf , images.cubeMap[j][i].hImgChunk->GetVirtAddr() , imgSize);
  3281. pCA->Free(images.cubeMap[j][i].hImgChunk);
  3282. images.cubeMap[j][i].hImgChunk = NULL;
  3283. images.cubeMap[j][i].imagedataLocation = TEXOBJCHUNK;
  3284. // Increment pointer
  3285. pImgBuf += offSetSize;
  3286. }
  3287. pImgBuf = pfaceStartBuf + ( j+1) * toffset;
  3288. }
  3289. fprintf(stderr, "\n image compiled ");
  3290. }
  3291. else
  3292. {
  3293. //level is already set in the glCompressed...... call.????
  3294. // Allocate buffer
  3295. hChunk = pCA->New(images.cubeMap[0][0].imgSize);
  3296. if(hChunk == NULL || (pImgBuf = (GLubyte*) hChunk->GetVirtAddr()) == NULL) {
  3297. //LOGMSG("Unable to get new chunk\n");
  3298. return GL_FALSE;
  3299. }
  3300. Plat::memcpy( pImgBuf /* Dst */, images.cubeMap[0][0].hImgChunk->GetVirtAddr()/*images.cubeMap[0][0].data Src */, images.cubeMap[0][0].imgSize /* Size */);
  3301. pCA->Free(images.cubeMap[0][0].hImgChunk);
  3302. images.cubeMap[0][0].hImgChunk = NULL;
  3303. images.cubeMap[0][0].imagedataLocation = TEXOBJCHUNK;
  3304. }
  3305. break;
  3306. case GL_TEXTURE_3D:
  3307. // parameters
  3308. width = images.tex3D[0].width;
  3309. height = images.tex3D[0].height;
  3310. depth = images.tex3D[0].depth;
  3311. internalFormat = images.tex3D[0].internalFormat;
  3312. nativeFormat = images.tex3D[0].nativeFormat;
  3313. levels = level;
  3314. if(!images.tex3D[0].isCompressed)
  3315. {
  3316. if(levels == 1){
  3317. hChunk = images.tex3D[0].hImgChunk;
  3318. images.tex3D[0].hImgChunk = NULL;
  3319. images.tex3D[0].imagedataLocation = TEXOBJCHUNK;
  3320. }else{
  3321. // Pixel size
  3322. pixSize = pixelSize(nativeFormat);
  3323. // Allocate memory chunk for the compiled image
  3324. hChunk = pCA->New(CompiledSize());
  3325. if(hChunk == NULL || (pImgBuf = (GLubyte*) hChunk->GetVirtAddr()) == NULL) {
  3326. //LOGMSG("Unable to get new chunk\n");
  3327. return GL_FALSE;
  3328. }
  3329. //offset
  3330. for(i = 1; i < levels; i++){
  3331. Offsets[i] = Offsets[i-1]+ (images.cubeMap[0][0].width >> (i-1) )* (images.cubeMap[0][0].height >> (i-1));
  3332. //LOGMSG("Offsets[%d] = %d \n", i, Offsets[i]);
  3333. }
  3334. // Copy images together
  3335. for(i = 0; i < levels; i++) {
  3336. imgSize = images.tex3D[i].width * images.tex3D[i].height * images.tex3D[i].depth * pixSize;
  3337. Plat::memcpy( pImgBuf , images.tex3D[i].hImgChunk->GetVirtAddr(), imgSize );
  3338. pCA->Free(images.tex3D[i].hImgChunk);
  3339. images.tex3D[i].hImgChunk = NULL;
  3340. // Increment pointer
  3341. pImgBuf += imgSize;
  3342. images.tex3D[i].imagedataLocation = TEXOBJCHUNK;
  3343. }
  3344. }
  3345. }
  3346. else
  3347. {
  3348. width = images.tex3D[0].width;
  3349. height = images.tex3D[0].height;
  3350. depth = images.tex3D[0].depth;
  3351. internalFormat = images.tex3D[0].internalFormat;
  3352. nativeFormat = images.tex3D[0].nativeFormat;
  3353. // levels has been set in the glCompressed..... call only.???
  3354. hChunk = pCA->New(images.tex3D[0].imgSize);
  3355. if(hChunk == NULL || (pImgBuf = (GLubyte*) hChunk->GetVirtAddr()) == NULL) {
  3356. //LOGMSG("Unable to get new chunk\n");
  3357. return GL_FALSE;
  3358. }
  3359. ////LOGMSG("pImageBuf %p imgsize %d \n",pImgBuf,images.tex3D[0].imgSize);
  3360. ////LOGMSG("virtAddr %p \n",images.tex3D[0].hImgChunk->GetVirtAddr());
  3361. Plat::memcpy( pImgBuf , images.tex3D[0].hImgChunk->GetVirtAddr(), images.tex3D[0].imgSize);
  3362. pCA->Free(images.tex3D[0].hImgChunk);
  3363. images.tex3D[0].hImgChunk = NULL;
  3364. images.tex3D[i].imagedataLocation = TEXOBJCHUNK;
  3365. }
  3366. break;
  3367. }
  3368. // Compile done. Mark flag
  3369. reCompile = GL_FALSE;
  3370. }
  3371. if(updateTexFGLState() == GL_FALSE){
  3372. return GL_FALSE;
  3373. }
  3374. return GL_TRUE;
  3375. }
  3376. GLboolean
  3377. TextureObject:: updateTexFGLState()
  3378. {
  3379. FGL_TexStatusParams texParams = {
  3380. FGL_TEX_2D,
  3381. FGL_CKEY_DISABLE,
  3382. FGL_TRUE, // Zero-padding
  3383. FGL_PALETTE_ARGB8888,
  3384. FGL_TEXEL_ARGB1555,
  3385. FGL_TEX_WRAP_REPEAT,
  3386. FGL_TEX_WRAP_REPEAT,
  3387. FGL_FALSE,
  3388. FGL_FALSE,
  3389. FGL_FALSE,
  3390. FGL_FILTER_DISABLE };
  3391. if(1 == 1){
  3392. switch(minFilter)
  3393. {
  3394. case GL_LINEAR:
  3395. texParams.bUseMinFilter = FGL_TRUE;
  3396. texParams.eMipMapFilter = FGL_FILTER_DISABLE;
  3397. break;
  3398. case GL_NEAREST:
  3399. texParams.bUseMinFilter = FGL_FALSE;
  3400. texParams.eMipMapFilter = FGL_FILTER_DISABLE;
  3401. break;
  3402. case GL_LINEAR_MIPMAP_LINEAR:
  3403. texParams.bUseMinFilter = FGL_TRUE;
  3404. texParams.eMipMapFilter = FGL_FILTER_LINEAR;
  3405. break;
  3406. case GL_LINEAR_MIPMAP_NEAREST:
  3407. texParams.bUseMinFilter = FGL_TRUE;
  3408. texParams.eMipMapFilter = FGL_FILTER_NEAREST;
  3409. break;
  3410. case GL_NEAREST_MIPMAP_LINEAR:
  3411. texParams.bUseMinFilter = FGL_FALSE;
  3412. texParams.eMipMapFilter = FGL_FILTER_LINEAR;
  3413. break;
  3414. case GL_NEAREST_MIPMAP_NEAREST:
  3415. texParams.bUseMinFilter = FGL_FALSE;
  3416. texParams.eMipMapFilter = FGL_FILTER_NEAREST;
  3417. break;
  3418. }
  3419. switch(magFilter)
  3420. {
  3421. case GL_NEAREST:
  3422. texParams.bUseMagFilter = FGL_FALSE;
  3423. break;
  3424. case GL_LINEAR:
  3425. texParams.bUseMagFilter = FGL_TRUE;
  3426. }
  3427. //GLenum pType,pFormat;
  3428. PxFmt texFormat=E_INVALID_PIXEL_FORMAT; // texture format to be used
  3429. // Texture type
  3430. switch(texType)
  3431. {
  3432. case GL_TEXTURE_2D:
  3433. texParams.eType = FGL_TEX_2D;
  3434. //pType = images.tex2D[0].PixType;
  3435. //pFormat = images.tex2D[0].internalFormat;
  3436. texFormat = nativeFormat; //images.tex2D[0].nativeFormat; //nativeFormat TO DO
  3437. break;
  3438. case GL_TEXTURE_3D:
  3439. texParams.eType = FGL_TEX_3D;
  3440. // pType = images.tex3D[0].PixType;
  3441. //pFormat = images.tex3D[0].internalFormat;
  3442. texFormat = nativeFormat; //images.tex3D[0].nativeFormat;
  3443. break;
  3444. case GL_TEXTURE_CUBE_MAP:
  3445. texParams.eType = FGL_TEX_CUBE;
  3446. texParams.eMipMapFilter = FGL_FILTER_NEAREST;
  3447. //pType = images.cubeMap[0][0].PixType;
  3448. // pFormat = images.cubeMap[0][0].internalFormat;
  3449. texFormat = nativeFormat; //images.cubeMap[0][0].nativeFormat;
  3450. break;
  3451. }
  3452. int offsetFromHeader = 0;
  3453. unsigned short numOfPaletteEntry = 0;
  3454. switch(texFormat)
  3455. {
  3456. case E_LUMINANCE8:
  3457. texParams.eFormat = FGL_TEXEL_I8;
  3458. break;
  3459. case E_LUMINANCE_ALPHA88: //check the format
  3460. texParams.eFormat = FGL_TEXEL_IA88;
  3461. break;
  3462. case E_LUMINANCE_ALPHA80: //check the format
  3463. texParams.eFormat = FGL_TEXEL_IA88;
  3464. break;
  3465. case E_LUMINANCE_ALPHA08:
  3466. texParams.eFormat = FGL_TEXEL_IA88;
  3467. break;
  3468. case E_ARGB8:
  3469. texParams.eFormat = FGL_TEXEL_ARGB8888;
  3470. break;
  3471. case E_ARGB4:
  3472. texParams.eFormat = FGL_TEXEL_ARGB4444;
  3473. break;
  3474. case E_RGB565:
  3475. texParams.eFormat = FGL_TEXEL_RGB565;
  3476. break;
  3477. case E_ARGB1555:
  3478. texParams.eFormat = FGL_TEXEL_ARGB1555;
  3479. break;
  3480. // to see for all these case later
  3481. case E_RGB_S3TC_OES:
  3482. case E_RGBA_S3TC_OES:
  3483. texParams.eFormat = FGL_TEXEL_S3TC;
  3484. break;
  3485. case E_PALETTE4_RGB8_OES:
  3486. texParams.eFormat = FGL_TEXEL_4BPP;
  3487. texParams.ePaletteFormat = FGL_PALETTE_ARGB8888;
  3488. numOfPaletteEntry = 16;
  3489. offsetFromHeader = numOfPaletteEntry * 3; //this is the byte offset from the start of a palette image. this header is the palette for the image.
  3490. break;
  3491. case E_PALETTE4_RGBA8_OES:
  3492. texParams.eFormat = FGL_TEXEL_4BPP;
  3493. texParams.ePaletteFormat = FGL_PALETTE_ARGB8888;
  3494. numOfPaletteEntry = 16;
  3495. offsetFromHeader = numOfPaletteEntry * 4; //this is the byte offset from the start of a palette image. this header is the palette for the image.
  3496. break;
  3497. case E_PALETTE4_R5_G6_B5_OES:
  3498. texParams.eFormat = FGL_TEXEL_4BPP;
  3499. texParams.ePaletteFormat = FGL_PALETTE_RGB565;
  3500. numOfPaletteEntry = 16;
  3501. offsetFromHeader = numOfPaletteEntry * 2; //this is the byte offset from the start of a palette image. this header is the palette for the image.
  3502. break;
  3503. case E_PALETTE4_RGBA4_OES:
  3504. texParams.eFormat = FGL_TEXEL_4BPP;
  3505. texParams.ePaletteFormat = FGL_PALETTE_ARGB4444;
  3506. numOfPaletteEntry = 16;
  3507. offsetFromHeader = numOfPaletteEntry * 2; //this is the byte offset from the start of a palette image. this header is the palette for the image.
  3508. break;
  3509. case E_PALETTE4_RGB5_A1_OES:
  3510. texParams.eFormat = FGL_TEXEL_4BPP;
  3511. texParams.ePaletteFormat = FGL_PALETTE_ARGB1555;
  3512. numOfPaletteEntry = 16;
  3513. offsetFromHeader = numOfPaletteEntry * 2;
  3514. break;
  3515. case E_PALETTE8_RGB8_OES:
  3516. texParams.eFormat = FGL_TEXEL_8BPP;
  3517. texParams.ePaletteFormat = FGL_PALETTE_ARGB8888;
  3518. numOfPaletteEntry = 256;
  3519. offsetFromHeader = numOfPaletteEntry * 3;
  3520. break;
  3521. case E_PALETTE8_RGBA8_OES:
  3522. texParams.eFormat = FGL_TEXEL_8BPP;
  3523. texParams.ePaletteFormat = FGL_PALETTE_ARGB8888;
  3524. numOfPaletteEntry = 256;
  3525. offsetFromHeader = numOfPaletteEntry * 4;
  3526. break;
  3527. case E_PALETTE8_R5_G6_B5_OES:
  3528. texParams.eFormat = FGL_TEXEL_8BPP;
  3529. texParams.ePaletteFormat = FGL_PALETTE_RGB565;
  3530. numOfPaletteEntry = 256;
  3531. offsetFromHeader = numOfPaletteEntry * 2;
  3532. break;
  3533. case E_PALETTE8_RGBA4_OES:
  3534. texParams.eFormat = FGL_TEXEL_8BPP;
  3535. texParams.ePaletteFormat = FGL_PALETTE_ARGB4444;
  3536. numOfPaletteEntry = 256;
  3537. offsetFromHeader = numOfPaletteEntry * 2;
  3538. break;
  3539. case E_PALETTE8_RGB5_A1_OES:
  3540. texParams.eFormat = FGL_TEXEL_8BPP;
  3541. texParams.ePaletteFormat = FGL_PALETTE_ARGB1555;
  3542. numOfPaletteEntry = 256;
  3543. offsetFromHeader = numOfPaletteEntry * 2;
  3544. break;
  3545. default:
  3546. texParams.eFormat = FGL_TEXEL_ARGB8888;
  3547. }
  3548. texParams.eUMode = TexWrapMode(wrapS); // Wrap
  3549. texParams.eVMode = TexWrapMode(wrapT);
  3550. if(texFGLState.palEntryFGLState)
  3551. Plat::safe_free(texFGLState.palEntryFGLState);
  3552. #if 1
  3553. texFGLState.noOfpalette = numOfPaletteEntry;
  3554. if(texFGLState.noOfpalette != 0){
  3555. unsigned int* pPaletteColor = (unsigned int*)hChunk->GetVirtAddr();
  3556. GLuint * tempFGLState = texFGLState.palEntryFGLState = (GLuint* )Plat::malloc(numOfPaletteEntry * sizeof(GLuint ));
  3557. for(unsigned int x=0; x < texFGLState.noOfpalette; x++)
  3558. {
  3559. WRITEMEM(tempFGLState++, *pPaletteColor++); // paletted color
  3560. }
  3561. }
  3562. #endif
  3563. setTexStatusParam(texFGLState.texSFRtate, &texParams); // Set texture status
  3564. setTexUSize(texFGLState.texSFRtate, width);
  3565. setTexVSize(texFGLState.texSFRtate, height);
  3566. if(texParams.eType == FGL_TEX_3D) {
  3567. setTexPSize( texFGLState.texSFRtate, depth);
  3568. }
  3569. if(texParams.eMipMapFilter != FGL_FILTER_DISABLE) {
  3570. unsigned int maxLev;
  3571. if(texParams.eFormat == FGL_TEXEL_S3TC) {
  3572. calculateMipmapOffsetS3TC(texFGLState.texSFRtate,width, height, &maxLev);
  3573. }
  3574. else if(texParams.eFormat >= FGL_TEXEL_Y1VY0U) {
  3575. calculateMipmapOffsetYUV(texFGLState.texSFRtate, width, height, &maxLev);
  3576. }
  3577. else {
  3578. calculateMipmapOffset(texFGLState.texSFRtate, width, height, &maxLev);
  3579. }
  3580. setTexMipmapLevel(texFGLState.texSFRtate, FGL_MIPMAP_MIN_LEVEL, 0);
  3581. setTexMipmapLevel(texFGLState.texSFRtate, FGL_MIPMAP_MAX_LEVEL, levels - 1);
  3582. }
  3583. if(isExtTex == GL_TRUE){
  3584. setTexBaseAddr (texFGLState.texSFRtate, (unsigned int) pExtTexchunk.paddr);
  3585. // fprintf(stderr, " line = %d func =%s \n" , __LINE__ , __FUNCTION__);
  3586. }else{
  3587. setTexBaseAddr (texFGLState.texSFRtate, (unsigned int) hChunk->GetPhyAddr() + offsetFromHeader);
  3588. }
  3589. isDirtyState = GL_FALSE;
  3590. //to set the bit so that the FGL registers are updated in glfSetTexture
  3591. texFGLState.fglStateDirty = GL_TRUE;
  3592. } else {
  3593. //-----------------------------------------------------------------
  3594. // Vertex texture
  3595. //-----------------------------------------------------------------
  3596. return GL_FALSE;
  3597. }
  3598. return GL_TRUE;
  3599. }
  3600. /*-----------------------------------------------------------------------*//*!
  3601. * Calculate the compiled data size for image object
  3602. *//*------------------------------------------------------------------------*/
  3603. GLsizei
  3604. TextureObject::CompiledSize()
  3605. {
  3606. switch (texType){
  3607. case GL_TEXTURE_2D:
  3608. return (GLsizei)(width * height * 1.5 * GetPixelSize(internalFormat,images.tex2D[0].PixType));
  3609. case GL_TEXTURE_CUBE_MAP:
  3610. return (GLsizei)(width * height * 6 * 1.5 * GetPixelSize(internalFormat,images.cubeMap[0][0].PixType));
  3611. case GL_TEXTURE_3D:
  3612. return (GLsizei)(width * height * depth * GetPixelSize(internalFormat,images.tex3D[0].PixType));
  3613. // *Pra* No mipmapping in 3D textures as of now
  3614. default:
  3615. return 0;
  3616. };
  3617. }
  3618. phyVirAddress
  3619. TextureObject::GetTex2DMipLevel(GLint level)
  3620. {
  3621. unsigned int offset = 0;
  3622. phyVirAddress phyAddVirAdd;
  3623. if(hChunk != NULL){
  3624. if(level >= levels){
  3625. phyAddVirAdd.phyAdress = NULL;
  3626. phyAddVirAdd.virAddress = NULL;
  3627. }else{
  3628. offset = Offsets[level] * pixelSize(nativeFormat);
  3629. phyAddVirAdd.phyAdress = ((GLubyte*)hChunk->GetPhyAddr()) + offset;
  3630. phyAddVirAdd.virAddress = ((GLubyte*)hChunk->GetVirtAddr()) + offset;
  3631. }
  3632. }else{
  3633. if(images.tex2D[level].hImgChunk != NULL){
  3634. phyAddVirAdd.phyAdress = images.tex2D[level].hImgChunk->GetPhyAddr();
  3635. phyAddVirAdd.virAddress = images.tex2D[level].hImgChunk->GetVirtAddr();
  3636. }else{
  3637. phyAddVirAdd.phyAdress = NULL;
  3638. phyAddVirAdd.virAddress = NULL;
  3639. }
  3640. }
  3641. return phyAddVirAdd;
  3642. }
  3643. phyVirAddress
  3644. TextureObject::GetTex3DMipLevel(GLint level)
  3645. {
  3646. unsigned int offset = 0;
  3647. phyVirAddress phyAddVirAdd;
  3648. if(hChunk != NULL){
  3649. if(level >= levels){
  3650. phyAddVirAdd.phyAdress = NULL;
  3651. phyAddVirAdd.virAddress = NULL;
  3652. }else{
  3653. offset = Offsets[level] * pixelSize(nativeFormat);
  3654. phyAddVirAdd.phyAdress = ((GLubyte*)hChunk->GetPhyAddr()) + offset;
  3655. phyAddVirAdd.virAddress = ((GLubyte*)hChunk->GetVirtAddr()) + offset;
  3656. }
  3657. }else{
  3658. if(images.tex3D[level].hImgChunk != NULL){
  3659. phyAddVirAdd.phyAdress = images.tex3D[level].hImgChunk->GetPhyAddr();
  3660. phyAddVirAdd.virAddress = images.tex3D[level].hImgChunk->GetVirtAddr();
  3661. }else{
  3662. phyAddVirAdd.phyAdress = NULL;
  3663. phyAddVirAdd.virAddress = NULL;
  3664. }
  3665. }
  3666. return phyAddVirAdd;
  3667. }
  3668. phyVirAddress
  3669. TextureObject::GetTexCubeMipLevel(GLint level, GLint texCubeMapFace)
  3670. {
  3671. unsigned int offset = 0;
  3672. unsigned int offsetFace0 = 0;
  3673. phyVirAddress phyAddVirAdd;
  3674. if(hChunk != NULL){
  3675. if(level >= levels){
  3676. phyAddVirAdd.phyAdress = NULL;
  3677. phyAddVirAdd.virAddress = NULL;
  3678. }else{
  3679. offsetFace0 = Offsets[level] * pixelSize(nativeFormat);
  3680. offset = offsetFace0 + texCubeMapFace * ( Offsets[levels - 1] +
  3681. images.cubeMap[0][levels - 1].width * images.cubeMap[0][levels - 1].width);
  3682. phyAddVirAdd.phyAdress = ((GLubyte*)hChunk->GetPhyAddr()) + offset;
  3683. phyAddVirAdd.virAddress = ((GLubyte*)hChunk->GetVirtAddr()) + offset;
  3684. }
  3685. }else{
  3686. if(images.tex3D[level].hImgChunk != NULL){
  3687. phyAddVirAdd.phyAdress = images.cubeMap[texCubeMapFace][level].hImgChunk->GetPhyAddr();
  3688. phyAddVirAdd.virAddress = images.cubeMap[texCubeMapFace][level].hImgChunk->GetVirtAddr();
  3689. }else{
  3690. phyAddVirAdd.phyAdress = NULL;
  3691. phyAddVirAdd.virAddress = NULL;
  3692. }
  3693. }
  3694. return phyAddVirAdd;
  3695. }
  3696. GLenum
  3697. TextureObject::Validate()
  3698. {
  3699. //tex object is in use
  3700. if(isUsed != GL_TRUE){
  3701. //LOGMSG(" The texture object is not in use \n");
  3702. return GL_FALSE;
  3703. }
  3704. //texture type
  3705. if(!((texType == GL_TEXTURE_2D) || (texType == GL_TEXTURE_3D)
  3706. || (texType == GL_TEXTURE_CUBE_MAP))){
  3707. //LOGMSG(" unknown texture type \n");
  3708. return GL_FALSE;
  3709. }
  3710. //level range
  3711. if((levels < 1) || (levels > MAX_MIPMAP_LEVELS)){
  3712. //LOGMSG(" The value of level is outside the range");
  3713. return GL_FALSE;
  3714. }
  3715. //width and height should be in range
  3716. if(width > (1 << (MAX_MIPMAP_LEVELS-1)) || height > (1 << (MAX_MIPMAP_LEVELS-1))
  3717. || width < 0 || height < 0 ) {
  3718. //LOGMSG("The width or height value is out of range");
  3719. return GL_FALSE;
  3720. }
  3721. //native format should be 1 of those supprted by fimg
  3722. if(ValidatePxFmt(nativeFormat) == GL_FALSE){
  3723. //LOGMSG("undetermined vlaue of the native texture format \n");
  3724. }
  3725. //wrap mode S
  3726. if(!((wrapS == GL_REPEAT) || (wrapS == GL_CLAMP_TO_EDGE) || (wrapS == GL_MIRRORED_REPEAT))){
  3727. //LOGMSG(" unknown wrap mode for S ");
  3728. return GL_FALSE;
  3729. }
  3730. //wrap mode R
  3731. if(!((wrapR == GL_REPEAT) || (wrapR == GL_CLAMP_TO_EDGE) || (wrapR == GL_MIRRORED_REPEAT))){
  3732. //LOGMSG(" unknown wrap mode for S ");
  3733. return GL_FALSE;
  3734. }
  3735. //wrap mode T
  3736. if(!((wrapT == GL_REPEAT) || (wrapT == GL_CLAMP_TO_EDGE) || (wrapT == GL_MIRRORED_REPEAT))){
  3737. //LOGMSG(" unknown wrap mode for S ");
  3738. return GL_FALSE;
  3739. }
  3740. //mag filter
  3741. if(!((magFilter == GL_LINEAR) || (magFilter == GL_NEAREST))){
  3742. //LOGMSG(" unknown mag filter type \n");
  3743. return GL_FALSE;
  3744. }
  3745. int CurLevel = 1;
  3746. //min filter and levels
  3747. if((minFilter == GL_LINEAR) || (minFilter == GL_NEAREST)){
  3748. CurLevel = 1;
  3749. }else if((minFilter == GL_NEAREST_MIPMAP_NEAREST) ||(minFilter == GL_LINEAR_MIPMAP_NEAREST)
  3750. || (minFilter == GL_NEAREST_MIPMAP_LINEAR ) || (minFilter == GL_LINEAR_MIPMAP_LINEAR)){
  3751. int tempMax = MAX(width , height);
  3752. int maxValue = MAX(tempMax, depth);
  3753. while (maxValue >>= 1){
  3754. CurLevel++;
  3755. }
  3756. }else{
  3757. //LOGMSG(" unknown min filter type \n");
  3758. return GL_FALSE;
  3759. }
  3760. if(CurLevel != levels){
  3761. //LOGMSG(" the level value is incorrect \n.");
  3762. return GL_FALSE;
  3763. }
  3764. //in case of cubemap width and height should be equal
  3765. if(texType == GL_TEXTURE_CUBE_MAP){
  3766. for(int j = 0; j < 6 ; j++){
  3767. for(int i = 0 ; i < levels ; i++){
  3768. if(images.cubeMap[j][i].height != images.cubeMap[j][i].width){
  3769. //LOGMSG(" Cubemap but width is not equal to height \n");
  3770. return GL_FALSE;
  3771. }
  3772. }
  3773. }
  3774. }
  3775. //tset the image structure
  3776. GLint currWidth = width;
  3777. GLint currHeight = height;
  3778. GLint currDepth = ((texType == GL_TEXTURE_3D)? depth :1);
  3779. for(int i = 0 ; i < levels ; i++) {
  3780. switch (texType){
  3781. case GL_TEXTURE_2D:
  3782. if(images.tex2D[i].isUsed != GL_TRUE || images.tex2D[i].nativeFormat != images.tex2D[0].nativeFormat ||
  3783. images.tex2D[i].width != currWidth || images.tex2D[i].height != currHeight || images.tex2D[i].imagedataLocation != TEXOBJCHUNK){
  3784. //LOGMSG(" the image structure value are not proper \n.");
  3785. return GL_FALSE ;
  3786. }
  3787. break;
  3788. case GL_TEXTURE_3D:
  3789. if(images.tex3D[i].isUsed != GL_TRUE || images.tex3D[i].nativeFormat != images.tex3D[0].nativeFormat ||
  3790. images.tex3D[i].width != currWidth || images.tex3D[i].height != currHeight || images.tex3D[i].depth != currDepth || images.tex3D[i].imagedataLocation != TEXOBJCHUNK) {
  3791. //LOGMSG(" the image structure value are not proper \n.");
  3792. return GL_FALSE ;
  3793. }
  3794. break;
  3795. case GL_TEXTURE_CUBE_MAP:
  3796. for(int j =0 ; j < 6 ; j++){
  3797. if(images.cubeMap[j][i].isUsed != GL_TRUE || images.cubeMap[j][i].nativeFormat != images.cubeMap[0][0].nativeFormat ||
  3798. images.cubeMap[j][i].width != currWidth || images.cubeMap[j][i].height != currHeight|| images.cubeMap[j][i].imagedataLocation != TEXOBJCHUNK) {
  3799. //LOGMSG(" the image structure value are not proper \n.");
  3800. return GL_FALSE;
  3801. }
  3802. }
  3803. break;
  3804. }
  3805. currWidth = ((currWidth / 2) > 1) ? (currWidth / 2) : 1;
  3806. currHeight = ((currHeight / 2) > 1) ? (currHeight / 2) : 1;
  3807. currDepth = ((currDepth / 2) > 1) ? (currDepth / 2) : 1;
  3808. }
  3809. //memory should be present in compiled state
  3810. if(isExtTex == GL_TRUE){
  3811. if(pExtTexchunk.paddr == NULL){
  3812. //LOGMSG("\n externall managed texture but memory is NULL \n");
  3813. return GL_FALSE;
  3814. }
  3815. }else{
  3816. if(hChunk == NULL){
  3817. //LOGMSG(" the memory is NULL \n");
  3818. return GL_FALSE;
  3819. }
  3820. }
  3821. //LOGMSG("texture object is validated \n");
  3822. return GL_TRUE;
  3823. }
  3824. extern "C" int isTexUnitMipMapped(GLuint texUnit)
  3825. {
  3826. //OGLState* pState = GET_OGL_STATE();
  3827. //TextureState* pTexState = &(pState->texState);
  3828. TextureObject* pTexObj = GetTextureObject(GL_TEXTURE_2D,true,texUnit); //index in array
  3829. ////LOGMSG( "\n texIndex =%d", texid );
  3830. if((pTexObj->minFilter == GL_LINEAR_MIPMAP_LINEAR) ||
  3831. (pTexObj->minFilter == GL_NEAREST_MIPMAP_LINEAR) ||
  3832. (pTexObj->minFilter == GL_LINEAR_MIPMAP_NEAREST) ||
  3833. (pTexObj->minFilter == GL_NEAREST_MIPMAP_NEAREST)){
  3834. return 1;
  3835. }else{
  3836. return 0;
  3837. }
  3838. //LOGMSG("\nthere is some issue in determining if mipmap is used.\n");
  3839. return -1;
  3840. }
  3841. void releaseBoundTextures(OGLState* pState)
  3842. {
  3843. TextureState* pTexState = &(pState->texState);
  3844. int index = -1;
  3845. for(int i =0 ; i < MAX_TEXTURE_UNITS ; i++){
  3846. //reduce the reference count of the 2D texture object bound to the current context
  3847. if(pTexState->texUnitBinding[i].texture2D != 0){
  3848. index = GetTexNameArrayIndex( pTexState->texUnitBinding[i].texture2D, 0);
  3849. pState->sharedState->sharedTexState.ReleaseTexObj(pState , index);
  3850. }
  3851. //reduce the reference count of the 3D texture object bound to the current context
  3852. if(pTexState->texUnitBinding[i].texture3D != 0){
  3853. index = GetTexNameArrayIndex( pTexState->texUnitBinding[i].texture3D, 0);
  3854. pState->sharedState->sharedTexState.ReleaseTexObj(pState , index);
  3855. }
  3856. //reduce the reference count of the cubemap texture object bound to the current context
  3857. if(pTexState->texUnitBinding[i].cubeMap != 0){
  3858. index = GetTexNameArrayIndex( pTexState->texUnitBinding[i].cubeMap, 0);
  3859. pState->sharedState->sharedTexState.ReleaseTexObj(pState , index);
  3860. }
  3861. }
  3862. }
  3863. void SharedTextureState::ReleaseTexObj(struct OGLState* pState , GLuint id)
  3864. {
  3865. if(texObjects[id]->release(pState) == GL_TRUE){
  3866. //removing the texture id from used texture names
  3867. pState->sharedState->sharedTexState.usedTexNames.erase(id);
  3868. delete (texObjects[id]);
  3869. //removing the texture object from the map
  3870. pState->sharedState->sharedTexState.texObjects.erase(id);
  3871. }
  3872. }
  3873. /*
  3874. *******************************************************************************
  3875. * Local functions
  3876. *******************************************************************************
  3877. */
  3878. /*-----------------------------------------------------------------------*//*!
  3879. *
  3880. *//*------------------------------------------------------------------------*/
  3881. TextureObject*
  3882. GetTextureObject ( GLenum target ,bool extTU,GLint texUnit)
  3883. {
  3884. OGLState* pState = GET_OGL_STATE();
  3885. TextureState* pTexState = &(pState->texState);
  3886. GLuint currentUnit ;
  3887. if(extTU == true)
  3888. currentUnit = texUnit;
  3889. else
  3890. currentUnit = pTexState->activeTexUnit;
  3891. //sanvd added the code below
  3892. GLint texRef ;
  3893. bool flag;
  3894. switch(target){
  3895. case GL_TEXTURE_2D:
  3896. if(pTexState->texUnitBinding[currentUnit].texture2D != 0)
  3897. {
  3898. texRef = pTexState->texUnitBinding[currentUnit].texture2D;
  3899. flag = false;
  3900. }
  3901. else
  3902. {
  3903. texRef = TEX_2D_DEFAULT;
  3904. flag = true;
  3905. }
  3906. break;
  3907. case GL_TEXTURE_3D:
  3908. if(pTexState->texUnitBinding[currentUnit].texture3D != 0)
  3909. {
  3910. texRef = pTexState->texUnitBinding[currentUnit].texture3D;
  3911. flag = false;
  3912. }
  3913. else
  3914. {
  3915. texRef = TEX_3D_DEFAULT;
  3916. flag = true;
  3917. }
  3918. break;
  3919. case GL_TEXTURE_CUBE_MAP:
  3920. if(pTexState->texUnitBinding[currentUnit].cubeMap != 0)
  3921. {
  3922. texRef = pTexState->texUnitBinding[currentUnit].cubeMap;
  3923. flag = false;
  3924. }
  3925. else
  3926. {
  3927. texRef = TEX_CUBE_MAP_DEFAULT;
  3928. flag = true;
  3929. }
  3930. break;
  3931. default:
  3932. gAssert(false && "Unexpected target"); //Sandeep
  3933. texRef = TEX_2D_DEFAULT;
  3934. flag = true;
  3935. };
  3936. int index = GetTexNameArrayIndex(texRef,flag);
  3937. if(index == TEX_2D_DEFAULT){
  3938. return &(pTexState->defaultTexObjects[TEX_2D_DEFAULT - TEX_2D_DEFAULT]);
  3939. }else if(index == TEX_3D_DEFAULT){
  3940. return &(pTexState->defaultTexObjects[TEX_3D_DEFAULT - TEX_2D_DEFAULT]);
  3941. }else if(index == TEX_CUBE_MAP_DEFAULT){
  3942. return &(pTexState->defaultTexObjects[TEX_CUBE_MAP_DEFAULT -TEX_2D_DEFAULT ]);
  3943. }else{
  3944. TexObjects::iterator iter = pState->sharedState->sharedTexState.texObjects.find(index);
  3945. return (iter->second);
  3946. }
  3947. }
  3948. /*-----------------------------------------------------------------------*//*!
  3949. *
  3950. *//*------------------------------------------------------------------------*/
  3951. /*check whether the internal format & format are same or not
  3952. & are one of the supported type */
  3953. static GLenum
  3954. checkTextureFormat ( OGLState* pState, GLenum internalFormat, GLenum format)
  3955. {
  3956. // Checking of internalFormat, format, type is to be done here
  3957. //return internalFormat;
  3958. //sanvd added the code below
  3959. if(internalFormat!=format)
  3960. return GL_INVALID_OPERATION;
  3961. if( !(internalFormat == GL_RGBA || internalFormat == GL_BGRA || internalFormat == GL_RGB ||internalFormat == GL_ALPHA
  3962. ||
  3963. internalFormat == GL_LUMINANCE ||internalFormat == GL_LUMINANCE_ALPHA) )
  3964. return GL_INVALID_VALUE;
  3965. return GL_NO_ERROR;
  3966. }
  3967. /* Function will determine the no of pixels depending on the textureFormat*/
  3968. GLuint GetPixSize(GLenum TextureFormat)
  3969. {
  3970. int bpp =2;
  3971. if((TextureFormat == E_LUMINANCE8) || (TextureFormat == E_ALPHA8)){
  3972. bpp = 1;
  3973. }
  3974. else if((TextureFormat == E_ALPHA_LUMINANCE88)||
  3975. ( TextureFormat == E_LUMINANCE_ALPHA88) ||
  3976. (TextureFormat == E_LUMINANCE_ALPHA80) ||
  3977. (TextureFormat == E_LUMINANCE_ALPHA08) ||
  3978. (TextureFormat == E_RGBA4) ||
  3979. (TextureFormat == E_RGB565) ||
  3980. (TextureFormat == E_RGBA5551)||
  3981. (TextureFormat == E_ARGB4) ||
  3982. (TextureFormat == E_ARGB1555))
  3983. {
  3984. bpp = 2;
  3985. }
  3986. else if((TextureFormat == E_ARGB8) || (TextureFormat == E_ABGR8) || (TextureFormat == E_ARGB0888)){
  3987. bpp =4;
  3988. }
  3989. else{
  3990. gAssert(false && "undetermined byte per pixel ");
  3991. }
  3992. return bpp;
  3993. }
  3994. GLenum CheckFormatTypeCombination(GLenum texFormat, GLenum type)
  3995. {
  3996. if(((texFormat == GL_BGRA || texFormat == GL_RGBA || texFormat == GL_RGB) && (type == GL_UNSIGNED_BYTE))
  3997. |((texFormat == GL_RGBA) && (type == GL_UNSIGNED_SHORT_4_4_4_4))
  3998. |((texFormat == GL_RGBA) && (type == GL_UNSIGNED_SHORT_5_5_5_1))
  3999. |((texFormat == GL_RGB) && (type == GL_UNSIGNED_SHORT_5_6_5))
  4000. |((texFormat == GL_LUMINANCE_ALPHA) && (type == GL_UNSIGNED_BYTE))
  4001. |((texFormat == GL_LUMINANCE) && (type == GL_UNSIGNED_BYTE))
  4002. |((texFormat == GL_ALPHA) && (type == GL_UNSIGNED_BYTE)))
  4003. return GL_NO_ERROR;
  4004. else{
  4005. gAssert(false && "Unhandled or unexpected format and type combination"); //Sandeep
  4006. return 1;
  4007. //to check what type of error to return if the are not matched
  4008. }
  4009. }
  4010. inline
  4011. void determineFormatAndType( GLenum * format , GLenum * type , PxFmt nativeformat )
  4012. {
  4013. switch(nativeformat){
  4014. case E_ARGB8:
  4015. * format = GL_RGBA;
  4016. *type = GL_UNSIGNED_BYTE;
  4017. break;
  4018. case E_ABGR8:
  4019. * format = GL_BGRA;
  4020. *type = GL_UNSIGNED_BYTE;
  4021. break;
  4022. case E_ARGB4:
  4023. * format = GL_RGBA;
  4024. *type = GL_UNSIGNED_SHORT_4_4_4_4;
  4025. break;
  4026. case E_ARGB1555:
  4027. *format = GL_RGBA;
  4028. *type = GL_UNSIGNED_SHORT_5_5_5_1;
  4029. break;
  4030. case E_RGB565:
  4031. *format = GL_RGB;
  4032. *type = GL_UNSIGNED_SHORT_5_6_5;
  4033. break;
  4034. //default:
  4035. //LOGMSG("unsupported PxFmt as external managed texture \n");
  4036. }
  4037. }
  4038. /**********************************************************************************************
  4039. / external texture object
  4040. ************************************************************************************************/
  4041. #ifdef USE_3D_PM
  4042. void PM_GLES2BindTexImage(const GLES2SurfaceData* pSurfData , GLenum target, GLint level , GLuint isMipmapped);
  4043. extern "C"
  4044. void GLES2BindTexImage(const GLES2SurfaceData* pSurfData , GLenum target, GLint level , GLuint isMipmapped)
  4045. {
  4046. lock3DCriticalSection();
  4047. PM_GLES2BindTexImage( pSurfData , target, level , isMipmapped);
  4048. unlock3DCriticalSection();
  4049. }
  4050. void PM_GLES2BindTexImage(const GLES2SurfaceData* pSurfData , GLenum target, GLint level , GLuint isMipmapped)
  4051. #else
  4052. extern "C"
  4053. void GLES2BindTexImage(const GLES2SurfaceData* pSurfData , GLenum target, GLint level , GLuint isMipmapped)
  4054. #endif
  4055. {
  4056. OGLState* pState = GET_OGL_STATE();
  4057. GLsizei pixSize; // Bytes per pixel
  4058. Image *pImgObj = NULL; // Pointer to image object
  4059. TextureObject* pTexObj =NULL;
  4060. GLenum format = GLenum(-1);
  4061. GLenum type = GLenum(-1);
  4062. //-------------------------------------------------------------------------
  4063. // Get texture object and VALIDATE INPUT PARAMETERS
  4064. //-------------------------------------------------------------------------
  4065. // Check MipMap level //TO DO SHOULD BE ALLOWED OTHER THAN LEVEL 0 AS IMAGE CAN NOT BE COMPILED
  4066. if(level != 0 ) {
  4067. SET_ERR(pState, GL_INVALID_VALUE, "bindTexImage");
  4068. return;
  4069. };
  4070. // Select object reference according to target
  4071. // Return error on invalid enum
  4072. if(target == GL_TEXTURE_2D){
  4073. pTexObj = GetTextureObject(GL_TEXTURE_2D,false,0);
  4074. pImgObj = &(pTexObj->images.tex2D[level]);
  4075. }else {
  4076. SET_ERR(pState, GL_INVALID_ENUM, "bindTexImage*");
  4077. return;
  4078. }
  4079. // Check width and height should be less than zero or greater than GL_MAX_TEXTURE_SIZE????
  4080. if(pSurfData->width > (1 << (MAX_MIPMAP_LEVELS-1)) || pSurfData->height > (1 << (MAX_MIPMAP_LEVELS-1))
  4081. || pSurfData->width < 0 || pSurfData->height < 0 ) {
  4082. SET_ERR(pState, GL_INVALID_VALUE, "bindTexImage");
  4083. return;
  4084. }
  4085. if(!((pSurfData->nativeColorFormat == E_ARGB8) ||(pSurfData->nativeColorFormat == E_ARGB4)
  4086. || (pSurfData->nativeColorFormat == E_ARGB1555) || (pSurfData->nativeColorFormat == E_RGB565)) ){
  4087. SET_ERR(pState, GL_INVALID_VALUE, "bindTexImage");
  4088. return;
  4089. }
  4090. determineFormatAndType(&format , &type , pSurfData->nativeColorFormat);
  4091. if((pSurfData->colorPaddr== NULL) || (pSurfData->colorVaddr == NULL)){
  4092. return;
  4093. }
  4094. //FLUSHING THE CACHE
  4095. FimgFinish(pState);
  4096. pixSize = pixelSize(pSurfData->nativeColorFormat);
  4097. if(getImageDataLocation(pTexObj , 0 , 0 , 0, 0, 0, 0 , 0, 0, 1) != NULL){
  4098. gAssert(false && " getImageDataLocation function did not return null in case of the external managed texture");
  4099. return;
  4100. }
  4101. pTexObj->pExtTexchunk.paddr = pSurfData->colorPaddr;
  4102. pTexObj->pExtTexchunk.vaddr = pSurfData->colorVaddr;
  4103. pTexObj->isExtTex =GL_TRUE;
  4104. pTexObj->width = pSurfData->width;;
  4105. pTexObj->height = pSurfData->height;
  4106. pTexObj->internalFormat = format;
  4107. pTexObj->nativeFormat = pSurfData->nativeColorFormat;
  4108. pTexObj->levels = 0;
  4109. pTexObj->isDirtyState = GL_TRUE;
  4110. pImgObj->imagedataLocation = TEXOBJCHUNK ;
  4111. pImgObj->isUsed = GL_TRUE;
  4112. pImgObj->width = pSurfData->width;
  4113. pImgObj->height = pSurfData->height;
  4114. pImgObj->internalFormat = format;
  4115. pImgObj->PixType = type;
  4116. pImgObj->imgSize = pSurfData->width * pSurfData->height * pixSize;
  4117. pImgObj->isCompressed = false;
  4118. pImgObj->nativeFormat = pSurfData->nativeColorFormat;
  4119. // fprintf(stderr, " line = %d func =%s" , __LINE__ , __FUNCTION__);
  4120. }
  4121. extern "C"
  4122. void GLES2ReleaseTexImage(const GLES2SurfaceData* pSurfData )
  4123. {
  4124. OGLState* pState = GET_OGL_STATE();
  4125. TextureObject* pTexObj =NULL;
  4126. #if 0
  4127. //find the texture object using the surface buffer in shared texture object
  4128. for(int i = 0; i <= pState->sharedState->sharedTexState.maxUsedUnit ; ++i){
  4129. if(pState->sharedState->sharedTexState.texObjects[i].pExtTexchunk.paddr == pSurfData->colorAddr.paddr){
  4130. pTexObj = &(pState->sharedState->sharedTexState.texObjects[i]);
  4131. break;
  4132. }
  4133. }
  4134. #else
  4135. TexObjects::iterator iter = pState->sharedState->sharedTexState.texObjects.begin();
  4136. for( ; iter != pState->sharedState->sharedTexState.texObjects.end() ; iter++){
  4137. if(iter->second->pExtTexchunk.paddr == pSurfData->colorPaddr){
  4138. pTexObj = (iter->second);
  4139. break;
  4140. }
  4141. }
  4142. #endif
  4143. //check in default texture object
  4144. if(pTexObj == NULL){
  4145. if(pState->texState.defaultTexObjects[0].pExtTexchunk.paddr == pSurfData->colorPaddr){
  4146. pTexObj = &(pState->texState.defaultTexObjects[0]);
  4147. }
  4148. }
  4149. if(pTexObj == NULL){
  4150. //not an error...
  4151. return;
  4152. }
  4153. pTexObj->releaseMem();
  4154. pTexObj->isExtTex = GL_FALSE;
  4155. pTexObj->images.tex2D[0].imagedataLocation = NONE;
  4156. pTexObj->levels = 0;
  4157. }
  4158. /*-----------------------------------------------------------------------*//*!
  4159. *
  4160. *//*------------------------------------------------------------------------*/
  4161. GLsizei
  4162. GetPixelSize ( GLenum texFormat, GLenum type )
  4163. {
  4164. int bpp = 2; //NOTE: HARDCODED must have some sanity checks and error values
  4165. // GLenum type = GL_UNSIGNED_BYTE; sanvd commented this
  4166. if((texFormat == GL_BGRA || texFormat == GL_RGBA || texFormat == GL_RGB) && (type == GL_UNSIGNED_BYTE))
  4167. {
  4168. bpp = 4;
  4169. }
  4170. /*else if((texFormat == GL_RGB) && (type == GL_UNSIGNED_BYTE))
  4171. {
  4172. bpp = 3;
  4173. }*/
  4174. else if((texFormat == GL_RGBA) && (type == GL_UNSIGNED_SHORT_4_4_4_4))
  4175. {
  4176. bpp = 2;
  4177. }
  4178. else if((texFormat == GL_RGBA) && (type == GL_UNSIGNED_SHORT_5_5_5_1))
  4179. {
  4180. bpp = 2;
  4181. }
  4182. else if((texFormat == GL_RGB) && (type == GL_UNSIGNED_SHORT_5_6_5))
  4183. {
  4184. bpp = 2;
  4185. }
  4186. else if((texFormat == GL_LUMINANCE_ALPHA) && (type == GL_UNSIGNED_BYTE))
  4187. {
  4188. bpp = 2;
  4189. }
  4190. else if((texFormat == GL_LUMINANCE) && (type == GL_UNSIGNED_BYTE))
  4191. {
  4192. bpp=2; //bpp = 1; //TODO: the actual native formats should be used to determine the pixel size for tansfer image!!
  4193. }
  4194. else if((texFormat == GL_ALPHA) && (type == GL_UNSIGNED_BYTE))
  4195. {
  4196. bpp = 2;
  4197. }
  4198. else
  4199. {
  4200. gAssert(false && "GetPixelSize"&& "Unhandled or unexpected format and type combination"); //Sandeep
  4201. }
  4202. return bpp;
  4203. }
  4204. /*-----------------------------------------------------------------------*//*!
  4205. *
  4206. *//*------------------------------------------------------------------------*/
  4207. static inline GLboolean
  4208. IsPowerOf2 ( GLuint n )
  4209. {
  4210. if(n > 0 && (n & (n-1)) == 0)
  4211. return GL_TRUE;
  4212. else
  4213. return GL_FALSE;
  4214. }
  4215. /*-----------------------------------------------------------------------*//*!
  4216. *
  4217. *//*------------------------------------------------------------------------*/
  4218. //supported FGL format
  4219. GLenum ValidatePxFmt(PxFmt pxFmtValue)
  4220. {
  4221. switch(pxFmtValue){
  4222. case E_LUMINANCE8:
  4223. case E_LUMINANCE_ALPHA88:
  4224. case E_LUMINANCE_ALPHA80:
  4225. case E_LUMINANCE_ALPHA08:
  4226. case E_ARGB8:
  4227. case E_ABGR8:
  4228. case E_ARGB4:
  4229. case E_RGB565:
  4230. case E_ARGB1555:
  4231. case E_RGB_S3TC_OES:
  4232. case E_RGBA_S3TC_OES:
  4233. case E_PALETTE4_RGB8_OES:
  4234. case E_PALETTE4_RGBA8_OES:
  4235. case E_PALETTE4_R5_G6_B5_OES:
  4236. case E_PALETTE4_RGBA4_OES:
  4237. case E_PALETTE4_RGB5_A1_OES:
  4238. case E_PALETTE8_RGB8_OES:
  4239. case E_PALETTE8_RGBA8_OES:
  4240. case E_PALETTE8_R5_G6_B5_OES:
  4241. case E_PALETTE8_RGBA4_OES:
  4242. case E_PALETTE8_RGB5_A1_OES:
  4243. return GL_TRUE;
  4244. default:
  4245. return GL_FALSE;
  4246. }
  4247. }
  4248. static
  4249. GLboolean CheckFormatConversion(GLenum dstFormat, GLenum srcFormat, GLenum* dstType)
  4250. {
  4251. switch(srcFormat)
  4252. {
  4253. case GL_RGBA8:
  4254. if(dstFormat == GL_RGBA || dstFormat == GL_RGB || dstFormat == GL_LUMINANCE_ALPHA || dstFormat == GL_LUMINANCE || dstFormat == GL_ALPHA)
  4255. {
  4256. *dstType = GL_UNSIGNED_BYTE;
  4257. return true;
  4258. }
  4259. return false;
  4260. break;
  4261. case GL_RGBA: //not supported as of now//can be converted to any format by dropping the channels
  4262. case GL_BGRA: //not supported as of now//can be converted to any format by dropping the channels
  4263. return true;
  4264. break;
  4265. case GL_RGB565:
  4266. case GL_RGB:
  4267. if(dstFormat == GL_RGB )
  4268. {
  4269. *dstType = GL_UNSIGNED_SHORT_5_6_5;
  4270. return true;
  4271. }
  4272. if( dstFormat == GL_LUMINANCE)
  4273. {
  4274. *dstType = GL_UNSIGNED_BYTE;
  4275. return true;
  4276. }
  4277. return false;
  4278. break;
  4279. case GL_LUMINANCE_ALPHA:
  4280. if(dstFormat == GL_RGB || dstFormat == GL_RGBA)
  4281. return false;
  4282. *dstType = GL_UNSIGNED_BYTE;
  4283. return true;
  4284. break;
  4285. case GL_LUMINANCE:
  4286. if(dstFormat == GL_LUMINANCE)
  4287. {
  4288. *dstType = GL_UNSIGNED_BYTE;
  4289. return true;
  4290. }
  4291. return false;
  4292. break;
  4293. case GL_ALPHA:
  4294. if(dstFormat == GL_ALPHA)
  4295. {
  4296. *dstType = GL_UNSIGNED_BYTE;
  4297. return true;
  4298. }
  4299. return false;
  4300. break;
  4301. }
  4302. return false;
  4303. }
  4304. static bool
  4305. s3tcCompressedTex(GLenum& texformat,GLenum internalformat,GLsizei width,GLsizei height,GLsizei imageSize)
  4306. {
  4307. if( width%4 !=0 || height%4!=0 || imageSize ==0)
  4308. return false;
  4309. int expectedSize =0;
  4310. switch(internalformat)
  4311. {
  4312. case GL_RGB_S3TC_OES:
  4313. texformat = GL_RGB;
  4314. width = (width+3) & ~3;
  4315. height = (height+3) & ~3;
  4316. expectedSize = width * height/2;
  4317. if (expectedSize< 8)
  4318. expectedSize =8;
  4319. if (expectedSize!= imageSize)
  4320. return false;
  4321. break;
  4322. case GL_RGBA_S3TC_OES:
  4323. texformat = GL_RGBA;
  4324. /*width = (width+3) & ~3;
  4325. height = (height+3) & ~3;
  4326. expectedSize = width * height;
  4327. if (expectedSize< 16)
  4328. expectedSize =16;
  4329. if (expectedSize!= imageSize)
  4330. return false;*/
  4331. break;
  4332. return true;
  4333. }
  4334. return true;
  4335. }
  4336. GLint GetTexNameArrayIndex(GLuint texName,bool IsDefault)
  4337. {
  4338. OGLState* pState = GET_OGL_STATE();
  4339. //TextureState* pTexState = &(pState->texState);
  4340. if(IsDefault)
  4341. {
  4342. if(texName == TEX_2D_DEFAULT)
  4343. return TEX_2D_DEFAULT;
  4344. else if(TEX_3D_DEFAULT)
  4345. return TEX_3D_DEFAULT;
  4346. else if(TEX_CUBE_MAP_DEFAULT)
  4347. return TEX_CUBE_MAP_DEFAULT;
  4348. else
  4349. return -1;
  4350. }
  4351. #if 0
  4352. GLint FEP = -1; //first position in the array whcih is unsued.
  4353. SharedTextureState *pSTState = &pState->sharedState->sharedTexState;
  4354. TextureObject *pTexObj;
  4355. //TODO: is it i =1 or i=0? -Sandeep K. shariq now it should be 0
  4356. for(int i = 1; i <= pSTState->maxUsedUnit + 1 ; ++i) //added 1 for new unit
  4357. {
  4358. if((pTexObj = pSTState->texObjects[i]) != NULL) {
  4359. if(FEP == -1 && pTexObj->isUsed ==false)
  4360. {
  4361. FEP = i;
  4362. }
  4363. //TODO: check for is used? -Sandeep K.
  4364. if(texName == pTexObj->id)
  4365. {
  4366. return i;
  4367. }
  4368. }
  4369. }
  4370. #else
  4371. // int count = pState->sharedState->sharedTexState.texObjects.count(texName);
  4372. // if(count == 0){
  4373. if(pState->sharedState->sharedTexState.texObjects.find(texName) == pState->sharedState->sharedTexState.texObjects.end()){ //liyue 090316
  4374. // if(pState->sharedState->sharedTexState.texObjects.count(texName) == 0){
  4375. TextureObject* pTexObj = new(TextureObject);
  4376. pTexObj->reset();
  4377. pState->sharedState->sharedTexState.texObjects[texName] = pTexObj;
  4378. }
  4379. return texName;
  4380. #endif
  4381. }
  4382. TextureObject* GetTexObject(GLuint texName)
  4383. {
  4384. OGLState* pState = GET_OGL_STATE();
  4385. //TextureState* pTexState = &(pState->texState);
  4386. TextureObject *pTexObj;
  4387. //TODO: is it i =1 or i=0? -Sandeep K.
  4388. for(int i=1; i< MAX_TEXTURE_OBJECTS; i++)
  4389. {
  4390. //TODO: check for is used? -Sandeep K.
  4391. if((pTexObj = pState->sharedState->sharedTexState.texObjects[i])
  4392. != NULL)
  4393. {
  4394. if(texName == pTexObj->id)
  4395. {
  4396. //TODO: Compile should not be required here
  4397. pTexObj->Compile();
  4398. return (pTexObj);
  4399. }
  4400. }
  4401. }
  4402. return 0;
  4403. }
  4404. void SwapRB(GLubyte* pixels,GLenum format,GLenum type, GLsizei dstWidth, GLsizei dstHeight,GLsizei dstDepth,
  4405. int xoff,int yoff,int zoff, GLsizei width, GLsizei height,GLsizei depth, int pixelSize)
  4406. {
  4407. GLubyte tmp = 0;
  4408. if(depth == 0)
  4409. depth = 1;
  4410. //if(format==GL_RGB && type == GL_UNSIGNED_BYTE)
  4411. // pixelSize = 3;
  4412. GLubyte *imgBuf = pixels + (zoff*dstWidth*dstHeight + yoff*dstWidth + xoff)*pixelSize;
  4413. if( (format == GL_RGB || format == GL_RGBA ) && type == GL_UNSIGNED_BYTE)
  4414. for(int d = 0; d < depth ; d++)
  4415. {
  4416. for(int h = 0; h < height ; h ++)
  4417. {
  4418. for(int w = 0; w < width ; w++)
  4419. {
  4420. tmp = imgBuf[0];
  4421. imgBuf[0] = imgBuf[2];
  4422. imgBuf[2] = tmp;
  4423. imgBuf+=pixelSize;
  4424. }
  4425. imgBuf += (dstWidth - width) * pixelSize ;
  4426. }
  4427. imgBuf += ((dstHeight - height)*dstWidth) * pixelSize ;
  4428. }
  4429. }
  4430. void* glMapTexture(GLenum target, GLint level, GLenum access)
  4431. {
  4432. OGLState* pState = GET_OGL_STATE();
  4433. TextureObject* pTexObj = 0;
  4434. Image* pImgObj = 0;
  4435. if (target == GL_TEXTURE_2D)
  4436. {
  4437. pTexObj = GetTextureObject(target,false,0);
  4438. pImgObj = &(pTexObj->images.tex2D[level]);
  4439. }
  4440. else if (target == GL_TEXTURE_3D)
  4441. {
  4442. pTexObj = GetTextureObject(target,false,0);
  4443. pImgObj = &(pTexObj->images.tex3D[level]);
  4444. }
  4445. else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
  4446. {
  4447. pTexObj = GetTextureObject(target,false,0);
  4448. pImgObj = &(pTexObj->images.cubeMap[target - GL_TEXTURE_CUBE_MAP_POSITIVE_X][level]);
  4449. }
  4450. else
  4451. {
  4452. SET_ERR(pState, GL_INVALID_ENUM, "glMapTexture*");
  4453. return 0;
  4454. }
  4455. if (pTexObj->isLocked == 1)
  4456. {
  4457. SET_ERR(pState, GL_INVALID_OPERATION, "glMapTexture*");
  4458. return 0;
  4459. }
  4460. if (pImgObj->imagedataLocation == IMAGECHUNK)
  4461. {
  4462. if (pImgObj->hImgChunk != 0)
  4463. {
  4464. pTexObj->isLocked = 1;
  4465. return pImgObj->hImgChunk->GetVirtAddr();
  4466. }
  4467. else
  4468. {
  4469. return 0;
  4470. }
  4471. }
  4472. else if (pImgObj->imagedataLocation == TEXOBJCHUNK)
  4473. {
  4474. if (pTexObj->hChunk != 0)
  4475. {
  4476. pTexObj->isLocked = 1;
  4477. return (char*)pTexObj->hChunk->GetVirtAddr() + pTexObj->Offsets[level];
  4478. }
  4479. else
  4480. {
  4481. return 0;
  4482. }
  4483. }
  4484. return 0;
  4485. }
  4486. GLboolean glUnmapTexture(GLenum target)
  4487. {
  4488. OGLState* pState = GET_OGL_STATE();
  4489. TextureObject* pTexObj = 0;
  4490. if (target == GL_TEXTURE_2D)
  4491. {
  4492. pTexObj = GetTextureObject(target,false,0);
  4493. }
  4494. else if (target == GL_TEXTURE_3D)
  4495. {
  4496. pTexObj = GetTextureObject(target,false,0);
  4497. }
  4498. else if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
  4499. {
  4500. pTexObj = GetTextureObject(target,false,0);
  4501. }
  4502. else
  4503. {
  4504. SET_ERR(pState, GL_INVALID_ENUM, "glMapTexture*");
  4505. return GL_FALSE;
  4506. }
  4507. // not locked
  4508. if (!pTexObj->isLocked)
  4509. {
  4510. set_err(GL_INVALID_OPERATION);
  4511. return GL_FALSE;
  4512. }
  4513. pTexObj->isLocked = 0;
  4514. return GL_TRUE;
  4515. }