Skip to content

Commit 46ff723

Browse files
committed
More abstractions
1 parent da4380e commit 46ff723

File tree

19 files changed

+106
-46
lines changed

19 files changed

+106
-46
lines changed

packages/arisu-app/plugin/render.lua

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ local UniformBlock = require("arisu.gl.uniform_block")
99
local TextureManager = require("arisu.gl.texture_manager")
1010
local FontManager = require("arisu.gl.font_manager")
1111

12-
local Device = require("arisu-gfx.device.gl")
12+
local Instance = require("arisu-gfx.instance")
1313

1414
---@class arisu.plugin.Render.Context
1515
---@field window winit.Window
16-
---@field renderCtx gfx.Context
16+
---@field surface gfx.gl.Surface
1717
---@field quadVAO VAO
1818
---@field quadPipeline Pipeline
1919
---@field quadVertex gfx.Buffer
@@ -42,12 +42,17 @@ local Device = require("arisu-gfx.device.gl")
4242
---@field mainCtx arisu.plugin.Render.Context?
4343
---@field contexts table<winit.Window, arisu.plugin.Render.Context>
4444
---@field sharedResources arisu.plugin.Render.SharedResources?
45+
---@field device gfx.Device
4546
local RenderPlugin = {}
4647
RenderPlugin.__index = RenderPlugin
4748

4849
---@param windowPlugin arisu.plugin.Window
4950
function RenderPlugin.new(windowPlugin)
50-
return setmetatable({ contexts = {}, windowPlugin = windowPlugin }, RenderPlugin)
51+
local instance = Instance.new()
52+
local adapter = instance:requestAdapter({ powerPreference = "high-performance" })
53+
local device = adapter:requestDevice()
54+
55+
return setmetatable({ device = device, contexts = {}, windowPlugin = windowPlugin }, RenderPlugin)
5156
end
5257

5358
---@param window winit.Window
@@ -65,26 +70,23 @@ function RenderPlugin:register(window)
6570
local ctx = self.windowPlugin:getContext(window)
6671
assert(ctx, "Window context not found for render plugin")
6772

68-
ctx.renderCtx:makeCurrent()
69-
70-
-- todo: dont make this here
71-
local device = Device.new()
73+
ctx.surface.context:makeCurrent()
7274

7375
local vertexDescriptor = VertexLayout.new()
7476
:withAttribute({ type = "f32", size = 3, offset = 0 }) -- position (vec3)
7577
:withAttribute({ type = "f32", size = 4, offset = 12 }) -- color (rgba)
7678
:withAttribute({ type = "f32", size = 2, offset = 28 }) -- uv
7779
:withAttribute({ type = "f32", size = 1, offset = 36 }) -- texture id
7880

79-
local quadVertex = device:createBuffer({ size = vertexDescriptor:getStride() * 1000, usages = { "VERTEX" } })
80-
local quadIndex = device:createBuffer({ size = util.sizeof("u16") * 1000, usages = { "INDEX" } })
81+
local quadVertex = self.device:createBuffer({ size = vertexDescriptor:getStride() * 1000, usages = { "VERTEX" } })
82+
local quadIndex = self.device:createBuffer({ size = util.sizeof("u16") * 1000, usages = { "INDEX" } })
8183

8284
local quadVAO = VAO.new()
8385
quadVAO:setVertexBuffer(quadVertex, vertexDescriptor)
8486
quadVAO:setIndexBuffer(quadIndex)
8587

86-
local overlayVertex = device:createBuffer({ size = vertexDescriptor:getStride() * 1000, usages = { "VERTEX" } })
87-
local overlayIndex = device:createBuffer({ size = util.sizeof("u16") * 1000, usages = { "INDEX" } })
88+
local overlayVertex = self.device:createBuffer({ size = vertexDescriptor:getStride() * 1000, usages = { "VERTEX" } })
89+
local overlayIndex = self.device:createBuffer({ size = util.sizeof("u16") * 1000, usages = { "INDEX" } })
8890

