ShaderParam.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; 21import java.util.HashMap; 22 23import com.android.scenegraph.Transform; 24 25import android.renderscript.Element; 26import android.renderscript.Matrix4f; 27import android.renderscript.ProgramFragment; 28import android.renderscript.ProgramStore; 29import android.renderscript.ProgramVertex; 30import android.renderscript.RenderScriptGL; 31import android.util.Log; 32 33/** 34 * @hide 35 */ 36public abstract class ShaderParam extends SceneGraphBase { 37 static final int FLOAT4_DATA = 0; 38 static final int FLOAT4_CAMERA_POS = 1; 39 static final int FLOAT4_CAMERA_DIR = 2; 40 static final int FLOAT4_LIGHT_COLOR = 3; 41 static final int FLOAT4_LIGHT_POS = 4; 42 static final int FLOAT4_LIGHT_DIR = 5; 43 44 static final int TRANSFORM_DATA = 100; 45 static final int TRANSFORM_VIEW = 101; 46 static final int TRANSFORM_PROJ = 102; 47 static final int TRANSFORM_VIEW_PROJ = 103; 48 static final int TRANSFORM_MODEL = 104; 49 static final int TRANSFORM_MODEL_VIEW = 105; 50 static final int TRANSFORM_MODEL_VIEW_PROJ = 106; 51 52 static final int TEXTURE = 200; 53 54 static final String cameraPos = "cameraPos"; 55 static final String cameraDir = "cameraDir"; 56 57 static final String lightColor = "lightColor"; 58 static final String lightPos = "lightPos"; 59 static final String lightDir = "lightDir"; 60 61 static final String view = "view"; 62 static final String proj = "proj"; 63 static final String viewProj = "viewProj"; 64 static final String model = "model"; 65 static final String modelView = "modelView"; 66 static final String modelViewProj = "modelViewProj"; 67 68 ScriptField_ShaderParam_s.Item mRsFieldItem; 69 70 String mParamName; 71 int mOffset; 72 73 static void fillInParams(Element constantElem, 74 HashMap<String, ShaderParam> sourceParams, 75 Transform transform, 76 ArrayList<ShaderParam> paramList) { 77 int subElemCount = constantElem.getSubElementCount(); 78 for (int i = 0; i < subElemCount; i ++) { 79 String inputName = constantElem.getSubElementName(i); 80 int offset = constantElem.getSubElementOffsetBytes(i); 81 82 ShaderParam matchingParam = sourceParams.get(inputName); 83 Element subElem = constantElem.getSubElement(i); 84 // Make one if it's not there 85 if (matchingParam == null) { 86 if (subElem.getDataType() == Element.DataType.FLOAT_32) { 87 matchingParam = new Float4Param(inputName); 88 } else if (subElem.getDataType() == Element.DataType.MATRIX_4X4) { 89 TransformParam trParam = new TransformParam(inputName); 90 trParam.setTransform(transform); 91 matchingParam = trParam; 92 } 93 } 94 matchingParam.setOffset(offset); 95 if (subElem.getDataType() == Element.DataType.FLOAT_32) { 96 Float4Param fParam = (Float4Param)matchingParam; 97 fParam.setVecSize(subElem.getVectorSize()); 98 } 99 paramList.add(matchingParam); 100 } 101 } 102 103 public ShaderParam(String name) { 104 mParamName = name; 105 } 106 107 public String getParamName() { 108 return mParamName; 109 } 110 111 void setOffset(int offset) { 112 mOffset = offset; 113 } 114 115 abstract void initLocalData(RenderScriptGL rs); 116 117 public ScriptField_ShaderParam_s.Item getRSData(RenderScriptGL rs) { 118 if (mRsFieldItem != null) { 119 return mRsFieldItem; 120 } 121 122 mRsFieldItem = new ScriptField_ShaderParam_s.Item(); 123 initLocalData(rs); 124 return mRsFieldItem; 125 } 126} 127 128 129 130 131 132