Skip to content

Commit 544b046

Browse files
committed
Huge progress
- Move context to an explicit gl_context module - Move surface context to a dedicated swapchain abstraction - Allow headless creation of OpenGL contexts, this is what will be used by default when creating buffers and stuff like that in devices when no surface/swapchain is present, and new created contexts will inherit from this context. Next thing to be done is a render pass needs to be created which explicitly sets the vertex/index buffers and creates the VAO for you internally (and caches it). Because I currently cant use this headless mode without it breaking stuff due to the vao not being shared.
1 parent 46ff723 commit 544b046

File tree

15 files changed

+98
-34
lines changed

15 files changed

+98
-34
lines changed

packages/arisu-app/plugin/render.lua

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ local Instance = require("arisu-gfx.instance")
1313

1414
---@class arisu.plugin.Render.Context
1515
---@field window winit.Window
16-
---@field surface gfx.gl.Surface
16+
---@field swapchain gfx.gl.Swapchain
1717
---@field quadVAO VAO
1818
---@field quadPipeline Pipeline
19-
---@field quadVertex gfx.Buffer
20-
---@field quadIndex gfx.Buffer
19+
---@field quadVertex gfx.gl.Buffer
20+
---@field quadIndex gfx.gl.Buffer
2121
---@field overlayVAO VAO
2222
---@field overlayPipeline Pipeline
23-
---@field overlayVertex gfx.Buffer
24-
---@field overlayIndex gfx.Buffer
23+
---@field overlayVertex gfx.gl.Buffer
24+
---@field overlayIndex gfx.gl.Buffer
2525
---@field ui? arisu.Element
2626
---@field layoutTree? arisu.Layout
2727
---@field computedLayout? arisu.ComputedLayout
@@ -70,7 +70,8 @@ function RenderPlugin:register(window)
7070
local ctx = self.windowPlugin:getContext(window)
7171
assert(ctx, "Window context not found for render plugin")
7272

73-
ctx.surface.context:makeCurrent()
73+
local swapchain = ctx.surface:configure(self.device, {}) --[[@as gfx.gl.Swapchain]]
74+
swapchain.ctx:makeCurrent()
7475

