PDA

View Full Version : Vertex buffers & DLL's



NecroDOME
14-04-2007, 08:12 PM
So, Im stuck with this the whole day (and a bit yesterday)

But what I'm trying to do is create a vertex buffer within a DLL.

Basicly my app request a pointer to from the dll to a vertex buffer (DirectX).
The DLL creates a D3DVertexBuffer9, then my app locks the buffer by sending the pointer from the buffer to the DLL. The DLL should lock the buffer and give a pointer to the data. Next I fill the data and this part goes wrong...

See the code below. I placed some comments in the code that are usefull, check them plz...

Here is some code of the app:

procedure TNecro3D_Mesh.CreateVertexBuffer;
var Param : TNecro3D_VertexBufferParam;
_vb: pointer;
i, VertexCount: Integer;
begin
Loaded := False;
VertexCount := High(VertexArray)+3; // +3, yeah i know...(for debuggin')
Param.StrideSize := SizeOf(TNecro3D_MeshVertex);
Param.Size := Param.StrideSize * VertexCount;
Param.Pool := PoolDefault;
Param.Format := [vfXYZ, vfNormal, vfTex1];
Param.Usage := useNormal;

VertexBuffer := TNecro3D_VertexBuffer.Create;
VertexBuffer.Init(Param);

// VertexBuffer = TNecro3D_VertexBuffer, not DX, it calls a proc inside the dll to do DX's work...
if VertexBuffer.Lock(0, Param.Size, lockDefault, _vb) then
begin
// VertexArray = array of TNecro3D_MeshVertex
// _vb is the pointer that should hold the pointer to what D3DX gave it...
CopyMemory(_vb, @VertexArray, SizeOf(TNecro3D_MeshVertex)* High(VertexArray));
// CopyMemory is the part that gives the error... but why? (i think it has something to do with the pointers :S

VertexBuffer.UnLock;
Loaded := True;
end;

end;


From the DLL:

// The lock-function, Data: Pointer should return Data piotner that I got from DX
function TNecro3D_VertexBuffer9.Lock(Offset, SizeToLock: Cardinal;
Flags: TNecro3D_VertexBufferLockFlags; Data: Pointer): boolean;
var LockFlags: Cardinal;
begin
Result := FBufferCreated;
if Result=False then
Exit;

case Flags of
lockDefault: LockFlags := 0;
lockDiscard: LockFlags := D3DLOCK_DISCARD;
lockAppend: LockFlags := D3DLOCK_NOOVERWRITE;
end;

// Buffer = IDirect3DVertexBuffer9
if Succeeded(Buffer.Lock(Offset, SizeToLock, Data, LockFlags)) then
begin
FLocked := True;
end else
FLocked := False;

Result := FLocked;
end;



// The way I create the vertex buffer:
constructor TNecro3D_VertexBuffer9.Create(Param: TNecro3D_VertexBufferParam);
var Device : TNecro3D_Device9;
Usage : cardinal;
Pool : TD3DPool;
begin
FParam := Param;

FLocked := False;
FBufferCreated := False;

FFVF := ConvertFVFFlags(Param.Format);

case Param.Usage of
useNormal : Usage := D3DUSAGE_WRITEONLY;
useDynamic: Usage := D3DUSAGE_DYNAMIC or D3DUSAGE_WRITEONLY;
end;

case Param.Pool of
PoolDefault: Pool := D3DPOOL_DEFAULT;
PoolSysMem : Pool := D3DPOOL_SYSTEMMEM;
end;

Device := TNecro3D_Device9(GetDevice);

// Buffer is IDriect3DVertexBuffer9
if Failed(Device.Screen.Device.CreateVertexBuffer(Par am.Size,
Usage, FFVF, Pool, Buffer, nil)) then
begin
FBufferCreated := False;
end else
FBufferCreated := True;
end;
:shock:

Clootie
14-04-2007, 11:53 PM
How about:
CopyMemory(_vb, @VertexArray[0], SizeOf(TNecro3D_MeshVertex) * (High(VertexArray) + 1) );

NecroDOME
15-04-2007, 11:32 AM
Well, it was a pointer problem...I changed all my procs to Lock(Start, Count, Flags): Pointer;, where the pointer gives the result of the lock or nil if it fails. Damn.. it took me 2 day's to figure this out... :)

I think it had someting to do that I used Lock(Start, Count, Falgs, Out Data:Pointer); (the "Out Data:Pointer")