fna-workbench

fna-workbench Commit Details


Date:2016-03-18 13:18:59 (8 years 9 months ago)
Author:Ethan Lee
Branch:master
Commit:a7e003b1f859cba951f340fe626a3f99e3ca3622
Parents: 453dd6a2658c634ce511599c520a4b7e3c98eb3c
Message:Getting GCHandles out of the GL/AL devices...

Changes:

File differences

src/Audio/DynamicSoundEffectInstance.cs
1010
1111
1212
13
1314
1415
1516
......
160161
161162
162163
164
163165
164166
165167
166
168
167169
168170
169171
170172
173
171174
172175
173176
......
328331
329332
330333
334
331335
332336
333337
334
338
339
335340
336341
342
337343
338344
339345
......
365371
366372
367373
374
368375
369376
370377
371
378
372379
373380
374381
375382
383
376384
377385
378386
#region Using Statements
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
#endregion
namespace Microsoft.Xna.Framework.Audio
// Push the data to OpenAL.
IALBuffer newBuf = availableBuffers.Dequeue();
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
AudioDevice.ALDevice.SetBufferData(
newBuf,
channels,
buffer,
handle.AddrOfPinnedObject(),
offset,
count,
sampleRate
);
handle.Free();
// If we're already playing, queue immediately.
if (INTERNAL_alSource != null)
{
if (INTERNAL_alSource != null && queuedBuffers.Count > 0)
{
GCHandle handle = GCHandle.Alloc(samples, GCHandleType.Pinned);
AudioDevice.ALDevice.GetBufferData(
INTERNAL_alSource,
queuedBuffers.ToArray(), // FIXME: Blech -flibit
samples,
handle.AddrOfPinnedObject(),
samples.Length,
channels
);
handle.Free();
}
else
{
// Push buffer to the AL.
IALBuffer newBuf = availableBuffers.Dequeue();
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
AudioDevice.ALDevice.SetBufferData(
newBuf,
channels,
buffer,
handle.AddrOfPinnedObject(),
offset,
count,
sampleRate
);
handle.Free();
// If we're already playing, queue immediately.
if (INTERNAL_alSource != null)
src/Audio/IALDevice.cs
3636
3737
3838
39
39
4040
4141
4242
4343
44
44
4545
4646
47
47
4848
4949
5050
......
7676
7777
7878
79
79
80
8081
8182
8283
void SetBufferData(
IALBuffer buffer,
AudioChannels channels,
byte[] data,
IntPtr data,
int offset,
int count,
int sampleRate
);
void SetBufferData(
void SetBufferFloatData(
IALBuffer buffer,
AudioChannels channels,
float[] data,
IntPtr data,
int offset,
int count,
int sampleRate
void GetBufferData(
IALSource source,
IALBuffer[] buffer,
float[] samples,
IntPtr samples,
int samplesLen,
AudioChannels channels
);
src/Audio/NullDevice.cs
9191
9292
9393
94
94
9595
9696
9797
......
9999
100100
101101
102
102
103103
104104
105
105
106106
107107
108108
......
219219
220220
221221
222
222
223
223224
224225
225226
public void SetBufferData(
IALBuffer buffer,
AudioChannels channels,
byte[] data,
IntPtr data,
int offset,
int count,
int sampleRate
// No-op, duh.
}
public void SetBufferData(
public void SetBufferFloatData(
IALBuffer buffer,
AudioChannels channels,
float[] data,
IntPtr data,
int offset,
int count,
int sampleRate
public void GetBufferData(
IALSource source,
IALBuffer[] buffer,
float[] samples,
IntPtr samples,
int samplesLen,
AudioChannels channels
) {
// No-op, duh.
src/Audio/OpenALDevice.cs
326326
327327
328328
329
329
330330
331331
332332
333333
334
335334
336335
337336
338
337
339338
340339
341340
342
343341
344342
345343
346344
347345
348
346
349347
350348
351
349
352350
353351
354352
355353
356
357354
358355
359356
360
357
361358
362359
363360
364
365361
366362
367363
......
659655
660656
661657
662
658
659
663660
664661
665
662
666663
667664
668665
......
696693
697694
698695
699
700696
701697
702698
......
707703
708704
709705
710
706
711707
712708
713709
......
721717
722718
723719
724
720
725721
726722
727
728723
729724
730725
public void SetBufferData(
IALBuffer buffer,
AudioChannels channels,
byte[] data,
IntPtr data,
int offset,
int count,
int sampleRate
) {
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
AL10.alBufferData(
(buffer as OpenALBuffer).Handle,
XNAToShort[(int) channels],
handle.AddrOfPinnedObject() + offset,
data + offset,
(IntPtr) count,
(IntPtr) sampleRate
);
handle.Free();
#if VERBOSE_AL_DEBUGGING
CheckALError();
#endif
}
public void SetBufferData(
public void SetBufferFloatData(
IALBuffer buffer,
AudioChannels channels,
float[] data,
IntPtr data,
int offset,
int count,
int sampleRate
) {
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
AL10.alBufferData(
(buffer as OpenALBuffer).Handle,
XNAToFloat[(int) channels],
handle.AddrOfPinnedObject() + (offset * 4),
data + (offset * 4),
(IntPtr) (count * 4),
(IntPtr) sampleRate
);
handle.Free();
#if VERBOSE_AL_DEBUGGING
CheckALError();
#endif
public void GetBufferData(
IALSource source,
IALBuffer[] buffer,
float[] samples,
IntPtr samples,
int samplesLen,
AudioChannels channels
) {
int copySize1 = samples.Length / (int) channels;
int copySize1 = samplesLen / (int) channels;
int copySize2 = 0;
// Where are we now?
}
// Copy!
GCHandle handle = GCHandle.Alloc(samples, GCHandleType.Pinned);
if (copySize1 > 0)
{
ALEXT.alGetBufferSamplesSOFT(
ALEXT.AL_STEREO_SOFT :
ALEXT.AL_MONO_SOFT,
ALEXT.AL_FLOAT_SOFT,
handle.AddrOfPinnedObject()
samples
);
offset = 0;
}
ALEXT.AL_STEREO_SOFT :
ALEXT.AL_MONO_SOFT,
ALEXT.AL_FLOAT_SOFT,
handle.AddrOfPinnedObject() + (copySize1 * (int) channels)
samples + (copySize1 * (int) channels)
);
}
handle.Free();
}
#endregion
src/Graphics/GraphicsDevice.cs
737737
738738
739739
740
740
741741
742742
743743
......
745745
746746
747747
748
748
749749
750750
751751
......
754754
755755
756756
757
757
758
759
760
761
762
763
764
765
766
767
758768
759769
760770
public void GetBackBufferData<T>(T[] data) where T : struct
{
GLDevice.ReadBackbuffer(data, 0, data.Length, null);
GetBackBufferData(null, data, 0, data.Length);
}
public void GetBackBufferData<T>(
int startIndex,
int elementCount
) where T : struct {
GLDevice.ReadBackbuffer(data, startIndex, elementCount, null);
GetBackBufferData(null, data, startIndex, elementCount);
}
public void GetBackBufferData<T>(
int startIndex,
int elementCount
) where T : struct {
GLDevice.ReadBackbuffer(data, startIndex, elementCount, rect);
int elementSizeInBytes = Marshal.SizeOf(typeof(T));
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
GLDevice.ReadBackbuffer(
handle.AddrOfPinnedObject(),
data.Length * elementSizeInBytes,
startIndex,
elementCount,
elementSizeInBytes,
rect
);
handle.Free();
}
#endregion
src/Graphics/IGLDevice.cs
148148
149149
150150
151
152
151
152
153
153154
154155
156
155157
156
158
157159
158160
159161
......
258260
259261
260262
261
263
262264
263
264265
265
266
266267
267268
269
268270
269
270
271
272
271273
272274
273
275
274276
275277
278
276279
277
280
278281
279282
280283
......
282285
283286
284287
285
288
286289
287290
288
291
289292
290293
294
291295
292
293
296
297
294298
295299
296
300
297301
298
299
302
303
304
300305
301306
302307
);
void ResolveTarget(RenderTargetBinding target);
void ReadBackbuffer<T>(
T[] data,
void ReadBackbuffer(
IntPtr data,
int dataLen,
int startIndex,
int elementCount,
int elementSizeInBytes,
Rectangle? rect
) where T : struct;
);
IGLTexture CreateTexture2D(
SurfaceFormat format,
int vertexStride
);
void AddDisposeVertexBuffer(IGLBuffer buffer);
void SetVertexBufferData<T>(
void SetVertexBufferData(
IGLBuffer buffer,
int elementSizeInBytes,
int offsetInBytes,
T[] data,
IntPtr data,
int startIndex,
int elementCount,
int elementSizeInBytes,
SetDataOptions options
) where T : struct;
void GetVertexBufferData<T>(
);
void GetVertexBufferData(
IGLBuffer buffer,
int offsetInBytes,
T[] data,
IntPtr data,
int startIndex,
int elementCount,
int elementSizeInBytes,
int vertexStride
) where T : struct;
);
IGLBuffer GenIndexBuffer(
bool dynamic,
IndexElementSize indexElementSize
);
void AddDisposeIndexBuffer(IGLBuffer buffer);
void SetIndexBufferData<T>(
void SetIndexBufferData(
IGLBuffer buffer,
int offsetInBytes,
T[] data,
IntPtr data,
int startIndex,
int elementCount,
int elementSizeInBytes,
SetDataOptions options
) where T : struct;
void GetIndexBufferData<T>(
);
void GetIndexBufferData(
IGLBuffer buffer,
int offsetInBytes,
T[] data,
IntPtr data,
int startIndex,
int elementCount
) where T : struct;
int elementCount,
int elementSizeInBytes
);
IGLEffect CreateEffect(byte[] effectCode);
IGLEffect CloneEffect(IGLEffect effect);
src/Graphics/OpenGLDevice.cs
21782178
21792179
21802180
2181
2181
21822182
2183
21842183
2185
2184
21862185
21872186
2187
21882188
2189
2189
21902190
21912191
21922192
......
22032203
22042204
22052205
2206
2207
22082206
22092207
22102208
22112209
2212
2210
22132211
22142212
2215
2216
22172213
22182214
22192215
22202216
22212217
2222
2218
22232219
22242220
2225
2221
22262222
22272223
2224
22282225
2229
2226
22302227
22312228
22322229
......
22432240
22442241
22452242
2246
2247
2248
22492243
22502244
22512245
22522246
2253
2247
22542248
22552249
2256
2257
22582250
22592251
22602252
......
22642256
22652257
22662258
2267
2259
22682260
22692261
2270
2262
22712263
22722264
2265
22732266
2274
2267
22752268
22762269
22772270
22782271
22792272
22802273
2281
2282
22832274
22842275
22852276
22862277
2287
2278
22882279
22892280
2290
2291
22922281
22932282
22942283
22952284
22962285
2297
2286
22982287
22992288
2300
2289
23012290
2302
2303
2291
2292
2293
23042294
23052295
23062296
23072297
23082298
23092299
2310
2311
2312
23132300
23142301
23152302
2316
2317
2303
2304
23182305
23192306
2320
2321
23222307
23232308
23242309
......
30933078
30943079
30953080
3096
3097
3081
3082
3083
30983084
30993085
3086
31003087
3101
3102
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
31033101
31043102
31053103
......
31323130
31333131
31343132
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3133
3134
3135
3136
3137
3138
3139
3140
3141
31523142
31533143
31543144
31553145
3156
3157
3146
3147
3148
3149
31583150
31593151
3160
3161
3162
3152
3153
3154
3155
31633156
3157
31643158
3159
3160
31653161
31663162
31673163
#region glSetBufferData Methods
public void SetVertexBufferData<T>(
public void SetVertexBufferData(
IGLBuffer buffer,
int elementSizeInBytes,
int offsetInBytes,
T[] data,
IntPtr data,
int startIndex,
int elementCount,
int elementSizeInBytes,
SetDataOptions options
) where T : struct {
) {
#if !DISABLE_THREADING
ForceToMainThread(() => {
#endif
);
}
GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
glBufferSubData(
GLenum.GL_ARRAY_BUFFER,
(IntPtr) offsetInBytes,
(IntPtr) (elementSizeInBytes * elementCount),
(IntPtr) (dataHandle.AddrOfPinnedObject().ToInt64() + startIndex * elementSizeInBytes)
data + (startIndex * elementSizeInBytes)
);
dataHandle.Free();
#if !DISABLE_THREADING
});
#endif
}
public void SetIndexBufferData<T>(
public void SetIndexBufferData(
IGLBuffer buffer,
int offsetInBytes,
T[] data,
IntPtr data,
int startIndex,
int elementCount,
int elementSizeInBytes,
SetDataOptions options
) where T : struct {
) {
#if !DISABLE_THREADING
ForceToMainThread(() => {
#endif
);
}
GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
int elementSizeInBytes = Marshal.SizeOf(typeof(T));
glBufferSubData(
GLenum.GL_ELEMENT_ARRAY_BUFFER,
(IntPtr) offsetInBytes,
(IntPtr) (elementSizeInBytes * elementCount),
(IntPtr) (dataHandle.AddrOfPinnedObject().ToInt64() + startIndex * elementSizeInBytes)
data + (startIndex * elementSizeInBytes)
);
dataHandle.Free();
#if !DISABLE_THREADING
});
#endif
#region glGetBufferData Methods
public void GetVertexBufferData<T>(
public void GetVertexBufferData(
IGLBuffer buffer,
int offsetInBytes,
T[] data,
IntPtr data,
int startIndex,
int elementCount,
int elementSizeInBytes,
int vertexStride
) where T : struct {
) {
#if !DISABLE_THREADING
ForceToMainThread(() => {
#endif
BindVertexBuffer(buffer);
GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
glGetBufferSubData(
GLenum.GL_ARRAY_BUFFER,
(IntPtr) offsetInBytes,
(IntPtr) (elementCount * vertexStride),
dataHandle.AddrOfPinnedObject() + (startIndex * Marshal.SizeOf(typeof(T)))
data + (startIndex * elementSizeInBytes)
);
dataHandle.Free();
#if !DISABLE_THREADING
});
#endif
}
public void GetIndexBufferData<T>(
public void GetIndexBufferData(
IGLBuffer buffer,
int offsetInBytes,
T[] data,
IntPtr data,
int startIndex,
int elementCount
) where T : struct {
int elementCount,
int elementSizeInBytes
) {
#if !DISABLE_THREADING
ForceToMainThread(() => {
#endif
BindIndexBuffer(buffer);
GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
int typeSize = Marshal.SizeOf(typeof(T));
glGetBufferSubData(
GLenum.GL_ELEMENT_ARRAY_BUFFER,
(IntPtr) offsetInBytes,
(IntPtr) (elementCount * typeSize),
dataHandle.AddrOfPinnedObject() + (startIndex * typeSize)
(IntPtr) (elementCount * elementSizeInBytes),
data + (startIndex * elementSizeInBytes)
);
dataHandle.Free();
#if !DISABLE_THREADING
});
#endif
#region glReadPixels Methods
public void ReadBackbuffer<T>(
T[] data,
public void ReadBackbuffer(
IntPtr data,
int dataLen,
int startIndex,
int elementCount,
int elementSizeInBytes,
Rectangle? rect
) where T : struct {
if (startIndex > 0 || elementCount != data.Length)
) {
/* FIXME: Right now we're expecting one of the following:
* - byte[]
* - int[]
* - uint[]
* - Color[]
* Anything else will freak out because we're using
* color backbuffers. Maybe check this out when adding
* support for more backbuffer types!
* -flibit
*/
if (startIndex > 0 || elementCount != (dataLen / elementSizeInBytes))
{
throw new NotImplementedException(
"ReadBackbuffer startIndex/elementCount"
h = Backbuffer.Height;
}
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
glReadPixels(
x,
y,
w,
h,
GLenum.GL_RGBA,
GLenum.GL_UNSIGNED_BYTE,
handle.AddrOfPinnedObject()
);
}
finally
{
handle.Free();
}
glReadPixels(
x,
y,
w,
h,
GLenum.GL_RGBA,
GLenum.GL_UNSIGNED_BYTE,
data
);
BindReadFramebuffer(prevReadBuffer);
// Now we get to do a software-based flip! Yes, really! -flibit
int pitch = w * 4 / Marshal.SizeOf(typeof(T));
T[] tempRow = new T[pitch];
int pitch = w * 4;
byte[] tempRow = new byte[pitch];
GCHandle handle = GCHandle.Alloc(tempRow, GCHandleType.Pinned);
IntPtr temp = handle.AddrOfPinnedObject();
for (int row = 0; row < h / 2; row += 1)
{
Array.Copy(data, row * pitch, tempRow, 0, pitch);
Array.Copy(data, (h - row - 1) * pitch, data, row * pitch, pitch);
Array.Copy(tempRow, 0, data, (h - row - 1) * pitch, pitch);
// Top to temp, bottom to top, temp to bottom
memcpy(temp, data + (row * pitch), (IntPtr) pitch);
memcpy(data + (row * pitch), data + ((h - row - 1) * pitch), (IntPtr) pitch);
memcpy(data + ((h - row - 1) * pitch), temp, (IntPtr) pitch);
}
handle.Free();
}
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void memcpy(IntPtr dst, IntPtr src, IntPtr len);
/// <summary>
/// Attempts to read the texture data directly from the FBO using glReadPixels
src/Graphics/Vertices/IndexBuffer.cs
179179
180180
181181
182
182183
183184
184185
185
186
186187
187
188
189
188190
191
189192
190193
191194
......
254257
255258
256259
260
257261
258262
259263
260
264
261265
262266
267
263268
264269
270
265271
266272
267273
);
}
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
GraphicsDevice.GLDevice.GetIndexBufferData(
buffer,
offsetInBytes,
data,
handle.AddrOfPinnedObject(),
startIndex,
elementCount
elementCount,
Marshal.SizeOf(typeof(T))
);
handle.Free();
}
#endregion
throw new InvalidOperationException("The array specified in the data parameter is not the correct size for the amount of data requested.");
}
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
GraphicsDevice.GLDevice.SetIndexBufferData(
buffer,
offsetInBytes,
data,
handle.AddrOfPinnedObject(),
startIndex,
elementCount,
Marshal.SizeOf(typeof(T)),
options
);
handle.Free();
}
#endregion
src/Graphics/Vertices/VertexBuffer.cs
173173
174174
175175
176
176177
177178
178
179
179180
180181
181182
......
183184
184185
185186
187
186188
187189
188190
189
191
190192
191193
194
192195
193196
197
194198
195199
196200
......
288292
289293
290294
295
291296
292297
293
294298
295
299
296300
297301
302
298303
299304
305
300306
301307
302308
throw new NotSupportedException("Calling GetData on a resource that was created with BufferUsage.WriteOnly is not supported.");
}
int elementSizeInBytes = Marshal.SizeOf(typeof(T));
if (vertexStride == 0)
{
vertexStride = Marshal.SizeOf(typeof(T));
vertexStride = elementSizeInBytes;
}
if (elementCount > 1 &&
(elementCount * vertexStride) > (VertexCount * VertexDeclaration.VertexStride))
throw new InvalidOperationException("The array is not the correct size for the amount of data requested.");
}
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
GraphicsDevice.GLDevice.GetVertexBufferData(
buffer,
offsetInBytes,
data,
handle.AddrOfPinnedObject(),
startIndex,
elementCount,
elementSizeInBytes,
vertexStride
);
handle.Free();
}
#endregion
);
}
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
GraphicsDevice.GLDevice.SetVertexBufferData(
buffer,
elementSizeInBytes,
offsetInBytes,
data,
handle.AddrOfPinnedObject(),
startIndex,
elementCount,
elementSizeInBytes,
options
);
handle.Free();
}
#endregion

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.10701s using 13 queries.