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 | |
| 30 | ShaderCache::ShaderCache() |
| 31 | { |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 32 | mEntries.setCapacity(16); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | ShaderCache::~ShaderCache() |
| 36 | { |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 37 | for (uint32_t ct=0; ct < mEntries.size(); ct++) { |
| 38 | glDeleteProgram(mEntries[ct]->program); |
| 39 | free(mEntries[ct]); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 40 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 43 | bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 44 | { |
| 45 | if (!vtx->getShaderID()) { |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 46 | vtx->loadShader(rsc); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 47 | } |
| 48 | if (!frag->getShaderID()) { |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 49 | frag->loadShader(rsc); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 50 | } |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 51 | |
| 52 | // Don't try to cache if shaders failed to load |
| 53 | if(!vtx->getShaderID() || !frag->getShaderID()) { |
| 54 | return false; |
| 55 | } |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 56 | //LOGV("ShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID()); |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 57 | uint32_t entryCount = mEntries.size(); |
| 58 | for(uint32_t ct = 0; ct < entryCount; ct ++) { |
| 59 | if ((mEntries[ct]->vtx == vtx->getShaderID()) && |
| 60 | (mEntries[ct]->frag == frag->getShaderID())) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 61 | |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 62 | //LOGV("SC using program %i", mEntries[ct]->program); |
| 63 | glUseProgram(mEntries[ct]->program); |
| 64 | mCurrent = mEntries[ct]; |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 65 | //LOGV("ShaderCache hit, using %i", ct); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 66 | rsc->checkError("ShaderCache::lookup (hit)"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 70 | |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 71 | //LOGV("ShaderCache miss"); |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 72 | //LOGE("e0 %x", glGetError()); |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 73 | entry_t *e = (entry_t *)malloc(sizeof(entry_t)); |
| 74 | mEntries.push(e); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 75 | mCurrent = e; |
| 76 | e->vtx = vtx->getShaderID(); |
| 77 | e->frag = frag->getShaderID(); |
| 78 | e->program = glCreateProgram(); |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 79 | e->vtxAttrCount = vtx->getAttribCount(); |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 80 | if (e->program) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 81 | GLuint pgm = e->program; |
| 82 | glAttachShader(pgm, vtx->getShaderID()); |
| 83 | //LOGE("e1 %x", glGetError()); |
| 84 | glAttachShader(pgm, frag->getShaderID()); |
| 85 | |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 86 | if (!vtx->isUserProgram()) { |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 87 | glBindAttribLocation(pgm, 0, "ATTRIB_position"); |
| 88 | glBindAttribLocation(pgm, 1, "ATTRIB_color"); |
| 89 | glBindAttribLocation(pgm, 2, "ATTRIB_normal"); |
Jason Sams | 53a93d5 | 2010-07-09 15:34:32 -0700 | [diff] [blame] | 90 | glBindAttribLocation(pgm, 3, "ATTRIB_texture0"); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 91 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 92 | |
| 93 | //LOGE("e2 %x", glGetError()); |
| 94 | glLinkProgram(pgm); |
| 95 | //LOGE("e3 %x", glGetError()); |
| 96 | GLint linkStatus = GL_FALSE; |
| 97 | glGetProgramiv(pgm, GL_LINK_STATUS, &linkStatus); |
| 98 | if (linkStatus != GL_TRUE) { |
| 99 | GLint bufLength = 0; |
| 100 | glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &bufLength); |
| 101 | if (bufLength) { |
| 102 | char* buf = (char*) malloc(bufLength); |
| 103 | if (buf) { |
| 104 | glGetProgramInfoLog(pgm, bufLength, NULL, buf); |
| 105 | LOGE("Could not link program:\n%s\n", buf); |
| 106 | free(buf); |
| 107 | } |
| 108 | } |
| 109 | glDeleteProgram(pgm); |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 110 | rsc->setError(RS_ERROR_BAD_SHADER, "Error linking GL Programs"); |
| 111 | return false; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 112 | } |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 113 | |
| 114 | for (uint32_t ct=0; ct < e->vtxAttrCount; ct++) { |
| 115 | e->mVtxAttribSlots[ct] = glGetAttribLocation(pgm, vtx->getAttribName(ct)); |
| 116 | e->mVtxAttribNames[ct] = vtx->getAttribName(ct).string(); |
| 117 | if (rsc->props.mLogShaders) { |
| 118 | LOGV("vtx A %i, %s = %d\n", ct, vtx->getAttribName(ct).string(), e->mVtxAttribSlots[ct]); |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 119 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 120 | } |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 121 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 122 | for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) { |
| 123 | e->mVtxUniformSlots[ct] = glGetUniformLocation(pgm, vtx->getUniformName(ct)); |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 124 | if (rsc->props.mLogShaders) { |
| 125 | LOGV("vtx U, %s = %d\n", vtx->getUniformName(ct).string(), e->mVtxUniformSlots[ct]); |
| 126 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 127 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 128 | for (uint32_t ct=0; ct < frag->getUniformCount(); ct++) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 129 | e->mFragUniformSlots[ct] = glGetUniformLocation(pgm, frag->getUniformName(ct)); |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 130 | if (rsc->props.mLogShaders) { |
| 131 | LOGV("frag U, %s = %d\n", frag->getUniformName(ct).string(), e->mFragUniformSlots[ct]); |
| 132 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 136 | e->mIsValid = true; |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 137 | //LOGV("SC made program %i", e->program); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 138 | glUseProgram(e->program); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 139 | rsc->checkError("ShaderCache::lookup (miss)"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 143 | int32_t ShaderCache::vtxAttribSlot(const String8 &attrName) const { |
| 144 | for (uint32_t ct=0; ct < mCurrent->vtxAttrCount; ct++) { |
| 145 | if(attrName == mCurrent->mVtxAttribNames[ct]) { |
| 146 | return mCurrent->mVtxAttribSlots[ct]; |
| 147 | } |
| 148 | } |
| 149 | return -1; |
| 150 | } |
| 151 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 152 | void ShaderCache::cleanupVertex(uint32_t id) |
| 153 | { |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 154 | int32_t numEntries = (int32_t)mEntries.size(); |
| 155 | for(int32_t ct = 0; ct < numEntries; ct ++) { |
| 156 | if (mEntries[ct]->vtx == id) { |
| 157 | glDeleteProgram(mEntries[ct]->program); |
| 158 | |
| 159 | free(mEntries[ct]); |
| 160 | mEntries.removeAt(ct); |
| 161 | numEntries = (int32_t)mEntries.size(); |
| 162 | ct --; |
| 163 | } |
| 164 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void ShaderCache::cleanupFragment(uint32_t id) |
| 168 | { |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 169 | int32_t numEntries = (int32_t)mEntries.size(); |
| 170 | for(int32_t ct = 0; ct < numEntries; ct ++) { |
| 171 | if (mEntries[ct]->frag == id) { |
| 172 | glDeleteProgram(mEntries[ct]->program); |
| 173 | |
| 174 | free(mEntries[ct]); |
| 175 | mEntries.removeAt(ct); |
| 176 | numEntries = (int32_t)mEntries.size(); |
| 177 | ct --; |
| 178 | } |
| 179 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void ShaderCache::cleanupAll() |
| 183 | { |
| 184 | } |
| 185 | |