• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Roland

Performance when rendering small triangles with expensive fragment shader

9 posts in this topic

Hello,

 

I have stumbled across a behavior I do not understand, when rendering many small triangles with an expensive fragment shader. I am currently trying to write a quite expensive fragment shader (only computations, no texture fetches). I have got the problem that rendering gets considerably slower (by up to a factor of 5) when I rasterize the same number of fragments using a larger number of small primitives instead of a single larger primitive. This is the case, even though I am probably not limited by the vertex shader (I use a trivial vertex shader) and rendering the same number of primitives without the expensive fragment shader is very fast, too. 

 

When I use a small number of primitives, the performance is nicely linear in the complexity of the shader. However, if I use a larger number of primitives (12000 quads, each approximately 6x6 pixels), the behavior is far more non-deterministic and the rendering gets much slower. Attached, I illustrate the performance in a plot. Each line corresponds to a certain number of triangles (ordered in a regular x*x grid of quads rendered into 512x512 pixels). The plot then show the rendering time in dependence on the number of loops performed in the shader.

 

Can anyone explain this behavior? I am aware of the fact that smaller triangles may result in some inefficiencies (e.g. due to fragments at the boundaries of the triangles needed to compute the finite differences), but can that explain a difference in performance by a factor of more than 5 (for example at 32 loops)? Is there some way to avoid this problem (apart from using deferred shading)?

 

For the case it might be relevant: I am using WebGl in Chrome for the rendering, the calls are thus translated by ANGLE to DirectX. The experiments were performed on a Geforce GTX680.

 

Best Regards,

Roland

1

Share this post


Link to post
Share on other sites

The GPU does shading in batches of 4 so if you have multiple triangles smaller than (or so that the "not covered space" of the groups is significant) that it will still run the shader 4 times.

Edited by Waterlimon
0

Share this post


Link to post
Share on other sites

Thank you for the fast answer! I have been wondering whether it is this effect. As far as I could find out, GPUs render blocks of 2x2 pixels as this facilitates the computation of derivatives via finite differences. If this is the case, the effect should be much smaller. For example, when rendering a 64x64 grid, each quad is stilll 8x8 pixels. So in the worst case, we should need 10x10 pixels plus those on the diagonal (should be altogether about 120 shaded pixels). This should not be more than a factor of two. Furthermore, in my tests, the quads should actually be perfectly aligned with the gird as I have a power of two texture and a power of two grid of primitives. If the the GPU actually uses even larger blocks, that might explain the effect, though.

0

Share this post


Link to post
Share on other sites
The GPU will likely be shading around 64 pixels at a time (or 16 2x2 quads).
Multiple triangles within a draw call, and even multiple consecutive draw-calls that use the same state should be able to be merged together before this 64-grouping of pixels occur... So ideally, this shouldn't have any effect on you, as in every case you're drawing thousands of pixels...
Perhaps though the WebGL layer is doing something wacky, like breaking every triangle out into a different draw-call, and changing states between them....? In that (very unlikely) case, your small triangles may only be filling say 18/64 pixels in a group, leading to huge inefficiency.
0

Share this post


Link to post
Share on other sites

Ok, I would also assume that the GPU processes a larger number of fragments at the same time. At least in CUDA, as far as I know, you usually have groups of 32 threads which run in parallel (a warp), so I think this should also be the case for the fragment shaders. However, what I am not sure is whether it is possible to fill these threads with pixels from different triangles. I would hope so, as otherwise GPUs could have a tremendous waste at the geometric complexity used today (e.g. when using displacement mapping you have triangles of just a few pixels).

 

I upload all vertices into one vertex buffer beforehand and then render the full batch of triangles in one drawArrays call (actually I render all triangles ten times, with ten identical calls to increase the times for the benchmark). So I would really hope, that WebGL transfers this in a straightforward fashion into corresponding DirectX calls. I might try the same experiment with OpenGl directly, but somehow I cannot imagine that this would make much of a difference.

 

One other question I am not sure about is in which way the pixels are grouped into the warps. Could it be, that in the case of one primitive only neighbouring pixels are grouped, whereas in the case of many triangles pixels from different places are mixed together, reducing coherence and increasing time lost due to branching. However, I again cannot imagine, that this would cause a difference by a factor of 5 as there is not that much branching in the shader (in the worst case if all branches have to be executed, this should not be much more than a factor of two).

 

Best regards,

Roland

0

Share this post


Link to post
Share on other sites

I have found the main reason for this behaviour. It seems, it was due to the repeated upload of the vertex buffers prior to the rendering. Astonishigly, this resulted in a drastic reduction of the rendering performance when using an expensive pixel shader, even though there were no problems when using a trivial one (as can be seen in the plot, where the performance is good if no loops are performed in the shader).

 

When I avoid repeated uploads and only perform the rendering calls in the loop, the performance is nicely linear in the number of loops and and scales with the number of triangles appoximately as one would expect. So rendering many small triangles takes about four times as long as rendering larger ones. Thank you for your help and suggestions.

2

Share this post


Link to post
Share on other sites

Important is to know (could not find it in your post) wheather the many triangles scenario covers up the same fill on the target. If so, the many triangles scenario will be slower, but I realy cannot say wheather factor of 4 is all ok. Maybe it is , my gess, for batching can do a lot.

0

Share this post


Link to post
Share on other sites

I have found the main reason for this behaviour. It seems, it was due to the repeated upload of the vertex buffers prior to the rendering. Astonishigly, this resulted in a drastic reduction of the rendering performance when using an expensive pixel shader, even though there were no problems when using a trivial one (as can be seen in the plot, where the performance is good if no loops are performed in the shader).

 

When I avoid repeated uploads and only perform the rendering calls in the loop, the performance is nicely linear in the number of loops and and scales with the number of triangles appoximately as one would expect. So rendering many small triangles takes about four times as long as rendering larger ones. Thank you for your help and suggestions.

 

I think I can explain this. Assuming you're using mapped VBOs to upload the data the problem becomes quite apparent. The expensive fragment shader effectively reduces rendering performance, meaning that the data is in use for a longer period of time. Mapping the same VBO is not possible while it's in use, so OpenGL will simply block until the data is no longer in use. This can effectively stall the pipeline completely, giving much worse performance than one would expect.

0

Share this post


Link to post
Share on other sites

from my experience, the problem is the interop between js and gl, it's just frickin slow. you can decode big jpg pictures and load them as textures in less time than just moving some vertices to gl. whatever you're doing, try to avoid that bottleneck, you can rather do tons of stupid computations per vertex to reuse vertices than to setup them again in js.

1

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