Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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" |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 18 | #include "rsProgram.h" |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 19 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
Alex Sakhartchouk | 748eb07 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 23 | Program::Program(Context *rsc, const char * shaderText, size_t shaderLength, |
| 24 | const uint32_t * params, size_t paramLength) |
Alex Sakhartchouk | 7f126c7 | 2011-05-05 16:56:27 -0700 | [diff] [blame] | 25 | : ProgramBase(rsc) { |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 26 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 27 | initMemberVars(); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 28 | for (uint32_t ct=0; ct < paramLength; ct+=2) { |
| 29 | if (params[ct] == RS_PROGRAM_PARAM_INPUT) { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 30 | mHal.state.inputElementsCount++; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 31 | } |
| 32 | if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 33 | mHal.state.constantsCount++; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 34 | } |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 35 | if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 36 | mHal.state.texturesCount++; |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 37 | } |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 40 | mTextures = new ObjectBaseRef<Allocation>[mHal.state.texturesCount]; |
| 41 | mSamplers = new ObjectBaseRef<Sampler>[mHal.state.texturesCount]; |
| 42 | mInputElements = new ObjectBaseRef<Element>[mHal.state.inputElementsCount]; |
| 43 | mConstantTypes = new ObjectBaseRef<Type>[mHal.state.constantsCount]; |
| 44 | mConstants = new ObjectBaseRef<Allocation>[mHal.state.constantsCount]; |
| 45 | |
| 46 | mHal.state.textures = new Allocation*[mHal.state.texturesCount]; |
| 47 | mHal.state.samplers = new Sampler*[mHal.state.texturesCount]; |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 48 | mHal.state.textureTargets = new RsTextureTarget[mHal.state.texturesCount]; |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 49 | mHal.state.inputElements = new Element*[mHal.state.inputElementsCount]; |
| 50 | mHal.state.constantTypes = new Type*[mHal.state.constantsCount]; |
| 51 | mHal.state.constants = new Allocation*[mHal.state.constantsCount]; |
| 52 | |
| 53 | // Will initialize everything |
| 54 | freeChildren(); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 55 | |
| 56 | uint32_t input = 0; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 57 | uint32_t constant = 0; |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 58 | uint32_t texture = 0; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 59 | for (uint32_t ct=0; ct < paramLength; ct+=2) { |
| 60 | if (params[ct] == RS_PROGRAM_PARAM_INPUT) { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 61 | mInputElements[input].set(reinterpret_cast<Element *>(params[ct+1])); |
| 62 | mHal.state.inputElements[input++] = reinterpret_cast<Element *>(params[ct+1]); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 63 | } |
| 64 | if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 65 | mConstantTypes[constant].set(reinterpret_cast<Type *>(params[ct+1])); |
| 66 | mHal.state.constantTypes[constant++] = reinterpret_cast<Type *>(params[ct+1]); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 67 | } |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 68 | if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 69 | mHal.state.textureTargets[texture++] = (RsTextureTarget)params[ct+1]; |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 70 | } |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 71 | } |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 72 | mIsInternal = false; |
| 73 | uint32_t internalTokenLen = strlen(RS_SHADER_INTERNAL); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 74 | if (shaderLength > internalTokenLen && |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 75 | strncmp(RS_SHADER_INTERNAL, shaderText, internalTokenLen) == 0) { |
| 76 | mIsInternal = true; |
| 77 | shaderText += internalTokenLen; |
| 78 | shaderLength -= internalTokenLen; |
| 79 | } |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 80 | mUserShader.setTo(shaderText, shaderLength); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 83 | Program::~Program() { |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 84 | freeChildren(); |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 85 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 86 | delete[] mTextures; |
| 87 | delete[] mSamplers; |
| 88 | delete[] mInputElements; |
| 89 | delete[] mConstantTypes; |
| 90 | delete[] mConstants; |
| 91 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 92 | delete[] mHal.state.textures; |
| 93 | delete[] mHal.state.samplers; |
| 94 | delete[] mHal.state.textureTargets; |
| 95 | delete[] mHal.state.inputElements; |
| 96 | delete[] mHal.state.constantTypes; |
| 97 | delete[] mHal.state.constants; |
| 98 | mHal.state.inputElementsCount = 0; |
| 99 | mHal.state.constantsCount = 0; |
| 100 | mHal.state.texturesCount = 0; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 103 | bool Program::freeChildren() { |
| 104 | for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) { |
| 105 | bindAllocation(NULL, NULL, ct); |
| 106 | } |
| 107 | |
| 108 | for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) { |
| 109 | bindTexture(NULL, ct, NULL); |
| 110 | bindSampler(NULL, ct, NULL); |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 115 | void Program::initMemberVars() { |
| 116 | mDirty = true; |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 117 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 118 | mHal.drv = NULL; |
| 119 | mHal.state.textures = NULL; |
| 120 | mHal.state.samplers = NULL; |
| 121 | mHal.state.textureTargets = NULL; |
| 122 | mHal.state.inputElements = NULL; |
| 123 | mHal.state.constantTypes = NULL; |
| 124 | mHal.state.constants = NULL; |
| 125 | |
| 126 | mHal.state.inputElementsCount = 0; |
| 127 | mHal.state.constantsCount = 0; |
| 128 | mHal.state.texturesCount = 0; |
| 129 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 130 | mTextures = NULL; |
| 131 | mSamplers = NULL; |
| 132 | mInputElements = NULL; |
| 133 | mConstantTypes = NULL; |
| 134 | mConstants = NULL; |
| 135 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 136 | mIsInternal = false; |
| 137 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 138 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 139 | void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) { |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 140 | if (alloc != NULL) { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 141 | if (slot >= mHal.state.constantsCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 142 | ALOGE("Attempt to bind alloc at slot %u, on shader id %u, but const count is %u", |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 143 | slot, (uint32_t)this, mHal.state.constantsCount); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 144 | rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation"); |
| 145 | return; |
| 146 | } |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 147 | if (alloc->getType() != mConstantTypes[slot].get()) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 148 | ALOGE("Attempt to bind alloc at slot %u, on shader id %u, but types mismatch", |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 149 | slot, (uint32_t)this); |
| 150 | rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation"); |
| 151 | return; |
| 152 | } |
| 153 | } |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 154 | if (mConstants[slot].get() == alloc) { |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 155 | return; |
| 156 | } |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 157 | if (mConstants[slot].get()) { |
| 158 | mConstants[slot]->removeProgramToDirty(this); |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 159 | } |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 160 | mConstants[slot].set(alloc); |
| 161 | mHal.state.constants[slot] = alloc; |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 162 | if (alloc) { |
| 163 | alloc->addProgramToDirty(this); |
| 164 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 165 | mDirty = true; |
| 166 | } |
| 167 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 168 | void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 169 | if (slot >= mHal.state.texturesCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 170 | ALOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mHal.state.texturesCount); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 171 | rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture"); |
Jason Sams | 7dad9c3 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 175 | if (a && a->getType()->getDimFaces() && mHal.state.textureTargets[slot] != RS_TEXTURE_CUBE) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 176 | ALOGE("Attempt to bind cubemap to slot %u but 2d texture needed", slot); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 177 | rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind cubemap to 2d texture slot"); |
| 178 | return; |
| 179 | } |
| 180 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 181 | mTextures[slot].set(a); |
| 182 | mHal.state.textures[slot] = a; |
| 183 | |
Jason Sams | 7dad9c3 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 184 | mDirty = true; |
| 185 | } |
| 186 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 187 | void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 188 | if (slot >= mHal.state.texturesCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 189 | ALOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mHal.state.texturesCount); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 190 | rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler"); |
Jason Sams | 7dad9c3 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 194 | mSamplers[slot].set(s); |
| 195 | mHal.state.samplers[slot] = s; |
Jason Sams | 7dad9c3 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 196 | mDirty = true; |
| 197 | } |
| 198 | |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 199 | namespace android { |
| 200 | namespace renderscript { |
| 201 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 202 | void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) { |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 203 | Program *p = static_cast<Program *>(vp); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 204 | p->bindAllocation(rsc, static_cast<Allocation *>(constants), slot); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 207 | void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) { |
Jason Sams | 7dad9c3 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 208 | Program *p = static_cast<Program *>(vpf); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 209 | p->bindTexture(rsc, slot, static_cast<Allocation *>(a)); |
Jason Sams | 7dad9c3 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 212 | void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) { |
Jason Sams | 7dad9c3 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 213 | Program *p = static_cast<Program *>(vpf); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 214 | p->bindSampler(rsc, slot, static_cast<Sampler *>(s)); |
Jason Sams | 7dad9c3 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 215 | } |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 216 | |
| 217 | } |
| 218 | } |
| 219 | |