## Performance of the function love.graphics.stencil

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Alexar
Party member
Posts: 174
Joined: Thu Feb 05, 2015 1:57 am
Location: Chengdu,China

### Performance of the function love.graphics.stencil

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

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Performance of the function love.graphics.stencil

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.

pgimeno
Party member
Posts: 2408
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Performance of the function love.graphics.stencil

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.


Alexar
Party member
Posts: 174
Joined: Thu Feb 05, 2015 1:57 am
Location: Chengdu,China

### Re: Performance of the function love.graphics.stencil

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.