-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
286 lines (229 loc) · 10.8 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
using System;
using System.Runtime.InteropServices;
using Microsoft.JSInterop.WebAssembly;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using WaveEngine.Bindings.OpenGLES;
using System.Runtime.CompilerServices;
using Microsoft.JSInterop;
using System.Threading.Tasks;
namespace WasmTest;
internal unsafe class MyRuntime : WebAssemblyJSRuntime
{
public MyRuntime()
{
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA2101:Specify marshaling for P/Invoke string arguments", Justification = "Incorrect warning generated")]
internal static class EGL
{
public const string LibEgl = "libEGL";
public const int EGL_NONE = 0x3038;
public const int EGL_RED_SIZE = 0x3024;
public const int EGL_GREEN_SIZE = 0x3023;
public const int EGL_BLUE_SIZE = 0x3022;
public const int EGL_DEPTH_SIZE = 0x3025;
public const int EGL_STENCIL_SIZE = 0x3026;
public const int EGL_SURFACE_TYPE = 0x3033;
public const int EGL_RENDERABLE_TYPE = 0x3040;
public const int EGL_SAMPLES = 0x3031;
public const int EGL_WINDOW_BIT = 0x0004;
public const int EGL_OPENGL_ES2_BIT = 0x0004;
public const int EGL_OPENGL_ES3_BIT = 0x00000040;
public const int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
public const int EGL_NO_CONTEXT = 0x0;
public const int EGL_NATIVE_VISUAL_ID = 0x302E;
public const int EGL_OPENGL_ES_API = 0x30A0;
[DllImport(LibEgl, EntryPoint = "eglGetProcAddress", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern IntPtr GetProcAddress(string proc);
[DllImport(LibEgl, EntryPoint = "eglGetDisplay", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern IntPtr GetDisplay(IntPtr displayId);
[DllImport(LibEgl, EntryPoint = "eglInitialize", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool Initialize(IntPtr display, out int major, out int minor);
[DllImport(LibEgl, EntryPoint = "eglChooseConfig", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ChooseConfig(IntPtr dpy, int[] attribList, ref IntPtr configs, IntPtr configSize/*fixed to 1*/, ref IntPtr numConfig);
[DllImport(LibEgl, EntryPoint = "eglBindAPI", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BindApi(int api);
[DllImport(LibEgl, EntryPoint = "eglCreateContext", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern IntPtr CreateContext(IntPtr/*EGLDisplay*/ display, IntPtr/*EGLConfig*/ config, IntPtr shareContext, int[] attribList);
[DllImport(LibEgl, EntryPoint = "eglGetConfigAttrib", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConfigAttrib(IntPtr/*EGLDisplay*/ display, IntPtr/*EGLConfig*/ config, IntPtr attribute, ref IntPtr value);
[DllImport(LibEgl, EntryPoint = "eglCreateWindowSurface", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern IntPtr CreateWindowSurface(IntPtr display, IntPtr config, IntPtr win, IntPtr attribList/*fixed to NULL*/);
[DllImport(LibEgl, EntryPoint = "eglDestroySurface", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern int DestroySurface(IntPtr display, IntPtr surface);
[DllImport(LibEgl, EntryPoint = "eglDestroyContext", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern int DestroyContext(IntPtr display, IntPtr ctx);
[DllImport(LibEgl, EntryPoint = "eglMakeCurrent", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool MakeCurrent(IntPtr display, IntPtr draw, IntPtr read, IntPtr ctx);
[DllImport(LibEgl, EntryPoint = "eglTerminate", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern int Terminate(IntPtr display);
[DllImport(LibEgl, EntryPoint = "eglSwapBuffers", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern int SwapBuffers(IntPtr display, IntPtr surface);
[DllImport(LibEgl, EntryPoint = "eglSwapInterval", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern int SwapInterval(IntPtr display, int interval);
}
public unsafe static class Test
{
private static IntPtr display, surface;
const string vertexShaderSource = @"#version 300 es
precision highp float;
layout (location = 0) in vec4 Position0;
layout (location = 1) in vec4 Color0;
out vec4 color;
void main()
{
gl_Position = Position0;
color = Color0;
}";
const string fragmentShaderSource = @"#version 300 es
precision highp float;
in vec4 color;
out vec4 fragColor;
void main()
{
fragColor = color;
}";
[JSInvokable("frame")]
public static void Frame(double time)
{
GL.glClearColor(1.0f, 1.0f * 0.35f, 0f, 1.0f);
GL.glClear((uint)AttribMask.ColorBufferBit);
}
public static int Main(string[] args)
{
Console.WriteLine($"Hello from .Net6!");
display = EGL.GetDisplay(IntPtr.Zero);
if (display == IntPtr.Zero)
throw new Exception("Display was null");
if (!EGL.Initialize(display, out int major, out int minor))
throw new Exception("Initialize() returned false.");
int[] attributeList = new int[]
{
EGL.EGL_RED_SIZE , 8,
EGL.EGL_GREEN_SIZE, 8,
EGL.EGL_BLUE_SIZE , 8,
EGL.EGL_DEPTH_SIZE, 24,
EGL.EGL_STENCIL_SIZE, 8,
EGL.EGL_SURFACE_TYPE, EGL.EGL_WINDOW_BIT,
EGL.EGL_RENDERABLE_TYPE, EGL.EGL_OPENGL_ES3_BIT,
EGL.EGL_SAMPLES, 16, //MSAA, 16 samples
EGL.EGL_NONE
};
var config = IntPtr.Zero;
var numConfig = IntPtr.Zero;
if (!EGL.ChooseConfig(display, attributeList, ref config, (IntPtr)1, ref numConfig))
throw new Exception("ChoseConfig() failed");
if (numConfig == IntPtr.Zero)
throw new Exception("ChoseConfig() returned no configs");
if (!EGL.BindApi(EGL.EGL_OPENGL_ES_API))
throw new Exception("BindApi() failed");
int[] ctxAttribs = new int[] { EGL.EGL_CONTEXT_CLIENT_VERSION, 3, EGL.EGL_NONE };
var context = EGL.CreateContext(display, config, (IntPtr)EGL.EGL_NO_CONTEXT, ctxAttribs);
if (context == IntPtr.Zero)
throw new Exception("CreateContext() failed");
// now create the surface
surface = EGL.CreateWindowSurface(display, config, IntPtr.Zero, IntPtr.Zero);
if (surface == IntPtr.Zero)
throw new Exception("CreateWindowSurface() failed");
if (!EGL.MakeCurrent(display, surface, surface, context))
throw new Exception("MakeCurrent() failed");
GL.LoadAllFunctions(EGL.GetProcAddress);
// int width = EGL.????
// int height = EGL.????
// GL.glViewport(0, 0, width, height);
EGL.SwapBuffers(display, surface);
// https://github.com/emepetres/dotnet-wasm-sample/blob/main/src/jsinteraction/wasm/WebAssemblyRuntime.cs
using var runtime = new MyRuntime();
runtime.InvokeVoid("initialize");
uint vertexShader = GL.glCreateShader(ShaderType.VertexShader);
IntPtr* textPtr = stackalloc IntPtr[1];
var lengthArray = stackalloc int[1];
lengthArray[0] = vertexShaderSource.Length;
textPtr[0] = Marshal.StringToHGlobalAnsi(vertexShaderSource);
GL.glShaderSource(vertexShader, 1, (IntPtr)textPtr, lengthArray);
GL.glCompileShader(vertexShader);
// checkErrors
int success = 0;
var infoLog = stackalloc char[512];
lengthArray[0] = success;
GL.glGetShaderiv(vertexShader, ShaderParameterName.CompileStatus, lengthArray);
if (success > 0)
{
GL.glGetShaderInfoLog(vertexShader, 512, (int*)0, infoLog);
Console.WriteLine($"Error: shader vertex compilation failed: {new string(infoLog)}");
}
uint fragmentShader = GL.glCreateShader(ShaderType.FragmentShader);
lengthArray[0] = fragmentShaderSource.Length;
textPtr[0] = Marshal.StringToHGlobalAnsi(fragmentShaderSource);
GL.glShaderSource(fragmentShader, 1, (IntPtr)textPtr, lengthArray);
GL.glCompileShader(fragmentShader);
// checkErrors
lengthArray[0] = success;
GL.glGetShaderiv(fragmentShader, ShaderParameterName.CompileStatus, lengthArray);
if (success > 0)
{
GL.glGetShaderInfoLog(fragmentShader, 512, (int*)0, infoLog);
Console.WriteLine($"Error: shader fragment compilation failed: {new string(infoLog)}");
}
uint shaderProgram = GL.glCreateProgram();
GL.glAttachShader(shaderProgram, vertexShader);
GL.glAttachShader(shaderProgram, fragmentShader);
GL.glLinkProgram(shaderProgram);
// checkErrors
lengthArray[0] = success;
GL.glGetProgramiv(shaderProgram, ProgramPropertyARB.LinkStatus, lengthArray);
if (success > 0)
{
GL.glGetProgramInfoLog(shaderProgram, 512, (int*)0, infoLog);
Console.WriteLine($"Error: shader program compilation failed: {new string(infoLog)}");
}
GL.glDeleteShader(vertexShader);
GL.glDeleteShader(fragmentShader);
float[] vertices = {
0f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f
};
uint VBO = 0;
uint VAO = 0;
GL.glGenVertexArrays(1, &VAO);
GL.glGenBuffers(1, &VBO);
GL.glBindVertexArray(VAO);
GL.glBindBuffer(BufferTargetARB.ArrayBuffer, VBO);
fixed (float* verticesPtr = &vertices[0])
{
GL.glBufferData(BufferTargetARB.ArrayBuffer, vertices.Length * sizeof(float), verticesPtr, BufferUsageARB.StaticDraw);
}
int stride = 8 * sizeof(float);
GL.glVertexAttribPointer(0, 4, VertexAttribPointerType.Float, false, stride, (void*)null);
GL.glEnableVertexAttribArray(0);
GL.glVertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, stride, (void*)16);
GL.glEnableVertexAttribArray(1);
GL.glBindBuffer(BufferTargetARB.ArrayBuffer, 0);
GL.glBindVertexArray(0);
GL.glUseProgram(shaderProgram);
GL.glBindVertexArray(VAO);
GL.glDrawArrays(PrimitiveType.Triangles, 0, 3);
EGL.SwapBuffers(display, surface);
return args.Length;
}
}