Skip to content

Commit

Permalink
refactor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatMG393 committed Aug 4, 2024
1 parent cdefb9a commit 607af80
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 140 deletions.
76 changes: 9 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,13 @@
# Fabric Example Mod
# legacy-vulkanmod
A port of [xCollateral's VulkanMod](https://github.com/xCollateral/VulkanMod) to Legacy Fabric 1.8.9

- [Quick start guide](#quick-start-guide)
- [Introduction to the folder structure](#introduction-to-the-folder-structure)
- [Creating your mod](#creating-your-mod)
- [Useful gradle commands](#useful-gradle-commands)
- [More info](#more-info)
- [License](#license)
# Why?
To get more experience developing Minecraft mods and also understanding Minecraft, OpenGL, and Vulkan.

## Quick start guide
And also because why not?

### Introduction to the folder structure
# Credits
- xCollateral: for the original VulkanMod

**Build files:**

| File | Description |
| ------------------- | -------------------------------------------------------- |
| `build.gradle` | Configures the compilation process. |
| `gradle.properties` | Contains properties for Minecraft, fabric, and your mod. |
| `settings.gradle` | Configures the plugin repositories. |

**Fabric files:**

These files are located at `src/main/resources`.

| File | Description | Additional information |
| ----------------------- | ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| `fabric.mod.json` | Contains metadata about your mod. | [wiki:fabric_mod_json_spec](https://fabricmc.net/wiki/documentation:fabric_mod_json_spec) |
| `modid.mixins.json` | Contains a list of all your mixin files. | [wiki:mixin_registration](https://fabricmc.net/wiki/tutorial:mixin_registration) |
| `assets/modid/icon.png` | The icon of your mod. | [wiki:fabric_mod_json_spec#icon](https://fabricmc.net/wiki/documentation:fabric_mod_json_spec?s[]=icon#custom_fields) |


### Creating your mod

First of you must replace all occurrences of `modid` with the id of your mod.

If your mod doesn't use mixins you can safely remove the mixin entry in your `fabric.mod.json` as well as delete any `*.mixin.json` files.

This template has the legacy fabric api included in it's build script, more info about the api can be found at it's [github repo](https://github.com/Legacy-Fabric/fabric).
If you know what you are doing you can also safely remove the api from the build script as it isn't required.

### Useful gradle commands

```sh
# Compile your mod
./gradlew build

# Remove old build files
./gradlew clean

# Generate Minecraft sources
./gradlew genSources

# Launch a modded Minecraft client
./gradlew runClient

# Kill gradle if it's doing stupid things
./gradlew --stop
```

## More info

Additional tutorials and tips can be found in the [wiki](https://github.com/Legacy-Fabric/fabric-example-mod/wiki).

For more detailed setup instructions please see the [fabric wiki](https://fabricmc.net/wiki/tutorial:setup).

If you are new to fabric or Minecraft modding in general then [this wiki page](https://fabricmc.net/wiki/tutorial:primer) may help you.

## License

This template is available under the CC0 license. Feel free to learn from it and incorporate it in your own projects.
# License
Haven't got any time to pick a proper license but it will probably be the same as VulkanMod's license.
28 changes: 10 additions & 18 deletions src/main/java/com/thatmg393/legacyvkm/vulkan/Vulkan.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,31 @@
import org.lwjgl.glfw.GLFWVulkan;
import org.lwjgl.opengl.Display;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VK11;
import org.lwjgl.vulkan.VkApplicationInfo;
import org.lwjgl.vulkan.VkInstance;
import org.lwjgl.vulkan.VkInstanceCreateInfo;
import org.lwjgl.vulkan.*;

import com.thatmg393.legacyvkm.LegacyVulkanMod;
import com.thatmg393.legacyvkm.vulkan.gpu.GPUManager;
import com.thatmg393.legacyvkm.vulkan.utils.ResultChecker;

import lombok.Getter;

public class Vulkan {
private static Vulkan SG_INSTANCE = new Vulkan();
private static final Vulkan SG_INSTANCE = new Vulkan();

public static Vulkan getInstance() {
return SG_INSTANCE;
}

private Vulkan() { }

private VkInstance instance;
@Getter
private VkInstance vkInstance;
private long surfacePtr;

public void initialize() {
createVkInstance();
setupSurface(Display.getHandle());

GPUManager.getInstance().initAndSelectDevice(instance);
GPUManager.getInstance().initAndSelectDevice(vkInstance);
}

private void createVkInstance() {
Expand All @@ -55,18 +54,15 @@ private void createVkInstance() {
PointerBuffer instancePtr = stack.mallocPointer(1);
ResultChecker.checkResult(vkCreateInstance(vici, null, instancePtr), "Failed to create a Vulkan instance");

instance = new VkInstance(instancePtr.get(0), vici);
vkInstance = new VkInstance(instancePtr.get(0), vici);
}
}

private void setupSurface(long windowPtr) {
LegacyVulkanMod.LOGGER.debug("VK Surface setup!");
LegacyVulkanMod.LOGGER.debug("Window handle -> " + windowPtr);

private void setupSurface(long windowPtr) {
try (MemoryStack stack = MemoryStack.stackPush()) {
LongBuffer surfacePtr = stack.longs(VK_NULL_HANDLE);

ResultChecker.checkResult(GLFWVulkan.glfwCreateWindowSurface(instance, windowPtr, null, surfacePtr), "Failed to create a Vulkan window");
ResultChecker.checkResult(GLFWVulkan.glfwCreateWindowSurface(vkInstance, windowPtr, null, surfacePtr), "Failed to create a Vulkan window");

this.surfacePtr = surfacePtr.get(0);
}
Expand All @@ -75,8 +71,4 @@ private void setupSurface(long windowPtr) {
private PointerBuffer getGLFWRequiredExtensions() {
return GLFWVulkan.glfwGetRequiredInstanceExtensions();
}

public VkInstance getVkInstance() {
return this.instance;
}
}
24 changes: 8 additions & 16 deletions src/main/java/com/thatmg393/legacyvkm/vulkan/gpu/GPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,28 @@
import org.lwjgl.util.vma.Vma;
import org.lwjgl.util.vma.VmaAllocatorCreateInfo;
import org.lwjgl.util.vma.VmaVulkanFunctions;
import org.lwjgl.vulkan.VK11;
import org.lwjgl.vulkan.VkDevice;
import org.lwjgl.vulkan.VkFormatProperties;
import org.lwjgl.vulkan.VkPhysicalDevice;
import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties;

import org.lwjgl.vulkan.VkPhysicalDeviceProperties;
import org.lwjgl.vulkan.*;

import com.thatmg393.legacyvkm.vulkan.Vulkan;
import com.thatmg393.legacyvkm.vulkan.queue.QueuesFamilyIndices;
import com.thatmg393.legacyvkm.vulkan.queue.QueueFamilyIndices;
import com.thatmg393.legacyvkm.vulkan.utils.GPUPropertiesUtil;
import com.thatmg393.legacyvkm.vulkan.utils.ResultChecker;

import lombok.Getter;

public class GPU {
public final String name;
public final String vendorName;
public final int apiVersion;

private VkDevice logicalDevice;
private VkPhysicalDevice physicalDevice;

public final VkPhysicalDeviceProperties phyDevProperties;
public final VkPhysicalDeviceMemoryProperties phyDevMemProperties;

@Getter
private long vmaPtr;

private VkDevice logicalDevice;
private VkPhysicalDevice physicalDevice;
private int depthFormat = -69420;

public GPU(VkPhysicalDevice physicalDevice) {
Expand All @@ -49,10 +45,6 @@ public GPU(VkPhysicalDevice physicalDevice) {
this.apiVersion = phyDevProperties.apiVersion();
}

public long getVMA() {
return vmaPtr;
}

public int getOptimalDepthFormat() {
if (depthFormat != -69420) return depthFormat;

Expand Down Expand Up @@ -84,7 +76,7 @@ public VkDevice asLogicalDevice() {

protected void init() {
initializeVMA();
QueuesFamilyIndices.findQueueFamilies(physicalDevice);
QueueFamilyIndices.findQueueFamilies(physicalDevice);
}

private void initializeVMA() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import lombok.Getter;

public class QueuesFamilyIndices {
public class QueueFamilyIndices {
@Getter
private static int graphicsFamily = VK_QUEUE_FAMILY_IGNORED, presentFamily = VK_QUEUE_FAMILY_IGNORED, transferFamily = VK_QUEUE_FAMILY_IGNORED;
private static boolean hasDedicatedTransferQueue = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.thatmg393.legacyvkm.vulkan.gpu.GPUManager;

public enum VkQueues implements AutoCloseable {
GraphicsQueue(QueuesFamilyIndices.getGraphicsFamily(), false);
GraphicsQueue(QueueFamilyIndices.getGraphicsFamily(), false);

private final int familyIndex;
private final CommandPool commandPool;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import com.thatmg393.legacyvkm.vulkan.synchronization.base.Synchronization;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import lombok.Getter;

public class SyncCommandBuffer extends Synchronization<CommandPool.CommandBuffer> {
private static final SyncCommandBuffer INSTANCE = new SyncCommandBuffer();

public static SyncCommandBuffer getInstance() {
return INSTANCE;
}
@Getter
private static final SyncCommandBuffer instance = new SyncCommandBuffer();

private final ObjectArrayList<CommandPool.CommandBuffer> commandBuffers = new ObjectArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.thatmg393.legacyvkm.vulkan.synchronization;

import com.thatmg393.legacyvkm.vulkan.gpu.GPUManager;
import com.thatmg393.legacyvkm.vulkan.synchronization.base.Synchronization;

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

import java.nio.LongBuffer;

import org.lwjgl.system.MemoryUtil;

import com.thatmg393.legacyvkm.vulkan.gpu.GPUManager;
import com.thatmg393.legacyvkm.vulkan.synchronization.base.Synchronization;

import lombok.Getter;

public class SyncFences extends Synchronization<Long> {
public static final int MAX_FENCES = 64; // TODO: just a warning -> ALWAYS CHANGE THIS!
private static final SyncFences INSTANCE = new SyncFences(MAX_FENCES);

public static synchronized SyncFences getInstance() {
return INSTANCE;
}
@Getter
private static final SyncFences instance = new SyncFences(MAX_FENCES);

private final LongBuffer fences;

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/thatmg393/legacyvkm/vulkan/vma/VMAManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.thatmg393.legacyvkm.vulkan.vma;

import static org.lwjgl.vulkan.VK10.*;
import static org.lwjgl.util.vma.Vma.*;
import static org.lwjgl.util.vma.Vma.vmaCreateBuffer;
import static org.lwjgl.util.vma.Vma.vmaMapMemory;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;

import java.nio.LongBuffer;

Expand All @@ -14,12 +15,11 @@
import com.thatmg393.legacyvkm.vulkan.gpu.GPUManager;
import com.thatmg393.legacyvkm.vulkan.utils.ResultChecker;

public class VMAManager {
private static final VMAManager INSTANCE = new VMAManager();
import lombok.Getter;

public static VMAManager getInstance() {
return INSTANCE;
}
public class VMAManager {
@Getter
private static final VMAManager instance = new VMAManager();

private VMAManager() { }

Expand Down

0 comments on commit 607af80

Please sign in to comment.