ShaderParam.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;
21import java.util.HashMap;
22
23import com.android.scenegraph.SceneManager;
24import com.android.scenegraph.Transform;
25
26import android.renderscript.Element;
27import android.renderscript.Matrix4f;
28import android.renderscript.ProgramFragment;
29import android.renderscript.ProgramStore;
30import android.renderscript.ProgramVertex;
31import android.renderscript.RenderScriptGL;
32import android.util.Log;
33
34/**
35 * @hide
36 */
37public abstract class ShaderParam extends SceneGraphBase {
38
39    static final String cameraPos        = "cameraPos";
40    static final String cameraDir        = "cameraDir";
41
42    static final String lightColor       = "lightColor";
43    static final String lightPos         = "lightPos";
44    static final String lightDir         = "lightDir";
45
46    static final String view             = "view";
47    static final String proj             = "proj";
48    static final String viewProj         = "viewProj";
49    static final String model            = "model";
50    static final String modelView        = "modelView";
51    static final String modelViewProj    = "modelViewProj";
52
53    ScriptField_ShaderParamData_s.Item mData;
54    ScriptField_ShaderParamData_s mField;
55
56    String mParamName;
57    Camera mCamera;
58
59    static ScriptField_ShaderParam_s fillInParams(Element constantElem,
60                                                  HashMap<String, ShaderParam> sourceParams,
61                                                  Transform transform) {
62        RenderScriptGL rs = SceneManager.getRS();
63        ArrayList<ScriptField_ShaderParam_s.Item> paramList;
64        paramList = new ArrayList<ScriptField_ShaderParam_s.Item>();
65
66        int subElemCount = constantElem.getSubElementCount();
67        for (int i = 0; i < subElemCount; i ++) {
68            String inputName = constantElem.getSubElementName(i);
69            int offset = constantElem.getSubElementOffsetBytes(i);
70
71            ShaderParam matchingParam = sourceParams.get(inputName);
72            Element subElem = constantElem.getSubElement(i);
73            // Make one if it's not there
74            if (matchingParam == null) {
75                if (subElem.getDataType() == Element.DataType.FLOAT_32) {
76                    matchingParam = new Float4Param(inputName);
77                } else if (subElem.getDataType() == Element.DataType.MATRIX_4X4) {
78                    TransformParam trParam = new TransformParam(inputName);
79                    trParam.setTransform(transform);
80                    matchingParam = trParam;
81                }
82            }
83            if (subElem.getDataType() == Element.DataType.FLOAT_32) {
84                Float4Param fParam = (Float4Param)matchingParam;
85                fParam.setVecSize(subElem.getVectorSize());
86            }
87            ScriptField_ShaderParam_s.Item paramRS = new ScriptField_ShaderParam_s.Item();
88            paramRS.bufferOffset = offset;
89            paramRS.transformTimestamp = 0;
90            paramRS.data = matchingParam.getRSData().getAllocation();
91
92            paramList.add(paramRS);
93        }
94
95        ScriptField_ShaderParam_s rsParams = null;
96        int paramCount = paramList.size();
97        if (paramCount != 0) {
98            rsParams = new ScriptField_ShaderParam_s(rs, paramCount);
99            for (int i = 0; i < paramCount; i++) {
100                rsParams.set(paramList.get(i), i, false);
101            }
102            rsParams.copyAll();
103        }
104        return rsParams;
105    }
106
107    public ShaderParam(String name) {
108        mParamName = name;
109        mData = new ScriptField_ShaderParamData_s.Item();
110    }
111
112    public String getParamName() {
113        return mParamName;
114    }
115
116    public void setCamera(Camera c) {
117        mCamera = c;
118        if (mField != null) {
119            mData.camera = mCamera.getRSData().getAllocation();
120            mField.set_camera(0, mData.camera, true);
121        }
122    }
123
124    abstract void initLocalData();
125
126    public ScriptField_ShaderParamData_s getRSData() {
127        if (mField != null) {
128            return mField;
129        }
130
131        RenderScriptGL rs = SceneManager.getRS();
132        mField = new ScriptField_ShaderParamData_s(rs, 1);
133
134        if (mParamName != null) {
135            mData.paramName = SceneManager.getCachedAlloc(mParamName);
136            if (mData.paramName == null) {
137                mData.paramName = SceneManager.getStringAsAllocation(rs, mParamName);
138                SceneManager.cacheAlloc(mParamName, mData.paramName);
139            }
140        }
141        initLocalData();
142
143        mField.set(mData, 0, true);
144        return mField;
145    }
146}
147
148
149
150
151
152