Advertisement
Guest User

Untitled

a guest
Aug 5th, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.58 KB | None | 0 0
  1. package renderEngine;
  2.  
  3. import org.lwjgl.vulkan.VkInstanceCreateInfo;
  4. import org.lwjgl.vulkan.VkPhysicalDevice;
  5. import org.lwjgl.vulkan.VkQueue;
  6. import org.lwjgl.vulkan.VkQueueFamilyProperties;
  7. import java.nio.ByteBuffer;
  8. import java.nio.FloatBuffer;
  9. import java.nio.IntBuffer;
  10. import java.nio.LongBuffer;
  11. import org.lwjgl.PointerBuffer;
  12. import org.lwjgl.vulkan.VkApplicationInfo;
  13. import org.lwjgl.vulkan.VkCommandBuffer;
  14. import org.lwjgl.vulkan.VkCommandBufferAllocateInfo;
  15. import org.lwjgl.vulkan.VkCommandBufferBeginInfo;
  16. import org.lwjgl.vulkan.VkCommandPoolCreateInfo;
  17. import org.lwjgl.vulkan.VkDevice;
  18. import org.lwjgl.vulkan.VkDeviceCreateInfo;
  19. import org.lwjgl.vulkan.VkDeviceQueueCreateInfo;
  20. import org.lwjgl.vulkan.VkInstance;
  21. import static org.lwjgl.vulkan.VK10.*;
  22. import static org.lwjgl.system.MemoryUtil.*;
  23.  
  24. public class Renderer{
  25.  
  26.         private static final ByteBuffer APPLICATION_NAME = memUTF8("Simple test");
  27.         private static final ByteBuffer ENGINE_NAME = memUTF8("Simple engine");
  28.         private static final int APPLICATION_VERSION = VK_MAKE_VERSION(0, 1, 0);
  29.         private static final int ENGINE_VERSION = VK_MAKE_VERSION(0, 1, 0);
  30.         private static final int API_VERSION = VK_MAKE_VERSION(1, 7, 7);
  31.         private int result = VK_SUCCESS;
  32.         private IntBuffer pPhysicalDeviceCount = memAllocInt(1);
  33.    
  34.     public void initializeRenderer(){
  35.         VkInstance instance = createInstance();
  36.         VkPhysicalDevice physicalDevice = createPhysicalDevice(instance);
  37.         VkDevice device = createDeviceAndGraphicsQueueFamilies(physicalDevice);
  38.         VkQueue queue = createQueue(device, 0, 0); //here I get Access Violation Error
  39.         long commandPool = createCommandPool(physicalDevice, device, 0);
  40.         VkCommandBuffer commandBuffer = createCommandBuffer(device, commandPool);
  41.         beginPrimaryCommandBuffer(commandBuffer);
  42.        
  43.         System.out.println(result);
  44.         cleanUp(instance, physicalDevice, device, commandPool, commandBuffer);
  45.     }
  46.  
  47.     private VkQueue createQueue(VkDevice device, int queueFamilyIndex, int queueIndex) {
  48.         PointerBuffer pQueue = memAllocPointer(1);
  49.        
  50.         vkGetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue); // Without this function everything works
  51.         long lQueue = pQueue.get(0);
  52.         VkQueue queue = new VkQueue(lQueue, device);
  53.        
  54.         return queue;
  55.     }
  56.  
  57.         private VkInstance createInstance() {
  58.             PointerBuffer pInstance = memAllocPointer(1);
  59.        
  60.             VkApplicationInfo pApplicationInfo = VkApplicationInfo.calloc().
  61.                 sType(VK_STRUCTURE_TYPE_APPLICATION_INFO).
  62.                 pNext(NULL).
  63.                 pApplicationName(APPLICATION_NAME).
  64.                 applicationVersion(APPLICATION_VERSION).
  65.                 pEngineName(ENGINE_NAME).
  66.                 engineVersion(ENGINE_VERSION).
  67.                 apiVersion(API_VERSION);
  68.        
  69.             VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.calloc().
  70.                 sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO).
  71.                 pNext(NULL).
  72.                 flags(0).
  73.                 pApplicationInfo(pApplicationInfo);
  74.        
  75.             result = vkCreateInstance(pCreateInfo, null, pInstance);
  76.             long lInstance = pInstance.get(0);
  77.            
  78.             if(result != VK_SUCCESS) {
  79.                 System.err.println("ERROR OCCURED 1");
  80.             }
  81.        
  82.             VkInstance instance = new VkInstance(lInstance, pCreateInfo);
  83.             return instance;
  84.         }  
  85.    
  86.        
  87.        
  88.         private VkPhysicalDevice createPhysicalDevice(VkInstance instance){
  89.             result = vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, null);
  90.             PointerBuffer pPhysicalDevices = memAllocPointer(pPhysicalDeviceCount.get(0));
  91.             result = vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
  92.            
  93.             if(result != VK_SUCCESS) {
  94.                 System.err.println("ERROR OCCURED 2");
  95.             }
  96.            
  97.             long lPhysicalDevice = pPhysicalDevices.get(0);
  98.             VkPhysicalDevice physicalDevice = new VkPhysicalDevice(lPhysicalDevice, instance);
  99.            
  100.             return physicalDevice;
  101.         }
  102.            
  103.        
  104.        
  105.         private VkDevice createDeviceAndGraphicsQueueFamilies(VkPhysicalDevice physicalDevice){
  106.             IntBuffer pQueueFamilyPropertyCount = memAllocInt(1);
  107.             FloatBuffer pQueuePriorities = memAllocFloat(1).put(0.0f);
  108.             PointerBuffer pDevice = memAllocPointer(1);
  109.            
  110.             vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, null);
  111.             int queueCount = pQueueFamilyPropertyCount.get(0);
  112.            
  113.             VkQueueFamilyProperties.Buffer queueProps = VkQueueFamilyProperties.calloc(queueCount);
  114.             vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, queueProps);
  115.            
  116.             int graphicsQueueFamilyIndex;
  117.             for (graphicsQueueFamilyIndex = 0; graphicsQueueFamilyIndex < queueCount; graphicsQueueFamilyIndex++) {
  118.                 if ((queueProps.get(graphicsQueueFamilyIndex).queueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0)
  119.                     break;
  120.             }
  121.            
  122.             VkDeviceQueueCreateInfo.Buffer pQueueCreateInfos = VkDeviceQueueCreateInfo.calloc(1).
  123.                 sType(VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO).
  124.                 pNext(NULL).
  125.                 flags(0).
  126.                 queueFamilyIndex(0).
  127.                 pQueuePriorities(pQueuePriorities);
  128.                
  129.             VkDeviceCreateInfo pCreateInfo = VkDeviceCreateInfo.calloc().
  130.                 sType(VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO).
  131.                 pNext(NULL).
  132.                 flags(0).
  133.                 pQueueCreateInfos(pQueueCreateInfos);
  134.            
  135.             result = vkCreateDevice(physicalDevice, pCreateInfo, null, pDevice);
  136.            
  137.             if(result != VK_SUCCESS) {
  138.                 System.err.println("ERROR OCCURED 3");
  139.             }
  140.            
  141.             long lDevice = pDevice.get(0);
  142.             VkDevice device = new VkDevice(lDevice, physicalDevice, pCreateInfo);
  143.            
  144.             return device;
  145.         }
  146.        
  147.        
  148.        
  149.         private long createCommandPool(VkPhysicalDevice physicalDevice, VkDevice device, int queueFamilyIndex){
  150.             LongBuffer pCommandPool = memAllocLong(1);
  151.            
  152.             VkCommandPoolCreateInfo pCreateInfo = VkCommandPoolCreateInfo.calloc().
  153.                     sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO).
  154.                     pNext(NULL).
  155.                     flags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT).
  156.                     queueFamilyIndex(queueFamilyIndex);
  157.            
  158.             result = vkCreateCommandPool(device, pCreateInfo, null, pCommandPool);
  159.            
  160.             if(result != VK_SUCCESS) {
  161.                 System.err.println("ERROR OCCURED 4");
  162.             }
  163.            
  164.             long commandPool = pCommandPool.get(0);
  165.             return commandPool;
  166.         }
  167.        
  168.        
  169.        
  170.         private VkCommandBuffer createCommandBuffer(VkDevice device, long commandPool){
  171.             PointerBuffer pCommandBuffers = memAllocPointer(1);
  172.            
  173.             VkCommandBufferAllocateInfo pAllocateInfo = VkCommandBufferAllocateInfo.calloc().
  174.                     sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO).
  175.                     pNext(NULL).
  176.                     commandPool(commandPool).
  177.                     level(VK_COMMAND_BUFFER_LEVEL_PRIMARY).
  178.                     commandBufferCount(1);
  179.            
  180.             result = vkAllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
  181.            
  182.             if(result != VK_SUCCESS) {
  183.                 System.err.println("ERROR OCCURED 5");
  184.             }
  185.            
  186.             long lCommandBuffer = pCommandBuffers.get(0);
  187.             VkCommandBuffer commandBuffer = new VkCommandBuffer(lCommandBuffer, device);
  188.            
  189.             return commandBuffer;
  190.         }
  191.        
  192.        
  193.        
  194.         private void beginPrimaryCommandBuffer(VkCommandBuffer commandBuffer){
  195.            
  196.             VkCommandBufferBeginInfo pBeginInfo = VkCommandBufferBeginInfo.calloc().
  197.                     sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO).
  198.                     pNext(NULL).
  199.                     flags(VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT).
  200.                     pInheritanceInfo(null);
  201.            
  202.             result = vkBeginCommandBuffer(commandBuffer, pBeginInfo);
  203.            
  204.             if(result != VK_SUCCESS) {
  205.                 System.err.println("ERROR OCCURED 6");
  206.             }
  207.         }
  208.        
  209.         private void cleanUp(VkInstance instance, VkPhysicalDevice physicalDevice, VkDevice device, long commandPool, VkCommandBuffer commandBuffer){
  210.             vkDeviceWaitIdle(device);
  211.             vkDestroyCommandPool(device, commandPool, null);
  212.             vkDestroyDevice(device, null);
  213.             vkDestroyInstance(instance, null); 
  214.         }
  215.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement