What makes the GPU so much faster at drawing lines?

In summary, the GPU is optimized for drawing and managing visual content, while the CPU is optimized for running the computer and its other tasks. This specialization allows the GPU to draw lines and other visual elements much faster than the CPU.
  • #1
Edi
177
1
First year learning programming here.
I am trying to write a line drawing algorithm that would be, perhaps, faster than algorithms used today (that would be nice, if I would turn out to be smart and accomplish something nice) so I wrote this piece of code (currently it only draws lines where difference in y is larger than diff in x, but that's just a manner of toppling everything to its side, so to speak :) ):
Code:
procedure TForm1.Brezenhem(x1, y1, x2, y2: integer);
var Pn, dx, dy, xn, yn, xi, yi,i,j, yprev, sign:integer;
var dyddx:single;
var start, stop, elap: cardinal;
begin

 start:=GetTickCount; i:=0;
while (i<200) do
begin

   image1.canvas.Pen.color:=RGB(150,0,255);  // mmm, Lilac - such a great color :)
if (x2-x1<0) then Pn:=-1  else Pn:=1;
dx:=x2-x1+Pn;
dy:=y2-y1;
             if abs(dy)>abs(dx) then
    begin
xi:=x1;
dyddx:=dy/ dx;
   if (y2>y1) then sign:=1 else sign:=-1;
yprev:=y1-sign;
      while(xi<>x1+dx) do  // main loop starts here. "x1" (and y1) is the center of the canvas.
      begin
      yn:=y1+round(dyddx*(xi+Pn-x1));
        {
            j:=0;
            while (j<abs(yprev-yn))  do
                begin
                   image1.Canvas.pixels[xi, yn-j*sign]:=RGB(255,200,0);
                   j:=j+1;
                end;   
          }
                            j:=abs(yprev-yn);
                            image1.canvas.MoveTo(xi,yn);
                            image1.canvas.LineTo(xi,yn-j*sign);
      xi:=xi+Pn;
      yprev:=yn;
      end;
     end;

   i:=i+1;
   end;
   stop:=GetTickCount;
   elap:=stop-start;
   edit1.Text:=IntToStr(elap);
   end;

The idea behind this code is this:
I simply divide difference in y-axis with diff x and I get a number of pixels to draw in y-axis per one iteration of x-axis (as a real value that is rounded). And it works.

But there are some things bothering me:
First I tried running it without the .lineTo, but with the commented-out loop of canvas.pixels and it was about as fast as Brezenhem.
Then i commented the loop out and added the .lineTo part (to only draw the vertical line) - now it works something like 6 times faster then Brezenhem (depending at the angle of the line) - that's quite interesting.
As someone explained to me - the .lineto function is optimized at assembler level, that's why it is faster.. is that right?
If I optimize my simple canvas.pixels vertical line loop at assembler level, it would work even faster than now, because there would only be that simple loop, not the whole, optimized, whatever is hiding under .lineTo, wouldn't it? ( But I have no idea how to do that yet)What is the fastest line algorithm used in games and visual programs? (every 3D model is made from triangles which are drawn using line algorithms)

If i get the fastest algorithm and write it in a code, it still would need to be optimized in assembler lever to really get its speed.. or what?
The optimization in assembler really confused me.
 
Last edited:
Technology news on Phys.org
  • #2
Last edited:
  • #3
This looks like Delphi code to me, if my memory is correct. The problem is that when you modify image1.Canvas.pixels[x, y] you're not directly drawing to the screen, you're executing a whole lot of Delphi library code (and your operating system's API). Whereas image1.canvas.MoveTo() and image1.canvas.LineTo() are executed less frequently and therefore with less overhead. The time taken isn't due to the calculations, it's due to the overhead of calling functions.

As DavidSnider pointed out, fast games can achieve their speed by getting the GPU to draw the line rather than working out the individual pixels in the CPU.
 
  • #4
FWIW: the Bresenham line drawing algorithm and others like you see at in the comparison at edepot, have to be modified by a software engineer to fit a particular environment. So what you see there is based on a PIII chip. Different cpus will do better or worse with the identical code and software libraries.

If you want a lot of speed, consider FORTRAN or C.

An example of software engineering is the discussion (not about line drawing, rather the basics of what your cpu architecture does for and against you because of memory):

http://www.akkadia.org/drepper/cpumemory.pdf Note: most of this article, though 7 years old, is still very relevant.
 
  • #5
DrGreg said:
As DavidSnider pointed out, fast games can achieve their speed by getting the GPU to draw the line rather than working out the individual pixels in the CPU.

Ok, but what exactly makes the GPU faster at drawing a line? The pixels are still calculated somehow, aren't they?
 
  • #6
Yes, but GPU is specialized, so can do the operations faster relying on hardware implementation.
 
  • #7
Edi said:
Ok, but what exactly makes the GPU faster at drawing a line? The pixels are still calculated somehow, aren't they?

To modify and expand on what Borek said:

1. The GPU processor is optimized to handle its calculations in vector or other parallel form, which is different from how the main CPU is designed.

2. The GPU is also equipped with a certain amount of the fastest memory (faster than the computer's RAM), which is located close to the GPU to reduce access time further. This memory is dedicated to the GPU and is not shared by the computer's RAM.

3. And finally, the GPU is relieved of having to manage the running of the rest of the computer like the CPU must, and thus most of its processing cycles are dedicated to drawing and managing images.
 

FAQ: What makes the GPU so much faster at drawing lines?

How can I draw a line faster?

There are a few techniques you can use to draw a line faster. One method is to use a straight edge or ruler to guide your hand and create a straight line. Another option is to use a drafting tool, such as a T-square or triangle, to ensure precise and quick lines. Additionally, you can practice drawing lines with fluid and continuous motions, rather than stopping and starting.

What is the best pencil for drawing lines quickly?

The best pencil for drawing lines quickly will depend on personal preference and the type of line you are trying to create. However, many artists recommend using a mechanical pencil with a hard lead (such as 2H or 4H) for clean and precise lines. These pencils are also easy to sharpen and maintain a consistent point, making them great for quick line work.

How can I improve my line drawing speed?

To improve your line drawing speed, it is important to practice regularly. This will help develop muscle memory and increase your hand-eye coordination. Additionally, you can try drawing with your whole arm instead of just your wrist, as this can help create longer and smoother lines. Experimenting with different drawing tools and techniques can also help you find the best method for drawing lines quickly.

How do I avoid smudging my lines while drawing?

Smudging is a common issue when drawing lines, especially with traditional media like graphite or charcoal. To avoid smudging, you can try using a piece of scrap paper under your hand while drawing to prevent your skin from coming into contact with the drawing surface. You can also use a fixative spray on your finished drawing to help prevent smudging and preserve your work.

Are there any digital tools that can help me draw lines faster?

Yes, there are many digital tools and software programs available that can help you draw lines faster. These include drawing tablets with pressure-sensitive styluses, which allow for more natural and precise lines. Many drawing programs also have features such as line smoothing and shape recognition, which can help create smoother and faster lines. It is important to experiment and find the tools and techniques that work best for you.

Similar threads

Replies
5
Views
6K
Replies
2
Views
1K
Replies
1
Views
2K
Back
Top