-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathProgram.cs
397 lines (340 loc) · 11.6 KB
/
Program.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/////////////////////////////////////////////////////// PLEASE READ! ///////////////////////////////////////////////////
// This provides a basic example of using our Direct3D 11 bindings in their current form. These bindings are still //
// improving over time, and as a result the content of this example may change. //
// Notably: //
// TODO remove Unsafe.NullRef once we've updated the bindings to not require it //
// TODO investigate making the D3DPrimitiveTopology enum more user friendly //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System.Text;
using System.Runtime.CompilerServices;
using Silk.NET.Core.Native;
using Silk.NET.Direct3D.Compilers;
using Silk.NET.Direct3D11;
using Silk.NET.DXGI;
using Silk.NET.Input;
using Silk.NET.Maths;
using Silk.NET.Windowing;
var backgroundColour = new[]{ 0.0f, 0.0f, 0.0f, 1.0f };
float[] vertices =
{
// X Y Z
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.5f,
};
uint[] indices =
{
0, 1, 3,
1, 2, 3,
};
var vertexStride = 3U * sizeof(float);
var vertexOffset = 0U;
const string shaderSource = @"
struct vs_in {
float3 position_local : POS;
};
struct vs_out {
float4 position_clip : SV_POSITION;
};
vs_out vs_main(vs_in input) {
vs_out output = (vs_out)0;
output.position_clip = float4(input.position_local, 1.0);
return output;
}
float4 ps_main(vs_out input) : SV_TARGET {
return float4( 1.0, 0.5, 0.2, 1.0 );
}
";
// Create a window.
var options = WindowOptions.Default;
options.Size = new Vector2D<int>(800, 600);
options.Title = "Learn Direct3D11 with Silk.NET";
options.API = GraphicsAPI.None; // <-- This bit is important, as your window will be configured for OpenGL by default.
var window = Window.Create(options);
// Load the DXGI and Direct3D11 libraries for later use.
// Given this is not tied to the window, this doesn't need to be done in the OnLoad event.
DXGI dxgi = null!;
D3D11 d3d11 = null!;
D3DCompiler compiler = null!;
// These variables are initialized within the Load event.
ComPtr<IDXGIFactory2> factory = default;
ComPtr<IDXGISwapChain1> swapchain = default;
ComPtr<ID3D11Device> device = default;
ComPtr<ID3D11DeviceContext> deviceContext = default;
ComPtr<ID3D11Buffer> vertexBuffer = default;
ComPtr<ID3D11Buffer> indexBuffer = default;
ComPtr<ID3D11VertexShader> vertexShader = default;
ComPtr<ID3D11PixelShader> pixelShader = default;
ComPtr<ID3D11InputLayout> inputLayout = default;
// Assign events.
window.Load += OnLoad;
window.Update += OnUpdate;
window.Render += OnRender;
window.FramebufferResize += OnFramebufferResize;
// Run the window.
window.Run();
// Clean up any resources.
factory.Dispose();
swapchain.Dispose();
device.Dispose();
deviceContext.Dispose();
vertexBuffer.Dispose();
indexBuffer.Dispose();
vertexShader.Dispose();
pixelShader.Dispose();
inputLayout.Dispose();
compiler.Dispose();
d3d11.Dispose();
dxgi.Dispose();
//dispose the window, and its internal resources
window.Dispose();
unsafe void OnLoad()
{
//Whether or not to force use of DXVK on platforms where native DirectX implementations are available
const bool forceDxvk = false;
dxgi = DXGI.GetApi(window, forceDxvk);
d3d11 = D3D11.GetApi(window, forceDxvk);
compiler = D3DCompiler.GetApi();
// Set-up input context.
var input = window.CreateInput();
foreach (var keyboard in input.Keyboards)
{
keyboard.KeyDown += OnKeyDown;
}
// Create our D3D11 logical device.
SilkMarshal.ThrowHResult
(
d3d11.CreateDevice
(
default(ComPtr<IDXGIAdapter>),
D3DDriverType.Hardware,
Software: default,
(uint) CreateDeviceFlag.Debug,
null,
0,
D3D11.SdkVersion,
ref device,
null,
ref deviceContext
)
);
//This is not supported under DXVK
//TODO: PR a stub into DXVK for this maybe?
if (OperatingSystem.IsWindows())
{
// Log debug messages for this device (given that we've enabled the debug flag). Don't do this in release code!
device.SetInfoQueueCallback(msg => Console.WriteLine(SilkMarshal.PtrToString((nint) msg.PDescription)));
}
// Create our swapchain.
var swapChainDesc = new SwapChainDesc1
{
BufferCount = 2, // double buffered
Format = Format.FormatB8G8R8A8Unorm,
BufferUsage = DXGI.UsageRenderTargetOutput,
SwapEffect = SwapEffect.FlipDiscard,
SampleDesc = new SampleDesc(1, 0)
};
// Create our DXGI factory to allow us to create a swapchain.
factory = dxgi.CreateDXGIFactory<IDXGIFactory2>();
// Create the swapchain.
SilkMarshal.ThrowHResult
(
factory.CreateSwapChainForHwnd
(
device,
window.Native!.DXHandle!.Value,
in swapChainDesc,
null,
ref Unsafe.NullRef<IDXGIOutput>(),
ref swapchain
)
);
// Create our vertex buffer.
var bufferDesc = new BufferDesc
{
ByteWidth = (uint) (vertices.Length * sizeof(float)),
Usage = Usage.Default,
BindFlags = (uint) BindFlag.VertexBuffer
};
fixed (float* vertexData = vertices)
{
var subresourceData = new SubresourceData
{
PSysMem = vertexData
};
SilkMarshal.ThrowHResult(device.CreateBuffer(in bufferDesc, in subresourceData, ref vertexBuffer));
}
// Create our index buffer.
bufferDesc = new BufferDesc
{
ByteWidth = (uint) (indices.Length * sizeof(uint)),
Usage = Usage.Default,
BindFlags = (uint) BindFlag.IndexBuffer
};
fixed (uint* indexData = indices)
{
var subresourceData = new SubresourceData
{
PSysMem = indexData
};
SilkMarshal.ThrowHResult(device.CreateBuffer(in bufferDesc, in subresourceData, ref indexBuffer));
}
var shaderBytes = Encoding.ASCII.GetBytes(shaderSource);
// Compile vertex shader.
ComPtr<ID3D10Blob> vertexCode = default;
ComPtr<ID3D10Blob> vertexErrors = default;
HResult hr = compiler.Compile
(
in shaderBytes[0],
(nuint) shaderBytes.Length,
nameof(shaderSource),
null,
ref Unsafe.NullRef<ID3DInclude>(),
"vs_main",
"vs_5_0",
0,
0,
ref vertexCode,
ref vertexErrors
);
// Check for compilation errors.
if (hr.IsFailure)
{
if (vertexErrors.Handle is not null)
{
Console.WriteLine(SilkMarshal.PtrToString((nint) vertexErrors.GetBufferPointer()));
}
hr.Throw();
}
// Compile pixel shader.
ComPtr<ID3D10Blob> pixelCode = default;
ComPtr<ID3D10Blob> pixelErrors = default;
hr = compiler.Compile
(
in shaderBytes[0],
(nuint)shaderBytes.Length,
nameof(shaderSource),
null,
ref Unsafe.NullRef<ID3DInclude>(),
"ps_main",
"ps_5_0",
0,
0,
ref pixelCode,
ref pixelErrors
);
// Check for compilation errors.
if (hr.IsFailure)
{
if (pixelErrors.Handle is not null)
{
Console.WriteLine(SilkMarshal.PtrToString((nint) pixelErrors.GetBufferPointer()));
}
hr.Throw();
}
// Create vertex shader.
SilkMarshal.ThrowHResult
(
device.CreateVertexShader
(
vertexCode.GetBufferPointer(),
vertexCode.GetBufferSize(),
ref Unsafe.NullRef<ID3D11ClassLinkage>(),
ref vertexShader
)
);
// Create pixel shader.
SilkMarshal.ThrowHResult
(
device.CreatePixelShader
(
pixelCode.GetBufferPointer(),
pixelCode.GetBufferSize(),
ref Unsafe.NullRef<ID3D11ClassLinkage>(),
ref pixelShader
)
);
// Describe the layout of the input data for the shader.
fixed (byte* name = SilkMarshal.StringToMemory("POS"))
{
var inputElement = new InputElementDesc
{
SemanticName = name,
SemanticIndex = 0,
Format = Format.FormatR32G32B32Float,
InputSlot = 0,
AlignedByteOffset = 0,
InputSlotClass = InputClassification.PerVertexData,
InstanceDataStepRate = 0
};
SilkMarshal.ThrowHResult
(
device.CreateInputLayout
(
in inputElement,
1,
vertexCode.GetBufferPointer(),
vertexCode.GetBufferSize(),
ref inputLayout
)
);
}
// Clean up any resources.
vertexCode.Dispose();
vertexErrors.Dispose();
pixelCode.Dispose();
pixelErrors.Dispose();
}
void OnUpdate(double deltaSeconds)
{
// Here all of the updates to program state ahead of rendering (e.g. physics) should be done. We don't have anything
// to do here at the moment, so we've left it blank.
}
unsafe void OnFramebufferResize(Vector2D<int> newSize)
{
// If the window resizes, we need to be sure to update the swapchain's back buffers.
SilkMarshal.ThrowHResult
(
swapchain.ResizeBuffers(0, (uint) newSize.X, (uint) newSize.Y, Format.FormatB8G8R8A8Unorm, 0)
);
}
unsafe void OnRender(double deltaSeconds)
{
// Obtain the framebuffer for the swapchain's backbuffer.
using var framebuffer = swapchain.GetBuffer<ID3D11Texture2D>(0);
// Create a view over the render target.
ComPtr<ID3D11RenderTargetView> renderTargetView = default;
SilkMarshal.ThrowHResult(device.CreateRenderTargetView(framebuffer, null, ref renderTargetView));
// Clear the render target to be all black ahead of rendering.
deviceContext.ClearRenderTargetView(renderTargetView, ref backgroundColour[0]);
// Update the rasterizer state with the current viewport.
var viewport = new Viewport(0, 0, window.FramebufferSize.X, window.FramebufferSize.Y, 0, 1);
deviceContext.RSSetViewports(1, in viewport);
// Tell the output merger about our render target view.
deviceContext.OMSetRenderTargets(1, ref renderTargetView, ref Unsafe.NullRef<ID3D11DepthStencilView>());
// Update the input assembler to use our shader input layout, and associated vertex & index buffers.
deviceContext.IASetPrimitiveTopology(D3DPrimitiveTopology.D3DPrimitiveTopologyTrianglelist);
deviceContext.IASetInputLayout(inputLayout);
deviceContext.IASetVertexBuffers(0, 1, vertexBuffer, in vertexStride, in vertexOffset);
deviceContext.IASetIndexBuffer(indexBuffer, Format.FormatR32Uint, 0);
// Bind our shaders.
deviceContext.VSSetShader(vertexShader, ref Unsafe.NullRef<ComPtr<ID3D11ClassInstance>>(), 0);
deviceContext.PSSetShader(pixelShader, ref Unsafe.NullRef<ComPtr<ID3D11ClassInstance>>(), 0);
// Draw the quad.
deviceContext.DrawIndexed(6, 0, 0);
// Present the drawn image.
swapchain.Present(1, 0);
// Clean up any resources created in this method.
renderTargetView.Dispose();
}
void OnKeyDown(IKeyboard keyboard, Key key, int scancode)
{
// Check to close the window on escape.
if (key == Key.Escape)
{
window.Close();
}
}