Skip to content

Commit

Permalink
add combined image samplers
Browse files Browse the repository at this point in the history
  • Loading branch information
aquagoose committed Jul 21, 2024
1 parent bf6a6f4 commit a28dce5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 89 deletions.
24 changes: 23 additions & 1 deletion src/grabs.Graphics.D3D11/D3D11CommandList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,18 @@ private void SetPreDrawParameters()
Context->VSSetSamplers(newBinding, 1, &state);
break;
}


case DescriptorType.Texture:
{
D3D11Texture texture = (D3D11Texture) description.Texture;
ID3D11ShaderResourceView* resView = texture.ResourceView;
D3D11Sampler sampler = (D3D11Sampler) description.Sampler;
ID3D11SamplerState* state = sampler.SamplerState;
Context->VSSetShaderResources(newBinding, 1, &resView);
Context->VSSetSamplers(newBinding, 1, &state);
break;
}

default:
throw new ArgumentOutOfRangeException();
}
Expand Down Expand Up @@ -254,6 +265,17 @@ private void SetPreDrawParameters()
break;
}

case DescriptorType.Texture:
{
D3D11Texture texture = (D3D11Texture) description.Texture;
ID3D11ShaderResourceView* resView = texture.ResourceView;
D3D11Sampler sampler = (D3D11Sampler) description.Sampler;
ID3D11SamplerState* state = sampler.SamplerState;
Context->PSSetShaderResources(newBinding, 1, &resView);
Context->PSSetSamplers(newBinding, 1, &state);
break;
}

default:
throw new ArgumentOutOfRangeException();
}
Expand Down
3 changes: 2 additions & 1 deletion src/grabs.Graphics/DescriptorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public enum DescriptorType
{
ConstantBuffer,
Image,
Sampler
Sampler,
Texture
}
120 changes: 33 additions & 87 deletions src/grabs.ShaderCompiler.Spirv/SpirvCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public static unsafe string TranspileSpirv(ShaderStage stage, ShaderLanguage lan
break;
case ShaderLanguage.Glsl430:
Spirv.CompilerOptionsSetUint(options, CompilerOption.GlslVersion, 430);

Spirv.CompilerBuildCombinedImageSamplers(compiler);
break;
default:
throw new ArgumentOutOfRangeException(nameof(language), language, null);
Expand All @@ -83,60 +81,42 @@ public static unsafe string TranspileSpirv(ShaderStage stage, ShaderLanguage lan
Spirv.CompilerCreateShaderResources(compiler, &resources);

remappings = new DescriptorRemappings();
RemapDescriptorBindingsForType(compiler, resources, ResourceType.UniformBuffer, ref remappings);
RemapDescriptorBindingsForType(compiler, resources, ResourceType.SeparateImage, ref remappings);
RemapDescriptorBindingsForType(compiler, resources, ResourceType.SeparateSamplers, ref remappings);

/*List<ShaderResource> allResources = new List<ShaderResource>();
GetShaderResourcesForType(compiler, resources, ResourceType.UniformBuffer, ref allResources);
GetShaderResourcesForType(compiler, resources, ResourceType.SeparateImage, ref allResources);
GetShaderResourcesForType(compiler, resources, ResourceType.SeparateSamplers, ref allResources);
//GetShaderResourcesForType(compiler, resources, ResourceType.SampledImage, ref allResources);
uint newBinding = 0;

uint totalMaxBinding = 0;
uint currentMaxBinding = 0;
uint currentSet = 0;
foreach (ShaderResource resource in allResources.OrderBy(resource => resource.Set))
switch (language)
{
if (resource.Set != currentSet)
case ShaderLanguage.Hlsl50:
{
currentSet = resource.Set;
totalMaxBinding = currentMaxBinding + 1;
currentMaxBinding = 0;
RemapDescriptorBindingsForType(compiler, resources, ResourceType.UniformBuffer, ref newBinding, ref remappings);
newBinding = 0;
RemapDescriptorBindingsForType(compiler, resources, ResourceType.SampledImage, ref newBinding, ref remappings);
uint currentBinding = newBinding;
RemapDescriptorBindingsForType(compiler, resources, ResourceType.SeparateSamplers, ref newBinding, ref remappings);
newBinding = currentBinding;
RemapDescriptorBindingsForType(compiler, resources, ResourceType.SeparateImage, ref newBinding, ref remappings);
break;
}
Console.WriteLine($"Resource: Set: {resource.Set}, Binding: {resource.Binding}, CurrentMaxBinding: {currentMaxBinding}, TotalMaxBinding: {totalMaxBinding}");
Spirv.CompilerUnsetDecoration(compiler, resource.Id, Decoration.DescriptorSet);
Spirv.CompilerSetDecoration(compiler, resource.Id, Decoration.Binding, resource.Binding + totalMaxBinding);
if (resource.Binding > currentMaxBinding)
currentMaxBinding = resource.Binding;
}*/

/*uint bindIndex = 0;
ChangeDescriptorBindingsForType(compiler, resources, ResourceType.UniformBuffer, ref bindIndex);
ChangeDescriptorBindingsForType(compiler, resources, ResourceType.SeparateSamplers, ref bindIndex);
ChangeDescriptorBindingsForType(compiler, resources, ResourceType.SeparateImage, ref bindIndex);
ChangeDescriptorBindingsForType(compiler, resources, ResourceType.SampledImage, ref bindIndex);*/



// 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);
CombinedImageSampler* samplers;
nuint numSamplers;
Spirv.CompilerGetCombinedImageSamplers(compiler, &samplers, &numSamplers);
case ShaderLanguage.Glsl430:
{
RemapDescriptorBindingsForType(compiler, resources, ResourceType.UniformBuffer, ref newBinding, ref remappings);
RemapDescriptorBindingsForType(compiler, resources, ResourceType.SampledImage, ref newBinding, ref remappings);

Spirv.CompilerBuildCombinedImageSamplers(compiler);

CombinedImageSampler* combinedSamplers;
nuint numSamplers;
Spirv.CompilerGetCombinedImageSamplers(compiler, &combinedSamplers, &numSamplers);
for (uint i = 0; i < (int) numSamplers; i++)
{
Spirv.CompilerSetDecoration(compiler, combinedSamplers[i].CombinedId, Decoration.Binding,
newBinding++);
}

for (uint i = 0; i < numSamplers; i++)
{
uint decoration = Spirv.CompilerGetDecoration(compiler, samplers[i].ImageId, Decoration.Binding);
Spirv.CompilerSetDecoration(compiler, samplers[i].CombinedId, Decoration.Binding, decoration);
}*/
break;
}
default:
throw new ArgumentOutOfRangeException(nameof(language), language, null);
}

if (constants != null)
{
Expand Down Expand Up @@ -207,30 +187,12 @@ public static unsafe string TranspileSpirv(ShaderStage stage, ShaderLanguage lan
return strResult;
}

private static unsafe void GetShaderResourcesForType(Compiler* compiler, Resources* resources, ResourceType type,
ref List<ShaderResource> resourcesList)
private static unsafe void RemapDescriptorBindingsForType(Compiler* compiler, Resources* resources, ResourceType type, ref uint newBinding, ref DescriptorRemappings remappings)
{
nuint numResources;
ReflectedResource* resourceList;
Spirv.ResourcesGetResourceListForType(resources, type, &resourceList, &numResources);

for (int i = 0; i < (int) numResources; i++)
{
uint id = resourceList[i].Id;
uint set = Spirv.CompilerGetDecoration(compiler, id, Decoration.DescriptorSet);
uint binding = Spirv.CompilerGetDecoration(compiler, id, Decoration.Binding);

resourcesList.Add(new ShaderResource(id, set, binding));
}
}

private static unsafe void RemapDescriptorBindingsForType(Compiler* compiler, Resources* resources, ResourceType type, ref DescriptorRemappings remappings)
{
nuint numResources;
ReflectedResource* resourceList;
Spirv.ResourcesGetResourceListForType(resources, type, &resourceList, &numResources);

uint newBinding = 0;

for (int i = 0; i < (int) numResources; i++)
{
/*Console.WriteLine(new string((sbyte*) resourceList[i].Name));
Expand Down Expand Up @@ -262,20 +224,4 @@ private static unsafe void RemapDescriptorBindingsForType(Compiler* compiler, Re
newBinding++;
}
}

private readonly struct ShaderResource
{
public readonly uint Id;

public readonly uint Set;

public readonly uint Binding;

public ShaderResource(uint id, uint set, uint binding)
{
Id = id;
Set = set;
Binding = binding;
}
}
}

0 comments on commit a28dce5

Please sign in to comment.