Skip to main content
GameDev.net gamedev.net
🔒 Locked

Locking surfaces

Started by poly-gone Jan 4, 2004 at 11:45 AM 8 replies 800+ views
Original Post
poly-gone
poly-gone
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.
curtmax_0
curtmax_0
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]
mdias
mdias
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]
Endurion
Endurion
Since pBits is a void pointer you also need to cast it to a byte pointer when you add the pitch (since pitch is byte offset to the next line).
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
boebi
boebi
You''ll need to insert a cast to DWORD* when you increment pBits

Ex: (DWORD*)g_LockedRect.pBits+=g_LockedRect.pitch;
poly-gone
poly-gone
Thanks for the quick reply guys.
poly-gone
poly-gone
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?
neneboricua19
neneboricua19
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]
poly-gone
poly-gone
Thanks man. I''ll try it and let you know.
poly-gone
poly-gone
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.