Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ e029600f

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 c90b9e01 Leszek Koltunski
import android.content.res.Resources;
23 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
24
import android.util.Log;
25 6a06a912 Leszek Koltunski
26 fe82a979 Leszek Koltunski
import org.distorted.library.R;
27
import org.distorted.library.effect.Effect;
28 da9b3f07 Leszek Koltunski
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.effect.EffectType;
30 7cd24173 leszek
import org.distorted.library.effect.FragmentEffect;
31
import org.distorted.library.effect.VertexEffect;
32 e458a4ba Leszek Koltunski
import org.distorted.library.message.EffectListener;
33 55c14a19 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
34 c90b9e01 Leszek Koltunski
import org.distorted.library.program.FragmentCompilationException;
35
import org.distorted.library.program.FragmentUniformsException;
36
import org.distorted.library.program.LinkingException;
37
import org.distorted.library.program.VertexCompilationException;
38
import org.distorted.library.program.VertexUniformsException;
39 a4835695 Leszek Koltunski
40 c90b9e01 Leszek Koltunski
import java.io.InputStream;
41 c638c1b0 Leszek Koltunski
import java.nio.ByteBuffer;
42
import java.nio.ByteOrder;
43
import java.nio.FloatBuffer;
44 8777ce17 Leszek Koltunski
import java.nio.IntBuffer;
45 c638c1b0 Leszek Koltunski
46 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
47 b329f352 Leszek Koltunski
/**
48 faa3ff56 Leszek Koltunski
 * Class containing Matrix, Vertex, Fragment and Postprocessing effect queues.
49 b73dcaa7 Leszek Koltunski
 * <p>
50 faa3ff56 Leszek Koltunski
 * The queues hold actual effects to be applied to a given (InputSurface,MeshObject) combo.
51 b329f352 Leszek Koltunski
 */
