1/*
2 * Copyright (C) 2015 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
17package com.example.android.rs.vr.engine;
18
19import android.renderscript.Matrix3f;
20import android.renderscript.Matrix4f;
21import android.renderscript.Script;
22import android.renderscript.ScriptIntrinsicResize;
23import android.util.Log;
24
25import java.text.DecimalFormat;
26
27public class VrPipline1 extends BasicPipeline {
28    private static final String LOGTAG = "VrPipline1";
29
30    float[] mMatrixBuffer = new float[16];
31    ScriptC_vr scriptC_vr;
32    ScriptIntrinsicResize script_resize;
33    Script.LaunchOptions options = new Script.LaunchOptions();
34
35    @Override
36    public void initBuffers(VrState state) {
37        super.initBuffers(state);
38    }
39
40    static DecimalFormat df = new DecimalFormat("      ##0.000");
41
42    private static String trim(double d) {
43        String s = df.format(d);
44        return s.substring(s.length() - 6);
45    }
46
47    private static String trim(float[] d) {
48        String ret = "";
49        for (int i = 0; i < d.length; i++) {
50            ret += ((i == 0) ? "[ " : " , ") + trim(d[i]);
51        }
52        return ret + ("]");
53    }
54
55    private void creatOpacityAllocation(VrState state) {
56        scriptC_vr.set_opacity(state.mMaterial.getOpacityAllocation(state.mRs));
57    }
58
59    private void creatColorMapAllocation(VrState state) {
60        scriptC_vr.set_color_map(state.mMaterial.getColorMapAllocation(state.mRs));
61    }
62
63    @Override
64    public void setupTriangles(VrState state) {
65        super.setupTriangles(state);
66        if (mCancel){
67            return;
68        }
69        Matrix m = state.mTransform.getMatrix(Transform.SCREEN_SPACE, Transform.VOLUME_SPACE);
70        m.getAsFloats(mMatrixBuffer);
71        Matrix4f matrix4f = new Matrix4f(mMatrixBuffer);
72        if (scriptC_vr == null) {
73            scriptC_vr = new ScriptC_vr(state.mRs);
74        }
75        if (script_resize == null) {
76            script_resize = ScriptIntrinsicResize.create(state.mRs);
77        }
78        scriptC_vr.set_matrix4(matrix4f);
79        for (int i = 0; i < 9; i++) {
80            int x = i % 3;
81            int y = i / 3;
82            mMatrixBuffer[i] = mMatrixBuffer[x + y * 4];
83        }
84        Matrix3f matrix3f = new Matrix3f(mMatrixBuffer);
85        scriptC_vr.set_matrix3(matrix3f);
86        creatColorMapAllocation(state);
87        creatOpacityAllocation(state);
88        scriptC_vr.invoke_setup_vectors();
89    }
90
91    @Override
92    public void raycast(VrState state) {
93        if (mCancel){
94            return;
95        }
96        scriptC_vr.set_volume(state.mVolume.mVolumeAllocation);
97        scriptC_vr.set_bricks(state.mRsMask.mBrick_allocation);
98        scriptC_vr.set_brick_dimx(state.mRsMask.m_bricks_dimx);
99        scriptC_vr.set_brick_dimy(state.mRsMask.m_bricks_dimy);
100        if (mCancel){
101            return;
102        }
103        scriptC_vr.set_zbuff(state.mzRangeFullAllocation);
104        if (mCancel){
105            return;
106        }
107        if (state.mImgWidth*state.mImgHeight < 512*512) {
108            scriptC_vr.forEach_draw_z_buffer(state.mzRangeFullAllocation, state.mScrAllocation);
109        } else {
110            int blocks = state.mImgWidth*state.mImgHeight/(256*256);
111            for (int i = 0; i < blocks; i++) {
112                options.setX(0,state.mImgWidth);
113                options.setY(i*state.mImgHeight/blocks, (i+1)*state.mImgHeight/blocks);
114                scriptC_vr.forEach_draw_z_buffer(state.mzRangeFullAllocation, state.mScrAllocation, options);
115                state.mRs.finish();
116                if (mCancel){
117                    Log.v(LOGTAG, "cancel");
118                    return;
119                }
120            }
121
122
123        }
124
125    }
126
127}
128