@@ -24,11 +24,26 @@ local function executeOp(op)
2424 end
2525end
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+
2738function 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
0 commit comments