Project

General

Profile

« Previous | Next » 

Revision 341151fc

Added by Leszek Koltunski over 6 years ago

Fixes ported from the 'OIT' branch:

- remove counting of transparent pixels
- various fixes for the Mali GPU

View differences:

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

  
58 57
  /// BLIT PROGRAM ///
59 58
  private static DistortedProgram mBlitProgram;
......
122 121
    EffectQueueVertex.getUniforms(mainProgramH);
123 122
    EffectQueueMatrix.getUniforms(mainProgramH);
124 123
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
125
    mCountIndexH = GLES31.glGetUniformLocation( mainProgramH, "u_currentIndex");
126 124

  
127 125
    // BLIT PROGRAM ////////////////////////////////////
128 126
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
......
285 283

  
286 284
    mMainProgram.useProgram();
287 285
    GLES31.glUniform1i(mMainTextureH, 0);
288
    GLES31.glUniform1i(mCountIndexH, surface.getNewCounter() );
289 286

  
290 287
    if( Distorted.GLSL >= 300 )
291 288
      {
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/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
      }
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/java/org/distorted/library/program/DistortedProgram.java
292 292
      final int[] compileStatus = new int[1];
293 293
      GLES31.glGetShaderiv(shaderHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
294 294

  
295
      if (compileStatus[0] != GLES31.GL_TRUE )
295
      if (compileStatus[0] != GLES31.GL_TRUE)
296 296
        {
297
        GLES31.glDeleteShader(shaderHandle);
298
        shaderHandle = 0;
299
        }
300
      }
297
        String error = GLES31.glGetShaderInfoLog(shaderHandle);
301 298

  
302
    if (shaderHandle == 0)
303
      {
304
      String error = GLES31.glGetShaderInfoLog(shaderHandle);
299
        //android.util.Log.e("Program", "error compiling :" + error);
305 300

  
306
      //android.util.Log.e("Program", "error compiling :"+error);
301
        GLES31.glDeleteShader(shaderHandle);
307 302

  
308
      switch(shaderType)
309
        {
310
        case GLES31.GL_VERTEX_SHADER  : throw new VertexCompilationException(error);
311
        case GLES31.GL_FRAGMENT_SHADER: throw new FragmentCompilationException(error);
312
        default                       : throw new RuntimeException(error);
303
        switch (shaderType)
304
          {
305
          case GLES31.GL_VERTEX_SHADER:   throw new VertexCompilationException(error);
306
          case GLES31.GL_FRAGMENT_SHADER: throw new FragmentCompilationException(error);
307
          default:                        throw new RuntimeException(error);
308
          }
313 309
        }
314 310
      }
315 311

  
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 highp 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 highp float;
21 21

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

Also available in: Unified diff