Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package renderEngine;
- import org.lwjgl.vulkan.VkInstanceCreateInfo;
- import org.lwjgl.vulkan.VkPhysicalDevice;
- import org.lwjgl.vulkan.VkQueue;
- import org.lwjgl.vulkan.VkQueueFamilyProperties;
- import java.nio.ByteBuffer;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import java.nio.LongBuffer;
- import org.lwjgl.PointerBuffer;
- import org.lwjgl.vulkan.VkApplicationInfo;
- import org.lwjgl.vulkan.VkCommandBuffer;
- import org.lwjgl.vulkan.VkCommandBufferAllocateInfo;
- import org.lwjgl.vulkan.VkCommandBufferBeginInfo;
- import org.lwjgl.vulkan.VkCommandPoolCreateInfo;
- import org.lwjgl.vulkan.VkDevice;
- import org.lwjgl.vulkan.VkDeviceCreateInfo;
- import org.lwjgl.vulkan.VkDeviceQueueCreateInfo;
- import org.lwjgl.vulkan.VkInstance;
- import static org.lwjgl.vulkan.VK10.*;
- import static org.lwjgl.system.MemoryUtil.*;
- public class Renderer{
- private static final ByteBuffer APPLICATION_NAME = memUTF8("Simple test");
- private static final ByteBuffer ENGINE_NAME = memUTF8("Simple engine");
- private static final int APPLICATION_VERSION = VK_MAKE_VERSION(0, 1, 0);
- private static final int ENGINE_VERSION = VK_MAKE_VERSION(0, 1, 0);
- private static final int API_VERSION = VK_MAKE_VERSION(1, 7, 7);
- private int result = VK_SUCCESS;
- private IntBuffer pPhysicalDeviceCount = memAllocInt(1);
- public void initializeRenderer(){
- VkInstance instance = createInstance();
- VkPhysicalDevice physicalDevice = createPhysicalDevice(instance);
- VkDevice device = createDeviceAndGraphicsQueueFamilies(physicalDevice);
- VkQueue queue = createQueue(device, 0, 0); //here I get Access Violation Error
- long commandPool = createCommandPool(physicalDevice, device, 0);
- VkCommandBuffer commandBuffer = createCommandBuffer(device, commandPool);
- beginPrimaryCommandBuffer(commandBuffer);
- System.out.println(result);
- cleanUp(instance, physicalDevice, device, commandPool, commandBuffer);
- }
- private VkQueue createQueue(VkDevice device, int queueFamilyIndex, int queueIndex) {
- PointerBuffer pQueue = memAllocPointer(1);
- vkGetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue); // Without this function everything works
- long lQueue = pQueue.get(0);
- VkQueue queue = new VkQueue(lQueue, device);
- return queue;
- }
- private VkInstance createInstance() {
- PointerBuffer pInstance = memAllocPointer(1);
- VkApplicationInfo pApplicationInfo = VkApplicationInfo.calloc().
- sType(VK_STRUCTURE_TYPE_APPLICATION_INFO).
- pNext(NULL).
- pApplicationName(APPLICATION_NAME).
- applicationVersion(APPLICATION_VERSION).
- pEngineName(ENGINE_NAME).
- engineVersion(ENGINE_VERSION).
- apiVersion(API_VERSION);
- VkInstanceCreateInfo pCreateInfo = VkInstanceCreateInfo.calloc().
- sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO).
- pNext(NULL).
- flags(0).
- pApplicationInfo(pApplicationInfo);
- result = vkCreateInstance(pCreateInfo, null, pInstance);
- long lInstance = pInstance.get(0);
- if(result != VK_SUCCESS) {
- System.err.println("ERROR OCCURED 1");
- }
- VkInstance instance = new VkInstance(lInstance, pCreateInfo);
- return instance;
- }
- private VkPhysicalDevice createPhysicalDevice(VkInstance instance){
- result = vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, null);
- PointerBuffer pPhysicalDevices = memAllocPointer(pPhysicalDeviceCount.get(0));
- result = vkEnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
- if(result != VK_SUCCESS) {
- System.err.println("ERROR OCCURED 2");
- }
- long lPhysicalDevice = pPhysicalDevices.get(0);
- VkPhysicalDevice physicalDevice = new VkPhysicalDevice(lPhysicalDevice, instance);
- return physicalDevice;
- }
- private VkDevice createDeviceAndGraphicsQueueFamilies(VkPhysicalDevice physicalDevice){
- IntBuffer pQueueFamilyPropertyCount = memAllocInt(1);
- FloatBuffer pQueuePriorities = memAllocFloat(1).put(0.0f);
- PointerBuffer pDevice = memAllocPointer(1);
- vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, null);
- int queueCount = pQueueFamilyPropertyCount.get(0);
- VkQueueFamilyProperties.Buffer queueProps = VkQueueFamilyProperties.calloc(queueCount);
- vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, queueProps);
- int graphicsQueueFamilyIndex;
- for (graphicsQueueFamilyIndex = 0; graphicsQueueFamilyIndex < queueCount; graphicsQueueFamilyIndex++) {
- if ((queueProps.get(graphicsQueueFamilyIndex).queueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0)
- break;
- }
- VkDeviceQueueCreateInfo.Buffer pQueueCreateInfos = VkDeviceQueueCreateInfo.calloc(1).
- sType(VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO).
- pNext(NULL).
- flags(0).
- queueFamilyIndex(0).
- pQueuePriorities(pQueuePriorities);
- VkDeviceCreateInfo pCreateInfo = VkDeviceCreateInfo.calloc().
- sType(VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO).
- pNext(NULL).
- flags(0).
- pQueueCreateInfos(pQueueCreateInfos);
- result = vkCreateDevice(physicalDevice, pCreateInfo, null, pDevice);
- if(result != VK_SUCCESS) {
- System.err.println("ERROR OCCURED 3");
- }
- long lDevice = pDevice.get(0);
- VkDevice device = new VkDevice(lDevice, physicalDevice, pCreateInfo);
- return device;
- }
- private long createCommandPool(VkPhysicalDevice physicalDevice, VkDevice device, int queueFamilyIndex){
- LongBuffer pCommandPool = memAllocLong(1);
- VkCommandPoolCreateInfo pCreateInfo = VkCommandPoolCreateInfo.calloc().
- sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO).
- pNext(NULL).
- flags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT).
- queueFamilyIndex(queueFamilyIndex);
- result = vkCreateCommandPool(device, pCreateInfo, null, pCommandPool);
- if(result != VK_SUCCESS) {
- System.err.println("ERROR OCCURED 4");
- }
- long commandPool = pCommandPool.get(0);
- return commandPool;
- }
- private VkCommandBuffer createCommandBuffer(VkDevice device, long commandPool){
- PointerBuffer pCommandBuffers = memAllocPointer(1);
- VkCommandBufferAllocateInfo pAllocateInfo = VkCommandBufferAllocateInfo.calloc().
- sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO).
- pNext(NULL).
- commandPool(commandPool).
- level(VK_COMMAND_BUFFER_LEVEL_PRIMARY).
- commandBufferCount(1);
- result = vkAllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
- if(result != VK_SUCCESS) {
- System.err.println("ERROR OCCURED 5");
- }
- long lCommandBuffer = pCommandBuffers.get(0);
- VkCommandBuffer commandBuffer = new VkCommandBuffer(lCommandBuffer, device);
- return commandBuffer;
- }
- private void beginPrimaryCommandBuffer(VkCommandBuffer commandBuffer){
- VkCommandBufferBeginInfo pBeginInfo = VkCommandBufferBeginInfo.calloc().
- sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO).
- pNext(NULL).
- flags(VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT).
- pInheritanceInfo(null);
- result = vkBeginCommandBuffer(commandBuffer, pBeginInfo);
- if(result != VK_SUCCESS) {
- System.err.println("ERROR OCCURED 6");
- }
- }
- private void cleanUp(VkInstance instance, VkPhysicalDevice physicalDevice, VkDevice device, long commandPool, VkCommandBuffer commandBuffer){
- vkDeviceWaitIdle(device);
- vkDestroyCommandPool(device, commandPool, null);
- vkDestroyDevice(device, null);
- vkDestroyInstance(instance, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement