From 7b08a44cd45ca365f2426cdb4b636040d1c82c94 Mon Sep 17 00:00:00 2001 From: thr3343 <125277899+thr3343@users.noreply.github.com> Date: Fri, 12 Jan 2024 12:08:22 +0000 Subject: [PATCH] Update SPIRVUtils + Cleanup SpecConstant enum --- .../vulkanmod/vulkan/shader/SPIRVUtils.java | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/vulkanmod/vulkan/shader/SPIRVUtils.java b/src/main/java/net/vulkanmod/vulkan/shader/SPIRVUtils.java index 20be710bf..5aaf7337a 100644 --- a/src/main/java/net/vulkanmod/vulkan/shader/SPIRVUtils.java +++ b/src/main/java/net/vulkanmod/vulkan/shader/SPIRVUtils.java @@ -103,7 +103,7 @@ public static SPIRV compileShader(String filename, String source, ShaderKind sha time += (System.nanoTime() - startTime) / 1000000.0f; - return new SPIRV(result, shaderc_result_get_bytes(result)); + return new SPIRV(result, shaderc_result_get_length(result)); } private static SPIRV readFromStream(InputStream inputStream) { @@ -113,7 +113,7 @@ private static SPIRV readFromStream(InputStream inputStream) { buffer.put(bytes); buffer.position(0); - return new SPIRV(MemoryUtil.memAddress(buffer), buffer); + return new SPIRV(MemoryUtil.memAddress(buffer), bytes.length); } catch (Exception e) { e.printStackTrace(); } @@ -124,28 +124,34 @@ private static SPIRV readFromStream(InputStream inputStream) { public enum SpecConstant { - USE_FOG(Boolean.class), - ALPHA_CUTOUT(Float.class); - - SpecConstant(Object floatClass) { - - } + USE_FOG, + ALPHA_CUTOUT, + MAX_OFFSET_COUNT, + COMPUTE_SIZE_Y, + COMPUTE_SIZE_X; + //Ordinals are used to provide the Constant_ID for VkSpecializationMapEntry public static SpecConstant getNamed(String name) { return switch (name) { case "USE_FOG" -> USE_FOG; case "ALPHA_CUTOUT" -> ALPHA_CUTOUT; + case "MAX_OFFSET_COUNT" -> MAX_OFFSET_COUNT; + case "COMPUTE_SIZE_Y" -> COMPUTE_SIZE_Y; + case "COMPUTE_SIZE_X" -> COMPUTE_SIZE_X; default -> null; }; } //Vulkan spec mandates that VkBool32 must always be aligned to uint32_t, which is 4 Bytes + //As a result to simplify alignment, ints are used for all types, regardless if its a bool, float, int or uint public int getValue() { return switch (this){ case USE_FOG -> Initializer.CONFIG.renderFog ? 1 : 0; case ALPHA_CUTOUT -> Float.floatToRawIntBits(VRenderSystem.alphaCutout); + case MAX_OFFSET_COUNT -> 512; + case COMPUTE_SIZE_Y, COMPUTE_SIZE_X -> 32; }; } } @@ -206,24 +212,16 @@ public void invoke(long user_data, long include_result) { } } - public static final class SPIRV implements NativeResource { - - private final long handle; - private ByteBuffer bytecode; - - public SPIRV(long handle, ByteBuffer bytecode) { - this.handle = handle; - this.bytecode = bytecode; - } + public record SPIRV (long handle, long size_t) implements NativeResource { public ByteBuffer bytecode() { - return bytecode; + return shaderc_result_get_bytes(handle, size_t); } @Override public void free() { -// shaderc_result_release(handle); - bytecode = null; // Help the GC + shaderc_result_release(handle); +// size_t = null; // Help the GC } }