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.string(), mUserShader.length(), 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 String8 shaderString(RS_SHADER_INTERNAL); 97 shaderString.append("varying lowp vec4 varColor;\n"); 98 shaderString.append("varying vec2 varTex0;\n"); 99 shaderString.append("void main() {\n"); 100 shaderString.append(" lowp vec4 col = UNI_Color;\n"); 101 shaderString.append(" gl_FragColor = col;\n"); 102 shaderString.append("}\n"); 103 104 ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4); 105 Element::Builder builder; 106 builder.add(colorElem.get(), "Color", 1); 107 ObjectBaseRef<const Element> constInput = builder.create(rsc); 108 109 ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false, 0); 110 111 uint32_t tmp[2]; 112 tmp[0] = RS_PROGRAM_PARAM_CONSTANT; 113 tmp[1] = (uint32_t)inputType.get(); 114 115 Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(), 116 RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS); 117 ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(), shaderString.length(), 118 NULL, 0, NULL, tmp, 2); 119 pf->bindAllocation(rsc, constAlloc, 0); 120 pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f); 121 122 mDefault.set(pf); 123 } 124 125 void ProgramFragmentState::deinit(Context *rsc) { 126 mDefault.clear(); 127 mLast.clear(); 128 } 129 130 namespace android { 131 namespace renderscript { 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 uint32_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 } 148 } 149 150