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.

C/C++ > Retrieving address of texture

#141757 - Rajveer - Sat Sep 29, 2007 12:56 pm

I want to retrieve the location of a texture in VRAM. glTexParameter has the following code which stores the address in the textures[i] table:

Code:
void glTexParameter(   uint8 sizeX, uint8 sizeY,
                  const uint32* addr,
                  GL_TEXTURE_TYPE_ENUM mode,
                  uint32 param) {
//---------------------------------------------------------------------------------
   textures[activeTexture] = param | (sizeX << 20) | (sizeY << 23) | (((uint32)addr >> 3) & 0xFFFF) | (mode << 26);
}


If I have a value stored in textures[i], how would I get rid of everything apart from addr?

#141774 - gmiller - Sat Sep 29, 2007 6:43 pm

based on:

textures[activeTexture] = param | (sizeX << 20) | (sizeY << 23) | (((uint32)addr >> 3) & 0xFFFF) | (mode << 26)

assuming that "param" is some value that does not set any bits lower than bit 20 then the address is on an 8 byte boundry (addr >> 3) so the lower 20 bits are the address.

addr = textures[i] & 0xFFFFF; // get low order 20 bits
addr = addr << 3; // add removed low order 3 bits


if param sets any bits in the lower 20 bits then this will negate the extraction.

#141791 - Rajveer - Sun Sep 30, 2007 1:16 am

Cheers for the helpful reply. Texture parameters can be the following, taken from libnds:

Code:
enum GL_TEXTURE_PARAM_ENUM {
   GL_TEXTURE_WRAP_S = (1 << 16), /*!< wrap(repeat) texture on S axis */
   GL_TEXTURE_WRAP_T = (1 << 17), /*!< wrap(repeat) texture on T axis */
   GL_TEXTURE_FLIP_S = (1 << 18), /*!< flip texture on S axis when wrapping */
   GL_TEXTURE_FLIP_T = (1 << 19), /*!< flip texture on T axis when wrapping */
   GL_TEXTURE_COLOR0_TRANSPARENT = (1<<29), /*!< interpret color 0 as clear, same as old GL_TEXTURE_ALPHA_MASK */
   TEXGEN_OFF      = (0<<30), /*!< use unmodified texcoord */
   TEXGEN_TEXCOORD = (1<<30), /*!< multiply texcoords by the texture-matrix */
   TEXGEN_NORMAL   = (2<<30), /*!< set texcoords equal to normal * texture-matrix, used for spherical reflection mapping */
   TEXGEN_POSITION = (3<<30)  /*!< set texcoords equal to vertex * texture-matrix */
};


So I guess that means that since the lowest bit set by param is bit 16, that addr is stored in the first 16 bits (0-15)? Would I then do

addr = textures[i] & 0xFFFF; // get low order 16 bits
addr = addr << 3; // add removed low order 3 bits

Also, what are the removed low order 3 bits? When addr is stored as (((uint32)addr >> 3) & 0xFFFF) in glTexParameter, wouldn't some information be lost when shifting to the right by 3 which wouldn't be recoverable when shifting back left by 3?

#141792 - gmiller - Sun Sep 30, 2007 1:29 am

Your right it is 16 bits (mask 0xFFFF) my mistake, I should look more closely. The missing low order 3 bits would be zero if the alignment of the data is on an 8 byte boundary so it might not be an issue. The address is only 18 bits in all (16 + 3) so the address is 0x00000 to 0x3FFF8 in range based just on the bits.

#141834 - Rajveer - Sun Sep 30, 2007 6:14 pm

Great, cheers for helping me. I have one more quick question: when trying to use the textures[] array I get a compiler error saying "'textures' undeclared (first use in this function)", I'm assuming its because its defined in videoGL.c. How would I access the array if I don't have the scope to access it in my code?

#141836 - tepples - Sun Sep 30, 2007 6:21 pm

From videoGL.h:
Code:
typedef struct {
  //[...]
  uint32 textures[MAX_TEXTURES];
  //[....]
} gl_hidden_globals;
//[...]
gl_hidden_globals* glGetGlobals();

You might have to change the code that uses this whenever libnds changes.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#141852 - Rajveer - Sun Sep 30, 2007 9:25 pm

So I'd make a prototype function in VideoGL.h which would return the struct gl_hidden_globals? Where would I store the function, or would I somehow recompile libnds's source?

#141897 - tepples - Mon Oct 01, 2007 12:32 pm

Rajveer wrote:
So I'd make a prototype function in VideoGL.h which would return the struct gl_hidden_globals? Where would I store the function, or would I somehow recompile libnds's source?

That was from CVS videoGL.h, which I'm guessing should be part of the new libnds released alongside devkitARM R21. But even released videoGL.h already has glGetTexturePointer.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#141898 - Rajveer - Mon Oct 01, 2007 12:37 pm

Oh...haha all this time and I overlooked that function. Cheers!