blob: fb7c8ca059c10be01740f7111f12dd5e24fef492 [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
138
139 ProgramStore(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700140 super(id, rs);
Jason Sams22534172009-08-04 16:58:20 -0700141 }
142
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800143 /**
144 * Returns a pre-defined program store object with the following
145 * characteristics:
146 * - incoming pixels are drawn if their depth value is less than
147 * the stored value in the depth buffer. If the pixel is
148 * drawn, its value is also stored in the depth buffer
149 * - incoming pixels override the value stored in the color
150 * buffer if it passes the depth test
151 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800152 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800153 **/
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700154 public static ProgramStore BLEND_NONE_DEPTH_TEST(RenderScript rs) {
155 if(rs.mProgramStore_BLEND_NONE_DEPTH_TEST == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700156 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
157 builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
158 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800159 builder.setDitherEnabled(false);
160 builder.setDepthMaskEnabled(true);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700161 rs.mProgramStore_BLEND_NONE_DEPTH_TEST = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700162 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700163 return rs.mProgramStore_BLEND_NONE_DEPTH_TEST;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700164 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800165 /**
166 * Returns a pre-defined program store object with the following
167 * characteristics:
168 * - incoming pixels always pass the depth test and their value
169 * is not stored in the depth buffer
170 * - incoming pixels override the value stored in the color
171 * buffer
172 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800173 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800174 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800175 public static ProgramStore BLEND_NONE_DEPTH_NONE(RenderScript rs) {
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700176 if(rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700177 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
178 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
179 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800180 builder.setDitherEnabled(false);
181 builder.setDepthMaskEnabled(false);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700182 rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700183 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700184 return rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700185 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800186 /**
187 * Returns a pre-defined program store object with the following
188 * characteristics:
189 * - incoming pixels are drawn if their depth value is less than
190 * the stored value in the depth buffer. If the pixel is
191 * drawn, its value is also stored in the depth buffer
192 * - if the incoming (Source) pixel passes depth test, its value
193 * is combined with the stored color (Dest) using the
194 * following formula
195 * Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
196 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800197 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800198 **/
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700199 public static ProgramStore BLEND_ALPHA_DEPTH_TEST(RenderScript rs) {
200 if(rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700201 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
202 builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
203 builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800204 builder.setDitherEnabled(false);
205 builder.setDepthMaskEnabled(true);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700206 rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700207 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700208 return rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700209 }
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800210 /**
211 * Returns a pre-defined program store object with the following
212 * characteristics:
213 * - incoming pixels always pass the depth test and their value
214 * is not stored in the depth buffer
215 * - incoming pixel's value is combined with the stored color
216 * (Dest) using the following formula
217 * Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A)
218 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800219 * @param rs Context to which the program will belong.
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800220 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800221 public static ProgramStore BLEND_ALPHA_DEPTH_NONE(RenderScript rs) {
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700222 if(rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH == null) {
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700223 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
224 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
225 builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800226 builder.setDitherEnabled(false);
227 builder.setDepthMaskEnabled(false);
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700228 rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH = builder.create();
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700229 }
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700230 return rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700231 }
Jason Sams22534172009-08-04 16:58:20 -0700232
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800233 /**
234 * Builder class for ProgramStore object. If the builder is left
235 * empty, the equivalent of BLEND_NONE_DEPTH_NONE would be
236 * returned
237 */
Jason Sams22534172009-08-04 16:58:20 -0700238 public static class Builder {
239 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700240 DepthFunc mDepthFunc;
241 boolean mDepthMask;
242 boolean mColorMaskR;
243 boolean mColorMaskG;
244 boolean mColorMaskB;
245 boolean mColorMaskA;
246 BlendSrcFunc mBlendSrc;
247 BlendDstFunc mBlendDst;
248 boolean mDither;
249
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700250 public Builder(RenderScript rs) {
251 mRS = rs;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700252 mDepthFunc = DepthFunc.ALWAYS;
253 mDepthMask = false;
254 mColorMaskR = true;
255 mColorMaskG = true;
256 mColorMaskB = true;
257 mColorMaskA = true;
258 mBlendSrc = BlendSrcFunc.ONE;
259 mBlendDst = BlendDstFunc.ZERO;
Jason Sams22534172009-08-04 16:58:20 -0700260 }
261
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800262 /**
263 * Specifies the depth testing behavior
264 *
265 * @param func function used for depth testing
266 *
267 * @return this
268 */
Jim Shuma288c8712010-07-07 14:24:21 -0700269 public Builder setDepthFunc(DepthFunc func) {
Jason Sams22534172009-08-04 16:58:20 -0700270 mDepthFunc = func;
Jim Shuma288c8712010-07-07 14:24:21 -0700271 return this;
Jason Sams22534172009-08-04 16:58:20 -0700272 }
273
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800274 /**
275 * Enables writes into the depth buffer
276 *
277 * @param enable specifies whether depth writes are
278 * enabled or disabled
279 *
280 * @return this
281 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800282 public Builder setDepthMaskEnabled(boolean enable) {
Jason Sams22534172009-08-04 16:58:20 -0700283 mDepthMask = enable;
Jim Shuma288c8712010-07-07 14:24:21 -0700284 return this;
Jason Sams22534172009-08-04 16:58:20 -0700285 }
286
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800287 /**
288 * Enables writes into the color buffer
289 *
290 * @param r specifies whether red channel is written
291 * @param g specifies whether green channel is written
292 * @param b specifies whether blue channel is written
293 * @param a specifies whether alpha channel is written
294 *
295 * @return this
296 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800297 public Builder setColorMaskEnabled(boolean r, boolean g, boolean b, boolean a) {
Jason Sams22534172009-08-04 16:58:20 -0700298 mColorMaskR = r;
299 mColorMaskG = g;
300 mColorMaskB = b;
301 mColorMaskA = a;
Jim Shuma288c8712010-07-07 14:24:21 -0700302 return this;
Jason Sams22534172009-08-04 16:58:20 -0700303 }
304
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800305 /**
306 * Specifies how incoming pixels are combined with the pixels
307 * stored in the framebuffer
308 *
309 * @param src specifies how the source blending factor is
310 * computed
311 * @param dst specifies how the destination blending factor is
312 * computed
313 *
314 * @return this
315 */
Jim Shuma288c8712010-07-07 14:24:21 -0700316 public Builder setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
Jason Sams22534172009-08-04 16:58:20 -0700317 mBlendSrc = src;
318 mBlendDst = dst;
Jim Shuma288c8712010-07-07 14:24:21 -0700319 return this;
Jason Sams22534172009-08-04 16:58:20 -0700320 }
321
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800322 /**
323 * Enables dithering
324 *
325 * @param enable specifies whether dithering is enabled or
326 * disabled
327 *
328 * @return this
329 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800330 public Builder setDitherEnabled(boolean enable) {
Jason Sams22534172009-08-04 16:58:20 -0700331 mDither = enable;
Jim Shuma288c8712010-07-07 14:24:21 -0700332 return this;
Jason Sams22534172009-08-04 16:58:20 -0700333 }
334
Alex Sakhartchouk623c54d2011-01-12 17:32:36 -0800335 /**
336 * Creates a program store from the current state of the builder
337 */
Jason Sams22534172009-08-04 16:58:20 -0700338 public ProgramStore create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800339 mRS.validate();
Jason Sams331bf9b2011-04-06 11:23:54 -0700340 int id = mRS.nProgramStoreCreate(mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA,
341 mDepthMask, mDither,
342 mBlendSrc.mID, mBlendDst.mID, mDepthFunc.mID);
343 return new ProgramStore(id, mRS);
Jason Sams22534172009-08-04 16:58:20 -0700344 }
345 }
346
347}
348
349
350
351