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 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 18 | #include "rsContext.h" |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | #include <GLES/glext.h> |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 21 | #include <GLES2/gl2.h> |
| 22 | #include <GLES2/gl2ext.h> |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 23 | #else |
| 24 | #include "rsContextHostStub.h" |
| 25 | #include <OpenGL/gl.h> |
| 26 | #include <OpenGL/glext.h> |
| 27 | #endif //ANDROID_RS_BUILD_FOR_HOST |
| 28 | |
| 29 | #include "rsProgramFragment.h" |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 30 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | using namespace android; |
| 32 | using namespace android::renderscript; |
| 33 | |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 34 | ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, |
| 35 | uint32_t shaderLength, const uint32_t * params, |
| 36 | uint32_t paramLength) : |
| 37 | Program(rsc, shaderText, shaderLength, params, paramLength) |
| 38 | { |
| 39 | mAllocFile = __FILE__; |
| 40 | mAllocLine = __LINE__; |
| 41 | |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 42 | mConstantColor[0] = 1.f; |
| 43 | mConstantColor[1] = 1.f; |
| 44 | mConstantColor[2] = 1.f; |
| 45 | mConstantColor[3] = 1.f; |
| 46 | |
Alex Sakhartchouk | 6e93421 | 2010-08-31 12:02:01 -0700 | [diff] [blame] | 47 | init(rsc); |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | ProgramFragment::~ProgramFragment() |
| 51 | { |
| 52 | } |
| 53 | |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 54 | void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 55 | { |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 56 | if(isUserProgram()) { |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 57 | LOGE("Attempting to set fixed function emulation color on user program"); |
| 58 | rsc->setError(RS_ERROR_BAD_SHADER, "Cannot set fixed function emulation color on user program"); |
| 59 | return; |
| 60 | } |
| 61 | if(mConstants[0].get() == NULL) { |
| 62 | LOGE("Unable to set fixed function emulation color because allocation is missing"); |
| 63 | 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] | 64 | return; |
| 65 | } |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 66 | mConstantColor[0] = r; |
| 67 | mConstantColor[1] = g; |
| 68 | mConstantColor[2] = b; |
| 69 | mConstantColor[3] = a; |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 70 | memcpy(mConstants[0]->getPtr(), mConstantColor, 4*sizeof(float)); |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 71 | mDirty = true; |
| 72 | } |
| 73 | |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 74 | void ProgramFragment::setupGL2(Context *rsc, ProgramFragmentState *state, ShaderCache *sc) |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 75 | { |
| 76 | //LOGE("sgl2 frag1 %x", glGetError()); |
| 77 | if ((state->mLast.get() == this) && !mDirty) { |
Jason Sams | e64c2f1 | 2010-06-22 17:22:13 -0700 | [diff] [blame] | 78 | return; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 79 | } |
| 80 | state->mLast.set(this); |
| 81 | |
Jason Sams | 3eb28f0 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 82 | rsc->checkError("ProgramFragment::setupGL2 start"); |
Jason Sams | 6445e52 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 83 | |
Alex Sakhartchouk | 6e93421 | 2010-08-31 12:02:01 -0700 | [diff] [blame] | 84 | rsc->checkError("ProgramFragment::setupGL2 begin uniforms"); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 85 | setupUserConstants(rsc, sc, true); |
Alex Sakhartchouk | 6e93421 | 2010-08-31 12:02:01 -0700 | [diff] [blame] | 86 | |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 87 | uint32_t numTexturesToBind = mTextureCount; |
| 88 | uint32_t numTexturesAvailable = rsc->getMaxFragmentTextures(); |
| 89 | if(numTexturesToBind >= numTexturesAvailable) { |
| 90 | LOGE("Attempting to bind %u textures on shader id %u, but only %u are available", |
| 91 | mTextureCount, (uint32_t)this, numTexturesAvailable); |
| 92 | rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind more textuers than available"); |
| 93 | numTexturesToBind = numTexturesAvailable; |
| 94 | } |
| 95 | |
| 96 | for (uint32_t ct=0; ct < numTexturesToBind; ct++) { |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 97 | glActiveTexture(GL_TEXTURE0 + ct); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 98 | if (!mTextures[ct].get()) { |
| 99 | LOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct); |
| 100 | rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 101 | continue; |
| 102 | } |
| 103 | |
Jason Sams | cf4c7c9 | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 104 | mTextures[ct]->uploadCheck(rsc); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 105 | glBindTexture(GL_TEXTURE_2D, mTextures[ct]->getTextureID()); |
Jason Sams | 3eb28f0 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 106 | rsc->checkError("ProgramFragment::setupGL2 tex bind"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 107 | if (mSamplers[ct].get()) { |
Jason Sams | 900f161 | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 108 | mSamplers[ct]->setupGL(rsc, mTextures[ct].get()); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 109 | } else { |
| 110 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 111 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Jason Sams | 900f161 | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 112 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 113 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Jason Sams | 3eb28f0 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 114 | rsc->checkError("ProgramFragment::setupGL2 tex env"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Alex Sakhartchouk | 660ccc7 | 2010-09-02 18:43:24 -0700 | [diff] [blame] | 117 | glUniform1i(sc->fragUniformSlot(mTextureUniformIndexStart + ct), ct); |
Jason Sams | 3eb28f0 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 118 | rsc->checkError("ProgramFragment::setupGL2 uniforms"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | glActiveTexture(GL_TEXTURE0); |
| 122 | mDirty = false; |
Jason Sams | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 123 | rsc->checkError("ProgramFragment::setupGL2"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Jason Sams | cd50653 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 126 | void ProgramFragment::loadShader(Context *rsc) { |
| 127 | Program::loadShader(rsc, GL_FRAGMENT_SHADER); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void ProgramFragment::createShader() |
| 131 | { |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 132 | if (mUserShader.length() > 1) { |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 133 | mShader.append("precision mediump float;\n"); |
Alex Sakhartchouk | 660ccc7 | 2010-09-02 18:43:24 -0700 | [diff] [blame] | 134 | appendUserConstants(); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 135 | char buf[256]; |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 136 | for (uint32_t ct=0; ct < mTextureCount; ct++) { |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 137 | sprintf(buf, "uniform sampler2D UNI_Tex%i;\n", ct); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 138 | mShader.append(buf); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 139 | } |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 140 | mShader.append(mUserShader); |
| 141 | } else { |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 142 | LOGE("ProgramFragment::createShader cannot create program, shader code not defined"); |
| 143 | rsAssert(0); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 144 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 145 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 146 | |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 147 | void ProgramFragment::init(Context *rsc) |
| 148 | { |
Alex Sakhartchouk | 660ccc7 | 2010-09-02 18:43:24 -0700 | [diff] [blame] | 149 | mUniformCount = 0; |
Alex Sakhartchouk | 6e93421 | 2010-08-31 12:02:01 -0700 | [diff] [blame] | 150 | if (mUserShader.size() > 0) { |
| 151 | for (uint32_t ct=0; ct < mConstantCount; ct++) { |
| 152 | initAddUserElement(mConstantTypes[ct]->getElement(), mUniformNames, &mUniformCount, "UNI_"); |
| 153 | } |
| 154 | } |
Alex Sakhartchouk | 660ccc7 | 2010-09-02 18:43:24 -0700 | [diff] [blame] | 155 | mTextureUniformIndexStart = mUniformCount; |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 156 | char buf[256]; |
| 157 | for (uint32_t ct=0; ct < mTextureCount; ct++) { |
| 158 | sprintf(buf, "UNI_Tex%i", ct); |
| 159 | mUniformNames[mUniformCount++].setTo(buf); |
| 160 | } |
Alex Sakhartchouk | 6e93421 | 2010-08-31 12:02:01 -0700 | [diff] [blame] | 161 | |
| 162 | createShader(); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 163 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 164 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 165 | void ProgramFragment::serialize(OStream *stream) const |
| 166 | { |
Jason Sams | e64c2f1 | 2010-06-22 17:22:13 -0700 | [diff] [blame] | 167 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) |
| 171 | { |
| 172 | return NULL; |
| 173 | } |
| 174 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 175 | ProgramFragmentState::ProgramFragmentState() |
| 176 | { |
| 177 | mPF = NULL; |
| 178 | } |
| 179 | |
| 180 | ProgramFragmentState::~ProgramFragmentState() |
| 181 | { |
| 182 | delete mPF; |
| 183 | |
| 184 | } |
| 185 | |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 186 | void ProgramFragmentState::init(Context *rsc) |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 187 | { |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 188 | String8 shaderString(RS_SHADER_INTERNAL); |
| 189 | shaderString.append("varying lowp vec4 varColor;\n"); |
| 190 | shaderString.append("varying vec4 varTex0;\n"); |
| 191 | shaderString.append("void main() {\n"); |
| 192 | shaderString.append(" lowp vec4 col = UNI_Color;\n"); |
| 193 | shaderString.append(" gl_FragColor = col;\n"); |
| 194 | shaderString.append("}\n"); |
| 195 | |
| 196 | const Element *colorElem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4); |
| 197 | rsc->mStateElement.elementBuilderBegin(); |
| 198 | rsc->mStateElement.elementBuilderAdd(colorElem, "Color", 1); |
| 199 | const Element *constInput = rsc->mStateElement.elementBuilderCreate(rsc); |
| 200 | |
| 201 | Type *inputType = new Type(rsc); |
| 202 | inputType->setElement(constInput); |
| 203 | inputType->setDimX(1); |
| 204 | inputType->compute(); |
| 205 | |
| 206 | uint32_t tmp[4]; |
| 207 | tmp[0] = RS_PROGRAM_PARAM_CONSTANT; |
| 208 | tmp[1] = (uint32_t)inputType; |
| 209 | tmp[2] = RS_PROGRAM_PARAM_TEXTURE_COUNT; |
| 210 | tmp[3] = 0; |
| 211 | |
| 212 | Allocation *constAlloc = new Allocation(rsc, inputType); |
| 213 | ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(), |
| 214 | shaderString.length(), tmp, 4); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 215 | pf->bindAllocation(rsc, constAlloc, 0); |
| 216 | pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f); |
Alex Sakhartchouk | e7ae69f | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 217 | |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 218 | mDefault.set(pf); |
| 219 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 220 | |
Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 221 | void ProgramFragmentState::deinit(Context *rsc) |
| 222 | { |
| 223 | mDefault.clear(); |
| 224 | mLast.clear(); |
| 225 | } |
| 226 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 227 | |
| 228 | namespace android { |
| 229 | namespace renderscript { |
| 230 | |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 231 | RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText, |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 232 | uint32_t shaderLength, const uint32_t * params, |
| 233 | uint32_t paramLength) |
| 234 | { |
| 235 | ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength); |
| 236 | pf->incUserRef(); |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame^] | 237 | //LOGE("rsi_ProgramFragmentCreate %p", pf); |
Jason Sams | f2e4fa2 | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 238 | return pf; |
| 239 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 240 | |
| 241 | } |
| 242 | } |
| 243 | |