Author Topic: Another raytracer.  (Read 15168 times)

0 Members and 1 Guest are viewing this topic.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #20 on: August 12, 2007 »
something liek this:

Code: [Select]
sub render_thread(world as world_struct pointer)
    dim as integer x,y
    dim as single d
    dim as integer pointer display=world->display+(world->y_start*world->wwidth)
    dim as vector_struct pointer vector=world->vectors+(world->y_start*world->wwidth)
    for y=world->y_start to world->y_end
        for x=0 to world->wwidth-1
            *display=raytrace(world,vector)
            display+=1
            vector+=1
        next
    next
    world->busy=0
end sub

sub render_world(world as world_struct pointer)
   
    dim as integer y=0,i=0
   
    dim world_thread(0 to 3) as world_struct
    dim as any pointer thread(0 to 3)
    world_thread(0)=*world
    world_thread(1)=*world
    world_thread(2)=*world
    world_thread(3)=*world
   
    while y<world->height
        i=(i+1)and 3
        if (@world_thread(i))->busy=0 then
            (@world_thread(i))->y_start=y
            (@world_thread(i))->y_end=y+9
            (@world_thread(i))->busy=1
            thread(i)=threadcreate(@render_thread,@world_thread(i))
            y+=10
        end if
    wend
   
    threadwait(thread(0))
    threadwait(thread(1))
    threadwait(thread(2))
    threadwait(thread(3))
   
end sub

with the field   'busy as integer'   added to the world_struct type.

Does it in blocks of 10 rows at a time, the current values need the screen size to be some multiple of 10 in height.
« Last Edit: August 12, 2007 by Stonemonkey »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Another raytracer.
« Reply #21 on: August 12, 2007 »
You have to watch out for that - the while loop that is checking for busy is very tight - it has no sleep in it so it will take up 100% cpu doing very little.  The correct way to do this is to use semaphores, but I'm not sure FB supports them directly - you might have to use Windows API.
eg. Windows has a function that says
Sleep until one or more of the threads completes and then tell me which one(s) it was.

Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #22 on: August 12, 2007 »
yep, i was wondering about that, i don't think fb can do that and sleep wouldn't be much help either.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #23 on: August 13, 2007 »
Is it something like this:

Code: [Select]
WaitForMultipleObjectsEx(4,@thread(0),0,1,0)

added into the loop?

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Another raytracer.
« Reply #24 on: August 13, 2007 »
Yes, something very like that - then check the return code (or the busy flags of all threads) to see which one to start next.  Is it known whether FB's thread handles are compatible with Windows ones?

Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #25 on: August 13, 2007 »
I have no idea and i'm just trying to make some sense of the standard access rights although i'm just looking at it kind of blankly atm.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #26 on: August 13, 2007 »
Something I'm not to sure about is bAlertable, the description is a little confusing.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Another raytracer.
« Reply #27 on: August 13, 2007 »
Set it to false.  There are many things that can be waited on - thread and process handles, semaphores, events, and overlapped IO, etc.  Overlapped IO is where you tell Windows to go and read or write a load of data, and then go to sleep until the read or write is completed.

You want to set the timeout to INFINITE.

Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #28 on: August 13, 2007 »
Now, if i put in the for loop to test each thread, it crashes/closes. doesn't do that if i just loop i in the while wend loop though. (not just with putting in infinite etc. was the same just before that too)

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #29 on: August 13, 2007 »
ok, found why it crashes, the for loop was letting it draw beyond the screen boundary.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #30 on: August 13, 2007 »
I think I'm getting there now:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Another raytracer.
« Reply #31 on: August 13, 2007 »
any chance of an exe for the C weenies??
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Another raytracer.
« Reply #32 on: August 14, 2007 »
->Fryer, it might be interesting as an experiment to colour code which areas are rendered by which cpu/thread.  I see you've made each thread do 10 lines at a time.  Good for debugging :)

->chris, it's exactly the same as one of the previous exes, except it's now using 4 concurrent threads to do the rendering.  This is making it 2-3x faster on my Core2, and it should help all the P4 HT people, and anyone lucky enough to have a Core2 Quad!

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Another raytracer.
« Reply #33 on: August 14, 2007 »
Stonemonkey dude, it crashes my end.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #34 on: August 14, 2007 »
Nice idea to see what goes on Jim, I'll give that a go.

Sorry Clyde, I take it you're not running the latest version of FB atm?

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Another raytracer.
« Reply #35 on: August 14, 2007 »
Won't run here either (FB1.5) so cant give feedback on speed and such, sorry :) I bet it looks nice though.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #36 on: August 14, 2007 »
Sorry about that, it still just looks the same as before. The only thing is now it's multithreaded so it'd hopefully run a bit better on muti core/cpu machines although mine isn't. It's just turnde into a bit of an experiment tbh, here's how it stands atm with colour banding showing up which thread is rendering.

.exe
« Last Edit: August 14, 2007 by Stonemonkey »

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Another raytracer.
« Reply #37 on: August 14, 2007 »
Doh! Silly me I'll need to bung on V17b.
And Mate thats mega impressive welldone indeed dude.
 :o
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Another raytracer.
« Reply #38 on: August 14, 2007 »
looks great and seems to run quicker on dual core too :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Another raytracer.
« Reply #39 on: August 14, 2007 »
Cool and thanks.

Another thing I'm wondering about this is sharing the data between the threads, there's stuff about locking memory (with mutexlock). I can see the point in doing that when a thread is writing to memory but in this case I don't write to any of the shared memory so to me that's not really an issue, is that right or is there anything else that should be taken care of?