GLUtil.cs 4.41 KB
Newer Older
pcy's avatar
initial  
pcy committed
1
2
using System;
using System.Collections.Generic;
pcy's avatar
lol    
pcy committed
3
using System.Diagnostics;
pcy's avatar
initial  
pcy committed
4
5
6
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
pcy's avatar
pcy committed
7
using System.Text;
pcy's avatar
initial  
pcy committed
8
9
10
11
12
13
14
15
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SDL2;

using static SDL2.SDL;

namespace PoroCYon.FNAGLSL {
	public static unsafe partial class GL {
pcy's avatar
pcy committed
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
		internal readonly static UTF8Encoding utf8 = new UTF8Encoding(false, true);

		static byte[] utf8buf = new byte[64];
		internal static byte[] GetNullTermUTF8(string s, bool newarr = false) {
			int nbytes = utf8.GetByteCount(s);
			byte[] ret;

			if (newarr) {
				ret = new byte[nbytes + 1];
			} else {
				if (utf8buf.Length < nbytes + 1)
					Array.Resize<byte>(ref utf8buf, nbytes + 1);
				ret = utf8buf;
			}

			utf8.GetBytes(s, 0, s.Length, ret, 0);
			ret[nbytes] = 0;
			return ret;
		}
		internal static string StrFromNullTermUTF8(byte[] s) {
			fixed (byte* b = s) {
				return StrFromNullTermUTF8((sbyte*)b, s.Length);
			}
		}
		internal static string StrFromNullTermUTF8(sbyte[] s) {
			fixed (sbyte* b = s) {
				return StrFromNullTermUTF8(b, s.Length);
			}
		}

		static long strlen(sbyte* s) {
			long r = 0;
			for (; *s != 0; ++s, ++r) ;
			return r;
		}
		internal static string StrFromNullTermUTF8(sbyte* s, int nbytes = -1) {
			if (nbytes < 0) {
				nbytes = (int)strlen(s);;
			} /*else {
				long sl = strlen(s);
				if (nbytes > sl) nbytes = (int)sl;
			}*/

			return new String(s, 0, nbytes, utf8);
		}

pcy's avatar
initial  
pcy committed
62
63
64
		public static void Throw() {
			GLError err = GetError();

pcy's avatar
pcy committed
65
			if (err != GLError.NoError) throw new GLException(err);
pcy's avatar
initial  
pcy committed
66
67
68
69
70
71
72
73
74
		}

		internal static void CheckFNA() {
			if (typeof(Vector2).Assembly != typeof(Texture2D).Assembly) {
				throw new NotSupportedException(
					"Running XNA or MonoGame, not FNA! Can't use FNA-GLSL.");
			}
		}

pcy's avatar
lol    
pcy committed
75
		static T GetProcAddress<T>(string name, bool req = true)
pcy's avatar
initial  
pcy committed
76
77
78
79
80
81
82
				where T : class {
			if (!typeof(T).IsSubclassOf(typeof(Delegate))) {
				throw new ArgumentException("Type '" + typeof(T) + "' is not a delegate type!");
			}

			IntPtr fn = SDL_GL_GetProcAddress("gl"+name);
			if (fn == IntPtr.Zero) {
pcy's avatar
lol    
pcy committed
83
84
				if (req) throw new NotSupportedException("gl"+name);
				else return null;
pcy's avatar
initial  
pcy committed
85
86
87
88
89
90
91
92
93
94
			}

			return (T)(object)Marshal.GetDelegateForFunctionPointer(fn, typeof(T));
		}

		static bool inited = false;
		public static void Init(GraphicsDevice gd) {
			CheckFNA();
			if (inited) return;

95
96
97
98
			var nam = gd.GLDevice().GetType().Name;
			if (!nam.Contains("GLDevice"))
				throw new NotSupportedException("need OpenGL, but FNA is using"+
					" the " + nam + " backend!");
pcy's avatar
initial  
pcy committed
99
100
101
102

			InitProcs();

			inited = true;
pcy's avatar
lol    
pcy committed
103
104
105
106
107
108
109
110

			GL.Enable(GL.DEBUG_OUTPUT);
			GL.Throw();

			GL.DebugMessageCallback(DebugCb, IntPtr.Zero);

			GL.Enable(GL.DEBUG_OUTPUT_SYNCHRONOUS);
			GL.Throw();
pcy's avatar
initial  
pcy committed
111
112
		}
		public static void Deinit() {
pcy's avatar
lol    
pcy committed
113
114
			GL.DebugMessageCallback(null, IntPtr.Zero);

pcy's avatar
initial  
pcy committed
115
116
117
			DeinitProcs();
			inited = false;
		}
pcy's avatar
lol    
pcy committed
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

		static void DebugCb(uint src, uint typ, uint id,
				uint sev, int len, sbyte* msg, IntPtr ud) {
			//if (sev == DEBUG_SEVERITY_NOTIFICATION) return;

			string str = new string(msg, 0, len);

			string ssrc = "???", styp = "???", ssev = "???";

			switch (src) {
				case DEBUG_SOURCE_API: ssrc = "API"; break;
				case DEBUG_SOURCE_WINDOW_SYSTEM: ssrc = "winsys"; break;
				case DEBUG_SOURCE_SHADER_COMPILER: ssrc = "shdrc"; break;
				case DEBUG_SOURCE_THIRD_PARTY: ssrc = "3rdp"; break;
				case DEBUG_SOURCE_APPLICATION: ssrc = "app"; break;
				case DEBUG_SOURCE_OTHER: ssrc = "other"; break;
			}

			switch (typ) {
				case DEBUG_TYPE_ERROR: styp = "err"; break;
				case DEBUG_TYPE_DEPRECATED_BEHAVIOR: styp = "depr"; break;
				case DEBUG_TYPE_UNDEFINED_BEHAVIOR: styp = "und"; break;
				case DEBUG_TYPE_PORTABILITY: styp = "port"; break;
				case DEBUG_TYPE_PERFORMANCE: styp = "perf"; break;
				case DEBUG_TYPE_MARKER: styp = "mark"; break;
				case DEBUG_TYPE_PUSH_GROUP: styp = "push"; break;
				case DEBUG_TYPE_POP_GROUP: styp = "pop"; break;
				case DEBUG_TYPE_OTHER: styp = "other"; break;
			}

			switch (sev) {
				case DEBUG_SEVERITY_HIGH: ssev = "high"; break;
				case DEBUG_SEVERITY_MEDIUM: ssev = "med"; break;
				case DEBUG_SEVERITY_LOW: ssev = "low"; break;
				case DEBUG_SEVERITY_NOTIFICATION: ssev = "notif"; break;
			}

			Console.WriteLine("GL dbgcb: src="+ssrc+" typ="+styp+" sev="+ssev+" id="+id
					+":\t"+str);

			if (sev == DEBUG_SEVERITY_HIGH || typ == DEBUG_TYPE_ERROR) {
				Console.WriteLine(new StackTrace(true));
				Console.WriteLine();
			}
		}
pcy's avatar
initial  
pcy committed
163
164
165
	}
}