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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
/*
* 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.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.lang.Math;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.android.scenegraph.Camera;
import com.android.scenegraph.FragmentShader;
import com.android.scenegraph.MatrixTransform;
import com.android.scenegraph.Scene;
import com.android.scenegraph.VertexShader;
import com.android.testapp.R;
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.Mesh;
import android.renderscript.RenderScriptGL;
import android.util.Log;
import android.view.SurfaceHolder;
/**
* @hide
*/
public class SceneManager extends SceneGraphBase {
HashMap<String, Allocation> mAllocationMap;
ScriptC_render mRenderLoop;
ScriptC mCameraScript;
ScriptC mLightScript;
ScriptC mObjectParamsScript;
ScriptC mFragmentParamsScript;
ScriptC mVertexParamsScript;
ScriptC mCullScript;
ScriptC_transform mTransformScript;
ScriptC_export mExportScript;
RenderScriptGL mRS;
Resources mRes;
Mesh mQuad;
int mWidth;
int mHeight;
Scene mActiveScene;
private static SceneManager sSceneManager;
private Allocation mDefault2D;
private Allocation mDefaultCube;
private FragmentShader mColor;
private FragmentShader mTexture;
private VertexShader mDefaultVertex;
private RenderState mDefaultState;
private Transform mDefaultTransform;
private static Allocation getDefault(boolean isCube) {
final int dimension = 4;
final int bytesPerPixel = 4;
int arraySize = dimension * dimension * bytesPerPixel;
RenderScriptGL rs = sSceneManager.mRS;
Type.Builder b = new Type.Builder(rs, Element.RGBA_8888(rs));
b.setX(dimension).setY(dimension);
if (isCube) {
b.setFaces(true);
arraySize *= 6;
}
Type bitmapType = b.create();
Allocation.MipmapControl mip = Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
int usage = Allocation.USAGE_GRAPHICS_TEXTURE;
Allocation defaultImage = Allocation.createTyped(rs, bitmapType, mip, usage);
byte imageData[] = new byte[arraySize];
defaultImage.copyFrom(imageData);
return defaultImage;
}
static Allocation getDefaultTex2D() {
if (sSceneManager == null) {
return null;
}
if (sSceneManager.mDefault2D == null) {
sSceneManager.mDefault2D = getDefault(false);
}
return sSceneManager.mDefault2D;
}
static Allocation getDefaultTexCube() {
if (sSceneManager == null) {
return null;
}
if (sSceneManager.mDefaultCube == null) {
sSceneManager.mDefaultCube = getDefault(true);
}
return sSceneManager.mDefaultCube;
}
public static boolean isSDCardPath(String path) {
int sdCardIndex = path.indexOf("sdcard/");
// We are looking for /sdcard/ or sdcard/
if (sdCardIndex == 0 || sdCardIndex == 1) {
return true;
}
sdCardIndex = path.indexOf("mnt/sdcard/");
if (sdCardIndex == 0 || sdCardIndex == 1) {
return true;
}
return false;
}
static Bitmap loadBitmap(String name, Resources res) {
InputStream is = null;
boolean loadFromSD = isSDCardPath(name);
try {
if (!loadFromSD) {
is = res.getAssets().open(name);
} else {
File f = new File(name);
is = new BufferedInputStream(new FileInputStream(f));
}
} catch (IOException e) {
Log.e("ImageLoaderTask", " Message: " + e.getMessage());
return null;
}
Bitmap b = BitmapFactory.decodeStream(is);
try {
is.close();
} catch (IOException e) {
Log.e("ImageLoaderTask", " Message: " + e.getMessage());
}
return b;
}
static Allocation createFromBitmap(Bitmap b, RenderScriptGL rs, boolean isCube) {
if (b == null) {
return null;
}
MipmapControl mip = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
int usage = Allocation.USAGE_GRAPHICS_TEXTURE;
if (isCube) {
return Allocation.createCubemapFromBitmap(rs, b, mip, usage);
}
return Allocation.createFromBitmap(rs, b, mip, usage);
}
public static Allocation loadCubemap(String name, RenderScriptGL rs, Resources res) {
return createFromBitmap(loadBitmap(name, res), rs, true);
}
public static Allocation loadCubemap(int id, RenderScriptGL rs, Resources res) {
return createFromBitmap(BitmapFactory.decodeResource(res, id), rs, true);
}
public static Allocation loadTexture2D(String name, RenderScriptGL rs, Resources res) {
return createFromBitmap(loadBitmap(name, res), rs, false);
}
public static Allocation loadTexture2D(int id, RenderScriptGL rs, Resources res) {
return createFromBitmap(BitmapFactory.decodeResource(res, id), rs, false);
}
public static ProgramStore BLEND_ADD_DEPTH_NONE(RenderScript rs) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
builder.setBlendFunc(ProgramStore.BlendSrcFunc.ONE, ProgramStore.BlendDstFunc.ONE);
builder.setDitherEnabled(false);
builder.setDepthMaskEnabled(false);
return builder.create();
}
static Allocation getStringAsAllocation(RenderScript rs, String str) {
if (str == null) {
return null;
}
if (str.length() == 0) {
return null;
}
byte[] allocArray = null;
byte[] nullChar = new byte[1];
nullChar[0] = 0;
try {
allocArray = str.getBytes("UTF-8");
Allocation alloc = Allocation.createSized(rs, Element.U8(rs),
allocArray.length + 1,
Allocation.USAGE_SCRIPT);
alloc.copy1DRangeFrom(0, allocArray.length, allocArray);
alloc.copy1DRangeFrom(allocArray.length, 1, nullChar);
return alloc;
}
catch (Exception e) {
throw new RSRuntimeException("Could not convert string to utf-8.");
}
}
static Allocation getCachedAlloc(String str) {
if (sSceneManager == null) {
throw new RuntimeException("Scene manager not initialized");
}
return sSceneManager.mAllocationMap.get(str);
}
static void cacheAlloc(String str, Allocation alloc) {
if (sSceneManager == null) {
throw new RuntimeException("Scene manager not initialized");
}
sSceneManager.mAllocationMap.put(str, alloc);
}
public static class SceneLoadedCallback implements Runnable {
public Scene mLoadedScene;
public String mName;
public void run() {
}
}
public Scene getActiveScene() {
return mActiveScene;
}
public void setActiveScene(Scene s) {
mActiveScene = s;
if (mActiveScene == null) {
return;
}
// Do some sanity checking
if (mActiveScene.getCameras().size() == 0) {
Matrix4f camPos = new Matrix4f();
camPos.translate(0, 0, 10);
MatrixTransform cameraTransform = new MatrixTransform();
cameraTransform.setName("_DefaultCameraTransform");
cameraTransform.setMatrix(camPos);
mActiveScene.appendTransform(cameraTransform);
Camera cam = new Camera();
cam.setName("_DefaultCamera");
cam.setTransform(cameraTransform);
mActiveScene.appendCamera(cam);
}
mActiveScene.appendShader(getDefaultVS());
mActiveScene.appendTransform(getDefaultTransform());
}
static RenderScriptGL getRS() {
if (sSceneManager == null) {
return null;
}
return sSceneManager.mRS;
}
static Resources getRes() {
if (sSceneManager == null) {
return null;
}
return sSceneManager.mRes;
}
// Provides the folowing inputs to fragment shader
// Assigned by default if nothing is present
// vec3 varWorldPos;
// vec3 varWorldNormal;
// vec2 varTex0;
public static VertexShader getDefaultVS() {
if (sSceneManager == null) {
return null;
}
if (sSceneManager.mDefaultVertex == null) {
RenderScriptGL rs = getRS();
Element.Builder b = new Element.Builder(rs);
b.add(Element.MATRIX_4X4(rs), "model");
Type.Builder objConstBuilder = new Type.Builder(rs, b.create());
b = new Element.Builder(rs);
b.add(Element.MATRIX_4X4(rs), "viewProj");
Type.Builder shaderConstBuilder = new Type.Builder(rs, b.create());
b = new Element.Builder(rs);
b.add(Element.F32_4(rs), "position");
b.add(Element.F32_2(rs), "texture0");
b.add(Element.F32_3(rs), "normal");
Element defaultIn = b.create();
final String code = "\n" +
"varying vec3 varWorldPos;\n" +
"varying vec3 varWorldNormal;\n" +
"varying vec2 varTex0;\n" +
"void main() {" +
" vec4 objPos = ATTRIB_position;\n" +
" vec4 worldPos = UNI_model * objPos;\n" +
" gl_Position = UNI_viewProj * worldPos;\n" +
" mat3 model3 = mat3(UNI_model[0].xyz, UNI_model[1].xyz, UNI_model[2].xyz);\n" +
" vec3 worldNorm = model3 * ATTRIB_normal;\n" +
" varWorldPos = worldPos.xyz;\n" +
" varWorldNormal = worldNorm;\n" +
" varTex0 = ATTRIB_texture0;\n" +
"}\n";
VertexShader.Builder sb = new VertexShader.Builder(rs);
sb.addInput(defaultIn);
sb.setObjectConst(objConstBuilder.setX(1).create());
sb.setShaderConst(shaderConstBuilder.setX(1).create());
sb.setShader(code);
sSceneManager.mDefaultVertex = sb.create();
}
return sSceneManager.mDefaultVertex;
}
public static FragmentShader getColorFS() {
if (sSceneManager == null) {
return null;
}
if (sSceneManager.mColor == null) {
RenderScriptGL rs = getRS();
Element.Builder b = new Element.Builder(rs);
b.add(Element.F32_4(rs), "color");
Type.Builder objConstBuilder = new Type.Builder(rs, b.create());
final String code = "\n" +
"varying vec2 varTex0;\n" +
"void main() {\n" +
" lowp vec4 col = UNI_color;\n" +
" gl_FragColor = col;\n" +
"}\n";
FragmentShader.Builder fb = new FragmentShader.Builder(rs);
fb.setShader(code);
fb.setObjectConst(objConstBuilder.create());
sSceneManager.mColor = fb.create();
}
return sSceneManager.mColor;
}
public static FragmentShader getTextureFS() {
if (sSceneManager == null) {
return null;
}
if (sSceneManager.mTexture == null) {
RenderScriptGL rs = getRS();
final String code = "\n" +
"varying vec2 varTex0;\n" +
"void main() {\n" +
" lowp vec4 col = texture2D(UNI_color, varTex0).rgba;\n" +
" gl_FragColor = col;\n" +
"}\n";
FragmentShader.Builder fb = new FragmentShader.Builder(rs);
fb.setShader(code);
fb.addTexture(Program.TextureType.TEXTURE_2D, "color");
sSceneManager.mTexture = fb.create();
sSceneManager.mTexture.mProgram.bindSampler(Sampler.CLAMP_LINEAR_MIP_LINEAR(rs), 0);
}
return sSceneManager.mTexture;
}
static RenderState getDefaultState() {
if (sSceneManager == null) {
return null;
}
if (sSceneManager.mDefaultState == null) {
sSceneManager.mDefaultState = new RenderState(getDefaultVS(), getColorFS(), null, null);
sSceneManager.mDefaultState.setName("__DefaultState");
}
return sSceneManager.mDefaultState;
}
static Transform getDefaultTransform() {
if (sSceneManager == null) {
return null;
}
if (sSceneManager.mDefaultTransform == null) {
sSceneManager.mDefaultTransform = new MatrixTransform();
sSceneManager.mDefaultTransform.setName("__DefaultTransform");
}
return sSceneManager.mDefaultTransform;
}
public static SceneManager getInstance() {
if (sSceneManager == null) {
sSceneManager = new SceneManager();
}
return sSceneManager;
}
protected SceneManager() {
}
public void loadModel(String name, SceneLoadedCallback cb) {
ColladaScene scene = new ColladaScene(name, cb);
scene.init(mRS, mRes);
}
public Mesh getScreenAlignedQuad() {
if (mQuad != null) {
return mQuad;
}
Mesh.TriangleMeshBuilder tmb = new Mesh.TriangleMeshBuilder(mRS,
3, Mesh.TriangleMeshBuilder.TEXTURE_0);
tmb.setTexture(0.0f, 1.0f).addVertex(-1.0f, 1.0f, 1.0f);
tmb.setTexture(0.0f, 0.0f).addVertex(-1.0f, -1.0f, 1.0f);
tmb.setTexture(1.0f, 0.0f).addVertex(1.0f, -1.0f, 1.0f);
tmb.setTexture(1.0f, 1.0f).addVertex(1.0f, 1.0f, 1.0f);
tmb.addTriangle(0, 1, 2);
tmb.addTriangle(2, 3, 0);
mQuad = tmb.create(true);
return mQuad;
}
public Renderable getRenderableQuad(String name, RenderState state) {
Renderable quad = new Renderable();
quad.setTransform(new MatrixTransform());
quad.setMesh(getScreenAlignedQuad());
quad.setName(name);
quad.setRenderState(state);
quad.setCullType(1);
return quad;
}
public void initRS(RenderScriptGL rs, Resources res, int w, int h) {
mRS = rs;
mRes = res;
mAllocationMap = new HashMap<String, Allocation>();
mQuad = null;
mDefault2D = null;
mDefaultCube = null;
mDefaultVertex = null;
mColor = null;
mTexture = null;
mDefaultState = null;
mDefaultTransform = null;
mExportScript = new ScriptC_export(rs, res, R.raw.export);
mTransformScript = new ScriptC_transform(rs, res, R.raw.transform);
mTransformScript.set_gTransformScript(mTransformScript);
mCameraScript = new ScriptC_camera(rs, res, R.raw.camera);
mLightScript = new ScriptC_light(rs, res, R.raw.light);
mObjectParamsScript = new ScriptC_object_params(rs, res, R.raw.object_params);
mFragmentParamsScript = new ScriptC_object_params(rs, res, R.raw.fragment_params);
mVertexParamsScript = new ScriptC_object_params(rs, res, R.raw.vertex_params);
mCullScript = new ScriptC_cull(rs, res, R.raw.cull);
mRenderLoop = new ScriptC_render(rs, res, R.raw.render);
mRenderLoop.set_gTransformScript(mTransformScript);
mRenderLoop.set_gCameraScript(mCameraScript);
mRenderLoop.set_gLightScript(mLightScript);
mRenderLoop.set_gObjectParamsScript(mObjectParamsScript);
mRenderLoop.set_gFragmentParamsScript(mFragmentParamsScript);
mRenderLoop.set_gVertexParamsScript(mVertexParamsScript);
mRenderLoop.set_gCullScript(mCullScript);
mRenderLoop.set_gPFSBackground(ProgramStore.BLEND_NONE_DEPTH_TEST(mRS));
}
public ScriptC getRenderLoop() {
return mRenderLoop;
}
}
|