Ejemplo n.º 1
0
        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();
        }