summaryrefslogtreecommitdiffstats
path: root/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java
blob: 7b710eed2b451fe4bdf231525efde1754c0bab72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.scenegraph;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.renderscript.*;
import android.renderscript.Allocation.MipmapControl;
import android.renderscript.Element.Builder;
import android.renderscript.Font.Style;
import android.renderscript.Program.TextureType;
import android.renderscript.ProgramStore.DepthFunc;
import android.util.Log;

import com.android.scenegraph.SceneManager.SceneLoadedCallback;

// This is where the scenegraph and the rendered objects are initialized and used
public class TestAppRS {

    private static String modelName = "orientation_test";
    private static String TAG = "TestAppRS";
    private final int STATE_LAST_FOCUS = 1;
    private final boolean mLoadFromSD = true;
    private static String mSDCardPath = "sdcard/scenegraph/";

    int mWidth;
    int mHeight;
    int mRotation;

    boolean mUseBlur;

    // Used to asynchronously load scene elements like meshes and transform hierarchies
    SceneLoadedCallback mLoadedCallback = new SceneLoadedCallback() {
        public void run() {
            prepareToRender(mLoadedScene);
        }
    };

    // Top level class that initializes all the elements needed to use the scene graph
    SceneManager mSceneManager;

    // Used to move the camera around in the 3D world
    TouchHandler mTouchHandler;

    public TestAppRS() {
        mUseBlur = false;
    }

    // This is a part of the test app, it's used to tests multiple render passes and is toggled
    // on and off in the menu, off by default
    void toggleBlur() {
        mUseBlur = !mUseBlur;

        mActiveScene.clearRenderPasses();
        initRenderPasses();
        mActiveScene.initRenderPassRS(mRS, mSceneManager);

        // This is just a hardcoded object in the scene that gets turned on and off for the demo
        // to make things look a bit better. This could be deleted in the cleanup
        Drawable plane = (Drawable)mActiveScene.getDrawableByName("pPlaneShape1");
        if (plane != null) {
            plane.setVisible(mRS, !mUseBlur);
        }
    }

    public void init(RenderScriptGL rs, Resources res, int width, int height) {
        mRS = rs;
        mRes = res;
        mWidth = width;
        mHeight = height;
        mRotation = 0;

        mTouchHandler = new TouchHandler();

        mSceneManager = new SceneManager();
        // Initializes all the RS specific scenegraph elements 
        mSceneManager.initRS(mRS, mRes, mWidth, mHeight);

        // Shows the loading screen with some text
        renderLoading();
        // Adds a little 3D bugdroid model to the laoding screen asynchronously.
        new LoadingScreenLoaderTask().execute();

        // Initi renderscript stuff specific to the app. This will need to be abstracted out later.
        initRS();

        // Load a scene to render
        mSceneManager.loadModel(modelName, mLoadedCallback);
    }

    // When a new model file is selected from the UI, this function gets called to init everything
    void loadModel(String path) {
        String shortName = path.substring(path.lastIndexOf('/') + 1);
        shortName = shortName.substring(0, shortName.lastIndexOf('.'));
        mScript.set_gInitialized(false);
        mActiveScene.destroyRS(mSceneManager);
        mSceneManager.loadModel(shortName, mLoadedCallback);
    }

    private Resources mRes;
    private RenderScriptGL mRS;
    private Sampler mSampler;
    private ProgramStore mPSBackground;
    private ProgramFragment mPFBackground;
    private ProgramVertex mPVBackground;
    private ProgramVertexFixedFunction.Constants mPVA;

    private ProgramFragment mPF_Paint;
    private ProgramFragment mPF_Aluminum;
    private ProgramFragment mPF_Plastic;
    private ProgramFragment mPF_Diffuse;
    private ProgramFragment mPF_Texture;
    ScriptField_FShaderParams_s mFsConst;
    private ProgramVertex mPV_Paint;
    ScriptField_VShaderParams_s mVsConst;
    
    private Allocation mDefaultCube;
    private Allocation mAllocPV;
    private Allocation mEnvCube;
    private Allocation mDiffCube;

    Scene mActiveScene;

    private ScriptC_scenegraph mScript;

