Esempio n. 1
0
    def free(self):
        api, i, d = self.api, self.instance, self.device

        hvk.device_wait_idle(api, d)

        hvk.destroy_command_pool(api, d, self.command_pool)
        hvk.destroy_fence(api, d, self.setup_fence)

        for scene in self.graph:
            scene.free()

        self.compute_runner.free()
        self.renderer.free()
        self.render_target.free()
        self.memory_manager.free()

        hvk.destroy_swapchain(api, d, self.swapchain)
        hvk.destroy_device(api, d)

        hvk.destroy_surface(api, i, self.surface)

        if self.debugger is not None:
            self.debugger.stop()

        if self.debug_ui is not None:
            self.debug_ui.close()

        hvk.destroy_instance(api, i)

        self.window.destroy()
def clean_resources():
    window.hide()
    hvk.device_wait_idle(api, device)

    hvk.destroy_fence(api, device, staging_fence)
    hvk.destroy_command_pool(api, device, staging_pool)

    hvk.destroy_command_pool(api, device, drawing_pool)

    hvk.destroy_buffer(api, device, mesh_buffer)
    hvk.free_memory(api, device, mesh_memory)

    hvk.destroy_sampler(api, device, texture_sampler)
    hvk.destroy_image_view(api, device, texture_view)
    hvk.destroy_image(api, device, texture_image)
    hvk.free_memory(api, device, texture_image_memory)

    hvk.destroy_descriptor_pool(api, device, descriptor_pool)
    hvk.destroy_buffer(api, device, uniforms_buffer)
    hvk.free_memory(api, device, uniforms_mem)

    hvk.destroy_pipeline(api, device, pipeline)
    hvk.destroy_pipeline_cache(api, device, pipeline_cache)
    hvk.destroy_pipeline_layout(api, device, pipeline_layout)

    for m in shader_modules:
        hvk.destroy_shader_module(api, device, m)

    hvk.destroy_descriptor_set_layout(api, device, descriptor_set_layout)

    for fb in framebuffers:
        hvk.destroy_framebuffer(api, device, fb)

    hvk.destroy_render_pass(api, device, render_pass)

    hvk.destroy_semaphore(api, device, image_ready)
    hvk.destroy_semaphore(api, device, rendering_done)
    for f in render_fences:
        hvk.destroy_fence(api, device, f)

    hvk.destroy_image(api, device, depth_stencil)
    hvk.destroy_image_view(api, device, depth_view)
    hvk.free_memory(api, device, depth_alloc)

    for v in swapchain_image_views:
        hvk.destroy_image_view(api, device, v)

    hvk.destroy_swapchain(api, device, swapchain)
    hvk.destroy_device(api, device)
    hvk.destroy_surface(api, instance, surface)

    debugger.stop()
    hvk.destroy_instance(api, instance)

    window.destroy()
def create_swapchain(recreate=False):
    global swapchain_image_format, swapchain

    # Swapchain Setup
    caps = hvk.physical_device_surface_capabilities(api, physical_device,
                                                    surface)
    formats = hvk.physical_device_surface_formats(api, physical_device,
                                                  surface)
    present_modes = hvk.physical_device_surface_present_modes(
        api, physical_device, surface)

    if not hvk.get_physical_device_surface_support(
            api, physical_device, surface, render_queue.family.index):
        raise RuntimeError(
            "Main rendering queue cannot present images to the surface")

    # Swapchain Format
    format_values = tuple(vkf.format for vkf in formats)
    required_formats = [vk.FORMAT_B8G8R8A8_SRGB, vk.FORMAT_B8G8R8A8_UNORM]
    for i, required_format in enumerate(required_formats):
        if required_format in format_values:
            required_formats[i] = format_values.index(required_format)
        else:
            required_formats[i] = None

    selected_format = next(
        (formats[i] for i in required_formats if i is not None), None)
    if selected_format is None:
        raise RuntimeError("Required swapchain image format not supported")

    # Swapchain Extent
    extent = caps.current_extent
    if extent.width == -1 or extent.height == -1:
        width, height = window.dimensions()
        extent.width = width
        extent.height = height

    # Min image count
    min_image_count = 2
    if caps.max_image_count != 0 and caps.max_image_count < min_image_count:
        raise RuntimeError("Minimum image count not met")
    elif caps.min_image_count > min_image_count:
        min_image_count = caps.min_image_count

    # Present mode
    present_mode = vk.PRESENT_MODE_FIFO_KHR
    if vk.PRESENT_MODE_MAILBOX_KHR in present_modes:
        present_mode = vk.PRESENT_MODE_MAILBOX_KHR
    elif vk.PRESENT_MODE_IMMEDIATE_KHR in present_modes:
        present_mode = vk.PRESENT_MODE_IMMEDIATE_KHR

    # Default image transformation
    transform = caps.current_transform
    if IntFlag(vk.SURFACE_TRANSFORM_IDENTITY_BIT_KHR) in IntFlag(
            caps.supported_transforms):
        transform = vk.SURFACE_TRANSFORM_IDENTITY_BIT_KHR

    # Swapchain creation
    old_swapchain = swapchain
    swapchain_image_format = selected_format.format
    swapchain = hvk.create_swapchain(
        api, device,
        hvk.swapchain_create_info(
            surface=surface,
            image_format=swapchain_image_format,
            image_color_space=selected_format.color_space,
            image_extent=extent,
            min_image_count=min_image_count,
            present_mode=present_mode,
            pre_transform=transform,
            old_swapchain=0))

    if recreate:
        hvk.destroy_swapchain(api, device, old_swapchain)