diff options
Diffstat (limited to 'libs/hwui/Program.cpp')
-rw-r--r-- | libs/hwui/Program.cpp | 125 |
1 files changed, 22 insertions, 103 deletions
diff --git a/libs/hwui/Program.cpp b/libs/hwui/Program.cpp index 86fc154..dbae38e 100644 --- a/libs/hwui/Program.cpp +++ b/libs/hwui/Program.cpp @@ -27,16 +27,6 @@ namespace uirenderer { #define SHADER_SOURCE(name, source) const char* name = #source -#include "shaders/drawColor.frag" - -#include "shaders/drawTexture.vert" -#include "shaders/drawTexture.frag" - -#include "shaders/drawText.frag" - -#include "shaders/drawLinearGradient.vert" -#include "shaders/drawLinearGradient.frag" - /////////////////////////////////////////////////////////////////////////////// // Base program /////////////////////////////////////////////////////////////////////////////// @@ -65,6 +55,10 @@ Program::Program(const char* vertex, const char* fragment) { } mUse = false; + + position = addAttrib("position"); + color = addUniform("color"); + transform = addUniform("transform"); } Program::~Program() { @@ -73,15 +67,6 @@ Program::~Program() { glDeleteProgram(id); } -void Program::use() { - glUseProgram(id); - mUse = true; -} - -void Program::remove() { - mUse = false; -} - int Program::addAttrib(const char* name) { int slot = glGetAttribLocation(id, name); attributes.add(name, slot); @@ -89,7 +74,11 @@ int Program::addAttrib(const char* name) { } int Program::getAttrib(const char* name) { - return attributes.valueFor(name); + ssize_t index = attributes.indexOfKey(name); + if (index >= 0) { + return attributes.valueAt(index); + } + return addAttrib(name); } int Program::addUniform(const char* name) { @@ -99,7 +88,11 @@ int Program::addUniform(const char* name) { } int Program::getUniform(const char* name) { - return uniforms.valueFor(name); + ssize_t index = uniforms.indexOfKey(name); + if (index >= 0) { + return uniforms.valueAt(index); + } + return addUniform(name); } GLuint Program::buildShader(const char* source, GLenum type) { @@ -121,28 +114,7 @@ GLuint Program::buildShader(const char* source, GLenum type) { return shader; } -/////////////////////////////////////////////////////////////////////////////// -// Draw color -/////////////////////////////////////////////////////////////////////////////// - -DrawColorProgram::DrawColorProgram(): - Program(gDrawTextureVertexShader, gDrawColorFragmentShader) { - getAttribsAndUniforms(); -} - -DrawColorProgram::DrawColorProgram(const char* vertex, const char* fragment): - Program(vertex, fragment) { - getAttribsAndUniforms(); -} - -void DrawColorProgram::getAttribsAndUniforms() { - position = addAttrib("position"); - texCoords = addAttrib("texCoords"); - color = addUniform("color"); - transform = addUniform("transform"); -} - -void DrawColorProgram::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, +void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, const mat4& transformMatrix) { mat4 t(projectionMatrix); t.multiply(transformMatrix); @@ -151,70 +123,17 @@ void DrawColorProgram::set(const mat4& projectionMatrix, const mat4& modelViewMa glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); } -void DrawColorProgram::use() { - Program::use(); - glEnableVertexAttribArray(position); - glEnableVertexAttribArray(texCoords); -} - -void DrawColorProgram::remove() { - Program::remove(); - glDisableVertexAttribArray(position); - glDisableVertexAttribArray(texCoords); -} - -/////////////////////////////////////////////////////////////////////////////// -// Draw texture -/////////////////////////////////////////////////////////////////////////////// - -DrawTextureProgram::DrawTextureProgram(): - DrawColorProgram(gDrawTextureVertexShader, gDrawTextureFragmentShader) { - sampler = addUniform("sampler"); -} - -DrawTextureProgram::DrawTextureProgram(const char* vertex, const char* fragment): - DrawColorProgram(vertex, fragment) { - sampler = addUniform("sampler"); -} - -void DrawTextureProgram::use() { - DrawColorProgram::use(); - glUniform1i(sampler, 0); -} - -void DrawTextureProgram::remove() { - DrawColorProgram::remove(); -} - -/////////////////////////////////////////////////////////////////////////////// -// Draw text -/////////////////////////////////////////////////////////////////////////////// - -DrawTextProgram::DrawTextProgram(): - DrawTextureProgram(gDrawTextureVertexShader, gDrawTextFragmentShader) { -} - -/////////////////////////////////////////////////////////////////////////////// -// Draw linear gradient -/////////////////////////////////////////////////////////////////////////////// +void Program::use() { + glUseProgram(id); + mUse = true; -DrawLinearGradientProgram::DrawLinearGradientProgram(): - DrawColorProgram(gDrawLinearGradientVertexShader, gDrawLinearGradientFragmentShader) { - gradient = addUniform("gradient"); - gradientLength = addUniform("gradientLength"); - sampler = addUniform("sampler"); - start = addUniform("start"); - screenSpace = addUniform("screenSpace"); + glEnableVertexAttribArray(position); } -void DrawLinearGradientProgram::use() { - DrawColorProgram::use(); - glActiveTexture(GL_TEXTURE0); - glUniform1i(sampler, 0); -} +void Program::remove() { + mUse = false; -void DrawLinearGradientProgram::remove() { - DrawColorProgram::remove(); + glDisableVertexAttribArray(position); } }; // namespace uirenderer |