From 143cd8f856f9d7e3053361f73d0a0ccc070a3f65 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 23 Oct 2023 18:58:12 +0100 Subject: [PATCH] Add support for signed and unsigned integer vertex attributes. --- OpenRA.Game/Graphics/ModelVertex.cs | 6 +++--- OpenRA.Game/Graphics/RenderPostProcessPassVertex.cs | 2 +- OpenRA.Game/Graphics/ShaderBindings.cs | 13 ++++++++++++- OpenRA.Game/Graphics/Vertex.cs | 8 ++++---- OpenRA.Platforms.Default/OpenGL.cs | 5 +++++ OpenRA.Platforms.Default/Shader.cs | 6 +++++- 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/OpenRA.Game/Graphics/ModelVertex.cs b/OpenRA.Game/Graphics/ModelVertex.cs index b4f591d184..42ffbf6e6d 100644 --- a/OpenRA.Game/Graphics/ModelVertex.cs +++ b/OpenRA.Game/Graphics/ModelVertex.cs @@ -45,9 +45,9 @@ namespace OpenRA.Graphics public override ShaderVertexAttribute[] Attributes { get; } = new[] { - new ShaderVertexAttribute("aVertexPosition", 3, 0), - new ShaderVertexAttribute("aVertexTexCoord", 4, 12), - new ShaderVertexAttribute("aVertexTexMetadata", 2, 28), + new ShaderVertexAttribute("aVertexPosition", ShaderVertexAttributeType.Float, 3, 0), + new ShaderVertexAttribute("aVertexTexCoord", ShaderVertexAttributeType.Float, 4, 12), + new ShaderVertexAttribute("aVertexTexMetadata", ShaderVertexAttributeType.Float, 2, 28), }; } } diff --git a/OpenRA.Game/Graphics/RenderPostProcessPassVertex.cs b/OpenRA.Game/Graphics/RenderPostProcessPassVertex.cs index 3e61dd3b3d..cd00106c30 100644 --- a/OpenRA.Game/Graphics/RenderPostProcessPassVertex.cs +++ b/OpenRA.Game/Graphics/RenderPostProcessPassVertex.cs @@ -31,7 +31,7 @@ namespace OpenRA.Graphics public override ShaderVertexAttribute[] Attributes { get; } = new[] { - new ShaderVertexAttribute("aVertexPosition", 2, 0) + new ShaderVertexAttribute("aVertexPosition", ShaderVertexAttributeType.Float, 2, 0) }; } } diff --git a/OpenRA.Game/Graphics/ShaderBindings.cs b/OpenRA.Game/Graphics/ShaderBindings.cs index eff4531c8a..6e11b3a63d 100644 --- a/OpenRA.Game/Graphics/ShaderBindings.cs +++ b/OpenRA.Game/Graphics/ShaderBindings.cs @@ -14,15 +14,26 @@ using System.Linq; namespace OpenRA.Graphics { + public enum ShaderVertexAttributeType + { + // Assign the underlying OpenGL type values + // to simplify enum use in the shader + Float = 0x1406, // GL_FLOAT + Int = 0x1404, // GL_INT + UInt = 0x1405 // GL_UNSIGNED_INT + } + public readonly struct ShaderVertexAttribute { public readonly string Name; + public readonly ShaderVertexAttributeType Type; public readonly int Components; public readonly int Offset; - public ShaderVertexAttribute(string name, int components, int offset) + public ShaderVertexAttribute(string name, ShaderVertexAttributeType type, int components, int offset) { Name = name; + Type = type; Components = components; Offset = offset; } diff --git a/OpenRA.Game/Graphics/Vertex.cs b/OpenRA.Game/Graphics/Vertex.cs index c84163e343..a7c726ffbe 100644 --- a/OpenRA.Game/Graphics/Vertex.cs +++ b/OpenRA.Game/Graphics/Vertex.cs @@ -55,10 +55,10 @@ namespace OpenRA.Graphics public override ShaderVertexAttribute[] Attributes { get; } = new[] { - new ShaderVertexAttribute("aVertexPosition", 3, 0), - new ShaderVertexAttribute("aVertexTexCoord", 4, 12), - new ShaderVertexAttribute("aVertexTexMetadata", 2, 28), - new ShaderVertexAttribute("aVertexTint", 4, 36) + new ShaderVertexAttribute("aVertexPosition", ShaderVertexAttributeType.Float, 3, 0), + new ShaderVertexAttribute("aVertexTexCoord", ShaderVertexAttributeType.Float, 4, 12), + new ShaderVertexAttribute("aVertexTexMetadata", ShaderVertexAttributeType.Float, 2, 28), + new ShaderVertexAttribute("aVertexTint", ShaderVertexAttributeType.Float, 4, 36) }; } } diff --git a/OpenRA.Platforms.Default/OpenGL.cs b/OpenRA.Platforms.Default/OpenGL.cs index 95e4c769fa..8b1e318a7d 100644 --- a/OpenRA.Platforms.Default/OpenGL.cs +++ b/OpenRA.Platforms.Default/OpenGL.cs @@ -386,6 +386,10 @@ namespace OpenRA.Platforms.Default int stride, IntPtr pointer); public static VertexAttribPointer glVertexAttribPointer { get; private set; } + public delegate void VertexAttribIPointer(int index, int size, int type, + int stride, IntPtr pointer); + public static VertexAttribIPointer glVertexAttribIPointer { get; private set; } + public delegate void EnableVertexAttribArray(int index); public static EnableVertexAttribArray glEnableVertexAttribArray { get; private set; } @@ -595,6 +599,7 @@ namespace OpenRA.Platforms.Default glDeleteBuffers = Bind("glDeleteBuffers"); glBindAttribLocation = Bind("glBindAttribLocation"); glVertexAttribPointer = Bind("glVertexAttribPointer"); + glVertexAttribIPointer = Bind("glVertexAttribIPointer"); glEnableVertexAttribArray = Bind("glEnableVertexAttribArray"); glDisableVertexAttribArray = Bind("glDisableVertexAttribArray"); glDrawArrays = Bind("glDrawArrays"); diff --git a/OpenRA.Platforms.Default/Shader.cs b/OpenRA.Platforms.Default/Shader.cs index a832ae2cc7..4e0c0223aa 100644 --- a/OpenRA.Platforms.Default/Shader.cs +++ b/OpenRA.Platforms.Default/Shader.cs @@ -13,6 +13,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; +using OpenRA.Graphics; namespace OpenRA.Platforms.Default { @@ -135,7 +136,10 @@ namespace OpenRA.Platforms.Default for (ushort i = 0; i < bindings.Attributes.Length; i++) { var attribute = bindings.Attributes[i]; - OpenGL.glVertexAttribPointer(i, attribute.Components, OpenGL.GL_FLOAT, false, bindings.Stride, new IntPtr(attribute.Offset)); + if (attribute.Type == ShaderVertexAttributeType.Float) + OpenGL.glVertexAttribPointer(i, attribute.Components, OpenGL.GL_FLOAT, false, bindings.Stride, new IntPtr(attribute.Offset)); + else + OpenGL.glVertexAttribIPointer(i, attribute.Components, (int)attribute.Type, bindings.Stride, new IntPtr(attribute.Offset)); OpenGL.CheckGLError(); } }