View Full Version : filling a IDIRECT3DTEXTURE9 without the d3dx9 library

24-04-2003, 03:38 PM
I'm looking for a method to fill a IDIRECT3DTEXTURE9 with a Bitmap (preferable from a TBitmap) manually without using the d3dx9 library.

If anyone has any idea how to do this or if you could send me in the right the direction I would be very thankfull ! :)

Useless Hacker
24-04-2003, 08:16 PM
Here's a slightly modified version of a function I started writing for my engine, but gave up on after I decided that D3DX did a much better job.
You will need to have created the texture with the correct size etc., and I think it has to be in D3DPOOL_MANAGED.
It only handles A8R8G8B8 textures at the moment.
procedure LoadTextureFromBitmap(Bitmap: TBitmap; Texture: IDirect3DTexture8; ColorKey: TD3DColor);
hr: HResult;

surf: IDirect3DSurface9;
lrect: TD3DLocked_Rect;
i, j: Integer;
sbits, dbits: Pointer;
// Get surface of texture
hr := Texture.GetSurfaceLevel(0, surf);
if (hr <> D3D_OK) then begin
raise Exception.Create('Get Surface Level '
+ '''' + DXGetErrorString9(hr) + '''');
// Lock surface
hr := surf.LockRect(lrect, nil, 0);
if (hr <> D3D_OK) then begin
raise Exception.Create('LockRect '
+ '''' + DXGetErrorString9(hr) + '''');
// Convert to 32bit pixel format
Bitmap.PixelFormat := pf32bit;
// Copy Pixel Data with colour keying
for i := 0 to (Bitmap.Height - 1) do begin
dbits := lrect.pBits;
sbits := Bitmap.ScanLine[i];
for j := 0 to (Bitmap.Width - 1) do begin
if ((PCardinal(sbits)^) and $00FFFFFF) = (ColorKey and $00FFFFFF) then
PCardinal(dbits)^ := $00000000
PCardinal(dbits)^ := PCardinal(sbits)^ or $FF000000;

sbits := Pointer(Integer(sbits) + 4);
dbits := Pointer(Integer(dbits) + 4);

lrect.pBits := Pointer(Integer(lrect.pBits) + lrect.Pitch);
// Unlock surface
hr := surf.UnlockRect;
if (hr <> D3D_OK) then begin
raise Exception.Create('UnlockRect '
+ '''' + DXGetErrorString9(hr) + '''');
// Release the surface interface
surf := nil;

Hope this helps.

BTW: I notice there are still some problems with the forum's syntax highlighting!

24-04-2003, 09:00 PM
Just to note. D3DX9 gives you free and DEBUGGED code what in most easy usage scenario do the next things:
1) Allocates texture with most close to your original bitmap format
2) Convert your data if no matched texture format found
3) Handles cases then your texture is
3a. either larger then supported by video-card dimentions of texture - then D3DX downsamples your data
3b. ether your texture dimentions are not power of 2 and current videocard do not support such textures - then D3DX stretches original data to fill square texture
4) Generated mip-map chain on your original data.

But you can still disable any of the "features" above.

PS. You can point in what memory pool texture should be allocated.

25-04-2003, 10:38 AM
Thanks for the solution! :)

I know the D3DX9 provides a lot of extra functions and tricks, however all I'm going to do is use Direct3D for 2D drawing functions that I know use in DelphiX. At the moment I have been able to load every bitmap I wanted to test with and it works marvelous.

The D3DX9 functions are great but I don't think they are worth 1.5 MB and when I can do things by hand I mostly try doing it by hand. :)

P.S. Yes I know I'm crazy. ;)

28-04-2003, 08:11 PM
The D3DX9 functions are great but I don't think they are worth 1.5 MB and when I can do things by hand I mostly try doing it by hand. http://clootie.narod.ru - now only 444Kb for texture handling subset of D3DX9 library.

Useless Hacker
29-04-2003, 10:06 AM
What is the difference between the d3dx9.dll included with the DirectX9 sdk from Microsoft, and the d3dx9ab.dll included with Clootie's headers? Both seem to work the same with me.

Summers Gone
29-04-2003, 03:54 PM
There is no file called "d3dx9.dll" included with the DX9 SDK. I guess what you mean is "d3dx9d.dll", with the d standing for Debug. Clear ?

And Clootie, nice idea with the Texture Function Subset !

Useless Hacker
29-04-2003, 09:19 PM
Sorry, my mistake, I was getting confused between DX9 and DX8!

30-04-2003, 10:09 AM
Microsoft never provided DLL version of any version of D3DX(7,8,9). Only from DirectX8 they started to provide debug version of D3DX in D3DXnD.dll.