Skip to content

Commit a6381b8

Browse files
committed
Moving CCRenderer methods into CCNoARC.m
1 parent ac78c0b commit a6381b8

File tree

4 files changed

+238
-198
lines changed

4 files changed

+238
-198
lines changed

cocos2d/CCNoARC.m

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#import "CCTexture.h"
22
#import "CCNode_Private.h"
33
#import "CCSprite_Private.h"
4+
#import "CCRenderer_Private.h"
5+
#import "CCShader_Private.h"
46

57

68
@implementation CCNode(NoARC)
@@ -25,7 +27,7 @@ -(GLKMatrix4)transform:(const GLKMatrix4 *)parentTransform
2527
return CCNodeTransform(self, *parentTransform);
2628
}
2729

28-
-(void) visit:(__unsafe_unretained CCRenderer *)renderer parentTransform:(const GLKMatrix4 *)parentTransform
30+
-(void) visit:(CCRenderer *)renderer parentTransform:(const GLKMatrix4 *)parentTransform
2931
{
3032
// quick return if not visible. children won't be drawn.
3133
if (!_visible) return;
@@ -114,3 +116,124 @@ -(void)enqueueTriangles:(CCRenderer *)renderer transform:(const GLKMatrix4 *)tra
114116
}
115117

116118
@end
119+
120+
121+
@implementation CCRenderer(NoARC)
122+
123+
-(CCRenderBuffer)enqueueTriangles:(NSUInteger)triangleCount andVertexes:(NSUInteger)vertexCount withState:(CCRenderState *)renderState globalSortOrder:(NSInteger)globalSortOrder;
124+
{
125+
// Need to record the first vertex or element index before pushing more vertexes.
126+
size_t firstVertex = _vertexBuffer.count;
127+
size_t firstElement = _elementBuffer.count;
128+
129+
size_t elementCount = 3*triangleCount;
130+
CCVertex *vertexes = CCGraphicsBufferPushElements(&_vertexBuffer, vertexCount, self);
131+
GLushort *elements = CCGraphicsBufferPushElements(&_elementBuffer, elementCount, self);
132+
133+
CCRenderCommandDraw *previous = _lastDrawCommand;
134+
if(previous && previous->_renderState == renderState && previous->_globalSortOrder == globalSortOrder){
135+
// Batch with the previous command.
136+
[previous batchElements:(GLsizei)elementCount];
137+
} else {
138+
// Start a new command.
139+
CCRenderCommandDraw *command = [[CCRenderCommandDraw alloc] initWithMode:GL_TRIANGLES renderState:renderState first:(GLint)firstElement elements:(GLsizei)elementCount globalSortOrder:globalSortOrder];
140+
[_queue addObject:command];
141+
[command release];
142+
143+
_lastDrawCommand = command;
144+
}
145+
146+
return (CCRenderBuffer){vertexes, elements, firstVertex};
147+
}
148+
149+
-(CCRenderBuffer)enqueueLines:(NSUInteger)lineCount andVertexes:(NSUInteger)vertexCount withState:(CCRenderState *)renderState globalSortOrder:(NSInteger)globalSortOrder;
150+
{
151+
// Need to record the first vertex or element index before pushing more vertexes.
152+
size_t firstVertex = _vertexBuffer.count;
153+
size_t firstElement = _elementBuffer.count;
154+
155+
size_t elementCount = 2*lineCount;
156+
CCVertex *vertexes = CCGraphicsBufferPushElements(&_vertexBuffer, vertexCount, self);
157+
GLushort *elements = CCGraphicsBufferPushElements(&_elementBuffer, elementCount, self);
158+
159+
CCRenderCommandDraw *command = [[CCRenderCommandDraw alloc] initWithMode:GL_LINES renderState:renderState first:(GLint)firstElement elements:(GLsizei)elementCount globalSortOrder:globalSortOrder];
160+
[_queue addObject:command];
161+
[command release];
162+
163+
// Line drawing commands are currently intended for debugging and cannot be batched.
164+
_lastDrawCommand = nil;
165+
166+
return(CCRenderBuffer){vertexes, elements, firstVertex};
167+
}
168+
169+
@end
170+
171+
172+
@implementation CCRenderer(NoARCPrivate)
173+
174+
-(void)setRenderState:(CCRenderState *)renderState
175+
{
176+
[self bindVAO:YES];
177+
if(renderState == _renderState) return;
178+
179+
glPushGroupMarkerEXT(0, "CCRenderer: Render State");
180+
181+
// Set the blending state.
182+
NSDictionary *blendOptions = renderState->_blendMode->_options;
183+
if(blendOptions != _blendOptions){
184+
glInsertEventMarkerEXT(0, "Blending mode");
185+
186+
if(blendOptions == CCBLEND_DISABLED_OPTIONS){
187+
if(_blendOptions != CCBLEND_DISABLED_OPTIONS) glDisable(GL_BLEND);
188+
} else {
189+
if(_blendOptions == nil || _blendOptions == CCBLEND_DISABLED_OPTIONS) glEnable(GL_BLEND);
190+
191+
glBlendFuncSeparate(
192+
[blendOptions[CCBlendFuncSrcColor] unsignedIntValue],
193+
[blendOptions[CCBlendFuncDstColor] unsignedIntValue],
194+
[blendOptions[CCBlendFuncSrcAlpha] unsignedIntValue],
195+
[blendOptions[CCBlendFuncDstAlpha] unsignedIntValue]
196+
);
197+
198+
glBlendEquationSeparate(
199+
[blendOptions[CCBlendEquationColor] unsignedIntValue],
200+
[blendOptions[CCBlendEquationAlpha] unsignedIntValue]
201+
);
202+
}
203+
204+
_blendOptions = blendOptions;
205+
}
206+
207+
// Bind the shader.
208+
CCShader *shader = renderState->_shader;
209+
if(shader != _shader){
210+
glInsertEventMarkerEXT(0, "Shader");
211+
212+
glUseProgram(shader->_program);
213+
214+
_shader = shader;
215+
_shaderUniforms = nil;
216+
}
217+
218+
// Set the shader's uniform state.
219+
NSDictionary *shaderUniforms = renderState->_shaderUniforms;
220+
NSDictionary *globalShaderUniforms = _globalShaderUniforms;
221+
if(shaderUniforms != _shaderUniforms){
222+
glInsertEventMarkerEXT(0, "Uniforms");
223+
224+
NSDictionary *setters = shader->_uniformSetters;
225+
for(NSString *uniformName in setters){
226+
CCUniformSetter setter = setters[uniformName];
227+
setter(self, shaderUniforms, globalShaderUniforms);
228+
}
229+
_shaderUniforms = shaderUniforms;
230+
}
231+
232+
CC_CHECK_GL_ERROR_DEBUG();
233+
glPopGroupMarkerEXT();
234+
235+
_renderState = renderState;
236+
return;
237+
}
238+
239+
@end

