Skip to content

Commit 1366e66

Browse files
committed
Explicit index buffer format
1 parent bc1783a commit 1366e66

File tree

6 files changed

+53
-21
lines changed

6 files changed

+53
-21
lines changed

packages/arisu-app/plugin/render.lua

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ function RenderPlugin:draw(ctx)
164164
op = {
165165
type = "clear",
166166
color = {
167-
r = (math.sin(os.clock() * 10) + 1) / 2,
167+
r = (math.sin(os.clock()) + 1) / 2,
168168
g = 0,
169-
b = (math.cos(os.clock() * 10) + 1) / 2,
169+
b = (math.cos(os.clock()) + 1) / 2,
170170
a = 1
171171
}
172172
},
@@ -178,26 +178,17 @@ function RenderPlugin:draw(ctx)
178178
encoder:setBindGroup(0, self.sharedResources.bindGroup)
179179
encoder:setViewport(0, 0, ctx.window.width, ctx.window.height)
180180
encoder:setVertexBuffer(0, ctx.quadVertex)
181-
encoder:setIndexBuffer(ctx.quadIndex)
182-
encoder:drawIndexed(ctx.nIndices, 1)
181+
encoder:setIndexBuffer(ctx.quadIndex, gfx.IndexType.u32)
182+
encoder:drawIndexed(0, 1)
183183
encoder:endRendering()
184184

185185
local commandBuffer = encoder:finish()
186186
self.device.queue:submit(commandBuffer)
187187

188-
-- gl.enable(gl.BLEND)
189-
-- gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
190-
188+
-- last things that need to be implemented
191189
-- gl.enable(gl.DEPTH_TEST)
192190
-- gl.depthFunc(gl.LESS_EQUAL)
193191
-- gl.clear(bit.bor(gl.COLOR_BUFFER_BIT, gl.DEPTH_BUFFER_BIT))
194-
195-
-- gl.viewport(0, 0, ctx.window.width, ctx.window.height)
196-
197-
-- ctx.quadPipeline:bind()
198-
-- self.sharedResources.textureManager:bind()
199-
-- ctx.quadVAO:bind()
200-
-- gl.drawElements(gl.TRIANGLES, ctx.nIndices, gl.UNSIGNED_INT, nil)
201192
end
202193

203194
---@param event winit.Event

packages/arisu-gfx/command_buffer/gl.lua

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,26 @@ local function executeOp(op)
2424
end
2525
end
2626

27+
---@type table<gfx.gl.Context, gfx.gl.VAO>
28+
local vaos = setmetatable({}, {
29+
__mode = "k",
30+
})
31+
32+
---@type table<gfx.IndexFormat, number>
33+
local indexFormatToGL = {
34+
[gfx.IndexType.u16] = gl.UNSIGNED_SHORT,
35+
[gfx.IndexType.u32] = gl.UNSIGNED_INT,
36+
}
37+
2738
function GLCommandBuffer:execute()
2839
---@type gfx.gl.Pipeline?
2940
local pipeline
30-
local vao = GLVAO.new()
31-
vao:bind()
41+
42+
--- TODO: absolutely cache the vao somewhere instead of recreating it every frame
43+
---@type gfx.gl.VAO?
44+
local vao
45+
46+
local indexType = gl.UNSIGNED_INT
3247

3348
for _, command in ipairs(self.commands) do
3449
if command.type == "beginRendering" then
@@ -41,6 +56,15 @@ function GLCommandBuffer:execute()
4156
end
4257

4358
texture.context:makeCurrent()
59+
if not vaos[texture.context] then
60+
local vao = GLVAO.new()
61+
vaos[texture.context] = vao
62+
63+
print("created vao", vao.id)
64+
end
65+
vao = vaos[texture.context]
66+
vao:bind()
67+
4468
gl.bindFramebuffer(gl.FRAMEBUFFER, texture.framebuffer)
4569
executeOp(attachment.op)
4670
end
@@ -68,13 +92,14 @@ function GLCommandBuffer:execute()
6892
vao:setVertexBuffer(command.buffer, descriptor, command.slot)
6993
elseif command.type == "setIndexBuffer" then
7094
vao:setIndexBuffer(command.buffer)
95+
indexType = indexFormatToGL[command.format]
7196
elseif command.type == "writeBuffer" then
7297
local buffer = command.buffer --[[@as gfx.gl.Buffer]]
7398
buffer:setSlice(command.size, command.offset, command.data)
7499
elseif command.type == "setBindGroup" then
75100
-- I don't think this needs to exist for OpenGL
76101
elseif command.type == "drawIndexed" then
77-
gl.drawElements(gl.TRIANGLES, command.indexCount, gl.UNSIGNED_INT, nil)
102+
gl.drawElements(gl.TRIANGLES, command.indexCount, indexType, nil)
78103
else
79104
print("Unknown command type: " .. tostring(command.type))
80105
end

packages/arisu-gfx/command_encoder/gl.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local GLCommandBuffer = require("arisu-gfx.command_buffer.gl")
55
---| { type: "endRendering" }
66
---| { type: "setViewport", x: number, y: number, width: number, height: number }
77
---| { type: "setVertexBuffer", slot: number, buffer: gfx.gl.Buffer, offset: number }
8-
---| { type: "setIndexBuffer", buffer: gfx.gl.Buffer, offset: number }
8+
---| { type: "setIndexBuffer", buffer: gfx.gl.Buffer, offset: number, format: gfx.IndexFormat }
99
---| { type: "setBindGroup", index: number, bindGroup: gfx.BindGroup }
1010
---| { type: "setPipeline", pipeline: gfx.gl.Pipeline }
1111
---| { type: "draw", vertexCount: number, instanceCount: number, firstVertex: number, firstInstance: number }
@@ -45,9 +45,10 @@ function GLCommandEncoder:setVertexBuffer(slot, buffer, offset)
4545
end
4646

4747
---@param buffer gfx.gl.Buffer
48-
---@param offset number
49-
function GLCommandEncoder:setIndexBuffer(buffer, offset)
50-
self.commands[#self.commands + 1] = { type = "setIndexBuffer", buffer = buffer, offset = offset or 0 }
48+
---@param format gfx.IndexFormat
49+
---@param offset number?
50+
function GLCommandEncoder:setIndexBuffer(buffer, format, offset)
51+
self.commands[#self.commands + 1] = { type = "setIndexBuffer", buffer = buffer, format = format, offset = offset or 0 }
5152
end
5253

5354
---@param index number

packages/arisu-gfx/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@ gfx.CompareFunction = {
4848
ALWAYS = 8,
4949
}
5050

51+
---@enum gfx.IndexFormat
52+
gfx.IndexType = {
53+
u16 = 1,
54+
u32 = 2,
55+
}
56+
5157
return gfx

packages/arisu-opengl/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ return {
158158

159159
FLOAT = 0x1406,
160160
UNSIGNED_INT = 0x1405,
161+
UNSIGNED_SHORT = 0x1403,
161162
INT = 0x1404,
162163

163164
TRIANGLES = 0x0004,

packages/arisu/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,14 @@ end
672672
---@param event winit.Event
673673
---@param handler winit.EventManager
674674
function App:event(event, handler)
675+
-- handler:setMode("poll")
676+
677+
-- if event.name == "aboutToWait" then
678+
-- for window in pairs(self.plugins.window.contexts) do
679+
-- handler:requestRedraw(window)
680+
-- end
681+
-- end
682+
675683
local windowUpdate = self.plugins.window:event(event, handler)
676684
if windowUpdate then
677685
return windowUpdate

0 commit comments

Comments
 (0)