blob: c46e6b9cfd859961b066ee4165d168d4a1658cee [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
20import android.util.Config;
21import android.util.Log;
22
23
24/**
Robert Ly11518ac2011-02-09 13:57:06 -080025 * <p>ProgramStore contains a set of parameters that control how
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080026 * the graphics hardware handles writes to the framebuffer.
Robert Ly11518ac2011-02-09 13:57:06 -080027 * It could be used to:</p>
28 * <ul>
29 * <li>enable/disable depth testing</li>
30 * <li>specify wheather depth writes are performed</li>
31 * <li>setup various blending modes for use in effects like
32 * transparency</li>
33 * <li>define write masks for color components written into the
34 * framebuffer</li>
35 * </ul>
Jason Sams22534172009-08-04 16:58:20 -070036 *
37 **/
38public class ProgramStore extends BaseObj {
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080039 /**
40 * Specifies the function used to determine whether a fragment
41 * will be drawn during the depth testing stage in the rendering
42 * pipeline by comparing its value with that already in the depth
43 * buffer. DepthFunc is only valid when depth buffer is present
44 * and depth testing is enabled
45 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080046 public enum DepthFunc {
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080047
48 /**
49 * Always drawn
50 */
Jason Sams22534172009-08-04 16:58:20 -070051 ALWAYS (0),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080052 /**
53 * Drawn if the incoming depth value is less than that in the
54 * depth buffer
55 */
Jason Sams22534172009-08-04 16:58:20 -070056 LESS (1),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080057 /**
58 * Drawn if the incoming depth value is less or equal to that in
59 * the depth buffer
60 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080061 LESS_OR_EQUAL (2),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080062 /**
63 * Drawn if the incoming depth value is greater than that in the
64 * depth buffer
65 */
Jason Sams22534172009-08-04 16:58:20 -070066 GREATER (3),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080067 /**
68 * Drawn if the incoming depth value is greater or equal to that
69 * in the depth buffer
70 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080071 GREATER_OR_EQUAL (4),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080072 /**
73 * Drawn if the incoming depth value is equal to that in the
74 * depth buffer
75 */
Jason Sams22534172009-08-04 16:58:20 -070076 EQUAL (5),
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080077 /**
78 * Drawn if the incoming depth value is not equal to that in the
79 * depth buffer
80 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080081 NOT_EQUAL (6);
Jason Sams22534172009-08-04 16:58:20 -070082
83 int mID;
84 DepthFunc(int id) {
85 mID = id;
86 }
87 }
88
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -080089 /**
90 * Specifies the functions used to combine incoming pixels with
91 * those already in the frame buffer.
92 *
93 * BlendSrcFunc describes how the coefficient used to scale the
94 * source pixels during the blending operation is computed
95 *
96 */
Jason Sams22534172009-08-04 16:58:20 -070097 public enum BlendSrcFunc {
98 ZERO (0),
99 ONE (1),
100 DST_COLOR (2),
101 ONE_MINUS_DST_COLOR (3),
102 SRC_ALPHA (4),
103 ONE_MINUS_SRC_ALPHA (5),
104 DST_ALPHA (6),
Jason Samseab4c752009-10-28 17:40:13 -0700105 ONE_MINUS_DST_ALPHA (7),
Jason Sams22534172009-08-04 16:58:20 -0700106 SRC_ALPHA_SATURATE (8);
107
108 int mID;
109 BlendSrcFunc(int id) {
110 mID = id;
111 }
112 }
113
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800114 /**
115 * Specifies the functions used to combine incoming pixels with
116 * those already in the frame buffer.
117 *
118 * BlendDstFunc describes how the coefficient used to scale the
119 * pixels already in the framebuffer is computed during the
120 * blending operation
121 *
122 */
Jason Sams22534172009-08-04 16:58:20 -0700123 public enum BlendDstFunc {
124 ZERO (0),
125 ONE (1),
126 SRC_COLOR (2),
127 ONE_MINUS_SRC_COLOR (3),
128 SRC_ALPHA (4),
129 ONE_MINUS_SRC_ALPHA (5),
130 DST_ALPHA (6),
Jason Samseab4c752009-10-28 17:40:13 -0700131 ONE_MINUS_DST_ALPHA (7);
Jason Sams22534172009-08-04 16:58:20 -0700132
133 int mID;
134 BlendDstFunc(int id) {
135 mID = id;
136 }
137 }
138
139
140 ProgramStore(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700141 super(id, rs);
Jason Sams22534172009-08-04 16:58:20 -0700142 }
143
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800144 /**
145 * Returns a pre-defined program store object with the following
146 * characteristics:
147 * - incoming pixels are drawn if their depth value is less than
148 * the stored value in the depth buffer. If the pixel is
149 * drawn, its value is also stored in the depth buffer
150 * - incoming pixels override the value stored in the color
151 * buffer if it passes the depth test
152 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800153 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800154 **/
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700155 public static ProgramStore BLEND_NONE_DEPTH_TEST(RenderScript rs) {
156 if(rs.mProgramStore_BLEND_NONE_DEPTH_TEST == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700157 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
158 builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
159 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800160 builder.setDitherEnabled(false);
161 builder.setDepthMaskEnabled(true);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700162 rs.mProgramStore_BLEND_NONE_DEPTH_TEST = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700163 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700164 return rs.mProgramStore_BLEND_NONE_DEPTH_TEST;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700165 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800166 /**
167 * Returns a pre-defined program store object with the following
168 * characteristics:
169 * - incoming pixels always pass the depth test and their value
170 * is not stored in the depth buffer
171 * - incoming pixels override the value stored in the color
172 * buffer
173 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800174 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800175 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800176 public static ProgramStore BLEND_NONE_DEPTH_NONE(RenderScript rs) {
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700177 if(rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700178 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
179 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
180 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800181 builder.setDitherEnabled(false);
182 builder.setDepthMaskEnabled(false);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700183 rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700184 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700185 return rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700186 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800187 /**
188 * Returns a pre-defined program store object with the following
189 * characteristics:
190 * - incoming pixels are drawn if their depth value is less than
191 * the stored value in the depth buffer. If the pixel is
192 * drawn, its value is also stored in the depth buffer
193 * - if the incoming (Source) pixel passes depth test, its value
194 * is combined with the stored color (Dest) using the
195 * following formula
196 * Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
197 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800198 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800199 **/
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700200 public static ProgramStore BLEND_ALPHA_DEPTH_TEST(RenderScript rs) {
201 if(rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700202 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
203 builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
204 builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800205 builder.setDitherEnabled(false);
206 builder.setDepthMaskEnabled(true);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700207 rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700208 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700209 return rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700210 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800211 /**
212 * Returns a pre-defined program store object with the following
213 * characteristics:
214 * - incoming pixels always pass the depth test and their value
215 * is not stored in the depth buffer
216 * - incoming pixel's value is combined with the stored color
217 * (Dest) using the following formula
218 * Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
219 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800220 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800221 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800222 public static ProgramStore BLEND_ALPHA_DEPTH_NONE(RenderScript rs) {
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700223 if(rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700224 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
225 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
226 builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800227 builder.setDitherEnabled(false);
228 builder.setDepthMaskEnabled(false);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700229 rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700230 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700231 return rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700232 }
Jason Sams22534172009-08-04 16:58:20 -0700233
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800234 /**
235 * Builder class for ProgramStore object. If the builder is left
236 * empty, the equivalent of BLEND_NONE_DEPTH_NONE would be
237 * returned
238 */
Jason Sams22534172009-08-04 16:58:20 -0700239 public static class Builder {
240 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700241 DepthFunc mDepthFunc;
242 boolean mDepthMask;
243 boolean mColorMaskR;
244 boolean mColorMaskG;
245 boolean mColorMaskB;
246 boolean mColorMaskA;
247 BlendSrcFunc mBlendSrc;
248 BlendDstFunc mBlendDst;
249 boolean mDither;
250
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700251 public Builder(RenderScript rs) {
252 mRS = rs;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700253 mDepthFunc = DepthFunc.ALWAYS;
254 mDepthMask = false;
255 mColorMaskR = true;
256 mColorMaskG = true;
257 mColorMaskB = true;
258 mColorMaskA = true;
259 mBlendSrc = BlendSrcFunc.ONE;
260 mBlendDst = BlendDstFunc.ZERO;
Jason Sams22534172009-08-04 16:58:20 -0700261 }
262
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800263 /**
264 * Specifies the depth testing behavior
265 *
266 * @param func function used for depth testing
267 *
268 * @return this
269 */
Jim Shuma288c8712010-07-07 14:24:21 -0700270 public Builder setDepthFunc(DepthFunc func) {
Jason Sams22534172009-08-04 16:58:20 -0700271 mDepthFunc = func;
Jim Shuma288c8712010-07-07 14:24:21 -0700272 return this;
Jason Sams22534172009-08-04 16:58:20 -0700273 }
274
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800275 /**
276 * Enables writes into the depth buffer
277 *
278 * @param enable specifies whether depth writes are
279 * enabled or disabled
280 *
281 * @return this
282 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800283 public Builder setDepthMaskEnabled(boolean enable) {
Jason Sams22534172009-08-04 16:58:20 -0700284 mDepthMask = enable;
Jim Shuma288c8712010-07-07 14:24:21 -0700285 return this;
Jason Sams22534172009-08-04 16:58:20 -0700286 }
287
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800288 /**
289 * Enables writes into the color buffer
290 *
291 * @param r specifies whether red channel is written
292 * @param g specifies whether green channel is written
293 * @param b specifies whether blue channel is written
294 * @param a specifies whether alpha channel is written
295 *
296 * @return this
297 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800298 public Builder setColorMaskEnabled(boolean r, boolean g, boolean b, boolean a) {
Jason Sams22534172009-08-04 16:58:20 -0700299 mColorMaskR = r;
300 mColorMaskG = g;
301 mColorMaskB = b;
302 mColorMaskA = a;
Jim Shuma288c8712010-07-07 14:24:21 -0700303 return this;
Jason Sams22534172009-08-04 16:58:20 -0700304 }
305
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800306 /**
307 * Specifies how incoming pixels are combined with the pixels
308 * stored in the framebuffer
309 *
310 * @param src specifies how the source blending factor is
311 * computed
312 * @param dst specifies how the destination blending factor is
313 * computed
314 *
315 * @return this
316 */
Jim Shuma288c8712010-07-07 14:24:21 -0700317 public Builder setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
Jason Sams22534172009-08-04 16:58:20 -0700318 mBlendSrc = src;
319 mBlendDst = dst;
Jim Shuma288c8712010-07-07 14:24:21 -0700320 return this;
Jason Sams22534172009-08-04 16:58:20 -0700321 }
322
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800323 /**
324 * Enables dithering
325 *
326 * @param enable specifies whether dithering is enabled or
327 * disabled
328 *
329 * @return this
330 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800331 public Builder setDitherEnabled(boolean enable) {
Jason Sams22534172009-08-04 16:58:20 -0700332 mDither = enable;
Jim Shuma288c8712010-07-07 14:24:21 -0700333 return this;
Jason Sams22534172009-08-04 16:58:20 -0700334 }
335
336 static synchronized ProgramStore internalCreate(RenderScript rs, Builder b) {
Jason Sams06d69de2010-11-09 17:11:40 -0800337 rs.nProgramStoreBegin(0, 0);
Jason Sams54db59c2010-05-13 18:30:11 -0700338 rs.nProgramStoreDepthFunc(b.mDepthFunc.mID);
339 rs.nProgramStoreDepthMask(b.mDepthMask);
340 rs.nProgramStoreColorMask(b.mColorMaskR,
Jason Sams22534172009-08-04 16:58:20 -0700341 b.mColorMaskG,
342 b.mColorMaskB,
343 b.mColorMaskA);
Jason Sams54db59c2010-05-13 18:30:11 -0700344 rs.nProgramStoreBlendFunc(b.mBlendSrc.mID, b.mBlendDst.mID);
345 rs.nProgramStoreDither(b.mDither);
Jason Sams22534172009-08-04 16:58:20 -0700346
Jason Sams54db59c2010-05-13 18:30:11 -0700347 int id = rs.nProgramStoreCreate();
Jason Sams22534172009-08-04 16:58:20 -0700348 return new ProgramStore(id, rs);
349 }
350
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800351 /**
352 * Creates a program store from the current state of the builder
353 */
Jason Sams22534172009-08-04 16:58:20 -0700354 public ProgramStore create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800355 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -0700356 return internalCreate(mRS, this);
357 }
358 }
359
360}
361
362
363
364