2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "rsContext.h"
18 #include "rsProgram.h"
20 #include <GLES2/gl2.h>
21 #include <GLES2/gl2ext.h>
23 using namespace android;
24 using namespace android::renderscript;
27 Program::Program(Context *rsc) : ObjectBase(rsc)
29 mAllocFile = __FILE__;
30 mAllocLine = __LINE__;
36 mInputElements = NULL;
37 mOutputElements = NULL;
38 mConstantTypes = NULL;
45 Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength,
46 const uint32_t * params, uint32_t paramLength) :
49 mAllocFile = __FILE__;
50 mAllocLine = __LINE__;
61 for (uint32_t ct=0; ct < paramLength; ct+=2) {
62 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
65 if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) {
68 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
71 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_COUNT) {
72 mTextureCount = params[ct+1];
76 mInputElements = new ObjectBaseRef<Element>[mInputCount];
77 mOutputElements = new ObjectBaseRef<Element>[mOutputCount];
78 mConstantTypes = new ObjectBaseRef<Type>[mConstantCount];
82 uint32_t constant = 0;
83 for (uint32_t ct=0; ct < paramLength; ct+=2) {
84 if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
85 mInputElements[input++].set(reinterpret_cast<Element *>(params[ct+1]));
87 if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) {
88 mOutputElements[output++].set(reinterpret_cast<Element *>(params[ct+1]));
90 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
91 mConstantTypes[constant++].set(reinterpret_cast<Type *>(params[ct+1]));
94 mUserShader.setTo(shaderText, shaderLength);
99 for (uint32_t ct=0; ct < MAX_UNIFORMS; ct++) {
100 bindAllocation(NULL, ct);
103 delete[] mInputElements;
104 delete[] mOutputElements;
105 delete[] mConstantTypes;
112 void Program::bindAllocation(Allocation *alloc, uint32_t slot)
114 if (mConstants[slot].get() == alloc) {
117 if (mConstants[slot].get()) {
118 mConstants[slot].get()->removeProgramToDirty(this);
120 mConstants[slot].set(alloc);
122 alloc->addProgramToDirty(this);
127 void Program::bindTexture(uint32_t slot, Allocation *a)
129 if (slot >= MAX_TEXTURE) {
130 LOGE("Attempt to bind a texture to a slot > MAX_TEXTURE");
134 //LOGE("bindtex %i %p", slot, a);
135 mTextures[slot].set(a);
139 void Program::bindSampler(uint32_t slot, Sampler *s)
141 if (slot >= MAX_TEXTURE) {
142 LOGE("Attempt to bind a Sampler to a slot > MAX_TEXTURE");
146 mSamplers[slot].set(s);
150 String8 Program::getGLSLInputString() const
153 for (uint32_t ct=0; ct < mInputCount; ct++) {
154 const Element *e = mInputElements[ct].get();
155 for (uint32_t field=0; field < e->getFieldCount(); field++) {
156 const Element *f = e->getField(field);
159 rsAssert(!f->getFieldCount());
160 switch(f->getComponent().getVectorSize()) {
161 case 1: s.append("attribute float ATTRIB_"); break;
162 case 2: s.append("attribute vec2 ATTRIB_"); break;
163 case 3: s.append("attribute vec3 ATTRIB_"); break;
164 case 4: s.append("attribute vec4 ATTRIB_"); break;
169 s.append(e->getFieldName(field));
176 String8 Program::getGLSLOutputString() const
181 String8 Program::getGLSLConstantString() const
187 void Program::createShader()
191 bool Program::loadShader(Context *rsc, uint32_t type)
193 mShaderID = glCreateShader(type);
196 if (rsc->props.mLogShaders) {
197 LOGV("Loading shader type %x, ID %i", type, mShaderID);
198 LOGV("%s", mShader.string());
202 const char * ss = mShader.string();
203 glShaderSource(mShaderID, 1, &ss, NULL);
204 glCompileShader(mShaderID);
207 glGetShaderiv(mShaderID, GL_COMPILE_STATUS, &compiled);
210 glGetShaderiv(mShaderID, GL_INFO_LOG_LENGTH, &infoLen);
212 char* buf = (char*) malloc(infoLen);
214 glGetShaderInfoLog(mShaderID, infoLen, NULL, buf);
215 LOGE("Could not compile shader \n%s\n", buf);
218 glDeleteShader(mShaderID);
220 rsc->setError(RS_ERROR_BAD_SHADER, "Error returned from GL driver loading shader text,");
226 if (rsc->props.mLogShaders) {
227 LOGV("--Shader load result %x ", glGetError());
233 void Program::setShader(const char *txt, uint32_t len)
235 mUserShader.setTo(txt, len);
241 namespace renderscript {
244 void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants)
246 Program *p = static_cast<Program *>(vp);
247 p->bindAllocation(static_cast<Allocation *>(constants), slot);
250 void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a)
252 Program *p = static_cast<Program *>(vpf);
253 p->bindTexture(slot, static_cast<Allocation *>(a));
256 void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s)
258 Program *p = static_cast<Program *>(vpf);
259 p->bindSampler(slot, static_cast<Sampler *>(s));