blob: 586a89fd1f60007f5caed12bb998efe48cc05b00 [file] [log] [blame]
Jason Samsd19f10d2009-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 Sakhartchoukaa7d2882010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsd19f10d2009-05-22 14:03:28 -070018#include "rsContext.h"
Jason Sams4b962e52009-06-22 17:15:15 -070019#include <GLES/gl.h>
20#include <GLES/glext.h>
Alex Sakhartchoukaa7d2882010-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 Sams4b962e52009-06-22 17:15:15 -070028
Jason Samsd19f10d2009-05-22 14:03:28 -070029using namespace android;
30using namespace android::renderscript;
31
32
Jason Sams54db59c2010-05-13 18:30:11 -070033ProgramStore::ProgramStore(Context *rsc) :
Jason Sams0011bcf2009-12-15 12:58:36 -080034 Program(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070035{
Jason Sams61f08d62009-09-25 16:37:33 -070036 mAllocFile = __FILE__;
37 mAllocLine = __LINE__;
Jason Samsd19f10d2009-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 Sams54db59c2010-05-13 18:30:11 -070055ProgramStore::~ProgramStore()
Jason Samsd19f10d2009-05-22 14:03:28 -070056{
57}
58
Jason Sams54db59c2010-05-13 18:30:11 -070059void ProgramStore::setupGL2(const Context *rsc, ProgramStoreState *state)
Jason Samsbb51c402009-11-25 13:22:07 -080060{
61 if (state->mLast.get() == this) {
62 return;
63 }
64 state->mLast.set(this);
65
66 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
77 //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
78
Jason Sams11c8af92010-10-13 15:31:10 -070079 if (rsc->mUserSurfaceConfig.depthMin > 0) {
80 glDepthMask(mDepthWriteEnable);
81 if(mDepthTestEnable || mDepthWriteEnable) {
82 glEnable(GL_DEPTH_TEST);
83 glDepthFunc(mDepthFunc);
84 } else {
85 glDisable(GL_DEPTH_TEST);
86 }
Jason Samsbb51c402009-11-25 13:22:07 -080087 } else {
Jason Sams11c8af92010-10-13 15:31:10 -070088 glDepthMask(false);
Jason Samsbb51c402009-11-25 13:22:07 -080089 glDisable(GL_DEPTH_TEST);
90 }
91
Jason Sams11c8af92010-10-13 15:31:10 -070092 if (rsc->mUserSurfaceConfig.stencilMin > 0) {
93 } else {
94 glStencilMask(0);
95 glDisable(GL_STENCIL_TEST);
96 }
97
Jason Samsbb51c402009-11-25 13:22:07 -080098 if (mDitherEnable) {
99 glEnable(GL_DITHER);
100 } else {
101 glDisable(GL_DITHER);
102 }
103}
104
105
Jason Sams54db59c2010-05-13 18:30:11 -0700106void ProgramStore::setDitherEnable(bool enable)
Jason Samsd19f10d2009-05-22 14:03:28 -0700107{
108 mDitherEnable = enable;
109}
110
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700111void ProgramStore::serialize(OStream *stream) const
112{
113
114}
115
116ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream)
117{
118 return NULL;
119}
120
121
Jason Sams54db59c2010-05-13 18:30:11 -0700122void ProgramStore::setDepthFunc(RsDepthFunc func)
Jason Samsd19f10d2009-05-22 14:03:28 -0700123{
124 mDepthTestEnable = true;
125
126 switch(func) {
127 case RS_DEPTH_FUNC_ALWAYS:
128 mDepthTestEnable = false;
129 mDepthFunc = GL_ALWAYS;
130 break;
131 case RS_DEPTH_FUNC_LESS:
132 mDepthFunc = GL_LESS;
133 break;
134 case RS_DEPTH_FUNC_LEQUAL:
135 mDepthFunc = GL_LEQUAL;
136 break;
137 case RS_DEPTH_FUNC_GREATER:
138 mDepthFunc = GL_GREATER;
139 break;
140 case RS_DEPTH_FUNC_GEQUAL:
141 mDepthFunc = GL_GEQUAL;
142 break;
143 case RS_DEPTH_FUNC_EQUAL:
144 mDepthFunc = GL_EQUAL;
145 break;
146 case RS_DEPTH_FUNC_NOTEQUAL:
147 mDepthFunc = GL_NOTEQUAL;
148 break;
149 }
150}
151
Jason Sams54db59c2010-05-13 18:30:11 -0700152void ProgramStore::setDepthMask(bool mask)
Jason Samsd19f10d2009-05-22 14:03:28 -0700153{
154 mDepthWriteEnable = mask;
155}
156
Jason Sams54db59c2010-05-13 18:30:11 -0700157void ProgramStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst)
Jason Samsd19f10d2009-05-22 14:03:28 -0700158{
159 mBlendEnable = true;
Jason Sams9bee51c2009-08-05 13:57:03 -0700160 if ((src == RS_BLEND_SRC_ONE) &&
Jason Samsd19f10d2009-05-22 14:03:28 -0700161 (dst == RS_BLEND_DST_ZERO)) {
162 mBlendEnable = false;
163 }
164
165 switch(src) {
166 case RS_BLEND_SRC_ZERO:
167 mBlendSrc = GL_ZERO;
168 break;
169 case RS_BLEND_SRC_ONE:
170 mBlendSrc = GL_ONE;
171 break;
172 case RS_BLEND_SRC_DST_COLOR:
173 mBlendSrc = GL_DST_COLOR;
174 break;
175 case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
176 mBlendSrc = GL_ONE_MINUS_DST_COLOR;
177 break;
178 case RS_BLEND_SRC_SRC_ALPHA:
179 mBlendSrc = GL_SRC_ALPHA;
180 break;
181 case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
182 mBlendSrc = GL_ONE_MINUS_SRC_ALPHA;
183 break;
184 case RS_BLEND_SRC_DST_ALPHA:
185 mBlendSrc = GL_DST_ALPHA;
186 break;
187 case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
188 mBlendSrc = GL_ONE_MINUS_DST_ALPHA;
189 break;
190 case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
191 mBlendSrc = GL_SRC_ALPHA_SATURATE;
192 break;
193 }
194
195 switch(dst) {
196 case RS_BLEND_DST_ZERO:
197 mBlendDst = GL_ZERO;
198 break;
199 case RS_BLEND_DST_ONE:
200 mBlendDst = GL_ONE;
201 break;
202 case RS_BLEND_DST_SRC_COLOR:
203 mBlendDst = GL_SRC_COLOR;
204 break;
205 case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
206 mBlendDst = GL_ONE_MINUS_SRC_COLOR;
207 break;
208 case RS_BLEND_DST_SRC_ALPHA:
209 mBlendDst = GL_SRC_ALPHA;
210 break;
211 case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
212 mBlendDst = GL_ONE_MINUS_SRC_ALPHA;
213 break;
214 case RS_BLEND_DST_DST_ALPHA:
215 mBlendDst = GL_DST_ALPHA;
216 break;
217 case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
218 mBlendDst = GL_ONE_MINUS_DST_ALPHA;
219 break;
220 }
221}
222
Jason Sams54db59c2010-05-13 18:30:11 -0700223void ProgramStore::setColorMask(bool r, bool g, bool b, bool a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700224{
225 mColorRWriteEnable = r;
226 mColorGWriteEnable = g;
227 mColorBWriteEnable = b;
228 mColorAWriteEnable = a;
229}
230
231
Jason Sams54db59c2010-05-13 18:30:11 -0700232ProgramStoreState::ProgramStoreState()
Jason Samsd19f10d2009-05-22 14:03:28 -0700233{
234 mPFS = NULL;
235}
236
Jason Sams54db59c2010-05-13 18:30:11 -0700237ProgramStoreState::~ProgramStoreState()
Jason Samsd19f10d2009-05-22 14:03:28 -0700238{
239 delete mPFS;
240
241}
242
Jason Samsf603d212010-05-14 15:30:29 -0700243void ProgramStoreState::init(Context *rsc)
Jason Sams9c54bdb2009-06-17 16:52:59 -0700244{
Jason Sams54db59c2010-05-13 18:30:11 -0700245 ProgramStore *pfs = new ProgramStore(rsc);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700246 mDefault.set(pfs);
247}
248
Jason Sams54db59c2010-05-13 18:30:11 -0700249void ProgramStoreState::deinit(Context *rsc)
Jason Sams61f08d62009-09-25 16:37:33 -0700250{
251 mDefault.clear();
252 mLast.clear();
253}
254
Jason Samsd19f10d2009-05-22 14:03:28 -0700255
Jason Samsd19f10d2009-05-22 14:03:28 -0700256namespace android {
257namespace renderscript {
258
Jason Sams54db59c2010-05-13 18:30:11 -0700259void rsi_ProgramStoreBegin(Context * rsc, RsElement in, RsElement out)
Jason Samsd19f10d2009-05-22 14:03:28 -0700260{
261 delete rsc->mStateFragmentStore.mPFS;
Jason Sams54db59c2010-05-13 18:30:11 -0700262 rsc->mStateFragmentStore.mPFS = new ProgramStore(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700263
264}
265
Jason Sams54db59c2010-05-13 18:30:11 -0700266void rsi_ProgramStoreDepthFunc(Context *rsc, RsDepthFunc func)
Jason Samsd19f10d2009-05-22 14:03:28 -0700267{
268 rsc->mStateFragmentStore.mPFS->setDepthFunc(func);
269}
270
Jason Sams54db59c2010-05-13 18:30:11 -0700271void rsi_ProgramStoreDepthMask(Context *rsc, bool mask)
Jason Samsd19f10d2009-05-22 14:03:28 -0700272{
273 rsc->mStateFragmentStore.mPFS->setDepthMask(mask);
274}
275
Jason Sams54db59c2010-05-13 18:30:11 -0700276void rsi_ProgramStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700277{
278 rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a);
279}
280
Jason Sams54db59c2010-05-13 18:30:11 -0700281void rsi_ProgramStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst)
Jason Samsd19f10d2009-05-22 14:03:28 -0700282{
283 rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst);
284}
285
Jason Sams54db59c2010-05-13 18:30:11 -0700286RsProgramStore rsi_ProgramStoreCreate(Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700287{
Jason Sams54db59c2010-05-13 18:30:11 -0700288 ProgramStore *pfs = rsc->mStateFragmentStore.mPFS;
Jason Sams07ae4062009-08-27 20:23:34 -0700289 pfs->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700290 rsc->mStateFragmentStore.mPFS = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700291 return pfs;
292}
293
Jason Sams54db59c2010-05-13 18:30:11 -0700294void rsi_ProgramStoreDither(Context *rsc, bool enable)
Jason Samsd19f10d2009-05-22 14:03:28 -0700295{
296 rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
297}
298
Jason Samsd19f10d2009-05-22 14:03:28 -0700299
300}
301}