Skip to main content
gamedev.net
🔒 Locked

Dynamic buffer problem

Started by Shnoutz Aug 1, 2002 at 11:37 PM 8 replies 400+ views
Original Post
Shnoutz
Shnoutz
Here is some code that display the bug, try it or tell me if you see something that is not conform to the spec. I''m using a gf3. DynamicBug.cpp
#include "DynamicVB.h"
#include "DynamicIB.h"

#include <d3d8.h>
#include <d3dx8.h>
#pragma comment(lib, "d3d8")
#pragma comment(lib, "d3dx8")

LRESULT CALLBACK Win32Proc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

struct DynamicVertex
{
	D3DXVECTOR3 Position;
	D3DXVECTOR3 Normal;
	D3DXVECTOR2 TexCoord;
};

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	/////////////////////
	//	Win32
	/////////////////////

	WNDCLASS wcWindowClass;
	wcWindowClass.style			= CS_HREDRAW | CS_VREDRAW;
	wcWindowClass.lpfnWndProc	= (WNDPROC) Win32Proc;
	wcWindowClass.cbClsExtra	= 0;
	wcWindowClass.cbWndExtra	= 0;
	wcWindowClass.hInstance		= hInstance;
	wcWindowClass.hIcon			= LoadIcon (NULL, IDI_EXCLAMATION);
	wcWindowClass.hCursor		= LoadCursor (NULL, IDC_ARROW);
	wcWindowClass.hbrBackground	= (HBRUSH) COLOR_WINDOW;
	wcWindowClass.lpszMenuName	= NULL;
	wcWindowClass.lpszClassName	= __DATE__ __TIME__;
	RegisterClass (&wcWindowClass);

	HWND hWin32 = CreateWindow(	wcWindowClass.lpszClassName, 
								"Test", 
								WS_OVERLAPPEDWINDOW^WS_THICKFRAME^WS_MAXIMIZEBOX,
								50, 50,
								640, 480,
								NULL,
								NULL,
								hInstance,
								NULL);

	SetForegroundWindow (hWin32);
	SetActiveWindow (hWin32);
	SetFocus (hWin32);
	ShowWindow (hWin32, SW_SHOW);
	ShowCursor (FALSE);

	/////////////////////
	//	Direct3D
	/////////////////////

	LPDIRECT3D8				D3DI	= 0;
	LPDIRECT3DDEVICE8		D3D		= 0;
	D3DPRESENT_PARAMETERS	D3DPP;
	ZeroMemory (&D3DPP, sizeof (D3DPP));

	D3DPP.BackBufferCount			= 2;
	D3DPP.hDeviceWindow				= hWin32;
	D3DPP.BackBufferWidth			= 640;
	D3DPP.BackBufferHeight			= 480;
	D3DPP.SwapEffect				= D3DSWAPEFFECT_DISCARD;
	D3DPP.EnableAutoDepthStencil	= TRUE;
	D3DPP.AutoDepthStencilFormat	= D3DFMT_D24S8;
	D3DPP.MultiSampleType			= D3DMULTISAMPLE_NONE;
	D3DPP.BackBufferFormat			= D3DFMT_A8R8G8B8;
	D3DPP.Windowed					= TRUE;

	D3DI = Direct3DCreate8 (D3D_SDK_VERSION);
	D3DI->CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWin32, D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_PUREDEVICE, &D3DPP, &D3D);

	/////////////////////
	//	Create DirectX 
	//	resources
	/////////////////////

	// Dynamic vertex/index buffers
	DynamicVB 	*dvb	= new DynamicVB  (D3D, 0, 2000);
	DynamicIB32					*dib	= new DynamicIB32 (D3D, 2000);

	// Shaders
	LPD3DXBUFFER CmpShader;

	// Vertex Shader
	DWORD VertexShaderHandle = 0;

	const DWORD declarator[] = 
	{
		D3DVSD_STREAM(0),
		D3DVSD_REG (0, D3DVSDT_FLOAT3),
		D3DVSD_REG (1, D3DVSDT_FLOAT1),
		D3DVSD_END ()
	};

	const char VsCode[] = 
		"vs.1.0"				"\n"
		"mul r0, v0.x, c0"		"\n"
		"mad r0, v0.y, c1, r0"	"\n"
		"mad r0, v0.z, c2, r0"	"\n"
		"add oPos, c3, r0"		"\n"
		"sge oD0, r0, r0"		"\n";

	D3DXAssembleShader (VsCode, sizeof (VsCode)-1, 0, 0, &CmpShader, 0);
	D3D->CreateVertexShader(declarator, (DWORD*)CmpShader->GetBufferPointer(), &VertexShaderHandle, 0);
	CmpShader->Release ();
	D3D->SetVertexShader (VertexShaderHandle);

	// Pixel Shader
	DWORD PixelShaderHandle = 0;

	const char PsCode[] = 
		"ps.1.0"		"\n"
		"mov r0, v0"	"\n";
	
	D3DXAssembleShader (PsCode, sizeof (PsCode)-1, 0, 0, &CmpShader, 0);
	D3D->CreatePixelShader((DWORD*)CmpShader->GetBufferPointer(), &PixelShaderHandle);
	CmpShader->Release ();
	D3D->SetPixelShader (PixelShaderHandle);

	/////////////////////
	//	Init render
	//	states
	/////////////////////

	D3DXMATRIX MatViewProj, MatTemp;
	D3DXMatrixLookAtLH (&MatTemp, &D3DXVECTOR3(50, 50, 50), &D3DXVECTOR3(0, 0, 0), &D3DXVECTOR3(0, 1, 0));
	D3DXMatrixPerspectiveFovLH (&MatViewProj, (float)atan (0.75), 1.33f, 1, 1000);
	D3DXMatrixMultiply (&MatViewProj, &MatTemp, &MatViewProj);
	D3D->SetVertexShaderConstant (0, &MatViewProj, 4);

	/////////////////////
	//	Run
	/////////////////////

	BOOL QuitProgram = FALSE;
	while (!QuitProgram)
	{
		MSG msg;
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;
			
			DispatchMessage(&msg);
		}else
		{
			// Begin Scene
			
			D3D->BeginScene ();
			D3D->Clear (0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0, 1.0f, 0);

			dvb->FlushAtFrameStart ();
			dib->FlushAtFrameStart ();

			const float pi = 3.14159f;
			unsigned int vertexcount;
			unsigned int indexcount;
			unsigned int triscount;
			unsigned int StartVertex;
			unsigned int StartIndex;
			DynamicVertex *pVertex;
			unsigned int *pIndex;
			unsigned int i;

			//	Ok, if you put the first indexed rendering
			//	section in comment the second indexed rendering 
			//	section is rendered correctly but if you 
			//	leave it, its blowing up
/*
			// Begin Direct Rendering
			triscount = 100;
			vertexcount = triscount * 3;
			indexcount = 0;
			pVertex = dvb->Lock (vertexcount, StartVertex);
			for (i = 0; i < triscount; i++)
			{
				pVertex[3 * i + 0].Position = D3DXVECTOR3 ((float)cos (i / (float)triscount * 2.0f * pi) * 20, (float)sin (i / (float)triscount * 2.0f * pi) * 20, -5.0f);
				pVertex[3 * i + 1].Position = D3DXVECTOR3 ((float)cos ((i + 1) / (float)triscount * 2.0f * pi) * 20, (float)sin ((i + 1) / (float)triscount * 2.0f * pi) * 20, -5.0f);
				pVertex[3 * i + 2].Position = D3DXVECTOR3 (0.0f, 0.0, 0.0f);
			}
			dvb->Unlock ();
			D3D->SetStreamSource (0, dvb->GetInterface(), sizeof (DynamicVertex));
			D3D->DrawPrimitive (D3DPT_TRIANGLELIST, StartVertex, triscount);
			// End Direct Rendering
*/
			// Begin Indexed Rendering One
			triscount = 100;
			vertexcount = triscount * 3;
			indexcount = triscount * 3;
			pVertex = dvb->Lock (vertexcount, StartVertex);
			for (i = 0; i < triscount; i++)
			{
				pVertex[3 * i + 0].Position = D3DXVECTOR3 (-40.0f + (float)cos (i / (float)triscount * 2.0f * pi) * 20, (float)sin (i / (float)triscount * 2.0f * pi) * 20, -5.0f);
				pVertex[3 * i + 1].Position = D3DXVECTOR3 (-40.0f + (float)cos ((i + 1) / (float)triscount * 2.0f * pi) * 20, (float)sin ((i + 1) / (float)triscount * 2.0f * pi) * 20, -5.0f);
				pVertex[3 * i + 2].Position = D3DXVECTOR3 (-40.0f, 0.0, 0.0f);
			}
			dvb->Unlock ();
			D3D->SetStreamSource (0, dvb->GetInterface(), sizeof (DynamicVertex));
			pIndex = dib->Lock (indexcount, StartIndex);
			for (i = 0; i < triscount; i++)
			{
				pIndex[3 * i + 0] = 3 * i + 0;
				pIndex[3 * i + 1] = 3 * i + 1;
				pIndex[3 * i + 2] = 3 * i + 2;
			}
			dib->Unlock ();
			D3D->SetIndices (dib->GetInterface(), StartVertex);
			D3D->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, vertexcount, StartIndex, triscount);
			// End Indexed Rendering One
