Project

General

Profile

« Previous | Next » 

Revision 89de975c

Added by Leszek Koltunski over 7 years ago

Add possibility to create FBOs with combined DEPTH/STENCIL.

View differences:

src/main/java/org/distorted/library/DistortedFramebuffer.java
33 33

  
34 34
///////////////////////////////////////////////////////////////////////////////////////////////////
35 35
// Must be called from a thread holding OpenGL Context
36
// Watch out - this has the side-effect of binding a Texture and a Framebuffer!
37 36

  
38 37
  void create()
39 38
    {
......
46 45
      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
47 46
      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
48 47
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mWidth, mHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
48
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
49 49

  
50 50
      GLES30.glGenFramebuffers(1, mFBOH, 0);
51 51
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
52 52
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
53
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
53 54

  
54 55
      mColorCreated = checkStatus("color");
55 56
      }
56
    if( mDepthCreated==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
57
    if( mDepthStencilCreated==NOT_CREATED_YET ) // we need to create a new DEPTH or STENCIL attachment
57 58
      {
58
      GLES30.glGenTextures(1, mDepthH, 0);
59
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
59
      GLES30.glGenTextures(1, mDepthStencilH, 0);
60
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]);
60 61
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
61 62
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
62 63
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
63 64
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
64
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
65 65

  
66
      if( mDepthStencil==DEPTH_NO_STENCIL )
67
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
68
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
69
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH24_STENCIL8, mWidth, mHeight, 0, GLES30.GL_DEPTH_STENCIL, GLES30.GL_UNSIGNED_SHORT, null);
70

  
71
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
66 72
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
67
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthH[0], 0);
68 73

  
69
      mDepthCreated = checkStatus("depth");
74
      if( mDepthStencil==DEPTH_NO_STENCIL )
75
        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[0], 0);
76
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
77
        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_STENCIL_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[0], 0);
78

  
79
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
80

  
81
      mDepthStencilCreated = checkStatus("depth");
70 82
      }
