35 |
35 |
|
36 |
36 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37 |
37 |
|
38 |
|
public class TransformFeedback
|
|
38 |
class TransformFeedback
|
39 |
39 |
{
|
40 |
|
private static DistortedProgram mFeedbackProgram;
|
41 |
|
private static int mProgramHandle;
|
|
40 |
private DistortedProgram mFeedbackProgram;
|
|
41 |
private int mBufferLength;
|
|
42 |
private int[] mVBO, mTBO;
|
42 |
43 |
|
43 |
44 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44 |
45 |
|
45 |
|
public TransformFeedback(final Context context)
|
|
46 |
TransformFeedback(final Context context)
|
46 |
47 |
{
|
47 |
48 |
final Resources resources = context.getResources();
|
48 |
49 |
final InputStream vertStream = resources.openRawResource(org.distorted.library.R.raw.feedback_vertex_shader);
|
... | ... | |
55 |
56 |
try
|
56 |
57 |
{
|
57 |
58 |
mFeedbackProgram = new DistortedProgram(vertStream,fragStream, vertHeader, fragHeader, Distorted.GLSL, feedback );
|
58 |
|
mProgramHandle = mFeedbackProgram.getProgramHandle();
|
59 |
59 |
}
|
60 |
60 |
catch(Exception ex)
|
61 |
61 |
{
|
62 |
62 |
Log.e("TransformFeedback", "exception creating feedback program: "+ex.getMessage());
|
63 |
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);
|
64 |
79 |
}
|
65 |
80 |
|
66 |
81 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67 |
82 |
|
68 |
83 |
void render()
|
69 |
84 |
{
|
70 |
|
GLES30.glUseProgram(mProgramHandle);
|
71 |
|
checkGlError("glUseProgram");
|
|
85 |
mFeedbackProgram.useProgram();
|
72 |
86 |
|
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);
|
|
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]);
|
78 |
90 |
|
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 |
91 |
GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD);
|
104 |
|
|
105 |
|
// Start the TransformFeedback, Draw the Arrays, then End the Transform Feedback
|
106 |
92 |
GLES30.glBeginTransformFeedback(GLES30.GL_POINTS);
|
107 |
|
GLES30.glDrawArrays(GLES30.GL_POINTS, 0, bufferLength);
|
108 |
|
checkGlError("glDrawArrays");
|
|
93 |
GLES30.glDrawArrays(GLES30.GL_POINTS, 0, mBufferLength);
|
109 |
94 |
GLES30.glEndTransformFeedback();
|
110 |
|
|
111 |
|
// Reenable Rasterizer if you need it, which you do if you are drawing anything.
|
112 |
95 |
GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD);
|
113 |
|
|
114 |
|
// Flush out anything in GL before mapping the buffer.
|
115 |
96 |
GLES30.glFlush();
|
116 |
|
checkGlError("pre-glMapBufferRange ");
|
117 |
97 |
|
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");
|
|
98 |
Buffer mappedBuffer = GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mBufferLength, GLES30.GL_MAP_READ_BIT);
|
122 |
99 |
|
123 |
|
// Read out the data, here we write to logcat.
|
124 |
|
if (mappedBuffer!=null){
|
|
100 |
if (mappedBuffer!=null)
|
|
101 |
{
|
125 |
102 |
ByteBuffer bb = ((ByteBuffer) mappedBuffer);
|
126 |
103 |
bb.order(ByteOrder.nativeOrder());
|
127 |
104 |
FloatBuffer transformedData = bb.asFloatBuffer();
|
128 |
105 |
|
129 |
|
Log.d( "TransofrmFeedback", String.format("output values = %f %f %f %f %f %f\n",
|
|
106 |
Log.d( "TransformFeedback", String.format("output values = %f %f %f %f %f %f\n",
|
130 |
107 |
transformedData.get(), transformedData.get(), transformedData.get(),
|
131 |
108 |
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 |
|
}
|
|
109 |
}
|
136 |
110 |
|
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 |
|
}
|
|
111 |
GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER);
|
148 |
112 |
}
|
149 |
113 |
}
|
Progress with moving Transform Feedback into library.