org.lwjgl.vulkan.VkInstance Java Examples

The following examples show how to use org.lwjgl.vulkan.VkInstance. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleDemo.java    From lwjgl3-awt with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    // Create the Vulkan instance
    VkInstance instance = createInstance();
    VKData data = new VKData();
    data.instance = instance; // <- set Vulkan instance
    JFrame frame = new JFrame("AWT test");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.setPreferredSize(new Dimension(600, 600));
    frame.add(new AWTVKCanvas(data) {
        private static final long serialVersionUID = 1L;
        public void initVK() {
            @SuppressWarnings("unused")
            long surface = this.surface;
            // Do something with surface...
        }
        public void paintVK() {
        }
    }, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}
 
Example #2
Source File: ClearScreenDemo.java    From lwjgl3-swt with MIT License 6 votes vote down vote up
/**
 * This function sets up the debug callback which the validation layers will use to yell at us when we make mistakes.
 */
private static long setupDebugging(VkInstance instance, int flags, VkDebugReportCallbackEXT callback) {
    // Again, a struct to create something, in this case the debug report callback
    VkDebugReportCallbackCreateInfoEXT dbgCreateInfo = VkDebugReportCallbackCreateInfoEXT.calloc()
            .sType(VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT) // <- the struct type
            .pNext(NULL) // <- must be NULL
            .pfnCallback(callback) // <- the actual function pointer (in LWJGL a Closure)
            .pUserData(NULL) // <- any user data provided to the debug report callback function
            .flags(flags); // <- indicates which kind of messages we want to receive
    LongBuffer pCallback = memAllocLong(1); // <- allocate a LongBuffer (for a non-dispatchable handle)
    // Actually create the debug report callback
    int err = vkCreateDebugReportCallbackEXT(instance, dbgCreateInfo, null, pCallback);
    long callbackHandle = pCallback.get(0);
    memFree(pCallback); // <- and free the LongBuffer
    dbgCreateInfo.free(); // <- and also the create-info struct
    if (err != VK_SUCCESS) {
        throw new AssertionError("Failed to create VkInstance: " + translateVulkanResult(err));
    }
    return callbackHandle;
}
 
Example #3
Source File: ClearScreenDemo.java    From lwjgl3-swt with MIT License 6 votes vote down vote up
/**
 * This method will enumerate the physical devices (i.e. GPUs) the system has available for us, and will just return
 * the first one. 
 */
private static VkPhysicalDevice getFirstPhysicalDevice(VkInstance instance) {
    IntBuffer pPhysicalDeviceCount = memAllocInt(1);
    int err = vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, null);
    if (err != VK_SUCCESS) {
        throw new AssertionError("Failed to get number of physical devices: " + translateVulkanResult(err));
    }
    PointerBuffer pPhysicalDevices = memAllocPointer(pPhysicalDeviceCount.get(0));
    err = vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
    long physicalDevice = pPhysicalDevices.get(0);
    memFree(pPhysicalDeviceCount);
    memFree(pPhysicalDevices);
    if (err != VK_SUCCESS) {
        throw new AssertionError("Failed to get physical devices: " + translateVulkanResult(err));
    }
    return new VkPhysicalDevice(physicalDevice, instance);
}
 
Example #4
Source File: VulkanInstance.java    From oreon-engine with GNU General Public License v3.0 6 votes vote down vote up
private long setupDebugging(VkInstance instance, int flags, VkDebugReportCallbackEXT callback) {

      VkDebugReportCallbackCreateInfoEXT debugCreateInfo = VkDebugReportCallbackCreateInfoEXT.calloc()
              .sType(VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT)
              .pNext(0)
              .pfnCallback(callback)
              .pUserData(0)
              .flags(flags);
      
      LongBuffer pCallback = memAllocLong(1);
      int err = vkCreateDebugReportCallbackEXT(instance, debugCreateInfo, null, pCallback);
      long callbackHandle = pCallback.get(0);
      
      if (err != VK_SUCCESS) {
          throw new AssertionError("Failed to create VkInstance: " + VkUtil.translateVulkanResult(err));
      }
      
      memFree(pCallback);
      debugCreateInfo.free();
      
      return callbackHandle;
  }
 
