Ejemplo n.º 1
0
 /// <summary>
 /// Allocates a new <see cref="Buffer{T}"/> instance of the specified type.
 /// </summary>
 /// <typeparam name="T">The type of items in the buffer.</typeparam>
 /// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param>
 /// <param name="type">The type of buffer to allocate.</param>
 /// <param name="length">The length of the buffer to create.</param>
 /// <param name="allocationMode">The allocation mode to use for the new resource.</param>
 /// <returns>A <see cref="Buffer{T}"/> instance of the requested size.</returns>
 public static Buffer <T> AllocateBuffer <T>(this GraphicsDevice device, Type type, int length, AllocationMode allocationMode = AllocationMode.Default)
     where T : unmanaged
 {
     return(type switch
     {
         _ when type == typeof(ConstantBuffer <>) => device.AllocateConstantBuffer <T>(length, allocationMode),
         _ when type == typeof(ReadOnlyBuffer <>) => device.AllocateReadOnlyBuffer <T>(length, allocationMode),
         _ when type == typeof(ReadWriteBuffer <>) => device.AllocateReadWriteBuffer <T>(length, allocationMode),
         _ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
     });
Ejemplo n.º 2
0
        private int runComputeSharpTest(int items, TimeSpan duration, GraphicsDevice device, bool copyBack)
        {
            Stopwatch sw      = new Stopwatch();
            var       b       = createBuffer(items);
            var       buffer1 = device.AllocateReadWriteBuffer(b.buffer1);
            var       buffer2 = device.AllocateReadOnlyBuffer(b.buffer2);
            int       count   = 0;

            Vector2[] tmp     = new Vector2[items];
            int       binsize = 1;
            int       bins    = items / binsize + ((items % binsize) == 0 ? 0 : 1);
            //warm up
            Action <ThreadIds> a = idx =>
            {
                int l   = binsize;
                int pos = binsize * idx.X;
                while (l > 0 && pos < items)
                {
                    buffer1[pos] = buffer1[pos] + buffer2[pos];
                    pos++;
                    l--;
                }
            };

            device.For(bins, a);


            sw.Start();
            while (sw.Elapsed < duration)
            {
                device.For(bins, a);
                if (copyBack)
                {
                    buffer1.GetData(tmp);
                }
                count++;
            }
            sw.Stop();
            return(count);
        }