Program.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*Gluint
  2. * Copyright 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdint.h>
  17. #include <log/log.h>
  18. #include "Program.h"
  19. #include "ProgramCache.h"
  20. #include "Description.h"
  21. #include <utils/String8.h>
  22. namespace android {
  23. Program::Program(const ProgramCache::Key& /*needs*/, const char* vertex, const char* fragment)
  24. : mInitialized(false) {
  25. GLuint vertexId = buildShader(vertex, GL_VERTEX_SHADER);
  26. GLuint fragmentId = buildShader(fragment, GL_FRAGMENT_SHADER);
  27. GLuint programId = glCreateProgram();
  28. glAttachShader(programId, vertexId);
  29. glAttachShader(programId, fragmentId);
  30. glBindAttribLocation(programId, position, "position");
  31. glBindAttribLocation(programId, texCoords, "texCoords");
  32. glLinkProgram(programId);
  33. GLint status;
  34. glGetProgramiv(programId, GL_LINK_STATUS, &status);
  35. if (status != GL_TRUE) {
  36. ALOGE("Error while linking shaders:");
  37. GLint infoLen = 0;
  38. glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLen);
  39. if (infoLen > 1) {
  40. GLchar log[infoLen];
  41. glGetProgramInfoLog(programId, infoLen, 0, &log[0]);
  42. ALOGE("%s", log);
  43. }
  44. glDetachShader(programId, vertexId);
  45. glDetachShader(programId, fragmentId);
  46. glDeleteShader(vertexId);
  47. glDeleteShader(fragmentId);
  48. glDeleteProgram(programId);
  49. } else {
  50. mProgram = programId;
  51. mVertexShader = vertexId;
  52. mFragmentShader = fragmentId;
  53. mInitialized = true;
  54. mColorMatrixLoc = glGetUniformLocation(programId, "colorMatrix");
  55. mProjectionMatrixLoc = glGetUniformLocation(programId, "projection");
  56. mTextureMatrixLoc = glGetUniformLocation(programId, "texture");
  57. mSamplerLoc = glGetUniformLocation(programId, "sampler");
  58. mColorLoc = glGetUniformLocation(programId, "color");
  59. mAlphaPlaneLoc = glGetUniformLocation(programId, "alphaPlane");
  60. mSamplerMaskLoc = glGetUniformLocation(programId, "samplerMask");
  61. mMaskAlphaThresholdLoc = glGetUniformLocation(programId, "maskAlphaThreshold");
  62. // set-up the default values for our uniforms
  63. glUseProgram(programId);
  64. const GLfloat m[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
  65. glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, m);
  66. glEnableVertexAttribArray(0);
  67. }
  68. }
  69. Program::~Program() {
  70. }
  71. bool Program::isValid() const {
  72. return mInitialized;
  73. }
  74. void Program::use() {
  75. glUseProgram(mProgram);
  76. }
  77. GLuint Program::getAttrib(const char* name) const {
  78. // TODO: maybe use a local cache
  79. return glGetAttribLocation(mProgram, name);
  80. }
  81. GLint Program::getUniform(const char* name) const {
  82. // TODO: maybe use a local cache
  83. return glGetUniformLocation(mProgram, name);
  84. }
  85. GLuint Program::buildShader(const char* source, GLenum type) {
  86. GLuint shader = glCreateShader(type);
  87. glShaderSource(shader, 1, &source, 0);
  88. glCompileShader(shader);
  89. GLint status;
  90. glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  91. if (status != GL_TRUE) {
  92. // Some drivers return wrong values for GL_INFO_LOG_LENGTH
  93. // use a fixed size instead
  94. GLchar log[512];
  95. glGetShaderInfoLog(shader, sizeof(log), 0, log);
  96. ALOGE("Error while compiling shader: \n%s\n%s", source, log);
  97. glDeleteShader(shader);
  98. return 0;
  99. }
  100. return shader;
  101. }
  102. String8& Program::dumpShader(String8& result, GLenum /*type*/) {
  103. GLuint shader = GL_FRAGMENT_SHADER ? mFragmentShader : mVertexShader;
  104. GLint l;
  105. glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &l);
  106. char* src = new char[l];
  107. glGetShaderSource(shader, l, NULL, src);
  108. result.append(src);
  109. delete [] src;
  110. return result;
  111. }
  112. void Program::setUniforms(const Description& desc) {
  113. // TODO: we should have a mechanism here to not always reset uniforms that
  114. // didn't change for this program.
  115. if (mSamplerLoc >= 0) {
  116. glUniform1i(mSamplerLoc, 0);
  117. glUniformMatrix4fv(mTextureMatrixLoc, 1, GL_FALSE, desc.mTexture.getMatrix().asArray());
  118. }
  119. if (mAlphaPlaneLoc >= 0) {
  120. glUniform1f(mAlphaPlaneLoc, desc.mPlaneAlpha);
  121. }
  122. if (mColorLoc >= 0) {
  123. glUniform4fv(mColorLoc, 1, desc.mColor);
  124. }
  125. if (mColorMatrixLoc >= 0) {
  126. glUniformMatrix4fv(mColorMatrixLoc, 1, GL_FALSE, desc.mColorMatrix.asArray());
  127. }
  128. // these uniforms are always present
  129. glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, desc.mProjectionMatrix.asArray());
  130. if (mSamplerMaskLoc >= 0) {
  131. glUniform1i(mSamplerMaskLoc, 1);
  132. }
  133. if (mMaskAlphaThresholdLoc >= 0) {
  134. glUniform1f(mMaskAlphaThresholdLoc, desc.mMaskAlphaThreshold);
  135. }
  136. }
  137. } /* namespace android */