Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 18 | #include "rsContext.h" |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | #include <GLES2/gl2.h> |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 21 | #else |
| 22 | #include "rsContextHostStub.h" |
| 23 | #include <OpenGL/gl.h> |
| 24 | #endif //ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 25 | |
| 26 | using namespace android; |
| 27 | using namespace android::renderscript; |
| 28 | |
| 29 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 30 | ShaderCache::ShaderCache() { |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 31 | mEntries.setCapacity(16); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 34 | ShaderCache::~ShaderCache() { |
Jason Sams | 2cbfc4c | 2011-01-05 03:37:48 -0800 | [diff] [blame] | 35 | cleanupAll(); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 38 | void ShaderCache::updateUniformArrayData(Context *rsc, Program *prog, uint32_t linkedID, |
| 39 | UniformData *data, const char* logTag, |
| 40 | UniformQueryData **uniformList, uint32_t uniListSize) { |
| 41 | |
| 42 | for (uint32_t ct=0; ct < prog->getUniformCount(); ct++) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 43 | if (data[ct].slot >= 0 && data[ct].arraySize > 1) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 44 | //Iterate over the list of active GL uniforms and find highest array index |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 45 | for (uint32_t ui = 0; ui < uniListSize; ui ++) { |
| 46 | if (prog->getUniformName(ct) == uniformList[ui]->name) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 47 | data[ct].arraySize = (uint32_t)uniformList[ui]->arraySize; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (rsc->props.mLogShaders) { |
| 54 | LOGV("%s U, %s = %d, arraySize = %d\n", logTag, |
| 55 | prog->getUniformName(ct).string(), data[ct].slot, data[ct].arraySize); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void ShaderCache::populateUniformData(Program *prog, uint32_t linkedID, UniformData *data) { |
| 61 | for (uint32_t ct=0; ct < prog->getUniformCount(); ct++) { |
| 62 | data[ct].slot = glGetUniformLocation(linkedID, prog->getUniformName(ct)); |
| 63 | data[ct].arraySize = prog->getUniformArraySize(ct); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | bool ShaderCache::hasArrayUniforms(ProgramVertex *vtx, ProgramFragment *frag) { |
| 68 | UniformData *data = mCurrent->vtxUniforms; |
| 69 | for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 70 | if (data[ct].slot >= 0 && data[ct].arraySize > 1) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | data = mCurrent->fragUniforms; |
| 75 | for (uint32_t ct=0; ct < frag->getUniformCount(); ct++) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 76 | if (data[ct].slot >= 0 && data[ct].arraySize > 1) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 83 | bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag) { |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 84 | if (!vtx->getShaderID()) { |
Jason Sams | cd50653 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 85 | vtx->loadShader(rsc); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 86 | } |
| 87 | if (!frag->getShaderID()) { |
Jason Sams | cd50653 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 88 | frag->loadShader(rsc); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 89 | } |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 90 | |
| 91 | // Don't try to cache if shaders failed to load |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 92 | if (!vtx->getShaderID() || !frag->getShaderID()) { |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
Jason Sams | f2a5d73 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 95 | //LOGV("ShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID()); |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 96 | uint32_t entryCount = mEntries.size(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 97 | for (uint32_t ct = 0; ct < entryCount; ct ++) { |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 98 | if ((mEntries[ct]->vtx == vtx->getShaderID()) && |
| 99 | (mEntries[ct]->frag == frag->getShaderID())) { |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 100 | |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 101 | //LOGV("SC using program %i", mEntries[ct]->program); |
| 102 | glUseProgram(mEntries[ct]->program); |
| 103 | mCurrent = mEntries[ct]; |
Jason Sams | f2a5d73 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 104 | //LOGV("ShaderCache hit, using %i", ct); |
Jason Sams | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 105 | rsc->checkError("ShaderCache::lookup (hit)"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 109 | |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 110 | //LOGV("ShaderCache miss"); |
Jason Sams | f2a5d73 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 111 | //LOGE("e0 %x", glGetError()); |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 112 | ProgramEntry *e = new ProgramEntry(vtx->getAttribCount(), |
| 113 | vtx->getUniformCount(), |
| 114 | frag->getUniformCount()); |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 115 | mEntries.push(e); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 116 | mCurrent = e; |
| 117 | e->vtx = vtx->getShaderID(); |
| 118 | e->frag = frag->getShaderID(); |
| 119 | e->program = glCreateProgram(); |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 120 | if (e->program) { |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 121 | GLuint pgm = e->program; |
| 122 | glAttachShader(pgm, vtx->getShaderID()); |
| 123 | //LOGE("e1 %x", glGetError()); |
| 124 | glAttachShader(pgm, frag->getShaderID()); |
| 125 | |
Jason Sams | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 126 | if (!vtx->isUserProgram()) { |
Jason Sams | 79f52df | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 127 | glBindAttribLocation(pgm, 0, "ATTRIB_position"); |
| 128 | glBindAttribLocation(pgm, 1, "ATTRIB_color"); |
| 129 | glBindAttribLocation(pgm, 2, "ATTRIB_normal"); |
Jason Sams | 479e292 | 2010-07-09 15:34:32 -0700 | [diff] [blame] | 130 | glBindAttribLocation(pgm, 3, "ATTRIB_texture0"); |
Jason Sams | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 131 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 132 | |
| 133 | //LOGE("e2 %x", glGetError()); |
| 134 | glLinkProgram(pgm); |
| 135 | //LOGE("e3 %x", glGetError()); |
| 136 | GLint linkStatus = GL_FALSE; |
| 137 | glGetProgramiv(pgm, GL_LINK_STATUS, &linkStatus); |
| 138 | if (linkStatus != GL_TRUE) { |
| 139 | GLint bufLength = 0; |
| 140 | glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &bufLength); |
| 141 | if (bufLength) { |
| 142 | char* buf = (char*) malloc(bufLength); |
| 143 | if (buf) { |
| 144 | glGetProgramInfoLog(pgm, bufLength, NULL, buf); |
| 145 | LOGE("Could not link program:\n%s\n", buf); |
| 146 | free(buf); |
| 147 | } |
| 148 | } |
| 149 | glDeleteProgram(pgm); |
Jason Sams | 87319de | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 150 | rsc->setError(RS_ERROR_FATAL_PROGRAM_LINK, "Error linking GL Programs"); |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 151 | return false; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 152 | } |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 153 | |
| 154 | for (uint32_t ct=0; ct < e->vtxAttrCount; ct++) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 155 | e->vtxAttrs[ct].slot = glGetAttribLocation(pgm, vtx->getAttribName(ct)); |
| 156 | e->vtxAttrs[ct].name = vtx->getAttribName(ct).string(); |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 157 | if (rsc->props.mLogShaders) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 158 | LOGV("vtx A %i, %s = %d\n", ct, vtx->getAttribName(ct).string(), e->vtxAttrs[ct].slot); |
Jason Sams | cd50653 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 159 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 160 | } |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 161 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 162 | populateUniformData(vtx, pgm, e->vtxUniforms); |
| 163 | populateUniformData(frag, pgm, e->fragUniforms); |
| 164 | |
| 165 | // Only populate this list if we have arrays in our uniforms |
| 166 | UniformQueryData **uniformList = NULL; |
| 167 | GLint numUniforms = 0; |
| 168 | bool hasArrays = hasArrayUniforms(vtx, frag); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 169 | if (hasArrays) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 170 | // Get the number of active uniforms and the length of the longest name |
| 171 | glGetProgramiv(pgm, GL_ACTIVE_UNIFORMS, &numUniforms); |
| 172 | GLint maxNameLength = 0; |
| 173 | glGetProgramiv(pgm, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLength); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 174 | if (numUniforms > 0 && maxNameLength > 0) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 175 | uniformList = new UniformQueryData*[numUniforms]; |
| 176 | // Iterate over all the uniforms and build the list we |
| 177 | // can later use to match our uniforms to |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 178 | for (uint32_t ct = 0; ct < (uint32_t)numUniforms; ct++) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 179 | uniformList[ct] = new UniformQueryData(maxNameLength); |
| 180 | glGetActiveUniform(pgm, ct, maxNameLength, &uniformList[ct]->writtenLength, |
| 181 | &uniformList[ct]->arraySize, &uniformList[ct]->type, |
| 182 | uniformList[ct]->name); |
| 183 | //LOGE("GL UNI idx=%u, arraySize=%u, name=%s", ct, |
| 184 | // uniformList[ct]->arraySize, uniformList[ct]->name); |
| 185 | } |
Jason Sams | cd50653 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 186 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 187 | } |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 188 | |
| 189 | // We now know the highest index of all of the array uniforms |
| 190 | // and we need to update our cache to reflect that |
| 191 | // we may have declared [n], but only m < n elements are used |
| 192 | updateUniformArrayData(rsc, vtx, pgm, e->vtxUniforms, "vtx", |
| 193 | uniformList, (uint32_t)numUniforms); |
| 194 | updateUniformArrayData(rsc, frag, pgm, e->fragUniforms, "frag", |
| 195 | uniformList, (uint32_t)numUniforms); |
| 196 | |
| 197 | // Clean up the uniform data from GL |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 198 | if (uniformList != NULL) { |
| 199 | for (uint32_t ct = 0; ct < (uint32_t)numUniforms; ct++) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 200 | delete uniformList[ct]; |
Jason Sams | cd50653 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 201 | } |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 202 | delete[] uniformList; |
| 203 | uniformList = NULL; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Jason Sams | f2a5d73 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 207 | //LOGV("SC made program %i", e->program); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 208 | glUseProgram(e->program); |
Jason Sams | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 209 | rsc->checkError("ShaderCache::lookup (miss)"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 213 | int32_t ShaderCache::vtxAttribSlot(const String8 &attrName) const { |
| 214 | for (uint32_t ct=0; ct < mCurrent->vtxAttrCount; ct++) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 215 | if (attrName == mCurrent->vtxAttrs[ct].name) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 216 | return mCurrent->vtxAttrs[ct].slot; |
Alex Sakhartchouk | 886f11a | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | return -1; |
| 220 | } |
| 221 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 222 | void ShaderCache::cleanupVertex(uint32_t id) { |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 223 | int32_t numEntries = (int32_t)mEntries.size(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 224 | for (int32_t ct = 0; ct < numEntries; ct ++) { |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 225 | if (mEntries[ct]->vtx == id) { |
| 226 | glDeleteProgram(mEntries[ct]->program); |
| 227 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 228 | delete mEntries[ct]; |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 229 | mEntries.removeAt(ct); |
| 230 | numEntries = (int32_t)mEntries.size(); |
| 231 | ct --; |
| 232 | } |
| 233 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 236 | void ShaderCache::cleanupFragment(uint32_t id) { |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 237 | int32_t numEntries = (int32_t)mEntries.size(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 238 | for (int32_t ct = 0; ct < numEntries; ct ++) { |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 239 | if (mEntries[ct]->frag == id) { |
| 240 | glDeleteProgram(mEntries[ct]->program); |
| 241 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 242 | delete mEntries[ct]; |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 243 | mEntries.removeAt(ct); |
| 244 | numEntries = (int32_t)mEntries.size(); |
| 245 | ct --; |
| 246 | } |
| 247 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 250 | void ShaderCache::cleanupAll() { |
Jason Sams | 2cbfc4c | 2011-01-05 03:37:48 -0800 | [diff] [blame] | 251 | for (uint32_t ct=0; ct < mEntries.size(); ct++) { |
| 252 | glDeleteProgram(mEntries[ct]->program); |
| 253 | free(mEntries[ct]); |
| 254 | } |
| 255 | mEntries.clear(); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 256 | } |
| 257 | |