Crash problem

BlitzMax Forums/BlitzMax Programming/Crash problem

The following fragment of code is crashing after some cycles of display.All the code is not shown for proprietary reasons. Please suggest the possible causes for the problem.

<CODE>
While Not KeyDown(KEY_ESCAPE)
Cls()
BindTexture( texture )
DrawTexture( texture )
Flip( 1 )
Wend


Function DrawTexture( texture:IDirectDrawSurface7 )
'some vertex initialization code here and finally this fucntion is called
device.DrawPrimitive( D3DPT_TRIANGLEFAN, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1, vertData, numVerts, 0 )
End Function


Function BindTexture( texture:IDirectDrawSurface7 )
Local device:IDirect3DDevice7 = D3D7GraphicsDriver().Direct3DDevice7()
device.SetTexture( 0, texture )
device.SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE )
'.... some other SetTextureState calls here
End Function


</CODE>

No clue after several tries. Your help is appreciated.

Does it crash in debug and release modes?

In debug mode doesn't it give you an idea of where it had a problem - sometimes you can get an idea of the location it fails at.

Sometimes it crashes in Flip , sometimes in DrawTexture with unhandled memory exception. sometimes it comes out of the program with process ended.

It crashes both in Debug and Release Modes. Sometimes it comes out with a bad ref obj message. Not very clear what the problem could be.

Are you potentially using many textures? Or differently asked: more than your GPU has VRAM.

I am testing only with one texture. This is to establish how to use DDS textures.

Should the Bind And Draw be done repatedly. Are there any example programs about how to correctly use DirectX for Textures, or otherwise.

What is the correct way to Draw a Texture. Please give some info on this.

Awaiting your reply.


Regards

Drawimage is the correct way to draw a texture.

I/We are trying out DDS textures which requires DirectX, DrawImage is used to draw images keeps images in uncompressed format in video ram DDS textures use less vram. please give your suggestions and advice.

write your own TCompressedImage that extends TImage and especially TD3D7CompressedImageFrame that extends TD3D7ImageFrame with its own draw method and use DrawImage with that one.

See Indiepaths Render2Texture Module as an idea which creates its own textures as well as stock Max2D textures can not be used as render targets

I/We are trying out DDS textures which requires DirectX,

Sorry, I must have missed where you explained that in your post.

DrawImage handles TImage which is stores images in uncompressed format in vram. DirectX texture drawing with DDS textures keeps the texture in compressed format in VRAM. This is needed to optimize VRAM usage for low end machines. What are the possible ways to do this?.

Regards

Can't you read them with Freeimage into a replica Timage system (or extend it as Dreamora suggests) and keep them in VRAM by not using default DX Texture Manager?
You might want to post the code, texture and full error messages.
I can't remember anybody discussing the use of DDS textures before (other than yourself) or anybody working with them (ditto). It is obviously a tricky area (I take it you are from a programming house so must be quite skilled in DX/OGL yet you are struggling) so I don't think people are going to drop what they're doing to work it out for you. The options I can see : Make it as easy as possible for somebody to help (e.g. provide code, textures etc) or give them an incentive to do it (payment or through technical interest).

Well here is the code, Sorry to give you the trouble. The teture file used ->dxt1.dds is created with DirectTexture tool of direct draw sdk. If you require I can mail you the texture file also. Thanks very much for your help.

SuperStrict

DDSTest( "dxt1.dds" )


Function DDSTest( fileName:String )
	Local fs:TStream = ReadFile( fileName )
	
	For Local i:Int = 1 To 128
		ReadByte(fs)
	Next

	SetGraphicsDriver( D3D7Max2DDriver() )
	Graphics( 800, 600 )
	
	Local width:Int = 512
	Local height:Int = 512
	
	Local texture:IDirectDrawSurface7 = CreateTexture( width, height, 0 )
	Local textureData:Byte Ptr = LockTexture( texture )
	
	For Local i:Int = 0 To 131072 - 1
		textureData[i] = ReadByte( fs )
	Next
	
	UnlockTexture( texture )
	
	SetClsColor( 255, 0, 255 )

	While Not KeyDown( KEY_ESCAPE )
		Cls()
		BindTexture( texture )
		DrawTexture( texture )
		Flip( 1 )
	Wend
