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 
17 package com.android.scenegraph;
18 
19 import java.lang.Math;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 
23 import com.android.scenegraph.SceneManager;
24 import com.android.scenegraph.Transform;
25 
26 import android.renderscript.Element;
27 import android.renderscript.Matrix4f;
28 import android.renderscript.ProgramFragment;
29 import android.renderscript.ProgramStore;
30 import android.renderscript.ProgramVertex;
31 import android.renderscript.RenderScriptGL;
32 import android.util.Log;
33 
34 /**
35  * @hide
36  */
37 public 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     static final long sMaxTimeStamp = 0xffffffffL;
54 
55     ScriptField_ShaderParamData_s.Item mData;
56     ScriptField_ShaderParamData_s mField;
57 
58     String mParamName;
59     Camera mCamera;
60 
fillInParams(Element constantElem, HashMap<String, ShaderParam> sourceParams, Transform transform)61     static ScriptField_ShaderParam_s fillInParams(Element constantElem,
62                                                   HashMap<String, ShaderParam> sourceParams,
63                                                   Transform transform) {
64         RenderScriptGL rs = SceneManager.getRS();
65         ArrayList<ScriptField_ShaderParam_s.Item> paramList;
66         paramList = new ArrayList<ScriptField_ShaderParam_s.Item>();
67 
68         int subElemCount = constantElem.getSubElementCount();
69         for (int i = 0; i < subElemCount; i ++) {
70             String inputName = constantElem.getSubElementName(i);
71             int offset = constantElem.getSubElementOffsetBytes(i);
72 
73             ShaderParam matchingParam = sourceParams.get(inputName);
74             Element subElem = constantElem.getSubElement(i);
75             // Make one if it's not there
76             if (matchingParam == null) {
77                 if (subElem.getDataType() == Element.DataType.FLOAT_32) {
78                     matchingParam = new Float4Param(inputName, 0.5f, 0.5f, 0.5f, 0.5f);
79                 } else if (subElem.getDataType() == Element.DataType.MATRIX_4X4) {
80                     TransformParam trParam = new TransformParam(inputName);
81                     trParam.setTransform(transform);
82                     matchingParam = trParam;
83                 }
84             }
85             ScriptField_ShaderParam_s.Item paramRS = new ScriptField_ShaderParam_s.Item();
86             paramRS.bufferOffset = offset;
87             paramRS.transformTimestamp = 0;
88             paramRS.dataTimestamp = 0;
89             paramRS.data = matchingParam.getRSData().getAllocation();
90             if (subElem.getDataType() == Element.DataType.FLOAT_32) {
91                 paramRS.float_vecSize = subElem.getVectorSize();
92             }
93 
94             paramList.add(paramRS);
95         }
96 
97         ScriptField_ShaderParam_s rsParams = null;
98         int paramCount = paramList.size();
99         if (paramCount != 0) {
100             rsParams = new ScriptField_ShaderParam_s(rs, paramCount);
101             for (int i = 0; i < paramCount; i++) {
102                 rsParams.set(paramList.get(i), i, false);
103             }
104             rsParams.copyAll();
105         }
106         return rsParams;
107     }
108 
ShaderParam(String name)109     public ShaderParam(String name) {
110         mParamName = name;
111         mData = new ScriptField_ShaderParamData_s.Item();
112     }
113 
getParamName()114     public String getParamName() {
115         return mParamName;
116     }
117 
setCamera(Camera c)118     public void setCamera(Camera c) {
119         mCamera = c;
120         if (mField != null) {
121             mData.camera = mCamera.getRSData().getAllocation();
122             mField.set_camera(0, mData.camera, true);
123         }
124     }
125 
incTimestamp()126     protected void incTimestamp() {
127         if (mField != null) {
128             mData.timestamp ++;
129             mData.timestamp %= sMaxTimeStamp;
130             mField.set_timestamp(0, mData.timestamp, true);
131         }
132     }
133 
initLocalData()134     abstract void initLocalData();
135 
getRSData()136     public ScriptField_ShaderParamData_s getRSData() {
137         if (mField != null) {
138             return mField;
139         }
140 
141         RenderScriptGL rs = SceneManager.getRS();
142         mField = new ScriptField_ShaderParamData_s(rs, 1);
143 
144         if (mParamName != null) {
145             mData.paramName = SceneManager.getCachedAlloc(mParamName);
146             if (mData.paramName == null) {
147                 mData.paramName = SceneManager.getStringAsAllocation(rs, mParamName);
148                 SceneManager.cacheAlloc(mParamName, mData.paramName);
149             }
150         }
151         initLocalData();
152         mData.timestamp = 1;
153 
154         mField.set(mData, 0, true);
155         return mField;
156     }
157 }
158 
159 
160 
161 
162 
163