From f9979a478da1eb3b3653aa96c2cb7b0f26611b7e Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 16 Aug 2024 21:32:13 -0700 Subject: [PATCH] Remove `#include "../opencl.h"` from source files during runtime instead of having a dummy header file. --- .../chunkynative/opencl/context/ClContext.java | 4 ---- .../chunkynative/opencl/context/KernelLoader.java | 11 ++++++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/dev/thatredox/chunkynative/opencl/context/ClContext.java b/src/main/java/dev/thatredox/chunkynative/opencl/context/ClContext.java index 5b9c2f4..31453dd 100644 --- a/src/main/java/dev/thatredox/chunkynative/opencl/context/ClContext.java +++ b/src/main/java/dev/thatredox/chunkynative/opencl/context/ClContext.java @@ -60,10 +60,6 @@ public cl_program loadProgram(Function sourceReader, String kern // Search for include headers HashMap headerFiles = new HashMap<>(); - // Fake `opencl.h` header - headerFiles.put("../opencl.h", clCreateProgramWithSource(context, 1, new String[] { "" }, - null, null)); - // Load headers readHeaders(kernel, headerFiles); diff --git a/src/main/java/dev/thatredox/chunkynative/opencl/context/KernelLoader.java b/src/main/java/dev/thatredox/chunkynative/opencl/context/KernelLoader.java index 7822d7c..515dfdb 100644 --- a/src/main/java/dev/thatredox/chunkynative/opencl/context/KernelLoader.java +++ b/src/main/java/dev/thatredox/chunkynative/opencl/context/KernelLoader.java @@ -10,9 +10,13 @@ import java.nio.file.Paths; import java.util.Scanner; import java.util.function.BiFunction; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class KernelLoader { private static final KernelLoader instance = new KernelLoader(); + private static final Pattern openclIncludeMatcher = Pattern.compile( + "^\\h*#include\\h*\"\\.\\./opencl\\.h\"\\h*$", Pattern.MULTILINE); private final BiFunction rawSourceReader; private final boolean hotReload; @@ -54,7 +58,12 @@ private KernelLoader() { * @return OpenCL program. */ public static cl_program loadProgram(ClContext context, String base, String kernelName) { - return context.loadProgram(file -> instance.rawSourceReader.apply(base, file), kernelName); + return context.loadProgram(file -> { + String program = instance.rawSourceReader.apply(base, file); + Matcher matcher = openclIncludeMatcher.matcher(program); + program = matcher.replaceFirst("// #include \"../opencl.h\""); + return program; + }, kernelName); } public static boolean canHotReload() {