Пример #1
0
        public static VkPipeline CreateComputePipeline(ref VkComputePipelineCreateInfo pCreateInfos)
        {
            VkPipeline pPipelines;

            VulkanUtil.CheckResult(vkCreateComputePipelines(device, pipelineCache, 1, Utilities.AsPtr(ref pCreateInfos), null, &pPipelines));
            return(pPipelines);
        }
Пример #2
0
        public void Build(int deviceIndex)
        {
            if (!locked)
            {
                unsafe
                {
                    var memHndl  = SpecializationData.Pin();
                    var specData = new VkSpecializationInfo()
                    {
                        mapEntryCount = (uint)SpecializationData.Length,
                        dataSize      = (uint)SpecializationData.Length * sizeof(int),
                        pMapEntries   = Shader.Specialize(),
                        pData         = (IntPtr)memHndl.Pointer
                    };
                    var specData_ptr = specData.Pointer();

                    var creatInfo = new VkComputePipelineCreateInfo()
                    {
                        sType = VkStructureType.StructureTypeComputePipelineCreateInfo,
                        stage = new VkPipelineShaderStageCreateInfo()
                        {
                            sType  = VkStructureType.StructureTypePipelineShaderStageCreateInfo,
                            stage  = (VkShaderStageFlags)Shader.ShaderType,
                            module = Shader.ids[deviceIndex],
                            pName  = "main",
                            pSpecializationInfo = specData_ptr
                        },
                        layout = PipelineLayout.hndl,
                    };

                    IntPtr pipeline_l = IntPtr.Zero;
                    if (vkCreateComputePipelines(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, IntPtr.Zero, 1, creatInfo.Pointer(), null, &pipeline_l) != VkResult.Success)
                    {
                        throw new Exception("Failed to create compute pipeline.");
                    }
                    hndl = pipeline_l;

                    if (GraphicsDevice.EnableValidation)
                    {
                        var objName = new VkDebugUtilsObjectNameInfoEXT()
                        {
                            sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                            pObjectName  = Name,
                            objectType   = VkObjectType.ObjectTypePipeline,
                            objectHandle = (ulong)hndl
                        };
                        GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, objName.Pointer());
                    }

                    devID  = deviceIndex;
                    locked = true;
                }
            }
            else
            {
                throw new Exception("Pipeline is locked.");
            }
        }
Пример #3
0
        public VkPipeline(VkGraphicsDevice gd, ref ComputePipelineDescription description)
            : base(ref description)
        {
            _gd = gd;
            IsComputePipeline = true;

            VkComputePipelineCreateInfo pipelineCI = VkComputePipelineCreateInfo.New();

            // Pipeline Layout
            ResourceLayout[]           resourceLayouts  = description.ResourceLayouts;
            VkPipelineLayoutCreateInfo pipelineLayoutCI = VkPipelineLayoutCreateInfo.New();

            pipelineLayoutCI.setLayoutCount = (uint)resourceLayouts.Length;
            VkDescriptorSetLayout *dsls = stackalloc VkDescriptorSetLayout[resourceLayouts.Length];

            for (int i = 0; i < resourceLayouts.Length; i++)
            {
                dsls[i] = Util.AssertSubtype <ResourceLayout, VkResourceLayout>(resourceLayouts[i]).DescriptorSetLayout;
            }
            pipelineLayoutCI.pSetLayouts = dsls;

            vkCreatePipelineLayout(_gd.Device, ref pipelineLayoutCI, null, out _pipelineLayout);
            pipelineCI.layout = _pipelineLayout;

            // Shader Stage
            Shader   shader   = description.ComputeShader;
            VkShader vkShader = Util.AssertSubtype <Shader, VkShader>(shader);
            VkPipelineShaderStageCreateInfo stageCI = VkPipelineShaderStageCreateInfo.New();

            stageCI.module   = vkShader.ShaderModule;
            stageCI.stage    = VkFormats.VdToVkShaderStages(shader.Stage);
            stageCI.pName    = CommonStrings.main; // Meh
            pipelineCI.stage = stageCI;

            VkResult result = vkCreateComputePipelines(
                _gd.Device,
                VkPipelineCache.Null,
                1,
                ref pipelineCI,
                null,
                out _devicePipeline);

            CheckResult(result);

            ResourceSetCount = (uint)description.ResourceLayouts.Length;
        }
Пример #4
0
        public override void Activate()
        {
            if (state != ActivableState.Activated)
            {
                layout.Activate();
                Cache?.Activate();

                using (ShaderInfo shader = new ShaderInfo(Dev, VkShaderStageFlags.Compute, SpirVPath)) {
                    VkComputePipelineCreateInfo info = VkComputePipelineCreateInfo.New();
                    info.layout             = layout.Handle;
                    info.stage              = shader.Info;
                    info.basePipelineHandle = 0;
                    info.basePipelineIndex  = 0;

                    Utils.CheckResult(Vk.vkCreateComputePipelines(Dev.VkDev, Cache == null ? VkPipelineCache.Null : Cache.handle, 1, ref info, IntPtr.Zero, out handle));
                }
            }
            base.Activate();
        }
