Home | History | Annotate | Download | only in rs
      1 /*
      2  * Copyright (C) 2009 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 "rsProgramFragment.h"
     19 
     20 using namespace android;
     21 using namespace android::renderscript;
     22 
     23 ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, size_t shaderLength,
     24                                  const char** textureNames, size_t textureNamesCount, const size_t *textureNamesLength,
     25 
     26                                  const uint32_t * params, size_t paramLength)
     27     : Program(rsc, shaderText, shaderLength, params, paramLength) {
     28     mConstantColor[0] = 1.f;
     29     mConstantColor[1] = 1.f;
     30     mConstantColor[2] = 1.f;
     31     mConstantColor[3] = 1.f;
     32 
     33     mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader, mUserShaderLen,
     34                                    textureNames, textureNamesCount, textureNamesLength);
     35 }
     36 
     37 ProgramFragment::~ProgramFragment() {
     38     mRSC->mHal.funcs.fragment.destroy(mRSC, this);
     39 }
     40 
     41 void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) {
     42     if (isUserProgram()) {
     43         ALOGE("Attempting to set fixed function emulation color on user program");
     44         rsc->setError(RS_ERROR_BAD_SHADER, "Cannot  set fixed function emulation color on user program");
     45         return;
     46     }
     47     if (mHal.state.constants[0] == NULL) {
     48         ALOGE("Unable to set fixed function emulation color because allocation is missing");
     49         rsc->setError(RS_ERROR_BAD_SHADER, "Unable to set fixed function emulation color because allocation is missing");
     50         return;
     51     }
     52     mConstantColor[0] = r;
     53     mConstantColor[1] = g;
     54     mConstantColor[2] = b;
     55     mConstantColor[3] = a;
     56     void *p = rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]);
     57     memcpy(p, mConstantColor, 4*sizeof(float));
     58     mDirty = true;
     59     rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
     60 }
     61 
     62 void ProgramFragment::setup(Context *rsc, ProgramFragmentState *state) {
     63     if ((state->mLast.get() == this) && !mDirty) {
     64         return;
     65     }
     66     state->mLast.set(this);
     67 
     68     for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
     69         if (!mHal.state.textures[ct]) {
     70             ALOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct);
     71             rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound");
     72             continue;
     73         }
     74     }
     75 
     76     rsc->mHal.funcs.fragment.setActive(rsc, this);
     77 }
     78 
     79 void ProgramFragment::serialize(Context *rsc, OStream *stream) const {
     80 }
     81 
     82 ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) {
     83     return NULL;
     84 }
     85 
     86 ProgramFragmentState::ProgramFragmentState() {
     87     mPF = NULL;
     88 }
     89 
     90 ProgramFragmentState::~ProgramFragmentState() {
     91     ObjectBase::checkDelete(mPF);
     92     mPF = NULL;
     93 }
     94 
     95 void ProgramFragmentState::init(Context *rsc) {
     96     const char *shaderString =
     97             RS_SHADER_INTERNAL
     98             "varying lowp vec4 varColor;\n"
     99             "varying vec2 varTex0;\n"
    100             "void main() {\n"
    101             "  lowp vec4 col = UNI_Color;\n"
    102             "  gl_FragColor = col;\n"
    103             "}\n";
    104 
    105     ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
    106 
    107     const char *enames[] = { "Color" };
    108     const Element *eins[] = {colorElem.get()};
    109     ObjectBaseRef<const Element> constInput = Element::create(rsc, 1, eins, enames);
    110 
    111     ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false, 0);
    112 
    113     uint32_t tmp[2];
    114     tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
    115     tmp[1] = (uint32_t)inputType.get();
    116 
    117     Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(),
    118                               RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
    119     ProgramFragment *pf = new ProgramFragment(rsc, shaderString, strlen(shaderString),
    120                                               NULL, 0, NULL, tmp, 2);
    121     pf->bindAllocation(rsc, constAlloc, 0);
    122     pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);
    123 
    124     mDefault.set(pf);
    125 }
    126 
    127 void ProgramFragmentState::deinit(Context *rsc) {
    128     mDefault.clear();
    129     mLast.clear();
    130 }
    131 
    132 namespace android {
    133 namespace renderscript {
    134 
    135 RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText,
    136                                             size_t shaderLength,
    137                                             const char** textureNames,
    138                                             size_t textureNamesCount,
    139                                             const size_t *textureNamesLength,
    140                                             const uint32_t * params, size_t paramLength) {
    141     ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength,
    142                                               textureNames, textureNamesCount, textureNamesLength,
    143                                               params, paramLength);
    144     pf->incUserRef();
    145     //ALOGE("rsi_ProgramFragmentCreate %p", pf);
    146     return pf;
    147 }
    148 
    149 }
    150 }
    151 
    152