From 0cd2827a9fca4be7a0dab82c2fd4aae1a8bbb390 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 2 Dec 2024 21:01:04 +0100 Subject: [PATCH] Update all drawable code to avoid unsafe contexts. --- Axolotl2D.Cef/CefBrowser.cs | 7 ++--- Axolotl2D/Drawable/BaseDrawable.cs | 16 +++++------ Axolotl2D/Drawable/SimpleQuad.cs | 45 ++---------------------------- Axolotl2D/Drawable/Sprite.cs | 11 ++++---- 4 files changed, 17 insertions(+), 62 deletions(-) diff --git a/Axolotl2D.Cef/CefBrowser.cs b/Axolotl2D.Cef/CefBrowser.cs index 83b941f..99e7c72 100644 --- a/Axolotl2D.Cef/CefBrowser.cs +++ b/Axolotl2D.Cef/CefBrowser.cs @@ -161,7 +161,7 @@ internal override void OnResize(Vector2 size) /// /// Draws the browser to the screen. /// - public unsafe override void Draw() + public override void Draw() { base.Draw(); @@ -239,7 +239,7 @@ internal override void CalculateVertices() base.vertices[19] = 1.0f; } - internal unsafe override void UpdateTexture() + internal override void UpdateTexture() { if (browserViewDirty) { @@ -251,8 +251,7 @@ internal unsafe override void UpdateTexture() openGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); openGL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); - fixed (byte* ptr = browserFrameBuffer) - openGL.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, (uint)renderedFrameSize.X, (uint)renderedFrameSize.Y, 0, PixelFormat.Bgra, PixelType.UnsignedByte, ptr); + openGL.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, (uint)renderedFrameSize.X, (uint)renderedFrameSize.Y, 0, PixelFormat.Bgra, PixelType.UnsignedByte, ref MemoryMarshal.GetReference(browserFrameBuffer.AsSpan())); int location = openGL.GetUniformLocation(game._shaderProgram, "uTexture"); openGL.Uniform1(location, 0); diff --git a/Axolotl2D/Drawable/BaseDrawable.cs b/Axolotl2D/Drawable/BaseDrawable.cs index 933f2cd..4c5207e 100644 --- a/Axolotl2D/Drawable/BaseDrawable.cs +++ b/Axolotl2D/Drawable/BaseDrawable.cs @@ -1,5 +1,6 @@ using Silk.NET.OpenGL; using System.Numerics; +using System.Runtime.InteropServices; namespace Axolotl2D.Drawable { @@ -87,7 +88,7 @@ public float Rotation internal readonly GL openGL; - internal unsafe BaseDrawable(Game game) + internal BaseDrawable(Game game) { this.game = game; openGL = game._openGL!; @@ -101,16 +102,14 @@ internal unsafe BaseDrawable(Game game) openGL.BindBuffer(BufferTargetARB.ArrayBuffer, vboPointer); // fix vertices and buffer data - fixed (void* vertices = this.vertices) - openGL.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(this.vertices.Length * sizeof(float)), vertices, BufferUsageARB.StaticDraw); + openGL.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(vertices.Length * sizeof(float)), ref MemoryMarshal.GetReference(vertices.AsSpan()), BufferUsageARB.StaticDraw); // Create an EBO. eboPointer = openGL.GenBuffer(); openGL.BindBuffer(BufferTargetARB.ElementArrayBuffer, eboPointer); // fix indices and buffer data - fixed (void* indices = this.indices) - openGL.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(this.indices.Length * sizeof(uint)), indices, BufferUsageARB.StaticDraw); + openGL.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), ref MemoryMarshal.GetReference(indices.AsSpan()), BufferUsageARB.StaticDraw); // Set up vertex attributes. const uint positionLocation = 0; @@ -142,20 +141,19 @@ internal unsafe BaseDrawable(Game game) /// /// Draws the drawable object with the currently defined position and size. /// - public unsafe virtual void Draw() + public virtual void Draw() { UpdateTexture(); openGL.BindBuffer(BufferTargetARB.ArrayBuffer, vboPointer); // fix vertices and buffer data - fixed (void* vertices = this.vertices) - openGL.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(this.vertices.Length * sizeof(float)), vertices, BufferUsageARB.StaticDraw); + openGL.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(vertices.Length * sizeof(float)), ref MemoryMarshal.GetReference(vertices.AsSpan()), BufferUsageARB.StaticDraw); openGL.BindBuffer(BufferTargetARB.ArrayBuffer, 0); openGL.BindVertexArray(vaoPointer); openGL.BindTexture(TextureTarget.Texture2D, texturePointer); - openGL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, (void*)0); + openGL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, ref MemoryMarshal.GetReference(Span.Empty)); openGL.BindVertexArray(0); } diff --git a/Axolotl2D/Drawable/SimpleQuad.cs b/Axolotl2D/Drawable/SimpleQuad.cs index 46ac2cb..4b2d8f9 100644 --- a/Axolotl2D/Drawable/SimpleQuad.cs +++ b/Axolotl2D/Drawable/SimpleQuad.cs @@ -9,55 +9,14 @@ namespace Axolotl2D.Drawable /// public class SimpleQuad : BaseDrawable { - private readonly uint _vbo; - private readonly uint _ebo; - private readonly uint _vao; - - private readonly GL _gl; - /// /// Initialize a new SimpleQuad object. /// /// Game to initialize on /// Position to initialize at /// Size to initialize at - public unsafe SimpleQuad(Game game, Vector2 position, Vector2 size) : base(game) - { - _gl = game._openGL!; - - // Create a VAO. - _vao = _gl.GenVertexArray(); - _gl.BindVertexArray(_vao); - - // Create a VBO. - _vbo = _gl.GenBuffer(); - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _vbo); - - // fix vertices and buffer data - fixed (void* vertices = base.vertices) - _gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(base.vertices.Length * sizeof(float)), vertices, BufferUsageARB.StaticDraw); - - // Create an EBO. - _ebo = _gl.GenBuffer(); - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _ebo); - - fixed (void* indices = base.indices) - _gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(base.indices.Length * sizeof(uint)), indices, BufferUsageARB.StaticDraw); - - const uint positionLocation = 0; - _gl.EnableVertexAttribArray(positionLocation); - _gl.VertexAttribPointer(positionLocation, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0); - - _gl.BindVertexArray(0); - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0); - } + public SimpleQuad(Game game, Vector2 position, Vector2 size) : base(game) { } - internal unsafe override void UpdateTexture() - { - const uint texCoordLoc = 1; - _gl.EnableVertexAttribArray(texCoordLoc); - _gl.VertexAttribPointer(texCoordLoc, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), (void*)(3 * sizeof(float))); - } + internal override void UpdateTexture() { } } } diff --git a/Axolotl2D/Drawable/Sprite.cs b/Axolotl2D/Drawable/Sprite.cs index c9989a8..e3481d5 100644 --- a/Axolotl2D/Drawable/Sprite.cs +++ b/Axolotl2D/Drawable/Sprite.cs @@ -12,7 +12,7 @@ public class Sprite : BaseDrawable { private readonly Stream imageFile; - internal unsafe Sprite(Game game, Stream imageFile) : base(game) + internal Sprite(Game game, Stream imageFile) : base(game) { this.imageFile = imageFile; } @@ -22,7 +22,7 @@ internal unsafe Sprite(Game game, Stream imageFile) : base(game) private int wrapMode = (int)TextureWrapMode.Repeat; private int minFilter = (int)TextureMinFilter.Nearest; private int magFilter = (int)TextureMagFilter.Nearest; - internal unsafe override void UpdateTexture() + internal override void UpdateTexture() { if (textureLoaded) // Ensure we don't load the texture on every tick. return; @@ -31,10 +31,9 @@ internal unsafe override void UpdateTexture() ImageResult img = ImageResult.FromStream(imageFile, ColorComponents.RedGreenBlueAlpha); - fixed (byte* ptr = img.Data) - // Here we use "result.Width" and "result.Height" to tell OpenGL about how big our texture is. - openGL.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, (uint)img.Width, - (uint)img.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, ptr); + ReadOnlySpan imgDataSpan = img.Data.AsSpan(); + openGL.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, (uint)img.Width, + (uint)img.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, imgDataSpan); openGL.TexParameterI(GLEnum.Texture2D, GLEnum.TextureWrapS, ref wrapMode); openGL.TexParameterI(GLEnum.Texture2D, GLEnum.TextureWrapT, ref wrapMode);