Exemplo n.º 1
0
        public unsafe InlineUniformBlock(ShaderResourceInfo resourceInfo)
        {
            this.resourceInfo = resourceInfo;
            this.size         = resourceInfo.size;
            this.data         = Utilities.Alloc((int)this.size);

            inlineUniformBlockEXT           = (VkWriteDescriptorSetInlineUniformBlockEXT *)Utilities.Alloc <VkWriteDescriptorSetInlineUniformBlockEXT>();
            inlineUniformBlockEXT->sType    = VkStructureType.WriteDescriptorSetInlineUniformBlockEXT;
            inlineUniformBlockEXT->pNext    = null;
            inlineUniformBlockEXT->pData    = (void *)this.data;
            inlineUniformBlockEXT->dataSize = this.size;
        }
Exemplo n.º 2
0
        public static ShaderReflection ReflectionShaderModule(string source, IntPtr bytecode, uint len)
        {
            ShaderReflection refl = new ShaderReflection();

            refl.descriptorSets = new List <ShaderResourceInfo>();

            using (var context = new SharpSPIRVCross.Context())
            {
                var ir       = context.ParseIr(bytecode, len);
                var compiler = context.CreateCompiler(Backend.GLSL, ir);

                var caps       = compiler.GetDeclaredCapabilities();
                var extensions = compiler.GetDeclaredExtensions();
                var resources  = compiler.CreateShaderResources();

                Action <SharpSPIRVCross.ResourceType> CollectShaderResources = (SharpSPIRVCross.ResourceType resourceType) =>
                {
                    if (resourceType == SharpSPIRVCross.ResourceType.PushConstant)
                    {
                        foreach (var rs in resources.GetResources(resourceType))
                        {
                            var set     = compiler.GetDecoration(rs.Id, SpvDecoration.DescriptorSet);
                            var binding = compiler.GetDecoration(rs.Id, SpvDecoration.Binding);
                            var type    = compiler.GetSpirvType(rs.TypeId);

                            if (type.MemberCount > 0)
                            {
                                refl.pushConstants = new List <BufferMember>();
                                compiler.GetDeclaredStructSize(type, out int size);

                                //Console.WriteLine($"  struct, size:{size}");
                                //u.size = (uint)size;
                                for (int i = 0; i < type.MemberCount; i++)
                                {
                                    var memberName = compiler.GetMemberName(rs.BaseTypeId, (uint)i);

                                    compiler.GetStructMemberOffset(type, i, out int offset);
                                    compiler.GetDeclaredStructMemberSize(type, i, out int memberSize);
                                    //Console.WriteLine($"  Member Name:{memberName}, Offset:{offset}, size:{memberSize}");
                                    refl.pushConstants.Add(new BufferMember {
                                        name = memberName, offset = offset, size = memberSize
                                    });
                                }
                            }
                        }


                        return;
                    }



                    foreach (var rs in resources.GetResources(resourceType))
                    {
                        //Console.WriteLine($"ID: {rs.Id}, BaseTypeID: {rs.BaseTypeId}, TypeID: {rs.TypeId}, Name: {rs.Name})");
                        var set     = compiler.GetDecoration(rs.Id, SpvDecoration.DescriptorSet);
                        var binding = compiler.GetDecoration(rs.Id, SpvDecoration.Binding);
                        var type    = compiler.GetSpirvType(rs.TypeId);
                        //Console.WriteLine($"  Set: {set}, Binding: {binding}");

                        bool isDynamic = rs.Name.EndsWith("dynamic", StringComparison.CurrentCultureIgnoreCase);
                        bool isInline  = rs.Name.EndsWith("inline", StringComparison.CurrentCultureIgnoreCase);

                        var descriptorType = resourceType switch
                        {
                            SharpSPIRVCross.ResourceType.UniformBuffer => isInline ? VkDescriptorType.InlineUniformBlockEXT
                                :(isDynamic ? VkDescriptorType.UniformBufferDynamic : VkDescriptorType.UniformBuffer),
                            SharpSPIRVCross.ResourceType.StorageBuffer => isDynamic ? VkDescriptorType.StorageBufferDynamic : VkDescriptorType.StorageBuffer,
                            SharpSPIRVCross.ResourceType.StageInput => throw new NotImplementedException(),
                                  SharpSPIRVCross.ResourceType.StageOutput => throw new NotImplementedException(),
                                        SharpSPIRVCross.ResourceType.SubpassInput => VkDescriptorType.InputAttachment,
                                        SharpSPIRVCross.ResourceType.StorageImage => type.ImageDimension == SpvDim.DimBuffer ? VkDescriptorType.StorageTexelBuffer : VkDescriptorType.StorageImage,
                                        SharpSPIRVCross.ResourceType.SampledImage => VkDescriptorType.CombinedImageSampler,
                                        SharpSPIRVCross.ResourceType.AtomicCounter => throw new NotImplementedException(),
                                              SharpSPIRVCross.ResourceType.PushConstant => throw new NotImplementedException(),
                                                    SharpSPIRVCross.ResourceType.SeparateImage => VkDescriptorType.SampledImage,
                                                    SharpSPIRVCross.ResourceType.SeparateSamplers => VkDescriptorType.Sampler,
                                                    SharpSPIRVCross.ResourceType.AccelerationStructure => throw new NotImplementedException(),
                                                          _ => throw new NotImplementedException(),
                        };

                        var u = new ShaderResourceInfo
                        {
                            name           = rs.Name,
                            set            = (int)set,
                            binding        = binding,
                            descriptorType = descriptorType
                        };

                        if (type.MemberCount > 0)
                        {
                            u.members = new List <BufferMember>();
                            compiler.GetDeclaredStructSize(type, out int size);
                            //Console.WriteLine($"  struct, size:{size}");
                            u.size = (uint)size;
                            for (int i = 0; i < type.MemberCount; i++)
                            {
                                var memberName = compiler.GetMemberName(rs.BaseTypeId, (uint)i);

                                compiler.GetStructMemberOffset(type, i, out int offset);
                                compiler.GetDeclaredStructMemberSize(type, i, out int memberSize);
                                //Console.WriteLine($"  Member Name:{memberName}, Offset:{offset}, size:{memberSize}");

//                                 compiler.GetStructMemberArrayStride(type, i, out int sz);
//                                 compiler.GetStructMemberMatrixStride(type, i, out int stride);
//                                 Console.WriteLine($"  ArrayStride:{sz}, MatrixStride:{stride}");

                                u.members.Add(new BufferMember {
                                    name = memberName, offset = offset, size = memberSize
                                });
                            }
                        }

                        refl.descriptorSets.Add(u);
                    }