3b20c53088
- Refactored DirectRendering class to support new IDirectRendering interface and manage textures directly. - Enhanced ImmediateMode to utilize ImmediateVertex struct for immediate rendering calls. - Updated Image class to use IDirectRendering for texture creation. - Modified test application to demonstrate immediate mode rendering with new structures. - Removed obsolete code related to previous rendering abstractions.
114 lines
3.8 KiB
C#
114 lines
3.8 KiB
C#
using System.Drawing;
|
|
using System.Numerics;
|
|
using Dashboard.Drawing;
|
|
using Dashboard.Pal;
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
namespace Dashboard.OpenGL.Drawing
|
|
{
|
|
public class DirectRendering : IDirectRendering, ITextureExtension
|
|
{
|
|
public GLDeviceContext Context { get; private set; } = null!;
|
|
public string DriverName { get; } = "Dashboard OpenGL";
|
|
public string DriverVendor { get; } = "Dashboard";
|
|
public Version DriverVersion { get; } = new Version(0, 1);
|
|
IContextBase IContextExtensionBase.Context => Context;
|
|
DeviceContext IContextExtensionBase<DeviceContext>.Context => Context;
|
|
|
|
public bool SupportsArbTextureStorage { get; private set; }
|
|
public bool SupportsAnisotropy { get; private set; }
|
|
|
|
private int _vao = -1;
|
|
private List<GLTexture> _textures = new List<GLTexture>();
|
|
|
|
|
|
public void Dispose()
|
|
{
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
public void Require(DeviceContext context)
|
|
{
|
|
Context = (GLDeviceContext)context;
|
|
|
|
SupportsArbTextureStorage = Context.DriverVersion >= new Version(4, 2) ||
|
|
Context.IsGLExtensionAvailable("GL_ARB_texture_storage");
|
|
SupportsAnisotropy = Context.DriverVersion >= new Version() ||
|
|
Context.IsGLExtensionAvailable("GL_EXT_texture_filter_anisotropic") ||
|
|
Context.IsGLExtensionAvailable("GL_ARB_texture_filter_anisotropic");
|
|
}
|
|
|
|
public void Require(IContextBase context) => Require((DeviceContext)context);
|
|
|
|
public void Begin()
|
|
{
|
|
if (_vao == -1)
|
|
{
|
|
GL.GenVertexArray(out _vao);
|
|
}
|
|
}
|
|
|
|
public void End()
|
|
{
|
|
|
|
}
|
|
|
|
public IShader CreateShader<T>(T createInfo) where T : IShaderCreateInfo
|
|
{
|
|
int program = createInfo switch
|
|
{
|
|
GlslShaderCreateInfo glsl => glsl.CreateProgram(),
|
|
GlslComputeShaderCreateInfo compute => compute.CreateProgram(),
|
|
SpirvShaderCreateInfo spirv => spirv.CreateProgram(),
|
|
_ => throw new Exception($"Unsupported shader pipeline type {createInfo.GetType()}."),
|
|
};
|
|
|
|
return new GLShader((GLDeviceContext)Context, program);
|
|
}
|
|
|
|
public IBuffer CreateBuffer(BufferAccessPattern pattern, long size)
|
|
{
|
|
return GLBuffer.Create((GLDeviceContext)Context, pattern, size);
|
|
}
|
|
|
|
public void Draw(DrawCall drawCall)
|
|
{
|
|
Vector2 size = Context.FramebufferSize;
|
|
drawCall.SetAll(size, _vao, false);
|
|
|
|
if (drawCall.VertexSpecification.ElementBuffer is null)
|
|
{
|
|
GL.DrawArrays(drawCall.Primitive.OpenGL, drawCall.First, drawCall.Count);
|
|
}
|
|
else if (drawCall.BaseVertex == 0)
|
|
{
|
|
GL.DrawElements(drawCall.Primitive.OpenGL, drawCall.Count, drawCall.IndexType.OpenGL, (nint)drawCall.Offset);
|
|
}
|
|
else
|
|
{
|
|
GL.DrawElementsBaseVertex(drawCall.Primitive.OpenGL, drawCall.Count, drawCall.IndexType.OpenGL, (nint)drawCall.Offset, drawCall.BaseVertex);
|
|
|
|
}
|
|
}
|
|
|
|
public GLTexture CreateTexture(TextureType type)
|
|
{
|
|
GLTexture texture = new GLTexture(this, type);
|
|
lock (_textures) _textures.Add(texture);
|
|
return texture;
|
|
}
|
|
|
|
internal void TextureDisposed(GLTexture texture)
|
|
{
|
|
lock (_textures) _textures.Remove(texture);
|
|
}
|
|
|
|
ITexture ITextureExtension.CreateTexture(TextureType type) => CreateTexture(type);
|
|
|
|
ITexture IDirectRendering.CreateTexture(TextureType type)
|
|
{
|
|
return CreateTexture(type);
|
|
}
|
|
}
|
|
}
|