0
\$\begingroup\$

I'm writing a 2D side-scrolling style game in C++ and openGL. In this game I have several cloud sprites which are rendered across the top of the screen. They rotate and scale and such so they're not static. I'm wondering if there is any way that I can optimize this, because currently every time one of these sprites is rendered, GL has to perform blending between everything already rendered (like the background, etc) and these sprites.

I'm thinking something along the lines of rendering ONLY the clouds together all at once, and then performing a single call where the final result is rendered and blended into the main scene. Not sure how to even go about that or if it will even result in a performance gain. Any suggestions would be greatly appreciated!

Just for clarity, here is what a final render looks like. The clouds are formed by several smaller sprites being populated close together around certain points.

enter image description here

\$\endgroup\$

2 Answers 2

-1
\$\begingroup\$

I dont know much about OpenGL, but in DirectX there is alot of speed to be gained by render to texture techniques

http://ogltotd.blogspot.com/2006/12/render-to-texture.html

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for this, this is along the lines of what I was thinking I just have yet to see if this will actually give a decent increase in performance on mobile. I'll get back to you. \$\endgroup\$
    – user6484
    Commented Feb 25, 2012 at 18:08
  • 1
    \$\begingroup\$ I can tell you that when I created my 3D drag and drop level editor in DirectX, with a custom shader pipeline using Alpha Blending and Antiscopic filtering, it almost tripled my frames, \$\endgroup\$ Commented Feb 25, 2012 at 18:16
  • \$\begingroup\$ Marked as correct since you actually pointed me in the right direction. For some reason when I read the other answer, I feel like I'm being lectured by my step father. There was indeed a huge performance bottleneck and although I didn't end up using this exact technique, you still pointed me down the road where I eventually ended up using batching and an alternative GL call to render these batched objects, which greatly increased performance. Not such a waste of time after all I guess, premature optimization that is. \$\endgroup\$
    – user6484
    Commented Mar 3, 2012 at 23:26
2
\$\begingroup\$

because currently every time one of these sprites is rendered, GL has to perform blending between everything already rendered (like the background, etc) and these sprites.

While rendering with blending isn't the fastest possible rendering mode, it isn't exactly slow either. It doesn't go to each "thing" it has rendered before and blend with it. It's just blending with the destination color in the framebuffer.

I'm thinking something along the lines of rendering ONLY the clouds together all at once, and then performing a single call where the final result is rendered and blended into the main scene.

This is a prime example of premature optimization being the root of all evil. You're "solving" an optimization problem, and your "solution" will not in fact solve the problem. It will create a new problem: overlapped blended objects won't work right. OpenGL's blending functions will only work when you're directly compositing with an opaque destination value. Composting with a transparent value, which you then layer on top of the destination, is not going to provide the correct results.

Don't optimize unless you know something is a performance bottleneck already. That knowledge can only be gained in one of two ways: having a lot of experience in a problem domain or having actual profiling data that shows where the bottleneck is. Just keep making your game; if it turns out to be a problem, you can find a way to solve it later. And you'll know that it is a problem rather than just guessing.

\$\endgroup\$
6
  • \$\begingroup\$ Thanks for the post. I do know for a fact that it is an issue, since I'm testing my game on 3 different mobile devices and 2 different desktops and this specific rendering call absolutely murders the framerate, unless I knock down the number of clouds to about 20 or so. They're not massive textures either. I understand that in the blending function it's performing ops with the destination colour. Does the "size" of the destination, that is both dimensions and the data it's already populated with, not have any effect on the blend performance? \$\endgroup\$
    – user6484
    Commented Feb 25, 2012 at 16:05
  • \$\begingroup\$ I should clarify - the call murders the framerate on mobile. They're not crappy devices either, dual core tablet and phone etc etc. \$\endgroup\$
    – user6484
    Commented Feb 25, 2012 at 16:06
  • 2
    \$\begingroup\$ @AscensionSystems: Blending is a per-sample/pixel operation. The performance of blending (minus other factors like texture access and bandwidth issues) only depends on the area being covered, not the data that it's blending with. \$\endgroup\$ Commented Feb 25, 2012 at 16:10
  • \$\begingroup\$ @AscensionSystems: "unless I knock down the number of clouds to about 20 or so." You were rendering more than 20? How many were you rendering? Are these 2D images of clouds or a particle system? \$\endgroup\$ Commented Feb 25, 2012 at 16:10
  • \$\begingroup\$ Yes I was rendering 50. I suppose in a way it is a particle system, I'm trying to render fairly realistic clouds. The clouds are formed by the merging of several smaller textured sprites. \$\endgroup\$
    – user6484
    Commented Feb 25, 2012 at 16:25

You must log in to answer this question.