I want to make my code parallel, but have some questions for the experienced.
1] Is it a good idea to do something like this?
#pragma omp parallel num_threads(2)
{
const int threadID=omp_get_thread_num();
if(threadID==0) while(WM_QUIT != msg.message){ // < Engine stuff
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}else{
engine.omp_renderPrepare();
}
}
if(threadID==1) while(WM_QUIT != msg.message){ // < Render stuff
engine.omp_renderApply();
}
}
// Functions from the engine class:
inline void omp_renderPrepare(){
if(gDevice.omp_isReady()){
#pragma omp critical(renderPrepare)
if(!omp_rendering){
omp_sceneReady=0;
renderPrepare();
omp_sceneReady=1;
}
}else{
Sleep(42); // If the device is not ready, dont spam.
}
}
inline void omp_renderApply(){
if(gDevice.omp_isReady()){
#pragma omp critical(omp_renderApply)
if(omp_sceneReady){
omp_rendering=1;
renderApply();
omp_rendering=0;
}
}else{
Sleep(42); // If the device is not ready, dont spam.
}
}
I already applied it, but it is heavy on the CPU & my code slows to a crawl when I resize the window, since it must:
- Set a bool “omp_deviceReady” to false so the rendering will stop.
- Wait for the parallel sections to stop.
- Resizing the graphics buffers.
- Set “omp_deviceReady” to true so rendering can happen again.
- Repeat as long as the user resizes the window.
2] Since my OMP code is heavy on the CPU [about 50% accord to task mgr], what is a good way to handle parallel rendering?
“OMP isn’t too well suited for MT rendering (OMP would work better for parallel processing of game tasks)” – Necrolis
“Deffered contexts is a good way to go, …” – catflier