Remove BGRA32 uploads from Clyde.

BGRA uploads are not supported on GLES. Use a shader to swizzle WebViewControl contents instead.
This commit is contained in:
Pieter-Jan Briers
2021-11-11 17:49:54 +01:00
parent fd5a8bf207
commit 25d6bd908b
7 changed files with 29 additions and 14 deletions

View File

@@ -7,3 +7,8 @@
- type: shader
id: shaded
kind: canvas
- type: shader
id: bgra
kind: source
path: "/Shaders/Internal/bgra.swsl"

View File

@@ -0,0 +1,6 @@
// Swaps B and R channel so you can render BGRA stuff without swizzling at upload time.
// Currently used by CEF.
void fragment() {
COLOR = zTexture(UV).bgra;
}

View File

@@ -9,14 +9,16 @@ namespace Robust.Client.WebView.Cef
{
internal sealed class ImageBuffer
{
public Image<Bgra32> Buffer { get; private set; } = new(1, 1);
public Image<Rgba32> Buffer { get; private set; } = new(1, 1);
public unsafe void UpdateBuffer(int width, int height, IntPtr buffer, CefRectangle dirtyRect)
{
if (width != Buffer.Width || height != Buffer.Height)
UpdateSize(width, height);
var span = new ReadOnlySpan<Bgra32>((void*) buffer, width * height);
// NOTE: Image data from CEF is actually BGRA32, not RGBA32.
// OpenGL ES does not allow uploading BGRA data, so we pretend it's RGBA32 and use a shader to swizzle it.
var span = new ReadOnlySpan<Rgba32>((void*) buffer, width * height);
ImageSharpExt.Blit(
span,
@@ -28,7 +30,7 @@ namespace Robust.Client.WebView.Cef
private void UpdateSize(int width, int height)
{
Buffer = new Image<Bgra32>(width, height);
Buffer = new Image<Rgba32>(width, height);
}
}
}

View File

@@ -18,7 +18,9 @@ namespace Robust.Client.WebView.Cef
{
public IWebViewControlImpl MakeControlImpl(WebViewControl owner)
{
var impl = new ControlImpl(owner);
var shader = _prototypeManager.Index<ShaderPrototype>("bgra");
var shaderInstance = shader.Instance();
var impl = new ControlImpl(owner, shaderInstance);
_dependencyCollection.InjectDependencies(impl);
return impl;
}
@@ -131,10 +133,12 @@ namespace Robust.Client.WebView.Cef
[Dependency] private readonly IInputManager _inputMgr = default!;
public readonly WebViewControl Owner;
private readonly ShaderInstance _shaderInstance;
public ControlImpl(WebViewControl owner)
public ControlImpl(WebViewControl owner, ShaderInstance shaderInstance)
{
Owner = owner;
_shaderInstance = shaderInstance;
}
private const int ScrollSpeed = 50;
@@ -183,7 +187,7 @@ namespace Robust.Client.WebView.Cef
// Create the web browser! And by default, we go to about:blank.
var browser = CefBrowserHost.CreateBrowserSync(info, client, settings, _startUrl);
var texture = _clyde.CreateBlankTexture<Bgra32>(Vector2i.One);
var texture = _clyde.CreateBlankTexture<Rgba32>(Vector2i.One);
_data = new LiveData(texture, client, browser, renderer);
}
@@ -386,7 +390,7 @@ namespace Robust.Client.WebView.Cef
_data.Browser.GetHost().NotifyMoveOrResizeStarted();
_data.Browser.GetHost().WasResized();
_data.Texture.Dispose();
_data.Texture = _clyde.CreateBlankTexture<Bgra32>((Owner.PixelWidth, Owner.PixelHeight));
_data.Texture = _clyde.CreateBlankTexture<Rgba32>((Owner.PixelWidth, Owner.PixelHeight));
}
public void Draw(DrawingHandleScreen handle)
@@ -404,6 +408,7 @@ namespace Robust.Client.WebView.Cef
Math.Min(Owner.PixelWidth, bufImg.Width),
Math.Min(Owner.PixelHeight, bufImg.Height)));
handle.UseShader(_shaderInstance);
handle.DrawTexture(_data.Texture, Vector2.Zero);
}

View File

@@ -3,6 +3,7 @@ using System.IO;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Xilium.CefGlue;
namespace Robust.Client.WebView.Cef
@@ -12,6 +13,7 @@ namespace Robust.Client.WebView.Cef
private CefApp _app = default!;
[Dependency] private readonly IDependencyCollection _dependencyCollection = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public void Initialize()
{

View File

@@ -155,10 +155,6 @@ namespace Robust.Client.Graphics.Clyde
{
isActuallySrgb = loadParams.Srgb;
}
else if (pixelType == typeof(Bgra32))
{
isActuallySrgb = loadParams.Srgb;
}
else if (pixelType == typeof(A8))
{
DebugTools.Assert(_hasGLTextureSwizzle);
@@ -264,7 +260,6 @@ namespace Robust.Client.Graphics.Clyde
// Note that if _hasGLSrgb is off, we import an sRGB texture as non-sRGB.
// Shaders are expected to compensate for this
Rgba32 => (srgb && _hasGLSrgb ? PIF.Srgb8Alpha8 : PIF.Rgba8, PF.Rgba, PT.UnsignedByte),
Bgra32 => (srgb && _hasGLSrgb ? PIF.Srgb8Alpha8 : PIF.Rgba8, PF.Bgra, PT.UnsignedByte),
A8 or L8 => (PIF.R8, PF.Red, PT.UnsignedByte),
_ => throw new NotSupportedException("Unsupported pixel type."),
};
@@ -445,7 +440,7 @@ namespace Robust.Client.Graphics.Clyde
{
return default(T) switch
{
Rgba32 or Bgra32 => TexturePixelType.Rgba32,
Rgba32 => TexturePixelType.Rgba32,
L8 => TexturePixelType.L8,
A8 => TexturePixelType.A8,
_ => throw new NotSupportedException("Unsupported pixel type."),

View File

@@ -22,7 +22,7 @@
<PackageReference Include="OpenToolkit.OpenAL" Version="4.0.0-pre9.1" />
<PackageReference Include="SpaceWizards.SharpFont" Version="1.0.1" />
<PackageReference Include="Robust.Natives" Version="0.1.0" />
<!--<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.20348-beta1" />-->
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.20348-beta1" />
</ItemGroup>
<ItemGroup Condition="'$(EnableClientScripting)' == 'True'">
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" Version="3.8.0" />