Пример #1
0
        /// <summary>
        /// Function to retrieve the appropriate CPU flags for the buffer.
        /// </summary>
        /// <param name="usage">The intended usage for the buffer.</param>
        /// <param name="binding">The binding flags being used by the buffer.</param>
        /// <param name="useIndirectArgs"><b>true</b> if the buffer will contain indirect arguments, <b>false</b> if not.</param>
        /// <returns>The D3D11 CPU access flags.</returns>
        private D3D11.CpuAccessFlags GetCpuFlags(ResourceUsage usage, BufferBinding binding, bool useIndirectArgs)
        {
            D3D11.CpuAccessFlags result = D3D11.CpuAccessFlags.None;

            if (useIndirectArgs)
            {
                return(result);
            }

            switch (usage)
            {
            case ResourceUsage.Dynamic:
                result = D3D11.CpuAccessFlags.Write;
                break;

            case ResourceUsage.Staging:
                // Staging resources are implicitly readable.
                result = D3D11.CpuAccessFlags.Read | D3D11.CpuAccessFlags.Write;
                break;

            case ResourceUsage.Default:
                if ((binding != BufferBinding.Shader) &&
                    (binding != BufferBinding.ReadWrite) &&
                    (binding != (BufferBinding.Shader | BufferBinding.ReadWrite)))
                {
                    break;
                }

                result = _info.AllowCpuRead ? D3D11.CpuAccessFlags.Read : D3D11.CpuAccessFlags.None;
                break;
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Function to initialize the buffer data.
        /// </summary>
        /// <param name="initialData">The initial data used to populate the buffer.</param>
        private void Initialize <T>(GorgonNativeBuffer <T> initialData)
            where T : unmanaged
        {
            D3D11.CpuAccessFlags cpuFlags = GetCpuFlags(false, D3D11.BindFlags.IndexBuffer);

            Log.Print($"{Name} Index Buffer: Creating D3D11 buffer. Size: {SizeInBytes} bytes", LoggingLevel.Simple);

            GorgonVertexBuffer.ValidateBufferBindings(_info.Usage, 0);

            D3D11.BindFlags bindFlags = D3D11.BindFlags.IndexBuffer;

            if ((_info.Binding & VertexIndexBufferBinding.StreamOut) == VertexIndexBufferBinding.StreamOut)
            {
                bindFlags |= D3D11.BindFlags.StreamOutput;
            }

            if ((_info.Binding & VertexIndexBufferBinding.UnorderedAccess) == VertexIndexBufferBinding.UnorderedAccess)
            {
                bindFlags |= D3D11.BindFlags.UnorderedAccess;
            }

            var desc = new D3D11.BufferDescription
            {
                SizeInBytes         = SizeInBytes,
                Usage               = (D3D11.ResourceUsage)_info.Usage,
                BindFlags           = bindFlags,
                OptionFlags         = D3D11.ResourceOptionFlags.None,
                CpuAccessFlags      = cpuFlags,
                StructureByteStride = 0
            };

            if ((initialData != null) && (initialData.Length > 0))
            {
                unsafe
                {
                    D3DResource = Native = new D3D11.Buffer(Graphics.D3DDevice, new IntPtr((void *)initialData), desc)
                    {
                        DebugName = Name
                    };
                }
            }
            else
            {
                D3DResource = Native = new D3D11.Buffer(Graphics.D3DDevice, desc)
                {
                    DebugName = Name
                };
            }
        }
        /// <summary>
        /// Function to initialize the buffer data.
        /// </summary>
        /// <param name="initialData">The initial data used to populate the buffer.</param>
        private void Initialize(GorgonNativeBuffer <byte> initialData)
        {
            // If the buffer is not aligned to 16 bytes, then pad the size.
            _info.SizeInBytes = (_info.SizeInBytes + 15) & ~15;

            TotalConstantCount = _info.SizeInBytes / (sizeof(float) * 4);

            D3D11.CpuAccessFlags cpuFlags = GetCpuFlags(false, D3D11.BindFlags.ConstantBuffer);

            Log.Print($"{Name} Constant Buffer: Creating D3D11 buffer. Size: {_info.SizeInBytes} bytes", LoggingLevel.Simple);

            var desc = new D3D11.BufferDescription
            {
                SizeInBytes         = _info.SizeInBytes,
                Usage               = (D3D11.ResourceUsage)_info.Usage,
                BindFlags           = D3D11.BindFlags.ConstantBuffer,
                OptionFlags         = D3D11.ResourceOptionFlags.None,
                CpuAccessFlags      = cpuFlags,
                StructureByteStride = 0
            };

            if ((initialData != null) && (initialData.Length > 0))
            {
                unsafe
                {
                    D3DResource = Native = new D3D11.Buffer(Graphics.D3DDevice, new IntPtr((void *)initialData), desc)
                    {
                        DebugName = Name
                    };
                }
            }
            else
            {
                D3DResource = Native = new D3D11.Buffer(Graphics.D3DDevice, desc)
                {
                    DebugName = Name
                };
            }
        }