/GT-I5700/gles20/src/glTex.cpp
C++ | 5533 lines | 3760 code | 1017 blank | 756 comment | 1205 complexity | 18e6b01492ad12521b320a8745f6d4c6 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /*
- *******************************************************************************
- *
- * SAMSUNG INDIA SOFTWARE OPERATIONS
- * Copyright(C) 2006
- * ALL RIGHTS RESERVED
- *
- * This program is proprietary to Samsung India Software Operations Pvt. Ltd.,
- * and is protected under International Copyright Act as an unpublished work.Its
- * use and disclosure is limited by the terms and conditions of a license agree-
- * -ment. It may not be copied or otherwise reproduced or disclosed to persons
- * outside the licensee's organization except in accordance with the terms and
- * conditions of such an agreement. All copies and reproductions shall be the
- * property of Samsung India Software Operations Pvt. Ltd. and must bear this
- * notice in its entirety.
- *
- *******************************************************************************
- */
-
- /*
- ***************************************************************************//*!
- *
- * \file glTex.cpp
- * \author Prashant Singh (prashant@samsung.com),
- * Anurag Ved (anuragv@samsung.com)
- * \brief Texture handling
- *
- *//*---------------------------------------------------------------------------
- * NOTES:
- *
- *//*---------------------------------------------------------------------------
- * HISTORY:
- *
- * 31.07.2006 Anurag Ved Initial draft implementation
- *
- * 3.08.2006 Prashant Singh Functions with new state and texture object
- *
- * 22.08.2006 Prashant Singh Compilable version with isComplete, Compile,
- * etc. Complete image specification, tex copy
- * and compressed image api is missing
- *
- * 24.08.2006 Prashant Singh Priliminary testing on host completed
- *
- *******************************************************************************
- */
-
- /*
- *******************************************************************************
- * Includes
- *******************************************************************************
- */
-
- #include "gl.h"
- #include "platform.h"
- #include "glState.h"
- #include "glTex.h"
- #include "buffers.h"
- #include "texfglstate.h"
- #include "glFimg.h"
- //#include "pixel.h"
- #include "ustl.h"
-
- /*
- *******************************************************************************
- * Macro definitions and enumerations
- *******************************************************************************
- */
-
- #define TEX_2D_DEFAULT (MAX_TEXTURE_OBJECTS)
- #define TEX_3D_DEFAULT (TEX_2D_DEFAULT + 1)
- #define TEX_CUBE_MAP_DEFAULT (TEX_3D_DEFAULT + 1)
- #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
- #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
-
- /*
- *******************************************************************************
- * Type, Structure & Class Definitions
- *******************************************************************************
- */
-
- /*
- *******************************************************************************
- * Global Variables
- *******************************************************************************
- */
-
-
- /*
- *******************************************************************************
- * Local Function Declarations
- *******************************************************************************
- */
-
- //static GLint GetTextureObject (OGLState* pState, GLenum target);
- static GLenum checkTextureFormat (OGLState* pState, GLenum internalFormat, GLenum format);
- static inline GLboolean IsPowerOf2 (GLuint n);
- static bool s3tcCompressedTex(GLenum& texformat,GLenum internalformat,GLsizei width,GLsizei height,GLsizei imageSize);
- static GLboolean CheckFormatConversion(GLenum dstFormat, GLenum srcFormat, GLenum* dstType);
-
-
- /*
- *******************************************************************************
- * Function Definitions
- *******************************************************************************
- */
-
-
- static void decodePalette4(const void *data, int level, int width, int height,
- void *surface, int stride, int format)
-
- {
- int indexBits = 8;
- int entrySize = 0;
- switch (format) {
- case GL_PALETTE4_RGB8_OES:
- indexBits = 4;
- /* FALLTHROUGH */
- case GL_PALETTE8_RGB8_OES:
- entrySize = 3;
- break;
-
- case GL_PALETTE4_RGBA8_OES:
- indexBits = 4;
- /* FALLTHROUGH */
- case GL_PALETTE8_RGBA8_OES:
- entrySize = 4;
- break;
-
- case GL_PALETTE4_R5_G6_B5_OES:
- case GL_PALETTE4_RGBA4_OES:
- case GL_PALETTE4_RGB5_A1_OES:
- indexBits = 4;
- /* FALLTHROUGH */
- case GL_PALETTE8_R5_G6_B5_OES:
- case GL_PALETTE8_RGBA4_OES:
- case GL_PALETTE8_RGB5_A1_OES:
- entrySize = 2;
- break;
- }
-
- const int paletteSize = (1 << indexBits) * entrySize;
- unsigned char const* pixels = (unsigned char *)data + paletteSize;
- for (int i=0 ; i<level ; i++) {
- int w = (width >> i) ? : 1;
- int h = (height >> i) ? : 1;
- pixels += h * ((w * indexBits) / 8);
- }
- width = (width >> level) ? : 1;
- height = (height >> level) ? : 1;
-
- if (entrySize == 2) {
- unsigned char const* const palette = (unsigned char*)data;
- for (int y=0 ; y<height ; y++) {
- unsigned char* p = (unsigned char*)surface + y*stride*2;
- if (indexBits == 8) {
- for (int x=0 ; x<width ; x++) {
- int index = 2 * (*pixels++);
- *p++ = palette[index + 0];
- *p++ = palette[index + 1];
- }
- } else {
- for (int x=0 ; x<width ; x+=2) {
- int v = *pixels++;
- int index = 2 * (v >> 4);
- *p++ = palette[index + 0];
- *p++ = palette[index + 1];
- if (x+1 < width) {
- index = 2 * (v & 0xF);
- *p++ = palette[index + 0];
- *p++ = palette[index + 1];
- }
- }
- }
- }
- } else if (entrySize == 3) {
- unsigned char const* const palette = (unsigned char*)data;
- for (int y=0 ; y<height ; y++) {
- unsigned char* p = (unsigned char*)surface + y*stride*3;
- if (indexBits == 8) {
- for (int x=0 ; x<width ; x++) {
- int index = 3 * (*pixels++);
- *p++ = palette[index + 0];
- *p++ = palette[index + 1];
- *p++ = palette[index + 2];
- }
- } else {
- for (int x=0 ; x<width ; x+=2) {
- int v = *pixels++;
- int index = 3 * (v >> 4);
- *p++ = palette[index + 0];
- *p++ = palette[index + 1];
- *p++ = palette[index + 2];
- if (x+1 < width) {
- index = 3 * (v & 0xF);
- *p++ = palette[index + 0];
- *p++ = palette[index + 1];
- *p++ = palette[index + 2];
- }
- }
- }
- }
- } else if (entrySize == 4) {
- unsigned char const* const palette = (unsigned char*)data;
- for (int y=0 ; y<height ; y++) {
- unsigned char* p = (unsigned char*)surface + y*stride*4;
- if (indexBits == 8) {
- for (int x=0 ; x<width ; x++) {
- int index = 4 * (*pixels++);
- *p++ = palette[index + 0];
- *p++ = palette[index + 1];
- *p++ = palette[index + 2];
- *p++ = palette[index + 3];
- }
- } else {
- for (int x=0 ; x<width ; x+=2) {
- int v = *pixels++;
- int index = 4 * (v >> 4);
- *p++ = palette[index + 0];
- *p++ = palette[index + 1];
- *p++ = palette[index + 2];
- *p++ = palette[index + 3];
- if (x+1 < width) {
- index = 4 * (v & 0xF);
- *p++ = palette[index + 0];
- *p++ = palette[index + 1];
- *p++ = palette[index + 2];
- *p++ = palette[index + 3];
- }
- }
- }
- }
- }
- }
-
-
-
- #if 1
-
- GLboolean getTextureNames( SharedTextureState* pSharedTexState ,GLsizei n, GLuint *textures)
- {
- GLuint lastValue = 1;
- GLsizei toAssign = n;
- 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
- unusedValueRange = (int(*)[2])Plat::malloc ( 2*sizeof(int));
- int index = 0 ;
- //right from the beginning to the end of the set there might be unused value , to assign these value first
- TexNameSet::iterator it = pSharedTexState->usedTexNames.begin();
- while(it != pSharedTexState->usedTexNames.end()){
- //if the values are not equal it means atleast 1 value is missing
- if(*it == lastValue){
- it++;
- lastValue++;
- }
- else{ //storing the unused name and the range
- GLsizei noOfConsecUnusedValue = *it - lastValue; //the no of unused name(not present in the set)
- unusedValueRange[index][0] = lastValue;
- unusedValueRange[index][1] = noOfConsecUnusedValue;
- index++;
- unusedValueRange = (int(*)[2])Plat::realloc(unusedValueRange, 2*(index+1)*sizeof(int));
- lastValue = lastValue + noOfConsecUnusedValue;
-
- }
- }
-
- //inserting the unused name in the set
- for(GLsizei var = 0; var < index; var++)
- {
- for (GLsizei count = 0; count < unusedValueRange[var][1] ; count++)
- {
- *textures++ = unusedValueRange[var][0];
- toAssign--;
- pSharedTexState->usedTexNames.insert(unusedValueRange[var][0]);
- unusedValueRange[var][0]++;
- if (toAssign == 0)
- {
- free(unusedValueRange);
- return GL_TRUE;
- }
- }
- }
-
- //some texture might be left to be assigned
- for( ; toAssign > 0 ; toAssign--){
- *textures++ = lastValue;
- pSharedTexState->usedTexNames.insert(lastValue);
- lastValue++;
- }
- Plat::safe_free(unusedValueRange);
- return GL_TRUE;
- }
-
- #else
-
- GLboolean getTextureNames( SharedTextureState* pSharedTexState ,GLsizei n, GLuint *textures)
- {
- GLuint lastValue = 1;
- GLsizei toAssign = n;
-
- TexNameSet::iterator it = pSharedTexState->usedTexNames.begin();
-
- //right from the beginning to the end of the set there might be unused value , to assign these value first
- while(it != pSharedTexState->usedTexNames.end()){
- //if the values are not equal it means atleast 1 value is missing
- if(*it == lastValue){
- lastValue ++ ;
- it++;
- }else{
- GLsizei noOfValue = *it - lastValue; //the no of unused name(not present in the set)
- for( GLsizei count = 0; count < noOfValue ; count++){
- *textures++ = lastValue;
- toAssign --;
- pSharedTexState->usedTexNames.insert(lastValue);
- lastValue++;
- if(toAssign == 0) return GL_TRUE;
- }
- }
- }
-
- //some texture might be left to be assigned
- for( ; toAssign > 0 ; toAssign--){
- *textures++ = lastValue;
- pSharedTexState->usedTexNames.insert(lastValue);
- lastValue++;
- }
- return GL_TRUE;
-
- }
- #endif
-
- static
- void deleteTexImage(TextureObject* pTexObj )
- {
- Image *pImgObj = NULL;
- switch(pTexObj->texType)
- {
- case GL_TEXTURE_2D:
- for(int i = 0; i < MAX_MIPMAP_LEVELS; i++)
- {
- pImgObj = &(pTexObj->images.tex2D[i]);
- if(pImgObj->hImgChunk != NULL){
- pCA->Free(pImgObj->hImgChunk);
- pImgObj->hImgChunk = NULL;
- pImgObj->imagedataLocation = NONE;
- }
-
- //Resetting the values of the image structure.
- pImgObj->imgSize = 0;
- pImgObj->isUsed = GL_FALSE;
- pImgObj->width = 0;
- pImgObj->height = 0;
- pImgObj->depth = 0;
- //not needed added just to be sure
- pImgObj->internalFormat = GLenum(-1);
- pImgObj->PixType = GLenum(-1);
- pImgObj->nativeFormat = E_INVALID_PIXEL_FORMAT;
- pImgObj->isCompressed = GL_FALSE;
- }
- break;
- case GL_TEXTURE_CUBE_MAP:
- for(int j = 0 ; j < 6; j++)
- for(int i = 0; i < MAX_MIPMAP_LEVELS; i++)
- {
- pImgObj = &(pTexObj->images.cubeMap[j][i]);
- if(pImgObj->hImgChunk != NULL){
- pCA->Free(pImgObj->hImgChunk);
- pImgObj->hImgChunk = NULL;
- pImgObj->imagedataLocation = NONE;
- }
- //Resetting the values of the image structure.
- pImgObj->imgSize = 0;
- pImgObj->isUsed = GL_FALSE;
- pImgObj->width = 0;
- pImgObj->height = 0;
- pImgObj->depth = 0;
- //not needed added just to be sure
- pImgObj->internalFormat = GLenum(-1);
- pImgObj->PixType = GLenum(-1);
- pImgObj->nativeFormat = E_INVALID_PIXEL_FORMAT;
- pImgObj->isCompressed = GL_FALSE;
-
- }
- break;
- case GL_TEXTURE_3D:
- for(int i = 0; i < MAX_MIPMAP_LEVELS; i++)
- {
- pImgObj = &(pTexObj->images.tex3D[i]);
- if(pImgObj->hImgChunk != NULL){
- pCA->Free(pImgObj->hImgChunk);
- pImgObj->hImgChunk = NULL;
- pImgObj->imagedataLocation = NONE;
- }
- //Resetting the values of the image structure.
- pImgObj->imgSize = 0;
- pImgObj->isUsed = GL_FALSE;
- pImgObj->width = 0;
- pImgObj->height = 0;
- pImgObj->depth = 0;
- //not needed added just to be sure
- pImgObj->internalFormat = GLenum(-1);
- pImgObj->PixType = GLenum(-1);
- pImgObj->nativeFormat = E_INVALID_PIXEL_FORMAT;
- pImgObj->isCompressed = GL_FALSE;
-
- }
- break;
- }
-
- }
-
-
-
- inline
- void transferImageFromTexChunckToImageChunck(TextureObject* pTexObj)
- {
-
- Image *pImgObj = NULL;
- GLubyte* pImgBuf = NULL;
-
- if(pTexObj->texType == GL_TEXTURE_2D){
- int bpp =0;
- if(pTexObj->images.tex2D[0].isCompressed == false) bpp = pixelSize(pTexObj->nativeFormat); //TO DO ONCE SURE HOW THE MIPMAPPED COMPRESSED IMAGE ARE DEALT
- GLubyte* texObjImagedata = (GLubyte*) pTexObj->hChunk->GetVirtAddr();
- for(int level = 0; level < pTexObj->levels; level++) {
- pImgObj = &(pTexObj->images.tex2D[level]);
- pImgObj->hImgChunk = pCA->New(pImgObj->imgSize);
- if(pImgObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImgObj->hImgChunk->GetVirtAddr()) == NULL)
- {
- //LOGMSG("OUT OF MEMORY \n");
- return ;
- }
- Plat::memcpy( pImgBuf, texObjImagedata+ pTexObj->Offsets[level] * bpp, pImgObj->imgSize );
- pImgObj->imagedataLocation = IMAGECHUNK;
- pTexObj->Offsets[level] = 0;
- }
- pCA->Free(pTexObj->hChunk);
- pTexObj->hChunk = NULL;
- pTexObj->levels = 0;
- texObjImagedata = NULL;
-
- }else if(pTexObj->texType == GL_TEXTURE_3D){
- int bpp = pixelSize(pTexObj->nativeFormat);
- GLubyte* texObjImagedata = (GLubyte*) pTexObj->hChunk->GetVirtAddr();
- for(int level = 0; level < pTexObj->levels; level++) {
- Image* pImageObj= &(pTexObj->images.tex3D[level]);
- pImageObj->hImgChunk = pCA->New( pImageObj->imgSize);
- if(pImageObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImageObj->hImgChunk->GetVirtAddr()) == NULL)
- {
- //LOGMSG("OUT OF MEMORY \n");
- return ;
- }
-
- Plat::memcpy( pImgBuf, texObjImagedata+ pTexObj->Offsets[level] * bpp, pImgObj->imgSize );
- pTexObj->Offsets[level] = 0;
- pImgObj->imagedataLocation = IMAGECHUNK;
- pImageObj = NULL;
- }
- pCA->Free(pTexObj->hChunk);
- pTexObj->hChunk = NULL;
- pTexObj->levels = 0;
- texObjImagedata = NULL;
-
- }else if(pTexObj->texType == GL_TEXTURE_CUBE_MAP){
- int faceOffset = pTexObj->Offsets[pTexObj->levels - 1] + 1 * 1 ;
- int bpp = pixelSize(pTexObj->nativeFormat);
- GLubyte* texObjImagedata = (GLubyte*) pTexObj->hChunk->GetVirtAddr();
- for(int faceNo = 0; faceNo < 6; faceNo++){
- for(int level = 0; level < pTexObj->levels; level++){
- Image* pImageObj= &(pTexObj->images.cubeMap[faceNo][level]);
- pImageObj->hImgChunk = pCA->New(pImageObj->imgSize);
- if(pImageObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImageObj->hImgChunk->GetVirtAddr()) == NULL)
- {
- //LOGMSG("OUT OF MEMORY \n");
- return ;
- }
- Plat::memcpy( pImgBuf, texObjImagedata + (faceOffset * faceNo + pTexObj->Offsets[level]) * bpp, pImageObj->imgSize );
- pTexObj->Offsets[level] = 0;
- pImgObj->imagedataLocation = IMAGECHUNK;
- pImageObj = NULL;
- }
- }
- pCA->Free(pTexObj->hChunk);
- pTexObj->hChunk = NULL;
- pTexObj->levels = 0;
- texObjImagedata = NULL;
- }
-
- }
-
- //called by glCopyTex* and glTex* function. If needed memory can be allocated
- GLubyte* getImageDataLocation(TextureObject* pTexObj,GLint level , GLint faceNo, GLint width , GLint height ,int depth ,GLenum format, GLenum type, GLint imgSize , GLint isExt)
- {
-
- Image *pImgObj = NULL;
- GLubyte* pImgBuf = NULL;
-
- int newDepth =1;
- if(depth != 0) newDepth =depth; //for 3D
-
- //get the pointer to the image structure
- if(pTexObj->texType == GL_TEXTURE_2D){
- pImgObj = &(pTexObj->images.tex2D[level]);
- }else if(pTexObj->texType == GL_TEXTURE_3D){
- pImgObj = &(pTexObj->images.tex3D[level]);
- }else if(pTexObj->texType == GL_TEXTURE_CUBE_MAP){
- pImgObj = &(pTexObj->images.cubeMap[faceNo][level]);
- }else{
- gAssert(false && "undetermined texture format in function getImageDataLocation");
- return NULL;
- }
-
- //if previous was externally managed texture
- if(pTexObj->isExtTex == GL_TRUE){
- pTexObj->releaseMem();
- pTexObj->images.tex2D[0].imagedataLocation = NONE;
- }
-
- //if external managed tex image: then delete all the previous mipmap level
- if(isExt == GL_TRUE){
- pTexObj->releaseMem();
- deleteTexImage(pTexObj);
- return NULL;
- }
-
- //if the image data is NULL(i.e. not present in imagechunk or tex object chunck)
- if(pImgObj->imagedataLocation == NONE){
- pImgObj->hImgChunk = pCA->New(imgSize);
- if(pImgObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImgObj->hImgChunk->GetVirtAddr()) == NULL)
- {
- // SET_ERR(pState, GL_OUT_OF_MEMORY, "glTexImage2D");
- return NULL;
- }else{
- pImgObj->imagedataLocation = IMAGECHUNK;
- pTexObj->reCompile = GL_TRUE;
- return pImgBuf;
- }
- }
-
- //if the image is present in the texture chunk .
- if(pImgObj->imagedataLocation == TEXOBJCHUNK){
- //if texture format, width , heigth , depth are same then we can use the texture chunk memory itself
- if((pImgObj->internalFormat == format) && (pImgObj->PixType == type) &&
- (pImgObj->width == width) && (pImgObj->height == height) && (pImgObj->depth == depth)){
- if(pImgObj->isCompressed == false){
-
- int bpp = GetPixelSize(format, type); //of the format specified for the current gldrawCall
- unsigned int faceOffset = (pTexObj->Offsets[pTexObj->levels - 1]) + 1 * 1;
- return(((GLubyte*)( pTexObj->hChunk->GetVirtAddr())) +(faceNo* faceOffset * bpp) + (pTexObj->Offsets[level] * bpp));
- }else{
- //TO DO ONCE SURE HOW THE MIPMAPPED COMPRESSED IMAGE ARE DEALT
- return(GLubyte*)( pTexObj->hChunk->GetVirtAddr()) ;
- }
- }else{
- //spill back the texture image from texture chunk to the image chunk
- transferImageFromTexChunckToImageChunck(pTexObj);
-
- }
- }
-
- //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)
- if(pImgObj->imagedataLocation == IMAGECHUNK){
- //if image size are same we can use the same image chunk memory location //TO DO WRONG
- if( pImgObj->imgSize == imgSize){
- pTexObj->reCompile = GL_TRUE;
- return ((GLubyte* )pImgObj->hImgChunk->GetVirtAddr());
- }else{
- //free the previous image data and then allocate the new one
- pCA->Free(pImgObj->hImgChunk);
- pImgObj->hImgChunk = NULL;
- pImgObj->hImgChunk = pCA->New(imgSize);
- if(pImgObj->hImgChunk == NULL || (pImgBuf = (GLubyte*) pImgObj->hImgChunk->GetVirtAddr()) == NULL) {
- //LOGMSG("OUT OF MEMORY \n");
- return NULL;
- }else{
- pTexObj->reCompile = GL_TRUE;
- return pImgBuf;
-
- }
-
- }
-
- }
-
- //LOGMSG(" should not have reached the end of the function :getImageDataLocation \n") ;
- return NULL;
-
- }
-
- //called by glTexSub* glCopyTexSub* function.As the memry has to be already allocated need to return the pointer
- GLubyte* getImageDataLocation(TextureObject* pTexObj, Image *pImgObj , GLint level , GLint faceNo)
- {
-
- if(pImgObj->imagedataLocation == NONE){
- gAssert(false && "subimage called but memory not allocated : an errror \n");
- }else if(pImgObj->imagedataLocation == TEXOBJCHUNK){
- if(pImgObj->isCompressed == false){
- int bpp = pixelSize(pTexObj->nativeFormat );
- unsigned int faceOffset = (pTexObj->Offsets[pTexObj->levels - 1]) + 1 * 1;
- return(((GLubyte*)( pTexObj->hChunk->GetVirtAddr())) +(faceNo* faceOffset * bpp) + (pTexObj->Offsets[level] * bpp));
- }else{
- //TO DO ONCE SURE HOW THE MIPMAPPED COMPRESSED IMAGE ARE DEALT
- return(GLubyte*)( pTexObj->hChunk->GetVirtAddr()) ;
- }
- }else if(pImgObj->imagedataLocation == IMAGECHUNK){
- return ((GLubyte* )pImgObj->hImgChunk->GetVirtAddr());
- }
- return NULL;
- }
-
-
- GLboolean isMipmapFilter(GLenum minFilter)
- {
- if((minFilter == GL_LINEAR) || (minFilter == GL_NEAREST)){
- return GL_FALSE;
- }else if((minFilter == GL_NEAREST_MIPMAP_NEAREST) ||(minFilter == GL_LINEAR_MIPMAP_NEAREST)
- || (minFilter == GL_NEAREST_MIPMAP_LINEAR ) || (minFilter == GL_LINEAR_MIPMAP_LINEAR)){
- return GL_TRUE;
-
- }else{
- gAssert(false && "undetermined mimfilter in function isMipmapFilter \n");
- return GL_TRUE;
- }
-
- }
-
- /*
- *******************************************************************************
- * OpenGL API functions
- *******************************************************************************
- */
-
- /*-----------------------------------------------------------------------*//*!
- * OpenGL active texture function.
- *
- * This function sets the active texture unit in texture state. The steps
- * performed are:
- * - Sanity checks and Set error.
- * - Store the index to active texture unit.
- *
- *//*------------------------------------------------------------------------*/
-
- GL_API void GL_APIENTRY
- glActiveTexture (GLenum texture)
- {
- OGLState* pState = GET_OGL_STATE();
- TextureState* pTexState = &(pState->texState);
-
- // Sanity checks and error reporting
- if(texture < GL_TEXTURE0 || texture >= (GL_TEXTURE0 + MAX_TEXTURE_UNITS)){
- SET_ERR(pState, GL_INVALID_OPERATION, "glActiveTexture");
- return;
- }
-
- // Store the index to texture unit
- pTexState->activeTexUnit = texture - GL_TEXTURE0;
- }
-
-
- /*-----------------------------------------------------------------------*//*!
- * OpenGL generate textures function.
- *
- * This function finds the free texture names and sets it in the pointer passed.
- * The steps performed are:
- * - Search the object array for free texture names and store as required.
- * - Record error if we don't find enough free texture names.
- *
- *//*------------------------------------------------------------------------*/
-
- GL_API void GL_APIENTRY
- glGenTextures (GLsizei n, GLuint *textures)
- {
- OGLState* pState = GET_OGL_STATE();
- SharedTextureState* psharedTexState = &(pState->sharedState->sharedTexState);
- GLuint *texID = textures;
-
- // Validate parameters
- if(n < 0 || textures == NULL){
- SET_ERR(pState, GL_INVALID_VALUE, "glGenTextures");
- return;
- }
-
- // Search in the object array for free textures
- // Texture 0 is default texture. Skip it.
-
- #if 1
- lockGLSharedTextureState(pState);
-
- if(getTextureNames(psharedTexState,n,textures) != GL_TRUE)
- {
- //this case is not expected to be reached
- SET_ERR(pState, GL_OUT_OF_MEMORY, "glGenTextures");
-
- }
-
- unlockGLSharedTextureState( pState);
-
-
- #else
- for(i = 1; i < MAX_TEXTURE_OBJECTS; i++) {
- if((pTexState->texObjects[i]).isUsed == GL_FALSE) {
- *(textures + j) = i;
- j++;
-
- if(j >= n) return; // We got all we wanted
- }
- }
-
- // We couldn't find the required no of free textures
- SET_ERR(pState, GL_OUT_OF_MEMORY, "glGenTextures");
-
- #endif
- while(n--)
- {
- while(psharedTexState->texObjects.count(*texID) > 0)
- psharedTexState->texObjects.erase(*texID);
- texID++;
- }
-
- return;
- }
-
-
- /*-----------------------------------------------------------------------*//*!
- * OpenGL bind texture function.
- *
- * This function creates or re-use texture objects and binds them to current
- * texture. The steps followed are:
- * - Validate input parameters.
- * - Check if texture object referenced is already in use. Create new object
- * if required.
- * - Check type if object already exists and raise error on mismatch. If the
- * texture referenced is default texture then type change will re-initialize
- * the default texture.
- *
- *//*------------------------------------------------------------------------*/
-
- GL_API void GL_APIENTRY
- glBindTexture (GLenum target, GLuint texture)
- {
- OGLState* pState = GET_OGL_STATE();
- TextureState* pTexState = &(pState->texState);
- SharedTextureState* pSharedTexState = &(pState->sharedState->sharedTexState);
-
- GLint texRef;
- GLuint currentUnit = pTexState->activeTexUnit;//sanvd added this
- TextureObject* lastTexObj = NULL;
- TextureObject *pTexObj;
- CurrentTexture *currTex = &pTexState->texUnitBinding[currentUnit];
-
- // -----------------------------------------------------------------------
- // Validate input parameters
- // -----------------------------------------------------------------------
-
- if(!(target == GL_TEXTURE_2D || target == GL_TEXTURE_3D || target == GL_TEXTURE_CUBE_MAP)){
- SET_ERR(pState, GL_INVALID_ENUM, "glBindTexture");
- return;
- }
-
- // -----------------------------------------------------------------------
- // Handle texture 0 special case
- // -----------------------------------------------------------------------
-
- lockGLSharedTextureState(pState);
-
- if(texture == 0) {
- // Texture zero has 3 different objects associated with it. One each
- // for different targets.
- switch(target) {
-
- case GL_TEXTURE_2D:
- //texRef = TEX_2D_DEFAULT;
- if(currTex->texture2D != 0){
- lastTexObj = GetTextureObject( target, false , 0);
- lastTexObj->release(pState);
- lastTexObj = NULL;
- }
- currTex->texture2D = texture;
- #if STORE_TEX_OBJ_POINTER == ENABLE
- pTexState->ptexUnitBinding[currentUnit].texture2D = &(pTexState->defaultTexObjects[TEX_2D_DEFAULT -TEX_2D_DEFAULT]);
- #endif
- break;
-
- case GL_TEXTURE_3D:
- //texRef = TEX_3D_DEFAULT;
- if(currTex->texture3D != 0){
- lastTexObj = GetTextureObject( target, false , 0);
- lastTexObj->release(pState);
- lastTexObj = NULL;
- }
- currTex->texture3D = texture;
- #if STORE_TEX_OBJ_POINTER == ENABLE
- pTexState->ptexUnitBinding[currentUnit].texture3D = &(pTexState->defaultTexObjects[TEX_3D_DEFAULT -TEX_2D_DEFAULT]);
- #endif
- break;
-
- case GL_TEXTURE_CUBE_MAP:
- //texRef = TEX_CUBE_MAP_DEFAULT;
- if(currTex->cubeMap != 0){
- lastTexObj = GetTextureObject( target, false , 0);
- lastTexObj->release(pState);
- lastTexObj = NULL;
- }
- currTex->cubeMap = texture;
- #if STORE_TEX_OBJ_POINTER == ENABLE
- pTexState->ptexUnitBinding[currentUnit].cubeMap = &(pTexState->defaultTexObjects[TEX_CUBE_MAP_DEFAULT -TEX_2D_DEFAULT]);
- #endif
- break;
- }
- //default textures are not shared
-
- // -----------------------------------------------------------------------
- // Handle other textures
- // -----------------------------------------------------------------------
-
- }
- else
- {
- texRef = GetTexNameArrayIndex(texture,false);
-
- if(texRef ==-1){
- SET_ERR(pState, GL_INVALID_VALUE, "glBindTexture");
- unlockGLSharedTextureState(pState);
- return;
- }
-
- // New texture object
- pTexObj = pSharedTexState->texObjects[texRef];
- if(pTexObj != NULL)
- {
- if(pTexObj->isUsed != GL_TRUE) {
- // Initialize, mark used
- pTexObj->Init(texture, target);
- //reaching inside the loop means that this is the first time the texture has been called by glBindTexture
- // so it might have been called using the glGenTexture or without it
- //this if condition can be removed as set contain only 1 copy of the value
- if(pSharedTexState->usedTexNames.count(texture) == 0){
- pSharedTexState->usedTexNames.insert(texture);
- }
-
- // Texture object reuse
-
- } else if(pTexObj->texType != target){
- // Raise error if type mismatches
- SET_ERR(pState, GL_INVALID_OPERATION, "glBindTexture");
- unlockGLSharedTextureState(pState);
- return;
- }
- // for reference count of the texture objects
- // & Store current texture value
- switch(target) {
- case GL_TEXTURE_2D:
- if(currTex->texture2D != texture)
- {
- if(currTex->texture2D != 0){
- lastTexObj = GetTextureObject( target, false , 0);
-
- lastTexObj->release(pState);
- lastTexObj = NULL;
- }
-
- pTexObj->acquire(pState);
- currTex->texture2D = texture;
- #if STORE_TEX_OBJ_POINTER == ENABLE
- pTexState->ptexUnitBinding[currentUnit].texture2D = (pSharedTexState->texObjects[texRef]);
- #endif
- }
- break;
-
- case GL_TEXTURE_3D:
-
- if(currTex->texture3D != texture){
- if(currTex->texture3D != 0){
- lastTexObj = GetTextureObject( target, false , 0);
- lastTexObj->release(pState);
- lastTexObj = NULL;
- }
- pTexObj->acquire(pState);
- currTex->texture3D = texture;
- #if STORE_TEX_OBJ_POINTER == ENABLE
- pTexState->ptexUnitBinding[currentUnit].texture3D = (pSharedTexState->texObjects[texRef]);
- #endif
- }
-
- break;
-
- case GL_TEXTURE_CUBE_MAP:
- if(currTex->cubeMap != texture){
- if(currTex->cubeMap != 0){
- lastTexObj = GetTextureObject( target, false , 0);
- lastTexObj->release(pState);
- lastTexObj = NULL;
- }
- pTexObj->acquire(pState);
- currTex->cubeMap = texture;
- #if STORE_TEX_OBJ_POINTER == ENABLE
- pTexState->ptexUnitBinding[currentUnit].cubeMap = (pSharedTexState->texObjects[texRef]);
- #endif
- }
-
- break;
- };
- }
-
- }
- unlockGLSharedTextureState(pState);
-
-
- #if 0
- // -----------------------------------------------------------------------
- // Store current texture value
- // -----------------------------------------------------------------------
-
- switch(target) {
-
- case GL_TEXTURE_2D:
- pTexState->texUnitBinding[currentUnit].texture2D = texture;
- break;
-
- case GL_TEXTURE_3D:
- pTexState->texUnitBinding[currentUnit].texture3D = texture;
- break;
-
- case GL_TEXTURE_CUBE_MAP:
- pTexState->texUnitBinding[currentUnit].cubeMap = texture;
- break;
- };
-
-
- #endif
-
- }
-
-
- /*-----------------------------------------------------------------------*//*!
- * OpenGL delete texture function.
- *//*------------------------------------------------------------------------*/
-
- GL_API void GL_APIENTRY
- glDeleteTextures (GLsizei n, const GLuint *textures)
- {
-
- OGLState* pState = GET_OGL_STATE();
- TextureState* pTexState = &(pState->texState);
- GLuint currentUnit = pTexState->activeTexUnit;
- int i = 0 ; //for count of the no of texture unit
-
-
- // Validate parameters
- if(n < 0 || textures == NULL){
- SET_ERR(pState, GL_INVALID_VALUE, "glDeleteTextures");
- return;
- }
-
- lockGLSharedTextureState(pState);
-
- while(n--){
-
- if(*textures == 0){
- continue; //silently ignore 0
- }
-
-
-
- GLint texRef = GetTexNameArrayIndex(*textures,false);//only delete userdefined tex objects..operation is heavy.....O( n )
-
- SharedTextureState *pSTState = &(pState->sharedState->sharedTexState);
- TextureObject* pTexObj = pSTState->texObjects[texRef];
- if((texRef == -1) || (pTexObj->isUsed == GL_FALSE))//it means that texture is not found in the list and that textureObject array is fulluy populated...
- //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...
- {
- //Sandeep K. spec says silently ignore 0 and invalid texture names
- //SET_ERR(pState, GL_INVALID_VALUE, "glDeleteTexture");
- // unlockGLSharedTextureState(pState);
- //return;
- continue; //it should try tio delete the other texture object
- }
- //check the reference count before deleting
- //the reference count need to be decreased for all the texture unit that is using the f=given texture object
- pTexObj->deleteTexObj = GL_TRUE;
- switch(pTexObj->texType){
-
- case GL_TEXTURE_2D:
- for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
- if ((pTexObj->id == pTexState->texUnitBinding[i].texture2D)
- && (pTexObj->id !=0)){
- pTexState->texUnitBinding[i].texture2D = 0;
- #if STORE_TEX_OBJ_POINTER == ENABLE
- pTexState->ptexUnitBinding[currentUnit].texture2D =
- &(pTexState->defaultTexObjects[TEX_2D_DEFAULT
- -TEX_2D_DEFAULT]);
- #endif
- int id = pTexObj->id;
- pSTState->ReleaseTexObj(pState, pTexObj->id) ;
- }
-
- }
- //if the object is not bounded to any texture object
- if( pTexObj->id != 0 && pSTState->texObjects.count(texRef)){
- int id = pTexObj->id;
- pSTState->ReleaseTexObj(pState, pTexObj->id);
- }
- pTexObj = NULL;
- //pSTState->texObjects[texRef] = NULL;
- break;
-
- case GL_TEXTURE_3D:
- for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
- if ((pTexObj->id == pTexState->texUnitBinding[i].texture3D)
- && (pTexObj->id !=0)){
- pTexState->texUnitBinding[i].texture3D = 0;
- #if STORE_TEX_OBJ_POINTER == ENABLE
- pTexState->ptexUnitBinding[currentUnit].texture3D =
- &(pTexState->defaultTexObjects[TEX_3D_DEFAULT
- -TEX_2D_DEFAULT]);
- #endif
- pSTState->ReleaseTexObj(pState, pTexObj->id);
- }
- }
- if( pTexObj->id != 0 && pSTState->texObjects.count(texRef)){
- pSTState->ReleaseTexObj(pState, pTexObj->id);
- }
- pTexObj = NULL;
- //pSTState->texObjects[texRef] = NULL;
- break;
-
- case GL_TEXTURE_CUBE_MAP:
- for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
- if( (pTexObj->id == pTexState->texUnitBinding[i].cubeMap)
- && (pTexObj->id !=0)){
- pTexState->texUnitBinding[i].cubeMap= 0;
- #if STORE_TEX_OBJ_POINTER == ENABLE
- pTexState->ptexUnitBinding[currentUnit].cubeMap =
- &(pTexState->defaultTexObjects[TEX_CUBE_MAP_DEFAULT
- -TEX_2D_DEFAULT]);
- #endif
- pSTState->ReleaseTexObj(pState, pTexObj->id);
- }
- }
- if( pTexObj->id != 0 && pSTState->texObjects.count(texRef)){
- pSTState->ReleaseTexObj(pState, pTexObj->id);
- }
- pTexObj = NULL;
- //pSTState->texObjects[texRef] = NULL;
- break;
- }
-
-
- #if 0
-
- //need to unbound from all texture unit
- switch(pTexState->psharedTexObjects->ptexObjects[texRef].texType ){
- case GL_TEXTURE_2D:
- for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
- if (*textures == pTexState->texUnitBinding[i].texture2D){
- pTexState->texUnitBinding[i].texture2D = 0;
- }
- }
- break;
- case GL_TEXTURE_3D:
- for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
- if (*textures == pTexState->texUnitBinding[i].texture3D){
- pTexState->texUnitBinding[i].texture3D = 0;
- }
- }
- break;
- case GL_TEXTURE_CUBE_MAP:
- for( i =0 ; i < MAX_TEXTURE_UNITS ; i++){
- if (*textures == pTexState->texUnitBinding[i].cubeMap){
- pTexState->texUnitBinding[i].cubeMap= 0;
- }
- }
- break;
- }
-
-
-
-
- if(*textures == pTexState->texUnitBinding[currentUnit].texture2D)
- pTexState->texUnitBinding[currentUnit].texture2D = 0;
- if(*textures == pTexState->texUnitBinding[currentUnit].texture3D)
- pTexState->texUnitBinding[currentUnit].texture3D = 0;
- if(*textures == pTexState->texUnitBinding[currentUnit].cubeMap)
- pTexState->texUnitBinding[currentUnit].cubeMap= 0;
-
-
- // Delete texture object
- if(*textures != 0 && pTexState->psharedTexObjects->ptexObjects[texRef].isUsed == GL_TRUE){
- pTexState->psharedTexObjects->ptexObjects[texRef].Delete();
- }
-
- //to remove form the used texture name set
- if(pTexState->psharedTexObjects->usedTexNames.count(*textures) != 0){
- pTexState->psharedTexObjects->usedTexNames.erase(*textures);
- }
- #endif
-
-
- //Sandeep K.
- //FBO spec says that is a texture is bound to the currently bound fbo it must be detached from all attachment points
- // it is attached to.
- pState->framebuffState.detachTexture(*textures);
-
- textures++; // Next object
-
- }
- unlockGLSharedTextureState(pState);
-
- }
-
-
- /*-----------------------------------------------------------------------*//*!
- * OpenGL IsTexture function.
- *//*------------------------------------------------------------------------*/
-
- GL_API GLboolean GL_APIENTRY
- glIsTexture (GLuint texture)
- {
- OGLState* pState = GET_OGL_STATE();
-
-
- // Validate parameters
- if(texture >= MAX_TEXTURE_OBJECTS){
- SET_ERR(pState, GL_INVALID_VALUE, "glIsTexture");
- return GL_FALSE;
- }
-
- GLint texRef = GetTexNameArrayIndex(texture,false);//only checks the non-default tex objs//operation is heavy.....O( n )
- if(texRef == -1) //it means that texture is not found in the list and that textureObject array is fulluy populated...
- return GL_FALSE;
-
- if(pState->sharedState->sharedTexState.texObjects[texRef]->isUsed == GL_TRUE)
- return GL_TRUE;
- else
- return GL_FALSE;
- }
-
- /*-----------------------------------------------------------------------*//*!
- * OpenGL texture parameter function (int).
- *//*------------------------------------------------------------------------*/
-
- GL_API void GL_APIENTRY
- glTexParameteri (GLenum target, GLenum pname, GLint param)
- {
- OGLState* pState = GET_OGL_STATE();
-
- TextureObject* pTexObj = NULL;
- //GLuint texRef;
-
- //Sandeep
- GLenum eParam = (GLenum)(param);
-
- // Select object reference according to target
- // Return error on invalid enum
-
- if(target == GL_TEXTURE_2D || target == GL_TEXTURE_3D || target == GL_TEXTURE_CUBE_MAP){
- pTexObj = GetTextureObject(target,false,0);
-
-
- } else {
- SET_ERR(pState, GL_INVALID_ENUM, "glTexParameter*");
- return;
- }
-
- GLboolean isMipmapFilterPrevious = isMipmapFilter(pTexObj->minFilter);
-
- // Store the parameter passed in texture state
- // Return error on invalid enum or value
-
- switch(pname) {
-
- case GL_TEXTURE_MIN_FILTER:
- if((eParam == GL_LINEAR) || (eParam == GL_NEAREST) || (eParam == GL_LINEAR_MIPMAP_LINEAR)
- || (eParam == GL_LINEAR_MIPMAP_NEAREST) || (eParam == GL_NEAREST_MIPMAP_LINEAR) ||
- (eParam == GL_NEAREST_MIPMAP_NEAREST)) {
- pTexObj->minFilter = eParam;
- break;
-
- } else {
- SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
- return;
- }
-
- case GL_TEXTURE_MAG_FILTER:
- if((eParam == GL_LINEAR) || (eParam == GL_NEAREST)){
- pTexObj->magFilter = eParam;
- break;
-
- } else {
- SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
- return;
- }
-
- case GL_TEXTURE_WRAP_S:
- if((eParam == GL_REPEAT) || (eParam == GL_CLAMP_TO_EDGE) || (eParam == GL_MIRRORED_REPEAT)) {
- pTexObj->wrapS = eParam;
- break;
-
- } else {
- SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
- return;
- }
-
- case GL_TEXTURE_WRAP_T:
- if((eParam == GL_REPEAT) || (eParam == GL_CLAMP_TO_EDGE) || (eParam == GL_MIRRORED_REPEAT)) {
- pTexObj->wrapT = eParam;
- break;
-
- } else {
- SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
- return;
- }
-
-
- case GL_TEXTURE_WRAP_R:
- if(target != GL_TEXTURE_3D) {
- SET_ERR(pState, GL_INVALID_ENUM, "glTexParameter*");
- return;
- }
-
- if((eParam == GL_REPEAT) || (eParam == GL_CLAMP_TO_EDGE) || (eParam == GL_MIRRORED_REPEAT)) {
- pTexObj->wrapR = eParam;
- break;
-
- } else {
- SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
- return;
- }
-
- default:
- SET_ERR(pState, GL_INVALID_ENUM, "glTexParameter*");
- return;
- };
-
- GLboolean isMipmapFilterPresent = isMipmapFilter(pTexObj->minFilter);
-
- if(isMipmapFilterPresent != isMipmapFilterPrevious){
- pTexObj->reCompile = GL_TRUE;
-
- }
- pTexObj->isDirtyState = GL_TRUE;
- }
-
-
- /*-----------------------------------------------------------------------*//*!
- * OpenGL texture parameter function (intv).
- *
- * See glTexParameteri() for details regarding glTexParameter* functions.
- *
- *//*------------------------------------------------------------------------*/
-
- GL_API void GL_APIENTRY
- glTexParameteriv (GLenum target, GLenum pname, const GLint *params)
- {
- OGLState* pState = GET_OGL_STATE();
-
- if(params == NULL) {
- SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
-
- } else {
- //Texture OES API - 2009.05.20
- //#ifndef GL_OES_draw_texture
- // glTexParameteri(EXPCTX_LOCAL target, pname, *params);
- //#else
- if ((target == GL_TEXTURE_2D) && (pname == GL_TEXTURE_CROP_RECT_OES)){
- TextureObject* pTexObj = NULL;
- pTexObj = GetTextureObject(target,false,0);
-
- // Plat::printf ("\nGL_TEXTURE_CROP_RECT_OES is called.\n");
-
- pTexObj->crop_rect[0] = params[0];
- pTexObj->crop_rect[1] = params[1];
- pTexObj->crop_rect[2] = params[2];
- pTexObj->crop_rect[3] = params[3];
- }else{
- glTexParameteri( target, pname, *params);
- }
- //#endif //GL_OES_draw_texture
- }
- }
-
-
- /*-----------------------------------------------------------------------*//*!
- * OpenGL texture parameter function (float).
- *
- * See glTexParameteri() for details regarding glTexParameter* functions.
- *
- *//*------------------------------------------------------------------------*/
-
- GL_API void GL_APIENTRY
- glTexParameterf (GLenum target, GLenum pname, GLfloat param)
- {
- glTexParameteri(target, pname, (GLint)param);
- }
-
-
- /*-----------------------------------------------------------------------*//*!
- * OpenGL texture parameter function (floatv).
- *
- * See glTexParameteri() for details regarding glTexParameter* functions.
- *
- *//*------------------------------------------------------------------------*/
-
- GL_API void GL_APIENTRY
- glTexParameterfv (GLenum target, GLenum pname, const GLfloat *params)
- {
- OGLState* pState = GET_OGL_STATE();
-
- if(params == NULL) {
- SET_ERR(pState, GL_INVALID_VALUE, "glTexParameter*");
-
- } else {
- //Tex…
Large files files are truncated, but you can click here to view the full file