Beispiel #1
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
                };
            }
        }
Beispiel #2
0
        /// <summary>
        /// Function to build the description structure used to create the buffer.
        /// </summary>
        /// <param name="bufferInfo">The parameters used to create the buffer.</param>
        /// <returns>A new D3D 11 buffer description used to create the buffer.</returns>
        private D3D11.BufferDescription BuildBufferDesc(GorgonBufferInfo bufferInfo)
        {
            D3D11.BindFlags bindFlags     = GetBindingFlags(bufferInfo.Binding);
            var             resourceUsage = (D3D11.ResourceUsage)bufferInfo.Usage;

            // Round up to the nearest multiple of 4.
            bufferInfo.StructureSize = (bufferInfo.StructureSize + 3) & ~3;

            if (bufferInfo.StructureSize > 2048)
            {
                throw new GorgonException(GorgonResult.CannotCreate, string.Format(Resources.GORGFX_ERR_BUFFER_STRUCTURE_SIZE_INVALID, bufferInfo.StructureSize));
            }

            if (bufferInfo.AllowRawView)
            {
                // Raw buffers use 4 byte alignment.
                bufferInfo.SizeInBytes = (bufferInfo.SizeInBytes + 3) & ~3;
            }

            ValidateBufferBindings(bufferInfo.Usage, bufferInfo.Binding, bufferInfo.StructureSize);

            D3D11.ResourceOptionFlags options = D3D11.ResourceOptionFlags.None;

            if (bufferInfo.IndirectArgs)
            {
                options |= D3D11.ResourceOptionFlags.DrawIndirectArguments;
            }

            if (bufferInfo.AllowRawView)
            {
                options |= D3D11.ResourceOptionFlags.BufferAllowRawViews;
            }

            if (bufferInfo.StructureSize > 0)
            {
                options |= D3D11.ResourceOptionFlags.BufferStructured;
            }

            return(new D3D11.BufferDescription
            {
                SizeInBytes = bufferInfo.SizeInBytes,
                Usage = resourceUsage,
                BindFlags = bindFlags,
                OptionFlags = options,
                CpuAccessFlags = GetCpuFlags(bufferInfo.Usage, bufferInfo.Binding, bufferInfo.IndirectArgs),
                StructureByteStride = bufferInfo.StructureSize
            });
        }
        /// <summary>
        /// Retrieves a list of flags matching the specified DirectX texture binding flags.
        /// </summary>
        /// <param name="bindFlags">DirectX texture binding flags to translate into Oculus SDK texture binding flags.</param>
        /// <returns>Oculus SDK texture binding flags matching the specified bindFlags.</returns>
        public static TextureBindFlags GetTextureBindFlags(SharpDX.Direct3D11.BindFlags bindFlags)
        {
            TextureBindFlags result = TextureBindFlags.None;

            if (bindFlags.HasFlag(SharpDX.Direct3D11.BindFlags.DepthStencil))
            {
                result |= TextureBindFlags.DX_DepthStencil;
            }

            if (bindFlags.HasFlag(SharpDX.Direct3D11.BindFlags.RenderTarget))
            {
                result |= TextureBindFlags.DX_RenderTarget;
            }

            if (bindFlags.HasFlag(SharpDX.Direct3D11.BindFlags.UnorderedAccess))
            {
                result |= TextureBindFlags.DX_DepthStencil;
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Function to retrieve the binding flags for the buffer.
        /// </summary>
        /// <param name="binding">The buffer binding type.</param>
        /// <returns>A set of Direct3D 11 binding flags.</returns>
        private static D3D11.BindFlags GetBindingFlags(BufferBinding binding)
        {
            D3D11.BindFlags bindFlags = D3D11.BindFlags.None;

            if ((binding & BufferBinding.Shader) == BufferBinding.Shader)
            {
                bindFlags |= D3D11.BindFlags.ShaderResource;
            }

            if ((binding & BufferBinding.ReadWrite) == BufferBinding.ReadWrite)
            {
                bindFlags |= D3D11.BindFlags.UnorderedAccess;
            }

            if ((binding & BufferBinding.StreamOut) == BufferBinding.StreamOut)
            {
                bindFlags |= D3D11.BindFlags.StreamOutput;
            }

            return(bindFlags);
        }
Beispiel #5
0
        public static Direct3D11.Texture2D CreateTexture2D(this Direct3D11.Device device,
                                                           int w, int h,
                                                           Direct3D11.BindFlags flags = Direct3D11.BindFlags.RenderTarget | Direct3D11.BindFlags.ShaderResource,
                                                           Format format = Format.B8G8R8A8_UNorm,
                                                           Direct3D11.ResourceOptionFlags options = Direct3D11.ResourceOptionFlags.Shared)
        {
            var colordesc = new Direct3D11.Texture2DDescription
            {
                BindFlags         = flags,
                Format            = format,
                Width             = w,
                Height            = h,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Direct3D11.ResourceUsage.Default,
                OptionFlags       = options,
                CpuAccessFlags    = Direct3D11.CpuAccessFlags.None,
                ArraySize         = 1
            };

            return(new Direct3D11.Texture2D(device, colordesc));
        }
Beispiel #6
0
        public static D3D.Buffer CreateDynamicBuffer(this D3D.Device device, int sizeInBytes, D3D.BindFlags bindFlags)
        {
            var desc = new D3D.BufferDescription((int)System.Math.Ceiling(sizeInBytes / 16f) * 16,
                                                 D3D.ResourceUsage.Dynamic,
                                                 bindFlags,
                                                 D3D.CpuAccessFlags.Write,
                                                 D3D.ResourceOptionFlags.None,
                                                 0);

            return(new D3D.Buffer(device, desc));
        }