Skip to content

Commit

Permalink
Avoid allocating shaders when values are default (OpenDreamProject#1570)
Browse files Browse the repository at this point in the history
* Avoid allocating shaders when values are default

* Update some outdated comments
  • Loading branch information
wixoaGit authored Dec 27, 2023
1 parent 2792c2d commit 3ccb916
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions OpenDream.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=noto/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=nullspace/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=occluder/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=opendream/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=oview/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=oviewers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Preproc/@EntryIndexedValue">True</s:Boolean>
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamClient/Rendering/DreamPlane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Draw(DreamViewOverlay overlay, DrawingHandleWorld handle) {
if (_temporaryRenderTarget != null) {
// Draw again, but with the color applied
handle.RenderInRenderTarget(_temporaryRenderTarget, () => {
handle.UseShader(overlay.GetBlendAndColorShader(Master, blendModeOverride: BlendMode.Overlay));
handle.UseShader(overlay.GetBlendAndColorShader(Master, useOverlayMode: true));
handle.SetTransform(overlay.CreateRenderTargetFlipMatrix(_temporaryRenderTarget.Size, Vector2.Zero));
handle.DrawTextureRect(_mainRenderTarget.Texture, new Box2(Vector2.Zero, _mainRenderTarget.Size));
handle.SetTransform(Matrix3.Identity);
Expand Down
24 changes: 15 additions & 9 deletions OpenDreamClient/Rendering/DreamViewOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public DreamViewOverlay(TransformSystem transformSystem, EntityLookupSystem look
_blockColorInstance = _protoManager.Index<ShaderPrototype>("blockcolor").InstanceUnique();
_colorInstance = _protoManager.Index<ShaderPrototype>("color").InstanceUnique();
_blendModeInstances = new(6) {
{BlendMode.Default, _protoManager.Index<ShaderPrototype>("blend_overlay").InstanceUnique()}, //BLEND_DEFAULT
{BlendMode.Overlay, _protoManager.Index<ShaderPrototype>("blend_overlay").InstanceUnique()}, //BLEND_OVERLAY (same as BLEND_DEFAULT)
{BlendMode.Default, _protoManager.Index<ShaderPrototype>("blend_overlay").InstanceUnique()}, //BLEND_DEFAULT (Same as BLEND_OVERLAY when there's no parent)
{BlendMode.Overlay, _protoManager.Index<ShaderPrototype>("blend_overlay").InstanceUnique()}, //BLEND_OVERLAY
{BlendMode.Add, _protoManager.Index<ShaderPrototype>("blend_add").InstanceUnique()}, //BLEND_ADD
{BlendMode.Subtract, _protoManager.Index<ShaderPrototype>("blend_subtract").InstanceUnique()}, //BLEND_SUBTRACT
{BlendMode.Multiply, _protoManager.Index<ShaderPrototype>("blend_multiply").InstanceUnique()}, //BLEND_MULTIPLY
Expand Down Expand Up @@ -379,16 +379,23 @@ private void ClearRenderTarget(IRenderTexture target, DrawingHandleWorld handle,
handle.RenderInRenderTarget(target, () => {}, clearColor);
}

public ShaderInstance GetBlendAndColorShader(RendererMetaData iconMetaData, Color? colorOverride = null, BlendMode? blendModeOverride = null) {
Color rgba = colorOverride ?? iconMetaData.ColorToApply.WithAlpha(iconMetaData.AlphaToApply);
public ShaderInstance? GetBlendAndColorShader(RendererMetaData iconMetaData, bool ignoreColor = false, bool useOverlayMode = false) {
BlendMode blendMode = useOverlayMode ? BlendMode.Overlay : iconMetaData.BlendMode;

ColorMatrix colorMatrix;
if (colorOverride != null || iconMetaData.ColorMatrixToApply.Equals(ColorMatrix.Identity))
colorMatrix = new ColorMatrix(rgba);
if (ignoreColor)
colorMatrix = ColorMatrix.Identity;
else if (iconMetaData.ColorMatrixToApply.Equals(ColorMatrix.Identity))
colorMatrix = new ColorMatrix(iconMetaData.ColorToApply.WithAlpha(iconMetaData.AlphaToApply));
else
colorMatrix = iconMetaData.ColorMatrixToApply;

if (!_blendModeInstances.TryGetValue(blendModeOverride ?? iconMetaData.BlendMode, out var blendAndColor))
// We can use no shader if everything is default
if (!iconMetaData.IsPlaneMaster && blendMode is BlendMode.Default or BlendMode.Overlay &&
colorMatrix.Equals(ColorMatrix.Identity))
return null;

if (!_blendModeInstances.TryGetValue(blendMode, out var blendAndColor))
blendAndColor = _blendModeInstances[BlendMode.Default];

blendAndColor = blendAndColor.Duplicate();
Expand Down Expand Up @@ -548,8 +555,7 @@ public ShaderInstance GetBlendAndColorShader(RendererMetaData iconMetaData, Colo
//then we return the Action that draws the actual icon with filters applied
iconDrawAction = renderTargetSize => {
//note we apply the color *before* the filters, so we use override here
handle.UseShader(GetBlendAndColorShader(iconMetaData, colorOverride: Color.White));

handle.UseShader(GetBlendAndColorShader(iconMetaData, ignoreColor: true));
handle.SetTransform(CreateRenderTargetFlipMatrix(renderTargetSize, pixelPosition - frame.Size / 2));
handle.DrawTextureRect(pong.Texture, new Box2(Vector2.Zero, pong.Size));
handle.UseShader(null);
Expand Down

0 comments on commit 3ccb916

Please sign in to comment.