Example #5
Source File: SimpleDemo.java    From lwjgl3-awt with MIT License 5 votes vote down vote up
/**
 * Create a Vulkan instance using LWJGL 3.
 * 
 * @return the VkInstance handle
 */
private static VkInstance createInstance() {
    VkApplicationInfo appInfo = VkApplicationInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_APPLICATION_INFO)
            .pApplicationName(memUTF8("AWT Vulkan Demo"))
            .pEngineName(memUTF8(""))
            .apiVersion(VK_MAKE_VERSION(1, 0, 2));
    ByteBuffer VK_KHR_SURFACE_EXTENSION = memUTF8(VK_KHR_SURFACE_EXTENSION_NAME);
    ByteBuffer VK_KHR_OS_SURFACE_EXTENSION;
    if (Platform.get() == Platform.WINDOWS)
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
    else
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
    PointerBuffer ppEnabledExtensionNames = memAllocPointer(2);
    ppEnabledExtensionNames.put(VK_KHR_SURFACE_EXTENSION);
    ppEnabledExtensionNames.put(VK_KHR_OS_SURFACE_EXTENSION);
    ppEnabledExtensionNames.flip();
    VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO)
            .pNext(0L)
            .pApplicationInfo(appInfo);
    if (ppEnabledExtensionNames.remaining() > 0) {
        pCreateInfo.ppEnabledExtensionNames(ppEnabledExtensionNames);
    }
    PointerBuffer pInstance = MemoryUtil.memAllocPointer(1);
    int err = vkCreateInstance(pCreateInfo, null, pInstance);
    if (err != VK_SUCCESS) {
        throw new RuntimeException("Failed to create VkInstance: " + translateVulkanResult(err));
    }
    long instance = pInstance.get(0);
    memFree(pInstance);
    VkInstance ret = new VkInstance(instance, pCreateInfo);
    memFree(ppEnabledExtensionNames);
    memFree(VK_KHR_OS_SURFACE_EXTENSION);
    memFree(VK_KHR_SURFACE_EXTENSION);
    appInfo.free();
    return ret;
}
 
Example #6
Source File: SimpleDemo.java    From lwjgl3-swt with MIT License 5 votes vote down vote up
/**
 * Create a Vulkan instance using LWJGL 3.
 * 
 * @return the VkInstance handle
 */
private static VkInstance createInstance() {
    VkApplicationInfo appInfo = VkApplicationInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_APPLICATION_INFO)
            .pApplicationName(memUTF8("SWT Vulkan Demo"))
            .pEngineName(memUTF8(""))
            .apiVersion(VK_MAKE_VERSION(1, 0, 2));
    ByteBuffer VK_KHR_SURFACE_EXTENSION = memUTF8(VK_KHR_SURFACE_EXTENSION_NAME);
    ByteBuffer VK_KHR_OS_SURFACE_EXTENSION;
    if (Platform.get() == Platform.WINDOWS)
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
    else
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
    PointerBuffer ppEnabledExtensionNames = memAllocPointer(2);
    ppEnabledExtensionNames.put(VK_KHR_SURFACE_EXTENSION);
    ppEnabledExtensionNames.put(VK_KHR_OS_SURFACE_EXTENSION);
    ppEnabledExtensionNames.flip();
    VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO)
            .pNext(0L)
            .pApplicationInfo(appInfo);
    if (ppEnabledExtensionNames.remaining() > 0) {
        pCreateInfo.ppEnabledExtensionNames(ppEnabledExtensionNames);
    }
    PointerBuffer pInstance = MemoryUtil.memAllocPointer(1);
    int err = vkCreateInstance(pCreateInfo, null, pInstance);
    if (err != VK_SUCCESS) {
        throw new RuntimeException("Failed to create VkInstance: " + translateVulkanResult(err));
    }
    long instance = pInstance.get(0);
    memFree(pInstance);
    VkInstance ret = new VkInstance(instance, pCreateInfo);
    memFree(ppEnabledExtensionNames);
    memFree(VK_KHR_OS_SURFACE_EXTENSION);
    memFree(VK_KHR_SURFACE_EXTENSION);
    appInfo.free();
    return ret;
}
 
