This commit is contained in:
2023-02-28 20:58:31 +01:00
commit f3ce543614
41 changed files with 4757 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
namespace DaggerFramework.Rendering
{
public class ColorRectShader : Shader
{
public override string FragmentSource => @"
#version 450
layout(location = 0) out vec4 fsout_Color;
layout (set = 0, binding = 0) uniform Uniforms
{
vec4 color;
};
void main()
{
fsout_Color = color;
}
";
public override string VertexSource => @"
#version 450
layout(location = 0) in vec2 Position;
// uniform MatrixBlock
// {
// // mat4 model = mat4();
// // mat4 view;
// // mat4 proj;
// mat4 transform = mat4(1.0, 1.0, 1.0, 1.0,
// 1.0, 1.0, 1.0, 1.0,
// 1.0, 1.0, 1.0, 1.0,
// 1.0, 1.0, 1.0, 1.0);
// };
// mat4 transform = mat4(1.0, 1.0, 0.0, 1.0,
// 0.0, 1.0, 1.0, 1.0,
// 0.0, 0.0, 1.0, 1.0,
// 0.0, 0.0, 0.0, 1.0);
mat4 transform = mat4(1.0);
void main()
{
// gl_Position = proj * view * model * vec4(Position, 0, 1);
// gl_Position = vec4(Position, 0, 1);
gl_Position = transform * vec4(Position, 0, 1);
}";
}
}

View File

@@ -0,0 +1,160 @@
using System.Numerics;
using System.Text;
using Raylib_cs;
namespace DaggerFramework.Rendering
{
public class RaylibRenderer : Renderer
{
public override Vector2 WindowSize => _windowSize;
public override void DrawCircle(float radius, System.Drawing.Color color)
{
Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, SystemColorToRaylibColor(color));
}
public override void SetTransform(Vector2 position, float rotation)
{
_position = position;
_rotation = rotation;
}
public override void CreateWindow(string title, Vector2 size)
{
Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
_windowSize = size;
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, title);
}
public override void SetWindowTitle(string title)
{
Raylib.SetWindowTitle(title);
}
public override void SetWindowVSync(bool value)
{
}
public override void SetTargetFps(int fps)
{
Raylib.SetTargetFPS(fps);
}
public override bool WindowShouldClose()
{
return Raylib.WindowShouldClose();
}
public override void CloseWindow()
{
Raylib.CloseWindow();
}
public override void BeginFrame()
{
Raylib.BeginDrawing();
}
public override void EndFrame()
{
Raylib.EndDrawing();
}
public override void ClearBackground(System.Drawing.Color color)
{
Raylib.ClearBackground(SystemColorToRaylibColor(color));
}
public override double GetFrameTime()
{
return (double)Raylib.GetFrameTime();
}
private Raylib_cs.Color SystemColorToRaylibColor(System.Drawing.Color color)
{
return new Color { r = color.R, g = color.G, b = color.B, a = color.A };
}
public override int LoadTexture(Texture2d texture)
{
Image image;
Texture2D rayTexture;
string ext = "png";
byte[] extBytes = Encoding.ASCII.GetBytes(ext);
// TODO: This can cause memory leaks.
unsafe
{
fixed (byte* textureData = texture.Data)
{
fixed (byte* bExt = extBytes)
{
sbyte* sbExt = (sbyte*)*bExt;
image = Raylib.LoadImageFromMemory(sbExt, textureData, texture.Data.Length);
}
}
}
rayTexture = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
_texturePool.Add(rayTexture);
return _texturePool.Count - 1;
}
public override int LoadTexture(string path)
{
Image image = Raylib.LoadImage(path);
var rayTexture = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
_texturePool.Add(rayTexture);
return _texturePool.Count - 1;
}
public override void DrawTexture(int id, System.Drawing.Color tint)
{
Raylib.DrawTextureV(_texturePool[id], _position, SystemColorToRaylibColor(tint));
}
public override void DrawRectangle(Vector2 size, System.Drawing.Color color)
{
// Raylib.DrawRectangleV(_position, size, SystemColorToRaylibColor(color));
Raylib.DrawRectanglePro(new Rectangle()
{
x = _position.X,
y = _position.Y,
width = size.X,
height = size.Y
}, Vector2.Zero, _rotation, SystemColorToRaylibColor(color));
}
public override void DrawDebugText(Vector2 position, string text, int fontSize, System.Drawing.Color color)
{
Raylib.DrawText(text, (int)position.X, (int)position.Y, fontSize, SystemColorToRaylibColor(color));
}
public override void Initialize(RendererSettings settings)
{
ConfigFlags flags = 0;
// MSAA
flags |= settings.Msaa == Msaa.Msaa4x ? ConfigFlags.FLAG_MSAA_4X_HINT : 0;
// VSync
flags |= settings.UseVSync ? ConfigFlags.FLAG_VSYNC_HINT : 0;
// Fullscreen
flags |= settings.Fullscreen ? ConfigFlags.FLAG_FULLSCREEN_MODE : 0;
Raylib.SetConfigFlags(flags);
}
// TODO
public override void SetTransform(Matrix4x4 transform) { }
private List<Texture2D> _texturePool = new List<Texture2D>();
private Vector2 _position;
private float _rotation;
private Vector2 _windowSize;
}
}

