blob: 677daddbfe1b986ba0f7593ac1b27078da5ddafc [file] [log] [blame]
Jason Sams22534172009-08-04 16:58:20 -07001/*
2 * Copyright (C) 2008 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
17package android.renderscript;
18
19
Jason Sams22534172009-08-04 16:58:20 -070020import android.util.Log;
21
22
23/**
Robert Ly11518ac2011-02-09 13:57:06 -080024 * <p>ProgramStore contains a set of parameters that control how
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080025 * the graphics hardware handles writes to the framebuffer.
Robert Ly11518ac2011-02-09 13:57:06 -080026 * It could be used to:</p>
27 * <ul>
28 * <li>enable/disable depth testing</li>
29 * <li>specify wheather depth writes are performed</li>
30 * <li>setup various blending modes for use in effects like
31 * transparency</li>
32 * <li>define write masks for color components written into the
33 * framebuffer</li>
34 * </ul>
Jason Sams22534172009-08-04 16:58:20 -070035 *
36 **/
37public class ProgramStore extends BaseObj {
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080038 /**
39 * Specifies the function used to determine whether a fragment
40 * will be drawn during the depth testing stage in the rendering
41 * pipeline by comparing its value with that already in the depth
42 * buffer. DepthFunc is only valid when depth buffer is present
43 * and depth testing is enabled
44 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080045 public enum DepthFunc {
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080046
47 /**
48 * Always drawn
49 */
Jason Sams22534172009-08-04 16:58:20 -070050 ALWAYS (0),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080051 /**
52 * Drawn if the incoming depth value is less than that in the
53 * depth buffer
54 */
Jason Sams22534172009-08-04 16:58:20 -070055 LESS (1),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080056 /**
57 * Drawn if the incoming depth value is less or equal to that in
58 * the depth buffer
59 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080060 LESS_OR_EQUAL (2),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080061 /**
62 * Drawn if the incoming depth value is greater than that in the
63 * depth buffer
64 */
Jason Sams22534172009-08-04 16:58:20 -070065 GREATER (3),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080066 /**
67 * Drawn if the incoming depth value is greater or equal to that
68 * in the depth buffer
69 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080070 GREATER_OR_EQUAL (4),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080071 /**
72 * Drawn if the incoming depth value is equal to that in the
73 * depth buffer
74 */
Jason Sams22534172009-08-04 16:58:20 -070075 EQUAL (5),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080076 /**
77 * Drawn if the incoming depth value is not equal to that in the
78 * depth buffer
79 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080080 NOT_EQUAL (6);
Jason Sams22534172009-08-04 16:58:20 -070081
82 int mID;
83 DepthFunc(int id) {
84 mID = id;
85 }
86 }
87
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080088 /**
89 * Specifies the functions used to combine incoming pixels with
90 * those already in the frame buffer.
91 *
92 * BlendSrcFunc describes how the coefficient used to scale the
93 * source pixels during the blending operation is computed
94 *
95 */
Jason Sams22534172009-08-04 16:58:20 -070096 public enum BlendSrcFunc {
97 ZERO (0),
98 ONE (1),
99 DST_COLOR (2),
100 ONE_MINUS_DST_COLOR (3),
101 SRC_ALPHA (4),
102 ONE_MINUS_SRC_ALPHA (5),
103 DST_ALPHA (6),
Jason Samseab4c752009-10-28 17:40:13 -0700104 ONE_MINUS_DST_ALPHA (7),
Jason Sams22534172009-08-04 16:58:20 -0700105 SRC_ALPHA_SATURATE (8);
106
107 int mID;
108 BlendSrcFunc(int id) {
109 mID = id;
110 }
111 }
112
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800113 /**
114 * Specifies the functions used to combine incoming pixels with
115 * those already in the frame buffer.
116 *
117 * BlendDstFunc describes how the coefficient used to scale the
118 * pixels already in the framebuffer is computed during the
119 * blending operation
120 *
121 */
Jason Sams22534172009-08-04 16:58:20 -0700122 public enum BlendDstFunc {
123 ZERO (0),
124 ONE (1),
125 SRC_COLOR (2),
126 ONE_MINUS_SRC_COLOR (3),
127 SRC_ALPHA (4),
128 ONE_MINUS_SRC_ALPHA (5),
129 DST_ALPHA (6),
Jason Samseab4c752009-10-28 17:40:13 -0700130 ONE_MINUS_DST_ALPHA (7);
Jason Sams22534172009-08-04 16:58:20 -0700131
132 int mID;
133 BlendDstFunc(int id) {
134 mID = id;
135 }
136 }
137
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700138 DepthFunc mDepthFunc;
139 boolean mDepthMask;
140 boolean mColorMaskR;
141 boolean mColorMaskG;
142 boolean mColorMaskB;
143 boolean mColorMaskA;
144 BlendSrcFunc mBlendSrc;
145 BlendDstFunc mBlendDst;
146 boolean mDither;
Jason Sams22534172009-08-04 16:58:20 -0700147
148 ProgramStore(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700149 super(id, rs);
Jason Sams22534172009-08-04 16:58:20 -0700150 }
151
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800152 /**
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700153 * @hide
154 * @return depth function
155 */
156 public DepthFunc getDepthFunc() {
157 return mDepthFunc;
158 }
159
160 /**
161 * @hide
162 * @return whether depth writes are enabled
163 */
164 public boolean getDepthMaskEnabled() {
165 return mDepthMask;
166 }
167
168 /**
169 * @hide
170 * @return red color channel mask
171 */
172 public boolean getColorMaskREnabled() {
173 return mColorMaskR;
174 }
175
176 /**
177 * @hide
178 * @return green color channel mask
179 */
180 public boolean getColorMaskGEnabled() {
181 return mColorMaskG;
182 }
183
184 /**
185 * @hide
186 * @return blue color channel mask
187 */
188 public boolean getColorMaskBEnabled() {
189 return mColorMaskB;
190 }
191
192 /**
193 * @hide
194 * @return alpha channel mask
195 */
196 public boolean getColorMaskAEnabled() {
197 return mColorMaskA;
198 }
199
200 /**
201 * @hide
202 * @return source blend function
203 */
204 public BlendSrcFunc getBlendSrcFunc() {
205 return mBlendSrc;
206 }
207
208 /**
209 * @hide
210 * @return destination blend function
211 */
212 public BlendDstFunc getBlendDstFunc() {
213 return mBlendDst;
214 }
215
216 /**
217 * @hide
218 * @return whether dither is enabled
219 */
220 public boolean getDitherEnabled() {
221 return mDither;
222 }
223
224 /**
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800225 * Returns a pre-defined program store object with the following
226 * characteristics:
227 * - incoming pixels are drawn if their depth value is less than
228 * the stored value in the depth buffer. If the pixel is
229 * drawn, its value is also stored in the depth buffer
230 * - incoming pixels override the value stored in the color
231 * buffer if it passes the depth test
232 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800233 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800234 **/
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700235 public static ProgramStore BLEND_NONE_DEPTH_TEST(RenderScript rs) {
236 if(rs.mProgramStore_BLEND_NONE_DEPTH_TEST == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700237 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
238 builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
239 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800240 builder.setDitherEnabled(false);
241 builder.setDepthMaskEnabled(true);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700242 rs.mProgramStore_BLEND_NONE_DEPTH_TEST = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700243 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700244 return rs.mProgramStore_BLEND_NONE_DEPTH_TEST;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700245 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800246 /**
247 * Returns a pre-defined program store object with the following
248 * characteristics:
249 * - incoming pixels always pass the depth test and their value
250 * is not stored in the depth buffer
251 * - incoming pixels override the value stored in the color
252 * buffer
253 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800254 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800255 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800256 public static ProgramStore BLEND_NONE_DEPTH_NONE(RenderScript rs) {
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700257 if(rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700258 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
259 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
260 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800261 builder.setDitherEnabled(false);
262 builder.setDepthMaskEnabled(false);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700263 rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700264 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700265 return rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700266 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800267 /**
268 * Returns a pre-defined program store object with the following
269 * characteristics:
270 * - incoming pixels are drawn if their depth value is less than
271 * the stored value in the depth buffer. If the pixel is
272 * drawn, its value is also stored in the depth buffer
273 * - if the incoming (Source) pixel passes depth test, its value
274 * is combined with the stored color (Dest) using the
275 * following formula
276 * Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
277 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800278 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800279 **/
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700280 public static ProgramStore BLEND_ALPHA_DEPTH_TEST(RenderScript rs) {
281 if(rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700282 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
283 builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
284 builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800285 builder.setDitherEnabled(false);
286 builder.setDepthMaskEnabled(true);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700287 rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700288 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700289 return rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700290 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800291 /**
292 * Returns a pre-defined program store object with the following
293 * characteristics:
294 * - incoming pixels always pass the depth test and their value
295 * is not stored in the depth buffer
296 * - incoming pixel's value is combined with the stored color
297 * (Dest) using the following formula
298 * Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
299 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800300 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800301 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800302 public static ProgramStore BLEND_ALPHA_DEPTH_NONE(RenderScript rs) {
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700303 if(rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700304 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
305 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
306 builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800307 builder.setDitherEnabled(false);
308 builder.setDepthMaskEnabled(false);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700309 rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700310 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700311 return rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700312 }
Jason Sams22534172009-08-04 16:58:20 -0700313
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800314 /**
315 * Builder class for ProgramStore object. If the builder is left
316 * empty, the equivalent of BLEND_NONE_DEPTH_NONE would be
317 * returned
318 */
Jason Sams22534172009-08-04 16:58:20 -0700319 public static class Builder {
320 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700321 DepthFunc mDepthFunc;
322 boolean mDepthMask;
323 boolean mColorMaskR;
324 boolean mColorMaskG;
325 boolean mColorMaskB;
326 boolean mColorMaskA;
327 BlendSrcFunc mBlendSrc;
328 BlendDstFunc mBlendDst;
329 boolean mDither;
330
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700331 public Builder(RenderScript rs) {
332 mRS = rs;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700333 mDepthFunc = DepthFunc.ALWAYS;
334 mDepthMask = false;
335 mColorMaskR = true;
336 mColorMaskG = true;
337 mColorMaskB = true;
338 mColorMaskA = true;
339 mBlendSrc = BlendSrcFunc.ONE;
340 mBlendDst = BlendDstFunc.ZERO;
Jason Sams22534172009-08-04 16:58:20 -0700341 }
342
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800343 /**
344 * Specifies the depth testing behavior
345 *
346 * @param func function used for depth testing
347 *
348 * @return this
349 */
Jim Shuma288c8712010-07-07 14:24:21 -0700350 public Builder setDepthFunc(DepthFunc func) {
Jason Sams22534172009-08-04 16:58:20 -0700351 mDepthFunc = func;
Jim Shuma288c8712010-07-07 14:24:21 -0700352 return this;
Jason Sams22534172009-08-04 16:58:20 -0700353 }
354
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800355 /**
356 * Enables writes into the depth buffer
357 *
358 * @param enable specifies whether depth writes are
359 * enabled or disabled
360 *
361 * @return this
362 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800363 public Builder setDepthMaskEnabled(boolean enable) {
Jason Sams22534172009-08-04 16:58:20 -0700364 mDepthMask = enable;
Jim Shuma288c8712010-07-07 14:24:21 -0700365 return this;
Jason Sams22534172009-08-04 16:58:20 -0700366 }
367
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800368 /**
369 * Enables writes into the color buffer
370 *
371 * @param r specifies whether red channel is written
372 * @param g specifies whether green channel is written
373 * @param b specifies whether blue channel is written
374 * @param a specifies whether alpha channel is written
375 *
376 * @return this
377 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800378 public Builder setColorMaskEnabled(boolean r, boolean g, boolean b, boolean a) {
Jason Sams22534172009-08-04 16:58:20 -0700379 mColorMaskR = r;
380 mColorMaskG = g;
381 mColorMaskB = b;
382 mColorMaskA = a;
Jim Shuma288c8712010-07-07 14:24:21 -0700383 return this;
Jason Sams22534172009-08-04 16:58:20 -0700384 }
385
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800386 /**
387 * Specifies how incoming pixels are combined with the pixels
388 * stored in the framebuffer
389 *
390 * @param src specifies how the source blending factor is
391 * computed
392 * @param dst specifies how the destination blending factor is
393 * computed
394 *
395 * @return this
396 */
Jim Shuma288c8712010-07-07 14:24:21 -0700397 public Builder setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
Jason Sams22534172009-08-04 16:58:20 -0700398 mBlendSrc = src;
399 mBlendDst = dst;
Jim Shuma288c8712010-07-07 14:24:21 -0700400 return this;
Jason Sams22534172009-08-04 16:58:20 -0700401 }
402
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800403 /**
404 * Enables dithering
405 *
406 * @param enable specifies whether dithering is enabled or
407 * disabled
408 *
409 * @return this
410 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800411 public Builder setDitherEnabled(boolean enable) {
Jason Sams22534172009-08-04 16:58:20 -0700412 mDither = enable;
Jim Shuma288c8712010-07-07 14:24:21 -0700413 return this;
Jason Sams22534172009-08-04 16:58:20 -0700414 }
415
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800416 /**
417 * Creates a program store from the current state of the builder
418 */
Jason Sams22534172009-08-04 16:58:20 -0700419 public ProgramStore create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800420 mRS.validate();
Jason Sams331bf9b2011-04-06 11:23:54 -0700421 int id = mRS.nProgramStoreCreate(mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA,
422 mDepthMask, mDither,
423 mBlendSrc.mID, mBlendDst.mID, mDepthFunc.mID);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700424 ProgramStore programStore = new ProgramStore(id, mRS);
425 programStore.mDepthFunc = mDepthFunc;
426 programStore.mDepthMask = mDepthMask;
427 programStore.mColorMaskR = mColorMaskR;
428 programStore.mColorMaskG = mColorMaskG;
429 programStore.mColorMaskB = mColorMaskB;
430 programStore.mColorMaskA = mColorMaskA;
431 programStore.mBlendSrc = mBlendSrc;
432 programStore.mBlendDst = mBlendDst;
433 programStore.mDither = mDither;
434 return programStore;
Jason Sams22534172009-08-04 16:58:20 -0700435 }
436 }
437
438}
439
440
441
442