gpt4 book ai didi

c++ - 缓冲通信速度噩梦

转载 作者:太空狗 更新时间:2023-10-29 21:29:01 25 4
gpt4 key购买 nike

我正在尝试使用缓冲区在我的程序中的多个“层”(线程)之间进行通信,现在我可以看到内部发生的事情,我意识到在这个过程中消耗了大量的时间使用这些缓冲区。

这里有一些关于我的代码中发生的事情的注释。

  • 当在该线程中触发渲染模式时,它开始向其下方的层(线程)发送尽可能多的点
  • 然后处理来自下线程的点,并通过下线程的输出缓冲区返回给本线程
  • 接收回的点(目前)映射为 D3D 表面中的白色像素

  • 如果我绕过缓冲区并将点直接放入表面像素中,则只需大约 3 秒即可完成整个工作

  • 如果我把要点传下来然后让下层把它传回去,跳过任何实际的数字运算,整个工作大约需要 30 分钟(这使得整个程序毫无用处)
  • 更改缓冲区大小对速度没有明显影响

  • 我最初在我的缓冲区中使用 MUTEX,但为了解决问题而删除了它们

有什么我可以做的不同的事情来解决我遇到的这个速度问题吗?...与我处理这些消息的方式有关???

这是我的代码很抱歉弄得这么乱。我不得不在这个项目上走得太快,我在评论中留下了很多我一直在试验的部分。

DWORD WINAPI CONTROLSUBSYSTEM::InternalExProcedure(__in LPVOID lpSelf)
{
XMSG xmsg;
LPCONTROLSUBSYSTEM lpThis = ((LPCONTROLSUBSYSTEM)lpSelf);
BOOL bStall;

BOOL bRendering = FALSE;
UINT64 iOutstandingPoints = 0; // points that are out being tested
UINT64 iPointsDone = 0;
UINT64 iPointsTotal = 0;

BOOL bAssigning;
DOUBLE dNextX;
DOUBLE dNextY;

while(1)
{
if( lpThis->hwTargetWindow!=NULL && lpThis->d3ddev!=NULL )
{
lpThis->d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
if(lpThis->d3ddev->BeginScene())
{
lpThis->d3ddev->StretchRect(lpThis->sfRenderingCanvas,NULL,lpThis->sfBackBuffer,NULL,D3DTEXF_NONE);
lpThis->d3ddev->EndScene();
}
lpThis->d3ddev->Present(NULL,NULL,NULL,NULL);
}

//bStall = TRUE;
// read input buffer
if(lpThis->bfInBuffer.PeekMessage(&xmsg))
{
bStall = FALSE;

if( HIBYTE(xmsg.wType)==HIBYTE(CONT_MSG) )
{
// take message off
lpThis->bfInBuffer.GetMessage(&xmsg);
// double check consistency
if( HIBYTE(xmsg.wType)==HIBYTE(CONT_MSG) )
{
switch(LOBYTE(xmsg.wType))
{
case SETRESOLUTION_MSG:
lpThis->iAreaWidth = (UINT)xmsg.dptPoint.X;
lpThis->iAreaHeight = (UINT)xmsg.dptPoint.Y;
lpThis->sfRenderingCanvas->Release();
if(lpThis->d3ddev->CreateOffscreenPlainSurface(
(UINT)xmsg.dptPoint.X,(UINT)xmsg.dptPoint.Y,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&(lpThis->sfRenderingCanvas),
NULL)!=D3D_OK)
{
MessageBox(NULL,"Error resizing surface.","ERROR",MB_ICONERROR);
}
else
{
D3DLOCKED_RECT lrt;
if(D3D_OK == lpThis->sfRenderingCanvas->LockRect(&lrt,NULL,0))
{
lpThis->iPitch = lrt.Pitch;
VOID *data;
data = lrt.pBits;
ZeroMemory(data,lpThis->iPitch*lpThis->iAreaHeight);
lpThis->sfRenderingCanvas->UnlockRect();

MessageBox(NULL,"Surface Resized","yay",0);
}
else
{
MessageBox(NULL,"Error resizing surface.","ERROR",MB_ICONERROR);
}
}
break;
case SETCOLORMETHOD_MSG:
break;
case SAVESNAPSHOT_MSG:
lpThis->SaveSnapshot();
break;
case FORCERENDER_MSG:
bRendering = TRUE;
iPointsTotal = lpThis->iAreaHeight*lpThis->iPitch;
iPointsDone = 0;

MessageBox(NULL,"yay, render something!",":o",0);
break;
default:
break;
}
}// else, lost this message
}
else
{
if( HIBYTE(xmsg.wType)==HIBYTE(MATH_MSG) )
{
XMSG xmsg2;
switch(LOBYTE(xmsg.wType))
{
case RESETFRAME_MSG:
case ZOOMIN_MSG:
case ZOOMOUT_MSG:
case PANUP_MSG:
case PANDOWN_MSG:
case PANLEFT_MSG:
case PANRIGHT_MSG:
// tell self to start a render
xmsg2.wType = CONT_MSG|FORCERENDER_MSG;
if(lpThis->bfInBuffer.PutMessage(&xmsg2))
{
// pass it down
while(!lpThis->lplrSubordinate->PutMessage(&xmsg));
// message passed so pull it from buffer
lpThis->bfInBuffer.GetMessage(&xmsg);
}
break;
default:
// pass it down
if(lpThis->lplrSubordinate->PutMessage(&xmsg))
{
// message passed so pull it from buffer
lpThis->bfInBuffer.GetMessage(&xmsg);
}
break;
}
}
else if( lpThis->lplrSubordinate!=NULL )
// pass message down
{
if(lpThis->lplrSubordinate->PutMessage(&xmsg))
{
// message passed so pull it from buffer
lpThis->bfInBuffer.GetMessage(&xmsg);
}
}
}
}
// read output buffer from subordinate
if( lpThis->lplrSubordinate!=NULL && lpThis->lplrSubordinate->PeekMessage(&xmsg) )
{
bStall = FALSE;
if( xmsg.wType==(REPLY_MSG|TESTPOINT_MSG) )
{
// got point test back
D3DLOCKED_RECT lrt;
if(D3D_OK == lpThis->sfRenderingCanvas->LockRect(&lrt,NULL,0))
{
INT pitch = lrt.Pitch;
VOID *data;
data = lrt.pBits;
INT Y=dRound((xmsg.dptPoint.Y/(DOUBLE)100)*((DOUBLE)lpThis->iAreaHeight));
INT X=dRound((xmsg.dptPoint.X/(DOUBLE)100)*((DOUBLE)pitch));

// decide color
if( xmsg.iNum==0 )
((WORD *)data)[X+Y*pitch] = 0xFFFFFFFF;
else
((WORD *)data)[X+Y*pitch] = 0xFFFFFFFF;

// message handled so remove from buffer
lpThis->lplrSubordinate->GetMessage(&xmsg);

lpThis->sfRenderingCanvas->UnlockRect();
}
}
else if(lpThis->bfOutBuffer.PutMessage(&xmsg))
{
// message sent so pull the real one off the buffer
lpThis->lplrSubordinate->GetMessage(&xmsg);
}
}
if( bRendering && lpThis->lplrSubordinate!=NULL )
{
bAssigning = TRUE;
while(bAssigning)
{
dNextX = 100*((DOUBLE)(iPointsDone%lpThis->iPitch))/((DOUBLE)lpThis->iPitch);
dNextY = 100*(DOUBLE)((INT)(iPointsDone/lpThis->iPitch))/(DOUBLE)(lpThis->iAreaHeight);
xmsg.dptPoint.X = dNextX;
xmsg.dptPoint.Y = dNextY;
//
//xmsg.iNum = 0;
//xmsg.wType = REPLY_MSG|TESTPOINT_MSG;
//
xmsg.wType = MATH_MSG|TESTPOINT_MSG;

/*D3DLOCKED_RECT lrt;
if(D3D_OK == lpThis->sfRenderingCanvas->LockRect(&lrt,NULL,0))
{
INT pitch = lrt.Pitch;
VOID *data;
data = lrt.pBits;
INT Y=dRound((dNextY/(DOUBLE)100)*((DOUBLE)lpThis->iAreaHeight));
INT X=dRound((dNextX/(DOUBLE)100)*((DOUBLE)pitch));
((WORD *)data)[X+Y*pitch] = 0xFFFFFFFF;
lpThis->sfRenderingCanvas->UnlockRect();
}
iPointsDone++;
if( iPointsDone>=iPointsTotal )
{
MessageBox(NULL,"done rendering","",0);
bRendering = FALSE;
bAssigning = FALSE;
}
*/
if( lpThis->lplrSubordinate->PutMessage(&xmsg) )
{
bStall = FALSE;
iPointsDone++;
if( iPointsDone>=iPointsTotal )
{
MessageBox(NULL,"done rendering","",0);
bRendering = FALSE;
bAssigning = FALSE;
}
}
else
{
bAssigning = FALSE;
}
}
}

//if( bStall )
//Sleep(10);
}

return 0;
}
}

(还在习惯这个论坛的代码块的东西)

编辑:

这是一个我认为在概念上相似的例子,尽管这个例子使用了它在同一个线程中产生的消息。

#include <Windows.h>
#include "BUFFER.h"

int main()
{
BUFFER myBuffer;

INT jobsTotal = 1024*768;
INT currentJob = 0;
INT jobsOut = 0;
XMSG xmsg;

while(1)
{
if(myBuffer.PeekMessage(&xmsg))
{
// do something with message
// ...

// if successful, remove message
myBuffer.GetMessage(&xmsg);
jobsOut--;
}

while( currentJob<jobsTotal )
{
if( myBuffer.PutMessage(&xmsg) )
{
currentJob++;
jobsOut++;
}
else
{
// buffer is full at the moment
// stop for now and put more on later
break;
}
}

if( currentJob==jobsTotal && jobsOut==0 )
{
MessageBox(NULL,"done","",0);
break;
}
}

return 0;
}

此示例的运行时间也约为 3 秒,而不是 30 分钟。

顺便说一句,如果有人知道为什么 visual studio 一直试图让我说 PeekMessageA 和 GetMessageA 而不是我定义的实际名称,那也很高兴知道。

最佳答案

锁定和解锁整个矩形以更改单个点可能不是很有效,您最好生成一个您打算修改的点列表,然后锁定一次矩形,遍历该列表并修改所有点, 然后解锁矩形。

当您锁定 rect 时,您实际上是在停止对它的并发访问,因此在这方面它就像 GPU 的互斥体 - 然后您只修改了一个像素。对每个像素重复执行此操作将不断地停止 GPU。您可以使用 D3DLOCK_NOSYSLOCK 在某种程度上避免这种情况,但我不确定它是否会在您的程序的更大上下文中很好地发挥作用。

我显然不完全确定你的算法的目标是什么,但如果你试图在 d3d 表面上并行处理像素,那么我认为最好的方法是通过 GPU 上的着色器。

基本上在系统内存中生成一个数组,在每个点/像素的基础上用“输入”值填充它,然后从该数组在 GPU 上生成纹理。接下来将纹理绘制到全屏四边形,然后使用像素着色器将其渲染到某个渲染目标。可以对着色器进行编码以按照您喜欢的任何方式处理每个点,GPU 将负责优化并行化。然后从该渲染目标生成一个新纹理,然后将该纹理复制到系统内存阵列中。然后您可以从该数组中提取所有输出。如果需要,您还可以将多个着色器应用到渲染目标结果返回到渲染目标以流水线化多个转换。

关于c++ - 缓冲通信速度噩梦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5738519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com