Example #1
0
            protected override void WriteBlock(DrawState state, IndexType[] data, int startIndex, int start, int length, object target, object parent)
            {
                state.UnbindBuffer((IndexBuffer)target);

                if (start == 0 && length == ((IndexBuffer)target).SizeInBytes)
                {
                    min = default(IndexType);
                    max = default(IndexType);
                }

                processor(data, length, this);
#if !XBOX360
                if (target is DynamicIndexBuffer)
                {
#if DEBUG
                    state.Application.currentFrame.DynamicIndexBufferByesCopied += Stride * length;
#endif
                    SetDataOptions setOp = SetDataOptions.None;

                    if (sequentialWriteFlag)
                    {
                        if (start == 0)
                        {
                            setOp = SetDataOptions.Discard;
                        }
                        else
                        {
                            setOp = SetDataOptions.NoOverwrite;

#if DEBUG
                            if (start < previousCopyEnd)
                            {
                                throw new InvalidOperationException("ResourceUsage.DynamicSequential data overwrite detected");
                            }
#endif
                        }
#if DEBUG
                        previousCopyEnd = start + length * Stride;
#endif
                    }
                    else
                    if (start == 0 && length * Stride == ((DynamicIndexBuffer)target).SizeInBytes)
                    {
                        setOp = SetDataOptions.Discard;
                    }

                    ((DynamicIndexBuffer)target).SetData(start, data, startIndex, length, setOp);
                }
                else
#endif
                {
#if DEBUG
#if XBOX360
                    if (target is DynamicIndexBuffer)
                    {
                        state.Application.currentFrame.DynamicIndexBufferByesCopied += Stride * length;
                    }
                    else
#endif
                    state.Application.currentFrame.IndexBufferByesCopied += Stride * length;
#endif
#if XBOX360
                    int instancingCount = Math.Max(this.instancingCount, 1);

                    //instancing requires data is duplicated over the index buffer multiple times, with an offset
                    //added to each duplication
                    if (instancingCount >= 1 && !convertToInt)
                    {
                        ((IndexBuffer)target).SetData(start, data, startIndex, length);
                    }

                    if (instancingCount > 1)
                    {
                        int max = this.i_max;
                        IndexBufferProcessor.Update(this, out i_min, out i_max);

                        if (max == 0)
                        {
                            if (start != 0 || length * Stride != ((IndexBuffer)target).SizeInBytes / instancingCount / (convertToInt ? 2 : 1))
                            {
                                throw new ArgumentException("XBOX Instancing error: Instancing requires the first index buffer write to copy the entire buffer");
                            }
                        }
                        else
                        {
                            if (this.i_max > max)
                            {
                                throw new ArgumentException("XBOX Instancing error: Max index in the index buffer has increased from the first write");
                            }
                        }

                        //copy in multiple versions of the same indices
                        if (convertToInt)
                        {
                            uint[] buffer = null;
                            try
                            {
                                uint increment = (uint)(this.i_max + 1);
                                buffer = IndexBufferProcessor.BeginLock <uint>(length);

                                //foreach instance copy, make a copy of the data.
                                for (int i = 0; i < instancingCount; i++)
                                {
                                    IndexBufferProcessor.CopyIncrementToInt(data, buffer, length, (uint)(increment * i));
                                    ((IndexBuffer)target).SetData(start + length * 4 * i, buffer, startIndex, length);
                                }
                            }
                            finally
                            {
                                IndexBufferProcessor.EndLock <uint>(buffer);
                            }
                        }
                        else
                        {
                            IndexType[] buffer = null;
                            try
                            {
                                uint increment = (uint)(this.i_max + 1);
                                buffer = IndexBufferProcessor.BeginLock <IndexType>(length);

                                //foreach instance copy, make a copy of the data.
                                for (int i = 1; i < instancingCount; i++)
                                {
                                    IndexBufferProcessor.CopyIncrement(data, buffer, length, (uint)(increment * i));
                                    ((IndexBuffer)target).SetData(start + length * Stride * i, buffer, startIndex, length);
                                }
                            }
                            finally
                            {
                                IndexBufferProcessor.EndLock <IndexType>(buffer);
                            }
                        }

                        IndexType[] da = new IndexType[((IndexBuffer)target).SizeInBytes / Stride];
                        ((IndexBuffer)target).GetData(da);

                        da = null;
                    }
#else
                    ((IndexBuffer)target).SetData(start, data, startIndex, length);
#endif
                }
            }
Example #2
0
 protected override void WriteComplete()
 {
     IndexBufferProcessor.Update(this, out i_min, out i_max);
     complete = true;
 }
Example #3
0
 public Implementation(IEnumerable <IndexType> vertices) : base(vertices)
 {
     IndexBufferProcessor.Init(this);
 }