blob: 876299f7a6cef301f5ef4969a6ed2924c654821e [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070018#include "rsContext.h"
Jason Sams1aa5a4e2009-06-22 17:15:15 -070019#include <GLES/gl.h>
20#include <GLES/glext.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#include <OpenGl/glext.h>
25#endif //ANDROID_RS_BUILD_FOR_HOST
26
27#include "rsProgramStore.h"
Jason Sams1aa5a4e2009-06-22 17:15:15 -070028
Jason Sams326e0dd2009-05-22 14:03:28 -070029using namespace android;
30using namespace android::renderscript;
31
32
Jason Samsccc010b2010-05-13 18:30:11 -070033ProgramStore::ProgramStore(Context *rsc) :
Jason Sams4815c0d2009-12-15 12:58:36 -080034 Program(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070035{
36 mDitherEnable = true;
37 mBlendEnable = false;
38 mColorRWriteEnable = true;
39 mColorGWriteEnable = true;
40 mColorBWriteEnable = true;
41 mColorAWriteEnable = true;
42 mBlendSrc = GL_ONE;
43 mBlendDst = GL_ZERO;
44
45
46 mDepthTestEnable = false;
47 mDepthWriteEnable = true;
48 mDepthFunc = GL_LESS;
49
50
51}
52
Jason Samsccc010b2010-05-13 18:30:11 -070053ProgramStore::~ProgramStore()
Jason Sams326e0dd2009-05-22 14:03:28 -070054{
55}
56
Jason Samsccc010b2010-05-13 18:30:11 -070057void ProgramStore::setupGL2(const Context *rsc, ProgramStoreState *state)
Jason Samsc460e552009-11-25 13:22:07 -080058{
59 if (state->mLast.get() == this) {
60 return;
61 }
62 state->mLast.set(this);
63
64 glColorMask(mColorRWriteEnable,
65 mColorGWriteEnable,
66 mColorBWriteEnable,
67 mColorAWriteEnable);
68 if (mBlendEnable) {
69 glEnable(GL_BLEND);
70 glBlendFunc(mBlendSrc, mBlendDst);
71 } else {
72 glDisable(GL_BLEND);
73 }
74
75 //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
76
Jason Sams6b8552a2010-10-13 15:31:10 -070077 if (rsc->mUserSurfaceConfig.depthMin > 0) {
78 glDepthMask(mDepthWriteEnable);
79 if(mDepthTestEnable || mDepthWriteEnable) {
80 glEnable(GL_DEPTH_TEST);
81 glDepthFunc(mDepthFunc);
82 } else {
83 glDisable(GL_DEPTH_TEST);
84 }
Jason Samsc460e552009-11-25 13:22:07 -080085 } else {
Jason Sams6b8552a2010-10-13 15:31:10 -070086 glDepthMask(false);
Jason Samsc460e552009-11-25 13:22:07 -080087 glDisable(GL_DEPTH_TEST);
88 }
89
Jason Sams6b8552a2010-10-13 15:31:10 -070090 if (rsc->mUserSurfaceConfig.stencilMin > 0) {
91 } else {
92 glStencilMask(0);
93 glDisable(GL_STENCIL_TEST);
94 }
95
Jason Samsc460e552009-11-25 13:22:07 -080096 if (mDitherEnable) {
97 glEnable(GL_DITHER);
98 } else {
99 glDisable(GL_DITHER);
100 }
101}
102
103
Jason Samsccc010b2010-05-13 18:30:11 -0700104void ProgramStore::setDitherEnable(bool enable)
Jason Sams326e0dd2009-05-22 14:03:28 -0700105{
106 mDitherEnable = enable;
107}
108
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700109void ProgramStore::serialize(OStream *stream) const
110{
111
112}
113
114ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream)
115{
116 return NULL;
117}
118
119
Jason Samsccc010b2010-05-13 18:30:11 -0700120void ProgramStore::setDepthFunc(RsDepthFunc func)
Jason Sams326e0dd2009-05-22 14:03:28 -0700121{
122 mDepthTestEnable = true;
123
124 switch(func) {
125 case RS_DEPTH_FUNC_ALWAYS:
126 mDepthTestEnable = false;
127 mDepthFunc = GL_ALWAYS;
128 break;
129 case RS_DEPTH_FUNC_LESS:
130 mDepthFunc = GL_LESS;
131 break;
132 case RS_DEPTH_FUNC_LEQUAL:
133 mDepthFunc = GL_LEQUAL;
134 break;
135 case RS_DEPTH_FUNC_GREATER:
136 mDepthFunc = GL_GREATER;
137 break;
138 case RS_DEPTH_FUNC_GEQUAL:
139 mDepthFunc = GL_GEQUAL;
140 break;
141 case RS_DEPTH_FUNC_EQUAL:
142 mDepthFunc = GL_EQUAL;
143 break;
144 case RS_DEPTH_FUNC_NOTEQUAL:
145 mDepthFunc = GL_NOTEQUAL;
146 break;
147 }
148}
149
Jason Samsccc010b2010-05-13 18:30:11 -0700150void ProgramStore::setDepthMask(bool mask)
Jason Sams326e0dd2009-05-22 14:03:28 -0700151{
152 mDepthWriteEnable = mask;
153}
154
Jason Samsccc010b2010-05-13 18:30:11 -0700155void ProgramStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst)
Jason Sams326e0dd2009-05-22 14:03:28 -0700156{
157 mBlendEnable = true;
Jason Samscfb1d112009-08-05 13:57:03 -0700158 if ((src == RS_BLEND_SRC_ONE) &&
Jason Sams326e0dd2009-05-22 14:03:28 -0700159 (dst == RS_BLEND_DST_ZERO)) {
160 mBlendEnable = false;
161 }
162
163 switch(src) {
164 case RS_BLEND_SRC_ZERO:
165 mBlendSrc = GL_ZERO;
166 break;
167 case RS_BLEND_SRC_ONE:
168 mBlendSrc = GL_ONE;
169 break;
170 case RS_BLEND_SRC_DST_COLOR:
171 mBlendSrc = GL_DST_COLOR;
172 break;
173 case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
174 mBlendSrc = GL_ONE_MINUS_DST_COLOR;
175 break;
176 case RS_BLEND_SRC_SRC_ALPHA:
177 mBlendSrc = GL_SRC_ALPHA;
178 break;
179 case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
180 mBlendSrc = GL_ONE_MINUS_SRC_ALPHA;
181 break;
182 case RS_BLEND_SRC_DST_ALPHA:
183 mBlendSrc = GL_DST_ALPHA;
184 break;
185 case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
186 mBlendSrc = GL_ONE_MINUS_DST_ALPHA;
187 break;
188 case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
189 mBlendSrc = GL_SRC_ALPHA_SATURATE;
190 break;
191 }
192
193 switch(dst) {
194 case RS_BLEND_DST_ZERO:
195 mBlendDst = GL_ZERO;
196 break;
197 case RS_BLEND_DST_ONE:
198 mBlendDst = GL_ONE;
199 break;
200 case RS_BLEND_DST_SRC_COLOR:
201 mBlendDst = GL_SRC_COLOR;
202 break;
203 case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
204 mBlendDst = GL_ONE_MINUS_SRC_COLOR;
205 break;
206 case RS_BLEND_DST_SRC_ALPHA:
207 mBlendDst = GL_SRC_ALPHA;
208 break;
209 case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
210 mBlendDst = GL_ONE_MINUS_SRC_ALPHA;
211 break;
212 case RS_BLEND_DST_DST_ALPHA:
213 mBlendDst = GL_DST_ALPHA;
214 break;
215 case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
216 mBlendDst = GL_ONE_MINUS_DST_ALPHA;
217 break;
218 }
219}
220
Jason Samsccc010b2010-05-13 18:30:11 -0700221void ProgramStore::setColorMask(bool r, bool g, bool b, bool a)
Jason Sams326e0dd2009-05-22 14:03:28 -0700222{
223 mColorRWriteEnable = r;
224 mColorGWriteEnable = g;
225 mColorBWriteEnable = b;
226 mColorAWriteEnable = a;
227}
228
229
Jason Samsccc010b2010-05-13 18:30:11 -0700230ProgramStoreState::ProgramStoreState()
Jason Sams326e0dd2009-05-22 14:03:28 -0700231{
232 mPFS = NULL;
233}
234
Jason Samsccc010b2010-05-13 18:30:11 -0700235ProgramStoreState::~ProgramStoreState()
Jason Sams326e0dd2009-05-22 14:03:28 -0700236{
Jason Sams225afd32010-10-21 14:06:55 -0700237 ObjectBase::checkDelete(mPFS);
238 mPFS = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700239}
240
Jason Sams771565f2010-05-14 15:30:29 -0700241void ProgramStoreState::init(Context *rsc)
Jason Sams8ce125b2009-06-17 16:52:59 -0700242{
Jason Samsccc010b2010-05-13 18:30:11 -0700243 ProgramStore *pfs = new ProgramStore(rsc);
Jason Sams8ce125b2009-06-17 16:52:59 -0700244 mDefault.set(pfs);
245}
246
Jason Samsccc010b2010-05-13 18:30:11 -0700247void ProgramStoreState::deinit(Context *rsc)
Jason Samsf2649a92009-09-25 16:37:33 -0700248{
249 mDefault.clear();
250 mLast.clear();
251}
252
Jason Sams326e0dd2009-05-22 14:03:28 -0700253
Jason Sams326e0dd2009-05-22 14:03:28 -0700254namespace android {
255namespace renderscript {
256
Jason Samsccc010b2010-05-13 18:30:11 -0700257void rsi_ProgramStoreBegin(Context * rsc, RsElement in, RsElement out)
Jason Sams326e0dd2009-05-22 14:03:28 -0700258{
Jason Sams225afd32010-10-21 14:06:55 -0700259 ObjectBase::checkDelete(rsc->mStateFragmentStore.mPFS);
Jason Samsccc010b2010-05-13 18:30:11 -0700260 rsc->mStateFragmentStore.mPFS = new ProgramStore(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700261}
262
Jason Samsccc010b2010-05-13 18:30:11 -0700263void rsi_ProgramStoreDepthFunc(Context *rsc, RsDepthFunc func)
Jason Sams326e0dd2009-05-22 14:03:28 -0700264{
265 rsc->mStateFragmentStore.mPFS->setDepthFunc(func);
266}
267
Jason Samsccc010b2010-05-13 18:30:11 -0700268void rsi_ProgramStoreDepthMask(Context *rsc, bool mask)
Jason Sams326e0dd2009-05-22 14:03:28 -0700269{
270 rsc->mStateFragmentStore.mPFS->setDepthMask(mask);
271}
272
Jason Samsccc010b2010-05-13 18:30:11 -0700273void rsi_ProgramStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a)
Jason Sams326e0dd2009-05-22 14:03:28 -0700274{
275 rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a);
276}
277
Jason Samsccc010b2010-05-13 18:30:11 -0700278void rsi_ProgramStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst)
Jason Sams326e0dd2009-05-22 14:03:28 -0700279{
280 rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst);
281}
282
Jason Samsccc010b2010-05-13 18:30:11 -0700283RsProgramStore rsi_ProgramStoreCreate(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700284{
Jason Samsccc010b2010-05-13 18:30:11 -0700285 ProgramStore *pfs = rsc->mStateFragmentStore.mPFS;
Jason Sams9397e302009-08-27 20:23:34 -0700286 pfs->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700287 rsc->mStateFragmentStore.mPFS = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700288 return pfs;
289}
290
Jason Samsccc010b2010-05-13 18:30:11 -0700291void rsi_ProgramStoreDither(Context *rsc, bool enable)
Jason Sams326e0dd2009-05-22 14:03:28 -0700292{
293 rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
294}
295
Jason Sams326e0dd2009-05-22 14:03:28 -0700296
297}
298}