【问题标题】:sdl2 drawing from separate threads on Linux vs Windowssdl2 从 Linux 与 Windows 上的单独线程中绘图
【发布时间】:2016-09-22 14:21:48
【问题描述】:

我在 SDL2 上进行了一些非常简单的绘图,在 Windows 下可以完美运行,但现在我已经将它移植到 Linux,并没有从我产生的不同线程中绘图。

我正在使用互斥锁控制对渲染器的访问。

函数按预期处理和输出日志记录,唯一似乎不起作用的是渲染器从不更新 Linux 下的显示。

如果我注释掉线程,并从 main 运行函数,我会得到预期的结果。 Linux 和 Windows 版本之间的代码没有变化。

代码在 -wall 和 -pedantic 下编译时没有警告。自从我开始使用 Linux 编译以来,我添加了 -pthread 标志以防万一(没有任何区别)。

如果有人知道任何问题或可能知道为什么这不起作用,那么您将帮我一个巨大的忙。

        static int thread_processing(void* data) 
    {
        srand(time(NULL));      //randomize 
        
        ThreadData *td = data;  
        Bucket *bucket; 
        int b_Id = -1;
        
        Color threadColor;
        //unique color for each respective thread
        color_randomize(&threadColor);
        threadColor.a = 255;
        
        fprintf(stderr, "%d Buckets to process..\n", td->bArray->size);
        while (1) {
            //check there are buckets left to process
            if (td->bArray->rendered >= td->bArray->size)
                break;
                
            //acquie/lock access to bucket array
            SDL_LockMutex(td->b_mutex);
            
            //retrieve buucket id to process
            b_Id = td->bArray->rendered;
            td->bArray->rendered++;
            
            fprintf(stderr, "Rendering bucket: %d\n", b_Id);
            //release access to bucket array
            SDL_UnlockMutex(td->b_mutex);
            
            //retrieve addr of bucket to process
            bucket = &(td->bArray->buckets[b_Id]);
            
            //lock access to renderer
            SDL_LockMutex(td->r_mutex);
            
            //draw rect on screen where bucket will be processed
            draw_bucketOutline(td->renderer, bucket, &threadColor);
            SDL_RenderPresent(td->renderer);
            
            //release access to renderer object
            SDL_UnlockMutex(td->r_mutex);
            
            //process the bucket
            process_bucket(td->scene, bucket);
            
            //acquire/lock acess to renderer object
            SDL_LockMutex(td->r_mutex);
            
            //draw the processed data ot the screen
            draw_bucket(td->renderer, bucket);
            SDL_RenderPresent(td->renderer);
            
            //release access to renderer object
            SDL_UnlockMutex(td->r_mutex);
        }
        
        return 0;
    }

    void draw_bucketOutline(SDL_Renderer *renderer, Bucket *b, Color *color)
{
    //set the colour of the outline
    SDL_SetRenderDrawColor(renderer, 125, 125, 125, 255);
    
    //set the 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 the outline
    SDL_RenderDrawRect(renderer, &rect);
    
    //crop rectangle inwards for filling inside of outline
    rect.w -= 2;
    rect.h -= 2;
    rect.x += 1;
    rect.y += 1;

    //set colour for fill area
    SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a);
    
    //draw fill area
    SDL_RenderFillRect(renderer, &rect);
}

主要.....

//iterate over threads, do the processing
int t;
for (t = 0; t < THREAD_COUNT; t++) {
    threads[t] = SDL_CreateThread(thread_processing, NULL, &td);
}

//iterate over threads, clean them up
for (t = 0; t < THREAD_COUNT; t++) {
    int status;
    SDL_WaitThread(threads[t], &status);
}

编译

gcc -Wall -pedantic -lm -I/usr/include/SDL2 -D_REENTRANT -lX11 -pthread raytracer.c -lSDL2 -o raytracer

【问题讨论】:

    标签: c sdl-2


    【解决方案1】:

    Linux 上的 SDL2 使用 OpenGL 加速渲染(在 Windows 上默认为 d3d 加速),并且 OpenGL 具有线程本地上下文。您不能在不同的线程中使用相同的 GL 上下文。您必须在单线程中渲染,例如通过将渲染命令放入队列中,然后在渲染线程中重播它们。首先是你shouldn't have used multiple rendering threads

    【讨论】:

    • 我没有使用 open gl。它的纯像素画。
    • SDL_Render 会,除非您明确使用软件渲染器。
    • 我正在使用 SDL_CreateSoftwareRenderer
    • 好的,那么 GL 位不正确。仍然绝对没有迹象表明您可以在多个线程中使用任何渲染功能(FAQ 说您不能)。例如。我可以在“错误”线程中使用SDL_WaitEvent 在 Windows 上轻松将其死锁,这将是我的错。
    • 附带说明,您可能可以在多个线程中绘制(如果互斥锁锁定)但只在图形线程中显示结果。
    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多