Shader: Group all shaders in one collective struct

Makes sharedstate.{cpp|h} a lot easier to read, and
adding new shaders a one liner.
This commit is contained in:
Jonas Kulla 2013-12-11 05:22:13 +01:00
parent 8c6648f47e
commit 0035c23641
9 changed files with 48 additions and 83 deletions

View File

@ -162,7 +162,7 @@ struct BitmapPrivate
if (pointArray.count() == 0)
return;
SimpleColorShader &shader = shState->simpleColorShader();
SimpleColorShader &shader = shState->shaders().simpleColor;
shader.bind();
shader.setTranslation(Vec2i());
@ -396,7 +396,7 @@ void Bitmap::stretchBlt(const IntRect &destRect,
((float) source.width() / sourceRect.w) * ((float) destRect.w / gpTex.width),
((float) source.height() / sourceRect.h) * ((float) destRect.h / gpTex.height));
BltShader &shader = shState->bltShader();
BltShader &shader = shState->shaders().blt;
shader.bind();
shader.setDestination(gpTex.tex);
shader.setSubRect(bltSubRect);
@ -465,7 +465,7 @@ void Bitmap::gradientFillRect(const IntRect &rect,
flush();
SimpleColorShader &shader = shState->simpleColorShader();
SimpleColorShader &shader = shState->shaders().simpleColor;
shader.bind();
shader.setTranslation(Vec2i());
@ -530,7 +530,7 @@ void Bitmap::blur()
TEXFBO auxTex = shState->texPool().request(width(), height());
BlurShader &shader = shState->blurShader();
BlurShader &shader = shState->shaders().blur;
BlurShader::HPass &pass1 = shader.pass1;
BlurShader::VPass &pass2 = shader.pass2;
@ -632,7 +632,7 @@ void Bitmap::radialBlur(int angle, int divisions)
glState.blendMode.pushSet(BlendAddition);
SimpleMatrixShader &shader = shState->simpleMatrixShader();
SimpleMatrixShader &shader = shState->shaders().simpleMatrix;
shader.bind();
p->bindTexture(shader);
@ -740,7 +740,7 @@ void Bitmap::hueChange(int hue)
hue = wrapRange(hue, 0, 359);
float hueAdj = -((M_PI * 2) / 360) * hue;
HueShader &shader = shState->hueShader();
HueShader &shader = shState->shaders().hue;
shader.bind();
shader.setHueAdjust(hueAdj);
@ -911,7 +911,7 @@ void Bitmap::drawText(const IntRect &rect, const char *str, int align)
(float) gpTexSize.x / gpTex2.width,
(float) gpTexSize.y / gpTex2.height);
BltShader &shader = shState->bltShader();
BltShader &shader = shState->shaders().blt;
shader.bind();
shader.setTexSize(gpTexSize);
shader.setSource();

View File