7576
local vertexDescriptor = VertexLayout.new()
7677
:withAttribute({ type = "f32", size = 3, offset = 0 }) -- position (vec3)
@@ -139,7 +140,7 @@ function RenderPlugin:register(window)
139140
---@type arisu.plugin.Render.Context
140141
local ctx = {
141142
window = window,
142-
surface = ctx.surface,
143+
swapchain = swapchain,
143144
quadVAO = quadVAO,
144145
quadPipeline = quadPipeline,
145146
quadVertex = quadVertex,
@@ -162,7 +163,7 @@ end
162163

163164
---@param ctx arisu.plugin.Render.Context
164165
function RenderPlugin:draw(ctx)
165-
ctx.surface.context:makeCurrent()
166+
ctx.swapchain.ctx:makeCurrent()
166167

167168
gl.enable(gl.BLEND)
168169
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
@@ -184,7 +185,7 @@ end
184185
function RenderPlugin:event(event, handler)
185186
if event.name == "resize" then
186187
local ctx = self:getContext(event.window)
187-
ctx.surface.context:makeCurrent()
188+
ctx.swapchain.ctx:makeCurrent()
188189
gl.viewport(0, 0, ctx.window.width, ctx.window.height)
189190

190191
if util.isWindows() then

packages/arisu-app/plugin/window.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local Surface = require("arisu-gfx.surface")
22

33
---@class arisu.plugin.Window.Context
44
---@field window winit.Window
5-
---@field surface gfx.gl.Surface
5+
---@field surface gfx.Surface
66

77
---@class arisu.plugin.Window<Message>: { onWindowCreate: Message }
88
---@field mainCtx arisu.plugin.Window.Context?

packages/arisu-gfx/device/gl.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
local GLBuffer = require("arisu-gfx.buffer.gl")
22
local GLEncoder = require("arisu-gfx.encoder.gl")
3+
local GLContext = require("arisu-gfx.gl_context")
34

45
---@class gfx.gl.Device
6+
---@field globalContext gfx.gl.Context
57
local GLDevice = {}
68
GLDevice.__index = GLDevice
79

810
function GLDevice.new()
9-
return setmetatable({}, GLDevice)
11+
local context = GLContext.fromHeadless()
12+
return setmetatable({ globalContext = context }, GLDevice)
1013
end
1114

1215
---@param descriptor gfx.BufferDescriptor
1316
function GLDevice:createBuffer(descriptor)
17+
-- todo: enable this when vaos dont exist
18+
-- self.globalContext:makeCurrent()
1419
return GLBuffer.new(descriptor)
1520
end
1621

packages/arisu-gfx/gl_context.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local util = require("arisu-util")
2+
3+
---@class gfx.gl.Context
4+
---@field fromHeadless fun(sharedCtx: gfx.gl.Context?): gfx.gl.Context
5+
---@field fromWindow fun(window: winit.Window, sharedCtx: gfx.gl.Context?): gfx.gl.Context
6+
---@field makeCurrent fun(self: gfx.gl.Context): boolean
7+
---@field swapBuffers fun(self: gfx.gl.Context)
8+
---@field destroy fun(self: gfx.gl.Context)
9+
local Context = util.isWindows()
10+
and require("arisu-gfx.gl_context.win32")
11+
or require("arisu-gfx.gl_context.x11") --[[@as gfx.gl.Context]]
12+
13+
return Context
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Win32Context.__index = Win32Context
1111

1212
---@param window winit.win32.Window
1313
---@param sharedCtx Win32Context?
14-
function Win32Context.new(window, sharedCtx)
14+
function Win32Context.fromWindow(window, sharedCtx)
1515
local hdc = user32.getDC(window.hwnd)
1616

1717
local pfDescriptor = gdi.newPFD()
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local glx = require("arisu-x11.glx")
22
local x11 = require("arisu-x11.x11")
3-
local gl = require("arisu-opengl")
43

54
--- @class X11Context
65
--- @field display XDisplay
@@ -9,10 +8,10 @@ local gl = require("arisu-opengl")
98
local X11Context = {}
109
X11Context.__index = X11Context
1110

12-
---@param window winit.x11.Window
11+
---@param display XDisplay
1312
---@param sharedCtx X11Context?
14-
function X11Context.new(window, sharedCtx)
15-
local display = window.display
13+
---@param window winit.x11.Window?
14+
function X11Context.new(display, sharedCtx, window)
1615
local screen = x11.defaultScreen(display)
1716

1817
local fbConfig = glx.chooseFBConfig(display, screen, {
@@ -42,13 +41,31 @@ function X11Context.new(window, sharedCtx)
4241
return setmetatable({ ctx = ctx, display = display, window = window }, X11Context)
4342
end
4443

44+
---@param sharedCtx X11Context?
45+
function X11Context.fromHeadless(sharedCtx)
46+
local display = x11.openDisplay(nil)
47+
return X11Context.new(display, sharedCtx)
48+
end
49+
50+
---@param window winit.x11.Window
51+
---@param sharedCtx X11Context?
52+
function X11Context.fromWindow(window, sharedCtx)
53+
return X11Context.new(window.display, sharedCtx, window)
54+
end
55+
4556
---@return boolean # true on success, false on failure
4657
function X11Context:makeCurrent()
47-
return glx.makeCurrent(self.display, self.window.id, self.ctx) ~= 0
58+
if self.window then
59+
return glx.makeCurrent(self.display, self.window.id, self.ctx) ~= 0
60+
end
61+
62+
return glx.makeContextCurrent(self.display, 0, 0, self.ctx) ~= 0
4863
end
4964

5065
function X11Context:swapBuffers()
51-
glx.swapBuffers(self.display, self.window.id)
66+
if self.window then
67+
glx.swapBuffers(self.display, self.window.id)
68+
end
5269
end
5370

5471
function X11Context:destroy()

packages/arisu-gfx/surface.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
---@class gfx.SurfaceConfig
2+
13
---@class gfx.Surface
24
---@field new fun(window: winit.Window): gfx.Surface
5+
---@field configure fun(self: gfx.Surface, device: gfx.Device, config: gfx.SurfaceConfig): gfx.Swapchain
36
local Surface = require("arisu-gfx.surface.gl") --[[@as gfx.Surface]]
47

58
return Surface

packages/arisu-gfx/surface/gl.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
local util = require("arisu-util")
22

3+
local GLContext = require("arisu-gfx.gl_context")
4+
local GLSwapchain = require("arisu-gfx.swapchain.gl")
5+
36
---@class gfx.gl.Surface
4-
---@field context gfx.Context
7+
---@field window winit.Window
58
local GLSurface = {}
69
GLSurface.__index = GLSurface
710

8-
---@class gfx.Context
9-
---@field new fun(window: winit.Window, sharedCtx: gfx.Context?): gfx.Context
10-
---@field makeCurrent fun(self: gfx.Context): boolean
11-
---@field swapBuffers fun(self: gfx.Context)
12-
---@field destroy fun(self: gfx.Context)
13-
local Context = util.isWindows()
14-
and require("arisu-gfx.surface.gl.win32")
15-
or require("arisu-gfx.surface.gl.x11") --[[@as gfx.Context]]
16-
1711
---@param window winit.win32.Window
1812
function GLSurface.new(window)
19-
local context = Context.new(window, nil)
20-
return setmetatable({ context = context, window = window }, GLSurface)
13+
return setmetatable({ window = window }, GLSurface)
14+
end
15+
16+
---@param device gfx.gl.Device
17+
---@param config gfx.SurfaceConfig
18+
function GLSurface:configure(device, config)
19+
local context = GLContext.fromWindow(self.window, device.globalContext)
20+
return GLSwapchain.new(context)
2121
end
2222

2323
return GLSurface

packages/arisu-gfx/swapchain.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---@class gfx.Swapchain
2+
---@field present fun(self: gfx.Swapchain)
3+
local Swapchain = require("arisu-gfx.swapchain.gl") --[[@as gfx.Swapchain]]
4+
5+
return Swapchain
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local gl = require("arisu-opengl")
2+
3+
---@class gfx.gl.Swapchain
4+
---@field ctx gfx.gl.Context
5+
local GLSwapchain = {}
6+
7+
---@param ctx gfx.gl.Context
8+
function GLSwapchain.new(ctx)
9+
return setmetatable({ ctx = ctx }, GLSwapchain)
10+
end
11+
12+
function GLSwapchain:present()
13+
self.ctx:swapBuffers()
14+
end
15+
16+
return GLSwapchain

0 commit comments

Comments
 (0)