Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion to add examples with .removeX() / .dispose() #169

Open
munrocket opened this issue Jun 3, 2020 · 3 comments
Open

Suggestion to add examples with .removeX() / .dispose() #169

munrocket opened this issue Jun 3, 2020 · 3 comments

Comments

@munrocket
Copy link
Contributor

munrocket commented Jun 3, 2020

Seems that we need an examples with textures, framebuffers etc.

gl.deleteBuffer(buffer)
gl.deleteFramebuffer(fb)
gl.deleteProgram(programm)
gl.deleteRenderbuffer(rb)
gl.deleteShader(shader)
gl.deleteTexture(texture)

https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/deleteTexture

@munrocket munrocket changed the title How to .dispose()? Suggestion to add examples with .removeX() / .dispose() Jun 3, 2020
@kdziamura
Copy link
Contributor

These methods mark items as unavailable as I understand (from the end-user perspective). Should the user track objects produced by TWGL manually, or maybe it makes sense to implement some general method to dispose of both WebGL raw data and TWGL objects?

@munrocket
Copy link
Contributor Author

Since twgl is thin wrapper on vanilla webgl probably enough just an example, but I didn't look carefully.

@greggman
Copy link
Owner

greggman commented Jul 7, 2020

Most things are pretty straight forward.

const tex = twgl.createTexture(...);
gl.deleteTexture(tex);
const textures = twgl.createTextures(...);
Object.values(textures).forEach(tex => gl.deleteTexture(tex));
const fbi = twgl.createFrameBufferInfo(...);
gl.deleteFramebuffer(fbi.framebuffer);
for (const attachment of fbi.attachments) {
  if (attachment instanceof WebGLRenderbuffer) {
    gl.deleteRenderbuffer(attachment);
  } else {
    gl.deleteTexture(attachment);
  }
}
const bufferInfo = twgl.createBufferInfoFromArrays(...);
for (const attrib of Object.values(bufferInfo.attribs)) {
  gl.deleteBuffer(attrib.buffer);
}
if (bufferInfo.indices) {
  gl.deleteBuffer(indices);
}

Maybe it would be okay to add functions to do that. The problem is twgl doesn't know how you used them.

you can do this

const bufferInfo1 = twgl.createBufferInfromFromArrays(gl, {
  position: positions,
});

const bufferInfo2 = twgl.createBufferFromArrays(gl, {
   position: { buffer: bufferInfo1.position.buffer },   // use the position buffer from bufferInfo1
});

Similarly you can do this

const fbi1 = twgl.createFramebufferInfo(gl);   // makes an RGBA/UNSIGNED_BYTE color buffer and a depth renderbuffer
const fb2 = twgl.createFramebufferInfo(gl, [
  { format: gl.RGBA, },
  { attach: gl.DEPTH_ATTACHMENT, attachment: fbt1.attachments[1]  },  // reuse same depth buffer
]);

But twgl has no way to track those things are being used twice so if delete functions are added you just have to be aware when you can't use them whereas creation has no such issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants