summaryrefslogtreecommitdiff
path: root/src/render
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2014-06-11 23:44:25 +0300
committerAndrei Karas <akaras@inbox.ru>2014-06-12 00:47:24 +0300
commit8e1fc2522ee907da5fc3e65691990fd608539a72 (patch)
tree888bdab76dc75120a68c4213c204d0ce13da6f61 /src/render
parent41836760fa59abc74098aed7450b4858dc288e56 (diff)
downloadmanaverse-8e1fc2522ee907da5fc3e65691990fd608539a72.tar.gz
manaverse-8e1fc2522ee907da5fc3e65691990fd608539a72.tar.bz2
manaverse-8e1fc2522ee907da5fc3e65691990fd608539a72.tar.xz
manaverse-8e1fc2522ee907da5fc3e65691990fd608539a72.zip
Use only one shader program for all drawing.
Diffstat (limited to 'src/render')
-rw-r--r--src/render/modernopenglgraphics.cpp130
-rw-r--r--src/render/modernopenglgraphics.h14
-rw-r--r--src/render/shaders/shadersmanager.cpp6
-rw-r--r--src/render/shaders/shadersmanager.h2
4 files changed, 55 insertions, 97 deletions
diff --git a/src/render/modernopenglgraphics.cpp b/src/render/modernopenglgraphics.cpp
index a1c2d2bb6..73a594f32 100644
--- a/src/render/modernopenglgraphics.cpp
+++ b/src/render/modernopenglgraphics.cpp
@@ -85,24 +85,20 @@ unsigned int ModernOpenGLGraphics::mLastDrawCalls = 0;
ModernOpenGLGraphics::ModernOpenGLGraphics() :
mFloatArray(nullptr),
mFloatArrayCached(nullptr),
- mSimpleProgram(nullptr),
- mTextureProgram(nullptr),
+ mProgram(nullptr),
mAlphaCached(1.0F),
mVpCached(0),
- mTexture(false),
mIsByteColor(false),
mByteColor(),
mImageCached(0),
mFloatColor(1.0F),
mMaxVertices(500),
- mSimpleProgramId(0U),
- mTextureProgramId(0U),
+ mProgramId(0U),
mSimpleColorUniform(0U),
- mSimplePosAttrib(0),
- mTexturePosAttrib(0),
+ mPosAttrib(0),
mTextureColorUniform(0U),
- mSimpleScreenUniform(0U),
- mTextureScreenUniform(0U),
+ mScreenUniform(0U),
+ mDrawTypeUniform(0U),
mVao(0U),
mVbo(0U),
mColorAlpha(false),
@@ -120,8 +116,8 @@ ModernOpenGLGraphics::ModernOpenGLGraphics() :
ModernOpenGLGraphics::~ModernOpenGLGraphics()
{
deleteArraysInternal();
- if (mSimpleProgram)
- mSimpleProgram->decRef();
+ if (mProgram)
+ mProgram->decRef();
if (mVbo)
mglDeleteBuffers(1, &mVbo);
if (mVao)
@@ -153,38 +149,34 @@ void ModernOpenGLGraphics::postInit()
mglBindBuffer(GL_ARRAY_BUFFER, mVbo);
logger->log("Compiling shaders");
- mSimpleProgram = shaders.getSimpleProgram();
- mSimpleProgramId = mSimpleProgram->getProgramId();
- mTextureProgram = shaders.getTextureProgram();
- mTextureProgramId = mTextureProgram->getProgramId();
- if (!mSimpleProgram || !mTextureProgram)
+ mProgram = shaders.getSimpleProgram();
+ mProgramId = mProgram->getProgramId();
+ if (!mProgram)
logger->error("Shaders compilation error.");
logger->log("Shaders compilation done.");
- mglUseProgram(mSimpleProgramId);
- mSimplePosAttrib = mglGetAttribLocation(mSimpleProgramId, "position");
- mglEnableVertexAttribArray(mSimplePosAttrib);
- mSimpleColorUniform = mglGetUniformLocation(mSimpleProgramId, "color");
- mSimpleScreenUniform = mglGetUniformLocation(mSimpleProgramId, "screen");
- mglVertexAttribFormat(mSimplePosAttrib, 2, GL_FLOAT, GL_FALSE, 0);
-
- mTexturePosAttrib = mglGetAttribLocation(mTextureProgramId, "position");
- mTextureColorUniform = mglGetUniformLocation(mTextureProgramId, "color");
+ mglUseProgram(mProgramId);
+
+ mPosAttrib = mglGetAttribLocation(mProgramId, "position");
+ mglEnableVertexAttribArray(mPosAttrib);
+ mglVertexAttribFormat(mPosAttrib, 4, GL_FLOAT, GL_FALSE, 0);
+
+ mSimpleColorUniform = mglGetUniformLocation(mProgramId, "color");
+ mScreenUniform = mglGetUniformLocation(mProgramId, "screen");
+ mDrawTypeUniform = mglGetUniformLocation(mProgramId, "drawType");
+ mTextureColorUniform = mglGetUniformLocation(mProgramId, "alpha");
+
mglUniform1f(mTextureColorUniform, 1.0f);
- mTextureScreenUniform = mglGetUniformLocation(mTextureProgramId, "screen");
- mglVertexAttribFormat(mTexturePosAttrib, 4, GL_FLOAT, GL_FALSE, 0);
+
+ mglBindVertexBuffer(0, mVbo, 0, 4 * sizeof(GLfloat));
+ mglVertexAttribBinding(mPosAttrib, 0);
screenResized();
}
void ModernOpenGLGraphics::screenResized()
{
- mglProgramUniform2f(mSimpleProgramId,
- mSimpleScreenUniform,
- static_cast<float>(mWidth) / 2.0f,
- static_cast<float>(mHeight) / 2.0f);
- mglProgramUniform2f(mTextureProgramId,
- mTextureScreenUniform,
+ mglUniform2f(mScreenUniform,
static_cast<float>(mWidth) / 2.0f,
static_cast<float>(mHeight) / 2.0f);
}
@@ -228,23 +220,11 @@ void ModernOpenGLGraphics::setColorAll(const Color &color,
if (mColor != color)
{
mColor = color;
- if (mTextureDraw)
- {
- mglProgramUniform4f(mSimpleProgramId,
- mSimpleColorUniform,
- static_cast<float>(color.r) / 255.0F,
- static_cast<float>(color.g) / 255.0F,
- static_cast<float>(color.b) / 255.0F,
- static_cast<float>(color.a) / 255.0F);
- }
- else
- {
- mglUniform4f(mSimpleColorUniform,
- static_cast<float>(color.r) / 255.0F,
- static_cast<float>(color.g) / 255.0F,
- static_cast<float>(color.b) / 255.0F,
- static_cast<float>(color.a) / 255.0F);
- }
+ mglUniform4f(mSimpleColorUniform,
+ static_cast<float>(color.r) / 255.0F,
+ static_cast<float>(color.g) / 255.0F,
+ static_cast<float>(color.b) / 255.0F,
+ static_cast<float>(color.a) / 255.0F);
}
}
@@ -283,13 +263,10 @@ void ModernOpenGLGraphics::drawQuad(const Image *const image,
x2, y2, texX2, texY2
};
- mglBindVertexBuffer(0, mVbo, 0, 4 * sizeof(GLfloat));
- mglVertexAttribBinding(mTexturePosAttrib, 0);
mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
vertices, GL_DYNAMIC_DRAW);
-
#ifdef DEBUG_DRAW_CALLS
- MobileOpenGLGraphics::mDrawCalls ++;
+ mDrawCalls ++;
#endif
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@@ -700,19 +677,17 @@ void ModernOpenGLGraphics::drawRectangle(const Rect& rect)
const int y2 = y1 + rect.height;
GLfloat vertices[] =
{
- x1, y1,
- x1, y2,
- x2, y2,
- x2, y1
+ x1, y1, 0.0f, 0.0f,
+ x1, y2, 0.0f, 0.0f,
+ x2, y2, 0.0f, 0.0f,
+ x2, y1, 0.0f, 0.0f
};
- mglBindVertexBuffer(3, mVbo, 0, 2 * sizeof(GLfloat));
-
- mglVertexAttribBinding(mSimplePosAttrib, 3);
-
mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
vertices, GL_DYNAMIC_DRAW);
-
+#ifdef DEBUG_DRAW_CALLS
+ mDrawCalls ++;
+#endif
glDrawArrays(GL_LINE_LOOP, 0, 4);
}
@@ -726,19 +701,17 @@ void ModernOpenGLGraphics::fillRectangle(const Rect& rect)
const int y2 = y1 + rect.height;
GLfloat vertices[] =
{
- x1, y1,
- x2, y1,
- x1, y2,
- x2, y2
+ x1, y1, 0.0f, 0.0f,
+ x2, y1, 0.0f, 0.0f,
+ x1, y2, 0.0f, 0.0f,
+ x2, y2, 0.0f, 0.0f
};
- mglBindVertexBuffer(3, mVbo, 0, 2 * sizeof(GLfloat));
-
- mglVertexAttribBinding(mSimplePosAttrib, 3);
-
mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
vertices, GL_DYNAMIC_DRAW);
-
+#ifdef DEBUG_DRAW_CALLS
+ mDrawCalls ++;
+#endif
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@@ -749,9 +722,7 @@ void ModernOpenGLGraphics::setTexturingAndBlending(const bool enable)
if (!mTextureDraw)
{
mTextureDraw = true;
- mglDisableVertexAttribArray(mSimplePosAttrib);
- mglUseProgram(mTextureProgramId);
- mglEnableVertexAttribArray(mTexturePosAttrib);
+ mglUniform1f(mDrawTypeUniform, 1.0f);
}
if (!mAlpha)
{
@@ -764,9 +735,7 @@ void ModernOpenGLGraphics::setTexturingAndBlending(const bool enable)
if (mTextureDraw)
{
mTextureDraw = false;
- mglDisableVertexAttribArray(mTexturePosAttrib);
- mglUseProgram(mSimpleProgramId);
- mglEnableVertexAttribArray(mSimplePosAttrib);
+ mglUniform1f(mDrawTypeUniform, 0.0f);
}
if (mAlpha && !mColorAlpha)
{
@@ -843,9 +812,10 @@ void ModernOpenGLGraphics::clearScreen() const
void ModernOpenGLGraphics::drawTriangleArray(const int size)
{
- mglBindVertexBuffer(0, mVbo, 0, 4 * sizeof(GLfloat));
- mglVertexAttribBinding(mTexturePosAttrib, 0);
mglBufferData(GL_ARRAY_BUFFER, size, mFloatArray, GL_DYNAMIC_DRAW);
+#ifdef DEBUG_DRAW_CALLS
+ mDrawCalls ++;
+#endif
glDrawArrays(GL_TRIANGLES, 0, size / 2);;
}
diff --git a/src/render/modernopenglgraphics.h b/src/render/modernopenglgraphics.h
index d9c043fc0..56e949aab 100644
--- a/src/render/modernopenglgraphics.h
+++ b/src/render/modernopenglgraphics.h
@@ -82,25 +82,21 @@ class ModernOpenGLGraphics final : public Graphics
GLfloat *mFloatArray;
GLfloat *mFloatArrayCached;
- ShaderProgram *mSimpleProgram;
- ShaderProgram *mTextureProgram;
+ ShaderProgram *mProgram;
float mAlphaCached;
int mVpCached;
- bool mTexture;
bool mIsByteColor;
Color mByteColor;
GLuint mImageCached;
float mFloatColor;
int mMaxVertices;
- GLuint mSimpleProgramId;
- GLuint mTextureProgramId;
+ GLuint mProgramId;
GLuint mSimpleColorUniform;
- GLint mSimplePosAttrib;
- GLint mTexturePosAttrib;
+ GLint mPosAttrib;
GLint mTextureColorUniform;
- GLuint mSimpleScreenUniform;
- GLuint mTextureScreenUniform;
+ GLuint mScreenUniform;
+ GLuint mDrawTypeUniform;
GLuint mVao;
GLuint mVbo;
bool mColorAlpha;
diff --git a/src/render/shaders/shadersmanager.cpp b/src/render/shaders/shadersmanager.cpp
index 8a1072deb..2c37f56d5 100644
--- a/src/render/shaders/shadersmanager.cpp
+++ b/src/render/shaders/shadersmanager.cpp
@@ -119,10 +119,4 @@ ShaderProgram *ShadersManager::getSimpleProgram()
dir + paths.getStringValue("simpleFragmentShader"));
}
-ShaderProgram *ShadersManager::getTextureProgram()
-{
- const std::string dir = paths.getStringValue("shaders");
- return createProgram(dir + paths.getStringValue("textureVertexShader"),
- dir + paths.getStringValue("textureFragmentShader"));
-}
#endif
diff --git a/src/render/shaders/shadersmanager.h b/src/render/shaders/shadersmanager.h
index b525d18e1..50e714aba 100644
--- a/src/render/shaders/shadersmanager.h
+++ b/src/render/shaders/shadersmanager.h
@@ -41,8 +41,6 @@ class ShadersManager final
A_WARN_UNUSED;
ShaderProgram *getSimpleProgram();
-
- ShaderProgram *getTextureProgram();
};
extern ShadersManager shaders;