Skip to content

Commit

Permalink
Improved GL texture compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
xCollateral committed Oct 25, 2023
1 parent d9061e2 commit e3dbe71
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
50 changes: 44 additions & 6 deletions src/main/java/net/vulkanmod/gl/GlTexture.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package net.vulkanmod.gl;

import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.blaze3d.systems.RenderSystem;
import it.unimi.dsi.fastutil.ints.Int2ReferenceOpenHashMap;
import net.vulkanmod.vulkan.texture.VTextureSelector;
import net.vulkanmod.vulkan.texture.VulkanImage;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

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

public class GlTexture {
private static int ID_COUNT = 0;
Expand Down Expand Up @@ -52,21 +55,35 @@ public static void texImage2D(int target, int level, int internalFormat, int wid
if(width == 0 || height == 0)
return;

boundTexture.internalFormat = internalFormat;

if(width != boundTexture.vulkanImage.width || height != boundTexture.vulkanImage.height || vulkanFormat(format, type) != boundTexture.vulkanImage.format) {
boundTexture.allocateVulkanImage(width, height);
}

boundTexture.uploadImage(pixels);
}


public static void texSubImage2D(int target, int level, int xOffset, int yOffset, int width, int height, int format, int type, @Nullable ByteBuffer pixels) {
if(width == 0 || height == 0)
return;

VTextureSelector.uploadSubTexture(level, width, height, xOffset, yOffset,0, 0, width, pixels);
}

public static int getTexLevelParameter(int target, int level, int pName) {
if(boundTexture == null || target == GL11.GL_TEXTURE_2D)
return -1;

return switch (pName) {
case GL11.GL_TEXTURE_INTERNAL_FORMAT -> getGlFormat(boundTexture.vulkanImage.format);
case GL11.GL_TEXTURE_WIDTH -> boundTexture.vulkanImage.width;
case GL11.GL_TEXTURE_HEIGHT -> boundTexture.vulkanImage.height;

default -> -1;
};
}

public static void setVulkanImage(int id, VulkanImage vulkanImage) {
GlTexture texture = map.get(id);

Expand All @@ -75,6 +92,7 @@ public static void setVulkanImage(int id, VulkanImage vulkanImage) {

final int id;
VulkanImage vulkanImage;
int internalFormat;

public GlTexture(int id) {
this.id = id;
Expand All @@ -93,8 +111,15 @@ private void uploadImage(@Nullable ByteBuffer pixels) {
int height = this.vulkanImage.height;

if(pixels != null) {
// if(pixels.remaining() != width * height * 4)
// throw new IllegalArgumentException("buffer size does not match image size");

if(internalFormat == GL11.GL_RGB && vulkanImage.format == VK_FORMAT_R8G8B8A8_UNORM) {

ByteBuffer RGBA_buffer = Util.RGBtoRGBA_buffer(pixels);
this.vulkanImage.uploadSubTextureAsync(0, width, height, 0, 0, 0, 0, 0, RGBA_buffer);
MemoryUtil.memFree(RGBA_buffer);

return;
}

this.vulkanImage.uploadSubTextureAsync(0, width, height, 0, 0, 0, 0, 0, pixels);
}
Expand All @@ -107,14 +132,27 @@ private void uploadImage(@Nullable ByteBuffer pixels) {

private static int vulkanFormat(int glFormat, int type) {
return switch (glFormat) {
case 6408 ->
case GL11.GL_RGBA ->
switch (type) {
case GL11.GL_UNSIGNED_BYTE -> VK_FORMAT_R8G8B8A8_UNORM;
default -> throw new IllegalStateException("Unexpected value: " + type);
};
case GL11.GL_RED ->
switch (type) {
case 5121 -> VK_FORMAT_R8G8B8A8_UNORM;
case GL11.GL_UNSIGNED_BYTE -> VK_FORMAT_R8_UNORM;
default -> throw new IllegalStateException("Unexpected value: " + type);
};

default -> throw new IllegalStateException("Unexpected value: " + glFormat);
};
}

public static int getGlFormat(int vFormat) {
return switch (vFormat) {
case VK_FORMAT_R8G8B8A8_UNORM -> GL11.GL_RGBA;
case VK_FORMAT_R8_UNORM -> GL11.GL_RED;
default -> throw new IllegalStateException("Unexpected value: " + vFormat);
};
}

}
22 changes: 22 additions & 0 deletions src/main/java/net/vulkanmod/gl/Util.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package net.vulkanmod.gl;

import net.vulkanmod.vulkan.shader.SPIRVUtils;
import org.apache.commons.lang3.Validate;
import org.lwjgl.system.MemoryUtil;

import java.nio.ByteBuffer;

public class Util {

Expand All @@ -11,4 +15,22 @@ public static SPIRVUtils.ShaderKind extToShaderKind(String in) {
default -> throw new RuntimeException("unknown shader type: " + in);
};
}

//Created buffer will need to be freed
public static ByteBuffer RGBtoRGBA_buffer(ByteBuffer in) {
Validate.isTrue(in.remaining() % 3 == 0, "bytebuffer is not RGB");

int outSize = in.remaining() * 4 / 3;
ByteBuffer out = MemoryUtil.memAlloc(outSize);

int j = 0;
for(int i = 0; i < outSize; i+=4, j+=3) {
out.put(i, in.get(j));
out.put(i + 1, in.get(j + 1));
out.put(i + 2, in.get(j + 2));
out.put(i + 3, (byte) 0xFF);
}

return out;
}
}
8 changes: 8 additions & 0 deletions src/main/java/net/vulkanmod/mixin/render/GlStateManagerM.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ public static void _texParameter(int i, int j, float k) {
//TODO
}

/**
* @author
*/
@Overwrite(remap = false)
public static int _getTexLevelParameter(int i, int j, int k) {
return GlTexture.getTexLevelParameter(i, j, k);
}

/**
* @author
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MTextureUtil {
*/
@Overwrite(remap = false)
public static int generateTextureId() {
return GlStateManager._genTexture();
return GlTexture.genTextureId();
}

/**
Expand Down

0 comments on commit e3dbe71

Please sign in to comment.