void resize(int capacity) { if (maximumCapacity > 0 && capacity > maximumCapacity) { throw new ArgumentException("Attempting to resize a dynamic buffer over it’s maximum capacity"); } BufferDesc desc = new BufferDesc(false); desc.Usage = Usage.Dynamic; desc.BindFlags = usage; desc.CPUAccessFlags = CpuAccessFlags.Write; desc.uiSizeInBytes = capacity; if (usage.HasFlag(BindFlags.ShaderResource)) { desc.Mode = BufferMode.Structured; desc.ElementByteStride = 16; } using (var device = context.renderContext.device) buffer = device.CreateBuffer(ref desc, null, IntPtr.Zero, 0); this.capacity = capacity; if (null != m_resizedEvent) { foreach (var sub in m_resizedEvent) { sub(this, capacity); } } }
/// <summary>Create buffer with initial content from the specified structure</summary> public static IBuffer CreateBuffer <T>(this IRenderDevice device, BufferDesc desc, ref T data, string name = null) where T : unmanaged { unsafe { fixed(T *pointer = &data) return(device.CreateBuffer(ref desc, name, (IntPtr)pointer, Marshal.SizeOf <T>())); } }
/// <summary>Create dynamic buffer for shader constants</summary> public static IBuffer CreateDynamicUniformBuffer(this IRenderDevice device, int cb, string name = null) { BufferDesc CBDesc = new BufferDesc(false); CBDesc.uiSizeInBytes = cb; CBDesc.Usage = Usage.Dynamic; CBDesc.BindFlags = BindFlags.UniformBuffer; CBDesc.CPUAccessFlags = CpuAccessFlags.Write; return(device.CreateBuffer(CBDesc, name)); }
/// <summary>Create buffer with initial content from a span</summary> public static IBuffer CreateBuffer <T>(this IRenderDevice device, BufferDesc desc, ReadOnlySpan <T> data, string name = null) where T : unmanaged { if (null == data || data.Length <= 0) { return(device.CreateBuffer(ref desc, name, IntPtr.Zero, 0)); } unsafe { int cb = Marshal.SizeOf <T>() * data.Length; fixed(T *pointer = data) return(device.CreateBuffer(ref desc, name, (IntPtr)pointer, cb)); } }
/// <summary>Create immutable buffer for shader constants.</summary> public static IBuffer CreateImmutableUniformBuffer <T>(this IRenderDevice device, ref T data, string name = null) where T : unmanaged { int cb = Marshal.SizeOf <T>(); if (0 != cb % 16) { throw new ArgumentException($"Constant buffer size must be a multiple of 16 bytes, yet sizeof( { typeof( T ).FullName } ) = { cb }"); } BufferDesc desc = new BufferDesc(false) { uiSizeInBytes = cb, BindFlags = BindFlags.UniformBuffer, Usage = Usage.Static, }; return(device.CreateBuffer(desc, ref data, name)); }
/// <summary>Create a buffer without initial data</summary> public static IBuffer CreateBuffer(this IRenderDevice device, BufferDesc desc, string name = null) { return(device.CreateBuffer(ref desc, name, IntPtr.Zero, 0)); }