/*
			// Begin Indexed Rendering Two
			triscount = 100;
			vertexcount = triscount * 3;
			indexcount = triscount * 3;
			pVertex = dvb->Lock (vertexcount, StartVertex);
			for (i = 0; i < triscount; i++)
			{
				pVertex[3 * i + 0].Position = D3DXVECTOR3 (40.0f + (float)cos (i / (float)triscount * 2.0f * pi) * 20, (float)sin (i / (float)triscount * 2.0f * pi) * 20, -5.0f);
				pVertex[3 * i + 1].Position = D3DXVECTOR3 (40.0f + (float)cos ((i + 1) / (float)triscount * 2.0f * pi) * 20, (float)sin ((i + 1) / (float)triscount * 2.0f * pi) * 20, -5.0f);
				pVertex[3 * i + 2].Position = D3DXVECTOR3 (40.0f, 0.0, 0.0f);
			}
			dvb->Unlock ();
			D3D->SetStreamSource (0, dvb->GetInterface(), sizeof (DynamicVertex));
			pIndex = dib->Lock (indexcount, StartIndex);
			for (i = 0; i < triscount; i++)
			{
				pIndex[3 * i + 0] = 3 * i + 0;
				pIndex[3 * i + 1] = 3 * i + 1;
				pIndex[3 * i + 2] = 3 * i + 2;
			}
			dib->Unlock ();
			D3D->SetIndices (dib->GetInterface(), StartVertex);
			D3D->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, vertexcount, StartIndex, triscount);
			// End Indexed Rendering Two
*/
			// End Scene

			D3D->EndScene ();
			D3D->Present (0, 0, 0, 0);
		}
	}

	/////////////////////
	//	Clearing
	/////////////////////

	delete dvb;
	delete dib;

	D3D->SetStreamSource (0, NULL, 0);
	D3D->SetIndices (NULL, 0);

	D3DI->Release ();
	D3D->Release ();

	DestroyWindow (hWin32);
	UnregisterClass (__DATE__ __TIME__, hInstance);

	return 0;
}

