Author Topic: I wished for a FAST version of ...  (Read 780 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
I wished for a FAST version of ...
« on: September 20, 2023, 05:35:32 PM »
I wished for a FAST version of ... GetPixel () and SetPixelV().  My wish never came true.

So I had to create my "Quick Pixels" library to do the job.  And today, I have achieved
something that Microsoft should have put into the GDI32.DLL 30 years ago.  It is fast.
It is REALLY !@#$%^& FAST!   And it is SUPER EASY to use.


SUB DrawPhongBall(Radius AS SINGLE, hdc AS HDC)
    '=======================================================
    ' A much simpler and much improved Phong Ball Generator
    ' Now adds reflection (shine) to the surface
    '=======================================================
    DIM AS INT r = RND2(1, 255) ' Random red component
    DIM AS INT g = RND2(1, 255) ' Random green component
    DIM AS INT b = RND2(1, 255) ' Random blue component
    DIM AS INT shine = 250      ' Shine intensity

    OpenBmpData (hdc)

    FOR INT Y = -Radius TO Radius
        FOR INT X = -Radius TO Radius
            DIM RAW distance = HYPOT(X, Y)
            IF distance <= Radius THEN
                DIM AS FLOAT alpha = 1.0 - (distance / Radius)                        ' Equation for semi-transparency
                DIM AS FLOAT shineAlpha = (1.0 - (distance / Radius)) * (shine / 255) ' Adjust shine based on distance
                DIM RAW AS DWORD GradientColor = RGB(r * alpha, g * alpha, b * alpha)
                DIM RAW AS DWORD ShineColor = RGB(shineAlpha * 255, shineAlpha * 255, shineAlpha * 255)
                ' Blend the main color and shine color
                GradientColor = (GradientColor BAND &H00FFFFFF) BOR ((ShineColor BAND &H00FFFFFF) << 24)
                SetPixelFast(X + Radius, Y + Radius, GradientColor)
            END IF
        NEXT
    NEXT

    CloseBmpData (hdc)
END FUNCTION



The original version of this demo did not have those three highlighted lines of code.  The original
version used BCX_PSET(0, X + Radius, Y + Radius, GradientColor, hdc) instead
of the SetPixelFast statement.

The original version took 15 seconds to draw 100 phong-shaded balls of varying sizes
and store them into an array of bitmaps for quick display.

My new "Quick Pixels" library reduced those 15 seconds down to 1 second.

I'll be uploading at least four new demos, after BCX 8.0.6 is released.  Until then, I just
wanted to give some of you something to look forward to. 

So tell me, is anyone looking forward to this?






jbk

  • Full Member
  • ***
  • Posts: 211
    • View Profile
Re: I wished for a FAST version of ...
« Reply #1 on: September 20, 2023, 06:06:22 PM »
I am  :)
on a side note, QB64 also has what they call hardware graphics, I believe that it talks to the GPU directly -- it's blazing fast see https://www.qb64tutorial.com/lesson22
they also have a _Limit function that regulates the FPS
« Last Edit: September 20, 2023, 06:08:10 PM by jbk »

Jeff

  • Administrator
  • Newbie
  • *****
  • Posts: 40
    • View Profile
Re: I wished for a FAST version of ...
« Reply #2 on: September 20, 2023, 06:27:19 PM »
So tell me, is anyone looking forward to this?

I always look forward to performance improvements!  :)
Certainly intrigued by this.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
Re: I wished for a FAST version of ...
« Reply #3 on: September 20, 2023, 07:19:31 PM »
I am  :)
on a side note, QB64 also has what they call hardware graphics, I believe that it talks to the GPU directly -- it's blazing fast see https://www.qb64tutorial.com/lesson22
they also have a _Limit function that regulates the FPS
Hi JB - why would a numbers guy be interested in graphics?    ;)

I might be wrong but I'm pretty sure OpenGL is at the heart of QB64 graphics and its QB45 support. 
OpenGL supports direct hardware acceleration, along with software emulation, so that's likely how
QB64 is using the GPU.  Certainly nothing wrong with any of that. 

QB64's _Limit is easy enough to emulate using SLEEP(SomeMilliSeconds) or using a timer.
You could probably set process affinity using BCX but I've never needed to and never tried it.

BCX's only dependency is the Windows API, and lately I've been trying to squeeze more juice from it.




Quickie notes:
https://www.easytechjunkie.com/what-is-accelerated-opengl-mode.htm


https://saturncloud.io/blog/gpu-programming-cuda-or-opencl/

OpenCL is not OpenGL although the common link is to make GPU's
do the heavy lifting, because they operate so much faster than CPU's.



MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
Re: I wished for a FAST version of ...
« Reply #4 on: September 20, 2023, 07:32:27 PM »
So tell me, is anyone looking forward to this?

I always look forward to performance improvements!  :)
Certainly intrigued by this.

Hi Jeff,

Lately, I've been re-learning Windows regions.  Regions help us place square bitmaps
into round holes, so to speak.  My goal was to take a square patch of moon surface, squeeze
it into a circle, blend it with just the right amount of phong shading, to see whether I could
produce something that looks like a 3D photo of the moon.  That required a GetPixel/SetPixel
speed improvement, big time.

Have a look at the before and after, in the attached jpg.



« Last Edit: September 21, 2023, 11:18:12 AM by MrBcx »

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
Re: I wished for a FAST version of ...
« Reply #5 on: September 20, 2023, 08:40:33 PM »
I am  :)
on a side note, QB64 also has what they call hardware graphics, I believe that it talks to the GPU directly -- it's blazing fast see https://www.qb64tutorial.com/lesson22
they also have a _Limit function that regulates the FPS

I just ran HWvsSW.bas on QB64.  WOW!

That is really cool and gives me something else to study up on.

My software FPS was 15 ... my hardware FPS was 300+

The thing is, the graphics card in this PC is an integrated Intel HD 530 which I didn't know
could be used that way.  I just found this link from 2 years ago, so I have homework to do.

https://hothardware.com/news/cuda-on-intel-gpus-zluda

SIDE NOTE:

The QB64 demo launched 800 little black and white icons.  I wondered how my "Quick Pixels"
and plain old GDI could handle a bump from 100 to 800 moving objects, so I just tried it on
one of my forthcoming demos that I call, "Marble Storm".  See attached screenshot.

Even with my "Quick Pixels", it took about 7 seconds for the demo to draw, convert to bitmap,
and store them in an array for animating.  All 800 animate smoothly and in good speed.


« Last Edit: September 21, 2023, 11:21:04 AM by MrBcx »

mekanixx

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: I wished for a FAST version of ...
« Reply #6 on: September 21, 2023, 12:52:05 PM »
I wished for a FAST version of ... GetPixel () and SetPixelV().  My wish never came true.

....

So tell me, is anyone looking forward to this?

You bet! Thanks for pushing the limits! I don't always reply but I appreciate your (and everyone elses) efforts to make BCX better! And a big thanks to Robert for all the fine documentation in the help file!

Doyle