Example #7
Source File: SimpleDemo.java    From lwjgl3-swt with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    // Create the Vulkan instance
    VkInstance instance = createInstance();

    // Create SWT Display, Shell and VKCanvas
    final Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE);
    shell.setLayout(new FillLayout());
    shell.addListener(SWT.Traverse, new Listener() {
        public void handleEvent(Event event) {
            switch (event.detail) {
            case SWT.TRAVERSE_ESCAPE:
                shell.close();
                event.detail = SWT.TRAVERSE_NONE;
                event.doit = false;
                break;
            }
        }
    });
    VKData data = new VKData();
    data.instance = instance; // <- set Vulkan instance
    final VKCanvas canvas = new VKCanvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE, data);
    shell.setSize(800, 600);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    canvas.dispose();
    display.dispose();
}
 
Example #8
Source File: PhysicalDevice.java    From oreon-engine with GNU General Public License v3.0 5 votes vote down vote up
public PhysicalDevice(VkInstance vkInstance, long surface) {

		IntBuffer pPhysicalDeviceCount = memAllocInt(1);
        int err = vkEnumeratePhysicalDevices(vkInstance, pPhysicalDeviceCount, null);
        if (err != VK_SUCCESS) {
            throw new AssertionError("Failed to get number of physical devices: " + VkUtil.translateVulkanResult(err));
        }
        
        log.info("Available Physical Devices: " + pPhysicalDeviceCount.get(0));
        
        PointerBuffer pPhysicalDevices = memAllocPointer(pPhysicalDeviceCount.get(0));
        err = vkEnumeratePhysicalDevices(vkInstance, pPhysicalDeviceCount, pPhysicalDevices);
        long physicalDevice = pPhysicalDevices.get(0);
       
        if (err != VK_SUCCESS) {
            throw new AssertionError("Failed to get physical devices: " + VkUtil.translateVulkanResult(err));
        }
        
        memFree(pPhysicalDeviceCount);
        memFree(pPhysicalDevices);
        
        handle =  new VkPhysicalDevice(physicalDevice, vkInstance);
        queueFamilies = new QueueFamilies(handle, surface);
        swapChainCapabilities = new SurfaceProperties(handle, surface);
        supportedExtensionNames = DeviceCapabilities.getPhysicalDeviceExtensionNamesSupport(handle);
        memoryProperties = VkPhysicalDeviceMemoryProperties.calloc();
        vkGetPhysicalDeviceMemoryProperties(handle, memoryProperties);
        
        properties = DeviceCapabilities.checkPhysicalDeviceProperties(handle);
        features = DeviceCapabilities.checkPhysicalDeviceFeatures(handle);
        
//        log.info(properties.apiVersion());
//        log.info(properties.driverVersion());
//        log.info(properties.vendorID());
//        log.info(properties.deviceID());
//        log.info(properties.deviceType());
//        log.info(properties.deviceNameString());
	}
 
Example #9
Source File: ClearScreenDemo.java    From lwjgl3-swt with MIT License 4 votes vote down vote up
/**
 * Create a Vulkan {@link VkInstance} using LWJGL 3.
 * <p>
 * The {@link VkInstance} represents a handle to the Vulkan API and we need that instance for about everything we do.
 * 
 * @return the VkInstance handle
 */
