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