Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Display.cs
浏览该文件的文档.
1#if DIRECT3D11
2using SharpDX;
3using SharpDX.Direct3D11;
4using SharpDX.DXGI;
5#else
6using Silk.NET.OpenGLES;
7using System.Runtime.InteropServices;
8#endif
9
10namespace Engine.Graphics {
11 public static class Display {
13
14 static RasterizerState m_rasterizerState = RasterizerState.CullCounterClockwise;
15
17
19
20 static bool m_useReducedZRange;
21
22 public static Point2 BackbufferSize { get; private set; }
23
24 public static Viewport Viewport { get; set; }
25
26 public static Rectangle ScissorRectangle { get; set; }
27
29 get => m_rasterizerState;
30 set {
31 ArgumentNullException.ThrowIfNull(value);
32 m_rasterizerState = value;
33 value.IsLocked = true;
34 }
35 }
36
39 set {
40 ArgumentNullException.ThrowIfNull(value);
41 m_depthStencilState = value;
42 value.IsLocked = true;
43 }
44 }
45
46 public static BlendState BlendState {
47 get => m_blendState;
48 set {
49 ArgumentNullException.ThrowIfNull(value);
50 m_blendState = value;
51 value.IsLocked = true;
52 }
53 }
54
56 get => m_renderTarget;
57 set {
58 m_renderTarget = value;
59 if (value != null) {
60 Viewport = new Viewport(0, 0, value.Width, value.Height);
61 ScissorRectangle = new Rectangle(0, 0, value.Width, value.Height);
62 }
63 else {
66 }
67 }
68 }
69
70 public static bool UseReducedZRange {
71 get => m_useReducedZRange;
72 set {
73 if (value != m_useReducedZRange) {
74 m_useReducedZRange = value;
75 foreach (GraphicsResource resource in GraphicsResource.m_resources) {
76 (resource as Shader)?.CompileShaders();
77 }
78 }
79 }
80 }
81
82 public static string DeviceDescription { get; set; }
83
84 public static int MaxTextureSize {
85 get {
86#if DIRECT3D11
88#else
90#endif
91 }
92 }
93
94 public static event Action DeviceLost;
95
96 public static event Action DeviceReset;
97
98 public static void DrawUser<T>(PrimitiveType primitiveType,
99 Shader shader,
100 VertexDeclaration vertexDeclaration,
101 T[] vertexData,
102 int startVertex,
103 int verticesCount) where T : unmanaged {
104 VerifyParametersDrawUser(primitiveType, shader, vertexDeclaration, vertexData, startVertex, verticesCount);
105#if DIRECT3D11
106 int num = DXWrapper.AppendUserVertices(vertexData, vertexDeclaration.VertexStride, startVertex, verticesCount);
111 DXWrapper.ApplyShaderAndRenderTarget(RenderTarget, shader, vertexDeclaration);
112 DXWrapper.ApplyVertexBuffer(DXWrapper.UserVertexBuffer, vertexDeclaration.VertexStride, num);
113 DXWrapper.ApplyIndexBuffer(null, Format.R32_UInt, 0);
114 DXWrapper.ApplyPrimitiveType(primitiveType);
115 DXWrapper.Context.Draw(verticesCount, 0);
116#else
117 GCHandle gCHandle = GCHandle.Alloc(vertexData, GCHandleType.Pinned);
118 try {
122 shader,
123 vertexDeclaration,
124 gCHandle.AddrOfPinnedObject() + startVertex * vertexDeclaration.VertexStride,
125 0,
126 null
127 );
131 GLWrapper.GL.DrawArrays(GLWrapper.TranslatePrimitiveType(primitiveType), startVertex, (uint)verticesCount);
132 }
133 finally {
134 gCHandle.Free();
135 }
136#endif
137 }
138
139 public static void DrawUserIndexed<T>(PrimitiveType primitiveType,
140 Shader shader,
141 VertexDeclaration vertexDeclaration,
142 T[] vertexData,
143 int startVertex,
144 int verticesCount,
145 int[] indexData,
146 int startIndex,
147 int indicesCount) where T : unmanaged {
148 VerifyParametersDrawUserIndexed(
149 primitiveType,
150 shader,
151 vertexDeclaration,
152 vertexData,
153 startVertex,
154 verticesCount,
155 indexData,
156 startIndex,
157 indicesCount
158 );
159#if DIRECT3D11
160 int num = DXWrapper.AppendUserVertices(vertexData, vertexDeclaration.VertexStride, startVertex, verticesCount);
161 int num2 = DXWrapper.AppendUserIndices(indexData, 4, startIndex, indicesCount);
166 DXWrapper.ApplyShaderAndRenderTarget(RenderTarget, shader, vertexDeclaration);
167 DXWrapper.ApplyVertexBuffer(DXWrapper.UserVertexBuffer, vertexDeclaration.VertexStride, num);
168 DXWrapper.ApplyIndexBuffer(DXWrapper.UserIndexBuffer, Format.R32_UInt, num2);
169 DXWrapper.ApplyPrimitiveType(primitiveType);
170 DXWrapper.Context.DrawIndexed(indicesCount, 0, 0);
171#else
172 unsafe {
173 GCHandle gCHandle = GCHandle.Alloc(vertexData, GCHandleType.Pinned);
174 GCHandle gCHandle2 = GCHandle.Alloc(indexData, GCHandleType.Pinned);
175 try {
178 GLWrapper.ApplyShaderAndBuffers(shader, vertexDeclaration, gCHandle.AddrOfPinnedObject(), 0, 0);
182 GLWrapper.GL.DrawElements(
183 GLWrapper.TranslatePrimitiveType(primitiveType),
184 (uint)indicesCount,
185 DrawElementsType.UnsignedInt,
186 (gCHandle2.AddrOfPinnedObject() + 4 * startIndex).ToPointer()
187 );
188 }
189 finally {
190 gCHandle.Free();
191 gCHandle2.Free();
192 }
193 }
194#endif
195 }
196
197 public static void Draw(PrimitiveType primitiveType, Shader shader, VertexBuffer vertexBuffer, int startVertex, int verticesCount) {
198 VerifyParametersDraw(primitiveType, shader, vertexBuffer, startVertex, verticesCount);
199#if DIRECT3D11
206 DXWrapper.ApplyIndexBuffer(null, Format.R32_UInt, 0);
207 DXWrapper.ApplyPrimitiveType(primitiveType);
208 DXWrapper.Context.Draw(verticesCount, startVertex);
209#else
212 GLWrapper.ApplyShaderAndBuffers(shader, vertexBuffer.VertexDeclaration, IntPtr.Zero, vertexBuffer.m_buffer, null);
216 GLWrapper.GL.DrawArrays(GLWrapper.TranslatePrimitiveType(primitiveType), startVertex, (uint)verticesCount);
217#endif
218 }
219
220 public static void DrawIndexed(PrimitiveType primitiveType,
221 Shader shader,
222 VertexBuffer vertexBuffer,
223 IndexBuffer indexBuffer,
224 int startIndex,
225 int indicesCount) {
226 VerifyParametersDrawIndexed(primitiveType, shader, vertexBuffer, indexBuffer, startIndex, indicesCount);
227#if DIRECT3D11
235 DXWrapper.ApplyPrimitiveType(primitiveType);
236 DXWrapper.Context.DrawIndexed(indicesCount, startIndex, 0);
237#else
238 unsafe {
241 GLWrapper.ApplyShaderAndBuffers(shader, vertexBuffer.VertexDeclaration, IntPtr.Zero, vertexBuffer.m_buffer, indexBuffer.m_buffer);
245 GLWrapper.GL.DrawElements(
246 GLWrapper.TranslatePrimitiveType(primitiveType),
247 (uint)indicesCount,
249 new IntPtr(startIndex * indexBuffer.IndexFormat.GetSize()).ToPointer()
250 );
251 }
252#endif
253 }
254
255 public static void Clear(Vector4? color, float? depth = null, int? stencil = null) {
256#if DIRECT3D11
257 if (color != null) {
258 if (RenderTarget != null
259 && RenderTarget.m_colorTextureView != null) {
260 DXWrapper.Context.ClearRenderTargetView(
261 RenderTarget.m_colorTextureView,
262 new Color4(color.Value.X, color.Value.Y, color.Value.Z, color.Value.W)
263 );
264 }
265 else if (DXWrapper.ColorBufferView != null) {
266 DXWrapper.Context.ClearRenderTargetView(
268 new Color4(color.Value.X, color.Value.Y, color.Value.Z, color.Value.W)
269 );
270 }
271 }
272 if (depth != null
273 || stencil != null) {
274 float num = 0f;
275 byte b = 0;
276 DepthStencilClearFlags depthStencilClearFlags = 0;
277 if (depth != null) {
278 depthStencilClearFlags |= DepthStencilClearFlags.Depth;
279 num = depth.Value;
280 }
281 if (stencil != null) {
282 depthStencilClearFlags |= DepthStencilClearFlags.Stencil;
283 b = (byte)stencil.Value;
284 }
285 if (RenderTarget != null
286 && RenderTarget.m_depthTextureView != null) {
287 DXWrapper.Context.ClearDepthStencilView(RenderTarget.m_depthTextureView, depthStencilClearFlags, num, b);
288 return;
289 }
290 if (DXWrapper.DepthBufferView != null) {
291 DXWrapper.Context.ClearDepthStencilView(DXWrapper.DepthBufferView, depthStencilClearFlags, num, b);
292 }
293 }
294#else
295 GLWrapper.Clear(RenderTarget, color, depth, stencil);
296#endif
297 }
298
299 public static void ResetGLStateCache() {
300#if !DIRECT3D11
302#endif
303 }
304
305 public static void Initialize() {
306#if DIRECT3D11
308#else
311#endif
312 Resize();
313 }
314
315 public static void Dispose() {
316#if DIRECT3D11
318#endif
319 }
320
321 public static void BeforeFrame() { }
322
323 public static void AfterFrame() { }
324
325 public static void Resize() {
326 Point2 size = Window.Size;
327 BackbufferSize = new Point2(size.X, size.Y);
328 Viewport = new Viewport(0, 0, size.X, size.Y);
329 ScissorRectangle = new Rectangle(0, 0, size.X, size.Y);
330#if DIRECT3D11
332#endif
333 }
334
335 public static long GetGpuMemoryUsage() {
336 long num = 8 * BackbufferSize.X * BackbufferSize.Y;
337 foreach (GraphicsResource resource in GraphicsResource.m_resources) {
338 num += resource.GetGpuMemoryUsage();
339 }
340 return num;
341 }
342
343 public static void Clear(Color? color, float? depth = null, int? stencil = null) {
344 Clear(color.HasValue ? new Vector4?(new Vector4(color.Value)) : null, depth, stencil);
345 }
346
347 public static void VerifyParametersDrawUser<T>(PrimitiveType primitiveType,
348 Shader shader,
349 VertexDeclaration vertexDeclaration,
350 T[] vertexData,
351 int startVertex,
352 int verticesCount) where T : unmanaged {
353 int num = Utilities.SizeOf<T>();
354 ArgumentNullException.ThrowIfNull(shader);
355 ArgumentNullException.ThrowIfNull(vertexDeclaration);
356 ArgumentNullException.ThrowIfNull(vertexData);
357 if (vertexDeclaration.VertexStride / num * num != vertexDeclaration.VertexStride) {
358 throw new InvalidOperationException(
359 $"Vertex is not an integer multiple of array element, vertex stride is {vertexDeclaration.VertexStride}, array element is {num}."
360 );
361 }
362 if (startVertex < 0
363 || verticesCount < 0
364 || startVertex + verticesCount > vertexData.Length) {
365 throw new ArgumentException("Vertices range is out of bounds.");
366 }
367 shader.VerifyNotDisposed();
368 }
369
370 public static void VerifyParametersDrawUserIndexed<T>(PrimitiveType primitiveType,
371 Shader shader,
372 VertexDeclaration vertexDeclaration,
373 T[] vertexData,
374 int startVertex,
375 int verticesCount,
376 int[] indexData,
377 int startIndex,
378 int indicesCount) where T : unmanaged {
379 int num = Utilities.SizeOf<T>();
380 ArgumentNullException.ThrowIfNull(shader);
381 ArgumentNullException.ThrowIfNull(vertexDeclaration);
382 ArgumentNullException.ThrowIfNull(vertexData);
383 ArgumentNullException.ThrowIfNull(indexData);
384 if (vertexDeclaration.VertexStride / num * num != vertexDeclaration.VertexStride) {
385 throw new InvalidOperationException(
386 $"Vertex is not an integer multiple of array element, vertex stride is {vertexDeclaration.VertexStride}, array element is {num}."
387 );
388 }
389 if (startVertex < 0
390 || verticesCount < 0
391 || startVertex + verticesCount > vertexData.Length) {
392 throw new ArgumentException("Vertices range is out of bounds.");
393 }
394 if (startIndex < 0
395 || indicesCount < 0
396 || startIndex + indicesCount > indexData.Length) {
397 throw new ArgumentException("Indices range is out of bounds.");
398 }
399 shader.VerifyNotDisposed();
400 }
401
402 public static void VerifyParametersDraw(PrimitiveType primitiveType,
403 Shader shader,
404 VertexBuffer vertexBuffer,
405 int startVertex,
406 int verticesCount) {
407 vertexBuffer.VerifyNotDisposed();
408 ArgumentNullException.ThrowIfNull(shader);
409 ArgumentNullException.ThrowIfNull(vertexBuffer);
410 if (startVertex < 0
411 || verticesCount < 0
412 || startVertex + verticesCount > vertexBuffer.VerticesCount) {
413 throw new ArgumentException("Vertices range is out of bounds.");
414 }
415 shader.VerifyNotDisposed();
416 }
417
418 public static void VerifyParametersDrawIndexed(PrimitiveType primitiveType,
419 Shader shader,
420 VertexBuffer vertexBuffer,
421 IndexBuffer indexBuffer,
422 int startIndex,
423 int indicesCount) {
424 ArgumentNullException.ThrowIfNull(shader);
425 ArgumentNullException.ThrowIfNull(vertexBuffer);
426 ArgumentNullException.ThrowIfNull(indexBuffer);
427 if (startIndex < 0
428 || indicesCount < 0
429 || startIndex + indicesCount > indexBuffer.IndicesCount) {
430 throw new ArgumentException("Indices range is out of bounds.");
431 }
432 shader.VerifyNotDisposed();
433 vertexBuffer.VerifyNotDisposed();
434 indexBuffer.VerifyNotDisposed();
435 }
436
437 public static void HandleDeviceLost() {
438 foreach (GraphicsResource resource in GraphicsResource.m_resources) {
439 resource.HandleDeviceLost();
440 }
441 DeviceLost?.Invoke();
442 }
443
444 public static void HandleDeviceReset() {
445 foreach (GraphicsResource resource in GraphicsResource.m_resources) {
446 resource.HandleDeviceReset();
447 }
448 DeviceReset?.Invoke();
449 }
450 }
451}
unsafe
定义 Main.cs:15
static DepthStencilView DepthBufferView
static bool ResizeSwapChainIfNeeded()
static void ApplyRasterizerState(RasterizerState rasterizerState)
static void ApplyVertexBuffer(SharpDX.Direct3D11.Buffer vertexBuffer, int vertexStride, int vertexOffset)
static void ApplyViewportScissor(Viewport viewport, Rectangle scissorRectangle)
static DeviceContext2 Context
static void ApplyBlendState(BlendState blendState)
static void ApplyShaderAndRenderTarget(RenderTarget2D renderTarget, Shader shader, VertexDeclaration vertexDeclaration)
static SharpDX.Direct3D11.Buffer UserIndexBuffer
static int REQ_TEXTURE2D_U_OR_V_DIMENSION
static void ApplyIndexBuffer(SharpDX.Direct3D11.Buffer indexBuffer, Format format, int indexOffset)
static int AppendUserIndices(int[] indices, int indexStride, int startIndex, int indicesCount)
static void ApplyDepthStencilState(DepthStencilState depthStencilState)
static RenderTargetView ColorBufferView
static SharpDX.Direct3D11.Buffer UserVertexBuffer
static Format TranslateIndexFormat(IndexFormat indexFormat)
static void ApplyPrimitiveType(PrimitiveType primitiveType)
static Action DeviceReset
static void Clear(Vector4? color, float? depth=null, int? stencil=null)
static DepthStencilState m_depthStencilState
static BlendState BlendState
static void VerifyParametersDrawUserIndexed< T >(PrimitiveType primitiveType, Shader shader, VertexDeclaration vertexDeclaration, T[] vertexData, int startVertex, int verticesCount, int[] indexData, int startIndex, int indicesCount)
static Rectangle ScissorRectangle
static Viewport Viewport
static void Draw(PrimitiveType primitiveType, Shader shader, VertexBuffer vertexBuffer, int startVertex, int verticesCount)
static RenderTarget2D m_renderTarget
static void VerifyParametersDrawUser< T >(PrimitiveType primitiveType, Shader shader, VertexDeclaration vertexDeclaration, T[] vertexData, int startVertex, int verticesCount)
static BlendState m_blendState
static DepthStencilState DepthStencilState
static bool m_useReducedZRange
static void VerifyParametersDraw(PrimitiveType primitiveType, Shader shader, VertexBuffer vertexBuffer, int startVertex, int verticesCount)
static void Clear(Color? color, float? depth=null, int? stencil=null)
static void HandleDeviceLost()
static void HandleDeviceReset()
static string DeviceDescription
static void DrawUserIndexed< T >(PrimitiveType primitiveType, Shader shader, VertexDeclaration vertexDeclaration, T[] vertexData, int startVertex, int verticesCount, int[] indexData, int startIndex, int indicesCount)
static Point2 BackbufferSize
static void ResetGLStateCache()
static long GetGpuMemoryUsage()
static RasterizerState m_rasterizerState
static void DrawIndexed(PrimitiveType primitiveType, Shader shader, VertexBuffer vertexBuffer, IndexBuffer indexBuffer, int startIndex, int indicesCount)
static RenderTarget2D RenderTarget
static void VerifyParametersDrawIndexed(PrimitiveType primitiveType, Shader shader, VertexBuffer vertexBuffer, IndexBuffer indexBuffer, int startIndex, int indicesCount)
static RasterizerState RasterizerState
static void DrawUser< T >(PrimitiveType primitiveType, Shader shader, VertexDeclaration vertexDeclaration, T[] vertexData, int startVertex, int verticesCount)
static void ApplyDepthStencilState(DepthStencilState state)
static void ApplyRasterizerState(RasterizerState state)
static void Clear(RenderTarget2D renderTarget, Vector4? color, float? depth, int? stencil)
static void ApplyViewportScissor(Viewport viewport, Rectangle scissorRectangle, bool isScissorEnabled)
static void ApplyBlendState(BlendState state)
static Silk.NET.OpenGLES.PrimitiveType TranslatePrimitiveType(PrimitiveType primitiveType)
static unsafe void ApplyShaderAndBuffers(Shader shader, VertexDeclaration vertexDeclaration, IntPtr vertexOffset, int arrayBuffer, int? elementArrayBuffer)
static DrawElementsType TranslateIndexFormat(IndexFormat indexFormat)
static void ApplyRenderTarget(RenderTarget2D renderTarget)
static HashSet< GraphicsResource > m_resources
static Point2 Size