LRESULT CALLBACK Win32Proc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_CLOSE :
		{
			PostQuitMessage (0);
			return 0;
		}

	case WM_DESTROY :
		{	return 0;
		}
	}

	return DefWindowProc (hWnd, uMsg, wParam, lParam);
}
 
Shnoutz
Shnoutz
DynamicIB and DynamicVB can be found here :
http://developer.nvidia.com/view.asp?IO=dynamic_vb_ib
Shnoutz
Shnoutz
Can anybody at least try it ? Just copy and paste the code in a cpp file in a win32 project. It is suppose to test the dynamic vertex and index buffers, I know it look like a lot of code at first but its not, its only a small WinMain that create a direct3d window and use the nvidia dynamic buffers headers to render some circles. But with my gf3 its not working...
Wreakon
Wreakon
Can you please post what your question is?...

-------
"Programming is like sex make one mistake, and you have to support it forever."
Homepage: http://students.washington.edu/andrey
-------Homepage: http://www.pclx.com
Shnoutz
Shnoutz
Ok I think I have not been specific enought :

Try the code and tell me if it work for you... You will have to remove the indicated commentaries to test it
but its about testing if dynamic index buffers are working.
So Im not directly asking a question but Im asking you to test it and tell me if it work...
if not tell me what you think would make it fail.

[edited by - Shnoutz on August 2, 2002 10:11:00 AM]
Evil Bill
Evil Bill
quote:

But with my gf3 its not working...


Whats wrong? Is nothing being drawn? Is there an access violation? Is a D3D function returning an error? If so what one? Are the vertices being drawn in odd places?

[edit]
D3DPP.BackBufferFormat = D3DFMT_A8R8G8B8;
You shouldn't have an alpha channel for your back buffer.
[/edit]

Steve
DirectX Programmer
Soon to be the new Bill Gates


[edited by - Evil Bill on August 2, 2002 12:39:26 PM]
Member of the Unban Mindwipe Society (UMWS)
Wreakon
Wreakon
Sorry but I would have an easier time helping you if you told me what was wrong. Not everyone helping, has VC++ readily available to do the testing, (at least me). That is preventing us/me from helping you .

-------
"Programming is like sex make one mistake, and you have to support it forever."
Homepage: http://students.washington.edu/andrey
-------Homepage: http://www.pclx.com
Shnoutz
Shnoutz
Ok.. well it seem that after the first rendering, all the
rendering using dynamic buffer are "exploded" I mean there
is no real sense of what is displayed... the polygons are
streched, they flicker and they are randomly blowing to
infinity (=:^O)... Sorry if I did not explained that before
but I tought that it would be (for somebody who tested it)
really obvious, my mistake... but I'm positive on that, the second rendering (indexed rendering one) is totally screwed
if the first one (direct rendering) is uncommented.

There is no directx error in debugger and no access violation.

And I must say that I appreciate your attention thx.

[edited by - Shnoutz on August 2, 2002 2:26:06 PM]

[edited by - Shnoutz on August 2, 2002 2:30:50 PM]
Evil Bill
Evil Bill
I haven''t looked through the code, but if your triangles are streaching to infinity, and off the screen and whatnot, it sounds like youve got uninitialized vertices, or you''re not locking the VBs properly. Remember that you shouldn''t be reading from the VB (implicity or explicitly). And watch out for things like setting the diffuse component of a vertex and leaving the position alone (D3D will bugger up the data in the buffer on purpose to prevent you reading it, so your vertices will have stupid values). Have a look Here for an example of what i mean.

HTH, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
Shnoutz
Shnoutz
Ok... There is something I did not tell you...
It is working if I use less that about 100 vertex per rendering... I'm not reading vertex back in any way...
You realise that I'm filling a little part of vertex/index
buffer with new information each time I render, I'm definitly
not using the actual content of the buffer. I leave some part of
each vertex unused because I use the same vertex format (no FVF)
for each effect. Some effect do not need the full 32bytes so
there is some memory per vertex that is left to random but it is
absolutly not used (look the declarator). I admit that this look
like a indexation of undefined vertex but I'm sure that the
vertex I indexate are defined... I would says that it is more
like if my driver was not considering BaseVertexIndex from the
SetIndices call or that in some extends the value is corrupted
between rendering calls. Note that all its working all the time
if I discard the content of the index buffer before each
rendering (In that case BaseVertexIndex is always equal to zero).

Btw... I know that I should not have used D3DFMT_A8R8G8B8 in windowed application but you know its working ...

[edited by - Shnoutz on August 2, 2002 3:11:39 PM]

Topic Locked

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

Sign in to reply to this topic.