Sign in to follow this  
Followers 0
irreversible

OpenGL
Efficient GUI clipping on the GPU

7 posts in this topic

While my question is quite simple, the answer is not turning out to be so straightforward: in OpenGL, given an arbitrary number of coplanar rectangular surfaces that overlap (eg a GUI of arbitrary dimensions from something like 150x300px to HD resolutions, including potentially multi-monitor setups), which would make the most sense:

1) do point-in-rect style tests per fragment in the shader
2) order elements on the cpu, keep depth testing off, ignore overdraw and just brute-force everything
3) implement a minuscule z offset and let depth testing do the clipping while ignoring overdraw
4) there's no one-glove-fits-all solution: different platforms require different solutions

I currently have depth testing and masking disabled and I'm brute-forcing everything. It's simple, but not necessarily elegant.

Regarding option 3: I do want to implement some form of clipping and Z-ordering anyway, but I'm not sure if Z-offsetting controls is the solution here. I'm mildly curious as to whether point-in-rect (essentially having a four-part if statement per fragment per intersecting element might be the most viable, especially considering that I want to prepare for a potential software fallback in case of a non-accelerated GL ES implementation or such).

Since the question is somewhat abstract due to scalability, I'd appreciate some thoughts! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by irreversible
0

Share this post


Link to post
Share on other sites
For rectangle clipping take a look at the [url="http://www.opengl.org/sdk/docs/man/xhtml/glScissor.xml"]scissor test [/url].
0

Share this post


Link to post
Share on other sites
A GUI implementation generally generates several and up to tens or even hundreds of clip rectangles.

Scissor test is useful for clipping pixels that fall outside the draw rectangle; I need to clip regions that are inside it (eg not draw under a button while drawing a toolbar).
0

Share this post


Link to post
Share on other sites
You may be solving the wrong problem.

Generally, UI code solves this problem by just drawing the windows in order, front to back (omitting only completely obscured windows). Why? Because that's actually faster than faffing about...

Yes, it generates some overdraw, but that's still usually easier than trying to clip -- UI pixels are often fairly cheap (being things like flat or shade fills of rectangles).

If you're actually running this on a 3D card, then the obvious answer is just to use the Z buffer to sort this problem out for you. Which would be the second approach I'd try, but only if the overdrawing method proved slow.

Go for the simplest approach until you really find it won't work.
2

Share this post


Link to post
Share on other sites
[quote name='irreversible' timestamp='1349307078' post='4986585']
3) implement a minuscule z offset and let depth testing do the clipping while ignoring overdraw
[/quote]Modern GPUs have "Hi-Z" capabilities, where as well as performing the standard z-test for each pixel, they also first perform a low res/less precise z-test that can discard a block of pixels before they're shaded. As long as you render from front-to-back, with a decent z separation per layer, this hardware will help reduce your overdraw costs quite a bit.
1

Share this post


Link to post
Share on other sites
[quote name='irreversible' timestamp='1349340175' post='4986690']
A GUI implementation generally generates several and up to tens or even hundreds of clip rectangles.

Scissor test is useful for clipping pixels that fall outside the draw rectangle; I need to clip regions that are inside it (eg not draw under a button while drawing a toolbar).
[/quote]
To be honest, I don't understand the issue you have yet.So there's the chance that I'm heading in the wrong direction [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] but...

a gui is often structured in a tree-like structure which gives you a natural drawing order. If you want to change this drawing order for elements on the same level (windows i.e.) you just change the node order.

Further on, once you need alpha blending, you need to render them in order,else you will get artifcats when using only the z-buffer.

To clip them on the GPU (ie to keep text in a given rect), use the scissor.

For example, my in-game gui is drawn in immediate mode (!) with several hundreds, even thousands, of rects per frame (ranging from 1024x600 to single chars of 8x8), using shaders, alpha blending, scissor tests etc. with an acceptable impact on performance. And there's quite some space left for optimizations (atlas/texture array, vbo etc.).
0

Share this post


Link to post
Share on other sites
My concerns are mainly for the unaccelerated/emulated systems and have to do with overdrawing controls that have a bunch of small print text or antialiased splines/curves in them, which could make use of early clipping. Katie's remarks about the simplest solution being the best make the most sense, though. I'll just resort to proper Z-ordering and minute offsets for now.

Ashaman - I'm afraid I mean clipping in the reverse sense to what the scissor rect does (as in not drawing what is behind child controls). I'm talking about low performance systems (GL ES) that may or may not be GPU-accelerated.
0

Share this post


Link to post
Share on other sites
I know of at least one AAA 3rd person adventure game, where their occlusion culling system simply selected the 5 largest rectangular walls that were on screen, and tested every object ([i]that passed the frustum cull test[/i]) to see if it was fully behind one of those walls. This seems ridiculously simple compared to PVS/octree/BVH/etc systems, but it worked anyway.

