+++ /dev/null
-/*\r
-* Copyright (c) 2007-2009 SlimDX Group\r
-* \r
-* Permission is hereby granted, free of charge, to any person obtaining a copy\r
-* of this software and associated documentation files (the "Software"), to deal\r
-* in the Software without restriction, including without limitation the rights\r
-* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-* copies of the Software, and to permit persons to whom the Software is\r
-* furnished to do so, subject to the following conditions:\r
-* \r
-* The above copyright notice and this permission notice shall be included in\r
-* all copies or substantial portions of the Software.\r
-* \r
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
-* THE SOFTWARE.\r
-*/\r
-using System;\r
-using System.Collections.Generic;\r
-using System.Reflection;\r
-using System.Runtime.InteropServices;\r
-using System.Security.Permissions;\r
-using SharpDX;\r
-using SharpDX.Direct3D9;\r
-\r
-namespace SampleFramework\r
-{\r
- /// <summary>\r
- /// Manages aspects of the graphics device unique to Direct3D9.\r
- /// </summary>\r
- public class Direct3D9Manager\r
- {\r
- GraphicsDeviceManager manager;\r
-\r
- /// <summary>\r
- /// Gets the graphics device.\r
- /// </summary>\r
- /// <value>The graphics device.</value>\r
-#if TEST_Direct3D9Ex\r
- public DeviceEx Device //yyagi\r
-#else\r
- public Device Device\r
-#endif\r
- {\r
- get;\r
- internal set;\r
- }\r
-\r
- /// <summary>\r
- /// Initializes a new instance of the <see cref="Direct3D9Manager"/> class.\r
- /// </summary>\r
- /// <param name="manager">The parent manager.</param>\r
- internal Direct3D9Manager(GraphicsDeviceManager manager)\r
- {\r
- this.manager = manager;\r
- }\r
-\r
- /// <summary>\r
- /// Creates a vertex declaration using the specified vertex type.\r
- /// </summary>\r
- /// <param name="vertexType">Type of the vertex.</param>\r
- /// <returns>The vertex declaration for the specified vertex type.</returns>\r
- [EnvironmentPermission(SecurityAction.LinkDemand)]\r
- public VertexDeclaration CreateVertexDeclaration(Type vertexType)\r
- {\r
- // ensure that we have a value type\r
- if (!vertexType.IsValueType)\r
- throw new InvalidOperationException("Vertex types must be value types.");\r
-\r
- // grab the list of elements in the vertex\r
- List<VertexElementAttribute> objectAttributes = new List<VertexElementAttribute>();\r
- FieldInfo[] fields = vertexType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);\r
- foreach (FieldInfo field in fields)\r
- {\r
- // check for the custom attribute\r
- VertexElementAttribute[] attributes = (VertexElementAttribute[])field.GetCustomAttributes(typeof(VertexElementAttribute), false);\r
- if (field.Name.Contains("<") && field.Name.Contains(">"))\r
- {\r
- // look up the property matching this field to see if it has the attribute\r
- int index1 = field.Name.IndexOf('<');\r
- int index2 = field.Name.IndexOf('>');\r
-\r
- // parse out the name\r
- string propertyName = field.Name.Substring(index1 + 1, index2 - index1 - 1);\r
- PropertyInfo property = vertexType.GetProperty(propertyName, field.FieldType);\r
- if (property != null)\r
- attributes = (VertexElementAttribute[])property.GetCustomAttributes(typeof(VertexElementAttribute), false);\r
- }\r
- if (attributes.Length == 1)\r
- {\r
- // add the attribute to the list\r
- attributes[0].Offset = Marshal.OffsetOf(vertexType, field.Name).ToInt32();\r
- objectAttributes.Add(attributes[0]);\r
- }\r
- }\r
-\r
- // make sure we have at least one element\r
- if (objectAttributes.Count < 1)\r
- throw new InvalidOperationException("The vertex type must have at least one field or property marked with the VertexElement attribute.");\r
-\r
- // loop through the attributes and start building vertex elements\r
- List<VertexElement> elements = new List<VertexElement>();\r
- Dictionary<DeclarationUsage, int> usages = new Dictionary<DeclarationUsage, int>();\r
- foreach (VertexElementAttribute attribute in objectAttributes)\r
- {\r
- // check the current usage index\r
- if (!usages.ContainsKey(attribute.Usage))\r
- usages.Add(attribute.Usage, 0);\r
-\r
- // advance the current usage count\r
- int index = usages[attribute.Usage];\r
- usages[attribute.Usage]++;\r
-\r
- // create the element\r
- elements.Add(new VertexElement((short)attribute.Stream, (short)attribute.Offset, attribute.Type,\r
- attribute.Method, attribute.Usage, (byte)index));\r
- }\r
-\r
- elements.Add(VertexElement.VertexDeclarationEnd);\r
- return new VertexDeclaration(Device, elements.ToArray());\r
- }\r
-\r
- /// <summary>\r
- /// Creates a render target surface that is compatible with the current device settings.\r
- /// </summary>\r
- /// <param name="width">The width of the surface.</param>\r
- /// <param name="height">The height of the surface.</param>\r
- /// <returns>The newly created render target surface.</returns>\r
- public Texture CreateRenderTarget(int width, int height)\r
- {\r
- return new Texture(Device, width, height, 1, Usage.RenderTarget, manager.CurrentSettings.BackBufferFormat, Pool.Default);\r
- }\r
-\r
- /// <summary>\r
- /// Creates a resolve target for capturing the back buffer.\r
- /// </summary>\r
- /// <returns>The newly created resolve target.</returns>\r
- public Texture CreateResolveTarget()\r
- {\r
- return new Texture(Device, manager.ScreenWidth, manager.ScreenHeight, 1, Usage.RenderTarget, manager.CurrentSettings.BackBufferFormat, Pool.Default);\r
- }\r
-\r
- /// <summary>\r
- /// Resolves the current back buffer into a texture.\r
- /// </summary>\r
- /// <param name="target">The target texture.</param>\r
- /// <exception cref="InvalidOperationException">Thrown when the resolve process fails.</exception>\r
- public void ResolveBackBuffer(Texture target)\r
- {\r
- ResolveBackBuffer(target, 0);\r
- }\r
-\r
- /// <summary>\r
- /// Resolves the current back buffer into a texture.\r
- /// </summary>\r
- /// <param name="target">The target texture.</param>\r
- /// <param name="backBufferIndex">The index of the back buffer.</param>\r
- /// <exception cref="InvalidOperationException">Thrown when the resolve process fails.</exception>\r
- public void ResolveBackBuffer(Texture target, int backBufferIndex)\r
- {\r
- // disable exceptions for this method\r
- //bool storedThrow = Configuration.ThrowOnError;\r
- //Configuration.ThrowOnError = false;\r
- Surface destination = null;\r
-\r
- try\r
- {\r
- // grab the current back buffer\r
- Surface backBuffer = Device.GetBackBuffer(0, backBufferIndex);\r
- if (backBuffer == null || Result.GetResultFromWin32Error( Marshal.GetLastWin32Error() ).Failure)\r
- throw new InvalidOperationException("Could not obtain back buffer surface.");\r
-\r
- // grab the destination surface\r
- destination = target.GetSurfaceLevel(0);\r
- if (destination == null || Result.GetResultFromWin32Error( Marshal.GetLastWin32Error() ).Failure)\r
- throw new InvalidOperationException("Could not obtain resolve target surface.");\r
-\r
- // first try to copy using linear filtering\r
- Device.StretchRectangle( backBuffer, destination, TextureFilter.Linear );\r
- if ( Result.GetResultFromWin32Error( Marshal.GetLastWin32Error() ).Failure )\r
- {\r
- // that failed, so try with no filtering\r
- Device.StretchRectangle( backBuffer, destination, TextureFilter.None );\r
- if( Result.GetResultFromWin32Error( Marshal.GetLastWin32Error() ).Failure )\r
- {\r
- // that failed as well, so the last thing we can try is a load surface call\r
- Surface.FromSurface( destination, backBuffer, Filter.Default, 0 );\r
- if( Result.GetResultFromWin32Error( Marshal.GetLastWin32Error() ).Failure )\r
- throw new InvalidOperationException("Could not copy surfaces.");\r
- }\r
- }\r
- }\r
- finally\r
- {\r
- if (destination != null)\r
- destination.Dispose();\r
- //Configuration.ThrowOnError = storedThrow;\r
- }\r
- }\r
-\r
- /// <summary>\r
- /// Resets the render target.\r
- /// </summary>\r
- public void ResetRenderTarget()\r
- {\r
- Surface backBuffer = Device.GetBackBuffer(0, 0);\r
-\r
- try\r
- {\r
- Device.SetRenderTarget(0, backBuffer);\r
- }\r
- finally\r
- {\r
- backBuffer.Dispose();\r
- }\r
- }\r
- }\r
-}\r