Skip to content

Commit 42e23d2

Browse files
committed
Buffer bindings now own their buffers instead of the renderer.
1 parent acf9cd6 commit 42e23d2

File tree

5 files changed

+114
-101
lines changed

5 files changed

+114
-101
lines changed

CCRendererGLSupport.m

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -185,40 +185,46 @@ -(void)commit
185185
#endif
186186

187187

188-
@interface CCGraphicsBufferBindingsGL : NSObject <CCGraphicsBufferBindings> @end
188+
@interface CCGraphicsBufferBindingsGL : CCGraphicsBufferBindings @end
189189
@implementation CCGraphicsBufferBindingsGL {
190190
GLuint _vao;
191191
}
192192

193-
-(instancetype)initWithVertexBuffer:(CCGraphicsBufferGLBasic *)vertexBuffer indexBuffer:(CCGraphicsBufferGLBasic *)indexBuffer
193+
-(instancetype)init
194194
{
195-
NSAssert([vertexBuffer isKindOfClass:[CCGraphicsBufferGLBasic class]], @"Wrong kind of buffer!");
196-
NSAssert([indexBuffer isKindOfClass:[CCGraphicsBufferGLBasic class]], @"Wrong kind of buffer!");
197-
198195
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();
196+
CCRenderDispatch(NO, ^{
197+
const NSUInteger CCRENDERER_INITIAL_VERTEX_CAPACITY = 16*1024;
198+
_vertexBuffer = [[CCGraphicsBufferClass alloc] initWithCapacity:CCRENDERER_INITIAL_VERTEX_CAPACITY elementSize:sizeof(CCVertex) type:CCGraphicsBufferTypeVertex];
199+
[_vertexBuffer prepare];
200+
201+
_indexBuffer = [[CCGraphicsBufferClass alloc] initWithCapacity:CCRENDERER_INITIAL_VERTEX_CAPACITY*1.5 elementSize:sizeof(uint16_t) type:CCGraphicsBufferTypeIndex];
202+
[_indexBuffer prepare];
203+
204+
CCGL_DEBUG_PUSH_GROUP_MARKER("CCGraphicsBufferBindingsGL: Creating VAO");
205+
206+
glGenVertexArrays(1, &_vao);
207+
glBindVertexArray(_vao);
208+
209+
glEnableVertexAttribArray(CCShaderAttributePosition);
210+
glEnableVertexAttribArray(CCShaderAttributeTexCoord1);
211+
glEnableVertexAttribArray(CCShaderAttributeTexCoord2);
212+
glEnableVertexAttribArray(CCShaderAttributeColor);
213+
214+
glBindBuffer(GL_ARRAY_BUFFER, ((CCGraphicsBufferGLBasic *)_vertexBuffer)->_buffer);
215+
glVertexAttribPointer(CCShaderAttributePosition, 4, GL_FLOAT, GL_FALSE, sizeof(CCVertex), (void *)offsetof(CCVertex, position));
216+
glVertexAttribPointer(CCShaderAttributeTexCoord1, 2, GL_FLOAT, GL_FALSE, sizeof(CCVertex), (void *)offsetof(CCVertex, texCoord1));
217+
glVertexAttribPointer(CCShaderAttributeTexCoord2, 2, GL_FLOAT, GL_FALSE, sizeof(CCVertex), (void *)offsetof(CCVertex, texCoord2));
218+
glVertexAttribPointer(CCShaderAttributeColor, 4, GL_FLOAT, GL_FALSE, sizeof(CCVertex), (void *)offsetof(CCVertex, color));
219+
220+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((CCGraphicsBufferGLBasic *)_indexBuffer)->_buffer);
221+
222+
glBindVertexArray(0);
223+
glBindBuffer(GL_ARRAY_BUFFER, 0);
224+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
225+
226+
CCGL_DEBUG_POP_GROUP_MARKER();
227+
});
222228
}
223229

224230
return self;
@@ -236,4 +242,16 @@ -(void)bind:(BOOL)bind
236242
glBindVertexArray(bind ? _vao : 0);
237243
}
238244

245+
-(void)prepare
246+
{
247+
[_vertexBuffer prepare];
248+
[_indexBuffer prepare];
249+
}
250+
251+
-(void)commit
252+
{
253+
[_vertexBuffer commit];
254+
[_indexBuffer commit];
255+
}
256+
239257
@end

