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.perftest;
18
19import android.os.Environment;
20import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.renderscript.*;
24import android.renderscript.Element.DataKind;
25import android.renderscript.Element.DataType;
26import android.renderscript.Allocation.MipmapControl;
27import android.renderscript.Program.TextureType;
28import android.renderscript.RenderScript.RSMessageHandler;
29import android.renderscript.Mesh.Primitive;
30import android.renderscript.Matrix4f;
31import android.renderscript.ProgramVertexFixedFunction;
32
33import android.util.Log;
34
35
36public class TorusTest implements RsBenchBaseTest{
37
38    private static final String TAG = "TorusTest";
39    private RenderScriptGL mRS;
40    private Resources mRes;
41
42    private ProgramStore mProgStoreBlendNoneDepth;
43    private ProgramStore mProgStoreBlendNone;
44    private ProgramStore mProgStoreBlendAlpha;
45
46    private ProgramFragment mProgFragmentTexture;
47    private ProgramFragment mProgFragmentColor;
48
49    private ProgramVertex mProgVertex;
50    private ProgramVertexFixedFunction.Constants mPVA;
51    private ProgramVertexFixedFunction.Constants mPvProjectionAlloc;
52
53    // Custom shaders
54    private ProgramVertex mProgVertexCustom;
55    private ProgramFragment mProgFragmentCustom;
56    private ProgramFragment mProgFragmentMultitex;
57    private ProgramVertex mProgVertexPixelLight;
58    private ProgramVertex mProgVertexPixelLightMove;
59    private ProgramFragment mProgFragmentPixelLight;
60    private ScriptField_VertexShaderConstants_s mVSConst;
61    private ScriptField_FragentShaderConstants_s mFSConst;
62    private ScriptField_VertexShaderConstants3_s mVSConstPixel;
63    private ScriptField_FragentShaderConstants3_s mFSConstPixel;
64
65    private Allocation mTexTorus;
66    private Mesh mTorus;
67
68    private ScriptC_torus_test mTorusScript;
69
70    private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();
71
72    ScriptField_TestScripts_s.Item[] mTests;
73
74    private final String[] mNames = {
75        "Geo test 25.6k flat color",
76        "Geo test 51.2k flat color",
77        "Geo test 204.8k small tries flat color",
78        "Geo test 25.6k single texture",
79        "Geo test 51.2k single texture",
80        "Geo test 204.8k small tries single texture",
81        "Geo test 25.6k geo heavy vertex",
82        "Geo test 51.2k geo heavy vertex",
83        "Geo test 204.8k geo raster load heavy vertex",
84        "Geo test 25.6k heavy fragment",
85        "Geo test 51.2k heavy fragment",
86        "Geo test 204.8k small tries heavy fragment",
87        "Geo test 25.6k heavy fragment heavy vertex",
88        "Geo test 51.2k heavy fragment heavy vertex",
89        "Geo test 204.8k small tries heavy fragment heavy vertex"
90    };
91
92    public TorusTest() {
93    }
94
95    void addTest(int index, int testId, int user1, int user2) {
96        mTests[index] = new ScriptField_TestScripts_s.Item();
97        mTests[index].testScript = mTorusScript;
98        mTests[index].testName = Allocation.createFromString(mRS,
99                                                             mNames[index],
100                                                             Allocation.USAGE_SCRIPT);
101        mTests[index].debugName = RsBenchRS.createZeroTerminatedAlloc(mRS,
102                                                                      mNames[index],
103                                                                      Allocation.USAGE_SCRIPT);
104
105        ScriptField_TorusTestData_s.Item dataItem = new ScriptField_TorusTestData_s.Item();
106        dataItem.testId = testId;
107        dataItem.user1 = user1;
108        dataItem.user2 = user2;
109        ScriptField_TorusTestData_s testData = new ScriptField_TorusTestData_s(mRS, 1);
110        testData.set(dataItem, 0, true);
111        mTests[index].testData = testData.getAllocation();
112    }
113
114    public boolean init(RenderScriptGL rs, Resources res) {
115        mRS = rs;
116        mRes = res;
117        initCustomShaders();
118        loadImages();
119        initMesh();
120        initTorusScript();
121        mTests = new ScriptField_TestScripts_s.Item[mNames.length];
122
123        int index = 0;
124        addTest(index++, 0, 0 /*useTexture*/, 1 /*numMeshes*/);
125        addTest(index++, 0, 0 /*useTexture*/, 2 /*numMeshes*/);
126        addTest(index++, 0, 0 /*useTexture*/, 8 /*numMeshes*/);
127        addTest(index++, 0, 1 /*useTexture*/, 1 /*numMeshes*/);
128        addTest(index++, 0, 1 /*useTexture*/, 2 /*numMeshes*/);
129        addTest(index++, 0, 1 /*useTexture*/, 8 /*numMeshes*/);
130
131        // Secont test
132        addTest(index++, 1, 1 /*numMeshes*/, 0 /*unused*/);
133        addTest(index++, 1, 2 /*numMeshes*/, 0 /*unused*/);
134        addTest(index++, 1, 8 /*numMeshes*/, 0 /*unused*/);
135
136        // Third test
137        addTest(index++, 2, 1 /*numMeshes*/, 0 /*heavyVertex*/);
138        addTest(index++, 2, 2 /*numMeshes*/, 0 /*heavyVertex*/);
139        addTest(index++, 2, 8 /*numMeshes*/, 0 /*heavyVertex*/);
140        addTest(index++, 2, 1 /*numMeshes*/, 1 /*heavyVertex*/);
141        addTest(index++, 2, 2 /*numMeshes*/, 1 /*heavyVertex*/);
142        addTest(index++, 2, 8 /*numMeshes*/, 1 /*heavyVertex*/);
143
144        return true;
145    }
146
147    public ScriptField_TestScripts_s.Item[] getTests() {
148        return mTests;
149    }
150
151    public String[] getTestNames() {
152        return mNames;
153    }
154
155    private void initCustomShaders() {
156        mVSConst = new ScriptField_VertexShaderConstants_s(mRS, 1);
157        mFSConst = new ScriptField_FragentShaderConstants_s(mRS, 1);
158
159        mVSConstPixel = new ScriptField_VertexShaderConstants3_s(mRS, 1);
160        mFSConstPixel = new ScriptField_FragentShaderConstants3_s(mRS, 1);
161
162        // Initialize the shader builder
163        ProgramVertex.Builder pvbCustom = new ProgramVertex.Builder(mRS);
164        // Specify the resource that contains the shader string
165        pvbCustom.setShader(mRes, R.raw.shaderv);
166        // Use a script field to specify the input layout
167        pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
168        // Define the constant input layout
169        pvbCustom.addConstant(mVSConst.getAllocation().getType());
170        mProgVertexCustom = pvbCustom.create();
171        // Bind the source of constant data
172        mProgVertexCustom.bindConstants(mVSConst.getAllocation(), 0);
173
174        ProgramFragment.Builder pfbCustom = new ProgramFragment.Builder(mRS);
175        // Specify the resource that contains the shader string
176        pfbCustom.setShader(mRes, R.raw.shaderf);
177        // Tell the builder how many textures we have
178        pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
179        // Define the constant input layout
180        pfbCustom.addConstant(mFSConst.getAllocation().getType());
181        mProgFragmentCustom = pfbCustom.create();
182        // Bind the source of constant data
183        mProgFragmentCustom.bindConstants(mFSConst.getAllocation(), 0);
184
185        pvbCustom = new ProgramVertex.Builder(mRS);
186        pvbCustom.setShader(mRes, R.raw.shader2v);
187        pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
188        pvbCustom.addConstant(mVSConstPixel.getAllocation().getType());
189        mProgVertexPixelLight = pvbCustom.create();
190        mProgVertexPixelLight.bindConstants(mVSConstPixel.getAllocation(), 0);
191
192        pvbCustom = new ProgramVertex.Builder(mRS);
193        pvbCustom.setShader(mRes, R.raw.shader2movev);
194        pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
195        pvbCustom.addConstant(mVSConstPixel.getAllocation().getType());
196        mProgVertexPixelLightMove = pvbCustom.create();
197        mProgVertexPixelLightMove.bindConstants(mVSConstPixel.getAllocation(), 0);
198
199        pfbCustom = new ProgramFragment.Builder(mRS);
200        pfbCustom.setShader(mRes, R.raw.shader2f);
201        pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
202        pfbCustom.addConstant(mFSConstPixel.getAllocation().getType());
203        mProgFragmentPixelLight = pfbCustom.create();
204        mProgFragmentPixelLight.bindConstants(mFSConstPixel.getAllocation(), 0);
205
206        pfbCustom = new ProgramFragment.Builder(mRS);
207        pfbCustom.setShader(mRes, R.raw.multitexf);
208        for (int texCount = 0; texCount < 3; texCount ++) {
209            pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
210        }
211        mProgFragmentMultitex = pfbCustom.create();
212
213        ProgramFragmentFixedFunction.Builder colBuilder = new ProgramFragmentFixedFunction.Builder(mRS);
214        colBuilder.setVaryingColor(false);
215        mProgFragmentColor = colBuilder.create();
216
217        ProgramFragmentFixedFunction.Builder texBuilder = new ProgramFragmentFixedFunction.Builder(mRS);
218        texBuilder.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
219                              ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
220        mProgFragmentTexture = texBuilder.create();
221
222        ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
223        mProgVertex = pvb.create();
224        ProgramVertexFixedFunction.Constants PVA = new ProgramVertexFixedFunction.Constants(mRS);
225        ((ProgramVertexFixedFunction)mProgVertex).bindConstants(PVA);
226        Matrix4f proj = new Matrix4f();
227        proj.loadOrthoWindow(1280, 720);
228        PVA.setProjection(proj);
229    }
230
231    private Allocation loadTextureRGB(int id) {
232        return Allocation.createFromBitmapResource(mRS, mRes, id,
233                Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
234                Allocation.USAGE_GRAPHICS_TEXTURE);
235    }
236
237    private void loadImages() {
238        mTexTorus = loadTextureRGB(R.drawable.torusmap);
239    }
240
241    private void initMesh() {
242        FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.torus);
243        FileA3D.IndexEntry entry = model.getIndexEntry(0);
244        if (entry == null || entry.getEntryType() != FileA3D.EntryType.MESH) {
245            Log.e("rs", "could not load model");
246        } else {
247            mTorus = (Mesh)entry.getObject();
248        }
249    }
250
251    void initTorusScript() {
252        mTorusScript = new ScriptC_torus_test(mRS, mRes, R.raw.torus_test);
253        mTorusScript.set_gCullFront(ProgramRaster.CULL_FRONT(mRS));
254        mTorusScript.set_gCullBack(ProgramRaster.CULL_BACK(mRS));
255        mTorusScript.set_gLinearClamp(Sampler.CLAMP_LINEAR(mRS));
256        mTorusScript.set_gTorusMesh(mTorus);
257        mTorusScript.set_gTexTorus(mTexTorus);
258        mTorusScript.set_gProgVertexCustom(mProgVertexCustom);
259        mTorusScript.set_gProgFragmentCustom(mProgFragmentCustom);
260        mTorusScript.set_gProgVertexPixelLight(mProgVertexPixelLight);
261        mTorusScript.set_gProgVertexPixelLightMove(mProgVertexPixelLightMove);
262        mTorusScript.set_gProgFragmentPixelLight(mProgFragmentPixelLight);
263        mTorusScript.bind_gVSConstPixel(mVSConstPixel);
264        mTorusScript.bind_gFSConstPixel(mFSConstPixel);
265        mTorusScript.bind_gVSConstants(mVSConst);
266        mTorusScript.bind_gFSConstants(mFSConst);
267        mTorusScript.set_gProgVertex(mProgVertex);
268        mTorusScript.set_gProgFragmentTexture(mProgFragmentTexture);
269        mTorusScript.set_gProgFragmentColor(mProgFragmentColor);
270        mTorusScript.set_gProgStoreBlendNoneDepth(mProgStoreBlendNoneDepth);
271    }
272}
273