blob: 7cd55faa4c8314df621a78a7a51f831640ec43c1 [file] [log] [blame]
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -08001/*
2 * Copyright (C) 2008 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
Alex Sakhartchoukc29a4442011-02-22 10:30:32 -080017package com.android.perftest;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080018
19import java.io.Writer;
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -070020import java.io.BufferedWriter;
21import java.io.FileWriter;
22import java.io.IOException;
23import java.io.File;
24import java.io.FileOutputStream;
25import java.io.OutputStream;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080026
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -070027import android.os.Environment;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080028import android.content.res.Resources;
29import android.graphics.Bitmap;
30import android.graphics.BitmapFactory;
31import android.renderscript.*;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070032import android.renderscript.Element.DataKind;
33import android.renderscript.Element.DataType;
Jason Sams6d8eb262010-12-15 01:41:00 -080034import android.renderscript.Allocation.MipmapControl;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080035import android.renderscript.Program.TextureType;
36import android.renderscript.ProgramStore.DepthFunc;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080037import android.renderscript.ProgramStore.BlendSrcFunc;
38import android.renderscript.ProgramStore.BlendDstFunc;
Xia Wang2738fb12011-04-15 17:24:08 -070039import android.renderscript.RenderScript.RSMessageHandler;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080040import android.renderscript.Sampler.Value;
41import android.util.Log;
42
43
44public class RsBenchRS {
45
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -070046 private static final String TAG = "RsBenchRS";
47
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080048 int mWidth;
49 int mHeight;
Xia Wang2738fb12011-04-15 17:24:08 -070050 int mLoops;
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -070051 int mCurrentLoop;
52
53 int mBenchmarkDimX;
54 int mBenchmarkDimY;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080055
56 public RsBenchRS() {
57 }
58
Xia Wang2738fb12011-04-15 17:24:08 -070059 public void init(RenderScriptGL rs, Resources res, int width, int height, int loops) {
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080060 mRS = rs;
61 mRes = res;
62 mWidth = width;
63 mHeight = height;
64 mOptionsARGB.inScaled = false;
65 mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888;
66 mMode = 0;
67 mMaxModes = 0;
Xia Wang2738fb12011-04-15 17:24:08 -070068 mLoops = loops;
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -070069 mCurrentLoop = 0;
70 mBenchmarkDimX = 1280;
71 mBenchmarkDimY = 720;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080072 initRS();
73 }
74
Xia Wang2738fb12011-04-15 17:24:08 -070075 private boolean stopTest = false;
76
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080077 private Resources mRes;
78 private RenderScriptGL mRS;
79
80 private Sampler mLinearClamp;
81 private Sampler mLinearWrap;
82 private Sampler mMipLinearWrap;
83 private Sampler mNearestClamp;
84 private Sampler mMipLinearAniso8;
85 private Sampler mMipLinearAniso15;
86
87 private ProgramStore mProgStoreBlendNoneDepth;
88 private ProgramStore mProgStoreBlendNone;
89 private ProgramStore mProgStoreBlendAlpha;
90 private ProgramStore mProgStoreBlendAdd;
91
92 private ProgramFragment mProgFragmentTexture;
93 private ProgramFragment mProgFragmentColor;
94
95 private ProgramVertex mProgVertex;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080096 private ProgramVertexFixedFunction.Constants mPVA;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080097
98 // Custom shaders
99 private ProgramVertex mProgVertexCustom;
100 private ProgramFragment mProgFragmentCustom;
101 private ProgramFragment mProgFragmentMultitex;
102 private ProgramVertex mProgVertexPixelLight;
103 private ProgramVertex mProgVertexPixelLightMove;
104 private ProgramFragment mProgFragmentPixelLight;
105 private ScriptField_VertexShaderConstants_s mVSConst;
106 private ScriptField_FragentShaderConstants_s mFSConst;
107 private ScriptField_VertexShaderConstants3_s mVSConstPixel;
108 private ScriptField_FragentShaderConstants3_s mFSConstPixel;
109
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800110 private ProgramRaster mCullBack;
111 private ProgramRaster mCullFront;
112 private ProgramRaster mCullNone;
113
114 private Allocation mTexTorus;
115 private Allocation mTexOpaque;
116 private Allocation mTexTransparent;
117 private Allocation mTexChecker;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800118
119 private Mesh m10by10Mesh;
120 private Mesh m100by100Mesh;
121 private Mesh mWbyHMesh;
122 private Mesh mTorus;
123
124 Font mFontSans;
125 Font mFontSerif;
126 Font mFontSerifBold;
127 Font mFontSerifItalic;
128 Font mFontSerifBoldItalic;
129 Font mFontMono;
130 private Allocation mTextAlloc;
131
132 private ScriptC_rsbench mScript;
133
134 private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();
135
136 int mMode;
137 int mMaxModes;
138
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700139 String[] mTestNames;
140 float[] mLocalTestResults;
141
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800142 public void onActionDown(int x, int y) {
143 mMode ++;
144 mMode = mMode % mMaxModes;
145 mScript.set_gDisplayMode(mMode);
146 }
147
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700148 private void saveTestResults() {
149 String state = Environment.getExternalStorageState();
150 if (!Environment.MEDIA_MOUNTED.equals(state)) {
151 Log.v(TAG, "sdcard is read only");
152 return;
153 }
154 File sdCard = Environment.getExternalStorageDirectory();
155 if (!sdCard.canWrite()) {
156 Log.v(TAG, "ssdcard is read only");
157 return;
158 }
159
160 File resultFile = new File(sdCard, "rsbench_result" + mCurrentLoop + ".csv");
161 resultFile.setWritable(true, false);
162
163 try {
164 BufferedWriter results = new BufferedWriter(new FileWriter(resultFile));
165 for (int i = 0; i < mLocalTestResults.length; i ++) {
166 results.write(mTestNames[i] + ", " + mLocalTestResults[i] + ",\n");
167 }
168 results.close();
169 Log.v(TAG, "Saved results in: " + resultFile.getAbsolutePath());
170 } catch (IOException e) {
171 Log.v(TAG, "Unable to write result file " + e.getMessage());
172 }
173 }
174
Xia Wang2738fb12011-04-15 17:24:08 -0700175 /**
176 * Create a message handler to handle message sent from the script
177 */
178 protected RSMessageHandler mRsMessage = new RSMessageHandler() {
179 public void run() {
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700180 if (mID == mScript.get_RS_MSG_RESULTS_READY()) {
181 for (int i = 0; i < mLocalTestResults.length; i ++) {
182 mLocalTestResults[i] = Float.intBitsToFloat(mData[i]);
183 }
184 saveTestResults();
185 if (mLoops > 0) {
186 mCurrentLoop ++;
187 mCurrentLoop = mCurrentLoop % mLoops;
188 }
189 return;
190
191 } else if (mID == mScript.get_RS_MSG_TEST_DONE()) {
Xia Wang2738fb12011-04-15 17:24:08 -0700192 synchronized(this) {
193 stopTest = true;
194 this.notifyAll();
195 }
196 return;
197 } else {
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700198 Log.v(TAG, "Perf test got unexpected message");
Xia Wang2738fb12011-04-15 17:24:08 -0700199 return;
200 }
201 }
202 };
203
204 /**
205 * Wait for message from the script
206 */
207 public boolean testIsFinished() {
208 synchronized(this) {
209 while (true) {
210 if (stopTest) {
211 return true;
212 } else {
213 try {
214 this.wait(60*1000);
215 } catch (InterruptedException e) {
216 e.printStackTrace();
217 }
218 }
219 }
220 }
221 }
222
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800223 ProgramStore BLEND_ADD_DEPTH_NONE(RenderScript rs) {
224 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
225 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
226 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE);
227 builder.setDitherEnabled(false);
228 builder.setDepthMaskEnabled(false);
229 return builder.create();
230 }
231
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800232 private Mesh getMbyNMesh(float width, float height, int wResolution, int hResolution) {
233
234 Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRS,
235 2, Mesh.TriangleMeshBuilder.TEXTURE_0);
236
237 for (int y = 0; y <= hResolution; y++) {
238 final float normalizedY = (float)y / hResolution;
239 final float yOffset = (normalizedY - 0.5f) * height;
240 for (int x = 0; x <= wResolution; x++) {
241 float normalizedX = (float)x / wResolution;
242 float xOffset = (normalizedX - 0.5f) * width;
243 tmb.setTexture((float)x % 2, (float)y % 2);
244 tmb.addVertex(xOffset, yOffset);
245 }
246 }
247
248 for (int y = 0; y < hResolution; y++) {
249 final int curY = y * (wResolution + 1);
250 final int belowY = (y + 1) * (wResolution + 1);
251 for (int x = 0; x < wResolution; x++) {
252 int curV = curY + x;
253 int belowV = belowY + x;
254 tmb.addTriangle(curV, belowV, curV + 1);
255 tmb.addTriangle(belowV, belowV + 1, curV + 1);
256 }
257 }
258
259 return tmb.create(true);
260 }
261
262 private void initProgramStore() {
263 // Use stock the stock program store object
264 mProgStoreBlendNoneDepth = ProgramStore.BLEND_NONE_DEPTH_TEST(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800265 mProgStoreBlendNone = ProgramStore.BLEND_NONE_DEPTH_NONE(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800266
267 // Create a custom program store
268 ProgramStore.Builder builder = new ProgramStore.Builder(mRS);
269 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
270 builder.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
271 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800272 builder.setDitherEnabled(false);
273 builder.setDepthMaskEnabled(false);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800274 mProgStoreBlendAlpha = builder.create();
275
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800276 mProgStoreBlendAdd = BLEND_ADD_DEPTH_NONE(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800277
278 mScript.set_gProgStoreBlendNoneDepth(mProgStoreBlendNoneDepth);
279 mScript.set_gProgStoreBlendNone(mProgStoreBlendNone);
280 mScript.set_gProgStoreBlendAlpha(mProgStoreBlendAlpha);
281 mScript.set_gProgStoreBlendAdd(mProgStoreBlendAdd);
282 }
283
284 private void initProgramFragment() {
285
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800286 ProgramFragmentFixedFunction.Builder texBuilder = new ProgramFragmentFixedFunction.Builder(mRS);
287 texBuilder.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
288 ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800289 mProgFragmentTexture = texBuilder.create();
290 mProgFragmentTexture.bindSampler(mLinearClamp, 0);
291
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800292 ProgramFragmentFixedFunction.Builder colBuilder = new ProgramFragmentFixedFunction.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800293 colBuilder.setVaryingColor(false);
294 mProgFragmentColor = colBuilder.create();
295
296 mScript.set_gProgFragmentColor(mProgFragmentColor);
297 mScript.set_gProgFragmentTexture(mProgFragmentTexture);
298 }
299
300 private void initProgramVertex() {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800301 ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800302 mProgVertex = pvb.create();
303
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800304 mPVA = new ProgramVertexFixedFunction.Constants(mRS);
305 ((ProgramVertexFixedFunction)mProgVertex).bindConstants(mPVA);
306 Matrix4f proj = new Matrix4f();
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700307 proj.loadOrthoWindow(mBenchmarkDimX, mBenchmarkDimY);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800308 mPVA.setProjection(proj);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800309
310 mScript.set_gProgVertex(mProgVertex);
311 }
312
313 private void initCustomShaders() {
314 mVSConst = new ScriptField_VertexShaderConstants_s(mRS, 1);
315 mFSConst = new ScriptField_FragentShaderConstants_s(mRS, 1);
316 mScript.bind_gVSConstants(mVSConst);
317 mScript.bind_gFSConstants(mFSConst);
318
319 mVSConstPixel = new ScriptField_VertexShaderConstants3_s(mRS, 1);
320 mFSConstPixel = new ScriptField_FragentShaderConstants3_s(mRS, 1);
321 mScript.bind_gVSConstPixel(mVSConstPixel);
322 mScript.bind_gFSConstPixel(mFSConstPixel);
323
324 // Initialize the shader builder
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800325 ProgramVertex.Builder pvbCustom = new ProgramVertex.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800326 // Specify the resource that contains the shader string
327 pvbCustom.setShader(mRes, R.raw.shaderv);
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800328 // Use a script field to specify the input layout
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800329 pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
330 // Define the constant input layout
331 pvbCustom.addConstant(mVSConst.getAllocation().getType());
332 mProgVertexCustom = pvbCustom.create();
333 // Bind the source of constant data
334 mProgVertexCustom.bindConstants(mVSConst.getAllocation(), 0);
335
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800336 ProgramFragment.Builder pfbCustom = new ProgramFragment.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800337 // Specify the resource that contains the shader string
338 pfbCustom.setShader(mRes, R.raw.shaderf);
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800339 // Tell the builder how many textures we have
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800340 pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
341 // Define the constant input layout
342 pfbCustom.addConstant(mFSConst.getAllocation().getType());
343 mProgFragmentCustom = pfbCustom.create();
344 // Bind the source of constant data
345 mProgFragmentCustom.bindConstants(mFSConst.getAllocation(), 0);
346
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800347 pvbCustom = new ProgramVertex.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800348 pvbCustom.setShader(mRes, R.raw.shader2v);
349 pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
350 pvbCustom.addConstant(mVSConstPixel.getAllocation().getType());
351 mProgVertexPixelLight = pvbCustom.create();
352 mProgVertexPixelLight.bindConstants(mVSConstPixel.getAllocation(), 0);
353
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800354 pvbCustom = new ProgramVertex.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800355 pvbCustom.setShader(mRes, R.raw.shader2movev);
356 pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
357 pvbCustom.addConstant(mVSConstPixel.getAllocation().getType());
358 mProgVertexPixelLightMove = pvbCustom.create();
359 mProgVertexPixelLightMove.bindConstants(mVSConstPixel.getAllocation(), 0);
360
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800361 pfbCustom = new ProgramFragment.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800362 pfbCustom.setShader(mRes, R.raw.shader2f);
363 pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
364 pfbCustom.addConstant(mFSConstPixel.getAllocation().getType());
365 mProgFragmentPixelLight = pfbCustom.create();
366 mProgFragmentPixelLight.bindConstants(mFSConstPixel.getAllocation(), 0);
367
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800368 pfbCustom = new ProgramFragment.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800369 pfbCustom.setShader(mRes, R.raw.multitexf);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800370 for (int texCount = 0; texCount < 3; texCount ++) {
371 pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
372 }
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800373 mProgFragmentMultitex = pfbCustom.create();
374
375 mScript.set_gProgVertexCustom(mProgVertexCustom);
376 mScript.set_gProgFragmentCustom(mProgFragmentCustom);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800377 mScript.set_gProgVertexPixelLight(mProgVertexPixelLight);
378 mScript.set_gProgVertexPixelLightMove(mProgVertexPixelLightMove);
379 mScript.set_gProgFragmentPixelLight(mProgFragmentPixelLight);
380 mScript.set_gProgFragmentMultitex(mProgFragmentMultitex);
381 }
382
383 private Allocation loadTextureRGB(int id) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800384 return Allocation.createFromBitmapResource(mRS, mRes, id,
385 Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
386 Allocation.USAGE_GRAPHICS_TEXTURE);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800387 }
388
389 private Allocation loadTextureARGB(int id) {
390 Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB);
Jason Sams6d8eb262010-12-15 01:41:00 -0800391 return Allocation.createFromBitmap(mRS, b,
392 Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
393 Allocation.USAGE_GRAPHICS_TEXTURE);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800394 }
395
396 private void loadImages() {
397 mTexTorus = loadTextureRGB(R.drawable.torusmap);
398 mTexOpaque = loadTextureRGB(R.drawable.data);
399 mTexTransparent = loadTextureARGB(R.drawable.leaf);
400 mTexChecker = loadTextureRGB(R.drawable.checker);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800401
402 mScript.set_gTexTorus(mTexTorus);
403 mScript.set_gTexOpaque(mTexOpaque);
404 mScript.set_gTexTransparent(mTexTransparent);
405 mScript.set_gTexChecker(mTexChecker);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800406 }
407
408 private void initFonts() {
409 // Sans font by family name
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800410 mFontSans = Font.create(mRS, mRes, "sans-serif", Font.Style.NORMAL, 8);
411 mFontSerif = Font.create(mRS, mRes, "serif", Font.Style.NORMAL, 8);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800412 // Create fonts by family and style
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800413 mFontSerifBold = Font.create(mRS, mRes, "serif", Font.Style.BOLD, 8);
414 mFontSerifItalic = Font.create(mRS, mRes, "serif", Font.Style.ITALIC, 8);
415 mFontSerifBoldItalic = Font.create(mRS, mRes, "serif", Font.Style.BOLD_ITALIC, 8);
416 mFontMono = Font.create(mRS, mRes, "mono", Font.Style.NORMAL, 8);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800417
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800418 mTextAlloc = Allocation.createFromString(mRS, "String from allocation", Allocation.USAGE_SCRIPT);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800419
420 mScript.set_gFontSans(mFontSans);
421 mScript.set_gFontSerif(mFontSerif);
422 mScript.set_gFontSerifBold(mFontSerifBold);
423 mScript.set_gFontSerifItalic(mFontSerifItalic);
424 mScript.set_gFontSerifBoldItalic(mFontSerifBoldItalic);
425 mScript.set_gFontMono(mFontMono);
426 mScript.set_gTextAlloc(mTextAlloc);
427 }
428
429 private void initMesh() {
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700430 m10by10Mesh = getMbyNMesh(mBenchmarkDimX, mBenchmarkDimY, 10, 10);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800431 mScript.set_g10by10Mesh(m10by10Mesh);
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700432 m100by100Mesh = getMbyNMesh(mBenchmarkDimX, mBenchmarkDimY, 100, 100);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800433 mScript.set_g100by100Mesh(m100by100Mesh);
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700434 mWbyHMesh= getMbyNMesh(mBenchmarkDimX, mBenchmarkDimY, mBenchmarkDimX/4, mBenchmarkDimY/4);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800435 mScript.set_gWbyHMesh(mWbyHMesh);
436
437 FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.torus);
438 FileA3D.IndexEntry entry = model.getIndexEntry(0);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800439 if (entry == null || entry.getEntryType() != FileA3D.EntryType.MESH) {
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800440 Log.e("rs", "could not load model");
441 } else {
442 mTorus = (Mesh)entry.getObject();
443 mScript.set_gTorusMesh(mTorus);
444 }
445 }
446
447 private void initSamplers() {
448 Sampler.Builder bs = new Sampler.Builder(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800449 bs.setMinification(Sampler.Value.LINEAR);
450 bs.setMagnification(Sampler.Value.LINEAR);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800451 bs.setWrapS(Sampler.Value.WRAP);
452 bs.setWrapT(Sampler.Value.WRAP);
453 mLinearWrap = bs.create();
454
455 mLinearClamp = Sampler.CLAMP_LINEAR(mRS);
456 mNearestClamp = Sampler.CLAMP_NEAREST(mRS);
457 mMipLinearWrap = Sampler.WRAP_LINEAR_MIP_LINEAR(mRS);
458
459 bs = new Sampler.Builder(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800460 bs.setMinification(Sampler.Value.LINEAR_MIP_LINEAR);
461 bs.setMagnification(Sampler.Value.LINEAR);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800462 bs.setWrapS(Sampler.Value.WRAP);
463 bs.setWrapT(Sampler.Value.WRAP);
464 bs.setAnisotropy(8.0f);
465 mMipLinearAniso8 = bs.create();
466 bs.setAnisotropy(15.0f);
467 mMipLinearAniso15 = bs.create();
468
469 mScript.set_gLinearClamp(mLinearClamp);
470 mScript.set_gLinearWrap(mLinearWrap);
471 mScript.set_gMipLinearWrap(mMipLinearWrap);
472 mScript.set_gMipLinearAniso8(mMipLinearAniso8);
473 mScript.set_gMipLinearAniso15(mMipLinearAniso15);
474 mScript.set_gNearestClamp(mNearestClamp);
475 }
476
477 private void initProgramRaster() {
478 mCullBack = ProgramRaster.CULL_BACK(mRS);
479 mCullFront = ProgramRaster.CULL_FRONT(mRS);
480 mCullNone = ProgramRaster.CULL_NONE(mRS);
481
482 mScript.set_gCullBack(mCullBack);
483 mScript.set_gCullFront(mCullFront);
484 mScript.set_gCullNone(mCullNone);
485 }
486
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700487 private int strlen(byte[] array) {
488 int count = 0;
489 while(count < array.length && array[count] != 0) {
490 count ++;
491 }
492 return count;
493 }
494
495 private void prepareTestData() {
496 mTestNames = new String[mMaxModes];
497 mLocalTestResults = new float[mMaxModes];
498 int scratchSize = 1024;
499 Allocation scratch = Allocation.createSized(mRS, Element.U8(mRS), scratchSize);
500 byte[] tmp = new byte[scratchSize];
501 mScript.bind_gStringBuffer(scratch);
502 for (int i = 0; i < mMaxModes; i ++) {
503 mScript.invoke_getTestName(i);
504 scratch.copyTo(tmp);
505 int len = strlen(tmp);
506 mTestNames[i] = new String(tmp, 0, len);
507 }
508 }
509
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800510 private void initRS() {
511
512 mScript = new ScriptC_rsbench(mRS, mRes, R.raw.rsbench);
Xia Wang2738fb12011-04-15 17:24:08 -0700513 mRS.setMessageHandler(mRsMessage);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800514
515 mMaxModes = mScript.get_gMaxModes();
Xia Wang2738fb12011-04-15 17:24:08 -0700516 mScript.set_gMaxLoops(mLoops);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800517
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700518 prepareTestData();
519
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800520 initSamplers();
521 initProgramStore();
522 initProgramFragment();
523 initProgramVertex();
524 initFonts();
525 loadImages();
526 initMesh();
527 initProgramRaster();
528 initCustomShaders();
529
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700530 Type.Builder b = new Type.Builder(mRS, Element.RGBA_8888(mRS));
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700531 b.setX(mBenchmarkDimX).setY(mBenchmarkDimY);
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700532 Allocation offscreen = Allocation.createTyped(mRS,
533 b.create(),
534 Allocation.USAGE_GRAPHICS_TEXTURE |
535 Allocation.USAGE_GRAPHICS_RENDER_TARGET);
536 mScript.set_gRenderBufferColor(offscreen);
537
538 b = new Type.Builder(mRS,
539 Element.createPixel(mRS, DataType.UNSIGNED_16,
540 DataKind.PIXEL_DEPTH));
Alex Sakhartchouk4d7b19a2011-05-04 14:38:53 -0700541 b.setX(mBenchmarkDimX).setY(mBenchmarkDimY);
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700542 offscreen = Allocation.createTyped(mRS,
543 b.create(),
544 Allocation.USAGE_GRAPHICS_RENDER_TARGET);
545 mScript.set_gRenderBufferDepth(offscreen);
546
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800547 mRS.bindRootScript(mScript);
548 }
549}