c - sdl2 drawing from separate threads on Linux vs Windows -
i have simple drawing going on sdl2 working under windows i've ported on linux, isn't drawing different threads i've spawned.
i'm controlling access renderer using mutex's.
the functions process , output logging expected, thing doesn't appear working renderer never updates display under linux.
if comment out the threading, , run function main expected result. code hasn't changed between linux , windows versions.
the code compiles without warnings under -wall , -pedantic. since i've started compiling linux i've added -pthread flag in case required (hasn't made difference).
if aware of gotchas or might have inkling why isn't working doing me enormous favour.
static int thread_processing(void* data) { srand(time(null)); //randomize threaddata *td = data; bucket *bucket; int b_id = -1; color threadcolor; //unique color each respective thread color_randomize(&threadcolor); threadcolor.a = 255; fprintf(stderr, "%d buckets process..\n", td->barray->size); while (1) { //check there buckets left process if (td->barray->rendered >= td->barray->size) break; //acquie/lock access bucket array sdl_lockmutex(td->b_mutex); //retrieve buucket id process b_id = td->barray->rendered; td->barray->rendered++; fprintf(stderr, "rendering bucket: %d\n", b_id); //release access bucket array sdl_unlockmutex(td->b_mutex); //retrieve addr of bucket process bucket = &(td->barray->buckets[b_id]); //lock access renderer sdl_lockmutex(td->r_mutex); //draw rect on screen bucket processed draw_bucketoutline(td->renderer, bucket, &threadcolor); sdl_renderpresent(td->renderer); //release access renderer object sdl_unlockmutex(td->r_mutex); //process bucket process_bucket(td->scene, bucket); //acquire/lock acess renderer object sdl_lockmutex(td->r_mutex); //draw processed data ot screen draw_bucket(td->renderer, bucket); sdl_renderpresent(td->renderer); //release access renderer object sdl_unlockmutex(td->r_mutex); } return 0; } void draw_bucketoutline(sdl_renderer *renderer, bucket *b, color *color) { //set colour of outline sdl_setrenderdrawcolor(renderer, 125, 125, 125, 255); //set outline position sdl_rect rect; rect.w = b->resolution.width; rect.h = b->resolution.height; rect.x = b->start.x; rect.y = b->start.y; //draw outline sdl_renderdrawrect(renderer, &rect); //crop rectangle inwards filling inside of outline rect.w -= 2; rect.h -= 2; rect.x += 1; rect.y += 1; //set colour fill area sdl_setrenderdrawcolor(renderer, color->r, color->g, color->b, color->a); //draw fill area sdl_renderfillrect(renderer, &rect); }
main.....
//iterate on threads, processing int t; (t = 0; t < thread_count; t++) { threads[t] = sdl_createthread(thread_processing, null, &td); } //iterate on threads, clean them (t = 0; t < thread_count; t++) { int status; sdl_waitthread(threads[t], &status); }
compiling
gcc -wall -pedantic -lm -i/usr/include/sdl2 -d_reentrant -lx11 -pthread raytracer.c -lsdl2 -o raytracer
sdl2 on linux uses opengl accelerated rendering (it defaults d3d acceleration on windows), , opengl have thread-local context. cannot use same gl context in separate threads. have render in single thread, e.g. putting rendering commands in queue , replaying them in rendering thread. , shouldn't have used multiple rendering threads in first place.
Comments
Post a Comment