Пример #5
0
        static internal VkPipeline[] CreatePipelinesInternal(Device device, ComputePipelineCreateInfo[] mInfos, VkPipelineCache cache)
        {
            int count            = mInfos.Length;
            var infosMarshalled  = new MarshalledArray <VkComputePipelineCreateInfo>(count);
            var pipelineResults  = new VkPipeline[count];
            var marshalledArrays = new DisposableList <IDisposable>(count);

            for (int i = 0; i < count; i++)
            {
                var mInfo = mInfos[i];
                VkComputePipelineCreateInfo info = new VkComputePipelineCreateInfo();
                info.sType = VkStructureType.ComputePipelineCreateInfo;
                info.flags = mInfo.flags;

                info.stage = mInfo.stage.GetNative(marshalledArrays);

                info.layout = mInfo.layout.Native;
                if (mInfo.basePipelineHandle != null)
                {
                    info.basePipelineHandle = mInfo.basePipelineHandle.Native;
                }
                info.basePipelineIndex = mInfo.basePipelineIndex;

                infosMarshalled[i] = info;
            }

            using (infosMarshalled)
                using (marshalledArrays)
                    using (var pipelinesMarshalled = new PinnedArray <VkPipeline>(pipelineResults)) {
                        var result = device.Commands.createComputePipelines(
                            device.Native, cache,
                            (uint)count, infosMarshalled.Address,
                            device.Instance.AllocationCallbacks, pipelinesMarshalled.Address);

                        if (result != VkResult.Success)
                        {
                            throw new PipelineException(string.Format("Error creating pipeline: {0}", result));
                        }
                        return(pipelineResults);
                    }
        }
Пример #6
0
        public VkPipeline(VkGraphicsDevice gd, ref ComputePipelineDescription description)
            : base(ref description)
        {
            _gd = gd;
            IsComputePipeline = true;
            RefCount          = new ResourceRefCount(DisposeCore);

            VkComputePipelineCreateInfo pipelineCI = VkComputePipelineCreateInfo.New();

            // Pipeline Layout
            ResourceLayout[]           resourceLayouts  = description.ResourceLayouts;
            VkPipelineLayoutCreateInfo pipelineLayoutCI = VkPipelineLayoutCreateInfo.New();

            pipelineLayoutCI.setLayoutCount = (uint)resourceLayouts.Length;
            VkDescriptorSetLayout *dsls = stackalloc VkDescriptorSetLayout[resourceLayouts.Length];

            for (int i = 0; i < resourceLayouts.Length; i++)
            {
                dsls[i] = Util.AssertSubtype <ResourceLayout, VkResourceLayout>(resourceLayouts[i]).DescriptorSetLayout;
            }
            pipelineLayoutCI.pSetLayouts = dsls;

            vkCreatePipelineLayout(_gd.Device, ref pipelineLayoutCI, null, out _pipelineLayout);
            pipelineCI.layout = _pipelineLayout;

            // Shader Stage

            VkSpecializationInfo specializationInfo;

            SpecializationConstant[] specDescs = description.Specializations;
            if (specDescs != null)
            {
                uint specDataSize = 0;
                foreach (SpecializationConstant spec in specDescs)
                {
                    specDataSize += VkFormats.GetSpecializationConstantSize(spec.Type);
                }
                byte *fullSpecData                   = stackalloc byte[(int)specDataSize];
                int   specializationCount            = specDescs.Length;
                VkSpecializationMapEntry *mapEntries = stackalloc VkSpecializationMapEntry[specializationCount];
                uint specOffset = 0;
                for (int i = 0; i < specializationCount; i++)
                {
                    ulong data     = specDescs[i].Data;
                    byte *srcData  = (byte *)&data;
                    uint  dataSize = VkFormats.GetSpecializationConstantSize(specDescs[i].Type);
                    Unsafe.CopyBlock(fullSpecData + specOffset, srcData, dataSize);
                    mapEntries[i].constantID = specDescs[i].ID;
                    mapEntries[i].offset     = specOffset;
                    mapEntries[i].size       = (UIntPtr)dataSize;
                    specOffset += dataSize;
                }
                specializationInfo.dataSize      = (UIntPtr)specDataSize;
                specializationInfo.pData         = fullSpecData;
                specializationInfo.mapEntryCount = (uint)specializationCount;
                specializationInfo.pMapEntries   = mapEntries;
            }

            Shader   shader   = description.ComputeShader;
            VkShader vkShader = Util.AssertSubtype <Shader, VkShader>(shader);
            VkPipelineShaderStageCreateInfo stageCI = VkPipelineShaderStageCreateInfo.New();

            stageCI.module = vkShader.ShaderModule;
            stageCI.stage  = VkFormats.VdToVkShaderStages(shader.Stage);
            stageCI.pName  = CommonStrings.main; // Meh
            stageCI.pSpecializationInfo = &specializationInfo;
            pipelineCI.stage            = stageCI;

            VkResult result = vkCreateComputePipelines(
                _gd.Device,
                VkPipelineCache.Null,
                1,
                ref pipelineCI,
                null,
                out _devicePipeline);

            CheckResult(result);

            ResourceSetCount    = (uint)description.ResourceLayouts.Length;
            DynamicOffsetsCount = 0;
            foreach (VkResourceLayout layout in description.ResourceLayouts)
            {
                DynamicOffsetsCount += layout.DynamicBufferCount;
            }
        }
Пример #7
0
        public static VkResult vkCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, VkComputePipelineCreateInfo createInfo, out VkPipeline pipeline)
        {
            VkPipeline pinPipeline;
            var        result = vkCreateComputePipelines(device, pipelineCache, 1, &createInfo, null, &pinPipeline);

            pipeline = pinPipeline;
            return(result);
        }
Пример #8
0
 public static VkResult vkCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, VkComputePipelineCreateInfo createInfo, out VkPipeline pipeline)
 {
     return(vkCreateComputePipelines(device, pipelineCache, 1, &createInfo, null, out pipeline));
 }