Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 1 | /* |
| 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 Sakhartchouk | c29a444 | 2011-02-22 10:30:32 -0800 | [diff] [blame] | 17 | package com.android.perftest; |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 18 | |
| 19 | import java.io.Writer; |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 20 | import java.io.BufferedWriter; |
| 21 | import java.io.FileWriter; |
| 22 | import java.io.IOException; |
| 23 | import java.io.File; |
| 24 | import java.io.FileOutputStream; |
| 25 | import java.io.OutputStream; |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 26 | |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 27 | import android.os.Environment; |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 28 | import android.content.res.Resources; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.BitmapFactory; |
| 31 | import android.renderscript.*; |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 32 | import android.renderscript.Element.DataKind; |
| 33 | import android.renderscript.Element.DataType; |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 34 | import android.renderscript.Allocation.MipmapControl; |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 35 | import android.renderscript.Program.TextureType; |
| 36 | import android.renderscript.ProgramStore.DepthFunc; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 37 | import android.renderscript.ProgramStore.BlendSrcFunc; |
| 38 | import android.renderscript.ProgramStore.BlendDstFunc; |
Xia Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 39 | import android.renderscript.RenderScript.RSMessageHandler; |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 40 | import android.renderscript.Sampler.Value; |
| 41 | import android.util.Log; |
| 42 | |
| 43 | |
| 44 | public class RsBenchRS { |
| 45 | |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 46 | private static final String TAG = "RsBenchRS"; |
| 47 | |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 48 | int mWidth; |
| 49 | int mHeight; |
Xia Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 50 | int mLoops; |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 51 | int mCurrentLoop; |
| 52 | |
| 53 | int mBenchmarkDimX; |
| 54 | int mBenchmarkDimY; |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 55 | |
| 56 | public RsBenchRS() { |
| 57 | } |
| 58 | |
Xia Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 59 | public void init(RenderScriptGL rs, Resources res, int width, int height, int loops) { |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 60 | 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 Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 68 | mLoops = loops; |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 69 | mCurrentLoop = 0; |
| 70 | mBenchmarkDimX = 1280; |
| 71 | mBenchmarkDimY = 720; |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 72 | initRS(); |
| 73 | } |
| 74 | |
Xia Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 75 | private boolean stopTest = false; |
| 76 | |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 77 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 96 | private ProgramVertexFixedFunction.Constants mPVA; |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 97 | |
| 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 Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 110 | 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 Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 118 | |
| 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 Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 139 | String[] mTestNames; |
| 140 | float[] mLocalTestResults; |
| 141 | |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 142 | public void onActionDown(int x, int y) { |
| 143 | mMode ++; |
| 144 | mMode = mMode % mMaxModes; |
| 145 | mScript.set_gDisplayMode(mMode); |
| 146 | } |
| 147 | |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 148 | 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 Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Create a message handler to handle message sent from the script |
| 177 | */ |
| 178 | protected RSMessageHandler mRsMessage = new RSMessageHandler() { |
| 179 | public void run() { |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 180 | 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 Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 192 | synchronized(this) { |
| 193 | stopTest = true; |
| 194 | this.notifyAll(); |
| 195 | } |
| 196 | return; |
| 197 | } else { |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 198 | Log.v(TAG, "Perf test got unexpected message"); |
Xia Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 199 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 223 | 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 Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 232 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 265 | mProgStoreBlendNone = ProgramStore.BLEND_NONE_DEPTH_NONE(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 266 | |
| 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 272 | builder.setDitherEnabled(false); |
| 273 | builder.setDepthMaskEnabled(false); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 274 | mProgStoreBlendAlpha = builder.create(); |
| 275 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 276 | mProgStoreBlendAdd = BLEND_ADD_DEPTH_NONE(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 277 | |
| 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 286 | ProgramFragmentFixedFunction.Builder texBuilder = new ProgramFragmentFixedFunction.Builder(mRS); |
| 287 | texBuilder.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE, |
| 288 | ProgramFragmentFixedFunction.Builder.Format.RGBA, 0); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 289 | mProgFragmentTexture = texBuilder.create(); |
| 290 | mProgFragmentTexture.bindSampler(mLinearClamp, 0); |
| 291 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 292 | ProgramFragmentFixedFunction.Builder colBuilder = new ProgramFragmentFixedFunction.Builder(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 293 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 301 | ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 302 | mProgVertex = pvb.create(); |
| 303 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 304 | mPVA = new ProgramVertexFixedFunction.Constants(mRS); |
| 305 | ((ProgramVertexFixedFunction)mProgVertex).bindConstants(mPVA); |
| 306 | Matrix4f proj = new Matrix4f(); |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 307 | proj.loadOrthoWindow(mBenchmarkDimX, mBenchmarkDimY); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 308 | mPVA.setProjection(proj); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 309 | |
| 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 325 | ProgramVertex.Builder pvbCustom = new ProgramVertex.Builder(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 326 | // Specify the resource that contains the shader string |
| 327 | pvbCustom.setShader(mRes, R.raw.shaderv); |
Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 328 | // Use a script field to specify the input layout |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 329 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 336 | ProgramFragment.Builder pfbCustom = new ProgramFragment.Builder(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 337 | // Specify the resource that contains the shader string |
| 338 | pfbCustom.setShader(mRes, R.raw.shaderf); |
Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 339 | // Tell the builder how many textures we have |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 340 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 347 | pvbCustom = new ProgramVertex.Builder(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 348 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 354 | pvbCustom = new ProgramVertex.Builder(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 355 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 361 | pfbCustom = new ProgramFragment.Builder(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 362 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 368 | pfbCustom = new ProgramFragment.Builder(mRS); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 369 | pfbCustom.setShader(mRes, R.raw.multitexf); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 370 | for (int texCount = 0; texCount < 3; texCount ++) { |
| 371 | pfbCustom.addTexture(Program.TextureType.TEXTURE_2D); |
| 372 | } |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 373 | mProgFragmentMultitex = pfbCustom.create(); |
| 374 | |
| 375 | mScript.set_gProgVertexCustom(mProgVertexCustom); |
| 376 | mScript.set_gProgFragmentCustom(mProgFragmentCustom); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 377 | 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 Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 384 | return Allocation.createFromBitmapResource(mRS, mRes, id, |
| 385 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, |
| 386 | Allocation.USAGE_GRAPHICS_TEXTURE); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | private Allocation loadTextureARGB(int id) { |
| 390 | Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB); |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 391 | return Allocation.createFromBitmap(mRS, b, |
| 392 | Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE, |
| 393 | Allocation.USAGE_GRAPHICS_TEXTURE); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 394 | } |
| 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 Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 401 | |
| 402 | mScript.set_gTexTorus(mTexTorus); |
| 403 | mScript.set_gTexOpaque(mTexOpaque); |
| 404 | mScript.set_gTexTransparent(mTexTransparent); |
| 405 | mScript.set_gTexChecker(mTexChecker); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | private void initFonts() { |
| 409 | // Sans font by family name |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 410 | mFontSans = Font.create(mRS, mRes, "sans-serif", Font.Style.NORMAL, 8); |
| 411 | mFontSerif = Font.create(mRS, mRes, "serif", Font.Style.NORMAL, 8); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 412 | // Create fonts by family and style |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 413 | 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 Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 417 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 418 | mTextAlloc = Allocation.createFromString(mRS, "String from allocation", Allocation.USAGE_SCRIPT); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 419 | |
| 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 Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 430 | m10by10Mesh = getMbyNMesh(mBenchmarkDimX, mBenchmarkDimY, 10, 10); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 431 | mScript.set_g10by10Mesh(m10by10Mesh); |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 432 | m100by100Mesh = getMbyNMesh(mBenchmarkDimX, mBenchmarkDimY, 100, 100); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 433 | mScript.set_g100by100Mesh(m100by100Mesh); |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 434 | mWbyHMesh= getMbyNMesh(mBenchmarkDimX, mBenchmarkDimY, mBenchmarkDimX/4, mBenchmarkDimY/4); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 435 | mScript.set_gWbyHMesh(mWbyHMesh); |
| 436 | |
| 437 | FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.torus); |
| 438 | FileA3D.IndexEntry entry = model.getIndexEntry(0); |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 439 | if (entry == null || entry.getEntryType() != FileA3D.EntryType.MESH) { |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 440 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 449 | bs.setMinification(Sampler.Value.LINEAR); |
| 450 | bs.setMagnification(Sampler.Value.LINEAR); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 451 | 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 Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 460 | bs.setMinification(Sampler.Value.LINEAR_MIP_LINEAR); |
| 461 | bs.setMagnification(Sampler.Value.LINEAR); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 462 | 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 Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 487 | 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 Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 510 | private void initRS() { |
| 511 | |
| 512 | mScript = new ScriptC_rsbench(mRS, mRes, R.raw.rsbench); |
Xia Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 513 | mRS.setMessageHandler(mRsMessage); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 514 | |
| 515 | mMaxModes = mScript.get_gMaxModes(); |
Xia Wang | 2738fb1 | 2011-04-15 17:24:08 -0700 | [diff] [blame] | 516 | mScript.set_gMaxLoops(mLoops); |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 517 | |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 518 | prepareTestData(); |
| 519 | |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 520 | initSamplers(); |
| 521 | initProgramStore(); |
| 522 | initProgramFragment(); |
| 523 | initProgramVertex(); |
| 524 | initFonts(); |
| 525 | loadImages(); |
| 526 | initMesh(); |
| 527 | initProgramRaster(); |
| 528 | initCustomShaders(); |
| 529 | |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 530 | Type.Builder b = new Type.Builder(mRS, Element.RGBA_8888(mRS)); |
Alex Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 531 | b.setX(mBenchmarkDimX).setY(mBenchmarkDimY); |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 532 | 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 Sakhartchouk | 4d7b19a | 2011-05-04 14:38:53 -0700 | [diff] [blame] | 541 | b.setX(mBenchmarkDimX).setY(mBenchmarkDimY); |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 542 | offscreen = Allocation.createTyped(mRS, |
| 543 | b.create(), |
| 544 | Allocation.USAGE_GRAPHICS_RENDER_TARGET); |
| 545 | mScript.set_gRenderBufferDepth(offscreen); |
| 546 | |
Alex Sakhartchouk | 6b5222d | 2010-12-13 14:48:21 -0800 | [diff] [blame] | 547 | mRS.bindRootScript(mScript); |
| 548 | } |
| 549 | } |