a semi working vulkan renderer

pull/12/head
hYdos 2020-03-04 07:01:29 +10:00
parent f37ae287df
commit 8acb688f19
7 changed files with 94 additions and 63 deletions

View File

@ -15,6 +15,7 @@ import com.github.hydos.ginger.engine.vulkan.*;
import com.github.hydos.ginger.engine.vulkan.api.VKGinger;
import com.github.hydos.ginger.engine.vulkan.model.*;
import com.github.hydos.ginger.engine.vulkan.registers.VKRegister;
import com.github.hydos.ginger.engine.vulkan.render.RenderUtils;
import com.github.hydos.ginger.engine.vulkan.render.renderers.*;
import com.github.hydos.ginger.engine.vulkan.render.ubo.*;
import com.github.hydos.ginger.engine.vulkan.shaders.*;
@ -267,7 +268,7 @@ public class VulkanStarter
VKRegister.commandPool = createCommandPool(VKRegister.device, queueFamilyIndex);
final VkCommandBuffer setupCommandBuffer = createCommandBuffer(VKRegister.device, VKRegister.commandPool);
VKRegister.queue = createDeviceQueue(VKRegister.device, queueFamilyIndex);
final long renderPass = ExampleRenderer.createRenderPass(VKRegister.device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat);
final long renderPass = RenderUtils.createRenderPass(VKRegister.device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat);
final long renderCommandPool = createCommandPool(VKRegister.device, queueFamilyIndex);
VKVertices vertices = VKModelConverter.convertModel(ModelLoader.getCubeMesh(), memoryProperties, VKRegister.device);
Ubo ubo = new Ubo(memoryProperties, VKRegister.device);
@ -303,11 +304,11 @@ public class VulkanStarter
if (framebuffers != null)
{ for (int i = 0; i < framebuffers.length; i++)
VK12.vkDestroyFramebuffer(VKRegister.device, framebuffers[i], null); }
framebuffers = ExampleRenderer.createFramebuffers(VKRegister.device, swapchain, renderPass, Window.getWidth(), Window.getHeight(), depthStencil);
framebuffers = RenderUtils.createFramebuffers(VKRegister.device, swapchain, renderPass, Window.getWidth(), Window.getHeight(), depthStencil);
// Create render command buffers
if (renderCommandBuffers != null)
{ VK12.vkResetCommandPool(VKRegister.device, renderCommandPool, VKUtils.VK_FLAGS_NONE); }
renderCommandBuffers = VKUtils.initRenderCommandBuffers(VKRegister.device, renderCommandPool, framebuffers, renderPass, Window.getWidth(), Window.getHeight(), pipeline, descriptorSet,
renderCommandBuffers = VKUtils.setupRenderCommandBuffer(VKRegister.device, renderCommandPool, framebuffers, renderPass, Window.getWidth(), Window.getHeight(), pipeline, descriptorSet,
vertices.vkVerticiesBuffer);
mustRecreate = false;
}
@ -358,7 +359,7 @@ public class VulkanStarter
float time = 0.0f;
while (!GLFW.glfwWindowShouldClose(Window.getWindow()))
{
GLFW.glfwPollEvents();
Window.update();
if (swapchainRecreator.mustRecreate)
swapchainRecreator.recreate();

View File

@ -207,22 +207,22 @@ public class Window
public static void update()
{
// Hack to make the FBO update when the screen size is changed
if ((oldWindowHeight != Window.getHeight() || oldWindowWidth != Window.getWidth()) && Window.getHeight() > 10 && Window.getWidth() > 10)
{
GingerGL.getInstance().contrastFbo.resizeFBOs();
oldWindowWidth = Window.getWidth();
oldWindowHeight = Window.getHeight();
if(renderAPI == RenderAPI.OpenGL) {
if ((oldWindowHeight != Window.getHeight() || oldWindowWidth != Window.getWidth()) && Window.getHeight() > 10 && Window.getWidth() > 10)
{
GingerGL.getInstance().contrastFbo.resizeFBOs();
oldWindowWidth = Window.getWidth();
oldWindowHeight = Window.getHeight();
}
GL11.glViewport(0, 0, width, height);
GL11.glClearColor(backgroundColour.x/255, backgroundColour.y/255, backgroundColour.z/255, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
}
IntBuffer widthBuffer = BufferUtils.createIntBuffer(1);
IntBuffer heightBuffer = BufferUtils.createIntBuffer(1);
GLFW.glfwGetWindowSize(getWindow(), widthBuffer, heightBuffer);
width = widthBuffer.get(0);
height = heightBuffer.get(0);
GL11.glViewport(0, 0, width, height);
GL11.glClearColor(backgroundColour.x/255, backgroundColour.y/255, backgroundColour.z/255, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GLFW.glfwPollEvents();
newX = Window.getMouseX();
newY = Window.getMouseY();

View File

@ -125,25 +125,6 @@ public class VKModelData
endSingleTimeCommands(commandBuffer);
}
}
private void memcpy(ByteBuffer buffer, int[] vertices) {
int i = 1;
for(int vertex : vertices) {
buffer.putFloat(vertex);
if(i == 3) {
buffer.putFloat(1);
buffer.putFloat(1);
buffer.putFloat(1);
buffer.putFloat(1);
buffer.putFloat(1);
i = 0;
}
//TODO: Finish
i++;
}
}
private void memcpy(ByteBuffer buffer, float[] indices) {

View File

@ -1,4 +1,4 @@
package com.github.hydos.ginger.engine.vulkan.render.renderers;
package com.github.hydos.ginger.engine.vulkan.render;
import static org.lwjgl.system.MemoryUtil.memAddress;
import static org.lwjgl.system.MemoryUtil.memAlloc;
@ -36,7 +36,7 @@ import com.github.hydos.ginger.engine.vulkan.memory.VKMemory;
import com.github.hydos.ginger.engine.vulkan.model.VKVertices;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
public class ExampleRenderer
public class RenderUtils
{
public static long createRenderPass(VkDevice device, int colorFormat, int depthFormat)

View File

@ -0,0 +1,48 @@
package com.github.hydos.ginger.engine.vulkan.render.renderers;
import static org.lwjgl.system.MemoryUtil.*;
import java.nio.LongBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.vulkan.registers.VKRegister;
public class EntityRenderer extends VKRenderer
{
@Override
public void render(MemoryStack stack, VkCommandBuffer renderCommandBuffer)
{
//Bind the models buffers
LongBuffer offsets = memAllocLong(1);
offsets.put(0, 0L);
LongBuffer vertexBuffers = stack.longs(VKRegister.exampleVKModel.vertexBuffer);
VK12.vkCmdBindVertexBuffers(renderCommandBuffer, 0, vertexBuffers, offsets);
VK12.vkCmdBindIndexBuffer(renderCommandBuffer, VKRegister.exampleVKModel.indexBuffer, 0, 3);// 3 = VK_INDEX_TYPE_UINT32
memFree(offsets);
//Render the texture
VK12.vkCmdDrawIndexed(renderCommandBuffer, VKRegister.exampleVKModel.mesh.getIndices().length, 1, 0, 0, 0);
}
public static void tempStaticRender(MemoryStack stack, VkCommandBuffer renderCommandBuffer)
{
//Bind the models buffers
LongBuffer offsets = memAllocLong(1);
offsets.put(0, 0L);
LongBuffer vertexBuffers = stack.longs(VKRegister.exampleVKModel.vertexBuffer);
VK12.vkCmdBindVertexBuffers(renderCommandBuffer, 0, vertexBuffers, offsets);
VK12.vkCmdBindIndexBuffer(renderCommandBuffer, VKRegister.exampleVKModel.indexBuffer, 0, 3);// 3 = VK_INDEX_TYPE_UINT32
memFree(offsets);
//Render the texture
VK12.vkCmdDrawIndexed(renderCommandBuffer, VKRegister.exampleVKModel.mesh.getIndices().length, 1, 0, 0, 0);
}
}

View File

@ -0,0 +1,11 @@
package com.github.hydos.ginger.engine.vulkan.render.renderers;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkCommandBuffer;
public abstract class VKRenderer
{
public abstract void render(MemoryStack stack, VkCommandBuffer renderCommandBuffer);
}

View File

@ -13,9 +13,10 @@ import org.lwjgl.system.MemoryStack;
import org.lwjgl.util.shaderc.*;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.common.tools.IOUtil;
import com.github.hydos.ginger.engine.vulkan.VKConstants;
import com.github.hydos.ginger.engine.vulkan.registers.VKRegister;
import com.github.hydos.ginger.engine.vulkan.render.renderers.EntityRenderer;
import com.github.hydos.ginger.engine.vulkan.shaders.Pipeline;
/** @author hydos
@ -167,7 +168,7 @@ public class VKUtils
}
}
public static VkCommandBuffer[] initRenderCommandBuffers(VkDevice device, long commandPool, long[] framebuffers, long renderPass, int width, int height,
public static VkCommandBuffer[] setupRenderCommandBuffer(VkDevice device, long commandPool, long[] framebuffers, long renderPass, int width, int height,
Pipeline pipeline, long descriptorSet, long verticesBuf)
{
try (MemoryStack stack = MemoryStack.stackPush())
@ -179,9 +180,9 @@ public class VKUtils
.level(VK12.VK_COMMAND_BUFFER_LEVEL_PRIMARY)
.commandBufferCount(framebuffers.length);
PointerBuffer pCommandBuffer = memAllocPointer(framebuffers.length);
int err = VK12.vkAllocateCommandBuffers(device, cmdBufAllocateInfo, pCommandBuffer);
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to create render command buffer: " + VKUtils.translateVulkanResult(err)); }
int result = VK12.vkAllocateCommandBuffers(device, cmdBufAllocateInfo, pCommandBuffer);
if (result != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to create render command buffer: " + VKUtils.translateVulkanResult(result)); }
VkCommandBuffer[] renderCommandBuffers = new VkCommandBuffer[framebuffers.length];
for (int i = 0; i < framebuffers.length; i++)
{ renderCommandBuffers[i] = new VkCommandBuffer(pCommandBuffer.get(i), device); }
@ -190,12 +191,12 @@ public class VKUtils
// Create the command buffer begin structure
VkCommandBufferBeginInfo cmdBufInfo = VkCommandBufferBeginInfo.calloc()
.sType(VK12.VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO);
// Specify clear color (cornflower blue)
// Specify clear colour
VkClearValue.Buffer clearValues = VkClearValue.calloc(2);
clearValues.get(0).color()
.float32(0, 100 / 255.0f)
.float32(1, 149 / 255.0f)
.float32(2, 237 / 255.0f)
.float32(0, Window.getColour().x() / 255.0f)
.float32(1, Window.getColour().y() / 255.0f)
.float32(2, Window.getColour().z() / 255.0f)
.float32(3, 1.0f);
// Specify clear depth-stencil
clearValues.get(1).depthStencil().depth(1.0f).stencil(0);
@ -211,9 +212,9 @@ public class VKUtils
{
// Set target frame buffer
renderPassBeginInfo.framebuffer(framebuffers[i]);
err = VK12.vkBeginCommandBuffer(renderCommandBuffers[i], cmdBufInfo);
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to begin render command buffer: " + VKUtils.translateVulkanResult(err)); }
result = VK12.vkBeginCommandBuffer(renderCommandBuffers[i], cmdBufInfo);
if (result != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to begin render command buffer: " + VKUtils.translateVulkanResult(result)); }
VK12.vkCmdBeginRenderPass(renderCommandBuffers[i], renderPassBeginInfo, VK12.VK_SUBPASS_CONTENTS_INLINE);
// Update dynamic viewport state
VkViewport.Buffer viewport = VkViewport.calloc(1)
@ -235,23 +236,12 @@ public class VKUtils
memFree(descriptorSets);
// Bind the rendering pipeline (including the shaders)
VK12.vkCmdBindPipeline(renderCommandBuffers[i], VK12.VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline);
// Bind triangle vertices
LongBuffer offsets = memAllocLong(1);
offsets.put(0, 0L);
LongBuffer pBuffers = memAllocLong(1);
pBuffers.put(0, verticesBuf);
LongBuffer vertexBuffers = stack.longs(VKRegister.exampleVKModel.vertexBuffer);
VK12.vkCmdBindVertexBuffers(renderCommandBuffers[i], 0, vertexBuffers, offsets);
VK12.vkCmdBindIndexBuffer(renderCommandBuffers[i], VKRegister.exampleVKModel.indexBuffer, 0, 3);// 3 = VK_INDEX_TYPE_UINT32
memFree(pBuffers);
memFree(offsets);
// Draw model
// VK12.vkCmdDraw(renderCommandBuffers[i], 6, 1, 0, 0); old method
VK12.vkCmdDrawIndexed(renderCommandBuffers[i], VKRegister.exampleVKModel.mesh.getIndices().length, 1, 0, 0, 0);
EntityRenderer.tempStaticRender(stack, renderCommandBuffers[i]);
VK12.vkCmdEndRenderPass(renderCommandBuffers[i]);
err = VK12.vkEndCommandBuffer(renderCommandBuffers[i]);
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to begin render command buffer: " + VKUtils.translateVulkanResult(err)); }
result = VK12.vkEndCommandBuffer(renderCommandBuffers[i]);
if (result != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to begin render command buffer: " + VKUtils.translateVulkanResult(result)); }
}
renderPassBeginInfo.free();
clearValues.free();