Project

General

Profile

« Previous | Next » 

Revision 2f35828c

Added by Leszek Koltunski over 6 years ago

- remove counting of transparent pixels from main fragment shader
- remove debugs and tidy up
- some fixes for the Mali GPU

View differences:

src/main/java/org/distorted/library/main/DistortedEffects.java
54 54
  /// MAIN PROGRAM ///
55 55
  private static DistortedProgram mMainProgram;
56 56
  private static int mMainTextureH;
57
  private static int mCountIndexH;
58 57

  
59 58
  /// BLIT PROGRAM ///
60 59
  private static DistortedProgram mBlitProgram;
......
122 121
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
123 122
    {
124 123
    // MAIN PROGRAM ////////////////////////////////////
125
    android.util.Log.e("Effects", "creating main program");
126

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

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

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

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

  
......
184 178
    mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
185 179

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

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

  
......
231 223
      }
232 224

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

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

  
......
253 243
    mBlitDepthRenderSizeH         = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
254 244

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

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

  
......
374 362

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

  
379 366
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
380 367
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
......
423 410
    GLES31.glUniform1f(mBlitDepthDepthH , 1.0f-surface.mNear);
424 411
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
425 412
    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 413
    }
431 414

  
432 415
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/main/DistortedFramebuffer.java
39 39
///////////////////////////////////////////////////////////////////////////////////////////////////
40 40
// Must be called from a thread holding OpenGL Context
41 41

  
42
  void createSurface()
42
  void create()
