2009-11-25 13:22:07 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-05-21 12:53:13 -07:00
|
|
|
#ifndef ANDROID_RS_BUILD_FOR_HOST
|
2009-11-25 13:22:07 -08:00
|
|
|
#include "rsContext.h"
|
|
|
|
#include <GLES/gl.h>
|
|
|
|
#include <GLES2/gl2.h>
|
2010-05-21 12:53:13 -07:00
|
|
|
#else
|
|
|
|
#include "rsContextHostStub.h"
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#endif //ANDROID_RS_BUILD_FOR_HOST
|
2009-11-25 13:22:07 -08:00
|
|
|
|
|
|
|
using namespace android;
|
|
|
|
using namespace android::renderscript;
|
|
|
|
|
|
|
|
|
|
|
|
ShaderCache::ShaderCache()
|
|
|
|
{
|
2010-10-01 10:54:06 -07:00
|
|
|
mEntries.setCapacity(16);
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderCache::~ShaderCache()
|
|
|
|
{
|
2010-10-01 10:54:06 -07:00
|
|
|
for (uint32_t ct=0; ct < mEntries.size(); ct++) {
|
|
|
|
glDeleteProgram(mEntries[ct]->program);
|
|
|
|
free(mEntries[ct]);
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 19:10:11 -08:00
|
|
|
bool ShaderCache::lookup(Context *rsc, ProgramVertex *vtx, ProgramFragment *frag)
|
2009-11-25 13:22:07 -08:00
|
|
|
{
|
|
|
|
if (!vtx->getShaderID()) {
|
2009-12-15 19:10:11 -08:00
|
|
|
vtx->loadShader(rsc);
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
|
|
|
if (!frag->getShaderID()) {
|
2009-12-15 19:10:11 -08:00
|
|
|
frag->loadShader(rsc);
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
2010-09-29 09:49:13 -07:00
|
|
|
|
|
|
|
// Don't try to cache if shaders failed to load
|
|
|
|
if(!vtx->getShaderID() || !frag->getShaderID()) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-30 14:49:55 -08:00
|
|
|
//LOGV("ShaderCache lookup vtx %i, frag %i", vtx->getShaderID(), frag->getShaderID());
|
2010-10-01 10:54:06 -07:00
|
|
|
uint32_t entryCount = mEntries.size();
|
|
|
|
for(uint32_t ct = 0; ct < entryCount; ct ++) {
|
|
|
|
if ((mEntries[ct]->vtx == vtx->getShaderID()) &&
|
|
|
|
(mEntries[ct]->frag == frag->getShaderID())) {
|
|
|
|
|
|
|
|
//LOGV("SC using program %i", mEntries[ct]->program);
|
|
|
|
glUseProgram(mEntries[ct]->program);
|
|
|
|
mCurrent = mEntries[ct];
|
2009-11-30 14:49:55 -08:00
|
|
|
//LOGV("ShaderCache hit, using %i", ct);
|
2010-01-06 11:57:52 -08:00
|
|
|
rsc->checkError("ShaderCache::lookup (hit)");
|
2009-11-25 13:22:07 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-01 10:54:06 -07:00
|
|
|
//LOGV("ShaderCache miss");
|
2009-11-30 14:49:55 -08:00
|
|
|
//LOGE("e0 %x", glGetError());
|
2010-10-01 10:54:06 -07:00
|
|
|
entry_t *e = (entry_t *)malloc(sizeof(entry_t));
|
|
|
|
mEntries.push(e);
|
2009-11-25 13:22:07 -08:00
|
|
|
mCurrent = e;
|
|
|
|
e->vtx = vtx->getShaderID();
|
|
|
|
e->frag = frag->getShaderID();
|
|
|
|
e->program = glCreateProgram();
|
2010-09-29 09:49:13 -07:00
|
|
|
e->vtxAttrCount = vtx->getAttribCount();
|
2010-10-01 10:54:06 -07:00
|
|
|
if (e->program) {
|
2009-11-25 13:22:07 -08:00
|
|
|
GLuint pgm = e->program;
|
|
|
|
glAttachShader(pgm, vtx->getShaderID());
|
|
|
|
//LOGE("e1 %x", glGetError());
|
|
|
|
glAttachShader(pgm, frag->getShaderID());
|
|
|
|
|
2010-01-06 11:57:52 -08:00
|
|
|
if (!vtx->isUserProgram()) {
|
2010-06-01 15:47:01 -07:00
|
|
|
glBindAttribLocation(pgm, 0, "ATTRIB_position");
|
|
|
|
glBindAttribLocation(pgm, 1, "ATTRIB_color");
|
|
|
|
glBindAttribLocation(pgm, 2, "ATTRIB_normal");
|
2010-07-09 15:34:32 -07:00
|
|
|
glBindAttribLocation(pgm, 3, "ATTRIB_texture0");
|
2010-01-06 11:57:52 -08:00
|
|
|
}
|
2009-11-25 13:22:07 -08:00
|
|
|
|
|
|
|
//LOGE("e2 %x", glGetError());
|
|
|
|
glLinkProgram(pgm);
|
|
|
|
//LOGE("e3 %x", glGetError());
|
|
|
|
GLint linkStatus = GL_FALSE;
|
|
|
|
glGetProgramiv(pgm, GL_LINK_STATUS, &linkStatus);
|
|
|
|
if (linkStatus != GL_TRUE) {
|
|
|
|
GLint bufLength = 0;
|
|
|
|
glGetProgramiv(pgm, GL_INFO_LOG_LENGTH, &bufLength);
|
|
|
|
if (bufLength) {
|
|
|
|
char* buf = (char*) malloc(bufLength);
|
|
|
|
if (buf) {
|
|
|
|
glGetProgramInfoLog(pgm, bufLength, NULL, buf);
|
|
|
|
LOGE("Could not link program:\n%s\n", buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glDeleteProgram(pgm);
|
2010-03-03 13:03:18 -08:00
|
|
|
rsc->setError(RS_ERROR_BAD_SHADER, "Error linking GL Programs");
|
|
|
|
return false;
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
2010-09-29 09:49:13 -07:00
|
|
|
|
|
|
|
for (uint32_t ct=0; ct < e->vtxAttrCount; ct++) {
|
|
|
|
e->mVtxAttribSlots[ct] = glGetAttribLocation(pgm, vtx->getAttribName(ct));
|
|
|
|
e->mVtxAttribNames[ct] = vtx->getAttribName(ct).string();
|
|
|
|
if (rsc->props.mLogShaders) {
|
|
|
|
LOGV("vtx A %i, %s = %d\n", ct, vtx->getAttribName(ct).string(), e->mVtxAttribSlots[ct]);
|
2009-12-15 19:10:11 -08:00
|
|
|
}
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
2010-09-29 09:49:13 -07:00
|
|
|
|
2009-11-25 13:22:07 -08:00
|
|
|
for (uint32_t ct=0; ct < vtx->getUniformCount(); ct++) {
|
|
|
|
e->mVtxUniformSlots[ct] = glGetUniformLocation(pgm, vtx->getUniformName(ct));
|
2009-12-15 19:10:11 -08:00
|
|
|
if (rsc->props.mLogShaders) {
|
|
|
|
LOGV("vtx U, %s = %d\n", vtx->getUniformName(ct).string(), e->mVtxUniformSlots[ct]);
|
|
|
|
}
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
2010-01-12 12:12:28 -08:00
|
|
|
for (uint32_t ct=0; ct < frag->getUniformCount(); ct++) {
|
2009-11-25 13:22:07 -08:00
|
|
|
e->mFragUniformSlots[ct] = glGetUniformLocation(pgm, frag->getUniformName(ct));
|
2009-12-15 19:10:11 -08:00
|
|
|
if (rsc->props.mLogShaders) {
|
|
|
|
LOGV("frag U, %s = %d\n", frag->getUniformName(ct).string(), e->mFragUniformSlots[ct]);
|
|
|
|
}
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-03 13:03:18 -08:00
|
|
|
e->mIsValid = true;
|
2009-11-30 14:49:55 -08:00
|
|
|
//LOGV("SC made program %i", e->program);
|
2009-11-25 13:22:07 -08:00
|
|
|
glUseProgram(e->program);
|
2010-01-06 11:57:52 -08:00
|
|
|
rsc->checkError("ShaderCache::lookup (miss)");
|
2009-11-25 13:22:07 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-29 09:49:13 -07:00
|
|
|
int32_t ShaderCache::vtxAttribSlot(const String8 &attrName) const {
|
|
|
|
for (uint32_t ct=0; ct < mCurrent->vtxAttrCount; ct++) {
|
|
|
|
if(attrName == mCurrent->mVtxAttribNames[ct]) {
|
|
|
|
return mCurrent->mVtxAttribSlots[ct];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-11-25 13:22:07 -08:00
|
|
|
void ShaderCache::cleanupVertex(uint32_t id)
|
|
|
|
{
|
2010-10-01 10:54:06 -07:00
|
|
|
int32_t numEntries = (int32_t)mEntries.size();
|
|
|
|
for(int32_t ct = 0; ct < numEntries; ct ++) {
|
|
|
|
if (mEntries[ct]->vtx == id) {
|
|
|
|
glDeleteProgram(mEntries[ct]->program);
|
|
|
|
|
|
|
|
free(mEntries[ct]);
|
|
|
|
mEntries.removeAt(ct);
|
|
|
|
numEntries = (int32_t)mEntries.size();
|
|
|
|
ct --;
|
|
|
|
}
|
|
|
|
}
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderCache::cleanupFragment(uint32_t id)
|
|
|
|
{
|
2010-10-01 10:54:06 -07:00
|
|
|
int32_t numEntries = (int32_t)mEntries.size();
|
|
|
|
for(int32_t ct = 0; ct < numEntries; ct ++) {
|
|
|
|
if (mEntries[ct]->frag == id) {
|
|
|
|
glDeleteProgram(mEntries[ct]->program);
|
|
|
|
|
|
|
|
free(mEntries[ct]);
|
|
|
|
mEntries.removeAt(ct);
|
|
|
|
numEntries = (int32_t)mEntries.size();
|
|
|
|
ct --;
|
|
|
|
}
|
|
|
|
}
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderCache::cleanupAll()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|