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