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