Skip to content

Commit

Permalink
Updated to java 21 and started work on new Virtual thread ticking sys…
Browse files Browse the repository at this point in the history
…tem.
  • Loading branch information
Hilligans committed Oct 10, 2023
1 parent d502266 commit c690676
Show file tree
Hide file tree
Showing 39 changed files with 669 additions and 338 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<lwjgl.version>3.3.1</lwjgl.version>
<lwjgl.natives>natives-linux</lwjgl.natives>

<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ public OpenglDefaultImpl(OpenGLEngine engine) {
@Override
public void drawMesh(OpenGLWindow window, GraphicsContext graphicsContext, MatrixStack matrixStack, long meshID, long indicesIndex, int length) {
Tuple<Integer, Integer> data = meshData.get((int)meshID);
/* if(texture != boundTexture) {
GL20.glBindTexture((Integer) textureTypes.get((int)texture), (int)texture);
boundTexture = texture;
}
if(program != boundProgram){
GL20.glUseProgram((int) program);
boundProgram = program;
}
*/

if(data == null) {
return;
}
Expand Down Expand Up @@ -87,7 +76,7 @@ public long createMesh(OpenGLWindow window, GraphicsContext graphicsContext, Ver
int stride = mesh.vertexFormat.getStride();
int pointer = 0;
for(VertexFormat.VertexPart part : mesh.vertexFormat.parts) {
glVertexAttribPointer(x,part.primitiveCount, getGLPrimitive(part.primitiveType),part.normalized,stride,pointer);
glVertexAttribPointer(x, part.primitiveCount, getGLPrimitive(part.primitiveType), part.normalized, stride, pointer);
glEnableVertexAttribArray(x);
pointer += part.getSize();
x++;
Expand Down Expand Up @@ -154,19 +143,7 @@ public void drawAndDestroyMesh(OpenGLWindow window, GraphicsContext graphicsCont
mesh.vertexFormat = getFormat(mesh.vertexFormatName);
}
glDisable(GL_DEPTH_TEST);
/* if(texture != boundTexture) {
// GL20.glBindTexture(textureTypes.get(texture), texture);
if (texture != 0) {
GL20.glBindTexture(GL_TEXTURE_2D, (int)texture);
boundTexture = texture;
}
}
if(program != boundProgram){
GL20.glUseProgram((int)program);
boundProgram = program;
}

*/
int VAO = glGenVertexArrays();
int VBO = glGenBuffers();
int EBO = glGenBuffers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@

import dev.hilligans.ourcraft.Client.Rendering.Graphics.Vulkan.Boilerplate.Window.VertexBufferManager;
import dev.hilligans.ourcraft.Client.Rendering.Graphics.Vulkan.VulkanEngineException;
import dev.hilligans.ourcraft.Client.Rendering.Graphics.Vulkan.VulkanMemoryManager;
import dev.hilligans.ourcraft.Client.Rendering.Graphics.Vulkan.VulkanWindow;
import dev.hilligans.ourcraft.Util.NamedThreadFactory;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;

import java.nio.LongBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

import static dev.hilligans.ourcraft.Client.Rendering.Widgets.FolderWidget.size;
import static org.lwjgl.vulkan.EXTMemoryBudget.VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
import static org.lwjgl.vulkan.VK10.*;
import static org.lwjgl.vulkan.VK11.VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
import static org.lwjgl.vulkan.VK11.vkGetPhysicalDeviceMemoryProperties2;

public class LogicalDevice {

public PhysicalDevice physicalDevice;
public VulkanInstance vulkanInstance;
public VulkanMemoryManager memoryManager;
public VkDevice device;
public VulkanQueueFamilyManager queueFamilyManager;
public VkDeviceQueueCreateInfo.Buffer buffer;
Expand All @@ -35,6 +32,7 @@ public class LogicalDevice {
public LogicalDevice(PhysicalDevice physicalDevice) {
this.vulkanInstance = physicalDevice.vulkanInstance;
this.physicalDevice = physicalDevice;
memoryManager = new VulkanMemoryManager(this);
getMemoryAllocations();

queueFamilyManager = new VulkanQueueFamilyManager(this);
Expand Down Expand Up @@ -108,7 +106,7 @@ public void getMemoryAllocations() {
}

public VulkanBuffer allocateBuffer(int size, int usage, int properties) {
return new VulkanBuffer(this, size, usage, properties);
return new VulkanBuffer(this, size, usage, properties, false);
}

public int findMemoryType(int filter, int properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class PhysicalDevice {
public VulkanInstance vulkanInstance;
public VkPhysicalDeviceProperties properties = VkPhysicalDeviceProperties.calloc();
public VkPhysicalDeviceFeatures deviceFeatures = VkPhysicalDeviceFeatures.calloc();
public VkPhysicalDeviceLimits deviceLimits;
public VkSurfaceCapabilitiesKHR surfaceCapabilities;
public IntBuffer presentModes;
public VkSurfaceFormatKHR.Buffer surfaceFormats;
Expand All @@ -28,6 +29,7 @@ public PhysicalDevice(VkPhysicalDevice physicalDevice, VulkanInstance vulkanInst
this.physicalDevice = physicalDevice;
this.vulkanInstance = vulkanInstance;
vkGetPhysicalDeviceProperties(physicalDevice, properties);
this.deviceLimits = properties.limits();
this.logicalDevice = createDevice();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ public class VulkanBuffer {
public LogicalDevice device;
public long buffer;
public long memory;
public long offset;
public int size;

public VulkanBuffer(LogicalDevice device, int size, int usage, int properties) {
public int properties;

public VulkanBuffer(LogicalDevice device, int size, int usage, int properties, boolean singleUseResource) {
this.size = size;
this.device = device;
try(MemoryStack memoryStack = MemoryStack.stackPush()) {
Expand All @@ -38,7 +41,7 @@ public VulkanBuffer(LogicalDevice device, int size, int usage, int properties) {
VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo.calloc(memoryStack);
allocInfo.sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
allocInfo.allocationSize(memRequirements.size());
allocInfo.memoryTypeIndex(device.findMemoryType(memRequirements.memoryTypeBits(), properties));
allocInfo.memoryTypeIndex(findMemoryType(memRequirements.memoryTypeBits(), properties));

LongBuffer mem = MemoryStack.stackCallocLong(1);

Expand All @@ -47,19 +50,47 @@ public VulkanBuffer(LogicalDevice device, int size, int usage, int properties) {
}
memory = mem.get(0);
vkBindBufferMemory(device.device, buffer, memory, 0);

}
}

public VulkanBuffer(long size, long buffer, long memoryRegion, long offset) {
this.size = (int) size;
this.buffer = buffer;
this.memory = memoryRegion;
this.offset = offset;
}


public void copyTo(VkCommandBuffer commandBuffer, VulkanBuffer destBuffer) {
try(MemoryStack memoryStack = MemoryStack.stackPush()) {
VkBufferCopy copyRegion = VkBufferCopy.calloc(memoryStack);
copyRegion.size(size);
copyRegion.srcOffset(offset);
copyRegion.dstOffset(destBuffer.offset);
VkBufferCopy.Buffer buf = VkBufferCopy.calloc(1, memoryStack);
buf.put(0, copyRegion);
vkCmdCopyBuffer(commandBuffer, buffer, destBuffer.buffer, buf);
}
}

public int findMemoryType(int filter, int properties) {
try(MemoryStack memoryStack = MemoryStack.stackPush()) {
VkPhysicalDeviceMemoryProperties memProperties = VkPhysicalDeviceMemoryProperties.calloc(memoryStack);
vkGetPhysicalDeviceMemoryProperties(device.physicalDevice.physicalDevice, memProperties);

for (int i = 0; i < memProperties.memoryTypeCount(); i++) {
if ((filter & (1 << i)) != 0 && (memProperties.memoryTypes(i).propertyFlags() & properties) == properties) {
this.properties = memProperties.memoryTypes(i).propertyFlags();
return i;
}
}
device.vulkanInstance.exit("Failed to find memory");
}
return -1;
}


public void free() {
vkDestroyBuffer(device.device, buffer, null);
vkFreeMemory(device.device, memory, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class VulkanTexture {
public VulkanTexture(LogicalDevice device, Image img) {
this.device = device;
try(MemoryStack memoryStack = MemoryStack.stackPush()) {
VulkanBuffer buffer = new VulkanBuffer(device, img.getSize(), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
VulkanBuffer buffer = new VulkanBuffer(device, img.getSize(), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, false);
PointerBuffer pos = memoryStack.mallocPointer(1);
vkMapMemory(device.device, buffer.memory, 0, img.getSize(), 0, pos);
this.texture = MemoryUtil.memByteBuffer(pos.get(0), img.getSize()).put(img.buffer);
Expand Down Expand Up @@ -61,7 +61,7 @@ public VulkanTexture(CommandBuffer commandBuffer, LogicalDevice device, ByteBuff
this.device = device;
int size = width * height * format;
try(MemoryStack memoryStack = MemoryStack.stackPush()) {
VulkanBuffer stagingBuffer = new VulkanBuffer(device, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
VulkanBuffer stagingBuffer = new VulkanBuffer(device, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, false);
PointerBuffer pos = memoryStack.mallocPointer(1);
vkMapMemory(device.device, stagingBuffer.memory, 0, size, 0, pos);
this.texture = MemoryUtil.memByteBuffer(pos.get(0), size).put(byteBuffer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package dev.hilligans.ourcraft.Client.Rendering.Graphics.Vulkan;

import dev.hilligans.ourcraft.Client.Rendering.Graphics.Vulkan.Boilerplate.VulkanBuffer;
import dev.hilligans.ourcraft.Client.Rendering.Graphics.Vulkan.VulkanEngineException;
import dev.hilligans.ourcraft.Client.Rendering.Graphics.Vulkan.VulkanMemoryManager;
import dev.hilligans.ourcraft.Data.Primitives.Tuple;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VK10;
import org.lwjgl.vulkan.VkBufferCreateInfo;

import java.nio.LongBuffer;
import java.util.ArrayList;

import static org.lwjgl.vulkan.VK10.*;
import static org.lwjgl.vulkan.VK10.VK_SUCCESS;

public class MemoryRegion {

public ArrayList<Tuple<Long, Long>> sizeOffsets;

public VulkanMemoryManager.MemoryHeap memoryHeap;
public long memory;
public long size;
public long biggestRegion;
public int regionWidth;

public MemoryRegion(VulkanMemoryManager.MemoryHeap memoryHeap, long memory, long size, int regionWidth) {
this.memoryHeap = memoryHeap;
this.memory = memory;
this.size = size;
this.regionWidth = regionWidth;
}

public VulkanBuffer createBuffer(long size, int usage) {
size += size % regionWidth;
if(size > biggestRegion) {
return null;
}

//long offset =

try(MemoryStack memoryStack = MemoryStack.stackPush()) {
VkBufferCreateInfo bufferInfo = VkBufferCreateInfo.calloc(memoryStack);
bufferInfo.sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
bufferInfo.size(size);
bufferInfo.usage(usage);
bufferInfo.sharingMode(VK_SHARING_MODE_EXCLUSIVE);

LongBuffer buf = MemoryStack.stackCallocLong(1);

if (vkCreateBuffer(memoryHeap.device.device, bufferInfo, null, buf) != VK_SUCCESS) {
throw new VulkanEngineException("Failed to allocate buffer");
}
vkBindBufferMemory(memoryHeap.device.device, buf.get(0), memory, 0);

return new VulkanBuffer(size, buf.get(0), memory, 0);
}
}


public long buildBuffer(int offset) {
Tuple<Long, Long> buf = sizeOffsets.get(offset);

long remaining = buf.typeA - offset;
sizeOffsets.remove(buf);
if(remaining != 0) {
add(new Tuple<>(remaining, buf.typeB + offset));
}
return 0;
}

public void add(Tuple<Long, Long> toAdd) {
for(int x = sizeOffsets.size() - 1; x >= 0; x--) {
Tuple<Long, Long> buf = sizeOffsets.get(x);
if(buf.typeA < toAdd.typeA) {
sizeOffsets.add(x + 1, toAdd);
}
}
}

public int findSmallestIndex(long size) {
for(int x = sizeOffsets.size() - 1; x >= 0; x--) {
Tuple<Long, Long> buf = sizeOffsets.get(x);
if(buf.typeA > size) {
return x;
}
}
return -1;
}

public void free() {
VK10.vkFreeMemory(memoryHeap.device.device, memory, null);
memoryHeap.allocatedSize.addAndGet(-size);
}
}
Loading

0 comments on commit c690676

Please sign in to comment.