blob: 5c073b390bc70c06ba685b788356effc409ccc91 [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
30ShaderCache::ShaderCache()
31{
32 mEntryCount = 0;
33 mEntryAllocationCount = 16;
34 mEntries = (entry_t *)calloc(mEntryAllocationCount, sizeof(entry_t));
35}
36
37ShaderCache::~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 Samscd506532009-12-15 19:10:11 -080048bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag)
Jason Samsc460e552009-11-25 13:22:07 -080049{
50 if (!vtx->getShaderID()) {
Jason Samscd506532009-12-15 19:10:11 -080051 vtx->loadShader(rsc);
Jason Samsc460e552009-11-25 13:22:07 -080052 }
53 if (!frag->getShaderID()) {
Jason Samscd506532009-12-15 19:10:11 -080054 frag->loadShader(rsc);
Jason Samsc460e552009-11-25 13:22:07 -080055 }
Jason Samsf2a5d732009-11-30 14:49:55 -080056 //LOGV("ShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID());
Jason Samsc460e552009-11-25 13:22:07 -080057
58 for (uint32_t ct=0; ct < mEntryCount; ct++) {
59 if ((mEntries[ct].vtx == vtx->getShaderID()) &&
60 (mEntries[ct].frag == frag->getShaderID())) {
61
62 //LOGV("SC using program %i", mEntries[ct].program);
63 glUseProgram(mEntries[ct].program);
64 mCurrent = &mEntries[ct];
Jason Samsf2a5d732009-11-30 14:49:55 -080065 //LOGV("ShaderCache hit, using %i", ct);
Jason Sams433eca32010-01-06 11:57:52 -080066 rsc->checkError("ShaderCache::lookup (hit)");
Jason Samsc460e552009-11-25 13:22:07 -080067 return true;
68 }
69 }
70 // Not in cache, add it.
71
72 if (mEntryAllocationCount == mEntryCount) {
73 // Out of space, make some.
74 mEntryAllocationCount *= 2;
75 entry_t *e = (entry_t *)calloc(mEntryAllocationCount, sizeof(entry_t));
76 if (!e) {
77 LOGE("Out of memory for ShaderCache::lookup");
78 return false;
79 }
80 memcpy(e, mEntries, sizeof(entry_t) * mEntryCount);
81 free(mEntries);
82 mEntries = e;
83 }
84
Jason Samsf2a5d732009-11-30 14:49:55 -080085 //LOGV("ShaderCache miss, using %i", mEntryCount);
86 //LOGE("e0 %x", glGetError());
Jason Samsc460e552009-11-25 13:22:07 -080087
88 entry_t *e = &mEntries[mEntryCount];
89 mCurrent = e;
90 e->vtx = vtx->getShaderID();
91 e->frag = frag->getShaderID();
92 e->program = glCreateProgram();
Jason Samsbe504f22010-01-25 12:31:24 -080093 e->mUserVertexProgram = vtx->isUserProgram();
Jason Samsc460e552009-11-25 13:22:07 -080094 if (mEntries[mEntryCount].program) {
95 GLuint pgm = e->program;
96 glAttachShader(pgm, vtx->getShaderID());
97 //LOGE("e1 %x", glGetError());
98 glAttachShader(pgm, frag->getShaderID());
99
Jason Sams433eca32010-01-06 11:57:52 -0800100 if (!vtx->isUserProgram()) {
Jason Sams79f52df2010-06-01 15:47:01 -0700101 glBindAttribLocation(pgm, 0, "ATTRIB_position");
102 glBindAttribLocation(pgm, 1, "ATTRIB_color");
103 glBindAttribLocation(pgm, 2, "ATTRIB_normal");
Jason Sams479e2922010-07-09 15:34:32 -0700104 glBindAttribLocation(pgm, 3, "ATTRIB_texture0");
Jason Sams433eca32010-01-06 11:57:52 -0800105 }
Jason Samsc460e552009-11-25 13:22:07 -0800106
107 //LOGE("e2 %x", glGetError());
108 glLinkProgram(pgm);
109 //LOGE("e3 %x", glGetError());
110 GLint linkStatus = GL_FALSE;
111 glGetProgramiv(pgm, GL_LINK_STATUS, &linkStatus);
112 if (linkStatus != GL_TRUE) {
113 GLint bufLength = 0;
114 glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &bufLength);
115 if (bufLength) {
116 char* buf = (char*) malloc(bufLength);
117 if (buf) {
118 glGetProgramInfoLog(pgm, bufLength, NULL, buf);
119 LOGE("Could not link program:\n%s\n", buf);
120 free(buf);
121 }
122 }
123 glDeleteProgram(pgm);
Jason Samsa2cf7552010-03-03 13:03:18 -0800124 rsc->setError(RS_ERROR_BAD_SHADER, "Error linking GL Programs");
125 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800126 }
Jason Samsbe504f22010-01-25 12:31:24 -0800127 if (vtx->isUserProgram()) {
128 for (uint32_t ct=0; ct < vtx->getAttribCount(); ct++) {
129 e->mVtxAttribSlots[ct] = glGetAttribLocation(pgm, vtx->getAttribName(ct));
130 if (rsc->props.mLogShaders) {
131 LOGV("vtx A %i, %s = %d\n", ct, vtx->getAttribName(ct).string(), e->mVtxAttribSlots[ct]);
132 }
Jason Samscd506532009-12-15 19:10:11 -0800133 }
Jason Samsc460e552009-11-25 13:22:07 -0800134 }
135 for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) {
136 e->mVtxUniformSlots[ct] = glGetUniformLocation(pgm, vtx->getUniformName(ct));
Jason Samscd506532009-12-15 19:10:11 -0800137 if (rsc->props.mLogShaders) {
138 LOGV("vtx U, %s = %d\n", vtx->getUniformName(ct).string(), e->mVtxUniformSlots[ct]);
139 }
Jason Samsc460e552009-11-25 13:22:07 -0800140 }
Jason Sams9ebb0c42010-01-12 12:12:28 -0800141 for (uint32_t ct=0; ct < frag->getUniformCount(); ct++) {
Jason Samsc460e552009-11-25 13:22:07 -0800142 e->mFragUniformSlots[ct] = glGetUniformLocation(pgm, frag->getUniformName(ct));
Jason Samscd506532009-12-15 19:10:11 -0800143 if (rsc->props.mLogShaders) {
144 LOGV("frag U, %s = %d\n", frag->getUniformName(ct).string(), e->mFragUniformSlots[ct]);
145 }
Jason Samsc460e552009-11-25 13:22:07 -0800146 }
147 }
148
Jason Samsa2cf7552010-03-03 13:03:18 -0800149 e->mIsValid = true;
Jason Samsf2a5d732009-11-30 14:49:55 -0800150 //LOGV("SC made program %i", e->program);
Jason Samsc460e552009-11-25 13:22:07 -0800151 glUseProgram(e->program);
152 mEntryCount++;
Jason Sams433eca32010-01-06 11:57:52 -0800153 rsc->checkError("ShaderCache::lookup (miss)");
Jason Samsc460e552009-11-25 13:22:07 -0800154 return true;
155}
156
157void ShaderCache::cleanupVertex(uint32_t id)
158{
159}
160
161void ShaderCache::cleanupFragment(uint32_t id)
162{
163}
164
165void ShaderCache::cleanupAll()
166{
167}
168