Beispiel #1
0
        private void PlatformConstruct(
            GraphicsDevice graphicsDevice,
            uint sizeInBytes,
            uint elementSizeInBytes,
            BufferBindFlags flags,
            ResourceUsage usage,
            byte[] initialData)
        {
            var optionFlags = flags.HasFlag(BufferBindFlags.ShaderResource)
                ? D3D11.ResourceOptionFlags.BufferStructured
                : D3D11.ResourceOptionFlags.None;

            var cpuAccessFlags = usage == ResourceUsage.Dynamic
                ? D3D11.CpuAccessFlags.Write
                : D3D11.CpuAccessFlags.None;

            var d3d11Usage = usage == ResourceUsage.Dynamic
                ? D3D11.ResourceUsage.Dynamic
                : D3D11.ResourceUsage.Immutable;

            var description = new D3D11.BufferDescription
            {
                BindFlags           = flags.ToBindFlags(),
                CpuAccessFlags      = cpuAccessFlags,
                OptionFlags         = optionFlags,
                SizeInBytes         = (int)sizeInBytes,
                StructureByteStride = (int)elementSizeInBytes,
                Usage = d3d11Usage
            };

            if (usage == ResourceUsage.Static)
            {
                using (var dataStream = DataStream.Create(initialData, true, false))
                {
                    DeviceBuffer = AddDisposable(new D3D11.Buffer(
                                                     graphicsDevice.Device,
                                                     dataStream,
                                                     description));
                }
            }
            else
            {
                DeviceBuffer = AddDisposable(new D3D11.Buffer(
                                                 graphicsDevice.Device,
                                                 description));
            }
        }
Beispiel #2
0
        protected Buffer(
            GraphicsDevice graphicsDevice,
            uint elementSizeInBytes,
            uint elementCount,
            BufferBindFlags flags,
            ResourceUsage usage,
            byte[] initialData)
            : base(graphicsDevice)
        {
            if (usage == ResourceUsage.Dynamic && initialData != null)
            {
                throw new ArgumentOutOfRangeException();
            }

            if (usage == ResourceUsage.Static && initialData == null)
            {
                throw new ArgumentOutOfRangeException();
            }

            ElementSizeInBytes = elementSizeInBytes;
            ElementCount       = elementCount;

            var alignedElementSize = flags.HasFlag(BufferBindFlags.ConstantBuffer)
                ? PlatformGetAlignedSize(elementSizeInBytes)
                : elementSizeInBytes;

            SizeInBytes = alignedElementSize * elementCount;

            BindFlags = flags;
            Usage     = usage;

            PlatformConstruct(
                graphicsDevice,
                SizeInBytes,
                elementSizeInBytes,
                flags,
                usage,
                initialData);
        }