    // The loading screen has some elements that shouldn't be loaded on the UI thread
    private class LoadingScreenLoaderTask extends AsyncTask<String, Void, Boolean> {
        Allocation robotTex;
        Mesh robotMesh;
        protected Boolean doInBackground(String... names) {
            long start = System.currentTimeMillis();
            robotTex = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot,
                                                           MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
                                                           Allocation.USAGE_GRAPHICS_TEXTURE);

            FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.robot);
            FileA3D.IndexEntry entry = model.getIndexEntry(0);
            if (entry != null && entry.getEntryType() == FileA3D.EntryType.MESH) {
                robotMesh = entry.getMesh();
            }

            initPFS();
            initPF();
            initPV();

            long end = System.currentTimeMillis();
            Log.v("TIMER", "Loading load time: " + (end - start));
            return new Boolean(true);
        }

        protected void onPostExecute(Boolean result) {
            mScript.set_gRobotTex(robotTex);
            mScript.set_gRobotMesh(robotMesh);
        }
    }

    // We use this to laod environment maps off the UI thread
    private class ImageLoaderTask extends AsyncTask<String, Void, Boolean> {
        Allocation tempEnv;
        Allocation tempDiff;

        InputStream openStream(String name) {
            InputStream is = null;
            try {
                if (!mLoadFromSD) {
                    is = mRes.getAssets().open(name);
                } else {
                    File f = new File(mSDCardPath + name);
                    is = new BufferedInputStream(new FileInputStream(f));
                }
            } catch (IOException e) {
                Log.e("PAINTSHADERS", " Message: " + e.getMessage());
            }
            return is;
        }

        protected Boolean doInBackground(String... names) {
            long start = System.currentTimeMillis();
            InputStream is = openStream("cube_env.png");
            if (is == null) {
                return new Boolean(false);
            }

            Bitmap b = BitmapFactory.decodeStream(is);
            tempEnv = Allocation.createCubemapFromBitmap(mRS,
                                                         b,
                                                         MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
                                                         Allocation.USAGE_GRAPHICS_TEXTURE);

            is = openStream("cube_spec.png");
            if (is == null) {
                return new Boolean(false);
            }

            b = BitmapFactory.decodeStream(is);
            tempDiff = Allocation.createCubemapFromBitmap(mRS,
                                                          b,
                                                          MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
                                                          Allocation.USAGE_GRAPHICS_TEXTURE);
            long end = System.currentTimeMillis();
            Log.v("TIMER", "Image load time: " + (end - start));
            return new Boolean(true);
        }

        protected void onPostExecute(Boolean result) {
            mEnvCube = tempEnv;
            mDiffCube = tempDiff;

            mPF_Paint.bindTexture(mEnvCube, 1);
            mPF_Aluminum.bindTexture(mDiffCube, 1);
        }
    }

    public void onActionDown(float x, float y) {
        mTouchHandler.onActionDown(x, y);

        //mSceneManager.getRenderLoop().invoke_pick((int)x, (int)y);
    }

    public void onActionScale(float scale) {
        mTouchHandler.onActionScale(scale);
    }

    public void onActionMove(float x, float y) {
        mTouchHandler.onActionMove(x, y);
    }

    // All the custom shaders used to render the scene are initialized here
    // This includes stuff like plastic, car paint, etc.
    private void initPaintShaders() {
        ProgramVertex.Builder vb = new ProgramVertex.Builder(mRS);
        mVsConst = new ScriptField_VShaderParams_s(mRS, 1);
        vb.addConstant(mVsConst.getAllocation().getType());
        vb.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
        vb.setShader(mRes, R.raw.shader2v);
        mPV_Paint = vb.create();
        mPV_Paint.bindConstants(mVsConst.getAllocation(), 0);

        ProgramFragment.Builder fb = new ProgramFragment.Builder(mRS);
        mFsConst = new ScriptField_FShaderParams_s(mRS, 1);
        fb.addConstant(mFsConst.getAllocation().getType());
        fb.setShader(mRes, R.raw.paintf);
        fb.addTexture(TextureType.TEXTURE_2D);
        fb.addTexture(TextureType.TEXTURE_CUBE);
        mPF_Paint = fb.create();

        mPF_Paint.bindConstants(mFsConst.getAllocation(), 0);
        mPF_Paint.bindSampler(Sampler.WRAP_LINEAR_MIP_LINEAR(mRS), 0);
        mPF_Paint.bindSampler(Sampler.CLAMP_LINEAR_MIP_LINEAR(mRS), 1);

        fb = new ProgramFragment.Builder(mRS);
        fb.addConstant(mFsConst.getAllocation().getType());
        fb.setShader(mRes, R.raw.metal);
        fb.addTexture(TextureType.TEXTURE_2D);
        fb.addTexture(TextureType.TEXTURE_CUBE);
        mPF_Aluminum = fb.create();

        mPF_Aluminum.bindConstants(mFsConst.getAllocation(), 0);
        mPF_Aluminum.bindSampler(Sampler.WRAP_LINEAR_MIP_LINEAR(mRS), 0);
        mPF_Aluminum.bindSampler(Sampler.CLAMP_LINEAR_MIP_LINEAR(mRS), 1);

        fb = new ProgramFragment.Builder(mRS);
        fb.addConstant(mFsConst.getAllocation().getType());
        fb.setShader(mRes, R.raw.plastic);
        fb.addTexture(TextureType.TEXTURE_2D);
        mPF_Plastic = fb.create();
        mPF_Plastic.bindConstants(mFsConst.getAllocation(), 0);
        mPF_Plastic.bindSampler(Sampler.WRAP_LINEAR_MIP_LINEAR(mRS), 0);

        fb = new ProgramFragment.Builder(mRS);
        fb.addConstant(mFsConst.getAllocation().getType());
        fb.setShader(mRes, R.raw.diffuse);
        fb.addTexture(TextureType.TEXTURE_2D);
        mPF_Diffuse = fb.create();
        mPF_Diffuse.bindConstants(mFsConst.getAllocation(), 0);
        mPF_Diffuse.bindSampler(Sampler.WRAP_LINEAR_MIP_LINEAR(mRS), 0);

        fb = new ProgramFragment.Builder(mRS);
        fb.addConstant(mFsConst.getAllocation().getType());
        fb.setShader(mRes, R.raw.texture);
        fb.addTexture(TextureType.TEXTURE_2D);
        mPF_Texture = fb.create();
        mPF_Texture.bindConstants(mFsConst.getAllocation(), 0);
        mPF_Texture.bindSampler(Sampler.WRAP_LINEAR_MIP_LINEAR(mRS), 0);

        FullscreenBlur.initShaders(mRes, mRS, mVsConst, mFsConst);
    }

    // This needs to be cleaned up a bit, it's one of the default render state objects
    private void initPFS() {
        ProgramStore.Builder b = new ProgramStore.Builder(mRS);

        b.setDepthFunc(ProgramStore.DepthFunc.LESS);
        b.setDitherEnabled(false);
        b.setDepthMaskEnabled(true);
        mPSBackground = b.create();

        mScript.set_gPFSBackground(mPSBackground);
    }

    // This needs to be cleaned up a bit, it's one of the default render state objects
    private void initPF() {
        Sampler.Builder bs = new Sampler.Builder(mRS);
        bs.setMinification(Sampler.Value.LINEAR);
        bs.setMagnification(Sampler.Value.LINEAR);
        bs.setWrapS(Sampler.Value.CLAMP);
        bs.setWrapT(Sampler.Value.CLAMP);
        mSampler = bs.create();

        ProgramFragmentFixedFunction.Builder b = new ProgramFragmentFixedFunction.Builder(mRS);
        b.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
                     ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
        mPFBackground = b.create();
        mPFBackground.bindSampler(Sampler.CLAMP_LINEAR(mRS), 0);

        mScript.set_gPFBackground(mPFBackground);
    }

    private void initPV() {
        ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
        mPVBackground = pvb.create();

        mPVA = new ProgramVertexFixedFunction.Constants(mRS);
        ((ProgramVertexFixedFunction)mPVBackground).bindConstants(mPVA);

        mScript.set_gPVBackground(mPVBackground);
    }

    // Creates a simple script to show a loding screen until everything is initialized
    // Could also be used to do some custom renderscript work before handing things over
    // to the scenegraph
    void renderLoading() {
        mScript = new ScriptC_scenegraph(mRS, mRes, R.raw.scenegraph);
        mRS.bindRootScript(mScript);
    }

    void initRenderPasses() {
        ArrayList<DrawableBase> allDraw = mActiveScene.getDrawables();
        int numDraw = allDraw.size();

        if (mUseBlur) {
            FullscreenBlur.addBlurPasses(mActiveScene, mRS, mSceneManager);
        }

        RenderPass mainPass = new RenderPass();
        mainPass.setClearColor(new Float4(1.0f, 1.0f, 1.0f, 1.0f));
        mainPass.setShouldClearColor(true);
        mainPass.setClearDepth(1.0f);
        mainPass.setShouldClearDepth(true);
        mainPass.setCamera(mActiveScene.getCameras().get(1));
        for (int i = 0; i < numDraw; i ++) {
            mainPass.appendDrawable((Drawable)allDraw.get(i));
        }
        mActiveScene.appendRenderPass(mainPass);

        if (mUseBlur) {
            FullscreenBlur.addCompositePass(mActiveScene, mRS, mSceneManager);
        }
    }

    public void prepareToRender(Scene s) {
        mActiveScene = s;
        RenderState plastic = new RenderState(mPV_Paint, mPF_Plastic, null, null);
        RenderState diffuse = new RenderState(mPV_Paint, mPF_Diffuse, null, null);
        RenderState paint = new RenderState(mPV_Paint, mPF_Paint, null, null);
        RenderState aluminum = new RenderState(mPV_Paint, mPF_Aluminum, null, null);
        RenderState glassTransp = new RenderState(mPV_Paint,
                                                  mPF_Paint,
                                                  ProgramStore.BLEND_ALPHA_DEPTH_TEST(mRS),
                                                  null);

        initRenderPasses();

        mActiveScene.assignRenderState(plastic);

        mActiveScene.assignRenderStateToMaterial(diffuse, "lambert2$");

        mActiveScene.assignRenderStateToMaterial(paint, "^#Paint");
        mActiveScene.assignRenderStateToMaterial(paint, "^#Carbon");
        mActiveScene.assignRenderStateToMaterial(paint, "^#Glass");
        mActiveScene.assignRenderStateToMaterial(paint, "^#MainGlass");

        mActiveScene.assignRenderStateToMaterial(aluminum, "^#Metal");
        mActiveScene.assignRenderStateToMaterial(aluminum, "^#Brake");

        mActiveScene.assignRenderStateToMaterial(glassTransp, "^#GlassLight");

        Drawable plane = (Drawable)mActiveScene.getDrawableByName("pPlaneShape1");
        if (plane != null) {
            RenderState texState = new RenderState(mPV_Paint, mPF_Texture, null, null);
            plane.setRenderState(texState);
            plane.setVisible(mRS, !mUseBlur);
        }

        mTouchHandler.init(mActiveScene);

        long start = System.currentTimeMillis();
        mActiveScene.initRS(mRS, mRes, mSceneManager);
        long end = System.currentTimeMillis();
        Log.v("TIMER", "Scene init time: " + (end - start));

        mScript.set_gInitialized(true);
    }

    private void initRS() {

        FullscreenBlur.createRenderTargets(mRS, mWidth, mHeight);
        initPaintShaders();
        new ImageLoaderTask().execute();

        Bitmap b = BitmapFactory.decodeResource(mRes, R.drawable.defaultcube);
        mDefaultCube = Allocation.createCubemapFromBitmap(mRS, b);
        mPF_Paint.bindTexture(mDefaultCube, 1);
        mPF_Aluminum.bindTexture(mDefaultCube, 1);

        ScriptC_render renderLoop = mSceneManager.getRenderLoop();
        renderLoop.bind_vConst(mVsConst);
        renderLoop.bind_fConst(mFsConst);

        mScript.set_gRenderLoop(renderLoop);
        Allocation dummyAlloc = Allocation.createSized(mRS, Element.I32(mRS), 1);
        mScript.set_gDummyAlloc(dummyAlloc);
    }
}