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