Project

General

Profile

« Previous | Next » 

Revision 89de975c

Added by Leszek Koltunski almost 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.

Also available in: Unified diff