43 43
    {
44 44
    if( mColorCreated==NOT_CREATED_YET )
45 45
      {
......
135 135
///////////////////////////////////////////////////////////////////////////////////////////////////
136 136
// Must be called from a thread holding OpenGL Context
137 137

  
138
  void deleteSurface()
138
  void delete()
139 139
    {
140 140
    if( mColorH[0]>0 )
141 141
      {
......
158 158
///////////////////////////////////////////////////////////////////////////////////////////////////
159 159
// called from onDestroy(); mark OpenGL assets as 'not created'
160 160

  
161
  void recreateSurface()
161
  void recreate()
162 162
    {
163 163
    if( mColorCreated!=DONT_CREATE )
164 164
      {
src/main/java/org/distorted/library/main/DistortedObjectCounter.java
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 java.util.ArrayList;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Return unique IDs of objects in a lazy way. When we create a new Object, return the lowest unused
27
 * integer. When an objects gets deleted, its ID then gets marked unused. We do not try to 'collapse'
28
 * unused holes.
29
 */
30
class DistortedObjectCounter
31
  {
32
  private ArrayList<Integer> mUsed;
33
  private int mFirstUnused;
34

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

  
37
  DistortedObjectCounter()
38
    {
39
    mUsed        = new ArrayList<>();
40
    mFirstUnused = 0;
41
    }
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

  
45
  int returnNext()
46
    {
47
    int size = mUsed.size();
48

  
49
    if( mFirstUnused<size )
50
      {
51
      int ret = mFirstUnused;
52

  
53
      mUsed.set(mFirstUnused,1);
54

  
55
      int found=-1;
56

  
57
      for(int i=mFirstUnused+1; i<size; i++ )
58
        {
59
        if( mUsed.get(i)==0 )
60
          {
61
          found = i;
62
          break;
63
          }
64
        }
65

  
66
      mFirstUnused = found<0 ? size : found;
67

  
68
      return ret;
69
      }
70

  
71
    mUsed.add(1);
72
    mFirstUnused++;
73

  
74
    return size;
75
    }
76

  
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

  
79
  void release(int objID)
80
    {
81
    int size = mUsed.size();
82

  
83
    if( objID<size && objID>=0 && mUsed.get(objID)==1 )
84
      {
85
      mUsed.set(objID,0);
86
      if( objID<mFirstUnused ) mFirstUnused = objID;
87
      }
88
    }
89

  
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

  
92
  void releaseAll()
93
    {
94
    mUsed.clear();
95
    mFirstUnused = 0;
96
    }
97
  }
src/main/java/org/distorted/library/main/DistortedOutputSurface.java
24 24

  
25 25
import org.distorted.library.effect.EffectQuality;
26 26

  
27
import java.nio.ByteBuffer;
28
import java.nio.ByteOrder;
29
import java.nio.IntBuffer;
30 27
import java.util.ArrayList;
31 28

  
32 29
///////////////////////////////////////////////////////////////////////////////////////////////////
......
105 102
                    // mWidth,mHeight are the sizes of the Viewport, those -
106 103
                    // sizes of the backing up texture.
107 104

  
108
  ////////////////////////////////////////////////////////////////////////////////
109
  // section dealing with Shader Storage Buffer Object (for counting transparency)
110
  private static final int FRAME_DELAY = 3;
111
  private static int mBufferSize       =10;
112
  private static DistortedObjectCounter mSurfaceCounter = new DistortedObjectCounter();
113
  private static int[] mSSBO = new int[1];
114
  private static IntBuffer mIntBuffer;
115

  
116
  static
117
    {
118
    mSSBO[0]= -1;
119
    }
120

  
121
  private int mSurfaceID;
122
  private int mLastIndex;
123
  private int[] mLastValue = new int[FRAME_DELAY];
124
  private int mLastDiff = -1;
125

  
126
  private static final int RUNNING_AVERAGE = 10;
127
  private int[] mRunningAvg = new int[RUNNING_AVERAGE];
128
  private int mAvgIndex;
129
  private int mAvgSum;
130
  // end section
131
  ////////////////////////////////////////////////////////////////////////////////
132

  
133 105
///////////////////////////////////////////////////////////////////////////////////////////////////
134 106

  
135 107
  abstract void prepareDebug(long time);
136 108
  abstract void renderDebug(long time);
137
  abstract void createSurface();
138
  abstract void deleteSurface();
139
  abstract void recreateSurface();
140 109

  
141 110
///////////////////////////////////////////////////////////////////////////////////////////////////
142 111

  
......
175 144
    createProjection();
176 145
    }
177 146

  
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
// Must be called from a thread holding OpenGL Context
180

  
181
  void create()
182
    {
183
    if( mSSBO[0]<0 )
184
      {
185
      GLES31.glGenBuffers(1,mSSBO,0);
186

  
187
      // here bind the new SSBO and map it
188
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
189
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, FRAME_DELAY *mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
190
      ByteBuffer buf = (ByteBuffer) GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, FRAME_DELAY *mBufferSize*4, GLES31.GL_MAP_READ_BIT );
191
      mIntBuffer = buf.order(ByteOrder.nativeOrder()).asIntBuffer();
192
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,0, mSSBO[0]);
193
      }
194

  
195
    mSurfaceID = mSurfaceCounter.returnNext();
196

  
197
    if( mSurfaceID>=mBufferSize )
198
      {
199
      // increase size of the Buffer, copy over old values, remap.
200
      int[] tmp = new int[FRAME_DELAY *mBufferSize];
201
      for(int i = 0; i< FRAME_DELAY *mBufferSize; i++) tmp[i] = mIntBuffer.get(i);
202

  
203
      mBufferSize*= 2;
204

  
205
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 0, 0);
206
      GLES31.glUnmapBuffer(GLES31.GL_SHADER_STORAGE_BUFFER);
207
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER,0);
208

  
209
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
210
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, FRAME_DELAY *mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
211
      ByteBuffer buf = (ByteBuffer) GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, FRAME_DELAY *mBufferSize*4, GLES31.GL_MAP_READ_BIT );
212
      mIntBuffer = buf.order(ByteOrder.nativeOrder()).asIntBuffer();
213
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,0, mSSBO[0]);
214

  
215
      for(int i=0;i<tmp.length;i++) mIntBuffer.put(i,tmp[i]);
216
      }
217

  
218
    createSurface();
219
    }
220

  
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
// Must be called from a thread holding OpenGL Context
223

  
224
  void delete()
225
    {
226
    mSurfaceCounter.release(mSurfaceID);
227
    deleteSurface();
228
    }
229

  
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

  
232
  void recreate()
233
    {
234
    mSSBO[0]  = -1;
235
    mLastDiff = -1;
236

  
237
    for(int i = 0; i< FRAME_DELAY; i++) mLastValue[i] = 0;
238
    mSurfaceCounter.releaseAll();
239

  
240
    recreateSurface();
241
    }
242

  
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

  
245
  int getNewCounter()
246
    {
247
    return FRAME_DELAY*mSurfaceID + mLastIndex;
248
    }
249

  
250 147
///////////////////////////////////////////////////////////////////////////////////////////////////
251 148

  
252 149
  private void createProjection()
