Project

General

Profile

« Previous | Next » 

Revision 12f45260

Added by Leszek Koltunski over 6 years ago

First try at the SSBO (doesn't work - reads in the application don't pick up changes in the shader; crashes.

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;
56 57

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

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

  
282 284
    mMainProgram.useProgram();
283 285
    GLES31.glUniform1i(mMainTextureH, 0);
286
    GLES31.glUniform1i(mCountIndexH, surface.resetNewCounter() );
287

  
288
    android.util.Log.d("surface", "SurfaceID "+ surface.getID()+" transparent fragments: "+ surface.returnCounter() );
284 289

  
285 290
    if( Distorted.GLSL >= 300 )
286 291
      {
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 create()
42
  void createSurface()
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 delete()
138
  void deleteSurface()
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 recreate()
161
  void recreateSurface()
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;
27 29
import java.util.ArrayList;
28 30

  
29 31
///////////////////////////////////////////////////////////////////////////////////////////////////
......
96 98

  
97 99
  private int mDebugLevel;
98 100

  
101
  ////////////////////////////////////////////////////////////////////////////////
102
  // section dealing with Shader Storage Buffer Object (for counting transparency)
103
  private static final int BUFFERING = 3;
104
  private static int mBufferSize     =10;
105
  private static DistortedObjectCounter mSurfaceCounter = new DistortedObjectCounter();
106
  private static int[] mSSBO = new int[1];
107
  private static ByteBuffer mSSBOBuffer;
108

  
109
  static
110
    {
111
    mSSBO[0]    = -1;
112
    mSSBOBuffer = ByteBuffer.allocateDirect(BUFFERING*mBufferSize*4).order(ByteOrder.nativeOrder());
113
    }
114

  
115
  private int mSurfaceID;
116
  private int mLastIndex;
117

  
118
  // end section
119
  ////////////////////////////////////////////////////////////////////////////////
120

  
99 121
///////////////////////////////////////////////////////////////////////////////////////////////////
100 122

  
101 123
  abstract void prepareDebug(long time);
102 124
  abstract void renderDebug(long time);
125
  abstract void createSurface();
126
  abstract void deleteSurface();
127
  abstract void recreateSurface();
103 128

  
104 129
///////////////////////////////////////////////////////////////////////////////////////////////////
105 130

  
......
137 162
    createProjection();
138 163
    }
139 164

  
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
// Must be called from a thread holding OpenGL Context
167

  
168
  void create()
169
    {
170
    if( mSSBO[0]<0 )
171
      {
172
      GLES31.glGenBuffers(1,mSSBO,0);
173

  
174
      // here bind the new SSBO and map it
175
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
176
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, BUFFERING*mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
177
      mSSBOBuffer = (ByteBuffer) GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, BUFFERING*mBufferSize*4, GLES31.GL_MAP_WRITE_BIT | GLES31.GL_MAP_WRITE_BIT );
178
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,1, mSSBO[0]);
179
      }
180

  
181
    mSurfaceID = mSurfaceCounter.returnNext();
182

  
183
    if( mSurfaceID>=mBufferSize )
184
      {
185
      // increase the size of mSSBOBuffer, copy over old values, remap.
186
      // TODO (don't need this if there are never more than mBufferSize=10 Surfaces)
187
      }
188

  
189
    createSurface();
190
    }
191

  
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// Must be called from a thread holding OpenGL Context
194

  
195
  void delete()
196
    {
197
    mSurfaceCounter.release(mSurfaceID);
198
    deleteSurface();
199
    }
200

  
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

  
203
  void recreate()
204
    {
205
    mSSBO[0] = -1;
206
    mSurfaceCounter.releaseAll();
207
    recreateSurface();
208
    }
209

  
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

  
212
  int resetNewCounter()
213
    {
214
    //mSSBOBuffer.putInt(BUFFERING*mSurfaceID+mLastIndex,0);
215
    return BUFFERING*mSurfaceID + mLastIndex;
216
    }
217

  
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

  
220
  int returnOldCounter()
221
    {
222
    int ret = mSSBOBuffer.getInt(BUFFERING*mSurfaceID+mLastIndex);
223

  
224
    mLastIndex++;
225
    if( mLastIndex>=BUFFERING ) mLastIndex-=BUFFERING;
226

  
227
    return ret;
228
    }
229

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

  
232
  int returnCounter()
233
    {
234
    return mSSBOBuffer.getInt(0);
235
    }
236

  
140 237
///////////////////////////////////////////////////////////////////////////////////////////////////
141 238

  
142 239
  private void createProjection()
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 create()   {}
60
  void delete()   {}
61
  void recreate() {}
59
  void createSurface()   {}
60
  void deleteSurface()   {}
61
  void recreateSurface() {}
62 62

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

  
src/main/res/raw/main_fragment_shader.glsl
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 (std140,binding=1) buffer SSBO   // PER-SURFACE count of transparent fragments.
47
  {                                     // Can be buffered, i.e. if we are for example
48
  int count[];                          // triple-buffered, then surfaceID=N uses 3
49
  };                                    // consecutive counts (N,N+1,N+2) during 3
50
                                        // consecutive frames.
51
uniform int u_currentIndex;             // Index into the count[] array we are supposed to be using
52
                                        // during this very invocation.
53

  
46 54
//////////////////////////////////////////////////////////////////////////////////////////////
47 55

  
48 56
void main()                    		
......
65 73
    }
66 74
#endif
67 75

  
76
  count[0]=17;
77

  
68 78
  FRAG_COLOR = vec4(color.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, color.a);
69 79
  }

Also available in: Unified diff