Katie's comment of "[i]omitting only completely obscured windows[/i]" makes sense in the same way -- instead of performing perfect clipping, you could just test small items for full occlusion by large objects. E.g. before submitting a control with an expensive spline, just check to see if it is fully occluded by another window.
If you do all this at once ([i]e.g. foreach object, for each occluder, test bounds[/i]) it should be pretty fast.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By Freezee
      Hi, I'm 16 years old (nearly 17) french guy who loves coding, and pizzas, and music, and a lot of other things...
      I started learning some programming languages 6 years ago, and always failed to achieve something with them. Then I decided to re-try Java 2 years ago, and it went pretty well. So well that from this time I did not stopped programming. I really started to dig into C++ a year ago because I wanted lower level programming skills, with one specific goal: create games. Unfortunately I always overestimate myself and my ideas, and I've not been able to create a single real game because of my lack of experience in that specific domain. So I'm looking for a 3D FPS game project (multiplayer would be great too) to see how that kind of project is managed, and to finally be able to create something. I would like for once to work with other people on the same thing, I think it could really help me to help back the others. I have a lot of free time right now and I'm ready to spend some (if not a lot) into a project.
      I learned a lot of C++ features when I started, but I feel like I'm missing a lot of other features and I want to learn them on something useful.
      I really prefer not working on a project with a pre-used game engine (GM, UE, Unity, ...) because for me the most interesting part is what happens at the lowest programming level of a game. I learned basics of modern OpenGL so if there is a graphical engine to improve, I can work on it. I'm also very interested into working on the game engine structure, and on implementing a scripting language if it's needed. If the game is multiplayer, I will not guarantee that I could really work on that (because I really don't know a lot about networking) but I'll try my best to continue learning things and maybe work on that too.
      If you're interested, feel free to contact me on Discord: Freezee#2283. If you don't have Discord, reply back a way to contact you
    • By Jon Alma
      Some time ago I implemented a particle system using billboarding techniques to ensure that the particles are always facing the viewer.  These billboards are always centered on one 3d coordinate.
      I would like to build on this and use billboarding as the basis for things like laser bolts and gunshots.  Here the difference is that instead of a single point particle I now have to draw a billboard between two points - the start and end of the laser bolt for example.  I appreciate that having two end points places limits on how much the billboard can be rotated to face the viewer, but I'm looking to code a best effort solution.  For the moment I am struggling to work out how to do this or find any tutorials / code examples that explain how to draw a billboard between two points ... can anyone help?
      Thanks.
    • By Sagaceil
      It's always better to fight with a bro.
    • By recp
      Hi,
      I'm working on new asset importer (https://github.com/recp/assetkit) based on COLLADA specs, the question is not about COLLADA directly
      also I'm working on a new renderer to render (https://github.com/recp/libgk) imported document.
      In the future I'll spend more time on this renderer of course, currently rendering imported (implemented parts) is enough for me
      assetkit imports COLLADA document (it will support glTF too),
      importing scene, geometries, effects/materials, 2d textures and rendering them seems working
      My actual confusion is about shaders. COLLADA has COMMON profile and GLSL... profiles,
      GLSL profile provides shaders for effects so I don't need to wory about them just compile, link, group them before render

      The problem occours in COMMON profile because I need to write shaders,
      Actually I wrote them for basic matrials and another version for 2d texture
      I would like to create multiple program but I am not sure how to split this this shader into smaller ones,

      Basic material version (only colors):
      https://github.com/recp/libgk/blob/master/src/default/shader/gk_default.frag
      Texture version:
      https://gist.github.com/recp/b0368c74c35d9d6912f524624bfbf5a3
      I used subroutines to bind materials, actually I liked it,
      In scene graph every node can have different program, and it switches between them if parentNode->program != node->program
      (I'll do scene graph optimizations e.g.  view frustum culling, grouping shaders... later)

      I'm going to implement transparency but I'm considering to create separate shaders,
      because default shader is going to be branching hell
      I can't generate shader for every node because I don't know how many node can be exist, there is no limit.
      I don't know how to write a good uber-shader for different cases:

      Here material struct:
      struct Material { ColorOrTexture emission; ColorOrTexture ambient; ColorOrTexture specular; ColorOrTexture reflective; ColorOrTexture transparent; ColorOrTexture diffuse; float shininess; float reflectivEyety; float transparency; float indexOfRefraction; }; ColorOrTexture could be color or 2d texture, if there would be single colorOrTex then I could split into two programs,
      Also I'm going to implement transparency, I am not sure how many program that I needed

      I'm considering to maintain a few default shaders for COMMON profile,
      1-no-texture, 2-one of colorOrTexture contains texture, 3-........

      Any advices in general or about how to optimize/split (if I need) these shaders which I provied as link?
      What do you think the shaders I wrote, I would like to write them without branching if posible,
      I hope I don't need to write 50+ or 100+ shaders, and 100+ default programs

      PS: These default shaders should render any document, they are not specific, they are general purpose...
             I'm compiling and linking default shaders when app launched

      Thanks
    • By CircleOfLight97
      Hi guys,
      I would like to contribute to a game project as a developer (open source possibly). I have some experiences in C/C++ in game development (perso projects). I don't know either unreal or unity but I have some knowledges in opengl, glsl and shading theory as I had some courses at university regarding to that. I have some knowledges in maths and basic in physics. I know a little how to use blender to do modelling, texturing and simple game assets (no characters, no animation no skinning/rigging). I have no game preferences but I like aventure game, dungeon crawler, platformers, randomly generated things. I know these kind of projects involve a lot of time and I'd be really to work on it but if there are no cleary defined specific design goals/stories/gameplay mechanics I would like to not be part of it x) and I would rather prefer a smaller but well defined project to work on that a huge and not 'finishable' one.
      CircleOfLight97
  • Popular Now