......
363 260
          surface.mRealWidth = maxw;
364 261
          surface.mRealHeight = maxh;
365 262

  
366
          surface.recreateSurface();
367
          surface.createSurface();
263
          surface.recreate();
264
          surface.create();
368 265
          }
369 266
        }
370 267
      }
......
634 531
      GLES31.glClearStencil(mClearStencil);
635 532
      GLES31.glClear(mClear);
636 533
      DistortedRenderState.colorDepthStencilRestore();
637
/*
638
      if( mSSBO[0]>=0 )
639
        {
640
        // yes, this DOES keep on working when 'value' overflows into negative territory.
641
        int value = mIntBuffer.get(FRAME_DELAY*mSurfaceID+mLastIndex);
642

  
643
        if( value-mLastValue[mLastIndex]!=mLastDiff )
644
          {
645
          android.util.Log.d("surface", "id " + mSurfaceID +
646
                             (mType == TYPE_USER ? " USER" : (mType == TYPE_SYST ? " SYST" : " TREE")) +
647
                             " viewport: (" + mWidth + "x" + mHeight + ") last frame: " + (value - mLastValue[mLastIndex])
648
                             + " avg: " + (mAvgSum/RUNNING_AVERAGE)
649
                            );
650
          }
651

  
652
        mLastDiff = value-mLastValue[mLastIndex];
653
        mLastValue[mLastIndex] = value;
654

  
655
        mAvgSum += (mLastDiff-mRunningAvg[mAvgIndex]);
656
        mRunningAvg[mAvgIndex] = mLastDiff;
657
        if( ++mAvgIndex>=RUNNING_AVERAGE ) mAvgIndex =0;
658
        }
659

  
660
      if( ++mLastIndex >= FRAME_DELAY ) mLastIndex=0;
661
*/
662 534
      }
663 535
    }
664 536

  
src/main/java/org/distorted/library/main/DistortedScreen.java
56 56
///////////////////////////////////////////////////////////////////////////////////////////////////
57 57
// here we don't manage underlying OpenGL assets ourselves
58 58

  
59
  void createSurface()   {}
60
  void deleteSurface()   {}
61
  void recreateSurface() {}
59
  void create()   {}
60
  void delete()   {}
61
  void recreate() {}
62 62

  
63 63
///////////////////////////////////////////////////////////////////////////////////////////////////
64 64

  
src/main/res/raw/main_fragment_shader.glsl
17 17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18 18
//////////////////////////////////////////////////////////////////////////////////////////////
19 19

  
20
precision lowp float;
20
precision mediump float;
21 21

  
22 22
#if __VERSION__ != 100
23 23
in vec3 v_Position;                     // Interpolated position for this fragment.
......
43 43
                                        // next describes the Region, i.e. area over which the effect is active.
44 44
#endif    // NUM_FRAGMENT>0
45 45

  
46
layout (std430,binding=0) buffer SSBO   // PER-SURFACE count of transparent fragments.
47
  {                                     // Can be buffered, i.e. if we are for example
48
  int ssbocount[];                      // triple-buffered, then surfaceID=N uses 3
49
  };                                    // consecutive counts (N,N+1,N+2) during 3
50
                                        // consecutive frames.
51
                                        // Cannot be an 'uint' because the Java application that's
52
                                        // reading this does not have unsigned types so it's reading
53
                                        // this into a signed int... Doesn't matter as things keep on
54
                                        // working even when this overflows into negative territory :)
55

  
56
uniform int u_currentIndex;             // Index into the count[] array we are supposed to be using
57
                                        // during this very invocation.
58

  
59 46
//////////////////////////////////////////////////////////////////////////////////////////////
60 47

  
61 48
void main()                    		
......
78 65
    }
79 66
#endif
80 67

  
81
  if( color.a < 1.0 && color.a > 0.0 ) atomicAdd(ssbocount[u_currentIndex],1);
82

  
83 68
  FRAG_COLOR = vec4(color.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, color.a);
84 69
  }
src/main/res/raw/main_vertex_shader.glsl
17 17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18 18
//////////////////////////////////////////////////////////////////////////////////////////////
19 19

  
20
precision lowp float;
20
precision mediump float;
21 21

  
22 22
#if __VERSION__ != 100
23 23
in vec3 a_Position;                  // Per-vertex position.

Also available in: Unified diff