private static VkInstance createInstance() {
    VkApplicationInfo appInfo = VkApplicationInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_APPLICATION_INFO)
            .pApplicationName(memUTF8("SWT Vulkan Demo"))
            .pEngineName(memUTF8(""))
            .apiVersion(VK_MAKE_VERSION(1, 0, 2));
    ByteBuffer VK_KHR_SURFACE_EXTENSION = memUTF8(VK_KHR_SURFACE_EXTENSION_NAME);
    ByteBuffer VK_EXT_DEBUG_REPORT_EXTENSION = memUTF8(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
    ByteBuffer VK_KHR_OS_SURFACE_EXTENSION;
    if (Platform.get() == Platform.WINDOWS)
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
    else
        VK_KHR_OS_SURFACE_EXTENSION = memUTF8(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
    PointerBuffer ppEnabledExtensionNames = memAllocPointer(3)
      .put(VK_KHR_SURFACE_EXTENSION)
      .put(VK_KHR_OS_SURFACE_EXTENSION)
      .put(VK_EXT_DEBUG_REPORT_EXTENSION)
      .flip();
    PointerBuffer ppEnabledLayerNames = memAllocPointer(layers.length);
    for (int i = 0; validation && i < layers.length; i++)
        ppEnabledLayerNames.put(layers[i]);
    ppEnabledLayerNames.flip();
    VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.calloc()
            .sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO)
            .pNext(NULL)
            .pApplicationInfo(appInfo)
            .ppEnabledExtensionNames(ppEnabledExtensionNames)
            .ppEnabledLayerNames(ppEnabledLayerNames);
    PointerBuffer pInstance = memAllocPointer(1);
    int err = vkCreateInstance(pCreateInfo, null, pInstance);
    long instance = pInstance.get(0);
    memFree(pInstance);
    if (err != VK_SUCCESS) {
        throw new AssertionError("Failed to create VkInstance: " + translateVulkanResult(err));
    }
    VkInstance ret = new VkInstance(instance, pCreateInfo);
    pCreateInfo.free();
    memFree(ppEnabledLayerNames);
    memFree(ppEnabledExtensionNames);
    memFree(VK_KHR_OS_SURFACE_EXTENSION);
    memFree(VK_EXT_DEBUG_REPORT_EXTENSION);
    memFree(VK_KHR_SURFACE_EXTENSION);
    appInfo.free();
    return ret;
}
 
Example #10
Source File: VulkanInstance.java    From oreon-engine with GNU General Public License v3.0 4 votes vote down vote up
public VulkanInstance(PointerBuffer ppEnabledLayerNames) {

PointerBuffer requiredExtensions = glfwGetRequiredInstanceExtensions();
      if (requiredExtensions == null) {
          throw new AssertionError("Failed to find list of required Vulkan extensions");
      }

      ByteBuffer VK_EXT_DEBUG_REPORT_EXTENSION = memUTF8(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
      
      // +1 due to VK_EXT_DEBUG_REPORT_EXTENSION
      PointerBuffer ppEnabledExtensionNames = memAllocPointer(requiredExtensions.remaining() + 1);
      ppEnabledExtensionNames.put(requiredExtensions);
      ppEnabledExtensionNames.put(VK_EXT_DEBUG_REPORT_EXTENSION);
      ppEnabledExtensionNames.flip();
      
      VkApplicationInfo appInfo = VkApplicationInfo.calloc()
              .sType(VK_STRUCTURE_TYPE_APPLICATION_INFO)
              .pApplicationName(memUTF8("Vulkan Demo"))
              .pEngineName(memUTF8("OREON ENGINE"))
              .apiVersion(VK_MAKE_VERSION(1, 1, 77));
      
      VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.calloc()
              .sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO)
              .pNext(0)
              .pApplicationInfo(appInfo)
              .ppEnabledExtensionNames(ppEnabledExtensionNames)
              .ppEnabledLayerNames(ppEnabledLayerNames);
      PointerBuffer pInstance = memAllocPointer(1);
      int err = vkCreateInstance(pCreateInfo, null, pInstance);
  
      if (err != VK_SUCCESS) {
          throw new AssertionError("Failed to create VkInstance: " + VkUtil.translateVulkanResult(err));
      }
      
      handle = new VkInstance(pInstance.get(0), pCreateInfo);
      
      pCreateInfo.free();
      memFree(pInstance);
      memFree(VK_EXT_DEBUG_REPORT_EXTENSION);
      memFree(ppEnabledExtensionNames);
      memFree(appInfo.pApplicationName());
      memFree(appInfo.pEngineName());
      appInfo.free();
      
      VkDebugReportCallbackEXT debugCallback = new VkDebugReportCallbackEXT() {
          public int invoke(int flags, int objectType, long object, long location, int messageCode, long pLayerPrefix, long pMessage, long pUserData) {
              System.err.println("ERROR OCCURED: " + VkDebugReportCallbackEXT.getString(pMessage));
              return 0;
          }
      };
      
      debugCallbackHandle = setupDebugging(handle, VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT, debugCallback);
  }