Page 1 of 1

Performance of the function love.graphics.stencil

Posted: Mon Apr 08, 2019 8:50 am
by Alexar
I am working on a ui lib.
It seems that stencil test function affects performance very much. Did i make some mistake ?
see the example below:
try to hit the no stencil button and see the difference. the FPS is shown as the title.
example.love
(9.65 MiB) Downloaded 625 times

Re: Performance of the function love.graphics.stencil

Posted: Mon Apr 08, 2019 10:34 am
by raidho36
It's very difficult to tell because that's not a minimal example; getting any info would require digging through your entire GUI library. Please create a single-file example with smallest amount of code possible that still demonstrates the problem.

Re: Performance of the function love.graphics.stencil

Posted: Mon Apr 08, 2019 11:00 am
by pgimeno
Used this way, yes it's slow. It's switching to stencil mode, drawing to the stencil, then switching to drawing mode and drawing to the screen, for every rectangle you're drawing, and that prevents parallelism.

Perhaps you could try to make it in two passes, first drawing everything to the stencil buffer and then drawing everything again to the screen with the stencil active. If you have less than 256 elements, you can even use one stencil value for each element.

And if you have 256 or more, you can maybe draw the first 255 to the stencil, then use the stencil to draw the first 255 to the screen, then clear the stencil and keep drawing the next 255.

Graphically, well, in ASCII art, this is what you're doing and what I propose:

Code: Select all

What you're doing:

Draw to the stencil with value 1

Stencil:
    111111111111111
    111111111111111
    111111111111111



Draw the contents where stencil = 1

Screen:
    +-------------+
    |    Hello    |
    +-------------+



Clear the stencil and draw to it with value 1

Stencil:
                       111111111
                       111111111
                       111111111


Draw the contents where stencil = 1

Screen:
    +-------------+
    |    Hello    |    +-------+
    +-------------+    | World |
                       +-------+

etc etc


What I propose:

Draw to the stencil with value 1

Stencil:
    111111111111111
    111111111111111
    111111111111111


Draw to the stencil with value 2

Stencil:
    111111111111111
    111111111111111    222222222
    111111111111111    222222222
                       222222222

etc etc (up to 255)

Draw the contents where stencil = 1

Screen:
    +-------------+
    |    Hello    |
    +-------------+


Draw the contents where stencil = 2

Screen:
    +-------------+
    |    Hello    |    +-------+
    +-------------+    | World |
                       +-------+

etc etc (up to 255)

If more than 255, clear the stencil and start with value 1 again.

Re: Performance of the function love.graphics.stencil

Posted: Wed Apr 10, 2019 12:30 am
by Alexar
pgimeno wrote: Mon Apr 08, 2019 11:00 am Used this way, yes it's slow. It's switching to stencil mode, drawing to the stencil, then switching to drawing mode and drawing to the screen, for every rectangle you're drawing, and that prevents parallelism.

Perhaps you could try to make it in two passes, first drawing everything to the stencil buffer and then drawing everything again to the screen with the stencil active. If you have less than 256 elements, you can even use one stencil value for each element.

And if you have 256 or more, you can maybe draw the first 255 to the stencil, then use the stencil to draw the first 255 to the screen, then clear the stencil and keep drawing the next 255.
Thank you , i will try with that !
raidho36 wrote: Mon Apr 08, 2019 10:34 am It's very difficult to tell because that's not a minimal example; getting any info would require digging through your entire GUI library. Please create a single-file example with smallest amount of code possible that still demonstrates the problem.
Thank you for your reply. that's my fault.

Re: Performance of the function love.graphics.stencil

Posted: Wed Apr 10, 2019 12:44 am
by pgimeno
Forgot to say. Maybe you can just use a scissor instead of a stencil? That should be faster.