cocos2d/CCNoARC.m

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,20 @@ @implementation CCRenderer(NoARC)
127127
-(CCRenderBuffer)enqueueTriangles:(NSUInteger)triangleCount andVertexes:(NSUInteger)vertexCount withState:(CCRenderState *)renderState globalSortOrder:(NSInteger)globalSortOrder;
128128
{
129129
// Need to record the first vertex or element index before pushing more vertexes.
130-
NSUInteger firstVertex = _vertexBuffer->_count;
131-
NSUInteger firstElement = _elementBuffer->_count;
130+
NSUInteger firstVertex = _buffers->_vertexBuffer->_count;
131+
NSUInteger firstIndex = _buffers->_indexBuffer->_count;
132132

133-
NSUInteger elementCount = 3*triangleCount;
134-
CCVertex *vertexes = CCGraphicsBufferPushElements(_vertexBuffer, vertexCount);
135-
uint16_t *elements = CCGraphicsBufferPushElements(_elementBuffer, elementCount);
133+
NSUInteger indexCount = 3*triangleCount;
134+
CCVertex *vertexes = CCGraphicsBufferPushElements(_buffers->_vertexBuffer, vertexCount);
135+
uint16_t *elements = CCGraphicsBufferPushElements(_buffers->_indexBuffer, indexCount);
136136

137137
CCRenderCommandDraw *previous = _lastDrawCommand;
138138
if(previous && previous->_renderState == renderState && previous->_globalSortOrder == globalSortOrder){
139139
// Batch with the previous command.
140-
[previous batch:elementCount];
140+
[previous batch:indexCount];
141141
} else {
142142
// Start a new command.
143-
CCRenderCommandDraw *command = [[CCRenderCommandDrawClass alloc] initWithMode:CCRenderCommandDrawTriangles renderState:renderState first:firstElement count:elementCount globalSortOrder:globalSortOrder];
143+
CCRenderCommandDraw *command = [[CCRenderCommandDrawClass alloc] initWithMode:CCRenderCommandDrawTriangles renderState:renderState first:firstIndex count:indexCount globalSortOrder:globalSortOrder];
144144
[_queue addObject:command];
145145
[command release];
146146

@@ -153,14 +153,14 @@ -(CCRenderBuffer)enqueueTriangles:(NSUInteger)triangleCount andVertexes:(NSUInte
153153
-(CCRenderBuffer)enqueueLines:(NSUInteger)lineCount andVertexes:(NSUInteger)vertexCount withState:(CCRenderState *)renderState globalSortOrder:(NSInteger)globalSortOrder;
154154
{
155155
// Need to record the first vertex or element index before pushing more vertexes.
156-
NSUInteger firstVertex = _vertexBuffer->_count;
157-
NSUInteger firstElement = _elementBuffer->_count;
156+
NSUInteger firstVertex = _buffers->_vertexBuffer->_count;
157+
NSUInteger firstIndex = _buffers->_indexBuffer->_count;
158158

159-
NSUInteger elementCount = 2*lineCount;
160-
CCVertex *vertexes = CCGraphicsBufferPushElements(_vertexBuffer, vertexCount);
161-
uint16_t *elements = CCGraphicsBufferPushElements(_elementBuffer, elementCount);
159+
NSUInteger indexCount = 2*lineCount;
160+
CCVertex *vertexes = CCGraphicsBufferPushElements(_buffers->_vertexBuffer, vertexCount);
161+
uint16_t *elements = CCGraphicsBufferPushElements(_buffers->_indexBuffer, indexCount);
162162

163-
CCRenderCommandDraw *command = [[CCRenderCommandDrawClass alloc] initWithMode:CCRenderCommandDrawLines renderState:renderState first:firstElement count:elementCount globalSortOrder:globalSortOrder];
163+
CCRenderCommandDraw *command = [[CCRenderCommandDrawClass alloc] initWithMode:CCRenderCommandDrawLines renderState:renderState first:firstIndex count:indexCount globalSortOrder:globalSortOrder];
164164
[_queue addObject:command];
165165
[command release];
166166

@@ -174,7 +174,7 @@ -(CCRenderBuffer)enqueueLines:(NSUInteger)lineCount andVertexes:(NSUInteger)vert
174174
CCRendererBindBuffers(CCRenderer *self, BOOL bind)
175175
{
176176
if(bind != self->_buffersBound){
177-
[self->_bufferBindings bind:bind];
177+
[self->_buffers bind:bind];
178178
self->_buffersBound = bind;
179179
}
180180
}

cocos2d/CCRenderer.m

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,19 @@ -(void)dealloc
605605
@end
606606

607607

608+
//MARK: CCGraphicsBufferBindings
609+
610+
611+
@implementation CCGraphicsBufferBindings
612+
613+
// Base implementations do do nothing.
614+
-(void)prepare {}
615+
-(void)commit {}
616+
-(void)bind {}
617+
618+
@end
619+
620+
608621
//MARK: Render Queue
609622

610623

@@ -620,31 +633,10 @@ -(void)invalidateState
620633
-(instancetype)init
621634
{
622635
if((self = [super init])){
623-
CCRenderDispatch(NO, ^{
624-
const NSUInteger CCRENDERER_INITIAL_VERTEX_CAPACITY = 16*1024;
625-
_vertexBuffer = [[CCGraphicsBufferClass alloc] initWithCapacity:CCRENDERER_INITIAL_VERTEX_CAPACITY elementSize:sizeof(CCVertex) type:CCGraphicsBufferTypeVertex];
626-
[_vertexBuffer prepare];
627-
628-
_elementBuffer = [[CCGraphicsBufferClass alloc] initWithCapacity:CCRENDERER_INITIAL_VERTEX_CAPACITY*1.5 elementSize:sizeof(uint16_t) type:CCGraphicsBufferTypeIndex];
629-
[_elementBuffer prepare];
630-
631-
_bufferBindings = [CCGraphicsBufferBindingsClass alloc];
632-
_bufferBindings = [_bufferBindings initWithVertexBuffer:_vertexBuffer indexBuffer:_elementBuffer];
633-
});
634-
636+
_buffers = [[CCGraphicsBufferBindingsClass alloc] init];
637+
635638
_threadsafe = YES;
636639
_queue = [NSMutableArray array];
637-
638-
#if __CC_METAL_SUPPORTED_AND_ENABLED
639-
if([CCConfiguration sharedConfiguration].graphicsAPI == CCGraphicsAPIMetal){
640-
// TODO: This is sort of gross. Would like it to move somewhere else.
641-
_metalContext = (CCMetalContext *)[NSClassFromString(@"CCMetalContext") currentContext];
642-
643-
// Default to half a megabyte of initial uniform storage.
644-
NSUInteger uniformCapacity = 500*1024;
645-
_uniformBuffer = [[CCGraphicsBufferClass alloc] initWithCapacity:uniformCapacity elementSize:1 type:CCGraphicsBufferTypeUniform];
646-
}
647-
#endif
648640
}
649641

650642
return self;
@@ -742,14 +734,8 @@ -(void)flush
742734
{
743735
CCRENDERER_DEBUG_PUSH_GROUP_MARKER(@"CCRenderer: Flush");
744736

745-
// Commit the geometry buffers to be used by the rendering commmands.
746-
CCRENDERER_DEBUG_INSERT_EVENT_MARKER(@"Buffering");
747-
[_vertexBuffer commit];
748-
[_elementBuffer commit];
749-
CCRENDERER_DEBUG_CHECK_ERRORS();
750-
751-
// The render commands build the uniform buffers. (only used by Metal currently)
752-
[_uniformBuffer prepare];
737+
// Commit the buffers.
738+
[_buffers commit];
753739

754740
// TODO This should probably be moved eventually, but I'm not sure where.
755741
// Probably never going to support uniform buffers in GL and making CCRenderer abstract is ugh...
@@ -779,12 +765,8 @@ -(void)flush
779765

780766
[_queue removeAllObjects];
781767

782-
// Commit the uniform buffers.
783-
[_uniformBuffer commit];
784-
785-
// Prepare the geometry buffers for use next time the renderer is reused.
786-
[_vertexBuffer prepare];
787-
[_elementBuffer prepare];
768+
// Prepare the buffers.
769+
[_buffers prepare];
788770

789771
CCRENDERER_DEBUG_POP_GROUP_MARKER();
790772
CCRENDERER_DEBUG_CHECK_ERRORS();

cocos2d/CCRenderer_private.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,30 @@ CCGraphicsBufferPushElements(CCGraphicsBuffer *buffer, size_t requestedCount)
186186

187187

188188
/// Internal abstract class used to wrap vertex buffer state. (GL VAOs, etc)
189-
@protocol CCGraphicsBufferBindings
190-
-(instancetype)initWithVertexBuffer:(CCGraphicsBuffer *)vertexBuffer indexBuffer:(CCGraphicsBuffer *)indexBuffer;
189+
@interface CCGraphicsBufferBindings : NSObject {
190+
@public
191+
CCGraphicsBuffer *_vertexBuffer;
192+
CCGraphicsBuffer *_indexBuffer;
193+
194+
// Not used by the GL2 renderer.
195+
CCGraphicsBuffer *_uniformBuffer;
196+
}
197+
198+
/// Prepare buffers for changes.
199+
-(void)prepare;
200+
201+
/// Commit changes to buffers.
202+
-(void)commit;
203+
204+
/// Bind the buffers. (Not used by Metal)
191205
-(void)bind:(BOOL)bind;
206+
192207
@end
193208

194209

195210
@interface CCRenderer(){
196211
@public
197-
CCGraphicsBuffer *_vertexBuffer;
198-
CCGraphicsBuffer *_elementBuffer;
199-
CCGraphicsBuffer *_uniformBuffer; // Currently only used by the Metal renderer.
200-
id<CCGraphicsBufferBindings> _bufferBindings;
212+
CCGraphicsBufferBindings *_buffers;
201213

202214
NSDictionary *_globalShaderUniforms;
203215

cocos2d/Platforms/iOS/CCMetalSupport.m

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,31 @@ -(void)commit; {}
130130
@end
131131

132132

133-
@interface CCGraphicsBufferBindingsMetal : NSObject <CCGraphicsBufferBindings> @end
134-
@implementation CCGraphicsBufferBindingsMetal {
135-
CCGraphicsBufferMetal *_vertexBuffer;
136-
CCGraphicsBufferMetal *_indexBuffer;
137-
}
133+
@interface CCGraphicsBufferBindingsMetal : CCGraphicsBufferBindings @end
134+
@implementation CCGraphicsBufferBindingsMetal
138135

139-
-(instancetype)initWithVertexBuffer:(CCGraphicsBufferMetal *)vertexBuffer indexBuffer:(CCGraphicsBufferMetal *)indexBuffer
136+
-(instancetype)init
140137
{
141138
if((self = [super init])){
142-
_vertexBuffer = vertexBuffer;
143-
_indexBuffer = indexBuffer;
139+
CCRenderDispatch(NO, ^{
140+
const NSUInteger CCRENDERER_INITIAL_VERTEX_CAPACITY = 16*1024;
141+
_vertexBuffer = [[CCGraphicsBufferMetal alloc] initWithCapacity:CCRENDERER_INITIAL_VERTEX_CAPACITY elementSize:sizeof(CCVertex) type:CCGraphicsBufferTypeVertex];
142+
[_vertexBuffer prepare];
143+
144+
_indexBuffer = [[CCGraphicsBufferMetal alloc] initWithCapacity:CCRENDERER_INITIAL_VERTEX_CAPACITY*1.5 elementSize:sizeof(uint16_t) type:CCGraphicsBufferTypeIndex];
145+
[_indexBuffer prepare];
146+
147+
// _metalContext = (CCMetalContext *)[NSClassFromString(@"CCMetalContext") currentContext];
148+
149+
// Default to half a megabyte of initial uniform storage.
150+
NSUInteger uniformCapacity = 500*1024;
151+
_uniformBuffer = [[CCGraphicsBufferClass alloc] initWithCapacity:uniformCapacity elementSize:1 type:CCGraphicsBufferTypeUniform];
152+
});
144153
}
145154

146155
return self;
147156
}
148157

149-
-(void)bind:(BOOL)bind
150-
{
151-
id<MTLRenderCommandEncoder> renderEncoder = [CCMetalContext currentContext].currentRenderCommandEncoder;
152-
153-
CCMTL_DEBUG_INSERT_EVENT_MARKER(renderEncoder, @"CCGraphicsBufferBindingsMetal: Bind vertex array.");
154-
[renderEncoder setVertexBuffer:_vertexBuffer->_buffer offset:0 atIndex:0];
155-
}
156-
157158
@end
158159

159160
#endif

0 commit comments

Comments
 (0)