Renderable.java revision e31264694e9729db49acbb2d32eab2703efc8501
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;
22import java.util.Iterator;
23
24import com.android.scenegraph.Float4Param;
25import com.android.scenegraph.ShaderParam;
26import com.android.scenegraph.TransformParam;
27
28import android.content.res.Resources;
29import android.renderscript.Allocation;
30import android.renderscript.Element;
31import android.renderscript.Element.DataType;
32import android.renderscript.Matrix4f;
33import android.renderscript.Mesh;
34import android.renderscript.ProgramFragment;
35import android.renderscript.ProgramStore;
36import android.renderscript.ProgramVertex;
37import android.renderscript.RenderScriptGL;
38import android.util.Log;
39
40/**
41 * @hide
42 */
43public class Renderable extends RenderableBase {
44    HashMap<String, ShaderParam> mSourceParams;
45
46    RenderState mRenderState;
47    Transform mTransform;
48
49    String mMeshName;
50    String mMeshIndexName;
51
52    public String mMaterialName;
53
54    ScriptField_Renderable_s mField;
55    ScriptField_Renderable_s.Item mData;
56
57    public Renderable() {
58        mSourceParams = new HashMap<String, ShaderParam>();
59        mData = new ScriptField_Renderable_s.Item();
60    }
61
62    public void setCullType(int cull) {
63        mData.cullType = cull;
64    }
65
66    public void setRenderState(RenderState renderState) {
67        mRenderState = renderState;
68    }
69
70    public void setMesh(Mesh mesh) {
71        mData.mesh = mesh;
72    }
73
74    public void setMesh(String mesh, String indexName) {
75        mMeshName = mesh;
76        mMeshIndexName = indexName;
77    }
78
79    public void setMaterialName(String name) {
80        mMaterialName = name;
81    }
82
83    public void setTransform(Transform t) {
84        mTransform = t;
85    }
86
87    public void appendSourceParams(ShaderParam p) {
88        mSourceParams.put(p.getParamName(), p);
89    }
90
91    public void resolveMeshData(Mesh mesh) {
92        mData.mesh = mesh;
93        if (mData.mesh == null) {
94            Log.v("DRAWABLE: ", "*** NO MESH *** " + mMeshName);
95            return;
96        }
97        int subIndexCount = mData.mesh.getPrimitiveCount();
98        if (subIndexCount == 1 || mMeshIndexName == null) {
99            mData.meshIndex = 0;
100        } else {
101            for (int i = 0; i < subIndexCount; i ++) {
102                if (mData.mesh.getIndexSetAllocation(i).getName().equals(mMeshIndexName)) {
103                    mData.meshIndex = i;
104                    break;
105                }
106            }
107        }
108        if (mField != null) {
109            mField.set(mData, 0, true);
110        }
111    }
112
113    void updateTextures(RenderScriptGL rs, Resources res) {
114        Iterator<ShaderParam> allParamsIter = mSourceParams.values().iterator();
115        int paramIndex = 0;
116        while (allParamsIter.hasNext()) {
117            ShaderParam sp = allParamsIter.next();
118            if (sp instanceof TextureParam) {
119                TextureParam p = (TextureParam)sp;
120                TextureBase tex = p.getTexture();
121                if (tex != null) {
122                    mData.pf_textures[paramIndex++] = tex.getRsData();
123                }
124            }
125        }
126        ProgramFragment pf = mRenderState.mFragment.mProgram;
127        mData.pf_num_textures = pf != null ? Math.min(pf.getTextureCount(), paramIndex) : 0;
128        mField.set(mData, 0, true);
129    }
130
131    public void setVisible(boolean vis) {
132        mData.cullType = vis ? 0 : 2;
133        if (mField != null) {
134            mField.set_cullType(0, mData.cullType, true);
135        }
136    }
137
138    ScriptField_Renderable_s getRsField(RenderScriptGL rs, Resources res) {
139        if (mField != null) {
140            return mField;
141        }
142        getRsFieldItem(rs, res);
143
144        mField = new ScriptField_Renderable_s(rs, 1);
145        mField.set(mData, 0, true);
146
147        return mField;
148    }
149
150    void getRsFieldItem(RenderScriptGL rs, Resources res) {
151        Allocation pvParams = null, pfParams = null;
152        Allocation vertexConstants = null, fragmentConstants = null;
153        VertexShader pv = mRenderState.mVertex;
154        if (pv != null && pv.getObjectConstants() != null) {
155            vertexConstants = Allocation.createTyped(rs, pv.getObjectConstants());
156            Element vertexConst = vertexConstants.getType().getElement();
157            pvParams = ShaderParam.fillInParams(vertexConst, mSourceParams,
158                                                mTransform).getAllocation();
159        }
160        FragmentShader pf = mRenderState.mFragment;
161        if (pf != null && pf.getObjectConstants() != null) {
162            fragmentConstants = Allocation.createTyped(rs, pf.getObjectConstants());
163            Element fragmentConst = fragmentConstants.getType().getElement();
164            pfParams = ShaderParam.fillInParams(fragmentConst, mSourceParams,
165                                                mTransform).getAllocation();
166        }
167
168        mData.pv_const = vertexConstants;
169        mData.pv_constParams = pvParams;
170        mData.pf_const = fragmentConstants;
171        mData.pf_constParams = pfParams;
172        if (mTransform != null) {
173            mData.transformMatrix = mTransform.getRSData().getAllocation();
174        }
175        mData.name = SceneManager.getStringAsAllocation(rs, getName());
176        mData.render_state = mRenderState.getRSData().getAllocation();
177        mData.bVolInitialized = 0;
178    }
179}
180
181
182
183
184
185