private void EvaluateSizeAndAlignment() { if (Fields.Count == 0) { _sizeInBytes = 0; _alignment = 0; return; } uint size = 0; uint maxAlignment = 16; foreach (var field in _fields) { var alignment = field.Type.Alignment; if (maxAlignment < alignment) { maxAlignment = alignment; } var end = field.ByteOffset.Value + field.Type.SizeInBytes; if (end > size) { size = end; } } _alignment = maxAlignment; _sizeInBytes = SpirvUtils.RoundUp(size, maxAlignment); }
private void UpdateFieldOffsets() { uint offset = 0; foreach (var field in _fields) { var alignment = field.Type.Alignment; var pos = SpirvUtils.RoundUp(offset, alignment); field.ByteOffset = pos; offset = pos + field.Type.SizeInBytes; } }
public void TestPermutation(TypeStruct fieldSet) { var(shaderInstructions, shaders) = CompileShaderForFieldSet(fieldSet); var shaderReflection = new ShaderReflection(shaderInstructions); var structure = shaderReflection.Structures.Where(_ => _.DebugName == fieldSet.DebugName).First(); CompareStructureLayout(structure, fieldSet); var bufferSize = structure.SizeInBytes; bufferSize = SpirvUtils.RoundUp(bufferSize, 16); var buffer = ResourceFactory.CreateBuffer(new BufferDescription(bufferSize, BufferUsage.UniformBuffer)); Disposables.Add(buffer); var bytes = new byte[bufferSize]; var counter = 1; var hash = 0; PopulateBuffer(bytes, 0, structure, ref counter, ref hash); GraphicsDevice.UpdateBuffer(buffer, 0, bytes); //structure.EvaluateLayout(); //var hlsl = SpirvCompilation.CompileVertexFragment(vertex.SpirvBytes, fragment.SpirvBytes, CrossCompileTarget.HLSL, new CrossCompileOptions()); //var glsl = SpirvCompilation.CompileVertexFragment(vertex.SpirvBytes, fragment.SpirvBytes, CrossCompileTarget.GLSL, new CrossCompileOptions()); //var msl = SpirvCompilation.CompileVertexFragment(vertex.SpirvBytes, fragment.SpirvBytes, CrossCompileTarget.MSL, new CrossCompileOptions()); //var essl = SpirvCompilation.CompileVertexFragment(vertex.SpirvBytes, fragment.SpirvBytes, CrossCompileTarget.ESSL, new CrossCompileOptions()); var layout = ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("ModelBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex))); Disposables.Add(layout); var resourceSet = ResourceFactory.CreateResourceSet(new ResourceSetDescription(layout, buffer)); Disposables.Add(resourceSet); var pd = new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.Disabled, RasterizerStateDescription.Default, PrimitiveTopology.PointList, new ShaderSetDescription(new[] { new VertexLayoutDescription(new VertexElementDescription[] { }) }, shaders), new[] { layout }, Framebuffer.OutputDescription); var pipeline = ResourceFactory.CreateGraphicsPipeline(pd); Disposables.Add(pipeline); CommandList.Begin(); CommandList.SetFramebuffer(Framebuffer); CommandList.SetPipeline(pipeline); CommandList.SetGraphicsResourceSet(0, resourceSet); CommandList.Draw(1); CommandList.End(); GraphicsDevice.SubmitCommands(CommandList); GraphicsDevice.WaitForIdle(); var expected = new RgbaByte((byte)(hash % 256), (byte)(hash / 256 % 256), (byte)(hash / 65536 % 256), (byte)(hash / 16777216 % 256)); var actual = ReadRenderTargetPixel(); Assert.AreEqual(expected, actual); }