public Texture2D(GraphicsDevice graphicsDevice, int width, int height, TextureFormat format, int arraySize, int mipLevels, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data) : base(graphicsDevice, new StackTrace(1)) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", "Width must be positive."); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "Height must be positive."); } if (mipLevels < 0) { throw new ArgumentOutOfRangeException("mipLevels", "MipLevels must not be negative."); } if (format == TextureFormat.Unknown) { throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format"); } if (arraySize <= 0) { throw new ArgumentOutOfRangeException("arraySize", "ArraySize must be positive."); } if (usage == ResourceUsage.Immutable && (data == null || data.Length == 0)) { throw new ArgumentException("data", "Immutable textures must be initialized with data."); } if (data != null && data.Length != 0 && data.Length < mipLevels * arraySize) { throw new ArgumentOutOfRangeException("data", data.Length, string.Format("data Lenght is too small for specified arraySize and mipLevels, expected: {0}", mipLevels * arraySize)); } Texture2DDescription desc = new Texture2DDescription() { Width = width, Height = height, Format = EnumConverter.Convert(format), ArraySize = arraySize, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), CpuAccessFlags = CpuAccessFlags.None, MipLevels = mipLevels > 0 ? mipLevels : 1, Usage = EnumConverter.Convert(usage) }; desc.BindFlags |= BindFlags.ShaderResource; if (data != null && data.Length > 1) { SharpDX.DataRectangle[] rects = new SharpDX.DataRectangle[data.Length]; for (int i = 0; i < data.Length; i++) { rects[i] = new SharpDX.DataRectangle(data[i].Pointer, data[i].Pitch); } this.Texture = new SharpDX.Direct3D11.Texture2D(graphicsDevice.Device, desc, rects); } else { this.Texture = new SharpDX.Direct3D11.Texture2D(graphicsDevice.Device, desc); } CreateViews(); }
public Texture2D(GraphicsDevice graphicsDevice, int width, int height, TextureFormat format, int mipLevels, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data) : this(graphicsDevice, width, height, format, 1, mipLevels, usage, data) { }
public Texture2D(GraphicsDevice graphicsDevice, int width, int height, TextureFormat format, bool isRenderTarget, bool generateMipMaps, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data) : this(graphicsDevice, width, height, format, 1, isRenderTarget, generateMipMaps, usage, data) { }
internal Texture2D(GraphicsDevice graphicsDevice, int width, int height, TextureFormat format, int arraySize, bool isRenderTarget, bool generateMipMaps, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data) : base(graphicsDevice, new StackTrace(1)) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", "Width must be positive."); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "Height must be positive."); } if (format == TextureFormat.Unknown) { throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format"); } if (arraySize <= 0) { throw new ArgumentOutOfRangeException("arraySize", "ArraySize must be positive."); } if (usage == ResourceUsage.Immutable && (data == null || data.Length == 0)) { throw new ArgumentException("data", "Immutable textures must be initialized with data."); } Texture2DDescription desc = new Texture2DDescription() { Width = width, Height = height, Format = EnumConverter.Convert(format), ArraySize = arraySize, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), CpuAccessFlags = CpuAccessFlags.None, MipLevels = !generateMipMaps ? 1 : 0, Usage = EnumConverter.Convert(usage) }; if (isRenderTarget) { if (format.IsDepth()) { desc.BindFlags |= BindFlags.DepthStencil; } else { desc.BindFlags |= BindFlags.RenderTarget; } } if (generateMipMaps) { desc.BindFlags |= BindFlags.RenderTarget; desc.OptionFlags |= ResourceOptionFlags.GenerateMipMaps; } desc.BindFlags |= BindFlags.ShaderResource; if (data != null && data.Length > 1) { SharpDX.DataRectangle[] rects = new SharpDX.DataRectangle[data.Length]; for (int i = 0; i < data.Length; i++) { rects[i] = new SharpDX.DataRectangle(data[i].Pointer, data[i].Pitch); } this.Texture = new SharpDX.Direct3D11.Texture2D(graphicsDevice.Device, desc, rects); } else { this.Texture = new SharpDX.Direct3D11.Texture2D(graphicsDevice.Device, desc); } CreateViews(); }