What makes the GPU so much faster at drawing lines?

  • Thread starter Thread starter Edi
  • Start date Start date
  • Tags Tags
    Drawing Line
Click For Summary

Discussion Overview

The discussion centers on the performance differences between CPU and GPU when drawing lines, particularly in the context of programming line drawing algorithms. Participants explore the efficiency of various methods, including the Bresenham algorithm, and the implications of using different programming languages and optimization techniques.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a line drawing algorithm they developed, noting its performance compared to other methods and expressing curiosity about optimization at the assembler level.
  • Another participant suggests that comparing CPU and GPU performance is complex, as modern games predominantly utilize GPUs.
  • A participant explains that using image1.canvas.MoveTo() and image1.canvas.LineTo() incurs less overhead than modifying individual pixels, which involves more extensive library and API calls.
  • There is mention of the need to adapt algorithms like Bresenham for specific environments, with performance varying based on CPU architecture.
  • Some participants highlight that GPUs are specialized for parallel processing, which contributes to their speed in drawing operations.
  • Discussion includes the GPU's access to faster memory and its dedicated processing for graphics, contrasting with the CPU's broader management responsibilities.

Areas of Agreement / Disagreement

Participants express varying views on the specifics of GPU advantages, with some agreeing on the general benefits of parallel processing and dedicated memory, while others seek further clarification on the mechanisms behind GPU performance.

Contextual Notes

Participants note that performance can depend on the specific hardware and software environment, and that optimizations may vary significantly between different programming languages and architectures.

Edi
Messages
176
Reaction score
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
Last edited:
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.
 
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.
 
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?
 
Yes, but GPU is specialized, so can do the operations faster relying on hardware implementation.
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K