FragmentShader.java revision 4fd35d8f49dbed174828da60b70c37e7a77a0d13
1/*
2 * Copyright (C) 2011 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 com.android.scenegraph;
18
19import java.lang.Math;
20import java.util.ArrayList;
21
22import android.content.res.Resources;
23import android.renderscript.*;
24import android.renderscript.ProgramFragment.Builder;
25import android.util.Log;
26
27/**
28 * @hide
29 */
30public class FragmentShader extends Shader {
31    ProgramFragment mProgram;
32    ScriptField_FragmentShader_s mField;
33
34    public static class Builder {
35
36        FragmentShader mShader;
37        ProgramFragment.Builder mBuilder;
38
39        public Builder(RenderScriptGL rs) {
40            mShader = new FragmentShader();
41            mBuilder = new ProgramFragment.Builder(rs);
42        }
43
44        public Builder setShader(Resources resources, int resourceID) {
45            mBuilder.setShader(resources, resourceID);
46            return this;
47        }
48
49        public Builder setObjectConst(Type type) {
50            mShader.mPerObjConstants = type;
51            return this;
52        }
53
54        public Builder setShaderConst(Type type) {
55            mShader.mPerShaderConstants = type;
56            return this;
57        }
58
59        public Builder addTexture(Program.TextureType texType, String name) {
60            mBuilder.addTexture(texType);
61            mShader.mTextureNames.add(name);
62            return this;
63        }
64
65        FragmentShader create() {
66            if (mShader.mPerShaderConstants != null) {
67                mBuilder.addConstant(mShader.mPerShaderConstants);
68            }
69            if (mShader.mPerObjConstants != null) {
70                mBuilder.addConstant(mShader.mPerObjConstants);
71            }
72            mShader.mProgram = mBuilder.create();
73            return mShader;
74        }
75    }
76
77    FragmentShader() {
78    }
79
80    public ScriptField_FragmentShader_s getRSData(RenderScriptGL rs) {
81        if (mField != null) {
82            return mField;
83        }
84
85        ScriptField_FragmentShader_s.Item item = new ScriptField_FragmentShader_s.Item();
86        item.program = mProgram;
87
88        linkConstants(rs);
89        if (mPerShaderConstants != null) {
90            item.shaderConst = mConstantBuffer;
91            item.shaderConstParams = mConstantBufferParams;
92            mProgram.bindConstants(item.shaderConst, 0);
93        }
94
95        item.objectConstIndex = -1;
96        if (mPerObjConstants != null) {
97            item.objectConstIndex = mPerShaderConstants != null ? 1 : 0;
98        }
99
100        mField = new ScriptField_FragmentShader_s(rs, 1);
101        mField.set(item, 0, true);
102        return mField;
103    }
104}
105