Archived

This topic is now archived and is closed to further replies.

Locking surfaces

This topic is 5094 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I lock a surface and try to copy directly to the surface, I get this compilation error :- g_pBackBuffer->LockRect( &g_LockedRect, NULL, 0 ); for( int y = 0; y < Height; y++ ) { for( int x = 0 ;x < Width; x++ ) { (DWORD*)g_LockedRect.pBits[x] = 255; } g_LockedRect.pBits+=g_LockedRect.pitch; } g_pBackBuffer->UnlockRect(); ----------------------------------------- error C2036: ''void *'' : unknown size ----------------------------------------- How do I properly do this? Thanks.

Share this post


Link to post
Share on other sites
Why cast the dword? that might be your problem...
I have never tried editing the pBits pointer directly, I always use memcpy... you might want to try that as well.

[edited by - curtmax_0 on January 4, 2004 12:52:18 PM]

Share this post


Link to post
Share on other sites
g_LockedRect.pBits is a void variable (I think)
so you should change your code to something like this:


g_pBackBuffer->LockRect( &g_LockedRect, NULL, 0 );
for( int y = 0; y < Height; y++ )
{
for( int x = 0 ;x < Width; x++ )
{
((DWORD*)g_LockedRect.pBits)[x] = 255; // Here is the change

}
g_LockedRect.pBits+=g_LockedRect.pitch;
}
g_pBackBuffer->UnlockRect();



[EDIT] oops... made a mistake... now it's correct


Kam1kaz3

[edited by - Kamikaze15 on January 4, 2004 12:53:32 PM]

Share this post


Link to post
Share on other sites
I''m now using this method to copy data directly to the texture''s surface.


if( FAILED( D3DXCreateTexture( g_pd3dDevice, 128, 128, 1, D3DUSAGE_DYNAMIC, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &g_pTexture ) ) )
{
MessageBox( g_hWnd, "Unable to create texture", "Error", MB_ICONINFORMATION );
}

D3DLOCKED_RECT lr;
D3DXCOLOR color[128][128];
for( int y = 0; y < 128; ++y )
{
for( int x = 0; x < 128; ++x )
{
color[x][y] = D3DXCOLOR( 10.0f, 10.0f, 10.0f, 0.0f );
}
}

g_pTexture->LockRect( 0, &lr, NULL, D3DLOCK_DISCARD );
memcpy( lr.pBits, color, sizeof( color ) );
g_pTexture->UnlockRect( 0 );


This method sets the texture color to the proper color (in the above code, white or whiter-than-white) when I use D3DFMT_A32B32G32R32F, but when I use the 64-bit format, (shown in the above code), even the 64-bit integer format, the texture gets a green color. Any color I use shows up as green on the texture. But the 128-bit format doesn''t have any problems. Any ideas why this might be happening?

Share this post


Link to post
Share on other sites
As said earlier, the size of your texture in memory is not necessarily the exact dimensions you specified. I've done this for textures that are 128bit surfaces, but not 64. I've written here the code I used to do this for 128 bit surfaces, but adjusted to work with 16 bit floats. I haven't tried it out yet, but give it a shot. It should do what you're asking.

const D3DXCOLOR initColor( 10.0f, 10.0f, 10.0f, 0.0f );
D3DLOCKED_RECT lr;
if( FAILED( hr = g_pTexture->LockRect( 0, &lr, NULL, D3DLOCK_DISCARD ) ) )
return DXTRACE_ERR_MSGBOX( "LockRect", hr );
D3DXFLOAT16 *textureData = static_cast< D3DXFLOAT16* >( lr.pBits );
// Pitch is the number of bytes in one row of the texture. We want the number of floats (which are 2 bytes each in this case) in one row of the texture.
DWORD fFloatsPerRow = lr.Pitch/2;
for( DWORD i = 0; i < 128; i++ )
{
DWORD index = i * fFloatsPerRow;
for( DWORD j = 0; j < 128; j++ )
{
// Since the texture is defined as D3DFMT_A16B16G16R16F we have to make sure we assign each channel the right value.
textureData[index + j*4 + 0] = initColor.a;
textureData[index + j*4 + 1] = initColor.b;
textureData[index + j*4 + 2] = initColor.g;
textureData[index + j*4 + 3] = initColor.r;

}
}
if( FAILED( hr = g_pTexture->Unlock(0) ) )
return DXTRACE_ERR_MSGBOX( "Unlock", hr );


Hope this helps,
neneboricua

[edited by - neneboricua19 on January 5, 2004 1:52:55 PM]

Share this post


Link to post
Share on other sites
I tried it and it works. Thanks man. But there is a little change.

textureData[index + j*4 + 0] = initColor.a;
textureData[index + j*4 + 1] = initColor.b;
textureData[index + j*4 + 2] = initColor.g;
textureData[index + j*4 + 3] = initColor.r;

Gave a blue color since the pixel values became (0.0f, 10.0f, 10.0f, 10.0f). So, the appropriate correction would be :-

textureData[index + j*4 + 0] = initColor.r;
textureData[index + j*4 + 1] = initColor.g;
textureData[index + j*4 + 2] = initColor.b;
textureData[index + j*4 + 3] = initColor.a;

Which did show up as white.

Share this post


Link to post
Share on other sites