Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame^] | 17 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 18 | #include "rsContext.h" |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 19 | #include <GLES2/gl2.h> |
| 20 | #include <GLES2/gl2ext.h> |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame^] | 21 | #else |
| 22 | #include "rsContextHostStub.h" |
| 23 | #include <OpenGL/gl.h> |
| 24 | #include <OpenGL/glext.h> |
| 25 | #endif //ANDROID_RS_BUILD_FOR_HOST |
| 26 | |
| 27 | #include "rsProgram.h" |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 28 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | using namespace android; |
| 30 | using namespace android::renderscript; |
| 31 | |
| 32 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 33 | Program::Program(Context *rsc) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 35 | mAllocFile = __FILE__; |
| 36 | mAllocLine = __LINE__; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 37 | mDirty = true; |
| 38 | mShaderID = 0; |
| 39 | mAttribCount = 0; |
| 40 | mUniformCount = 0; |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 41 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 42 | mInputElements = NULL; |
| 43 | mOutputElements = NULL; |
| 44 | mConstantTypes = NULL; |
| 45 | mInputCount = 0; |
| 46 | mOutputCount = 0; |
| 47 | mConstantCount = 0; |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 48 | mIsValid = false; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength, |
| 52 | const uint32_t * params, uint32_t paramLength) : |
| 53 | ObjectBase(rsc) |
| 54 | { |
| 55 | mAllocFile = __FILE__; |
| 56 | mAllocLine = __LINE__; |
| 57 | mDirty = true; |
| 58 | mShaderID = 0; |
| 59 | mAttribCount = 0; |
| 60 | mUniformCount = 0; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 61 | mTextureCount = 0; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 62 | |
| 63 | mInputCount = 0; |
| 64 | mOutputCount = 0; |
| 65 | mConstantCount = 0; |
| 66 | |
| 67 | for (uint32_t ct=0; ct < paramLength; ct+=2) { |
| 68 | if (params[ct] == RS_PROGRAM_PARAM_INPUT) { |
| 69 | mInputCount++; |
| 70 | } |
| 71 | if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) { |
| 72 | mOutputCount++; |
| 73 | } |
| 74 | if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { |
| 75 | mConstantCount++; |
| 76 | } |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 77 | if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_COUNT) { |
| 78 | mTextureCount = params[ct+1]; |
| 79 | } |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | mInputElements = new ObjectBaseRef<Element>[mInputCount]; |
| 83 | mOutputElements = new ObjectBaseRef<Element>[mOutputCount]; |
| 84 | mConstantTypes = new ObjectBaseRef<Type>[mConstantCount]; |
| 85 | |
| 86 | uint32_t input = 0; |
| 87 | uint32_t output = 0; |
| 88 | uint32_t constant = 0; |
| 89 | for (uint32_t ct=0; ct < paramLength; ct+=2) { |
| 90 | if (params[ct] == RS_PROGRAM_PARAM_INPUT) { |
| 91 | mInputElements[input++].set(reinterpret_cast<Element *>(params[ct+1])); |
| 92 | } |
| 93 | if (params[ct] == RS_PROGRAM_PARAM_OUTPUT) { |
| 94 | mOutputElements[output++].set(reinterpret_cast<Element *>(params[ct+1])); |
| 95 | } |
| 96 | if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { |
| 97 | mConstantTypes[constant++].set(reinterpret_cast<Type *>(params[ct+1])); |
| 98 | } |
| 99 | } |
| 100 | mUserShader.setTo(shaderText, shaderLength); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | Program::~Program() |
| 104 | { |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 105 | for (uint32_t ct=0; ct < MAX_UNIFORMS; ct++) { |
| 106 | bindAllocation(NULL, ct); |
| 107 | } |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 108 | |
| 109 | delete[] mInputElements; |
| 110 | delete[] mOutputElements; |
| 111 | delete[] mConstantTypes; |
| 112 | mInputCount = 0; |
| 113 | mOutputCount = 0; |
| 114 | mConstantCount = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 118 | void Program::bindAllocation(Allocation *alloc, uint32_t slot) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 119 | { |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 120 | if (mConstants[slot].get() == alloc) { |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 123 | if (mConstants[slot].get()) { |
| 124 | mConstants[slot].get()->removeProgramToDirty(this); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 125 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 126 | mConstants[slot].set(alloc); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 127 | if (alloc) { |
| 128 | alloc->addProgramToDirty(this); |
| 129 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 130 | mDirty = true; |
| 131 | } |
| 132 | |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 133 | void Program::bindTexture(uint32_t slot, Allocation *a) |
| 134 | { |
| 135 | if (slot >= MAX_TEXTURE) { |
| 136 | LOGE("Attempt to bind a texture to a slot > MAX_TEXTURE"); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | //LOGE("bindtex %i %p", slot, a); |
| 141 | mTextures[slot].set(a); |
| 142 | mDirty = true; |
| 143 | } |
| 144 | |
| 145 | void Program::bindSampler(uint32_t slot, Sampler *s) |
| 146 | { |
| 147 | if (slot >= MAX_TEXTURE) { |
| 148 | LOGE("Attempt to bind a Sampler to a slot > MAX_TEXTURE"); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | mSamplers[slot].set(s); |
| 153 | mDirty = true; |
| 154 | } |
| 155 | |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 156 | String8 Program::getGLSLInputString() const |
| 157 | { |
| 158 | String8 s; |
| 159 | for (uint32_t ct=0; ct < mInputCount; ct++) { |
| 160 | const Element *e = mInputElements[ct].get(); |
| 161 | for (uint32_t field=0; field < e->getFieldCount(); field++) { |
| 162 | const Element *f = e->getField(field); |
| 163 | |
| 164 | // Cannot be complex |
| 165 | rsAssert(!f->getFieldCount()); |
| 166 | switch(f->getComponent().getVectorSize()) { |
| 167 | case 1: s.append("attribute float ATTRIB_"); break; |
| 168 | case 2: s.append("attribute vec2 ATTRIB_"); break; |
| 169 | case 3: s.append("attribute vec3 ATTRIB_"); break; |
| 170 | case 4: s.append("attribute vec4 ATTRIB_"); break; |
| 171 | default: |
| 172 | rsAssert(0); |
| 173 | } |
| 174 | |
| 175 | s.append(e->getFieldName(field)); |
| 176 | s.append(";\n"); |
| 177 | } |
| 178 | } |
| 179 | return s; |
| 180 | } |
| 181 | |
| 182 | String8 Program::getGLSLOutputString() const |
| 183 | { |
| 184 | return String8(); |
| 185 | } |
| 186 | |
| 187 | String8 Program::getGLSLConstantString() const |
| 188 | { |
| 189 | return String8(); |
| 190 | } |
| 191 | |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 192 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 193 | void Program::createShader() |
| 194 | { |
| 195 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 196 | |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 197 | bool Program::loadShader(Context *rsc, uint32_t type) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 198 | { |
| 199 | mShaderID = glCreateShader(type); |
| 200 | rsAssert(mShaderID); |
| 201 | |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 202 | if (rsc->props.mLogShaders) { |
| 203 | LOGV("Loading shader type %x, ID %i", type, mShaderID); |
Nick Kralevich | ce2cbe4 | 2010-05-13 14:46:27 -0700 | [diff] [blame] | 204 | LOGV("%s", mShader.string()); |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 205 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 206 | |
| 207 | if (mShaderID) { |
| 208 | const char * ss = mShader.string(); |
| 209 | glShaderSource(mShaderID, 1, &ss, NULL); |
| 210 | glCompileShader(mShaderID); |
| 211 | |
| 212 | GLint compiled = 0; |
| 213 | glGetShaderiv(mShaderID, GL_COMPILE_STATUS, &compiled); |
| 214 | if (!compiled) { |
| 215 | GLint infoLen = 0; |
| 216 | glGetShaderiv(mShaderID, GL_INFO_LOG_LENGTH, &infoLen); |
| 217 | if (infoLen) { |
| 218 | char* buf = (char*) malloc(infoLen); |
| 219 | if (buf) { |
| 220 | glGetShaderInfoLog(mShaderID, infoLen, NULL, buf); |
| 221 | LOGE("Could not compile shader \n%s\n", buf); |
| 222 | free(buf); |
| 223 | } |
| 224 | glDeleteShader(mShaderID); |
| 225 | mShaderID = 0; |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 226 | rsc->setError(RS_ERROR_BAD_SHADER, "Error returned from GL driver loading shader text,"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | } |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 231 | |
| 232 | if (rsc->props.mLogShaders) { |
| 233 | LOGV("--Shader load result %x ", glGetError()); |
| 234 | } |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 235 | mIsValid = true; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 236 | return true; |
| 237 | } |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 238 | |
| 239 | void Program::setShader(const char *txt, uint32_t len) |
| 240 | { |
| 241 | mUserShader.setTo(txt, len); |
| 242 | } |
| 243 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 244 | |
| 245 | |
| 246 | namespace android { |
| 247 | namespace renderscript { |
| 248 | |
| 249 | |
| 250 | void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) |
| 251 | { |
| 252 | Program *p = static_cast<Program *>(vp); |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 253 | p->bindAllocation(static_cast<Allocation *>(constants), slot); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 256 | void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) |
| 257 | { |
| 258 | Program *p = static_cast<Program *>(vpf); |
| 259 | p->bindTexture(slot, static_cast<Allocation *>(a)); |
| 260 | } |
| 261 | |
| 262 | void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) |
| 263 | { |
| 264 | Program *p = static_cast<Program *>(vpf); |
| 265 | p->bindSampler(slot, static_cast<Sampler *>(s)); |
| 266 | } |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 267 | |
| 268 | } |
| 269 | } |
| 270 | |