blob: 3f90d7ac88dc83b7383d53bff74731f380b8e273 [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
79 glDepthMask(mDepthWriteEnable);
80 if(mDepthTestEnable || mDepthWriteEnable) {
81 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 }
92}
93
94
Jason Sams54db59c2010-05-13 18:30:11 -070095void ProgramStore::setDitherEnable(bool enable)
Jason Samsd19f10d2009-05-22 14:03:28 -070096{
97 mDitherEnable = enable;
98}
99
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700100void ProgramStore::serialize(OStream *stream) const
101{
102
103}
104
105ProgramStore *ProgramStore::createFromStream(Context *rsc, IStream *stream)
106{
107 return NULL;
108}
109
110
Jason Sams54db59c2010-05-13 18:30:11 -0700111void ProgramStore::setDepthFunc(RsDepthFunc func)
Jason Samsd19f10d2009-05-22 14:03:28 -0700112{
113 mDepthTestEnable = true;
114
115 switch(func) {
116 case RS_DEPTH_FUNC_ALWAYS:
117 mDepthTestEnable = false;
118 mDepthFunc = GL_ALWAYS;
119 break;
120 case RS_DEPTH_FUNC_LESS:
121 mDepthFunc = GL_LESS;
122 break;
123 case RS_DEPTH_FUNC_LEQUAL:
124 mDepthFunc = GL_LEQUAL;
125 break;
126 case RS_DEPTH_FUNC_GREATER:
127 mDepthFunc = GL_GREATER;
128 break;
129 case RS_DEPTH_FUNC_GEQUAL:
130 mDepthFunc = GL_GEQUAL;
131 break;
132 case RS_DEPTH_FUNC_EQUAL:
133 mDepthFunc = GL_EQUAL;
134 break;
135 case RS_DEPTH_FUNC_NOTEQUAL:
136 mDepthFunc = GL_NOTEQUAL;
137 break;
138 }
139}
140
Jason Sams54db59c2010-05-13 18:30:11 -0700141void ProgramStore::setDepthMask(bool mask)
Jason Samsd19f10d2009-05-22 14:03:28 -0700142{
143 mDepthWriteEnable = mask;
144}
145
Jason Sams54db59c2010-05-13 18:30:11 -0700146void ProgramStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst)
Jason Samsd19f10d2009-05-22 14:03:28 -0700147{
148 mBlendEnable = true;
Jason Sams9bee51c2009-08-05 13:57:03 -0700149 if ((src == RS_BLEND_SRC_ONE) &&
Jason Samsd19f10d2009-05-22 14:03:28 -0700150 (dst == RS_BLEND_DST_ZERO)) {
151 mBlendEnable = false;
152 }
153
154 switch(src) {
155 case RS_BLEND_SRC_ZERO:
156 mBlendSrc = GL_ZERO;
157 break;
158 case RS_BLEND_SRC_ONE:
159 mBlendSrc = GL_ONE;
160 break;
161 case RS_BLEND_SRC_DST_COLOR:
162 mBlendSrc = GL_DST_COLOR;
163 break;
164 case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
165 mBlendSrc = GL_ONE_MINUS_DST_COLOR;
166 break;
167 case RS_BLEND_SRC_SRC_ALPHA:
168 mBlendSrc = GL_SRC_ALPHA;
169 break;
170 case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
171 mBlendSrc = GL_ONE_MINUS_SRC_ALPHA;
172 break;
173 case RS_BLEND_SRC_DST_ALPHA:
174 mBlendSrc = GL_DST_ALPHA;
175 break;
176 case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
177 mBlendSrc = GL_ONE_MINUS_DST_ALPHA;
178 break;
179 case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
180 mBlendSrc = GL_SRC_ALPHA_SATURATE;
181 break;
182 }
183
184 switch(dst) {
185 case RS_BLEND_DST_ZERO:
186 mBlendDst = GL_ZERO;
187 break;
188 case RS_BLEND_DST_ONE:
189 mBlendDst = GL_ONE;
190 break;
191 case RS_BLEND_DST_SRC_COLOR:
192 mBlendDst = GL_SRC_COLOR;
193 break;
194 case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
195 mBlendDst = GL_ONE_MINUS_SRC_COLOR;
196 break;
197 case RS_BLEND_DST_SRC_ALPHA:
198 mBlendDst = GL_SRC_ALPHA;
199 break;
200 case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
201 mBlendDst = GL_ONE_MINUS_SRC_ALPHA;
202 break;
203 case RS_BLEND_DST_DST_ALPHA:
204 mBlendDst = GL_DST_ALPHA;
205 break;
206 case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
207 mBlendDst = GL_ONE_MINUS_DST_ALPHA;
208 break;
209 }
210}
211
Jason Sams54db59c2010-05-13 18:30:11 -0700212void ProgramStore::setColorMask(bool r, bool g, bool b, bool a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700213{
214 mColorRWriteEnable = r;
215 mColorGWriteEnable = g;
216 mColorBWriteEnable = b;
217 mColorAWriteEnable = a;
218}
219
220
Jason Sams54db59c2010-05-13 18:30:11 -0700221ProgramStoreState::ProgramStoreState()
Jason Samsd19f10d2009-05-22 14:03:28 -0700222{
223 mPFS = NULL;
224}
225
Jason Sams54db59c2010-05-13 18:30:11 -0700226ProgramStoreState::~ProgramStoreState()
Jason Samsd19f10d2009-05-22 14:03:28 -0700227{
228 delete mPFS;
229
230}
231
Jason Samsf603d212010-05-14 15:30:29 -0700232void ProgramStoreState::init(Context *rsc)
Jason Sams9c54bdb2009-06-17 16:52:59 -0700233{
Jason Sams54db59c2010-05-13 18:30:11 -0700234 ProgramStore *pfs = new ProgramStore(rsc);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700235 mDefault.set(pfs);
236}
237
Jason Sams54db59c2010-05-13 18:30:11 -0700238void ProgramStoreState::deinit(Context *rsc)
Jason Sams61f08d62009-09-25 16:37:33 -0700239{
240 mDefault.clear();
241 mLast.clear();
242}
243
Jason Samsd19f10d2009-05-22 14:03:28 -0700244
Jason Samsd19f10d2009-05-22 14:03:28 -0700245namespace android {
246namespace renderscript {
247
Jason Sams54db59c2010-05-13 18:30:11 -0700248void rsi_ProgramStoreBegin(Context * rsc, RsElement in, RsElement out)
Jason Samsd19f10d2009-05-22 14:03:28 -0700249{
250 delete rsc->mStateFragmentStore.mPFS;
Jason Sams54db59c2010-05-13 18:30:11 -0700251 rsc->mStateFragmentStore.mPFS = new ProgramStore(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700252
253}
254
Jason Sams54db59c2010-05-13 18:30:11 -0700255void rsi_ProgramStoreDepthFunc(Context *rsc, RsDepthFunc func)
Jason Samsd19f10d2009-05-22 14:03:28 -0700256{
257 rsc->mStateFragmentStore.mPFS->setDepthFunc(func);
258}
259
Jason Sams54db59c2010-05-13 18:30:11 -0700260void rsi_ProgramStoreDepthMask(Context *rsc, bool mask)
Jason Samsd19f10d2009-05-22 14:03:28 -0700261{
262 rsc->mStateFragmentStore.mPFS->setDepthMask(mask);
263}
264
Jason Sams54db59c2010-05-13 18:30:11 -0700265void rsi_ProgramStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700266{
267 rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a);
268}
269
Jason Sams54db59c2010-05-13 18:30:11 -0700270void rsi_ProgramStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst)
Jason Samsd19f10d2009-05-22 14:03:28 -0700271{
272 rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst);
273}
274
Jason Sams54db59c2010-05-13 18:30:11 -0700275RsProgramStore rsi_ProgramStoreCreate(Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700276{
Jason Sams54db59c2010-05-13 18:30:11 -0700277 ProgramStore *pfs = rsc->mStateFragmentStore.mPFS;
Jason Sams07ae4062009-08-27 20:23:34 -0700278 pfs->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700279 rsc->mStateFragmentStore.mPFS = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700280 return pfs;
281}
282
Jason Sams54db59c2010-05-13 18:30:11 -0700283void rsi_ProgramStoreDither(Context *rsc, bool enable)
Jason Samsd19f10d2009-05-22 14:03:28 -0700284{
285 rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
286}
287
Jason Samsd19f10d2009-05-22 14:03:28 -0700288
289}
290}