Some Multithread rendering stuff :)

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Some Multithread rendering stuff :)

Post by mrminecrafttnt »

This simple Demo is not optimized but this should show that is possible :)

Code: Select all

const rs = 32 ' raster size
const res_x = 18
const res_y = 14
dim shared as uinteger exitmode
type pixeldata
    dim as integer x
    dim as integer y
    dim as integer c
end type


sub gpu_core_pixel (pixel as pixeldata)
    dim as integer oc = pixel.c
        do
           
            with pixel
            if oc <> .c then
                line(.x * rs, .y *rs) - ((.x*rs)+rs,(.y*rs)+rs),.c,bf
                oc = .c
            else
                sleep 1
            end if
           end with
        loop until exitmode = 1


end sub


dim shared as pixeldata gpu_pixel(res_x*res_y)

'inialize gpu
dim as integer counter
for x as integer = 1 to res_x
    for y as integer = 1 to res_y-1
        with gpu_pixel(counter)
            .x = x
            .y = y
        end with       
        counter + = 1
    next
next

dim as any ptr gpu_pixel_runadr (ubound(gpu_pixel))
sub test
    print "TEST"
end sub

screenres 640,480
print "INIT THREADS"
for i as integer = 0 to ubound(gpu_pixel)
    gpu_pixel_runadr(i) = threadcall gpu_core_pixel (gpu_pixel(i))
    sleep 2
next

cls
locate 1,5
print "RUN"
do
for i as integer = 0 to ubound(gpu_pixel)
    gpu_pixel(i).c + = 1
    
next
    sleep 5
loop until inkey <> ""
exitmode = 1
end
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Some Multithread rendering stuff :)

Post by fxm »

Without detailing the little errors in your code, have you tried running this program by removing the temporization in the thread creation loop?

Code: Select all

for i as integer = 0 to ubound(gpu_pixel)
    gpu_pixel_runadr(i) = threadcall gpu_core_pixel (gpu_pixel(i))
    'sleep 2
next
This highlights the bug on the ThreadCall keyword:
documentation wrote:.....
WARNING: Presently when Threadcall involves to pass parameters to the thread, there is no guarantee that the corresponding data are still maintained after the end of the Threadcall statement and this until the thread is launched. That can cause bad behavior.
.....
It is always safer to use TreadCreate, although less easy to code (I took the opportunity to correct small errors):

Code: Select all

const rs = 32 ' raster size
const res_x = 18
const res_y = 13
dim shared as uinteger exitmode

type pixeldata
    dim as integer x
    dim as integer y
    dim as integer c
end type

sub gpu_core_pixel (byval p as any ptr)
    with *cptr(pixeldata ptr, p)
        dim as integer oc = .c
        do
            if oc <> .c then
                line(.x * rs, .y *rs) - step(rs, rs), .c, bf
                oc = .c
            else
                sleep 1
            end if
        loop until exitmode = 1
    end with
end sub

dim shared as pixeldata gpu_pixel(1 to res_x * res_y)

'inialize gpu
dim as integer counter = 1
for x as integer = 1 to res_x
    for y as integer = 1 to res_y
        with gpu_pixel(counter)
            .x = x
            .y = y
        end with       
        counter += 1
    next
next

dim as any ptr gpu_pixel_runadr(1 to ubound(gpu_pixel))

screenres (res_x + 2) * rs ,(res_y + 2) * rs
print "INIT THREADS"
for i as integer = 1 to ubound(gpu_pixel)
    gpu_pixel_runadr(i) = threadcreate(@gpu_core_pixel, @gpu_pixel(i))
next

cls
locate 1,5
print "RUN"
do
    for i as integer = 1 to ubound(gpu_pixel)
        gpu_pixel(i).c += 1
    next
    sleep 5
loop until inkey <> ""

exitmode = 1

for i as integer = 1 to ubound(gpu_pixel)
    threadwait(gpu_pixel_runadr(i))
next
Post Reply