@@ -83,31 +83,6 @@ - (CGAffineTransform)nodeToWorldTransform
83
83
@end
84
84
85
85
86
- @interface CCRenderTextureFBO : NSObject
87
-
88
- @property (nonatomic , readonly ) GLuint FBO;
89
- @property (nonatomic , readonly ) GLuint depthRenderBuffer;
90
- @property (nonatomic , readonly ) GLuint stencilRenderBuffer;
91
-
92
- @end
93
-
94
-
95
- @implementation CCRenderTextureFBO
96
-
97
- - (id )initWithFBO : (GLuint)fbo depthRenderBuffer : (GLuint)depthBuffer stencilRenderBuffer : (GLuint)stencilRenderBuffer
98
- {
99
- if ((self = [super init ]))
100
- {
101
- _FBO = fbo;
102
- _depthRenderBuffer = depthBuffer;
103
- _stencilRenderBuffer = stencilRenderBuffer;
104
- }
105
- return self;
106
- }
107
-
108
- @end
109
-
110
-
111
86
@implementation CCRenderTexture
112
87
113
88
+(id )renderTextureWithWidth : (int )w height : (int )h pixelFormat : (CCTexturePixelFormat) format depthStencilFormat : (GLuint)depthStencilFormat
@@ -139,7 +114,6 @@ - (id)initWithWidth:(int)w height:(int)h pixelFormat:(CCTexturePixelFormat)forma
139
114
-(id )initWithWidth : (int )width height : (int )height pixelFormat : (CCTexturePixelFormat) format depthStencilFormat : (GLuint)depthStencilFormat
140
115
{
141
116
if ((self = [super init ])){
142
-
143
117
NSAssert (format != CCTexturePixelFormat_A8, @" only RGB and RGBA formats are valid for a render texture" );
144
118
145
119
CCDirector *director = [CCDirector sharedDirector ];
@@ -149,20 +123,21 @@ -(id)initWithWidth:(int)width height:(int)height pixelFormat:(CCTexturePixelForm
149
123
CCLOGWARN (@" cocos2d: WARNING. CCRenderTexture is running on its own thread. Make sure that an OpenGL context is being used on this thread!" );
150
124
151
125
_contentScale = [CCDirector sharedDirector ].contentScaleFactor ;
152
- [self setContentSize: CGSizeMake (width, height)];
126
+ [self setContentSize: CGSizeMake (width, height)];
153
127
_pixelFormat = format;
154
128
_depthStencilFormat = depthStencilFormat;
155
129
156
130
// Flip the projection matrix on the y-axis since Cocos2D uses upside down textures.
157
131
_projection = GLKMatrix4MakeOrtho (0 .0f , width, 0 .0f , height, -1024 .0f , 1024 .0f );
158
132
159
- CCRenderTextureSprite *rtSprite = [CCRenderTextureSprite spriteWithTexture: [CCTexture none ]];
160
- rtSprite.renderTexture = self;
161
- _sprite = rtSprite;
133
+ CCRenderTextureSprite *rtSprite = [CCRenderTextureSprite spriteWithTexture: [CCTexture none ]];
134
+ rtSprite.renderTexture = self;
135
+ _sprite = rtSprite;
162
136
163
137
// Diabled by default.
164
138
_autoDraw = NO ;
165
139
}
140
+
166
141
return self;
167
142
}
168
143
@@ -182,8 +157,6 @@ -(void)createTextureAndFboWithPixelSize:(CGSize)pixelSize
182
157
{
183
158
CCGL_DEBUG_PUSH_GROUP_MARKER (" CCRenderTexture: Create" );
184
159
185
- glGetIntegerv (GL_FRAMEBUFFER_BINDING, &_oldFBO);
186
-
187
160
// textures must be power of two
188
161
NSUInteger powW;
189
162
NSUInteger powH;
@@ -197,120 +170,21 @@ -(void)createTextureAndFboWithPixelSize:(CGSize)pixelSize
197
170
}
198
171
199
172
void *data = calloc (powW*powH, 4 );
200
-
201
173
CCTexture *texture = [[CCTexture alloc ] initWithData: data pixelFormat: _pixelFormat pixelsWide: powW pixelsHigh: powH contentSizeInPixels: pixelSize contentScale: _contentScale];
202
174
self.texture = texture;
203
175
free (data);
204
176
205
177
// Render textures are nearest filtered for legacy reasons.
206
178
self.texture .antialiased = NO ;
207
179
208
- CCRenderDispatch (NO , ^{
209
- CCGL_DEBUG_PUSH_GROUP_MARKER (" CCRenderTexture: Create" );
210
-
211
- glGetIntegerv (GL_FRAMEBUFFER_BINDING, &_oldFBO);
212
-
213
- GLint oldRBO;
214
- glGetIntegerv (GL_RENDERBUFFER_BINDING, &oldRBO);
215
-
216
- // generate FBO
217
- GLuint fbo;
218
- glGenFramebuffers (1 , &fbo);
219
- glBindFramebuffer (GL_FRAMEBUFFER, fbo);
220
-
221
- // associate texture with FBO
222
- glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.name , 0 );
223
-
224
- GLuint depthRenderBuffer = 0 ;
225
- GLuint stencilRenderBuffer = 0 ;
226
-
227
- #if __CC_PLATFORM_ANDROID
228
-
229
- // Some android devices *only* support combined depth buffers (like all iOS devices), some android devices do not
230
- // support combined depth buffers, thus we have to create a seperate stencil buffer
231
- if (_depthStencilFormat)
232
- {
233
- // create and attach depth buffer
234
-
235
- if (![[CCConfiguration sharedConfiguration ] supportsPackedDepthStencil ])
236
- {
237
- glGenRenderbuffers (1 , &depthRenderBuffer);
238
- glBindRenderbuffer (GL_RENDERBUFFER, depthRenderBuffer);
239
- glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, (GLsizei)powW, (GLsizei)powH); // GL_DEPTH_COMPONENT24_OES
240
- glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
241
-
242
- // if depth format is the one with stencil part, bind same render buffer as stencil attachment
243
- if (_depthStencilFormat == GL_DEPTH24_STENCIL8)
244
- {
245
- glGenRenderbuffers (1 , &stencilRenderBuffer);
246
- glBindRenderbuffer (GL_RENDERBUFFER, stencilRenderBuffer);
247
- glRenderbufferStorage (GL_RENDERBUFFER, GL_STENCIL_INDEX8, (GLsizei)powW, (GLsizei)powH);
248
- glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencilRenderBuffer);
249
- }
250
- }
251
- else
252
- {
253
- glGenRenderbuffers (1 , &depthRenderBuffer);
254
- glBindRenderbuffer (GL_RENDERBUFFER, depthRenderBuffer);
255
- glRenderbufferStorage (GL_RENDERBUFFER, _depthStencilFormat, (GLsizei)powW, (GLsizei)powH);
256
- glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
257
-
258
- // if depth format is the one with stencil part, bind same render buffer as stencil attachment
259
- if (_depthStencilFormat == GL_DEPTH24_STENCIL8){
260
- glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
261
- }
262
- }
263
- }
264
-
265
- #else
266
-
267
- if (_depthStencilFormat)
268
- {
269
- // create and attach depth buffer
270
- glGenRenderbuffers (1 , &depthRenderBuffer);
271
- glBindRenderbuffer (GL_RENDERBUFFER, depthRenderBuffer);
272
- glRenderbufferStorage (GL_RENDERBUFFER, _depthStencilFormat, (GLsizei)powW, (GLsizei)powH);
273
- glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
274
-
275
- // associate texture with FBO
276
- glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.name , 0 );
277
-
278
- if (_depthStencilFormat){
279
- // create and attach depth buffer
280
- glGenRenderbuffers (1 , &depthRenderBuffer);
281
- glBindRenderbuffer (GL_RENDERBUFFER, depthRenderBuffer);
282
- glRenderbufferStorage (GL_RENDERBUFFER, _depthStencilFormat, (GLsizei)powW, (GLsizei)powH);
283
- glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
284
-
285
- // if depth format is the one with stencil part, bind same render buffer as stencil attachment
286
- if (_depthStencilFormat == GL_DEPTH24_STENCIL8){
287
- glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
288
- }
289
- }
290
- }
291
-
292
- #endif
293
-
294
- // check if it worked (probably worth doing :) )
295
- // NSAssert( glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, @"Could not attach texture to framebuffer");
296
-
297
- _FBO = [[CCRenderTextureFBO alloc ] initWithFBO: fbo depthRenderBuffer: depthRenderBuffer stencilRenderBuffer: stencilRenderBuffer];
298
-
299
- glBindRenderbuffer (GL_RENDERBUFFER, oldRBO);
300
- glBindFramebuffer (GL_FRAMEBUFFER, _oldFBO);
301
-
302
- CCGL_DEBUG_POP_GROUP_MARKER ();
303
- CC_CHECK_GL_ERROR_DEBUG ();
304
- });
180
+ _framebuffer = [[CCFrameBufferObject alloc ] initWithTexture: texture depthStencilFormat: _depthStencilFormat];
305
181
306
182
// XXX Thayer says: I think this is incorrect for any situations where the content
307
183
// size type isn't (points, points). The call to setTextureRect below eventually arrives
308
184
// at some code that assumes the supplied size is in points so, if the size is not in points,
309
185
// things break.
310
- CGRect rect = CGRectMake (0 , 0 , _contentSize.width , _contentSize.height );
311
-
312
186
[self assignSpriteTexture ];
313
- [_sprite setTextureRect: rect ];
187
+ [_sprite setTextureRect: CGRectMake ( 0 , 0 , _contentSize.width, _contentSize.height) ];
314
188
}
315
189
316
190
-(void )assignSpriteTexture
@@ -329,25 +203,7 @@ -(void)setContentScale:(float)contentScale
329
203
330
204
-(void )destroy
331
205
{
332
- GLuint fbo = _FBO.FBO ;
333
- GLuint depthRenderBuffer = _FBO.depthRenderBuffer ;
334
- GLuint stencilRenderBuffer = _FBO.stencilRenderBuffer ;
335
-
336
- CCRenderDispatch (YES , ^{
337
- glDeleteFramebuffers (1 , &fbo);
338
-
339
- if (depthRenderBuffer)
340
- {
341
- glDeleteRenderbuffers (1 , &depthRenderBuffer);
342
- }
343
-
344
- if (depthRenderBuffer)
345
- {
346
- glDeleteRenderbuffers (1 , &stencilRenderBuffer);
347
- }
348
- });
349
-
350
- _FBO = nil ;
206
+ _framebuffer = nil ;
351
207
self.texture = nil ;
352
208
}
353
209
@@ -366,39 +222,20 @@ -(CCTexture *)texture
366
222
return super.texture ;
367
223
}
368
224
369
- -(GLuint)fbo
370
- {
371
- if (super.texture == nil )
372
- {
373
- [self create ];
374
- }
375
-
376
- return _FBO.FBO ;
377
- }
378
-
379
225
-(CCRenderer *)begin
380
226
{
381
227
CCTexture *texture = self.texture ;
228
+ if (texture == nil ){
229
+ [self create ];
230
+ texture = self.texture ;
231
+ }
382
232
383
233
CCRenderer *renderer = [[CCDirector sharedDirector ] rendererFromPool ];
384
- [renderer prepareWithProjection: &_projection viewSize: texture.contentSize contentScale: texture.contentScale ];
234
+ [renderer prepareWithProjection: &_projection framebuffer: _framebuffer ];
385
235
386
236
_previousRenderer = [CCRenderer currentRenderer ];
387
237
[CCRenderer bindRenderer: renderer];
388
238
389
- CGSize pixelSize = texture.contentSizeInPixels ;
390
- GLuint fbo = [self fbo ];
391
-
392
- [renderer pushGroup ];
393
-
394
- [renderer enqueueBlock: ^{
395
- glGetFloatv (GL_VIEWPORT, _oldViewport.v );
396
- glViewport (0 , 0 , pixelSize.width , pixelSize.height );
397
-
398
- glGetIntegerv (GL_FRAMEBUFFER_BINDING, &_oldFBO);
399
- glBindFramebuffer (GL_FRAMEBUFFER, fbo);
400
- } globalSortOrder: NSIntegerMin debugLabel: @" CCRenderTexture: Bind FBO" threadSafe: NO ];
401
-
402
239
return renderer;
403
240
}
404
241
@@ -427,13 +264,6 @@ -(void)end
427
264
{
428
265
CCRenderer *renderer = [CCRenderer currentRenderer ];
429
266
430
- [renderer enqueueBlock: ^{
431
- glBindFramebuffer (GL_FRAMEBUFFER, _oldFBO);
432
- glViewport (_oldViewport.v [0 ], _oldViewport.v [1 ], _oldViewport.v [2 ], _oldViewport.v [3 ]);
433
- } globalSortOrder: NSIntegerMax debugLabel: @" CCRenderTexture: Restore FBO" threadSafe: NO ];
434
-
435
- [renderer popGroupWithDebugLabel: @" CCRenderTexture begin/end" globalSortOrder: 0 ];
436
-
437
267
__unsafe_unretained CCDirector *director = [CCDirector sharedDirector ];
438
268
CCRenderDispatch (renderer.threadsafe , ^{
439
269
[director addFrameCompletionHandler: ^{
0 commit comments