vertex cache

Started by
4 comments, last by supagu 20 years, 9 months ago
im trying to set up a vertex cache, i have a function

void StartCache()
{
	//lock buffers	
	if( FAILED( indexCache->Lock(0, 0, (void**)&index, 0) ))
	{
		systemLog.WriteLog("Lock Cache Index buffer [FAILED]");
		return;
	}
	
	if( FAILED( vertexCache->Lock(0, 0, (void**)&vertex, 0) ))
	{
		systemLog.WriteLog("Lock Cache Vertex buffer [FAILED]");
		return;
	}
}
 
which loks the buffers so i can write to them through out the scene, the problem is with the "index" and "vertex" variables which are defined as:

WORD* index;
STANDARD_VERTEX* vertex;
 
now when i use the sample code:

		index[ 0 ] = 0;
		index[ 1 ] = 1;
		index[ 2 ] = 2;

	STANDARD_VERTEX temp;
	temp.p = D3DXVECTOR3(0,0,0);

		vertex[ 0 ] = temp;
		vertex[ 1 ] = temp;
		vertex[ 2 ] = temp;
 
and i put that code after i lock the buffers (in the same function), then it works fine, but if i goto use that same peice of code any where else (before locking the buffers, and in the same file so they have access to "index" and "vertex", it causes my app to crash. any ideas on what is acuasing this?
Advertisement
I have an idea (hopefully the right one).
When you are locking the vertex/index buffer, notice that you are sending a pointer to a pointer. This means that in the lock function the value of the pointer changes.
It changes to point on a region of memory which is validly allocated, whereas when you use that piece of code elsewhere, the two pointers point to garbage, probably causing an access violation.
I hope this helps
_________ Evious Ltd.
Pointers obtained from Lock() calls aren''t guaranteed to be preserved after Unlock(). i.e. When you Unlock(), the pointers point to invalid memory.

Always Lock() to get your pointers, and when you Unlock(), set your pointers to NULL.

Peace,
Muhammad Haggag

By the way, I believe there''s a tutorial about writing a vertex cache here

Peace,
Muhammad Haggag

yeah thats the tut im reading,

i dont use the pointers i get when i lock after i unlock, i use them in between the lock and unlock only...

it seems to me something is funny like the variables are going out of scope at the end of the function call, but they shouldnt be because they are global
:-/
Try initializing them to NULL, sometimes that helps (Sometimes it doesn''t, but heck)

.lick

This topic is closed to new replies.

Advertisement