Exemplo n.º 1
0
        /// <summary></summary>
        /// <param name="state"></param>
        protected sealed override void DrawElement(DrawState state)
        {
            if (state.SupportsHardwareInstancing && instanceCount > HardwareInstancingMinimum)
            {
                Matrix identity = Matrix.Identity;
                state.PushWorldMatrix(ref identity);

                state.DrawBatch(vertices, indices, PrimitiveType.TriangleList, null, instances, instanceCount);

                state.PopWorldMatrix();
            }
            else
            {
                Graphics2D.NonInstancingSprite shader = state.GetShader <Graphics2D.NonInstancingSprite>();

                for (int i = 0; i < instanceCount; i += NonInstancingRenderCount)
                {
                    int count = Math.Min(NonInstancingRenderCount, (instanceCount - i));

                    shader.Instances.SetArray(this.instances, i);
                    this.verticesSI.Draw(state, this.indicesSI, PrimitiveType.TriangleList, 2 * count, 0, 0);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary></summary>
        /// <param name="state"></param>
        /// <param name="maskOnly"></param>
        protected override sealed void BindShader(DrawState state, bool maskOnly)
        {
            if (this.vertices == null)
            {
                this.vertices = state.UserValues[GetType().FullName + ".vertices"] as IVertices;
                this.indices  = state.UserValues[GetType().FullName + ".indices"] as Indices <ushort>;

                this.verticesSI = state.UserValues[GetType().FullName + ".verticesSI"] as IVertices;
                this.indicesSI  = state.UserValues[GetType().FullName + ".indicesSI"] as Indices <ushort>;

                if (this.vertices == null)
                {
                    //still null, create the global vertices
                    this.vertices = new Vertices <Vector4>(
                        new Vector4(0, 0, 0, 1),
                        new Vector4(1, 0, 0, 1),
                        new Vector4(1, 1, 0, 1),
                        new Vector4(0, 1, 0, 1));

                    this.indices = new Indices <ushort>(0, 2, 1, 0, 3, 2);

                    //shader instancing..
                    List <InstanceVertex> verts = new List <InstanceVertex>();
                    List <ushort>         inds  = new List <ushort>();

                    for (int i = 0; i < NonInstancingRenderCount; i++)
                    {
                        verts.Add(new InstanceVertex(new Vector3(0, 0, 0), (float)i));
                        verts.Add(new InstanceVertex(new Vector3(1, 0, 0), (float)i));
                        verts.Add(new InstanceVertex(new Vector3(1, 1, 0), (float)i));
                        verts.Add(new InstanceVertex(new Vector3(0, 1, 0), (float)i));

                        inds.Add((ushort)(0 + i * 4));
                        inds.Add((ushort)(2 + i * 4));
                        inds.Add((ushort)(1 + i * 4));
                        inds.Add((ushort)(0 + i * 4));
                        inds.Add((ushort)(3 + i * 4));
                        inds.Add((ushort)(2 + i * 4));
                    }

                    this.verticesSI = new Vertices <InstanceVertex>(verts.ToArray());
                    this.indicesSI  = new Indices <ushort>(inds.ToArray());

                    state.UserValues[GetType().FullName + ".vertices"]   = vertices;
                    state.UserValues[GetType().FullName + ".indices"]    = indices;
                    state.UserValues[GetType().FullName + ".verticesSI"] = verticesSI;
                    state.UserValues[GetType().FullName + ".indicesSI"]  = indicesSI;
                }
            }

            if (state.SupportsHardwareInstancing && instanceCount > HardwareInstancingMinimum)
            {
                Graphics2D.InstancingSprite shader = state.GetShader <Graphics2D.InstancingSprite>();
                Matrix world;
                state.GetWorldMatrix(out world);
                shader.SetSpriteWorldMatrix(ref world);
                shader.CustomTexture = texture ?? Xen.Ex.Material.WhiteTexture.GetTexture(state);
                shader.Bind(state);
            }
            else
            {
                Graphics2D.NonInstancingSprite shader = state.GetShader <Graphics2D.NonInstancingSprite>();
                shader.CustomTexture = texture ?? Xen.Ex.Material.WhiteTexture.GetTexture(state);
                shader.Bind(state);
            }
        }