RECT rect not being enforced.

Started by
4 comments, last by 21st Century Moose 10 years, 1 month ago
Hi Guys,

I am in the process of making a dynamic texture using LockRect() which is all working well. Except the boundaries of the rectangle are not being enforced.

I have specified the 'rect' to be 8x8 (so a total of 64 pixels).

With the following code I would expect it to give me an 8x8 white cube. But, all I get is a single line that is 64 pixels long.


RECT rect;
rect.top=0;
rect.left=0;
rect.right=8;
rect.bottom=8;

D3DLOCKED_RECT lockedRect={0};
hr=mRenderer->textureVideoGet()->LockRect(0,&lockedRect,&rect,0);
if(FAILED(hr))
{
	std::cout<<hr<<"\r\n";
	system("PAUSE");
	return 0;
}

unsigned char* bytes=(unsigned char*)lockedRect.pBits;

for(i=0;i<64*4;i++)			// 64*4 = 4 bits per pixel ARGB
{
	bytes[i]=255;			//b
	bytes[i+1]=255;			//g
	bytes[i+2]=255;			//r
	bytes[i+3]=255;			//a
}

mRenderer->textureVideoGet()->UnlockRect(0);
Any ideas as to why the 'rect' isn't being enforced would be awesome!
Advertisement

You (incorrectly) assume that the pitch of the locked area equals (locked area width*bytes per pixel).

Niko Suni

You are correct.

I had added pitch to my code a few minutes ago. But in this case the assumption was correct, so that is not the root cause of this particular problem, unfortunately.

The pitch is the scanline width of the whole surface, regardless of the dimensions of the locked rectangle.

The pointer you get from lock is to the whole surface data, offset by the rectangle's top left (even if you specify a rectangle), but you can only expect meaningful access to those pixels that fall inside your rectangle. Writing outside the rect (which is what you happen to do) results in unspecified behavior. In this case, the GPU or driver just doesn't care, since you still write to that resource's memory - even though all of it is not within your rect.

Niko Suni

It won't help, but you've a bug and a buffer overrun in your code:


for ( i=0; i+3 < 64*4; i+=4 ) {
	bytes[i] = 255;
	bytes[i+1] = 255;
	bytes[i+2] = 255;
	bytes[i+3] = 255;
}
Have you checked the HRESULT of UnlockRect?

For this kind of update you may find it easier to Lock the entire texture with D3DLOCK_NO_DIRTY_UPDATE then call AddDirtyRect when done. That can be useful if you're updating more than one region of the texture - instead of multiple updates, everything is consolidated into a single update and can run much faster. Use the Windows SetRect call to initialize a RECT representing a region on the texture, and UnionRect to accumulate RECTs representing the entire dirty region. Don't worry too much if the final full RECT contains portions of the texture that you didn't touch - a single big update is going to be MUCH faster than many many small updates, even if it contains unnecessary extra data.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement