# SDL OpenGL ES2 autobatching patch

**URL:** <https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017>\
**Category:** SDL Development\
**Created:** [September 14, 2018, 3:28pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017 "2018-09-14T15:28:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmg.nik](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rmg.nik](https://discourse.libsdl.org/u/rmg.nik)\
**Post date:** [September 14, 2018, 3:28pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/1 "2018-09-14T15:28:30Z")

</div>

I do not know how to contribute, so I publish my patch here.  
I wrote a simple autobatching for the OpenGL ES2 backend.  
Perhaps this is not optimal, and someone can optimize it.  
But now I can use a particle system with many calls to SDL\_RenderCopyEx.

[sdl\_opengles2\_batch.patch](https://discourse.libsdl.org/uploads/default/original/2X/9/928ca32c416019dfb6f96ddbc0f3a602598b3551.patch) (43.9 KB)

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [September 17, 2018, 4:08pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/2 "2018-09-17T16:08:23Z")

</div>

It’s funny you should mention this, because I’m working on something similar:

> <https://twitter.com/icculus/status/1038523554839576576>

The demo there is in the GLES2 renderer, but I’m in the process of reworking this for the higher level so all the rendering backends can do batching.

---

<div class="post-metadata">

**Author:** ![rmg.nik](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rmg.nik](https://discourse.libsdl.org/u/rmg.nik)\
**Post date:** [September 17, 2018, 4:30pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/3 "2018-09-17T16:30:07Z")

</div>

😆

> **[rmg-nik/sdl\_batch](https://github.com/rmg-nik/sdl_batch)**
>
> Modification SDL2 for batch drawing. Contribute to rmg-nik/sdl\_batch development by creating an account on GitHub.

  
This is my old variant. New patch is compatible with SDL 2.0.8.  
I look forward to your changes!  
And it would be great to provide API for customizing shaders and working with vertices. For example, my dream, Spine on the SDL.

If you want, you can use my code without any references or restrictions. In my implementation, you can determine the maximum number of vertices at compile time.

---

<div class="post-metadata">

**Author:** ![rtrussell](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/rtrussell/32/4768_2.png) [@rtrussell](https://discourse.libsdl.org/u/rtrussell)\
**Post date:** [September 21, 2018, 3:11pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/4 "2018-09-21T15:11:08Z")

</div>

> [@icculus](#):
>
> I’m in the process of reworking this for the higher level so all the rendering backends can do batching.

This is exciting! Will the batching work when rendering to a target texture as well as when using the default render target?

---

<div class="post-metadata">

**Author:** ![rmg.nik](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rmg.nik](https://discourse.libsdl.org/u/rmg.nik)\
**Post date:** [October 25, 2018, 11:31am UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/5 "2018-10-25T11:31:15Z")

</div>

I’ve found an error in my code.  
When I flip by vertical I miss `vertex_index_start`, so correct code is  
`tmp = vertices[vertex_index_start + 0].pos[1];`

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [October 29, 2018, 6:49pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/6 "2018-10-29T18:49:15Z")

</div>

Mine is sitting in a branch that will merge after 2.0.9 is complete:

[https://hg.libsdl.org/SDL/shortlog/SDL-ryan-batching-renderer](https://hg.libsdl.org/SDL/shortlog/SDL-ryan-batching-renderer)

---

<div class="post-metadata">

**Author:** ![rmg.nik](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rmg.nik](https://discourse.libsdl.org/u/rmg.nik)\
**Post date:** [October 30, 2018, 4:43am UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/7 "2018-10-30T04:43:02Z")

</div>

Maybe it is better to move a color from the uniform to the vertex attribute?  
In my games I use a particle system, and there are many particles with different colors, therefore, to avoid problems with batching, I keep the color at the vertex attribute.

---

<div class="post-metadata">

**Author:** ![AntTheAlchemist](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/antthealchemist/32/3894_2.png) [@AntTheAlchemist](https://discourse.libsdl.org/u/AntTheAlchemist)\
**Post date:** [November 3, 2018, 11:58pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/8 "2018-11-03T23:58:40Z")

</div>

I noticed a bunch of float methods appeared, for non pixel aligned coordinates I presume? This is a major thing, why are bards not singing and dancing about this?

Also, I am being a bit thick. Is the only way to use your branch to compile from source? Do you have a DLL? I’m not seeing the float methods in the includes in the 2.0.9 release.

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [November 5, 2018, 12:30am UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/9 "2018-11-05T00:30:10Z")

</div>

> [@AntTheAlchemist](#):
>
> This is a major thing, why are bards not singing and dancing about this?

lol! Please test it if you get a chance; lots of people have asked for this, but I need test cases!

> [@AntTheAlchemist](#):
>
> Is the only way to use your branch to compile from source? Do you have a DLL? I’m not seeing the float methods in the includes in the 2.0.9 release.

It’s not in 2.0.9 (but it has now been merged, so it’s not on a branch any more). If you build the latest from revision control from source, you’ll have it.

–ryan.

---

<div class="post-metadata">

**Author:** ![rmg.nik](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rmg.nik](https://discourse.libsdl.org/u/rmg.nik)\
**Post date:** [November 5, 2018, 10:45am UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/10 "2018-11-05T10:45:56Z")

</div>

How about move color to the vertex attribute for opengles2?

---

<div class="post-metadata">

**Author:** ![AntTheAlchemist](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/antthealchemist/32/3894_2.png) [@AntTheAlchemist](https://discourse.libsdl.org/u/AntTheAlchemist)\
**Post date:** [November 5, 2018, 1:17pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/11 "2018-11-05T13:17:23Z")

</div>

> [@icculus](#):
>
> It’s not in 2.0.9 (but it has now been merged, so it’s not on a branch any more). If you build the latest from revision control from source, you’ll have it.

I’m all over this - it’s very exciting! (maybe not the best choice of phrase).

Just built with the VisualC project (ah yes, smaller DLL as you said before - Steam must have compiled with VS).

Initial observation: I’m drawing 10000 coloured filled boxes (SDL\_SetRenderDrawColor & SDL\_RenderFillRect). The new build is twice as slow as the official build’s DLL? I notice SDL\_RenderDrawRect calls SDL\_RenderDrawRectF, but it shouldn’t make it twice as slow? And resizing a window causes all drawing to stop working. Perhaps I’m building it wrong or it’s broken in a big way? I’m happy to start submitting bugs, but just making sure this is SDL’s side and not a VisualC & me specific thing?

Edit: the halting of any rendering after window resize only happens with the Direct3D renderer. A bug introduced after 2.0.9 release.

---

<div class="post-metadata">

**Author:** ![AntTheAlchemist](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/antthealchemist/32/3894_2.png) [@AntTheAlchemist](https://discourse.libsdl.org/u/AntTheAlchemist)\
**Post date:** [November 6, 2018, 9:35am UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/12 "2018-11-06T09:35:20Z")

</div>

Okay, quite concerned at the results on Windows. Just doing simple filled rects in openGL renderer in my test and without batching I’m getting 1000fps, with batching I’m getting 200fps - ouch. Even direct3d renderer gives me 800fps without, and 500fps with.

Am I missing something? Can someone supply a new DLL built with cmake for me? (so I know it’s not VisualC’s build that’s choking this). Anyone else joining me on the this batching quest?

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [November 6, 2018, 10:22am UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/13 "2018-11-06T10:22:01Z")

</div>

Did you set the hint ?  
SDL\_SetHint(SDL\_HINT\_RENDER\_BATCHING, “1”);

---

<div class="post-metadata">

**Author:** ![AntTheAlchemist](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/antthealchemist/32/3894_2.png) [@AntTheAlchemist](https://discourse.libsdl.org/u/AntTheAlchemist)\
**Post date:** [November 6, 2018, 10:36am UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/14 "2018-11-06T10:36:50Z")

</div>

> [@Sylvain\_Becker](#):
>
> Did you set the hint ?  
> SDL\_SetHint(SDL\_HINT\_RENDER\_BATCHING, “1”);

No, I didn’t know about this, sorry. That works, thank-you!

But it’s no faster than 2.0.9 release, so was it batching in the background anyway before? With this hint it’s the same speed it was before. post 2.0.9 it’s much slower (especially opengles2) unless you turn this hint on. I can’t see an improvement.

[edit] Unless this is specific to mobile?

---

<div class="post-metadata">

**Author:** ![AntTheAlchemist](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/antthealchemist/32/3894_2.png) [@AntTheAlchemist](https://discourse.libsdl.org/u/AntTheAlchemist)\
**Post date:** [November 6, 2018, 10:45am UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/15 "2018-11-06T10:45:37Z")

</div>

Okay wait, just re-tested everything. I’m getting 1600fps on 2.0.9, and 2200fps with Ryan’s new batching, so it does appear to work well (opengles2). On opengl I’m getting 2700 before, and 2950 now.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [November 6, 2018, 10:52am UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/16 "2018-11-06T10:52:09Z")

</div>

Ok Great !

So, I think, if you don’t activate the hint, it works as a compatibility mode not to break existing games. It draws immediately when requested.  
When you activate it, every commands get grouped and it draws in one shot.

see: [https://hg.libsdl.org/SDL/diff/dc9108cd4340/include/SDL\_hints.h](https://hg.libsdl.org/SDL/diff/dc9108cd4340/include/SDL_hints.h)

On mobile / Android, Dashboard “Android Vitals”, there is a metric named “Slow rendering” which will maybe better with this feature, let me know if you have the change to see that.

---

<div class="post-metadata">

**Author:** ![AntTheAlchemist](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/antthealchemist/32/3894_2.png) [@AntTheAlchemist](https://discourse.libsdl.org/u/AntTheAlchemist)\
**Post date:** [November 6, 2018, 12:06pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/17 "2018-11-06T12:06:12Z")

</div>

Is there anything special we need to do to activate between-pixel alignment? I’m using the float drawing methods but rects are still aligning to pixels.

Edit: Scratch that(!) I had my AA NVIDIA settings all turned off 🤦‍♂️. It works great now! It’s beautiful!

---

<div class="post-metadata">

**Author:** ![AntTheAlchemist](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/antthealchemist/32/3894_2.png) [@AntTheAlchemist](https://discourse.libsdl.org/u/AntTheAlchemist)\
**Post date:** [November 6, 2018, 4:05pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/19 "2018-11-06T16:05:14Z")

</div>

The only way I could get things to draw between pixels was to set NVIDIA’s antialiasing mode to override applications. Is this because SDL\_HINT\_RENDER\_SCALE\_QUALITY anisotropic setting is not implemented? Currently textures have either no filtering, or linear filtering, neither of which allow the texture boundaries to fall outside of pixel alignment.

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [November 12, 2018, 10:35pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/20 "2018-11-12T22:35:53Z")

</div>

> [@AntTheAlchemist](#):
>
> The only way I could get things to draw between pixels was to set NVIDIA’s antialiasing mode to override applications. Is this because SDL\_HINT\_RENDER\_SCALE\_QUALITY anisotropic setting is not implemented? Currently textures have either no filtering, or linear filtering, neither of which allow the texture boundaries to fall outside of pixel alignment.

The float stuff was thrown in quickly just to pass through without casting things to ints, but it’s not tested _at all_ yet, so it’s likely there are obvious bugs to sort out still.

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [November 12, 2018, 10:45pm UTC](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017/21 "2018-11-12T22:45:24Z")

</div>

> [@Sylvain\_Becker](#):
>
> So, I think, if you don’t activate the hint, it works as a compatibility mode not to break existing games.

That’s right, but you only need the hint if you request a specific renderer. If you call SDL\_CreateRenderer() with a -1 index (and haven’t forced it with the SDL\_RENDER\_DRIVER hint), it’ll do batching, under the assumption that you won’t call OpenGL/Direct3D/whatever directly, since you might not have gotten any specific GPU API anyhow. If you tell SDL you _need_ OpenGL or Direct3D or whatever, we have to assume you might call it at any time, and thus turn off batching. That being said, my testing with most renderer backends without batching is _still_ faster than 2.0.9 due to other optimizations, but YMMV for several reasons.

It’s good practice to set the hint to allow batching anyhow, though, since an end-user might force a renderer with an environment variable, even if your app doesn’t call any lower level APIs.

(It’s also worth noting that Metal _always_ does batching; because of the nature of its command queue, we can make guarantees that we can’t in the GL state machine. I expect a potential future Vulkan or Direct3D 12 renderer would be similar.)

In 2.1, we’ll probably remove this hint and make it so you _must_ call SDL\_RendererFlush() before touching a native API and always do batching otherwise.

–ryan.

[Next page](https://discourse.libsdl.org/t/sdl-opengl-es2-autobatching-patch/25017.md?page=2)
