// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using SharpDX.Mathematics.Interop; namespace SharpDX.Direct3D9 { /// /// D3DX constants and methods /// public static class D3DX { /// /// The value used to signify that the default value for a parameter should be used. /// /// D3DX_DEFAULT public const int Default = -1; /// /// The default value for non power-of-two textures. /// /// D3DX_DEFAULT_NONPOW2 public const int DefaultNonPowerOf2 = -2; /// /// Indicates that the method should format from file. /// /// D3DFMT_FROM_FILE public const int FormatFromFile = -3; /// /// Indicates that the method should load from file. /// /// D3DX_FROM_FILE public const int FromFile = -3; /// /// Checks the D3DX runtime version against this compiled version. /// /// True if version are compatible /// BOOL D3DXCheckVersion([In] unsigned int D3DSdkVersion,[In] unsigned int D3DXSdkVersion) public static bool CheckVersion() { return D3DX9.CheckVersion(D3D9.SdkVersion, D3DX9.SdkVersion); } /// /// Get and set debug mute mode. /// /// if set to true [mute]. /// Return the debug mute mode /// BOOL D3DXDebugMute([In] BOOL Mute) public static bool DebugMute(bool mute) { return D3DX9.DebugMute(mute); } /// /// Converts a declarator from a flexible vertex format (FVF) code. /// /// Combination of that describes the FVF from which to generate the returned declarator array.. /// /// A declarator from a flexible vertex format (FVF) code. /// /// HRESULT D3DXDeclaratorFromFVF([In] D3DFVF FVF,[In, Buffer] D3DVERTEXELEMENT9* pDeclarator) public static VertexElement[] DeclaratorFromFVF(VertexFormat fvf) { var vertices = new VertexElement[(int)VertexFormatDeclaratorCount.Max]; var result = D3DX9.DeclaratorFromFVF(fvf, vertices); if (result.Failure) return null; var copy = new VertexElement[D3DX9.GetDeclLength(vertices) + 1]; Array.Copy(vertices, copy, copy.Length); copy[copy.Length - 1] = VertexElement.VertexDeclarationEnd; return copy; } /// /// Converts a flexible vertex format (FVF) code from a declarator. /// /// The declarator array. /// A that describes the vertex format returned from the declarator. /// HRESULT D3DXFVFFromDeclarator([In, Buffer] const D3DVERTEXELEMENT9* pDeclarator,[Out] D3DFVF* pFVF) public static VertexFormat FVFFromDeclarator(VertexElement[] declarator) { return D3DX9.FVFFromDeclarator(declarator); } /// /// Generates an output vertex declaration from the input declaration. The output declaration is intended for use by the mesh tessellation functions. /// /// The input declaration. /// The output declaration /// HRESULT D3DXGenerateOutputDecl([In, Buffer] D3DVERTEXELEMENT9* pOutput,[In, Buffer] const D3DVERTEXELEMENT9* pInput) public static VertexElement[] GenerateOutputDeclaration(VertexElement[] declaration) { var vertices = new VertexElement[(int)VertexFormatDeclaratorCount.Max]; var result = D3DX9.GenerateOutputDecl(vertices, declaration); if (result.Failure) return null; var copy = new VertexElement[D3DX9.GetDeclLength(vertices) + 1]; Array.Copy(vertices, copy, copy.Length); copy[copy.Length - 1] = VertexElement.VertexDeclarationEnd; return copy; } /// /// Gets the number of elements in the vertex declaration. /// /// The declaration. /// The number of elements in the vertex declaration. /// unsigned int D3DXGetDeclLength([In, Buffer] const D3DVERTEXELEMENT9* pDecl) public static int GetDeclarationLength(VertexElement[] declaration) { return D3DX9.GetDeclLength(declaration); } /// /// Gets the size of a vertex from the vertex declaration. /// /// The elements. /// The stream. /// The vertex declaration size, in bytes. /// unsigned int D3DXGetDeclVertexSize([In, Buffer] const D3DVERTEXELEMENT9* pDecl,[In] unsigned int Stream) public static int GetDeclarationVertexSize(VertexElement[] elements, int stream) { return D3DX9.GetDeclVertexSize(elements, stream); } /// /// Returns the size of a vertex for a flexible vertex format (FVF). /// /// The vertex format. /// The FVF vertex size, in bytes. /// unsigned int D3DXGetFVFVertexSize([In] D3DFVF FVF) public static int GetFVFVertexSize(VertexFormat fvf) { return D3DX9.GetFVFVertexSize(fvf); } /// /// Gets the size of the rectangle patch. /// /// The segment count. /// The triangle count. /// The vertex count. /// A object describing the result of the operation. /// HRESULT D3DXRectPatchSize([In] const float* pfNumSegs,[In] unsigned int* pdwTriangles,[In] unsigned int* pdwVertices) public static Result GetRectanglePatchSize(float segmentCount, out int triangleCount, out int vertexCount) { return D3DX9.RectPatchSize(segmentCount, out triangleCount, out vertexCount); } /// /// Gets the size of the triangle patch. /// /// The segment count. /// The triangle count. /// The vertex count. /// A object describing the result of the operation. /// HRESULT D3DXTriPatchSize([In] const float* pfNumSegs,[In] unsigned int* pdwTriangles,[In] unsigned int* pdwVertices) public static Result GetTrianglePatchSize(float segmentCount, out int triangleCount, out int vertexCount) { return D3DX9.TriPatchSize(segmentCount, out triangleCount, out vertexCount); } /// /// Gets an array of from a . /// /// The stream. /// The vertex count. /// The format. /// An array of public static RawVector3[] GetVectors(DataStream stream, int vertexCount, VertexFormat format) { int stride = GetFVFVertexSize(format); return GetVectors(stream, vertexCount, stride); } /// /// Gets an array of from a . /// /// The stream. /// The vertex count. /// The stride. /// An array of public static RawVector3[] GetVectors(DataStream stream, int vertexCount, int stride) { unsafe { var results = new RawVector3[vertexCount]; for (int i = 0; i < vertexCount; i++) { results[i] = stream.Read(); stream.Position += stride - sizeof(RawVector3); } return results; } } /// /// Creates a FOURCC Format code from bytes description. /// /// The c1. /// The c2. /// The c3. /// The c4. /// A Format FourCC /// MAKEFOURCC public static Format MakeFourCC(byte c1, byte c2, byte c3, byte c4) { return (Format)((((((c4 << 8) | c3) << 8) | c2) << 8) | c1); } /// /// Generates an optimized face remapping for a triangle list. /// /// The indices. /// The face count. /// The vertex count. /// The original mesh face that was split to generate the current face. /// HRESULT D3DXOptimizeFaces([In] const void* pbIndices,[In] unsigned int cFaces,[In] unsigned int cVertices,[In] BOOL b32BitIndices,[In, Buffer] int* pFaceRemap) public static int[] OptimizeFaces(short[] indices, int faceCount, int vertexCount) { unsafe { var faces = new int[faceCount]; Result result; fixed (void* pIndices = indices) result = D3DX9.OptimizeFaces((IntPtr)pIndices, faceCount, indices.Length, false, faces); if (result.Failure) return null; return faces; } } /// /// Generates an optimized vertex remapping for a triangle list. This function is commonly used after applying the face remapping generated by D3DXOptimizeFaces. /// /// The indices. /// The face count. /// The vertex count. /// The original mesh face that was split to generate the current face. /// HRESULT D3DXOptimizeFaces([In] const void* pbIndices,[In] unsigned int cFaces,[In] unsigned int cVertices,[In] BOOL b32BitIndices,[In, Buffer] int* pFaceRemap) public static int[] OptimizeFaces(int[] indices, int faceCount, int vertexCount) { unsafe { var faces = new int[faceCount]; Result result; fixed (void* pIndices = indices) result = D3DX9.OptimizeFaces((IntPtr)pIndices, faceCount, indices.Length, true, faces); if (result.Failure) return null; return faces; } } /// /// Generates an optimized vertex remapping for a triangle list. This function is commonly used after applying the face remapping generated by . /// /// The indices. /// The face count. /// The vertex count. /// A buffer that will contain the new index for each vertex. The value stored in pVertexRemap for a given element is the source vertex location in the new vertex ordering. /// HRESULT D3DXOptimizeVertices([In] const void* pbIndices,[In] unsigned int cFaces,[In] unsigned int cVertices,[In] BOOL b32BitIndices,[In, Buffer] int* pVertexRemap) public static int[] OptimizeVertices(short[] indices, int faceCount, int vertexCount) { unsafe { var vertices = new int[vertexCount]; Result result; fixed (void* pIndices = indices) result = D3DX9.OptimizeVertices((IntPtr)pIndices, faceCount, indices.Length, false, vertices); if (result.Failure) return null; return vertices; } } /// /// Generates an optimized vertex remapping for a triangle list. This function is commonly used after applying the face remapping generated by . /// /// The indices. /// The face count. /// The vertex count. /// A buffer that will contain the new index for each vertex. The value stored in pVertexRemap for a given element is the source vertex location in the new vertex ordering. /// HRESULT D3DXOptimizeVertices([In] const void* pbIndices,[In] unsigned int cFaces,[In] unsigned int cVertices,[In] BOOL b32BitIndices,[In, Buffer] int* pVertexRemap) public static int[] OptimizeVertices(int[] indices, int faceCount, int vertexCount) { unsafe { var vertices = new int[vertexCount]; Result result; fixed (void* pIndices = indices) result = D3DX9.OptimizeVertices((IntPtr)pIndices, faceCount, indices.Length, true, vertices); if (result.Failure) return null; return vertices; } } } }