blob: 45384c98f2474d3bf329918b997f128b18e254f8 [file] [log] [blame]
Jason Samsbb51c402009-11-25 13:22:07 -08001/*
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 Sakhartchoukaa7d2882010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsbb51c402009-11-25 13:22:07 -080018#include "rsContext.h"
Jason Samsbb51c402009-11-25 13:22:07 -080019#include <GLES/gl.h>
20#include <GLES2/gl2.h>
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Samsbb51c402009-11-25 13:22:07 -080025
26using namespace android;
27using namespace android::renderscript;
28
29
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -080030ShaderCache::ShaderCache() {
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -070031 mEntries.setCapacity(16);
Jason Samsbb51c402009-11-25 13:22:07 -080032}
33
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -080034ShaderCache::~ShaderCache() {
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -070035 for (uint32_t ct=0; ct < mEntries.size(); ct++) {
36 glDeleteProgram(mEntries[ct]->program);
37 free(mEntries[ct]);
Jason Samsbb51c402009-11-25 13:22:07 -080038 }
Jason Samsbb51c402009-11-25 13:22:07 -080039}
40
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -080041void 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 Sakhartchouked9f2102010-11-09 17:00:54 -080046 if (data[ct].slot >= 0 && data[ct].arraySize > 1) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -080047 //Iterate over the list of active GL uniforms and find highest array index
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080048 for (uint32_t ui = 0; ui < uniListSize; ui ++) {
49 if (prog->getUniformName(ct) == uniformList[ui]->name) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -080050 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
63void 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
70bool ShaderCache::hasArrayUniforms(ProgramVertex *vtx, ProgramFragment *frag) {
71 UniformData *data = mCurrent->vtxUniforms;
72 for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080073 if (data[ct].slot >= 0 && data[ct].arraySize > 1) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -080074 return true;
75 }
76 }
77 data = mCurrent->fragUniforms;
78 for (uint32_t ct=0; ct < frag->getUniformCount(); ct++) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080079 if (data[ct].slot >= 0 && data[ct].arraySize > 1) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -080080 return true;
81 }
82 }
83 return false;
84}
85
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080086bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag) {
Jason Samsbb51c402009-11-25 13:22:07 -080087 if (!vtx->getShaderID()) {
Jason Sams5dad8b42009-12-15 19:10:11 -080088 vtx->loadShader(rsc);
Jason Samsbb51c402009-11-25 13:22:07 -080089 }
90 if (!frag->getShaderID()) {
Jason Sams5dad8b42009-12-15 19:10:11 -080091 frag->loadShader(rsc);
Jason Samsbb51c402009-11-25 13:22:07 -080092 }
Alex Sakhartchouk4378f112010-09-29 09:49:13 -070093
94 // Don't try to cache if shaders failed to load
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080095 if (!vtx->getShaderID() || !frag->getShaderID()) {
Alex Sakhartchouk4378f112010-09-29 09:49:13 -070096 return false;
97 }
Jason Sams54c0ec12009-11-30 14:49:55 -080098 //LOGV("ShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID());
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -070099 uint32_t entryCount = mEntries.size();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800100 for (uint32_t ct = 0; ct < entryCount; ct ++) {
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700101 if ((mEntries[ct]->vtx == vtx->getShaderID()) &&
102 (mEntries[ct]->frag == frag->getShaderID())) {
Jason Samsbb51c402009-11-25 13:22:07 -0800103
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700104 //LOGV("SC using program %i", mEntries[ct]->program);
105 glUseProgram(mEntries[ct]->program);
106 mCurrent = mEntries[ct];
Jason Sams54c0ec12009-11-30 14:49:55 -0800107 //LOGV("ShaderCache hit, using %i", ct);
Jason Samsa09a6e12010-01-06 11:57:52 -0800108 rsc->checkError("ShaderCache::lookup (hit)");
Jason Samsbb51c402009-11-25 13:22:07 -0800109 return true;
110 }
111 }
Jason Samsbb51c402009-11-25 13:22:07 -0800112
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700113 //LOGV("ShaderCache miss");
Jason Sams54c0ec12009-11-30 14:49:55 -0800114 //LOGE("e0 %x", glGetError());
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800115 ProgramEntry *e = new ProgramEntry(vtx->getAttribCount(),
116 vtx->getUniformCount(),
117 frag->getUniformCount());
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700118 mEntries.push(e);
Jason Samsbb51c402009-11-25 13:22:07 -0800119 mCurrent = e;
120 e->vtx = vtx->getShaderID();
121 e->frag = frag->getShaderID();
122 e->program = glCreateProgram();
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700123 if (e->program) {
Jason Samsbb51c402009-11-25 13:22:07 -0800124 GLuint pgm = e->program;
125 glAttachShader(pgm, vtx->getShaderID());
126 //LOGE("e1 %x", glGetError());
127 glAttachShader(pgm, frag->getShaderID());
128
Jason Samsa09a6e12010-01-06 11:57:52 -0800129 if (!vtx->isUserProgram()) {
Jason Sams8cb39de2010-06-01 15:47:01 -0700130 glBindAttribLocation(pgm, 0, "ATTRIB_position");
131 glBindAttribLocation(pgm, 1, "ATTRIB_color");
132 glBindAttribLocation(pgm, 2, "ATTRIB_normal");
Jason Sams53a93d52010-07-09 15:34:32 -0700133 glBindAttribLocation(pgm, 3, "ATTRIB_texture0");
Jason Samsa09a6e12010-01-06 11:57:52 -0800134 }
Jason Samsbb51c402009-11-25 13:22:07 -0800135
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 Samsadd9d962010-11-22 16:20:16 -0800153 rsc->setError(RS_ERROR_FATAL_PROGRAM_LINK, "Error linking GL Programs");
Jason Sams156cce62010-03-03 13:03:18 -0800154 return false;
Jason Samsbb51c402009-11-25 13:22:07 -0800155 }
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700156
157 for (uint32_t ct=0; ct < e->vtxAttrCount; ct++) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800158 e->vtxAttrs[ct].slot = glGetAttribLocation(pgm, vtx->getAttribName(ct));
159 e->vtxAttrs[ct].name = vtx->getAttribName(ct).string();
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700160 if (rsc->props.mLogShaders) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800161 LOGV("vtx A %i, %s = %d\n", ct, vtx->getAttribName(ct).string(), e->vtxAttrs[ct].slot);
Jason Sams5dad8b42009-12-15 19:10:11 -0800162 }
Jason Samsbb51c402009-11-25 13:22:07 -0800163 }
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700164
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800165 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 Sakhartchouked9f2102010-11-09 17:00:54 -0800172 if (hasArrays) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800173 // 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 Sakhartchouked9f2102010-11-09 17:00:54 -0800177 if (numUniforms > 0 && maxNameLength > 0) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800178 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 Sakhartchouked9f2102010-11-09 17:00:54 -0800181 for (uint32_t ct = 0; ct < (uint32_t)numUniforms; ct++) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800182 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 Sams5dad8b42009-12-15 19:10:11 -0800189 }
Jason Samsbb51c402009-11-25 13:22:07 -0800190 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800191
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 Sakhartchouked9f2102010-11-09 17:00:54 -0800201 if (uniformList != NULL) {
202 for (uint32_t ct = 0; ct < (uint32_t)numUniforms; ct++) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800203 delete uniformList[ct];
Jason Sams5dad8b42009-12-15 19:10:11 -0800204 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800205 delete[] uniformList;
206 uniformList = NULL;
Jason Samsbb51c402009-11-25 13:22:07 -0800207 }
208 }
209
Jason Sams54c0ec12009-11-30 14:49:55 -0800210 //LOGV("SC made program %i", e->program);
Jason Samsbb51c402009-11-25 13:22:07 -0800211 glUseProgram(e->program);
Jason Samsa09a6e12010-01-06 11:57:52 -0800212 rsc->checkError("ShaderCache::lookup (miss)");
Jason Samsbb51c402009-11-25 13:22:07 -0800213 return true;
214}
215
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700216int32_t ShaderCache::vtxAttribSlot(const String8 &attrName) const {
217 for (uint32_t ct=0; ct < mCurrent->vtxAttrCount; ct++) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800218 if (attrName == mCurrent->vtxAttrs[ct].name) {
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800219 return mCurrent->vtxAttrs[ct].slot;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700220 }
221 }
222 return -1;
223}
224
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800225void ShaderCache::cleanupVertex(uint32_t id) {
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700226 int32_t numEntries = (int32_t)mEntries.size();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800227 for (int32_t ct = 0; ct < numEntries; ct ++) {
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700228 if (mEntries[ct]->vtx == id) {
229 glDeleteProgram(mEntries[ct]->program);
230
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800231 delete mEntries[ct];
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700232 mEntries.removeAt(ct);
233 numEntries = (int32_t)mEntries.size();
234 ct --;
235 }
236 }
Jason Samsbb51c402009-11-25 13:22:07 -0800237}
238
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800239void ShaderCache::cleanupFragment(uint32_t id) {
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700240 int32_t numEntries = (int32_t)mEntries.size();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800241 for (int32_t ct = 0; ct < numEntries; ct ++) {
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700242 if (mEntries[ct]->frag == id) {
243 glDeleteProgram(mEntries[ct]->program);
244
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800245 delete mEntries[ct];
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700246 mEntries.removeAt(ct);
247 numEntries = (int32_t)mEntries.size();
248 ct --;
249 }
250 }
Jason Samsbb51c402009-11-25 13:22:07 -0800251}
252
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800253void ShaderCache::cleanupAll() {
Jason Samsbb51c402009-11-25 13:22:07 -0800254}
255