ReflUtil.cs 9.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PoroCYon.FNAGLSL {
	public static class ReflUtil {
		readonly static FieldInfo gd_GLDevice_fi = typeof(GraphicsDevice).GetField(
			"GLDevice", BindingFlags.GetField|BindingFlags.NonPublic|BindingFlags.Instance);
		readonly static FieldInfo ef_glEffect_fi = typeof(Effect).GetField(
			"glEffect", BindingFlags.GetField|BindingFlags.NonPublic|BindingFlags.Instance);
pcy's avatar
pcy committed
15
16
17
18
		readonly static FieldInfo ep_texture_fi  = typeof(EffectParameter).GetField(
			"texture" , BindingFlags.GetField|BindingFlags.NonPublic|BindingFlags.Instance);
		readonly static FieldInfo tx_texture_fi  = typeof(Texture).GetField(
			"texture" , BindingFlags.GetField|BindingFlags.NonPublic|BindingFlags.Instance);
pcy's avatar
lol    
pcy committed
19
20
		readonly static FieldInfo gd_vbufCnt_fi  = typeof(GraphicsDevice).GetField(
			"vertexBufferCount", BindingFlags.GetField|BindingFlags.NonPublic|BindingFlags.Instance);
21

22
23
24
25
26
27
28
29
30
		readonly static PropertyInfo ef_Techniques_pi = typeof(Effect).GetProperty(
			"Techniques", BindingFlags.SetProperty|BindingFlags.Public|BindingFlags.Instance),
									 ef_Parameters_pi = typeof(Effect).GetProperty(
			"Parameters", BindingFlags.SetProperty|BindingFlags.Public|BindingFlags.Instance);

		readonly static ConstructorInfo
			ci_EffTechColl, ci_EffParamColl, ci_EffAnnotColl,
			ci_EffTech, ci_EffParam, ci_EffAnnot;

pcy's avatar
pcy committed
31
32
33
34
35
36
		readonly static int ci_EffParam_plen;

		static MethodInfo gldev_DeleteEffect_mi;
		static PropertyInfo ef_GLEffectData_pi, ef_EffectData_pi,
			gltex_Handle_pi, gltex_Target_pi;

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
		static ReflUtil() {
			var pm1f = new ParameterModifier(1);
			pm1f[0] = false;

			var pm4f = new ParameterModifier(4);
			pm4f[3] = pm4f[2] = pm4f[1] = pm4f[0] = false;

			var pm11f = new ParameterModifier(11);
			pm11f[3] = pm11f[2] = pm11f[1] = pm11f[0] = false;
			pm11f[7] = pm11f[6] = pm11f[5] = pm11f[4] = false;
			           pm11f[10]= pm11f[9] = pm11f[8] = false;

			var pm7f = new ParameterModifier(7);
			pm7f[3] = pm7f[2] = pm7f[1] = pm7f[0] = false;
			          pm7f[6] = pm7f[5] = pm7f[4] = false;

			var pm1fa = new[]{pm1f};
			ci_EffTechColl = typeof(EffectTechniqueCollection)
				.GetConstructor(BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(List<EffectTechnique>)}, pm1fa);

			ci_EffParamColl = typeof(EffectParameterCollection)
				.GetConstructor(BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(List<EffectParameter>)}, pm1fa);

			ci_EffAnnotColl = typeof(EffectAnnotationCollection)
				.GetConstructor(BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(List<EffectAnnotation>)}, pm1fa);

			ci_EffTech = typeof(EffectTechnique).GetConstructor(
				BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(string), typeof(IntPtr),
					typeof(EffectPassCollection), typeof(EffectAnnotationCollection)},
				new[]{pm4f});

			ci_EffParam = /*typeof(EffectParameter).GetConstructor( // FIXME: no worky
				BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(string), typeof(string), typeof(int),
					typeof(int), typeof(int), typeof(EffectParameterClass),
					typeof(EffectParameterType), typeof(EffectParameterCollection),
					typeof(EffectAnnotationCollection), typeof(IntPtr), typeof(uint)},
				new[]{pm11f});*/
				// fuck it
				typeof(EffectParameter).GetConstructors(BindingFlags.Instance
					|BindingFlags.CreateInstance|BindingFlags.NonPublic)[0];
