Skip to content

Commit

Permalink
opengl samplers working
Browse files Browse the repository at this point in the history
  • Loading branch information
aquagoose committed Jul 25, 2024
1 parent d2c6b70 commit de0a9fb
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 51 deletions.
91 changes: 67 additions & 24 deletions src/grabs.Graphics.GL43/GL43Device.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using grabs.ShaderCompiler.Spirv;
using Silk.NET.OpenGL;
using Silk.NET.SPIRV.Cross;

namespace grabs.Graphics.GL43;

Expand All @@ -11,7 +14,7 @@ public class GL43Device : Device
private GL43Swapchain _swapchain;

private GL43Pipeline _currentPipeline;
private GL43DescriptorSet[] _setSets;
private Dictionary<uint, GL43DescriptorSet> _boundSets;

private DrawElementsType _currentDrawElementsType;
private uint _currentDrawElementsSizeInBytes;
Expand All @@ -22,7 +25,7 @@ public GL43Device(GL gl, GL43Surface surface)
_gl = gl;
_surface = surface;

_setSets = new GL43DescriptorSet[16];
_boundSets = new Dictionary<uint, GL43DescriptorSet>();

// Scissor test is always enabled.
_gl.Enable(EnableCap.ScissorTest);
Expand Down Expand Up @@ -326,7 +329,7 @@ public override unsafe void ExecuteCommandList(CommandList list)
case CommandListActionType.SetDescriptor:
{
GL43DescriptorSet set = action.DescriptorSet;
_setSets[action.Slot] = set;
_boundSets[action.Slot] = set;

break;
}
Expand Down Expand Up @@ -361,34 +364,74 @@ public override unsafe void ExecuteCommandList(CommandList list)

private void SetPreDrawParameters()
{
if (_currentPipeline.Layouts == null)
return;

for (int i = 0; i < _currentPipeline.Layouts.Length; i++)
foreach ((uint slot, GL43DescriptorSet set) in _boundSets)
{
GL43DescriptorLayout layout = _currentPipeline.Layouts[i];
GL43DescriptorLayout layout = _currentPipeline.Layouts[slot];
DescriptorRemappings vertexRemappings = _currentPipeline.VertexRemappings;
DescriptorRemappings pixelRemappings = _currentPipeline.PixelRemappings;

GL43Sampler currentSampler = null;

for (int j = 0; j < layout.Bindings.Length; j++)
for (int i = 0; i < layout.Bindings.Length; i++)
{
ref DescriptorBindingDescription binding = ref layout.Bindings[j];
ref DescriptorSetDescription desc = ref _setSets[i].Descriptions[j];
ref DescriptorBindingDescription binding = ref layout.Bindings[i];
ref DescriptorSetDescription description = ref set.Descriptions[i];

if (binding.Type is DescriptorType.Sampler)
currentSampler = (GL43Sampler) description.Sampler;

Remapping remapping;
uint newBinding;

switch (binding.Type)
if ((vertexRemappings.TryGetRemappedSet(slot, out remapping) ||
pixelRemappings.TryGetRemappedSet(slot, out remapping)) &&
remapping.TryGetRemappedBinding(binding.Binding, out newBinding))
{
case DescriptorType.ConstantBuffer:
_gl.BindBufferBase(BufferTargetARB.UniformBuffer, binding.Binding, ((GL43Buffer) desc.Buffer).Buffer);
break;
case DescriptorType.Image:
GL43Texture texture = (GL43Texture) desc.Texture;

_gl.ActiveTexture(TextureUnit.Texture0 + (int) binding.Binding);
_gl.BindTexture(texture.Target, texture.Texture);
break;
default:
throw new ArgumentOutOfRangeException();
switch (binding.Type)
{
case DescriptorType.ConstantBuffer:
_gl.BindBufferBase(GLEnum.UniformBuffer, newBinding, ((GL43Buffer) description.Buffer).Buffer);
break;

case DescriptorType.Image:
{
GL43Texture texture = (GL43Texture) description.Texture;

_gl.ActiveTexture(TextureUnit.Texture0 + (int) newBinding);
_gl.BindTexture(texture.Target, texture.Texture);

if (currentSampler != null)
_gl.BindSampler(newBinding, currentSampler.Sampler);

break;
}

case DescriptorType.Sampler:
{
currentSampler = (GL43Sampler) description.Sampler;
break;
}

case DescriptorType.Texture:
{
GL43Texture texture = (GL43Texture) description.Texture;
GL43Sampler sampler = (GL43Sampler) description.Sampler;

_gl.ActiveTexture(TextureUnit.Texture0 + (int) newBinding);
_gl.BindTexture(texture.Target, texture.Texture);
_gl.BindSampler(newBinding, sampler.Sampler);

break;
}

default:
throw new ArgumentOutOfRangeException();
}
}
}
}
}

_boundSets.Clear();
}

