Added Mesh class, removed attrib pointer management from Program, shifted to mesh. MUST use Program.UseDefault instead of GL.Use(0) to prevent crash, but meshes can't render with it anyway.
This commit is contained in:
@@ -8,7 +8,7 @@ using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace Diamond.Buffers
|
||||
{
|
||||
public class GLBuffer : GLObject
|
||||
public class GLBuffer<T> : GLObject where T : struct
|
||||
{
|
||||
public readonly BufferTarget Target;
|
||||
public readonly BufferUsageHint Usage;
|
||||
@@ -27,7 +27,7 @@ namespace Diamond.Buffers
|
||||
|
||||
protected override void Delete() => GL.DeleteBuffer(Id);
|
||||
|
||||
public void Data<T>(T[] data) where T : struct
|
||||
public void Data(T[] data)
|
||||
{
|
||||
var size = Marshal.SizeOf<T>();
|
||||
Bind();
|
||||
|
||||
@@ -45,42 +45,6 @@ namespace Diamond.Buffers
|
||||
yield return Array[Offset + i];
|
||||
}
|
||||
|
||||
public static T1[] Join<T1>(params SubArray<T1>[] subArrays) => Join((IEnumerable<SubArray<T1>>) subArrays);
|
||||
|
||||
public static T1[] Join<T1>(IEnumerable<SubArray<T1>> subArrays)
|
||||
{
|
||||
HashSet<T1[]> uniqueArrays = new HashSet<T1[]>();
|
||||
foreach (var subArray in subArrays)
|
||||
{
|
||||
uniqueArrays.Add(subArray.Array);
|
||||
}
|
||||
|
||||
if (uniqueArrays.Count == 0) return new T1[0];
|
||||
if (uniqueArrays.Count == 1) return uniqueArrays.ToArray()[0];
|
||||
|
||||
var length = 0;
|
||||
var offsets = new Dictionary<T1[], int>();
|
||||
foreach (var uniqueArray in uniqueArrays)
|
||||
{
|
||||
offsets[uniqueArray] = length;
|
||||
length += uniqueArray.Length;
|
||||
}
|
||||
|
||||
var array = new T1[length];
|
||||
foreach (var uniqueArray in uniqueArrays)
|
||||
{
|
||||
System.Array.ConstrainedCopy(uniqueArray, 0, array, offsets[uniqueArray], uniqueArray.Length);
|
||||
}
|
||||
|
||||
foreach (var subArray in subArrays)
|
||||
{
|
||||
subArray.Offset = offsets[subArray.Array];
|
||||
subArray.Array = array;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
public T[] ToArray()
|
||||
{
|
||||
var arr = new T[Length];
|
||||
@@ -96,4 +60,44 @@ namespace Diamond.Buffers
|
||||
return $"[{string.Join(", ", this)}]";
|
||||
}
|
||||
}
|
||||
|
||||
public static class SubArray
|
||||
{
|
||||
public static T[] Join<T>(params SubArray<T>[] subArrays) => Join((IEnumerable<SubArray<T>>)subArrays);
|
||||
|
||||
public static T[] Join<T>(IEnumerable<SubArray<T>> subArrays)
|
||||
{
|
||||
HashSet<T[]> uniqueArrays = new HashSet<T[]>();
|
||||
foreach (var subArray in subArrays)
|
||||
{
|
||||
uniqueArrays.Add(subArray.Array);
|
||||
}
|
||||
|
||||
if (uniqueArrays.Count == 0) return new T[0];
|
||||
if (uniqueArrays.Count == 1) return uniqueArrays.ToArray()[0];
|
||||
|
||||
var length = 0;
|
||||
var offsets = new Dictionary<T[], int>();
|
||||
foreach (var uniqueArray in uniqueArrays)
|
||||
{
|
||||
offsets[uniqueArray] = length;
|
||||
length += uniqueArray.Length;
|
||||
}
|
||||
|
||||
var array = new T[length];
|
||||
foreach (var uniqueArray in uniqueArrays)
|
||||
{
|
||||
System.Array.ConstrainedCopy(uniqueArray, 0, array, offsets[uniqueArray], uniqueArray.Length);
|
||||
}
|
||||
|
||||
foreach (var subArray in subArrays)
|
||||
{
|
||||
subArray.Offset = offsets[subArray.Array];
|
||||
subArray.Array = array;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ namespace Diamond.Buffers
|
||||
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
|
||||
public sealed class VertexDataAttribute : Attribute
|
||||
{
|
||||
public int Divisor { get; set; } = 0;
|
||||
|
||||
public VertexDataAttribute()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Runtime.InteropServices;
|
||||
using Diamond.Shaders;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace Diamond.Buffers
|
||||
@@ -32,12 +36,6 @@ namespace Diamond.Buffers
|
||||
/// </summary>
|
||||
public bool Normalized { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// The divisor to Instanced draw calls
|
||||
/// Corresponds to the <code>divisor</code> parameter to <code>glVertexAttribDivisor</code>
|
||||
/// </summary>
|
||||
public int Divisor { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The offset of this attribute within each element
|
||||
/// Corresponds to the <code>offset</code> parameter to <code>glVertexAttribPointer</code>
|
||||
@@ -50,4 +48,85 @@ namespace Diamond.Buffers
|
||||
Size = size;
|
||||
}
|
||||
}
|
||||
|
||||
public class VertexDataInfo
|
||||
{
|
||||
public readonly IReadOnlyCollection<VertexPointerAttribute> Pointers;
|
||||
public readonly int Stride;
|
||||
public readonly int Divisor;
|
||||
|
||||
private VertexDataInfo(IList<VertexPointerAttribute> pointers, int stride, int divisor)
|
||||
{
|
||||
Pointers = new ReadOnlyCollection<VertexPointerAttribute>(pointers);
|
||||
Stride = stride;
|
||||
Divisor = divisor;
|
||||
}
|
||||
|
||||
public void EnableVertexPointers()
|
||||
{
|
||||
if (Program.Current == null)
|
||||
{
|
||||
throw new Exception("Cant render a mesh with no active shader."); // todo make an exception here.
|
||||
}
|
||||
|
||||
foreach (var attr in Pointers)
|
||||
{
|
||||
if (!Program.Current.TryGetAttribute(attr.Name, out int loc)) continue;
|
||||
GL.EnableVertexAttribArray(loc);
|
||||
GL.VertexAttribDivisor(loc, Divisor);
|
||||
}
|
||||
}
|
||||
|
||||
public void DisableVertexPointers()
|
||||
{
|
||||
if (Program.Current == null)
|
||||
{
|
||||
throw new Exception("Cant render a mesh with no active shader."); // todo make an exception here.
|
||||
}
|
||||
|
||||
foreach (var attr in Pointers)
|
||||
{
|
||||
if (!Program.Current.TryGetAttribute(attr.Name, out int loc)) continue;
|
||||
GL.DisableVertexAttribArray(loc);
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<Type, VertexDataInfo> attribCache =
|
||||
new Dictionary<Type, VertexDataInfo>();
|
||||
|
||||
public static VertexDataInfo GetInfo<T>() where T : struct
|
||||
{
|
||||
if (attribCache.ContainsKey(typeof(T))) return attribCache[typeof(T)];
|
||||
|
||||
var vertexDataAttributes = typeof(T).GetCustomAttributes(typeof(VertexDataAttribute), false);
|
||||
|
||||
if (vertexDataAttributes.Length != 1)
|
||||
{
|
||||
throw new Exception("Can't use type {typeof(T)} as mesh data."); // todo make an exception here.
|
||||
}
|
||||
|
||||
var vertdataattrib = (VertexDataAttribute) vertexDataAttributes[0];
|
||||
var divisor = vertdataattrib.Divisor;
|
||||
|
||||
|
||||
var attribList = new List<VertexPointerAttribute>();
|
||||
var stride = Marshal.SizeOf<T>();
|
||||
|
||||
foreach (var fieldInfo in typeof(T).GetFields())
|
||||
{
|
||||
var attrs = fieldInfo.GetCustomAttributes(typeof(VertexPointerAttribute), false);
|
||||
if (attrs.Length == 0) continue;
|
||||
|
||||
var offset = (int) Marshal.OffsetOf<T>(fieldInfo.Name);
|
||||
foreach (var attr in attrs)
|
||||
{
|
||||
var vpa = (VertexPointerAttribute) attr;
|
||||
vpa.Offset = offset;
|
||||
attribList.Add(vpa);
|
||||
}
|
||||
}
|
||||
|
||||
return new VertexDataInfo(attribList, stride, divisor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ namespace Diamond.Shaders
|
||||
{
|
||||
public class Program : GLObject
|
||||
{
|
||||
public static Program Current { get; private set; }
|
||||
|
||||
private Dictionary<string, int> uniforms = new Dictionary<string, int>();
|
||||
private Dictionary<string, int> attributes = new Dictionary<string, int>();
|
||||
|
||||
@@ -84,57 +86,42 @@ namespace Diamond.Shaders
|
||||
return id;
|
||||
}
|
||||
|
||||
public void SetAttribPointers(GLBuffer buff, Type vertexType)
|
||||
public void SetAttribPointers<T>(GLBuffer<T> buff) where T : struct
|
||||
{
|
||||
if (vertexType.GetCustomAttributes(typeof(VertexDataAttribute), false).Length == 0)
|
||||
{
|
||||
throw new ShaderException($"Cannot attach buffer {buff} to program {this}" +
|
||||
$" with vertex {vertexType} because it has no" +
|
||||
$" VertexData attribute.");
|
||||
}
|
||||
|
||||
var attribList = new List<VertexPointerAttribute>();
|
||||
var Stride = Marshal.SizeOf(vertexType);
|
||||
|
||||
foreach (var fieldInfo in vertexType.GetFields())
|
||||
{
|
||||
var attrs = fieldInfo.GetCustomAttributes(typeof(VertexPointerAttribute), false);
|
||||
if (attrs.Length == 0) continue;
|
||||
|
||||
var offset = (int) Marshal.OffsetOf(vertexType, fieldInfo.Name);
|
||||
foreach (var attr in attrs)
|
||||
{
|
||||
var vpa = (VertexPointerAttribute) attr;
|
||||
vpa.Offset = offset;
|
||||
attribList.Add(vpa);
|
||||
}
|
||||
}
|
||||
var vdi = VertexDataInfo.GetInfo<T>();
|
||||
|
||||
buff.Bind();
|
||||
foreach (var attr in attribList)
|
||||
foreach (var attr in vdi.Pointers)
|
||||
{
|
||||
if (!TryGetAttribute(attr.Name, out int loc)) continue;
|
||||
GL.VertexAttribPointer(loc, attr.Size, attr.Type, attr.Normalized, Stride, attr.Offset);
|
||||
GL.VertexAttribDivisor(loc, attr.Divisor);
|
||||
GL.VertexAttribPointer(loc, attr.Size, attr.Type, attr.Normalized, vdi.Stride, attr.Offset);
|
||||
}
|
||||
}
|
||||
|
||||
public void EnableAllAttribArrays()
|
||||
// public void EnableAllAttribArrays()
|
||||
// {
|
||||
// foreach (var loc in attributes.Values)
|
||||
// GL.EnableVertexAttribArray(loc);
|
||||
// }
|
||||
//
|
||||
// public void DisableAllAttribArrays()
|
||||
// {
|
||||
// foreach (var loc in attributes.Values)
|
||||
// GL.DisableVertexAttribArray(loc);
|
||||
// }
|
||||
|
||||
public void Use()
|
||||
{
|
||||
foreach (var loc in attributes.Values)
|
||||
GL.EnableVertexAttribArray(loc);
|
||||
GL.UseProgram(Id);
|
||||
Current = this;
|
||||
}
|
||||
|
||||
public void DisableAllAttribArrays()
|
||||
public static void UseDefault()
|
||||
{
|
||||
foreach (var loc in attributes.Values)
|
||||
GL.DisableVertexAttribArray(loc);
|
||||
GL.UseProgram(0);
|
||||
Current = null;
|
||||
}
|
||||
|
||||
public void Use() => GL.UseProgram(Id);
|
||||
|
||||
public static void UseDefault() => GL.UseProgram(0);
|
||||
|
||||
public static Program FromShaders(params Shader[] shaders)
|
||||
{
|
||||
var p = new Program();
|
||||
|
||||
Reference in New Issue
Block a user