-
Notifications
You must be signed in to change notification settings - Fork 3
/
GameViewController.cs
192 lines (154 loc) · 7.82 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Runtime.InteropServices;
using AppKit;
using Foundation;
using Metal;
using MetalKit;
using System.Numerics;
namespace DrawIndexedQuad
{
public struct PositionTexture
{
public Vector4 Position;
public Vector2 TexCoord;
public PositionTexture(Vector4 position, Vector2 texcoord)
{
this.Position = position;
this.TexCoord = texcoord;
}
}
public partial class GameViewController : NSViewController, IMTKViewDelegate
{
PositionTexture[] vertexData = new PositionTexture[]
{
new PositionTexture(new Vector4(-0.5f, -0.7f, 0.0f, 1.0f), new Vector2(0, 1)),
new PositionTexture(new Vector4(-0.5f, 0.7f, 0.0f, 1.0f), new Vector2(0, 0)),
new PositionTexture(new Vector4( 0.5f, 0.7f, 0.0f, 1.0f), new Vector2(1, 0)),
new PositionTexture(new Vector4(-0.5f, -0.7f, 0.0f, 1.0f), new Vector2(0, 1)),
new PositionTexture(new Vector4( 0.5f, 0.7f, 0.0f, 1.0f), new Vector2(1, 0)),
new PositionTexture(new Vector4( 0.5f, -0.7f, 0.0f, 1.0f), new Vector2(1, 1)),
};
// view
MTKView view;
// renderer
IMTLDevice device;
IMTLCommandQueue commandQueue;
IMTLLibrary defaultLibrary;
IMTLRenderPipelineState pipelineState;
IMTLDepthStencilState depthState;
IMTLBuffer vertexBuffer;
IMTLTexture texture;
IMTLSamplerState sampler;
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("quad_vertex");
// Load the fragment program into the library
IMTLFunction fragmentProgram = defaultLibrary.CreateFunction("quad_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.Float2;
vertexDescriptor.Attributes[1].BufferIndex = 0;
vertexDescriptor.Attributes[1].Offset = 4 * sizeof(float);
vertexDescriptor.Layouts[0].Stride = 6 * sizeof(float);
vertexDescriptor.Layouts[0].StepRate = 1;
vertexDescriptor.Layouts[0].StepFunction = MTLVertexStepFunction.PerVertex;
this.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);
// Texture KTX
NSUrl url = NSBundle.MainBundle.GetUrlForResource("DiffuseArray", "ktx");
MTKTextureLoader mTKTextureLoader = new MTKTextureLoader(device);
this.texture = mTKTextureLoader.FromUrl(url, new MTKTextureLoaderOptions(), out error);
Console.WriteLine("Failed to created pipeline state, error {0}", error);
MTLSamplerDescriptor samplerDescriptor = new MTLSamplerDescriptor()
{
MinFilter = MTLSamplerMinMagFilter.Linear,
MagFilter = MTLSamplerMinMagFilter.Linear,
MipFilter = MTLSamplerMipFilter.Linear,
SAddressMode = MTLSamplerAddressMode.ClampToEdge,
TAddressMode = MTLSamplerAddressMode.ClampToEdge,
};
this.sampler = device.CreateSamplerState(samplerDescriptor);
}
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);
renderEncoder.SetFragmentTexture(this.texture, 0);
renderEncoder.SetFragmentSamplerState(this.sampler, 0);
// Tell the render context we want to draw our primitives
renderEncoder.DrawPrimitives(MTLPrimitiveType.Triangle, 0, (uint)vertexData.Length);
// 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();
}
}
}