diff --git a/src/EngineKit/Graphics/IHasName.cs b/src/EngineKit/Graphics/IHasName.cs new file mode 100644 index 0000000..8512ef4 --- /dev/null +++ b/src/EngineKit/Graphics/IHasName.cs @@ -0,0 +1,6 @@ +namespace EngineKit.Graphics; + +public interface IHasName +{ + string Name { get; } +} \ No newline at end of file diff --git a/src/EngineKit/Graphics/IMaterialLibrary.cs b/src/EngineKit/Graphics/IMaterialLibrary.cs index d6e0fb5..10be354 100644 --- a/src/EngineKit/Graphics/IMaterialLibrary.cs +++ b/src/EngineKit/Graphics/IMaterialLibrary.cs @@ -10,7 +10,7 @@ public interface IMaterialLibrary IList GetMaterialNames(); - Material GetMaterialByName(string materialName); + Material GetMaterialByName(string? materialName); Material GetRandomMaterial(); diff --git a/src/EngineKit/Graphics/Material.cs b/src/EngineKit/Graphics/Material.cs index 63af8ec..3689192 100644 --- a/src/EngineKit/Graphics/Material.cs +++ b/src/EngineKit/Graphics/Material.cs @@ -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; @@ -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; } @@ -265,7 +308,8 @@ public void LoadTextures( IDictionary 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; diff --git a/src/EngineKit/Graphics/MeshPool.cs b/src/EngineKit/Graphics/MeshPool.cs index 9a8ee7d..674c1b7 100644 --- a/src/EngineKit/Graphics/MeshPool.cs +++ b/src/EngineKit/Graphics/MeshPool.cs @@ -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); diff --git a/src/EngineKit/Graphics/Model.cs b/src/EngineKit/Graphics/Model.cs index ce40400..5d7b20b 100644 --- a/src/EngineKit/Graphics/Model.cs +++ b/src/EngineKit/Graphics/Model.cs @@ -2,7 +2,7 @@ namespace EngineKit.Graphics; -public class Model +public class Model : IHasName { public Model(string name, IEnumerable modelMeshes) { diff --git a/src/EngineKit/Graphics/ModelMesh.cs b/src/EngineKit/Graphics/ModelMesh.cs index fb8d396..7264bf2 100644 --- a/src/EngineKit/Graphics/ModelMesh.cs +++ b/src/EngineKit/Graphics/ModelMesh.cs @@ -1,6 +1,6 @@ namespace EngineKit.Graphics; -public class ModelMesh +public class ModelMesh : IHasName { public ModelMesh(MeshPrimitive meshPrimitive) { @@ -8,4 +8,6 @@ public ModelMesh(MeshPrimitive meshPrimitive) } public MeshPrimitive MeshPrimitive { get; } + + public string Name => MeshPrimitive.MeshName; } \ No newline at end of file diff --git a/src/EngineKit/UI/UIRenderer.cs b/src/EngineKit/UI/UIRenderer.cs index 459c5bc..1df86d4 100644 --- a/src/EngineKit/UI/UIRenderer.cs +++ b/src/EngineKit/UI/UIRenderer.cs @@ -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; }; @@ -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 @@ -90,6 +90,8 @@ void main() private readonly IDictionary _fonts; + private Array _keyValues; + public UIRenderer( ILogger logger, IGraphicsContext graphicsContext, @@ -127,6 +129,8 @@ public bool Load(int width, int height) _framebufferWidth = width; _framebufferHeight = height; + _keyValues = Enum.GetValuesAsUnderlyingType(); + var imGuiContext = ImGui.CreateContext(); ImGui.SetCurrentContext(imGuiContext); @@ -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() @@ -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) { @@ -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]; @@ -524,7 +530,6 @@ private void RenderDrawData(ImDrawDataPtr drawDataPtr) GL.Disable(GL.EnableType.Blend); GL.Disable(GL.EnableType.ScissorTest); - GL.PopDebugGroup(); } public void Dispose()