Пример #1
0
        public void Build(CommandPool pool)
        {
            if (!locked)
            {
                unsafe
                {
                    var allocInfo = new VkCommandBufferAllocateInfo()
                    {
                        sType = VkStructureType.StructureTypeCommandBufferAllocateInfo,
                        commandBufferCount = 1,
                        commandPool        = pool.hndl,
                        level = VkCommandBufferLevel.CommandBufferLevelPrimary,
                    };


                    devID = pool.devID;
                    if (pool.queueFamily == GraphicsDevice.DeviceInformation[devID].ComputeFamily)
                    {
                        IsRadRayStream = true;
                    }

                    IntPtr cmdBufferPtr_l = IntPtr.Zero;
                    if (vkAllocateCommandBuffers(GraphicsDevice.GetDeviceInfo(devID).Device, allocInfo.Pointer(), &cmdBufferPtr_l) != VkResult.Success)
                    {
                        throw new Exception("Failed to allocate command buffer.");
                    }
                    cmdPool = pool;
                    hndl    = cmdBufferPtr_l;
                    IsEmpty = true;

                    if (IsRadRayStream)
                    {
                        IntPtr radRayStrmPtr_l = IntPtr.Zero;
                        if (rrGetCommandStreamFromVkCommandBuffer(GraphicsDevice.DeviceInformation[devID].RaysContext, cmdBufferPtr_l, &radRayStrmPtr_l) != RRError.RrSuccess)
                        {
                            throw new Exception("Failed to create RadeonRays stream.");
                        }
                        radRayStream = radRayStrmPtr_l;
                    }

                    if (GraphicsDevice.EnableValidation)
                    {
                        var objName = new VkDebugUtilsObjectNameInfoEXT()
                        {
                            sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                            pObjectName  = Name,
                            objectType   = VkObjectType.ObjectTypeCommandBuffer,
                            objectHandle = (ulong)hndl
                        };
                        GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(devID).Device, objName.Pointer());
                    }
                }
                locked = true;
            }
            else
            {
                throw new Exception("Command buffer locked.");
            }
        }
Пример #2
0
        public void Build(int device_index, CommandQueueKind queue)
        {
            if (!locked)
            {
                unsafe
                {
                    var devInfo = GraphicsDevice.GetDeviceInfo(device_index);
                    queueFamily = queue switch
                    {
                        CommandQueueKind.Graphics => devInfo.GraphicsFamily,
                        CommandQueueKind.Compute => devInfo.ComputeFamily,
                        CommandQueueKind.Transfer => devInfo.TransferFamily,
                        CommandQueueKind.Present => devInfo.PresentFamily,
                        _ => throw new Exception("Unknown command queue type.")
                    };
                    queueFam = queue switch
                    {
                        CommandQueueKind.Graphics => devInfo.GraphicsQueue,
                        CommandQueueKind.Compute => devInfo.ComputeQueue,
                        CommandQueueKind.Transfer => devInfo.TransferQueue,
                        CommandQueueKind.Present => devInfo.PresentQueue,
                        _ => throw new Exception("Unknown command queue type.")
                    };

                    var poolInfo = new VkCommandPoolCreateInfo()
                    {
                        sType            = VkStructureType.StructureTypeCommandPoolCreateInfo,
                        queueFamilyIndex = queueFamily,
                        flags            = Transient ? VkCommandPoolCreateFlags.CommandPoolCreateTransientBit : VkCommandPoolCreateFlags.CommandPoolCreateResetCommandBufferBit,
                    };

                    IntPtr commandPoolPtr_l = IntPtr.Zero;
                    if (vkCreateCommandPool(devInfo.Device, poolInfo.Pointer(), null, &commandPoolPtr_l) != VkResult.Success)
                    {
                        throw new Exception("Failed to create command pool.");
                    }
                    hndl  = commandPoolPtr_l;
                    devID = device_index;

                    if (GraphicsDevice.EnableValidation && Name != null)
                    {
                        var objName = new VkDebugUtilsObjectNameInfoEXT()
                        {
                            sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                            pObjectName  = Name,
                            objectType   = VkObjectType.ObjectTypeCommandPool,
                            objectHandle = (ulong)hndl
                        };
                        GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(devID).Device, objName.Pointer());
                    }
                }
                locked = true;
            }
            else
            {
                throw new Exception("CommandPool is locked.");
            }
        }
Пример #3
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.");
            }
        }