cocos2d/CCRenderer.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,6 @@ extern const NSString *CCBlendEquationAlpha;
194194
/// Enqueued commands are sorted by their globalSortOrder value before rendering. Currently this value is 0 for everything except custom draw methods.
195195
-(void)enqueueClear:(GLbitfield)mask color:(GLKVector4)color4 depth:(GLclampf)depth stencil:(GLint)stencil globalSortOrder:(NSInteger)globalSortOrder;
196196

197-
/// Enqueue a drawing command for some triangles.
198-
/// Returns a CCRendereBuffer that you should fill using CCRenderBufferSetVertex() and CCRenderBufferSetTriangle().
199-
/// Enqueued commands are sorted by their globalSortOrder value before rendering. Currently this value is 0 for everything except custom draw methods.
200-
-(CCRenderBuffer)enqueueTriangles:(NSUInteger)triangleCount andVertexes:(NSUInteger)vertexCount withState:(CCRenderState *)renderState globalSortOrder:(NSInteger)globalSortOrder;
201-
202-
/// Enqueue a drawing command for some lines.
203-
/// Returns a CCRendereBuffer that you should fill using CCRenderBufferSetVertex() and CCRenderBufferSetLine().
204-
/// Note: These are primitive OpenGL lines that you'll only want to use for debug rendering. They are not batched.
205-
/// Enqueued commands are sorted by their globalSortOrder value before rendering. Currently this value is 0 for everything except custom draw methods.
206-
-(CCRenderBuffer)enqueueLines:(NSUInteger)lineCount andVertexes:(NSUInteger)vertexCount withState:(CCRenderState *)renderState globalSortOrder:(NSInteger)globalSortOrder;
207-
208197
/// Enqueue a block that performs GL commands. The debugLabel is optional and will show up in in the GLES frame debugger.
209198
/// Enqueued commands are sorted by their globalSortOrder value before rendering. Currently this value is 0 for everything except custom draw methods.
210199
-(void)enqueueBlock:(void (^)())block globalSortOrder:(NSInteger)globalSortOrder debugLabel:(NSString *)debugLabel threadSafe:(BOOL)threadSafe;
@@ -222,3 +211,19 @@ extern const NSString *CCBlendEquationAlpha;
222211
-(void)popGroupWithDebugLabel:(NSString *)debugLabel globalSortOrder:(NSInteger)globalSortOrder;
223212

224213
@end
214+
215+
@interface CCRenderer(NoARC)
216+
217+
/// Enqueue a drawing command for some triangles.
218+
/// Returns a CCRendereBuffer that you should fill using CCRenderBufferSetVertex() and CCRenderBufferSetTriangle().
219+
/// Enqueued commands are sorted by their globalSortOrder value before rendering. Currently this value is 0 for everything except custom draw methods.
220+
-(CCRenderBuffer)enqueueTriangles:(NSUInteger)triangleCount andVertexes:(NSUInteger)vertexCount withState:(CCRenderState *)renderState globalSortOrder:(NSInteger)globalSortOrder;
221+
222+
/// Enqueue a drawing command for some lines.
223+
/// Returns a CCRendereBuffer that you should fill using CCRenderBufferSetVertex() and CCRenderBufferSetLine().
224+
/// Note: These are primitive OpenGL lines that you'll only want to use for debug rendering. They are not batched.
225+
/// Enqueued commands are sorted by their globalSortOrder value before rendering. Currently this value is 0 for everything except custom draw methods.
226+
-(CCRenderBuffer)enqueueLines:(NSUInteger)lineCount andVertexes:(NSUInteger)vertexCount withState:(CCRenderState *)renderState globalSortOrder:(NSInteger)globalSortOrder;
227+
228+
@end
229+

0 commit comments

Comments
 (0)