Exemple #1
0
        internal void PrepareSubset(int start, int count)
        {
            int end = start + count;

            var _batches = Batches.GetBuffer();

            for (int i = start; i < end; i++)
            {
                var batch = _batches[i];
                if (batch != null)
                {
                    batch.Prepare();
                }
            }
        }
Exemple #2
0
        public Buffer GetBuffer(bool writable)
        {
            if (writable)
            {
                EnsureList();
            }

            if (_HasList)
            {
                return new Buffer {
                           IsTemporary = false,
                           Data        = Items.GetBuffer(),
                           Count       = Items.Count
                }
            }
            ;
            else
            {
                var alloc = BufferPool <T> .Allocate(4);

                var buf = alloc.Data;
                buf[0] = Item1;
                buf[1] = Item2;
                buf[2] = Item3;
                buf[3] = Item4;
                return(new Buffer {
                    IsTemporary = true,
                    Data = buf,
                    BufferPoolAllocation = alloc,
                    Count = _Count
                });
            }
        }
        public bool RemoveRandomParticle(ParticleCollection sector)
        {
            // If no sector is provided to remove from, semirandomly pick a sector from the last update.
            if (sector == null)
            {
                if (_SectorsFromLastUpdate.Count == 0)
                {
                    return(false);
                }

                var index = RNG.Next(0, _SectorsFromLastUpdate.Count);
                sector = _SectorsFromLastUpdate.GetBuffer()[index];
            }

            if (sector.Count == 0)
            {
                return(false);
            }

            var particleIndex = RNG.Next(0, sector.Count);

            sector.RemoveAt(particleIndex);

            return(true);
        }
Exemple #4
0
        /// <summary>
        /// Notifies the scheduler that new items have been added to the queues.
        /// This ensures that any sleeping worker threads wake up, and that
        ///  new threads are created if necessary
        /// </summary>
        public void NotifyQueuesChanged(bool assumeBusy = false)
        {
            ConsiderNewThread(assumeBusy);

            // Skip locking 'Threads' here since it's expensive.
            // This means we might fail to wake a brand new thread,
            //  but that's fine.
            var threads = Threads.GetBuffer();

            for (int i = 0; i < CurrentThreadCount; i++)
            {
                var thread = threads[i];

                if (thread != null)
                {
                    thread.WakeEvent.Set();
                }
            }
        }
        /// <summary>
        /// Scans over a list of batches and applies batch combiners to reduce the total number of batches sent to the GPU and
        ///  improve batch preparation efficiency. Batches eliminated by combination are replaced with null.
        /// </summary>
        /// <param name="batches">The list of batches to perform a combination pass over.</param>
        /// <returns>The number of batches eliminated.</returns>
        public static int CombineBatches(UnorderedList <Batch> batches)
        {
#if PSM
            batches.Timsort(BatchTypeSorter);
#else
            batches.Sort(BatchTypeSorter);
#endif

            int i = 0, j = i + 1, l = batches.Count, eliminatedCount = 0;

            Batch a, b;
            Type  aType, bType;

            var _batches = batches.GetBuffer();

            while ((i < l) && (j < l))
            {
                a = _batches[i];

                if (a == null)
                {
                    i += 1;
                    j  = i + 1;
                    continue;
                }
                aType = a.GetType();

                b = _batches[j];

                if (b == null)
                {
                    j += 1;
                    continue;
                }
                bType = b.GetType();

                if ((aType != bType) || (a.Layer != b.Layer))
                {
                    i = j;
                    j = i + 1;
                }
                else
                {
                    bool combined = false;

                    foreach (var combiner in Combiners)
                    {
                        if (combined = combiner.CanCombine(a, b))
                        {
                            _batches[i]           = _batches[j] = null;
                            _batches[i]           = combiner.Combine(a, b);
                            _batches[i].Container = a.Container;

                            if ((a != _batches[i]) && (a.ReleaseAfterDraw))
                            {
                                a.ReleaseResources();
                            }
                            if (b.ReleaseAfterDraw)
                            {
                                b.ReleaseResources();
                            }

                            eliminatedCount += 1;
                            break;
                        }
                    }

                    j += 1;
                }
            }

            if (false && eliminatedCount > 0)
            {
                Console.WriteLine("Eliminated {0:0000} of {1:0000} batch(es)", eliminatedCount, batches.Count);
            }

            return(eliminatedCount);
        }