Пример #4
0
        public void Build(int deviceIndex)
        {
            if (!locked)
            {
                unsafe
                {
                    var samplerCreatInfo = new VkSamplerCreateInfo()
                    {
                        sType                   = VkStructureType.StructureTypeSamplerCreateInfo,
                        magFilter               = MagLinearFilter ? VkFilter.FilterLinear : VkFilter.FilterNearest,
                        minFilter               = MinLinearFilter ? VkFilter.FilterLinear : VkFilter.FilterNearest,
                        mipmapMode              = MipLinearFilter ? VkSamplerMipmapMode.SamplerMipmapModeLinear : VkSamplerMipmapMode.SamplerMipmapModeNearest,
                        addressModeU            = (VkSamplerAddressMode)EdgeU,
                        addressModeV            = (VkSamplerAddressMode)EdgeV,
                        addressModeW            = (VkSamplerAddressMode)EdgeW,
                        mipLodBias              = 0,
                        anisotropyEnable        = AnisotropicSamples == 0,
                        maxAnisotropy           = AnisotropicSamples,
                        compareEnable           = false,
                        compareOp               = VkCompareOp.CompareOpAlways,
                        minLod                  = -1000,
                        maxLod                  = 1000,
                        borderColor             = (VkBorderColor)Border,
                        unnormalizedCoordinates = UnnormalizedCoords
                    };

                    IntPtr samplerPtr_l = IntPtr.Zero;
                    if (vkCreateSampler(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, samplerCreatInfo.Pointer(), null, &samplerPtr_l) != VkResult.Success)
                    {
                        throw new Exception("Failed to create sampler.");
                    }
                    hndl  = samplerPtr_l;
                    devID = deviceIndex;

                    if (GraphicsDevice.EnableValidation)
                    {
                        var objName = new VkDebugUtilsObjectNameInfoEXT()
                        {
                            sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                            pObjectName  = Name,
                            objectType   = VkObjectType.ObjectTypeSampler,
                            objectHandle = (ulong)hndl
                        };
                        GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, objName.Pointer());
                    }
                }
                locked = true;
            }
            else
            {
                throw new Exception("Sampler is locked.");
            }
        }
Пример #5
0
        /// <summary>
        /// if debug marker extension is activated, this will set the name for debuggers
        /// </summary>
        public void SetName(string name)
        {
            this.name = name;

            if (!Dev.debugUtilsEnabled)
            {
                return;
            }

            VkDebugUtilsObjectNameInfoEXT dmo = DebugUtilsInfo;

            dmo.pObjectName = name.Pin();
            Utils.CheckResult(vkSetDebugUtilsObjectNameEXT(Dev.VkDev, ref dmo));
            name.Unpin();
        }
Пример #6
0
        public static void SetDebugMarkerName(this VkFence obj, Device dev, string name)
        {
            if (!dev.debugUtilsEnabled)
            {
                return;
            }
            VkDebugUtilsObjectNameInfoEXT dmo = new VkDebugUtilsObjectNameInfoEXT(VkObjectType.Fence,
                                                                                  (ulong)obj.Handle)
            {
                pObjectName = name.Pin()
            };

            Utils.CheckResult(vkSetDebugUtilsObjectNameEXT(dev.VkDev, ref dmo));
            name.Unpin();
        }
Пример #7
0
        public void Build(int devId)
        {
            if (!locked)
            {
                if (Layout.Layouts.Count == 0 | Pool.PoolEntries.Count == 0)
                {
                    return;
                }

                unsafe
                {
                    var layout_sets = stackalloc IntPtr[] { Layout.hndl };

                    var desc_set_alloc_info = new VkDescriptorSetAllocateInfo()
                    {
                        sType              = VkStructureType.StructureTypeDescriptorSetAllocateInfo,
                        descriptorPool     = Pool.hndl,
                        descriptorSetCount = 1,
                        pSetLayouts        = layout_sets,
                    };

                    fixed(IntPtr *hndl_p = &hndl)
                    if (vkAllocateDescriptorSets(GraphicsDevice.GetDeviceInfo(devId).Device, desc_set_alloc_info.Pointer(), hndl_p) != VkResult.Success)
                    {
                        throw new Exception("Failed to allocate descriptor sets.");
                    }
                    devID = devId;

                    if (GraphicsDevice.EnableValidation)
                    {
                        var objName = new VkDebugUtilsObjectNameInfoEXT()
                        {
                            sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                            pObjectName  = Name,
                            objectType   = VkObjectType.ObjectTypeDescriptorSet,
                            objectHandle = (ulong)hndl
                        };
                        GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(devID).Device, objName.Pointer());
                    }
                }
                locked = true;
            }
            else
            {
                throw new Exception("DescriptorSet is locked.");
            }
        }
