How to use wglAllocateMemoryNV?

Where can I get some simple examples about wglAllocateMemoryNV?I had saw the example named SphereMask on,but there are still someplace I cant understand.Could you give me a hand?

check out the spec for NV_VERTEX_ARRAY_RANGE.

You use wglAllocateMemoryNV in place of the C++ ‘new’ operator. A simple example would be:

int gridXSize = 100;
int gridYSize = 100;

GLfloat *vertexArray = NULL; //Vertex locations
unsigned int *quadList = NULL; //Connectivity of faces

//Allocate the memory…

vertexArray = (GLfloat*)wglAllocateMemoryNV(3gridXSizegridYSize)*0, 0, 0.26f); // 0.26f for AGP memory

//Make the vertex array random landscape…

for (int y=0;y<gridYSize;y++)
for (int x=0;x<gridXSize;x++){
vertexArray[x + (y*gridXSize)] = rand() * 10;

//Set up the connectivity of all the vertex points in another array…

quadList = new unsigned int[4*(gridXSize-1)*(gridYSize-1)];

int count = 0;
for (int y=0;y<(gridYSize-1);y++)
for (int x=0;x<(gridXSize-1);x++){
quadList[(gridXSize4y)+(x4)+0] = count;
4y)+(x4)+1] = count+1;
quadList[(gridXSize4y)+(x4)+2] = count+1+gridXSize;
4y)+(x4)+3] = count+gridXSize;

//Make vertexArray the “current” vertex buffer to work with…

glVertexArrayRangeNV((3gridXSizegridYSize)*sizeof(GLfloat), vertexArray);

//Draw the vertex array using the following…


glVertexPointer(3, GL_FLOAT, 0, vertexArray);

glLockArraysEXT(0, (gridXSizegridYSize3));

glDrawElements(GL_QUADS, (gridXSizegridYSize3), GL_UNSIGNED_INT, quadList);

glUnlockArraysEXT(); // unlock vertex arrays


//Free the memory…


I have only just started playing with this stuff, but it gives amazing speedups! Good luck!

Thank you for your help.There is another quertion that how to see whether wglAllocateMemoryNV works correctly?

If you get a pointer back from the allocation
function, and you store data into the area
pointed at by that pointer, and you don’t
crash (indeed, if you render correctly) you
can assume it’s working correctly.

Another way is to assign into the memory in
sequential fashion and time it with
QueryPerformanceCounter(); then assign as
many elements into it in a random access
pattern and time it again; the second pass
should run much slower than the first pass
(because the memory is not cacheable).


wglAllocateMemoryNV = (PFNWGLALLOCATEMEMORYNVPROC)wglGetProcAddress(“wglAllocateMemoryNV”);

I wrote the code as below.And I have already initialize the wglAllocateMemoryNV at beginning as above.I want to know whether they are right?

struct vtxData {
GLfloat vertex [4345][3];
} *theObject=NULL,*theObjectLocal=NULL,*theObjectAGP=NULL;

if (wglAllocateMemoryNV)
theObjectAGP = (vtxData*)wglAllocateMemoryNV(sizeof(*theObject),0.0f,0.0f,1.0f);

theObjectLocal = (vtxData*)malloc(sizeof(*theObject));
theObject = theObjectLocal;

for(int num=0;num<sizeof(vertices)/sizeof(vertices[0]);num++)
theObject->vertex[num][0]=vertices[num][0];//Here vertices are the Object vertices

int i,j;
GLint lid=glGenLists(1);
glNewList(lid , GL_COMPILE);
int vi=face_indicies[i][j];//Vertex index
glVertex3f (vertices[vi][0],vertices[vi][1],vertices[vi][2]);
glEnd ();
return lid;


if you use display lists i don’t think that wglAllocateMemoryNV will speed your code because the driver will store display lists in a efficient way in the VRAM…
wglAllocateMemoryNV is usefull for vertex array techs.

BTW, when i allocate 4 Mbytes with wglAllocateMemoryNV on a 32 Mbytes geforce 2 mx NULL is returned. Is it Possible ? Why ?

Your system probably doesn’t have 4MB of free AGP memory.

  • Matt

Ok, but how could i know how many AGP MB do my system support ? Is there a way to change this value ?

Correct me if I’m wrong, but AGP memory has
to be physically contiguous. If your system
has been up for a while and doing a bunch of
things, the kernel page allocator may have
fragmented memory so badly that it can’t find
4 MB of contiguous pages.

It’s especially bad under Windows 9x, where
any page in use cannot be moved (paged out)
to make room, even if it isn’t explicitly
pinned down by a driver for DMA purposes. At
least that’s what I’ve heard :slight_smile:

No, Win9x can defragment memory just fine – you’re thinking of Win16.

Quantities of AGP memory are extremely varied among systems because of OS bugs and “features”, OS differences, chipset bugs, chipset driver bugs, motherboard bugs, video chip bugs, video driver bugs, problems with the AGP specification, and BIOS settings.

  • Matt