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
|
class TransformFeedback
|
39
|
{
|
40
|
private DistortedProgram mFeedbackProgram;
|
41
|
private int mBufferLength;
|
42
|
private int[] mVBO, mTBO;
|
43
|
|
44
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45
|
|
46
|
TransformFeedback(final Context context)
|
47
|
{
|
48
|
final Resources resources = context.getResources();
|
49
|
final InputStream vertStream = resources.openRawResource(org.distorted.library.R.raw.feedback_vertex_shader);
|
50
|
final InputStream fragStream = resources.openRawResource(org.distorted.library.R.raw.feedback_fragment_shader);
|
51
|
|
52
|
String vertHeader= Distorted.GLSL_VERSION;
|
53
|
String fragHeader= Distorted.GLSL_VERSION;
|
54
|
String[] feedback = { "outValue" };
|
55
|
|
56
|
try
|
57
|
{
|
58
|
mFeedbackProgram = new DistortedProgram(vertStream,fragStream, vertHeader, fragHeader, Distorted.GLSL, feedback );
|
59
|
}
|
60
|
catch(Exception ex)
|
61
|
{
|
62
|
Log.e("TransformFeedback", "exception creating feedback program: "+ex.getMessage());
|
63
|
}
|
64
|
|
65
|
float[] floatData = { 2.0f, 4.0f, 9.0f, 16.0f, 25.0f, 100.0f };
|
66
|
mBufferLength = floatData.length * 4;
|
67
|
FloatBuffer data = ByteBuffer.allocateDirect(mBufferLength).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
68
|
data.put(floatData).position(0);
|
69
|
|
70
|
mVBO = new int[1];
|
71
|
GLES30.glGenBuffers(1, mVBO, 0);
|
72
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVBO[0]);
|
73
|
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, mBufferLength, data, GLES30.GL_STATIC_READ);
|
74
|
|
75
|
mTBO = new int[1];
|
76
|
GLES30.glGenBuffers(1, mTBO, 0);
|
77
|
GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mTBO[0]);
|
78
|
GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mBufferLength, null, GLES30.GL_STATIC_READ);
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
void render()
|
84
|
{
|
85
|
mFeedbackProgram.useProgram();
|
86
|
|
87
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVBO[0]);
|
88
|
GLES30.glVertexAttribPointer(mFeedbackProgram.mAttribute[0], 1, GLES30.GL_FLOAT, false, 4, 0);
|
89
|
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mTBO[0]);
|
90
|
|
91
|
GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD);
|
92
|
GLES30.glBeginTransformFeedback(GLES30.GL_POINTS);
|
93
|
GLES30.glDrawArrays(GLES30.GL_POINTS, 0, mBufferLength);
|
94
|
GLES30.glEndTransformFeedback();
|
95
|
GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD);
|
96
|
GLES30.glFlush();
|
97
|
|
98
|
Buffer mappedBuffer = GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mBufferLength, GLES30.GL_MAP_READ_BIT);
|
99
|
|
100
|
if (mappedBuffer!=null)
|
101
|
{
|
102
|
ByteBuffer bb = ((ByteBuffer) mappedBuffer);
|
103
|
bb.order(ByteOrder.nativeOrder());
|
104
|
FloatBuffer transformedData = bb.asFloatBuffer();
|
105
|
|
106
|
Log.d( "TransformFeedback", String.format("output values = %f %f %f %f %f %f\n",
|
107
|
transformedData.get(), transformedData.get(), transformedData.get(),
|
108
|
transformedData.get(), transformedData.get(), transformedData.get() ));
|
109
|
}
|
110
|
|
111
|
GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER);
|
112
|
}
|
113
|
}
|