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