Exemple #1
0
 public static UniformValueSampler Create(Sampler s, int textureSlot = 0)
 {
     return(new UniformValueSampler(s, textureSlot));
 }
Exemple #2
0
        public void DrawBatch(Comparison <SpriteBatchItem> sortFunction, Program shader, Sampler sampler)
        {
            if (_batchItems.Count == 0)
            {
                return; // no batch items to draw.
            }
            // sort the batch items.
            _batchItems.Sort(sortFunction);
            _batchItems.Sort(sortFunction);
            // for some reson the above sort makes no change
            //List<SpriteBatchItem> batches = _batchItems;
            //batches.Sort(sortFunction);

            // Determine how many iterations through the drawing code we need to make.
            int batchIndex = 0;
            int batchCount = _batchItems.Count;

            // Iterate through the batches, doing short.MaxValue sets of verticies at a time.
            while (batchCount > 0)
            {
                int       verticesIndex = 0;
                int       indicesIndex  = 0;
                Texture2D texture       = null;

                int numBatchesToProcess = batchCount;
                if (numBatchesToProcess > MaxBatchSize)
                {
                    numBatchesToProcess = MaxBatchSize;
                }
                EnsureArrayCapacity(numBatchesToProcess);

                // draw the batches.
                for (int i = 0; i < numBatchesToProcess; i++, batchIndex++)
                {
                    SpriteBatchItem item = _batchItems[batchIndex];
                    //SpriteBatchItem item = batches[batchIndex];
                    // if the texture changed, we need to draw the current vertecies and bind the new texture.
                    if (!ReferenceEquals(item.Texture, texture))
                    {
                        FlushVertexArray(indicesIndex, verticesIndex, shader);

                        texture       = item.Texture;
                        verticesIndex = 0;
                        indicesIndex  = 0;
                        shader.Bind();
                        //sampler.Texture = texture;
                        Sampler s = (Sampler)sampler.Clone();
                        s.Texture = texture;
                        _shaderUniforms["Texture"] = UniformValue.Create(s);
                        shader.BindUniforms(_shaderUniforms);
                        //Sampler s = (Sampler)sampler.Clone();
                        //s.Texture = texture;
                        //shader.BindUniforms(new Dictionary<string, IUniformValue>()
                        //{
                        //  {"Texture", UniformValue.Create(s)}
                        //});
                        shader.Unbind();
                        //_device.Textures[0] = texture;
                    }

                    Vector4  color = new Vector4(item.Color.R, item.Color.G, item.Color.B, item.Color.A);
                    ushort[] itemIndices;
                    VertexPositionColorTexture[] itemVertieces = item.Sprite.GetVertices(item.X, item.Y, item.Width, item.Height, item.Depth, item.Color, out itemIndices);

                    if (item.SpriteEffect != SpriteEffect.None)
                    {
                        for (int j = 0; j < itemVertieces.Length; j++)
                        {
                            Vector2 tmp = itemVertieces[j].TextureCoordinate;
                            if (item.SpriteEffect == SpriteEffect.FlipVertically)
                            {
                                tmp.Y = 1.0f - tmp.Y;
                            }
                            if (item.SpriteEffect == SpriteEffect.FlipHorizontally)
                            {
                                tmp.X = 1.0f - tmp.X;
                            }
                            itemVertieces[j].TextureCoordinate = tmp;
                        }
                    }

                    for (int j = 0; j < itemIndices.Length; j++)
                    {
                        itemIndices[j] = (ushort)(itemIndices[j] + verticesIndex);
                    }
                    Array.Copy(itemIndices, 0, _index, indicesIndex, itemIndices.Length);
                    indicesIndex += itemIndices.Length;
                    Array.Copy(itemVertieces, 0, _vertexArray, verticesIndex, itemVertieces.Length);
                    verticesIndex += itemVertieces.Length;

                    // release the texture and return the item to the queue.
                    item.Sprite = null;
                    _freeBatchItems.Enqueue(item);
                }

                // flush the remaining vertex array
                FlushVertexArray(indicesIndex, verticesIndex, shader);

                // update the batch count to continue the process with larger batches
                batchCount -= numBatchesToProcess;
            }
            _batchItems.Clear();
        }
 public UniformValueSampler(Sampler sampler,
                            int textureSlot)
 {
     _sampler = sampler;
     _slot    = textureSlot;
 }