Пример #8
0
        internal GpuQueue(CommandQueueKind queue_kind, IntPtr hndl, uint family, int device_index)
        {
            QueueKind   = queue_kind;
            Handle      = hndl;
            Family      = family;
            DeviceIndex = device_index;

            if (GraphicsDevice.EnableValidation)
            {
                var objName = new VkDebugUtilsObjectNameInfoEXT()
                {
                    sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                    pObjectName  = $"{QueueKind.ToString()}_{family}",
                    objectType   = VkObjectType.ObjectTypeQueue,
                    objectHandle = (ulong)hndl
                };

                var p = objName.Pointer();
                GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(device_index).Device, p);
            }
        }
Пример #9
0
        public void Build(int devID)
        {
            if (!locked)
            {
                unsafe
                {
                    var fenceCreateInfo = new VkFenceCreateInfo()
                    {
                        sType = VkStructureType.StructureTypeFenceCreateInfo,
                        flags = CreateSignaled ? VkFenceCreateFlags.FenceCreateSignaledBit : 0
                    };

                    IntPtr hndl_l = IntPtr.Zero;
                    if (vkCreateFence(GraphicsDevice.GetDeviceInfo(devID).Device, fenceCreateInfo.Pointer(), null, &hndl_l) != VkResult.Success)
                    {
                        throw new Exception("Failed to create fence.");
                    }
                    hndl       = hndl_l;
                    this.devID = devID;

                    if (GraphicsDevice.EnableValidation && Name != null)
                    {
                        var objName = new VkDebugUtilsObjectNameInfoEXT()
                        {
                            sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                            pObjectName  = Name,
                            objectType   = VkObjectType.ObjectTypeFence,
                            objectHandle = (ulong)hndl
                        };
                        GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(devID).Device, objName.Pointer());
                    }
                }
                locked = true;
            }
            else
            {
                throw new Exception("Fence is locked.");
            }
        }
Пример #10
0
        public void Build(int deviceIndex)
        {
            if (!locked)
            {
                unsafe
                {
                    devID = deviceIndex;

                    var descLayouts = stackalloc IntPtr[Descriptors == null ? 0 : Descriptors.Length];
                    if (Descriptors != null)
                    {
                        for (int i = 0; i < Descriptors.Length; i++)
                        {
                            descLayouts[i] = Descriptors[i].Layout.hndl;
                        }
                    }

                    var pushConstants = new VkPushConstantRange[PushConstants == null ? 0 : PushConstants.Length];
                    if (PushConstants != null)
                    {
                        for (int i = 0; i < PushConstants.Length; i++)
                        {
                            pushConstants[i] = new VkPushConstantRange()
                            {
                                offset     = PushConstants[i].Offset,
                                size       = PushConstants[i].Size,
                                stageFlags = (VkShaderStageFlags)PushConstants[i].Stages
                            }
                        }
                    }
                    ;
                    var pushConstants_arr = pushConstants.Pointer();

                    var pipelineLayoutInfo = new VkPipelineLayoutCreateInfo()
                    {
                        sType                  = VkStructureType.StructureTypePipelineLayoutCreateInfo,
                        setLayoutCount         = Descriptors == null ? 0 : (uint)Descriptors.Length,     //TODO: add descriptor support
                        pSetLayouts            = descLayouts,
                        pushConstantRangeCount = PushConstants == null ? 0 : (uint)PushConstants.Length, //TODO: setup push constants
                        pPushConstantRanges    = PushConstants == null ? IntPtr.Zero : pushConstants_arr,
                    };

                    IntPtr pipelineLayout_l = IntPtr.Zero;
                    if (vkCreatePipelineLayout(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, pipelineLayoutInfo.Pointer(), null, &pipelineLayout_l) != VkResult.Success)
                    {
                        throw new Exception("Failed to create pipeline layout.");
                    }
                    hndl = pipelineLayout_l;

                    if (GraphicsDevice.EnableValidation)
                    {
                        var objName = new VkDebugUtilsObjectNameInfoEXT()
                        {
                            sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                            pObjectName  = Name,
                            objectType   = VkObjectType.ObjectTypePipelineLayout,
                            objectHandle = (ulong)hndl
                        };
                        GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, objName.Pointer());
                    }
                }
                locked = true;
            }
            else
            {
                throw new Exception("PipelineLayout is locked.");
            }
        }
