Skip to content

Commit

Permalink
Update all drawable code to avoid unsafe contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Naamloos committed Dec 2, 2024
1 parent 00c356e commit 0cd2827
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 62 deletions.
7 changes: 3 additions & 4 deletions Axolotl2D.Cef/CefBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal override void OnResize(Vector2 size)
/// <summary>
/// Draws the browser to the screen.
/// </summary>
public unsafe override void Draw()
public override void Draw()
{
base.Draw();

Expand Down Expand Up @@ -239,7 +239,7 @@ internal override void CalculateVertices()
base.vertices[19] = 1.0f;
}

internal unsafe override void UpdateTexture()
internal override void UpdateTexture()
{
if (browserViewDirty)
{
Expand All @@ -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);
Expand Down
16 changes: 7 additions & 9 deletions Axolotl2D/Drawable/BaseDrawable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Silk.NET.OpenGL;
using System.Numerics;
using System.Runtime.InteropServices;

namespace Axolotl2D.Drawable
{
Expand Down Expand Up @@ -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!;
Expand All @@ -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;
Expand Down Expand Up @@ -142,20 +141,19 @@ internal unsafe BaseDrawable(Game game)
/// <summary>
/// Draws the drawable object with the currently defined position and size.
/// </summary>
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<uint>.Empty));
openGL.BindVertexArray(0);
}

Expand Down
45 changes: 2 additions & 43 deletions Axolotl2D/Drawable/SimpleQuad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,14 @@ namespace Axolotl2D.Drawable
/// </summary>
public class SimpleQuad : BaseDrawable
{
private readonly uint _vbo;
private readonly uint _ebo;
private readonly uint _vao;

private readonly GL _gl;

/// <summary>
/// Initialize a new SimpleQuad object.
/// </summary>
/// <param name="game">Game to initialize on</param>
/// <param name="position">Position to initialize at</param>
/// <param name="size">Size to initialize at</param>
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() { }
}
}
11 changes: 5 additions & 6 deletions Axolotl2D/Drawable/Sprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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<byte> 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);
Expand Down

0 comments on commit 0cd2827

Please sign in to comment.