Clyde correct sRGB support.

This makes light stacking look less... awful.

Also minor cleanup.
This commit is contained in:
Pieter-Jan Briers
2019-05-08 14:37:48 +02:00
parent 0340cecd6d
commit 6da21ab6c1
12 changed files with 28 additions and 169 deletions

View File

@@ -1,7 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Graphics.OpenGL;
using Robust.Shared.Utility;
namespace Robust.Client.Graphics.Clyde

View File

@@ -1,7 +1,7 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Graphics.OpenGL;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Map;
using Robust.Shared.Maths;

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Graphics.OpenGL;
using Robust.Client.GameObjects;
using Robust.Client.Graphics.ClientEye;
using Robust.Client.Graphics.Drawing;
@@ -287,6 +287,8 @@ namespace Robust.Client.Graphics.Clyde
return;
}
GL.Disable(EnableCap.FramebufferSrgb);
void DrawLight(Vector2 pos, float range, float power, Color color)
{
_lightShader.SetUniform("lightCenter", pos);
@@ -304,7 +306,8 @@ namespace Robust.Client.Graphics.Clyde
GL.BindFramebuffer(FramebufferTarget.Framebuffer, LightFBO.Handle);
var complete = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
GL.ClearColor(0.1f, 0.1f, 0.1f, 1);
var converted = Color.FromSrgb(new Color(0.1f, 0.1f, 0.1f));
GL.ClearColor(converted.R, converted.G, converted.B, 1);
GL.Clear(ClearBufferMask.ColorBufferBit);
var (lightW, lightH) = _lightMapSize();
@@ -330,6 +333,8 @@ namespace Robust.Client.Graphics.Clyde
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
GL.Viewport(0, 0, _window.Width, _window.Height);
GL.Enable(EnableCap.FramebufferSrgb);
_lightingReady = true;
}

View File

@@ -1,4 +1,4 @@
using OpenTK.Graphics.OpenGL4;
using OpenTK.Graphics.OpenGL;
namespace Robust.Client.Graphics.Clyde
{

View File

@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Graphics.OpenGL;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
@@ -176,15 +176,18 @@ namespace Robust.Client.Graphics.Clyde
}
}
public void SetUniform(string uniformName, in Color color)
public void SetUniform(string uniformName, in Color color, bool convertToLinear=true)
{
var uniformId = GetUniform(uniformName);
var converted = color;
if (convertToLinear)
{
converted = Color.FromSrgb(color);
}
unsafe
{
fixed (Color* ptr = &color)
{
GL.Uniform4(uniformId, 1, (float*) ptr);
}
GL.Uniform4(uniformId, 1, (float*) &converted);
}
}

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Graphics.OpenGL;
using Robust.Client.Graphics.Shaders;
using Robust.Client.ResourceManagement.ResourceTypes;
using Robust.Shared.Utility;

View File

@@ -2,13 +2,13 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Graphics.OpenGL;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
using OGLTextureWrapMode = OpenTK.Graphics.OpenGL4.TextureWrapMode;
using OGLTextureWrapMode = OpenTK.Graphics.OpenGL.TextureWrapMode;
namespace Robust.Client.Graphics.Clyde
{
@@ -83,7 +83,7 @@ namespace Robust.Client.Graphics.Clyde
if (pixelType == typeof(Rgba32))
{
internalFormat = PixelInternalFormat.Rgba8;
internalFormat = PixelInternalFormat.Srgb8Alpha8;
pixelDataFormat = PixelFormat.Rgba;
pixelDataType = PixelType.UnsignedByte;
}
@@ -110,8 +110,6 @@ namespace Robust.Client.Graphics.Clyde
unsafe
{
var span = image.GetPixelSpan();
var length = span.Length;
var val1 = span[0];
fixed (T* ptr = span)
{
GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat, image.Width, image.Height, 0,
@@ -139,109 +137,6 @@ namespace Robust.Client.Graphics.Clyde
return new OpenGLTexture(id, image.Width, image.Height);
}
public TextureArray LoadArrayFromImages<T>(ICollection<Image<T>> images, string name = null,
TextureLoadParameters? loadParams = null)
where T : unmanaged, IPixel<T>
{
DebugTools.Assert(images.Count != 0);
(int x, int y)? size = null;
foreach (var image in images)
{
if (size == null)
{
size = (image.Width, image.Height);
}
if (size.Value.x != image.Width || size.Value.y != image.Height)
{
throw new ArgumentException("All images must be of the same dimensions.", nameof(images));
}
}
var textureId = ++_nextTextureId;
var texture = new OGLHandle(GL.GenTexture());
GL.BindTexture(TextureTarget.Texture2DArray, texture.Handle);
if (name != null)
{
_objectLabelMaybe(ObjectLabelIdentifier.Texture, texture, name);
}
DebugTools.Assert(size.HasValue);
var (width, height) = size.Value;
GL.TexImage3D(TextureTarget.Texture2DArray, 0, PixelInternalFormat.Rgba8, width, height, images.Count, 0,
PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
var actualParams = loadParams ?? TextureLoadParameters.Default;
if (actualParams.SampleParameters.Filter)
{
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter,
(int) TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter,
(int) TextureMagFilter.Linear);
}
else
{
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter,
(int) TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter,
(int) TextureMagFilter.Nearest);
}
switch (actualParams.SampleParameters.WrapMode)
{
case TextureWrapMode.None:
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS,
(int) OGLTextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT,
(int) OGLTextureWrapMode.ClampToEdge);
break;
case TextureWrapMode.Repeat:
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS,
(int) OGLTextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT,
(int) OGLTextureWrapMode.Repeat);
break;
case TextureWrapMode.MirroredRepeat:
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS,
(int) OGLTextureWrapMode.MirroredRepeat);
GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT,
(int) OGLTextureWrapMode.MirroredRepeat);
break;
default:
throw new ArgumentOutOfRangeException();
}
var index = 0;
var refTextureList = new List<OpenGLTexture>();
foreach (var image in images)
{
var span = ((Image<Rgba32>) (object) image).GetPixelSpan();
unsafe
{
fixed (Rgba32* ptr = span)
{
GL.TexSubImage3D(TextureTarget.Texture2DArray, 0, 0, 0, index, width, height, 1,
PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr) ptr);
}
}
index += 1;
refTextureList.Add(new OpenGLTexture(textureId, width, height, index));
}
var loaded = new LoadedTexture
{
OpenGLObject = texture,
Width = width,
Height = height,
ArrayDepth = images.Count,
Name = name,
Type = LoadedTextureType.Array2D
};
_loadedTextures.Add(textureId, loaded);
return new TextureArray(refTextureList.ToArray());
}
private void _loadStockTextures()
{
var white = new Image<Rgba32>(1, 1);
@@ -259,7 +154,6 @@ namespace Robust.Client.Graphics.Clyde
public LoadedTextureType Type;
public int Width;
public int Height;
public int? ArrayDepth;
public string Name;
public Vector2i Size => new Vector2i(Width, Height);
}
@@ -267,7 +161,6 @@ namespace Robust.Client.Graphics.Clyde
private enum LoadedTextureType
{
Texture2D,
Array2D,
}
}
}

