Skip to content

Commit

Permalink
more misc. cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesGameDev committed Apr 11, 2024
1 parent 59cc8ba commit f54b4c1
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 110 deletions.
17 changes: 12 additions & 5 deletions Fushigi.Logger/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Fushigi.Logger
using System;

namespace Fushigi.Logger
{
public static class Logger
{
Expand Down Expand Up @@ -41,10 +43,7 @@ public static void HandleUnhandledException(object sender, UnhandledExceptionEve
if (e.ExceptionObject is not Exception exception) return;

Console.ForegroundColor = ConsoleColor.Red;
if (exception.StackTrace == null)
Log(exception.Message);
else
Log($"[ERROR] {exception.GetType().Name}: {exception.Message}\n{exception.StackTrace}");
LogError(exception);
Console.ForegroundColor = ConsoleColor.White;

Environment.Exit(1);
Expand Down Expand Up @@ -77,5 +76,13 @@ public static void LogError(object from, object msg)
Log($"[ERROR] [{from}]: {msg}");
Console.ForegroundColor = ConsoleColor.White;
}

public static void LogError(Exception e)
{
if (e.StackTrace == null)
Log(e.Message);
else
Log($"[ERROR] {e.GetType().Name}: {e.Message}\n{e.StackTrace}");
}
}
}
73 changes: 29 additions & 44 deletions Fushigi/gl/Bfres/Shaders/BfshaShaderRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class BfshaShaderRender
private Material Material;

public BfshaFile ShaderFile;
public BfshaFile.ShaderModel ShaderModel;
public ShaderModel ShaderModel;

//Resources
private BfshaFile.ShaderProgram ShaderProgram;
private ShaderProgram ShaderProgram;
private UniformBlock ConstantVSBlock;
private UniformBlock ConstantFSBlock;

