| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.device.BackendCreationException;
|
| 4 | import com.mojang.renderpearl.api.device.GpuBackend;
|
| 5 | import com.mojang.renderpearl.api.device.GpuDebugOptions;
|
| 6 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 7 | import com.mojang.renderpearl.frontend.FrontendGpuDevice;
|
| 8 | import java.util.Objects;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 | import org.lwjgl.opengl.GL;
|
| 13 | import org.lwjgl.sdl.SDLError;
|
| 14 | import org.lwjgl.sdl.SDLVideo;
|
| 15 | import org.lwjgl.system.SharedLibrary;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class GlBackend implements GpuBackend {
|
| 19 | private static final int VERSION_MAJOR = 3;
|
| 20 | private static final int VERSION_MINOR = 3;
|
| 21 | private boolean libraryLoaded;
|
| 22 | private @Nullable BackendCreationException libraryLoadFailure;
|
| 23 |
|
| 24 | @Override
|
| 25 | public String getName() {
|
| 26 | return "OpenGL";
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public void loadLibrary() throws BackendCreationException {
|
| 31 | if (!this.libraryLoaded) {
|
| 32 | if (this.libraryLoadFailure != null) {
|
| 33 | throw this.libraryLoadFailure;
|
| 34 | }
|
| 35 |
|
| 36 | if (!SDLVideo.SDL_GL_LoadLibrary(((SharedLibrary)GL.getFunctionProvider()).getPath())) {
|
| 37 | this.libraryLoadFailure = new BackendCreationException(
|
| 38 | "OpenGL is not supported: " + Objects.requireNonNullElse(SDLError.SDL_GetError(), "<no error>"), BackendCreationException.Reason.OPENGL_MISSING
|
| 39 | );
|
| 40 | throw this.libraryLoadFailure;
|
| 41 | }
|
| 42 |
|
| 43 | if (GL.getFunctionProvider().getFunctionAddress("glGetError") != SDLVideo.SDL_GL_GetProcAddress("glGetError")) {
|
| 44 | this.libraryLoadFailure = new BackendCreationException("glGetError mismatch", BackendCreationException.Reason.OPENGL_MISSING);
|
| 45 | SDLVideo.SDL_GL_UnloadLibrary();
|
| 46 | throw this.libraryLoadFailure;
|
| 47 | }
|
| 48 |
|
| 49 | this.libraryLoaded = true;
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void unloadLibrary() {
|
| 55 | if (this.libraryLoaded) {
|
| 56 | SDLVideo.SDL_GL_UnloadLibrary();
|
| 57 | this.libraryLoaded = false;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public long createWindow(final @Nullable String title, final int width, final int height, final long flags) {
|
| 63 | SDLVideo.SDL_GL_SetAttribute(17, 3);
|
| 64 | SDLVideo.SDL_GL_SetAttribute(18, 3);
|
| 65 | SDLVideo.SDL_GL_SetAttribute(20, 1);
|
| 66 | SDLVideo.SDL_GL_SetAttribute(19, 2);
|
| 67 | SDLVideo.SDL_GL_SetAttribute(22, 1);
|
| 68 | return SDLVideo.SDL_CreateWindow(title, width, height, 2L | flags);
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public GpuDevice createDevice(final GpuDebugOptions debugOptions) throws BackendCreationException {
|
| 73 | return new FrontendGpuDevice(new GlDevice(this, debugOptions));
|
| 74 | }
|
| 75 | }
|
| 76 |
|