This repository has been archived by the owner on May 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSharpBgfx.cs
8453 lines (7201 loc) · 330 KB
/
SharpBgfx.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015-2019 Michael Popoloski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
namespace SharpBgfx {
/// <summary>
/// Provides an interface for programs to respond to callbacks from the bgfx library.
/// </summary>
public interface ICallbackHandler {
/// <summary>
/// Called when an error occurs in the library.
/// </summary>
/// <param name="fileName">The name of the source file in which the message originated.</param>
/// <param name="line">The line number in which the message originated.</param>
/// <param name="errorType">The type of error that occurred.</param>
/// <param name="message">Message string detailing what went wrong.</param>
/// <remarks>
/// If the error type is not <see cref="ErrorType.DebugCheck"/>, bgfx is in an
/// unrecoverable state and the application should terminate.
///
/// This method can be called from any thread.
/// </remarks>
void ReportError (string fileName, int line, ErrorType errorType, string message);
/// <summary>
/// Called to print debug messages.
/// </summary>
/// <param name="fileName">The name of the source file in which the message originated.</param>
/// <param name="line">The line number in which the message originated.</param>
/// <param name="format">The message format string.</param>
/// <param name="args">A pointer to format arguments.</param>
/// <remarks>This method can be called from any thread.</remarks>
void ReportDebug (string fileName, int line, string format, IntPtr args);
/// <summary>
/// Called when a profiling region is entered.
/// </summary>
/// <param name="name">The name of the region.</param>
/// <param name="color">The color of the region.</param>
/// <param name="filePath">The path of the source file containing the region.</param>
/// <param name="line">The line number on which the region was started.</param>
void ProfilerBegin (string name, int color, string filePath, int line);
/// <summary>
/// Called when a profiling region is ended.
/// </summary>
void ProfilerEnd ();
/// <summary>
/// Queries the size of a cache item.
/// </summary>
/// <param name="id">The cache entry ID.</param>
/// <returns>The size of the cache item, or 0 if the item is not found.</returns>
int GetCachedSize (long id);
/// <summary>
/// Retrieves an entry from the cache.
/// </summary>
/// <param name="id">The cache entry ID.</param>
/// <param name="data">A pointer that should be filled with data from the cache.</param>
/// <param name="size">The size of the memory block pointed to be <paramref name="data"/>.</param>
/// <returns><c>true</c> if the item is found in the cache; otherwise, <c>false</c>.</returns>
bool GetCacheEntry (long id, IntPtr data, int size);
/// <summary>
/// Saves an entry in the cache.
/// </summary>
/// <param name="id">The cache entry ID.</param>
/// <param name="data">A pointer to the data to save in the cache.</param>
/// <param name="size">The size of the memory block pointed to be <paramref name="data"/>.</param>
void SetCacheEntry (long id, IntPtr data, int size);
/// <summary>
/// Save a captured screenshot.
/// </summary>
/// <param name="path">The path at which to save the image.</param>
/// <param name="width">The width of the image.</param>
/// <param name="height">The height of the image.</param>
/// <param name="pitch">The number of bytes between lines in the image.</param>
/// <param name="data">A pointer to the image data to save.</param>
/// <param name="size">The size of the image memory.</param>
/// <param name="flipVertical"><c>true</c> if the image origin is bottom left instead of top left; otherwise, <c>false</c>.</param>
void SaveScreenShot (string path, int width, int height, int pitch, IntPtr data, int size, bool flipVertical);
/// <summary>
/// Notifies that a frame capture has begun.
/// </summary>
/// <param name="width">The width of the capture surface.</param>
/// <param name="height">The height of the capture surface.</param>
/// <param name="pitch">The number of bytes between lines in the captured frames.</param>
/// <param name="format">The format of captured frames.</param>
/// <param name="flipVertical"><c>true</c> if the image origin is bottom left instead of top left; otherwise, <c>false</c>.</param>
void CaptureStarted (int width, int height, int pitch, TextureFormat format, bool flipVertical);
/// <summary>
/// Notifies that a frame capture has finished.
/// </summary>
void CaptureFinished ();
/// <summary>
/// Notifies that a frame has been captured.
/// </summary>
/// <param name="data">A pointer to the frame data.</param>
/// <param name="size">The size of the frame data.</param>
void CaptureFrame (IntPtr data, int size);
}
/// <summary>
/// Managed interface to the bgfx graphics library.
/// </summary>
public unsafe static class Bgfx {
/// <summary>
/// Attempts to allocate both a transient vertex buffer and index buffer.
/// </summary>
/// <param name="vertexCount">The number of vertices to allocate.</param>
/// <param name="layout">The layout of each vertex.</param>
/// <param name="indexCount">The number of indices to allocate.</param>
/// <param name="vertexBuffer">Returns the allocated transient vertex buffer.</param>
/// <param name="indexBuffer">Returns the allocated transient index buffer.</param>
/// <returns><c>true</c> if both space requirements are satisfied and the buffers were allocated.</returns>
public static bool AllocateTransientBuffers (int vertexCount, VertexLayout layout, int indexCount, out TransientVertexBuffer vertexBuffer, out TransientIndexBuffer indexBuffer) {
return NativeMethods.bgfx_alloc_transient_buffers(out vertexBuffer, ref layout.data, (ushort)vertexCount, out indexBuffer, (ushort)indexCount);
}
/// <summary>
/// Packs a vector into vertex stream format.
/// </summary>
/// <param name="input">The four element vector to pack.</param>
/// <param name="inputNormalized"><c>true</c> if the input vector is normalized.</param>
/// <param name="attribute">The attribute usage of the vector data.</param>
/// <param name="layout">The layout of the vertex stream.</param>
/// <param name="data">The pointer to the vertex data stream.</param>
/// <param name="index">The index of the vertex within the stream.</param>
public static void VertexPack (float* input, bool inputNormalized, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0) {
NativeMethods.bgfx_vertex_pack(input, inputNormalized, attribute, ref layout.data, data, index);
}
/// <summary>
/// Unpack a vector from a vertex stream.
/// </summary>
/// <param name="output">A pointer to four floats that will receive the unpacked vector.</param>
/// <param name="attribute">The usage of the vertex attribute.</param>
/// <param name="layout">The layout of the vertex stream.</param>
/// <param name="data">A pointer to the vertex data stream.</param>
/// <param name="index">The index of the vertex within the stream.</param>
public static void VertexUnpack (float* output, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0) {
NativeMethods.bgfx_vertex_unpack(output, attribute, ref layout.data, data, index);
}
/// <summary>
/// Converts a stream of vertex data from one format to another.
/// </summary>
/// <param name="destinationLayout">The destination format.</param>
/// <param name="destinationData">A pointer to the output location.</param>
/// <param name="sourceLayout">The source format.</param>
/// <param name="sourceData">A pointer to the source vertex data to convert.</param>
/// <param name="count">The number of vertices to convert.</param>
public static void VertexConvert (VertexLayout destinationLayout, IntPtr destinationData, VertexLayout sourceLayout, IntPtr sourceData, int count = 1) {
NativeMethods.bgfx_vertex_convert(ref destinationLayout.data, destinationData, ref sourceLayout.data, sourceData, count);
}
/// <summary>
/// Welds vertices that are close together.
/// </summary>
/// <param name="layout">The layout of the vertex stream.</param>
/// <param name="data">A pointer to the vertex data stream.</param>
/// <param name="count">The number of vertices in the stream.</param>
/// <param name="remappingTable">An output remapping table from the original vertices to the welded ones.</param>
/// <param name="epsilon">The tolerance for welding vertex positions.</param>
/// <returns>
/// The number of unique vertices after welding.
/// </returns>
public static int WeldVertices (VertexLayout layout, IntPtr data, int count, out int[] remappingTable, float epsilon = 0.001f) {
var output = stackalloc ushort[count];
var result = NativeMethods.bgfx_weld_vertices(output, ref layout.data, data, (ushort)count, epsilon);
remappingTable = new int[count];
for (int i = 0; i < count; i++)
remappingTable[i] = output[i];
return result;
}
/// <summary>
/// Swizzles an RGBA8 image to BGRA8.
/// </summary>
/// <param name="destination">The destination image data.</param>
/// <param name="width">The width of the image.</param>
/// <param name="height">The height of the image.</param>
/// <param name="pitch">The pitch of the image (in bytes).</param>
/// <param name="source">The source image data.</param>
/// <remarks>
/// This method can operate in-place on the image (i.e. src == dst).
/// </remarks>
public static void ImageSwizzleBgra8(IntPtr destination, int width, int height, int pitch, IntPtr source) {
NativeMethods.bgfx_image_swizzle_bgra8(destination, width, height, pitch, source);
}
/// <summary>
/// Downsamples an RGBA8 image with a 2x2 pixel average filter.
/// </summary>
/// <param name="destination">The destination image data.</param>
/// <param name="width">The width of the image.</param>
/// <param name="height">The height of the image.</param>
/// <param name="pitch">The pitch of the image (in bytes).</param>
/// <param name="source">The source image data.</param>
/// <remarks>
/// This method can operate in-place on the image (i.e. src == dst).
/// </remarks>
public static void ImageRgba8Downsample2x2 (IntPtr destination, int width, int height, int pitch, IntPtr source) {
NativeMethods.bgfx_image_rgba8_downsample_2x2(destination, width, height, pitch, source);
}
/// <summary>
/// Sets platform-specific data pointers to hook into low-level library functionality.
/// </summary>
/// <param name="platformData">A collection of platform-specific data pointers.</param>
public static void SetPlatformData (PlatformData platformData) {
NativeMethods.bgfx_set_platform_data(ref platformData);
}
/// <summary>
/// Sets the handle of the main rendering window.
/// </summary>
/// <param name="windowHandle">The handle of the native OS window.</param>
public static void SetWindowHandle (IntPtr windowHandle) {
var data = new PlatformData { WindowHandle = windowHandle };
NativeMethods.bgfx_set_platform_data(ref data);
}
/// <summary>
/// Gets access to underlying API internals for interop scenarios.
/// </summary>
/// <returns>A structure containing API context information.</returns>
public static InternalData GetInternalData () {
unsafe { return *NativeMethods.bgfx_get_internal_data(); }
}
/// <summary>
/// Manually renders a frame. Use this to control the Bgfx render loop.
/// </summary>
/// <param name="timeoutMs">
/// The amount of time to wait, in milliseconds, for the next frame to be rendered.
/// If the timeout is exceeded, the call
/// returns.
/// </param>
/// <returns>The result of the render call.</returns>
/// <remarks>
/// Use this function if you don't want Bgfx to create and maintain a
/// separate render thread. Call this once before <see cref="Bgfx.Init(RendererBackend, Adapter, ICallbackHandler)"/>
/// to avoid having the thread created internally.
/// </remarks>
public static RenderFrameResult ManuallyRenderFrame (int timeoutMs = -1) {
return NativeMethods.bgfx_render_frame(timeoutMs);
}
/// <summary>
/// Gets the currently active rendering backend API.
/// </summary>
/// <returns>The currently active rendering backend.</returns>
public static RendererBackend GetCurrentBackend () {
return NativeMethods.bgfx_get_renderer_type();
}
/// <summary>
/// Closes the library and releases all resources.
/// </summary>
public static void Shutdown () {
NativeMethods.bgfx_shutdown();
CallbackShim.FreeShim();
}
/// <summary>
/// Gets the capabilities of the rendering device.
/// </summary>
/// <returns>Information about the capabilities of the device.</returns>
public static Capabilities GetCaps () {
return new Capabilities(NativeMethods.bgfx_get_caps());
}
/// <summary>
/// Gets frame performance statistics.
/// </summary>
/// <returns>Information about frame performance.</returns>
public static PerfStats GetStats () {
return new PerfStats(NativeMethods.bgfx_get_stats());
}
/// <summary>
/// Resets graphics settings and surfaces.
/// </summary>
/// <param name="width">The width of the main window.</param>
/// <param name="height">The height of the main window.</param>
/// <param name="flags">Flags used to configure rendering output.</param>
public static void Reset (int width, int height, ResetFlags flags = ResetFlags.None) {
Reset(width, height, flags, (TextureFormat)TextureFormatCount);
}
/// <summary>
/// Resets graphics settings and surfaces.
/// </summary>
/// <param name="width">The width of the main window.</param>
/// <param name="height">The height of the main window.</param>
/// <param name="flags">Flags used to configure rendering output.</param>
/// <param name="format">The format of the backbuffer.</param>
public static void Reset (int width, int height, ResetFlags flags, TextureFormat format) {
NativeMethods.bgfx_reset(width, height, flags, format);
}
/// <summary>
/// Advances to the next frame.
/// </summary>
/// <param name="capture">If <c>true</c> the frame is captured for debugging.</param>
/// <returns>The current frame number.</returns>
/// <remarks>
/// When using a multithreaded renderer, this call
/// just swaps internal buffers, kicks render thread, and returns. In a
/// singlethreaded renderer this call does frame rendering.
/// </remarks>
public static int Frame (bool capture = false) {
return NativeMethods.bgfx_frame(capture);
}
/// <summary>
/// Initializes the graphics library on the specified adapter.
/// </summary>
/// <param name="settings">Settings that control initialization, or <c>null</c> to use sane defaults.</param>
/// <returns><c>true</c> if initialization succeeds; otherwise, <c>false</c>.</returns>
public static bool Init (InitSettings settings = null) {
InitSettings.Native native;
NativeMethods.bgfx_init_ctor(&native);
settings = settings ?? new InitSettings();
native.Backend = settings.Backend;
native.VendorId = (ushort)settings.Adapter.Vendor;
native.DeviceId = (ushort)settings.Adapter.DeviceId;
native.Debug = (byte)(settings.Debug ? 1 : 0);
native.Profiling = (byte)(settings.Profiling ? 1 : 0);
native.Resolution.Format = settings.Format;
native.Resolution.Width = (uint)settings.Width;
native.Resolution.Height = (uint)settings.Height;
native.Resolution.Flags = (uint)settings.ResetFlags;
native.Resolution.NumBackBuffers = (byte)settings.BackBufferCount;
native.Resolution.MaxFrameLatency = (byte)settings.MaxFrameLatency;
native.Callbacks = CallbackShim.CreateShim(settings.CallbackHandler ?? new DefaultCallbackHandler());
native.PlatformData = settings.PlatformData;
return NativeMethods.bgfx_init(&native);
}
/// <summary>
/// Gets the set of supported rendering backends.
/// </summary>
/// <returns></returns>
public static RendererBackend[] GetSupportedBackends () {
var types = new RendererBackend[(int)RendererBackend.Default];
var count = NativeMethods.bgfx_get_supported_renderers((byte)types.Length, types);
return types.Take(count).ToArray();
}
/// <summary>
/// Gets the friendly name of a specific rendering backend.
/// </summary>
/// <param name="backend">The backend for which to retrieve a name.</param>
/// <returns>The friendly name of the specified backend.</returns>
public static string GetBackendName (RendererBackend backend) {
return Marshal.PtrToStringAnsi(new IntPtr(NativeMethods.bgfx_get_renderer_name(backend)));
}
/// <summary>
/// Enables debugging features.
/// </summary>
/// <param name="features">The set of debug features to enable.</param>
public static void SetDebugFeatures (DebugFeatures features) {
NativeMethods.bgfx_set_debug(features);
}
/// <summary>
/// Sets a marker that can be used for debugging purposes.
/// </summary>
/// <param name="marker">The user-defined name of the marker.</param>
public static void SetDebugMarker (string marker) {
NativeMethods.bgfx_set_marker(marker);
}
/// <summary>
/// Clears the debug text buffer.
/// </summary>
/// <param name="color">The color with which to clear the background.</param>
/// <param name="smallText"><c>true</c> to use a small font for debug output; <c>false</c> to use normal sized text.</param>
public static void DebugTextClear (DebugColor color = DebugColor.Black, bool smallText = false) {
var attr = (byte)((byte)color << 4);
NativeMethods.bgfx_dbg_text_clear(attr, smallText);
}
/// <summary>
/// Writes debug text to the screen.
/// </summary>
/// <param name="x">The X position, in cells.</param>
/// <param name="y">The Y position, in cells.</param>
/// <param name="foreColor">The foreground color of the text.</param>
/// <param name="backColor">The background color of the text.</param>
/// <param name="format">The format of the message.</param>
/// <param name="args">The arguments with which to format the message.</param>
public static void DebugTextWrite (int x, int y, DebugColor foreColor, DebugColor backColor, string format, params object[] args) {
DebugTextWrite(x, y, foreColor, backColor, string.Format(CultureInfo.CurrentCulture, format, args));
}
/// <summary>
/// Writes debug text to the screen.
/// </summary>
/// <param name="x">The X position, in cells.</param>
/// <param name="y">The Y position, in cells.</param>
/// <param name="foreColor">The foreground color of the text.</param>
/// <param name="backColor">The background color of the text.</param>
/// <param name="message">The message to write.</param>
public static void DebugTextWrite (int x, int y, DebugColor foreColor, DebugColor backColor, string message) {
var attr = (byte)(((byte)backColor << 4) | (byte)foreColor);
NativeMethods.bgfx_dbg_text_printf((ushort)x, (ushort)y, attr, "%s", message);
}
/// <summary>
/// Writes debug text to the screen.
/// </summary>
/// <param name="x">The X position, in cells.</param>
/// <param name="y">The Y position, in cells.</param>
/// <param name="foreColor">The foreground color of the text.</param>
/// <param name="backColor">The background color of the text.</param>
/// <param name="message">The message to write.</param>
public static void DebugTextWrite (int x, int y, DebugColor foreColor, DebugColor backColor, IntPtr message) {
var attr = (byte)(((byte)backColor << 4) | (byte)foreColor);
var format = stackalloc byte[3];
format[0] = (byte)'%';
format[1] = (byte)'s';
format[2] = 0;
NativeMethods.bgfx_dbg_text_printf((ushort)x, (ushort)y, attr, format, message);
}
/// <summary>
/// Draws data directly into the debug text buffer.
/// </summary>
/// <param name="x">The X position, in cells.</param>
/// <param name="y">The Y position, in cells.</param>
/// <param name="width">The width of the image to draw.</param>
/// <param name="height">The height of the image to draw.</param>
/// <param name="data">The image data bytes.</param>
/// <param name="pitch">The pitch of each line in the image data.</param>
public static void DebugTextImage (int x, int y, int width, int height, IntPtr data, int pitch) {
NativeMethods.bgfx_dbg_text_image((ushort)x, (ushort)y, (ushort)width, (ushort)height, data, (ushort)pitch);
}
/// <summary>
/// Draws data directly into the debug text buffer.
/// </summary>
/// <param name="x">The X position, in cells.</param>
/// <param name="y">The Y position, in cells.</param>
/// <param name="width">The width of the image to draw.</param>
/// <param name="height">The height of the image to draw.</param>
/// <param name="data">The image data bytes.</param>
/// <param name="pitch">The pitch of each line in the image data.</param>
public static void DebugTextImage (int x, int y, int width, int height, byte[] data, int pitch) {
fixed (byte* ptr = data)
NativeMethods.bgfx_dbg_text_image((ushort)x, (ushort)y, (ushort)width, (ushort)height, new IntPtr(ptr), (ushort)pitch);
}
/// <summary>
/// Sets the name of a rendering view, for debugging purposes.
/// </summary>
/// <param name="id">The index of the view.</param>
/// <param name="name">The name of the view.</param>
public static void SetViewName (ushort id, string name) {
NativeMethods.bgfx_set_view_name(id, name);
}
/// <summary>
/// Sets the viewport for the given rendering view.
/// </summary>
/// <param name="id">The index of the view.</param>
/// <param name="x">The X coordinate of the viewport.</param>
/// <param name="y">The Y coordinate of the viewport.</param>
/// <param name="width">The width of the viewport, in pixels.</param>
/// <param name="height">The height of the viewport, in pixels.</param>
public static void SetViewRect (ushort id, int x, int y, int width, int height) {
NativeMethods.bgfx_set_view_rect(id, (ushort)x, (ushort)y, (ushort)width, (ushort)height);
}
/// <summary>
/// Sets the viewport for the given rendering view.
/// </summary>
/// <param name="id">The index of the view.</param>
/// <param name="x">The X coordinate of the viewport.</param>
/// <param name="y">The Y coordinate of the viewport.</param>
/// <param name="ratio">The ratio with which to automatically size the viewport.</param>
public static void SetViewRect (ushort id, int x, int y, BackbufferRatio ratio) {
NativeMethods.bgfx_set_view_rect_auto(id, (ushort)x, (ushort)y, ratio);
}
/// <summary>
/// Sets the scissor rectangle for a specific view.
/// </summary>
/// <param name="id">The index of the view.</param>
/// <param name="x">The X coordinate of the scissor rectangle.</param>
/// <param name="y">The Y coordinate of the scissor rectangle.</param>
/// <param name="width">The width of the scissor rectangle.</param>
/// <param name="height">The height of the scissor rectangle.</param>
/// <remarks>
/// Set all values to zero to disable the scissor test.
/// </remarks>
public static void SetViewScissor (ushort id, int x, int y, int width, int height) {
NativeMethods.bgfx_set_view_scissor(id, (ushort)x, (ushort)y, (ushort)width, (ushort)height);
}
/// <summary>
/// Sets view clear flags.
/// </summary>
/// <param name="id">The index of the view.</param>
/// <param name="targets">The target surfaces that should be cleared.</param>
/// <param name="colorRgba">The clear color.</param>
/// <param name="depth">The value to fill the depth buffer.</param>
/// <param name="stencil">The value to fill the stencil buffer.</param>
public static void SetViewClear (ushort id, ClearTargets targets, int colorRgba, float depth = 1.0f, byte stencil = 0) {
NativeMethods.bgfx_set_view_clear(id, targets, colorRgba, depth, stencil);
}
/// <summary>
/// Sets view clear flags for multiple render targets.
/// </summary>
/// <param name="id">The index of the view.</param>
/// <param name="targets">The target surfaces that should be cleared.</param>
/// <param name="depth">The value to fill the depth buffer.</param>
/// <param name="stencil">The value to fill the stencil buffer.</param>
/// <param name="rt0">The color palette index for render target 0.</param>
/// <param name="rt1">The color palette index for render target 1.</param>
/// <param name="rt2">The color palette index for render target 2.</param>
/// <param name="rt3">The color palette index for render target 3.</param>
/// <param name="rt4">The color palette index for render target 4.</param>
/// <param name="rt5">The color palette index for render target 5.</param>
/// <param name="rt6">The color palette index for render target 6.</param>
/// <param name="rt7">The color palette index for render target 7.</param>
public static void SetViewClear (
ushort id,
ClearTargets targets,
float depth,
byte stencil,
byte rt0 = byte.MaxValue,
byte rt1 = byte.MaxValue,
byte rt2 = byte.MaxValue,
byte rt3 = byte.MaxValue,
byte rt4 = byte.MaxValue,
byte rt5 = byte.MaxValue,
byte rt6 = byte.MaxValue,
byte rt7 = byte.MaxValue
) {
NativeMethods.bgfx_set_view_clear_mrt(
id,
targets,
depth,
stencil,
rt0,
rt1,
rt2,
rt3,
rt4,
rt5,
rt6,
rt7
);
}
/// <summary>
/// Sets an entry in the color palette.
/// </summary>
/// <param name="index">The index of the palette entry to set.</param>
/// <param name="color">The color to set.</param>
/// <remarks>
/// The clear color palette is used with SetViewClear for clearing multiple render targets
/// to different color values.
/// </remarks>
public static void SetPaletteColor (byte index, float* color) {
NativeMethods.bgfx_set_palette_color(index, color);
}
/// <summary>
/// Sets the sorting mode to use for the given view.
/// </summary>
/// <param name="id">The index of the view.</param>
/// <param name="mode">The sorting mode to use.</param>
public static void SetViewMode (ushort id, ViewMode mode) {
NativeMethods.bgfx_set_view_mode(id, mode);
}
/// <summary>
/// Sets the view and projection transforms for the given rendering view.
/// </summary>
/// <param name="id">The index of the view.</param>
/// <param name="view">The 4x4 view transform matrix.</param>
/// <param name="projection">The 4x4 projection transform matrix.</param>
public static void SetViewTransform (ushort id, float* view, float* projection) {
NativeMethods.bgfx_set_view_transform(id, view, projection);
}
/// <summary>
/// Sets the frame buffer used by a particular view.
/// </summary>
/// <param name="id">The index of the view.</param>
/// <param name="frameBuffer">The frame buffer to set.</param>
public static void SetViewFrameBuffer (ushort id, FrameBuffer frameBuffer) {
NativeMethods.bgfx_set_view_frame_buffer(id, frameBuffer.handle);
}
/// <summary>
/// Sets the model transform to use for drawing primitives.
/// </summary>
/// <param name="matrix">A pointer to one or more matrices to set.</param>
/// <param name="count">The number of matrices in the array.</param>
/// <returns>An index into the matrix cache to allow reusing the matrix in other calls.</returns>
public static int SetTransform (float* matrix, int count = 1) {
return NativeMethods.bgfx_set_transform(matrix, (ushort)count);
}
/// <summary>
/// Sets a model transform from the cache.
/// </summary>
/// <param name="cacheIndex">The index of the cached matrix.</param>
/// <param name="count">The number of matrices to set from the cache.</param>
public static void SetTransform (int cacheIndex, int count = 1) {
NativeMethods.bgfx_set_transform_cached(cacheIndex, (ushort)count);
}
/// <summary>
/// Sets the scissor rectangle to use for clipping primitives.
/// </summary>
/// <param name="x">The X coordinate of the scissor rectangle.</param>
/// <param name="y">The Y coordinate of the scissor rectangle.</param>
/// <param name="width">The width of the rectangle.</param>
/// <param name="height">The height of the rectangle.</param>
/// <returns>
/// An index into the scissor cache to allow reusing the rectangle in other calls.
/// </returns>
public static int SetScissor (int x, int y, int width, int height) {
return NativeMethods.bgfx_set_scissor((ushort)x, (ushort)y, (ushort)width, (ushort)height);
}
/// <summary>
/// Sets a scissor rectangle from the cache.
/// </summary>
/// <param name="cacheIndex">The index of the cached scissor rectangle, or -1 to unset.</param>
public static void SetScissor (int cacheIndex = -1) {
NativeMethods.bgfx_set_scissor_cached((ushort)cacheIndex);
}
/// <summary>
/// Sets the index buffer to use for drawing primitives.
/// </summary>
/// <param name="indexBuffer">The index buffer to set.</param>
public static void SetIndexBuffer (IndexBuffer indexBuffer) {
NativeMethods.bgfx_set_index_buffer(indexBuffer.handle, 0, -1);
}
/// <summary>
/// Sets the index buffer to use for drawing primitives.
/// </summary>
/// <param name="indexBuffer">The index buffer to set.</param>
/// <param name="firstIndex">The first index in the buffer to use.</param>
/// <param name="count">The number of indices to pull from the buffer.</param>
public static void SetIndexBuffer (IndexBuffer indexBuffer, int firstIndex, int count) {
NativeMethods.bgfx_set_index_buffer(indexBuffer.handle, firstIndex, count);
}
/// <summary>
/// Sets the vertex buffer to use for drawing primitives.
/// </summary>
/// <param name="stream">The index of the vertex stream to set.</param>
/// <param name="vertexBuffer">The vertex buffer to set.</param>
public static void SetVertexBuffer (int stream, VertexBuffer vertexBuffer) {
NativeMethods.bgfx_set_vertex_buffer((byte)stream, vertexBuffer.handle, 0, -1);
}
/// <summary>
/// Sets the vertex buffer to use for drawing primitives.
/// </summary>
/// <param name="stream">The index of the vertex stream to set.</param>
/// <param name="vertexBuffer">The vertex buffer to set.</param>
/// <param name="firstVertex">The index of the first vertex to use.</param>
/// <param name="count">The number of vertices to pull from the buffer.</param>
public static void SetVertexBuffer (int stream, VertexBuffer vertexBuffer, int firstVertex, int count) {
NativeMethods.bgfx_set_vertex_buffer((byte)stream, vertexBuffer.handle, firstVertex, count);
}
/// <summary>
/// Sets the index buffer to use for drawing primitives.
/// </summary>
/// <param name="indexBuffer">The index buffer to set.</param>
public static void SetIndexBuffer (DynamicIndexBuffer indexBuffer) {
NativeMethods.bgfx_set_dynamic_index_buffer(indexBuffer.handle, 0, -1);
}
/// <summary>
/// Sets the index buffer to use for drawing primitives.
/// </summary>
/// <param name="indexBuffer">The index buffer to set.</param>
/// <param name="firstIndex">The first index in the buffer to use.</param>
/// <param name="count">The number of indices to pull from the buffer.</param>
public static void SetIndexBuffer (DynamicIndexBuffer indexBuffer, int firstIndex, int count) {
NativeMethods.bgfx_set_dynamic_index_buffer(indexBuffer.handle, firstIndex, count);
}
/// <summary>
/// Sets the vertex buffer to use for drawing primitives.
/// </summary>
/// <param name="stream">The index of the vertex stream to set.</param>
/// <param name="vertexBuffer">The vertex buffer to set.</param>
public static void SetVertexBuffer (int stream, DynamicVertexBuffer vertexBuffer) {
NativeMethods.bgfx_set_dynamic_vertex_buffer((byte)stream, vertexBuffer.handle, 0, -1);
}
/// <summary>
/// Sets the vertex buffer to use for drawing primitives.
/// </summary>
/// <param name="stream">The index of the vertex stream to set.</param>
/// <param name="vertexBuffer">The vertex buffer to set.</param>
/// <param name="startVertex">The index of the first vertex to use.</param>
/// <param name="count">The number of vertices to pull from the buffer.</param>
public static void SetVertexBuffer (int stream, DynamicVertexBuffer vertexBuffer, int startVertex, int count) {
NativeMethods.bgfx_set_dynamic_vertex_buffer((byte)stream, vertexBuffer.handle, startVertex, count);
}
/// <summary>
/// Sets the index buffer to use for drawing primitives.
/// </summary>
/// <param name="indexBuffer">The index buffer to set.</param>
public static void SetIndexBuffer (TransientIndexBuffer indexBuffer) {
NativeMethods.bgfx_set_transient_index_buffer(ref indexBuffer, 0, -1);
}
/// <summary>
/// Sets the index buffer to use for drawing primitives.
/// </summary>
/// <param name="indexBuffer">The index buffer to set.</param>
/// <param name="firstIndex">The first index in the buffer to use.</param>
/// <param name="count">The number of indices to pull from the buffer.</param>
public static void SetIndexBuffer (TransientIndexBuffer indexBuffer, int firstIndex, int count) {
NativeMethods.bgfx_set_transient_index_buffer(ref indexBuffer, firstIndex, count);
}
/// <summary>
/// Sets the vertex buffer to use for drawing primitives.
/// </summary>
/// <param name="stream">The index of the vertex stream to set.</param>
/// <param name="vertexBuffer">The vertex buffer to set.</param>
public static void SetVertexBuffer (int stream, TransientVertexBuffer vertexBuffer) {
NativeMethods.bgfx_set_transient_vertex_buffer((byte)stream, ref vertexBuffer, 0, -1);
}
/// <summary>
/// Sets the vertex buffer to use for drawing primitives.
/// </summary>
/// <param name="stream">The index of the vertex stream to set.</param>
/// <param name="vertexBuffer">The vertex buffer to set.</param>
/// <param name="firstVertex">The index of the first vertex to use.</param>
/// <param name="count">The number of vertices to pull from the buffer.</param>
public static void SetVertexBuffer (int stream, TransientVertexBuffer vertexBuffer, int firstVertex, int count) {
NativeMethods.bgfx_set_transient_vertex_buffer((byte)stream, ref vertexBuffer, firstVertex, count);
}
/// <summary>
/// Sets the number of auto-generated vertices for use with gl_VertexID.
/// </summary>
/// <param name="count">The number of auto-generated vertices.</param>
public static void SetVertexCount(int count) {
NativeMethods.bgfx_set_vertex_count(count);
}
/// <summary>
/// Sets the number of auto-generated indices for use with gl_InstanceID.
/// </summary>
/// <param name="count">The number of auto-generated instances.</param>
public static void SetInstanceCount (int count) {
NativeMethods.bgfx_set_instance_count(count);
}
/// <summary>
/// Sets instance data to use for drawing primitives.
/// </summary>
/// <param name="instanceData">The instance data.</param>
/// <param name="start">The starting offset in the buffer.</param>
/// <param name="count">The number of entries to pull from the buffer.</param>
public static void SetInstanceDataBuffer (ref InstanceDataBuffer instanceData, int start = 0, int count = -1) {
NativeMethods.bgfx_set_instance_data_buffer(ref instanceData.data, (uint)start, (uint)count);
}
/// <summary>
/// Sets instance data to use for drawing primitives.
/// </summary>
/// <param name="vertexBuffer">The vertex buffer containing instance data.</param>
/// <param name="firstVertex">The index of the first vertex to use.</param>
/// <param name="count">The number of vertices to pull from the buffer.</param>
public static void SetInstanceDataBuffer (VertexBuffer vertexBuffer, int firstVertex, int count) {
NativeMethods.bgfx_set_instance_data_from_vertex_buffer(vertexBuffer.handle, firstVertex, count);
}
/// <summary>
/// Sets instance data to use for drawing primitives.
/// </summary>
/// <param name="vertexBuffer">The vertex buffer containing instance data.</param>
/// <param name="firstVertex">The index of the first vertex to use.</param>
/// <param name="count">The number of vertices to pull from the buffer.</param>
public static void SetInstanceDataBuffer (DynamicVertexBuffer vertexBuffer, int firstVertex, int count) {
NativeMethods.bgfx_set_instance_data_from_dynamic_vertex_buffer(vertexBuffer.handle, firstVertex, count);
}
/// <summary>
/// Sets the value of a uniform parameter.
/// </summary>
/// <param name="uniform">The uniform to set.</param>
/// <param name="value">A pointer to the uniform's data.</param>
/// <param name="arraySize">The size of the data array, if the uniform is an array.</param>
public static void SetUniform (Uniform uniform, float value, int arraySize = 1) {
NativeMethods.bgfx_set_uniform(uniform.handle, &value, (ushort)arraySize);
}
/// <summary>
/// Sets the value of a uniform parameter.
/// </summary>
/// <param name="uniform">The uniform to set.</param>
/// <param name="value">A pointer to the uniform's data.</param>
/// <param name="arraySize">The size of the data array, if the uniform is an array.</param>
public static void SetUniform (Uniform uniform, void* value, int arraySize = 1) {
NativeMethods.bgfx_set_uniform(uniform.handle, value, (ushort)arraySize);
}
/// <summary>
/// Sets the value of a uniform parameter.
/// </summary>
/// <param name="uniform">The uniform to set.</param>
/// <param name="value">A pointer to the uniform's data.</param>
/// <param name="arraySize">The size of the data array, if the uniform is an array.</param>
public static void SetUniform (Uniform uniform, IntPtr value, int arraySize = 1) {
NativeMethods.bgfx_set_uniform(uniform.handle, value.ToPointer(), (ushort)arraySize);
}
/// <summary>
/// Sets a texture to use for drawing primitives.
/// </summary>
/// <param name="textureUnit">The texture unit to set.</param>
/// <param name="sampler">The sampler uniform.</param>
/// <param name="texture">The texture to set.</param>
public static void SetTexture (byte textureUnit, Uniform sampler, Texture texture) {
NativeMethods.bgfx_set_texture(textureUnit, sampler.handle, texture.handle, uint.MaxValue);
}
/// <summary>
/// Sets a texture to use for drawing primitives.
/// </summary>
/// <param name="textureUnit">The texture unit to set.</param>
/// <param name="sampler">The sampler uniform.</param>
/// <param name="texture">The texture to set.</param>
/// <param name="flags">Sampling flags that override the default flags in the texture itself.</param>
public static void SetTexture (byte textureUnit, Uniform sampler, Texture texture, TextureFlags flags) {
NativeMethods.bgfx_set_texture(textureUnit, sampler.handle, texture.handle, (uint)flags);
}
/// <summary>
/// Sets a texture mip as a compute image.
/// </summary>
/// <param name="stage">The buffer stage to set.</param>
/// <param name="texture">The texture to set.</param>
/// <param name="mip">The index of the mip level within the texture to set.</param>
/// <param name="format">The format of the buffer data.</param>
/// <param name="access">Access control flags.</param>
public static void SetComputeImage (byte stage, Texture texture, byte mip, ComputeBufferAccess access, TextureFormat format = TextureFormat.Unknown) {
NativeMethods.bgfx_set_image(stage, texture.handle, mip, format, access);
}
/// <summary>
/// Sets an index buffer as a compute resource.
/// </summary>
/// <param name="stage">The resource stage to set.</param>
/// <param name="buffer">The buffer to set.</param>
/// <param name="access">Access control flags.</param>
public static void SetComputeBuffer (byte stage, IndexBuffer buffer, ComputeBufferAccess access) {
NativeMethods.bgfx_set_compute_index_buffer(stage, buffer.handle, access);
}
/// <summary>
/// Sets a verterx buffer as a compute resource.
/// </summary>
/// <param name="stage">The resource stage to set.</param>
/// <param name="buffer">The buffer to set.</param>
/// <param name="access">Access control flags.</param>
public static void SetComputeBuffer (byte stage, VertexBuffer buffer, ComputeBufferAccess access) {
NativeMethods.bgfx_set_compute_vertex_buffer(stage, buffer.handle, access);
}
/// <summary>
/// Sets a dynamic index buffer as a compute resource.
/// </summary>
/// <param name="stage">The resource stage to set.</param>
/// <param name="buffer">The buffer to set.</param>
/// <param name="access">Access control flags.</param>
public static void SetComputeBuffer (byte stage, DynamicIndexBuffer buffer, ComputeBufferAccess access) {
NativeMethods.bgfx_set_compute_dynamic_index_buffer(stage, buffer.handle, access);
}
/// <summary>
/// Sets a dynamic vertex buffer as a compute resource.
/// </summary>
/// <param name="stage">The resource stage to set.</param>
/// <param name="buffer">The buffer to set.</param>
/// <param name="access">Access control flags.</param>
public static void SetComputeBuffer (byte stage, DynamicVertexBuffer buffer, ComputeBufferAccess access) {
NativeMethods.bgfx_set_compute_dynamic_vertex_buffer(stage, buffer.handle, access);
}
/// <summary>
/// Sets an indirect buffer as a compute resource.
/// </summary>
/// <param name="stage">The resource stage to set.</param>
/// <param name="buffer">The buffer to set.</param>
/// <param name="access">Access control flags.</param>
public static void SetComputeBuffer (byte stage, IndirectBuffer buffer, ComputeBufferAccess access) {
NativeMethods.bgfx_set_compute_indirect_buffer(stage, buffer.handle, access);
}
/// <summary>
/// Marks a view as "touched", ensuring that its background is cleared even if nothing is rendered.
/// </summary>
/// <param name="id">The index of the view to touch.</param>
/// <returns>The number of draw calls.</returns>
public static int Touch (ushort id) {
return NativeMethods.bgfx_touch(id);
}
/// <summary>
/// Resets all view settings to default.
/// </summary>
/// <param name="id">The index of the view to reset.</param>
public static void ResetView (ushort id) {
NativeMethods.bgfx_reset_view(id);
}
/// <summary>
/// Submits the current batch of primitives for rendering.
/// </summary>
/// <param name="id">The index of the view to submit.</param>
/// <param name="program">The program with which to render.</param>
/// <param name="depth">A depth value to use for sorting the batch.</param>
/// <param name="preserveState"><c>true</c> to preserve internal draw state after the call.</param>
/// <returns>The number of draw calls.</returns>
public static int Submit (ushort id, Program program, int depth = 0, bool preserveState = false) {
return NativeMethods.bgfx_submit(id, program.handle, depth, preserveState);
}
/// <summary>
/// Submits the current batch of primitives for rendering.
/// </summary>
/// <param name="id">The index of the view to submit.</param>
/// <param name="program">The program with which to render.</param>
/// <param name="query">An occlusion query to use as a predicate during rendering.</param>
/// <param name="depth">A depth value to use for sorting the batch.</param>
/// <param name="preserveState"><c>true</c> to preserve internal draw state after the call.</param>
/// <returns>The number of draw calls.</returns>
public static int Submit (ushort id, Program program, OcclusionQuery query, int depth = 0, bool preserveState = false) {
return NativeMethods.bgfx_submit_occlusion_query(id, program.handle, query.handle, depth, preserveState);
}
/// <summary>
/// Submits an indirect batch of drawing commands to be used for rendering.
/// </summary>
/// <param name="id">The index of the view to submit.</param>
/// <param name="program">The program with which to render.</param>
/// <param name="indirectBuffer">The buffer containing drawing commands.</param>
/// <param name="startIndex">The index of the first command to process.</param>