Project

General

Profile

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

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

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.library.main;
21

    
22
import android.content.res.Resources;
23
import android.opengl.GLES31;
24
import android.util.Log;
25

    
26
import org.distorted.library.R;
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.effect.EffectType;
30
import org.distorted.library.effect.FragmentEffect;
31
import org.distorted.library.effect.VertexEffect;
32
import org.distorted.library.message.EffectListener;
33
import org.distorted.library.program.DistortedProgram;
34
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

    
40
import java.io.InputStream;
41
import java.nio.ByteBuffer;
42
import java.nio.ByteOrder;
43
import java.nio.FloatBuffer;
44
import java.nio.IntBuffer;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
/**
48
 * Class containing Matrix, Vertex, Fragment and Postprocessing effect queues.
49
 * <p>
50
 * The queues hold actual effects to be applied to a given (InputSurface,MeshObject) combo.
51
 */
52
public class DistortedEffects
53
  {
54
  /// MAIN PROGRAM ///
55
  private static DistortedProgram mMainProgram;
56
  private static int mMainTextureH;
57
  private static int mCountIndexH;
58

    
59
  /// BLIT PROGRAM ///
60
  private static DistortedProgram mBlitProgram;
61
  private static int mBlitTextureH;
62
  private static int mBlitDepthH;
63
  private static final FloatBuffer mQuadPositions;
64

    
65
  static
66
    {
67
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
68
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
69
    mQuadPositions.put(positionData).position(0);
70
    }
71

    
72
  /// BLIT DEPTH PROGRAM ///
73
  private static DistortedProgram mBlitDepthProgram;
74
  private static int mBlitDepthTextureH;
75
  private static int mBlitDepthDepthTextureH;
76
  private static int mBlitDepthDepthH;
77
  private static int mBlitDepthTexCorrH;
78
  private static int mBlitDepthSizeH;
79
  private static int mBlitDepthNumRecordsH;
80

    
81
  private static int[] mLinkedListSSBO = new int[1];
82
  private static int[] mAtomicCounter = new int[1];
83

    
84
  static
85
    {
86
    mLinkedListSSBO[0]= -1;
87
    mAtomicCounter[0] = -1;
88
    }
89

    
90
  private static int mBufferSize=(0x1<<23);  // 8 million entries
91

    
92
  private static IntBuffer mIntBuffer;
93

    
94
private static ByteBuffer mBuf;
95
private static IntBuffer mIntBuf;
96

    
97
  /// BLIT DEPTH RENDER PROGRAM ///
98
  private static DistortedProgram mBlitDepthRenderProgram;
99
  private static int mBlitDepthRenderDepthTextureH;
100
  private static int mBlitDepthRenderDepthH;
101
  private static int mBlitDepthRenderTexCorrH;
102
  private static int mBlitDepthRenderSizeH;
103

    
104
  /// NORMAL PROGRAM /////
105
  private static DistortedProgram mNormalProgram;
106
  private static int mNormalMVPMatrixH;
107
  /// END PROGRAMS //////
108

    
109
  private static long mNextID =0;
110
  private long mID;
111

    
112
  private EffectQueueMatrix mM;
113
  private EffectQueueFragment mF;
114
  private EffectQueueVertex mV;
115
  private EffectQueuePostprocess mP;
116

    
117
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  static void createProgram(Resources resources)
122
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
123
    {
124
    // MAIN PROGRAM ////////////////////////////////////
125
    android.util.Log.e("Effects", "creating main program");
126

    
127
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
128
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
129

    
130
    int numF = FragmentEffect.getNumEnabled();
131
    int numV = VertexEffect.getNumEnabled();
132

    
133
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
134
    String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
135
    String enabledEffectV= VertexEffect.getGLSL();
136
    String enabledEffectF= FragmentEffect.getGLSL();
137

    
138
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
139
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
140
    //android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
141
    //android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
142

    
143
    String[] feedback = { "v_Position", "v_endPosition" };
144

    
145
    try
146
      {
147
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
148
                                          enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
149
      }
150
    catch(Exception e)
151
      {
152
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
153
      throw new RuntimeException(e.getMessage());
154
      }
155

    
156
    int mainProgramH = mMainProgram.getProgramHandle();
157
    EffectQueueFragment.getUniforms(mainProgramH);
158
    EffectQueueVertex.getUniforms(mainProgramH);
159
    EffectQueueMatrix.getUniforms(mainProgramH);
160
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
161
    mCountIndexH = GLES31.glGetUniformLocation( mainProgramH, "u_currentIndex");
162

    
163
    // BLIT PROGRAM ////////////////////////////////////
164
    android.util.Log.e("Effects", "creating blit program");
165

    
166
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
167
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
168

    
169
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
170
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
171

    
172
    try
173
      {
174
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
175
      }
176
    catch(Exception e)
177
      {
178
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
179
      throw new RuntimeException(e.getMessage());
180
      }
181

    
182
    int blitProgramH = mBlitProgram.getProgramHandle();
183
    mBlitTextureH  = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
184
    mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
185

    
186
    // BLIT DEPTH PROGRAM ////////////////////////////////////
187
    android.util.Log.e("Effects", "creating blit depth program");
188

    
189
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
190
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
191

    
192
    try
193
      {
194
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
195
      }
196
    catch(Exception e)
197
      {
198
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
199
      throw new RuntimeException(e.getMessage());
200
      }
201

    
202
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
203
    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
204
    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
205
    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
206
    mBlitDepthTexCorrH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
207
    mBlitDepthSizeH         = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Size");
208
    mBlitDepthNumRecordsH   = GLES31.glGetUniformLocation( blitDepthProgramH, "u_numRecords");
209

    
210
    mIntBuffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
211
    mIntBuffer.put(0,0);
212

    
213
    if( mLinkedListSSBO[0]<0 )
214
      {
215
      GLES31.glGenBuffers(1,mLinkedListSSBO,0);
216
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
217
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
218

    
219
      mBuf = (ByteBuffer)GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, mBufferSize*4, GLES31.GL_MAP_READ_BIT);
220
      mIntBuf = mBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
221

    
222
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
223
      }
224

    
225
    if( mAtomicCounter[0]<0 )
226
      {
227
      GLES31.glGenBuffers(1,mAtomicCounter,0);
228
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
229
      GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
230
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
231
      }
232

    
233
    // BLIT DEPTH RENDER PROGRAM ///////////////////////////
234
    android.util.Log.e("Effects", "creating blit depth render program");
235

    
236
    final InputStream blitDepthRenderVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
237
    final InputStream blitDepthRenderFragStream = resources.openRawResource(R.raw.blit_depth_render_fragment_shader);
238

    
239
    try
240
      {
241
      mBlitDepthRenderProgram = new DistortedProgram(blitDepthRenderVertStream,blitDepthRenderFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
242
      }
243
    catch(Exception e)
244
      {
245
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH RENDER program: "+e.getMessage());
246
      throw new RuntimeException(e.getMessage());
247
      }
248

    
249
    int blitDepthRenderProgramH   = mBlitDepthRenderProgram.getProgramHandle();
250
    mBlitDepthRenderDepthTextureH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_DepthTexture");
251
    mBlitDepthRenderDepthH        = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Depth");
252
    mBlitDepthRenderTexCorrH      = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_TexCorr");
253
    mBlitDepthRenderSizeH         = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
254

    
255
    // NORMAL PROGRAM //////////////////////////////////////
256
    android.util.Log.e("Effects", "creating normal program");
257

    
258
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
259
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
260

    
261
    try
262
      {
263
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
264
      }
265
    catch(Exception e)
266
      {
267
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
268
      throw new RuntimeException(e.getMessage());
269
      }
270

    
271
    int normalProgramH = mNormalProgram.getProgramHandle();
272
    mNormalMVPMatrixH  = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  private void initializeEffectLists(DistortedEffects d, int flags)
278
    {
279
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
280
      {
281
      mM = d.mM;
282
      matrixCloned = true;
283
      }
284
    else
285
      {
286
      mM = new EffectQueueMatrix(mID);
287
      matrixCloned = false;
288
      }
289
    
290
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
291
      {
292
      mV = d.mV;
293
      vertexCloned = true;
294
      }
295
    else
296
      {
297
      mV = new EffectQueueVertex(mID);
298
      vertexCloned = false;
299
      }
300
    
301
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
302
      {
303
      mF = d.mF;
304
      fragmentCloned = true;
305
      }
306
    else
307
      {
308
      mF = new EffectQueueFragment(mID);
309
      fragmentCloned = false;
310
      }
311

    
312
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
313
      {
314
      mP = d.mP;
315
      postprocessCloned = true;
316
      }
317
    else
318
      {
319
      mP = new EffectQueuePostprocess(mID);
320
      postprocessCloned = false;
321
      }
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  EffectQueuePostprocess getPostprocess()
327
    {
328
    return mP;
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
  void newNode(DistortedNode node)
334
    {
335
    mM.newNode(node);
336
    mF.newNode(node);
337
    mV.newNode(node);
338
    mP.newNode(node);
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  private void displayNormals(MeshObject mesh)
344
    {
345
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
346
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
347
    DistortedRenderState.switchOffDrawing();
348
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
349
    DistortedRenderState.restoreDrawing();
350
    GLES31.glEndTransformFeedback();
351
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
352

    
353
    mNormalProgram.useProgram();
354
    GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
355
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
356
    GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
357
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
358
    GLES31.glLineWidth(8.0f);
359
    GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
365
    {
366
    float halfZ = halfW*mesh.zFactor;
367

    
368
    mM.compute(currTime);
369
    mV.compute(currTime,halfW,halfH,halfZ);
370
    mF.compute(currTime,halfW,halfH);
371
    mP.compute(currTime);
372

    
373
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
374

    
375
    mMainProgram.useProgram();
376
    GLES31.glUniform1i(mMainTextureH, 0);
377
    GLES31.glUniform1i(mCountIndexH, surface.getNewCounter() );
378

    
379
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
380
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
381
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
382
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
383
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
384

    
385
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
386
    mV.send();
387
    mF.send();
388

    
389
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
390

    
391
    if( mesh.mShowNormals ) displayNormals(mesh);
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
/**
396
 * Only for use by the library itself.
397
 *
398
 * @y.exclude
399
 */
400
  public static void blitPriv(DistortedOutputSurface surface)
401
    {
402
    mBlitProgram.useProgram();
403

    
404
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
405
    GLES31.glUniform1i(mBlitTextureH, 0);
406
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
407
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
408
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

    
413
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
414
    {
415
    mBlitDepthProgram.useProgram();
416

    
417
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
418
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
419
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
420
    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
421
    GLES31.glUniform2i(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
422
    GLES31.glUniform1ui(mBlitDepthNumRecordsH, (mBufferSize-surface.mWidth*surface.mHeight)/3 );  // see the fragment shader
423
    GLES31.glUniform1f(mBlitDepthDepthH , 1.0f-surface.mNear);
424
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
425
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
426

    
427

    
428
    //android.util.Log.e("effects", "width="+surface.mWidth+" height="+surface.mHeight+
429
    //    " bufferSize: "+mBufferSize+" numRecords: "+((mBufferSize-surface.mWidth*surface.mHeight)/3) );
430
    }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433
// render all the transparent pixels from the per-pixel linked lists. This is in the 'merge
434
// postprocessing buckets' stage.
435

    
436
  static void blitDepthRenderPriv(DistortedOutputSurface surface, float corrW, float corrH)
437
    {
438
    mBlitDepthRenderProgram.useProgram();
439

    
440
    analyzeBuffer(surface.mWidth, surface.mHeight);
441

    
442
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
443
    GLES31.glUniform1i(mBlitDepthRenderDepthTextureH, 1);
444
    GLES31.glUniform2f(mBlitDepthRenderTexCorrH, corrW, corrH );
445
    GLES31.glUniform2i(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
446
    GLES31.glUniform1f( mBlitDepthRenderDepthH , 1.0f-surface.mNear);
447
    GLES31.glVertexAttribPointer(mBlitDepthRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
448
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
449

    
450
    // reset atomic counter to 0
451
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
452
    GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
453
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
454
    }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458

    
459
  private static void analyzeBuffer(int w, int h)
460
    {
461
    int ptr;
462
    final int GREEN = (255<<16) + 255;
463
    final int RED   = (255<<24) + 255;
464

    
465
    int left1=0, left2=0;
466
    int right1=0, right2=0;
467
    int overflow = 0;
468

    
469
    for(int row=0; row<h; row++)
470
      for(int col=0; col<w; col++)
471
        {
472
        ptr = mIntBuf.get( col+row*w );
473

    
474
        if( ptr!=0 )
475
          {
476
          if( ptr>0 && ptr<mBufferSize )
477
            {
478
            ptr = mIntBuf.get(ptr);
479

    
480
            if ((2*col>w) && ptr != RED)
481
              {
482
              if (ptr==GREEN) right1++;
483
              else            right2++;
484
              }
485
            if ((2*col<=w) && ptr != GREEN)
486
              {
487
              if (ptr==RED) left1++;
488
              else          left2++;
489
              }
490
            }
491
          else
492
            {
493
            overflow++;
494
            }
495
          }
496
        }
497

    
498
    android.util.Log.e("surface", " left1: "+left1+" left2: "+left2+" right1: "+right1+" right2: "+right2+" overflow: "+overflow);
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

    
503
  private void releasePriv()
504
    {
505
    if( !matrixCloned   )   mM.abortAll(false);
506
    if( !vertexCloned   )   mV.abortAll(false);
507
    if( !fragmentCloned )   mF.abortAll(false);
508
    if( !postprocessCloned) mP.abortAll(false);
509

    
510
    mM = null;
511
    mV = null;
512
    mF = null;
513
    mP = null;
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  static void onDestroy()
519
    {
520
    mNextID           =  0;
521
    mLinkedListSSBO[0]= -1;
522
    mAtomicCounter[0] = -1;
523
    }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526
// PUBLIC API
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528
/**
529
 * Create empty effect queue.
530
 */
531
  public DistortedEffects()
532
    {
533
    mID = ++mNextID;
534
    initializeEffectLists(this,0);
535
    }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538
/**
539
 * Copy constructor.
540
 * <p>
541
 * Whatever we do not clone gets created just like in the default constructor.
542
 *
543
 * @param dc    Source object to create our object from
544
 * @param flags A bitmask of values specifying what to copy.
545
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
546
 */
547
  public DistortedEffects(DistortedEffects dc, int flags)
548
    {
549
    mID = ++mNextID;
550
    initializeEffectLists(dc,flags);
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554
/**
555
 * Releases all resources. After this call, the queue should not be used anymore.
556
 */
557
  @SuppressWarnings("unused")
558
  public synchronized void delete()
559
    {
560
    releasePriv();
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564
/**
565
 * Returns unique ID of this instance.
566
 *
567
 * @return ID of the object.
568
 */
569
  public long getID()
570
      {
571
      return mID;
572
      }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575
/**
576
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
577
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
578
 * 
579
 * @param el A class implementing the EffectListener interface that wants to get notifications.
580
 */
581
  @SuppressWarnings("unused")
582
  public void registerForMessages(EffectListener el)
583
    {
584
    mM.registerForMessages(el);
585
    mV.registerForMessages(el);
586
    mF.registerForMessages(el);
587
    mP.registerForMessages(el);
588
    }
589

    
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591
/**
592
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
593
 * 
594
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
595
 */
596
  @SuppressWarnings("unused")
597
  public void deregisterForMessages(EffectListener el)
598
    {
599
    mM.deregisterForMessages(el);
600
    mV.deregisterForMessages(el);
601
    mF.deregisterForMessages(el);
602
    mP.deregisterForMessages(el);
603
    }
604

    
605
///////////////////////////////////////////////////////////////////////////////////////////////////
606
/**
607
 * Aborts all Effects.
608
 * @return Number of effects aborted.
609
 */
610
  public int abortAllEffects()
611
    {
612
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
613
    }
614

    
615
///////////////////////////////////////////////////////////////////////////////////////////////////
616
/**
617
 * Aborts all Effects of a given type, for example all MATRIX Effects.
618
 * 
619
 * @param type one of the constants defined in {@link EffectType}
620
 * @return Number of effects aborted.
621
 */
622
  public int abortByType(EffectType type)
623
    {
624
    switch(type)
625
      {
626
      case MATRIX     : return mM.abortAll(true);
627
      case VERTEX     : return mV.abortAll(true);
628
      case FRAGMENT   : return mF.abortAll(true);
629
      case POSTPROCESS: return mP.abortAll(true);
630
      default         : return 0;
631
      }
632
    }
633

    
634
///////////////////////////////////////////////////////////////////////////////////////////////////
635
/**
636
 * Aborts an Effect by its ID.
637
 *
638
 * @param id the Id of the Effect to be removed, as returned by getID().
639
 * @return Number of effects aborted.
640
 */
641
  public int abortById(long id)
642
    {
643
    long type = id&EffectType.MASK;
644

    
645
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
646
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
647
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
648
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
649

    
650
    return 0;
651
    }
652

    
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654
/**
655
 * Aborts a single Effect.
656
 * 
657
 * @param effect the Effect we want to abort.
658
 * @return number of Effects aborted. Always either 0 or 1.
659
 */
660
  public int abortEffect(Effect effect)
661
    {
662
    switch(effect.getType())
663
      {
664
      case MATRIX     : return mM.removeEffect(effect);
665
      case VERTEX     : return mV.removeEffect(effect);
666
      case FRAGMENT   : return mF.removeEffect(effect);
667
      case POSTPROCESS: return mP.removeEffect(effect);
668
      default         : return 0;
669
      }
670
    }
671

    
672
///////////////////////////////////////////////////////////////////////////////////////////////////
673
/**
674
 * Abort all Effects of a given name, for example all rotations.
675
 * 
676
 * @param name one of the constants defined in {@link EffectName}
677
 * @return number of Effects aborted.
678
 */
679
  public int abortByName(EffectName name)
680
    {
681
    switch(name.getType())
682
      {
683
      case MATRIX     : return mM.removeByName(name);
684
      case VERTEX     : return mV.removeByName(name);
685
      case FRAGMENT   : return mF.removeByName(name);
686
      case POSTPROCESS: return mP.removeByName(name);
687
      default                : return 0;
688
      }
689
    }
690

    
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692
/**
693
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
694
 * single (InputSurface,MeshObject) combo.
695
 *
696
 * @param type {@link EffectType}
697
 * @return The maximum number of effects of a given type.
698
 */
699
  @SuppressWarnings("unused")
700
  public static int getMax(EffectType type)
701
    {
702
    return EffectQueue.getMax(type.ordinal());
703
    }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706
/**
707
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
708
 * This can fail if:
709
 * <ul>
710
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
711
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
712
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
713
 *     time only decreasing the value of 'max' is permitted.
714
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
715
 * </ul>
716
 *
717
 * @param type {@link EffectType}
718
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
719
 *            than Byte.MAX_VALUE
720
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
721
 */
722
  @SuppressWarnings("unused")
723
  public static boolean setMax(EffectType type, int max)
724
    {
725
    return EffectQueue.setMax(type.ordinal(),max);
726
    }
727

    
728
///////////////////////////////////////////////////////////////////////////////////////////////////
729
/**
730
 * Add a new Effect to our queue.
731
 *
732
 * @param effect The Effect to add.
733
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
734
 */
735
  public boolean apply(Effect effect)
736
    {
737
    switch(effect.getType())
738
      {
739
      case MATRIX      : return mM.add(effect);
740
      case VERTEX      : return mV.add(effect);
741
      case FRAGMENT    : return mF.add(effect);
742
      case POSTPROCESS : return mP.add(effect);
743
      }
744

    
745
    return false;
746
    }
747
  }
(2-2/22)