VertexShader.java revision 0c9523357f48a26c8214ccff0b0d95c6927ac0d3
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.util.Log;
25
26/**
27 * @hide
28 */
29public class VertexShader extends Shader {
30    ProgramVertex mProgram;
31    ScriptField_VertexShader_s mField;
32
33    public static class Builder {
34        VertexShader mShader;
35        ProgramVertex.Builder mBuilder;
36
37        public Builder(RenderScriptGL rs) {
38            mShader = new VertexShader();
39            mBuilder = new ProgramVertex.Builder(rs);
40        }
41
42        public Builder setShader(Resources resources, int resourceID) {
43            mBuilder.setShader(resources, resourceID);
44            return this;
45        }
46
47        public Builder setObjectConst(Type type) {
48            mShader.mPerObjConstants = type;
49            return this;
50        }
51
52        public Builder setShaderConst(Type type) {
53            mShader.mPerShaderConstants = type;
54            return this;
55        }
56
57        public Builder addInput(Element e) {
58            mBuilder.addInput(e);
59            return this;
60        }
61
62        public VertexShader create() {
63            if (mShader.mPerShaderConstants != null) {
64                mBuilder.addConstant(mShader.mPerShaderConstants);
65            }
66            if (mShader.mPerObjConstants != null) {
67                mBuilder.addConstant(mShader.mPerObjConstants);
68            }
69            mShader.mProgram = mBuilder.create();
70            return mShader;
71        }
72    }
73
74    public ProgramVertex getProgram() {
75        return mProgram;
76    }
77
78    ScriptField_VertexShader_s getRSData() {
79        if (mField != null) {
80            return mField;
81        }
82
83        RenderScriptGL rs = SceneManager.getRS();
84        Resources res = SceneManager.getRes();
85        if (rs == null || res == null) {
86            return null;
87        }
88
89        ScriptField_VertexShader_s.Item item = new ScriptField_VertexShader_s.Item();
90        item.program = mProgram;
91
92        linkConstants(rs);
93        if (mPerShaderConstants != null) {
94            item.shaderConst = mConstantBuffer;
95            item.shaderConstParams = mConstantBufferParams.getAllocation();
96            mProgram.bindConstants(item.shaderConst, 0);
97        }
98
99        item.objectConstIndex = -1;
100        if (mPerObjConstants != null) {
101            item.objectConstIndex = mPerShaderConstants != null ? 1 : 0;
102        }
103
104        mField = new ScriptField_VertexShader_s(rs, 1);
105        mField.set(item, 0, true);
106        return mField;
107    }
108}
109