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