71
    if( mDepthCreated==DONT_CREATE && mDepthH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
83
    if( mDepthStencilCreated==DONT_CREATE && mDepthStencilH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
72 84
      {
73
      GLES30.glDeleteTextures(1, mDepthH, 0);
74
      mDepthH[0]=0;
85
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
86
      mDepthStencilH[0]=0;
75 87
      }
76 88
    }
77 89

  
......
86 98
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
87 99

  
88 100
      GLES30.glDeleteTextures(1, mColorH, 0);
89
      GLES30.glDeleteTextures(1, mDepthH, 0);
101
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
90 102
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
91 103
      mFBOH[0]= 0;
92 104

  
......
103 115
    {
104 116
    if( mColorH[0]>0 )
105 117
      {
106
      if( mDepthH[0]>0 )
118
      if( mDepthStencilH[0]>0 )
107 119
        {
108
        GLES30.glDeleteTextures(1, mDepthH, 0);
109
        mDepthH[0]=0;
110
        mDepthCreated = NOT_CREATED_YET;
120
        GLES30.glDeleteTextures(1, mDepthStencilH, 0);
121
        mDepthStencilH[0]=0;
122
        mDepthStencilCreated = NOT_CREATED_YET;
111 123
        }
112 124

  
113 125
      GLES30.glDeleteTextures(1, mColorH, 0);
......
129 141
      mColorCreated = NOT_CREATED_YET;
130 142
      mColorH[0] = 0;
131 143
      }
132
    if( mDepthCreated!=DONT_CREATE )
144
    if( mDepthStencilCreated!=DONT_CREATE )
133 145
      {
134
      mDepthCreated = NOT_CREATED_YET;
135
      mDepthH[0] = 0;
146
      mDepthStencilCreated = NOT_CREATED_YET;
147
      mDepthStencilH[0] = 0;
136 148
      }
137 149
    }
138 150

  
......
142 154
// inside a Tree of DistortedNodes (TREE)
143 155
// SYSTEM surfaces do not get removed in onDestroy().
144 156

  
145
  DistortedFramebuffer(boolean depthEnabled, int type, int width, int height)
157
  DistortedFramebuffer(int depthStencil, int type, int width, int height)
146 158
    {
147
    super(width,height,NOT_CREATED_YET, (depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET, type);
159
    super(width,height,NOT_CREATED_YET,depthStencil,NOT_CREATED_YET, type);
148 160
    }
149 161

  
150 162
///////////////////////////////////////////////////////////////////////////////////////////////////
......
152 164

  
153 165
  boolean setAsDepth()
154 166
    {
155
    if( mDepthH[0]>0 )
167
    if( mDepthStencilH[0]>0 )
156 168
      {
157 169
      GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
158
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
170
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]);
159 171
      return true;
160 172
      }
161 173

  
......
170 182
 *
171 183
 * @param width Width of the COLOR attachment.
172 184
 * @param height Height of the COLOR attachment.
173
 * @param depthEnabled Add DEPTH attachment?
185
 * @param depthStencil Add DEPTH or STENCIL attachment?
186
 *                     Valid values: NO_DEPTH_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
174 187
 */
175 188
  @SuppressWarnings("unused")
176
  public DistortedFramebuffer(int width, int height, boolean depthEnabled)
189
  public DistortedFramebuffer(int width, int height, int depthStencil)
177 190
    {
178
    super(width,height,NOT_CREATED_YET,(depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET,TYPE_USER);
191
    super(width,height,NOT_CREATED_YET,depthStencil,NOT_CREATED_YET,TYPE_USER);
179 192
    }
180 193

  
181 194
///////////////////////////////////////////////////////////////////////////////////////////////////
182 195

  
183 196
/**
184
 * Create a new offscreen Framebuffer.
197
 * Create a new offscreen Framebuffer. No DEPTH or STENCIL buffer will be created.
185 198
 *
186 199
 * @param width Width of the COLOR attachment.
187 200
 * @param height Height of the COLOR attachment.
......
189 202
  @SuppressWarnings("unused")
190 203
  public DistortedFramebuffer(int width, int height)
191 204
    {
192
    super(width,height,NOT_CREATED_YET,DONT_CREATE,NOT_CREATED_YET,TYPE_USER);
205
    super(width,height,NOT_CREATED_YET,NO_DEPTH_STENCIL,NOT_CREATED_YET,TYPE_USER);
193 206
    }
194 207

  
195 208
///////////////////////////////////////////////////////////////////////////////////////////////////
......
210 223
    return false;
211 224
    }
212 225

  
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
/**
228
 * Enable.disable DEPTH and STENCIL buffers.
229
 *
230
 * @param depthStencil Valid values: NO_DEPTH_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
231
 */
232
  public void enableDepthStencil(int depthStencil)
233
    {
234
    if( depthStencil!=NO_DEPTH_STENCIL && mDepthStencilCreated==DONT_CREATE )
235
      {
236
      mDepthStencilCreated = NOT_CREATED_YET;
237
      mDepthStencil = depthStencil;
238

  
239
      if( mBuffer1[0]!=null )
240
        {
241
        for(int i=0; i<EffectQuality.LENGTH; i++) mBuffer1[i].enableDepthStencil(depthStencil);
242
        }
243

  
244
      markForCreation();
245
      }
246
    if( depthStencil==NO_DEPTH_STENCIL && mDepthStencilCreated!=DONT_CREATE )
247
      {
248
      mDepthStencilCreated = DONT_CREATE;
249
      mDepthStencil = depthStencil;
250

  
251
      if( mBuffer1[0]!=null )
252
        {
253
        for(int i=0; i<EffectQuality.LENGTH; i++) mBuffer1[i].enableDepthStencil(depthStencil);
254
        }
255

  
256
      markForCreation();
257
      }
258
    }
259

  
213 260
///////////////////////////////////////////////////////////////////////////////////////////////////
214 261
/**
215 262
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
src/main/java/org/distorted/library/DistortedNode.java
245 245
      }
246 246
    else if( createNewFBO )
247 247
      {
248
      newData.mFBO = new DistortedFramebuffer(true, DistortedSurface.TYPE_TREE, mSurface.getWidth(),mSurface.getHeight());
248
      newData.mFBO = new DistortedFramebuffer(DistortedFramebuffer.DEPTH_NO_STENCIL, DistortedSurface.TYPE_TREE, mSurface.getWidth(),mSurface.getHeight());
249 249
      //android.util.Log.d("NODE", "creating new FBO "+newData.mFBO.getID() );
250 250
      }
251 251

  
......
289 289

  
290 290
      if( mData.mFBO==null )
291 291
        {
292
        mData.mFBO = new DistortedFramebuffer(true, DistortedSurface.TYPE_TREE, mSurface.getWidth(),mSurface.getHeight());
292
        mData.mFBO = new DistortedFramebuffer(DistortedFramebuffer.DEPTH_NO_STENCIL, DistortedSurface.TYPE_TREE, mSurface.getWidth(),mSurface.getHeight());
293 293
        }
294 294

  
295 295
      mData.mFBO.setAsOutput(currTime);
......
402 402
        }
403 403
      else if( node.mSurface instanceof DistortedFramebuffer )
404 404
        {
405
        boolean hasDepth = ((DistortedFramebuffer) node.mSurface).hasDepth();
406
        mSurface = new DistortedFramebuffer(hasDepth,DistortedSurface.TYPE_TREE,w,h);
405
        int depthStencil = DistortedFramebuffer.NO_DEPTH_STENCIL;
406

  
407
        if( ((DistortedFramebuffer) node.mSurface).hasDepth() )
408
          {
409
          boolean hasStencil = ((DistortedFramebuffer) node.mSurface).hasStencil();
410
          depthStencil = (hasStencil ? DistortedFramebuffer.BOTH_DEPTH_STENCIL:DistortedFramebuffer.DEPTH_NO_STENCIL);
411
          }
412

  
413
        mSurface = new DistortedFramebuffer(depthStencil,DistortedSurface.TYPE_TREE,w,h);
407 414
        }
408 415
      }
409 416
    if( (flags & Distorted.CLONE_CHILDREN) != 0 )
src/main/java/org/distorted/library/DistortedOutputSurface.java
27 27

  
28 28
abstract class DistortedOutputSurface extends DistortedSurface implements DistortedSlave
29 29
{
30
/**
31
 * Do not create DEPTH or STENCIL attachment
32
 */
33
  public static final int NO_DEPTH_STENCIL = 0;
34
/**
35
 * Create DEPTH, but not STENCIL
36
 */
37
  public static final int DEPTH_NO_STENCIL = 1;
38
/**
39
 * Create both DEPTH and STENCIL
40
 */
41
  public static final int BOTH_DEPTH_STENCIL= 2;
42

  
30 43
  private static final int ATTACH = 0;
31 44
  private static final int DETACH = 1;
32 45
  private static final int DETALL = 2;
......
58 71
  float mDistance, mNear;
59 72
  float[] mProjectionMatrix;
60 73

  
61
  int mDepthCreated;
62
  int[] mDepthH = new int[1];
63
  int[] mFBOH   = new int[1];
74
  int mDepthStencilCreated;
75
  int mDepthStencil;
76
  int[] mDepthStencilH = new int[1];
77
  int[] mFBOH          = new int[1];
64 78

  
65 79
  private float mClearR, mClearG, mClearB, mClearA;
66 80
  private float mClearDepth;
......
72 86

  
73 87
///////////////////////////////////////////////////////////////////////////////////////////////////
74 88

  
75
  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
89
  DistortedOutputSurface(int width, int height, int createColor, int depthStencil, int fbo, int type)
76 90
    {
77 91
    super(width,height,createColor,type);
78 92

  
......
81 95
    mFOV = 60.0f;
82 96
    mNear=  0.5f;
83 97

  
84
    mDepthCreated= createDepth;
85
    mFBOH[0]     = fbo;
86
    mDepthH[0]   = 0;
98
    mDepthStencilCreated= (depthStencil==NO_DEPTH_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
99
    mDepthStencil = depthStencil;
100

  
101
    mFBOH[0]         = fbo;
102
    mDepthStencilH[0]= 0;
87 103

  
88 104
    mTime = 0;
89 105

  
......
180 196

  
181 197
          for(int j=0; j<EffectQuality.LENGTH; j++)
182 198
            {
183
            mBuffer1[j] = new DistortedFramebuffer( mDepthCreated!=DONT_CREATE, DistortedObject.TYPE_SYST,
199
            mBuffer1[j] = new DistortedFramebuffer( mDepthStencil   , DistortedObject.TYPE_SYST,
184 200
                                                    (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
185
            mBuffer2[j] = new DistortedFramebuffer( false                     , DistortedObject.TYPE_SYST,
201
            mBuffer2[j] = new DistortedFramebuffer( NO_DEPTH_STENCIL, DistortedObject.TYPE_SYST,
186 202
                                                    (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
187 203
            mBuffer1[j].mMipmap = mipmap;
188 204
            mipmap *= EffectQuality.MULTIPLIER;
......
431 447

  
432 448
///////////////////////////////////////////////////////////////////////////////////////////////////
433 449
/**
434
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTH attachment and recreate it.
450
 * Return true if the Surface contains a DEPTH attachment.
435 451
 *
436
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
437
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
452
 * @return <bold>true</bold> if the Surface contains a DEPTH attachment.
438 453
 */
439
  public void enableDepth(boolean enable)
454
  public boolean hasDepth()
440 455
    {
441
    if( enable && mDepthCreated==DONT_CREATE )
442
      {
443
      mDepthCreated = NOT_CREATED_YET;
444
      markForCreation();
445
      }
446
    if( !enable && mDepthCreated!=DONT_CREATE )
447
      {
448
      mDepthCreated = DONT_CREATE;
449
      markForCreation();
450
      }
456
    return mDepthStencilCreated==CREATED;
451 457
    }
452 458

  
453 459
///////////////////////////////////////////////////////////////////////////////////////////////////
454 460
/**
455
 * Return true if the Surface contains a DEPTH attachment.
461
 * Return true if the Surface contains a STENCIL attachment.
456 462
 *
457
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
463
 * @return <bold>true</bold> if the Surface contains a STENCIL attachment.
458 464
 */
459
  public boolean hasDepth()
465
  public boolean hasStencil()
460 466
    {
461
    return mDepthCreated==CREATED;
467
    return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
462 468
    }
463 469

  
464 470
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedScreen.java
25 25
import android.content.Context;
26 26
import android.content.pm.ConfigurationInfo;
27 27
import android.opengl.GLSurfaceView;
28
import android.view.SurfaceView;
29 28

  
30 29
/**
31 30
 * Class which represents the Screen.
......
54 53
    {
55 54
    // set color to 'DONT_CREATE' so that Screens will not get added to the Surface lists
56 55
    // set depth to 'CREATED' so that depth will be, by default, on.
57
    super(0,0,DONT_CREATE,CREATED,0,TYPE_USER);
56
    super(0,0,DONT_CREATE,DEPTH_NO_STENCIL,0,TYPE_USER);
58 57

  
59 58
    if( view!=null )
60 59
      {

Also available in: Unified diff