rsProgramStore.cpp revision 771565f47fc44608444c00aa8fa3bda769ceaece
1/*
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 "rsProgramStore.h"
19
20#include <GLES/gl.h>
21#include <GLES/glext.h>
22
23using namespace android;
24using namespace android::renderscript;
25
26
27ProgramStore::ProgramStore(Context *rsc) :
28    Program(rsc)
29{
30    mAllocFile = __FILE__;
31    mAllocLine = __LINE__;
32    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
49ProgramStore::~ProgramStore()
50{
51}
52
53void ProgramStore::setupGL(const Context *rsc, ProgramStoreState *state)
54{
55    if (state->mLast.get() == this) {
56        return;
57    }
58    state->mLast.set(this);
59
60    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
71    //LOGE("pfs  %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
72
73    glDepthMask(mDepthWriteEnable);
74    if(mDepthTestEnable || mDepthWriteEnable) {
75        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
88void ProgramStore::setupGL2(const Context *rsc, ProgramStoreState *state)
89{
90    if (state->mLast.get() == this) {
91        return;
92    }
93    state->mLast.set(this);
94
95    glColorMask(mColorRWriteEnable,
96                mColorGWriteEnable,
97                mColorBWriteEnable,
98                mColorAWriteEnable);
99    if (mBlendEnable) {
100        glEnable(GL_BLEND);
101        glBlendFunc(mBlendSrc, mBlendDst);
102    } else {
103        glDisable(GL_BLEND);
104    }
105
106    //LOGE("pfs  %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
107
108    glDepthMask(mDepthWriteEnable);
109    if(mDepthTestEnable || mDepthWriteEnable) {
110        glEnable(GL_DEPTH_TEST);
111        glDepthFunc(mDepthFunc);
112    } else {
113        glDisable(GL_DEPTH_TEST);
114    }
115
116    if (mDitherEnable) {
117        glEnable(GL_DITHER);
118    } else {
119        glDisable(GL_DITHER);
120    }
121}
122
123
124void ProgramStore::setDitherEnable(bool enable)
125{
126    mDitherEnable = enable;
127}
128
129void ProgramStore::setDepthFunc(RsDepthFunc func)
130{
131    mDepthTestEnable = true;
132
133    switch(func) {
134    case RS_DEPTH_FUNC_ALWAYS:
135        mDepthTestEnable = false;
136        mDepthFunc = GL_ALWAYS;
137        break;
138    case RS_DEPTH_FUNC_LESS:
139        mDepthFunc = GL_LESS;
140        break;
141    case RS_DEPTH_FUNC_LEQUAL:
142        mDepthFunc = GL_LEQUAL;
143        break;
144    case RS_DEPTH_FUNC_GREATER:
145        mDepthFunc = GL_GREATER;
146        break;
147    case RS_DEPTH_FUNC_GEQUAL:
148        mDepthFunc = GL_GEQUAL;
149        break;
150    case RS_DEPTH_FUNC_EQUAL:
151        mDepthFunc = GL_EQUAL;
152        break;
153    case RS_DEPTH_FUNC_NOTEQUAL:
154        mDepthFunc = GL_NOTEQUAL;
155        break;
156    }
157}
158
159void ProgramStore::setDepthMask(bool mask)
160{
161    mDepthWriteEnable = mask;
162}
163
164void ProgramStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst)
165{
166    mBlendEnable = true;
167    if ((src == RS_BLEND_SRC_ONE) &&
168        (dst == RS_BLEND_DST_ZERO)) {
169        mBlendEnable = false;
170    }
171
172    switch(src) {
173    case RS_BLEND_SRC_ZERO:
174        mBlendSrc = GL_ZERO;
175        break;
176    case RS_BLEND_SRC_ONE:
177        mBlendSrc = GL_ONE;
178        break;
179    case RS_BLEND_SRC_DST_COLOR:
180        mBlendSrc = GL_DST_COLOR;
181        break;
182    case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
183        mBlendSrc = GL_ONE_MINUS_DST_COLOR;
184        break;
185    case RS_BLEND_SRC_SRC_ALPHA:
186        mBlendSrc = GL_SRC_ALPHA;
187        break;
188    case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
189        mBlendSrc = GL_ONE_MINUS_SRC_ALPHA;
190        break;
191    case RS_BLEND_SRC_DST_ALPHA:
192        mBlendSrc = GL_DST_ALPHA;
193        break;
194    case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
195        mBlendSrc = GL_ONE_MINUS_DST_ALPHA;
196        break;
197    case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
198        mBlendSrc = GL_SRC_ALPHA_SATURATE;
199        break;
200    }
201
202    switch(dst) {
203    case RS_BLEND_DST_ZERO:
204        mBlendDst = GL_ZERO;
205        break;
206    case RS_BLEND_DST_ONE:
207        mBlendDst = GL_ONE;
208        break;
209    case RS_BLEND_DST_SRC_COLOR:
210        mBlendDst = GL_SRC_COLOR;
211        break;
212    case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
213        mBlendDst = GL_ONE_MINUS_SRC_COLOR;
214        break;
215    case RS_BLEND_DST_SRC_ALPHA:
216        mBlendDst = GL_SRC_ALPHA;
217        break;
218    case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
219        mBlendDst = GL_ONE_MINUS_SRC_ALPHA;
220        break;
221    case RS_BLEND_DST_DST_ALPHA:
222        mBlendDst = GL_DST_ALPHA;
223        break;
224    case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
225        mBlendDst = GL_ONE_MINUS_DST_ALPHA;
226        break;
227    }
228}
229
230void ProgramStore::setColorMask(bool r, bool g, bool b, bool a)
231{
232    mColorRWriteEnable = r;
233    mColorGWriteEnable = g;
234    mColorBWriteEnable = b;
235    mColorAWriteEnable = a;
236}
237
238
239ProgramStoreState::ProgramStoreState()
240{
241    mPFS = NULL;
242}
243
244ProgramStoreState::~ProgramStoreState()
245{
246    delete mPFS;
247
248}
249
250void ProgramStoreState::init(Context *rsc)
251{
252    ProgramStore *pfs = new ProgramStore(rsc);
253    mDefault.set(pfs);
254}
255
256void ProgramStoreState::deinit(Context *rsc)
257{
258    mDefault.clear();
259    mLast.clear();
260}
261
262
263namespace android {
264namespace renderscript {
265
266void rsi_ProgramStoreBegin(Context * rsc, RsElement in, RsElement out)
267{
268    delete rsc->mStateFragmentStore.mPFS;
269    rsc->mStateFragmentStore.mPFS = new ProgramStore(rsc);
270
271}
272
273void rsi_ProgramStoreDepthFunc(Context *rsc, RsDepthFunc func)
274{
275    rsc->mStateFragmentStore.mPFS->setDepthFunc(func);
276}
277
278void rsi_ProgramStoreDepthMask(Context *rsc, bool mask)
279{
280    rsc->mStateFragmentStore.mPFS->setDepthMask(mask);
281}
282
283void rsi_ProgramStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a)
284{
285    rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a);
286}
287
288void rsi_ProgramStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst)
289{
290    rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst);
291}
292
293RsProgramStore rsi_ProgramStoreCreate(Context *rsc)
294{
295    ProgramStore *pfs = rsc->mStateFragmentStore.mPFS;
296    pfs->incUserRef();
297    rsc->mStateFragmentStore.mPFS = 0;
298    return pfs;
299}
300
301void rsi_ProgramStoreDither(Context *rsc, bool enable)
302{
303    rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
304}
305
306
307}
308}
309