Home | History | Annotate | Download | only in rs
      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 
     20 using namespace android;
     21 using namespace android::renderscript;
     22 
     23 Program::Program(Context *rsc, const char * shaderText, size_t shaderLength,
     24                  const uint32_t * params, size_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     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];
     48     mHal.state.textureTargets = new RsTextureTarget[mHal.state.texturesCount];
     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();
     55 
     56     uint32_t input = 0;
     57     uint32_t constant = 0;
     58     uint32_t texture = 0;
     59     for (uint32_t ct=0; ct < paramLength; ct+=2) {
     60         if (params[ct] == RS_PROGRAM_PARAM_INPUT) {
     61             mInputElements[input].set(reinterpret_cast<Element *>(params[ct+1]));
     62             mHal.state.inputElements[input++] = reinterpret_cast<Element *>(params[ct+1]);
     63         }
     64         if (params[ct] == RS_PROGRAM_PARAM_CONSTANT) {
     65             mConstantTypes[constant].set(reinterpret_cast<Type *>(params[ct+1]));
     66             mHal.state.constantTypes[constant++] = reinterpret_cast<Type *>(params[ct+1]);
     67         }
     68         if (params[ct] == RS_PROGRAM_PARAM_TEXTURE_TYPE) {
     69             mHal.state.textureTargets[texture++] = (RsTextureTarget)params[ct+1];
     70         }
     71     }
     72     mIsInternal = false;
     73     uint32_t internalTokenLen = strlen(RS_SHADER_INTERNAL);
     74     if (shaderLength > internalTokenLen &&
     75        strncmp(RS_SHADER_INTERNAL, shaderText, internalTokenLen) == 0) {
     76         mIsInternal = true;
     77         shaderText += internalTokenLen;
     78         shaderLength -= internalTokenLen;
     79     }
     80     mUserShader.setTo(shaderText, shaderLength);
     81 }
     82 
     83 Program::~Program() {
     84     freeChildren();
     85 
     86     delete[] mTextures;
     87     delete[] mSamplers;
     88     delete[] mInputElements;
     89     delete[] mConstantTypes;
     90     delete[] mConstants;
     91 
     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;
    101 }
    102 
    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 
    115 void Program::initMemberVars() {
    116     mDirty = true;
    117 
    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 
    130     mTextures = NULL;
    131     mSamplers = NULL;
    132     mInputElements = NULL;
    133     mConstantTypes = NULL;
    134     mConstants = NULL;
    135 
    136     mIsInternal = false;
    137 }
    138 
    139 void Program::bindAllocation(Context *rsc, Allocation *alloc, uint32_t slot) {
    140     if (alloc != NULL) {
    141         if (slot >= mHal.state.constantsCount) {
    142             ALOGE("Attempt to bind alloc at slot %u, on shader id %u, but const count is %u",
    143                  slot, (uint32_t)this, mHal.state.constantsCount);
    144             rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
    145             return;
    146         }
    147         if (alloc->getType() != mConstantTypes[slot].get()) {
    148             ALOGE("Attempt to bind alloc at slot %u, on shader id %u, but types mismatch",
    149                  slot, (uint32_t)this);
    150             rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind allocation");
    151             return;
    152         }
    153     }
    154     if (mConstants[slot].get() == alloc) {
    155         return;
    156     }
    157     if (mConstants[slot].get()) {
    158         mConstants[slot]->removeProgramToDirty(this);
    159     }
    160     mConstants[slot].set(alloc);
    161     mHal.state.constants[slot] = alloc;
    162     if (alloc) {
    163         alloc->addProgramToDirty(this);
    164     }
    165     mDirty = true;
    166 }
    167 
    168 void Program::bindTexture(Context *rsc, uint32_t slot, Allocation *a) {
    169     if (slot >= mHal.state.texturesCount) {
    170         ALOGE("Attempt to bind texture to slot %u but tex count is %u", slot, mHal.state.texturesCount);
    171         rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind texture");
    172         return;
    173     }
    174 
    175     if (a && a->getType()->getDimFaces() && mHal.state.textureTargets[slot] != RS_TEXTURE_CUBE) {
    176         ALOGE("Attempt to bind cubemap to slot %u but 2d texture needed", slot);
    177         rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind cubemap to 2d texture slot");
    178         return;
    179     }
    180 
    181     mTextures[slot].set(a);
    182     mHal.state.textures[slot] = a;
    183 
    184     mDirty = true;
    185 }
    186 
    187 void Program::bindSampler(Context *rsc, uint32_t slot, Sampler *s) {
    188     if (slot >= mHal.state.texturesCount) {
    189         ALOGE("Attempt to bind sampler to slot %u but tex count is %u", slot, mHal.state.texturesCount);
    190         rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind sampler");
    191         return;
    192     }
    193 
    194     mSamplers[slot].set(s);
    195     mHal.state.samplers[slot] = s;
    196     mDirty = true;
    197 }
    198 
    199 namespace android {
    200 namespace renderscript {
    201 
    202 void rsi_ProgramBindConstants(Context *rsc, RsProgram vp, uint32_t slot, RsAllocation constants) {
    203     Program *p = static_cast<Program *>(vp);
    204     p->bindAllocation(rsc, static_cast<Allocation *>(constants), slot);
    205 }
    206 
    207 void rsi_ProgramBindTexture(Context *rsc, RsProgram vpf, uint32_t slot, RsAllocation a) {
    208     Program *p = static_cast<Program *>(vpf);
    209     p->bindTexture(rsc, slot, static_cast<Allocation *>(a));
    210 }
    211 
    212 void rsi_ProgramBindSampler(Context *rsc, RsProgram vpf, uint32_t slot, RsSampler s) {
    213     Program *p = static_cast<Program *>(vpf);
    214     p->bindSampler(rsc, slot, static_cast<Sampler *>(s));
    215 }
    216 
    217 }
    218 }
    219 
    220