pcy's avatar
pcy committed
82
83
84
			// ^ turns out, depending on the FNA version, the dataSize (uint)
			//   parameter mightn't be included
			ci_EffParam_plen = ci_EffParam.GetParameters().Length;
85
86
87
88
89
90
91
92
93
94
95
96
97

			ci_EffAnnot = typeof(EffectAnnotation).GetConstructor(
				BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(string), typeof(string), typeof(int),
					typeof(int), typeof(EffectParameterClass),
					typeof(EffectParameterType), typeof(IntPtr)}, new[]{pm7f});

			if (ci_EffTechColl == null || ci_EffParamColl == null ||
					ci_EffAnnotColl == null || ci_EffTech == null ||
					ci_EffParam == null || ci_EffAnnot == null)
				throw new Exception("can't find ctors");
		}

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
		public static object GLDevice(this GraphicsDevice gd) =>
			gd_GLDevice_fi.GetValue(gd);
		public static object glEffect(this Effect eff) =>
			ef_glEffect_fi.GetValue(eff);

		static object[] arg1 = new object[1];
		public static void DeleteEffect(this GraphicsDevice gd, Effect eff) {
			var gldev = gd.GLDevice();
			if (gldev_DeleteEffect_mi == null) {
				// type of the gldev not known at compile-time!
				gldev_DeleteEffect_mi = gldev.GetType().GetMethod("DeleteEffect",
					BindingFlags.InvokeMethod|BindingFlags.NonPublic|BindingFlags.Instance);
			}

			arg1[0] = eff.glEffect();
			gldev_DeleteEffect_mi.Invoke(gldev, arg1);
		}

		public static IntPtr GetGLEffectData(this Effect eff) {
			var gleff = eff.glEffect();

			if (ef_GLEffectData_pi == null) {
				// type of the gleff not known at compile-time!
				ef_GLEffectData_pi = gleff.GetType().GetProperty("GLEffectData",
					BindingFlags.SetProperty|BindingFlags.NonPublic|BindingFlags.Instance);
			}

125
			return (IntPtr)ef_GLEffectData_pi.GetValue(gleff, null);
126
127
128
129
130
131
132
133
134
135
		}
		public static void SetGLEffectData(this Effect eff, IntPtr value) {
			var gleff = eff.glEffect();

			if (ef_GLEffectData_pi == null) {
				// type of the gleff not known at compile-time!
				ef_GLEffectData_pi = gleff.GetType().GetProperty("GLEffectData",
					BindingFlags.SetProperty|BindingFlags.NonPublic|BindingFlags.Instance);
			}

136
			ef_GLEffectData_pi.SetValue(gleff, value, null);
137
138
139
140
141
142
143
144
145
146
147
		}

		public static IntPtr GetEffectData(this Effect eff) {
			var gleff = eff.glEffect();

			if (ef_EffectData_pi == null) {
				// type of the gleff not known at compile-time!
				ef_EffectData_pi = gleff.GetType().GetProperty("EffectData",
					BindingFlags.SetProperty|BindingFlags.NonPublic|BindingFlags.Instance);
			}

148
			return (IntPtr)ef_EffectData_pi.GetValue(gleff, null);
149
150
151
152
153
154
155
156
157
158
		}
		public static void SetEffectData(this Effect eff, IntPtr value) {
			var gleff = eff.glEffect();

			if (ef_EffectData_pi == null) {
				// type of the gleff not known at compile-time!
				ef_EffectData_pi = gleff.GetType().GetProperty("EffectData",
					BindingFlags.SetProperty|BindingFlags.NonPublic|BindingFlags.Instance);
			}

159
			ef_EffectData_pi.SetValue(gleff, value, null);
160
		}
