Improve Graphics API initialization

This commit is contained in:
JeremyStar™ 2024-07-21 21:12:59 +02:00
parent 2469e124bd
commit 80478e6a69
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
5 changed files with 71 additions and 29 deletions

View file

@ -24,6 +24,7 @@ import de.staropensource.sosengine.base.annotations.EventListener;
import de.staropensource.sosengine.base.classes.helpers.EventHelper; import de.staropensource.sosengine.base.classes.helpers.EventHelper;
import de.staropensource.sosengine.base.data.info.EngineInformation; import de.staropensource.sosengine.base.data.info.EngineInformation;
import de.staropensource.sosengine.base.data.versioning.StarOpenSourceVersioningSystem; import de.staropensource.sosengine.base.data.versioning.StarOpenSourceVersioningSystem;
import de.staropensource.sosengine.base.internal.events.InternalEngineShutdownEvent;
import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.logging.LoggerInstance;
import de.staropensource.sosengine.base.types.CodePart; import de.staropensource.sosengine.base.types.CodePart;
import de.staropensource.sosengine.base.types.DependencyVector; import de.staropensource.sosengine.base.types.DependencyVector;
@ -36,6 +37,7 @@ import de.staropensource.sosengine.graphics.classes.ApiMainClass;
import de.staropensource.sosengine.graphics.classes.ApiManagementClass; import de.staropensource.sosengine.graphics.classes.ApiManagementClass;
import de.staropensource.sosengine.graphics.events.GraphicsApiErrorEvent; import de.staropensource.sosengine.graphics.events.GraphicsApiErrorEvent;
import de.staropensource.sosengine.graphics.opengl.events.GraphicsErrorEvent; import de.staropensource.sosengine.graphics.opengl.events.GraphicsErrorEvent;
import de.staropensource.sosengine.graphics.opengl.exceptions.GlfwInitializationException;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWErrorCallback;
@ -133,24 +135,18 @@ public final class OpenGlSubsystem implements ApiMainClass {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void initializeApi() { public void initializeApi() {
logger.verb("Initializing Graphics API"); // Set error callback
try (GLFWErrorCallback errorCallback = GLFWErrorCallback.create(new GraphicsErrorEvent())) {
errorCallback.set();
}
long initTime = Miscellaneous.measureExecutionTime(() -> { // Initialize GLFW
// Set error callback if (!glfwInit())
try (GLFWErrorCallback errorCallback = GLFWErrorCallback.create(new GraphicsErrorEvent())) { throw new GlfwInitializationException();
errorCallback.set();
}
// Initialize GLFW // Initialize management class
if (!glfwInit()) internalApi = new OpenGlInternalApi();
logger.crash("Unable to initialize GLFW"); management = new OpenGlManagement();
// Initialize management class
internalApi = new OpenGlInternalApi();
management = new OpenGlManagement();
});
logger.info("Initialized Graphics API in " + initTime + "ms");
} }
/** /**

View file

@ -0,0 +1,34 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Contributors
* Licensed under the GNU Affero General Public License v3
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.staropensource.sosengine.graphics.opengl.exceptions;
/**
* Thrown when GLFW fails to initialize.
*
* @since v1-alpha2
*/
public class GlfwInitializationException extends RuntimeException {
/**
* Constructs this exception.
*
* @since v1-alpha2
*/
public GlfwInitializationException() {}
}

View file

@ -24,4 +24,11 @@ package de.staropensource.sosengine.graphics.opengl.exceptions;
* *
* @since v1-alpha2 * @since v1-alpha2
*/ */
public class NotOnMainThreadException extends Throwable {} public class NotOnMainThreadException extends RuntimeException {
/**
* Constructs this exception.
*
* @since v1-alpha2
*/
public NotOnMainThreadException() {}
}

View file

@ -201,20 +201,21 @@ public final class GraphicsSubsystem implements SubsystemMainClass {
// Check if registered apis are compatible // Check if registered apis are compatible
for (String apiName : registeredApis.keySet()) for (String apiName : registeredApis.keySet())
if (registeredApis.get(apiName).isCompatible()) { if (registeredApis.get(apiName).isCompatible()) {
logger.diag("Choosing a compatible Graphics API: " + apiName + " is compatible"); logger.diag("" + apiName + " is compatible");
compatibleApis.add(apiName); compatibleApis.add(apiName);
} else } else
logger.diag("Choosing a compatible Graphics API: " + apiName + " is not compatible"); logger.diag(apiName + " is not compatible");
logger.diag("Choosing a compatible Graphics API: Compatible are: " + ListFormatter.formatList(compatibleApis)); logger.diag("Compatible are " + ListFormatter.formatList(compatibleApis));
// Choose last item in list. // Choose last item in list.
if (!compatibleApis.isEmpty()) { if (!compatibleApis.isEmpty()) {
logger.verb("Choosing a compatible Graphics API: Chose Graphics API " + compatibleApis.getLast()); logger.verb("Chose Graphics API " + compatibleApis.getLast());
api = registeredApis.get(compatibleApis.getLast()); api = registeredApis.get(compatibleApis.getLast());
try { try {
api.initializeApi(); logger.diag("Initializing Graphics API " + api.getApiName());
logger.diag("Initialized Graphics API " + api.getApiName() + " in " + Miscellaneous.measureExecutionTime(() -> api.initializeApi()) + "ms");
} catch (Throwable throwable) { } catch (Throwable throwable) {
logger.crash("Graphics API " + api.getApiName() + " failed to initialize", throwable, true); logger.crash("Graphics API " + api.getApiName() + " failed to initialize", throwable, true);
throw throwable; throw throwable;
@ -231,20 +232,25 @@ public final class GraphicsSubsystem implements SubsystemMainClass {
* Sets the Graphics API to use. * Sets the Graphics API to use.
* *
* @param name name of the Graphics API * @param name name of the Graphics API
* @return if the Graphics API has been found
* @see GraphicsSubsystem#setGraphicsApi() * @see GraphicsSubsystem#setGraphicsApi()
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void setGraphicsApi(@NotNull String name) { public boolean setGraphicsApi(@NotNull String name) {
logger.verb("Setting Graphics API " + name);
if (!registeredApis.containsKey(name)) if (!registeredApis.containsKey(name))
logger.crash("Unable to set Graphics API: Graphics API " + name + " is not registered"); return false;
logger.verb("Setting Graphics API " + name);
if (api == null) if (api == null)
api = registeredApis.get(name); api = registeredApis.get(name);
else else
logger.crash("Unable to set Graphics API: Graphics API " + api.getApiName() + " already registered"); logger.crash("Unable to set Graphics API: Graphics API " + api.getApiName() + " already registered");
api.initializeApi(); // Initialize API
logger.diag("Initializing Graphics API " + api.getApiName());
logger.diag("Initialized Graphics API " + api.getApiName() + " in " + Miscellaneous.measureExecutionTime(() -> api.initializeApi()) + "ms");
return true;
} }
} }

View file

@ -103,9 +103,8 @@ public class Main {
logger.info("Hello world!"); logger.info("Hello world!");
// Choose Graphics API to use // Choose Graphics API to use
if (!GraphicsSubsystem.getInstance().setGraphicsApi()) { if (!GraphicsSubsystem.getInstance().setGraphicsApi())
logger.crash("No Graphics API is compatible"); logger.crash("No Graphics API is compatible");
}
// Define 'api' & 'management' variables // Define 'api' & 'management' variables
ApiMainClass api = GraphicsSubsystem.getInstance().getApi(); ApiMainClass api = GraphicsSubsystem.getInstance().getApi();