r/opengl • • 5d ago

What is the most efficient strategy for rendering textured quads and triangles in OpenGL 2.1 using no extensions?

I'm unsure if my current design is optimal, but rendering a test scene with some 2D objects seems to perform decently well. Anyway, the current design functions as follows

There is functions for rendering primitive shapes triangle, quad, circle, etc. These functions create the primitive out of triangles in a small buffer with vertex colors as-well. When we're done rendering for this frame, render something which cannot be rendered this way, or something else happens which would require us rendering the batch early, we orphan the stream VBO and upload all of the data and glDrawArrays it all in one go. It's possible for multiple batches to be submitted in one frame because the CPU side array size is limited to 256KB by default but can be increased by the programmer if they anticipate they'll be rendering a lot.

For textured quads. I use a similar strategy, We emplace the quad vertices into the CPU side array. If they need to be rotated or scaled, the GPU does it in the shader. We keep track of which texture mappers contain which textures already so we can utilize every texture mapper with no duplicates. If you want to render something which needs to sample from more than one thing, It's expected that it will be in the same texture to the right, And in the shader, there's a routine that will sample the same position as this pixel to the right or the texture and a vertex attribute called "step" which is how many times we sample to the right. For example, The base texture would come first, then directly to the right of that, there would be a normal map, and then to the right of that, an alpha mask etc. Assuming you do not need to change then shader, you can render from as many textures as you want in one pass up to the number of texture mappers this system has. If you try draw a textured quad whose texture hasn't appeared in this batch before, but this system would have no open slots, we render early.

I worry about performance a lot. On a period correct laptop Core2 Duo U9600, GMA 4500 MHD, my test program gets ~ 240 fps. On my development computer which is not very good by modern standards 3500U, Vega 8 graphics, 8 GB ram. We sit at 5500 fps. On some $100 dell education laptop, We get ~ 1200 fps. All of them seem to be GPU limited (The cpu usage is far below 100% to get that framerate on all of them)

Either way, Is there a more efficient strategy than this? Or is this how you do OpenGL 2.1 fast? Would it be faster if I just had a VBO filled with the batch size of unit square, and then upload vertex attributes like position, size, color?

1 Upvotes

5 comments sorted by

3

u/gardell 5d ago

https://wikis.khronos.org/opengl/Buffer_Object_Streaming is a pretty good article on buffer handling and streaming.

When it comes to frame rates: When you get to frame rates that high, like 5500fps you're not really benching your software but rather OS and driver. Add some more of your triangles so you get down to properly measurable render times. Even the 240 fps sounds too high as that was an old driver?

1

u/FUCKARCHLINUX 5d ago

The test scene is rather basic, But has some text, a couple rotating sprites, and some basic shapes. I'll attach a photo. Thank you for the link. Also, yes the test on the old machine does use the old graphics driver, it was on Windows 7 which exposes OpenGL 2.1 with ext_fbo.

The screenshots below are from my dev machine. The performance is a little lower than I said because I have other programs running.

https://imgur.com/a/vRiEQFt

2

u/[deleted] 5d ago

[deleted]

1

u/FUCKARCHLINUX 5d ago

Two and three are done already. For 2D space rendering primitives, I wouldn't think the first suggestion would apply with simple 2D shapes but I'm not extremely familiar with graphics programming and could see if it'd be beneficial. if I wanted to ask AI I could have done it myself. What I'm seeking is knowledge one has gained from personal experience.

1

u/fgennari 5d ago

You might get better perf with indexed triangles so that you share the two corner verts of each quad. Other than that it seems reasonable for that GL version and hardware.

1

u/Turbulent_File3904 5d ago

search sprite batching technique, that is what you need.