Пример #11
0
        public void Build(int deviceIndex)
        {
            if (!locked)
            {
                unsafe
                {
                    //Setup framebuffer
                    var attachmentCnt = (ColorAttachments == null ? 0 : ColorAttachments.Length) + (DepthAttachment == null ? 0 : 1);
                    var attachments   = stackalloc IntPtr[attachmentCnt];
                    if (ColorAttachments != null)
                    {
                        for (int i = 0; i < ColorAttachments.Length; i++)
                        {
                            attachments[i] = ColorAttachments[i].hndl;
                            if (ColorAttachments[i].Width != Width)
                            {
                                throw new Exception();
                            }
                            if (ColorAttachments[i].Height != Height)
                            {
                                throw new Exception();
                            }
                        }
                    }
                    if (DepthAttachment != null)
                    {
                        attachments[attachmentCnt - 1] = DepthAttachment.hndl;
                    }

                    var framebufferInfo = new VkFramebufferCreateInfo()
                    {
                        sType           = VkStructureType.StructureTypeFramebufferCreateInfo,
                        attachmentCount = (uint)attachmentCnt,
                        pAttachments    = attachments,
                        renderPass      = RenderPass.hndl,
                        width           = Width,
                        height          = Height,
                        layers          = 1
                    };

                    IntPtr framebuffer_l = IntPtr.Zero;
                    if (vkCreateFramebuffer(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, framebufferInfo.Pointer(), null, &framebuffer_l) != VkResult.Success)
                    {
                        throw new Exception("Failed to create framebuffer");
                    }
                    hndl = framebuffer_l;

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

                    locked = true;
                }
            }
            else
            {
                throw new Exception("Framebuffer is locked.");
            }
        }
Пример #12
0
        public void Build(int deviceIndex)
        {
            if (!locked)
            {
                var devInfo = GraphicsDevice.GetDeviceInfo(deviceIndex);

                unsafe
                {
                    fixed(uint *queueFamInds = devInfo.QueueFamilyIndices)
                    {
                        var creatInfo = new VkImageCreateInfo()
                        {
                            sType       = VkStructureType.StructureTypeImageCreateInfo,
                            flags       = 0,
                            format      = (VkFormat)Format,
                            usage       = (VkImageUsageFlags)Usage,
                            mipLevels   = Levels,
                            arrayLayers = Layers,
                            extent      = new VkExtent3D()
                            {
                                width  = Width,
                                height = Height,
                                depth  = Depth
                            },
                            samples               = VkSampleCountFlags.SampleCount1Bit,
                            tiling                = VkImageTiling.ImageTilingOptimal,
                            sharingMode           = VkSharingMode.SharingModeExclusive,
                            initialLayout         = (VkImageLayout)InitialLayout,
                            pQueueFamilyIndices   = queueFamInds,
                            queueFamilyIndexCount = (uint)devInfo.QueueFamilyIndices.Length,
                        };

                        creatInfo.imageType = Dimensions switch
                        {
                            1 => VkImageType.ImageType1d,
                            2 => VkImageType.ImageType2d,
                            3 => VkImageType.ImageType3d,
                            _ => throw new Exception("Unknown Image Shape.")
                        };
                        var vmaCreatInfo = new VmaAllocationCreateInfo()
                        {
                            usage = (VmaMemoryUsage)MemoryUsage
                        };

                        var allocInfo_p = new ManagedPtr <VmaAllocationInfo>();
                        var res         = GraphicsDevice.CreateImage(deviceIndex, creatInfo.Pointer(), vmaCreatInfo.Pointer(), out var img_l, out var imgAlloc_l, allocInfo_p);

                        hndl      = img_l;
                        imgAlloc  = imgAlloc_l;
                        allocInfo = allocInfo_p.Value;
                        devID     = deviceIndex;

                        CurrentLayout     = InitialLayout;
                        CurrentAccesses   = AccessFlags.None;
                        CurrentUsageStage = PipelineStage.Top;

                        if (GraphicsDevice.EnableValidation)
                        {
                            var objName = new VkDebugUtilsObjectNameInfoEXT()
                            {
                                sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                                pObjectName  = Name,
                                objectType   = VkObjectType.ObjectTypeImage,
                                objectHandle = (ulong)hndl
                            };
                            GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, objName.Pointer());
                        }
                    }
                }
                locked = true;
            }
            else
            {
                throw new Exception("Image is locked.");
            }
        }
