blob: 504ffbad0851b4d73af132cf11723d80591577c2 [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");
104 glBindAttribLocation(pgm, 3, "ATTRIB_pointSize");
105 glBindAttribLocation(pgm, 4, "ATTRIB_texture0");
Jason Sams433eca32010-01-06 11:57:52 -0800106 }
Jason Samsc460e552009-11-25 13:22:07 -0800107
108 //LOGE("e2 %x", glGetError());
109 glLinkProgram(pgm);
110 //LOGE("e3 %x", glGetError());
111 GLint linkStatus = GL_FALSE;
112 glGetProgramiv(pgm, GL_LINK_STATUS, &linkStatus);
113 if (linkStatus != GL_TRUE) {
114 GLint bufLength = 0;
115 glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &bufLength);
116 if (bufLength) {
117 char* buf = (char*) malloc(bufLength);
118 if (buf) {
119 glGetProgramInfoLog(pgm, bufLength, NULL, buf);
120 LOGE("Could not link program:\n%s\n", buf);
121 free(buf);
122 }
123 }
124 glDeleteProgram(pgm);
Jason Samsa2cf7552010-03-03 13:03:18 -0800125 rsc->setError(RS_ERROR_BAD_SHADER, "Error linking GL Programs");
126 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800127 }
Jason Samsbe504f22010-01-25 12:31:24 -0800128 if (vtx->isUserProgram()) {
129 for (uint32_t ct=0; ct < vtx->getAttribCount(); ct++) {
130 e->mVtxAttribSlots[ct] = glGetAttribLocation(pgm, vtx->getAttribName(ct));
131 if (rsc->props.mLogShaders) {
132 LOGV("vtx A %i, %s = %d\n", ct, vtx->getAttribName(ct).string(), e->mVtxAttribSlots[ct]);
133 }
Jason Samscd506532009-12-15 19:10:11 -0800134 }
Jason Samsc460e552009-11-25 13:22:07 -0800135 }
136 for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) {
137 e->mVtxUniformSlots[ct] = glGetUniformLocation(pgm, vtx->getUniformName(ct));
Jason Samscd506532009-12-15 19:10:11 -0800138 if (rsc->props.mLogShaders) {
139 LOGV("vtx U, %s = %d\n", vtx->getUniformName(ct).string(), e->mVtxUniformSlots[ct]);
140 }
Jason Samsc460e552009-11-25 13:22:07 -0800141 }
Jason Sams9ebb0c42010-01-12 12:12:28 -0800142 for (uint32_t ct=0; ct < frag->getUniformCount(); ct++) {
Jason Samsc460e552009-11-25 13:22:07 -0800143 e->mFragUniformSlots[ct] = glGetUniformLocation(pgm, frag->getUniformName(ct));
Jason Samscd506532009-12-15 19:10:11 -0800144 if (rsc->props.mLogShaders) {
145 LOGV("frag U, %s = %d\n", frag->getUniformName(ct).string(), e->mFragUniformSlots[ct]);
146 }
Jason Samsc460e552009-11-25 13:22:07 -0800147 }
148 }
149
Jason Samsa2cf7552010-03-03 13:03:18 -0800150 e->mIsValid = true;
Jason Samsf2a5d732009-11-30 14:49:55 -0800151 //LOGV("SC made program %i", e->program);
Jason Samsc460e552009-11-25 13:22:07 -0800152 glUseProgram(e->program);
153 mEntryCount++;
Jason Sams433eca32010-01-06 11:57:52 -0800154 rsc->checkError("ShaderCache::lookup (miss)");
Jason Samsc460e552009-11-25 13:22:07 -0800155 return true;
156}
157
158void ShaderCache::cleanupVertex(uint32_t id)
159{
160}
161
162void ShaderCache::cleanupFragment(uint32_t id)
163{
164}
165
166void ShaderCache::cleanupAll()
167{
168}
169