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