gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Modifying loaded texture

#167439 - 3dfx - Thu Mar 12, 2009 3:40 am

I was poking around the libnds source code and was wondering how to modify a texture I loaded?

I was figuring I could change glTexImage2D to return the address of the loaded texture, then to update it, I'd use parts of the original glTexImage2D in a new function that takes the address to store the texture as an argument like so:

Code:

uint32 *textureAddress = glTexImage2D( .... );

//
void UpdateTexture(int target, int empty1, GL_TEXTURE_TYPE_ENUM type, int sizeX, int sizeY, int empty2, GL_TEXTURE_PARAM_ENUM  param, const uint8* texture, uint32 *addr)
{
   uint32 size = 0;
   uint32 vramTemp;

   size = 1 << (sizeX + sizeY + 6);

   switch (type) {
      case GL_RGB:
      case GL_RGBA:
         size = size << 1;
         break;
      case GL_RGB4:
         size = size >> 2;
         break;
      case GL_RGB16:
         size = size >> 1;
         break;
      default:
         break;
   }

   
   // unlock texture memory
   vramTemp = vramSetMainBanks(VRAM_A_LCD,VRAM_B_LCD,VRAM_C_LCD,VRAM_D_LCD);

   if (type == GL_RGB) {
      // We do GL_RGB as GL_RGBA, but we set each alpha bit to 1 during the copy
      u16 * src = (u16*)texture;
      u16 * dest = (u16*)addr;

      glTexParameter(sizeX, sizeY, addr, GL_RGBA, param);

      while (size--) {
         *dest++ = *src | (1 << 15);
         src++;
      }
   } else {
      // For everything else, we do a straight copy
      glTexParameter(sizeX, sizeY, addr, type, param);
      swiCopy((uint32*)texture, addr , size / 4 | COPY_MODE_WORD);
   }
   vramRestoreMainBanks(vramTemp);
}


where UpdateTexture would be nearly identical to glTexImage2D other than not looking for a free texture slot.
Would this method work?

#167446 - TwentySeven - Thu Mar 12, 2009 10:18 am

yep. I did exactly that :)

#167452 - 3dfx - Thu Mar 12, 2009 3:54 pm

Awesome! Thanks a mil. :D

#167457 - elhobbs - Thu Mar 12, 2009 6:06 pm

why do you need to modify glTexImage2D? you can use glGetTexturePointer to get a pointer to VRAM.

#167458 - 3dfx - Thu Mar 12, 2009 6:22 pm

:O
I was not aware of that, (how'd I miss that function?).
Thanks for the info. :)

#167483 - elhobbs - Fri Mar 13, 2009 6:17 pm

I am not sure about older versions, but the current version of glTexImage2D does not allocate a new block of vram each call. It first checks to see if there if already memory allocated first and will reuse it. so, you do not even need a special UpdateTexture function either.