Project

General

Profile

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

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

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