Skip to content

Commit b34b959

Browse files
committed
Basic rendering finally works again.
1 parent 5e0e52d commit b34b959

File tree

7 files changed

+105
-10
lines changed

7 files changed

+105
-10
lines changed

cocos2d-ios.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@
10751075
D39FA9C718C1BC6B00441627 /* CCShader_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCShader_Private.h; sourceTree = "<group>"; };
10761076
D3A2E7D0181E12750033614C /* CCPhysicsShape.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPhysicsShape.h; sourceTree = "<group>"; };
10771077
D3A2E7D1181E12750033614C /* CCPhysicsShape.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCPhysicsShape.m; sourceTree = "<group>"; };
1078+
D3C12815199D40AC005D2119 /* CCShaders.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = CCShaders.metal; sourceTree = "<group>"; };
10781079
E01E6D8A121F130E001A484F /* CCLabelBMFont.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = CCLabelBMFont.h; sourceTree = "<group>"; };
10791080
E01E6D8B121F130E001A484F /* CCLabelBMFont.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = CCLabelBMFont.m; sourceTree = "<group>"; };
10801081
E02BB6D4126CA93A006E46A2 /* CCAnimationCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCAnimationCache.h; sourceTree = "<group>"; };
@@ -1879,6 +1880,7 @@
18791880
E0EAD0E9121F4B4600B0C81C /* iOS */ = {
18801881
isa = PBXGroup;
18811882
children = (
1883+
D3C12815199D40AC005D2119 /* CCShaders.metal */,
18821884
D3903B12199528B6003AA81A /* CCMetalView.h */,
18831885
D3903B13199528B6003AA81A /* CCMetalView.m */,
18841886
B78AE46A17E7AF6C0028BE0B /* UITouch+CC.h */,

cocos2d-tests-ios.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
D34CD3C518ABF4AA00D8E537 /* CCRendererTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D34CD3C418ABF4AA00D8E537 /* CCRendererTests.m */; };
7979
D3870C6018B440150033D885 /* SpritePerformanceTest.m in Sources */ = {isa = PBXBuildFile; fileRef = D3870C5F18B440150033D885 /* SpritePerformanceTest.m */; };
8080
D3B2A7E5187DD60B00406C5A /* CCBMFontTest.m in Sources */ = {isa = PBXBuildFile; fileRef = D3B2A7E4187DD60B00406C5A /* CCBMFontTest.m */; };
81+
D3C1281D199D43E0005D2119 /* CCShaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = D3C1281C199D43E0005D2119 /* CCShaders.metal */; };
8182
D3C9C867188078B900C58900 /* CCSchedulerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = D3C9C866188078B900C58900 /* CCSchedulerTest.m */; };
8283
D3D6CF5F18BD5F0500A51531 /* CCRendererTest.m in Sources */ = {isa = PBXBuildFile; fileRef = D3D6CF5E18BD5F0500A51531 /* CCRendererTest.m */; };
8384
D4AFE9131977100000261299 /* A Damn Mess.ttf in Resources */ = {isa = PBXBuildFile; fileRef = D4AFE8D81977100000261299 /* A Damn Mess.ttf */; };
@@ -281,6 +282,7 @@
281282
D34CD3C418ABF4AA00D8E537 /* CCRendererTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCRendererTests.m; sourceTree = "<group>"; };
282283
D3870C5F18B440150033D885 /* SpritePerformanceTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SpritePerformanceTest.m; path = "cocos2d-ui-tests/tests/SpritePerformanceTest.m"; sourceTree = SOURCE_ROOT; };
283284
D3B2A7E4187DD60B00406C5A /* CCBMFontTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCBMFontTest.m; path = "cocos2d-ui-tests/tests/CCBMFontTest.m"; sourceTree = SOURCE_ROOT; };
285+
D3C1281C199D43E0005D2119 /* CCShaders.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; name = CCShaders.metal; path = cocos2d/Platforms/iOS/CCShaders.metal; sourceTree = SOURCE_ROOT; };
284286
D3C9C866188078B900C58900 /* CCSchedulerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSchedulerTest.m; path = "cocos2d-ui-tests/tests/CCSchedulerTest.m"; sourceTree = SOURCE_ROOT; };
285287
D3D6CF5E18BD5F0500A51531 /* CCRendererTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCRendererTest.m; path = "cocos2d-ui-tests/tests/CCRendererTest.m"; sourceTree = SOURCE_ROOT; };
286288
D4AFE8D81977100000261299 /* A Damn Mess.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "A Damn Mess.ttf"; sourceTree = "<group>"; };
@@ -595,6 +597,7 @@
595597
B7E2620117E7D34D007067F0 /* Resources-iOS */ = {
596598
isa = PBXGroup;
597599
children = (
600+
D3C1281C199D43E0005D2119 /* CCShaders.metal */,
598601
B7E2620217E7D34D007067F0 /* [email protected] */,
599602
B7E2620317E7D34D007067F0 /* Default-Landscape~ipad.png */,
600603
B7E2620417E7D34D007067F0 /* Default.png */,
@@ -941,6 +944,7 @@
941944
B71B088217EA5B6A0082EBC0 /* CCScrollViewTest.m in Sources */,
942945
B7EE697918186D5200B983FE /* CCTextFieldTest.m in Sources */,
943946
D29698121926DF7500BBB097 /* CCEffectPongTest.m in Sources */,
947+
D3C1281D199D43E0005D2119 /* CCShaders.metal in Sources */,
944948
B7E2620017E7D321007067F0 /* AppDelegate.m in Sources */,
945949
0E28FE1A197FCE4500F78989 /* CCCacheTest.m in Sources */,
946950
B77060AB1832E3310043CC67 /* CCTextureCacheTest.m in Sources */,

cocos2d/CCRenderer_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ CCGraphicsBufferPushElements(CCGraphicsBuffer *buffer, size_t requestedCount, CC
172172

173173
@interface CCRenderer(){
174174
@public
175+
CCGraphicsBuffer *_vertexBuffer;
175176
CCGraphicsBuffer *_elementBuffer;
176177

177178
@private
178-
CCGraphicsBuffer *_vertexBuffer;
179179
id<CCGraphicsBufferBindings> _bufferBindings;
180180

181181
NSDictionary *_globalShaderUniforms;

cocos2d/Platforms/iOS/CCMetalSupport.m

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,35 @@
2727

2828
#if __CC_METAL_SUPPORTED_AND_ENABLED
2929

30-
@implementation CCMetalContext
30+
@implementation CCMetalContext {
31+
@public
32+
id<MTLRenderPipelineState> _tempPiplelineState;
33+
}
3134

3235
-(instancetype)init
3336
{
3437
if((self = [super init])){
3538
_device = MTLCreateSystemDefaultDevice();
39+
_library = [_device newDefaultLibrary];
40+
3641
_commandQueue = [_device newCommandQueue];
3742
_currentCommandBuffer = [_commandQueue commandBuffer];
43+
44+
#warning Temporary
45+
id <MTLFunction> vertexProgram = [_library newFunctionWithName:@"CCVertexFunctionDefault"];
46+
id <MTLFunction> fragmentProgram = [_library newFunctionWithName:@"CCFragmentFunctionDefaultColor"];
47+
48+
MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
49+
[pipelineStateDescriptor setSampleCount: 1];
50+
[pipelineStateDescriptor setVertexFunction:vertexProgram];
51+
[pipelineStateDescriptor setFragmentFunction:fragmentProgram];
52+
53+
MTLRenderPipelineColorAttachmentDescriptor *colorDescriptor = [MTLRenderPipelineColorAttachmentDescriptor new];
54+
colorDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm;
55+
colorDescriptor.blendingEnabled = NO;
56+
pipelineStateDescriptor.colorAttachments[0] = colorDescriptor;
57+
58+
_tempPiplelineState = [_device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:nil];
3859
}
3960

4061
return self;
@@ -81,6 +102,8 @@ -(void)prepareCommandBuffer
81102

82103
-(void)commitCurrentCommandBuffer
83104
{
105+
[_currentRenderCommandEncoder endEncoding];
106+
84107
[_currentCommandBuffer commit];
85108
_currentCommandBuffer = nil;
86109
}
@@ -130,18 +153,23 @@ -(void)commit; {}
130153
@implementation CCRenderCommandDrawMetal
131154

132155
static const MTLPrimitiveType MetalDrawModes[] = {
133-
GL_TRIANGLES,
134-
GL_LINES,
156+
MTLPrimitiveTypeTriangle,
157+
MTLPrimitiveTypeLine,
135158
};
136159

137160
-(void)invokeOnRenderer:(CCRenderer *)renderer
138161
{
139-
id<MTLRenderCommandEncoder> renderEncoder = [CCMetalContext currentContext].currentRenderCommandEncoder;
140-
CCGraphicsBufferMetal *indexBuffer = (CCGraphicsBufferMetal *)renderer->_elementBuffer;
162+
CCMetalContext *context = [CCMetalContext currentContext];
163+
id<MTLRenderCommandEncoder> renderEncoder = context.currentRenderCommandEncoder;
164+
165+
id<MTLBuffer> vertexBuffer = ((CCGraphicsBufferMetal *)renderer->_vertexBuffer)->_buffer;
166+
id<MTLBuffer> indexBuffer = ((CCGraphicsBufferMetal *)renderer->_elementBuffer)->_buffer;
141167

142168
[renderEncoder pushDebugGroup:@"CCRendererCommandDraw: Invoke"];
143-
[renderer setRenderState:_renderState];
144-
[renderEncoder drawIndexedPrimitives:MetalDrawModes[_mode] indexCount:_count indexType:MTLIndexTypeUInt16 indexBuffer:indexBuffer->_buffer indexBufferOffset:2*_first];
169+
// [renderer setRenderState:_renderState];
170+
[renderEncoder setRenderPipelineState:context->_tempPiplelineState];
171+
[renderEncoder setVertexBuffer:vertexBuffer offset:0 atIndex:0];
172+
[renderEncoder drawIndexedPrimitives:MetalDrawModes[_mode] indexCount:_count indexType:MTLIndexTypeUInt16 indexBuffer:indexBuffer indexBufferOffset:2*_first];
145173
[renderEncoder popDebugGroup];
146174
}
147175

cocos2d/Platforms/iOS/CCMetalSupport_Private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
@interface CCMetalContext : NSObject
3535

3636
@property(nonatomic, readonly) id<MTLDevice> device;
37+
@property(nonatomic, readonly) id<MTLLibrary> library;
38+
3739
@property(nonatomic, readonly) id<MTLCommandQueue> commandQueue;
3840
@property(nonatomic, readonly) id<MTLCommandBuffer> currentCommandBuffer;
3941

cocos2d/Platforms/iOS/CCMetalView.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,15 @@ -(void)beginFrame
157157
// }
158158

159159
id<CAMetalDrawable> drawable = [self.metalLayer nextDrawable];
160+
[_context.currentCommandBuffer presentDrawable:drawable];
161+
160162
_currentDrawable = drawable;
161163
_context.destinationTexture = drawable.texture;
162164
}
163165

164166
- (void)presentFrame
165167
{
166168
[_context commitCurrentCommandBuffer];
167-
168-
[_currentDrawable present];
169169
_currentDrawable = nil;
170170
}
171171

cocos2d/Platforms/iOS/CCShaders.metal

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <metal_stdlib>
2+
#include <simd/simd.h>
3+
4+
using namespace metal;
5+
6+
typedef struct CCVertex {
7+
float4 position;
8+
float2 texCoord1, texCoord2;
9+
float4 color;
10+
} CCVertex;
11+
12+
typedef struct CCFragData {
13+
float4 position [[position]];
14+
float2 texCoord1;
15+
float2 texCoord2;
16+
half4 color;
17+
} CCFragData;
18+
19+
vertex CCFragData
20+
CCVertexFunctionDefault(
21+
device CCVertex* verts [[buffer(0)]],
22+
unsigned int vid [[vertex_id]]
23+
){
24+
CCFragData out;
25+
26+
out.position = verts[vid].position;
27+
out.texCoord1 = verts[vid].texCoord1;
28+
out.texCoord2 = verts[vid].texCoord2;
29+
out.color = clamp(half4(verts[vid].color), half4(0.0), half4(1.0));
30+
31+
return out;
32+
}
33+
34+
fragment half4
35+
CCFragmentFunctionDefaultColor(
36+
CCFragData in [[stage_in]]
37+
){
38+
return in.color;
39+
}
40+
41+
fragment half4
42+
CCFragmentFunctionDefaultTextureColor(
43+
CCFragData in [[stage_in]],
44+
texture2d<half> mainTexture [[texture(0)]],
45+
sampler mainTextureSampler [[sampler(0)]]
46+
){
47+
return in.color*mainTexture.sample(mainTextureSampler, in.texCoord1);
48+
}
49+
50+
fragment half4
51+
CCFragmentFunctionDefaultTextureA8Color(
52+
CCFragData in [[stage_in]],
53+
texture2d<half> mainTexture [[texture(0)]],
54+
sampler mainTextureSampler [[sampler(0)]]
55+
){
56+
return in.color*mainTexture.sample(mainTextureSampler, in.texCoord1).a;
57+
}
58+
59+
// TODO "alpha test" shader

0 commit comments

Comments
 (0)