jzy3d / jzy3d-api

A Java API for 3d and 2d charts
BSD 3-Clause "New" or "Revised" License
282 stars 145 forks source link

[EmulGL] Software rendering performance can be significantly improved by optimising blend_pixel #261

Open valb3r opened 2 years ago

valb3r commented 2 years ago

switch - case inside blend_pixel causes almost 2x performance degradation due to mixing of branching and numeric pipeline

Pipeline flame graph:

Screenshot 2022-04-30 at 17 11 46

As BlendFunc rarely changes during polygon rendering, one can try to use lambdas to optimise pixel blending performance

jzy3d commented 2 years ago

Good catch! Is this x2 factor an estimate or did you already try?

One thing you may be interested in is BenchmarkRun and BenchmarkPlot in module jzy3d-tests-java9. It evaluates rendering time based on content to render. I used it to show how sensitive EmulGL and jGL are to the number of polygons to render, assuming these polygons (either a single, 10 or 100) always cover the same number of pixels.

Capture d’écran 2022-04-30 à 18 38 36

valb3r commented 2 years ago

Is this x2 factor an estimate or did you already try?

I've tested that on M1 Air with Apple Silicon, went from 120ms to 60ms per frame when I removed switch-case and kept only actual branch Will try BenchmarkRun for better consistency

jzy3d commented 2 years ago

You will see that this test only involves black pixel on a flat surface. The idea was mainly to have a guess on sensitivity to number of pixels to paint vs. polygon you’re draw. It should not be hard to derive it and evaluate blending cost on a colored surface.

One point worth mentioning : EmulGL has an unsatisfying way of handling blending. The less alpha you configure, the darker polygon you have. I’ll send you links about it.

jzy3d commented 2 years ago

Quality.Advanced() with alpha has faded colors.

image

Quality.Intermediate with no alpha has vivid colors

image

I am willing to understand where this fading effect comes from. Using alpha = 0 creates black pixels instead of background color pixels.