End Function


Function CreateTexture:IDirectDrawSurface7( width:Int, height:Int, flags:Int )
	Local desc:DDSURFACEDESC2 = New DDSURFACEDESC2
	
	desc.dwSize = SizeOf(desc)
	desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT
	desc.dwWidth = width
	desc.dwHeight = height
	desc.ddsCaps = DDSCAPS_TEXTURE
	desc.ddsCaps2 = DDSCAPS2_TEXTUREMANAGE
	desc.ddpf_dwSize = SizeOf(DDPIXELFORMAT)
	
	desc.ddpf_dwFlags = DDPF_FOURCC
	desc.ddpf_dwFourCC = $31545844 'dxt1
'	desc.ddpf_dwFourCC = $32545844 'dxt2
'	desc.ddpf_dwFourCC = $33545844 'dxt3
'	desc.ddpf_dwFourCC = $34545844 'dxt4

	'for argb8888 format 
	Rem
	desc.ddpf_dwFlags = DDPF_RGB 
	desc.ddpf_BitCount = 32
	desc.ddpf_BitMask_0 = $00ff0000
	desc.ddpf_BitMask_1 = $0000ff00
	desc.ddpf_BitMask_2 = $000000ff
	desc.ddpf_BitMask_3 = $ff000000
	End Rem
	
	If flags & MIPMAPPEDIMAGE Then 
		DebugLog "mipmapped image"
		desc.ddsCaps :| DDSCAPS_MIPMAP | DDSCAPS_COMPLEX
	End If	
	
	Local surf:IDirectDrawSurface7 = D3D7GraphicsDriver().CreateSurface( desc )
	If Not surf Then Throw "Create DX7 surface Failed"
	
	Return surf
End Function


Function DrawTexture( texture:IDirectDrawSurface7 )
	Local device:IDirect3DDevice7 = D3D7GraphicsDriver().Direct3DDevice7()
	
	Local numVerts:Int = 4
	Local vertData:Byte Ptr = New Byte[numVerts * 6]
	Local vertFloat:Float Ptr = Float Ptr( vertData )
	Local vertInt:Int Ptr = Int Ptr( vertData )
	
	vertFloat[0] = 0
	vertFloat[1] = 0
	vertFloat[2] = 0
	vertInt[3] = $ffffffff
	vertFloat[4] = 0
	vertFloat[5] = 0
	
	vertFloat[6] = 512
	vertFloat[7] = 0
	vertFloat[8] = 0
	vertInt[9] = $ffffffff
	vertFloat[10] = 1
	vertFloat[11] = 0
	
	vertFloat[12] = 512
	vertFloat[13] = 512
	vertFloat[14] = 0
	vertInt[15] = $ffffffff
	vertFloat[16] = 1
	vertFloat[17] = 1
	
	vertFloat[18] = 0
	vertFloat[19] = 512
	vertFloat[20] = 0
	vertInt[21] = $ffffffff
	vertFloat[22] = 0
	vertFloat[23] = 1
	
	device.DrawPrimitive( D3DPT_TRIANGLEFAN, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1, vertData, numVerts, 0 )
End Function


Function BindTexture( texture:IDirectDrawSurface7 )
	Local device:IDirect3DDevice7 = D3D7GraphicsDriver().Direct3DDevice7()
	device.SetTexture( 0, texture )
	device.SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE )
	device.SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE )
	device.SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTFG_LINEAR )
	device.SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTFG_LINEAR )
	device.SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTFG_LINEAR )
End Function


Function LockTexture:Byte Ptr( texture:IDirectDrawSurface7 )
	Local desc:DDSURFACEDESC2 = New DDSURFACEDESC2
	desc.dwSize = SizeOf( desc )
	Local flags:Int = DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR | DDLOCK_WRITEONLY
	Local hresult:Int = texture.Lock( Null, desc, flags, Null )
	If ( hresult <> DD_OK ) Then RuntimeError "DD3D7ImageFrame Lock failed"
	Return desc.lpSurface
End Function


