Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
IndexBuffer.cs
浏览该文件的文档.
1#if DIRECT3D11
2using SharpDX;
3using SharpDX.Direct3D11;
4#else
5using Silk.NET.OpenGLES;
6#endif
7using System.Runtime.InteropServices;
8
9namespace Engine.Graphics {
11#if DIRECT3D11
12 public SharpDX.Direct3D11.Buffer m_buffer;
13#else
14 public int m_buffer;
15#endif
16
17 public string DebugName {
18 get {
19#if DIRECT3D11
20 return m_buffer.DebugName;
21#else
22 return string.Empty;
23#endif
24 }
25 // ReSharper disable ValueParameterNotUsed
26 set
27 // ReSharper restore ValueParameterNotUsed
28 {
29#if DIRECT3D11
30 m_buffer.DebugName = value;
31#endif
32 }
33 }
34
35 public IndexFormat IndexFormat { get; set; }
36
37 public int IndicesCount { get; set; }
38
39 public object Tag { get; set; }
40
41 public IndexBuffer(IndexFormat indexFormat, int indicesCount) {
42 InitializeIndexBuffer(indexFormat, indicesCount);
44 }
45
46 public override void Dispose() {
47 base.Dispose();
49 }
50
51 public void SetData<T>(T[] source, int sourceStartIndex, int sourceCount, int targetStartIndex = 0) where T : unmanaged {
52 VerifyParametersSetData(source, sourceStartIndex, sourceCount, targetStartIndex);
53 GCHandle gCHandle = GCHandle.Alloc(source, GCHandleType.Pinned);
54 try {
55 int num = Utilities.SizeOf<T>();
56 int size = IndexFormat.GetSize();
57#if DIRECT3D11
58 DataBox dataBox = new(gCHandle.AddrOfPinnedObject() + sourceStartIndex * num, 1, 0);
59 ResourceRegion resourceRegion = new(targetStartIndex * size, 0, 0, targetStartIndex * size + sourceCount * num, 1, 1);
60 DXWrapper.Context.UpdateSubresource(dataBox, m_buffer, 0, resourceRegion);
61#else
62 unsafe {
63 GLWrapper.BindBuffer(BufferTargetARB.ElementArrayBuffer, m_buffer);
64 GLWrapper.GL.BufferSubData(
65 BufferTargetARB.ElementArrayBuffer,
66 new IntPtr(targetStartIndex * size),
67 new UIntPtr((uint)(num * sourceCount)),
68 (gCHandle.AddrOfPinnedObject() + sourceStartIndex * num).ToPointer()
69 );
70 }
71#endif
72 }
73 finally {
74 gCHandle.Free();
75 }
76 }
77
78 public override void HandleDeviceLost() {
80 }
81
82 public override void HandleDeviceReset() {
84 }
85
86 public void AllocateBuffer() {
87#if DIRECT3D11
88 m_buffer = new SharpDX.Direct3D11.Buffer(
90 IndexFormat.GetSize() * IndicesCount,
91 ResourceUsage.Default,
92 BindFlags.IndexBuffer,
93 CpuAccessFlags.None,
94 ResourceOptionFlags.None,
95 0
96 );
97#else
98 unsafe {
99 GLWrapper.GL.GenBuffers(1, out uint buffer);
100 m_buffer = (int)buffer;
101 GLWrapper.BindBuffer(BufferTargetARB.ElementArrayBuffer, m_buffer);
102 GLWrapper.GL.BufferData(
103 BufferTargetARB.ElementArrayBuffer,
104 new UIntPtr((uint)(IndexFormat.GetSize() * IndicesCount)),
105 null,
106 BufferUsageARB.StaticDraw
107 );
108 }
109#endif
110 }
111
112 public void DeleteBuffer() {
113#if DIRECT3D11
114 Utilities.Dispose(ref m_buffer);
115#else
116 if (m_buffer != 0) {
117 GLWrapper.DeleteBuffer(BufferTargetARB.ElementArrayBuffer, m_buffer);
118 m_buffer = 0;
119 }
120#endif
121 }
122
123 public override int GetGpuMemoryUsage() => IndicesCount * IndexFormat.GetSize();
124
125 void InitializeIndexBuffer(IndexFormat indexFormat, int indicesCount) {
126 if (indicesCount <= 0) {
127 throw new ArgumentException("Indices count must be greater than 0.");
128 }
129 IndexFormat = indexFormat;
130 IndicesCount = indicesCount;
131 }
132
133 void VerifyParametersSetData<T>(T[] source, int sourceStartIndex, int sourceCount, int targetStartIndex = 0) where T : unmanaged {
135 int num = Utilities.SizeOf<T>();
136 int size = IndexFormat.GetSize();
137 ArgumentNullException.ThrowIfNull(source);
138 if (sourceStartIndex < 0
139 || sourceCount < 0
140 || sourceStartIndex + sourceCount > source.Length) {
141 throw new ArgumentException("Range is out of source bounds.");
142 }
143 if (targetStartIndex < 0
144 || targetStartIndex * size + sourceCount * num > IndicesCount * size) {
145 throw new ArgumentException("Range is out of target bounds.");
146 }
147 }
148 }
149}
unsafe
定义 Main.cs:15
static DeviceContext2 Context
static void DeleteBuffer(BufferTargetARB target, int buffer)
static void BindBuffer(BufferTargetARB target, int buffer)
void VerifyParametersSetData< T >(T[] source, int sourceStartIndex, int sourceCount, int targetStartIndex=0)
void InitializeIndexBuffer(IndexFormat indexFormat, int indicesCount)
void SetData< T >(T[] source, int sourceStartIndex, int sourceCount, int targetStartIndex=0)
IndexBuffer(IndexFormat indexFormat, int indicesCount)