Graphics: Optimize Viewport effect rendering
Using the kitchen sink plane shader for viewport effects, even if only a small part of them are active, incurs great performance loss on mobile, so split the rendering into multiple optional passes which additionally use the blending hardware for faster mixing (lerping). Also, don't mirror the PingPong textures if the viewport effect covers the entire screen area anyway.
This commit is contained in:
		
							parent
							
								
									3c0a530eba
								
							
						
					
					
						commit
						373b90af00
					
				
					 9 changed files with 206 additions and 20 deletions
				
			
		| 
						 | 
				
			
			@ -60,6 +60,11 @@ struct Vec4
 | 
			
		|||
	{
 | 
			
		||||
		return (x == other.x && y == other.y && z == other.z && w == other.w);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bool xyzHasEffect() const
 | 
			
		||||
	{
 | 
			
		||||
		return (x != 0.0 || y != 0.0 || z != 0.0);
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct Vec2i
 | 
			
		||||
| 
						 | 
				
			
			@ -129,6 +134,14 @@ struct IntRect : SDL_Rect
 | 
			
		|||
		SDL_Rect r = { x, y, w, h };
 | 
			
		||||
		return r;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bool encloses(const IntRect &o) const
 | 
			
		||||
	{
 | 
			
		||||
		return (x   <= o.x &&
 | 
			
		||||
		        y   <= o.y &&
 | 
			
		||||
		        x+w >= o.x+o.w &&
 | 
			
		||||
		        y+h >= o.y+o.h);
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct StaticRect { float x, y, w, h; };
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										116
									
								
								src/graphics.cpp
									
										
									
									
									
								
							
							
						
						
									
										116
									
								
								src/graphics.cpp
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -171,36 +171,112 @@ public:
 | 
			
		|||
 | 
			
		||||
	void requestViewportRender(Vec4 &c, Vec4 &f, Vec4 &t)
 | 
			
		||||
	{
 | 
			
		||||
		pp.swapRender();
 | 
			
		||||
		const IntRect &viewpRect = glState.scissorBox.get();
 | 
			
		||||
		const IntRect &screenRect = geometry.rect;
 | 
			
		||||
 | 
			
		||||
		/* Scissor test _does_ affect FBO blit operations,
 | 
			
		||||
		 * and since we're inside the draw cycle, it will
 | 
			
		||||
		 * be turned on, so turn it off temporarily */
 | 
			
		||||
		glState.scissorTest.pushSet(false);
 | 
			
		||||
		if (t.w != 0.0)
 | 
			
		||||
		{
 | 
			
		||||
			pp.swapRender();
 | 
			
		||||
 | 
			
		||||
		GLMeta::blitBegin(pp.frontBuffer());
 | 
			
		||||
		GLMeta::blitSource(pp.backBuffer());
 | 
			
		||||
		GLMeta::blitRectangle(geometry.rect, Vec2i());
 | 
			
		||||
		GLMeta::blitEnd();
 | 
			
		||||
			if (!viewpRect.encloses(screenRect))
 | 
			
		||||
			{
 | 
			
		||||
				/* Scissor test _does_ affect FBO blit operations,
 | 
			
		||||
				 * and since we're inside the draw cycle, it will
 | 
			
		||||
				 * be turned on, so turn it off temporarily */
 | 
			
		||||
				glState.scissorTest.pushSet(false);
 | 
			
		||||
 | 
			
		||||
		glState.scissorTest.pop();
 | 
			
		||||
				GLMeta::blitBegin(pp.frontBuffer());
 | 
			
		||||
				GLMeta::blitSource(pp.backBuffer());
 | 
			
		||||
				GLMeta::blitRectangle(geometry.rect, Vec2i());
 | 
			
		||||
				GLMeta::blitEnd();
 | 
			
		||||
 | 
			
		||||
		PlaneShader &shader = shState->shaders().plane;
 | 
			
		||||
				glState.scissorTest.pop();
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			GrayShader &shader = shState->shaders().gray;
 | 
			
		||||
			shader.bind();
 | 
			
		||||
			shader.setGray(t.w);
 | 
			
		||||
			shader.applyViewportProj();
 | 
			
		||||
			shader.setTexSize(screenRect.size());
 | 
			
		||||
 | 
			
		||||
			TEX::bind(pp.backBuffer().tex);
 | 
			
		||||
 | 
			
		||||
			glState.blend.pushSet(false);
 | 
			
		||||
			screenQuad.draw();
 | 
			
		||||
			glState.blend.pop();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		bool toneEffect = t.xyzHasEffect();
 | 
			
		||||
		bool colorEffect = c.xyzHasEffect();
 | 
			
		||||
		bool flashEffect = f.xyzHasEffect();
 | 
			
		||||
 | 
			
		||||
		if (!toneEffect && !colorEffect && !flashEffect)
 | 
			
		||||
			return;
 | 
			
		||||
 | 
			
		||||
		FlatColorShader &shader = shState->shaders().flatColor;
 | 
			
		||||
		shader.bind();
 | 
			
		||||
		shader.setColor(c);
 | 
			
		||||
		shader.setFlash(f);
 | 
			
		||||
		shader.setTone(t);
 | 
			
		||||
		shader.setOpacity(1.0);
 | 
			
		||||
		shader.applyViewportProj();
 | 
			
		||||
		shader.setTexSize(geometry.rect.size());
 | 
			
		||||
 | 
			
		||||
		TEX::bind(pp.backBuffer().tex);
 | 
			
		||||
		/* Apply tone */
 | 
			
		||||
		if (toneEffect)
 | 
			
		||||
		{
 | 
			
		||||
			/* First split up additive / substractive components */
 | 
			
		||||
			Vec4 add, sub;
 | 
			
		||||
 | 
			
		||||
		glState.blend.pushSet(false);
 | 
			
		||||
			if (t.x > 0)
 | 
			
		||||
				add.x = t.x;
 | 
			
		||||
			if (t.y > 0)
 | 
			
		||||
				add.y = t.y;
 | 
			
		||||
			if (t.z > 0)
 | 
			
		||||
				add.z = t.z;
 | 
			
		||||
 | 
			
		||||
		screenQuad.draw();
 | 
			
		||||
			if (t.x < 0)
 | 
			
		||||
				sub.x = -t.x;
 | 
			
		||||
			if (t.y < 0)
 | 
			
		||||
				sub.y = -t.y;
 | 
			
		||||
			if (t.z < 0)
 | 
			
		||||
				sub.z = -t.z;
 | 
			
		||||
 | 
			
		||||
		glState.blend.pop();
 | 
			
		||||
			/* Then apply them using hardware blending */
 | 
			
		||||
			gl.BlendFuncSeparate(GL_ONE, GL_ONE, GL_ZERO, GL_ONE);
 | 
			
		||||
 | 
			
		||||
			if (add.xyzHasEffect())
 | 
			
		||||
			{
 | 
			
		||||
				gl.BlendEquation(GL_FUNC_ADD);
 | 
			
		||||
				shader.setColor(add);
 | 
			
		||||
 | 
			
		||||
				screenQuad.draw();
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if (sub.xyzHasEffect())
 | 
			
		||||
			{
 | 
			
		||||
				gl.BlendEquation(GL_FUNC_REVERSE_SUBTRACT);
 | 
			
		||||
				shader.setColor(sub);
 | 
			
		||||
 | 
			
		||||
				screenQuad.draw();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (colorEffect || flashEffect)
 | 
			
		||||
		{
 | 
			
		||||
			gl.BlendEquation(GL_FUNC_ADD);
 | 
			
		||||
			gl.BlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
 | 
			
		||||
			                     GL_ZERO, GL_ONE);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (colorEffect)
 | 
			
		||||
		{
 | 
			
		||||
			shader.setColor(c);
 | 
			
		||||
			screenQuad.draw();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (flashEffect)
 | 
			
		||||
		{
 | 
			
		||||
			shader.setColor(f);
 | 
			
		||||
			screenQuad.draw();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		glState.blendMode.refresh();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	void setBrightness(float norm)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -35,11 +35,14 @@
 | 
			
		|||
#include "transSimple.frag.xxd"
 | 
			
		||||
#include "bitmapBlit.frag.xxd"
 | 
			
		||||
#include "plane.frag.xxd"
 | 
			
		||||
#include "gray.frag.xxd"
 | 
			
		||||
#include "flatColor.frag.xxd"
 | 
			
		||||
#include "simple.frag.xxd"
 | 
			
		||||
#include "simpleColor.frag.xxd"
 | 
			
		||||
#include "simpleAlpha.frag.xxd"
 | 
			
		||||
#include "simpleAlphaUni.frag.xxd"
 | 
			
		||||
#include "flashMap.frag.xxd"
 | 
			
		||||
#include "minimal.vert.xxd"
 | 
			
		||||
#include "simple.vert.xxd"
 | 
			
		||||
#include "simpleColor.vert.xxd"
 | 
			
		||||
#include "sprite.vert.xxd"
 | 
			
		||||
| 
						 | 
				
			
			@ -269,6 +272,21 @@ void ShaderBase::setTranslation(const Vec2i &value)
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
FlatColorShader::FlatColorShader()
 | 
			
		||||
{
 | 
			
		||||
	INIT_SHADER(minimal, flatColor, FlatColorShader);
 | 
			
		||||
 | 
			
		||||
	ShaderBase::init();
 | 
			
		||||
 | 
			
		||||
	GET_U(color);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void FlatColorShader::setColor(const Vec4 &value)
 | 
			
		||||
{
 | 
			
		||||
	setVec4Uniform(u_color, value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
SimpleShader::SimpleShader()
 | 
			
		||||
{
 | 
			
		||||
	INIT_SHADER(simple, simple, SimpleShader);
 | 
			
		||||
| 
						 | 
				
			
			@ -480,6 +498,21 @@ void PlaneShader::setOpacity(float value)
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
GrayShader::GrayShader()
 | 
			
		||||
{
 | 
			
		||||
	INIT_SHADER(simple, gray, GrayShader);
 | 
			
		||||
 | 
			
		||||
	ShaderBase::init();
 | 
			
		||||
 | 
			
		||||
	GET_U(gray);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void GrayShader::setGray(float value)
 | 
			
		||||
{
 | 
			
		||||
	gl.Uniform1f(u_gray, value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
TilemapShader::TilemapShader()
 | 
			
		||||
{
 | 
			
		||||
	INIT_SHADER(tilemap, simple, TilemapShader);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										24
									
								
								src/shader.h
									
										
									
									
									
								
							
							
						
						
									
										24
									
								
								src/shader.h
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -87,6 +87,17 @@ protected:
 | 
			
		|||
	GLint u_texSizeInv, u_translation;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class FlatColorShader : public ShaderBase
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	FlatColorShader();
 | 
			
		||||
 | 
			
		||||
	void setColor(const Vec4 &value);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
	GLint u_color;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class SimpleShader : public ShaderBase
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
| 
						 | 
				
			
			@ -191,6 +202,17 @@ private:
 | 
			
		|||
	GLint u_tone, u_color, u_flash, u_opacity;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class GrayShader : public ShaderBase
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	GrayShader();
 | 
			
		||||
 | 
			
		||||
	void setGray(float value);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
	GLint u_gray;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class TilemapShader : public ShaderBase
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
| 
						 | 
				
			
			@ -285,6 +307,7 @@ private:
 | 
			
		|||
/* Global object containing all available shaders */
 | 
			
		||||
struct ShaderSet
 | 
			
		||||
{
 | 
			
		||||
	FlatColorShader flatColor;
 | 
			
		||||
	SimpleShader simple;
 | 
			
		||||
	SimpleColorShader simpleColor;
 | 
			
		||||
	SimpleAlphaShader simpleAlpha;
 | 
			
		||||
| 
						 | 
				
			
			@ -292,6 +315,7 @@ struct ShaderSet
 | 
			
		|||
	AlphaSpriteShader alphaSprite;
 | 
			
		||||
	SpriteShader sprite;
 | 
			
		||||
	PlaneShader plane;
 | 
			
		||||
	GrayShader gray;
 | 
			
		||||
	TilemapShader tilemap;
 | 
			
		||||
	FlashMapShader flashMap;
 | 
			
		||||
	TransShader trans;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue