Metal example code cleanup

This commit is contained in:
Camilla Berglund 2015-12-22 16:08:50 +01:00 committed by Camilla Löwy
parent 8eb2345347
commit 70297aeb49

View File

@ -24,16 +24,15 @@
//======================================================================== //========================================================================
//! [code] //! [code]
#import <Metal/Metal.h>
#import <QuartzCore/QuartzCore.h>
#import <simd/simd.h>
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
#import <GLFW/glfw3.h> #import <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_COCOA #define GLFW_EXPOSE_NATIVE_COCOA
#define GLFW_EXPOSE_NATIVE_NSGL
#import <GLFW/glfw3native.h> #import <GLFW/glfw3native.h>
#import <Metal/Metal.h>
#import <QuartzCore/QuartzCore.h>
#import <simd/simd.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -53,11 +52,7 @@ int main(void)
{ {
id<MTLDevice> device = MTLCreateSystemDefaultDevice(); id<MTLDevice> device = MTLCreateSystemDefaultDevice();
if (!device) if (!device)
{
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
GLFWwindow* window;
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
@ -65,28 +60,28 @@ int main(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
window = glfwCreateWindow(640, 480, "Metal Example", NULL, NULL); GLFWwindow* window = glfwCreateWindow(640, 480, "Metal Example", NULL, NULL);
if (!window) if (!window)
{ {
glfwTerminate(); glfwTerminate();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
NSWindow *nswin = glfwGetCocoaWindow(window); NSWindow* nswin = glfwGetCocoaWindow(window);
CAMetalLayer *layer = [CAMetalLayer layer]; CAMetalLayer* layer = [CAMetalLayer layer];
layer.device = device; layer.device = device;
layer.pixelFormat = MTLPixelFormatBGRA8Unorm; layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
nswin.contentView.layer = layer; nswin.contentView.layer = layer;
nswin.contentView.wantsLayer = YES; nswin.contentView.wantsLayer = YES;
MTLCompileOptions *compileOptions = [MTLCompileOptions new]; MTLCompileOptions* compileOptions = [MTLCompileOptions new];
compileOptions.languageVersion = MTLLanguageVersion1_1; compileOptions.languageVersion = MTLLanguageVersion1_1;
NSError *compileError; NSError* compileError;
id<MTLLibrary> lib = [device newLibraryWithSource: id<MTLLibrary> lib = [device newLibraryWithSource:
@"#include <metal_stdlib>\n" @"#include <metal_stdlib>\n"
"using namespace metal;\n" "using namespace metal;\n"
"vertex float4 v_simple(\n" "vertex float4 v_simple(\n"
" constant float4 *in [[buffer(0)]],\n" " constant float4* in [[buffer(0)]],\n"
" uint vid [[vertex_id]])\n" " uint vid [[vertex_id]])\n"
"{\n" "{\n"
" return in[vid];\n" " return in[vid];\n"
@ -112,7 +107,7 @@ int main(void)
id<MTLCommandQueue> cq = [device newCommandQueue]; id<MTLCommandQueue> cq = [device newCommandQueue];
assert(cq); assert(cq);
MTLRenderPipelineDescriptor *rpd = [MTLRenderPipelineDescriptor new]; MTLRenderPipelineDescriptor* rpd = [MTLRenderPipelineDescriptor new];
rpd.vertexFunction = vs; rpd.vertexFunction = vs;
rpd.fragmentFunction = fs; rpd.fragmentFunction = fs;
rpd.colorAttachments[0].pixelFormat = layer.pixelFormat; rpd.colorAttachments[0].pixelFormat = layer.pixelFormat;
@ -135,8 +130,8 @@ int main(void)
id<MTLCommandBuffer> cb = [cq commandBuffer]; id<MTLCommandBuffer> cb = [cq commandBuffer];
MTLRenderPassDescriptor *rpd = [MTLRenderPassDescriptor new]; MTLRenderPassDescriptor* rpd = [MTLRenderPassDescriptor new];
MTLRenderPassColorAttachmentDescriptor *cd = rpd.colorAttachments[0]; MTLRenderPassColorAttachmentDescriptor* cd = rpd.colorAttachments[0];
cd.texture = drawable.texture; cd.texture = drawable.texture;
cd.loadAction = MTLLoadActionClear; cd.loadAction = MTLLoadActionClear;
cd.clearColor = MTLClearColorMake(1.0, 1.0, 1.0, 1.0); cd.clearColor = MTLClearColorMake(1.0, 1.0, 1.0, 1.0);