这里使用ComputeShader做拾取,主要为了熟悉下ComputeShader,为自己后面一个项目做准备。拾取基本原理前面博客也讲了,都差不多,将射线从屏幕坐标系转换到物体局部坐标系,然后再判断三角形是否被射线射中。主要是代码的实施上不一样,这里我尝试并行地通过ComputeShader,由线程i来负责判断三角形i是否被选中。
HLSL的代码:
//=========================================
//compute shader base technology
//======================================
#define BLOCK_SIZE 32
struct VertexStruc
{
float3 posW;
float3 normalW;
float2 texcoord;
};
struct PickedStruc
{
int picked;//记录三角形i是否被选中,选中为1,否则为0
};
StructuredBuffer<int> indiceBuf;
StructuredBuffer<VertexStruc> inTriPointBuf;
RWStructuredBuffer<PickedStruc> pickedTriBoolArray;
//每个线程负责一个三角形
[numthreads(BLOCK_SIZE, 1, 1)]
void CS_PickTriangle( uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI : SV_GroupIndex )
{
if(DTid.x>=numFaces)return ;
////根据之前博客,把射线从屏幕坐标转换到物体的局部坐标
float3 pickOrigin=float3(0.0f,0.0f,0.0f);
float3 pickDirection;
pickDirection.x=(2.0f*winPos.x/backBufferDesc.x-1.0f)/projMtx[0][0];
pickDirection.y=(-2.0f*winPos.y/backBufferDesc.y+1.0f)/projMtx[1][1];
pickDirection.z=1.0f;
pickOrigin=mul(float4(pickOrigin,1.0f),invViewMtx).xyz;
pickDirection=mul(float4(pickDirection,0.0f),invViewMtx).xyz;
pickOrigin=mul(float4(pickOrigin,1.0f),invWorldMtx).xyz;
pickDirection=mul(float4(pickDirection,0.0f),invWorldMtx).xyz;
float u=0;
float v=0;
float t=0;
float3 pos[3];
//读取第DTid.x个三角形的三个顶点位置。
for(int i=0;i<3;i++)
{
pos[i]=inTriPointBuf[ indiceBuf[DTid.x*3+i]].posW;
}
//由第DTid.x个线程判定第DTid.x三角形是否被射线选中
if(IntersectTriangle(pickOrigin,pickDirection,pos[0],pos[1],pos[2],t,u,v))
{
pickedTriBoolArray[DTid.x].picked=1;
}
else
{
pickedTriBoolArray[DTid.x].picked=0;
}
return ;
}
比较麻烦的是Directx中的代码。因为ComputeShader只理解StructuredBuffer,不理解VertexBuffer和IndexBuffer,所以要把VertexBuffer和IndexBuffer中的数据拷贝到相应的StructuredBuffer中。
HRESULT Mesh::DrawPickMesh_CSAPBUF(CModelViewerCamera *pCamera,POINT cursor,POINT backDesc)
{
HRESULT hr=S_OK;
//调用拾取算法
GetPickedTriangleArray_CS(pCamera,cursor,backDesc);
//绘制选中的三角形
DrawPickedTriangleArray_CS(pCamera);
//以网格形式绘制其他三角形,便于显示
DrawFrameMesh(pCamera);
return hr;
}
HRESULT Mesh::GetPickedTriangleArray_CS(CModelViewerCamera *pCamera,POINT cursor,POINT backDesc)
{
HRESULT hr=S_OK;
D3D11_BUFFER_DESC bufDesc1;
D3D11_BUFFER_DESC bufDesc2;
ZeroMemory(&bufDesc1,sizeof(D3D11_BUFFER_DESC));
ZeroMemory(&bufDesc2,sizeof(D3D11_BUFFER_DESC));
//把VertexBuffer中的数据拷贝到相应的StructuredBuffer中并创建相应ResourceView。
m_pVsStrucBuffer=NULL;
ID3D11Buffer *pSrcBuffer=NULL;
pSrcBuffer=m_pMesh11->GetVB11(0,0);
pSrcBuffer->GetDesc(&bufDesc2);
bufDesc1.ByteWidth=bufDesc2.ByteWidth;
bufDesc1.BindFlags=D3D11_BIND_SHADER_RESOURCE;
bufDesc1.CPUAccessFlags=0;
bufDesc1.MiscFlags=D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
bufDesc1.StructureByteStride=sizeof(MeshVertex);
bufDesc1.Usage=D3D11_USAGE_DEFAULT;
V_RETURN(m_pDevice->CreateBuffer(&bufDesc1,NULL,&m_pVsStrucBuffer));
m_pContext->CopyResource(m_pVsStrucBuffer,pSrcBuffer);
#if defined (DEBUG)||defined(_DEBUG)
m_pVsStrucBuffer->SetPrivateData(WKPDID_D3DDebugObjectName ,sizeof("m_pVsStrucBuffer")-1,"m_pVsStrucBuffer");
#endif
// 创建相应ResourceView。ID3D11ShaderResourceView *pVSStrucBufSRV;
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc,sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
srvDesc.Buffer.FirstElement=0;
srvDesc.Buffer.NumElements=m_pMesh11->GetNumVertices(0,0);
srvDesc.Format=DXGI_FORMAT_UNKNOWN;
srvDesc.ViewDimension=D3D11_SRV_DIMENSION_BUFFER;
V_RETURN(m_pDevice->CreateShaderResourceView(m_pVsStrucBuffer,&srvDesc,&pVSStrucBufSRV));
#if defined (DEBUG)||defined(_DEBUG)
pVSStrucBufSRV->SetPrivateData(WKPDID_D3DDebugObjectName ,sizeof("pVSStrucBufSRV")-1,"pVSStrucBufSRV");
#endif
//把IndexBuffer中的数据拷贝到相应的StructuredBuffer中并创建相应ResourceView。
m_pIndexStrucBuffer=NULL;
pSrcBuffer=m_pMesh11->GetIB11(0);
pSrcBuffer->GetDesc(&bufDesc2);
bufDesc1.ByteWidth=bufDesc2.ByteWidth;
bufDesc1.BindFlags=D3D11_BIND_SHADER_RESOURCE;
bufDesc1.CPUAccessFlags=0;
bufDesc1.MiscFlags=D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
if(m_pMesh11->GetIBFormat11(0)==DXGI_FORMAT_R16_UINT)
bufDesc1.StructureByteStride=2;
else
bufDesc1.StructureByteStride=4;
bufDesc1.Usage=D3D11_USAGE_DEFAULT;
V_RETURN(m_pDevice->CreateBuffer(&bufDesc1,NULL,&m_pIndexStrucBuffer));
m_pContext->CopyResource(m_pIndexStrucBuffer,pSrcBuffer);
#if defined (DEBUG)||defined(_DEBUG)
m_pIndexStrucBuffer->SetPrivateData(WKPDID_D3DDebugObjectName ,sizeof("m_pIndexStrucBuffer")-1,"m_pIndexStrucBuffer");
#endif
//创建相应ResourceView。
ID3D11ShaderResourceView *pIndexStrucBufSRV;
ZeroMemory(&srvDesc,sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
srvDesc.Buffer.FirstElement=0;
srvDesc.Buffer.NumElements=m_pMesh11->GetNumIndices(0);
srvDesc.Format=DXGI_FORMAT_UNKNOWN;
srvDesc.ViewDimension=D3D11_SRV_DIMENSION_BUFFER;
V_RETURN(m_pDevice->CreateShaderResourceView(m_pIndexStrucBuffer,&srvDesc,&pIndexStrucBufSRV));
#if defined (DEBUG)||defined(_DEBUG)
pIndexStrucBufSRV->SetPrivateData(WKPDID_D3DDebugObjectName ,sizeof("pIndexStrucBufSRV")-1,"pIndexStrucBufSRV");
#endif
//创建一个Buffer来存储三角形i是否被选中。
m_pPickedTriBoolArrBuf=NULL;
ZeroMemory(&bufDesc1,sizeof(D3D11_BUFFER_DESC));
bufDesc1.ByteWidth=m_pMesh11->GetNumIndices(0)/3*sizeof(PickedStruc);
bufDesc1.BindFlags=D3D11_BIND_UNORDERED_ACCESS;
bufDesc1.CPUAccessFlags=0;
bufDesc1.MiscFlags=D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
bufDesc1.StructureByteStride=sizeof(PickedStruc);
bufDesc1.Usage=D3D11_USAGE_DEFAULT;
V_RETURN(m_pDevice->CreateBuffer(&bufDesc1,NULL,&m_pPickedTriBoolArrBuf));
#if defined (DEBUG)||defined(_DEBUG)
m_pPickedTriBoolArrBuf->SetPrivateData(WKPDID_D3DDebugObjectName ,sizeof("m_pPickedTriBoolArrBuf")-1,"m_pPickedTriBoolArrBuf");
#endif
//创建相应ResourceView。
m_pPickedTriBoolArrUAV=NULL;
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc;
ZeroMemory(&uavDesc,sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC));
uavDesc.Buffer.FirstElement=0;
uavDesc.Buffer.Flags=0;
uavDesc.Buffer.NumElements=m_pMesh11->GetNumIndices(0)/3;
uavDesc.Format=DXGI_FORMAT_UNKNOWN;
uavDesc.ViewDimension=D3D11_UAV_DIMENSION_BUFFER;
V_RETURN(m_pDevice->CreateUnorderedAccessView(m_pPickedTriBoolArrBuf,&uavDesc,&m_pPickedTriBoolArrUAV));
#if defined (DEBUG)||defined(_DEBUG)
m_pPickedTriBoolArrUAV->SetPrivateData(WKPDID_D3DDebugObjectName ,sizeof("m_pPickedTriBoolArrUAV")-1,"m_pPickedTriBoolArrUAV");
#endif
//calculate the matrix
D3DXMATRIX invWorld;
D3DXMatrixInverse(&invWorld,0,&m_World);
D3DXMATRIX viewMtx=*pCamera->GetViewMatrix();
D3DXMATRIX invViewMtx;
D3DXMatrixInverse(&invViewMtx,0,&viewMtx);
const D3DXMATRIX projMtx=*pCamera->GetProjMatrix();
//calculate the cursor and backbuffer
MFloat2 mcur;
MFloat2 mback;
mcur.x=(float)cursor.x;
mcur.y=(float)cursor.y;
mback.x=(float)backDesc.x;
mback.y=(float)backDesc.y;
//set the effect variable
m_pfxInterViewMtx->SetMatrix((float*)&viewMtx);
m_pfxInterInvViewMtx->SetMatrix((float*)&invViewMtx);
m_pfxInterProjMtx->SetMatrix((float*)&projMtx);
m_pfxInterWorldMtx->SetMatrix((float*)(&m_World));
m_pfxInterInvWorldMtx->SetMatrix((float*)&invWorld);
m_pfxInterWinPos->SetRawValue((void*)&mcur,0,sizeof(MFloat2));
m_pfxInterBackbufferDesc->SetRawValue((void*)&mback,0,sizeof(MFloat2));
m_pfxIndexStrucSRV->SetResource(pIndexStrucBufSRV);
m_pfxVertexStrucSRV->SetResource(pVSStrucBufSRV);
//m_pfxVSSrucAppUAV->SetUnorderedAccessView(m_pPickedTriBoolArrUAV);
UINT numfaces=m_pMesh11->GetNumIndices(0)/3;
m_pfxNumFaces->SetInt(numfaces);
m_pfxPickedTriBoolArr->SetUnorderedAccessView(m_pPickedTriBoolArrUAV);
//m_pfxTestBufUAV->SetUnorderedAccessView(pTestBufUAV);
D3DX11_TECHNIQUE_DESC techDesc;
//1apply intersected technique
m_pfxCSInterTech->GetDesc(&techDesc);
for(int i=0;i<techDesc.Passes;i++)
{
m_pfxCSInterTech->GetPassByIndex(i)->Apply(0,m_pContext);
UINT groupSizeX=(numfaces+CS_APP_BLOCK_SIZE-1)/CS_APP_BLOCK_SIZE;
m_pContext->Dispatch(groupSizeX,1,1);
}
SAFE_RELEASE(pIndexStrucBufSRV);
SAFE_RELEASE(pVSStrucBufSRV);
return hr;
}
//绘制那些被选中的三角形
HRESULT Mesh::DrawPickedTriangleArray_CS(CModelViewerCamera *pCamera)
{
HRESULT hr=S_OK;
//m_pPickedTriBoolArrBuf,看哪个三角形被选中
ID3D11Buffer* pReadPickedBuf=NULL;
pReadPickedBuf=CopyToDebugBuffer(m_pPickedTriBoolArrBuf);
D3D11_MAPPED_SUBRESOURCE mapSubData;
ZeroMemory(&mapSubData,sizeof(D3D11_MAPPED_SUBRESOURCE));
m_pContext->Map(pReadPickedBuf,0,D3D11_MAP_READ,0,&mapSubData);
PickedStruc *pBoolArr=(PickedStruc*)mapSubData.pData;
m_pContext->Unmap(pReadPickedBuf,0);
//读出模型所有的VertexBuffer
ID3D11Buffer* pReadVertexBuffer=NULL;
pReadVertexBuffer=CopyToDebugBuffer(m_pVsStrucBuffer);
ZeroMemory(&mapSubData,sizeof(D3D11_MAPPED_SUBRESOURCE));
m_pContext->Map(pReadVertexBuffer,0,D3D11_MAP_READ,0,&mapSubData);
MeshVertex *pVertex=(MeshVertex*)mapSubData.pData;
m_pContext->Unmap(pReadVertexBuffer,0);
//读出模型所有的IndexBuffer
ID3D11Buffer* pReadIndexBuffer=NULL;
pReadIndexBuffer=CopyToDebugBuffer(m_pIndexStrucBuffer);
ZeroMemory(&mapSubData,sizeof(D3D11_MAPPED_SUBRESOURCE));
m_pContext->Map(pReadIndexBuffer,0,D3D11_MAP_READ,0,&mapSubData);
UINT *pIndex=(UINT*)mapSubData.pData;
m_pContext->Unmap(pReadIndexBuffer,0);
//看哪些三角形被选中if(picked==1)
UINT numfaces=m_pMesh11->GetNumIndices(0)/3;
MeshVertex *pPickedVertex=new MeshVertex[300];
int pickedNum=0;
for(int i=0;i<numfaces;i++)
{
if(pBoolArr[i].picked==1)
{
//这个三角形被选中了,读出它的三个点来绘制
for(int j=0;j<3;j++)
{
pPickedVertex[3*pickedNum+j].normal=pVertex[pIndex[3*i+j]].normal;
pPickedVertex[3*pickedNum+j].pos=pVertex[pIndex[3*i+j]].pos;
pPickedVertex[3*pickedNum+j].texcoord=pVertex[pIndex[3*i+j]].texcoord;
}
pickedNum++;
}
}
//根据上面读出的顶点绘制三角形
//根据上面读出的点生成一个用来绘制的Vertex Buffer
if(pickedNum>0)
{
ID3D11Buffer *pPickedVertexBuffer=NULL;
D3D11_BUFFER_DESC bufDesc;
ZeroMemory(&bufDesc,sizeof(D3D11_BUFFER_DESC));
bufDesc.ByteWidth=pickedNum*3*sizeof(MeshVertex);
bufDesc.BindFlags=D3D11_BIND_VERTEX_BUFFER;
bufDesc.CPUAccessFlags=0;
bufDesc.MiscFlags=0;
bufDesc.StructureByteStride=0;
bufDesc.Usage=D3D11_USAGE_DEFAULT;
D3D11_SUBRESOURCE_DATA vSSubData;
ZeroMemory(&vSSubData,sizeof(D3D11_SUBRESOURCE_DATA));
vSSubData.pSysMem=(void*)pPickedVertex;
m_pDevice->CreateBuffer(&bufDesc,&vSSubData,&pPickedVertexBuffer);
//绘制上面刚创建的三角形的vertex buffer
D3DX11_TECHNIQUE_DESC techDesc;
ZeroMemory(&techDesc,sizeof(D3DX11_TECHNIQUE_DESC));
m_pfxWorldMtx->SetMatrix((float*)&m_World);
m_pfxViewMtx->SetMatrix((float*)pCamera->GetViewMatrix());
m_pfxProjMtx->SetMatrix((float*)pCamera->GetProjMatrix());
m_pfxMeshTech->GetDesc(&techDesc);
m_pContext->IASetInputLayout(m_pInputLayout);
m_pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride=sizeof(MeshVertex);
UINT offset=0;
m_pContext->IASetVertexBuffers(0,1,&pPickedVertexBuffer,&stride,&offset);
for(int i=0;i<techDesc.Passes;i++)
{
m_pfxMeshTech->GetPassByIndex(0)->Apply(0,m_pContext);
m_pContext->Draw(3*pickedNum,0);
}
SAFE_RELEASE(pPickedVertexBuffer);
}
SAFE_RELEASE(pReadPickedBuf);
SAFE_RELEASE(pReadVertexBuffer);
SAFE_RELEASE(pReadIndexBuffer);
delete[] pPickedVertex;
return hr;
}
最后以网格形式绘制其余的三角形
void Mesh::DrawFrameMesh(CModelViewerCamera* pCamera)
{
m_pfxWorldMtx->SetMatrix((float*)&m_World);
m_pfxViewMtx->SetMatrix((float*)pCamera->GetViewMatrix());
m_pfxProjMtx->SetMatrix((float*)pCamera->GetProjMatrix());
D3DX11_TECHNIQUE_DESC techDesc;
ZeroMemory(&techDesc,sizeof(D3DX11_TECHNIQUE_DESC));
UINT stride=m_pMesh11->GetVertexStride(0,0);
UINT offset=0;
ID3D11Buffer *pVertexBuffer[1];
pVertexBuffer[0]=m_pMesh11->GetVB11(0,0);
m_pContext->IASetVertexBuffers(0,1,pVertexBuffer,&stride,&offset);
m_pContext->IASetIndexBuffer(m_pMesh11->GetIB11(0),m_pMesh11->GetIBFormat11(0),0);
m_pContext->IASetInputLayout(m_pInputLayout);
m_pfxWireFrameTech->GetDesc(&techDesc);
SDKMESH_SUBSET *pSubSet=NULL;
for(int i=0;i<techDesc.Passes;i++)
{
for(int subset=0;subset<m_pMesh11->GetNumSubsets(0);subset++)
{
pSubSet=m_pMesh11->GetSubset(0,subset);
m_pfxWireFrameTech->GetPassByIndex(0)->Apply(0,m_pContext);
//get primitiveType
D3D11_PRIMITIVE_TOPOLOGY primType;
primType=CDXUTSDKMesh::GetPrimitiveType11((SDKMESH_PRIMITIVE_TYPE)pSubSet->PrimitiveType );
m_pContext->IASetPrimitiveTopology(primType);
//get material
ID3D11ShaderResourceView *pSV=NULL;
pSV=m_pMesh11->GetMaterial(pSubSet->MaterialID)->pDiffuseRV11;
m_pfxDiffTex->SetResource(pSV);
//draw the wireframe mesh
m_pContext->DrawIndexed((UINT)pSubSet->IndexCount,0,(UINT)pSubSet->VertexStart);
}
}
}
最后拾取效果
因为完整的代码太琐碎又太大了,我就不一一贴上了。完整的代码可以从这里下载:http://download.csdn.net/detail/qiul12345/3952333。