示例#1
0
def render():
    # Render commands submitting

    # Get next image in core swapchain
    image_index, result = hvk.acquire_next_image(api,
                                                 device,
                                                 swapchain,
                                                 semaphore=image_ready)

    # Wait until the rendering of the last scene is done
    fence = render_fences[image_index]
    hvk.wait_for_fences(api, device, (fence, ))
    hvk.reset_fences(api, device, (fence, ))

    # Start rendering on the next image
    submit_info = hvk.submit_info(
        wait_dst_stage_mask=(vk.PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, ),
        wait_semaphores=(image_ready, ),
        signal_semaphores=(rendering_done, ),
        command_buffers=(cmd_draw[image_index], ))

    hvk.queue_submit(api, render_queue.handle, (submit_info, ), fence=fence)

    # Present the next image once rendering is done
    hvk.queue_present(
        api, render_queue.handle,
        hvk.present_info(swapchains=(swapchain, ),
                         image_indices=(image_index, ),
                         wait_semaphores=(rendering_done, )))
示例#2
0
def compute_noise():

    hvk.begin_command_buffer(api, staging_cmd, hvk.command_buffer_begin_info())

    # Execute the compute shader
    hvk.bind_pipeline(api, staging_cmd, compute_pipeline,
                      vk.PIPELINE_BIND_POINT_COMPUTE)
    hvk.bind_descriptor_sets(api, staging_cmd, vk.PIPELINE_BIND_POINT_COMPUTE,
                             compute_pipeline_layout,
                             (compute_descriptor_set, ))
    hvk.dispatch(api, staging_cmd, 256 // 16, 256 // 16, 1)

    # Move the image layout to shader read optimal for rendering
    barrier = hvk.image_memory_barrier(
        image=noise_image,
        old_layout=vk.IMAGE_LAYOUT_GENERAL,
        new_layout=vk.IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
        src_access_mask=vk.ACCESS_TRANSFER_WRITE_BIT,
        dst_access_mask=vk.ACCESS_SHADER_READ_BIT,
    )

    hvk.pipeline_barrier(api,
                         staging_cmd, (barrier, ),
                         dst_stage_mask=vk.PIPELINE_STAGE_FRAGMENT_SHADER_BIT)

    hvk.end_command_buffer(api, staging_cmd)

    # Submit the staging command buffer
    hvk.reset_fences(api, device, (staging_fence, ))
    submit_info = hvk.submit_info(command_buffers=(staging_cmd, ))
    hvk.queue_submit(api,
                     render_queue.handle, (submit_info, ),
                     fence=staging_fence)
    hvk.wait_for_fences(api, device, (staging_fence, ))
示例#3
0
    def submit_setup_command(self, wait=False):
        api, device = self.api, self.device
        fence = 0

        if wait:
            fence = self.setup_fence

        infos = (hvk.submit_info(command_buffers=(self.setup_command_buffer,)),)
        hvk.queue_submit(api, self.render_queue.handle, infos, fence)

        if wait:
            f = (fence,)
            hvk.wait_for_fences(api, device, f)
            hvk.reset_fences(api, device, f)
示例#4
0
    def run(self, data_scene, data_compute, group, sync, after, before, callback):
        if data_compute in self.running:
            raise RuntimeError(f"Compute shader {data_compute.compute.name} is already running")

        engine, api, device = self.ctx
        queue = data_compute.queue
        cmd = data_scene.compute_commands[data_compute.command_index]
        pipeline = data_scene.compute_pipelines[data_compute.pipeline]
        x, y, z = group

        before = () if before is None else before
        after = () if after is None else after
        
        # Record the commands
        hvk.begin_command_buffer(api, cmd, hvk.command_buffer_begin_info())

        hvk.bind_pipeline(api, cmd, pipeline, vk.PIPELINE_BIND_POINT_COMPUTE)
        hvk.bind_descriptor_sets(api, cmd, vk.PIPELINE_BIND_POINT_COMPUTE, data_compute.pipeline_layout, data_compute.descriptor_sets)

        CommandsRunner.run_device(before, api, cmd, queue, data_scene)
        hvk.dispatch(api, cmd, x, y, z)
        CommandsRunner.run_device(after, api, cmd, queue, data_scene)

        hvk.end_command_buffer(api, cmd)    

        # Execute the command buffer 
        cmds = (cmd,)
        fence = data_compute.fence
        infos = (hvk.submit_info(command_buffers=cmds),)
        hvk.queue_submit(api, queue.handle, infos, fence)

        if sync:
            f = (fence,)
            hvk.wait_for_fences(api, device, f)
            hvk.reset_fences(api, device, f)
    
            CommandsRunner.run_app(before, data_scene)
            CommandsRunner.run_app(after, data_scene)
            callback()
        else:
            self.running.add(data_compute)
            raise NotImplementedError("Compute without sync is not yet implemented")
示例#5
0
def noise_layout_to_general():
    # Image that support write operation must have the layout `IMAGE_LAYOUT_GENERAL`
    hvk.begin_command_buffer(api, staging_cmd, hvk.command_buffer_begin_info())

    barrier = hvk.image_memory_barrier(
        image=noise_image,
        new_layout=vk.IMAGE_LAYOUT_GENERAL,
        dst_access_mask=vk.ACCESS_TRANSFER_WRITE_BIT,
    )

    hvk.pipeline_barrier(api,
                         staging_cmd, (barrier, ),
                         dst_stage_mask=vk.PIPELINE_STAGE_TRANSFER_BIT)

    hvk.end_command_buffer(api, staging_cmd)

    # Submit the staging command buffer
    hvk.reset_fences(api, device, (staging_fence, ))
    submit_info = hvk.submit_info(command_buffers=(staging_cmd, ))
    hvk.queue_submit(api,
                     render_queue.handle, (submit_info, ),
                     fence=staging_fence)
    hvk.wait_for_fences(api, device, (staging_fence, ))
def texture_to_device():
    global texture_image, texture_image_memory, texture_image_layout, texture_view, texture_sampler
    global texture, staging_texture_buffer, staging_texture_memory

    # Create the vulkan image
    texture_image_layout = vk.IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL

    texture_image = hvk.create_image(
        api, device,
        hvk.image_create_info(
            format=texture.format,
            mip_levels=len(texture.mipmaps),
            extent=vk.Extent3D(texture.width, texture.height, texture.depth),
            usage=vk.IMAGE_USAGE_TRANSFER_DST_BIT | vk.IMAGE_USAGE_SAMPLED_BIT,
        ))

    img_req = hvk.image_memory_requirements(api, device, texture_image)
    mt_index = find_memory_type(vk.MEMORY_HEAP_DEVICE_LOCAL_BIT,
                                vk.MEMORY_HEAP_DEVICE_LOCAL_BIT)

    texture_image_memory = hvk.allocate_memory(
        api, device,
        hvk.memory_allocate_info(allocation_size=img_req.size,
                                 memory_type_index=mt_index))

    hvk.bind_image_memory(api, device, texture_image, texture_image_memory, 0)

    # Build the copy regions (1 for each mipmap)
    regions = []
    for i, m in enumerate(texture.mipmaps):
        region = hvk.buffer_image_copy(
            image_subresource=hvk.image_subresource_layers(mip_level=i),
            image_extent=vk.Extent3D(m.width, m.height, 1),
            buffer_offset=m.offset)

        regions.append(region)

    # Build the image barrier for the image layout transitions
    barrier = hvk.image_memory_barrier(
        image=texture_image,
        new_layout=0,
        dst_access_mask=0,
        subresource_range=hvk.image_subresource_range(
            level_count=len(texture.mipmaps)))

    # Transfer the staging data to device
    hvk.begin_command_buffer(api, staging_cmd, hvk.command_buffer_begin_info())

    barrier.new_layout = vk.IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
    barrier.dst_access_mask = vk.ACCESS_TRANSFER_WRITE_BIT
    hvk.pipeline_barrier(api,
                         staging_cmd, (barrier, ),
                         dst_stage_mask=vk.PIPELINE_STAGE_TRANSFER_BIT)

    hvk.copy_buffer_to_image(api, staging_cmd, staging_texture_buffer,
                             texture_image,
                             vk.IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, regions)

    barrier.old_layout = vk.IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
    barrier.new_layout = vk.IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
    barrier.src_access_mask = vk.ACCESS_TRANSFER_WRITE_BIT
    barrier.dst_access_mask = vk.ACCESS_SHADER_READ_BIT
    hvk.pipeline_barrier(api,
                         staging_cmd, (barrier, ),
                         dst_stage_mask=vk.PIPELINE_STAGE_FRAGMENT_SHADER_BIT)

    hvk.end_command_buffer(api, staging_cmd)

    # Submit the staging command buffer
    hvk.reset_fences(api, device, (staging_fence, ))
    submit_info = hvk.submit_info(command_buffers=(staging_cmd, ))
    hvk.queue_submit(api,
                     render_queue.handle, (submit_info, ),
                     fence=staging_fence)
    hvk.wait_for_fences(api, device, (staging_fence, ))

    # Create the image view and the sampler
    texture_view = hvk.create_image_view(
        api, device,
        hvk.image_view_create_info(
            image=texture_image,
            format=texture.format,
            subresource_range=hvk.image_subresource_range(
                level_count=len(texture.mipmaps))))

    texture_sampler = hvk.create_sampler(
        api, device,
        hvk.sampler_create_info(
            mag_filter=vk.FILTER_LINEAR,
            min_filter=vk.FILTER_LINEAR,
            max_lod=len(texture.mipmaps),
        ))

    # Free staging resources
    hvk.destroy_buffer(api, device, staging_texture_buffer)
    hvk.free_memory(api, device, staging_texture_memory)
    del texture, staging_texture_buffer, staging_texture_memory