summaryrefslogtreecommitdiff
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
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.
-rw-r--r--data/graphics/shaders/CMakeLists.txt2
-rw-r--r--data/graphics/shaders/Makefile.am4
-rw-r--r--data/graphics/shaders/simple_frag.glsl11
-rw-r--r--data/graphics/shaders/simple_vertex.glsl4
-rw-r--r--data/graphics/shaders/texture_frag.glsl9
-rw-r--r--data/graphics/shaders/texture_vertex.glsl9
-rw-r--r--src/defaults.cpp2
-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
11 files changed, 69 insertions, 124 deletions
diff --git a/data/graphics/shaders/CMakeLists.txt b/data/graphics/shaders/CMakeLists.txt
index bc4c3e78f..0dc6cc124 100644
--- a/data/graphics/shaders/CMakeLists.txt
+++ b/data/graphics/shaders/CMakeLists.txt
@@ -1,8 +1,6 @@
SET(FILES
simple_frag.glsl
simple_vertex.glsl
- texture_frag.glsl
- texture_vertex.glsl
)
INSTALL(FILES ${FILES} DESTINATION ${DATA_DIR}/graphics/shaders)
diff --git a/data/graphics/shaders/Makefile.am b/data/graphics/shaders/Makefile.am
index 54aab1330..db10da9b1 100644
--- a/data/graphics/shaders/Makefile.am
+++ b/data/graphics/shaders/Makefile.am
@@ -2,9 +2,7 @@ shadersdir = $(pkgdatadir)/data/graphics/shaders
shaders_DATA = \
simple_frag.glsl \
- simple_vertex.glsl \
- texture_frag.glsl \
- texture_vertex.glsl
+ simple_vertex.glsl
EXTRA_DIST = \
$(shaders_DATA) \
diff --git a/data/graphics/shaders/simple_frag.glsl b/data/graphics/shaders/simple_frag.glsl
index 02fbf5519..45c1ddd73 100644
--- a/data/graphics/shaders/simple_frag.glsl
+++ b/data/graphics/shaders/simple_frag.glsl
@@ -1,7 +1,16 @@
#version 150 core
+in vec2 Texcoord;
out vec4 outColor;
+uniform sampler2D tex;
uniform vec4 color;
+uniform float drawType;
+uniform float alpha;
+
void main()
{
- outColor = color;
+ outColor = texture(tex, Texcoord);
+ if (drawType >= 0.1)
+ outColor = texture(tex, Texcoord) * vec4(1.0, 1.0, 1.0, alpha);
+ else
+ outColor = color;
}
diff --git a/data/graphics/shaders/simple_vertex.glsl b/data/graphics/shaders/simple_vertex.glsl
index 3be44c6d8..a10a39b13 100644
--- a/data/graphics/shaders/simple_vertex.glsl
+++ b/data/graphics/shaders/simple_vertex.glsl
@@ -1,7 +1,9 @@
#version 150 core
-in vec2 position;
+in vec4 position;
+out vec2 Texcoord;
uniform vec2 screen;
void main()
{
+ Texcoord = vec2(position.z, position.w);
gl_Position = vec4(position.x / screen.x - 1, 1 - position.y / screen.y, 0.0, 1.0);
}
diff --git a/data/graphics/shaders/texture_frag.glsl b/data/graphics/shaders/texture_frag.glsl
deleted file mode 100644
index db6c7176f..000000000
--- a/data/graphics/shaders/texture_frag.glsl
+++ /dev/null
@@ -1,9 +0,0 @@
-#version 150 core
-in vec2 Texcoord;
-out vec4 outColor;
-uniform sampler2D tex;
-uniform float color;
-void main()
-{
- outColor = texture(tex, Texcoord) * vec4(1.0, 1.0, 1.0, color);
-}
diff --git a/data/graphics/shaders/texture_vertex.glsl b/data/graphics/shaders/texture_vertex.glsl
deleted file mode 100644
index a10a39b13..000000000
--- a/data/graphics/shaders/texture_vertex.glsl
+++ /dev/null
@@ -1,9 +0,0 @@
-#version 150 core
-in vec4 position;
-out vec2 Texcoord;
-uniform vec2 screen;
-void main()
-{
- Texcoord = vec2(position.z, position.w);
- gl_Position = vec4(position.x / screen.x - 1, 1 - position.y / screen.y, 0.0, 1.0);
-}
diff --git a/src/defaults.cpp b/src/defaults.cpp
index 2f4bcae20..756bfb3fd 100644
--- a/src/defaults.cpp
+++ b/src/defaults.cpp
@@ -465,8 +465,6 @@ DefaultsData* getPathsDefaults()
AddDEF("simpleVertexShader", "simple_vertex.glsl");
AddDEF("simpleFragmentShader", "simple_frag.glsl");
- AddDEF("textureVertexShader", "texture_vertex.glsl");
- AddDEF("textureFragmentShader", "texture_frag.glsl");
AddDEF("particles", "graphics/particles/");
AddDEF("portalEffectFile", "warparea.particle.xml");
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;