ProgramFragment.java revision 9c9ad3f8c218954e46aab81f9af7834cea5675ca
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.Log;
21
22
23/**
24 * <p>The Renderscript fragment program, also known as fragment shader is responsible
25 * for manipulating pixel data in a user defined way. It's constructed from a GLSL
26 * shader string containing the program body, textures inputs, and a Type object
27 * that describes the constants used by the program. Similar to the vertex programs,
28 * when an allocation with constant input values is bound to the shader, its values
29 * are sent to the graphics program automatically.</p>
30 * <p> The values inside the allocation are not explicitly tracked. If they change between two draw
31 * calls using the same program object, the runtime needs to be notified of that
32 * change by calling rsgAllocationSyncAll so it could send the new values to hardware.
33 * Communication between the vertex and fragment programs is handled internally in the
34 * GLSL code. For example, if the fragment program is expecting a varying input called
35 * varTex0, the GLSL code inside the program vertex must provide it.
36 * </p>
37 *
38 **/
39public class ProgramFragment extends Program {
40    ProgramFragment(int id, RenderScript rs) {
41        super(id, rs);
42    }
43
44    public static class Builder extends BaseProgramBuilder {
45        /**
46         * Create a builder object.
47         *
48         * @param rs Context to which the program will belong.
49         */
50        public Builder(RenderScript rs) {
51            super(rs);
52        }
53
54        /**
55         * Creates ProgramFragment from the current state of the builder
56         *
57         * @return  ProgramFragment
58         */
59        public ProgramFragment create() {
60            mRS.validate();
61            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
62            String[] texNames = new String[mTextureCount];
63            int idx = 0;
64
65            for (int i=0; i < mInputCount; i++) {
66                tmp[idx++] = ProgramParam.INPUT.mID;
67                tmp[idx++] = mInputs[i].getID(mRS);
68            }
69            for (int i=0; i < mOutputCount; i++) {
70                tmp[idx++] = ProgramParam.OUTPUT.mID;
71                tmp[idx++] = mOutputs[i].getID(mRS);
72            }
73            for (int i=0; i < mConstantCount; i++) {
74                tmp[idx++] = ProgramParam.CONSTANT.mID;
75                tmp[idx++] = mConstants[i].getID(mRS);
76            }
77            for (int i=0; i < mTextureCount; i++) {
78                tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
79                tmp[idx++] = mTextureTypes[i].mID;
80                texNames[i] = mTextureNames[i];
81            }
82
83            int id = mRS.nProgramFragmentCreate(mShader, texNames, tmp);
84            ProgramFragment pf = new ProgramFragment(id, mRS);
85            initProgram(pf);
86            return pf;
87        }
88    }
89}
90
91
92
93