blob: e741c0a5a484e2b8ee073aaf5ed4269f353ec327 [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{
Jason Samsf2649a92009-09-25 16:37:33 -070036 mAllocFile = __FILE__;
37 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070038 mDitherEnable = true;
39 mBlendEnable = false;
40 mColorRWriteEnable = true;
41 mColorGWriteEnable = true;
42 mColorBWriteEnable = true;
43 mColorAWriteEnable = true;
44 mBlendSrc = GL_ONE;
45 mBlendDst = GL_ZERO;
46
47
48 mDepthTestEnable = false;
49 mDepthWriteEnable = true;
50 mDepthFunc = GL_LESS;
51
52
53}
54
Jason Samsccc010b2010-05-13 18:30:11 -070055ProgramStore::~ProgramStore()
Jason Sams326e0dd2009-05-22 14:03:28 -070056{
57}
58
Jason Samsccc010b2010-05-13 18:30:11 -070059void ProgramStore::setupGL(const Context *rsc, ProgramStoreState *state)
Jason Sams326e0dd2009-05-22 14:03:28 -070060{
Jason Samscfb1d112009-08-05 13:57:03 -070061 if (state->mLast.get() == this) {
62 return;
63 }
64 state->mLast.set(this);
65
Jason Sams326e0dd2009-05-22 14:03:28 -070066 glColorMask(mColorRWriteEnable,
67 mColorGWriteEnable,
68 mColorBWriteEnable,
69 mColorAWriteEnable);
70 if (mBlendEnable) {
71 glEnable(GL_BLEND);
72 glBlendFunc(mBlendSrc, mBlendDst);
73 } else {
74 glDisable(GL_BLEND);
75 }
76
Jason Samsfd10b712009-07-15 18:35:54 -070077 //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
78
Jason Sams326e0dd2009-05-22 14:03:28 -070079 glDepthMask(mDepthWriteEnable);
Jason Samsfd10b712009-07-15 18:35:54 -070080 if(mDepthTestEnable || mDepthWriteEnable) {
Jason Sams326e0dd2009-05-22 14:03:28 -070081 glEnable(GL_DEPTH_TEST);
82 glDepthFunc(mDepthFunc);
83 } else {
84 glDisable(GL_DEPTH_TEST);
85 }
86
87 if (mDitherEnable) {
88 glEnable(GL_DITHER);
89 } else {
90 glDisable(GL_DITHER);
91 }
Jason Sams326e0dd2009-05-22 14:03:28 -070092}
93
Jason Samsccc010b2010-05-13 18:30:11 -070094void ProgramStore::setupGL2(const Context *rsc, ProgramStoreState *state)
Jason Samsc460e552009-11-25 13:22:07 -080095{
96 if (state->mLast.get() == this) {
97 return;
98 }
99 state->mLast.set(this);
100
101 glColorMask(mColorRWriteEnable,
102 mColorGWriteEnable,
103 mColorBWriteEnable,
104 mColorAWriteEnable);
105 if (mBlendEnable) {
106 glEnable(GL_BLEND);
107 glBlendFunc(mBlendSrc, mBlendDst);
108 } else {
109 glDisable(GL_BLEND);
110 }
111
112 //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
113
114 glDepthMask(mDepthWriteEnable);
115 if(mDepthTestEnable || mDepthWriteEnable) {
116 glEnable(GL_DEPTH_TEST);
117 glDepthFunc(mDepthFunc);
118 } else {
119 glDisable(GL_DEPTH_TEST);
120 }
121
122 if (mDitherEnable) {
123 glEnable(GL_DITHER);
124 } else {
125 glDisable(GL_DITHER);
126 }
127}
128
129
Jason Samsccc010b2010-05-13 18:30:11 -0700130void ProgramStore::setDitherEnable(bool enable)
Jason Sams326e0dd2009-05-22 14:03:28 -0700131{
132 mDitherEnable = enable;
133}
134
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700135void ProgramStore::serialize(OStream *stream) const
136{
137
138}
139
140ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream)
141{
142 return NULL;
143}
144
145
Jason Samsccc010b2010-05-13 18:30:11 -0700146void ProgramStore::setDepthFunc(RsDepthFunc func)
Jason Sams326e0dd2009-05-22 14:03:28 -0700147{
148 mDepthTestEnable = true;
149
150 switch(func) {
151 case RS_DEPTH_FUNC_ALWAYS:
152 mDepthTestEnable = false;
153 mDepthFunc = GL_ALWAYS;
154 break;
155 case RS_DEPTH_FUNC_LESS:
156 mDepthFunc = GL_LESS;
157 break;
158 case RS_DEPTH_FUNC_LEQUAL:
159 mDepthFunc = GL_LEQUAL;
160 break;
161 case RS_DEPTH_FUNC_GREATER:
162 mDepthFunc = GL_GREATER;
163 break;
164 case RS_DEPTH_FUNC_GEQUAL:
165 mDepthFunc = GL_GEQUAL;
166 break;
167 case RS_DEPTH_FUNC_EQUAL:
168 mDepthFunc = GL_EQUAL;
169 break;
170 case RS_DEPTH_FUNC_NOTEQUAL:
171 mDepthFunc = GL_NOTEQUAL;
172 break;
173 }
174}
175
Jason Samsccc010b2010-05-13 18:30:11 -0700176void ProgramStore::setDepthMask(bool mask)
Jason Sams326e0dd2009-05-22 14:03:28 -0700177{
178 mDepthWriteEnable = mask;
179}
180
Jason Samsccc010b2010-05-13 18:30:11 -0700181void ProgramStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst)
Jason Sams326e0dd2009-05-22 14:03:28 -0700182{
183 mBlendEnable = true;
Jason Samscfb1d112009-08-05 13:57:03 -0700184 if ((src == RS_BLEND_SRC_ONE) &&
Jason Sams326e0dd2009-05-22 14:03:28 -0700185 (dst == RS_BLEND_DST_ZERO)) {
186 mBlendEnable = false;
187 }
188
189 switch(src) {
190 case RS_BLEND_SRC_ZERO:
191 mBlendSrc = GL_ZERO;
192 break;
193 case RS_BLEND_SRC_ONE:
194 mBlendSrc = GL_ONE;
195 break;
196 case RS_BLEND_SRC_DST_COLOR:
197 mBlendSrc = GL_DST_COLOR;
198 break;
199 case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
200 mBlendSrc = GL_ONE_MINUS_DST_COLOR;
201 break;
202 case RS_BLEND_SRC_SRC_ALPHA:
203 mBlendSrc = GL_SRC_ALPHA;
204 break;
205 case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
206 mBlendSrc = GL_ONE_MINUS_SRC_ALPHA;
207 break;
208 case RS_BLEND_SRC_DST_ALPHA:
209 mBlendSrc = GL_DST_ALPHA;
210 break;
211 case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
212 mBlendSrc = GL_ONE_MINUS_DST_ALPHA;
213 break;
214 case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
215 mBlendSrc = GL_SRC_ALPHA_SATURATE;
216 break;
217 }
218
219 switch(dst) {
220 case RS_BLEND_DST_ZERO:
221 mBlendDst = GL_ZERO;
222 break;
223 case RS_BLEND_DST_ONE:
224 mBlendDst = GL_ONE;
225 break;
226 case RS_BLEND_DST_SRC_COLOR:
227 mBlendDst = GL_SRC_COLOR;
228 break;
229 case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
230 mBlendDst = GL_ONE_MINUS_SRC_COLOR;
231 break;
232 case RS_BLEND_DST_SRC_ALPHA:
233 mBlendDst = GL_SRC_ALPHA;
234 break;
235 case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
236 mBlendDst = GL_ONE_MINUS_SRC_ALPHA;
237 break;
238 case RS_BLEND_DST_DST_ALPHA:
239 mBlendDst = GL_DST_ALPHA;
240 break;
241 case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
242 mBlendDst = GL_ONE_MINUS_DST_ALPHA;
243 break;
244 }
245}
246
Jason Samsccc010b2010-05-13 18:30:11 -0700247void ProgramStore::setColorMask(bool r, bool g, bool b, bool a)
Jason Sams326e0dd2009-05-22 14:03:28 -0700248{
249 mColorRWriteEnable = r;
250 mColorGWriteEnable = g;
251 mColorBWriteEnable = b;
252 mColorAWriteEnable = a;
253}
254
255
Jason Samsccc010b2010-05-13 18:30:11 -0700256ProgramStoreState::ProgramStoreState()
Jason Sams326e0dd2009-05-22 14:03:28 -0700257{
258 mPFS = NULL;
259}
260
Jason Samsccc010b2010-05-13 18:30:11 -0700261ProgramStoreState::~ProgramStoreState()
Jason Sams326e0dd2009-05-22 14:03:28 -0700262{
263 delete mPFS;
264
265}
266
Jason Sams771565f2010-05-14 15:30:29 -0700267void ProgramStoreState::init(Context *rsc)
Jason Sams8ce125b2009-06-17 16:52:59 -0700268{
Jason Samsccc010b2010-05-13 18:30:11 -0700269 ProgramStore *pfs = new ProgramStore(rsc);
Jason Sams8ce125b2009-06-17 16:52:59 -0700270 mDefault.set(pfs);
271}
272
Jason Samsccc010b2010-05-13 18:30:11 -0700273void ProgramStoreState::deinit(Context *rsc)
Jason Samsf2649a92009-09-25 16:37:33 -0700274{
275 mDefault.clear();
276 mLast.clear();
277}
278
Jason Sams326e0dd2009-05-22 14:03:28 -0700279
Jason Sams326e0dd2009-05-22 14:03:28 -0700280namespace android {
281namespace renderscript {
282
Jason Samsccc010b2010-05-13 18:30:11 -0700283void rsi_ProgramStoreBegin(Context * rsc, RsElement in, RsElement out)
Jason Sams326e0dd2009-05-22 14:03:28 -0700284{
285 delete rsc->mStateFragmentStore.mPFS;
Jason Samsccc010b2010-05-13 18:30:11 -0700286 rsc->mStateFragmentStore.mPFS = new ProgramStore(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700287
288}
289
Jason Samsccc010b2010-05-13 18:30:11 -0700290void rsi_ProgramStoreDepthFunc(Context *rsc, RsDepthFunc func)
Jason Sams326e0dd2009-05-22 14:03:28 -0700291{
292 rsc->mStateFragmentStore.mPFS->setDepthFunc(func);
293}
294
Jason Samsccc010b2010-05-13 18:30:11 -0700295void rsi_ProgramStoreDepthMask(Context *rsc, bool mask)
Jason Sams326e0dd2009-05-22 14:03:28 -0700296{
297 rsc->mStateFragmentStore.mPFS->setDepthMask(mask);
298}
299
Jason Samsccc010b2010-05-13 18:30:11 -0700300void rsi_ProgramStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a)
Jason Sams326e0dd2009-05-22 14:03:28 -0700301{
302 rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a);
303}
304
Jason Samsccc010b2010-05-13 18:30:11 -0700305void rsi_ProgramStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst)
Jason Sams326e0dd2009-05-22 14:03:28 -0700306{
307 rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst);
308}
309
Jason Samsccc010b2010-05-13 18:30:11 -0700310RsProgramStore rsi_ProgramStoreCreate(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700311{
Jason Samsccc010b2010-05-13 18:30:11 -0700312 ProgramStore *pfs = rsc->mStateFragmentStore.mPFS;
Jason Sams9397e302009-08-27 20:23:34 -0700313 pfs->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700314 rsc->mStateFragmentStore.mPFS = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700315 return pfs;
316}
317
Jason Samsccc010b2010-05-13 18:30:11 -0700318void rsi_ProgramStoreDither(Context *rsc, bool enable)
Jason Sams326e0dd2009-05-22 14:03:28 -0700319{
320 rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
321}
322
Jason Sams326e0dd2009-05-22 14:03:28 -0700323
324}
325}