Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Texture"/> class.
 /// </summary>
 /// <param name="usage">The usage.</param>
 /// <param name="texUsage">The texture usage.</param>
 public TypelessTexture(Usage usage, TextureUsage texUsage, CPUAccess cpuAccess, GraphicsLocality locality)
 {
     this.usage     = usage;
     textureUsage   = texUsage;
     this.locality  = locality;
     this.cpuAccess = cpuAccess;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TypelessTexture2D"/> class.
        /// </summary>
        /// <param name="usage">The usage.</param>
        /// <param name="textureUsage">The texture usage.</param>
        /// <param name="fmt">The FMT.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="dp">The dp.</param>
        public TypelessTexture2D(GraphicsDevice device, Usage usage, TextureUsage textureUsage,
                                 CPUAccess cpuAccess, PixelFormat fmt, uint width, uint height, uint mipmaps,
                                 uint sampleCount, uint sampleQuality, GraphicsLocality locality, byte[][] data)
            : base(usage, textureUsage, cpuAccess, locality)
        {
            Validate(mipmaps, width, height);
            this.width       = width;
            this.height      = height;
            this.format      = fmt;
            this.mipmapCount = mipmaps == 0 ? Images.MipmapHelper.MipmapCount(width, height) : mipmaps;

            // We create driver part.
            if (locality != GraphicsLocality.SystemMemoryOnly)
            {
                this.device     = device;
                this.driverPart = device.DriverDevice.CreateTexture2D(usage, fmt.CommonFormatLayout,
                                                                      CPUAccess, width, height, mipmaps, textureUsage, sampleCount, sampleQuality, data);
            }

            // We may need to create sw part.
            if (locality == GraphicsLocality.DeviceAndSystemMemory ||
                locality == GraphicsLocality.SystemMemoryOnly)
            {
                this.swData = data;
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a graphics buffer directly on hardware, with set usage,
 /// format (e.g. is typed) and element count.
 /// </summary>
 /// <param name="device"></param>
 /// <param name="format"></param>
 /// <param name="elementCount"></param>
 public TypelessBuffer([NotNull] GraphicsDevice device, Usage usage, BufferUsage bufferUsage,
                       CPUAccess access, GraphicsLocality locality, byte[] data)
     : this(usage, bufferUsage, access, locality, 0)
 {
     this.swData = data.Clone() as byte[];
     BindToDevice(device);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphicsBuffer"/> class.
 /// </summary>
 /// <param name="usage">The usage.</param>
 /// <param name="bufferUsage">The buffer usage.</param>
 /// <param name="access">The access.</param>
 /// <param name="locality">The locality.</param>
 /// <param name="format">The format.</param>
 /// <param name="elementCount">The element count.</param>
 public TypelessBuffer(Usage usage, BufferUsage bufferUsage, CPUAccess access,
                       GraphicsLocality locality, ulong byteSize)
 {
     this.usage       = usage;
     this.bufferUsage = bufferUsage;
     this.cpuAccess   = access;
     this.locality    = locality;
     this.byteSize    = byteSize;
     this.swData      = new byte[byteSize];
 }
Ejemplo n.º 5
0
        /// <summary>
        /// A helper to create a vertex buffer.
        /// </summary>
        /// <returns></returns>
        public static VertexBufferView Create(GraphicsDevice device, VertexFormat format, Usage usage,
                                              CPUAccess access, GraphicsLocality locality, ulong elementCount)
        {
            TypelessBuffer buffer = new TypelessBuffer(usage, BufferUsage.VertexBuffer, access, locality,
                                                       format.ByteSize * elementCount);

            buffer.DisposeOnViewDispose = true;

            return(buffer.CreateVertexBuffer(format));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Texture2D"/> class.
        /// </summary>
        /// <param name="usage">The usage.</param>
        /// <param name="textureUsage">The texture usage.</param>
        /// <param name="fmt">The FMT.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="mipmaps">The number of mipmaps.</param>
        /// <param name="locality">The locality.</param>
        /// <param name="data">The data.</param>
        public TypelessTexture2D(Usage usage, TextureUsage textureUsage, CPUAccess cpuAccess, PixelFormat fmt,
                                 uint width, uint height, uint mipmaps, GraphicsLocality locality, byte[][] data)
            : base(usage, textureUsage, cpuAccess, locality)
        {
            // Mipmaps first.
            Validate(mipmaps, width, height);
            this.mipmapCount = mipmaps == 0 ? Images.MipmapHelper.MipmapCount(width, height) : mipmaps;

            // We copy data if it exists, otherwise we dont do it.
            if (data != null)
            {
                if (data.Length != mipmapCount)
                {
                    throw new ArgumentException("Data is not provided for all mipmaps.");
                }

                swData = new byte[mipmapCount][];

                for (uint i = 0; i < mipmapCount; i++)
                {
                    if (data[i].Length != fmt.Size * width * height)
                    {
                        throw new ArgumentOutOfRangeException(
                                  "Byte size is not the same as texture size for mipmap " + i.ToString());
                    }

                    // We copy the data now.
                    swData[i] = data[i];
                }
            }
            else
            {
                swData = new byte[mipmapCount][];
                for (uint i = 0; i < mipmapCount; i++)
                {
                    uint w, h;
                    Images.MipmapHelper.ComputeDimensions(width, height, i, out w, out h);
                    swData[i] = new byte[w * h * fmt.Size];
                }
            }

            // And we copy all data.
            this.width  = width;
            this.height = height;
            this.format = fmt;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates and fills vertex buffer.
        /// </summary>
        /// <returns></returns>
        public static VertexBufferView Create <T>(GraphicsDevice device, VertexFormat format, Usage usage,
                                                  CPUAccess access, GraphicsLocality locality, params T[] elements) where T : struct
        {
            VertexBufferView buffer = Create(device, format, usage, access,
                                             locality, (ulong)elements.LongLength);

            try
            {
                buffer.SetData <T>(elements);
            }
            catch (Exception)
            {
                buffer.Dispose();
                throw;
            }

            return(buffer);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Texture"/> class.
 /// </summary>
 /// <param name="info">The info.</param>
 /// <param name="context">The context.</param>
 protected TypelessTexture(SerializationInfo info, StreamingContext context)
 {
     this.usage        = (Usage)info.GetValue("Usage", typeof(Usage));
     this.textureUsage = (TextureUsage)info.GetValue("TextureUsage", typeof(TextureUsage));
     this.locality     = (GraphicsLocality)info.GetValue("Locality", typeof(GraphicsLocality));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates a graphics buffer directly on hardware, with set usage,
 /// format (e.g. is typed) and element count.
 /// </summary>
 /// <param name="device"></param>
 /// <param name="format"></param>
 /// <param name="elementCount"></param>
 public TypelessBuffer([NotNull] GraphicsDevice device, Usage usage, BufferUsage bufferUsage,
                       CPUAccess access, GraphicsLocality locality, ulong byteSize)
     : this(usage, bufferUsage, access, locality, byteSize)
 {
     BindToDevice(device);
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Creates a constant buffer layout.
        /// </summary>
        /// <returns></returns>
        public static ConstantBufferView Create(GraphicsDevice device, Usage usage, CPUAccess access, GraphicsLocality locality,
                                                ConstantBufferLayout layout)
        {
            TypelessBuffer buffer = new TypelessBuffer(device, usage, BufferUsage.ConstantBuffer, access,
                                                       locality, layout.MinimumBufferSizeInBytes);

            buffer.DisposeOnViewDispose = true;

            return(buffer.CreateConstantBuffer(layout));
        }