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