Пример #13
0
        public void Build(int deviceIndex)
        {
            if (!locked)
            {
                unsafe
                {
                    //create pipeline shader stages
                    var shaderStages                   = new VkPipelineShaderStageCreateInfo[Shaders.Length];
                    var shaderSpecializations          = new ManagedPtr <VkSpecializationInfo> [Shaders.Length];
                    var shaderSpecializationBufferPtrs = new MemoryHandle[Shaders.Length];
                    for (int i = 0; i < shaderStages.Length; i++)
                    {
                        if (SpecializationData != null)
                        {
                            shaderSpecializationBufferPtrs[i] = SpecializationData[i].Pin();

                            shaderSpecializations[i] = new VkSpecializationInfo()
                            {
                                mapEntryCount = (uint)SpecializationData[i].Length,
                                pMapEntries   = Shaders[i].Specialize(),
                                dataSize      = (uint)SpecializationData[i].Length * sizeof(int),
                                pData         = (IntPtr)shaderSpecializationBufferPtrs[i].Pointer
                            }.Pointer();
                        }

                        shaderStages[i].sType  = VkStructureType.StructureTypePipelineShaderStageCreateInfo;
                        shaderStages[i].stage  = (VkShaderStageFlags)Shaders[i].ShaderType;
                        shaderStages[i].module = Shaders[i].ids[deviceIndex];
                        shaderStages[i].pName  = "main";
                        shaderStages[i].pSpecializationInfo = shaderSpecializations[i] != null ? shaderSpecializations[i] : IntPtr.Zero;
                    }
                    var shaderStages_ptr = shaderStages.Pointer();

                    //VkPipelineVertexInputStateCreateInfo - dummy, engine doesn't support fix function vertex input
                    var vertexInputInfo = new VkPipelineVertexInputStateCreateInfo()
                    {
                        sType = VkStructureType.StructureTypePipelineVertexInputStateCreateInfo,
                        vertexBindingDescriptionCount   = 0,
                        vertexAttributeDescriptionCount = 0,
                    };
                    var vertexInputInfo_ptr = vertexInputInfo.Pointer();

                    //VkPipelineInputAssemblyStateCreateInfo - state
                    var inputAssembly = new VkPipelineInputAssemblyStateCreateInfo()
                    {
                        sType    = VkStructureType.StructureTypePipelineInputAssemblyStateCreateInfo,
                        topology = (VkPrimitiveTopology)Topology,
                        primitiveRestartEnable = false,
                    };
                    var inputAssembly_ptr = inputAssembly.Pointer();

                    //VkPipelineViewportStateCreateInfo - dynamic state, no scissor
                    var viewport = new VkViewport()
                    {
                        x        = ViewportX,
                        y        = ViewportY,
                        width    = ViewportWidth,
                        height   = ViewportHeight,
                        minDepth = ViewportMinDepth,
                        maxDepth = ViewportMaxDepth,
                    };

                    var scissor = new VkRect2D()
                    {
                        offset = new VkOffset2D()
                        {
                            x = (int)ViewportX,
                            y = (int)ViewportY
                        },
                        extent = new VkExtent2D()
                        {
                            width  = ViewportWidth,
                            height = ViewportHeight
                        }
                    };

                    var viewport_ptr       = viewport.Pointer();
                    var scissor_ptr        = scissor.Pointer();
                    var viewportCreateInfo = new VkPipelineViewportStateCreateInfo()
                    {
                        sType         = VkStructureType.StructureTypePipelineViewportStateCreateInfo,
                        viewportCount = 1,
                        pViewports    = viewport_ptr,
                        scissorCount  = 1,
                        pScissors     = scissor_ptr
                    };
                    var viewportCreateInfo_ptr = viewportCreateInfo.Pointer();

                    //VkPipelineRasterizationStateCreateInfo - state
                    var rasterizer = new VkPipelineRasterizationStateCreateInfo()
                    {
                        sType                   = VkStructureType.StructureTypePipelineRasterizationStateCreateInfo,
                        depthClampEnable        = DepthClamp,
                        rasterizerDiscardEnable = RasterizerDiscard,
                        polygonMode             = (VkPolygonMode)Fill,
                        lineWidth               = LineWidth,
                        cullMode                = (VkCullModeFlags)CullMode,
                        frontFace               = VkFrontFace.FrontFaceCounterClockwise, //OpenGL default
                        depthBiasEnable         = false,
                        depthBiasConstantFactor = 0,
                        depthBiasClamp          = 0,
                        depthBiasSlopeFactor    = 0,
                    };
                    var rasterizer_ptr = rasterizer.Pointer();

                    //VkPipelineMultisampleStateCreateInfo - don't support for now
                    var multisampling = new VkPipelineMultisampleStateCreateInfo()
                    {
                        sType = VkStructureType.StructureTypePipelineMultisampleStateCreateInfo,
                        sampleShadingEnable   = false,
                        rasterizationSamples  = VkSampleCountFlags.SampleCount1Bit,
                        minSampleShading      = 1,
                        pSampleMask           = null,
                        alphaToCoverageEnable = false,
                        alphaToOneEnable      = false
                    };
                    var multisampling_ptr = multisampling.Pointer();

                    //VkPipelineDepthStencilStateCreateInfo - state
                    var depthStencil = new VkPipelineDepthStencilStateCreateInfo()
                    {
                        sType                 = VkStructureType.StructureTypePipelineDepthStencilStateCreateInfo,
                        depthTestEnable       = true,
                        depthCompareOp        = (VkCompareOp)DepthTest,
                        depthWriteEnable      = DepthWrite,
                        depthBoundsTestEnable = false,
                        stencilTestEnable     = false,
                        maxDepthBounds        = ViewportMaxDepth,
                        minDepthBounds        = ViewportMinDepth,
                    };
                    var depthStencil_ptr = depthStencil.Pointer();


                    //VkPipelineColorBlendStateCreateInfo - state
                    var colorBlendStates = new VkPipelineColorBlendAttachmentState[RenderPass.ColorAttachments == null ? 0 : RenderPass.ColorAttachments.Length];
                    for (int i = 0; i < colorBlendStates.Length; i++)
                    {
                        colorBlendStates[i] = new VkPipelineColorBlendAttachmentState()
                        {
                            colorWriteMask = VkColorComponentFlags.ColorComponentRBit | VkColorComponentFlags.ColorComponentGBit | VkColorComponentFlags.ColorComponentBBit | VkColorComponentFlags.ColorComponentABit,
                            blendEnable    = false,
                        };
                    }
                    var colorBlendStates_ptr = colorBlendStates.Pointer();

                    var colorBlend = new VkPipelineColorBlendStateCreateInfo()
                    {
                        sType           = VkStructureType.StructureTypePipelineColorBlendStateCreateInfo,
                        logicOpEnable   = false,
                        logicOp         = VkLogicOp.LogicOpCopy,
                        attachmentCount = (uint)colorBlendStates.Length,
                        pAttachments    = colorBlendStates_ptr,
                    };
                    colorBlend.blendConstants[0] = 0;
                    colorBlend.blendConstants[1] = 0;
                    colorBlend.blendConstants[2] = 0;
                    colorBlend.blendConstants[3] = 0;
                    var colorBlend_ptr = colorBlend.Pointer();

                    var dynamicStates = stackalloc VkDynamicState[]
                    {
                        VkDynamicState.DynamicStateViewport
                    };

                    var dynamicState = new VkPipelineDynamicStateCreateInfo()
                    {
                        sType             = VkStructureType.StructureTypePipelineDynamicStateCreateInfo,
                        dynamicStateCount = ViewportDynamic ? 1u : 0u,
                        pDynamicStates    = dynamicStates
                    };
                    var dynamicState_ptr = dynamicState.Pointer();

                    //Setup graphics pipeline
                    var pipelineInfo = new VkGraphicsPipelineCreateInfo()
                    {
                        sType               = VkStructureType.StructureTypeGraphicsPipelineCreateInfo,
                        stageCount          = (uint)shaderStages.Length,
                        pStages             = shaderStages_ptr,
                        pVertexInputState   = vertexInputInfo_ptr,
                        pInputAssemblyState = inputAssembly_ptr,
                        pViewportState      = viewportCreateInfo_ptr,
                        pRasterizationState = rasterizer_ptr,
                        pMultisampleState   = multisampling_ptr,
                        pDepthStencilState  = depthStencil_ptr,
                        pColorBlendState    = colorBlend_ptr,
                        pDynamicState       = dynamicState_ptr,
                        layout              = PipelineLayout.hndl,
                        renderPass          = RenderPass.hndl,
                        subpass             = 0,
                        basePipelineHandle  = IntPtr.Zero,
                        basePipelineIndex   = -1,
                    };

                    IntPtr pipeline_l = IntPtr.Zero;
                    if (vkCreateGraphicsPipelines(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, IntPtr.Zero, 1, pipelineInfo.Pointer(), null, &pipeline_l) != VkResult.Success)
                    {
                        throw new Exception("Failed to create graphics 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.");
            }
        }
Пример #14
0
        public void Build(int device_index)
        {
            if (!locked)
            {
                unsafe
                {
                    uint colorAttachmentCnt = (uint)(ColorAttachments == null ? 0 : ColorAttachments.Length);
                    uint depthAttachmentCnt = DepthAttachment != null ? 1u : 0u;

                    var colorAttachments = new VkAttachmentReference[colorAttachmentCnt];
                    for (int i = 0; i < colorAttachmentCnt; i++)
                    {
                        colorAttachments[i].attachment = (uint)i;
                        colorAttachments[i].layout     = (VkImageLayout)ColorAttachments[i].StartLayout;
                    }
                    var depthAttachment = new VkAttachmentReference()
                    {
                        attachment = colorAttachmentCnt,
                        layout     = DepthAttachment == null ? VkImageLayout.ImageLayoutUndefined : (VkImageLayout)DepthAttachment.StartLayout
                    };

                    var colorAttachments_ptr = colorAttachments.Pointer();
                    var depthAttachments_ptr = depthAttachment.Pointer();
                    var preserveAttachments  = stackalloc uint[] { colorAttachmentCnt };

                    var subpassDesc = new VkSubpassDescription()
                    {
                        pipelineBindPoint       = VkPipelineBindPoint.PipelineBindPointGraphics,
                        colorAttachmentCount    = colorAttachmentCnt,
                        pColorAttachments       = colorAttachments_ptr,
                        pDepthStencilAttachment = depthAttachmentCnt > 0 ? depthAttachments_ptr : IntPtr.Zero,
                        preserveAttachmentCount = 0u,
                        pPreserveAttachments    = preserveAttachments,
                    };
                    var subpassDesc_ptr = subpassDesc.Pointer();

                    var colorAttachmentDesc = new VkAttachmentDescription[colorAttachmentCnt + depthAttachmentCnt];
                    for (int i = 0; i < colorAttachmentCnt; i++)
                    {
                        colorAttachmentDesc[i].format         = (VkFormat)ColorAttachments[i].Format;
                        colorAttachmentDesc[i].samples        = VkSampleCountFlags.SampleCount1Bit;
                        colorAttachmentDesc[i].loadOp         = (VkAttachmentLoadOp)ColorAttachments[i].LoadOp;
                        colorAttachmentDesc[i].storeOp        = (VkAttachmentStoreOp)ColorAttachments[i].StoreOp;
                        colorAttachmentDesc[i].stencilLoadOp  = VkAttachmentLoadOp.AttachmentLoadOpDontCare;
                        colorAttachmentDesc[i].stencilStoreOp = VkAttachmentStoreOp.AttachmentStoreOpDontCare;
                        colorAttachmentDesc[i].initialLayout  = (VkImageLayout)ColorAttachments[i].InitialLayout;
                        colorAttachmentDesc[i].finalLayout    = (VkImageLayout)ColorAttachments[i].FinalLayout;
                    }
                    if (depthAttachmentCnt > 0)
                    {
                        colorAttachmentDesc[colorAttachmentCnt].format         = (VkFormat)DepthAttachment.Format;
                        colorAttachmentDesc[colorAttachmentCnt].samples        = VkSampleCountFlags.SampleCount1Bit;
                        colorAttachmentDesc[colorAttachmentCnt].loadOp         = (VkAttachmentLoadOp)DepthAttachment.LoadOp;
                        colorAttachmentDesc[colorAttachmentCnt].storeOp        = (VkAttachmentStoreOp)DepthAttachment.StoreOp;
                        colorAttachmentDesc[colorAttachmentCnt].stencilLoadOp  = VkAttachmentLoadOp.AttachmentLoadOpDontCare;
                        colorAttachmentDesc[colorAttachmentCnt].stencilStoreOp = VkAttachmentStoreOp.AttachmentStoreOpDontCare;
                        colorAttachmentDesc[colorAttachmentCnt].initialLayout  = (VkImageLayout)DepthAttachment.InitialLayout;
                        colorAttachmentDesc[colorAttachmentCnt].finalLayout    = (VkImageLayout)DepthAttachment.FinalLayout;
                    }
                    var colorAttachmentDesc_ptr = colorAttachmentDesc.Pointer();

                    var subpassDependency = new VkSubpassDependency()
                    {
                        srcSubpass    = VkSubpassExternal,
                        dstSubpass    = 0,
                        srcStageMask  = VkPipelineStageFlags.PipelineStageAllCommandsBit,
                        srcAccessMask = 0,
                        dstStageMask  = VkPipelineStageFlags.PipelineStageVertexShaderBit,
                        dstAccessMask = VkAccessFlags.AccessMemoryReadBit,
                    };
                    var subpassDependency_ptr = subpassDependency.Pointer();

                    var renderPassInfo = new VkRenderPassCreateInfo()
                    {
                        sType           = VkStructureType.StructureTypeRenderPassCreateInfo,
                        attachmentCount = colorAttachmentCnt + depthAttachmentCnt,
                        pAttachments    = colorAttachmentDesc_ptr,
                        subpassCount    = 1,
                        pSubpasses      = subpassDesc_ptr,
                        dependencyCount = 1,
                        pDependencies   = subpassDependency_ptr
                    };
                    IntPtr renderPass_l = IntPtr.Zero;
                    if (vkCreateRenderPass(GraphicsDevice.GetDeviceInfo(device_index).Device, renderPassInfo.Pointer(), null, &renderPass_l) != VkResult.Success)
                    {
                        throw new Exception("Failed to create RenderPass.");
                    }
                    hndl  = renderPass_l;
                    devID = device_index;

                    if (GraphicsDevice.EnableValidation)
                    {
                        var objName = new VkDebugUtilsObjectNameInfoEXT()
                        {
                            sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                            pObjectName  = Name,
                            objectType   = VkObjectType.ObjectTypeRenderPass,
                            objectHandle = (ulong)hndl
                        };
                        GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(devID).Device, objName.Pointer());
                    }
                }
                locked = true;
            }
            else
            {
                throw new Exception("RenderPass is locked.");
            }
        }
Пример #15
0
        public void Build(int deviceIndex, bool timeline, ulong value)
        {
            if (!locked)
            {
                unsafe
                {
                    IntPtr semaphorePtr_l = IntPtr.Zero;
                    this.timeline = timeline;
                    if (timeline)
                    {
                        var semaphoreTypeInfo = new VkSemaphoreTypeCreateInfo()
                        {
                            sType         = VkStructureType.StructureTypeSemaphoreTypeCreateInfo,
                            semaphoreType = VkSemaphoreType.SemaphoreTypeTimeline,
                            initialValue  = value
                        };
                        var semaphoreTypeInfo_ptr = semaphoreTypeInfo.Pointer();

                        var semaphoreInfo = new VkSemaphoreCreateInfo()
                        {
                            sType = VkStructureType.StructureTypeSemaphoreCreateInfo,
                            pNext = semaphoreTypeInfo_ptr
                        };

                        if (vkCreateSemaphore(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, semaphoreInfo.Pointer(), null, &semaphorePtr_l) != VkResult.Success)
                        {
                            throw new Exception("Failed to create semaphore.");
                        }
                    }
                    else
                    {
                        var semaphoreInfo = new VkSemaphoreCreateInfo()
                        {
                            sType = VkStructureType.StructureTypeSemaphoreCreateInfo,
                        };

                        if (vkCreateSemaphore(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, semaphoreInfo.Pointer(), null, &semaphorePtr_l) != VkResult.Success)
                        {
                            throw new Exception("Failed to create semaphore.");
                        }
                    }
                    hndl  = semaphorePtr_l;
                    devID = deviceIndex;

                    if (GraphicsDevice.EnableValidation && Name != null)
                    {
                        var objName = new VkDebugUtilsObjectNameInfoEXT()
                        {
                            sType        = VkStructureType.StructureTypeDebugUtilsObjectNameInfoExt,
                            pObjectName  = Name,
                            objectType   = VkObjectType.ObjectTypeSemaphore,
                            objectHandle = (ulong)hndl
                        };
                        GraphicsDevice.SetDebugUtilsObjectNameEXT(GraphicsDevice.GetDeviceInfo(deviceIndex).Device, objName.Pointer());
                    }
                }
                locked = true;
            }
            else
            {
                throw new Exception("GpuSemaphore is locked.");
            }
        }