Sign in to follow this  
of_ownage

LockRect on a texture

Recommended Posts

hi guys, i have a problem with manually writting to a texture. I have this texture creation code.

	g_Device->CreateTexture( 256, 1, 1, 0, D3DFMT_A16B16G16R16, D3DPOOL_MANAGED, m_ResponseTexture, NULL );


is D3DFMT_A16B16G16R16 enough for values between: float3(-15.0 - 15.0 - 15.0); float3(1.800015, 1.774629, 2.230880); ?????????????? now i want to put data from this array into the texture: D3DXVECTOR3 CameraColor[256]; which is filled with values that are in the range from above. That means i cannot use ARGB8. I found this code:


hr = pD3DDev->CreateTexture( width, height,
									1,				// mip levels
									0,				// usage
									D3DFMT_A8R8G8B8,
									D3DPOOL_MANAGED,
									ppTex2D,
									NULL );



	// Now lock and fill the texture with the gradient
	IDirect3DTexture9 * pTex = *ppTex2D;
	D3DLOCKED_RECT lr;
	hr = pTex->LockRect( 0, &lr,
						NULL,		// region to lock - NULL locks whole thing
						0 );		// No special lock flags
									// Can't use LOCK_DISCARD unless texture is DYNAMIC

	byte * pixel;
	byte * pBase = (BYTE*) lr.pBits;
	int bytes_per_pixel = 4;
	assert( bytes_per_pixel * width == lr.Pitch );

	byte colu;
	byte colv;
	int i,j;
	D3DXVECTOR3 sum;

	for( j=0; j < height; j++ )
	{
		for( i=0; i < width; i++ )
		{
			pixel = pBase + j * lr.Pitch + i * bytes_per_pixel;

			// calculate gradient values
			colu = (byte)( lower + (upper-lower) * (float)i / ((float)width - 1.0f));
			colv = (byte)( lower + (upper-lower) * (float)j / ((float)height - 1.0f));
			sum = colu * col_mask_u + colv * col_mask_v;

			// alpha should be 255 because it is used later in alpha test
			*((D3DCOLOR*)pixel) = D3DCOLOR_ARGB( 0, (BYTE)( sum.x ), (BYTE)( sum.y ), (BYTE)( sum.z ) );
		}	
	}

	hr = pTex->UnlockRect( 0 );



well i know i must modify some stuff cos this guy assumes 32 bit per pixel. ARGB8 and i have ARGB16. So first thing i changed is: int bytes_per_pixel = 4; //4*8 = 32 to int bytes_per_pixel = 8; //8*8 = 64 = (16 bits per channel) but the problem is this line: *((D3DCOLOR*)pixel) = D3DCOLOR_ARGB( 0, (BYTE)( sum.x ), (BYTE)( sum.y ), (BYTE)( sum.z ) he is assuming a D3DCOLOR which is a unsigned long which is 0xffffffff so he has his 256 values per channel. But i have a bigger size since i have R16G16B16A16. So i cannot use Bytes. What do i have to do here? This is my current code:

	g_Device->CreateTexture( 256, 1, 1, 0, D3DFMT_A16B16G16R16, D3DPOOL_MANAGED, m_ResponseTexture, NULL );


	// Now lock and fill the texture with the gradient
	D3DLOCKED_RECT lr;
	HRESULT hr = m_ResponseTexture->LockRect( 0, &lr,
						NULL,		// region to lock - NULL locks whole thing
						0 );		// No special lock flags
									// Can't use LOCK_DISCARD unless texture is DYNAMIC

	byte * pixel;
	byte * pBase = (BYTE*) lr.pBits;
	int bytes_per_pixel = 8;

	byte colu;
	byte colv;
	int i,j;
	D3DXVECTOR3 sum;

	for( j=0; j < 256; j++ )
	{
		for( i=0; i < 1; i++ )
		{
			pixel = pBase + j * lr.Pitch + i * bytes_per_pixel;

			// alpha should be 255 because it is used later in alpha test
			*((D3DCOLOR*)pixel) = D3DCOLOR_ARGB( 0, (BYTE)( 0 ), (BYTE)( 0), (BYTE)( 0 ) );

		}	
	}


i must somehow change that line where pixel is assigned but i have no idea how to do that. Thank you for any help

Share this post


Link to post
Share on other sites
i have this code now:





g_Device->CreateTexture( 1, 256, 1, 0,D3DFMT_A32B32G32R32F , D3DPOOL_MANAGED, &m_ResponseTexture, NULL );

//D3DFMT_A32B32G32R32F
// Now lock and fill the texture with the gradient
D3DLOCKED_RECT lr;
HRESULT hr = m_ResponseTexture->LockRect( 0, &lr,
NULL, // region to lock - NULL locks whole thing
0 ); // No special lock flags
// Can't use LOCK_DISCARD unless texture is DYNAMIC

D3DXVECTOR4 * pixel;
D3DXVECTOR4 * pBase = (D3DXVECTOR4*) lr.pBits;
int bytes_per_pixel = 16; //4*8 = 32 4*32 = 128


int i = 0,j;



for( j=0; j < 256; j++ ) //height
{

pixel = pBase + j * lr.Pitch + i * bytes_per_pixel;


//*pixel = D3DXVECTOR4(CameraColor[j].x, CameraColor[j].y, CameraColor[j].z, 0.0f);
*pixel = D3DXVECTOR4(1,0,0,0);
}




it crashes at j = 217 and i dont know why :( :(

it seems memory is valid from pBase to pBase + 3480 and i absolutely dont get it. I have a D3DXVECTOR4 now which should fit the ARGB32 means one float for every 32 bit component but its not working it does not fully will it it crashes before


any idea?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this