void SmoothHeightMesh::MakeSmoothMesh()
{
	ScopedOnceTimer timer("SmoothHeightMesh::MakeSmoothMesh");

	// info:
	//   height-value array has size <maxx + 1> * <maxy + 1>
	//   and represents a grid of <maxx> cols by <maxy> rows
	//   maximum legal index is ((maxx + 1) * (maxy + 1)) - 1
	//
	//   row-width (number of height-value corners per row) is (maxx + 1)
	//   col-height (number of height-value corners per col) is (maxy + 1)
	//
	//   1st row has indices [maxx*(  0) + (  0), maxx*(1) + (  0)] inclusive
	//   2nd row has indices [maxx*(  1) + (  1), maxx*(2) + (  1)] inclusive
	//   3rd row has indices [maxx*(  2) + (  2), maxx*(3) + (  2)] inclusive
	//   ...
	//   Nth row has indices [maxx*(N-1) + (N-1), maxx*(N) + (N-1)] inclusive
	//
	const size_t size = (this->maxx + 1) * (this->maxy + 1);
	// use sliding window of maximums to reduce computational complexity
	const int intrad = smoothRadius / resolution;
	const int smoothrad = 3;

	assert(mesh.empty());
	mesh.resize(size);

	std::vector<float> colsMaxima(maxx + 1, -std::numeric_limits<float>::max());
	std::vector<int> maximaRows(maxx + 1, -1);
	std::vector<float> smoothed(size);

	FindMaximumColumnHeights(maxx, maxy, intrad, resolution, colsMaxima, maximaRows);

	for (int y = 0; y <= maxy; ++y) {
		AdvanceMaximaRows(y, maxx, resolution, colsMaxima, maximaRows);
		FindRadialMaximum(y, maxx, intrad, resolution, colsMaxima, mesh);
		FixRemainingMaxima(y, maxx, maxy, intrad, resolution, colsMaxima, maximaRows);

#ifdef _DEBUG
		CheckInvariants(y, maxx, maxy, intrad, resolution, colsMaxima, maximaRows);
#endif
	}

	// actually smooth with approximate Gaussian blur passes
	for (int numBlurs = 3; numBlurs > 0; --numBlurs) {
		BlurHorizontal(maxx, maxy, smoothrad, resolution, mesh, smoothed); mesh.swap(smoothed);
		BlurVertical(maxx, maxy, smoothrad, resolution, mesh, smoothed); mesh.swap(smoothed);
	}

	// `mesh` now contains the smoothed heightmap, save it in origMesh
	origMesh.resize(size);
	std::copy(mesh.begin(), mesh.end(), origMesh.begin());
}
Exemple #2
0
//==================================================================================================================================
void HDR::ProcessInternal()
{
	//hdrTarget = PostProcessManager::backbufferTarget;
	
	HRESULT hr = S_OK;
	
	UpdateBloomConstants(fMiddleGreyValue, fBloomThreshold, fBloomMultiplier);
	
	ID3D11SamplerState* pSamplerState[] = {mD3DSystem->Linear(), mD3DSystem->Point()};
	mD3DSystem->GetDeviceContext()->CSSetSamplers(0, 2, pSamplerState);
	mD3DSystem->GetDeviceContext()->PSSetSamplers(0, 2, pSamplerState);
	
	//mD3DSystem->GetDeviceContext()->OMSetRenderTargets(1, &mOutputTargets[0], NULL);
	
	ID3D11RenderTargetView* rtv[] = { nullptr };
	mD3DSystem->GetDeviceContext()->OMSetRenderTargets(1, rtv, PostProcessManager::DSView());
	
	int width = mD3DSystem->GetEngineOptions()->m_screenWidth / 2;
	int height = mD3DSystem->GetEngineOptions()->m_screenHeight / 2;
	int mipLevel0 = 0;
	int mipLevel1 = 0;
	
	ComputeLuminanceAndBrightPass(
		//target->SRView,// I think this is the scene SRV or the input SRV
		mInputTextures[0],// This is the SRV taken from the scene
		bright->UAView[0],
		bright->UAView[1],
		bright->UAView[2],
		bright->UAView[3],
		width,
		height
	);
	
	mipLevel0 = 3;
	mipLevel1 = 4;
	width = mD3DSystem->GetEngineOptions()->m_screenWidth >> (mipLevel0 + 1);
	height = mD3DSystem->GetEngineOptions()->m_screenHeight >> (mipLevel0 + 1);
	
	UpdateContantBuffer(mipLevel0, mipLevel1, width, height);
	BlurHorizontal(bright->SRView, blurredH->UAView[mipLevel0], width, height);
	BlurVertical(blurredH->SRView, blurredHV->UAView[mipLevel0], width, height);
	
	mipLevel0--;
	mipLevel1--;
	width = mD3DSystem->GetEngineOptions()->m_screenWidth >> (mipLevel0 + 1);
	height = mD3DSystem->GetEngineOptions()->m_screenHeight >> (mipLevel0 + 1);
	
	UpdateContantBuffer(mipLevel0, mipLevel1, width, height);
	Add(bright->SRView, blurredHV->SRView, summed->UAView[mipLevel0], width, height);
	BlurHorizontal(bright->SRView, blurredH->UAView[mipLevel0], width, height);
	BlurVertical(blurredH->SRView, blurredHV->UAView[mipLevel0], width, height);
	
	mipLevel0--;
	mipLevel1--;
	width = mD3DSystem->GetEngineOptions()->m_screenWidth >> (mipLevel0 + 1);
	height = mD3DSystem->GetEngineOptions()->m_screenHeight >> (mipLevel0 + 1);
	
	UpdateContantBuffer(mipLevel0, mipLevel1, width, height);
	Add(bright->SRView, blurredHV->SRView, summed->UAView[mipLevel0], width, height);
	BlurHorizontal(summed->SRView, blurredH->UAView[mipLevel0], width, height);
	BlurVertical(blurredH->SRView, blurredHV->UAView[mipLevel0], width, height);
	
	mipLevel0--;
	mipLevel1--;
	width = mD3DSystem->GetEngineOptions()->m_screenWidth >> (mipLevel0 + 1);
	height = mD3DSystem->GetEngineOptions()->m_screenHeight >> (mipLevel0 + 1);
	
	UpdateContantBuffer(mipLevel0, mipLevel1, width, height);
	Add(bright->SRView, blurredHV->SRView, summed->UAView[mipLevel0], width, height);
	BlurHorizontal(summed->SRView, blurredH->UAView[mipLevel0], width, height);
	BlurVertical(blurredH->SRView, blurredHV->UAView[mipLevel0], width, height);
	
	ID3D11UnorderedAccessView* pNULLUAV[] = {NULL, NULL, NULL};
	mD3DSystem->GetDeviceContext()->CSSetUnorderedAccessViews(0, 3, pNULLUAV, NULL);
	//ToneMapWithBloom(target->SRView, blurredHV->SRView, NULL);
	
	// Restore original render targets
	ID3D11RenderTargetView* rtv2[] = { PostProcessManager::RTView() };
	mD3DSystem->GetDeviceContext()->OMSetRenderTargets(1, rtv2, PostProcessManager::DSView());
	
	ToneMapWithBloom(mInputTextures[0], blurredHV->SRView, NULL);
	
	//SAFE_RELEASE(mOutputTargets[0]);
	//SAFE_RELEASE(mDSView);
	
	/*int backbufferWidth = mD3DSystem->GetEngineOptions()->m_screenWidth;
	int backbufferHeight = mD3DSystem->GetEngineOptions()->m_screenHeight;
	int TotalBackbufferPixels = backbufferWidth * backbufferHeight;
	
	// Run the compute shaders for HDR
	hdrTarget = PostProcessManager::CreateRenderTarget(backbufferWidth, backbufferHeight, 0, 0, DXGI_FORMAT_R16G16B16A16_FLOAT);
	{
		AddOutputTarget(hdrTarget->RTView);
		AddOutputViewport(hdrTarget->viewport);
		
		ID3D11Buffer* cs_cbs[1] = { m_pDownscaleCB };
		mD3DSystem->GetDeviceContext()->CSSetConstantBuffers(0, 1, cs_cbs);
		
		ID3D11ShaderResourceView* cs0_srvs[1] = { mInputTextures[0] };
		mD3DSystem->GetDeviceContext()->CSSetShaderResources(0, 1, cs0_srvs);
		ID3D11UnorderedAccessView* cs0_uavs[1] = { mIntermediateLuminance->UAView };
		mD3DSystem->GetDeviceContext()->CSSetUnorderedAccessViews(0, 1, cs0_uavs, (UINT*)(&cs0_uavs[0]));
		
		PostProcessManager::mShader->SwitchTo("DownScaleFirstPass", ZShadeSandboxShader::EShaderTypes::ST_COMPUTE);
		PostProcessManager::mShader->RenderCS11(1024, 1, 1);
		
		ID3D11ShaderResourceView* cs1_srvs[1] = { mIntermediateLuminance->SRView };
		mD3DSystem->GetDeviceContext()->CSSetShaderResources(0, 1, cs1_srvs);
		ID3D11UnorderedAccessView* cs1_uavs[1] = { mAverageLuminance->UAView };
		mD3DSystem->GetDeviceContext()->CSSetUnorderedAccessViews(0, 1, cs1_uavs, (UINT*)(&cs1_uavs[0]));
		
		PostProcessManager::mShader->SwitchTo("DownScaleSecondPass", ZShadeSandboxShader::EShaderTypes::ST_COMPUTE);
		PostProcessManager::mShader->RenderCS11(64, 1, 1);
		
		// Set the final pass constant buffer for the pixel shader
		{
			cbFinalPassConstants cFPC;
			cFPC.g_MiddleGrey = fMiddleGreyValue;
			cFPC.g_LumWhiteSqr = SQR(fWhiteValue * fMiddleGreyValue);
			D3D11_MAPPED_SUBRESOURCE mapped_res;
			mD3DSystem->GetDeviceContext()->Map(m_pFinalPassCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_res);
			{
				assert(mapped_res.pData);
				*(cbFinalPassConstants*)mapped_res.pData = cFPC;
			}
			mD3DSystem->GetDeviceContext()->Unmap(m_pFinalPassCB, 0);
		}
		ID3D11Buffer* ps_cbs[1] = { m_pFinalPassCB };
		mD3DSystem->GetDeviceContext()->PSSetConstantBuffers(0, 1, ps_cbs);
		
		// Set the input textures for HDR and average luminance
		AddInputTexture(mAverageLuminance->SRView);
		
		// Process the HDR pixel shader
		Process("PostProcessHDR");
		
		// Cleanup
		cs0_srvs[0] = { NULL };
		mD3DSystem->GetDeviceContext()->CSSetShaderResources(0, 1, cs0_srvs);
		cs0_uavs[0] = { NULL };
		mD3DSystem->GetDeviceContext()->CSSetUnorderedAccessViews(0, 1, cs0_uavs, (UINT*)(&cs0_uavs[0]));
		
		cs1_srvs[0] = { NULL };
		mD3DSystem->GetDeviceContext()->CSSetShaderResources(0, 1, cs1_srvs);
		cs1_uavs[0] = { NULL };
		mD3DSystem->GetDeviceContext()->CSSetUnorderedAccessViews(0, 1, cs1_uavs, (UINT*)(&cs1_uavs[0]));
	}*/
}