def test_render_colored_cube(self):
     from hello_pyvkoffscreen import render_textured_cube
     cube_coords = get_xyzw_uv_cube_coords()
     with VkContextManager(
             vertex_data=cube_coords,
             surface_type=VkContextManager.VKC_OFFSCREEN) as vkc:
         self.assertIsNotNone(vkc)
         render_textured_cube(vkc, cube_coords)
 def test_create_command_pool(self):
     with VkContextManager(VkContextManager.VKC_INIT_SWAP_CHAIN_EXT) as vkc:
         with vkreleasing(
                 vk.createCommandPool(
                     vkc.device,
                     vk.CommandPoolCreateInfo(
                         vk.VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
                         vkc.graphics_queue_family_index))) as command_pool:
             self.assertIsNotNone(command_pool)
 def test_allocate_command_buffers(self):
     with VkContextManager(VkContextManager.VKC_INIT_SWAP_CHAIN_EXT) as vkc:
         with vkreleasing(
                 vk.createCommandPool(
                     vkc.device,
                     vk.CommandPoolCreateInfo(
                         vk.VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
                         vkc.graphics_queue_family_index))) as command_pool:
             cbai = vk.CommandBufferAllocateInfo(
                 command_pool, vk.VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1)
             self.assertIsNotNone(cbai)
             with vkreleasing(vk.allocateCommandBuffers(
                     vkc.device, cbai)) as command_buffers:
                 self.assertIsNotNone(command_buffers)
                 vk.beginCommandBuffer(command_buffers[0],
                                       vk.CommandBufferBeginInfo(0, None))
                 vk.endCommandBuffer(command_buffers[0])
    def test_setup_depth_stencil(self):
        with VkContextManager(VkContextManager.VKC_INIT_COMMAND_BUFFER) as vkc:
            ici = vk.ImageCreateInfo(
                0, vk.VK_IMAGE_TYPE_2D, vk.VK_FORMAT_D24_UNORM_S8_UINT,
                vk.Extent3D(640, 480, 1), 1, 1, vk.VK_SAMPLE_COUNT_1_BIT,
                vk.VK_IMAGE_TILING_OPTIMAL,
                vk.VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
                | vk.VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
                vk.VK_SHARING_MODE_EXCLUSIVE, [], vk.VK_IMAGE_LAYOUT_UNDEFINED)

            with vkreleasing(vk.createImage(vkc.device, ici)) as image:
                self.assertIsNotNone(image)
                memory_requirements = vk.getImageMemoryRequirements(
                    vkc.device, image)
                self.assertIsNotNone(memory_requirements)
                mem_type_index = memory_type_from_properties(
                    vkc.physical_devices[0],
                    memory_requirements.memoryTypeBits,
                    vk.VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
                self.assertIsNotNone(mem_type_index)

                with vkreleasing(
                        vk.allocateMemory(
                            vkc.device,
                            vk.MemoryAllocateInfo(memory_requirements.size,
                                                  mem_type_index))) as memory:
                    vk.bindImageMemory(vkc.device, image, memory, 0)
                    image_memory_barrier = vk.ImageMemoryBarrier(
                        0, vk.VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
                        vk.VK_IMAGE_LAYOUT_UNDEFINED,
                        vk.VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 0,
                        0, image,
                        vk.ImageSubresourceRange(
                            vk.VK_IMAGE_ASPECT_DEPTH_BIT
                            | vk.VK_IMAGE_ASPECT_STENCIL_BIT, 0, 1, 0, 1))

                    vk.cmdPipelineBarrier(
                        vkc.command_buffers[0],
                        vk.VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
                        vk.VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0,
                        vk.VkMemoryBarrierVector(),
                        vk.VkBufferMemoryBarrierVector(),
                        vk.VkImageMemoryBarrierVector(1, image_memory_barrier))
Example #5
0
 def test_render_colored_cube(self):
     cube_coords = get_xyzw_uv_cube_coords()
     with VkContextManager(vertex_data=cube_coords,
                           surface_type=VkContextManager.VKC_WIN32) as vkc:
         self.assertIsNotNone(vkc)
         render_textured_cube(vkc, cube_coords, [1])
Example #6
0
def win32_vk_main(vulkan_render_fct, redraw_interval_ms):

    WndProc = WNDPROCTYPE(PyWndProcedure)
    hInst = windll.kernel32.GetModuleHandleW(0)
    wclassName = u'Hello Vulkan Win32 Class'
    wname = u'Hello Vulkan Win32 Window'

    wndClass = WNDCLASSEX()
    wndClass.cbSize = sizeof(WNDCLASSEX)
    wndClass.style = CS_HREDRAW | CS_VREDRAW
    wndClass.lpfnWndProc = WndProc
    wndClass.cbClsExtra = 0
    wndClass.cbWndExtra = 0
    wndClass.hInstance = hInst
    wndClass.hIcon = 0
    wndClass.hCursor = windll.user32.LoadCursorW(0, IDC_ARROW)
    wndClass.lpszMenuName = 0
    wndClass.hbrBackground = windll.gdi32.GetStockObject(WHITE_BRUSH)
    wndClass.lpszClassName = wclassName
    wndClass.hIconSm = windll.user32.LoadIconW(0, IDI_WINLOGO)

    regRes = windll.user32.RegisterClassExW(byref(wndClass))
    windll.user32.AdjustWindowRect(RECT(0, 0, 640, 480), WS_OVERLAPPEDWINDOW,
                                   0)
    hWnd = windll.user32.CreateWindowExW(
        0, wclassName, wname,
        WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 0, 0, hInst, 0)

    if not hWnd:
        print('Failed to create window')
        exit(0)
    print('ShowWindow', windll.user32.ShowWindow(hWnd, SW_SHOW))
    print('UpdateWindow', windll.user32.UpdateWindow(hWnd))

    msg = MSG()
    lpmsg = pointer(msg)

    print('Creating Vulkan Context')
    with VkContextManager(VkContextManager.VKC_INIT_PIPELINE,
                          surface_type=VkContextManager.VKC_WIN32,
                          widget=Win32Window(hWnd)) as vkc:
        print('Entering message loop')
        set_timer = True
        redraw = True
        while True:
            quit = False
            while windll.user32.PeekMessageW(lpmsg, 0, 0, 0, PM_REMOVE) != 0:
                if msg.message == WM_QUIT:
                    quit = True
                    break

                if msg.message == WM_TIMER and msg.wParam == 1:
                    redraw = True

                windll.user32.TranslateMessage(lpmsg)
                windll.user32.DispatchMessageW(lpmsg)

            if quit:
                break

            if redraw:
                vulkan_render_fct(vkc)
                redraw = False

            if set_timer:
                success = windll.user32.SetTimer(hWnd, 1, redraw_interval_ms,
                                                 0)
                set_timer = False

        print('done.')
def hello_pyvk(texture_file, output_img_file):
    cube_coords = get_xyzw_uv_cube_coords()
    print('Creating Vulkan Context')
    with VkContextManager(VkContextManager.VKC_INIT_PIPELINE,
                          VkContextManager.VKC_OFFSCREEN) as vkc:
        render_textured_cube(vkc, cube_coords)