-
Notifications
You must be signed in to change notification settings - Fork 3
/
GameViewController.cs
157 lines (123 loc) · 6.09 KB
/
GameViewController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using AppKit;
using Foundation;
using Metal;
using MetalKit;
using System.Numerics;
namespace DrawTriangle
{
public partial class GameViewController : NSViewController, IMTKViewDelegate
{
Vector4[] vertexData = new Vector4[]
{
// TriangleList
new Vector4(0f, 0.5f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
new Vector4(0.5f, -0.5f, 0.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
new Vector4(-0.5f, -0.5f, 0.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f)
};
// view
MTKView view;
// renderer
IMTLDevice device;
IMTLCommandQueue commandQueue;
IMTLLibrary defaultLibrary;
IMTLRenderPipelineState pipelineState;
IMTLDepthStencilState depthState;
IMTLBuffer vertexBuffer;
public GameViewController(IntPtr handle)
: base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Set the view to use the default device
device = MTLDevice.SystemDefault;
if (device == null)
{
Console.WriteLine("Metal is not supported on this device");
View = new NSView(View.Frame);
}
// Create a new command queue
commandQueue = device.CreateCommandQueue();
// Load all the shader files with a metal file extension in the project
defaultLibrary = device.CreateDefaultLibrary();
// Setup view
view = (MTKView)View;
view.Delegate = this;
view.Device = device;
view.SampleCount = 1;
view.DepthStencilPixelFormat = MTLPixelFormat.Depth32Float_Stencil8;
view.ColorPixelFormat = MTLPixelFormat.BGRA8Unorm;
view.PreferredFramesPerSecond = 60;
view.ClearColor = new MTLClearColor(0.5f, 0.5f, 0.5f, 1.0f);
// Load the vertex program into the library
IMTLFunction vertexProgram = defaultLibrary.CreateFunction("triangle_vertex");
// Load the fragment program into the library
IMTLFunction fragmentProgram = defaultLibrary.CreateFunction("triangle_fragment");
// Create a vertex descriptor from the MTKMesh
MTLVertexDescriptor vertexDescriptor = new MTLVertexDescriptor();
vertexDescriptor.Attributes[0].Format = MTLVertexFormat.Float4;
vertexDescriptor.Attributes[0].BufferIndex = 0;
vertexDescriptor.Attributes[0].Offset = 0;
vertexDescriptor.Attributes[1].Format = MTLVertexFormat.Float4;
vertexDescriptor.Attributes[1].BufferIndex = 0;
vertexDescriptor.Attributes[1].Offset = 4 * sizeof(float);
vertexDescriptor.Layouts[0].Stride = 8 * sizeof(float);
vertexDescriptor.Layouts[0].StepRate = 1;
vertexDescriptor.Layouts[0].StepFunction = MTLVertexStepFunction.PerVertex;
vertexBuffer = device.CreateBuffer(vertexData, MTLResourceOptions.CpuCacheModeDefault);// (MTLResourceOptions)0);
// Create a reusable pipeline state
var pipelineStateDescriptor = new MTLRenderPipelineDescriptor
{
SampleCount = view.SampleCount,
VertexFunction = vertexProgram,
FragmentFunction = fragmentProgram,
VertexDescriptor = vertexDescriptor,
DepthAttachmentPixelFormat = view.DepthStencilPixelFormat,
StencilAttachmentPixelFormat = view.DepthStencilPixelFormat
};
pipelineStateDescriptor.ColorAttachments[0].PixelFormat = view.ColorPixelFormat;
NSError error;
pipelineState = device.CreateRenderPipelineState(pipelineStateDescriptor, out error);
if (pipelineState == null)
Console.WriteLine("Failed to created pipeline state, error {0}", error);
var depthStateDesc = new MTLDepthStencilDescriptor
{
DepthCompareFunction = MTLCompareFunction.Less,
DepthWriteEnabled = true
};
depthState = device.CreateDepthStencilState(depthStateDesc);
}
public void DrawableSizeWillChange(MTKView view, CoreGraphics.CGSize size)
{
}
public void Draw(MTKView view)
{
// Create a new command buffer for each renderpass to the current drawable
IMTLCommandBuffer commandBuffer = commandQueue.CommandBuffer();
// Call the view's completion handler which is required by the view since it will signal its semaphore and set up the next buffer
var drawable = view.CurrentDrawable;
// Obtain a renderPassDescriptor generated from the view's drawable textures
MTLRenderPassDescriptor renderPassDescriptor = view.CurrentRenderPassDescriptor;
// If we have a valid drawable, begin the commands to render into it
if (renderPassDescriptor != null)
{
// Create a render command encoder so we can render into something
IMTLRenderCommandEncoder renderEncoder = commandBuffer.CreateRenderCommandEncoder(renderPassDescriptor);
// Set context state
renderEncoder.SetDepthStencilState(depthState);
renderEncoder.SetRenderPipelineState(pipelineState);
renderEncoder.SetVertexBuffer(vertexBuffer, 0, 0);
// Tell the render context we want to draw our primitives
renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, 3);
// We're done encoding commands
renderEncoder.EndEncoding();
// Schedule a present once the framebuffer is complete using the current drawable
commandBuffer.PresentDrawable(drawable);
}
// Finalize rendering here & push the command buffer to the GPU
commandBuffer.Commit();
}
}
}