1/*
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 **/
28public class ProgramFragment extends Program {
29    ProgramFragment(int id, RenderScript rs) {
30        super(id, rs);
31    }
32
33    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    }
64
65    public static class Builder {
66        public static final int MAX_TEXTURE = 2;
67        RenderScript mRS;
68        boolean mPointSpriteEnable;
69
70        public enum EnvMode {
71            REPLACE (1),
72            MODULATE (2),
73            DECAL (3);
74
75            int mID;
76            EnvMode(int id) {
77                mID = id;
78            }
79        }
80
81        public enum Format {
82            ALPHA (1),
83            LUMINANCE_ALPHA (2),
84            RGB (3),
85            RGBA (4);
86
87            int mID;
88            Format(int id) {
89                mID = id;
90            }
91        }
92
93        private class Slot {
94            EnvMode env;
95            Format format;
96            Slot(EnvMode _env, Format _fmt) {
97                env = _env;
98                format = _fmt;
99            }
100        }
101        Slot[] mSlots;
102
103        public Builder(RenderScript rs) {
104            mRS = rs;
105            mSlots = new Slot[MAX_TEXTURE];
106            mPointSpriteEnable = false;
107        }
108
109        public void setTexture(EnvMode env, Format fmt, int slot)
110            throws IllegalArgumentException {
111            if((slot < 0) || (slot >= MAX_TEXTURE)) {
112                throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
113            }
114            mSlots[slot] = new Slot(env, fmt);
115        }
116
117        public void setPointSpriteTexCoordinateReplacement(boolean enable) {
118            mPointSpriteEnable = enable;
119        }
120
121        public ProgramFragment create() {
122            mRS.validate();
123            int[] tmp = new int[MAX_TEXTURE * 2 + 1];
124            if (mSlots[0] != null) {
125                tmp[0] = mSlots[0].env.mID;
126                tmp[1] = mSlots[0].format.mID;
127            }
128            if (mSlots[1] != null) {
129                tmp[2] = mSlots[1].env.mID;
130                tmp[3] = mSlots[1].format.mID;
131            }
132            tmp[4] = mPointSpriteEnable ? 1 : 0;
133            int id = mRS.nProgramFragmentCreate(tmp);
134            ProgramFragment pf = new ProgramFragment(id, mRS);
135            pf.mTextureCount = MAX_TEXTURE;
136            return pf;
137        }
138    }
139}
140
141
142
143