public override void Dispose() { }
Expand Down
28 changes: 8 additions & 20 deletions src/grabs.Graphics.GL43/GL43Pipeline.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using grabs.ShaderCompiler.Spirv;
using Silk.NET.OpenGL;

namespace grabs.Graphics.GL43;
Expand All @@ -8,11 +9,12 @@ public class GL43Pipeline : Pipeline
private readonly GL _gl;

public readonly uint VertexProgram;

public readonly uint FragmentProgram;

public readonly DescriptorRemappings VertexRemappings;
public readonly DescriptorRemappings PixelRemappings;

public readonly uint Pipeline;

public readonly uint Vao;

public readonly Silk.NET.OpenGL.PrimitiveType PrimitiveType;
Expand All @@ -32,6 +34,9 @@ public GL43Pipeline(GL gl, in PipelineDescription description)
GL43ShaderModule vShaderModule = (GL43ShaderModule) description.VertexShader;
GL43ShaderModule pShaderModule = (GL43ShaderModule) description.PixelShader;

VertexRemappings = vShaderModule.DescriptorRemappings;
PixelRemappings = pShaderModule.DescriptorRemappings;

VertexProgram = CreateShaderProgram(gl, vShaderModule);
FragmentProgram = CreateShaderProgram(gl, pShaderModule);

Expand Down Expand Up @@ -95,24 +100,7 @@ public GL43Pipeline(GL gl, in PipelineDescription description)
};

if (description.DescriptorLayouts != null)
{
Layouts = new GL43DescriptorLayout[description.DescriptorLayouts.Length];
uint currentBindIndex = 0;
for (int i = 0; i < Layouts.Length; i++)
{
GL43DescriptorLayout layout = (GL43DescriptorLayout) description.DescriptorLayouts[i];
DescriptorBindingDescription[] bindings = new DescriptorBindingDescription[layout.Bindings.Length];

for (int j = 0; j < bindings.Length; j++)
{
DescriptorBindingDescription desc = layout.Bindings[j];
desc.Binding = currentBindIndex++;
bindings[j] = desc;
}

Layouts[i] = new GL43DescriptorLayout(bindings);
}
}
Layouts = Array.ConvertAll(description.DescriptorLayouts, layout => (GL43DescriptorLayout) layout);
}

public override void Dispose()
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 BasicTest();
using TestBase test = new CubeTest();
test.Run(api, new Size(1280, 720));

return;
Expand Down
4 changes: 2 additions & 2 deletions tests/grabs.Tests/Shaders/Basic3D.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ cbuffer CameraMatrices : register(b0, space0)
float4x4 View;
}

cbuffer WorldMatrix : register(b5, space0)
cbuffer WorldMatrix : register(b1, space0)
{
float4x4 World;
}

SamplerState Sampler : register(s10, space1);
SamplerState Sampler : register(s0, space1);
Texture2D Texture : register(t1, space1);

VSOutput Vertex(const in VSInput input)
Expand Down
10 changes: 6 additions & 4 deletions tests/grabs.Tests/Tests/CubeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,22 @@ protected override void Initialize()

DescriptorLayoutDescription transformLayoutDesc = new DescriptorLayoutDescription(
new DescriptorBindingDescription(0, DescriptorType.ConstantBuffer, ShaderStage.Vertex),
new DescriptorBindingDescription(5, DescriptorType.ConstantBuffer, ShaderStage.Vertex));
new DescriptorBindingDescription(1, DescriptorType.ConstantBuffer, ShaderStage.Vertex));

DescriptorLayoutDescription textureLayoutDesc =
new DescriptorLayoutDescription(
new DescriptorBindingDescription(10, DescriptorType.Sampler, ShaderStage.Pixel),
new DescriptorBindingDescription(0, DescriptorType.Sampler, ShaderStage.Pixel),
new DescriptorBindingDescription(1, DescriptorType.Image, ShaderStage.Pixel)
);

DescriptorLayout transformLayout = Device.CreateDescriptorLayout(transformLayoutDesc);
DescriptorLayout textureLayout = Device.CreateDescriptorLayout(textureLayoutDesc);

const bool debug = true;

string hlsl = File.ReadAllText("Shaders/Basic3D.hlsl");
byte[] vSpv = Compiler.CompileToSpirV(hlsl, "Vertex", ShaderStage.Vertex, false);
byte[] pSpv = Compiler.CompileToSpirV(hlsl, "Pixel", ShaderStage.Pixel, false);
byte[] vSpv = Compiler.CompileToSpirV(hlsl, "Vertex", ShaderStage.Vertex, debug);
byte[] pSpv = Compiler.CompileToSpirV(hlsl, "Pixel", ShaderStage.Pixel, debug);

using ShaderModule vModule = Device.CreateShaderModule(ShaderStage.Vertex, vSpv, "Vertex");
using ShaderModule pModule = Device.CreateShaderModule(ShaderStage.Pixel, pSpv, "Pixel");
Expand Down

0 comments on commit de0a9fb

Please sign in to comment.