// 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 System.IO; using System.Runtime.InteropServices; using SharpDX.Direct3D; using SharpDX.DXGI; namespace SharpDX.Direct3D10 { public partial class Resource { /// /// Gets a swap chain back buffer. /// /// The type of the buffer. /// The swap chain to get the buffer from. /// The index of the desired buffer. /// The buffer interface, or null on failure. public static T FromSwapChain(SwapChain swapChain, int index) where T : Resource { return swapChain.GetBackBuffer(index); } /// /// Loads a texture from an image file. /// /// The device used to load the texture. /// Path to the file on disk. /// The loaded texture object. public static T FromFile(Device device, string fileName) where T : Resource { IntPtr temp; Result resultOut; D3DX10.CreateTextureFromFile(device, fileName, null, IntPtr.Zero, out temp, out resultOut); // TODO Test resultOut return FromPointer(temp); } /// /// Loads a texture from an image file. /// /// The device used to load the texture. /// Path to the file on disk. /// Specifies information used to load the texture. /// The loaded texture object. public static T FromFile(Device device, string fileName, ImageLoadInformation loadInfo) where T : Resource { IntPtr temp; Result resultOut; D3DX10.CreateTextureFromFile(device, fileName, loadInfo, IntPtr.Zero, out temp, out resultOut); // TODO test resultOut? return FromPointer(temp); } /// /// Loads a texture from an image in memory. /// /// The device used to load the texture. /// Array of memory containing the image data to load. /// The loaded texture object. public static T FromMemory(Device device, byte[] memory) where T : Resource { unsafe { IntPtr temp; Result resultOut; fixed (void* pBuffer = &memory[0]) D3DX10.CreateTextureFromMemory(device, (IntPtr)pBuffer, memory.Length, null, IntPtr.Zero, out temp, out resultOut); // TODO test resultOut return FromPointer(temp); } } /// /// Loads a texture from an image in memory. /// /// The device used to load the texture. /// Array of memory containing the image data to load. /// Specifies information used to load the texture. /// The loaded texture object. public static T FromMemory(Device device, byte[] memory, ImageLoadInformation loadInfo) where T : Resource { unsafe { IntPtr temp; Result resultOut; fixed (void* pBuffer = &memory[0]) D3DX10.CreateTextureFromMemory(device, (IntPtr)pBuffer, memory.Length, loadInfo, IntPtr.Zero, out temp, out resultOut); // TODO test resultOut? return FromPointer(temp); } } /// /// Loads a texture from a stream of data. /// /// The device used to load the texture. /// A stream containing the image data to load. /// Size of the image to load. /// The loaded texture object. public static T FromStream(Device device, Stream stream, int sizeInBytes) where T : Resource { byte[] buffer = Utilities.ReadStream(stream, ref sizeInBytes); return FromMemory(device, buffer); } /// /// Loads a texture from a stream of data. /// /// The device used to load the texture. /// A stream containing the image data to load. /// Size of the image to load. /// Specifies information used to load the texture. /// The loaded texture object. public static T FromStream(Device device, Stream stream, int sizeInBytes, ImageLoadInformation loadInfo) where T : Resource { byte[] buffer = Utilities.ReadStream(stream, ref sizeInBytes); return FromMemory(device, buffer, loadInfo); } /// /// Saves a texture to file. /// /// The texture to save. /// The format the texture will be saved as. /// Name of the destination output file where the texture will be saved. /// A object describing the result of the operation. public static void ToFile(T texture, ImageFileFormat format, string fileName) where T : Resource { D3DX10.SaveTextureToFile(texture, format, fileName); } /// /// Saves a texture to a stream. /// /// The texture to save. /// The format the texture will be saved as. /// Destination memory stream where the image will be saved. /// A object describing the result of the operation. public static void ToStream(T texture, ImageFileFormat format, Stream stream) where T : Resource { Blob blob; D3DX10.SaveTextureToMemory(texture, format, out blob, 0); IntPtr bufferPtr = blob.BufferPointer; int blobSize = blob.BufferSize; // Write byte-by-byte to avoid allocating a managed byte[] that will wastefully persist. for (int byteIndex = 0; byteIndex < blobSize; ++byteIndex) stream.WriteByte(Marshal.ReadByte(bufferPtr, byteIndex)); blob.Dispose(); } /// /// Load a texture from a texture. /// /// Pointer to the source texture. See . /// Pointer to the destination texture. See . /// Pointer to texture loading parameters. See . /// The return value is one of the values listed in {{Direct3D 10 Return Codes}}. /// HRESULT D3DX10LoadTextureFromTexture([None] ID3D10Resource* pSrcTexture,[None] D3DX10_TEXTURE_LOAD_INFO* pLoadInfo,[None] ID3D10Resource* pDstTexture) public static void LoadTextureFromTexture(Resource source, Resource destination, TextureLoadInformation loadInformation) { D3DX10.LoadTextureFromTexture(source, loadInformation, destination); } /// /// Generates mipmap chain using a particular texture filter. /// /// The mipmap level whose data is used to generate the rest of the mipmap chain. /// Flags controlling how each miplevel is filtered (or D3DX10_DEFAULT for D3DX10_FILTER_BOX). See . /// The return value is one of the values listed in {{Direct3D 10 Return Codes}}. /// HRESULT D3DX10FilterTexture([None] ID3D10Resource* pTexture,[None] int SrcLevel,[None] int MipFilter) public void FilterTexture(int sourceLevel, FilterFlags mipFilter) { D3DX10.FilterTexture(this, sourceLevel, (int) mipFilter); } /// /// Returns a DXGI Surface for this resource. /// /// The buffer interface, or null on failure. public Surface AsSurface() { return QueryInterface(); } /// /// Calculates a subresource index. /// /// The index of the desired mip slice. /// The index of the desired array slice. /// The total number of mip levels. /// The subresource index (equivalent to mipSlice + (arraySlice * mipLevels)). public static int CalculateSubResourceIndex(int mipSlice, int arraySlice, int mipLevels) { return ((arraySlice * mipLevels) + mipSlice); } /// /// Calculate the MipSize /// /// /// /// internal static int GetMipSize(int mipSlice, int baseSliceSize) { float size = baseSliceSize; if (mipSlice > 0) { do { size = (float)Math.Floor(size * 0.5f); mipSlice -= 1; } while (mipSlice > 0); } return (int)size; } } }