panic: runtime error: cgo argument has Go pointer to unpinned Go pointer (in Go Vulkan While Creating a Swapchain)

55 views Asked by At

I am Creating a swapchain in go vulkan im not sure why the above error is occuring below is the piece of code the error occur in vulkan CreateSwapchain function updated the code .. new here .. i am trying to solve this for long time using go-vulkan quering for swapchain support in above function and then using it but the above error is occuring

Side Function

func (p *Engine) QuerySwapchainSupport() SwapchainSupport {
    var sCapabilities vk.SurfaceCapabilities
    result := vk.GetPhysicalDeviceSurfaceCapabilities(p.PhysicalDevice, p.Surface, &sCapabilities)
    if result != vk.Success {
        panic(handleVkError(result))
    }

    var pSurfaceFormatCount uint32
    result = vk.GetPhysicalDeviceSurfaceFormats(p.PhysicalDevice, p.Surface, &pSurfaceFormatCount, nil)
    if result != vk.Success {
        panic(handleVkError(result))
    }
    surfaceFormat := make([]vk.SurfaceFormat, pSurfaceFormatCount)
    result = vk.GetPhysicalDeviceSurfaceFormats(p.PhysicalDevice, p.Surface, &pSurfaceFormatCount, surfaceFormat)
    if result != vk.Success {
        panic(handleVkError(result))
    }
    fmt.Println("Surface Formats:")

    var pSurfaceOresentCount uint32
    result = vk.GetPhysicalDeviceSurfacePresentModes(p.PhysicalDevice, p.Surface, &pSurfaceOresentCount, nil)
    if result != vk.Success {
        panic(handleVkError(result))
    }
    presentMode := make([]vk.PresentMode, pSurfaceFormatCount)
    result = vk.GetPhysicalDeviceSurfacePresentModes(p.PhysicalDevice, p.Surface, &pSurfaceFormatCount, presentMode)
    if result != vk.Success {
        panic(handleVkError(result))
    }

    var support SwapchainSupport
    support.sCapabilities = sCapabilities
    support.surfaceFormat = surfaceFormat
    support.presentMode = presentMode
    return support

}

Main Function

func (p *Engine) CreateSwapchain() {
    swapchainSupport := p.QuerySwapchainSupport()
    for _, avilableFormat := range swapchainSupport.surfaceFormat {
        avilableFormat.Deref()
        if avilableFormat.Format == vk.Format(vk.FormatB8g8r8a8Srgb) && avilableFormat.ColorSpace == vk.ColorSpace(vk.ColorspaceSrgbNonlinear) {
            p.SurfaceFrormat = avilableFormat
        }
    }

    for _, present := range swapchainSupport.presentMode {
        if present == vk.PresentMode(vk.PresentModeFifo) {
            p.PresentMode = present
        }
    }
    if swapchainSupport.sCapabilities.CurrentExtent.Width != vk.MaxUint32 {
        swapchainSupport.sCapabilities.CurrentExtent.Deref()
        p.Extend = swapchainSupport.sCapabilities.CurrentExtent
    } else {
        swapchainSupport.sCapabilities.MinImageExtent.Deref()
        swapchainSupport.sCapabilities.MaxImageExtent.Deref()

        actualExtend := &vk.Extent2D{
            Width:  uint32(p.width),
            Height: uint32(p.Height),
        }
        actualExtend.Width = maxUint32(swapchainSupport.sCapabilities.MinImageExtent.Width, minUint32(swapchainSupport.sCapabilities.MaxImageExtent.Width, actualExtend.Width))
        actualExtend.Width = maxUint32(swapchainSupport.sCapabilities.MinImageExtent.Height, minUint32(swapchainSupport.sCapabilities.MaxImageExtent.Height, actualExtend.Height))
        p.Extend = *actualExtend
    }
    var imageCount uint32
    swapchainSupport.sCapabilities.Deref()
    minImageCount := swapchainSupport.sCapabilities.MinImageCount + 1
    if swapchainSupport.sCapabilities.MaxImageCount > 0 && minImageCount > swapchainSupport.sCapabilities.MaxImageCount {
        imageCount = swapchainSupport.sCapabilities.MaxImageCount
    } else {
        imageCount = minImageCount
    }

    createInfo := vk.SwapchainCreateInfo{
        SType:            vk.StructureTypeSwapchainCreateInfo,
        Surface:          vk.Surface(p.Surface),
        MinImageCount:    imageCount,
        ImageFormat:      p.SurfaceFrormat.Format,
        ImageColorSpace:  p.SurfaceFrormat.ColorSpace,
        ImageExtent:      p.Extend,
        ImageArrayLayers: 1,
        ImageUsage:       vk.ImageUsageFlags(vk.ImageUsageColorAttachmentBit),
    }

    indices := p.findQueueFamily()
    var queuefamilyIndices []uint32
    queuefamilyIndices = append(queuefamilyIndices, *indices.graphicsFamily)
    queuefamilyIndices = append(queuefamilyIndices, *indices.presentFamily)
    if *indices.presentFamily != *indices.graphicsFamily {
        createInfo.ImageSharingMode = vk.SharingMode(vk.SharingModeConcurrent)
        createInfo.QueueFamilyIndexCount = 1
        createInfo.PQueueFamilyIndices = queuefamilyIndices
    } else {
        createInfo.ImageSharingMode = vk.SharingMode(vk.SharingModeExclusive)
    }
    createInfo.PreTransform = swapchainSupport.sCapabilities.CurrentTransform
    createInfo.CompositeAlpha = vk.CompositeAlphaFlagBits(vk.CompositeAlphaOpaqueBit)
    createInfo.PresentMode = p.PresentMode
    createInfo.Clipped = vk.True

    ***
here is the error 
> result := vk.CreateSwapchain(p.LogicalDevice, &createInfo, nil,
> &p.Swapchain)

***
    if result != vk.Success {
        panic(handleVkError(result))
    }
    var swapchainImageCount uint32
    vk.GetSwapchainImages(p.LogicalDevice, p.Swapchain, &swapchainImageCount, nil)

    pSwapchainImages := make([]vk.Image, swapchainImageCount)
    vk.GetSwapchainImages(p.LogicalDevice, p.Swapchain, &swapchainImageCount, pSwapchainImages)
    p.SwapchainFrame = make([]SwapchainFrame, swapchainImageCount)
    for i := 0; i < int(swapchainImageCount); i++ {
        createInfo := vk.ImageViewCreateInfo{}
        createInfo.Image = pSwapchainImages[i]
        createInfo.ViewType = vk.ImageViewType(vk.ImageViewType2d)
        createInfo.Components.R = vk.ComponentSwizzle(vk.ComponentSwizzleIdentity)
        createInfo.Components.G = vk.ComponentSwizzle(vk.ComponentSwizzleIdentity)
        createInfo.Components.B = vk.ComponentSwizzle(vk.ComponentSwizzleIdentity)
        createInfo.Components.A = vk.ComponentSwizzle(vk.ComponentSwizzleIdentity)
        createInfo.Format = p.SurfaceFrormat.Format
        createInfo.SubresourceRange.AspectMask = vk.ImageAspectFlags(vk.ImageAspectColorBit)
        createInfo.SubresourceRange.BaseMipLevel = 0
        createInfo.SubresourceRange.LevelCount = 1
        createInfo.SubresourceRange.BaseArrayLayer = 0
        createInfo.SubresourceRange.LayerCount = 1
        result := vk.CreateImageView(p.LogicalDevice, &createInfo, nil, &p.SwapchainFrame[i].ImageView)
        if result != vk.Success {
            panic(handleVkError(result))
        }
        p.SwapchainFrame[i].Image = pSwapchainImages[i]
    }
}

Here is the Error

result := vk.CreateSwapchain(p.LogicalDevice, &createInfo, nil,
&p.Swapchain)

I tried Free() Deref() on the createinfo struct its not working Note i am not using New() anywhere this is happening in go-Vulkan

0

There are 0 answers