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 | d0f5bd1 | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 18 | #ifndef ANDROID_RS_SERIALIZE |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | #include <GLES/glext.h> |
Alex Sakhartchouk | d0f5bd1 | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 21 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 22 | |
| 23 | #include "rsProgramStore.h" |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 24 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 25 | using namespace android; |
| 26 | using namespace android::renderscript; |
| 27 | |
| 28 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 29 | ProgramStore::ProgramStore(Context *rsc) : Program(rsc) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | mDitherEnable = true; |
| 31 | mBlendEnable = false; |
| 32 | mColorRWriteEnable = true; |
| 33 | mColorGWriteEnable = true; |
| 34 | mColorBWriteEnable = true; |
| 35 | mColorAWriteEnable = true; |
| 36 | mBlendSrc = GL_ONE; |
| 37 | mBlendDst = GL_ZERO; |
| 38 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 39 | mDepthTestEnable = false; |
| 40 | mDepthWriteEnable = true; |
| 41 | mDepthFunc = GL_LESS; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 44 | ProgramStore::~ProgramStore() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 47 | void ProgramStore::setupGL2(const Context *rsc, ProgramStoreState *state) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 48 | if (state->mLast.get() == this) { |
| 49 | return; |
| 50 | } |
| 51 | state->mLast.set(this); |
| 52 | |
| 53 | glColorMask(mColorRWriteEnable, |
| 54 | mColorGWriteEnable, |
| 55 | mColorBWriteEnable, |
| 56 | mColorAWriteEnable); |
| 57 | if (mBlendEnable) { |
| 58 | glEnable(GL_BLEND); |
| 59 | glBlendFunc(mBlendSrc, mBlendDst); |
| 60 | } else { |
| 61 | glDisable(GL_BLEND); |
| 62 | } |
| 63 | |
| 64 | //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc); |
| 65 | |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 66 | if (rsc->mUserSurfaceConfig.depthMin > 0) { |
| 67 | glDepthMask(mDepthWriteEnable); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 68 | if (mDepthTestEnable || mDepthWriteEnable) { |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 69 | glEnable(GL_DEPTH_TEST); |
| 70 | glDepthFunc(mDepthFunc); |
| 71 | } else { |
| 72 | glDisable(GL_DEPTH_TEST); |
| 73 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 74 | } else { |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 75 | glDepthMask(false); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 76 | glDisable(GL_DEPTH_TEST); |
| 77 | } |
| 78 | |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 79 | if (rsc->mUserSurfaceConfig.stencilMin > 0) { |
| 80 | } else { |
| 81 | glStencilMask(0); |
| 82 | glDisable(GL_STENCIL_TEST); |
| 83 | } |
| 84 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 85 | if (mDitherEnable) { |
| 86 | glEnable(GL_DITHER); |
| 87 | } else { |
| 88 | glDisable(GL_DITHER); |
| 89 | } |
| 90 | } |
| 91 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 92 | void ProgramStore::setDitherEnable(bool enable) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 93 | mDitherEnable = enable; |
| 94 | } |
| 95 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 96 | void ProgramStore::serialize(OStream *stream) const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 99 | ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
| 102 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 103 | void ProgramStore::setDepthFunc(RsDepthFunc func) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 104 | mDepthTestEnable = true; |
| 105 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 106 | switch (func) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 107 | case RS_DEPTH_FUNC_ALWAYS: |
| 108 | mDepthTestEnable = false; |
| 109 | mDepthFunc = GL_ALWAYS; |
| 110 | break; |
| 111 | case RS_DEPTH_FUNC_LESS: |
| 112 | mDepthFunc = GL_LESS; |
| 113 | break; |
| 114 | case RS_DEPTH_FUNC_LEQUAL: |
| 115 | mDepthFunc = GL_LEQUAL; |
| 116 | break; |
| 117 | case RS_DEPTH_FUNC_GREATER: |
| 118 | mDepthFunc = GL_GREATER; |
| 119 | break; |
| 120 | case RS_DEPTH_FUNC_GEQUAL: |
| 121 | mDepthFunc = GL_GEQUAL; |
| 122 | break; |
| 123 | case RS_DEPTH_FUNC_EQUAL: |
| 124 | mDepthFunc = GL_EQUAL; |
| 125 | break; |
| 126 | case RS_DEPTH_FUNC_NOTEQUAL: |
| 127 | mDepthFunc = GL_NOTEQUAL; |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 132 | void ProgramStore::setDepthMask(bool mask) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 133 | mDepthWriteEnable = mask; |
| 134 | } |
| 135 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 136 | void ProgramStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 137 | mBlendEnable = true; |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 138 | if ((src == RS_BLEND_SRC_ONE) && |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 139 | (dst == RS_BLEND_DST_ZERO)) { |
| 140 | mBlendEnable = false; |
| 141 | } |
| 142 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 143 | switch (src) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 144 | case RS_BLEND_SRC_ZERO: |
| 145 | mBlendSrc = GL_ZERO; |
| 146 | break; |
| 147 | case RS_BLEND_SRC_ONE: |
| 148 | mBlendSrc = GL_ONE; |
| 149 | break; |
| 150 | case RS_BLEND_SRC_DST_COLOR: |
| 151 | mBlendSrc = GL_DST_COLOR; |
| 152 | break; |
| 153 | case RS_BLEND_SRC_ONE_MINUS_DST_COLOR: |
| 154 | mBlendSrc = GL_ONE_MINUS_DST_COLOR; |
| 155 | break; |
| 156 | case RS_BLEND_SRC_SRC_ALPHA: |
| 157 | mBlendSrc = GL_SRC_ALPHA; |
| 158 | break; |
| 159 | case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA: |
| 160 | mBlendSrc = GL_ONE_MINUS_SRC_ALPHA; |
| 161 | break; |
| 162 | case RS_BLEND_SRC_DST_ALPHA: |
| 163 | mBlendSrc = GL_DST_ALPHA; |
| 164 | break; |
| 165 | case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA: |
| 166 | mBlendSrc = GL_ONE_MINUS_DST_ALPHA; |
| 167 | break; |
| 168 | case RS_BLEND_SRC_SRC_ALPHA_SATURATE: |
| 169 | mBlendSrc = GL_SRC_ALPHA_SATURATE; |
| 170 | break; |
| 171 | } |
| 172 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 173 | switch (dst) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 174 | case RS_BLEND_DST_ZERO: |
| 175 | mBlendDst = GL_ZERO; |
| 176 | break; |
| 177 | case RS_BLEND_DST_ONE: |
| 178 | mBlendDst = GL_ONE; |
| 179 | break; |
| 180 | case RS_BLEND_DST_SRC_COLOR: |
| 181 | mBlendDst = GL_SRC_COLOR; |
| 182 | break; |
| 183 | case RS_BLEND_DST_ONE_MINUS_SRC_COLOR: |
| 184 | mBlendDst = GL_ONE_MINUS_SRC_COLOR; |
| 185 | break; |
| 186 | case RS_BLEND_DST_SRC_ALPHA: |
| 187 | mBlendDst = GL_SRC_ALPHA; |
| 188 | break; |
| 189 | case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA: |
| 190 | mBlendDst = GL_ONE_MINUS_SRC_ALPHA; |
| 191 | break; |
| 192 | case RS_BLEND_DST_DST_ALPHA: |
| 193 | mBlendDst = GL_DST_ALPHA; |
| 194 | break; |
| 195 | case RS_BLEND_DST_ONE_MINUS_DST_ALPHA: |
| 196 | mBlendDst = GL_ONE_MINUS_DST_ALPHA; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 201 | void ProgramStore::setColorMask(bool r, bool g, bool b, bool a) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 202 | mColorRWriteEnable = r; |
| 203 | mColorGWriteEnable = g; |
| 204 | mColorBWriteEnable = b; |
| 205 | mColorAWriteEnable = a; |
| 206 | } |
| 207 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 208 | ProgramStoreState::ProgramStoreState() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 209 | mPFS = NULL; |
| 210 | } |
| 211 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 212 | ProgramStoreState::~ProgramStoreState() { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 213 | ObjectBase::checkDelete(mPFS); |
| 214 | mPFS = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 217 | void ProgramStoreState::init(Context *rsc) { |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 218 | ProgramStore *pfs = new ProgramStore(rsc); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 219 | mDefault.set(pfs); |
| 220 | } |
| 221 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 222 | void ProgramStoreState::deinit(Context *rsc) { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 223 | mDefault.clear(); |
| 224 | mLast.clear(); |
| 225 | } |
| 226 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 227 | namespace android { |
| 228 | namespace renderscript { |
| 229 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 230 | void rsi_ProgramStoreBegin(Context * rsc, RsElement in, RsElement out) { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 231 | ObjectBase::checkDelete(rsc->mStateFragmentStore.mPFS); |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 232 | rsc->mStateFragmentStore.mPFS = new ProgramStore(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 235 | void rsi_ProgramStoreDepthFunc(Context *rsc, RsDepthFunc func) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 236 | rsc->mStateFragmentStore.mPFS->setDepthFunc(func); |
| 237 | } |
| 238 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 239 | void rsi_ProgramStoreDepthMask(Context *rsc, bool mask) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 240 | rsc->mStateFragmentStore.mPFS->setDepthMask(mask); |
| 241 | } |
| 242 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 243 | void rsi_ProgramStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 244 | rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a); |
| 245 | } |
| 246 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 247 | void rsi_ProgramStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 248 | rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst); |
| 249 | } |
| 250 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 251 | RsProgramStore rsi_ProgramStoreCreate(Context *rsc) { |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 252 | ProgramStore *pfs = rsc->mStateFragmentStore.mPFS; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 253 | pfs->incUserRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 254 | rsc->mStateFragmentStore.mPFS = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 255 | return pfs; |
| 256 | } |
| 257 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 258 | void rsi_ProgramStoreDither(Context *rsc, bool enable) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 259 | rsc->mStateFragmentStore.mPFS->setDitherEnable(enable); |
| 260 | } |
| 261 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 262 | } |
| 263 | } |