gpu/internal/opengl: save and restore GL state only for shared contexts

Most Gio programs have exlusive access to their OpenGL contexts, where
saving and restoring of the GL state is not required.

This change applies to all OpenGL platforms, but matters most for WASM
where syscalls are slow.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-10-05 08:41:12 +02:00
parent 3abc891680
commit 30ecf75a0f
2 changed files with 17 additions and 3 deletions
+3
View File
@@ -50,6 +50,9 @@ type OpenGL struct {
// empty for all other platforms; an OpenGL context is assumed current when // empty for all other platforms; an OpenGL context is assumed current when
// calling NewDevice. // calling NewDevice.
Context gl.Context Context gl.Context
// Shared instructs users of the context to restore the GL state after
// use.
Shared bool
} }
type Direct3D11 struct { type Direct3D11 struct {
+14 -3
View File
@@ -23,6 +23,7 @@ type Backend struct {
glstate glState glstate glState
state state state state
savedState glState savedState glState
sharedCtx bool
glver [2]int glver [2]int
gles bool gles bool
@@ -201,6 +202,7 @@ func newOpenGLDevice(api driver.OpenGL) (driver.Device, error) {
floatTriple: floatTriple, floatTriple: floatTriple,
alphaTriple: alphaTripleFor(ver), alphaTriple: alphaTripleFor(ver),
srgbaTriple: srgbaTriple, srgbaTriple: srgbaTriple,
sharedCtx: api.Shared,
} }
b.feats.BottomLeftOrigin = true b.feats.BottomLeftOrigin = true
if srgbErr == nil { if srgbErr == nil {
@@ -216,13 +218,20 @@ func newOpenGLDevice(api driver.OpenGL) (driver.Device, error) {
b.feats.Features |= driver.FeatureTimers b.feats.Features |= driver.FeatureTimers
} }
b.feats.MaxTextureSize = f.GetInteger(gl.MAX_TEXTURE_SIZE) b.feats.MaxTextureSize = f.GetInteger(gl.MAX_TEXTURE_SIZE)
if !b.sharedCtx {
// We have exclusive access to the context, so query the GL state once
// instead of at each frame.
b.glstate = b.queryState()
}
return b, nil return b, nil
} }
func (b *Backend) BeginFrame(target driver.RenderTarget, clear bool, viewport image.Point) driver.Texture { func (b *Backend) BeginFrame(target driver.RenderTarget, clear bool, viewport image.Point) driver.Texture {
b.clear = clear b.clear = clear
b.glstate = b.queryState() if b.sharedCtx {
b.savedState = b.glstate b.glstate = b.queryState()
b.savedState = b.glstate
}
b.state = state{} b.state = state{}
var renderFBO gl.Framebuffer var renderFBO gl.Framebuffer
if target != nil { if target != nil {
@@ -286,7 +295,9 @@ func (b *Backend) EndFrame() {
} }
b.sRGBFBO.Blit() b.sRGBFBO.Blit()
} }
b.restoreState(b.savedState) if b.sharedCtx {
b.restoreState(b.savedState)
}
} }
func (b *Backend) queryState() glState { func (b *Backend) queryState() glState {