161
162

		public static void SetTechniques(this Effect eff, EffectTechniqueCollection value) {
163
			ef_Techniques_pi.SetValue(eff, value, null);
164
165
		}
		public static void SetParameters(this Effect eff, EffectParameterCollection value) {
166
			ef_Parameters_pi.SetValue(eff, value, null);
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
		}

		public static EffectTechniqueCollection CreateTechColl(List<EffectTechnique> techniques)
			=> (EffectTechniqueCollection)ci_EffTechColl.Invoke(new[]{techniques});
		public static EffectParameterCollection CreateParamColl(List<EffectParameter> parms)
			=> (EffectParameterCollection)ci_EffParamColl.Invoke(new[]{parms});
		public static EffectAnnotationCollection CreateAnnotColl(List<EffectAnnotation> annots)
			=> (EffectAnnotationCollection)ci_EffAnnotColl.Invoke(new[]{annots});

		public static EffectTechnique CreateTechnique(string name, IntPtr pointer,
				EffectPassCollection passes, EffectAnnotationCollection annots)
			=> (EffectTechnique)ci_EffTech.Invoke(new object[]{name, pointer,
					passes, annots});
		public static EffectParameter CreateParameter(string name, string semantic,
				int rowCount, int colCount, int elemCount,
				EffectParameterClass paramClass, EffectParameterType paramType,
				EffectParameterCollection structMembers, EffectAnnotationCollection annots,
184
				IntPtr data, uint dataSize) {
pcy's avatar
pcy committed
185
			int plen = ci_EffParam_plen;
186
187
188
189
190
191
192
193
194
195
196
197
			if (plen == 11) {
				return (EffectParameter)ci_EffParam.Invoke(new object[]{name, semantic,
						rowCount, colCount, elemCount, paramClass, paramType,
						structMembers, annots, data, dataSize});
			} else if (plen == 10) {
				return (EffectParameter)ci_EffParam.Invoke(new object[]{name, semantic,
						rowCount, colCount, elemCount, paramClass, paramType,
						structMembers, annots, data});
			} else {
				throw new NotSupportedException("Unsupported nargs for EffectParameter ctor! (" + plen + ")");
			}
		}
198
199
200
201
202
		public static EffectAnnotation CreateAnnotation(string name, string semantic,
				int rowCount, int colCount, EffectParameterClass paramClass,
				EffectParameterType paramType, IntPtr data)
			=> (EffectAnnotation)ci_EffAnnot.Invoke(new object[]{name, semantic,
					rowCount, colCount, paramClass, paramType, data});
pcy's avatar
pcy committed
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

		public static Texture GetValueTexture(this EffectParameter ep)
			=> (Texture)ep_texture_fi.GetValue(ep);

		public static uint GetGLTexID(this Texture tex) { // gl object id
			var igltex = tx_texture_fi.GetValue(tex);

			if (gltex_Handle_pi == null) {
				// IGLTexture type not known at compile-time
				gltex_Handle_pi = igltex.GetType().GetProperty("Handle",
					BindingFlags.GetProperty|BindingFlags.Instance|BindingFlags.Public);
			}

			return (uint)gltex_Handle_pi.GetValue(igltex, null);
		}
		public static int GetGLTexTarget(this Texture tex) { // GL_TEXTURE_[123]D etc
			var igltex = tx_texture_fi.GetValue(tex);

			if (gltex_Target_pi == null) {
				// IGLTexture type not known at compile-time
				gltex_Target_pi = igltex.GetType().GetProperty("Target",
					BindingFlags.GetProperty|BindingFlags.Instance|BindingFlags.Public);
			}

			return (int)gltex_Target_pi.GetValue(igltex, null);
		}
pcy's avatar
lol    
pcy committed
229
230
231
232
233
234

		public static int GetVertexBufferCount(this GraphicsDevice gd) =>
			(int)gd_vbufCnt_fi.GetValue(gd);
		public static void SetVertexBufferCount(this GraphicsDevice gd, int value) {
			gd_vbufCnt_fi.SetValue(gd, value);
		}
235
236
237
	}
}