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