Skip to content

Commit

Permalink
Use Format for vertex input setup. This will fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
deccer committed Dec 7, 2023
1 parent d4ec36b commit 96e09df
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ private bool LoadPipelines()
var graphicsPipelineResult = GraphicsContext.CreateGraphicsPipelineBuilder()
.WithShadersFromFiles("Shaders/FST.vs.glsl", "Shaders/Texture.fs.glsl")
.WithVertexInput(new VertexInputDescriptorBuilder()
.AddAttribute(0, DataType.Float, 3, 0)
.AddAttribute(0, DataType.Float, 2, 12)
.AddAttribute(0, Format.R32G32B32Float, 0)
.AddAttribute(0, Format.R32G32Float, 12)
.Build(nameof(VertexPositionUv)))
.WithTopology(PrimitiveTopology.Triangles)
.WithFaceWinding(FaceWinding.Clockwise)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,10 @@ private bool CreatePipelines()
var gBufferGraphicsPipelineResult = GraphicsContext.CreateGraphicsPipelineBuilder()
.WithShadersFromFiles("Shaders/Scene.vs.glsl", "Shaders/Scene.fs.glsl")
.WithVertexInput(new VertexInputDescriptorBuilder()
.AddAttribute(0, DataType.Float, 3, 0)
.AddAttribute(0, DataType.Float, 3, 12)
.AddAttribute(0, DataType.Float, 2, 24)
.AddAttribute(0, DataType.Float, 4, 32)
.AddAttribute(0, Format.R32G32B32Float, 0)
.AddAttribute(0, Format.R32G32B32Float, 12)
.AddAttribute(0, Format.R32G32Float, 24)
.AddAttribute(0, Format.R32G32B32A32Float, 32)
.Build("Scene"))
.EnableCulling(CullMode.Back)
.Build("GBuffer-Pipeline");
Expand All @@ -444,8 +444,8 @@ private bool CreatePipelines()
var finalGraphicsPipelineResult = GraphicsContext.CreateGraphicsPipelineBuilder()
.WithShadersFromFiles("Shaders/FST.vs.glsl", "Shaders/Texture.fs.glsl")
.WithVertexInput(new VertexInputDescriptorBuilder()
.AddAttribute(0, DataType.Float, 3, 0)
.AddAttribute(0, DataType.Float, 2, 12)
.AddAttribute(0, Format.R32G32B32Float, 0)
.AddAttribute(0, Format.R32G32Float, 12)
.Build("FST"))
.Build("Final-Pipeline");
if (finalGraphicsPipelineResult.IsFailure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ private bool LoadPipelines()
var graphicsPipelineResult = GraphicsContext.CreateGraphicsPipelineBuilder()
.WithShadersFromFiles("Shaders/Scene.vs.glsl", "Shaders/Scene.fs.glsl")
.WithVertexInput(new VertexInputDescriptorBuilder()
.AddAttribute(0, DataType.Float, 3, 0)
.AddAttribute(0, DataType.Float, 3, 12)
.AddAttribute(0, DataType.Float, 2, 24)
.AddAttribute(0, DataType.Float, 4, 32)
.AddAttribute(0, Format.R32G32B32Float, 0)
.AddAttribute(0, Format.R32G32B32Float, 12)
.AddAttribute(0, Format.R32G32Float, 24)
.AddAttribute(0, Format.R32G32B32A32Float, 32)
.Build(nameof(VertexPositionNormalUvTangent)))
.WithTopology(PrimitiveTopology.Triangles)
.WithFaceWinding(FaceWinding.CounterClockwise)
Expand Down
20 changes: 10 additions & 10 deletions src/EngineKit/Graphics/InputLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public InputLayout(VertexInputDescriptor vertexInputDescriptor)
_id = GL.CreateVertexArray();

var label = string.IsNullOrEmpty(vertexInputDescriptor.Label)
? $"InputLayout-{vertexInputDescriptor.Label}-"
: "InputLayout-";
? "InputLayout-"
: $"InputLayout-{vertexInputDescriptor.Label}-";
foreach (var vertexBinding in vertexInputDescriptor.VertexBindingDescriptors)
{
GL.EnableVertexArrayAttrib(_id, vertexBinding.Location);
Expand All @@ -27,31 +27,31 @@ public InputLayout(VertexInputDescriptor vertexInputDescriptor)

switch (componentDataType)
{
case DataType.Float:
case GL.DataType.Float:
GL.VertexArrayAttribFormat(
_id,
vertexBinding.Location,
componentCount,
componentDataType.ToGL(),
componentDataType,
vertexBinding.IsNormalized,
vertexBinding.Offset);
break;
case DataType.Integer:
case DataType.UnsignedInteger:
case GL.DataType.Int:
case GL.DataType.UnsignedInt:
GL.VertexArrayAttribIFormat(
_id,
vertexBinding.Location,
componentCount,
componentDataType.ToGL(),
componentDataType,
vertexBinding.Offset);
break;
case DataType.Byte:
case DataType.UnsignedByte:
case GL.DataType.Byte:
case GL.DataType.UnsignedByte:
GL.VertexArrayAttribFormat(
_id,
vertexBinding.Location,
componentCount,
componentDataType.ToGL(),
componentDataType,
true,
vertexBinding.Offset);
break;
Expand Down
4 changes: 3 additions & 1 deletion src/EngineKit/Graphics/VertexInputBindingDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using EngineKit.Native.OpenGL;

namespace EngineKit.Graphics;

public readonly record struct VertexInputBindingDescriptor(
uint Location,
uint Binding,
DataType DataType,
GL.DataType DataType,
int ComponentCount,
uint Offset,
bool IsNormalized = false);
3 changes: 2 additions & 1 deletion src/EngineKit/Graphics/VertexInputDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using EngineKit.Extensions;
using ImGuiNET;

namespace EngineKit.Graphics;
Expand Down Expand Up @@ -80,7 +81,7 @@ private static VertexInputDescriptor BuildVertexInputDescriptorFor<TVertexType>(
var offset = (uint)Marshal.OffsetOf<TVertexType>(vertexTypeAttribute.Name);
var isNormalized = GetNormalizedFromFieldType(vertexTypeAttribute.FieldType);
return new VertexInputBindingDescriptor(location, binding, dataType, componentCount, offset, isNormalized);
return new VertexInputBindingDescriptor(location, binding, dataType.ToGL(), componentCount, offset, isNormalized);
});
return new VertexInputDescriptor(vertexInputBindingDescriptors.ToArray(), vertexType.Name);
}
Expand Down
211 changes: 189 additions & 22 deletions src/EngineKit/Graphics/VertexInputDescriptorBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EngineKit.Extensions;
using EngineKit.Native.OpenGL;

namespace EngineKit.Graphics;

Expand All @@ -12,40 +15,204 @@ public VertexInputDescriptorBuilder()
_vertexInputBindingDescriptors = new List<VertexInputBindingDescriptor>();
}