Function UnlockTexture( texture:IDirectDrawSurface7 )
	texture.Unlock( Null )
End Function



write your own TCompressedImage that extends TImage and especially TD3D7CompressedImageFrame that extends TD3D7ImageFrame with its own draw method and use DrawImage with that one.

See Indiepaths Render2Texture Module as an idea which creates its own textures as well as stock Max2D textures can not be used as render targets


Especially the r2t implementation should give you more than only simple hints on how to implement it cleanly.
As well as BRL.D3D7Max2D Module and the TD3D7Image (Frame) implementations

And you won't be able to safe that much of VRAM as you currently imagine. Any reduction of VRAM comes at a cost. DXT at the cost of quality reduction, so you will most likely use it for larger images like backgrounds etc, but not for everything. at least if your target was to have the same quality as with png

PS: and I would suggest learning the board tags. Its hard to impossible to take a "developer" serious that wants to mess with a System API but is not even capable of reading a simple board faq

How about uploading a .zip file with the code and the texture?
p.s I apologise if I have given the impression I am going to help get this to work. What I was suggesting is things for you to check and how you might get help from other people here,

Can you give me your email id so that I can send it.
Thanking you for your response.

Can you give me your email id so that I can send it.


You still seem to believe I have agreed to help on this. Just to be clear... I have not.
If you post the code and textures so people have got a chance to run it for themselves then *somebody* might help.

Thanks for your response. I request anybody who knows the solution to this problem to help me. The code is in previous post starting with "well the code.." is complete, it requires a texture file and I don't know how to upload the file to your site.
Appreciating your help.

Regards

Upload the texture file to *YOUR* site and then post a link to it here.

<EDIT> P.S. This is more community forum than a support forum. Sometimes the developers respond but, basically, you are relying on the user community to take their own time to help out of the goodness of their own hearts.
<EDIT2> If it helps your problems is when the GC runs.
Turn off GC and it should run OK (or, at least, for much longer).
<edit3> Loop through your vertdata array. It seems not to be holding the values you think it does.

Please use the following texture file

http://1800edrive.com/1800edrive/download/direct/workspace/SpacesStore/bf4794f7-df1c-11dc-8c20-6d2f8b3d9a91/dxt1.dds
Regards

except you need to register to download it ;-)

Have you tried asking one of the other two people that regularly use this account?

I'm sure you could figure out the solution much quicker between the three of you, even though you are strewn across three continents.

We have not found the solution to this problem within the group. The code given will run with the below texture file

Link for texture file is below

http://1800edrive.com/1800edrive/download/direct/workspace/SpacesStore/bf4794f7-df1c-11dc-8c20-6d2f8b3d9a91/dxt1.dds

Thanks for your help

You seem not to have read or commented on this :
<EDIT2> If it helps your problems is when the GC runs.
Turn off GC and it should run OK (or, at least, for much longer).
<edit3> Loop through your vertdata array. It seems not to be holding the values you think it does.



Thanks,

I set GCSetmode(2) and called GCCollect() in the while loop

I got the following error after two iterations

bad refs:obj=$18c9a20 refs=$0

Regards

... and if you *don't* call GCCOLLECT() what happens?
This is simply to prove to you that the issue is with GC.
If you can prove that then check the vertdata as I suggested. *IF* it is the vertdata causing the problems then try a different method OR, using the obj ref, check using debugstop which object this refers to.
My guess is GC is running and removing one of your pointers to vertdata. You might want to add them as fields in a type to check/prevent this.

P.S. I am personally a bit 'put-out' that you are charging people to write their code and then using these forums as support.
How about releasing some of this code into the code archives for the community to use?

Thta will not help.
If he has an object reference with 0 refs on it, something badly mixed up and that normally only happens when you are crazy / stupid enough to toy with the value of the references or use varptr which is not safe and must not be used within BM because it will not raise the reference counter. so when the original reference is cleared, the varptr points at a dead place

Sorry Dreamora. What are you saying will not help? I am not suggesting a solution but an investigation route.

Thats what I given.
Don't use pointers to objects that don't life long enough, otherwise the pointer will point to NULL not an object, as a pointer has no influece on the lifetime of an object!