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 #include <GLES/gl.h> 21 #include <GLES/glext.h> 22 23 using namespace android; 24 using namespace android::renderscript; 25 26 27 ProgramFragmentStore::ProgramFragmentStore(Context *rsc) : 28 Program(rsc) 29 { 30 mAllocFile = __FILE__; 31 mAllocLine = __LINE__; 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 53 void ProgramFragmentStore::setupGL(const Context *rsc, ProgramFragmentStoreState *state) 54 { 55 if (state->mLast.get() == this) { 56 return; 57 } 58 state->mLast.set(this); 59 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 71 //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc); 72 73 glDepthMask(mDepthWriteEnable); 74 if(mDepthTestEnable || mDepthWriteEnable) { 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 void ProgramFragmentStore::setupGL2(const Context *rsc, ProgramFragmentStoreState *state) 89 { 90 if (state->mLast.get() == this) { 91 return; 92 } 93 state->mLast.set(this); 94 95 glColorMask(mColorRWriteEnable, 96 mColorGWriteEnable, 97 mColorBWriteEnable, 98 mColorAWriteEnable); 99 if (mBlendEnable) { 100 glEnable(GL_BLEND); 101 glBlendFunc(mBlendSrc, mBlendDst); 102 } else { 103 glDisable(GL_BLEND); 104 } 105 106 //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc); 107 108 glDepthMask(mDepthWriteEnable); 109 if(mDepthTestEnable || mDepthWriteEnable) { 110 glEnable(GL_DEPTH_TEST); 111 glDepthFunc(mDepthFunc); 112 } else { 113 glDisable(GL_DEPTH_TEST); 114 } 115 116 if (mDitherEnable) { 117 glEnable(GL_DITHER); 118 } else { 119 glDisable(GL_DITHER); 120 } 121 } 122 123 124 void ProgramFragmentStore::setDitherEnable(bool enable) 125 { 126 mDitherEnable = enable; 127 } 128 129 void ProgramFragmentStore::setDepthFunc(RsDepthFunc func) 130 { 131 mDepthTestEnable = true; 132 133 switch(func) { 134 case RS_DEPTH_FUNC_ALWAYS: 135 mDepthTestEnable = false; 136 mDepthFunc = GL_ALWAYS; 137 break; 138 case RS_DEPTH_FUNC_LESS: 139 mDepthFunc = GL_LESS; 140 break; 141 case RS_DEPTH_FUNC_LEQUAL: 142 mDepthFunc = GL_LEQUAL; 143 break; 144 case RS_DEPTH_FUNC_GREATER: 145 mDepthFunc = GL_GREATER; 146 break; 147 case RS_DEPTH_FUNC_GEQUAL: 148 mDepthFunc = GL_GEQUAL; 149 break; 150 case RS_DEPTH_FUNC_EQUAL: 151 mDepthFunc = GL_EQUAL; 152 break; 153 case RS_DEPTH_FUNC_NOTEQUAL: 154 mDepthFunc = GL_NOTEQUAL; 155 break; 156 } 157 } 158 159 void ProgramFragmentStore::setDepthMask(bool mask) 160 { 161 mDepthWriteEnable = mask; 162 } 163 164 void ProgramFragmentStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst) 165 { 166 mBlendEnable = true; 167 if ((src == RS_BLEND_SRC_ONE) && 168 (dst == RS_BLEND_DST_ZERO)) { 169 mBlendEnable = false; 170 } 171 172 switch(src) { 173 case RS_BLEND_SRC_ZERO: 174 mBlendSrc = GL_ZERO; 175 break; 176 case RS_BLEND_SRC_ONE: 177 mBlendSrc = GL_ONE; 178 break; 179 case RS_BLEND_SRC_DST_COLOR: 180 mBlendSrc = GL_DST_COLOR; 181 break; 182 case RS_BLEND_SRC_ONE_MINUS_DST_COLOR: 183 mBlendSrc = GL_ONE_MINUS_DST_COLOR; 184 break; 185 case RS_BLEND_SRC_SRC_ALPHA: 186 mBlendSrc = GL_SRC_ALPHA; 187 break; 188 case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA: 189 mBlendSrc = GL_ONE_MINUS_SRC_ALPHA; 190 break; 191 case RS_BLEND_SRC_DST_ALPHA: 192 mBlendSrc = GL_DST_ALPHA; 193 break; 194 case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA: 195 mBlendSrc = GL_ONE_MINUS_DST_ALPHA; 196 break; 197 case RS_BLEND_SRC_SRC_ALPHA_SATURATE: 198 mBlendSrc = GL_SRC_ALPHA_SATURATE; 199 break; 200 } 201 202 switch(dst) { 203 case RS_BLEND_DST_ZERO: 204 mBlendDst = GL_ZERO; 205 break; 206 case RS_BLEND_DST_ONE: 207 mBlendDst = GL_ONE; 208 break; 209 case RS_BLEND_DST_SRC_COLOR: 210 mBlendDst = GL_SRC_COLOR; 211 break; 212 case RS_BLEND_DST_ONE_MINUS_SRC_COLOR: 213 mBlendDst = GL_ONE_MINUS_SRC_COLOR; 214 break; 215 case RS_BLEND_DST_SRC_ALPHA: 216 mBlendDst = GL_SRC_ALPHA; 217 break; 218 case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA: 219 mBlendDst = GL_ONE_MINUS_SRC_ALPHA; 220 break; 221 case RS_BLEND_DST_DST_ALPHA: 222 mBlendDst = GL_DST_ALPHA; 223 break; 224 case RS_BLEND_DST_ONE_MINUS_DST_ALPHA: 225 mBlendDst = GL_ONE_MINUS_DST_ALPHA; 226 break; 227 } 228 } 229 230 void ProgramFragmentStore::setColorMask(bool r, bool g, bool b, bool a) 231 { 232 mColorRWriteEnable = r; 233 mColorGWriteEnable = g; 234 mColorBWriteEnable = b; 235 mColorAWriteEnable = a; 236 } 237 238 239 ProgramFragmentStoreState::ProgramFragmentStoreState() 240 { 241 mPFS = NULL; 242 } 243 244 ProgramFragmentStoreState::~ProgramFragmentStoreState() 245 { 246 delete mPFS; 247 248 } 249 250 void ProgramFragmentStoreState::init(Context *rsc, int32_t w, int32_t h) 251 { 252 ProgramFragmentStore *pfs = new ProgramFragmentStore(rsc); 253 mDefault.set(pfs); 254 } 255 256 void ProgramFragmentStoreState::deinit(Context *rsc) 257 { 258 mDefault.clear(); 259 mLast.clear(); 260 } 261 262 263 namespace android { 264 namespace renderscript { 265 266 void rsi_ProgramFragmentStoreBegin(Context * rsc, RsElement in, RsElement out) 267 { 268 delete rsc->mStateFragmentStore.mPFS; 269 rsc->mStateFragmentStore.mPFS = new ProgramFragmentStore(rsc); 270 271 } 272 273 void rsi_ProgramFragmentStoreDepthFunc(Context *rsc, RsDepthFunc func) 274 { 275 rsc->mStateFragmentStore.mPFS->setDepthFunc(func); 276 } 277 278 void rsi_ProgramFragmentStoreDepthMask(Context *rsc, bool mask) 279 { 280 rsc->mStateFragmentStore.mPFS->setDepthMask(mask); 281 } 282 283 void rsi_ProgramFragmentStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a) 284 { 285 rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a); 286 } 287 288 void rsi_ProgramFragmentStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst) 289 { 290 rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst); 291 } 292 293 RsProgramFragmentStore rsi_ProgramFragmentStoreCreate(Context *rsc) 294 { 295 ProgramFragmentStore *pfs = rsc->mStateFragmentStore.mPFS; 296 pfs->incUserRef(); 297 rsc->mStateFragmentStore.mPFS = 0; 298 return pfs; 299 } 300 301 void rsi_ProgramFragmentStoreDither(Context *rsc, bool enable) 302 { 303 rsc->mStateFragmentStore.mPFS->setDitherEnable(enable); 304 } 305 306 307 } 308 } 309