public IEnumerable <Node> BuildDecorations(TypeStruct typeStruct, uint index) { if (Name != null) { yield return new MemberName() { Type = typeStruct, Member = index, Name = Name } } ; if (BuiltIn != null) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.BuiltIn(BuiltIn) } } ; if (ByteOffset != null) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Offset(ByteOffset.Value) } } ; if (RowMajor) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.RowMajor() } } ; if (ColMajor) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.ColMajor() } } ; if (MatrixStride != null) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.MatrixStride(MatrixStride.Value) } } ; if (Location != null) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Location(Location.Value) } } ; if (Component != null) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Location(Component.Value) } } ; if (NoPerspective) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.NoPerspective() } } ; if (Flat) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Flat() } } ; if (Patch) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Patch() } } ; if (Centroid) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Centroid() } } ; if (Sample) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Sample() } } ; if (Volatile) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Volatile() } } ; if (Coherent) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Coherent() } } ; if (NonWritable) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.NonWritable() } } ; if (NonReadable) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.NonReadable() } } ; if (Uniform) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Uniform() } } ; if (RelaxedPrecision) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.RelaxedPrecision() } } ; if (Invariant) { yield return new MemberDecorate() { StructureType = typeStruct, Member = index, Decoration = Decoration.Invariant() } } ; }
public static Variable gl_VertexIndex() { return(new Variable(SpirvTypeBase.Int.MakePointer(StorageClass.Input()), StorageClass.Input(), null, "gl_VertexIndex") .WithDecoration(Decoration.BuiltIn(BuiltIn.VertexIndex()))); }
public void GreenQuadReflectionTest() { byte[] vertexByteCode; byte[] framentByteCode; { var const_1f = new Constant(-1.0f); var const1f = new Constant(1.0f); var const0f = new Constant(0.0f); var const0i = new Constant(0); var arrayType = new TypeArray(SpirvTypeBase.Vec4, 4); var a = new ConstantComposite(SpirvTypeBase.Vec4, const_1f, const1f, const0f, const1f); var b = new ConstantComposite(SpirvTypeBase.Vec4, const1f, const1f, const1f, const1f); var c = new ConstantComposite(SpirvTypeBase.Vec4, const_1f, const_1f, const0f, const1f); var d = new ConstantComposite(SpirvTypeBase.Vec4, const1f, const_1f, const1f, const1f); var positions = new ConstantComposite(arrayType, a, b, c, d); var clipArrayType = new TypeArray(SpirvTypeBase.Float, 1); var gl_PerVertex = new TypeStruct("gl_PerVertex", new TypeStructureField(SpirvTypeBase.Vec4, "gl_Position") .WithDecoration(Decoration.BuiltIn(BuiltIn.Position())), new TypeStructureField(SpirvTypeBase.Float, "gl_PointSize") .WithDecoration(Decoration.BuiltIn(BuiltIn.PointSize())), new TypeStructureField(clipArrayType, "gl_ClipDistance") .WithDecoration(Decoration.BuiltIn(BuiltIn.ClipDistance())), new TypeStructureField(clipArrayType, "gl_CullDistance") .WithDecoration(Decoration.BuiltIn(BuiltIn.CullDistance())) ).WithDecoration(Decoration.Block()); var outputVar = new Variable(gl_PerVertex.MakePointer(StorageClass.Output()), StorageClass.Output(), null, ""); var localVar = new Variable( arrayType.MakePointer(StorageClass.Function()), StorageClass.Function(), null, "indexable"); Node positionPointer = new AccessChain(SpirvTypeBase.Vec4.MakePointer(StorageClass.Output()), outputVar, new[] { const0i }); var glVertexIndex = Variable.gl_VertexIndex(); Node position = new Load(SpirvTypeBase.Vec4, new AccessChain(SpirvTypeBase.Vec4.MakePointer(StorageClass.Function()), localVar, new Node[] { new Load(SpirvTypeBase.Int, glVertexIndex, null) }), null); var vertexShaderMain = new Function(Spv.FunctionControl.Enumerant.None, new TypeFunction() { ReturnType = SpirvTypeBase.Void }, "main") .SetUp(_ => _ .ThenLabel() .ThenStore(localVar, positions, null) .ThenStore(positionPointer, position, null) .ThenReturn() ); var reflection = new ShaderReflection() .WithCapability(Capability.Shader()) .WithExtInstImport("GLSL.std.450") .WithMemoryModel(AddressingModel.Logical(), MemoryModel.GLSL450()) .WithEntryPoint(ExecutionModel.Vertex(), vertexShaderMain, "main"); var shader = reflection .BuildShader(); vertexByteCode = shader.Build(); Console.WriteLine("-----------------------"); Node node = reflection.EntryPointInstructions.First().Value; while (node != null) { Console.WriteLine($".Then(new {node})"); node = node.GetNext(); } //var vertexSource = @"#version 450 //#extension GL_KHR_vulkan_glsl : enable //const vec4 QuadInfos[4] = //{ // vec4(-1, 1, 0, 1), // vec4(1, 1, 1, 1), // vec4(-1, -1, 0, 1), // vec4(1, -1, 1, 1), //}; //void main() //{ // gl_Position = QuadInfos[gl_VertexIndex]; //}"; //vertexByteCode = ShaderTestBase.CompileToBytecode(vertexSource, ShaderStages.Vertex, true, true); } { var const0f = new Constant(0.0f); var const1f = new Constant(1.0f); var redColor = new ConstantComposite(SpirvTypeBase.Vec4, const0f, const1f, const0f, const1f); var v = new Variable(SpirvTypeBase.Vec4.MakePointer(StorageClass.Output()), StorageClass.Output(), null, "fsout_Color0") { Location = 0 }; var fragmentShaderMain = new Function(Spv.FunctionControl.Enumerant.None, new TypeFunction(SpirvTypeBase.Void), "main") .SetUp(_ => _ .ThenLabel() .ThenStore(v, redColor, null) .ThenReturn() ); var reflection = new ShaderReflection() .WithCapability(Capability.Shader()) .WithExtInstImport("GLSL.std.450") .WithMemoryModel(AddressingModel.Logical(), MemoryModel.GLSL450()) .WithEntryPoint(ExecutionModel.Fragment(), fragmentShaderMain, "main") .WithExecutionMode(fragmentShaderMain, ExecutionMode.OriginUpperLeft()); var shader = reflection .BuildShader(); framentByteCode = shader .Build(); Console.WriteLine("-----------------------"); Node node = reflection.EntryPointInstructions.First().Value; while (node != null) { Console.WriteLine($".Then(new {node})"); node = node.GetNext(); } // var fragmentSource = @"#version 450 //layout(location = 0) out vec4 fsout_Color0; //void main() //{ // fsout_Color0 = vec4(0,1,0,1); //}"; // framentByteCode = ShaderTestBase.CompileToBytecode(fragmentSource, ShaderStages.Fragment, true, true); } { var glsl = Veldrid.SPIRV.SpirvCompilation.CompileVertexFragment(vertexByteCode, framentByteCode, CrossCompileTarget.GLSL); Console.WriteLine(glsl.VertexShader); Console.WriteLine(glsl.FragmentShader); } var shaders = ResourceFactory.CreateFromSpirv( new ShaderDescription(ShaderStages.Vertex, vertexByteCode, "main"), new ShaderDescription(ShaderStages.Fragment, framentByteCode, "main")); var readRenderTargetPixel = RenderQuad(shaders); Assert.AreEqual(new RgbaByte(0, 255, 0, 255), readRenderTargetPixel); }