Skip to content

Commit 672a3f3

Browse files
committed
Partial changes for uniform binding before realizing I’ve been working out of the wrong branch. -_-
1 parent 741c5b0 commit 672a3f3

File tree

8 files changed

+126
-46
lines changed

8 files changed

+126
-46
lines changed

cocos2d/CCNoARC.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ -(CCRenderBuffer)enqueueTriangles:(NSUInteger)triangleCount andVertexes:(NSUInte
127127
NSUInteger firstElement = _elementBuffer->_count;
128128

129129
NSUInteger elementCount = 3*triangleCount;
130-
CCVertex *vertexes = CCGraphicsBufferPushElements(_vertexBuffer, vertexCount, self);
131-
uint16_t *elements = CCGraphicsBufferPushElements(_elementBuffer, elementCount, self);
130+
CCVertex *vertexes = CCGraphicsBufferPushElements(_vertexBuffer, vertexCount);
131+
uint16_t *elements = CCGraphicsBufferPushElements(_elementBuffer, elementCount);
132132

133133
CCRenderCommandDraw *previous = _lastDrawCommand;
134134
if(previous && previous->_renderState == renderState && previous->_globalSortOrder == globalSortOrder){
@@ -153,8 +153,8 @@ -(CCRenderBuffer)enqueueLines:(NSUInteger)lineCount andVertexes:(NSUInteger)vert
153153
NSUInteger firstElement = _elementBuffer->_count;
154154

155155
NSUInteger elementCount = 2*lineCount;
156-
CCVertex *vertexes = CCGraphicsBufferPushElements(_vertexBuffer, vertexCount, self);
157-
uint16_t *elements = CCGraphicsBufferPushElements(_elementBuffer, elementCount, self);
156+
CCVertex *vertexes = CCGraphicsBufferPushElements(_vertexBuffer, vertexCount);
157+
uint16_t *elements = CCGraphicsBufferPushElements(_elementBuffer, elementCount);
158158

159159
CCRenderCommandDraw *command = [[CCRenderCommandDrawClass alloc] initWithMode:CCRenderCommandDrawLines renderState:renderState first:firstElement count:elementCount globalSortOrder:globalSortOrder];
160160
[_queue addObject:command];

cocos2d/CCRenderer_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ typedef NS_ENUM(NSUInteger, CCGraphicsBufferType){
148148
/// Return a pointer to an array of elements that is 'requestedCount' in size.
149149
/// The buffer is resized by calling [CCGraphicsBuffer resize:] if necessary.
150150
static inline void *
151-
CCGraphicsBufferPushElements(CCGraphicsBuffer *buffer, size_t requestedCount, CCRenderer *renderer)
151+
CCGraphicsBufferPushElements(CCGraphicsBuffer *buffer, size_t requestedCount)
152152
{
153153
NSCAssert(requestedCount > 0, @"Requested count must be positive.");
154154

cocos2d/CCShader.m

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#import "CCCache.h"
3737
#import "CCGL.h"
3838
#import "CCRenderDispatch.h"
39+
#import "CCMetalSupport_Private.h"
3940

4041

4142
const NSString *CCShaderUniformProjection = @"cc_Projection";
@@ -166,6 +167,7 @@ @implementation CCShaderCache
166167

167168
-(id)createSharedDataForKey:(id<NSCopying>)key
168169
{
170+
#warning TODO Need Metal path here.
169171
NSString *shaderName = (NSString *)key;
170172

171173
NSString *fragmentName = [shaderName stringByAppendingPathExtension:@"fsh"];
@@ -352,8 +354,10 @@ @implementation CCShader {
352354

353355
//MARK: Init Methods:
354356

355-
-(instancetype)initWithProgram:(GLuint)program uniformSetters:(NSDictionary *)uniformSetters ownsProgram:(BOOL)ownsProgram
357+
-(instancetype)initWithGLProgram:(GLuint)program uniformSetters:(NSDictionary *)uniformSetters ownsProgram:(BOOL)ownsProgram
356358
{
359+
NSAssert([CCConfiguration sharedConfiguration].graphicsAPI == CCGraphicsAPIGL, @"GL graphics not configured.");
360+
357361
if((self = [super init])){
358362
_program = program;
359363
_uniformSetters = uniformSetters;
@@ -363,6 +367,19 @@ -(instancetype)initWithProgram:(GLuint)program uniformSetters:(NSDictionary *)un
363367
return self;
364368
}
365369

370+
-(instancetype)initWithMetalVertexFunction:(id<MTLFunction>)vertexFunction fragmentFunction:(id<MTLFunction>)fragmentFunction
371+
{
372+
if((self = [super init])){
373+
_vertexFunction = vertexFunction;
374+
_fragmentFunction = fragmentFunction;
375+
376+
#warning TODO setup _uniformSetters
377+
// _uniformSetters = uniformSetters;
378+
}
379+
380+
return self;
381+
}
382+
366383
-(instancetype)initWithVertexShaderSource:(NSString *)vertexSource fragmentShaderSource:(NSString *)fragmentSource
367384
{
368385
#warning TODO
@@ -393,7 +410,7 @@ -(instancetype)initWithVertexShaderSource:(NSString *)vertexSource fragmentShade
393410

394411
CCGL_DEBUG_POP_GROUP_MARKER();
395412

396-
blockself = [blockself initWithProgram:program uniformSetters:UniformSettersForProgram(program) ownsProgram:YES];
413+
blockself = [blockself initWithGLProgram:program uniformSetters:UniformSettersForProgram(program) ownsProgram:YES];
397414
});
398415

399416
return blockself;
@@ -418,7 +435,7 @@ - (void)dealloc
418435

419436
-(instancetype)copyWithZone:(NSZone *)zone
420437
{
421-
return [[CCShader allocWithZone:zone] initWithProgram:_program uniformSetters:_uniformSetters ownsProgram:NO];
438+
return [[CCShader allocWithZone:zone] initWithGLProgram:_program uniformSetters:_uniformSetters ownsProgram:NO];
422439
}
423440

424441
static CCShaderCache *CC_SHADER_CACHE = nil;
@@ -432,27 +449,49 @@ +(void)initialize
432449
// +initialize may be called due to loading a subclass.
433450
if(self != [CCShader class]) return;
434451

435-
CC_SHADER_CACHE = [[CCShaderCache alloc] init];
436-
437-
// Setup the builtin shaders.
438-
CC_SHADER_POS_COLOR = [[self alloc] initWithFragmentShaderSource:@"void main(){gl_FragColor = cc_FragColor;}"];
439-
CC_SHADER_POS_COLOR.debugName = @"CCPositionColorShader";
440-
441-
CC_SHADER_POS_TEX_COLOR = [[self alloc] initWithFragmentShaderSource:@"void main(){gl_FragColor = cc_FragColor*texture2D(cc_MainTexture, cc_FragTexCoord1);}"];
442-
CC_SHADER_POS_TEX_COLOR.debugName = @"CCPositionTextureColorShader";
443-
444-
CC_SHADER_POS_TEXA8_COLOR = [[self alloc] initWithFragmentShaderSource:@"void main(){gl_FragColor = cc_FragColor*texture2D(cc_MainTexture, cc_FragTexCoord1).a;}"];
445-
CC_SHADER_POS_TEXA8_COLOR.debugName = @"CCPositionTextureA8ColorShader";
452+
NSAssert([CCConfiguration sharedConfiguration].graphicsAPI != CCGraphicsAPIInvalid, @"Graphics API not configured.");
446453

447-
CC_SHADER_POS_TEX_COLOR_ALPHA_TEST = [[self alloc] initWithFragmentShaderSource:CC_GLSL(
448-
uniform float cc_AlphaTestValue;
449-
void main(){
450-
vec4 tex = texture2D(cc_MainTexture, cc_FragTexCoord1);
451-
if(tex.a <= cc_AlphaTestValue) discard;
452-
gl_FragColor = cc_FragColor*tex;
453-
}
454-
)];
455-
CC_SHADER_POS_TEX_COLOR_ALPHA_TEST.debugName = @"CCPositionTextureColorAlphaTestShader";
454+
#if __CC_METAL_SUPPORTED_AND_ENABLED
455+
if([CCConfiguration sharedConfiguration].graphicsAPI == CCGraphicsAPIMetal){
456+
id<MTLLibrary> library = [CCMetalContext currentContext].library;
457+
id<MTLFunction> vertex = [library newFunctionWithName:@"CCVertexFunctionDefault"];
458+
459+
CC_SHADER_POS_COLOR = [[self alloc] initWithMetalVertexFunction:vertex fragmentFunction:[library newFunctionWithName:@"CCFragmentFunctionDefaultColor"]];
460+
CC_SHADER_POS_COLOR.debugName = @"CCPositionColorShader";
461+
462+
CC_SHADER_POS_TEX_COLOR = [[self alloc] initWithMetalVertexFunction:vertex fragmentFunction:[library newFunctionWithName:@"CCFragmentFunctionDefaultTextureColor"]];
463+
CC_SHADER_POS_TEX_COLOR.debugName = @"CCPositionTextureColorShader";
464+
465+
CC_SHADER_POS_TEXA8_COLOR = [[self alloc] initWithMetalVertexFunction:vertex fragmentFunction:[library newFunctionWithName:@"CCFragmentFunctionDefaultTextureA8Color"]];
466+
CC_SHADER_POS_TEXA8_COLOR.debugName = @"CCPositionTextureA8ColorShader";
467+
468+
CC_SHADER_POS_TEX_COLOR_ALPHA_TEST = [[self alloc] initWithMetalVertexFunction:vertex fragmentFunction:[library newFunctionWithName:@"CCFragmentFunctionUnsupported"]];
469+
CC_SHADER_POS_TEX_COLOR_ALPHA_TEST.debugName = @"CCPositionTextureColorAlphaTestShader";
470+
} else
471+
#endif
472+
{
473+
CC_SHADER_CACHE = [[CCShaderCache alloc] init];
474+
475+
// Setup the builtin shaders.
476+
CC_SHADER_POS_COLOR = [[self alloc] initWithFragmentShaderSource:@"void main(){gl_FragColor = cc_FragColor;}"];
477+
CC_SHADER_POS_COLOR.debugName = @"CCPositionColorShader";
478+
479+
CC_SHADER_POS_TEX_COLOR = [[self alloc] initWithFragmentShaderSource:@"void main(){gl_FragColor = cc_FragColor*texture2D(cc_MainTexture, cc_FragTexCoord1);}"];
480+
CC_SHADER_POS_TEX_COLOR.debugName = @"CCPositionTextureColorShader";
481+
482+
CC_SHADER_POS_TEXA8_COLOR = [[self alloc] initWithFragmentShaderSource:@"void main(){gl_FragColor = cc_FragColor*texture2D(cc_MainTexture, cc_FragTexCoord1).a;}"];
483+
CC_SHADER_POS_TEXA8_COLOR.debugName = @"CCPositionTextureA8ColorShader";
484+
485+
CC_SHADER_POS_TEX_COLOR_ALPHA_TEST = [[self alloc] initWithFragmentShaderSource:CC_GLSL(
486+
uniform float cc_AlphaTestValue;
487+
void main(){
488+
vec4 tex = texture2D(cc_MainTexture, cc_FragTexCoord1);
489+
if(tex.a <= cc_AlphaTestValue) discard;
490+
gl_FragColor = cc_FragColor*tex;
491+
}
492+
)];
493+
CC_SHADER_POS_TEX_COLOR_ALPHA_TEST.debugName = @"CCPositionTextureColorAlphaTestShader";
494+
}
456495
}
457496

458497
+(instancetype)positionColorShader

cocos2d/CCShader_Private.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#import "ccMacros.h"
2+
3+
#if __CC_METAL_SUPPORTED_AND_ENABLED
4+
#import <Metal/Metal.h>
5+
#endif
6+
17
#import "CCShader.h"
28

39
@class CCRenderer;
@@ -11,6 +17,11 @@ typedef void (^CCUniformSetter)(
1117
@public
1218
GLuint _program;
1319
NSDictionary *_uniformSetters;
20+
21+
// TODO This should really be split into a separate subclass somehow.
22+
#if __CC_METAL_SUPPORTED_AND_ENABLED
23+
id<MTLFunction> _vertexFunction, _fragmentFunction;
24+
#endif
1425
}
1526

1627
@end

cocos2d/CCTexture.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ + (id) textureWithFile:(NSString*)file
227227

228228
- (id) initWithData:(const void*)data pixelFormat:(CCTexturePixelFormat)pixelFormat pixelsWide:(NSUInteger)width pixelsHigh:(NSUInteger)height contentSizeInPixels:(CGSize)sizeInPixels contentScale:(CGFloat)contentScale
229229
{
230+
NSAssert([CCConfiguration sharedConfiguration].graphicsAPI != CCGraphicsAPIInvalid, @"Graphics API not configured.");
231+
230232
if((self = [super init])) {
231233
#if __CC_METAL_SUPPORTED_AND_ENABLED
232234
if([CCConfiguration sharedConfiguration].graphicsAPI == CCGraphicsAPIMetal){

cocos2d/CCTexture_Private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
/* texture name */
5353
@property(nonatomic,readonly) GLuint name;
5454

55+
// TODO This should really be split into a separate subclass somehow.
5556
#if __CC_METAL_SUPPORTED_AND_ENABLED
5657
@property(nonatomic,readonly) id<MTLTexture> metalTexture;
5758
@property(nonatomic,readonly) id<MTLSamplerState> metalSampler;

cocos2d/Platforms/iOS/CCMetalSupport.m

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,8 @@ -(void)prepare
213213
MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
214214
pipelineStateDescriptor.sampleCount = 1;
215215

216-
id<MTLLibrary> library = context.library;
217-
#warning TEMP Hard coded shaders.
218-
pipelineStateDescriptor.vertexFunction = [library newFunctionWithName:@"CCVertexFunctionDefault"];
219-
if(_shader == [CCShader positionColorShader]){
220-
pipelineStateDescriptor.fragmentFunction = [library newFunctionWithName:@"CCFragmentFunctionDefaultColor"];
221-
} else if(_shader == [CCShader positionTextureColorShader]){
222-
pipelineStateDescriptor.fragmentFunction = [library newFunctionWithName:@"CCFragmentFunctionDefaultTextureColor"];
223-
} else if(_shader == [CCShader positionTextureA8ColorShader]){
224-
pipelineStateDescriptor.fragmentFunction = [library newFunctionWithName:@"CCFragmentFunctionDefaultTextureA8Color"];
225-
} else {
226-
pipelineStateDescriptor.fragmentFunction = [library newFunctionWithName:@"TempUnsupported"];
227-
}
216+
pipelineStateDescriptor.vertexFunction = _shader->_vertexFunction;
217+
pipelineStateDescriptor.fragmentFunction = _shader->_fragmentFunction;
228218

229219
NSDictionary *blendOptions = _blendMode.options;
230220
MTLRenderPipelineColorAttachmentDescriptor *colorDescriptor = [MTLRenderPipelineColorAttachmentDescriptor new];
@@ -247,6 +237,19 @@ -(void)transitionRenderer:(CCRenderer *)renderer FromState:(CCRenderState *)prev
247237
id<MTLRenderCommandEncoder> renderEncoder = CCMetalContextCurrent().currentRenderCommandEncoder;
248238
[renderEncoder setRenderPipelineState:_renderPipelineState];
249239

240+
// CCGraphicsBufferMetal *uniformGraphicsBuffer = nil;
241+
// id<MTLBuffer> uniformMetalBuffer = uniformGraphicsBuffer->_buffer;
242+
//
243+
// // Set the global uniform buffers.
244+
// [renderEncoder setVertexBuffer:uniformMetalBuffer offset:0 atIndex:1];
245+
// [renderEncoder setFragmentBuffer:uniformMetalBuffer offset:0 atIndex:1];
246+
//
247+
// // Set the uniform buffers.
248+
// CCGraphicsBufferPushElements(uniformGraphicsBuffer, # of bytes);
249+
// [renderEncoder setVertexBuffer:uniformMetalBuffer offset:offset atIndex:2];
250+
// [renderEncoder setFragmentBuffer:uniformMetalBuffer offset:offset atIndex:2];
251+
252+
// Set textures and samplers.
250253
CCTexture *mainTexture = _shaderUniforms[CCShaderUniformMainTexture];
251254
[renderEncoder setFragmentSamplerState:mainTexture.metalSampler atIndex:0];
252255
[renderEncoder setFragmentTexture:mainTexture.metalTexture atIndex:0];

cocos2d/Platforms/iOS/CCShaders.metal

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,39 @@
33

44
using namespace metal;
55

6+
// Default vertex attributes.
67
typedef struct CCVertex {
78
float4 position;
89
float2 texCoord1, texCoord2;
910
float4 color;
1011
} CCVertex;
1112

13+
// Default fragment varyings.
1214
typedef struct CCFragData {
1315
float4 position [[position]];
1416
float2 texCoord1;
1517
float2 texCoord2;
1618
half4 color;
1719
} CCFragData;
1820

21+
// Standard set of global uniform values.
22+
typedef struct CCGlobalUniforms {
23+
float4x4 projection;
24+
float4x4 projectionInv;
25+
float2 viewSize;
26+
float2 viewSizeInPixels;
27+
float4 time;
28+
float4 sinTime;
29+
float4 cosTime;
30+
float4 random01;
31+
} CCGlobalUniforms;
32+
33+
// Default vertex function.
1934
vertex CCFragData
2035
CCVertexFunctionDefault(
21-
device CCVertex* verts [[buffer(0)]],
36+
const device CCVertex* verts [[buffer(0)]],
37+
const device CCGlobalUniforms *globalUniforms [[buffer(1)]],
38+
const device CCGlobalUniforms *uniforms [[buffer(2)]],
2239
unsigned int vid [[vertex_id]]
2340
){
2441
CCFragData out;
@@ -33,14 +50,17 @@ CCVertexFunctionDefault(
3350

3451
fragment half4
3552
CCFragmentFunctionDefaultColor(
36-
CCFragData in [[stage_in]]
53+
const CCFragData in [[stage_in]],
54+
const device CCGlobalUniforms *globals [[buffer(0)]]
3755
){
3856
return in.color;
3957
}
4058

4159
fragment half4
4260
CCFragmentFunctionDefaultTextureColor(
43-
CCFragData in [[stage_in]],
61+
const CCFragData in [[stage_in]],
62+
const device CCGlobalUniforms *globalUniforms [[buffer(1)]],
63+
const device CCGlobalUniforms *uniforms [[buffer(2)]],
4464
texture2d<half> mainTexture [[texture(0)]],
4565
sampler mainTextureSampler [[sampler(0)]]
4666
){
@@ -49,16 +69,20 @@ CCFragmentFunctionDefaultTextureColor(
4969

5070
fragment half4
5171
CCFragmentFunctionDefaultTextureA8Color(
52-
CCFragData in [[stage_in]],
72+
const CCFragData in [[stage_in]],
73+
const device CCGlobalUniforms *globalUniforms [[buffer(1)]],
74+
const device CCGlobalUniforms *uniforms [[buffer(2)]],
5375
texture2d<half> mainTexture [[texture(0)]],
5476
sampler mainTextureSampler [[sampler(0)]]
5577
){
5678
return in.color*mainTexture.sample(mainTextureSampler, in.texCoord1).a;
5779
}
5880

5981
fragment half4
60-
TempUnsupported(
61-
CCFragData in [[stage_in]],
82+
CCFragmentFunctionUnsupported(
83+
const CCFragData in [[stage_in]],
84+
const device CCGlobalUniforms *globalUniforms [[buffer(1)]],
85+
const device CCGlobalUniforms *uniforms [[buffer(2)]],
6286
texture2d<half> mainTexture [[texture(0)]],
6387
sampler mainTextureSampler [[sampler(0)]]
6488
){

0 commit comments

Comments
 (0)