public VertexInputDescriptorBuilder AddAttribute(
uint binding,
DataType dataType,
int componentCount,
uint offset,
bool isNormalized = false)
{
var location = (uint)_vertexInputBindingDescriptors.Count;
_vertexInputBindingDescriptors.Add(new VertexInputBindingDescriptor(location, binding, dataType, componentCount, offset, isNormalized));
return this;
}

/*
public VertexInputDescriptorBuilder AddAttribute(
uint binding,
Format format,
uint offset)
{
var location = (uint)_vertexInputBindingDescriptors.Count;
var (dataType, componentCount, isNormalized) = FormatToDataTypeAndComponentCount(format);
_vertexInputBindingDescriptors.Add(new VertexInputBindingDescriptor(location, binding, dataType, componentCount, offset, isNormalized));

var attributeDataType = GetDataType(format);
var componentCount = GetComponentCount(format);
var isNormalized = GetIsFormatNormalized(format);
_vertexInputBindingDescriptors.Add(new VertexInputBindingDescriptor(location, binding, attributeDataType, componentCount, offset, isNormalized));
return this;
}
*/

public VertexInputDescriptor Build(Label label)
private static GL.DataType GetDataType(Format format)
{
return new VertexInputDescriptor(_vertexInputBindingDescriptors.ToArray(), label);
switch (format)
{
case Format.R8UNorm:
case Format.R8G8UNorm:
case Format.R8G8B8UNorm:
case Format.R8G8B8A8UNorm:
case Format.R8SNorm:
case Format.R8G8SNorm:
case Format.R8G8B8SNorm:
case Format.R8G8B8A8SNorm:
case Format.R16SNorm:
case Format.R16G16SNorm:
case Format.R16G16B16SNorm:
case Format.R16G16B16A16SNorm:
case Format.R16UNorm:
case Format.R16G16UNorm:
case Format.R16G16B16UNorm:
case Format.R16G16B16A16UNorm:
case Format.R16Float:
case Format.R16G16Float:
case Format.R16G16B16Float:
case Format.R16G16B16A16Float:
case Format.R32Float:
case Format.R32G32Float:
case Format.R32G32B32Float:
case Format.R32G32B32A32Float:
return DataType.Float.ToGL();
case Format.R8SInt:
case Format.R8G8SInt:
case Format.R8G8B8SInt:
case Format.R8G8B8A8SInt:
case Format.R16SInt:
case Format.R16G16SInt:
case Format.R16G16B16SInt:
case Format.R16G16B16A16SInt:
case Format.R32SInt:
case Format.R32G32SInt:
case Format.R32G32B32SInt:
case Format.R32G32B32A32SInt:
return DataType.Integer.ToGL();
case Format.R8UInt:
case Format.R8G8UInt:
case Format.R8G8B8UInt:
case Format.R8G8B8A8UInt:
case Format.R16UInt:
case Format.R16G16UInt:
case Format.R16G16B16UInt:
case Format.R16G16B16A16UInt:
case Format.R32UInt:
case Format.R32G32UInt:
case Format.R32G32B32UInt:
case Format.R32G32B32A32UInt:
return DataType.UnsignedInteger.ToGL();
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}
}

