ReflUtil.cs 9.33 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);
19

20
21
22
23
24
25
26
27
28
		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
29
30
31
32
33
34
		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;

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
		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
80
81
82
			// ^ turns out, depending on the FNA version, the dataSize (uint)
			//   parameter mightn't be included
			ci_EffParam_plen = ci_EffParam.GetParameters().Length;
83
84
85
86
87
88
89
90
91
92
93
94
95

			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");
		}

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
		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);
			}

123
			return (IntPtr)ef_GLEffectData_pi.GetValue(gleff, null);
124
125
126
127
128
129
130
131
132
133
		}
		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);
			}

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

		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);
			}

146
			return (IntPtr)ef_EffectData_pi.GetValue(gleff, null);
147
148
149
150
151
152
153
154
155
156
		}
		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);
			}

157
			ef_EffectData_pi.SetValue(gleff, value, null);
158
		}
159
160

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

		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,
182
				IntPtr data, uint dataSize) {
pcy's avatar
pcy committed
183
			int plen = ci_EffParam_plen;
184
185
186
187
188
189
190
191
192
193
194
195
			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 + ")");
			}
		}
196
197
198
199
200
		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
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

		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);
		}
227
228
229
	}
}