Created
January 20, 2021 12:01
-
-
Save kssreeram/d1306ceb2e56d355f29b6639286977af to your computer and use it in GitHub Desktop.
Bug in MetalKit?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This appears to be a bug in MTKView. | |
// | |
// To reproduce the bug: | |
// 1. Create a zero-sized window. | |
// 2. Add MTKView to window. | |
// 3. Set window size to a non-zero value. | |
// | |
// The MTKView never displays its content! | |
// It's as if the MTKView doesn't even exist. | |
// | |
// To avoid this problem, make window non-zero sized | |
// before adding the MTKView (uncomment line 53). | |
// | |
// To compile and run this program: | |
// clang metal-bug.m -fobjc-arc -framework Cocoa -framework Metal -framework MetalKit | |
// ./a.out | |
// | |
#import <Cocoa/Cocoa.h> | |
#import <MetalKit/MetalKit.h> | |
@interface MyView: MTKView | |
@end | |
@implementation MyView | |
-(void)drawRect:(NSRect)dirtyRect { | |
id<CAMetalDrawable> drawable = [self currentDrawable]; | |
if (!drawable) { | |
return; | |
} | |
MTLRenderPassDescriptor *pass = [MTLRenderPassDescriptor renderPassDescriptor]; | |
pass.colorAttachments[0].texture = drawable.texture; | |
pass.colorAttachments[0].loadAction = MTLLoadActionClear; | |
pass.colorAttachments[0].storeAction = MTLStoreActionStore; | |
pass.colorAttachments[0].clearColor = MTLClearColorMake(1, 0, 0, 1); | |
id<MTLCommandQueue> queue = [self.device newCommandQueue]; | |
id<MTLCommandBuffer> buffer = [queue commandBuffer]; | |
id<MTLRenderCommandEncoder> encoder = [buffer renderCommandEncoderWithDescriptor:pass]; | |
[encoder endEncoding]; | |
[buffer presentDrawable:drawable]; | |
[buffer commit]; | |
} | |
@end | |
static void initialize_ui(void) { | |
NSRect initial = NSZeroRect; | |
// | |
// UNCOMMENT THE FOLLOWING LINE TO WORKAROUND THE BUG. | |
// | |
// initial = NSMakeRect(0, 0, 500, 500); | |
NSWindowStyleMask style = | |
NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | NSWindowStyleMaskClosable; | |
NSWindow *window = [[NSWindow alloc] initWithContentRect:initial | |
styleMask:style | |
backing:NSBackingStoreBuffered | |
defer:YES]; | |
MyView *view = [[MyView alloc] init]; | |
view.device = MTLCreateSystemDefaultDevice(); | |
window.contentView = view; | |
[window makeKeyAndOrderFront:nil]; | |
[window setFrame:NSMakeRect(0, 0, 500, 500) display:YES]; | |
} | |
@interface AppDelegate: NSObject<NSApplicationDelegate> | |
@end | |
@implementation AppDelegate | |
-(void)applicationDidFinishLaunching:(NSNotification *)notification { | |
initialize_ui(); | |
} | |
@end | |
int main() { | |
@autoreleasepool { | |
NSApplication *app = NSApplication.sharedApplication; | |
AppDelegate *delegate = [[AppDelegate alloc] init]; | |
app.delegate = delegate; | |
[app run]; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment