rsProgram.cpp revision c2c02a88641620f50a69cc174077ac8bbef40478
1/* 2 * Copyright (C) 2011 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 20using namespace android; 21using namespace android::renderscript; 22 23Program::Program(Context *rsc) : ObjectBase(rsc) { 24 initMemberVars(); 25} 26 27Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength, 28 const uint32_t * params, uint32_t paramLength) 29 : ObjectBase(rsc) { 30 31 initMemberVars(); 32 for (uint32_t ct=0; ct < paramLength; ct+=2) { 33 if (params[ct] == RS_PROGRAM_PARAM_INPUT) { 34 mHal.state.inputElementsCount++; 35 } 36 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { 37 mHal.state.constantsCount++; 38 } 39 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) { 40 mHal.state.texturesCount++; 41 } 42 } 43 44 mHal.state.textures = new ObjectBaseRef<Allocation>[mHal.state.texturesCount]; 45 mHal.state.samplers = new ObjectBaseRef<Sampler>[mHal.state.texturesCount]; 46 mHal.state.textureTargets = new RsTextureTarget[mHal.state.texturesCount]; 47 mHal.state.inputElements = new ObjectBaseRef<Element>[mHal.state.inputElementsCount]; 48 mHal.state.constantTypes = new ObjectBaseRef<Type>[mHal.state.constantsCount]; 49 mHal.state.constants = new ObjectBaseRef<Allocation>[mHal.state.constantsCount]; 50 51 uint32_t input = 0; 52 uint32_t constant = 0; 53 uint32_t texture = 0; 54 for (uint32_t ct=0; ct < paramLength; ct+=2) { 55 if (params[ct] == RS_PROGRAM_PARAM_INPUT) { 56 mHal.state.inputElements[input++].set(reinterpret_cast<Element *>(params[ct+1])); 57 } 58 if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) { 59 mHal.state.constantTypes[constant++].set(reinterpret_cast<Type *>(params[ct+1])); 60 } 61 if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) { 62 mHal.state.textureTargets[texture++] = (RsTextureTarget)params[ct+1]; 63 } 64 } 65 mIsInternal = false; 66 uint32_t internalTokenLen = strlen(RS_SHADER_INTERNAL); 67 if (shaderLength > internalTokenLen && 68 strncmp(RS_SHADER_INTERNAL, shaderText, internalTokenLen) == 0) { 69 mIsInternal = true; 70 shaderText += internalTokenLen; 71 shaderLength -= internalTokenLen; 72 } 73 mUserShader.setTo(shaderText, shaderLength); 74} 75 76Program::~Program() { 77 78 for (uint32_t ct=0; ct < mHal.state.constantsCount; ct++) { 79 bindAllocation(NULL, NULL, ct); 80 } 81 82 for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) { 83 bindTexture(NULL, ct, NULL); 84 bindSampler(NULL, ct, NULL); 85 } 86 delete[] mHal.state.textures; 87 delete[] mHal.state.samplers; 88 delete[] mHal.state.textureTargets; 89 delete[] mHal.state.inputElements; 90 delete[] mHal.state.constantTypes; 91 delete[] mHal.state.constants; 92 mHal.state.inputElementsCount = 0; 93 mHal.state.constantsCount = 0; 94 mHal.state.texturesCount = 0; 95} 96 97void Program::initMemberVars() { 98 mDirty = true; 99 100 mHal.drv = NULL; 101 mHal.state.textures = NULL; 102 mHal.state.samplers = NULL; 103 mHal.state.textureTargets = NULL; 104 mHal.state.inputElements = NULL; 105 mHal.state.constantTypes = NULL; 106 mHal.state.constants = NULL; 107 108 mHal.state.inputElementsCount = 0; 109 mHal.state.constantsCount = 0; 110 mHal.state.texturesCount = 0; 111 112 mIsInternal = false; 113} 114 115void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) { 116 if (alloc != NULL) { 117 if (slot >= mHal.state.constantsCount) { 118 LOGE("Attempt to bind alloc at slot %u, on shader id %u, but const count is %u", 119 slot, (uint32_t)this, mHal.state.constantsCount); 120 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation"); 121 return; 122 } 123 if (!alloc->getType()->isEqual(mHal.state.constantTypes[slot].get())) { 124 LOGE("Attempt to bind alloc at slot %u, on shader id %u, but types mismatch", 125 slot, (uint32_t)this); 126 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation"); 127 return; 128 } 129 } 130 if (mHal.state.constants[slot].get() == alloc) { 131 return; 132 } 133 if (mHal.state.constants[slot].get()) { 134 mHal.state.constants[slot].get()->removeProgramToDirty(this); 135 } 136 mHal.state.constants[slot].set(alloc); 137 if (alloc) { 138 alloc->addProgramToDirty(this); 139 } 140 mDirty = true; 141} 142 143void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) { 144 if (slot >= mHal.state.texturesCount) { 145 LOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mHal.state.texturesCount); 146 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture"); 147 return; 148 } 149 150 if (a && a->getType()->getDimFaces() && mHal.state.textureTargets[slot] != RS_TEXTURE_CUBE) { 151 LOGE("Attempt to bind cubemap to slot %u but 2d texture needed", slot); 152 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind cubemap to 2d texture slot"); 153 return; 154 } 155 156 mHal.state.textures[slot].set(a); 157 mDirty = true; 158} 159 160void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) { 161 if (slot >= mHal.state.texturesCount) { 162 LOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mHal.state.texturesCount); 163 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler"); 164 return; 165 } 166 167 mHal.state.samplers[slot].set(s); 168 mDirty = true; 169} 170 171namespace android { 172namespace renderscript { 173 174void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) { 175 Program *p = static_cast<Program *>(vp); 176 p->bindAllocation(rsc, static_cast<Allocation *>(constants), slot); 177} 178 179void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) { 180 Program *p = static_cast<Program *>(vpf); 181 p->bindTexture(rsc, slot, static_cast<Allocation *>(a)); 182} 183 184void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) { 185 Program *p = static_cast<Program *>(vpf); 186 p->bindSampler(rsc, slot, static_cast<Sampler *>(s)); 187} 188 189} 190} 191 192