private static int GetComponentCount(Format format)
{
switch (format)
{
case Format.R8UNorm:
case Format.R8SNorm:
case Format.R16SNorm:
case Format.R16UNorm:
case Format.R16Float:
case Format.R32Float:
case Format.R8SInt:
case Format.R16SInt:
case Format.R32SInt:
case Format.R8UInt:
case Format.R16UInt:
case Format.R32UInt:
return 1;
case Format.R8G8UNorm:
case Format.R8G8SNorm:
case Format.R16G16SNorm:
case Format.R16G16UNorm:
case Format.R16G16Float:
case Format.R32G32Float:
case Format.R8G8SInt:
case Format.R16G16SInt:
case Format.R32G32SInt:
case Format.R8G8UInt:
case Format.R16G16UInt:
case Format.R32G32UInt:
return 2;
case Format.R8G8B8UNorm:
case Format.R8G8B8SNorm:
case Format.R16G16B16SNorm:
case Format.R16G16B16UNorm:
case Format.R16G16B16Float:
case Format.R32G32B32Float:
case Format.R8G8B8SInt:
case Format.R16G16B16SInt:
case Format.R32G32B32SInt:
case Format.R8G8B8UInt:
case Format.R16G16B16UInt:
case Format.R32G32B32UInt:
return 3;
case Format.R8G8B8A8UNorm:
case Format.R8G8B8A8SNorm:
case Format.R16G16B16A16SNorm:
case Format.R16G16B16A16UNorm:
case Format.R16G16B16A16Float:
case Format.R32G32B32A32Float:
case Format.R8G8B8A8SInt:
case Format.R16G16B16A16SInt:
case Format.R32G32B32A32SInt:
case Format.R8G8B8A8UInt:
case Format.R16G16B16A16UInt:
case Format.R32G32B32A32UInt:
return 4;
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}
}

private static bool GetIsFormatNormalized(Format format)
{
switch (format)
{
case Format.R8UNorm:
case Format.R8G8UNorm:
case Format.R8G8B8UNorm:
case Format.R8G8B8A8UNorm:
case Format.R8SNorm:
case Format.R8G8SNorm:
case Format.R8G8B8SNorm:
case Format.R8G8B8A8SNorm:
case Format.R16SNorm:
case Format.R16G16SNorm:
case Format.R16G16B16SNorm:
case Format.R16G16B16A16SNorm:
case Format.R16UNorm:
case Format.R16G16UNorm:
case Format.R16G16B16UNorm:
case Format.R16G16B16A16UNorm:
return true;
case Format.R16Float:
case Format.R16G16Float:
case Format.R16G16B16Float:
case Format.R16G16B16A16Float:
case Format.R32Float:
case Format.R32G32Float:
case Format.R32G32B32Float:
case Format.R32G32B32A32Float:

case Format.R8SInt:
case Format.R8G8SInt:
case Format.R8G8B8SInt:
case Format.R8G8B8A8SInt:
case Format.R16SInt:
case Format.R16G16SInt:
case Format.R16G16B16SInt:
case Format.R16G16B16A16SInt:
case Format.R32SInt:
case Format.R32G32SInt:
case Format.R32G32B32SInt:
case Format.R32G32B32A32SInt:

case Format.R8UInt:
case Format.R8G8UInt:
case Format.R8G8B8UInt:
case Format.R8G8B8A8UInt:
case Format.R16UInt:
case Format.R16G16UInt:
case Format.R16G16B16UInt:
case Format.R16G16B16A16UInt:
case Format.R32UInt:
case Format.R32G32UInt:
case Format.R32G32B32UInt:
case Format.R32G32B32A32UInt:
return false;
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}
}

/*
private (DataType DataType, int ComponentType, bool IsNormalized) FormatToDataTypeAndComponentCount(Format format)
public VertexInputDescriptor Build(Label label)
{
var dataType = format.ToDataType();
return new VertexInputDescriptor(_vertexInputBindingDescriptors.ToArray(), label);
}
*/
}

0 comments on commit 96e09df

Please sign in to comment.