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