52
Source/Rendering/Renderer.cs Executable file
View File

@@ -0,0 +1,52 @@
using System.Drawing;
using System.Numerics;
namespace DaggerFramework.Rendering
{
public abstract class Renderer
{
// INIT
public abstract void Initialize(RendererSettings settings);
// WINDOW
public abstract Vector2 WindowSize { get; }
public abstract void CreateWindow(string title, Vector2 size);
public abstract void SetWindowTitle(string title);
public abstract void SetWindowVSync(bool value);
public abstract void SetTargetFps(int fps);
public abstract bool WindowShouldClose();
public abstract void CloseWindow();
// DRAWING
public abstract void BeginFrame();
public abstract void EndFrame();
public abstract void ClearBackground(Color color);
public abstract double GetFrameTime();
public abstract int LoadTexture(Texture2d texture);
/// <summary>
/// Temporary workaround for Raylib's LoadImageFromMemory.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public abstract int LoadTexture(string path);
public abstract void SetTransform(Vector2 position, float rotation = 0.0f);
public abstract void SetTransform(Matrix4x4 transform);
public abstract void DrawCircle(float radius, Color color);
public abstract void DrawRectangle(Vector2 size, Color color);
public abstract void DrawDebugText(Vector2 position, string text, int fontSize, Color color);
public abstract void DrawTexture(int id, Color tint);
}
public enum Msaa
{
None,
Msaa2x,
Msaa4x,
Msaa8x
}
public struct RendererSettings
{
public Msaa Msaa;
public bool UseVSync;
public bool Fullscreen;
}
}

8
Source/Rendering/Shader.cs Executable file
View File

@@ -0,0 +1,8 @@
namespace DaggerFramework.Rendering
{
public abstract class Shader
{
public abstract string FragmentSource { get; }
public abstract string VertexSource { get; }
}
}

View File

