Skip to content

Commit 69910e4

Browse files
committed
Fixes for frame buffering in CCMetalView.
1 parent 5f37659 commit 69910e4

File tree

2 files changed

+11
-63
lines changed

2 files changed

+11
-63
lines changed

cocos2d-tests.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@
11961196
);
11971197
runOnlyForDeploymentPostprocessing = 0;
11981198
shellPath = /usr/bin/ruby;
1199-
shellScript = "# Special thanks to Filip Iliescu at Apple for the help with this.\n\n# Environment variables.\nSDK = ENV[\"PLATFORM_NAME\"]\nOBJECT_FILE_DIR = ENV[\"OBJECT_FILE_DIR\"]\nSRCROOT = ENV[\"SRCROOT\"]\nBUILT_PRODUCTS_DIR = ENV[\"BUILT_PRODUCTS_DIR\"]\nPRODUCT_NAME = ENV[\"PRODUCT_NAME\"]\n\n# User variables\n# Directory with your custom .metal files in it.\nSHADER_DIR = \"#{SRCROOT}/cocos2d-ui-tests/MetalShaders\"\n# Location of the cocos2d library directory.\nCOCOS2D_DIR = \"#{SRCROOT}/cocos2d\"\n\nMETAL_ARGS = [\n\t\"-arch air64\",\n\t\"-emit-llvm\",\n\t\"-c\",\n\t\"-gline-tables-only\",\n\t\"-ffast-math\",\n\t\"-std=ios-metal1.0\",\n %{-I \"#{COCOS2D_DIR}\"},\n]\n\ndef system(cmd)\n\tputs cmd\n Kernel.system cmd\nend\n\nPRODUCTS = \"#{OBJECT_FILE_DIR}/CCMetalShaders\"\nARCHIVE = \"#{PRODUCTS}/default.metal-ar\"\n\ndef compile_shader(src)\n\tdst = \"#{PRODUCTS}/#{File.basename(src, \".metal\")}.air\"\n\t\n\t# Compile and archive the shader.\n\tsystem %{xcrun -sdk #{SDK} metal #{METAL_ARGS.join(\" \")} \"#{src}\" -o \"#{dst}\"}\n\tsystem %{xcrun -sdk #{SDK} ar r \"#{ARCHIVE}\" \"#{dst}\"}\nend\n\nif SDK == \"iphoneos\"\n\t# Cannot use default.metallib to avoid clashing with Xcode build magic.\n\t# This is part of a workaround due to the iOS simulator not supporting Metal and may go away in the future.\n\tmetal_lib = \"#{BUILT_PRODUCTS_DIR}/#{PRODUCT_NAME}.app/CCShaders.metallib\"\n \n\t# Delete the old products dir and metal lib.\n\tsystem %{rm -rf \"#{PRODUCTS}\"}\n system %{rm -f \"#{metal_lib}\"}\n system %{mkdir -p \"#{PRODUCTS}\"}\n \n # Compile the builtin shaders.\n compile_shader(%{#{COCOS2D_DIR}/Platforms/iOS/CCShaders.metal})\n \n\t# Compile/archive all the shaders in the shader directory.\n Dir.glob(\"#{SHADER_DIR}/*.metal\").each{|src| compile_shader(src)}\n \n\t# Link all shaders into default lib.\n\tsystem %{xcrun -sdk #{SDK} metallib -o \"#{metal_lib}\" \"#{ARCHIVE}\"}\nend\n";
1199+
shellScript = "# Special thanks to Filip Iliescu at Apple for the help with this.\n\n# Environment variables.\nSDK = ENV[\"PLATFORM_NAME\"]\nOBJECT_FILE_DIR = ENV[\"OBJECT_FILE_DIR\"]\nSRCROOT = ENV[\"SRCROOT\"]\nBUILT_PRODUCTS_DIR = ENV[\"BUILT_PRODUCTS_DIR\"]\nPRODUCT_NAME = ENV[\"PRODUCT_NAME\"]\n\n# User variables\n# Directory with your custom .metal files in it.\nSHADER_DIR = \"#{SRCROOT}/cocos2d-ui-tests/MetalShaders\"\n# Location of the cocos2d library directory.\nCOCOS2D_DIR = \"#{SRCROOT}/cocos2d\"\n\nMETAL_ARGS = [\n\t\"-arch air64\",\n\t\"-emit-llvm\",\n\t\"-c\",\n\t\"-gline-tables-only\",\n\t\"-ffast-math\",\n\t\"-std=ios-metal1.0\",\n\t%{-I \"#{COCOS2D_DIR}\"},\n]\n\ndef system(cmd)\n\tputs cmd\n\tKernel.system cmd\nend\n\nPRODUCTS = \"#{OBJECT_FILE_DIR}/CCMetalShaders\"\nARCHIVE = \"#{PRODUCTS}/default.metal-ar\"\n\ndef compile_shader(src)\n\tdst = \"#{PRODUCTS}/#{File.basename(src, \".metal\")}.air\"\n\t\n\t# Compile and archive the shader.\n\tsystem %{xcrun -sdk #{SDK} metal #{METAL_ARGS.join(\" \")} \"#{src}\" -o \"#{dst}\"}\n\tsystem %{xcrun -sdk #{SDK} ar r \"#{ARCHIVE}\" \"#{dst}\"}\nend\n\nif SDK == \"iphoneos\"\n\t# Cannot use default.metallib to avoid clashing with Xcode build magic.\n\t# This is part of a workaround due to the iOS simulator not supporting Metal and may go away in the future.\n\tmetal_lib = \"#{BUILT_PRODUCTS_DIR}/#{PRODUCT_NAME}.app/CCShaders.metallib\"\n\t\t\n\t# Delete the old products dir and metal lib.\n\tsystem %{rm -rf \"#{PRODUCTS}\"}\n\tsystem %{rm -f \"#{metal_lib}\"}\n\tsystem %{mkdir -p \"#{PRODUCTS}\"}\n\t\n\t# Compile the builtin shaders.\n\tcompile_shader(%{#{COCOS2D_DIR}/Platforms/iOS/CCShaders.metal})\n\t\n\t# Compile/archive all the shaders in the shader directory.\n\tDir.glob(\"#{SHADER_DIR}/*.metal\").each{|src| compile_shader(src)}\n\t\n\t# Link all shaders into default lib.\n\tsystem %{xcrun -sdk #{SDK} metallib -o \"#{metal_lib}\" \"#{ARCHIVE}\"}\nend\n";
12001200
};
12011201
/* End PBXShellScriptBuildPhase section */
12021202