Expand All @@ -52,8 +52,11 @@ public virtual void Init(GL gl, BfresRender.BfresModel modelRender, BfresRender.
ShaderFile = TryGetShader(material.ShaderAssign.ShaderArchiveName, material.ShaderAssign.ShadingModelName);

//Search for shader model
if (ShaderFile != null && ShaderFile.ShaderModels.ContainsKey(material.ShaderAssign.ShadingModelName))
ShaderModel = ShaderFile.ShaderModels[material.ShaderAssign.ShadingModelName];
if (ShaderFile != null &&
ShaderFile.ShaderModels.TryGetValue(material.ShaderAssign.ShadingModelName, out ShaderModel? value))
{
ShaderModel = value;
}

if (ShaderModel == null)
return;
Expand All @@ -65,7 +68,7 @@ public virtual void Init(GL gl, BfresRender.BfresModel modelRender, BfresRender.
if (SupportBlock == null)
{
SupportBlock = new UniformBlock(gl);
this.LoadSupportingBlock();
LoadSupportingBlock();
}

GLUtil.Label(gl, ObjectIdentifier.Buffer, MaterialBlock.ID, "Material Block");
Expand Down Expand Up @@ -94,7 +97,7 @@ public virtual void Init(GL gl, BfresRender.BfresModel modelRender, BfresRender.

public virtual void ReloadMaterialBlock()
{
SetMaterialBlock(this.MaterialBlock, this.Material);
SetMaterialBlock(MaterialBlock, Material);
}


Expand Down Expand Up @@ -129,36 +132,25 @@ public virtual void SetSkeletonBlock(UniformBlock block, BfresRender.BfresModel
/// <summary>
/// Finds the shader to use via material options and mesh skin count.
/// </summary>
public virtual BfshaFile.ShaderProgram ReloadProgram(byte skinCount)
{
return null;
}
public virtual ShaderProgram ReloadProgram(byte skinCount) => null;

/// <summary>
/// Looks for a texture to use that is not present in the bfres.
/// </summary>
public virtual GLTexture GetExternalTexture(GL gl, string sampler)
{
return null;
}
public virtual GLTexture GetExternalTexture(GL gl, string sampler) => null;

/// <summary>
/// Looks for the uniform block resource to bind.
/// </summary>
public virtual UniformBlock GetUniformBlock(string name)
{
return null;
}
public virtual UniformBlock GetUniformBlock(string name) => null;

/// <summary>
/// Compiles the given bfsha program to "ShaderInfo" with glsl code.
/// </summary>
public void CompileShader(GL gl, BfshaFile.ShaderProgram program)
public void CompileShader(GL gl, ShaderProgram program)
{
var variation = ShaderModel.GetShaderVariation(program);
if (variation == null)
throw new Exception($"Failed to load shader variation!");

var variation = ShaderModel.GetShaderVariation(program)
?? throw new Exception($"Failed to load shader variation!");
ShaderInfo = TegraShaderDecoder.LoadShaderProgram(gl, variation);

if (ShaderInfo.VertexConstants != null)
Expand All @@ -182,7 +174,7 @@ public virtual void Render(GL gl, BfresRender renderer, BfresRender.BfresModel m

SkeletonBlock = model.SkeletonBuffer;

this.SetShapeBlock(ShapeBlock, transform);
SetShapeBlock(ShapeBlock, transform);
SetSkeletonBlock(SkeletonBlock, model, transform);

if (ShaderInfo == null)
Expand All @@ -203,12 +195,10 @@ private void BindUniformBlocks(GL gl)
int ubo_bind = 1;

//Constants
if (this.ConstantVSBlock != null)
ConstantVSBlock.Render(this.ShaderInfo.Shader.ID, "_vp_c1", ubo_bind++);
if (this.ConstantFSBlock != null)
ConstantFSBlock.Render(this.ShaderInfo.Shader.ID, "_fp_c1", ubo_bind++);
ConstantVSBlock?.Render(ShaderInfo.Shader.ID, "_vp_c1", ubo_bind++);
ConstantFSBlock?.Render(ShaderInfo.Shader.ID, "_fp_c1", ubo_bind++);

SupportBlock.Render(this.ShaderInfo.Shader.ID, "_support_buffer", ubo_bind++);
SupportBlock.Render(ShaderInfo.Shader.ID, "_support_buffer", ubo_bind++);

ubo_bind = 5;

Expand All @@ -217,8 +207,7 @@ private void BindUniformBlocks(GL gl)
{
string name = ShaderModel.UniformBlocks.GetKey(i);
//Get the shader location info
var locationInfo = this.ShaderProgram.UniformBlockIndices[i];
int fragLocation = locationInfo.FragmentLocation;
var locationInfo = ShaderProgram.UniformBlockIndices[i];
int vertLocation = locationInfo.VertexLocation;

//Block unused for this program so skip it
Expand All @@ -233,16 +222,15 @@ private void BindUniformBlocks(GL gl)

//Bind uniform data to the vertex and/or pixel location and prepare a binding ID
//Prepare a unique binding per stage
BindUniformBlock(shaderBlock, this.ShaderInfo.Shader.ID, vertLocation, -1, ubo_bind++);
BindUniformBlock(shaderBlock, ShaderInfo.Shader.ID, vertLocation, -1, ubo_bind++);
}

for (int i = 0; i < ShaderModel.UniformBlocks.Count; i++)
{
string name = ShaderModel.UniformBlocks.GetKey(i);
//Get the shader location info
var locationInfo = this.ShaderProgram.UniformBlockIndices[i];
var locationInfo = ShaderProgram.UniformBlockIndices[i];
int fragLocation = locationInfo.FragmentLocation;
int vertLocation = locationInfo.VertexLocation;

//Block unused for this program so skip it
if (fragLocation == -1)
Expand All @@ -256,16 +244,13 @@ private void BindUniformBlocks(GL gl)

//Bind uniform data to the vertex and/or pixel location and prepare a binding ID
//Prepare a unique binding per stage
BindUniformBlock(shaderBlock, this.ShaderInfo.Shader.ID, -1, fragLocation, ubo_bind++);
BindUniformBlock(shaderBlock, ShaderInfo.Shader.ID, -1, fragLocation, ubo_bind++);
}
}

//Bind sampler data
private void BindSamplers(GL gl, BfresRender renderer)
{
int id = 1;


BindBindlessTextures(gl, ShaderInfo.Shader);

for (int i = 0; i < ShaderModel.Samplers.Count; i++)
Expand All @@ -281,7 +266,7 @@ private void BindSamplers(GL gl, BfresRender renderer)

for (int i = 0; i < ShaderModel.Samplers.Count; i++)
{
var locationInfo = this.ShaderProgram.SamplerIndices[i];
var locationInfo = ShaderProgram.SamplerIndices[i];
//Currently only using the vertex and fragment stages
if (locationInfo.VertexLocation == -1 && locationInfo.FragmentLocation == -1)
continue;
Expand All @@ -292,14 +277,14 @@ private void BindSamplers(GL gl, BfresRender renderer)
// Console.WriteLine($"sampler {sampler} loc {locationInfo.FragmentLocation} slot {i} uniform {ConvertSamplerID(locationInfo.FragmentLocation)}");

//Sampler assign has a key list of fragment shader samplers, value list of bfres material samplers
if (this.Material.ShaderAssign.SamplerAssign.ContainsKey(sampler))
if (Material.ShaderAssign.SamplerAssign.ContainsKey(sampler))
{
//Get the resource sampler
//Important to note, fragment samplers are unique while material samplers can be the same
//So we need to lookup which material sampler the current fragment sampler uses.
string resSampler = this.Material.ShaderAssign.SamplerAssign[sampler].String;
string resSampler = Material.ShaderAssign.SamplerAssign[sampler].String;
//Find a texture using the sampler
textureIndex = this.Material.Samplers.Keys.ToList().FindIndex(x => x == resSampler);
textureIndex = Material.Samplers.Keys.ToList().FindIndex(x => x == resSampler);
}

var slot = i + 1;
Expand All @@ -315,7 +300,7 @@ private void BindSamplers(GL gl, BfresRender renderer)
}
else //Bind texture inside the bfres material
{
var texMap = this.Material.Textures[textureIndex];
var texMap = Material.Textures[textureIndex];
tex = TryGetTexture(gl, renderer, texMap);
}

Expand All @@ -330,7 +315,7 @@ private void BindSamplers(GL gl, BfresRender renderer)

if (textureIndex != -1)
{
var samplerConfig = this.Material.Samplers[textureIndex];
var samplerConfig = Material.Samplers[textureIndex];
SetupSampler(gl, tex, samplerConfig);
}

Expand Down
10 changes: 1 addition & 9 deletions Fushigi/gl/Bfres/Shaders/WonderGameShader.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
using Fushigi.Bfres;
using Silk.NET.OpenGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Fushigi.util;
using Fushigi.env;
using System.Runtime.Intrinsics.Arm;
using static Fushigi.gl.Bfres.GsysEnvironment;
using Silk.NET.Core.Native;

namespace Fushigi.gl.Bfres
{
Expand Down Expand Up @@ -83,7 +75,7 @@ public void Update(UniformBlock block)
}
}

class ShapeBlock
new class ShapeBlock
{
public Matrix4x4 ShapeMatrix = Matrix4x4.Identity;

Expand Down
5 changes: 0 additions & 5 deletions Fushigi/gl/Bfres/TileBfresRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
using Silk.NET.OpenGL;
using System.Diagnostics;
using System.Numerics;
using System.Reflection;
using static Fushigi.course.CourseUnit;

namespace Fushigi.gl.Bfres
{
public class TileBfresRender
{
private TileParamBlock TileParams;

private BfresRender BfresRender;
private Matrix4x4 Transform = Matrix4x4.Identity;
private SkinDivision mSkinDivision;

public record struct MaterialNames(string? Edge, string? Wall, string? Ground);
Expand Down
1 change: 1 addition & 0 deletions Fushigi/rstb/RSTB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void Save()
}
catch (IOException e)
{
Logger.Logger.LogError(e);
//Likely due to the course being open in the game, caught to prevent crash
//TODO: notify the user
}
Expand Down
3 changes: 2 additions & 1 deletion Fushigi/ui/EditContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ internal class EditContextBase
{
private class BatchAction(EditContextBase context) : ICommittable
{
public string Name { get; private set; }
public string? Name { get; private set; }

public void Commit(string name)
{
Name = name;
Expand Down
57 changes: 25 additions & 32 deletions Fushigi/ui/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,36 @@ public MainWindow()
iconConfig->GlyphOffset = new Vector2(0);

float size = 16;
mDefaultFont = io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "Font.ttf"),
size, nativeConfig, io.Fonts.GetGlyphRangesDefault());

{
mDefaultFont = io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "Font.ttf"),
size, nativeConfig, io.Fonts.GetGlyphRangesDefault());

io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "NotoSansCJKjp-Medium.otf"),
size, nativeConfigJP, io.Fonts.GetGlyphRangesJapanese());
io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "NotoSansCJKjp-Medium.otf"),
size, nativeConfigJP, io.Fonts.GetGlyphRangesJapanese());

//other fonts go here and follow the same schema
GCHandle rangeHandle = GCHandle.Alloc(new ushort[] { IconUtil.MIN_GLYPH_RANGE, IconUtil.MAX_GLYPH_RANGE, 0 }, GCHandleType.Pinned);
try
{
io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "la-regular-400.ttf"),
size, iconConfig, rangeHandle.AddrOfPinnedObject());
//other fonts go here and follow the same schema
GCHandle rangeHandle = GCHandle.Alloc(new ushort[] { IconUtil.MIN_GLYPH_RANGE, IconUtil.MAX_GLYPH_RANGE, 0 }, GCHandleType.Pinned);
try
{
io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "la-regular-400.ttf"),
size, iconConfig, rangeHandle.AddrOfPinnedObject());

io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "la-solid-900.ttf"),
size, iconConfig, rangeHandle.AddrOfPinnedObject());
io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "la-solid-900.ttf"),
size, iconConfig, rangeHandle.AddrOfPinnedObject());

io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "la-brands-400.ttf"),
size, iconConfig, rangeHandle.AddrOfPinnedObject());
io.Fonts.AddFontFromFileTTF(
Path.Combine("res", "la-brands-400.ttf"),
size, iconConfig, rangeHandle.AddrOfPinnedObject());

io.Fonts.Build();
}
finally
{
if (rangeHandle.IsAllocated)
rangeHandle.Free();
}
io.Fonts.Build();
}
finally
{
if (rangeHandle.IsAllocated)
rangeHandle.Free();
}
}
});
Expand All @@ -105,9 +102,7 @@ public async Task<bool> TryCloseCourse()
return true;
}
else
{
return false;
}
}

return true;
Expand All @@ -132,7 +127,6 @@ public void Close()
mSkipCloseTest = true;
mWindow.Close();
}

}).ConfigureAwait(false); //fire and forget
}

Expand Down Expand Up @@ -175,7 +169,6 @@ await ProgressBarDialog.ShowDialogForAsyncAction(this,

await LoadParamDBWithProgressBar(this);
await Task.Delay(500);

}

string? latestCourse = UserSettings.GetLatestCourse();
Expand Down
Loading

0 comments on commit f54b4c1

Please sign in to comment.