@@ -0,0 +1,227 @@
using System.Drawing;
using System.Numerics;
using System.Text;
// using Veldrid;
// using Veldrid.Sdl2;
// using Veldrid.SPIRV;
// using Veldrid.StartupUtilities;
namespace DaggerFramework.Rendering
{
// public class StandardRenderer : Renderer
// {
// public override void BeginFrame()
// {
// _commandList.Begin();
// _commandList.SetFramebuffer(_graphicsDevice.SwapchainFramebuffer);
// }
// public override void ClearBackground(Color color)
// {
// _commandList.ClearColorTarget(0, SystemColorToRgbaFloat(color));
// }
// public override void CloseWindow()
// {
// _window.Close();
// }
// public override void CreateWindow(string title, Vector2 size)
// {
// var windowCreateInfo = new WindowCreateInfo()
// {
// // TODO: add position property to CreateWindow.
// X = (int)size.X / 2,
// Y = (int)size.Y / 2,
// WindowWidth = (int)size.X,
// WindowHeight = (int)size.Y,
// WindowTitle = title,
// };
// _window = VeldridStartup.CreateWindow(ref windowCreateInfo);
// }
// public override void DrawCircle(float radius, Color color)
// {
// throw new NotImplementedException();
// }
// public override void DrawDebugText(Vector2 position, string text, int fontSize, Color color)
// {
// throw new NotImplementedException();
// }
// public override void DrawRectangle(Vector2 size, Color color)
// {
// _commandList.SetVertexBuffer(0, _vertexBuffer);
// _commandList.SetIndexBuffer(_indexBuffer, IndexFormat.UInt16);
// _commandList.SetPipeline(_graphicsPipeline);
// _commandList.SetGraphicsResourceSet(0, _resourceSet);
// _graphicsDevice.UpdateBuffer(_uniformBuffer, 0, SystemColorToRgbaFloat(color));
// _commandList.DrawIndexed(indexCount: 4, instanceCount: 1, 0, 0, 0);
// }
// public override void DrawTexture(int id, Color tint)
// {
// throw new NotImplementedException();
// }
// public override void EndFrame()
// {
// _commandList.End();
// _graphicsDevice.SubmitCommands(_commandList);
// _graphicsDevice.SwapBuffers();
// }
// public override double GetFrameTime()
// {
// return 0.16f;
// }
// private void CreateResources()
// {
// _resourceFactory = _graphicsDevice.ResourceFactory;
// _commandList = _resourceFactory.CreateCommandList();
// _vertexBuffer = _resourceFactory.CreateBuffer(new BufferDescription(4 * 8, BufferUsage.VertexBuffer));
// _indexBuffer = _resourceFactory.CreateBuffer(new BufferDescription(4 * sizeof(ushort), BufferUsage.IndexBuffer));
// _uniformBuffer = _resourceFactory.CreateBuffer(new BufferDescription(4 * sizeof(float), BufferUsage.UniformBuffer));
// FillBuffers();
// var vertexDesc = new ShaderDescription(ShaderStages.Vertex, Encoding.UTF8.GetBytes(_colorRectShader.VertexSource), "main");
// var fragmentDesc = new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(_colorRectShader.FragmentSource), "main");
// _shaders = _resourceFactory.CreateFromSpirv(vertexDesc, fragmentDesc);
// CreatePipeline();
// }
// private void CreatePipeline()
// {
// VertexLayoutDescription vertexLayout = new VertexLayoutDescription(
// new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2));
// var pipelineDesc = new GraphicsPipelineDescription();
// pipelineDesc.BlendState = BlendStateDescription.SingleOverrideBlend;
// pipelineDesc.RasterizerState = new RasterizerStateDescription(
// cullMode: FaceCullMode.Back,
// fillMode: PolygonFillMode.Solid,
// frontFace: FrontFace.Clockwise,
// depthClipEnabled: true,
// scissorTestEnabled: false);
// pipelineDesc.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
// var layout = _resourceFactory.CreateResourceLayout(new ResourceLayoutDescription(
// new ResourceLayoutElementDescription("color", ResourceKind.UniformBuffer, ShaderStages.Fragment)
// ));
// _resourceSet = _resourceFactory.CreateResourceSet(new ResourceSetDescription(layout, _uniformBuffer));
// pipelineDesc.ResourceLayouts = new ResourceLayout[] { layout };
// pipelineDesc.ShaderSet = new ShaderSetDescription(vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, _shaders);
// pipelineDesc.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription;
// _graphicsPipeline = _resourceFactory.CreateGraphicsPipeline(pipelineDesc);
// }
// private void FillBuffers()
// {
// _graphicsDevice.UpdateBuffer(_vertexBuffer, 0, _quadVertices);
// _graphicsDevice.UpdateBuffer(_indexBuffer, 0, _quadIndices);
// }
// public override void Initialize(RendererSettings settings)
// {
// _settings = settings;
// _graphicsDevice = VeldridStartup.CreateGraphicsDevice(_window, new GraphicsDeviceOptions
// {
// PreferStandardClipSpaceYDirection = true,
// PreferDepthRangeZeroToOne = true
// });
// CreateResources();
// }
// public override int LoadTexture(Texture2d texture)
// {
// throw new NotImplementedException();
// }
// public override int LoadTexture(string path)
// {
// // throw new NotImplementedException();
// return -1;
// }
// public override void SetTargetFps(int fps)
// {
// _window.PollIntervalInMs = 1 / fps;
// }
// public override void SetTransform(Vector2 position)
// {
// // throw new NotImplementedException();
// }
// public override void SetWindowTitle(string title)
// {
// throw new NotImplementedException();
// }
// public override void SetWindowVSync(bool value)
// {
// throw new NotImplementedException();
// }
// public override bool WindowShouldClose()
// {
// _window.PumpEvents();
// return !_window.Exists;
// }
// private RgbaFloat SystemColorToRgbaFloat(System.Drawing.Color color)
// {
// return new RgbaFloat(color.R, color.G, color.B, color.A);
// }
// public override void SetTransform(Matrix4x4 transform)
// {
// _transform = transform;
// }
// // Transform
// private Matrix4x4 _transform;
// // TEMP: quad stuff.
// private readonly Vector2[] _quadVertices =
// {
// new Vector2(-0.5f, 0.5f),
// new Vector2(0.5f, 0.5f),
// new Vector2(-0.5f, -0.5f),
// new Vector2(0.5f, -0.5f)
// };
// private readonly ushort[] _quadIndices = { 0, 1, 2, 3 };
// private ColorRectShader _colorRectShader = new ColorRectShader();
// private Sdl2Window _window;
// private RendererSettings _settings;
// private GraphicsDevice _graphicsDevice;
// // Resources
// private ResourceFactory _resourceFactory;
// private CommandList _commandList;
// private Pipeline _graphicsPipeline;
// private Veldrid.Shader[] _shaders;
// // Quad resources
// private DeviceBuffer _vertexBuffer;
// private DeviceBuffer _indexBuffer;
// private DeviceBuffer _uniformBuffer;
// private ResourceSet _resourceSet;
// }
}