Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
deccer committed Jul 30, 2023
1 parent 9a04e45 commit 6765f26
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 29 deletions.
6 changes: 6 additions & 0 deletions src/EngineKit/Graphics/IHasName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EngineKit.Graphics;

public interface IHasName
{
string Name { get; }
}
2 changes: 1 addition & 1 deletion src/EngineKit/Graphics/IMaterialLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IMaterialLibrary

IList<string> GetMaterialNames();

Material GetMaterialByName(string materialName);
Material GetMaterialByName(string? materialName);

Material GetRandomMaterial();

Expand Down
70 changes: 57 additions & 13 deletions src/EngineKit/Graphics/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace EngineKit.Graphics;

public record Material(string Name) : IDisposable
{
public const string MaterialNotFoundName = "M_NotFound";

private float _metallicFactor;
private float _roughnessFactor;
private Vector3 _specularFactor;
Expand Down Expand Up @@ -238,19 +240,60 @@ public void LoadTextures(
//TODO(deccer) TextureLoader should be called and pass in material, to set its Texture objects perhaps
//TODO(deccer) this needs to be inverted, ie someone else needs to load the stuff per material, not the material itself

BaseColorTexture = CreateTextureFromImage(BaseColorImage, Format.R8G8B8A8Srgb, BaseColorTextureSamplerInformation, logger,
graphicsContext, samplerLibrary, textures, makeResident);
NormalTexture = CreateTextureFromImage(NormalImage, Format.R8G8B8A8UNorm, NormalTextureSamplerInformation, logger, graphicsContext,
samplerLibrary, textures, makeResident);
MetalnessRoughnessTexture = CreateTextureFromImage(MetalnessRoughnessImage, Format.R8G8B8A8UNorm,
MetalnessRoughnessTextureSamplerInformation, logger, graphicsContext, samplerLibrary, textures,
BaseColorTexture = CreateTextureFromImage(
BaseColorImage,
Format.R8G8B8A8Srgb,
BaseColorTextureSamplerInformation,
logger,
graphicsContext,
samplerLibrary,
textures,
makeResident);
NormalTexture = CreateTextureFromImage(
NormalImage,
Format.R8G8B8A8UNorm,
NormalTextureSamplerInformation,
logger,
graphicsContext,
samplerLibrary,
textures,
makeResident);
MetalnessRoughnessTexture = CreateTextureFromImage(
MetalnessRoughnessImage,
Format.R8G8B8A8UNorm,
MetalnessRoughnessTextureSamplerInformation,
logger,
graphicsContext,
samplerLibrary,
textures,
makeResident);
SpecularTexture = CreateTextureFromImage(
SpecularImage,
Format.R8G8B8A8UNorm,
SpecularTextureSamplerInformation,
logger,
graphicsContext,
samplerLibrary,
textures,
makeResident);
OcclusionTexture = CreateTextureFromImage(
OcclusionImage,
Format.R8G8B8A8UNorm,
OcclusionTextureSamplerInformation,
logger,
graphicsContext,
samplerLibrary,
textures,
makeResident);
EmissiveTexture = CreateTextureFromImage(
EmissiveImage,
Format.R8G8B8A8Srgb,
EmissiveTextureSamplerInformation,
logger,
graphicsContext,
samplerLibrary,
textures,
makeResident);
SpecularTexture = CreateTextureFromImage(SpecularImage, Format.R8G8B8A8UNorm, SpecularTextureSamplerInformation, logger,
graphicsContext, samplerLibrary, textures, makeResident);
OcclusionTexture = CreateTextureFromImage(OcclusionImage, Format.R8G8B8A8UNorm, OcclusionTextureSamplerInformation, logger,
graphicsContext, samplerLibrary, textures, makeResident);
EmissiveTexture = CreateTextureFromImage(EmissiveImage, Format.R8G8B8A8Srgb, EmissiveTextureSamplerInformation, logger,
graphicsContext, samplerLibrary, textures, makeResident);

TexturesLoaded = true;
}
Expand All @@ -265,7 +308,8 @@ public void LoadTextures(
IDictionary<string, ITexture> textures,
bool makeResident)
{
if (image == null || string.IsNullOrEmpty(image.Name) ||
if (image == null ||
string.IsNullOrEmpty(image.Name) ||
textures.TryGetValue(image.Name, out var texture))
{
return null;
Expand Down
9 changes: 8 additions & 1 deletion src/EngineKit/Graphics/MeshPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ public PooledMesh GetOrAdd(MeshPrimitive meshPrimitive)
var vertexOffset = _pooledMeshes.Values.Sum(pm => pm.VertexCount);
var vertexCount = meshPrimitive.VertexCount;

pooledMesh = new PooledMesh((uint)indexCount, (uint)indexOffset, vertexCount, vertexOffset, meshPrimitive.MaterialName);
pooledMesh = new PooledMesh(
(uint)indexCount,
(uint)indexOffset,
vertexCount,
vertexOffset,
meshPrimitive.BoundingBox.Maximum,
meshPrimitive.BoundingBox.Minimum,
meshPrimitive.MaterialName);

var vertices = meshPrimitive.GetVertices();
VertexBuffer.Update(vertices, pooledMesh.VertexOffset);
Expand Down
2 changes: 1 addition & 1 deletion src/EngineKit/Graphics/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace EngineKit.Graphics;

public class Model
public class Model : IHasName
{
public Model(string name, IEnumerable<ModelMesh> modelMeshes)
{
Expand Down
4 changes: 3 additions & 1 deletion src/EngineKit/Graphics/ModelMesh.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace EngineKit.Graphics;

public class ModelMesh
public class ModelMesh : IHasName
{
public ModelMesh(MeshPrimitive meshPrimitive)
{
MeshPrimitive = meshPrimitive;
}

public MeshPrimitive MeshPrimitive { get; }

public string Name => MeshPrimitive.MeshName;
}
29 changes: 17 additions & 12 deletions src/EngineKit/UI/UIRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ internal sealed class UIRenderer : IUIRenderer
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_explicit_uniform_location : enable
layout(location = 0) in vec2 in_position;
layout(location = 1) in vec2 in_uv;
layout(location = 2) in vec4 in_color;
layout(location = 0) in vec2 i_position;
layout(location = 1) in vec2 i_uv;
layout(location = 2) in vec4 i_color;
out gl_PerVertex
out gl_PerVertex
{
vec4 gl_Position;
};
Expand All @@ -39,11 +39,11 @@ out gl_PerVertex
void main()
{
gl_Position = ProjectionMatrix * vec4(in_position, 0, 1);
v_color = in_color;
v_uv = in_uv;
gl_Position = ProjectionMatrix * vec4(i_position, 0, 1);
v_color = i_color;
v_uv = i_uv;
}
";
";

private const string ImGuiFragmentShader = @"
#version 460 core
Expand Down Expand Up @@ -90,6 +90,8 @@ void main()

private readonly IDictionary<string, ImFontPtr> _fonts;

private Array _keyValues;

public UIRenderer(
ILogger logger,
IGraphicsContext graphicsContext,
Expand Down Expand Up @@ -127,6 +129,8 @@ public bool Load(int width, int height)
_framebufferWidth = width;
_framebufferHeight = height;

_keyValues = Enum.GetValuesAsUnderlyingType<Glfw.Key>();

var imGuiContext = ImGui.CreateContext();
ImGui.SetCurrentContext(imGuiContext);

Expand All @@ -146,11 +150,14 @@ public bool Load(int width, int height)
var imGuiGraphicsPipelineResult = _graphicsContext.CreateGraphicsPipelineBuilder()
.WithShadersFromStrings(ImGuiVertexShader, ImGuiFragmentShader)
.WithTopology(PrimitiveTopology.Triangles)
/*
.WithVertexInput(new VertexInputDescriptorBuilder()
.AddAttribute(0, DataType.Float, 2, 0)
.AddAttribute(0, DataType.Float, 2, 8)
.AddAttribute(0, DataType.UnsignedByte, 4, 16, true)
.AddAttribute(0, DataType.UnsignedByte, 4, 16)
.Build("UI"))
*/
.WithVertexInput(VertexInputDescriptor.ForVertexType(VertexType.ImGui))
.EnableBlending(ColorBlendAttachmentDescriptor.PreMultiplied)
.DisableDepthTest()
.DisableDepthWrite()
Expand Down Expand Up @@ -359,7 +366,7 @@ private void UpdateImGuiInput()
: 0;
_scrollWheelValue = (int)currentMouseState.Scroll.Y;

foreach (Glfw.Key key in Enum.GetValues(typeof(Glfw.Key)))
foreach (Glfw.Key key in _keyValues)
{
if (key == Glfw.Key.Unknown)
{
Expand Down Expand Up @@ -423,7 +430,6 @@ private void RenderDrawData(ImDrawDataPtr drawDataPtr)
return;
}

GL.PushDebugGroup("UI");
for (var i = 0; i < drawDataPtr.CmdListsCount; i++)
{
var commandList = drawDataPtr.CmdListsRange[i];
Expand Down Expand Up @@ -524,7 +530,6 @@ private void RenderDrawData(ImDrawDataPtr drawDataPtr)

GL.Disable(GL.EnableType.Blend);
GL.Disable(GL.EnableType.ScissorTest);
GL.PopDebugGroup();
}

public void Dispose()
Expand Down

0 comments on commit 6765f26

Please sign in to comment.