cocos2d/Platforms/iOS/CCMetalView.m

+10-62
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
@implementation CCMetalView {
2222
CCMetalContext *_context;
23-
// id<MTLCommandQueue> _queue;
2423
id<MTLDrawable> _currentDrawable;
2524

2625
dispatch_semaphore_t _queuedFramesSemaphore;
@@ -62,25 +61,6 @@ - (id) initWithFrame:(CGRect)frame
6261
return self;
6362
}
6463

65-
//-(id) initWithCoder:(NSCoder *)aDecoder
66-
//{
67-
// if( (self = [super initWithCoder:aDecoder]) ) {
68-
// CAMetalLayer *layer = (CAMetalLayer *)self.layer;
69-
//
70-
// _pixelformat = kEAGLColorFormatRGB565;
71-
// _depthFormat = 0;
72-
// _multiSampling= NO;
73-
// _requestedSamples = 0;
74-
// _surfaceSize = [layer bounds].size;
75-
//
76-
// if(![self setupSurfaceWithSharegroup:nil]){
77-
// return nil;
78-
// }
79-
// }
80-
//
81-
// return self;
82-
//}
83-
8464
- (void) dealloc
8565
{
8666
CCLOGINFO(@"cocos2d: deallocing %@", self);
@@ -100,55 +80,23 @@ - (void) layoutSubviews
10080
[[CCDirector sharedDirector] reshapeProjection:_surfaceSize];
10181
}
10282

103-
//- (MTLRenderPassDescriptor *)renderPassDescriptorForTexture:(id <MTLTexture>)texture
104-
//{
105-
// MTLRenderPassDescriptor *descriptor = [MTLRenderPassDescriptor renderPassDescriptor];
106-
//
107-
// MTLRenderPassColorAttachmentDescriptor *colorAttachment = [MTLRenderPassColorAttachmentDescriptor new];
108-
// colorAttachment.texture = texture;
109-
// colorAttachment.loadAction = MTLLoadActionClear;
110-
// colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 0);
111-
// colorAttachment.storeAction = MTLStoreActionStore;
112-
//
113-
// descriptor.colorAttachments[0] = colorAttachment;
114-
//
115-
// return descriptor;
116-
//
117-
//// if (!_depthTex || (_depthTex && (_depthTex.width != texture.width || _depthTex.height != texture.height)))
118-
//// {
119-
//// // If we need a depth texture and don't have one, or if the depth texture we have is the wrong size
120-
//// // Then allocate one of the proper size
121-
////
122-
//// MTLTextureDescriptor* desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat: MTLPixelFormatDepth32Float width: texture.width height: texture.height mipmapped: NO];
123-
//// _depthTex = [_device newTextureWithDescriptor: desc];
124-
//// _depthTex.label = @"Depth";
125-
////
126-
//// MTLRenderPassAttachmentDescriptor *depthAttachment = [MTLRenderPassAttachmentDescriptor new];
127-
//// depthAttachment.texture = _depthTex;
128-
//// [depthAttachment setLoadAction:MTLLoadActionClear];
129-
//// [depthAttachment setClearValue:MTLClearValueMakeDepth(1.0)];
130-
//// [depthAttachment setStoreAction: MTLStoreActionDontCare];
131-
////
132-
//// _renderPassDescriptor.depthAttachment = depthAttachment;
133-
//// }
134-
//}
135-
13683
-(void)beginFrame
13784
{
85+
dispatch_semaphore_wait(_queuedFramesSemaphore, DISPATCH_TIME_FOREVER);
86+
13887
if(_layerSizeDidUpdate){
13988
self.metalLayer.drawableSize = _surfaceSize;
14089
_layerSizeDidUpdate = NO;
14190
}
14291

143-
// id<CAMetalDrawable> drawable = nil;
144-
// while(drawable == nil){
145-
// drawable = [self.metalLayer nextDrawable];
146-
//
147-
// if(drawable == nil) NSLog(@"nil drawable? (Why does this happen?)");
148-
// }
149-
150-
id<CAMetalDrawable> drawable = [self.metalLayer nextDrawable];
151-
[_context.currentCommandBuffer presentDrawable:drawable];
92+
id<CAMetalDrawable> drawable = nil;
93+
while(drawable == nil){
94+
drawable = [self.metalLayer nextDrawable];
95+
96+
#if DEBUG
97+
if(drawable == nil) NSLog(@"Metal drawable pool exhausted. You may be rendering too much in a frame.");
98+
#endif
99+
}
152100

153101
_currentDrawable = drawable;
154102
_destinationTexture = drawable.texture;

0 commit comments

Comments
 (0)