blob: de33d9c5ed0432881044eff3a866b4fe14276276 [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
17#include "rsContext.h"
18#include "rsProgramFragmentStore.h"
19
Jason Sams1aa5a4e2009-06-22 17:15:15 -070020#include <GLES/gl.h>
21#include <GLES/glext.h>
22
Jason Sams326e0dd2009-05-22 14:03:28 -070023using namespace android;
24using namespace android::renderscript;
25
26
Jason Samse514b452009-09-25 14:51:22 -070027ProgramFragmentStore::ProgramFragmentStore(Context *rsc, Element *in, Element *out) :
28 Program(rsc, in, out)
Jason Sams326e0dd2009-05-22 14:03:28 -070029{
Jason Samsf2649a92009-09-25 16:37:33 -070030 mAllocFile = __FILE__;
31 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070032 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
49ProgramFragmentStore::~ProgramFragmentStore()
50{
51}
52
Jason Samsafcb25c2009-08-25 11:34:49 -070053void ProgramFragmentStore::setupGL(const Context *rsc, ProgramFragmentStoreState *state)
Jason Sams326e0dd2009-05-22 14:03:28 -070054{
Jason Samscfb1d112009-08-05 13:57:03 -070055 if (state->mLast.get() == this) {
56 return;
57 }
58 state->mLast.set(this);
59
Jason Sams326e0dd2009-05-22 14:03:28 -070060 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
Jason Samsfd10b712009-07-15 18:35:54 -070071 //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
72
Jason Sams326e0dd2009-05-22 14:03:28 -070073 glDepthMask(mDepthWriteEnable);
Jason Samsfd10b712009-07-15 18:35:54 -070074 if(mDepthTestEnable || mDepthWriteEnable) {
Jason Sams326e0dd2009-05-22 14:03:28 -070075 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}
89
90void ProgramFragmentStore::setDitherEnable(bool enable)
91{
92 mDitherEnable = enable;
93}
94
95void ProgramFragmentStore::setDepthFunc(RsDepthFunc func)
96{
97 mDepthTestEnable = true;
98
99 switch(func) {
100 case RS_DEPTH_FUNC_ALWAYS:
101 mDepthTestEnable = false;
102 mDepthFunc = GL_ALWAYS;
103 break;
104 case RS_DEPTH_FUNC_LESS:
105 mDepthFunc = GL_LESS;
106 break;
107 case RS_DEPTH_FUNC_LEQUAL:
108 mDepthFunc = GL_LEQUAL;
109 break;
110 case RS_DEPTH_FUNC_GREATER:
111 mDepthFunc = GL_GREATER;
112 break;
113 case RS_DEPTH_FUNC_GEQUAL:
114 mDepthFunc = GL_GEQUAL;
115 break;
116 case RS_DEPTH_FUNC_EQUAL:
117 mDepthFunc = GL_EQUAL;
118 break;
119 case RS_DEPTH_FUNC_NOTEQUAL:
120 mDepthFunc = GL_NOTEQUAL;
121 break;
122 }
123}
124
125void ProgramFragmentStore::setDepthMask(bool mask)
126{
127 mDepthWriteEnable = mask;
128}
129
130void ProgramFragmentStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst)
131{
132 mBlendEnable = true;
Jason Samscfb1d112009-08-05 13:57:03 -0700133 if ((src == RS_BLEND_SRC_ONE) &&
Jason Sams326e0dd2009-05-22 14:03:28 -0700134 (dst == RS_BLEND_DST_ZERO)) {
135 mBlendEnable = false;
136 }
137
138 switch(src) {
139 case RS_BLEND_SRC_ZERO:
140 mBlendSrc = GL_ZERO;
141 break;
142 case RS_BLEND_SRC_ONE:
143 mBlendSrc = GL_ONE;
144 break;
145 case RS_BLEND_SRC_DST_COLOR:
146 mBlendSrc = GL_DST_COLOR;
147 break;
148 case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
149 mBlendSrc = GL_ONE_MINUS_DST_COLOR;
150 break;
151 case RS_BLEND_SRC_SRC_ALPHA:
152 mBlendSrc = GL_SRC_ALPHA;
153 break;
154 case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
155 mBlendSrc = GL_ONE_MINUS_SRC_ALPHA;
156 break;
157 case RS_BLEND_SRC_DST_ALPHA:
158 mBlendSrc = GL_DST_ALPHA;
159 break;
160 case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
161 mBlendSrc = GL_ONE_MINUS_DST_ALPHA;
162 break;
163 case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
164 mBlendSrc = GL_SRC_ALPHA_SATURATE;
165 break;
166 }
167
168 switch(dst) {
169 case RS_BLEND_DST_ZERO:
170 mBlendDst = GL_ZERO;
171 break;
172 case RS_BLEND_DST_ONE:
173 mBlendDst = GL_ONE;
174 break;
175 case RS_BLEND_DST_SRC_COLOR:
176 mBlendDst = GL_SRC_COLOR;
177 break;
178 case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
179 mBlendDst = GL_ONE_MINUS_SRC_COLOR;
180 break;
181 case RS_BLEND_DST_SRC_ALPHA:
182 mBlendDst = GL_SRC_ALPHA;
183 break;
184 case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
185 mBlendDst = GL_ONE_MINUS_SRC_ALPHA;
186 break;
187 case RS_BLEND_DST_DST_ALPHA:
188 mBlendDst = GL_DST_ALPHA;
189 break;
190 case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
191 mBlendDst = GL_ONE_MINUS_DST_ALPHA;
192 break;
193 }
194}
195
196void ProgramFragmentStore::setColorMask(bool r, bool g, bool b, bool a)
197{
198 mColorRWriteEnable = r;
199 mColorGWriteEnable = g;
200 mColorBWriteEnable = b;
201 mColorAWriteEnable = a;
202}
203
204
205ProgramFragmentStoreState::ProgramFragmentStoreState()
206{
207 mPFS = NULL;
208}
209
210ProgramFragmentStoreState::~ProgramFragmentStoreState()
211{
212 delete mPFS;
213
214}
215
Jason Sams8ce125b2009-06-17 16:52:59 -0700216void ProgramFragmentStoreState::init(Context *rsc, int32_t w, int32_t h)
217{
Jason Samse514b452009-09-25 14:51:22 -0700218 ProgramFragmentStore *pfs = new ProgramFragmentStore(rsc, NULL, NULL);
Jason Sams8ce125b2009-06-17 16:52:59 -0700219 mDefault.set(pfs);
220}
221
Jason Samsf2649a92009-09-25 16:37:33 -0700222void ProgramFragmentStoreState::deinit(Context *rsc)
223{
224 mDefault.clear();
225 mLast.clear();
226}
227
Jason Sams326e0dd2009-05-22 14:03:28 -0700228
Jason Sams326e0dd2009-05-22 14:03:28 -0700229namespace android {
230namespace renderscript {
231
232void rsi_ProgramFragmentStoreBegin(Context * rsc, RsElement in, RsElement out)
233{
234 delete rsc->mStateFragmentStore.mPFS;
Jason Samse514b452009-09-25 14:51:22 -0700235 rsc->mStateFragmentStore.mPFS = new ProgramFragmentStore(rsc, (Element *)in, (Element *)out);
Jason Sams326e0dd2009-05-22 14:03:28 -0700236
237}
238
239void rsi_ProgramFragmentStoreDepthFunc(Context *rsc, RsDepthFunc func)
240{
241 rsc->mStateFragmentStore.mPFS->setDepthFunc(func);
242}
243
244void rsi_ProgramFragmentStoreDepthMask(Context *rsc, bool mask)
245{
246 rsc->mStateFragmentStore.mPFS->setDepthMask(mask);
247}
248
249void rsi_ProgramFragmentStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a)
250{
251 rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a);
252}
253
254void rsi_ProgramFragmentStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst)
255{
256 rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst);
257}
258
259RsProgramFragmentStore rsi_ProgramFragmentStoreCreate(Context *rsc)
260{
261 ProgramFragmentStore *pfs = rsc->mStateFragmentStore.mPFS;
Jason Sams9397e302009-08-27 20:23:34 -0700262 pfs->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700263 rsc->mStateFragmentStore.mPFS = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700264 return pfs;
265}
266
267void rsi_ProgramFragmentStoreDither(Context *rsc, bool enable)
268{
269 rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
270}
271
Jason Sams326e0dd2009-05-22 14:03:28 -0700272
273}
274}