Skip to content

Commit

Permalink
Fix texture disposing. Handle srgb correction
Browse files Browse the repository at this point in the history
  • Loading branch information
KillzXGaming committed Nov 19, 2023
1 parent 967c0cd commit 0897e94
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Fushigi/gl/Bfres/BfresTextureRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class BfresTextureRender : GLTexture

private Task<byte[]> Decoder;

private bool IsSrgb = false;

public BfresTextureRender(GL gl, BntxTexture texture) : base(gl)
{
Load(texture);
Expand All @@ -26,6 +28,7 @@ public BfresTextureRender(GL gl, BntxTexture texture) : base(gl)
public void Load(BntxTexture texture)
{
this.Target = TextureTarget.Texture2D;
this.IsSrgb = texture.IsSrgb;

if (texture.SurfaceDim == SurfaceDim.Dim2DArray)
this.Target = TextureTarget.Texture2DArray;
Expand Down Expand Up @@ -113,11 +116,13 @@ static int GetSwizzle(ChannelType channel)

public void CheckState()
{
if (TextureState == State.Decoded && Decoder.IsCompleted)
if (TextureState == State.Decoded && Decoder.IsCompleted && !this.IsDisposed)
{
Bind();

var formatInfo = GLFormatHelper.ConvertPixelFormat(SurfaceFormat.R8_G8_B8_A8_UNORM);
var format = IsSrgb ? SurfaceFormat.R8_G8_B8_A8_SRGB : SurfaceFormat.R8_G8_B8_A8_UNORM;

var formatInfo = GLFormatHelper.ConvertPixelFormat(format);
GLTextureDataLoader.LoadImage(_gl, this.Target, Width, Height, 0, formatInfo, Decoder.Result, 0);

// var internalFormat = GLFormatHelper.ConvertCompressedFormat(SurfaceFormat.BC7_UNORM, true);
Expand Down
49 changes: 49 additions & 0 deletions Fushigi/gl/HDRScreenBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Fushigi.gl.Shaders;
using Silk.NET.OpenGL;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fushigi.gl
{
public class HDRScreenBuffer
{
public GLTexture2D GetOutput() => (GLTexture2D)Framebuffer.Attachments[0];

private GLFramebuffer Framebuffer;

private ScreenQuad ScreenQuad;

Check failure on line 18 in Fushigi/gl/HDRScreenBuffer.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ScreenQuad' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 18 in Fushigi/gl/HDRScreenBuffer.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ScreenQuad' could not be found (are you missing a using directive or an assembly reference?)

public void Render(GL gl, int width, int height, GLTexture2D input)
{
if (Framebuffer == null)
Framebuffer = new GLFramebuffer(gl, FramebufferTarget.Framebuffer, (uint)width, (uint)height, InternalFormat.Rgba);

//Resize if needed
if (Framebuffer.Width != (uint)width || Framebuffer.Height != (uint)height)
Framebuffer.Resize((uint)width, (uint)height);

Framebuffer.Bind();

gl.ClearColor(0, 0, 0, 0);
gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
gl.Viewport(0, 0, Framebuffer.Width, Framebuffer.Height);

var shader = GLShaderCache.GetShader(gl, "PostEffect",
Path.Combine("res", "shaders", "screen.vert"),
Path.Combine("res", "shaders", "screen.frag"));

shader.Use();
shader.SetTexture("screenTexture", input, 1);

if (ScreenQuad == null) ScreenQuad = new ScreenQuad(gl, 1f);

ScreenQuad.Draw(shader);

gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
}
}
7 changes: 6 additions & 1 deletion Fushigi/gl/Mesh/VertexArrayObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ private unsafe void VertexAttributePointer(uint index, VertexAttribute attr)
_gl.EnableVertexAttribArray(index);
Buffers[attr.bufferIndex].Bind();

if (attr.type == VertexAttribPointerType.Int)
if (attr.type == VertexAttribPointerType.Int ||
attr.type == VertexAttribPointerType.UnsignedShort && !attr.normalized ||
attr.type == VertexAttribPointerType.UnsignedByte && !attr.normalized ||
attr.type == VertexAttribPointerType.UnsignedInt && !attr.normalized)
{
_gl.VertexAttribIPointer(index, attr.elementCount, (VertexAttribIType)attr.type, attr.stride, (void*)(attr.offset));
}
else
_gl.VertexAttribPointer(index, attr.elementCount, attr.type, attr.normalized, attr.stride, (void*)(attr.offset));

Expand Down
4 changes: 4 additions & 0 deletions Fushigi/gl/Textures/GLTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class GLTexture : GLObject, IFramebufferAttachment

internal GL _gl { get; private set; }

public bool IsDisposed = false;

public GLTexture(GL gl) : base(gl.GenTexture())
{
_gl = gl;
Expand Down Expand Up @@ -65,6 +67,8 @@ public void UpdateParameters()

public void Dispose()
{
IsDisposed = true;
_gl.DeleteTexture(ID);
}

internal static uint CalculateMipDimension(uint baseLevelDimension, uint mipLevel)
Expand Down

0 comments on commit 0897e94

Please sign in to comment.