From 05ea5649a2831e5c8c243a85b6d9da001a1154e2 Mon Sep 17 00:00:00 2001 From: Stephan Sundermann Date: Thu, 22 Mar 2012 22:33:59 +0100 Subject: [PATCH 1/3] More error handling in Init() --- Vocaluxe/Lib/Draw/CDirect3D.cs | 57 +++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/Vocaluxe/Lib/Draw/CDirect3D.cs b/Vocaluxe/Lib/Draw/CDirect3D.cs index 4b5d68ed..a67078ca 100644 --- a/Vocaluxe/Lib/Draw/CDirect3D.cs +++ b/Vocaluxe/Lib/Draw/CDirect3D.cs @@ -179,7 +179,6 @@ namespace Vocaluxe.Lib.Draw try { _Device = new Device(_D3D, _D3D.Adapters.DefaultAdapter.Adapter, DeviceType.Hardware, Handle, flags, _PresentParameters); - //_Device.Dispose(); } catch (Exception e) { @@ -422,25 +421,47 @@ namespace Vocaluxe.Lib.Draw Matrix translate = Matrix.Translation(new Vector3(-CSettings.iRenderW / 2, CSettings.iRenderH / 2, 0)); Matrix projection = Matrix.OrthoOffCenterLH(-CSettings.iRenderW / 2, CSettings.iRenderW / 2, -CSettings.iRenderH / 2, CSettings.iRenderH / 2, CSettings.zNear, CSettings.zFar); _VertexBuffer = new VertexBuffer(_Device, CSettings.iVertexBufferElements * (4 * Marshal.SizeOf(typeof(TexturedColoredVertex))), Usage.WriteOnly | Usage.Dynamic, VertexFormat.Position | VertexFormat.Texture1 | VertexFormat.Diffuse, Pool.Default); - _Device.SetStreamSource(0, _VertexBuffer, 0, Marshal.SizeOf(typeof(TexturedColoredVertex))); + + Capabilities caps = _D3D.GetDeviceCaps(_D3D.Adapters.DefaultAdapter.Adapter, DeviceType.Hardware); + + if (_Device.SetStreamSource(0, _VertexBuffer, 0, Marshal.SizeOf(typeof(TexturedColoredVertex))).IsFailure) + CLog.LogError("Failed to set stream source"); _Device.VertexDeclaration = TexturedColoredVertex.GetDeclaration(_Device); - _Device.SetTransform(TransformState.Projection, projection); - _Device.SetTransform(TransformState.World, translate); - _Device.SetRenderState(RenderState.CullMode, Cull.None); - _Device.SetRenderState(RenderState.AlphaBlendEnable, true); - _Device.SetRenderState(RenderState.Lighting, false); - _Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha); - _Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha); + if(_Device.SetTransform(TransformState.Projection, projection).IsFailure) + CLog.LogError("Failed to set orthogonal matrix"); + if(_Device.SetTransform(TransformState.World, translate).IsFailure) + CLog.LogError("Failed to set translation matrix"); + if(_Device.SetRenderState(RenderState.CullMode, Cull.None).IsFailure) + CLog.LogError("Failed to set cull mode"); + if(_Device.SetRenderState(RenderState.AlphaBlendEnable, true).IsFailure) + CLog.LogError("Failed to enable alpha blending"); + if(_Device.SetRenderState(RenderState.Lighting, false).IsFailure) + CLog.LogError("Failed to disable lighting"); + if(_Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha).IsFailure) + CLog.LogError("Failed to set destination blend"); + if(_Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha).IsFailure) + CLog.LogError("Failed to set source blend"); if (_PresentParameters.Multisample != MultisampleType.None) - _Device.SetRenderState(RenderState.MultisampleAntialias, true); - _Device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear); - _Device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Linear); - _Device.SetSamplerState(0, SamplerState.MipFilter, TextureFilter.Linear); - _Device.SetSamplerState(0, SamplerState.AddressU, TextureAddress.Clamp); - _Device.SetSamplerState(0, SamplerState.AddressV, TextureAddress.Clamp); - _Device.SetTextureStageState(0, TextureStage.AlphaArg1, TextureArgument.Texture); - _Device.SetTextureStageState(0, TextureStage.AlphaArg2, TextureArgument.Diffuse); - _Device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Modulate); + { + if(_Device.SetRenderState(RenderState.MultisampleAntialias, true).IsFailure) + CLog.LogError("Failed to set antialiasing"); + } + if(_Device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear).IsFailure) + CLog.LogError("Failed to set min filter"); + if(_Device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Linear).IsFailure) + CLog.LogError("Failed to set mag filter"); + if(_Device.SetSamplerState(0, SamplerState.MipFilter, TextureFilter.Linear).IsFailure) + CLog.LogError("Failed to set mip filter"); + if(_Device.SetSamplerState(0, SamplerState.AddressU, TextureAddress.Clamp).IsFailure) + CLog.LogError("Failed to set clamping on u"); + if(_Device.SetSamplerState(0, SamplerState.AddressV, TextureAddress.Clamp).IsFailure) + CLog.LogError("Failed to set claming on v"); + if(_Device.SetTextureStageState(0, TextureStage.AlphaArg1, TextureArgument.Texture).IsFailure) + CLog.LogError("Failed to set alpha argument 1"); + if(_Device.SetTextureStageState(0, TextureStage.AlphaArg2, TextureArgument.Diffuse).IsFailure) + CLog.LogError("Failed to set alpha argument 2"); + if(_Device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Modulate).IsFailure) + CLog.LogError("Failed to set alpha operation"); Int16[] indices = new Int16[6]; indices[0] = 0; From f31fdd4668cb1aa56b278635aae56ebe70d50a81 Mon Sep 17 00:00:00 2001 From: Stephan Sundermann Date: Thu, 22 Mar 2012 22:37:07 +0100 Subject: [PATCH 2/3] Fixed index buffer --- Vocaluxe/Lib/Draw/CDirect3D.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Vocaluxe/Lib/Draw/CDirect3D.cs b/Vocaluxe/Lib/Draw/CDirect3D.cs index a67078ca..880edf2a 100644 --- a/Vocaluxe/Lib/Draw/CDirect3D.cs +++ b/Vocaluxe/Lib/Draw/CDirect3D.cs @@ -471,7 +471,7 @@ namespace Vocaluxe.Lib.Draw indices[4] = 2; indices[5] = 3; - _IndexBuffer = new IndexBuffer(_Device, 6 * sizeof(int), Usage.WriteOnly, Pool.Managed, true); + _IndexBuffer = new IndexBuffer(_Device, 6 * sizeof(Int16), Usage.WriteOnly, Pool.Managed, true); DataStream stream = _IndexBuffer.Lock(0, 0, LockFlags.None); stream.WriteRange(indices); From bdf02e65250ac1cbd3f5acbfaa4c1adfd38605b3 Mon Sep 17 00:00:00 2001 From: Stephan Sundermann Date: Thu, 22 Mar 2012 22:47:06 +0100 Subject: [PATCH 3/3] More error handling in CDirect3D.cs --- Vocaluxe/Lib/Draw/CDirect3D.cs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Vocaluxe/Lib/Draw/CDirect3D.cs b/Vocaluxe/Lib/Draw/CDirect3D.cs index 880edf2a..9745fa92 100644 --- a/Vocaluxe/Lib/Draw/CDirect3D.cs +++ b/Vocaluxe/Lib/Draw/CDirect3D.cs @@ -422,8 +422,6 @@ namespace Vocaluxe.Lib.Draw Matrix projection = Matrix.OrthoOffCenterLH(-CSettings.iRenderW / 2, CSettings.iRenderW / 2, -CSettings.iRenderH / 2, CSettings.iRenderH / 2, CSettings.zNear, CSettings.zFar); _VertexBuffer = new VertexBuffer(_Device, CSettings.iVertexBufferElements * (4 * Marshal.SizeOf(typeof(TexturedColoredVertex))), Usage.WriteOnly | Usage.Dynamic, VertexFormat.Position | VertexFormat.Texture1 | VertexFormat.Diffuse, Pool.Default); - Capabilities caps = _D3D.GetDeviceCaps(_D3D.Adapters.DefaultAdapter.Adapter, DeviceType.Hardware); - if (_Device.SetStreamSource(0, _VertexBuffer, 0, Marshal.SizeOf(typeof(TexturedColoredVertex))).IsFailure) CLog.LogError("Failed to set stream source"); _Device.VertexDeclaration = TexturedColoredVertex.GetDeclaration(_Device); @@ -516,12 +514,14 @@ namespace Vocaluxe.Lib.Draw //Clear the previous Frame ClearScreen(); //We want to begin drawing - _Device.BeginScene(); + if (_Device.BeginScene().IsFailure) + CLog.LogError("Failed to begin scene"); CheckQueque(); _Run = _Run && CGraphics.Draw(); RenderVertexBuffer(); //We finished drawing the frame - _Device.EndScene(); + if (_Device.EndScene().IsFailure) + CLog.LogError("Failed to end scene"); _Run = CGraphics.UpdateGameLogic(_Keys, _Mouse); try { @@ -571,7 +571,8 @@ namespace Vocaluxe.Lib.Draw TexturedColoredVertex.GetDeclaration(_Device).Dispose(); _VertexBuffer.Dispose(); _IndexBuffer.Dispose(); - _Device.Reset(_PresentParameters); + if (_Device.Reset(_PresentParameters).IsFailure) + CLog.LogError("Failed to reset the device"); } /// @@ -671,11 +672,14 @@ namespace Vocaluxe.Lib.Draw for (int i = 0; i < _Vertices.Count; i += 4) { //Apply rotation - _Device.SetTransform(TransformState.World, _VerticesRotationMatrices.Dequeue()); + if (_Device.SetTransform(TransformState.World, _VerticesRotationMatrices.Dequeue()).IsFailure) + CLog.LogError("Failed to set world transformation"); //Apply texture - _Device.SetTexture(0, _VerticesTextures.Dequeue()); + if (_Device.SetTexture(0, _VerticesTextures.Dequeue()).IsFailure) + CLog.LogError("Failed to set texture"); //Draw 2 triangles from vertexbuffer - _Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, i, 0, 4, 0, 2); + if (_Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, i, 0, 4, 0, 2).IsFailure) + CLog.LogError("Failed to draw quad"); } //Clear the queues for the next frame _Vertices.Clear(); @@ -692,7 +696,8 @@ namespace Vocaluxe.Lib.Draw /// public void ClearScreen() { - _Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); + if (_Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0).IsFailure) + CLog.LogError("Failed to clear the backbuffer"); } ///