blob: b958021b37a80d378f2b8d92ce0387f92d2483fe [file] [log] [blame]
Jason Samsc460e552009-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 Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsc460e552009-11-25 13:22:07 -080018#include "rsContext.h"
Jason Samsc460e552009-11-25 13:22:07 -080019#include <GLES/gl.h>
20#include <GLES2/gl2.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Samsc460e552009-11-25 13:22:07 -080025
26using namespace android;
27using namespace android::renderscript;
28
29
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080030ShaderCache::ShaderCache() {
Alex Sakhartchouk889fe502010-10-01 10:54:06 -070031 mEntries.setCapacity(16);
Jason Samsc460e552009-11-25 13:22:07 -080032}
33
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080034ShaderCache::~ShaderCache() {
Jason Sams2cbfc4c2011-01-05 03:37:48 -080035 cleanupAll();
Jason Samsc460e552009-11-25 13:22:07 -080036}
37
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080038void ShaderCache::updateUniformArrayData(Context *rsc, Program *prog, uint32_t linkedID,
39 UniformData *data, const char* logTag,
40 UniformQueryData **uniformList, uint32_t uniListSize) {
41
42 for (uint32_t ct=0; ct < prog->getUniformCount(); ct++) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080043 if (data[ct].slot >= 0 && data[ct].arraySize > 1) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080044 //Iterate over the list of active GL uniforms and find highest array index
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080045 for (uint32_t ui = 0; ui < uniListSize; ui ++) {
46 if (prog->getUniformName(ct) == uniformList[ui]->name) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080047 data[ct].arraySize = (uint32_t)uniformList[ui]->arraySize;
48 break;
49 }
50 }
51 }
52
53 if (rsc->props.mLogShaders) {
54 LOGV("%s U, %s = %d, arraySize = %d\n", logTag,
55 prog->getUniformName(ct).string(), data[ct].slot, data[ct].arraySize);
56 }
57 }
58}
59
60void ShaderCache::populateUniformData(Program *prog, uint32_t linkedID, UniformData *data) {
61 for (uint32_t ct=0; ct < prog->getUniformCount(); ct++) {
62 data[ct].slot = glGetUniformLocation(linkedID, prog->getUniformName(ct));
63 data[ct].arraySize = prog->getUniformArraySize(ct);
64 }
65}
66
67bool ShaderCache::hasArrayUniforms(ProgramVertex *vtx, ProgramFragment *frag) {
68 UniformData *data = mCurrent->vtxUniforms;
69 for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080070 if (data[ct].slot >= 0 && data[ct].arraySize > 1) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080071 return true;
72 }
73 }
74 data = mCurrent->fragUniforms;
75 for (uint32_t ct=0; ct < frag->getUniformCount(); ct++) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080076 if (data[ct].slot >= 0 && data[ct].arraySize > 1) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080077 return true;
78 }
79 }
80 return false;
81}
82
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080083bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag) {
Jason Samsc460e552009-11-25 13:22:07 -080084 if (!vtx->getShaderID()) {
Jason Samscd506532009-12-15 19:10:11 -080085 vtx->loadShader(rsc);
Jason Samsc460e552009-11-25 13:22:07 -080086 }
87 if (!frag->getShaderID()) {
Jason Samscd506532009-12-15 19:10:11 -080088 frag->loadShader(rsc);
Jason Samsc460e552009-11-25 13:22:07 -080089 }
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070090
91 // Don't try to cache if shaders failed to load
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080092 if (!vtx->getShaderID() || !frag->getShaderID()) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070093 return false;
94 }
Jason Samsf2a5d732009-11-30 14:49:55 -080095 //LOGV("ShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID());
Alex Sakhartchouk889fe502010-10-01 10:54:06 -070096 uint32_t entryCount = mEntries.size();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080097 for (uint32_t ct = 0; ct < entryCount; ct ++) {
Alex Sakhartchouk889fe502010-10-01 10:54:06 -070098 if ((mEntries[ct]->vtx == vtx->getShaderID()) &&
99 (mEntries[ct]->frag == frag->getShaderID())) {
Jason Samsc460e552009-11-25 13:22:07 -0800100
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700101 //LOGV("SC using program %i", mEntries[ct]->program);
102 glUseProgram(mEntries[ct]->program);
103 mCurrent = mEntries[ct];
Jason Samsf2a5d732009-11-30 14:49:55 -0800104 //LOGV("ShaderCache hit, using %i", ct);
Jason Sams433eca32010-01-06 11:57:52 -0800105 rsc->checkError("ShaderCache::lookup (hit)");
Jason Samsc460e552009-11-25 13:22:07 -0800106 return true;
107 }
108 }
Jason Samsc460e552009-11-25 13:22:07 -0800109
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700110 //LOGV("ShaderCache miss");
Jason Samsf2a5d732009-11-30 14:49:55 -0800111 //LOGE("e0 %x", glGetError());
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800112 ProgramEntry *e = new ProgramEntry(vtx->getAttribCount(),
113 vtx->getUniformCount(),
114 frag->getUniformCount());
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700115 mEntries.push(e);
Jason Samsc460e552009-11-25 13:22:07 -0800116 mCurrent = e;
117 e->vtx = vtx->getShaderID();
118 e->frag = frag->getShaderID();
119 e->program = glCreateProgram();
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700120 if (e->program) {
Jason Samsc460e552009-11-25 13:22:07 -0800121 GLuint pgm = e->program;
122 glAttachShader(pgm, vtx->getShaderID());
123 //LOGE("e1 %x", glGetError());
124 glAttachShader(pgm, frag->getShaderID());
125
Jason Sams433eca32010-01-06 11:57:52 -0800126 if (!vtx->isUserProgram()) {
Jason Sams79f52df2010-06-01 15:47:01 -0700127 glBindAttribLocation(pgm, 0, "ATTRIB_position");
128 glBindAttribLocation(pgm, 1, "ATTRIB_color");
129 glBindAttribLocation(pgm, 2, "ATTRIB_normal");
Jason Sams479e2922010-07-09 15:34:32 -0700130 glBindAttribLocation(pgm, 3, "ATTRIB_texture0");
Jason Sams433eca32010-01-06 11:57:52 -0800131 }
Jason Samsc460e552009-11-25 13:22:07 -0800132
133 //LOGE("e2 %x", glGetError());
134 glLinkProgram(pgm);
135 //LOGE("e3 %x", glGetError());
136 GLint linkStatus = GL_FALSE;
137 glGetProgramiv(pgm, GL_LINK_STATUS, &linkStatus);
138 if (linkStatus != GL_TRUE) {
139 GLint bufLength = 0;
140 glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &bufLength);
141 if (bufLength) {
142 char* buf = (char*) malloc(bufLength);
143 if (buf) {
144 glGetProgramInfoLog(pgm, bufLength, NULL, buf);
145 LOGE("Could not link program:\n%s\n", buf);
146 free(buf);
147 }
148 }
149 glDeleteProgram(pgm);
Jason Sams87319de2010-11-22 16:20:16 -0800150 rsc->setError(RS_ERROR_FATAL_PROGRAM_LINK, "Error linking GL Programs");
Jason Samsa2cf7552010-03-03 13:03:18 -0800151 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800152 }
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700153
154 for (uint32_t ct=0; ct < e->vtxAttrCount; ct++) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800155 e->vtxAttrs[ct].slot = glGetAttribLocation(pgm, vtx->getAttribName(ct));
156 e->vtxAttrs[ct].name = vtx->getAttribName(ct).string();
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700157 if (rsc->props.mLogShaders) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800158 LOGV("vtx A %i, %s = %d\n", ct, vtx->getAttribName(ct).string(), e->vtxAttrs[ct].slot);
Jason Samscd506532009-12-15 19:10:11 -0800159 }
Jason Samsc460e552009-11-25 13:22:07 -0800160 }
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700161
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800162 populateUniformData(vtx, pgm, e->vtxUniforms);
163 populateUniformData(frag, pgm, e->fragUniforms);
164
165 // Only populate this list if we have arrays in our uniforms
166 UniformQueryData **uniformList = NULL;
167 GLint numUniforms = 0;
168 bool hasArrays = hasArrayUniforms(vtx, frag);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800169 if (hasArrays) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800170 // Get the number of active uniforms and the length of the longest name
171 glGetProgramiv(pgm, GL_ACTIVE_UNIFORMS, &numUniforms);
172 GLint maxNameLength = 0;
173 glGetProgramiv(pgm, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLength);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800174 if (numUniforms > 0 && maxNameLength > 0) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800175 uniformList = new UniformQueryData*[numUniforms];
176 // Iterate over all the uniforms and build the list we
177 // can later use to match our uniforms to
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800178 for (uint32_t ct = 0; ct < (uint32_t)numUniforms; ct++) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800179 uniformList[ct] = new UniformQueryData(maxNameLength);
180 glGetActiveUniform(pgm, ct, maxNameLength, &uniformList[ct]->writtenLength,
181 &uniformList[ct]->arraySize, &uniformList[ct]->type,
182 uniformList[ct]->name);
183 //LOGE("GL UNI idx=%u, arraySize=%u, name=%s", ct,
184 // uniformList[ct]->arraySize, uniformList[ct]->name);
185 }
Jason Samscd506532009-12-15 19:10:11 -0800186 }
Jason Samsc460e552009-11-25 13:22:07 -0800187 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800188
189 // We now know the highest index of all of the array uniforms
190 // and we need to update our cache to reflect that
191 // we may have declared [n], but only m < n elements are used
192 updateUniformArrayData(rsc, vtx, pgm, e->vtxUniforms, "vtx",
193 uniformList, (uint32_t)numUniforms);
194 updateUniformArrayData(rsc, frag, pgm, e->fragUniforms, "frag",
195 uniformList, (uint32_t)numUniforms);
196
197 // Clean up the uniform data from GL
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800198 if (uniformList != NULL) {
199 for (uint32_t ct = 0; ct < (uint32_t)numUniforms; ct++) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800200 delete uniformList[ct];
Jason Samscd506532009-12-15 19:10:11 -0800201 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800202 delete[] uniformList;
203 uniformList = NULL;
Jason Samsc460e552009-11-25 13:22:07 -0800204 }
205 }
206
Jason Samsf2a5d732009-11-30 14:49:55 -0800207 //LOGV("SC made program %i", e->program);
Jason Samsc460e552009-11-25 13:22:07 -0800208 glUseProgram(e->program);
Jason Sams433eca32010-01-06 11:57:52 -0800209 rsc->checkError("ShaderCache::lookup (miss)");
Jason Samsc460e552009-11-25 13:22:07 -0800210 return true;
211}
212
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700213int32_t ShaderCache::vtxAttribSlot(const String8 &attrName) const {
214 for (uint32_t ct=0; ct < mCurrent->vtxAttrCount; ct++) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800215 if (attrName == mCurrent->vtxAttrs[ct].name) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800216 return mCurrent->vtxAttrs[ct].slot;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700217 }
218 }
219 return -1;
220}
221
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800222void ShaderCache::cleanupVertex(uint32_t id) {
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700223 int32_t numEntries = (int32_t)mEntries.size();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800224 for (int32_t ct = 0; ct < numEntries; ct ++) {
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700225 if (mEntries[ct]->vtx == id) {
226 glDeleteProgram(mEntries[ct]->program);
227
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800228 delete mEntries[ct];
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700229 mEntries.removeAt(ct);
230 numEntries = (int32_t)mEntries.size();
231 ct --;
232 }
233 }
Jason Samsc460e552009-11-25 13:22:07 -0800234}
235
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800236void ShaderCache::cleanupFragment(uint32_t id) {
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700237 int32_t numEntries = (int32_t)mEntries.size();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800238 for (int32_t ct = 0; ct < numEntries; ct ++) {
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700239 if (mEntries[ct]->frag == id) {
240 glDeleteProgram(mEntries[ct]->program);
241
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800242 delete mEntries[ct];
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700243 mEntries.removeAt(ct);
244 numEntries = (int32_t)mEntries.size();
245 ct --;
246 }
247 }
Jason Samsc460e552009-11-25 13:22:07 -0800248}
249
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800250void ShaderCache::cleanupAll() {
Jason Sams2cbfc4c2011-01-05 03:37:48 -0800251 for (uint32_t ct=0; ct < mEntries.size(); ct++) {
252 glDeleteProgram(mEntries[ct]->program);
253 free(mEntries[ct]);
254 }
255 mEntries.clear();
Jason Samsc460e552009-11-25 13:22:07 -0800256}
257