コード例 #1
0
void VolumetricLighting::Execute(RenderContext& context) {
	bool peti = true;
	if (peti) {
		return;
	}

	ComputeCommandList& commandList = context.AsCompute();

	//swap dest textures
	auto tmp = m_volDstTexUAV[0];
	m_volDstTexUAV[0] = m_volDstTexUAV[1];
	m_volDstTexUAV[1] = tmp;

	Uniforms uniformsCBData;

	uniformsCBData.camNear = m_camera->GetNearPlane();
	uniformsCBData.camFar = m_camera->GetFarPlane();
	uniformsCBData.numSdfs = 1;
	uniformsCBData.v = m_camera->GetViewMatrix();
	uniformsCBData.p = m_camera->GetProjectionMatrix();

	static uint32_t haltonIndex = 0;
	const uint32_t haltonBase = 2;
	uniformsCBData.haltonFactor = getHalton(haltonIndex++, haltonBase);

	uniformsCBData.camPos = Vec4(m_camera->GetPosition(), 1);

	uniformsCBData.sunDirection = Vec4(0.8f, -0.7f, -0.9f, 0.0f);
	uniformsCBData.sunColor = Vec4(1.0f, 0.9f, 0.85f, 1.0f);

	Mat44 VP = m_camera->GetViewMatrix() * m_camera->GetProjectionMatrix();
	Mat44 invVP = VP.Inverse();

	//worldVec: ndcVec * invVP
	//reprojNdcVec: worldVec * oldVP
	uniformsCBData.oldVP = m_prevVP;

	uniformsCBData.invVP = invVP;

	/*//far/near ndc corners
	Vec4 ndcCorners[] = 
	{
		Vec4(-1.f, -1.f, 0.f, 1.f),
		Vec4(-1.f, 1.f, 0.f, 1.f),
		Vec4(1.f, 1.f, 0.f, 1.f),
		Vec4(1.f, -1.f, 0.f, 1.f),
		Vec4(1.f, 1.f, 0.f, 1.f),
	};

	//convert to world space frustum corners
	ndcCorners[0] = ndcCorners[0] * invVP;
	ndcCorners[1] = ndcCorners[1] * invVP;
	ndcCorners[2] = ndcCorners[2] * invVP;
	ndcCorners[3] = ndcCorners[3] * invVP;
	ndcCorners[4] = ndcCorners[4] * invVP;
	ndcCorners[0] /= ndcCorners[0].w;
	ndcCorners[1] /= ndcCorners[1].w;
	ndcCorners[2] /= ndcCorners[2].w;
	ndcCorners[3] /= ndcCorners[3].w;
	ndcCorners[4] /= ndcCorners[4].w;

	uniformsCBData.near_plane_ll_corner = Vec4(ndcCorners[0].xyz, 1.0);
	uniformsCBData.near_plane_x_axis = Vec4((Vec3(ndcCorners[2].xyz) - Vec3(ndcCorners[1].xyz).Normalized()), 0.0f);
	uniformsCBData.near_plane_y_axis = Vec4((Vec3(ndcCorners[4].xyz) - Vec3(ndcCorners[3].xyz).Normalized()), 0.0f);
	uniformsCBData.near_plane_xy_sizes = Vec4((Vec3(ndcCorners[2].xyz) - Vec3(ndcCorners[1].xyz)).Length(), (Vec3(ndcCorners[4].xyz) - Vec3(ndcCorners[3].xyz)).Length(), 0, 0);*/

	//DebugDrawManager::GetInstance().AddSphere(Vec3(-0.5, 0, 1), 5.0f, 0);

	uint32_t dispatchW, dispatchH;
	SetWorkgroupSize((unsigned)m_depthTexSrv.GetResource().GetWidth(), (unsigned)m_depthTexSrv.GetResource().GetHeight(), 16, 16, dispatchW, dispatchH);

	uniformsCBData.numWorkgroupsX = dispatchW;
	uniformsCBData.numWorkgroupsY = dispatchH;

	{ //cull SDFs
		//view space for culling
		uniformsCBData.sd[0].vsPosition = Vec4(Vec3(-4.0, 0, 1), 1.0f) * m_camera->GetViewMatrix();
		uniformsCBData.sd[0].radius = 3.0f;

		//create single-frame only cb
		gxeng::VolatileConstBuffer cb = context.CreateVolatileConstBuffer(&uniformsCBData, sizeof(Uniforms));
		cb.SetName("SDF culling volatile CB");
		gxeng::ConstBufferView cbv = context.CreateCbv(cb, 0, sizeof(Uniforms));


		commandList.SetResourceState(m_dstTexUAV.GetResource(), gxapi::eResourceState::UNORDERED_ACCESS);
		commandList.SetResourceState(m_sdfCullDataUAV.GetResource(), gxapi::eResourceState::UNORDERED_ACCESS);
		commandList.SetResourceState(m_volDstTexUAV[0].GetResource(), gxapi::eResourceState::UNORDERED_ACCESS);
		commandList.SetResourceState(m_volDstTexUAV[1].GetResource(), gxapi::eResourceState::UNORDERED_ACCESS);
		commandList.SetResourceState(m_depthTexSrv.GetResource(), { gxapi::eResourceState::PIXEL_SHADER_RESOURCE, gxapi::eResourceState::NON_PIXEL_SHADER_RESOURCE });
		commandList.SetResourceState(m_colorTexSRV.GetResource(), { gxapi::eResourceState::PIXEL_SHADER_RESOURCE, gxapi::eResourceState::NON_PIXEL_SHADER_RESOURCE });

		commandList.SetPipelineState(m_sdfCullingCSO.get());
		commandList.SetComputeBinder(&m_binder);
		commandList.BindCompute(m_inputColorBindParam, m_colorTexSRV);
		commandList.BindCompute(m_cullBindParam, m_sdfCullDataUAV);
		commandList.BindCompute(m_dstBindParam, m_dstTexUAV);
		commandList.BindCompute(m_volDst0BindParam, m_volDstTexUAV[0]);
		commandList.BindCompute(m_volDst1BindParam, m_volDstTexUAV[1]);
		commandList.BindCompute(m_depthBindParam, m_depthTexSrv);
		commandList.BindCompute(m_uniformsBindParam, cbv);
		commandList.Dispatch(dispatchW, dispatchH, 1);
		commandList.UAVBarrier(m_sdfCullDataUAV.GetResource());
		commandList.UAVBarrier(m_volDstTexUAV[0].GetResource());
	}

	{ //do volumetric lighting
		//world space for lighting / raymarching
		uniformsCBData.sd[0].vsPosition = Vec4(Vec3(-4.0, 0, 1), 1.0f);
		uniformsCBData.sd[0].radius = 3.0f;

		uniformsCBData.ld[0].vsPosition = Vec4(Vec3(0, 0, 1), 1.0f);
		uniformsCBData.ld[0].attenuationEnd = 5.0f;
		uniformsCBData.ld[0].diffuseLightColor = Vec4(1.f, 0.f, 0.f, 1.f);

		//create single-frame only cb
		gxeng::VolatileConstBuffer cb = context.CreateVolatileConstBuffer(&uniformsCBData, sizeof(Uniforms));
		cb.SetName("SDF culling volatile CB");
		gxeng::ConstBufferView cbv = context.CreateCbv(cb, 0, sizeof(Uniforms));


		commandList.SetResourceState(m_dstTexUAV.GetResource(), gxapi::eResourceState::UNORDERED_ACCESS);
		commandList.SetResourceState(m_volDstTexUAV[0].GetResource(), gxapi::eResourceState::UNORDERED_ACCESS);
		commandList.SetResourceState(m_volDstTexUAV[1].GetResource(), gxapi::eResourceState::UNORDERED_ACCESS);
		commandList.SetResourceState(m_depthTexSrv.GetResource(), { gxapi::eResourceState::PIXEL_SHADER_RESOURCE, gxapi::eResourceState::NON_PIXEL_SHADER_RESOURCE });
		commandList.SetResourceState(m_colorTexSRV.GetResource(), { gxapi::eResourceState::PIXEL_SHADER_RESOURCE, gxapi::eResourceState::NON_PIXEL_SHADER_RESOURCE });
		commandList.SetResourceState(m_sdfCullDataSRV.GetResource(), { gxapi::eResourceState::PIXEL_SHADER_RESOURCE, gxapi::eResourceState::NON_PIXEL_SHADER_RESOURCE });
		commandList.SetResourceState(m_lightCullDataSRV.GetResource(), { gxapi::eResourceState::PIXEL_SHADER_RESOURCE, gxapi::eResourceState::NON_PIXEL_SHADER_RESOURCE });
		commandList.SetResourceState(m_csmTexSRV.GetResource(), { gxapi::eResourceState::PIXEL_SHADER_RESOURCE, gxapi::eResourceState::NON_PIXEL_SHADER_RESOURCE });
		commandList.SetResourceState(m_shadowMXTexSRV.GetResource(), { gxapi::eResourceState::PIXEL_SHADER_RESOURCE, gxapi::eResourceState::NON_PIXEL_SHADER_RESOURCE });
		commandList.SetResourceState(m_csmSplitsTexSRV.GetResource(), { gxapi::eResourceState::PIXEL_SHADER_RESOURCE, gxapi::eResourceState::NON_PIXEL_SHADER_RESOURCE });

		commandList.SetPipelineState(m_volumetricLightingCSO.get());
		commandList.SetComputeBinder(&m_binder);
		commandList.BindCompute(m_csmTexBindParam, m_csmTexSRV);
		commandList.BindCompute(m_shadowMxTexBindParam, m_shadowMXTexSRV);
		commandList.BindCompute(m_csmSplitsTexBindParam, m_csmSplitsTexSRV);
		commandList.BindCompute(m_inputColorBindParam, m_colorTexSRV);
		commandList.BindCompute(m_cullRoBindParam, m_sdfCullDataSRV);
		commandList.BindCompute(m_lightCullBindParam, m_lightCullDataSRV);
		commandList.BindCompute(m_dstBindParam, m_dstTexUAV);
		commandList.BindCompute(m_volDst0BindParam, m_volDstTexUAV[0]);
		commandList.BindCompute(m_volDst1BindParam, m_volDstTexUAV[1]);
		commandList.BindCompute(m_depthBindParam, m_depthTexSrv);
		commandList.BindCompute(m_uniformsBindParam, cbv);
		commandList.Dispatch(dispatchW, dispatchH, 1);
		commandList.UAVBarrier(m_volDstTexUAV[0].GetResource());
	}

	m_prevVP = VP;
}