52 86d322b5 Leszek Koltunski
public class DistortedEffects
53 d425545a Leszek Koltunski
  {
54 1aedf874 leszek
  /// MAIN PROGRAM ///
55
  private static DistortedProgram mMainProgram;
56
  private static int mMainTextureH;
57 8fa96e69 Leszek Koltunski
58 c1e24646 leszek
  /// BLIT PROGRAM ///
59
  private static DistortedProgram mBlitProgram;
60
  private static int mBlitTextureH;
61 c2c08950 leszek
  private static int mBlitDepthH;
62 c90b9e01 Leszek Koltunski
  private static final FloatBuffer mQuadPositions;
63
64
  static
65
    {
66
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
67
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
68
    mQuadPositions.put(positionData).position(0);
69
    }
70
71 7170e4eb leszek
  /// BLIT DEPTH PROGRAM ///
72
  private static DistortedProgram mBlitDepthProgram;
73 8777ce17 Leszek Koltunski
  private static int mBlitDepthSizeH;
74
75
  private static int[] mLinkedListSSBO = new int[1];
76
  private static int[] mAtomicCounter = new int[1];
77
78
  static
79
    {
80
    mLinkedListSSBO[0]= -1;
81
    mAtomicCounter[0] = -1;
82
    }
83
84
  private static int mBufferSize=(0x1<<23);  // 8 million entries
85 cee0369a Leszek Koltunski
86 8777ce17 Leszek Koltunski
  private static IntBuffer mIntBuffer;
87
88 cee0369a Leszek Koltunski
private static ByteBuffer mBuf;
89
private static IntBuffer mIntBuf;
90
91 8777ce17 Leszek Koltunski
  /// BLIT DEPTH RENDER PROGRAM ///
92
  private static DistortedProgram mBlitDepthRenderProgram;
93
  private static int mBlitDepthRenderSizeH;
94 7170e4eb leszek
95 3fc9327a Leszek Koltunski
  /// NORMAL PROGRAM /////
96
  private static DistortedProgram mNormalProgram;
97
  private static int mNormalMVPMatrixH;
98
  /// END PROGRAMS //////
99 c638c1b0 Leszek Koltunski
100 8e34674e Leszek Koltunski
  private static long mNextID =0;
101 4e2382f3 Leszek Koltunski
  private long mID;
102 8e34674e Leszek Koltunski
103 310e14fb leszek
  private EffectQueueMatrix mM;
104 3fc9327a Leszek Koltunski
  private EffectQueueFragment mF;
105 310e14fb leszek
  private EffectQueueVertex mV;
106 80b3acf6 Leszek Koltunski
  private EffectQueuePostprocess mP;
107
108
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
109 3d590d8d Leszek Koltunski
110 9361b337 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111 55c14a19 Leszek Koltunski
112 c90b9e01 Leszek Koltunski
  static void createProgram(Resources resources)
113
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
114 55c14a19 Leszek Koltunski
    {
115 cab7c165 Leszek Koltunski
    // MAIN PROGRAM ////////////////////////////////////
116 1aedf874 leszek
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
117
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
118
119 7cd24173 leszek
    int numF = FragmentEffect.getNumEnabled();
120
    int numV = VertexEffect.getNumEnabled();
121 c90b9e01 Leszek Koltunski
122 7cd24173 leszek
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
123
    String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
124
    String enabledEffectV= VertexEffect.getGLSL();
125
    String enabledEffectF= FragmentEffect.getGLSL();
126 c90b9e01 Leszek Koltunski
127 03cb451d Leszek Koltunski
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
128
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
129 7cd24173 leszek
    //android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
130
    //android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
131 c90b9e01 Leszek Koltunski
132 3fc9327a Leszek Koltunski
    String[] feedback = { "v_Position", "v_endPosition" };
133 cab7c165 Leszek Koltunski
134 cee0369a Leszek Koltunski
    try
135
      {
136
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
137
                                          enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
138
      }
139
    catch(Exception e)
140
      {
141
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
142
      throw new RuntimeException(e.getMessage());
143
      }
144 c90b9e01 Leszek Koltunski
145 c1e24646 leszek
    int mainProgramH = mMainProgram.getProgramHandle();
146 c90b9e01 Leszek Koltunski
    EffectQueueFragment.getUniforms(mainProgramH);
147 1aedf874 leszek
    EffectQueueVertex.getUniforms(mainProgramH);
148
    EffectQueueMatrix.getUniforms(mainProgramH);
149 e6519ac8 Leszek Koltunski
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
150 c1e24646 leszek
151 1aedf874 leszek
    // BLIT PROGRAM ////////////////////////////////////
152 c1e24646 leszek
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
153
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
154
155 94f6d472 Leszek Koltunski
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
156
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
157 c1e24646 leszek
158 f2367b75 Leszek Koltunski
    try
159
      {
160 94f6d472 Leszek Koltunski
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
161 f2367b75 Leszek Koltunski
      }
162
    catch(Exception e)
163
      {
164 cee0369a Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
165 f2367b75 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
166
      }
167 c1e24646 leszek
168
    int blitProgramH = mBlitProgram.getProgramHandle();
169 e6519ac8 Leszek Koltunski
    mBlitTextureH  = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
170
    mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
171 c90b9e01 Leszek Koltunski
172 7170e4eb leszek
    // BLIT DEPTH PROGRAM ////////////////////////////////////
173
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
174
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
175
176
    try
177
      {
178
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
179
      }
180
    catch(Exception e)
181
      {
182 cee0369a Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
183 7170e4eb leszek
      throw new RuntimeException(e.getMessage());
184
      }
185
186
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
187 8777ce17 Leszek Koltunski
    mBlitDepthSizeH         = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Size");
188
189
    mIntBuffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
190
    mIntBuffer.put(0,0);
191
192
    if( mLinkedListSSBO[0]<0 )
193
      {
194
      GLES31.glGenBuffers(1,mLinkedListSSBO,0);
195
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
196 cee0369a Leszek Koltunski
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
197
198
      mBuf = (ByteBuffer)GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, mBufferSize*4, GLES31.GL_MAP_READ_BIT);
199
      mIntBuf = mBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
200
201 8777ce17 Leszek Koltunski
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
202
      }
203
204
    if( mAtomicCounter[0]<0 )
205
      {
206
      GLES31.glGenBuffers(1,mAtomicCounter,0);
207
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
208
      GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
209
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
210
      }
211
212
    // BLIT DEPTH RENDER PROGRAM ///////////////////////////
213
    final InputStream blitDepthRenderVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
214
    final InputStream blitDepthRenderFragStream = resources.openRawResource(R.raw.blit_depth_render_fragment_shader);
215
216
    try
217
      {
218
      mBlitDepthRenderProgram = new DistortedProgram(blitDepthRenderVertStream,blitDepthRenderFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
219
      }
220
    catch(Exception e)
221
      {
222 cee0369a Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH RENDER program: "+e.getMessage());
223 8777ce17 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
224
      }
225
226
    int blitDepthRenderProgramH   = mBlitDepthRenderProgram.getProgramHandle();
227
    mBlitDepthRenderSizeH         = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
228 7170e4eb leszek
229 3fc9327a Leszek Koltunski
    // NORMAL PROGRAM //////////////////////////////////////
230
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
231
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
232 c90b9e01 Leszek Koltunski
233 f2367b75 Leszek Koltunski
    try
234
      {
235 3fc9327a Leszek Koltunski
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
236 f2367b75 Leszek Koltunski
      }
237
    catch(Exception e)
238
      {
239 cee0369a Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
240 f2367b75 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
241
      }
242 c90b9e01 Leszek Koltunski
243 3fc9327a Leszek Koltunski
    int normalProgramH = mNormalProgram.getProgramHandle();
244 e6519ac8 Leszek Koltunski
    mNormalMVPMatrixH  = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
245 55c14a19 Leszek Koltunski
    }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249 421c2728 Leszek Koltunski
  private void initializeEffectLists(DistortedEffects d, int flags)
250 d425545a Leszek Koltunski
    {
251 015642fb Leszek Koltunski
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
252 6a06a912 Leszek Koltunski
      {
253 d425545a Leszek Koltunski
      mM = d.mM;
254
      matrixCloned = true;
255
      }
256
    else
257
      {
258 4e2382f3 Leszek Koltunski
      mM = new EffectQueueMatrix(mID);
259 d425545a Leszek Koltunski
      matrixCloned = false;
260
      }
261 6a06a912 Leszek Koltunski
    
262 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
263
      {
264
      mV = d.mV;
265
      vertexCloned = true;
266
      }
267
    else
268
      {
269 4e2382f3 Leszek Koltunski
      mV = new EffectQueueVertex(mID);
270 d425545a Leszek Koltunski
      vertexCloned = false;
271
      }
272 6a06a912 Leszek Koltunski
    
273 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
274
      {
275
      mF = d.mF;
276
      fragmentCloned = true;
277 6a06a912 Leszek Koltunski
      }
278 d425545a Leszek Koltunski
    else
279
      {
280 4e2382f3 Leszek Koltunski
      mF = new EffectQueueFragment(mID);
281 d425545a Leszek Koltunski
      fragmentCloned = false;
282
      }
283 80b3acf6 Leszek Koltunski
284
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
285
      {
286
      mP = d.mP;
287
      postprocessCloned = true;
288
      }
289
    else
290
      {
291
      mP = new EffectQueuePostprocess(mID);
292
      postprocessCloned = false;
293
      }
294
    }
295
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297
298 70b6a155 Leszek Koltunski
  EffectQueuePostprocess getPostprocess()
299 80b3acf6 Leszek Koltunski
    {
300 70b6a155 Leszek Koltunski
    return mP;
301 80b3acf6 Leszek Koltunski
    }
302
303 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
304
305
  void newNode(DistortedNode node)
306
    {
307
    mM.newNode(node);
308
    mF.newNode(node);
309
    mV.newNode(node);
310
    mP.newNode(node);
311
    }
312
313 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
314 c90b9e01 Leszek Koltunski
315 3fc9327a Leszek Koltunski
  private void displayNormals(MeshObject mesh)
316 c90b9e01 Leszek Koltunski
    {
317 e6519ac8 Leszek Koltunski
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
318
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
319 604b2899 Leszek Koltunski
    DistortedRenderState.switchOffDrawing();
320 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
321 604b2899 Leszek Koltunski
    DistortedRenderState.restoreDrawing();
322 e6519ac8 Leszek Koltunski
    GLES31.glEndTransformFeedback();
323
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
324 604b2899 Leszek Koltunski
325 3fc9327a Leszek Koltunski
    mNormalProgram.useProgram();
326 e6519ac8 Leszek Koltunski
    GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
327
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
328
    GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
329
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
330
    GLES31.glLineWidth(8.0f);
331
    GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
332 c90b9e01 Leszek Koltunski
    }
333 1aedf874 leszek
334 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
335
336 270c27bc Leszek Koltunski
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
337 d425545a Leszek Koltunski
    {
338 c1e24646 leszek
    float halfZ = halfW*mesh.zFactor;
339 638b5b5c leszek
340 8fbd0237 Leszek Koltunski
    mM.compute(currTime);
341
    mV.compute(currTime,halfW,halfH,halfZ);
342
    mF.compute(currTime,halfW,halfH);
343 1149be8f leszek
    mP.compute(currTime);
344 8fbd0237 Leszek Koltunski
345 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
346 d6e94c84 Leszek Koltunski
347 b9798977 leszek
    mMainProgram.useProgram();
348 e6519ac8 Leszek Koltunski
    GLES31.glUniform1i(mMainTextureH, 0);
349 12f45260 Leszek Koltunski
350 8777ce17 Leszek Koltunski
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
351
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
352
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
353
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
354
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
355 cab7c165 Leszek Koltunski
356 270c27bc Leszek Koltunski
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
357 8fbd0237 Leszek Koltunski
    mV.send();
358
    mF.send();
359 42571056 Leszek Koltunski
360 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
361 c90b9e01 Leszek Koltunski
362 3fc9327a Leszek Koltunski
    if( mesh.mShowNormals ) displayNormals(mesh);
363 d425545a Leszek Koltunski
    }
364 6a06a912 Leszek Koltunski
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366 7266d8ef Leszek Koltunski
/**
367
 * Only for use by the library itself.
368
 *
369
 * @y.exclude
370
 */
371
  public static void blitPriv(DistortedOutputSurface surface)
372 d425545a Leszek Koltunski
    {
373 c1e24646 leszek
    mBlitProgram.useProgram();
374
375 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
376
    GLES31.glUniform1i(mBlitTextureH, 0);
377
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
378
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
379
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
380 d425545a Leszek Koltunski
    }
381 7170e4eb leszek
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383
384 ae2802b1 Leszek Koltunski
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
385 7170e4eb leszek
    {
386
    mBlitDepthProgram.useProgram();
387
388 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
389 e029600f Leszek Koltunski
    GLES31.glUniform2f(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
390 e6519ac8 Leszek Koltunski
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
391
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
392 8777ce17 Leszek Koltunski
    }
393
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
// render all the transparent pixels from the per-pixel linked lists. This is in the 'merge
396
// postprocessing buckets' stage.
397
398
  static void blitDepthRenderPriv(DistortedOutputSurface surface, float corrW, float corrH)
399
    {
400
    mBlitDepthRenderProgram.useProgram();
401
402 e029600f Leszek Koltunski
    //analyzeBuffer(surface.mWidth, surface.mHeight);
403 cee0369a Leszek Koltunski
404 8777ce17 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
405 e029600f Leszek Koltunski
    GLES31.glUniform2f(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
406 8777ce17 Leszek Koltunski
    GLES31.glVertexAttribPointer(mBlitDepthRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
407
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
408
409
    // reset atomic counter to 0
410
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
411
    GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
412
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
413 7170e4eb leszek
    }
414
415 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
416 cee0369a Leszek Koltunski
417
418
  private static void analyzeBuffer(int w, int h)
419
    {
420
    int ptr;
421 e029600f Leszek Koltunski
    final int GREEN = 1;
422
    final int RED   = 2;
423 cee0369a Leszek Koltunski
424
    int left1=0, left2=0;
425
    int right1=0, right2=0;
426
    int overflow = 0;
427
428
    for(int row=0; row<h; row++)
429
      for(int col=0; col<w; col++)
430
        {
431
        ptr = mIntBuf.get( col+row*w );
432
433
        if( ptr!=0 )
434
          {
435
          if( ptr>0 && ptr<mBufferSize )
436
            {
437
            ptr = mIntBuf.get(ptr);
438
439
            if ((2*col>w) && ptr != RED)
440
              {
441
              if (ptr==GREEN) right1++;
442
              else            right2++;
443
              }
444
            if ((2*col<=w) && ptr != GREEN)
445
              {
446
              if (ptr==RED) left1++;
447
              else          left2++;
448
              }
449
            }
450
          else
451
            {
452
            overflow++;
453
            }
454
          }
455
        }
456
457
    android.util.Log.e("surface", " left1: "+left1+" left2: "+left2+" right1: "+right1+" right2: "+right2+" overflow: "+overflow);
458
    }
459
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461
462 8e34674e Leszek Koltunski
  private void releasePriv()
463 d425545a Leszek Koltunski
    {
464 80b3acf6 Leszek Koltunski
    if( !matrixCloned   )   mM.abortAll(false);
465
    if( !vertexCloned   )   mV.abortAll(false);
466
    if( !fragmentCloned )   mF.abortAll(false);
467
    if( !postprocessCloned) mP.abortAll(false);
468 d425545a Leszek Koltunski
469 4e2382f3 Leszek Koltunski
    mM = null;
470
    mV = null;
471
    mF = null;
472 80b3acf6 Leszek Koltunski
    mP = null;
473 8e34674e Leszek Koltunski
    }
474
475 1942537e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
476
477 7b8086eb Leszek Koltunski
  static void onDestroy()
478 1942537e Leszek Koltunski
    {
479 8777ce17 Leszek Koltunski
    mNextID           =  0;
480
    mLinkedListSSBO[0]= -1;
481
    mAtomicCounter[0] = -1;
482 1942537e Leszek Koltunski
    }
483
484 8e34674e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
485
// PUBLIC API
486 ada90d33 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
487 d425545a Leszek Koltunski
/**
488 4e2382f3 Leszek Koltunski
 * Create empty effect queue.
489 d425545a Leszek Koltunski
 */
490 421c2728 Leszek Koltunski
  public DistortedEffects()
491 d425545a Leszek Koltunski
    {
492 c7da4e65 leszek
    mID = ++mNextID;
493 4e2382f3 Leszek Koltunski
    initializeEffectLists(this,0);
494 d425545a Leszek Koltunski
    }
495 ada90d33 Leszek Koltunski
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497 d425545a Leszek Koltunski
/**
498 4e2382f3 Leszek Koltunski
 * Copy constructor.
499 d425545a Leszek Koltunski
 * <p>
500
 * Whatever we do not clone gets created just like in the default constructor.
501
 *
502
 * @param dc    Source object to create our object from
503
 * @param flags A bitmask of values specifying what to copy.
504 e6ab30eb Leszek Koltunski
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
505 d425545a Leszek Koltunski
 */
506 421c2728 Leszek Koltunski
  public DistortedEffects(DistortedEffects dc, int flags)
507 d425545a Leszek Koltunski
    {
508 c7da4e65 leszek
    mID = ++mNextID;
509 4e2382f3 Leszek Koltunski
    initializeEffectLists(dc,flags);
510 d425545a Leszek Koltunski
    }
511 ada90d33 Leszek Koltunski
512 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
513
/**
514 07305c87 Leszek Koltunski
 * Releases all resources. After this call, the queue should not be used anymore.
515 6a06a912 Leszek Koltunski
 */
516 13687207 leszek
  @SuppressWarnings("unused")
517 8e34674e Leszek Koltunski
  public synchronized void delete()
518 d425545a Leszek Koltunski
    {
519
    releasePriv();
520
    }
521 6a06a912 Leszek Koltunski
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523
/**
524 4e2382f3 Leszek Koltunski
 * Returns unique ID of this instance.
525
 *
526
 * @return ID of the object.
527 6a06a912 Leszek Koltunski
 */
528 4e2382f3 Leszek Koltunski
  public long getID()
529 d425545a Leszek Koltunski
      {
530 4e2382f3 Leszek Koltunski
      return mID;
531 d425545a Leszek Koltunski
      }
532 4e2382f3 Leszek Koltunski
533 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
534
/**
535
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
536 faa3ff56 Leszek Koltunski
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
537 6a06a912 Leszek Koltunski
 * 
538
 * @param el A class implementing the EffectListener interface that wants to get notifications.
539
 */
540 13687207 leszek
  @SuppressWarnings("unused")
541 3fc994b2 Leszek Koltunski
  public void registerForMessages(EffectListener el)
542 d425545a Leszek Koltunski
    {
543 26a4e5f6 leszek
    mM.registerForMessages(el);
544 3fc994b2 Leszek Koltunski
    mV.registerForMessages(el);
545
    mF.registerForMessages(el);
546 80b3acf6 Leszek Koltunski
    mP.registerForMessages(el);
547 d425545a Leszek Koltunski
    }
548 6a06a912 Leszek Koltunski
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
/**
551 faa3ff56 Leszek Koltunski
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
552 6a06a912 Leszek Koltunski
 * 
553
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
554
 */
555 13687207 leszek
  @SuppressWarnings("unused")
556 3fc994b2 Leszek Koltunski
  public void deregisterForMessages(EffectListener el)
557 d425545a Leszek Koltunski
    {
558 26a4e5f6 leszek
    mM.deregisterForMessages(el);
559 3fc994b2 Leszek Koltunski
    mV.deregisterForMessages(el);
560
    mF.deregisterForMessages(el);
561 80b3acf6 Leszek Koltunski
    mP.deregisterForMessages(el);
562 d425545a Leszek Koltunski
    }
563 6a06a912 Leszek Koltunski
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565
/**
566 d07f2950 Leszek Koltunski
 * Aborts all Effects.
567
 * @return Number of effects aborted.
568 6a06a912 Leszek Koltunski
 */
569 d425545a Leszek Koltunski
  public int abortAllEffects()
570 13687207 leszek
    {
571
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
572
    }
573 6a06a912 Leszek Koltunski
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575
/**
576 d07f2950 Leszek Koltunski
 * Aborts all Effects of a given type, for example all MATRIX Effects.
577 6a06a912 Leszek Koltunski
 * 
578 da9b3f07 Leszek Koltunski
 * @param type one of the constants defined in {@link EffectType}
579 d07f2950 Leszek Koltunski
 * @return Number of effects aborted.
580 6a06a912 Leszek Koltunski
 */
581 da9b3f07 Leszek Koltunski
  public int abortByType(EffectType type)
582 d425545a Leszek Koltunski
    {
583
    switch(type)
584 6a06a912 Leszek Koltunski
      {
585 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.abortAll(true);
586
      case VERTEX     : return mV.abortAll(true);
587
      case FRAGMENT   : return mF.abortAll(true);
588 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.abortAll(true);
589 da9b3f07 Leszek Koltunski
      default         : return 0;
590 6a06a912 Leszek Koltunski
      }
591 d425545a Leszek Koltunski
    }
592 47316d20 leszek
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594
/**
595 faa3ff56 Leszek Koltunski
 * Aborts an Effect by its ID.
596 47316d20 leszek
 *
597
 * @param id the Id of the Effect to be removed, as returned by getID().
598
 * @return Number of effects aborted.
599
 */
600
  public int abortById(long id)
601
    {
602
    long type = id&EffectType.MASK;
603
604 2ef5dd9e leszek
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
605
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
606
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
607 80b3acf6 Leszek Koltunski
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
608 47316d20 leszek
609
    return 0;
610
    }
611
612 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
613
/**
614
 * Aborts a single Effect.
615
 * 
616 6bb59aad Leszek Koltunski
 * @param effect the Effect we want to abort.
617 476bbc81 Leszek Koltunski
 * @return number of Effects aborted. Always either 0 or 1.
618 6a06a912 Leszek Koltunski
 */
619 6bb59aad Leszek Koltunski
  public int abortEffect(Effect effect)
620 d425545a Leszek Koltunski
    {
621 6bb59aad Leszek Koltunski
    switch(effect.getType())
622
      {
623 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeEffect(effect);
624
      case VERTEX     : return mV.removeEffect(effect);
625
      case FRAGMENT   : return mF.removeEffect(effect);
626 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeEffect(effect);
627 da9b3f07 Leszek Koltunski
      default         : return 0;
628 6bb59aad Leszek Koltunski
      }
629 d425545a Leszek Koltunski
    }
630 6a06a912 Leszek Koltunski
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632
/**
633 e8c81a8e Leszek Koltunski
 * Abort all Effects of a given name, for example all rotations.
634 6a06a912 Leszek Koltunski
 * 
635 da9b3f07 Leszek Koltunski
 * @param name one of the constants defined in {@link EffectName}
636 476bbc81 Leszek Koltunski
 * @return number of Effects aborted.
637 6a06a912 Leszek Koltunski
 */
638 da9b3f07 Leszek Koltunski
  public int abortByName(EffectName name)
639 d425545a Leszek Koltunski
    {
640 da9b3f07 Leszek Koltunski
    switch(name.getType())
641 6a06a912 Leszek Koltunski
      {
642 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeByName(name);
643
      case VERTEX     : return mV.removeByName(name);
644
      case FRAGMENT   : return mF.removeByName(name);
645 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeByName(name);
646 6bb59aad Leszek Koltunski
      default                : return 0;
647 6a06a912 Leszek Koltunski
      }
648 d425545a Leszek Koltunski
    }
649 432442f9 Leszek Koltunski
650
///////////////////////////////////////////////////////////////////////////////////////////////////
651
/**
652 faa3ff56 Leszek Koltunski
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
653
 * single (InputSurface,MeshObject) combo.
654 432442f9 Leszek Koltunski
 *
655 fe6fe99a leszek
 * @param type {@link EffectType}
656
 * @return The maximum number of effects of a given type.
657 432442f9 Leszek Koltunski
 */
658 13687207 leszek
  @SuppressWarnings("unused")
659 fe6fe99a leszek
  public static int getMax(EffectType type)
660 432442f9 Leszek Koltunski
    {
661 fe6fe99a leszek
    return EffectQueue.getMax(type.ordinal());
662 432442f9 Leszek Koltunski
    }
663
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665
/**
666 fe6fe99a leszek
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
667 432442f9 Leszek Koltunski
 * This can fail if:
668
 * <ul>
669
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
670
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
671
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
672
 *     time only decreasing the value of 'max' is permitted.
673
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
674
 * </ul>
675
 *
676 fe6fe99a leszek
 * @param type {@link EffectType}
677
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
678 80b3acf6 Leszek Koltunski
 *            than Byte.MAX_VALUE
679
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
680
 */
681
  @SuppressWarnings("unused")
682 fe6fe99a leszek
  public static boolean setMax(EffectType type, int max)
683 80b3acf6 Leszek Koltunski
    {
684 fe6fe99a leszek
    return EffectQueue.setMax(type.ordinal(),max);
685 80b3acf6 Leszek Koltunski
    }
686
687 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
688
/**
689 6bb59aad Leszek Koltunski
 * Add a new Effect to our queue.
690 f2fe7e28 Leszek Koltunski
 *
691 6bb59aad Leszek Koltunski
 * @param effect The Effect to add.
692 ae77d55e Leszek Koltunski
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
693 6a06a912 Leszek Koltunski
 */
694 ae77d55e Leszek Koltunski
  public boolean apply(Effect effect)
695 d425545a Leszek Koltunski
    {
696 6bb59aad Leszek Koltunski
    switch(effect.getType())
697
      {
698 26a4e5f6 leszek
      case MATRIX      : return mM.add(effect);
699 ae77d55e Leszek Koltunski
      case VERTEX      : return mV.add(effect);
700
      case FRAGMENT    : return mF.add(effect);
701 80b3acf6 Leszek Koltunski
      case POSTPROCESS : return mP.add(effect);
702 6bb59aad Leszek Koltunski
      }
703 ae77d55e Leszek Koltunski
704
    return false;
705 4fde55a0 Leszek Koltunski
    }
706 f2fe7e28 Leszek Koltunski
  }