Halo render.SuppressEngineLighting #1113

Merged
merged 3 commits into from Mar 1, 2016

Projects

None yet

7 participants

@Luabee
Contributor
Luabee commented Jan 15, 2016

Lighting is not needed when rendering to a stencil buffer because the model is not visibly drawn. Rather, it writes to the buffer in a boolean format. As such, it's wasted computing power to render lighting on models which will never visibly appear.

Using the following code, I ran benchmark tests for rendering a single blue barrel prop's halo (Entity(42)):

//
// RunBenchmark - PRE JUNE 2014 - Josh 'Acecool' Moser
//
function util.Benchmark( _times, func, ... )
    local function benchmark_process( func, ... )
        local x = os.clock( );
        local ret = func( ... );
        local _time = ( os.clock( ) - x );
        MsgC( Color( 0, 255, 255, 255 ), "Benchmark has finished with a total elapsed time of: " .. _time .. "\n" );
        return _time;
    end

    local _time = 0;
    for i = 1, _times do
        _time = _time + benchmark_process( func, ... );
    end

    MsgC( Color( 0, 255, 0, 255 ), "Total Time: \t" .. _time .. "\tseconds\n" );
    MsgC( Color( 0, 255, 0, 255 ), "Average Time: \t" .. _time / _times .. "\tseconds\n" );
end

HALO_RENDER =
            {
                Ents = {Entity(42)},
                Color = Color(255,255,255),
                Hidden = false,
                BlurX = blurx or 2,
                BlurY = blury or 2,
                DrawPasses = passes or 1,
                Additive = false,
                IgnoreZ = false,
                SuppressLighting = true
            }

concommand.Add( "dev_benchload", function( _p, _cmd, _args )
    _p:SendLua([[
        util.Benchmark( 1000, function( )
            halo.Render(HALO_RENDER)
        end )
    ]])
end )

My results:

WITH LIGHTING:

...
Benchmark has finished with a total elapsed time of: 0
Benchmark has finished with a total elapsed time of: 0.00099999999997635
Benchmark has finished with a total elapsed time of: 0
Benchmark has finished with a total elapsed time of: 0.11099999999999
Benchmark has finished with a total elapsed time of: 0.00099999999997635
Benchmark has finished with a total elapsed time of: 0.00099999999997635
Benchmark has finished with a total elapsed time of: 0
Benchmark has finished with a total elapsed time of: 0.00099999999997635
Total Time:     5.354   seconds
Average Time:   0.005354    seconds

SUPPRESSED LIGHTING:

...
Benchmark has finished with a total elapsed time of: 0
Benchmark has finished with a total elapsed time of: 0.00099999999997635
Benchmark has finished with a total elapsed time of: 0
Benchmark has finished with a total elapsed time of: 0
Benchmark has finished with a total elapsed time of: 0
Benchmark has finished with a total elapsed time of: 0
Benchmark has finished with a total elapsed time of: 0
Total Time:     4.8999999999999 seconds
Average Time:   0.0048999999999999  seconds

That's an 8% increase in rendering speed for no change in visual output.

@meepdarknessmeep
Contributor

๐Ÿ‘‰ ๐Ÿ‘‡
๐Ÿ‘† ๐Ÿ‘ˆ

@bmwalters
Contributor

:thumb:

@Kefta
Contributor
Kefta commented Jan 15, 2016

๐Ÿ‘ฏ

@notcake
notcake commented Jan 15, 2016

Please use SysTime() instead of os.clock() for the benchmarks.
SysTime() has better precision.

@ZehM4tt
ZehM4tt commented Jan 16, 2016

Also you should use the correct hook to render Halos whichis PostDrawEffects, you call it from a concommand callback.

@notcake
notcake commented Jan 18, 2016

Your benchmark results are really questionable because you're using a clock without enough precision.

It looks like os.clock has 1 ms precision. ie. maximum error of 0.5 ms
The time difference in your results is 0.454 ms.
Your claimed performance difference has the same order of magnitude as the possible error introduced by your timing source.

@Luabee
Contributor
Luabee commented Jan 18, 2016

I'll run a benchmark with SysTime and report the results shortly.

@Luabee
Contributor
Luabee commented Jan 19, 2016

UPDATED BENCHMARK:


