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