Skip to content

Commit 6765f26

Browse files
committed
More code cleanup
1 parent 9a04e45 commit 6765f26

File tree

7 files changed

+93
-29
lines changed

7 files changed

+93
-29
lines changed

src/EngineKit/Graphics/IHasName.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace EngineKit.Graphics;
2+
3+
public interface IHasName
4+
{
5+
string Name { get; }
6+
}

src/EngineKit/Graphics/IMaterialLibrary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface IMaterialLibrary
1010

1111
IList<string> GetMaterialNames();
1212

13-
Material GetMaterialByName(string materialName);
13+
Material GetMaterialByName(string? materialName);
1414

1515
Material GetRandomMaterial();
1616

src/EngineKit/Graphics/Material.cs

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace EngineKit.Graphics;
99

1010
public record Material(string Name) : IDisposable
1111
{
12+
public const string MaterialNotFoundName = "M_NotFound";
13+
1214
private float _metallicFactor;
1315
private float _roughnessFactor;
1416
private Vector3 _specularFactor;
@@ -238,19 +240,60 @@ public void LoadTextures(
238240
//TODO(deccer) TextureLoader should be called and pass in material, to set its Texture objects perhaps
239241
//TODO(deccer) this needs to be inverted, ie someone else needs to load the stuff per material, not the material itself
240242

241-
BaseColorTexture = CreateTextureFromImage(BaseColorImage, Format.R8G8B8A8Srgb, BaseColorTextureSamplerInformation, logger,
242-
graphicsContext, samplerLibrary, textures, makeResident);
243-
NormalTexture = CreateTextureFromImage(NormalImage, Format.R8G8B8A8UNorm, NormalTextureSamplerInformation, logger, graphicsContext,
244-
samplerLibrary, textures, makeResident);
245-
MetalnessRoughnessTexture = CreateTextureFromImage(MetalnessRoughnessImage, Format.R8G8B8A8UNorm,
246-
MetalnessRoughnessTextureSamplerInformation, logger, graphicsContext, samplerLibrary, textures,
243+
BaseColorTexture = CreateTextureFromImage(
244+
BaseColorImage,
245+
Format.R8G8B8A8Srgb,
246+
BaseColorTextureSamplerInformation,
247+
logger,
248+
graphicsContext,
249+
samplerLibrary,
250+
textures,
251+
makeResident);
252+
NormalTexture = CreateTextureFromImage(
253+
NormalImage,
254+
Format.R8G8B8A8UNorm,
255+
NormalTextureSamplerInformation,
256+
logger,
257+
graphicsContext,
258+
samplerLibrary,
259+
textures,
260+
makeResident);
261+
MetalnessRoughnessTexture = CreateTextureFromImage(
262+
MetalnessRoughnessImage,
263+
Format.R8G8B8A8UNorm,
264+
MetalnessRoughnessTextureSamplerInformation,
265+
logger,
266+
graphicsContext,
267+
samplerLibrary,
268+
textures,
269+
makeResident);
270+
SpecularTexture = CreateTextureFromImage(
271+
SpecularImage,
272+
Format.R8G8B8A8UNorm,
273+
SpecularTextureSamplerInformation,
274+
logger,
275+
graphicsContext,
276+
samplerLibrary,
277+
textures,
278+
makeResident);
279+
OcclusionTexture = CreateTextureFromImage(
280+
OcclusionImage,
281+
Format.R8G8B8A8UNorm,
282+
OcclusionTextureSamplerInformation,
283+
logger,
284+
graphicsContext,
285+
samplerLibrary,
286+
textures,
287+
makeResident);
288+
EmissiveTexture = CreateTextureFromImage(
289+
EmissiveImage,
290+
Format.R8G8B8A8Srgb,
291+
EmissiveTextureSamplerInformation,
292+
logger,
293+
graphicsContext,
294+
samplerLibrary,
295+
textures,
247296
makeResident);
248-
SpecularTexture = CreateTextureFromImage(SpecularImage, Format.R8G8B8A8UNorm, SpecularTextureSamplerInformation, logger,
249-
graphicsContext, samplerLibrary, textures, makeResident);
250-
OcclusionTexture = CreateTextureFromImage(OcclusionImage, Format.R8G8B8A8UNorm, OcclusionTextureSamplerInformation, logger,
251-
graphicsContext, samplerLibrary, textures, makeResident);
252-
EmissiveTexture = CreateTextureFromImage(EmissiveImage, Format.R8G8B8A8Srgb, EmissiveTextureSamplerInformation, logger,
253-
graphicsContext, samplerLibrary, textures, makeResident);
254297

255298
TexturesLoaded = true;
256299
}
@@ -265,7 +308,8 @@ public void LoadTextures(
265308
IDictionary<string, ITexture> textures,
266309
bool makeResident)
267310
{
268-
if (image == null || string.IsNullOrEmpty(image.Name) ||
311+
if (image == null ||
312+
string.IsNullOrEmpty(image.Name) ||
269313
textures.TryGetValue(image.Name, out var texture))
270314
{
271315
return null;

src/EngineKit/Graphics/MeshPool.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ public PooledMesh GetOrAdd(MeshPrimitive meshPrimitive)
3939
var vertexOffset = _pooledMeshes.Values.Sum(pm => pm.VertexCount);
4040
var vertexCount = meshPrimitive.VertexCount;
4141

42-
pooledMesh = new PooledMesh((uint)indexCount, (uint)indexOffset, vertexCount, vertexOffset, meshPrimitive.MaterialName);
42+
pooledMesh = new PooledMesh(
43+
(uint)indexCount,
44+
(uint)indexOffset,
45+
vertexCount,
46+
vertexOffset,
47+
meshPrimitive.BoundingBox.Maximum,
48+
meshPrimitive.BoundingBox.Minimum,
49+
meshPrimitive.MaterialName);
4350

4451
var vertices = meshPrimitive.GetVertices();
4552
VertexBuffer.Update(vertices, pooledMesh.VertexOffset);

src/EngineKit/Graphics/Model.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace EngineKit.Graphics;
44

5-
public class Model
5+
public class Model : IHasName
66
{
77
public Model(string name, IEnumerable<ModelMesh> modelMeshes)
88
{
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
namespace EngineKit.Graphics;
22

3-
public class ModelMesh
3+
public class ModelMesh : IHasName
44
{
55
public ModelMesh(MeshPrimitive meshPrimitive)
66
{
77
MeshPrimitive = meshPrimitive;
88
}
99

1010
public MeshPrimitive MeshPrimitive { get; }
11+
12+
public string Name => MeshPrimitive.MeshName;
1113
}

src/EngineKit/UI/UIRenderer.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ internal sealed class UIRenderer : IUIRenderer
2121
#extension GL_ARB_separate_shader_objects : enable
2222
#extension GL_ARB_explicit_uniform_location : enable
2323
24-
layout(location = 0) in vec2 in_position;
25-
layout(location = 1) in vec2 in_uv;
26-
layout(location = 2) in vec4 in_color;
24+
layout(location = 0) in vec2 i_position;
25+
layout(location = 1) in vec2 i_uv;
26+
layout(location = 2) in vec4 i_color;
2727
28-
out gl_PerVertex
28+
out gl_PerVertex
2929
{
3030
vec4 gl_Position;
3131
};
@@ -39,11 +39,11 @@ out gl_PerVertex
3939
4040
void main()
4141
{
42-
gl_Position = ProjectionMatrix * vec4(in_position, 0, 1);
43-
v_color = in_color;
44-
v_uv = in_uv;
42+
gl_Position = ProjectionMatrix * vec4(i_position, 0, 1);
43+
v_color = i_color;
44+
v_uv = i_uv;
4545
}
46-
";
46+
";
4747

4848
private const string ImGuiFragmentShader = @"
4949
#version 460 core
@@ -90,6 +90,8 @@ void main()
9090

9191
private readonly IDictionary<string, ImFontPtr> _fonts;
9292

93+
private Array _keyValues;
94+
9395
public UIRenderer(
9496
ILogger logger,
9597
IGraphicsContext graphicsContext,
@@ -127,6 +129,8 @@ public bool Load(int width, int height)
127129
_framebufferWidth = width;
128130
_framebufferHeight = height;
129131

132+
_keyValues = Enum.GetValuesAsUnderlyingType<Glfw.Key>();
133+
130134
var imGuiContext = ImGui.CreateContext();
131135
ImGui.SetCurrentContext(imGuiContext);
132136

@@ -146,11 +150,14 @@ public bool Load(int width, int height)
146150
var imGuiGraphicsPipelineResult = _graphicsContext.CreateGraphicsPipelineBuilder()
147151
.WithShadersFromStrings(ImGuiVertexShader, ImGuiFragmentShader)
148152
.WithTopology(PrimitiveTopology.Triangles)
153+
/*
149154
.WithVertexInput(new VertexInputDescriptorBuilder()
150155
.AddAttribute(0, DataType.Float, 2, 0)
151156
.AddAttribute(0, DataType.Float, 2, 8)
152-
.AddAttribute(0, DataType.UnsignedByte, 4, 16, true)
157+
.AddAttribute(0, DataType.UnsignedByte, 4, 16)
153158
.Build("UI"))
159+
*/
160+
.WithVertexInput(VertexInputDescriptor.ForVertexType(VertexType.ImGui))
154161
.EnableBlending(ColorBlendAttachmentDescriptor.PreMultiplied)
155162
.DisableDepthTest()
156163
.DisableDepthWrite()
@@ -359,7 +366,7 @@ private void UpdateImGuiInput()
359366
: 0;
360367
_scrollWheelValue = (int)currentMouseState.Scroll.Y;
361368

362-
foreach (Glfw.Key key in Enum.GetValues(typeof(Glfw.Key)))
369+
foreach (Glfw.Key key in _keyValues)
363370
{
364371
if (key == Glfw.Key.Unknown)
365372
{
@@ -423,7 +430,6 @@ private void RenderDrawData(ImDrawDataPtr drawDataPtr)
423430
return;
424431
}
425432

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

525531
GL.Disable(GL.EnableType.Blend);
526532
GL.Disable(GL.EnableType.ScissorTest);
527-
GL.PopDebugGroup();
528533
}
529534

530535
public void Dispose()

0 commit comments

Comments
 (0)