Skip to content

Commit

Permalink
Made ShanqShader non-generic
Browse files Browse the repository at this point in the history
Added support for multiple input structures
  • Loading branch information
Andrew Armstrong authored and Andrew Armstrong committed Sep 22, 2016
1 parent b006ed5 commit c74feb6
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 215 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,7 @@ _Pvt_Extensions
.fake/

# Include contents of Lib folder
!libs/*
!libs/*

#Ignore local testing code
Test/
Binary file modified SharpVk/Lib/SharpVk.VkXml.dll
Binary file not shown.
12 changes: 9 additions & 3 deletions SharpVk/SharpVk.Shanq/ShanqQueryExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Remotion.Linq;
using Remotion.Linq.Clauses;
using SharpVk.Spirv;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -63,9 +64,14 @@ public IEnumerable<T> ExecuteCollection<T>(QueryModel queryModel)
hasBuiltInOutput |= field.GetCustomAttribute<BuiltInAttribute>() != null;
}

var inputType = queryModel.MainFromClause.ItemType;
var inputTypes = new List<Type> { queryModel.MainFromClause.ItemType };

foreach (var field in inputType.GetFields())
foreach(var clause in queryModel.BodyClauses.OfType<AdditionalFromClause>())
{
inputTypes.Add(clause.ItemType);
}

foreach (var field in inputTypes.SelectMany(type => type.GetFields()))
{
if (field.GetCustomAttribute<LocationAttribute>() != null)
{
Expand Down Expand Up @@ -153,7 +159,7 @@ public IEnumerable<T> ExecuteCollection<T>(QueryModel queryModel)
{
file.AddFunctionStatement(Op.OpStore, fieldMapping[field], valueId);
}
else if(builtinList.ContainsKey(field))
else if (builtinList.ContainsKey(field))
{
ResultId constantIndex = expressionVisitor.Visit(Expression.Constant(builtinList[field].Item3));
ResultId fieldId = file.GetNextResultId();
Expand Down
49 changes: 37 additions & 12 deletions SharpVk/SharpVk.Shanq/ShanqShader.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,64 @@
using Remotion.Linq.Parsing.Structure;
using SharpVk.Spirv;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpVk.Shanq
{
public static class ShanqShader<TInput>
public interface IShanqFactory
{
public static void CreateFragment<TOutput>(Stream outputStream, Func<IQueryable<TInput>, IQueryable<TOutput>> shaderFunction)
IQueryable<T> GetInput<T>();

IQueryable<T> GetBinding<T>();
}

internal class ShanqFactory
: IShanqFactory
{
private readonly ShanqQueryExecutor executor;

public ShanqFactory(ExecutionModel model, Stream outputStream)
{
Create(ExecutionModel.Fragment, outputStream, shaderFunction);
this.executor = new ShanqQueryExecutor(model, outputStream);
}

public IQueryable<T> GetBinding<T>()
{
throw new NotImplementedException();
}

public static void Create<TOutput>(ExecutionModel model, Stream outputStream, Func<IQueryable<TInput>, IQueryable<TOutput>> shaderFunction)
public IQueryable<T> GetInput<T>()
{
var queryable = new ShanqQueryable<TInput>(QueryParser.CreateDefault(), new ShanqQueryExecutor(model, outputStream));
return new ShanqQueryable<T>(QueryParser.CreateDefault(), this.executor);
}
}

public static class ShanqShader
{
public static void Create<TOutput>(ExecutionModel model, Stream outputStream, Func<IShanqFactory, IQueryable<TOutput>> shaderFunction)
{
var factory = new ShanqFactory(model, outputStream);

shaderFunction(queryable).ToArray();
shaderFunction(factory).ToArray();
}

public static void CreateFragment<TOutput>(Stream outputStream, Func<IShanqFactory, IQueryable<TOutput>> shaderFunction)
{
Create(ExecutionModel.Fragment, outputStream, shaderFunction);
}

public static ShaderModule CreateVertexModule<TOutput>(Device device, Func<IQueryable<TInput>, IQueryable<TOutput>> shaderFunction)
public static ShaderModule CreateVertexModule<TOutput>(Device device, Func<IShanqFactory, IQueryable<TOutput>> shaderFunction)
{
return CreateModule(device, ExecutionModel.Vertex, shaderFunction);
}

public static ShaderModule CreateFragmentModule<TOutput>(Device device, Func<IQueryable<TInput>, IQueryable<TOutput>> shaderFunction)
public static ShaderModule CreateFragmentModule<TOutput>(Device device, Func<IShanqFactory, IQueryable<TOutput>> shaderFunction)
{
return CreateModule(device, ExecutionModel.Fragment, shaderFunction);
}

private static ShaderModule CreateModule<TOutput>(Device device, ExecutionModel model, Func<IQueryable<TInput>, IQueryable<TOutput>> shaderFunction)
private static ShaderModule CreateModule<TOutput>(Device device, ExecutionModel model, Func<IShanqFactory, IQueryable<TOutput>> shaderFunction)
{
var shaderStream = new MemoryStream();

Expand Down
48 changes: 37 additions & 11 deletions SharpVk/Test Harness/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void MainLoop()
{
this.UpdateUniformBuffer();
this.DrawFrame();

Application.DoEvents();
}
}
Expand Down Expand Up @@ -191,6 +191,28 @@ private void TearDown()
this.imageAvailableSemaphore.Dispose();
this.imageAvailableSemaphore = null;

this.descriptorPool.Dispose();
this.descriptorPool = null;
this.descriptorSet = null;

this.device.FreeMemory(this.uniformBufferMemory);
this.uniformBufferMemory = null;

this.uniformBuffer.Dispose();
this.uniformBuffer = null;

this.device.FreeMemory(this.uniformStagingBufferMemory);
this.uniformStagingBufferMemory = null;

this.uniformStagingBuffer.Dispose();
this.uniformStagingBuffer = null;

this.device.FreeMemory(this.indexBufferMemory);
this.indexBufferMemory = null;

this.indexBuffer.Dispose();
this.indexBuffer = null;

this.device.FreeMemory(this.vertexBufferMemory);
this.vertexBufferMemory = null;

Expand All @@ -199,6 +221,7 @@ private void TearDown()

this.commandPool.Dispose();
this.commandPool = null;
this.commandBuffers = null;

foreach (var frameBuffer in this.frameBuffers)
{
Expand All @@ -218,6 +241,9 @@ private void TearDown()
}
this.swapChainImageViews = null;

this.descriptorSetLayout.Dispose();
this.descriptorSetLayout = null;

this.renderPass.Dispose();
this.renderPass = null;

Expand Down Expand Up @@ -247,7 +273,7 @@ private void UpdateUniformBuffer()
Proj = mat4.Perspective((float)Math.PI / 4f, this.swapChainExtent.Width / (float)this.swapChainExtent.Height, 0.1f, 10)
};

ubo.Proj[1,1] *= -1;
ubo.Proj[1, 1] *= -1;

uint uboSize = MemUtil.SizeOf<UniformBufferObject>();

Expand Down Expand Up @@ -495,7 +521,7 @@ private void CreateDescriptorSetLayout()
{
this.descriptorSetLayout = device.CreateDescriptorSetLayout(new DescriptorSetLayoutCreateInfo
{
Bindings = new []
Bindings = new[]
{
new DescriptorSetLayoutBinding
{
Expand All @@ -519,12 +545,12 @@ private void CreateGraphicsPipeline()
CodeSize = codeSize
});

var fragShader = ShanqShader<FragmentInput>.CreateFragmentModule(this.device,
fragmentInput => from input in fragmentInput
select new FragmentOutput
{
Colour = new vec4(input.Colour, 1)
});
var fragShader = ShanqShader.CreateFragmentModule(this.device,
shanq => from input in shanq.GetInput<FragmentInput>()
select new FragmentOutput
{
Colour = new vec4(input.Colour, 1)
});

var bindingDescription = Vertex.GetBindingDescription();
var attributeDescriptions = Vertex.GetAttributeDescriptions();
Expand Down Expand Up @@ -713,7 +739,7 @@ private void CreateUniformBuffer()
{
uint bufferSize = MemUtil.SizeOf<UniformBufferObject>();

this.CreateBuffer(bufferSize, BufferUsageFlags.TransferSource, MemoryPropertyFlags.HostVisible | MemoryPropertyFlags.HostCoherent, out this.uniformStagingBuffer, out this.uniformStagingBufferMemory);
this.CreateBuffer(bufferSize, BufferUsageFlags.TransferSource, MemoryPropertyFlags.HostVisible | MemoryPropertyFlags.HostCoherent, out this.uniformStagingBuffer, out this.uniformStagingBufferMemory);
this.CreateBuffer(bufferSize, BufferUsageFlags.TransferDestination | BufferUsageFlags.UniformBuffer, MemoryPropertyFlags.DeviceLocal, out this.uniformBuffer, out this.uniformBufferMemory);
}

Expand All @@ -738,7 +764,7 @@ private void CreateDescriptorSet()
this.descriptorSet = this.device.AllocateDescriptorSets(new DescriptorSetAllocateInfo
{
DescriptorPool = this.descriptorPool,
SetLayouts = new []
SetLayouts = new[]
{
this.descriptorSetLayout
}
Expand Down
Loading

1 comment on commit c74feb6

@FacticiusVir
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#12

Please sign in to comment.