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