Jason Sams | 326e0dd | 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 | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 18 | #include "rsProgramFragment.h" |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 19 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
Jason Sams | f2e4fa2 | 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 | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 25 | uint32_t paramLength) |
| 26 | : Program(rsc, shaderText, shaderLength, params, paramLength) { |
Jason Sams | 6445e52 | 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 | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame^] | 32 | mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader.string(), mUserShader.length()); |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 35 | ProgramFragment::~ProgramFragment() { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame^] | 36 | mRSC->mHal.funcs.fragment.destroy(mRSC, this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Alex Sakhartchouk | afb743a | 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 | 383e5b1 | 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 | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame^] | 45 | if (mHal.state.constants[0].get() == NULL) { |
Alex Sakhartchouk | 383e5b1 | 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 | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 48 | return; |
| 49 | } |
Jason Sams | 6445e52 | 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 | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame^] | 54 | memcpy(mHal.state.constants[0]->getPtr(), mConstantColor, 4*sizeof(float)); |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 55 | mDirty = true; |
| 56 | } |
| 57 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame^] | 58 | void ProgramFragment::setupGL2(Context *rsc, ProgramFragmentState *state) { |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 59 | //LOGE("sgl2 frag1 %x", glGetError()); |
| 60 | if ((state->mLast.get() == this) && !mDirty) { |
Jason Sams | e64c2f1 | 2010-06-22 17:22:13 -0700 | [diff] [blame] | 61 | return; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 62 | } |
| 63 | state->mLast.set(this); |
| 64 | |
Jason Sams | 3eb28f0 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 65 | rsc->checkError("ProgramFragment::setupGL2 start"); |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 66 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame^] | 67 | for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) { |
| 68 | if (!mHal.state.textures[ct].get()) { |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 69 | LOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct); |
| 70 | rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 71 | continue; |
| 72 | } |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame^] | 73 | mHal.state.textures[ct]->uploadCheck(rsc); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame^] | 76 | rsc->mHal.funcs.fragment.setActive(rsc, this); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 77 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 78 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 79 | void ProgramFragment::serialize(OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 82 | ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
| 85 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 86 | ProgramFragmentState::ProgramFragmentState() { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 87 | mPF = NULL; |
| 88 | } |
| 89 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 90 | ProgramFragmentState::~ProgramFragmentState() { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 91 | ObjectBase::checkDelete(mPF); |
| 92 | mPF = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 95 | void ProgramFragmentState::init(Context *rsc) { |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 96 | String8 shaderString(RS_SHADER_INTERNAL); |
| 97 | shaderString.append("varying lowp vec4 varColor;\n"); |
Alex Sakhartchouk | 7ffcaf2 | 2010-10-06 11:15:01 -0700 | [diff] [blame] | 98 | shaderString.append("varying vec2 varTex0;\n"); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 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 | const Element *colorElem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4); |
| 105 | rsc->mStateElement.elementBuilderBegin(); |
| 106 | rsc->mStateElement.elementBuilderAdd(colorElem, "Color", 1); |
| 107 | const Element *constInput = rsc->mStateElement.elementBuilderCreate(rsc); |
| 108 | |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 109 | Type *inputType = Type::getType(rsc, constInput, 1, 0, 0, false, false); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 110 | |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 111 | uint32_t tmp[2]; |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 112 | tmp[0] = RS_PROGRAM_PARAM_CONSTANT; |
| 113 | tmp[1] = (uint32_t)inputType; |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 114 | |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 115 | Allocation *constAlloc = new Allocation(rsc, inputType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 116 | ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(), |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 117 | shaderString.length(), tmp, 2); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 118 | pf->bindAllocation(rsc, constAlloc, 0); |
| 119 | pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 120 | |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 121 | mDefault.set(pf); |
| 122 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 123 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 124 | void ProgramFragmentState::deinit(Context *rsc) { |
Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 125 | mDefault.clear(); |
| 126 | mLast.clear(); |
| 127 | } |
| 128 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 129 | namespace android { |
| 130 | namespace renderscript { |
| 131 | |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 132 | RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText, |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 133 | uint32_t shaderLength, const uint32_t * params, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 134 | uint32_t paramLength) { |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 135 | ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength); |
| 136 | pf->incUserRef(); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 137 | //LOGE("rsi_ProgramFragmentCreate %p", pf); |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 138 | return pf; |
| 139 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 140 | |
| 141 | } |
| 142 | } |
| 143 | |