|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using kajarlabs.Framework.Graphics.Rendering.Vertices; |
| 3 | + |
1 | 4 | namespace kajarlabs.Framework.Graphics.Rendering; |
2 | 5 |
|
3 | 6 | [Flags] |
@@ -61,3 +64,128 @@ internal readonly record struct GpuOffscreenFrame( |
61 | 64 | nint ColorTargetTexture, |
62 | 65 | int Width, |
63 | 66 | int Height); |
| 67 | + |
| 68 | +internal enum GpuShaderStage |
| 69 | +{ |
| 70 | + Vertex = 0, |
| 71 | + Fragment = 1, |
| 72 | +} |
| 73 | + |
| 74 | +internal enum GpuPrimitiveTopology |
| 75 | +{ |
| 76 | + TriangleList = 0, |
| 77 | + TriangleStrip = 1, |
| 78 | + LineList = 2, |
| 79 | + LineStrip = 3, |
| 80 | + PointList = 4, |
| 81 | +} |
| 82 | + |
| 83 | +internal enum GpuVertexInputRate |
| 84 | +{ |
| 85 | + Vertex = 0, |
| 86 | + Instance = 1, |
| 87 | +} |
| 88 | + |
| 89 | +internal enum GpuVertexElementFormat |
| 90 | +{ |
| 91 | + Float = 0, |
| 92 | + Float2 = 1, |
| 93 | + Float3 = 2, |
| 94 | + Float4 = 3, |
| 95 | +} |
| 96 | + |
| 97 | +internal readonly record struct GpuVertexBufferBinding( |
| 98 | + uint Slot, |
| 99 | + uint Pitch, |
| 100 | + GpuVertexInputRate InputRate = GpuVertexInputRate.Vertex, |
| 101 | + uint InstanceStepRate = 0); |
| 102 | + |
| 103 | +internal readonly record struct GpuVertexAttributeDescription( |
| 104 | + uint Location, |
| 105 | + uint BufferSlot, |
| 106 | + GpuVertexElementFormat Format, |
| 107 | + uint Offset); |
| 108 | + |
| 109 | +internal sealed class GpuVertexLayout : IEquatable<GpuVertexLayout> |
| 110 | +{ |
| 111 | + public static readonly GpuVertexLayout TEXTURED_VERTEX_2D = new( |
| 112 | + [new GpuVertexBufferBinding(0, (uint)Marshal.SizeOf<TexturedVertex2D>())], |
| 113 | + [ |
| 114 | + new GpuVertexAttributeDescription(0, 0, GpuVertexElementFormat.Float2, 0), |
| 115 | + new GpuVertexAttributeDescription(1, 0, GpuVertexElementFormat.Float2, 8), |
| 116 | + new GpuVertexAttributeDescription(2, 0, GpuVertexElementFormat.Float4, 16), |
| 117 | + ]); |
| 118 | + |
| 119 | + private readonly GpuVertexBufferBinding[] bufferBindings; |
| 120 | + private readonly GpuVertexAttributeDescription[] attributes; |
| 121 | + private readonly int cachedHashCode; |
| 122 | + |
| 123 | + public GpuVertexLayout( |
| 124 | + ReadOnlySpan<GpuVertexBufferBinding> bufferBindings, |
| 125 | + ReadOnlySpan<GpuVertexAttributeDescription> attributes) |
| 126 | + { |
| 127 | + this.bufferBindings = bufferBindings.ToArray(); |
| 128 | + this.attributes = attributes.ToArray(); |
| 129 | + cachedHashCode = computeHashCode(this.bufferBindings, this.attributes); |
| 130 | + } |
| 131 | + |
| 132 | + public ReadOnlySpan<GpuVertexBufferBinding> BufferBindings => bufferBindings; |
| 133 | + |
| 134 | + public ReadOnlySpan<GpuVertexAttributeDescription> Attributes => attributes; |
| 135 | + |
| 136 | + public static bool operator ==(GpuVertexLayout? left, GpuVertexLayout? right) => Equals(left, right); |
| 137 | + |
| 138 | + public static bool operator !=(GpuVertexLayout? left, GpuVertexLayout? right) => !Equals(left, right); |
| 139 | + |
| 140 | + public bool Equals(GpuVertexLayout? other) |
| 141 | + { |
| 142 | + if (ReferenceEquals(this, other)) |
| 143 | + return true; |
| 144 | + |
| 145 | + if (other is null || cachedHashCode != other.cachedHashCode) |
| 146 | + return false; |
| 147 | + |
| 148 | + return bufferBindings.AsSpan().SequenceEqual(other.bufferBindings) |
| 149 | + && attributes.AsSpan().SequenceEqual(other.attributes); |
| 150 | + } |
| 151 | + |
| 152 | + public override bool Equals(object? obj) => obj is GpuVertexLayout other && Equals(other); |
| 153 | + |
| 154 | + public override int GetHashCode() => cachedHashCode; |
| 155 | + |
| 156 | + private static int computeHashCode( |
| 157 | + ReadOnlySpan<GpuVertexBufferBinding> bindings, |
| 158 | + ReadOnlySpan<GpuVertexAttributeDescription> attrs) |
| 159 | + { |
| 160 | + var hash = new HashCode(); |
| 161 | + hash.Add(bindings.Length); |
| 162 | + |
| 163 | + foreach (var binding in bindings) |
| 164 | + hash.Add(binding); |
| 165 | + |
| 166 | + hash.Add(attrs.Length); |
| 167 | + |
| 168 | + foreach (var attr in attrs) |
| 169 | + hash.Add(attr); |
| 170 | + |
| 171 | + return hash.ToHashCode(); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +internal readonly record struct GpuShaderCreateDescription( |
| 176 | + GpuShaderStage Stage, |
| 177 | + GpuShaderFormats Format, |
| 178 | + string Entrypoint, |
| 179 | + ReadOnlyMemory<byte> Bytecode, |
| 180 | + uint NumSamplers, |
| 181 | + uint NumStorageTextures, |
| 182 | + uint NumStorageBuffers, |
| 183 | + uint NumUniformBuffers); |
| 184 | + |
| 185 | +internal readonly record struct GpuGraphicsPipelineDescription( |
| 186 | + nint VertexShader, |
| 187 | + nint FragmentShader, |
| 188 | + GpuVertexLayout VertexLayout, |
| 189 | + BlendingParameters Blending, |
| 190 | + GpuPrimitiveTopology Topology, |
| 191 | + GpuTextureFormat ColorTargetFormat); |
0 commit comments