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