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 | |
Ian Rogers | f8852d0 | 2014-01-29 15:35:17 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 22 | using namespace android; |
| 23 | using namespace android::renderscript; |
| 24 | |
Alex Sakhartchouk | 748eb07 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 25 | ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, size_t shaderLength, |
| 26 | const char** textureNames, size_t textureNamesCount, const size_t *textureNamesLength, |
Ian Rogers | f8852d0 | 2014-01-29 15:35:17 -0800 | [diff] [blame] | 27 | const uintptr_t * params, size_t paramLength) |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 28 | : Program(rsc, shaderText, shaderLength, params, paramLength) { |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 29 | mConstantColor[0] = 1.f; |
| 30 | mConstantColor[1] = 1.f; |
| 31 | mConstantColor[2] = 1.f; |
| 32 | mConstantColor[3] = 1.f; |
| 33 | |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 34 | mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader, mUserShaderLen, |
Alex Sakhartchouk | 748eb07 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 35 | textureNames, textureNamesCount, textureNamesLength); |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 38 | ProgramFragment::~ProgramFragment() { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 39 | mRSC->mHal.funcs.fragment.destroy(mRSC, this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 42 | void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) { |
| 43 | if (isUserProgram()) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 44 | ALOGE("Attempting to set fixed function emulation color on user program"); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 45 | rsc->setError(RS_ERROR_BAD_SHADER, "Cannot set fixed function emulation color on user program"); |
| 46 | return; |
| 47 | } |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 48 | if (mHal.state.constants[0] == NULL) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 49 | ALOGE("Unable to set fixed function emulation color because allocation is missing"); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 50 | 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] | 51 | return; |
| 52 | } |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 53 | mConstantColor[0] = r; |
| 54 | mConstantColor[1] = g; |
| 55 | mConstantColor[2] = b; |
| 56 | mConstantColor[3] = a; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 57 | void *p = rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]); |
| 58 | memcpy(p, mConstantColor, 4*sizeof(float)); |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 59 | mDirty = true; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 60 | rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]); |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Alex Sakhartchouk | c19ff01 | 2011-05-06 14:59:45 -0700 | [diff] [blame] | 63 | void ProgramFragment::setup(Context *rsc, ProgramFragmentState *state) { |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 64 | if ((state->mLast.get() == this) && !mDirty) { |
Jason Sams | e64c2f1 | 2010-06-22 17:22:13 -0700 | [diff] [blame] | 65 | return; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 66 | } |
| 67 | state->mLast.set(this); |
| 68 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 69 | for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 70 | if (!mHal.state.textures[ct]) { |
Ian Rogers | f8852d0 | 2014-01-29 15:35:17 -0800 | [diff] [blame] | 71 | ALOGE("No texture bound for shader id %" PRIuPTR ", texture unit %u", (uintptr_t)this, ct); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 72 | rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 73 | continue; |
| 74 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 77 | rsc->mHal.funcs.fragment.setActive(rsc, this); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 78 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 79 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 80 | void ProgramFragment::serialize(Context *rsc, OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 83 | ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 84 | return NULL; |
| 85 | } |
| 86 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 87 | ProgramFragmentState::ProgramFragmentState() { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 88 | mPF = NULL; |
| 89 | } |
| 90 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 91 | ProgramFragmentState::~ProgramFragmentState() { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 92 | ObjectBase::checkDelete(mPF); |
| 93 | mPF = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 96 | void ProgramFragmentState::init(Context *rsc) { |
Jason Sams | a7f5e04 | 2013-07-08 16:46:18 -0700 | [diff] [blame] | 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"; |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 105 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 106 | ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4); |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 107 | |
| 108 | const char *enames[] = { "Color" }; |
| 109 | const Element *eins[] = {colorElem.get()}; |
| 110 | ObjectBaseRef<const Element> constInput = Element::create(rsc, 1, eins, enames); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 111 | |
Jason Sams | a572aca | 2013-01-09 11:52:26 -0800 | [diff] [blame] | 112 | ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false, 0); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 113 | |
Ian Rogers | f8852d0 | 2014-01-29 15:35:17 -0800 | [diff] [blame] | 114 | uintptr_t tmp[2]; |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 115 | tmp[0] = RS_PROGRAM_PARAM_CONSTANT; |
Ian Rogers | f8852d0 | 2014-01-29 15:35:17 -0800 | [diff] [blame] | 116 | tmp[1] = (uintptr_t)inputType.get(); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 117 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 118 | Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(), |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 119 | RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS); |
Jason Sams | a7f5e04 | 2013-07-08 16:46:18 -0700 | [diff] [blame] | 120 | ProgramFragment *pf = new ProgramFragment(rsc, shaderString, strlen(shaderString), |
Alex Sakhartchouk | 748eb07 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 121 | NULL, 0, NULL, tmp, 2); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 122 | pf->bindAllocation(rsc, constAlloc, 0); |
| 123 | pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 124 | |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 125 | mDefault.set(pf); |
| 126 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 127 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 128 | void ProgramFragmentState::deinit(Context *rsc) { |
Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 129 | mDefault.clear(); |
| 130 | mLast.clear(); |
| 131 | } |
| 132 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 133 | namespace android { |
| 134 | namespace renderscript { |
| 135 | |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 136 | RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText, |
Alex Sakhartchouk | 748eb07 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 137 | size_t shaderLength, |
| 138 | const char** textureNames, |
| 139 | size_t textureNamesCount, |
| 140 | const size_t *textureNamesLength, |
Ian Rogers | f8852d0 | 2014-01-29 15:35:17 -0800 | [diff] [blame] | 141 | const uintptr_t * params, size_t paramLength) { |
Alex Sakhartchouk | 748eb07 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 142 | ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, |
| 143 | textureNames, textureNamesCount, textureNamesLength, |
| 144 | params, paramLength); |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 145 | pf->incUserRef(); |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 146 | //ALOGE("rsi_ProgramFragmentCreate %p", pf); |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 147 | return pf; |
| 148 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 149 | |
| 150 | } |
| 151 | } |
| 152 | |