// 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.DXGI; namespace SharpDX.Direct3D10 { public partial class Texture2D { /// /// Initializes a new instance of the class. /// /// The device with which to associate the texture. /// The description of the texture. public Texture2D(Device device, Texture2DDescription description) : base(IntPtr.Zero) { device.CreateTexture2D(ref description, null, this); } /// /// Initializes a new instance of the class. /// /// The device with which to associate the texture. /// The description of the texture. /// The initial texture data. public Texture2D(Device device, Texture2DDescription description, DataRectangle data) : this(device, description, new[] {data}) { } /// /// Initializes a new instance of the class. /// /// The device with which to associate the texture. /// The description of the texture. /// An array of initial texture data for each subresource. public Texture2D(Device device, Texture2DDescription description, DataRectangle[] data) : base(IntPtr.Zero) { DataBox[] subResourceDatas = null; if (data != null) { subResourceDatas = new DataBox[data.Length]; for (int i = 0; i < subResourceDatas.Length; i++) { subResourceDatas[i].DataPointer = data[i].DataPointer; subResourceDatas[i].RowPitch = data[i].Pitch; } } device.CreateTexture2D(ref description, subResourceDatas, this); } /// /// Converts a height map into a normal map. The (x,y,z) components of each normal are mapped to the (r,g,b) channels of the output texture. /// /// The source height map texture. /// The destination texture. /// One or more flags that control generation of normal maps. /// One or more flag specifying the source of height information. /// Constant value multiplier that increases (or decreases) the values in the normal map. Higher values usually make bumps more visible, lower values usually make bumps less visible. /// A object describing the result of the operation. public static void ComputeNormalMap(Texture2D source, Texture2D destination, NormalMapFlags flags, Channel channel, float amplitude) { D3DX10.ComputeNormalMap(source, flags, channel, amplitude, destination); } /// /// Maps the texture, providing CPU access to its contents. /// /// The mip slice to map. /// The IO operations to enable on the CPU. /// Flags indicating how the CPU should respond when the GPU is busy. /// A data rectangle containing the mapped data. This data stream is invalidated when the buffer is unmapped. public DataRectangle Map(int mipSlice, MapMode mode, MapFlags flags) { var desc = Description; int subresource = CalculateSubResourceIndex(mipSlice, 0, desc.MipLevels); DataRectangle mappedTexture2D; Map(subresource, mode, flags, out mappedTexture2D); return mappedTexture2D; } /// /// Maps the texture, providing CPU access to its contents. /// /// The mip slice to map. /// The IO operations to enable on the CPU. /// Flags indicating how the CPU should respond when the GPU is busy. /// The data stream. /// /// A data rectangle containing the mapped data. This data stream is invalidated when the buffer is unmapped. /// public DataRectangle Map(int mipSlice, MapMode mode, MapFlags flags, out DataStream dataStream) { var desc = Description; int subresource = CalculateSubResourceIndex(mipSlice, 0, desc.MipLevels); int mipHeight = GetMipSize(mipSlice, desc.Height); DataRectangle mappedTexture2D; Map(subresource, mode, flags, out mappedTexture2D); bool canRead = mode == MapMode.Read || mode == MapMode.ReadWrite; bool canWrite = mode != MapMode.Read; dataStream = new DataStream(mappedTexture2D.DataPointer, mipHeight * mappedTexture2D.Pitch, canRead, canWrite); return mappedTexture2D; } } }