シェーダーの内部画素を取得だけの経路で処理する
このコミットが含まれているのは:
5ファイル変更
+202
-26
検索条件に一致するファイルはありません
@@ -58,42 +58,42 @@
|
||||
"frames": [
|
||||
0
|
||||
],
|
||||
"hash": "F04D40A433833C43BD0B2537CB619D3324B6D6F4DE3158004E68748C61EEE13F"
|
||||
"hash": "723C55E85A3D1EB1F3736F600B30F39C0D27092F70DA44FA3CAE7ACE2D04E2D3"
|
||||
},
|
||||
{
|
||||
"name": "radial-center-offset",
|
||||
"frames": [
|
||||
0
|
||||
],
|
||||
"hash": "8E00699F852665476BD164A2FD15B911ADA2476DE2BD0203881307CD6654E090"
|
||||
"hash": "91467D7D74495AD514ECE14F2A94EFBBDD6892D88E762B660E0475E00AA656D9"
|
||||
},
|
||||
{
|
||||
"name": "rotational-24",
|
||||
"frames": [
|
||||
0
|
||||
],
|
||||
"hash": "E15861DD08C67F71DD60B99386E18D52A0E6B72CEA0716CB0B2DF204C00ABD2B"
|
||||
"hash": "E35B9BC3CD6EE9BC12F2D13AA35E2BFADF5335B29C4AE502FB802F550B7CA4E0"
|
||||
},
|
||||
{
|
||||
"name": "rotational-center-offset",
|
||||
"frames": [
|
||||
0
|
||||
],
|
||||
"hash": "7689979D21E6F1F615C0CFB93485426219A2E43F3E00EC08B9C246103C63278B"
|
||||
"hash": "7F653DFB18B001CE1D647F14D5EAF823E8DA8AF296952F7F06A164E8867F0F43"
|
||||
},
|
||||
{
|
||||
"name": "prism",
|
||||
"frames": [
|
||||
0
|
||||
],
|
||||
"hash": "1EA00234D147D407D80B2B779ED768CFE0D7BDB3875DBBABD4173FA5AC73C2CD"
|
||||
"hash": "172ED78BF9F93F0610EF604031679D49249A6ACD05AFBF13AA25E5A15E27E967"
|
||||
},
|
||||
{
|
||||
"name": "reference-450",
|
||||
"frames": [
|
||||
0
|
||||
],
|
||||
"hash": "09E1544F83FB0C44BE8D94C339D3C303BC7CD3A398199F69A5B8FDC9FC1E6E30"
|
||||
"hash": "D9944892591B102B8B0BE7CC47B3114259D30A058F9B49B494753CECA15C3F42"
|
||||
},
|
||||
{
|
||||
"name": "reference-650",
|
||||
|
||||
@@ -5,6 +5,7 @@ static const int MaxSteps = 64;
|
||||
static const int ModeRadial = 2;
|
||||
static const int ModeRotational = 4;
|
||||
static const float MaxRadialShift = 0.5;
|
||||
static const float InteriorGuard = 0.51;
|
||||
|
||||
cbuffer Constants : register(b0)
|
||||
{
|
||||
@@ -16,6 +17,7 @@ cbuffer Constants : register(b0)
|
||||
float strength : packoffset(c2.z);
|
||||
int mode : packoffset(c2.w);
|
||||
int steps : packoffset(c3.x);
|
||||
float spreadScale : packoffset(c3.y);
|
||||
float4 samples[MaxSteps] : packoffset(c4);
|
||||
};
|
||||
|
||||
@@ -40,6 +42,17 @@ float2 SamplePosition(float2 scenePosition, float2 offset, float coefficient)
|
||||
return scenePosition - shift * direction;
|
||||
}
|
||||
|
||||
float2 Spread(float2 offset)
|
||||
{
|
||||
if (mode == ModeRadial)
|
||||
return abs(offset) * spreadScale;
|
||||
|
||||
if (mode == ModeRotational)
|
||||
return length(offset) * spreadScale;
|
||||
|
||||
return abs(shiftDistance) * abs(direction);
|
||||
}
|
||||
|
||||
float4 SampleCovered(float2 uv, float2 pixelStep, float2 scenePosition, float2 target)
|
||||
{
|
||||
float2 minimum = inputBounds.xy + 0.5;
|
||||
@@ -50,17 +63,21 @@ float4 SampleCovered(float2 uv, float2 pixelStep, float2 scenePosition, float2 t
|
||||
return color * (coverage.x * coverage.y);
|
||||
}
|
||||
|
||||
float4 main(
|
||||
float4 position : SV_POSITION,
|
||||
float4 scenePosition : SCENE_POSITION,
|
||||
float4 uv0 : TEXCOORD0
|
||||
) : SV_TARGET
|
||||
void Accumulate(float4 entry, float4 shifted, inout float3 color, inout float3 coverage)
|
||||
{
|
||||
float4 source = SampleCovered(uv0.xy, uv0.zw, scenePosition.xy, scenePosition.xy);
|
||||
if (shiftDistance == 0.0 || strength <= 0.0)
|
||||
return source;
|
||||
color += entry.rgb * shifted.rgb;
|
||||
coverage += entry.rgb * shifted.a;
|
||||
}
|
||||
|
||||
float2 offset = scenePosition.xy - center;
|
||||
float4 Resolve(float3 color, float3 coverage)
|
||||
{
|
||||
float alpha = max(coverage.r, max(coverage.g, coverage.b));
|
||||
return float4(min(color, alpha), alpha);
|
||||
}
|
||||
|
||||
float4 GatherCovered(float2 uv, float2 pixelStep, float2 scenePosition)
|
||||
{
|
||||
float2 offset = scenePosition - center;
|
||||
float3 color = 0.0;
|
||||
float3 coverage = 0.0;
|
||||
|
||||
@@ -68,12 +85,82 @@ float4 main(
|
||||
for (int index = 0; index < steps; index++)
|
||||
{
|
||||
float4 entry = samples[index];
|
||||
float4 shifted = SampleCovered(uv0.xy, uv0.zw, scenePosition.xy, SamplePosition(scenePosition.xy, offset, entry.w));
|
||||
color += entry.rgb * shifted.rgb;
|
||||
coverage += entry.rgb * shifted.a;
|
||||
Accumulate(entry, SampleCovered(uv, pixelStep, scenePosition, SamplePosition(scenePosition, offset, entry.w)), color, coverage);
|
||||
}
|
||||
|
||||
float alpha = max(coverage.r, max(coverage.g, coverage.b));
|
||||
float4 result = float4(min(color, alpha), alpha);
|
||||
return lerp(source, result, strength);
|
||||
return Resolve(color, coverage);
|
||||
}
|
||||
|
||||
float4 GatherInside(float2 uv, float2 pixelStep, float2 scenePosition)
|
||||
{
|
||||
float2 offset = scenePosition - center;
|
||||
float3 color = 0.0;
|
||||
float3 coverage = 0.0;
|
||||
|
||||
if (mode == ModeRadial)
|
||||
{
|
||||
float2 offsetStep = offset * pixelStep;
|
||||
float rate = shiftDistance / radius;
|
||||
|
||||
[loop]
|
||||
for (int index = 0; index < steps; index++)
|
||||
{
|
||||
float4 entry = samples[index];
|
||||
float scale = clamp(entry.w * rate, -MaxRadialShift, MaxRadialShift);
|
||||
Accumulate(entry, SourceTexture.SampleLevel(SourceSampler, uv - offsetStep * scale, 0), color, coverage);
|
||||
}
|
||||
}
|
||||
else if (mode == ModeRotational)
|
||||
{
|
||||
float rate = shiftDistance / radius;
|
||||
|
||||
[loop]
|
||||
for (int index = 0; index < steps; index++)
|
||||
{
|
||||
float4 entry = samples[index];
|
||||
float sine;
|
||||
float cosine;
|
||||
sincos(entry.w * rate, sine, cosine);
|
||||
float2 delta = float2((cosine - 1.0) * offset.x + sine * offset.y, (cosine - 1.0) * offset.y - sine * offset.x);
|
||||
Accumulate(entry, SourceTexture.SampleLevel(SourceSampler, uv + delta * pixelStep, 0), color, coverage);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float2 stride = shiftDistance * direction * pixelStep;
|
||||
|
||||
[loop]
|
||||
for (int index = 0; index < steps; index++)
|
||||
{
|
||||
float4 entry = samples[index];
|
||||
Accumulate(entry, SourceTexture.SampleLevel(SourceSampler, uv - entry.w * stride, 0), color, coverage);
|
||||
}
|
||||
}
|
||||
|
||||
return Resolve(color, coverage);
|
||||
}
|
||||
|
||||
float4 main(
|
||||
float4 position : SV_POSITION,
|
||||
float4 scenePosition : SCENE_POSITION,
|
||||
float4 uv0 : TEXCOORD0
|
||||
) : SV_TARGET
|
||||
{
|
||||
if (shiftDistance == 0.0 || strength <= 0.0)
|
||||
return SampleCovered(uv0.xy, uv0.zw, scenePosition.xy, scenePosition.xy);
|
||||
|
||||
float2 margin = Spread(scenePosition.xy - center) + InteriorGuard;
|
||||
bool inside = all(scenePosition.xy >= inputBounds.xy + margin) && all(scenePosition.xy <= inputBounds.zw - margin);
|
||||
|
||||
float4 result;
|
||||
[branch]
|
||||
if (inside)
|
||||
result = GatherInside(uv0.xy, uv0.zw, scenePosition.xy);
|
||||
else
|
||||
result = GatherCovered(uv0.xy, uv0.zw, scenePosition.xy);
|
||||
|
||||
if (strength >= 1.0)
|
||||
return result;
|
||||
|
||||
return lerp(SampleCovered(uv0.xy, uv0.zw, scenePosition.xy, scenePosition.xy), result, strength);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using static SpectrumColorShift.SpectrumColorShiftCustomEffect;
|
||||
|
||||
namespace SpectrumColorShift.Tests;
|
||||
|
||||
public sealed partial class ShaderContractTests
|
||||
{
|
||||
static readonly Dictionary<string, string> Fields = new()
|
||||
{
|
||||
["inputBounds"] = "InputBounds",
|
||||
["direction"] = "Direction",
|
||||
["center"] = "Center",
|
||||
["shiftDistance"] = "Distance",
|
||||
["radius"] = "Radius",
|
||||
["strength"] = "Strength",
|
||||
["mode"] = "Mode",
|
||||
["steps"] = "Steps",
|
||||
["spreadScale"] = "SpreadScale",
|
||||
["samples"] = "Samples",
|
||||
};
|
||||
|
||||
static string Source { get; } = File.ReadAllText(Path.Combine(Path.GetDirectoryName(Here())!, "..", "SpectrumColorShift.Shader", "SpectrumColorShift.hlsl"));
|
||||
|
||||
static string Here([CallerFilePath] string path = "") => path;
|
||||
|
||||
static Type ConstantBuffer => typeof(EffectImpl).GetNestedType("ConstantBuffer", BindingFlags.NonPublic)!;
|
||||
|
||||
[GeneratedRegex(@"static const (?:int|float) (?<name>\w+) = (?<value>[0-9.]+);")]
|
||||
private static partial Regex ConstantPattern();
|
||||
|
||||
[GeneratedRegex(@"^\s*(?:float4|float2|float|int)\s+(?<name>\w+)(?:\[\w+\])?\s*:\s*packoffset\(c(?<register>\d+)(?:\.(?<component>[xyzw]))?\);", RegexOptions.Multiline)]
|
||||
private static partial Regex MemberPattern();
|
||||
|
||||
static float Constant(string name)
|
||||
=> float.Parse(ConstantPattern().Matches(Source).Single(match => match.Groups["name"].Value == name).Groups["value"].Value, CultureInfo.InvariantCulture);
|
||||
|
||||
[Fact]
|
||||
public void TheShaderAndTheEffectAgreeOnTheConstants()
|
||||
{
|
||||
Assert.Equal(Spectrum.MaxSteps, Constant("MaxSteps"));
|
||||
Assert.Equal((int)ShiftMode.Radial, Constant("ModeRadial"));
|
||||
Assert.Equal((int)ShiftMode.Rotational, Constant("ModeRotational"));
|
||||
Assert.Equal(ShiftGeometry.MaxRadialShift, Constant("MaxRadialShift"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheConstantBufferMatchesTheShaderLayout()
|
||||
{
|
||||
var members = MemberPattern().Matches(Source)
|
||||
.ToDictionary(
|
||||
match => match.Groups["name"].Value,
|
||||
match => int.Parse(match.Groups["register"].Value, CultureInfo.InvariantCulture) * 16
|
||||
+ (match.Groups["component"].Success ? "xyzw".IndexOf(match.Groups["component"].Value[0]) * 4 : 0));
|
||||
|
||||
Assert.Equal(Fields.Keys.Order(), members.Keys.Order());
|
||||
Assert.All(members, member => Assert.Equal(member.Value, (int)Marshal.OffsetOf(ConstantBuffer, Fields[member.Key])));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheConstantBufferFillsWholeRegistersAndHoldsTheTable()
|
||||
{
|
||||
var size = (int)typeof(Unsafe).GetMethod(nameof(Unsafe.SizeOf))!.MakeGenericMethod(ConstantBuffer).Invoke(null, null)!;
|
||||
|
||||
Assert.Equal(16 * (4 + Spectrum.MaxSteps), size);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,17 @@ internal static class ShiftGeometry
|
||||
|
||||
public static float Radius(RawRect rect, Vector2 center) => Math.Max(Farthest(rect, center), 1f);
|
||||
|
||||
public static float Spread(ShiftMode mode, float distance, float radius)
|
||||
{
|
||||
var reach = Math.Abs(distance);
|
||||
return mode switch
|
||||
{
|
||||
ShiftMode.Radial => RadialRatio(reach, radius),
|
||||
ShiftMode.Rotational => Chord(SweepAngle(reach, radius)),
|
||||
_ => 0f,
|
||||
};
|
||||
}
|
||||
|
||||
public static RawRect Output(RawRect rect, ShiftMode mode, float distance, Vector2 direction, Vector2 center, float radius)
|
||||
{
|
||||
var reach = Math.Abs(distance);
|
||||
@@ -23,8 +34,8 @@ internal static class ShiftGeometry
|
||||
|
||||
return mode switch
|
||||
{
|
||||
ShiftMode.Radial => Scaled(rect, center, Math.Min(reach / radius, MaxRadialShift)),
|
||||
ShiftMode.Rotational => Swept(rect, center, Math.Min(reach / radius, MathF.PI)),
|
||||
ShiftMode.Radial => Scaled(rect, center, RadialRatio(reach, radius)),
|
||||
ShiftMode.Rotational => Swept(rect, center, SweepAngle(reach, radius)),
|
||||
_ => Straight(rect, reach, direction),
|
||||
};
|
||||
}
|
||||
@@ -38,19 +49,25 @@ internal static class ShiftGeometry
|
||||
switch (mode)
|
||||
{
|
||||
case ShiftMode.Radial:
|
||||
var ratio = Math.Min(reach / radius, MaxRadialShift);
|
||||
var ratio = RadialRatio(reach, radius);
|
||||
return Inflate(
|
||||
output,
|
||||
Margin(ratio * Math.Max(Math.Abs(output.Left - center.X), Math.Abs(output.Right - center.X))),
|
||||
Margin(ratio * Math.Max(Math.Abs(output.Top - center.Y), Math.Abs(output.Bottom - center.Y))));
|
||||
case ShiftMode.Rotational:
|
||||
var chord = 2f * MathF.Sin(Math.Min(reach / radius, MathF.PI) * 0.5f) * Farthest(output, center);
|
||||
var chord = Chord(SweepAngle(reach, radius)) * Farthest(output, center);
|
||||
return Inflate(output, Margin(chord), Margin(chord));
|
||||
default:
|
||||
return Straight(output, reach, direction);
|
||||
}
|
||||
}
|
||||
|
||||
private static float RadialRatio(float reach, float radius) => Math.Min(reach / radius, MaxRadialShift);
|
||||
|
||||
private static float SweepAngle(float reach, float radius) => Math.Min(reach / radius, MathF.PI);
|
||||
|
||||
private static float Chord(float angle) => 2f * MathF.Sin(angle * 0.5f);
|
||||
|
||||
private static RawRect Straight(RawRect rect, float reach, Vector2 direction)
|
||||
=> Inflate(rect, Margin(reach * Math.Abs(direction.X)), Margin(reach * Math.Abs(direction.Y)));
|
||||
|
||||
|
||||
@@ -173,6 +173,7 @@ internal sealed class SpectrumColorShiftCustomEffect(IGraphicsDevicesAndContext
|
||||
_tableStale = false;
|
||||
}
|
||||
|
||||
_cb.SpreadScale = ShiftGeometry.Spread(_mode, _cb.Distance, _cb.Radius);
|
||||
drawInformation.SetPixelShaderConstantBuffer(_cb);
|
||||
}
|
||||
catch (Exception exception)
|
||||
@@ -250,7 +251,7 @@ internal sealed class SpectrumColorShiftCustomEffect(IGraphicsDevicesAndContext
|
||||
public float Strength;
|
||||
public int Mode;
|
||||
public int Steps;
|
||||
public float Pad0;
|
||||
public float SpreadScale;
|
||||
public float Pad1;
|
||||
public float Pad2;
|
||||
public SampleTable Samples;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする