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