@ -154,7 +154,7 @@ public:
#ifdef RGSS2
if (brightEffect)
{
SimpleColorShader &shader = shState->simpleColorShader();
SimpleColorShader &shader = shState->shaders().simpleColor;
shader.bind();
shader.applyViewportProj();
shader.setTranslation(Vec2i());
@ -177,7 +177,7 @@ public:
pp.blitFBOs();
glState.scissorTest.pop();
PlaneShader &shader = shState->planeShader();
PlaneShader &shader = shState->shaders().plane;
shader.bind();
shader.setColor(c);
shader.setFlash(f);
@ -585,7 +585,7 @@ void Graphics::transition(int duration,
* we can use a simplified shader */
if (transMap)
{
TransShader &shader = shState->transShader();
TransShader &shader = shState->shaders().trans;
shader.bind();
shader.applyViewportProj();
shader.setFrozenScene(p->frozenScene.tex);
@ -596,7 +596,7 @@ void Graphics::transition(int duration,
}
else
{
SimpleTransShader &shader = shState->sTransShader();
SimpleTransShader &shader = shState->shaders().simpleTrans;
shader.bind();
shader.applyViewportProj();
shader.setFrozenScene(p->frozenScene.tex);
@ -617,9 +617,9 @@ void Graphics::transition(int duration,
const float prog = i * (1.0 / duration);
if (transMap)
shState->transShader().setProg(prog);
shState->shaders().trans.setProg(prog);
else
shState->sTransShader().setProg(prog);
shState->shaders().simpleTrans.setProg(prog);
/* Draw the composed frame to a buffer first
* (we need this because we're skipping PingPong) */

View File

@ -204,7 +204,7 @@ void Plane::draw()
if (p->color->hasEffect() || p->tone->hasEffect() || p->opacity != 255)
{
PlaneShader &shader = shState->planeShader();
PlaneShader &shader = shState->shaders().plane;
shader.bind();
shader.applyViewportProj();
@ -217,7 +217,7 @@ void Plane::draw()
}
else
{
SimpleShader &shader = shState->simpleShader();
SimpleShader &shader = shState->shaders().simple;
shader.bind();
shader.applyViewportProj();

View File

@ -247,4 +247,25 @@ private:
GLint u_source, u_destination, u_subRect, u_opacity;
};
/* Global object containing all available shaders */
struct ShaderSet
{
SimpleShader simple;
SimpleColorShader simpleColor;
SimpleAlphaShader simpleAlpha;
SimpleSpriteShader simpleSprite;
SpriteShader sprite;
PlaneShader plane;
FlashMapShader flashMap;
TransShader trans;
SimpleTransShader simpleTrans;
HueShader hue;
BltShader blt;
#ifdef RGSS2
SimpleMatrixShader simpleMatrix;
BlurShader blur;
#endif
};
#endif // SHADER_H

View File

@ -65,22 +65,7 @@ struct SharedStatePrivate
GLState _glState;
SimpleShader simpleShader;
SimpleColorShader simpleColorShader;
SimpleAlphaShader simpleAlphaShader;
SimpleSpriteShader simpleSpriteShader;
SpriteShader spriteShader;
PlaneShader planeShader;
FlashMapShader flashMapShader;
TransShader transShader;
SimpleTransShader sTransShader;
HueShader hueShader;
BltShader bltShader;
#ifdef RGSS2
SimpleMatrixShader simpleMatrixShader;
BlurShader blurShader;
#endif
ShaderSet shaders;
TexPool texPool;
FontPool fontPool;
@ -192,26 +177,11 @@ GSATT(Graphics&, graphics)
GSATT(Input&, input)
GSATT(Audio&, audio)
GSATT(GLState&, _glState)
GSATT(SimpleShader&, simpleShader)
GSATT(SimpleColorShader&, simpleColorShader)
GSATT(SimpleAlphaShader&, simpleAlphaShader)
GSATT(SimpleSpriteShader&, simpleSpriteShader)
GSATT(SpriteShader&, spriteShader)
GSATT(PlaneShader&, planeShader)
GSATT(FlashMapShader&, flashMapShader)
GSATT(TransShader&, transShader)
GSATT(SimpleTransShader&, sTransShader)
GSATT(HueShader&, hueShader)
GSATT(BltShader&, bltShader)
GSATT(ShaderSet&, shaders)
GSATT(TexPool&, texPool)
GSATT(FontPool&, fontPool)
GSATT(Quad&, gpQuad)
#ifdef RGSS2
GSATT(SimpleMatrixShader&, simpleMatrixShader)
GSATT(BlurShader&, blurShader)
#endif
void SharedState::setBindingData(void *data)
{
p->bindingData = data;

View File

@ -34,6 +34,7 @@ struct mrb_state;
struct SDL_Window;
struct TEXFBO;
struct Quad;
struct ShaderSet;
class Scene;
class FileSystem;
@ -42,18 +43,6 @@ class Graphics;
class Input;
class Audio;
class GLState;
class SimpleShader;
class SimpleColorShader;
class SimpleAlphaShader;
class SimpleSpriteShader;
class SimpleMatrixShader;
class SpriteShader;
class PlaneShader;
class FlashMapShader;
class TransShader;
class SimpleTransShader;
class HueShader;
class BltShader;
class TexPool;
class FontPool;
class Font;
@ -84,22 +73,7 @@ struct SharedState
GLState &_glState();
SimpleShader &simpleShader();
SimpleColorShader &simpleColorShader();
SimpleAlphaShader &simpleAlphaShader();
SimpleSpriteShader &simpleSpriteShader();
SpriteShader &spriteShader();
PlaneShader &planeShader();
FlashMapShader &flashMapShader();
TransShader &transShader();
SimpleTransShader &sTransShader();
HueShader &hueShader();
BltShader &bltShader();
#ifdef RGSS2
SimpleMatrixShader &simpleMatrixShader();
BlurShader &blurShader();
#endif
ShaderSet &shaders();
TexPool &texPool();
FontPool &fontPool();

View File

@ -378,7 +378,7 @@ void Sprite::draw()
if (renderEffect)
{
SpriteShader &shader = shState->spriteShader();
SpriteShader &shader = shState->shaders().sprite;
shader.bind();
shader.applyViewportProj();
@ -400,7 +400,7 @@ void Sprite::draw()
}
else
{
SimpleSpriteShader &shader = shState->simpleSpriteShader();
SimpleSpriteShader &shader = shState->shaders().simpleSprite;
shader.bind();
shader.setSpriteMat(p->trans.getMatrix());

View File

@ -1114,7 +1114,7 @@ GroundLayer::GroundLayer(TilemapPrivate *p, Viewport *viewport)
void GroundLayer::draw()
{
SimpleShader &shader = shState->simpleShader();
SimpleShader &shader = shState->shaders().simple;
shader.bind();
shader.applyViewportProj();
@ -1142,7 +1142,7 @@ void GroundLayer::draw()
glState.blendMode.pushSet(BlendAddition);
glState.texture2D.pushSet(false);
FlashMapShader &shader = shState->flashMapShader();
FlashMapShader &shader = shState->shaders().flashMap;
shader.bind();
shader.applyViewportProj();
shader.setAlpha(flashAlpha[p->flash.alphaIdx] / 255.f);
@ -1199,7 +1199,7 @@ void ScanRow::draw()
if (batchedFlag)
return;
SimpleShader &shader = shState->simpleShader();
SimpleShader &shader = shState->shaders().simple;
shader.bind();
shader.applyViewportProj();

View File

@ -422,7 +422,7 @@ struct WindowPrivate
glState.viewport.pushSet(IntRect(0, 0, baseTex.width, baseTex.height));
glState.clearColor.pushSet(Vec4());
SimpleAlphaShader &shader = shState->simpleAlphaShader();
SimpleAlphaShader &shader = shState->shaders().simpleAlpha;
shader.bind();
shader.applyViewportProj();
shader.setTranslation(Vec2i());
@ -560,7 +560,7 @@ struct WindowPrivate
Vec2i trans(position.x + sceneOffset.x,
position.y + sceneOffset.y);
SimpleAlphaShader &shader = shState->simpleAlphaShader();
SimpleAlphaShader &shader = shState->shaders().simpleAlpha;
shader.bind();
shader.applyViewportProj();
shader.setTranslation(trans);
@ -609,7 +609,7 @@ struct WindowPrivate
glState.scissorBox.push();
glState.scissorBox.setIntersect(windowRect);
SimpleAlphaShader &shader = shState->simpleAlphaShader();
SimpleAlphaShader &shader = shState->shaders().simpleAlpha;
shader.bind();
shader.applyViewportProj();
shader.setTranslation(Vec2i(effectX, effectY));