我正在嘗試在單獨的線程中渲染來自處理草圖的幀。Processing 有一個loadPixels()函數。問題在于 OpenGL 渲染器(P2D或P3D在處理中。):PGraphicsOpenGL loadPixels()這是我的示例草圖:void setup(){ size(300,300,P2D); noFill(); background(255); rectMode(CENTER); stroke(0,32); smooth(8); // instantiate and start rendering thread new PNGRenderer(this);}void draw(){ // draw moving shapes translate(width * 0.5, height * 0.5); rotate(sin(frameCount * 0.075)); scale(sin(frameCount * 0.01)); translate(cos(frameCount * 0.01) * width * 0.1,sin(frameCount * 0.01) * height * 0.1); rect(0,0,width * .9,height * .9);}public class PNGRenderer implements Runnable{ PApplet parent; PImage frame; boolean shouldSave = false; int savedFrameCount; boolean isRunning = true; PNGRenderer(PApplet parent){ this.parent = parent; this.parent.registerMethod("draw",this); frame = createImage(parent.width,parent.height,ARGB); frame.loadPixels(); Thread renderThread = new Thread(this); renderThread.setName("Renderer-Thread"); renderThread.start(); } public void draw(){ // all is well if I sample pixels in the same OpenGL thread //parent.loadPixels(); shouldSave = true; } synchronized void sampleAndSavePixels(){ if(shouldSave){ // program crashes if I try to save in a separate thread parent.loadPixels(); arrayCopy(parent.pixels,frame.pixels); frame.updatePixels(); frame.save(dataPath("frames/frame_"+nf(savedFrameCount++,4)+".png")); println("saved frame",savedFrameCount); shouldSave = false; } } public void run(){ while(isRunning){ sampleAndSavePixels(); } }}據我所知(到目前為止我還不是 OpenGL 向導)讀取像素需要在同一個 OpenGL 線程中完成。這是我試圖避免的放緩。
1 回答

慕碼人2483693
TA貢獻1860條經驗 獲得超9個贊
OpenGL 上下文是線程本地的。上下文必須在線程中成為“當前”。不同的線程可以使用相同的上下文,但是一個上下文不能同時在多個線程中是當前的。
您必須確保線程不會同時使用相同的上下文。當然,您可以為不同的線程創建不同的上下文,并將一個上下文共享給另一個。
請參見OPENGL 多上下文
但請注意,GPU 已經并行運行。在不同線程中使用 1 個 GPU 沒有任何好處。GPU一次只能處理1個線程的指令。當然,渲染管道的每個階段都會并行處理頂點、片段等
添加回答
舉報
0/150
提交
取消