Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
365 commits
Select commit Hold shift + click to select a range
f1a7764
ArrayShortParameter preconditions for buffer size
Kuba663 Aug 11, 2026
ba4a50c
BufferByteParameter preconditions for buffer size
Kuba663 Aug 11, 2026
b7811e3
BufferDoubleParameter preconditions for buffer size
Kuba663 Aug 11, 2026
8cd27e0
BufferFloatParameter preconditions for buffer size
Kuba663 Aug 11, 2026
9a1a2f4
BufferIntParameter preconditions for buffer size
Kuba663 Aug 11, 2026
f482db6
BufferLongParameter preconditions for buffer size
Kuba663 Aug 11, 2026
74439bd
BufferShortParameter preconditions for buffer size
Kuba663 Aug 11, 2026
d20c927
Array writes!
Kuba663 Aug 11, 2026
e4d61e1
Read error check function
Kuba663 Aug 11, 2026
b68e101
Read functions into buffers
Kuba663 Aug 11, 2026
2574f67
Read functions into arrays
Kuba663 Aug 11, 2026
084c7c4
OpenCL buffer addition to KernelParameterList
Kuba663 Aug 11, 2026
3b32077
CommandQueue float array write queueing.
Kuba663 Aug 11, 2026
78b6928
CommandQueue double buffer & array write queueing.
Kuba663 Aug 11, 2026
c5bec9d
CommandQueue byte & short buffer write queueing.
Kuba663 Aug 11, 2026
fdf2c63
Rename some of the next functions to write
Kuba663 Aug 11, 2026
e2475d8
Buffer & array write functions for CommandQueue
Kuba663 Aug 12, 2026
2ccdbe7
Editor folds
Kuba663 Aug 12, 2026
111aa06
Buffer & array read function for floats.
Kuba663 Aug 12, 2026
fcb8218
Buffer & array read function for double precision floating point numb…
Kuba663 Aug 12, 2026
70d5b5e
Buffer & array read function for bytes.
Kuba663 Aug 12, 2026
765b341
Buffer & array read function for short integers.
Kuba663 Aug 12, 2026
8e03e8f
Buffer & array read function for integers.
Kuba663 Aug 12, 2026
e2cfded
Fix poorly written test code.
Kuba663 Aug 12, 2026
42ba0ab
KernelTest written
Kuba663 Aug 12, 2026
54f6e95
Consequent execution test
Kuba663 Aug 12, 2026
46d5a21
Stackless buffer instantiation.
Kuba663 Aug 12, 2026
eb704c4
Stackless tests
Kuba663 Aug 12, 2026
ed15d59
Fix """stackless""" CommandQueue methods
Kuba663 Aug 12, 2026
6041799
2D byte buffer parameter kernel test
Kuba663 Aug 12, 2026
54c96c5
Fix subbuffer creation error
Kuba663 Aug 12, 2026
b8afd9b
Task Execution
Kuba663 Aug 12, 2026
ef09faf
CommandQueue task dispatch
Kuba663 Aug 12, 2026
edcc01d
Task dispatch test
Kuba663 Aug 12, 2026
90c4031
ChannelOrder
Kuba663 Aug 12, 2026
2711c9d
ChannelType
Kuba663 Aug 13, 2026
239dcd5
Move everything to a separate package
Kuba663 Aug 14, 2026
3b2f690
Error type for Images
Kuba663 Aug 14, 2026
1f1359c
Ways to find the ChannelType and ChannelOrder from their OpenCL ids
Kuba663 Aug 14, 2026
e5b8d4c
Image class
Kuba663 Aug 14, 2026
cb8557e
Removed substack creation from Image creation as it will be done in t…
Kuba663 Aug 14, 2026
3f04dd0
Constructor workaround.
Kuba663 Aug 14, 2026
1dfeba3
Change type of OpenCL ids to `int`
Kuba663 Aug 14, 2026
6e79092
Make Image sealed.
Kuba663 Aug 14, 2026
9786cb9
Reduce the amount of fill overloads with generics & add 0 mipmap over…
Kuba663 Aug 14, 2026
5c30896
Error handling function for image filling.
Kuba663 Aug 14, 2026
a3dd701
One dimensional Image
Kuba663 Aug 14, 2026
a2a556d
Helper function for copy functions.
Kuba663 Aug 14, 2026
02cb72a
I forgot to rewind
Kuba663 Aug 14, 2026
4711d57
Mistake in makeParameterBuffer in Image1D
Kuba663 Aug 14, 2026
0b8001a
getRegion function
Kuba663 Aug 14, 2026
4e01aea
Change signature of copy function
Kuba663 Aug 14, 2026
ccb2611
Optimize imports
Kuba663 Aug 14, 2026
a342321
Function for handling copy image errors
Kuba663 Aug 14, 2026
f832e26
Image1D copy function
Kuba663 Aug 14, 2026
76e605f
Fix wrong error message
Kuba663 Aug 15, 2026
4fa2efe
Handling read and write errors
Kuba663 Aug 15, 2026
8981ebe
Write and read functions for buffers
Kuba663 Aug 15, 2026
d19c257
Write and read functions for buffers implemented in Image1D
Kuba663 Aug 15, 2026
036db28
Write and read functions for arrays
Kuba663 Aug 15, 2026
b9c0047
Write and read functions for arrays implemented in Image1D
Kuba663 Aug 15, 2026
fb4e76e
Added size to Image.java
Kuba663 Aug 15, 2026
8281ce2
Preconditions in Image1D
Kuba663 Aug 15, 2026
adb695b
Fix row_pitch error
Kuba663 Aug 15, 2026
6124b83
Fix copy method signature
Kuba663 Aug 15, 2026
ca1c198
Permit Image2D
Kuba663 Aug 15, 2026
d143ae3
Image2D
Kuba663 Aug 15, 2026
47a6d8b
Image3D
Kuba663 Aug 15, 2026
3b37880
Fix wrong image type error
Kuba663 Aug 15, 2026
764a382
Make Images Closeable
Kuba663 Aug 15, 2026
fa384cd
Fix memory gibberish related error
Kuba663 Aug 15, 2026
ed7833e
Fix library loading error
Kuba663 Aug 15, 2026
598b0d1
copy Variants in Image without mipmaps
Kuba663 Aug 15, 2026
0e3bc30
Image functions for NIO Buffer parameters.
Kuba663 Aug 15, 2026
9108c42
Image Tests
Kuba663 Aug 15, 2026
89faae3
Image1DArray
Kuba663 Aug 16, 2026
b96d6a8
Remove extra newline
Kuba663 Aug 16, 2026
d3f2848
Add forgotten mipmap related fixes.
Kuba663 Aug 16, 2026
2c23933
Image2DArray
Kuba663 Aug 16, 2026
11fa052
Image1DBuffer
Kuba663 Aug 16, 2026
22e31d9
ColorUtils
Kuba663 Aug 16, 2026
4399293
Image1D write test
Kuba663 Aug 16, 2026
50fc34d
Queue deletion in ImageTest
Kuba663 Aug 16, 2026
b22b666
Array parameter image functions in CommandQueue
Kuba663 Aug 16, 2026
e846693
Samplers
Kuba663 Aug 16, 2026
7014899
ImageParameter
Kuba663 Aug 17, 2026
c3775a8
SamplerParameter
Kuba663 Aug 17, 2026
69774d9
Organize KernelParameterList better
Kuba663 Aug 17, 2026
eac21e2
Put ImageParameter and SamplerParameter into KernelParameterList
Kuba663 Aug 17, 2026
1c9957f
Pipes
Kuba663 Aug 17, 2026
c4e0f7b
PipeParameter
Kuba663 Aug 17, 2026
ecb968c
tweak program & kernel metadata
Kuba663 Aug 17, 2026
d8ee3d9
Fix redundant extension addition + make requirements optional
Kuba663 Aug 17, 2026
028d766
New program JSONs
Kuba663 Aug 17, 2026
86cad59
I put program metadata into the kernel map :facepalm:
Kuba663 Aug 17, 2026
ae7258b
Store capabilities of all devices.
Kuba663 Aug 17, 2026
5195e02
Check if a kernel that uses mipmaps meets the requirements
Kuba663 Aug 17, 2026
a08b514
OpenCL feature checks
Kuba663 Aug 18, 2026
d68bbfa
Forgot to make programs closeable
Kuba663 Aug 18, 2026
3744860
Fix backwards query error
Kuba663 Aug 18, 2026
352cfa5
Availability check
Kuba663 Aug 18, 2026
4a37ada
Task check
Kuba663 Aug 18, 2026
2f24c44
Device binary search
Kuba663 Aug 18, 2026
ba12051
Stopping the support detection loop when all is detected.
Kuba663 Aug 18, 2026
d59bb34
Libraries can now depend on each-other.
Kuba663 Aug 18, 2026
3debb09
Accommodate library dependency chains in ComputeProgram
Kuba663 Aug 18, 2026
d0f6eb3
CommandQueueParameter
Kuba663 Aug 18, 2026
4effbe8
Now that I look at it, I don't think this was necessary.
Kuba663 Aug 18, 2026
6ea3518
Fix library acquisition error
Kuba663 Aug 18, 2026
ad339da
Fix Event memory leak.
Kuba663 Aug 18, 2026
b5183cc
Making the type enum universal.
Kuba663 Aug 18, 2026
e7ebe80
Another rename, the OpenCLType.java name will be restricted to another.
Kuba663 Aug 18, 2026
69363f6
Making a package for them atp.
Kuba663 Aug 18, 2026
993c63e
Vector type
Kuba663 Aug 18, 2026
5055612
Buffer type
Kuba663 Aug 18, 2026
97ca6d6
Put unsigned variants directly into OpenCLPrimitive
Kuba663 Aug 18, 2026
1698da7
Image Type
Kuba663 Aug 18, 2026
6861e25
Pipe Type
Kuba663 Aug 18, 2026
4f4155f
Fix vector type size mistake
Kuba663 Aug 18, 2026
33172c9
Forgot depth in toString
Kuba663 Aug 18, 2026
a8e4113
Handle all types
Kuba663 Aug 18, 2026
febff9a
OpenCLTypeDeserializer
Kuba663 Aug 18, 2026
489b09c
Add type deserialization to JSON metadata
Kuba663 Aug 18, 2026
50ed400
Enable OpenGL sharing.
Kuba663 Aug 18, 2026
2dbf68b
Remove redundant lines from SetupTest
Kuba663 Aug 19, 2026
31ef76b
GL Sharing support in Buffer
Kuba663 Aug 19, 2026
90cd170
Function for checking if Buffer is sharing with OpenGL
Kuba663 Aug 19, 2026
71d29f9
Dealing with GL Sharing in Kernel
Kuba663 Aug 19, 2026
0335921
GL Sharing support for image filling
Kuba663 Aug 20, 2026
203a5d1
Resolve conflicts
Kuba663 Aug 21, 2026
0172f46
Move compute to new folder
Kuba663 Aug 21, 2026
68ab4ed
Try to fix errors from the merge and move
Kuba663 Aug 21, 2026
266222a
Image1D copy GL sharing
Kuba663 Aug 21, 2026
fff7313
Fix test package names
Kuba663 Aug 21, 2026
5e05d32
Image GL Sharing
Kuba663 Aug 21, 2026
ef2bb74
Handling Image acquisition in kernels.
Kuba663 Aug 21, 2026
a75bef0
These should only apply to host memory access.
Kuba663 Aug 21, 2026
bd77e13
It was not even a buffer flag. Why the fuck did I put it in there?
Kuba663 Aug 21, 2026
3e9e98d
Missing Buffer features
Kuba663 Aug 21, 2026
d26120c
I think I might have accidentally forgotten to add those files back.
Kuba663 Aug 21, 2026
1bbc3ea
Change Sampler to a final class as preparation for smart pointers
Kuba663 Aug 22, 2026
7efeb42
Remove unused import
Kuba663 Aug 22, 2026
34c5982
SmartPointer class
Kuba663 Aug 22, 2026
fc76bf5
Separate Garbaggio Collector class for the cleaning functions
Kuba663 Aug 23, 2026
e9bd840
Smart pointer support for Buffer
Kuba663 Aug 23, 2026
0ed36d8
Smart pointer support for Image
Kuba663 Aug 23, 2026
61b0a59
Image1D implementation
Kuba663 Aug 23, 2026
a15ea7d
Image1DArray implementation
Kuba663 Aug 23, 2026
577f5e3
Image1DBuffer implementation
Kuba663 Aug 23, 2026
4eeb418
Image2D implementation
Kuba663 Aug 23, 2026
5e7a77b
Image2DArray implementation
Kuba663 Aug 23, 2026
07a0a91
Image3D implementation
Kuba663 Aug 23, 2026
df9497d
Sampler Smart Pointers
Kuba663 Aug 23, 2026
a2848e1
Pipe Smart Pointers
Kuba663 Aug 23, 2026
4443820
Change the way that the garbage collection works & add documentation.
Kuba663 Aug 24, 2026
45cba50
Refreshing the CommandQueue on execution
Kuba663 Aug 24, 2026
bc09f1f
package-info.java
Kuba663 Aug 24, 2026
ab66053
@NullMarked everything!!!
Kuba663 Aug 24, 2026
ae23a39
Make the singleton instance @Nullable
Kuba663 Aug 24, 2026
97e0bb8
Checking for non-blocking write before operation
Kuba663 Aug 24, 2026
9b7498a
Warnings on operations after non-blocking writes + barrier
Kuba663 Aug 24, 2026
aca8f46
Slimming down Buffer
Kuba663 Aug 24, 2026
c4c7bf7
Slimming down CommandQueue
Kuba663 Aug 24, 2026
72a7d57
Proper passing down of the nonBlockingWrites Set
Kuba663 Aug 24, 2026
f0b16a9
Documentation for Buffer
Kuba663 Aug 25, 2026
0812ce2
Documentation for BufferFlags
Kuba663 Aug 25, 2026
af5bd4b
Documentation for com.cleanroommc.compute.buffers
Kuba663 Aug 25, 2026
122c8df
I can add more links in @see
Kuba663 Aug 25, 2026
d38d857
Documented Kernel Dispatch in CommandQueue
Kuba663 Aug 25, 2026
606526e
Documented Buffer Write Float in CommandQueue
Kuba663 Aug 25, 2026
b683aa0
Documented CommandQueue class and constructor + make constructor hidden
Kuba663 Aug 25, 2026
6cdda82
Documented Buffer Write Double CommandQueue
Kuba663 Aug 25, 2026
0c35f1b
Documented Buffer Write NIO Buffer in CommandQueue
Kuba663 Aug 25, 2026
2037993
Documented Buffer Write Short Array in CommandQueue
Kuba663 Aug 25, 2026
b08cb11
Documented Buffer Write Int Array in CommandQueue
Kuba663 Aug 25, 2026
eded6ad
Documented Buffer Read Float Array in CommandQueue
Kuba663 Aug 25, 2026
765cdf9
Documented Buffer Read Double Array in CommandQueue
Kuba663 Aug 25, 2026
e759654
Documented Buffer Read NIO Buffer in CommandQueue
Kuba663 Aug 25, 2026
2a3ea31
Documented Buffer Read Short Array in CommandQueue
Kuba663 Aug 25, 2026
3938f51
Documented Buffer Read Int Array in CommandQueue
Kuba663 Aug 25, 2026
0c81086
Documented Image fill in CommandQueue
Kuba663 Aug 25, 2026
1193e64
Documented Image Copy in CommandQueue
Kuba663 Aug 25, 2026
d8ba761
Documented Image Read in CommandQueue
Kuba663 Aug 25, 2026
419c3fd
Documented Image Write in CommandQueue
Kuba663 Aug 25, 2026
0496de7
Documented Kernel dispatch in Event
Kuba663 Aug 25, 2026
15242df
Documented Buffer Write Float Array in Event
Kuba663 Aug 25, 2026
e9e8cc7
Documented Buffer Write Double Array in Event
Kuba663 Aug 25, 2026
03398ff
Documented Buffer Write Short Array in Event
Kuba663 Aug 25, 2026
51019b0
Documented Buffer Write Int Array in Event
Kuba663 Aug 25, 2026
964fd75
Documented Buffer Read Float Array in Event
Kuba663 Aug 25, 2026
ff802df
Documented Buffer Read Double Array in Event
Kuba663 Aug 25, 2026
d503dd3
Documented Buffer Read NIO Buffer in Event
Kuba663 Aug 25, 2026
3ca2ccf
Documented Buffer Read Short Array in Event
Kuba663 Aug 25, 2026
ef646a0
Documented Buffer Read Int Array in Event
Kuba663 Aug 25, 2026
630bc14
Documented Image Fill in Event
Kuba663 Aug 25, 2026
53acd36
Documented Image Copy in Event
Kuba663 Aug 25, 2026
a661b8a
Documented Image Read in Event
Kuba663 Aug 25, 2026
d9edf5d
Documented Image Write in Event
Kuba663 Aug 25, 2026
3790dab
Documented `barrier` in Event
Kuba663 Aug 25, 2026
9e4e07c
A bunch of shit
Kuba663 Aug 25, 2026
34ee291
Fix up method signatures
Kuba663 Aug 25, 2026
0f86519
Smart Pointer for events
Kuba663 Aug 25, 2026
13359e0
Document CommandQueueDispatch
Kuba663 Aug 25, 2026
02ba75d
Document com.cleanroommc.compute.cmd
Kuba663 Aug 25, 2026
f77a371
Document Image
Kuba663 Aug 26, 2026
6c5a791
Document Image1D
Kuba663 Aug 26, 2026
ec8f387
Document Image1DArray
Kuba663 Aug 26, 2026
b60be82
Document Image1DBuffer
Kuba663 Aug 26, 2026
4090dff
Document Image2D
Kuba663 Aug 26, 2026
7e577ed
Document Image2DArray
Kuba663 Aug 26, 2026
8a0ca9a
Document Image3D
Kuba663 Aug 26, 2026
844b710
Minor fixes
Kuba663 Aug 26, 2026
37fe3cd
Document Kernel
Kuba663 Aug 26, 2026
618d71e
KernelMetadata was already documented
Kuba663 Aug 26, 2026
484af43
Document Pipe
Kuba663 Aug 26, 2026
f0bb6a5
Fix typo
Kuba663 Aug 27, 2026
95dd72a
Fix a Segmentation Fault
Kuba663 Aug 27, 2026
a6b1b8c
Accommodate the new Kernel invocation signature
Kuba663 Aug 27, 2026
8c02417
Document Sampler
Kuba663 Aug 27, 2026
c17a90c
Document FilteringMode and AddressingMode
Kuba663 Aug 27, 2026
569d69a
Document ChannelOrder
Kuba663 Aug 27, 2026
971acc0
Remove unused variables
Kuba663 Aug 27, 2026
91d75c2
Document Kernels
Kuba663 Aug 27, 2026
3b0041d
Document Compute
Kuba663 Aug 27, 2026
13a475e
Document ComputeSetup
Kuba663 Aug 27, 2026
eef6bf9
Document Device
Kuba663 Aug 27, 2026
3bf483b
Document KernelParameterList
Kuba663 Aug 27, 2026
aae8f7d
Document KernelParameter
Kuba663 Aug 27, 2026
1bffb23
Document params that need documenting.
Kuba663 Aug 27, 2026
9557c20
Document params that need documenting in KernelParameterList.
Kuba663 Aug 27, 2026
8edd02b
Move Compute init to FMLLaunchHandler
Kuba663 Aug 27, 2026
eed2d8d
GC Wash
Kuba663 Aug 27, 2026
47c8e10
SweepTask
Kuba663 Aug 27, 2026
b4f2b0b
Put ReentrantLocks into GarbageCollector
Kuba663 Aug 27, 2026
82b4dde
Proper atomic operations for running & stopping the sweep task
Kuba663 Aug 27, 2026
2ac9c19
Merge remote-tracking branch 'official/feature/cleanroomgradle' into …
Kuba663 Aug 27, 2026
2da837d
Shut the task down immediately in case of a `wash`.
Kuba663 Aug 27, 2026
8bddfd8
Reentrant locks in SmartPointer
Kuba663 Aug 27, 2026
2d0461f
Reentrant locks in Buffer
Kuba663 Aug 27, 2026
18b8200
Reentrant locks in CommandQueue
Kuba663 Aug 27, 2026
0b4bb0c
Make ttl atomic
Kuba663 Aug 27, 2026
ad08b84
Add locks to images
Kuba663 Aug 27, 2026
13e03d4
Hopefully fixed test
Kuba663 Aug 28, 2026
46e2581
Add the new test annotation
Kuba663 Aug 28, 2026
188f434
New LICENSE.md
Kuba663 Aug 28, 2026
f120a8e
Merge branch 'feature/cleanroomgradle' into feature/compute
Kuba663 Aug 28, 2026
e668479
Remove some weird exception type from nowhere
Kuba663 Aug 28, 2026
7e358c5
RegisterComputeProgramsEvent
Kuba663 Aug 29, 2026
d74902d
Compiling programs
Kuba663 Aug 29, 2026
5fa6579
Better method of side acquisition
Kuba663 Aug 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ dependencies {

api platform('org.lwjgl:lwjgl-bom:3.4.3')
api 'org.lwjgl:lwjgl'
api 'org.lwjgl:lwjgl-opencl'
api 'org.lwjgl:lwjgl-jemalloc'
api 'org.lwjgl:lwjgl-opengl'
api 'org.lwjgl:lwjgl-openal'
Expand All @@ -199,7 +200,8 @@ dependencies {
// Test
testImplementation platform('org.junit:junit-bom:6.1.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'org.junit.vintage:junit-vintage-engine'
testImplementation 'org.junit.platform:junit-platform-launcher'
}

tasks.withType(Test).configureEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cleanroommc.boot;
package com.cleanroommc.cleanroom.boot;

import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cleanroommc.boot;
package com.cleanroommc.cleanroom.boot;

import java.util.LinkedHashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cleanroommc.boot;
package com.cleanroommc.cleanroom.boot;

public class MainServer extends Main {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
package com.cleanroommc.client.windows;

import com.cleanroommc.client.sdl.SystemTheme;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Library;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;


public interface NtDll extends Library {

/* NTDLL */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cleanroommc.common;
package com.cleanroommc.cleanroom.common;

import com.google.common.eventbus.EventBus;
import net.minecraftforge.fml.common.DummyModContainer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cleanroommc.common;
package com.cleanroommc.cleanroom.common;

import com.google.common.eventbus.EventBus;
import net.minecraftforge.common.ForgeEarlyConfig;
Expand Down
247 changes: 247 additions & 0 deletions module/cleanroom/src/main/java/com/cleanroommc/compute/Compute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package com.cleanroommc.compute;

import com.cleanroommc.compute.cmd.CommandQueueDispatch;
import com.cleanroommc.compute.programs.ComputeProgram;
import com.cleanroommc.compute.programs.ProgramCacheIntegrityTable;
import com.cleanroommc.kirino.utils.MinecraftResourceUtils;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;
import com.google.common.graph.Traverser;
import it.unimi.dsi.fastutil.Stack;
import it.unimi.dsi.fastutil.longs.LongArraySet;
import it.unimi.dsi.fastutil.objects.Object2ObjectAVLTreeMap;
import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import net.minecraft.util.ResourceLocation;
import org.apache.logging.log4j.Logger;
import org.jspecify.annotations.Nullable;
import org.lwjgl.opencl.CL10;
import org.lwjgl.opencl.CLCapabilities;
import org.lwjgl.system.MemoryStack;

import java.nio.IntBuffer;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

/**
* Hub of the API.
* @author EΣrie
* @apiNote Singleton.
*/
public class Compute {

private static @Nullable Compute INSTANCE = null;

/**
* Platform capabilities.
*/
public final CLCapabilities PLATFORM_CAPABILITIES;

/**
* Logging.
*/
public final Logger LOGGER;

/**
* The OpenCL context.
*/
public final long context;
/**
* All devices available to the platform.
*/
public final Device[] devices;
/**
* How all the headers depend on eachother.
*/
public final MutableGraph<ResourceLocation> dependencyGraph = GraphBuilder.directed().build();
/**
* Cache of all the headers.
*/
public final Map<ResourceLocation, Long> libraries = new Object2ObjectAVLTreeMap<>();
/**
* All programs.
*/
public final Map<ResourceLocation, ComputeProgram> programs = new Object2ObjectAVLTreeMap<>();
/**
* Used for dispatching queues.
*/
public final CommandQueueDispatch queueDispatch = new CommandQueueDispatch();

/**
* Is there a device that supports images?
*/
public final boolean supportsImages;
/**
* Is there a device that supports mipmaps?
*/
public final boolean supportsMipmaps;
/**
* Is there a device that supports pipes?
*/
public final boolean supportsPipes;

/**
* Is GL sharing supported?
*/
public final boolean glSharing;

/**
* Unused
*/
private final ProgramCacheIntegrityTable programCacheIntegrityTable = new ProgramCacheIntegrityTable();

/**
* Initialize Compute.
* @param log logger
* @param platformCapabilities platform capabilities
* @param context OpenCL context
* @param devices all devices
* @param isClient is this on the client (if yes enable gl sharing)
* @author EΣrie
*/
private Compute(Logger log, CLCapabilities platformCapabilities, long context, Device[] devices, boolean isClient) {
this.LOGGER = log;
this.PLATFORM_CAPABILITIES = platformCapabilities;
this.context = context;
this.devices = devices;

Arrays.sort(devices);

boolean supportsImages = false;
boolean supportsMipmaps = false;
boolean supportsPipes = false;

for (Device device : devices) {
if (device.supportsImages()) supportsImages = true;
if (device.supportsMipmaps()) supportsMipmaps = true;
if (device.supportsPipes()) supportsPipes = true;
if (supportsImages && supportsMipmaps && supportsPipes)
break;
}

this.supportsImages = supportsImages;
this.supportsMipmaps = supportsMipmaps;
this.supportsPipes = supportsPipes;

this.glSharing = isClient;
}

/**
* @return Singleton instance.
* @author EΣrie
*/
@SuppressWarnings("DataFlowIssue")
public static Compute instance() {
return INSTANCE;
}

/**
* Register a program for compilation.
* @param location Where is the program in the "assets/modid/compute/" subfolder?
* @author EΣrie
*/
public void registerProgram(ResourceLocation location) {
programs.put(location, new ComputeProgram(location));
}

/**
* Compile all programs.
* @author EΣrie
*/
void compilePrograms() {
for (ComputeProgram program : programs.values()) {
try (MemoryStack stack = MemoryStack.stackPush()) {
program.compile(programCacheIntegrityTable, stack);
}
}
}

/**
* Reads headers.
* @param rl start header
* @param stack MemoryStack for temporary variables.
* @return OpenCL handles to all headers.
* @apiNote This caches headers, then puts them into a dependency graph to account for headers that include headers.
* @author EΣrie
*/
public Long[] getOrCreateLibraries(ResourceLocation rl, MemoryStack stack) {
Set<Long> found = new LongArraySet();
if (libraries.containsKey(rl)) {
Traverser<ResourceLocation> dependencies = Traverser.forGraph(dependencyGraph);
dependencies.breadthFirst(rl).forEach(dep -> found.add(libraries.get(dep)));
found.add(libraries.get(rl));
return found.toArray(new Long[0]);
}
Stack<ResourceLocation> browsable = new ReferenceArrayList<>();
browsable.push(rl);
while (!browsable.isEmpty()) {
ResourceLocation curr = browsable.pop();
String src = MinecraftResourceUtils.readText(new ResourceLocation(curr.getNamespace(),
"compute/" + curr.getPath()),
MinecraftResourceUtils.NewLineType.BACK_SLASH_N);
IntBuffer err_code = stack.mallocInt(1);
long program = CL10.clCreateProgramWithSource(Compute.instance().context, src, err_code);
switch(err_code.get(0)) {
case CL10.CL_INVALID_VALUE -> throw new NullPointerException(String.format("Source code of %s is null. ", curr));
case CL10.CL_OUT_OF_RESOURCES, CL10.CL_OUT_OF_HOST_MEMORY -> throw new OutOfMemoryError("Not enough resources available to create OpenCL program.");
}
libraries.put(curr, program);
dependencyGraph.addNode(curr);
Set<ResourceLocation> dependencies = ComputeProgram.getHeadersFromFile(src, curr);
for (ResourceLocation dep : dependencies) {
browsable.push(dep);
dependencyGraph.putEdge(curr, dep);
}
found.add(program);
}
return found.toArray(new Long[0]);
}

/**
* Get a device by its handle.
* @param handle device handle
* @return {@link Device}
* @author EΣrie
*/
public Device getDevice(long handle) {
if (devices[0].handle() == handle)
return devices[0];
int div = 2;
int idx = Math.clamp(devices.length / div, 1, devices.length - 1);
do {
if (devices[idx].handle() == handle)
return devices[idx];
div += 2;
if (devices[idx].handle() < handle)
idx = Math.clamp(idx - (devices.length / div), 1, devices.length - 1);
else
idx = Math.clamp(idx + (devices.length / div), 1, devices.length - 1);
} while (div <= devices.length);
throw new IllegalArgumentException("Device not present.");
}

/**
* Initialize Compute.
* @param log Logger
* @param platform Platform capabilities
* @param context OpenCL context
* @param isClient Is this on the client?
* @param devices All devices
* @apiNote Initialise the singleton.
* @author EΣrie
*/
static void init(Logger log, CLCapabilities platform, long context, boolean isClient, Device... devices) {
if (INSTANCE != null) {
throw new RuntimeException("Second attempt at invoking singleton constructor. ");
}
INSTANCE = new Compute(log, platform, context, devices, isClient);
}

/**
* @return Is OpenCL available
* @author EΣrie
*/
public static boolean isAvailable() {
return INSTANCE != null;
}
}
Loading