blob: 317948496777fdee943846a7c5f598aa346bfe11 [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
27ProgramFragmentStore::ProgramFragmentStore(Element *in, Element *out) :
28 Program(in, out)
29{
30 mDitherEnable = true;
31 mBlendEnable = false;
32 mColorRWriteEnable = true;
33 mColorGWriteEnable = true;
34 mColorBWriteEnable = true;
35 mColorAWriteEnable = true;
36 mBlendSrc = GL_ONE;
37 mBlendDst = GL_ZERO;
38
39
40 mDepthTestEnable = false;
41 mDepthWriteEnable = true;
42 mDepthFunc = GL_LESS;
43
44
45}
46
47ProgramFragmentStore::~ProgramFragmentStore()
48{
49}
50
Jason Samsafcb25c2009-08-25 11:34:49 -070051void ProgramFragmentStore::setupGL(const Context *rsc, ProgramFragmentStoreState *state)
Jason Sams326e0dd2009-05-22 14:03:28 -070052{
Jason Samscfb1d112009-08-05 13:57:03 -070053 if (state->mLast.get() == this) {
54 return;
55 }
56 state->mLast.set(this);
57
Jason Sams326e0dd2009-05-22 14:03:28 -070058 glColorMask(mColorRWriteEnable,
59 mColorGWriteEnable,
60 mColorBWriteEnable,
61 mColorAWriteEnable);
62 if (mBlendEnable) {
63 glEnable(GL_BLEND);
64 glBlendFunc(mBlendSrc, mBlendDst);
65 } else {
66 glDisable(GL_BLEND);
67 }
68
Jason Samsfd10b712009-07-15 18:35:54 -070069 //LOGE("pfs %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
70
Jason Sams326e0dd2009-05-22 14:03:28 -070071 glDepthMask(mDepthWriteEnable);
Jason Samsfd10b712009-07-15 18:35:54 -070072 if(mDepthTestEnable || mDepthWriteEnable) {
Jason Sams326e0dd2009-05-22 14:03:28 -070073 glEnable(GL_DEPTH_TEST);
74 glDepthFunc(mDepthFunc);
75 } else {
76 glDisable(GL_DEPTH_TEST);
77 }
78
79 if (mDitherEnable) {
80 glEnable(GL_DITHER);
81 } else {
82 glDisable(GL_DITHER);
83 }
84
85
86}
87
88void ProgramFragmentStore::setDitherEnable(bool enable)
89{
90 mDitherEnable = enable;
91}
92
93void ProgramFragmentStore::setDepthFunc(RsDepthFunc func)
94{
95 mDepthTestEnable = true;
96
97 switch(func) {
98 case RS_DEPTH_FUNC_ALWAYS:
99 mDepthTestEnable = false;
100 mDepthFunc = GL_ALWAYS;
101 break;
102 case RS_DEPTH_FUNC_LESS:
103 mDepthFunc = GL_LESS;
104 break;
105 case RS_DEPTH_FUNC_LEQUAL:
106 mDepthFunc = GL_LEQUAL;
107 break;
108 case RS_DEPTH_FUNC_GREATER:
109 mDepthFunc = GL_GREATER;
110 break;
111 case RS_DEPTH_FUNC_GEQUAL:
112 mDepthFunc = GL_GEQUAL;
113 break;
114 case RS_DEPTH_FUNC_EQUAL:
115 mDepthFunc = GL_EQUAL;
116 break;
117 case RS_DEPTH_FUNC_NOTEQUAL:
118 mDepthFunc = GL_NOTEQUAL;
119 break;
120 }
121}
122
123void ProgramFragmentStore::setDepthMask(bool mask)
124{
125 mDepthWriteEnable = mask;
126}
127
128void ProgramFragmentStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst)
129{
130 mBlendEnable = true;
Jason Samscfb1d112009-08-05 13:57:03 -0700131 if ((src == RS_BLEND_SRC_ONE) &&
Jason Sams326e0dd2009-05-22 14:03:28 -0700132 (dst == RS_BLEND_DST_ZERO)) {
133 mBlendEnable = false;
134 }
135
136 switch(src) {
137 case RS_BLEND_SRC_ZERO:
138 mBlendSrc = GL_ZERO;
139 break;
140 case RS_BLEND_SRC_ONE:
141 mBlendSrc = GL_ONE;
142 break;
143 case RS_BLEND_SRC_DST_COLOR:
144 mBlendSrc = GL_DST_COLOR;
145 break;
146 case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
147 mBlendSrc = GL_ONE_MINUS_DST_COLOR;
148 break;
149 case RS_BLEND_SRC_SRC_ALPHA:
150 mBlendSrc = GL_SRC_ALPHA;
151 break;
152 case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
153 mBlendSrc = GL_ONE_MINUS_SRC_ALPHA;
154 break;
155 case RS_BLEND_SRC_DST_ALPHA:
156 mBlendSrc = GL_DST_ALPHA;
157 break;
158 case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
159 mBlendSrc = GL_ONE_MINUS_DST_ALPHA;
160 break;
161 case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
162 mBlendSrc = GL_SRC_ALPHA_SATURATE;
163 break;
164 }
165
166 switch(dst) {
167 case RS_BLEND_DST_ZERO:
168 mBlendDst = GL_ZERO;
169 break;
170 case RS_BLEND_DST_ONE:
171 mBlendDst = GL_ONE;
172 break;
173 case RS_BLEND_DST_SRC_COLOR:
174 mBlendDst = GL_SRC_COLOR;
175 break;
176 case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
177 mBlendDst = GL_ONE_MINUS_SRC_COLOR;
178 break;
179 case RS_BLEND_DST_SRC_ALPHA:
180 mBlendDst = GL_SRC_ALPHA;
181 break;
182 case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
183 mBlendDst = GL_ONE_MINUS_SRC_ALPHA;
184 break;
185 case RS_BLEND_DST_DST_ALPHA:
186 mBlendDst = GL_DST_ALPHA;
187 break;
188 case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
189 mBlendDst = GL_ONE_MINUS_DST_ALPHA;
190 break;
191 }
192}
193
194void ProgramFragmentStore::setColorMask(bool r, bool g, bool b, bool a)
195{
196 mColorRWriteEnable = r;
197 mColorGWriteEnable = g;
198 mColorBWriteEnable = b;
199 mColorAWriteEnable = a;
200}
201
202
203ProgramFragmentStoreState::ProgramFragmentStoreState()
204{
205 mPFS = NULL;
206}
207
208ProgramFragmentStoreState::~ProgramFragmentStoreState()
209{
210 delete mPFS;
211
212}
213
Jason Sams8ce125b2009-06-17 16:52:59 -0700214void ProgramFragmentStoreState::init(Context *rsc, int32_t w, int32_t h)
215{
216 ProgramFragmentStore *pfs = new ProgramFragmentStore(NULL, NULL);
217 mDefault.set(pfs);
218}
219
Jason Sams326e0dd2009-05-22 14:03:28 -0700220
Jason Sams326e0dd2009-05-22 14:03:28 -0700221namespace android {
222namespace renderscript {
223
224void rsi_ProgramFragmentStoreBegin(Context * rsc, RsElement in, RsElement out)
225{
226 delete rsc->mStateFragmentStore.mPFS;
227 rsc->mStateFragmentStore.mPFS = new ProgramFragmentStore((Element *)in, (Element *)out);
228
229}
230
231void rsi_ProgramFragmentStoreDepthFunc(Context *rsc, RsDepthFunc func)
232{
233 rsc->mStateFragmentStore.mPFS->setDepthFunc(func);
234}
235
236void rsi_ProgramFragmentStoreDepthMask(Context *rsc, bool mask)
237{
238 rsc->mStateFragmentStore.mPFS->setDepthMask(mask);
239}
240
241void rsi_ProgramFragmentStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a)
242{
243 rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a);
244}
245
246void rsi_ProgramFragmentStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst)
247{
248 rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst);
249}
250
251RsProgramFragmentStore rsi_ProgramFragmentStoreCreate(Context *rsc)
252{
253 ProgramFragmentStore *pfs = rsc->mStateFragmentStore.mPFS;
Jason Sams9397e302009-08-27 20:23:34 -0700254 pfs->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700255 rsc->mStateFragmentStore.mPFS = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700256 return pfs;
257}
258
259void rsi_ProgramFragmentStoreDither(Context *rsc, bool enable)
260{
261 rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
262}
263
Jason Sams326e0dd2009-05-22 14:03:28 -0700264
265}
266}