Skip to content

Commit

Permalink
rm lovr.graphics.initialize/isInitialized;
Browse files Browse the repository at this point in the history
This only existed because the graphics module had to know about the
window during initialization, but this is no longer the case.
  • Loading branch information
bjornbytes committed Nov 3, 2023
1 parent c98db5b commit 8ed5572
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 78 deletions.
4 changes: 0 additions & 4 deletions etc/boot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ function lovr.boot()
lovr.system.openWindow(conf.window)
end

if lovr.graphics then
lovr.graphics.initialize()
end

if lovr.headset then
lovr.headset.start()
end
Expand Down
110 changes: 49 additions & 61 deletions src/api/l_graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,65 +320,6 @@ static void luax_writeshadercache(void) {
free(data);
}

static int l_lovrGraphicsInitialize(lua_State* L) {
GraphicsConfig config = {
.debug = false,
.vsync = false,
.stencil = false,
.antialias = true
};

bool shaderCache = true;

luax_pushconf(L);
lua_getfield(L, -1, "graphics");
if (lua_istable(L, -1)) {
lua_getfield(L, -1, "debug");
config.debug = lua_toboolean(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "vsync");
config.vsync = lua_toboolean(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "stencil");
config.stencil = lua_toboolean(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "antialias");
config.antialias = lua_toboolean(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "shadercache");
shaderCache = lua_toboolean(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 2);

if (shaderCache) {
config.cacheData = luax_readfile(".lovrshadercache", &config.cacheSize);
}

if (lovrGraphicsInit(&config)) {
luax_atexit(L, lovrGraphicsDestroy);

// Finalizers run in the opposite order they were added, so this has to go last
if (shaderCache) {
luax_atexit(L, luax_writeshadercache);
}
}

free(config.cacheData);

return 0;
}

static int l_lovrGraphicsIsInitialized(lua_State* L) {
bool initialized = lovrGraphicsIsInitialized();
lua_pushboolean(L, initialized);
return 1;
}

static int l_lovrGraphicsIsTimingEnabled(lua_State* L) {
bool enabled = lovrGraphicsIsTimingEnabled();
lua_pushboolean(L, enabled);
Expand Down Expand Up @@ -1420,8 +1361,6 @@ static int l_lovrGraphicsNewPass(lua_State* L) {
}

static const luaL_Reg lovrGraphics[] = {
{ "initialize", l_lovrGraphicsInitialize },
{ "isInitialized", l_lovrGraphicsIsInitialized },
{ "isTimingEnabled", l_lovrGraphicsIsTimingEnabled },
{ "setTimingEnabled", l_lovrGraphicsSetTimingEnabled },
{ "submit", l_lovrGraphicsSubmit },
Expand Down Expand Up @@ -1472,5 +1411,54 @@ int luaopen_lovr_graphics(lua_State* L) {
luax_registertype(L, Model);
luax_registertype(L, Readback);
luax_registertype(L, Pass);

GraphicsConfig config = {
.debug = false,
.vsync = false,
.stencil = false,
.antialias = true
};

bool shaderCache = true;

luax_pushconf(L);
lua_getfield(L, -1, "graphics");
if (lua_istable(L, -1)) {
lua_getfield(L, -1, "debug");
config.debug = lua_toboolean(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "vsync");
config.vsync = lua_toboolean(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "stencil");
config.stencil = lua_toboolean(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "antialias");
config.antialias = lua_toboolean(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "shadercache");
shaderCache = lua_toboolean(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 2);

if (shaderCache) {
config.cacheData = luax_readfile(".lovrshadercache", &config.cacheSize);
}

if (lovrGraphicsInit(&config)) {
luax_atexit(L, lovrGraphicsDestroy);

// Finalizers run in the opposite order they were added, so this has to go last
if (shaderCache) {
luax_atexit(L, luax_writeshadercache);
}
}

free(config.cacheData);
return 1;
}
22 changes: 9 additions & 13 deletions src/modules/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,34 +624,30 @@ static void onMessage(void* context, const char* message, bool severe);
bool lovrGraphicsInit(GraphicsConfig* config) {
if (state.initialized) return false;

state.config = *config;

gpu_config gpu = {
.debug = config->debug,
.callback = onMessage,
.engineName = "LOVR",
.engineVersion = { LOVR_VERSION_MAJOR, LOVR_VERSION_MINOR, LOVR_VERSION_PATCH },
.device = &state.device,
.features = &state.features,
.limits = &state.limits
};

.limits = &state.limits,
#ifdef LOVR_VK
gpu.vk.cacheData = config->cacheData;
gpu.vk.cacheSize = config->cacheSize;
#ifndef LOVR_DISABLE_HEADSET
if (lovrHeadsetInterface) {
gpu.vk.getPhysicalDevice = lovrHeadsetInterface->getVulkanPhysicalDevice;
gpu.vk.createInstance = lovrHeadsetInterface->createVulkanInstance;
gpu.vk.createDevice = lovrHeadsetInterface->createVulkanDevice;
}
.vk.cacheData = config->cacheData,
.vk.cacheSize = config->cacheSize,
#endif
#if defined(LOVR_VK) && !defined(LOVR_DISABLE_HEADSET)
.vk.getPhysicalDevice = lovrHeadsetInterface ? lovrHeadsetInterface->getVulkanPhysicalDevice : NULL,
.vk.createInstance = lovrHeadsetInterface ? lovrHeadsetInterface->createVulkanInstance : NULL,
.vk.createDevice = lovrHeadsetInterface ? lovrHeadsetInterface->createVulkanDevice : NULL,
#endif
};

if (!gpu_init(&gpu)) {
lovrThrow("Failed to initialize GPU");
}

state.config = *config;
state.timingEnabled = config->debug;

// Temporary frame memory uses a large 1GB virtual memory allocation, committing pages as needed
Expand Down

0 comments on commit 8ed5572

Please sign in to comment.