View File

@@ -12,7 +12,7 @@ using System.Threading;
using JetBrains.Annotations;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Graphics.OpenGL;
using Robust.Client.Audio;
using Robust.Client.Input;
using Robust.Client.Interfaces.Graphics;
@@ -240,6 +240,7 @@ namespace Robust.Client.Graphics.Clyde
_loadExtensions();
GL.Enable(EnableCap.Blend);
GL.Enable(EnableCap.FramebufferSrgb);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
var vendor = GL.GetString(StringName.Vendor);

View File

@@ -1,13 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using JetBrains.Annotations;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using Robust.Client.Interfaces.Graphics;
using Robust.Client.Utility;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
@@ -89,19 +85,6 @@ namespace Robust.Client.Graphics
}
}
public static TextureArray LoadArrayFromImages<T>(ICollection<Image<T>> images, string name = null,
TextureLoadParameters? loadParameters = null)
where T : unmanaged, IPixel<T>
{
if (GameController.Mode != GameController.DisplayMode.Clyde)
{
throw new NotImplementedException();
}
var manager = IoCManager.Resolve<IClyde>();
return manager.LoadArrayFromImages(images, name, loadParameters);
}
Texture IDirectionalTextureProvider.Default => this;
Texture IDirectionalTextureProvider.TextureFor(Direction dir)
@@ -110,29 +93,6 @@ namespace Robust.Client.Graphics
}
}
public sealed class TextureArray : IReadOnlyList<Texture>
{
public Texture this[int index] => _subTextures[index];
private readonly OpenGLTexture[] _subTextures;
public int Count => _subTextures.Length;
internal TextureArray(OpenGLTexture[] subTextures)
{
_subTextures = subTextures;
}
public IEnumerator<Texture> GetEnumerator()
{
return ((IReadOnlyCollection<Texture>) _subTextures).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
internal sealed class DummyTexture : Texture
{
public override int Width { get; }

View File

@@ -22,9 +22,6 @@ namespace Robust.Client.Interfaces.Graphics
TextureLoadParameters? loadParams = null);
Texture LoadTextureFromImage<T>(Image<T> image, string name=null,
TextureLoadParameters? loadParams = null) where T : unmanaged, IPixel<T>;
TextureArray LoadArrayFromImages<T>(ICollection<Image<T>> images, string name = null,
TextureLoadParameters? loadParams = null)
where T : unmanaged, IPixel<T>;
int LoadShader(ParsedShader shader, string name = null);

View File

@@ -242,7 +242,7 @@ namespace Robust.Shared.Maths
}
/// <summary>
/// Converts sRGB color values to RGB color values.
/// Converts sRGB color values to linear RGB color values.
/// </summary>
/// <returns>
/// Returns the converted color value.
@@ -273,7 +273,7 @@ namespace Robust.Shared.Maths
}
/// <summary>
/// Converts RGB color values to sRGB color values.
/// Converts linear RGB color values to sRGB color values.
/// </summary>
/// <returns>
/// Returns the converted color value.

View File

@@ -372,7 +372,7 @@ namespace Robust.Shared.GameObjects
where T : IComponent
{
if (_dictComponents.TryGetValue(typeof(T), out var typeDict))
return typeDict.Values.Cast<T>();
return typeDict.Values.Cast<T>().Where(c => !c.Deleted);
return Enumerable.Empty<T>();
}