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 | |
| 17 | #include "rsContext.h" |
| 18 | |
| 19 | #include <GLES/gl.h> |
| 20 | #include <GLES2/gl2.h> |
| 21 | |
| 22 | using namespace android; |
| 23 | using namespace android::renderscript; |
| 24 | |
| 25 | |
| 26 | ShaderCache::ShaderCache() |
| 27 | { |
| 28 | mEntryCount = 0; |
| 29 | mEntryAllocationCount = 16; |
| 30 | mEntries = (entry_t *)calloc(mEntryAllocationCount, sizeof(entry_t)); |
| 31 | } |
| 32 | |
| 33 | ShaderCache::~ShaderCache() |
| 34 | { |
| 35 | for (uint32_t ct=0; ct < mEntryCount; ct++) { |
| 36 | glDeleteProgram(mEntries[ct].program); |
| 37 | } |
| 38 | |
| 39 | mEntryCount = 0; |
| 40 | mEntryAllocationCount = 0; |
| 41 | free(mEntries); |
| 42 | } |
| 43 | |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 44 | bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 45 | { |
| 46 | if (!vtx->getShaderID()) { |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 47 | vtx->loadShader(rsc); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 48 | } |
| 49 | if (!frag->getShaderID()) { |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 50 | frag->loadShader(rsc); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 51 | } |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 52 | //LOGV("ShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID()); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 53 | |
| 54 | for (uint32_t ct=0; ct < mEntryCount; ct++) { |
| 55 | if ((mEntries[ct].vtx == vtx->getShaderID()) && |
| 56 | (mEntries[ct].frag == frag->getShaderID())) { |
| 57 | |
| 58 | //LOGV("SC using program %i", mEntries[ct].program); |
| 59 | glUseProgram(mEntries[ct].program); |
| 60 | mCurrent = &mEntries[ct]; |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 61 | //LOGV("ShaderCache hit, using %i", ct); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 62 | rsc->checkError("ShaderCache::lookup (hit)"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | } |
| 66 | // Not in cache, add it. |
| 67 | |
| 68 | if (mEntryAllocationCount == mEntryCount) { |
| 69 | // Out of space, make some. |
| 70 | mEntryAllocationCount *= 2; |
| 71 | entry_t *e = (entry_t *)calloc(mEntryAllocationCount, sizeof(entry_t)); |
| 72 | if (!e) { |
| 73 | LOGE("Out of memory for ShaderCache::lookup"); |
| 74 | return false; |
| 75 | } |
| 76 | memcpy(e, mEntries, sizeof(entry_t) * mEntryCount); |
| 77 | free(mEntries); |
| 78 | mEntries = e; |
| 79 | } |
| 80 | |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 81 | //LOGV("ShaderCache miss, using %i", mEntryCount); |
| 82 | //LOGE("e0 %x", glGetError()); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 83 | |
| 84 | entry_t *e = &mEntries[mEntryCount]; |
| 85 | mCurrent = e; |
| 86 | e->vtx = vtx->getShaderID(); |
| 87 | e->frag = frag->getShaderID(); |
| 88 | e->program = glCreateProgram(); |
| 89 | if (mEntries[mEntryCount].program) { |
| 90 | GLuint pgm = e->program; |
| 91 | glAttachShader(pgm, vtx->getShaderID()); |
| 92 | //LOGE("e1 %x", glGetError()); |
| 93 | glAttachShader(pgm, frag->getShaderID()); |
| 94 | |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 95 | if (!vtx->isUserProgram()) { |
| 96 | glBindAttribLocation(pgm, VertexArray::POSITION, "ATTRIB_Position"); |
| 97 | glBindAttribLocation(pgm, VertexArray::COLOR, "ATTRIB_Color"); |
| 98 | glBindAttribLocation(pgm, VertexArray::NORMAL, "ATTRIB_Normal"); |
| 99 | glBindAttribLocation(pgm, VertexArray::POINT_SIZE, "ATTRIB_PointSize"); |
| 100 | glBindAttribLocation(pgm, VertexArray::TEXTURE, "ATTRIB_T0"); |
| 101 | } else { |
| 102 | |
| 103 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 104 | |
| 105 | //LOGE("e2 %x", glGetError()); |
| 106 | glLinkProgram(pgm); |
| 107 | //LOGE("e3 %x", glGetError()); |
| 108 | GLint linkStatus = GL_FALSE; |
| 109 | glGetProgramiv(pgm, GL_LINK_STATUS, &linkStatus); |
| 110 | if (linkStatus != GL_TRUE) { |
| 111 | GLint bufLength = 0; |
| 112 | glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &bufLength); |
| 113 | if (bufLength) { |
| 114 | char* buf = (char*) malloc(bufLength); |
| 115 | if (buf) { |
| 116 | glGetProgramInfoLog(pgm, bufLength, NULL, buf); |
| 117 | LOGE("Could not link program:\n%s\n", buf); |
| 118 | free(buf); |
| 119 | } |
| 120 | } |
| 121 | glDeleteProgram(pgm); |
| 122 | } |
| 123 | for (uint32_t ct=0; ct < vtx->getAttribCount(); ct++) { |
| 124 | e->mVtxAttribSlots[ct] = glGetAttribLocation(pgm, vtx->getAttribName(ct)); |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 125 | if (rsc->props.mLogShaders) { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 126 | 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] | 127 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 128 | } |
| 129 | for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) { |
| 130 | e->mVtxUniformSlots[ct] = glGetUniformLocation(pgm, vtx->getUniformName(ct)); |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 131 | if (rsc->props.mLogShaders) { |
| 132 | LOGV("vtx U, %s = %d\n", vtx->getUniformName(ct).string(), e->mVtxUniformSlots[ct]); |
| 133 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 134 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame^] | 135 | for (uint32_t ct=0; ct < frag->getUniformCount(); ct++) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 136 | e->mFragUniformSlots[ct] = glGetUniformLocation(pgm, frag->getUniformName(ct)); |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 137 | if (rsc->props.mLogShaders) { |
| 138 | LOGV("frag U, %s = %d\n", frag->getUniformName(ct).string(), e->mFragUniformSlots[ct]); |
| 139 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 143 | //LOGV("SC made program %i", e->program); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 144 | glUseProgram(e->program); |
| 145 | mEntryCount++; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 146 | rsc->checkError("ShaderCache::lookup (miss)"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
| 150 | void ShaderCache::cleanupVertex(uint32_t id) |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | void ShaderCache::cleanupFragment(uint32_t id) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | void ShaderCache::cleanupAll() |
| 159 | { |
| 160 | } |
| 161 | |