Skip to content

Commit

Permalink
Update SPIRVUtils + Cleanup SpecConstant enum
Browse files Browse the repository at this point in the history
  • Loading branch information
thr3343 committed Jan 12, 2024
1 parent 78b6120 commit 7b08a44
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions src/main/java/net/vulkanmod/vulkan/shader/SPIRVUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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;
};
}
}
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 7b08a44

Please sign in to comment.