blob: 9757ec693f00108956153ae2a9276c7b9eafdd6a [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;
20
21import android.content.res.Resources;
22import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
24import android.renderscript.*;
Jason Sams6d8eb262010-12-15 01:41:00 -080025import android.renderscript.Allocation.MipmapControl;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080026import android.renderscript.Program.TextureType;
27import android.renderscript.ProgramStore.DepthFunc;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080028import android.renderscript.ProgramStore.BlendSrcFunc;
29import android.renderscript.ProgramStore.BlendDstFunc;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080030import android.renderscript.Sampler.Value;
31import android.util.Log;
32
33
34public class RsBenchRS {
35
36 int mWidth;
37 int mHeight;
38
39 public RsBenchRS() {
40 }
41
42 public void init(RenderScriptGL rs, Resources res, int width, int height) {
43 mRS = rs;
44 mRes = res;
45 mWidth = width;
46 mHeight = height;
47 mOptionsARGB.inScaled = false;
48 mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888;
49 mMode = 0;
50 mMaxModes = 0;
51 initRS();
52 }
53
54 private Resources mRes;
55 private RenderScriptGL mRS;
56
57 private Sampler mLinearClamp;
58 private Sampler mLinearWrap;
59 private Sampler mMipLinearWrap;
60 private Sampler mNearestClamp;
61 private Sampler mMipLinearAniso8;
62 private Sampler mMipLinearAniso15;
63
64 private ProgramStore mProgStoreBlendNoneDepth;
65 private ProgramStore mProgStoreBlendNone;
66 private ProgramStore mProgStoreBlendAlpha;
67 private ProgramStore mProgStoreBlendAdd;
68
69 private ProgramFragment mProgFragmentTexture;
70 private ProgramFragment mProgFragmentColor;
71
72 private ProgramVertex mProgVertex;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080073 private ProgramVertexFixedFunction.Constants mPVA;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080074
75 // Custom shaders
76 private ProgramVertex mProgVertexCustom;
77 private ProgramFragment mProgFragmentCustom;
78 private ProgramFragment mProgFragmentMultitex;
79 private ProgramVertex mProgVertexPixelLight;
80 private ProgramVertex mProgVertexPixelLightMove;
81 private ProgramFragment mProgFragmentPixelLight;
82 private ScriptField_VertexShaderConstants_s mVSConst;
83 private ScriptField_FragentShaderConstants_s mFSConst;
84 private ScriptField_VertexShaderConstants3_s mVSConstPixel;
85 private ScriptField_FragentShaderConstants3_s mFSConstPixel;
86
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080087 private ProgramRaster mCullBack;
88 private ProgramRaster mCullFront;
89 private ProgramRaster mCullNone;
90
91 private Allocation mTexTorus;
92 private Allocation mTexOpaque;
93 private Allocation mTexTransparent;
94 private Allocation mTexChecker;
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -080095
96 private Mesh m10by10Mesh;
97 private Mesh m100by100Mesh;
98 private Mesh mWbyHMesh;
99 private Mesh mTorus;
100
101 Font mFontSans;
102 Font mFontSerif;
103 Font mFontSerifBold;
104 Font mFontSerifItalic;
105 Font mFontSerifBoldItalic;
106 Font mFontMono;
107 private Allocation mTextAlloc;
108
109 private ScriptC_rsbench mScript;
110
111 private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();
112
113 int mMode;
114 int mMaxModes;
115
116 public void onActionDown(int x, int y) {
117 mMode ++;
118 mMode = mMode % mMaxModes;
119 mScript.set_gDisplayMode(mMode);
120 }
121
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800122 ProgramStore BLEND_ADD_DEPTH_NONE(RenderScript rs) {
123 ProgramStore.Builder builder = new ProgramStore.Builder(rs);
124 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
125 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE);
126 builder.setDitherEnabled(false);
127 builder.setDepthMaskEnabled(false);
128 return builder.create();
129 }
130
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800131 private Mesh getMbyNMesh(float width, float height, int wResolution, int hResolution) {
132
133 Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRS,
134 2, Mesh.TriangleMeshBuilder.TEXTURE_0);
135
136 for (int y = 0; y <= hResolution; y++) {
137 final float normalizedY = (float)y / hResolution;
138 final float yOffset = (normalizedY - 0.5f) * height;
139 for (int x = 0; x <= wResolution; x++) {
140 float normalizedX = (float)x / wResolution;
141 float xOffset = (normalizedX - 0.5f) * width;
142 tmb.setTexture((float)x % 2, (float)y % 2);
143 tmb.addVertex(xOffset, yOffset);
144 }
145 }
146
147 for (int y = 0; y < hResolution; y++) {
148 final int curY = y * (wResolution + 1);
149 final int belowY = (y + 1) * (wResolution + 1);
150 for (int x = 0; x < wResolution; x++) {
151 int curV = curY + x;
152 int belowV = belowY + x;
153 tmb.addTriangle(curV, belowV, curV + 1);
154 tmb.addTriangle(belowV, belowV + 1, curV + 1);
155 }
156 }
157
158 return tmb.create(true);
159 }
160
161 private void initProgramStore() {
162 // Use stock the stock program store object
163 mProgStoreBlendNoneDepth = ProgramStore.BLEND_NONE_DEPTH_TEST(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800164 mProgStoreBlendNone = ProgramStore.BLEND_NONE_DEPTH_NONE(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800165
166 // Create a custom program store
167 ProgramStore.Builder builder = new ProgramStore.Builder(mRS);
168 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
169 builder.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
170 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800171 builder.setDitherEnabled(false);
172 builder.setDepthMaskEnabled(false);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800173 mProgStoreBlendAlpha = builder.create();
174
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800175 mProgStoreBlendAdd = BLEND_ADD_DEPTH_NONE(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800176
177 mScript.set_gProgStoreBlendNoneDepth(mProgStoreBlendNoneDepth);
178 mScript.set_gProgStoreBlendNone(mProgStoreBlendNone);
179 mScript.set_gProgStoreBlendAlpha(mProgStoreBlendAlpha);
180 mScript.set_gProgStoreBlendAdd(mProgStoreBlendAdd);
181 }
182
183 private void initProgramFragment() {
184
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800185 ProgramFragmentFixedFunction.Builder texBuilder = new ProgramFragmentFixedFunction.Builder(mRS);
186 texBuilder.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
187 ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800188 mProgFragmentTexture = texBuilder.create();
189 mProgFragmentTexture.bindSampler(mLinearClamp, 0);
190
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800191 ProgramFragmentFixedFunction.Builder colBuilder = new ProgramFragmentFixedFunction.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800192 colBuilder.setVaryingColor(false);
193 mProgFragmentColor = colBuilder.create();
194
195 mScript.set_gProgFragmentColor(mProgFragmentColor);
196 mScript.set_gProgFragmentTexture(mProgFragmentTexture);
197 }
198
199 private void initProgramVertex() {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800200 ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800201 mProgVertex = pvb.create();
202
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800203 mPVA = new ProgramVertexFixedFunction.Constants(mRS);
204 ((ProgramVertexFixedFunction)mProgVertex).bindConstants(mPVA);
205 Matrix4f proj = new Matrix4f();
206 proj.loadOrthoWindow(mWidth, mHeight);
207 mPVA.setProjection(proj);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800208
209 mScript.set_gProgVertex(mProgVertex);
210 }
211
212 private void initCustomShaders() {
213 mVSConst = new ScriptField_VertexShaderConstants_s(mRS, 1);
214 mFSConst = new ScriptField_FragentShaderConstants_s(mRS, 1);
215 mScript.bind_gVSConstants(mVSConst);
216 mScript.bind_gFSConstants(mFSConst);
217
218 mVSConstPixel = new ScriptField_VertexShaderConstants3_s(mRS, 1);
219 mFSConstPixel = new ScriptField_FragentShaderConstants3_s(mRS, 1);
220 mScript.bind_gVSConstPixel(mVSConstPixel);
221 mScript.bind_gFSConstPixel(mFSConstPixel);
222
223 // Initialize the shader builder
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800224 ProgramVertex.Builder pvbCustom = new ProgramVertex.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800225 // Specify the resource that contains the shader string
226 pvbCustom.setShader(mRes, R.raw.shaderv);
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800227 // Use a script field to specify the input layout
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800228 pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
229 // Define the constant input layout
230 pvbCustom.addConstant(mVSConst.getAllocation().getType());
231 mProgVertexCustom = pvbCustom.create();
232 // Bind the source of constant data
233 mProgVertexCustom.bindConstants(mVSConst.getAllocation(), 0);
234
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800235 ProgramFragment.Builder pfbCustom = new ProgramFragment.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800236 // Specify the resource that contains the shader string
237 pfbCustom.setShader(mRes, R.raw.shaderf);
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800238 // Tell the builder how many textures we have
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800239 pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
240 // Define the constant input layout
241 pfbCustom.addConstant(mFSConst.getAllocation().getType());
242 mProgFragmentCustom = pfbCustom.create();
243 // Bind the source of constant data
244 mProgFragmentCustom.bindConstants(mFSConst.getAllocation(), 0);
245
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800246 pvbCustom = new ProgramVertex.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800247 pvbCustom.setShader(mRes, R.raw.shader2v);
248 pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
249 pvbCustom.addConstant(mVSConstPixel.getAllocation().getType());
250 mProgVertexPixelLight = pvbCustom.create();
251 mProgVertexPixelLight.bindConstants(mVSConstPixel.getAllocation(), 0);
252
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800253 pvbCustom = new ProgramVertex.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800254 pvbCustom.setShader(mRes, R.raw.shader2movev);
255 pvbCustom.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
256 pvbCustom.addConstant(mVSConstPixel.getAllocation().getType());
257 mProgVertexPixelLightMove = pvbCustom.create();
258 mProgVertexPixelLightMove.bindConstants(mVSConstPixel.getAllocation(), 0);
259
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800260 pfbCustom = new ProgramFragment.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800261 pfbCustom.setShader(mRes, R.raw.shader2f);
262 pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
263 pfbCustom.addConstant(mFSConstPixel.getAllocation().getType());
264 mProgFragmentPixelLight = pfbCustom.create();
265 mProgFragmentPixelLight.bindConstants(mFSConstPixel.getAllocation(), 0);
266
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800267 pfbCustom = new ProgramFragment.Builder(mRS);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800268 pfbCustom.setShader(mRes, R.raw.multitexf);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800269 for (int texCount = 0; texCount < 3; texCount ++) {
270 pfbCustom.addTexture(Program.TextureType.TEXTURE_2D);
271 }
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800272 mProgFragmentMultitex = pfbCustom.create();
273
274 mScript.set_gProgVertexCustom(mProgVertexCustom);
275 mScript.set_gProgFragmentCustom(mProgFragmentCustom);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800276 mScript.set_gProgVertexPixelLight(mProgVertexPixelLight);
277 mScript.set_gProgVertexPixelLightMove(mProgVertexPixelLightMove);
278 mScript.set_gProgFragmentPixelLight(mProgFragmentPixelLight);
279 mScript.set_gProgFragmentMultitex(mProgFragmentMultitex);
280 }
281
282 private Allocation loadTextureRGB(int id) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800283 return Allocation.createFromBitmapResource(mRS, mRes, id,
284 Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
285 Allocation.USAGE_GRAPHICS_TEXTURE);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800286 }
287
288 private Allocation loadTextureARGB(int id) {
289 Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB);
Jason Sams6d8eb262010-12-15 01:41:00 -0800290 return Allocation.createFromBitmap(mRS, b,
291 Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
292 Allocation.USAGE_GRAPHICS_TEXTURE);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800293 }
294
295 private void loadImages() {
296 mTexTorus = loadTextureRGB(R.drawable.torusmap);
297 mTexOpaque = loadTextureRGB(R.drawable.data);
298 mTexTransparent = loadTextureARGB(R.drawable.leaf);
299 mTexChecker = loadTextureRGB(R.drawable.checker);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800300
301 mScript.set_gTexTorus(mTexTorus);
302 mScript.set_gTexOpaque(mTexOpaque);
303 mScript.set_gTexTransparent(mTexTransparent);
304 mScript.set_gTexChecker(mTexChecker);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800305 }
306
307 private void initFonts() {
308 // Sans font by family name
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800309 mFontSans = Font.create(mRS, mRes, "sans-serif", Font.Style.NORMAL, 8);
310 mFontSerif = Font.create(mRS, mRes, "serif", Font.Style.NORMAL, 8);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800311 // Create fonts by family and style
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800312 mFontSerifBold = Font.create(mRS, mRes, "serif", Font.Style.BOLD, 8);
313 mFontSerifItalic = Font.create(mRS, mRes, "serif", Font.Style.ITALIC, 8);
314 mFontSerifBoldItalic = Font.create(mRS, mRes, "serif", Font.Style.BOLD_ITALIC, 8);
315 mFontMono = Font.create(mRS, mRes, "mono", Font.Style.NORMAL, 8);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800316
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800317 mTextAlloc = Allocation.createFromString(mRS, "String from allocation", Allocation.USAGE_SCRIPT);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800318
319 mScript.set_gFontSans(mFontSans);
320 mScript.set_gFontSerif(mFontSerif);
321 mScript.set_gFontSerifBold(mFontSerifBold);
322 mScript.set_gFontSerifItalic(mFontSerifItalic);
323 mScript.set_gFontSerifBoldItalic(mFontSerifBoldItalic);
324 mScript.set_gFontMono(mFontMono);
325 mScript.set_gTextAlloc(mTextAlloc);
326 }
327
328 private void initMesh() {
329 m10by10Mesh = getMbyNMesh(mWidth, mHeight, 10, 10);
330 mScript.set_g10by10Mesh(m10by10Mesh);
331 m100by100Mesh = getMbyNMesh(mWidth, mHeight, 100, 100);
332 mScript.set_g100by100Mesh(m100by100Mesh);
333 mWbyHMesh= getMbyNMesh(mWidth, mHeight, mWidth/4, mHeight/4);
334 mScript.set_gWbyHMesh(mWbyHMesh);
335
336 FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.torus);
337 FileA3D.IndexEntry entry = model.getIndexEntry(0);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800338 if (entry == null || entry.getEntryType() != FileA3D.EntryType.MESH) {
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800339 Log.e("rs", "could not load model");
340 } else {
341 mTorus = (Mesh)entry.getObject();
342 mScript.set_gTorusMesh(mTorus);
343 }
344 }
345
346 private void initSamplers() {
347 Sampler.Builder bs = new Sampler.Builder(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800348 bs.setMinification(Sampler.Value.LINEAR);
349 bs.setMagnification(Sampler.Value.LINEAR);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800350 bs.setWrapS(Sampler.Value.WRAP);
351 bs.setWrapT(Sampler.Value.WRAP);
352 mLinearWrap = bs.create();
353
354 mLinearClamp = Sampler.CLAMP_LINEAR(mRS);
355 mNearestClamp = Sampler.CLAMP_NEAREST(mRS);
356 mMipLinearWrap = Sampler.WRAP_LINEAR_MIP_LINEAR(mRS);
357
358 bs = new Sampler.Builder(mRS);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800359 bs.setMinification(Sampler.Value.LINEAR_MIP_LINEAR);
360 bs.setMagnification(Sampler.Value.LINEAR);
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800361 bs.setWrapS(Sampler.Value.WRAP);
362 bs.setWrapT(Sampler.Value.WRAP);
363 bs.setAnisotropy(8.0f);
364 mMipLinearAniso8 = bs.create();
365 bs.setAnisotropy(15.0f);
366 mMipLinearAniso15 = bs.create();
367
368 mScript.set_gLinearClamp(mLinearClamp);
369 mScript.set_gLinearWrap(mLinearWrap);
370 mScript.set_gMipLinearWrap(mMipLinearWrap);
371 mScript.set_gMipLinearAniso8(mMipLinearAniso8);
372 mScript.set_gMipLinearAniso15(mMipLinearAniso15);
373 mScript.set_gNearestClamp(mNearestClamp);
374 }
375
376 private void initProgramRaster() {
377 mCullBack = ProgramRaster.CULL_BACK(mRS);
378 mCullFront = ProgramRaster.CULL_FRONT(mRS);
379 mCullNone = ProgramRaster.CULL_NONE(mRS);
380
381 mScript.set_gCullBack(mCullBack);
382 mScript.set_gCullFront(mCullFront);
383 mScript.set_gCullNone(mCullNone);
384 }
385
386 private void initRS() {
387
388 mScript = new ScriptC_rsbench(mRS, mRes, R.raw.rsbench);
389
390 mMaxModes = mScript.get_gMaxModes();
391
392 initSamplers();
393 initProgramStore();
394 initProgramFragment();
395 initProgramVertex();
396 initFonts();
397 loadImages();
398 initMesh();
399 initProgramRaster();
400 initCustomShaders();
401
402 mRS.bindRootScript(mScript);
403 }
404}
405
406
407