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 setShader(String code) {
48            mBuilder.setShader(code);
49            return this;
50        }
51
52        public Builder setObjectConst(Type type) {
53            mShader.mPerObjConstants = type;
54            return this;
55        }
56
57        public Builder setShaderConst(Type type) {
58            mShader.mPerShaderConstants = type;
59            return this;
60        }
61
62        public Builder addInput(Element e) {
63            mBuilder.addInput(e);
64            return this;
65        }
66
67        public VertexShader create() {
68            if (mShader.mPerShaderConstants != null) {
69                mBuilder.addConstant(mShader.mPerShaderConstants);
70            }
71            if (mShader.mPerObjConstants != null) {
72                mBuilder.addConstant(mShader.mPerObjConstants);
73            }
74            mShader.mProgram = mBuilder.create();
75            return mShader;
76        }
77    }
78
79    public ProgramVertex getProgram() {
80        return mProgram;
81    }
82
83    ScriptField_VertexShader_s getRSData() {
84        if (mField != null) {
85            return mField;
86        }
87
88        RenderScriptGL rs = SceneManager.getRS();
89        Resources res = SceneManager.getRes();
90        if (rs == null || res == null) {
91            return null;
92        }
93
94        ScriptField_VertexShader_s.Item item = new ScriptField_VertexShader_s.Item();
95        item.program = mProgram;
96
97        linkConstants(rs);
98        if (mPerShaderConstants != null) {
99            item.shaderConst = mConstantBuffer;
100            item.shaderConstParams = mConstantBufferParams.getAllocation();
101            mProgram.bindConstants(item.shaderConst, 0);
102        }
103
104        item.objectConstIndex = -1;
105        if (mPerObjConstants != null) {
106            item.objectConstIndex = mPerShaderConstants != null ? 1 : 0;
107        }
108
109        mField = new ScriptField_VertexShader_s(rs, 1);
110        mField.set(item, 0, true);
111        return mField;
112    }
113}
114