Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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" |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 18 | #include "rsProgramFragment.h" |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 19 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 23 | ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, |
| 24 | uint32_t shaderLength, const uint32_t * params, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 25 | uint32_t paramLength) |
| 26 | : Program(rsc, shaderText, shaderLength, params, paramLength) { |
Jason Sams | 442a647 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 27 | mConstantColor[0] = 1.f; |
| 28 | mConstantColor[1] = 1.f; |
| 29 | mConstantColor[2] = 1.f; |
| 30 | mConstantColor[3] = 1.f; |
| 31 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 32 | mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader.string(), mUserShader.length()); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 35 | ProgramFragment::~ProgramFragment() { |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 36 | mRSC->mHal.funcs.fragment.destroy(mRSC, this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 39 | void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) { |
| 40 | if (isUserProgram()) { |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 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 | } |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 45 | if (mHal.state.constants[0].get() == NULL) { |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 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"); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 48 | return; |
| 49 | } |
Jason Sams | 442a647 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 50 | mConstantColor[0] = r; |
| 51 | mConstantColor[1] = g; |
| 52 | mConstantColor[2] = b; |
| 53 | mConstantColor[3] = a; |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 54 | memcpy(mHal.state.constants[0]->getPtr(), mConstantColor, 4*sizeof(float)); |
Jason Sams | 442a647 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 55 | mDirty = true; |
| 56 | } |
| 57 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 58 | void ProgramFragment::setupGL2(Context *rsc, ProgramFragmentState *state) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 59 | if ((state->mLast.get() == this) && !mDirty) { |
Jason Sams | 7c1f4c3 | 2010-06-22 17:22:13 -0700 | [diff] [blame] | 60 | return; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 61 | } |
| 62 | state->mLast.set(this); |
| 63 | |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 64 | rsc->checkError("ProgramFragment::setupGL2 start"); |
Jason Sams | 442a647 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 65 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 66 | for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) { |
| 67 | if (!mHal.state.textures[ct].get()) { |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 68 | LOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct); |
| 69 | rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 70 | continue; |
| 71 | } |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 72 | mHal.state.textures[ct]->uploadCheck(rsc); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 75 | rsc->mHal.funcs.fragment.setActive(rsc, this); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 76 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 77 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 78 | void ProgramFragment::serialize(OStream *stream) const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 81 | ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 82 | return NULL; |
| 83 | } |
| 84 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 85 | ProgramFragmentState::ProgramFragmentState() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 86 | mPF = NULL; |
| 87 | } |
| 88 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 89 | ProgramFragmentState::~ProgramFragmentState() { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 90 | ObjectBase::checkDelete(mPF); |
| 91 | mPF = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 94 | void ProgramFragmentState::init(Context *rsc) { |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 95 | String8 shaderString(RS_SHADER_INTERNAL); |
| 96 | shaderString.append("varying lowp vec4 varColor;\n"); |
Alex Sakhartchouk | d209163 | 2010-10-06 11:15:01 -0700 | [diff] [blame] | 97 | shaderString.append("varying vec2 varTex0;\n"); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 98 | shaderString.append("void main() {\n"); |
| 99 | shaderString.append(" lowp vec4 col = UNI_Color;\n"); |
| 100 | shaderString.append(" gl_FragColor = col;\n"); |
| 101 | shaderString.append("}\n"); |
| 102 | |
| 103 | const Element *colorElem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4); |
| 104 | rsc->mStateElement.elementBuilderBegin(); |
| 105 | rsc->mStateElement.elementBuilderAdd(colorElem, "Color", 1); |
| 106 | const Element *constInput = rsc->mStateElement.elementBuilderCreate(rsc); |
| 107 | |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 108 | Type *inputType = Type::getType(rsc, constInput, 1, 0, 0, false, false); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 109 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 110 | uint32_t tmp[2]; |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 111 | tmp[0] = RS_PROGRAM_PARAM_CONSTANT; |
| 112 | tmp[1] = (uint32_t)inputType; |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 113 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 114 | Allocation *constAlloc = new Allocation(rsc, inputType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 115 | ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(), |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 116 | shaderString.length(), tmp, 2); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 117 | pf->bindAllocation(rsc, constAlloc, 0); |
| 118 | pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 119 | |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 120 | mDefault.set(pf); |
| 121 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 122 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 123 | void ProgramFragmentState::deinit(Context *rsc) { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 124 | mDefault.clear(); |
| 125 | mLast.clear(); |
| 126 | } |
| 127 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 128 | namespace android { |
| 129 | namespace renderscript { |
| 130 | |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 131 | RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText, |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 132 | uint32_t shaderLength, const uint32_t * params, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 133 | uint32_t paramLength) { |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 134 | ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength); |
| 135 | pf->incUserRef(); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 136 | //LOGE("rsi_ProgramFragmentCreate %p", pf); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 137 | return pf; |
| 138 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 139 | |
| 140 | } |
| 141 | } |
| 142 | |