8991
local overlayVertexDescriptor = VertexLayout.new()
9092
:withAttribute({ type = "f32", size = 3, offset = 0 }) -- position (vec3)
@@ -137,7 +139,7 @@ function RenderPlugin:register(window)
137139
---@type arisu.plugin.Render.Context
138140
local ctx = {
139141
window = window,
140-
renderCtx = ctx.renderCtx,
142+
surface = ctx.surface,
141143
quadVAO = quadVAO,
142144
quadPipeline = quadPipeline,
143145
quadVertex = quadVertex,
@@ -160,7 +162,7 @@ end
160162

161163
---@param ctx arisu.plugin.Render.Context
162164
function RenderPlugin:draw(ctx)
163-
ctx.renderCtx:makeCurrent()
165+
ctx.surface.context:makeCurrent()
164166

165167
gl.enable(gl.BLEND)
166168
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
@@ -182,7 +184,7 @@ end
182184
function RenderPlugin:event(event, handler)
183185
if event.name == "resize" then
184186
local ctx = self:getContext(event.window)
185-
ctx.renderCtx:makeCurrent()
187+
ctx.surface.context:makeCurrent()
186188
gl.viewport(0, 0, ctx.window.width, ctx.window.height)
187189

188190
if util.isWindows() then

packages/arisu-app/plugin/window.lua

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
local Context = require("arisu-gfx")
1+
local Surface = require("arisu-gfx.surface")
22

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

77
---@class arisu.plugin.Window<Message>: { onWindowCreate: Message }
88
---@field mainCtx arisu.plugin.Window.Context?
@@ -18,17 +18,14 @@ end
1818

1919
---@param window winit.Window
2020
function WindowPlugin:register(window)
21-
local renderCtx = Context.new(window, self.mainCtx and self.mainCtx.renderCtx)
21+
local surface = Surface.new(window)
2222

2323
local windowCtx = {
2424
window = window,
25-
renderCtx = renderCtx
25+
surface = surface
2626
}
2727

28-
if not self.mainCtx then
29-
self.mainCtx = windowCtx
30-
end
31-
28+
self.mainCtx = self.mainCtx or windowCtx
3229
self.contexts[window] = windowCtx
3330
end
3431

packages/arisu-gfx/adapter.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---@class gfx.AdapterConfig
2+
---@field powerPreference? "low-power" | "high-performance"
3+
4+
---@class gfx.Adapter
5+
---@field requestDevice fun(self: gfx.Adapter): gfx.Device
6+
local Adapter = require("arisu-gfx.adapter.gl") --[[@as gfx.Adapter]]
7+
8+
return Adapter

packages/arisu-gfx/adapter/gl.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local GLDevice = require("arisu-gfx.device.gl")
2+
3+
---@class gfx.gl.Adapter
4+
local GLAdapter = {}
5+
GLAdapter.__index = GLAdapter
6+
7+
---@param config gfx.AdapterConfig
8+
function GLAdapter.new(config)
9+
return setmetatable({ config = config }, GLAdapter)
10+
end
11+
12+
function GLAdapter:requestDevice()
13+
return GLDevice.new()
14+
end
15+
16+
return GLAdapter

packages/arisu-gfx/adapter/vk.lua

Whitespace-only changes.

packages/arisu-gfx/init.lua

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
local util = require("arisu-util")
2-
31
---@alias gfx.Color { r: number, g: number, b: number, a: number }
4-
5-
---@class gfx.Context
6-
---@field new fun(window: winit.Window, sharedCtx: gfx.Context?): gfx.Context
7-
---@field makeCurrent fun(self: gfx.Context): boolean
8-
---@field swapBuffers fun(self: gfx.Context)
9-
---@field destroy fun(self: gfx.Context)
10-
local Context = util.isWindows()
11-
and require("arisu-gfx.context.win32")
12-
or require("arisu-gfx.context.x11") --[[@as gfx.Context]]
13-
14-
return Context

packages/arisu-gfx/instance.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---@class gfx.Instance
2+
---@field new fun(): gfx.Instance
3+
---@field requestAdapter fun(self: gfx.Instance, options: gfx.AdapterOptions?): gfx.Adapter
4+
local Instance = require("arisu-gfx.instance.gl") --[[@as gfx.Instance]]
5+
6+
return Instance

packages/arisu-gfx/instance/gl.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local GLAdapter = require("arisu-gfx.adapter.gl")
2+
3+
---@class gfx.gl.Instance
4+
local GLInstance = {}
5+
GLInstance.__index = GLInstance
6+
7+
function GLInstance.new()
8+
return setmetatable({}, GLInstance)
9+
end
10+
11+
---@param config gfx.AdapterConfig
12+
function GLInstance:requestAdapter(config)
13+
return GLAdapter.new(config)
14+
end
15+
16+
return GLInstance

packages/arisu-gfx/instance/vk.lua

Whitespace-only changes.

packages/arisu-gfx/surface.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---@class gfx.Surface
2+
---@field new fun(window: winit.Window): gfx.Surface
3+
local Surface = require("arisu-gfx.surface.gl") --[[@as gfx.Surface]]
4+
5+
return Surface

0 commit comments

Comments
 (0)