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 | |
| 44 | bool ShaderCache::lookup(ProgramVertex *vtx, ProgramFragment *frag) |
| 45 | { |
| 46 | if (!vtx->getShaderID()) { |
| 47 | vtx->loadShader(); |
| 48 | } |
| 49 | if (!frag->getShaderID()) { |
| 50 | frag->loadShader(); |
| 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 | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | // Not in cache, add it. |
| 66 | |
| 67 | if (mEntryAllocationCount == mEntryCount) { |
| 68 | // Out of space, make some. |
| 69 | mEntryAllocationCount *= 2; |
| 70 | entry_t *e = (entry_t *)calloc(mEntryAllocationCount, sizeof(entry_t)); |
| 71 | if (!e) { |
| 72 | LOGE("Out of memory for ShaderCache::lookup"); |
| 73 | return false; |
| 74 | } |
| 75 | memcpy(e, mEntries, sizeof(entry_t) * mEntryCount); |
| 76 | free(mEntries); |
| 77 | mEntries = e; |
| 78 | } |
| 79 | |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 80 | //LOGV("ShaderCache miss, using %i", mEntryCount); |
| 81 | //LOGE("e0 %x", glGetError()); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 82 | |
| 83 | entry_t *e = &mEntries[mEntryCount]; |
| 84 | mCurrent = e; |
| 85 | e->vtx = vtx->getShaderID(); |
| 86 | e->frag = frag->getShaderID(); |
| 87 | e->program = glCreateProgram(); |
| 88 | if (mEntries[mEntryCount].program) { |
| 89 | GLuint pgm = e->program; |
| 90 | glAttachShader(pgm, vtx->getShaderID()); |
| 91 | //LOGE("e1 %x", glGetError()); |
| 92 | glAttachShader(pgm, frag->getShaderID()); |
| 93 | |
| 94 | glBindAttribLocation(pgm, VertexArray::POSITION, "attrib_Position"); |
| 95 | glBindAttribLocation(pgm, VertexArray::COLOR, "attrib_Color"); |
| 96 | |
| 97 | |
| 98 | //LOGE("e2 %x", glGetError()); |
| 99 | glLinkProgram(pgm); |
| 100 | //LOGE("e3 %x", glGetError()); |
| 101 | GLint linkStatus = GL_FALSE; |
| 102 | glGetProgramiv(pgm, GL_LINK_STATUS, &linkStatus); |
| 103 | if (linkStatus != GL_TRUE) { |
| 104 | GLint bufLength = 0; |
| 105 | glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &bufLength); |
| 106 | if (bufLength) { |
| 107 | char* buf = (char*) malloc(bufLength); |
| 108 | if (buf) { |
| 109 | glGetProgramInfoLog(pgm, bufLength, NULL, buf); |
| 110 | LOGE("Could not link program:\n%s\n", buf); |
| 111 | free(buf); |
| 112 | } |
| 113 | } |
| 114 | glDeleteProgram(pgm); |
| 115 | } |
| 116 | for (uint32_t ct=0; ct < vtx->getAttribCount(); ct++) { |
| 117 | e->mVtxAttribSlots[ct] = glGetAttribLocation(pgm, vtx->getAttribName(ct)); |
| 118 | LOGV("vtx A, %s = %d\n", vtx->getAttribName(ct).string(), e->mVtxAttribSlots[ct]); |
| 119 | } |
| 120 | for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) { |
| 121 | e->mVtxUniformSlots[ct] = glGetUniformLocation(pgm, vtx->getUniformName(ct)); |
| 122 | LOGV("vtx U, %s = %d\n", vtx->getUniformName(ct).string(), e->mVtxUniformSlots[ct]); |
| 123 | } |
| 124 | for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) { |
| 125 | e->mFragUniformSlots[ct] = glGetUniformLocation(pgm, frag->getUniformName(ct)); |
| 126 | LOGV("frag U, %s = %d\n", frag->getUniformName(ct).string(), e->mFragUniformSlots[ct]); |
| 127 | } |
| 128 | } |
| 129 | |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 130 | //LOGV("SC made program %i", e->program); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 131 | glUseProgram(e->program); |
| 132 | mEntryCount++; |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | void ShaderCache::cleanupVertex(uint32_t id) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | void ShaderCache::cleanupFragment(uint32_t id) |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | void ShaderCache::cleanupAll() |
| 145 | { |
| 146 | } |
| 147 | |