Skip to content

OpenGL state management system #145

Closed

Description

@kkaefer

We should maintain all current state in the painter object so that we can avoid doing redundant calls (e.g. to bind buffers or switch shaders). Some of that is already done in the switchShader code, but we're still happily binding buffers repeatedly and setting other WebGL state that isn't strictly necessary.

Activity

  1. added this to the milestone on Jun 24, 2014
  2. removed this from the milestone on Jun 15, 2015
  3. lucaswoj commented on Dec 14, 2015

    @lucaswoj
    Contributor

    My dream interface here is to do away with all GL getters / setters in favor of passing all desired state to the draw* call as an object. This is a clean abstraction that will allow us to run the smallest possible # of WebGL api calls, prevent bugs stemming from unexpected GL state, and make our code easier to understand.

  4. jfirebaugh commented on Dec 17, 2015

    @jfirebaugh
    Contributor

    I've been meaning to have a closer look at the API of glium (Rust OpenGL bindings). It promises:

    Glium is stateless. There are no set_something() functions in the entire library, and everything is done by parameter passing. The same set of function calls will always produce the same results, which greatly reduces the number of potential problems.

  5. lucaswoj commented on Jan 21, 2016

    @lucaswoj
    Contributor

    As part of this refactoring, we should merge Painter and gl_utils

  6. lucaswoj commented on Mar 2, 2016

    @lucaswoj
    Contributor

    💭

    gl2.drawElements({
        shader: getShader('circle'),
        mode: gl2.TRIANGLES,
    
        // Allow circles to be drawn across boundaries, so that
        // large circles are not clipped to tiles
        stencilTest: false,
    
        depthMask: false,
        depthSubrange: getDepthSubrange(0),
    
        count: group.getCount(),
        offset: group.getOffset(),
        elementBuffer: group.getElementBuffer(),
        vertexBuffer: group.getVertexBuffer(),
    
        attributes: {
            a_pos: group.getVertexBuffer().pos,
        },
    
        uniforms: {
            u_exmatrix: transform.exMatrix,
            u_posmatrix: translatePosMatrix(
                calculatePosMatrix(coord, source.maxzoom),
                tile,
                layer.paint['circle-translate'],
                layer.paint['circle-translate-anchor']
            ),
            u_color: util.premultiply(layer.paint['circle-color'], layer.paint['circle-opacity']),
            u_blur: Math.max(layer.paint['circle-blur'], 1 / browser.devicePixelRatio / layer.paint['circle-radius'])),
            u_size: layer.paint['circle-radius']
        }
    });
  7. lucaswoj commented on Mar 2, 2016

    @lucaswoj
    Contributor

    It'd be cool to build this as an external library, maybe including our Buffer implementation too.

  8. lucaswoj commented on Jun 1, 2016

    @lucaswoj
    Contributor

    As long as performance is acceptable, https://github.com/mikolalysenko/regl would be a perfect fit for this.

  9. lucaswoj commented on Jul 28, 2016

    @lucaswoj
    Contributor

    As part of this work, I am interested in exploring other abstractions around VAOs

  10. added this to the Barcelona milestone on Jul 28, 2016
  11. 16 remaining items

  12. self-assigned this
    on Dec 19, 2016
  13. removed their assignment
    on Jan 9, 2017
  14. changed the title [-]Maintain state in renderer to avoid redundant calls[/-] [+]OpenGL state management system[/+] on May 10, 2017
  15. ibgreen commented on Jun 26, 2017

    @ibgreen

    @kkaefer Based on our discussion, we (the luma.gl/deck.gl team) would like to propose the following stand-alone webgl state management system: trackContextState as a possible base for enabling on a true WebGL plugin API in mapbox-gl-js.

    In its current incarnation, this state management system basically offers three functions, trackContextState(gl), pushContextState(gl) and popContextState(gl).

    Comments:

    • While this code is currently part of the luma.gl repository, it has no dependencies on luma and is in the completely independent base webgl-utils folder.
    • We can make this available as a separate repository + npm module (or you could copy the code and use this as a base as you see fit).
    • Currently this only covers context parameters, but it can be extend it based on your requirements, for instance to also cover buffer bindings etc. You'll need to set the requirements here.
    • This module currently also caches state access for speed. If you already have similar solutions we can make sure we disable that part for you.
    • As discussed we might be able to collaborate on/support you on a PR to help you integrate this, should you like it.

    Let me know if the code is reviewable in its current state or if we need to further preparations.
    The following files are of interest:

  16. self-assigned this
    on Oct 24, 2017
  17. mourner commented on Mar 1, 2019

    @mourner
    Member

    There don't seem to be any clear next actions here after we started tracking most of the GL state. Should we close this @kkaefer? We could outline any potential remaining work in a new ticket.

  18. ibgreen commented on Mar 1, 2019

    @ibgreen

    There don't seem to be any clear next actions here after we started tracking most of the GL state. Should we close thi

    @mourner Just an update from our side, we have now published our WebGL state tracker as a separate module.

    Since it sounds like you have rolled your own system, maybe we should sync to make sure our two state tracking systems are compatible?

    Ours is based on WebGL context interception and is thus very generic, is that how you also went about things?

    CC: @Pessimistress @tsherif

  19. chloekraw commented on Apr 10, 2019

    @chloekraw
    Contributor
  20. ansis commented on Apr 10, 2019

    @ansis
    Contributor

    Our implementation can mostly be found in https://github.com/mapbox/mapbox-gl-js/blob/master/src/gl/value.js and https://github.com/mapbox/mapbox-gl-js/blob/master/src/gl/context.js. I'm not that familiar with the design choices behind our implementation.

    maybe we should sync to make sure our two state tracking systems are compatible?

    While this would be nice, do you think the benefits here would be significant? My guess would be that cost of assuming the state is undefined when you switch between libraries once or twice a frame should be pretty insignificant.

  21. ibgreen commented on Apr 10, 2019

    @ibgreen

    @ansis Thanks.

    do you think the benefits of [syncing to make sure our systems are compatible] would be significant?

    If there aren't any problems there should not be a need to spend much time on this. That said it doesn't hurt if we understand each other's systems.

    Some observations looking at your code:

    • Looks like you are building a nice typesafe wrapper class on top of the WebGLContext.
    • Our implementation is more raw, it replaces functions on the webgl context itself.

    So in that sense, our implementations do not clash. Thoughts:

    • We can track all changes to the context, including the ones your Context class make, as your methods ultimately hit the gl... methods
    • However, as far as I can tell, you may not see changes that we make to the context.
    • Our solution has push state and pop state (save and restore), I couldn't see if you have functions that completely restores a context or just per-setting management.
    • @tsherif who is tech leading luma.gl now.
  22. mourner commented on Sep 11, 2019

    @mourner
    Member

    Closing as an issue without clear next actions — let's open new issues for any specific items we should address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature 🍏medium priorityperformance ⚡

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions