Skip to content

Commit

Permalink
done using color as patch_id
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwaylive committed Jan 10, 2015
1 parent a63eb6b commit 55a9704
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
17 changes: 11 additions & 6 deletions DepthSensing/Shaders/IntegrateFromGlobalHash.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static const uint linBlockSize = SDF_BLOCK_SIZE * SDF_BLOCK_SIZE * SDF_BLOCK_SIZ
[numthreads(groupthreads, 1, 1)]
void integrateFromGlobalHashPass1CS(int3 dTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI: SV_GroupIndex, uint3 GID : SV_GroupID)
{
uint bucketID = start+groupthreads*(GID.x + GID.y * NUM_GROUPS_X)+GI;
if(bucketID < g_HashNumBuckets*g_HashBucketSize)
uint bucketID = start+groupthreads * (GID.x + GID.y * NUM_GROUPS_X)+GI;
if(bucketID < g_HashNumBuckets * g_HashBucketSize)
{
HashEntry entry = getHashEntry(g_Hash, bucketID);

Expand Down Expand Up @@ -87,6 +87,7 @@ StructuredBuffer<SDFBlockDesc> g_SDFBLockDescs : register(t0);
RWBuffer<float> g_SDFBlocksSDF : register(u0);
RWBuffer<int> g_outputSDFBlocks : register(u1);
RWBuffer<int> g_SDFBlocksRGBW : register(u2);
RWBuffer<float> g_SDFBlocksID : register(u3);

[numthreads(SDF_BLOCK_SIZE*SDF_BLOCK_SIZE*SDF_BLOCK_SIZE, 1, 1)]
void integrateFromGlobalHashPass2CS(int3 dTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI: SV_GroupIndex, uint3 GID : SV_GroupID)
Expand All @@ -97,13 +98,17 @@ void integrateFromGlobalHashPass2CS(int3 dTid : SV_DispatchThreadID, uint3 GTid
{
SDFBlockDesc desc = g_SDFBLockDescs[idxBlock];

// Copy SDF block to CPU
g_outputSDFBlocks[2*(linBlockSize*idxBlock+GI)+0] = asint(g_SDFBlocksSDF[desc.ptr+GI]);
g_outputSDFBlocks[2*(linBlockSize*idxBlock+GI)+1] = g_SDFBlocksRGBW[desc.ptr+GI];
//这里需要针对sdfblockid修改代码!!!从这里开始!!!

// Copy SDF block to CPU, 添加了id之后 每个voxel占3个字节
g_outputSDFBlocks[3 * (linBlockSize*idxBlock + GI) + 0] = asint(g_SDFBlocksSDF[desc.ptr + GI]);
g_outputSDFBlocks[3 * (linBlockSize*idxBlock + GI) + 1] = g_SDFBlocksRGBW[desc.ptr + GI];
// wei add, 拷贝sdfblocks中每个voxel的id
g_outputSDFBlocks[3 * (linBlockSize*idxBlock + GI) + 2] = g_SDFBlocksID[desc.ptr + GI];

//// Reset SDF Block
//g_SDFBlocks[2*(desc.ptr+GI)+0] = 0;
//g_SDFBlocks[2*(desc.ptr+GI)+1] = 0;
deleteVoxel(g_SDFBlocksSDF, g_SDFBlocksRGBW, desc.ptr+GI);
deleteVoxel(g_SDFBlocksSDF, g_SDFBlocksRGBW, g_SDFBlocksID, desc.ptr + GI);
}
}
12 changes: 8 additions & 4 deletions DepthSensing/Shaders/PhongLighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct VS_OUTPUT

#define MINF asfloat(0xff800000)


//这个函数就是实际看到的点的Pixel shader函数
float4 PhongPS(VS_OUTPUT Input) : SV_TARGET
{
Expand All @@ -47,6 +48,9 @@ float4 PhongPS(VS_OUTPUT Input) : SV_TARGET
float3 color = inputColors.Sample(g_PointSampler, Input.vTexcoord).xyz;
//wei add
float id = inputIDs.Sample(g_PointSampler, Input.vTexcoord);
//test using color as id
//color.x = 0.01;
id = (asint(color.x * 100)) % 4;

if (id != MINF && position.x != MINF && color.x != MINF && normal.x != MINF)
{
Expand All @@ -55,17 +59,17 @@ float4 PhongPS(VS_OUTPUT Input) : SV_TARGET

if(g_useMaterial == 1)
{
//material = float4(color, 1.0f);
if (id == 1.0) {
if (id == 0.0f){
material = float4(1.0f, 1.0f, 1.0f, 0.0f);
}else if (id == 1.0) {
material = float4(1.0f, 0.0f, 0.0f, 0.0f);
} else if (id == 2.0) {
material = float4(0.0f, 1.0f, 0.0f, 0.0f);
} else if (id == 3.0) {
material = float4(0.0f, 0.0f, 1.0f, 0.0f);
} else {
} else if (id == 4.0){
material = float4(1.0f, 1.0f, 0.0f, 0.0f);
}
// material = float4(0.0f, 1.0f, 0.0f, 0.0f);
// material = float4(color, 1.0f);
}

Expand Down
23 changes: 17 additions & 6 deletions DepthSensing/Source/DX11SceneRepChunkGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ HRESULT DX11SceneRepChunkGrid::StreamOutToCPUPass0GPU(ID3D11DeviceContext* conte

ID3D11UnorderedAccessView* hashUAV = sceneRepHashSDF.GetHashUAV();
ID3D11UnorderedAccessView* sdfBlocksSDFUAV = sceneRepHashSDF.GetSDFBlocksSDFUAV();
//wei add
ID3D11UnorderedAccessView* sdfBlocksIDUAV = sceneRepHashSDF.GetSDFBlocksIDUAV();

ID3D11UnorderedAccessView* sdfBlocksRGBWUAV = sceneRepHashSDF.GetSDFBlocksRGBWUAV();
ID3D11UnorderedAccessView* heapAppendUAV = sceneRepHashSDF.GetHeapUAV();
ID3D11UnorderedAccessView* hashBucketMutex = sceneRepHashSDF.GetAndClearHashBucketMutex(context);
Expand Down Expand Up @@ -109,6 +112,7 @@ HRESULT DX11SceneRepChunkGrid::StreamOutToCPUPass0GPU(ID3D11DeviceContext* conte
context->CSSetConstantBuffers(1, 1, &m_constantBufferIntegrateFromGlobalHash);
ID3D11Buffer* CBGlobalAppState = GlobalAppState::getInstance().MapAndGetConstantBuffer(context);
context->CSSetConstantBuffers(8, 1, &CBGlobalAppState);

context->CSSetShader(m_pComputeShaderIntegrateFromGlobalHashPass1, 0, 0);

// Run compute shader
Expand Down Expand Up @@ -148,21 +152,24 @@ HRESULT DX11SceneRepChunkGrid::StreamOutToCPUPass0GPU(ID3D11DeviceContext* conte
V_RETURN(context->Map(m_constantBufferIntegrateFromGlobalHash, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));
cbuffer = (CBufferIntegrateFromGlobalHash*)mappedResource.pData;

cbuffer->nSDFBlockDescs = nSDFBlockDescs;
cbuffer->nSDFBlockDescs = nSDFBlockDescs;

context->Unmap(m_constantBufferIntegrateFromGlobalHash, 0);

// Setup pipeline
context->CSSetUnorderedAccessViews(0, 1, &sdfBlocksSDFUAV, 0);
//1号位置上的这个UVA是负责接收GPU的输出的,见下面shader的入口函数
//1号位置上的这个UVA是负责接收GPU的sdfBlock的输出,见下面shader的入口函数
context->CSSetUnorderedAccessViews(1, 1, &m_pSDFBlockOutputUAV, 0);
context->CSSetUnorderedAccessViews(2, 1, &sdfBlocksRGBWUAV, 0);
//wei add,这个对应于文件IntegrateFromGlobalHash.hlsl的函数integrateFromGlobalHashPass2CS中,register(u3)
context->CSSetUnorderedAccessViews(3, 1, &sdfBlocksIDUAV, 0);

context->CSSetShaderResources(0, 1, &m_pSDFBlockDescOutputSRV);
context->CSSetConstantBuffers(0, 1, &CBsceneRepSDF);
context->CSSetConstantBuffers(1, 1, &m_constantBufferIntegrateFromGlobalHash);
context->CSSetConstantBuffers(8, 1, &CBGlobalAppState);

//这里是compute shader阶段
//这里是compute shader阶段, 对应于integrateFromGlobalHashPass2CS in file “IntegrateFromGlobalHash.hlsl”
context->CSSetShader(m_pComputeShaderIntegrateFromGlobalHashPass2, 0, 0);

// Run compute shader
Expand All @@ -179,17 +186,20 @@ HRESULT DX11SceneRepChunkGrid::StreamOutToCPUPass0GPU(ID3D11DeviceContext* conte
ID3D11Buffer* nullB2[2] = {NULL, NULL};

context->CSSetUnorderedAccessViews(0, 3, nullUAV2, 0);
//wei add
context->CSSetUnorderedAccessViews(3, 1, NULL, 0);

context->CSSetShaderResources(0, 1, nullSRV);
context->CSSetConstantBuffers(0, 2, nullB2);
context->CSSetConstantBuffers(8, 1, nullB2);
context->CSSetShader(0, 0, 0);

// Copy to CPU and Integrate
const unsigned int linBlockSize = 2*SDF_BLOCK_SIZE*SDF_BLOCK_SIZE*SDF_BLOCK_SIZE;
D3D11_BOX sourceRegionDesc = {0, 0, 0, 4*sizeof(int)*nSDFBlockDescs, 1, 1};
const unsigned int linBlockSize = 2 * SDF_BLOCK_SIZE * SDF_BLOCK_SIZE * SDF_BLOCK_SIZE;
D3D11_BOX sourceRegionDesc = {0, 0, 0, 4 * sizeof(int) * nSDFBlockDescs, 1, 1};
context->CopySubresourceRegion(m_pSDFBlockDescOutputCPU, 0, 0, 0, 0, m_pSDFBlockDescOutput, 0, &sourceRegionDesc);

D3D11_BOX sourceRegionBlock = {0, 0, 0, sizeof(int)*linBlockSize*nSDFBlockDescs, 1, 1};
D3D11_BOX sourceRegionBlock = { 0, 0, 0, sizeof(int)*linBlockSize*nSDFBlockDescs, 1, 1 };
context->CopySubresourceRegion(m_pSDFBlockOutputCPU, 0, 0, 0, 0, m_pSDFBlockOutput, 0, &sourceRegionBlock);
}

Expand Down Expand Up @@ -433,6 +443,7 @@ HRESULT DX11SceneRepChunkGrid::StreamInToGPUPass1GPU(ID3D11DeviceContext* contex
context->CSSetConstantBuffers(1, 1, &m_constantBufferChunkToGlobalHash);
ID3D11Buffer* CBGlobalAppState = GlobalAppState::getInstance().MapAndGetConstantBuffer(context);
context->CSSetConstantBuffers(8, 1, &CBGlobalAppState);
//设置shader语言
context->CSSetShader(m_pComputeShaderChunkToGlobalHashPass1, 0, 0);

// Run compute shader
Expand Down
4 changes: 2 additions & 2 deletions DepthSensing/Source/DX11SceneRepChunkGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,8 @@ class DX11SceneRepChunkGrid
ID3D11Buffer* m_pSDFBlockDescOutputCPU;
ID3D11Buffer* m_pSDFBlockDescInputCPU;

ID3D11ComputeShader* m_pComputeShaderIntegrateFromGlobalHashPass1;
ID3D11ComputeShader* m_pComputeShaderIntegrateFromGlobalHashPass2;
ID3D11ComputeShader* m_pComputeShaderIntegrateFromGlobalHashPass1;//这个同步的是heap相关的
ID3D11ComputeShader* m_pComputeShaderIntegrateFromGlobalHashPass2;//这个同步的是voxel里面的sdf, weight, id等

ID3D11ComputeShader* m_pComputeShaderChunkToGlobalHashPass1;
ID3D11ComputeShader* m_pComputeShaderChunkToGlobalHashPass2;
Expand Down
3 changes: 3 additions & 0 deletions DepthSensing/Source/DX11SceneRepHashSDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class DX11SceneRepHashSDF {
ID3D11UnorderedAccessView* GetSDFBlocksRGBWUAV() {
return m_SDFBlocksRGBWUAV;
}
ID3D11UnorderedAccessView* GetSDFBlocksIDUAV() {
return m_SDFBlocksIDUAV;
}
ID3D11UnorderedAccessView* GetHeapUAV() {
return m_HeapUAV;
}
Expand Down
3 changes: 2 additions & 1 deletion DepthSensing/Source/DepthSensing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext*
GlobalAppState::getInstance().s_Timer.start();
}

//处理新到的每一帧数据!!
HRESULT hr0 = g_Sensor.processDepth(pd3dImmediateContext); // shouldn't hr0 and h1 be used to check if new work has to be done? registration etc
HRESULT hr1 = g_Sensor.processColor(pd3dImmediateContext);

Expand Down Expand Up @@ -1051,7 +1052,7 @@ void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext*
GlobalAppState::getInstance().s_Timer.start();
}

//??这个是做什么的?
//这个是CPU和GPU同步数据的核心了!!
g_SceneRepChunkGrid.StreamOutToCPUPass0GPU(DXUTGetD3D11DeviceContext(), g_SceneRepSDFLocal, p, GlobalAppState::getInstance().s_StreamingRadius, true, true);
g_SceneRepChunkGrid.StreamInToGPUPass1GPU(DXUTGetD3D11DeviceContext(), g_SceneRepSDFLocal, true);

Expand Down

0 comments on commit 55a9704

Please sign in to comment.