public void AddVertices <T>(BatchInfo material, VertexMode vertexMode, params T[] vertices) where T : struct, IVertexData { this.AddVertices <T>(material, vertexMode, vertices, vertices.Length); }
public void AddVertices <T>(BatchInfo material, VertexMode vertexMode, T[] vertexBuffer, int vertexCount) where T : struct, IVertexData { if (vertexCount == 0) { return; } if (vertexBuffer == null || vertexBuffer.Length == 0) { return; } if (vertexCount > vertexBuffer.Length) { vertexCount = vertexBuffer.Length; } if (material == null) { material = Material.Checkerboard.Res.InfoDirect; } if (this.pickingIndex != 0) { ColorRgba clr = new ColorRgba((this.pickingIndex << 8) | 0xFF); for (int i = 0; i < vertexCount; ++i) { vertexBuffer[i].Color = clr; } material = new BatchInfo(material); material.Technique = DrawTechnique.Picking; if (material.Textures == null) { material.MainTexture = Texture.White; } } else if (material.Technique == null || !material.Technique.IsAvailable) { material = new BatchInfo(material); material.Technique = DrawTechnique.Solid; } else if (material.Technique.Res.NeedsPreprocess) { material = new BatchInfo(material); material.Technique.Res.PreprocessBatch <T>(this, material, ref vertexMode, ref vertexBuffer, ref vertexCount); if (vertexCount == 0) { return; } if (vertexBuffer == null || vertexBuffer.Length == 0) { return; } if (vertexCount > vertexBuffer.Length) { vertexCount = vertexBuffer.Length; } if (material.Technique == null || !material.Technique.IsAvailable) { material.Technique = DrawTechnique.Solid; } } // When rendering without depth writing, use z sorting everywhere - there's no real depth buffering! bool zSort = !this.DepthWrite || material.Technique.Res.NeedsZSort; List <IDrawBatch> buffer = zSort ? this.drawBufferZSort : this.drawBuffer; float zSortIndex = zSort ? DrawBatch <T> .CalcZSortIndex(vertexBuffer, vertexCount) : 0.0f; if (buffer.Count > 0 && buffer[buffer.Count - 1].CanAppendJIT <T>( zSort ? 1.0f / this.zSortAccuracy : 0.0f, zSortIndex, material, vertexMode)) { buffer[buffer.Count - 1].AppendJIT(vertexBuffer, vertexCount); } else { buffer.Add(new DrawBatch <T>(material, vertexMode, vertexBuffer, vertexCount, zSortIndex)); } ++this.numRawBatches; }
/// <summary> /// Adds a parameterized set of vertices to the drawing devices rendering schedule. /// </summary> /// <typeparam name="T">The type of vertex data to add.</typeparam> /// <param name="material">The <see cref="Duality.Drawing.BatchInfo"/> to use for rendering the vertices.</param> /// <param name="vertexMode">The vertices drawing mode.</param> /// <param name="vertexBuffer"> /// A vertex data buffer that stores the vertices to add. Ownership of the buffer /// remains at the callsite, while the <see cref="IDrawDevice"/> copies the required /// data into internal storage. /// </param> /// <param name="vertexCount">The number of vertices to add, from the beginning of the buffer.</param> public void AddVertices <T>(BatchInfo material, VertexMode vertexMode, T[] vertexBuffer, int vertexCount) where T : struct, IVertexData { if (vertexCount == 0) { return; } if (vertexBuffer == null || vertexBuffer.Length == 0) { return; } if (vertexCount > vertexBuffer.Length) { vertexCount = vertexBuffer.Length; } if (material == null) { material = Material.Checkerboard.Res.Info; } // Move the added vertices to an internal shared buffer VertexSlice <T> slice = this.drawVertices.Rent <T>(vertexCount); Array.Copy(vertexBuffer, 0, slice.Data, slice.Offset, slice.Length); // In picking mode, override incoming vertices material and vertex colors // to generate a lookup texture by which we can retrieve each pixels object. if (this.pickingIndex != 0) { ColorRgba clr = new ColorRgba((this.pickingIndex << 8) | 0xFF); for (int i = 0; i < vertexCount; ++i) { slice.Data[slice.Offset + i].Color = clr; } material = this.RentMaterial(material); material.Technique = DrawTechnique.Picking; material.MainColor = ColorRgba.White; } else if (material.Technique == null || !material.Technique.IsAvailable) { material = this.RentMaterial(material); material.Technique = DrawTechnique.Solid; } // Aggregate all info we have about our incoming vertices VertexDrawItem drawItem = new VertexDrawItem { Offset = (ushort)slice.Offset, Count = (ushort)slice.Length, BufferIndex = (byte)(this.drawVertices.GetBatchCount <T>() - 1), TypeIndex = (byte)VertexDeclaration.Get <T>().TypeIndex, Mode = vertexMode, Material = material }; // Determine whether we need depth sorting and calculate a reference depth bool sortByDepth = (this.projection == ProjectionMode.Screen) || material.Technique.Res.NeedsZSort; RawList <SortItem> sortBuffer = sortByDepth ? this.sortBufferBlended : this.sortBufferSolid; SortItem sortItem = new SortItem(); if (sortByDepth) { sortItem.SortDepth = this.CalcZSortIndex <T>(slice.Data, slice.Offset, slice.Length); } // Determine whether we can batch the new vertex item with the previous one int prevDrawIndex = -1; if (vertexMode.IsBatchableMode() && sortBuffer.Count > 0) { // Since we require a complete material match to append items, we can // assume that the previous items sortByDepth value is the same as ours. SortItem prevSortItem = sortBuffer.Data[sortBuffer.Count - 1]; // Compare the previously added item with the new one. If everything // except the vertices themselves is the same, we can append them directly. if (this.drawBuffer.Data[prevSortItem.DrawItemIndex].CanAppend(ref drawItem) && (!sortByDepth || sortItem.CanShareDepth(prevSortItem.SortDepth))) { prevDrawIndex = prevSortItem.DrawItemIndex; } } // Append the new item directly to the previous one, or add it as a new one if (prevDrawIndex != -1) { if (sortByDepth) { sortBuffer.Data[sortBuffer.Count - 1].MergeDepth( sortItem.SortDepth, this.drawBuffer.Data[prevDrawIndex].Count, drawItem.Count); } this.drawBuffer.Data[prevDrawIndex].Count += drawItem.Count; } else { sortItem.DrawItemIndex = this.drawBuffer.Count; sortBuffer.Add(sortItem); this.drawBuffer.Add(drawItem); } ++this.numRawBatches; }
public DrawBatch(VertexBuffer buffer, RawList <VertexDrawRange> ranges, VertexMode mode, BatchInfo material) { this.vertexBuffer = buffer; this.vertexRanges = ranges; this.vertexMode = mode; this.material = material; }
public DrawBatch(VertexDeclaration type, RawList <VertexDrawRange> ranges, VertexMode mode, BatchInfo material) { this.vertexType = type; this.vertexRanges = ranges; this.vertexMode = mode; this.material = material; }