function AddThatHook()

    local HALO_RENDER = {
        Ents = ents.FindByClass( "prop_physics*" ),
        Color = Color(255,255,255),
        Hidden = false,
        BlurX = blurx or 2,
        BlurY = blury or 2,
        DrawPasses = passes or 1,
        Additive = false,
        IgnoreZ = false
    }

    local iterations = 0
    local total = 0
    local avg = 0

    hook.Add("PostDrawEffects", "Benchmarking", function()

        //Get Time
        local x = SysTime();

        //Source: halo.lua
        cam.Start3D()

            halo.Render(HALO_RENDER)

        cam.End3D()

        //Calculate change in time
        local deltaTime = ( SysTime() - x );

        //Calculate statistics
        iterations = iterations + 1
        total = total + deltaTime
        avg = total / iterations


    end)

    hook.Add("Think","Benchmarking", function()
        print("Current reported average: "..avg)
    end)

end
function RemoveThatHook()
    hook.Remove("PostDrawEffects","Benchmarking")
    hook.Remove("Think","Benchmarking")
end

concommand.Add( "dev_benchload", function( _p, _cmd, _args )
    if _args[1] then
        _p:SendLua([[
            AddThatHook()
        ]])
    else
        _p:SendLua([[
            RemoveThatHook()
        ]])
    end
end );

RESULTS:

NO LIGHTING:
...
Current reported average: 0.00054767413406919
Current reported average: 0.00054796440222707
Current reported average: 0.00054796529405078
Current reported average: 0.00054810288411507
Current reported average: 0.00054813972653571
Current reported average: 0.00054802844333631
Current reported average: 0.00054799427593348
Current reported average: 0.00054804276003321
Current reported average: 0.00054806733632809
Current reported average: 0.00054812353004109
Current reported average: 0.00054836361892328
Current reported average: 0.00054834715584249
Current reported average: 0.00054819765017192
Current reported average: 0.00054807583127992
Current reported average: 0.00054801413046668
Current reported average: 0.00054795677357062

WITH LIGHTING:
...
Current reported average: 0.00058913388756667
Current reported average: 0.00058910474569248
Current reported average: 0.00058916806316109
Current reported average: 0.0005891611544134
Current reported average: 0.00058915350520739
Current reported average: 0.00058909549397157
Current reported average: 0.0005890470205492
Current reported average: 0.00058901852924402
Current reported average: 0.00058899529854653
Current reported average: 0.00058892806065924
Current reported average: 0.00058892355729117
Current reported average: 0.00058888377332369
Current reported average: 0.00058890065878753
Current reported average: 0.00058883114988906

This is on gm_construct in the spawn area using two blue barrels and a models/props_building_details/Storefront_Template001a_Bars.mdl.

The difference is .00004 seconds faster in this case.

Test 2 using 26 props:

NO LIGHTING:
Current reported average: 0.00085073990272901
Current reported average: 0.00085074859507516
Current reported average: 0.00085070099147253
Current reported average: 0.00085077014080843
Current reported average: 0.00085084757441337
Current reported average: 0.00085086912194419
Current reported average: 0.00085080212451044
Current reported average: 0.00085107294860344
Current reported average: 0.00085100208084445
Current reported average: 0.00085091307636529
Current reported average: 0.00085080142188407
Current reported average: 0.00085070971530284
Current reported average: 0.00085059274747168
Current reported average: 0.0008506856438193
Current reported average: 0.00085065602630583
Current reported average: 0.00085059807707418
Current reported average: 0.0008518950362866
Current reported average: 0.00085191089664038
Current reported average: 0.00085208186221827
Current reported average: 0.00085232908167869
Current reported average: 0.00085228649234349


WITH LIGHTING:
Current reported average: 0.00127392881773
Current reported average: 0.0012738554558506
Current reported average: 0.0012737604077541
Current reported average: 0.0012736598897812
Current reported average: 0.001273502477668
Current reported average: 0.0012734154920359
Current reported average: 0.0012732252122491
Current reported average: 0.0012731041697309
Current reported average: 0.0012731292578192
Current reported average: 0.0012729247742731
Current reported average: 0.0012730209396948
Current reported average: 0.0012728695246906
Current reported average: 0.0012727284405475
Current reported average: 0.0012727656673363
Current reported average: 0.0012726185918108
Current reported average: 0.0012724907376099
Current reported average: 0.0012724957490152
Current reported average: 0.0012724012626863
Current reported average: 0.0012722553843518
Current reported average: 0.0012722116695391
Current reported average: 0.0012720510271537
Current reported average: 0.0012718962118446
Current reported average: 0.0012719708307017

Here the speed difference increases by a magnitude of 10 to .0004 seconds

CONCLUSION:

While the difference isn't as large as previously thought, it is still clear that computing engine lighting takes longer than suppressing it. Furthermore, this difference increases as the number of entities increases.

@notcake
notcake commented Jan 20, 2016

๐Ÿ‘

@robotboy655 robotboy655 merged commit 0d62a10 into garrynewman:master Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment