blob: 04091a39abc2f35442a62e6d96a3886d4b8662ac [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/**
25 * @hide
26 *
27 **/
Jason Sams7e5ab3b2009-12-15 13:27:04 -080028public class ProgramFragment extends Program {
Jason Sams22534172009-08-04 16:58:20 -070029 ProgramFragment(int id, RenderScript rs) {
Jason Sams7e5ab3b2009-12-15 13:27:04 -080030 super(id, rs);
Jason Sams22534172009-08-04 16:58:20 -070031 }
32
Jason Sams7e5ab3b2009-12-15 13:27:04 -080033 public static class ShaderBuilder extends BaseProgramBuilder {
34 public ShaderBuilder(RenderScript rs) {
35 super(rs);
36 }
37
38 public ProgramFragment create() {
39 mRS.validate();
40 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + 1) * 2];
41 int idx = 0;
42
43 for (int i=0; i < mInputCount; i++) {
44 tmp[idx++] = 0;
45 tmp[idx++] = mInputs[i].mID;
46 }
47 for (int i=0; i < mOutputCount; i++) {
48 tmp[idx++] = 1;
49 tmp[idx++] = mOutputs[i].mID;
50 }
51 for (int i=0; i < mConstantCount; i++) {
52 tmp[idx++] = 2;
53 tmp[idx++] = mConstants[i].mID;
54 }
55 tmp[idx++] = 3;
56 tmp[idx++] = mTextureCount;
57
58 int id = mRS.nProgramFragmentCreate2(mShader, tmp);
59 ProgramFragment pf = new ProgramFragment(id, mRS);
60 initProgram(pf);
61 return pf;
62 }
63 }
Jason Sams22534172009-08-04 16:58:20 -070064
65 public static class Builder {
Jason Sams68afd012009-12-17 16:55:08 -080066 public static final int MAX_TEXTURE = 2;
Jason Sams22534172009-08-04 16:58:20 -070067 RenderScript mRS;
Jason Sams25ffcdc2009-08-20 16:10:36 -070068 boolean mPointSpriteEnable;
Jason Sams442a6472010-08-04 17:50:20 -070069 boolean mVaryingColorEnable;
Jason Sams68afd012009-12-17 16:55:08 -080070
71 public enum EnvMode {
72 REPLACE (1),
73 MODULATE (2),
74 DECAL (3);
75
76 int mID;
77 EnvMode(int id) {
78 mID = id;
79 }
80 }
81
82 public enum Format {
83 ALPHA (1),
84 LUMINANCE_ALPHA (2),
85 RGB (3),
86 RGBA (4);
87
88 int mID;
89 Format(int id) {
90 mID = id;
91 }
92 }
Jason Sams22534172009-08-04 16:58:20 -070093
94 private class Slot {
Jason Sams68afd012009-12-17 16:55:08 -080095 EnvMode env;
96 Format format;
97 Slot(EnvMode _env, Format _fmt) {
98 env = _env;
99 format = _fmt;
Jason Sams22534172009-08-04 16:58:20 -0700100 }
101 }
102 Slot[] mSlots;
103
Jason Sams68afd012009-12-17 16:55:08 -0800104 public Builder(RenderScript rs) {
Jason Sams22534172009-08-04 16:58:20 -0700105 mRS = rs;
Jason Sams68afd012009-12-17 16:55:08 -0800106 mSlots = new Slot[MAX_TEXTURE];
Jason Sams25ffcdc2009-08-20 16:10:36 -0700107 mPointSpriteEnable = false;
Jason Sams22534172009-08-04 16:58:20 -0700108 }
109
Jim Shuma288c8712010-07-07 14:24:21 -0700110 public Builder setTexture(EnvMode env, Format fmt, int slot)
Jason Sams22534172009-08-04 16:58:20 -0700111 throws IllegalArgumentException {
Jason Sams68afd012009-12-17 16:55:08 -0800112 if((slot < 0) || (slot >= MAX_TEXTURE)) {
113 throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
Jason Sams22534172009-08-04 16:58:20 -0700114 }
Jason Sams68afd012009-12-17 16:55:08 -0800115 mSlots[slot] = new Slot(env, fmt);
Jim Shuma288c8712010-07-07 14:24:21 -0700116 return this;
Jason Sams22534172009-08-04 16:58:20 -0700117 }
118
Jim Shuma288c8712010-07-07 14:24:21 -0700119 public Builder setPointSpriteTexCoordinateReplacement(boolean enable) {
Jason Sams25ffcdc2009-08-20 16:10:36 -0700120 mPointSpriteEnable = enable;
Jim Shuma288c8712010-07-07 14:24:21 -0700121 return this;
Jason Sams25ffcdc2009-08-20 16:10:36 -0700122 }
Jason Sams22534172009-08-04 16:58:20 -0700123
Jason Sams442a6472010-08-04 17:50:20 -0700124 public Builder setVaryingColor(boolean enable) {
125 mVaryingColorEnable = enable;
126 return this;
127 }
128
Jason Sams22534172009-08-04 16:58:20 -0700129 public ProgramFragment create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800130 mRS.validate();
Jason Sams442a6472010-08-04 17:50:20 -0700131 int[] tmp = new int[MAX_TEXTURE * 2 + 2];
Jason Sams68afd012009-12-17 16:55:08 -0800132 if (mSlots[0] != null) {
133 tmp[0] = mSlots[0].env.mID;
134 tmp[1] = mSlots[0].format.mID;
135 }
136 if (mSlots[1] != null) {
137 tmp[2] = mSlots[1].env.mID;
138 tmp[3] = mSlots[1].format.mID;
139 }
140 tmp[4] = mPointSpriteEnable ? 1 : 0;
Jason Sams442a6472010-08-04 17:50:20 -0700141 tmp[5] = mVaryingColorEnable ? 1 : 0;
Jason Sams68afd012009-12-17 16:55:08 -0800142 int id = mRS.nProgramFragmentCreate(tmp);
143 ProgramFragment pf = new ProgramFragment(id, mRS);
144 pf.mTextureCount = MAX_TEXTURE;
145 return pf;
Jason Sams22534172009-08-04 16:58:20 -0700146 }
147 }
148}
149
150
151