Thursday, December 15, 2016

How exactly does XNA's SpriteBatch work?


To be more precise, if I needed to recreate this functionality from scratch in another API (e.g. in OpenGL) what would it need to be capable of doing?


I do have a general idea of some of the steps, such as how it prepares an orthographic projection matrix and creates a quad for each draw call.



I'm not too familiar, however, with the batching process itself. Are all quads stored in the same vertex buffer? Does it need an index buffer? How are different textures handled?


If possible I'd be grateful if you could guide me through the process from when SpriteBatch.Begin() is called until SpriteBatch.End(), at least when using the default Deferred mode.



Answer



I have sort of replicated the behaviour of SpriteBatch in deferred mode for a cross-platform engine I'm working on, so here are the steps I have reverse engineered so far:




  1. SpriteBatch constructor: creates a DynamicIndexBuffer, DynamicVertexBuffer and array of VertexPositionColorTexture of fixed size (in this case, the maximum batch size - 2048 for sprites and 8192 for vertices).



    • The index buffer is filled with the vertex indices of the quads that will be drawn (0-1-2, 0-2-3, 4-5-6, 4-6-7 and so on).

    • An internal array of SpriteInfo structs is created, too. This will store temporal sprite settings to be used when batching.





  2. SpriteBatch.Begin: internally stores the values of BlendState, SamplerState, etc. specified and checks if it has been called twice without a SpriteBatch.End in between.




  3. SpriteBatch.Draw: takes all the sprite info (texture, position, color) and copies it to a SpriteInfo. If the max batch size is reached, the entire batch is drawn to make room for new sprites.



    • SpriteBatch.DrawString just issues a Draw for each character of the string, taking into account kerning and spacing.





  4. SpriteBatch.End: does the following operations:



    • Sets the render states specified in Begin.

    • Creates the orthographic projection matrix.

    • Applies the SpriteBatch shader.

    • Binds the DynamicVertexBuffer and DynamicIndexBuffer.


    • Performs the following batching operation:



      startingOffset = 0;
      currentTexture, oldTexture = null;

      // Iterate through all sprites
      foreach SpriteInfo in SpriteBuffer
      {
      // Store sprite index and texture
      spriteIndex = SpriteBuffer.IndexOf(SpriteInfo);
      currentTexture = SpriteInfo.Texture;


      // Issue draw call if batch count > 0 and there is a texture change
      if (currentTexture != oldTexture)
      {
      if (spriteIndex > startingOffset)
      {
      RenderBatch(currentTexture, SpriteBuffer, startingOffset,
      spriteIndex - startingOffset);
      }
      startingOffset = spriteIndex;
      oldTexture = currentTexture;

      }
      }

      // Draw remaining batch and clear the sprite data
      RenderBatch(currentTexture, SpriteBuffer, startingOffset,
      SpriteBuffer.Count - startingOffset);
      SpriteBuffer.Clear();





  5. SpriteBatch.RenderBatch: executes the following operations for each of the SpriteInfo in the batch:



    • Takes the position of the sprite and calculates the final position of the four vertices according to the origin and size. Applies existing rotation.

    • Calculates the UV coordinates and applies specified SpriteEffects to them.

    • Copies the sprite color.

    • These values are then stored in the array of VertexPositionColorTexture elements previously created. When all sprites have been calculated, SetData is called on the DynamicVertexBuffer and a DrawIndexedPrimitives call is issued.

    • The vertex shader only performs a tranform operation, and the pixel shader applies the tinting on the color fetched from the texture.





No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...