Skip to content

Commit

Permalink
sampler descriptions, start sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
aquagoose committed Jul 19, 2024
1 parent e6bcadf commit 073a5b8
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/grabs.Graphics/Sampler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace grabs.Graphics;

public abstract class Sampler : IDisposable
{
public abstract void Dispose();
}
84 changes: 84 additions & 0 deletions src/grabs.Graphics/SamplerDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Numerics;

namespace grabs.Graphics;

public struct SamplerDescription
{
public TextureFilter MinFilter;

public TextureFilter MagFilter;

public TextureFilter MipFilter;

public TextureAddress AddressU;

public TextureAddress AddressV;

public TextureAddress AddressW;

public float MipLodBias;

public bool EnableAnisotropy;

public uint MaxAnisotropy;

public ComparisonFunction Comparison;

public float MinLod;

public float MaxLod;

public Vector4 BorderColor;

public SamplerDescription(TextureFilter minFilter, TextureFilter magFilter, TextureFilter mipFilter,
TextureAddress addressU, TextureAddress addressV, TextureAddress addressW, float mipLodBias,
bool enableAnisotropy, uint maxAnisotropy, ComparisonFunction comparison, float minLod, float maxLod,
Vector4 borderColor)
{
MinFilter = minFilter;
MagFilter = magFilter;
MipFilter = mipFilter;
AddressU = addressU;
AddressV = addressV;
AddressW = addressW;
MipLodBias = mipLodBias;
EnableAnisotropy = enableAnisotropy;
MaxAnisotropy = maxAnisotropy;
Comparison = comparison;
MinLod = minLod;
MaxLod = maxLod;
BorderColor = borderColor;
}

public SamplerDescription(TextureFilter minFilter, TextureFilter magFilter, TextureFilter mipFilter,
TextureAddress address, bool enableAnisotropy = false, uint maxAnisotropy = 0) : this(minFilter, magFilter,
mipFilter, address, address, address, 0, enableAnisotropy, maxAnisotropy, ComparisonFunction.LessEqual,
float.MinValue, float.MaxValue, Vector4.One) { }

public SamplerDescription(TextureFilter filter, TextureAddress address, bool enableAnisotropy = false,
uint maxAnisotropy = 0) : this(filter, filter, filter, address, enableAnisotropy, maxAnisotropy) { }

public static SamplerDescription PointWrap =>
new SamplerDescription(TextureFilter.Point, TextureAddress.RepeatWrap);

public static SamplerDescription PointClamp =>
new SamplerDescription(TextureFilter.Point, TextureAddress.ClampToEdge);

public static SamplerDescription LinearWrap =>
new SamplerDescription(TextureFilter.Linear, TextureAddress.RepeatWrap);

public static SamplerDescription LinearClamp =>
new SamplerDescription(TextureFilter.Linear, TextureAddress.ClampToEdge);

public static SamplerDescription Anisotropic2x =>
new SamplerDescription(TextureFilter.Linear, TextureAddress.RepeatWrap, true, 2);

public static SamplerDescription Anisotropic4x =>
new SamplerDescription(TextureFilter.Linear, TextureAddress.RepeatWrap, true, 4);

public static SamplerDescription Anisotropic8x =>
new SamplerDescription(TextureFilter.Linear, TextureAddress.RepeatWrap, true, 8);

public static SamplerDescription Anisotropic16x =>
new SamplerDescription(TextureFilter.Linear, TextureAddress.RepeatWrap, true, 16);
}
9 changes: 9 additions & 0 deletions src/grabs.Graphics/TextureAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace grabs.Graphics;

public enum TextureAddress
{
RepeatWrap,
RepeatWrapMirrored,
ClampToEdge,
ClampToBorder
}
7 changes: 7 additions & 0 deletions src/grabs.Graphics/TextureFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace grabs.Graphics;

public enum TextureFilter
{
Point,
Linear
}
7 changes: 5 additions & 2 deletions src/grabs.ShaderCompiler.Spirv/SpirvCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public static unsafe string TranspileSpirv(ShaderStage stage, ShaderLanguage lan
ChangeDescriptorBindingsForType(compiler, resources, ResourceType.SeparateImage, ref bindIndex);
ChangeDescriptorBindingsForType(compiler, resources, ResourceType.SeparateSamplers, ref bindIndex);

// I have absolutely no idea how I figured this out. Copied directly from Pie's compiler.
// TODO: Combined image samplers need to be reimplemented. This should be tested with GLSL though, as I can't figure out DXC's combined samplers right now. Something is broken here!
/*// I have absolutely no idea how I figured this out. Copied directly from Pie's compiler.
uint id;
Spirv.CompilerBuildDummySamplerForCombinedImages(compiler, &id);
Spirv.CompilerBuildCombinedImageSamplers(compiler);
Expand All @@ -96,7 +97,7 @@ public static unsafe string TranspileSpirv(ShaderStage stage, ShaderLanguage lan
{
uint decoration = Spirv.CompilerGetDecoration(compiler, samplers[i].ImageId, Decoration.Binding);
Spirv.CompilerSetDecoration(compiler, samplers[i].CombinedId, Decoration.Binding, decoration);
}
}*/

if (constants != null)
{
Expand Down Expand Up @@ -161,6 +162,8 @@ public static unsafe string TranspileSpirv(ShaderStage stage, ShaderLanguage lan

Spirv.ContextReleaseAllocations(context);
Spirv.ContextDestroy(context);

Console.WriteLine(strResult);

return strResult;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/grabs.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
api = (GraphicsApi) buttonId;
}

using TestBase test = new CubemapTest();
using TestBase test = new CubeTest();
test.Run(api, new Size(1280, 720));

return;
Expand Down

0 comments on commit 073a5b8

Please sign in to comment.