Project

General

Profile

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

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

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 c731c612 Leszek Koltunski
private static ByteBuffer mBuf, mAtomicBuf;
89
private static IntBuffer mIntBuf, mAtomicIntBuf;
90 cee0369a Leszek Koltunski
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 c731c612 Leszek Koltunski
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
196 8777ce17 Leszek Koltunski
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
197 cee0369a Leszek Koltunski
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
198 c731c612 Leszek Koltunski
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
199 8777ce17 Leszek Koltunski
      }
200
201
    if( mAtomicCounter[0]<0 )
202
      {
203
      GLES31.glGenBuffers(1,mAtomicCounter,0);
204 c731c612 Leszek Koltunski
      GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[0]);
205 8777ce17 Leszek Koltunski
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
206
      GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
207
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
208
      }
209
210
    // BLIT DEPTH RENDER PROGRAM ///////////////////////////
211
    final InputStream blitDepthRenderVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
212
    final InputStream blitDepthRenderFragStream = resources.openRawResource(R.raw.blit_depth_render_fragment_shader);
213
214
    try
215
      {
216
      mBlitDepthRenderProgram = new DistortedProgram(blitDepthRenderVertStream,blitDepthRenderFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
217
      }
218
    catch(Exception e)
219
      {
220 cee0369a Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH RENDER program: "+e.getMessage());
221 8777ce17 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
222
      }
223
224
    int blitDepthRenderProgramH   = mBlitDepthRenderProgram.getProgramHandle();
225
    mBlitDepthRenderSizeH         = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
226 7170e4eb leszek
227 3fc9327a Leszek Koltunski
    // NORMAL PROGRAM //////////////////////////////////////
228
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
229
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
230 c90b9e01 Leszek Koltunski
231 f2367b75 Leszek Koltunski
    try
232
      {
233 3fc9327a Leszek Koltunski
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
234 f2367b75 Leszek Koltunski
      }
235
    catch(Exception e)
236
      {
237 cee0369a Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
238 f2367b75 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
239
      }
240 c90b9e01 Leszek Koltunski
241 3fc9327a Leszek Koltunski
    int normalProgramH = mNormalProgram.getProgramHandle();
242 e6519ac8 Leszek Koltunski
    mNormalMVPMatrixH  = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
243 55c14a19 Leszek Koltunski
    }
244
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247 421c2728 Leszek Koltunski
  private void initializeEffectLists(DistortedEffects d, int flags)
248 d425545a Leszek Koltunski
    {
249 015642fb Leszek Koltunski
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
250 6a06a912 Leszek Koltunski
      {
251 d425545a Leszek Koltunski
      mM = d.mM;
252
      matrixCloned = true;
253
      }
254
    else
255
      {
256 4e2382f3 Leszek Koltunski
      mM = new EffectQueueMatrix(mID);
257 d425545a Leszek Koltunski
      matrixCloned = false;
258
      }
259 6a06a912 Leszek Koltunski
    
260 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
261
      {
262
      mV = d.mV;
263
      vertexCloned = true;
264
      }
265
    else
266
      {
267 4e2382f3 Leszek Koltunski
      mV = new EffectQueueVertex(mID);
268 d425545a Leszek Koltunski
      vertexCloned = false;
269
      }
270 6a06a912 Leszek Koltunski
    
271 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
272
      {
273
      mF = d.mF;
274
      fragmentCloned = true;
275 6a06a912 Leszek Koltunski
      }
276 d425545a Leszek Koltunski
    else
277
      {
278 4e2382f3 Leszek Koltunski
      mF = new EffectQueueFragment(mID);
279 d425545a Leszek Koltunski
      fragmentCloned = false;
280
      }
281 80b3acf6 Leszek Koltunski
282
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
283
      {
284
      mP = d.mP;
285
      postprocessCloned = true;
286
      }
287
    else
288
      {
289
      mP = new EffectQueuePostprocess(mID);
290
      postprocessCloned = false;
291
      }
292
    }
293
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296 70b6a155 Leszek Koltunski
  EffectQueuePostprocess getPostprocess()
297 80b3acf6 Leszek Koltunski
    {
298 70b6a155 Leszek Koltunski
    return mP;
299 80b3acf6 Leszek Koltunski
    }
300
301 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
302
303
  void newNode(DistortedNode node)
304
    {
305
    mM.newNode(node);
306
    mF.newNode(node);
307
    mV.newNode(node);
308
    mP.newNode(node);
309
    }
310
311 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
312 c90b9e01 Leszek Koltunski
313 3fc9327a Leszek Koltunski
  private void displayNormals(MeshObject mesh)
314 c90b9e01 Leszek Koltunski
    {
315 e6519ac8 Leszek Koltunski
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
316
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
317 604b2899 Leszek Koltunski
    DistortedRenderState.switchOffDrawing();
318 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
319 604b2899 Leszek Koltunski
    DistortedRenderState.restoreDrawing();
320 e6519ac8 Leszek Koltunski
    GLES31.glEndTransformFeedback();
321
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
322 604b2899 Leszek Koltunski
323 3fc9327a Leszek Koltunski
    mNormalProgram.useProgram();
324 e6519ac8 Leszek Koltunski
    GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
325
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
326
    GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
327
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
328
    GLES31.glLineWidth(8.0f);
329
    GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
330 c90b9e01 Leszek Koltunski
    }
331 1aedf874 leszek
332 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
333
334 270c27bc Leszek Koltunski
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
335 d425545a Leszek Koltunski
    {
336 c1e24646 leszek
    float halfZ = halfW*mesh.zFactor;
337 638b5b5c leszek
338 8fbd0237 Leszek Koltunski
    mM.compute(currTime);
339
    mV.compute(currTime,halfW,halfH,halfZ);
340
    mF.compute(currTime,halfW,halfH);
341 1149be8f leszek
    mP.compute(currTime);
342 8fbd0237 Leszek Koltunski
343 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
344 d6e94c84 Leszek Koltunski
345 b9798977 leszek
    mMainProgram.useProgram();
346 e6519ac8 Leszek Koltunski
    GLES31.glUniform1i(mMainTextureH, 0);
347 12f45260 Leszek Koltunski
348 8777ce17 Leszek Koltunski
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
349
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
350
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
351
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
352
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
353 cab7c165 Leszek Koltunski
354 270c27bc Leszek Koltunski
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
355 8fbd0237 Leszek Koltunski
    mV.send();
356
    mF.send();
357 42571056 Leszek Koltunski
358 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
359 c90b9e01 Leszek Koltunski
360 3fc9327a Leszek Koltunski
    if( mesh.mShowNormals ) displayNormals(mesh);
361 d425545a Leszek Koltunski
    }
362 6a06a912 Leszek Koltunski
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364 7266d8ef Leszek Koltunski
/**
365
 * Only for use by the library itself.
366
 *
367
 * @y.exclude
368
 */
369
  public static void blitPriv(DistortedOutputSurface surface)
370 d425545a Leszek Koltunski
    {
371 c1e24646 leszek
    mBlitProgram.useProgram();
372
373 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
374
    GLES31.glUniform1i(mBlitTextureH, 0);
375
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
376
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
377
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
378 d425545a Leszek Koltunski
    }
379 7170e4eb leszek
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381
382 ae2802b1 Leszek Koltunski
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
383 7170e4eb leszek
    {
384
    mBlitDepthProgram.useProgram();
385
386 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
387 e029600f Leszek Koltunski
    GLES31.glUniform2f(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
388 e6519ac8 Leszek Koltunski
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
389
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
390 8777ce17 Leszek Koltunski
    }
391
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
// render all the transparent pixels from the per-pixel linked lists. This is in the 'merge
394
// postprocessing buckets' stage.
395
396
  static void blitDepthRenderPriv(DistortedOutputSurface surface, float corrW, float corrH)
397
    {
398
    mBlitDepthRenderProgram.useProgram();
399
400 e029600f Leszek Koltunski
    //analyzeBuffer(surface.mWidth, surface.mHeight);
401 cee0369a Leszek Koltunski
402 8777ce17 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
403 e029600f Leszek Koltunski
    GLES31.glUniform2f(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
404 8777ce17 Leszek Koltunski
    GLES31.glVertexAttribPointer(mBlitDepthRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
405
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
406
407
    // reset atomic counter to 0
408
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
409 c731c612 Leszek Koltunski
410
    mAtomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
411
                                                      GLES31.GL_MAP_READ_BIT|GLES31.GL_MAP_WRITE_BIT);
412
    if( mAtomicBuf!=null )
413
      {
414
      mAtomicIntBuf = mAtomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
415
416
      int counter = mAtomicIntBuf.get(0);
417
      mAtomicIntBuf.put(0, 0);
418
      //android.util.Log.e("counter", "now = "+counter+" w="+surface.mWidth+" h="+surface.mHeight
419
      //                             +" diff="+(counter-surface.mWidth*surface.mHeight));
420
      }
421
    else
422
      {
423
      android.util.Log.e("counter", "failed to map buffer");
424
      }
425
426
    GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
427 8777ce17 Leszek Koltunski
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
428 7170e4eb leszek
    }
429
430 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
431 cee0369a Leszek Koltunski
432
  private static void analyzeBuffer(int w, int h)
433
    {
434 c731c612 Leszek Koltunski
    int ptr, index;
435
    int errors = 0;
436 cee0369a Leszek Koltunski
437 c731c612 Leszek Koltunski
    GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
438
    mBuf = (ByteBuffer)GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, mBufferSize*4, GLES31.GL_MAP_READ_BIT);
439
    mIntBuf = mBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
440 cee0369a Leszek Koltunski
441 c731c612 Leszek Koltunski
    for(int col=0; col<w; col++)
442
      for(int row=0; row<h; row++)
443 cee0369a Leszek Koltunski
        {
444 c731c612 Leszek Koltunski
        index = col+row*w;
445
        ptr = mIntBuf.get(index);
446 cee0369a Leszek Koltunski
447
        if( ptr!=0 )
448
          {
449
          if( ptr>0 && ptr<mBufferSize )
450
            {
451
            ptr = mIntBuf.get(ptr);
452 c731c612 Leszek Koltunski
            if( ptr != index )
453 cee0369a Leszek Koltunski
              {
454 c731c612 Leszek Koltunski
              android.util.Log.d("surface", "col="+col+" row="+row+" val="+ptr+" expected: "+index);
455
              errors++;
456 cee0369a Leszek Koltunski
              }
457
            }
458
          else
459
            {
460 c731c612 Leszek Koltunski
            android.util.Log.d("surface", "overflow!");
461 cee0369a Leszek Koltunski
            }
462
          }
463
        }
464
465 c731c612 Leszek Koltunski
    GLES31.glUnmapBuffer(GLES31.GL_SHADER_STORAGE_BUFFER);
466
    GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
467
468
    if( errors>0 ) android.util.Log.e("surface", "errors: "+errors);
469 cee0369a Leszek Koltunski
    }
470
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472
473 8e34674e Leszek Koltunski
  private void releasePriv()
474 d425545a Leszek Koltunski
    {
475 80b3acf6 Leszek Koltunski
    if( !matrixCloned   )   mM.abortAll(false);
476
    if( !vertexCloned   )   mV.abortAll(false);
477
    if( !fragmentCloned )   mF.abortAll(false);
478
    if( !postprocessCloned) mP.abortAll(false);
479 d425545a Leszek Koltunski
480 4e2382f3 Leszek Koltunski
    mM = null;
481
    mV = null;
482
    mF = null;
483 80b3acf6 Leszek Koltunski
    mP = null;
484 8e34674e Leszek Koltunski
    }
485
486 1942537e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
487
488 7b8086eb Leszek Koltunski
  static void onDestroy()
489 1942537e Leszek Koltunski
    {
490 8777ce17 Leszek Koltunski
    mNextID           =  0;
491
    mLinkedListSSBO[0]= -1;
492
    mAtomicCounter[0] = -1;
493 1942537e Leszek Koltunski
    }
494
495 8e34674e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
496
// PUBLIC API
497 ada90d33 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
498 d425545a Leszek Koltunski
/**
499 4e2382f3 Leszek Koltunski
 * Create empty effect queue.
500 d425545a Leszek Koltunski
 */
501 421c2728 Leszek Koltunski
  public DistortedEffects()
502 d425545a Leszek Koltunski
    {
503 c7da4e65 leszek
    mID = ++mNextID;
504 4e2382f3 Leszek Koltunski
    initializeEffectLists(this,0);
505 d425545a Leszek Koltunski
    }
506 ada90d33 Leszek Koltunski
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508 d425545a Leszek Koltunski
/**
509 4e2382f3 Leszek Koltunski
 * Copy constructor.
510 d425545a Leszek Koltunski
 * <p>
511
 * Whatever we do not clone gets created just like in the default constructor.
512
 *
513
 * @param dc    Source object to create our object from
514
 * @param flags A bitmask of values specifying what to copy.
515 e6ab30eb Leszek Koltunski
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
516 d425545a Leszek Koltunski
 */
517 421c2728 Leszek Koltunski
  public DistortedEffects(DistortedEffects dc, int flags)
518 d425545a Leszek Koltunski
    {
519 c7da4e65 leszek
    mID = ++mNextID;
520 4e2382f3 Leszek Koltunski
    initializeEffectLists(dc,flags);
521 d425545a Leszek Koltunski
    }
522 ada90d33 Leszek Koltunski
523 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
524
/**
525 07305c87 Leszek Koltunski
 * Releases all resources. After this call, the queue should not be used anymore.
526 6a06a912 Leszek Koltunski
 */
527 13687207 leszek
  @SuppressWarnings("unused")
528 8e34674e Leszek Koltunski
  public synchronized void delete()
529 d425545a Leszek Koltunski
    {
530
    releasePriv();
531
    }
532 6a06a912 Leszek Koltunski
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534
/**
535 4e2382f3 Leszek Koltunski
 * Returns unique ID of this instance.
536
 *
537
 * @return ID of the object.
538 6a06a912 Leszek Koltunski
 */
539 4e2382f3 Leszek Koltunski
  public long getID()
540 d425545a Leszek Koltunski
      {
541 4e2382f3 Leszek Koltunski
      return mID;
542 d425545a Leszek Koltunski
      }
543 4e2382f3 Leszek Koltunski
544 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
545
/**
546
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
547 faa3ff56 Leszek Koltunski
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
548 6a06a912 Leszek Koltunski
 * 
549
 * @param el A class implementing the EffectListener interface that wants to get notifications.
550
 */
551 13687207 leszek
  @SuppressWarnings("unused")
552 3fc994b2 Leszek Koltunski
  public void registerForMessages(EffectListener el)
553 d425545a Leszek Koltunski
    {
554 26a4e5f6 leszek
    mM.registerForMessages(el);
555 3fc994b2 Leszek Koltunski
    mV.registerForMessages(el);
556
    mF.registerForMessages(el);
557 80b3acf6 Leszek Koltunski
    mP.registerForMessages(el);
558 d425545a Leszek Koltunski
    }
559 6a06a912 Leszek Koltunski
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561
/**
562 faa3ff56 Leszek Koltunski
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
563 6a06a912 Leszek Koltunski
 * 
564
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
565
 */
566 13687207 leszek
  @SuppressWarnings("unused")
567 3fc994b2 Leszek Koltunski
  public void deregisterForMessages(EffectListener el)
568 d425545a Leszek Koltunski
    {
569 26a4e5f6 leszek
    mM.deregisterForMessages(el);
570 3fc994b2 Leszek Koltunski
    mV.deregisterForMessages(el);
571
    mF.deregisterForMessages(el);
572 80b3acf6 Leszek Koltunski
    mP.deregisterForMessages(el);
573 d425545a Leszek Koltunski
    }
574 6a06a912 Leszek Koltunski
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576
/**
577 d07f2950 Leszek Koltunski
 * Aborts all Effects.
578
 * @return Number of effects aborted.
579 6a06a912 Leszek Koltunski
 */
580 d425545a Leszek Koltunski
  public int abortAllEffects()
581 13687207 leszek
    {
582
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
583
    }
584 6a06a912 Leszek Koltunski
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586
/**
587 d07f2950 Leszek Koltunski
 * Aborts all Effects of a given type, for example all MATRIX Effects.
588 6a06a912 Leszek Koltunski
 * 
589 da9b3f07 Leszek Koltunski
 * @param type one of the constants defined in {@link EffectType}
590 d07f2950 Leszek Koltunski
 * @return Number of effects aborted.
591 6a06a912 Leszek Koltunski
 */
592 da9b3f07 Leszek Koltunski
  public int abortByType(EffectType type)
593 d425545a Leszek Koltunski
    {
594
    switch(type)
595 6a06a912 Leszek Koltunski
      {
596 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.abortAll(true);
597
      case VERTEX     : return mV.abortAll(true);
598
      case FRAGMENT   : return mF.abortAll(true);
599 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.abortAll(true);
600 da9b3f07 Leszek Koltunski
      default         : return 0;
601 6a06a912 Leszek Koltunski
      }
602 d425545a Leszek Koltunski
    }
603 47316d20 leszek
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605
/**
606 faa3ff56 Leszek Koltunski
 * Aborts an Effect by its ID.
607 47316d20 leszek
 *
608
 * @param id the Id of the Effect to be removed, as returned by getID().
609
 * @return Number of effects aborted.
610
 */
611
  public int abortById(long id)
612
    {
613
    long type = id&EffectType.MASK;
614
615 2ef5dd9e leszek
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
616
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
617
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
618 80b3acf6 Leszek Koltunski
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
619 47316d20 leszek
620
    return 0;
621
    }
622
623 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
624
/**
625
 * Aborts a single Effect.
626
 * 
627 6bb59aad Leszek Koltunski
 * @param effect the Effect we want to abort.
628 476bbc81 Leszek Koltunski
 * @return number of Effects aborted. Always either 0 or 1.
629 6a06a912 Leszek Koltunski
 */
630 6bb59aad Leszek Koltunski
  public int abortEffect(Effect effect)
631 d425545a Leszek Koltunski
    {
632 6bb59aad Leszek Koltunski
    switch(effect.getType())
633
      {
634 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeEffect(effect);
635
      case VERTEX     : return mV.removeEffect(effect);
636
      case FRAGMENT   : return mF.removeEffect(effect);
637 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeEffect(effect);
638 da9b3f07 Leszek Koltunski
      default         : return 0;
639 6bb59aad Leszek Koltunski
      }
640 d425545a Leszek Koltunski
    }
641 6a06a912 Leszek Koltunski
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643
/**
644 e8c81a8e Leszek Koltunski
 * Abort all Effects of a given name, for example all rotations.
645 6a06a912 Leszek Koltunski
 * 
646 da9b3f07 Leszek Koltunski
 * @param name one of the constants defined in {@link EffectName}
647 476bbc81 Leszek Koltunski
 * @return number of Effects aborted.
648 6a06a912 Leszek Koltunski
 */
649 da9b3f07 Leszek Koltunski
  public int abortByName(EffectName name)
650 d425545a Leszek Koltunski
    {
651 da9b3f07 Leszek Koltunski
    switch(name.getType())
652 6a06a912 Leszek Koltunski
      {
653 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeByName(name);
654
      case VERTEX     : return mV.removeByName(name);
655
      case FRAGMENT   : return mF.removeByName(name);
656 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeByName(name);
657 6bb59aad Leszek Koltunski
      default                : return 0;
658 6a06a912 Leszek Koltunski
      }
659 d425545a Leszek Koltunski
    }
660 432442f9 Leszek Koltunski
661
///////////////////////////////////////////////////////////////////////////////////////////////////
662
/**
663 faa3ff56 Leszek Koltunski
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
664
 * single (InputSurface,MeshObject) combo.
665 432442f9 Leszek Koltunski
 *
666 fe6fe99a leszek
 * @param type {@link EffectType}
667
 * @return The maximum number of effects of a given type.
668 432442f9 Leszek Koltunski
 */
669 13687207 leszek
  @SuppressWarnings("unused")
670 fe6fe99a leszek
  public static int getMax(EffectType type)
671 432442f9 Leszek Koltunski
    {
672 fe6fe99a leszek
    return EffectQueue.getMax(type.ordinal());
673 432442f9 Leszek Koltunski
    }
674
675
///////////////////////////////////////////////////////////////////////////////////////////////////
676
/**
677 fe6fe99a leszek
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
678 432442f9 Leszek Koltunski
 * This can fail if:
679
 * <ul>
680
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
681
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
682
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
683
 *     time only decreasing the value of 'max' is permitted.
684
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
685
 * </ul>
686
 *
687 fe6fe99a leszek
 * @param type {@link EffectType}
688
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
689 80b3acf6 Leszek Koltunski
 *            than Byte.MAX_VALUE
690
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
691
 */
692
  @SuppressWarnings("unused")
693 fe6fe99a leszek
  public static boolean setMax(EffectType type, int max)
694 80b3acf6 Leszek Koltunski
    {
695 fe6fe99a leszek
    return EffectQueue.setMax(type.ordinal(),max);
696 80b3acf6 Leszek Koltunski
    }
697
698 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
699
/**
700 6bb59aad Leszek Koltunski
 * Add a new Effect to our queue.
701 f2fe7e28 Leszek Koltunski
 *
702 6bb59aad Leszek Koltunski
 * @param effect The Effect to add.
703 ae77d55e Leszek Koltunski
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
704 6a06a912 Leszek Koltunski
 */
705 ae77d55e Leszek Koltunski
  public boolean apply(Effect effect)
706 d425545a Leszek Koltunski
    {
707 6bb59aad Leszek Koltunski
    switch(effect.getType())
708
      {
709 26a4e5f6 leszek
      case MATRIX      : return mM.add(effect);
710 ae77d55e Leszek Koltunski
      case VERTEX      : return mV.add(effect);
711
      case FRAGMENT    : return mF.add(effect);
712 80b3acf6 Leszek Koltunski
      case POSTPROCESS : return mP.add(effect);
713 6bb59aad Leszek Koltunski
      }
714 ae77d55e Leszek Koltunski
715
    return false;
716 4fde55a0 Leszek Koltunski
    }
717 f2fe7e28 Leszek Koltunski
  }