2222 * THE SOFTWARE.
2323 */
2424
25- #import " CCRenderer_private.h"
25+ #import " CCRenderer_Private.h"
26+ #import " CCTexture_Private.h"
2627
2728#import " CCRenderDispatch.h"
2829
@@ -185,40 +186,46 @@ -(void)commit
185186#endif
186187
187188
188- @interface CCGraphicsBufferBindingsGL : NSObject < CCGraphicsBufferBindings> @end
189+ @interface CCGraphicsBufferBindingsGL : CCGraphicsBufferBindings @end
189190@implementation CCGraphicsBufferBindingsGL {
190191 GLuint _vao;
191192}
192193
193- -(instancetype )initWithVertexBuffer : (CCGraphicsBufferGLBasic *) vertexBuffer indexBuffer : (CCGraphicsBufferGLBasic *) indexBuffer
194+ -(instancetype )init
194195{
195- NSAssert ([vertexBuffer isKindOfClass: [CCGraphicsBufferGLBasic class ]], @" Wrong kind of buffer!" );
196- NSAssert ([indexBuffer isKindOfClass: [CCGraphicsBufferGLBasic class ]], @" Wrong kind of buffer!" );
197-
198196 if ((self = [super init ])){
199- CCGL_DEBUG_PUSH_GROUP_MARKER (" CCGraphicsBufferBindingsGL: Creating VAO" );
200-
201- glGenVertexArrays (1 , &_vao);
202- glBindVertexArray (_vao);
203-
204- glEnableVertexAttribArray (CCShaderAttributePosition);
205- glEnableVertexAttribArray (CCShaderAttributeTexCoord1);
206- glEnableVertexAttribArray (CCShaderAttributeTexCoord2);
207- glEnableVertexAttribArray (CCShaderAttributeColor);
208-
209- glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer->_buffer );
210- glVertexAttribPointer (CCShaderAttributePosition, 4 , GL_FLOAT, GL_FALSE, sizeof (CCVertex), (void *)offsetof (CCVertex, position));
211- glVertexAttribPointer (CCShaderAttributeTexCoord1, 2 , GL_FLOAT, GL_FALSE, sizeof (CCVertex), (void *)offsetof (CCVertex, texCoord1));
212- glVertexAttribPointer (CCShaderAttributeTexCoord2, 2 , GL_FLOAT, GL_FALSE, sizeof (CCVertex), (void *)offsetof (CCVertex, texCoord2));
213- glVertexAttribPointer (CCShaderAttributeColor, 4 , GL_FLOAT, GL_FALSE, sizeof (CCVertex), (void *)offsetof (CCVertex, color));
214-
215- glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, indexBuffer->_buffer );
216-
217- glBindVertexArray (0 );
218- glBindBuffer (GL_ARRAY_BUFFER, 0 );
219- glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0 );
220-
221- CCGL_DEBUG_POP_GROUP_MARKER ();
197+ CCRenderDispatch (NO , ^{
198+ const NSUInteger CCRENDERER_INITIAL_VERTEX_CAPACITY = 16 *1024 ;
199+ _vertexBuffer = [[CCGraphicsBufferClass alloc ] initWithCapacity: CCRENDERER_INITIAL_VERTEX_CAPACITY elementSize: sizeof (CCVertex) type: CCGraphicsBufferTypeVertex];
200+ [_vertexBuffer prepare ];
201+
202+ _indexBuffer = [[CCGraphicsBufferClass alloc ] initWithCapacity: CCRENDERER_INITIAL_VERTEX_CAPACITY*1.5 elementSize: sizeof (uint16_t ) type: CCGraphicsBufferTypeIndex];
203+ [_indexBuffer prepare ];
204+
205+ CCGL_DEBUG_PUSH_GROUP_MARKER (" CCGraphicsBufferBindingsGL: Creating VAO" );
206+
207+ glGenVertexArrays (1 , &_vao);
208+ glBindVertexArray (_vao);
209+
210+ glEnableVertexAttribArray (CCShaderAttributePosition);
211+ glEnableVertexAttribArray (CCShaderAttributeTexCoord1);
212+ glEnableVertexAttribArray (CCShaderAttributeTexCoord2);
213+ glEnableVertexAttribArray (CCShaderAttributeColor);
214+
215+ glBindBuffer (GL_ARRAY_BUFFER, ((CCGraphicsBufferGLBasic *)_vertexBuffer)->_buffer );
216+ glVertexAttribPointer (CCShaderAttributePosition, 4 , GL_FLOAT, GL_FALSE, sizeof (CCVertex), (void *)offsetof (CCVertex, position));
217+ glVertexAttribPointer (CCShaderAttributeTexCoord1, 2 , GL_FLOAT, GL_FALSE, sizeof (CCVertex), (void *)offsetof (CCVertex, texCoord1));
218+ glVertexAttribPointer (CCShaderAttributeTexCoord2, 2 , GL_FLOAT, GL_FALSE, sizeof (CCVertex), (void *)offsetof (CCVertex, texCoord2));
219+ glVertexAttribPointer (CCShaderAttributeColor, 4 , GL_FLOAT, GL_FALSE, sizeof (CCVertex), (void *)offsetof (CCVertex, color));
220+
221+ glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, ((CCGraphicsBufferGLBasic *)_indexBuffer)->_buffer );
222+
223+ glBindVertexArray (0 );
224+ glBindBuffer (GL_ARRAY_BUFFER, 0 );
225+ glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0 );
226+
227+ CCGL_DEBUG_POP_GROUP_MARKER ();
228+ });
222229 }
223230
224231 return self;
@@ -237,3 +244,138 @@ -(void)bind:(BOOL)bind
237244}
238245
239246@end
247+
248+
249+ @interface CCFrameBufferObjectGL : CCFrameBufferObject @end
250+ @implementation CCFrameBufferObjectGL {
251+ GLuint _fbo;
252+ GLuint _depthRenderBuffer;
253+ GLuint _stencilRenderBuffer;
254+ }
255+
256+ -(instancetype )initWithTexture : (CCTexture *)texture depthStencilFormat : (GLuint)depthStencilFormat
257+ {
258+ if ((self = [super initWithTexture: texture depthStencilFormat: depthStencilFormat])){
259+ CCRenderDispatch (NO , ^{
260+ CCGL_DEBUG_PUSH_GROUP_MARKER (" CCRenderTexture: Create" );
261+
262+ // generate FBO
263+ glGenFramebuffers (1 , &_fbo);
264+ glBindFramebuffer (GL_FRAMEBUFFER, _fbo);
265+
266+ // associate texture with FBO
267+ glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.name , 0 );
268+
269+ GLuint width = texture.pixelWidth ;
270+ GLuint height = texture.pixelHeight ;
271+
272+ #if __CC_PLATFORM_ANDROID
273+
274+ // Some android devices *only* support combined depth buffers (like all iOS devices), some android devices do not
275+ // support combined depth buffers, thus we have to create a seperate stencil buffer
276+ if (_depthStencilFormat)
277+ {
278+ // create and attach depth buffer
279+
280+ if (![[CCConfiguration sharedConfiguration ] supportsPackedDepthStencil ])
281+ {
282+ glGenRenderbuffers (1 , &_depthRenderBuffer);
283+ glBindRenderbuffer (GL_RENDERBUFFER, _depthRenderBuffer);
284+ glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height); // GL_DEPTH_COMPONENT24_OES
285+ glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
286+
287+ // if depth format is the one with stencil part, bind same render buffer as stencil attachment
288+ if (_depthStencilFormat == GL_DEPTH24_STENCIL8)
289+ {
290+ glGenRenderbuffers (1 , &_stencilRenderBuffer);
291+ glBindRenderbuffer (GL_RENDERBUFFER, _stencilRenderBuffer);
292+ glRenderbufferStorage (GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height);
293+ glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _stencilRenderBuffer);
294+ }
295+ }
296+ else
297+ {
298+ glGenRenderbuffers (1 , &_depthRenderBuffer);
299+ glBindRenderbuffer (GL_RENDERBUFFER, _depthRenderBuffer);
300+ glRenderbufferStorage (GL_RENDERBUFFER, _depthStencilFormat, width, height);
301+ glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
302+
303+ // if depth format is the one with stencil part, bind same render buffer as stencil attachment
304+ if (_depthStencilFormat == GL_DEPTH24_STENCIL8){
305+ glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
306+ }
307+ }
308+ }
309+
310+ #else
311+
312+ if (depthStencilFormat){
313+ // create and attach depth buffer
314+ glGenRenderbuffers (1 , &_depthRenderBuffer);
315+ glBindRenderbuffer (GL_RENDERBUFFER, _depthRenderBuffer);
316+ glRenderbufferStorage (GL_RENDERBUFFER, depthStencilFormat, width, height);
317+ glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
318+
319+ // if depth format is the one with stencil part, bind same render buffer as stencil attachment
320+ if (depthStencilFormat == GL_DEPTH24_STENCIL8){
321+ glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
322+ }
323+ }
324+
325+ #endif
326+
327+ // check if it worked (probably worth doing :) )
328+ NSAssert ( glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, @"Could not attach texture to framebuffer");
329+
330+ CCGL_DEBUG_POP_GROUP_MARKER ();
331+ CC_CHECK_GL_ERROR_DEBUG ();
332+ });
333+ }
334+ return self;
335+ }
336+
337+ -(void )dealloc
338+ {
339+ GLuint fbo = _fbo;
340+ GLuint depthRenderBuffer = _depthRenderBuffer;
341+ GLuint stencilRenderBuffer = _stencilRenderBuffer;
342+
343+ CCRenderDispatch (YES , ^{
344+ glDeleteFramebuffers (1 , &fbo);
345+
346+ if (depthRenderBuffer){
347+ glDeleteRenderbuffers (1 , &depthRenderBuffer);
348+ }
349+
350+ if (depthRenderBuffer){
351+ glDeleteRenderbuffers (1 , &stencilRenderBuffer);
352+ }
353+ });
354+ }
355+
356+ -(void )bindWithClear : (GLbitfield)mask color : (GLKVector4)color4 depth : (GLclampf)depth stencil : (GLint)stencil
357+ {
358+ glBindFramebuffer (GL_FRAMEBUFFER, _fbo);
359+
360+ CGSize size = self.sizeInPixels ;
361+ glViewport (0 , 0 , size.width , size.height );
362+
363+ if (mask & GL_COLOR_BUFFER_BIT) glClearColor (color4.r , color4.g , color4.b , color4.a );
364+ if (mask & GL_DEPTH_BUFFER_BIT) glClearDepth (depth);
365+ if (mask & GL_STENCIL_BUFFER_BIT) glClearStencil (stencil);
366+ if (mask) glClear (mask);
367+
368+ // Enabled depth testing if there is a depth buffer.
369+ if (_depthStencilFormat){
370+ glEnable (GL_DEPTH_TEST);
371+ glDepthFunc (GL_LEQUAL);
372+ }
373+ }
374+
375+ -(void )syncWithView : (CC_VIEW<CCDirectorView> *)view ;
376+ {
377+ [super syncWithView: view];
378+ _fbo = [(CCGLView *)view fbo ];
379+ }
380+
381+ @end
0 commit comments