Project

General

Profile

Download (7.01 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / feedback / TransformFeedback.java @ 8beaa293

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.feedback;
21

    
22
import android.content.Context;
23
import android.content.res.Resources;
24
import android.opengl.GLES30;
25
import android.util.Log;
26

    
27
import org.distorted.library.Distorted;
28
import org.distorted.library.program.DistortedProgram;
29

    
30
import java.io.InputStream;
31
import java.nio.Buffer;
32
import java.nio.ByteBuffer;
33
import java.nio.ByteOrder;
34
import java.nio.FloatBuffer;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class TransformFeedback
39
    {
40
    private static DistortedProgram mFeedbackProgram;
41
    private static int mProgramHandle;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
    public TransformFeedback(final Context context)
46
        {
47
        final Resources resources = context.getResources();
48
        final InputStream vertStream = resources.openRawResource(org.distorted.library.R.raw.feedback_vertex_shader);
49
        final InputStream fragStream = resources.openRawResource(org.distorted.library.R.raw.feedback_fragment_shader);
50

    
51
        String vertHeader= Distorted.GLSL_VERSION;
52
        String fragHeader= Distorted.GLSL_VERSION;
53
        String[] feedback = { "outValue" };
54

    
55
        try
56
          {
57
          mFeedbackProgram = new DistortedProgram(vertStream,fragStream, vertHeader, fragHeader, Distorted.GLSL, feedback );
58
          mProgramHandle = mFeedbackProgram.getProgramHandle();
59
          }
60
        catch(Exception ex)
61
          {
62
          Log.e("TransformFeedback", "exception creating feedback program: "+ex.getMessage());
63
          }
64
        }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
    void render()
69
        {
70
        GLES30.glUseProgram(mProgramHandle);
71
        checkGlError("glUseProgram");
72

    
73
        // Create data to fill VBO
74
        float[] floatData = { 1.0f, 4.0f, 9.0f, 16.0f, 25.0f, 100.0f };
75
        int bufferLength = floatData.length * 4;
76
        FloatBuffer data = ByteBuffer.allocateDirect(bufferLength).order(ByteOrder.nativeOrder()).asFloatBuffer();
77
        data.put(floatData).position(0);
78

    
79
        // Create VBO and fill with data
80
        int[] vbo = new int[1];
81
        GLES30.glGenBuffers(1, vbo, 0);
82
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, vbo[0]);
83
        GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, bufferLength, data, GLES30.GL_STATIC_READ);
84
        checkGlError("glBufferData GL_ARRAY_BUFFER");
85

    
86
        // Link created VBO to shader attribute "inValue"
87
        int inputAttrib = GLES30.glGetAttribLocation(mProgramHandle, "inValue");
88
        GLES30.glEnableVertexAttribArray(inputAttrib);
89
        GLES30.glVertexAttribPointer(inputAttrib, 1, GLES30.GL_FLOAT, false, 4, 0);
90
        checkGlError("glVertexAttribPointer");
91

    
92
        // Create transform feedback buffer object and bind to transform feedback
93
        //  this creates space in a buffer object for the TransformFeedback.
94
        int[] tbo = new int[1];
95
        GLES30.glGenBuffers(1, tbo, 0);
96
        GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, tbo[0]);
97
        GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, bufferLength, null, GLES30.GL_STATIC_READ);
98
        GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo[0]); //very important
99

    
100
        checkGlError("glBindBufferBase");
101

    
102
        // Disable Rasterizer if you just need the data
103
        GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD);
104

    
105
        // Start the TransformFeedback, Draw the Arrays, then End the Transform Feedback
106
        GLES30.glBeginTransformFeedback(GLES30.GL_POINTS);
107
        GLES30.glDrawArrays(GLES30.GL_POINTS, 0, bufferLength);
108
        checkGlError("glDrawArrays");
109
        GLES30.glEndTransformFeedback();
110

    
111
        // Reenable Rasterizer if you need it, which you do if you are drawing anything.
112
        GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD);
113

    
114
        // Flush out anything in GL before mapping the buffer.
115
        GLES30.glFlush();
116
        checkGlError("pre-glMapBufferRange ");
117

    
118
        // Map the transform feedback buffer to local address space.
119
        Buffer mappedBuffer =  GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER,
120
                0, bufferLength, GLES30.GL_MAP_READ_BIT);
121
        checkGlError("glMapBufferRange");
122

    
123
        // Read out the data, here we write to logcat.
124
        if (mappedBuffer!=null){
125
            ByteBuffer bb = ((ByteBuffer) mappedBuffer);
126
            bb.order(ByteOrder.nativeOrder());
127
            FloatBuffer transformedData = bb.asFloatBuffer();
128

    
129
            Log.d( "TransofrmFeedback", String.format("output values = %f %f %f %f %f %f\n",
130
                    transformedData.get(), transformedData.get(), transformedData.get(),
131
                    transformedData.get(), transformedData.get(), transformedData.get() ));
132
        }
133
        // Don't forget to Unmap the Transform Feeback Buffer.
134
        GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER);
135
        }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    private static void checkGlError(String op)
140
        {
141
        int error = GLES30.glGetError();
142

    
143
        if (error != GLES30.GL_NO_ERROR)
144
          {
145
          String msg = op + ": glError 0x" + Integer.toHexString(error);
146
          throw new RuntimeException(msg);
147
          }
148
        }
149
}
(4-4/4)