Exemplo n.º 1
0
	void MultiResSILLayer::CreateNormalConeMipMap()
	{
		gbuffer_to_normal_cone_pp_->InputPin(0, g_buffer_texs_[0]);
		gbuffer_to_normal_cone_pp_->OutputPin(0, normal_cone_tex_);
		float delta_x = 1.0f / g_buffer_texs_[0]->Width(0);
		float delta_y = 1.0f / g_buffer_texs_[0]->Height(0);
		float4 delta_offset(delta_x, delta_y, delta_x / 2, delta_y / 2);
		gbuffer_to_normal_cone_pp_->SetParam(0, delta_offset);
		gbuffer_to_normal_cone_pp_->Apply();

		normal_cone_mipmap_pp_->InputPin(0, normal_cone_tex_);
		for (uint32_t i = 1; i < normal_cone_tex_->NumMipMaps(); ++ i)
		{
			int width = normal_cone_tex_->Width(i - 1);
			int height = normal_cone_tex_->Height(i - 1);
			float delta_x = 1.0f / width;
			float delta_y = 1.0f / height;
			float4 delta_offset(delta_x, delta_y, delta_x / 2, delta_y / 2);

			normal_cone_mipmap_pp_->SetParam(0, delta_offset);
			normal_cone_mipmap_pp_->SetParam(1, i - 1.0f);

			normal_cone_mipmap_pp_->OutputPin(0, normal_cone_small_tex_, i - 1);
			normal_cone_mipmap_pp_->Apply();

			normal_cone_small_tex_->CopyToSubTexture2D(*normal_cone_tex_, 0, i, 0, 0, width / 2, height / 2,
				0, i - 1, 0, 0, width / 2, height / 2);
		}
	}
Exemplo n.º 2
0
	void MultiResLayer::CreateDepthDerivativeMipMap(CameraPtr const & vp_camera)
	{
		gbuffer_to_depth_derivate_pp_->InputPin(0, g_buffer_rt0_tex_);
		gbuffer_to_depth_derivate_pp_->InputPin(1, g_buffer_depth_tex_);
		gbuffer_to_depth_derivate_pp_->OutputPin(0, depth_deriative_tex_);
		float delta_x = 1.0f / g_buffer_rt0_tex_->Width(0);
		float delta_y = 1.0f / g_buffer_rt0_tex_->Height(0);
		float4 delta_offset(delta_x, delta_y, delta_x / 2, delta_y / 2);
		gbuffer_to_depth_derivate_pp_->SetParam(0, delta_offset);
		gbuffer_to_depth_derivate_pp_->SetParam(1, float2(vp_camera->FarPlane(), 1.0f / vp_camera->FarPlane()));
		gbuffer_to_depth_derivate_pp_->Apply();

		depth_derivate_mipmap_pp_->InputPin(0, depth_deriative_tex_);
		for (uint32_t i = 1; i < depth_deriative_tex_->NumMipMaps(); ++ i)
		{
			int width = depth_deriative_tex_->Width(i - 1);
			int height = depth_deriative_tex_->Height(i - 1);

			delta_x = 1.0f / width;
			delta_y = 1.0f / height;
			delta_offset = float4(delta_x, delta_y, delta_x / 2, delta_y / 2);
			depth_derivate_mipmap_pp_->SetParam(0, delta_offset);
			depth_derivate_mipmap_pp_->SetParam(1, float2(vp_camera->FarPlane(), 1.0f / vp_camera->FarPlane()));
			
			depth_derivate_mipmap_pp_->OutputPin(0, depth_deriative_small_tex_, i - 1);
			depth_derivate_mipmap_pp_->Apply();

			depth_deriative_small_tex_->CopyToSubTexture2D(*depth_deriative_tex_, 0, i, 0, 0, width / 2, height / 2,
				0, i - 1, 0, 0, width / 2, height / 2);
		}
	}
Exemplo n.º 3
0
	void SDSMCascadedShadowLayer::UpdateCascades(Camera const & camera, float4x4 const & light_view_proj,
			float3 const & light_space_border)
	{
		RenderFactory& rf = Context::Instance().RenderFactoryInstance();
		RenderEngine& re = rf.RenderEngineInstance();

		uint32_t const num_cascades = static_cast<uint32_t>(intervals_.size());
		uint32_t const copy_index = frame_index_ & 1;
		uint32_t const read_back_index = (0 == frame_index_) ? copy_index : !copy_index;

		if (cs_support_)
		{
			re.BindFrameBuffer(FrameBufferPtr());

			float max_blur_light_space = 8.0f / 1024;
			float3 max_cascade_scale(max_blur_light_space / light_space_border.x(),
				max_blur_light_space / light_space_border.y(),
				std::numeric_limits<float>::max());

			int const TILE_DIM = 128;

			int dispatch_x = (depth_tex_->Width(0) + TILE_DIM - 1) / TILE_DIM;
			int dispatch_y = (depth_tex_->Height(0) + TILE_DIM - 1) / TILE_DIM;

			*interval_buff_param_ = interval_buff_;
			*interval_buff_uint_param_ = interval_buff_;
			*interval_buff_read_param_ = interval_buff_;
			*cascade_min_buff_uint_param_ = cascade_min_buff_;
			*cascade_max_buff_uint_param_ = cascade_max_buff_;
			*cascade_min_buff_read_param_ = cascade_min_buff_;
			*cascade_max_buff_read_param_ = cascade_max_buff_;
			*scale_buff_param_ = scale_buff_;
			*bias_buff_param_ = bias_buff_;
			*depth_tex_param_ = depth_tex_;
			*num_cascades_param_ = static_cast<int32_t>(num_cascades);
			*inv_depth_width_height_param_ = float2(1.0f / depth_tex_->Width(0), 1.0f / depth_tex_->Height(0));
			*near_far_param_ = float2(camera.NearPlane(), camera.FarPlane());
			float4x4 const & inv_proj = camera.InverseProjMatrix();
			float3 upper_left = MathLib::transform_coord(float3(-1, +1, 1), inv_proj);
			float3 upper_right = MathLib::transform_coord(float3(+1, +1, 1), inv_proj);
			float3 lower_left = MathLib::transform_coord(float3(-1, -1, 1), inv_proj);
			*upper_left_param_ = upper_left;
			*xy_dir_param_ = float2(upper_right.x() - upper_left.x(), lower_left.y() - upper_left.y());
			*view_to_light_view_proj_param_ = camera.InverseViewMatrix() * light_view_proj;
			*light_space_border_param_ = light_space_border;
			*max_cascade_scale_param_ = max_cascade_scale;

			re.Dispatch(*clear_z_bounds_tech_, 1, 1, 1);
			re.Dispatch(*reduce_z_bounds_from_depth_tech_, dispatch_x, dispatch_y, 1);
			re.Dispatch(*compute_log_cascades_from_z_bounds_tech_, 1, 1, 1);
			re.Dispatch(*clear_cascade_bounds_tech_, 1, 1, 1);
			re.Dispatch(*reduce_bounds_from_depth_tech_, dispatch_x, dispatch_y, 1);
			re.Dispatch(*compute_custom_cascades_tech_, 1, 1, 1);

			interval_buff_->CopyToBuffer(*interval_cpu_buffs_[copy_index]);
			scale_buff_->CopyToBuffer(*scale_cpu_buffs_[copy_index]);
			bias_buff_->CopyToBuffer(*bias_cpu_buffs_[copy_index]);

			GraphicsBuffer::Mapper interval_mapper(*interval_cpu_buffs_[read_back_index], BA_Read_Only);
			GraphicsBuffer::Mapper scale_mapper(*scale_cpu_buffs_[read_back_index], BA_Read_Only);
			GraphicsBuffer::Mapper bias_mapper(*bias_cpu_buffs_[read_back_index], BA_Read_Only);
			float2* interval_ptr = interval_mapper.Pointer<float2>();
			float3* scale_ptr = scale_mapper.Pointer<float3>();
			float3* bias_ptr = bias_mapper.Pointer<float3>();

			for (size_t i = 0; i < intervals_.size(); ++ i)
			{
				float3 const & scale = scale_ptr[i];
				float3 const & bias = bias_ptr[i];

				intervals_[i] = interval_ptr[i];
				scales_[i] = scale;
				biases_[i] = bias;
			}
		}
		else
		{
			float2 const near_far(camera.NearPlane(), camera.FarPlane());

			reduce_z_bounds_from_depth_pp_->SetParam(1, near_far);
			reduce_z_bounds_from_depth_pp_->Apply();

			for (uint32_t i = 1; i < depth_deriative_tex_->NumMipMaps(); ++ i)
			{
				int width = depth_deriative_tex_->Width(i - 1);
				int height = depth_deriative_tex_->Height(i - 1);

				float delta_x = 1.0f / width;
				float delta_y = 1.0f / height;
				float4 delta_offset(delta_x, delta_y, -delta_x / 2, -delta_y / 2);			
				reduce_z_bounds_from_depth_mip_map_pp_->SetParam(0, delta_offset);

				reduce_z_bounds_from_depth_mip_map_pp_->OutputPin(0, depth_deriative_small_tex_, i - 1);
				reduce_z_bounds_from_depth_mip_map_pp_->Apply();

				int sw = depth_deriative_tex_->Width(i);
				int sh = depth_deriative_tex_->Height(i);

				depth_deriative_small_tex_->CopyToSubTexture2D(*depth_deriative_tex_, 0, i, 0, 0, sw, sh,
					0, i - 1, 0, 0, sw, sh);
			}

			compute_log_cascades_from_z_bounds_pp_->SetParam(1, static_cast<int32_t>(num_cascades));
			compute_log_cascades_from_z_bounds_pp_->SetParam(2, near_far);
			compute_log_cascades_from_z_bounds_pp_->Apply();

			interval_tex_->CopyToSubTexture2D(*interval_cpu_texs_[copy_index], 0, 0, 0, 0, num_cascades, 1,
				0, 0, 0, 0, num_cascades, 1);

			Texture::Mapper interval_mapper(*interval_cpu_texs_[read_back_index], 0, 0,
				TMA_Read_Only, 0, 0, num_cascades, 1);
			Vector_T<half, 2>* interval_ptr = interval_mapper.Pointer<Vector_T<half, 2> >();

			for (size_t i = 0; i < intervals_.size(); ++ i)
			{
				float2 const interval(static_cast<float>(interval_ptr[i].x()),
					static_cast<float>(interval_ptr[i].y()));

				AABBox aabb = CalcFrustumExtents(camera, interval.x(), interval.y(), light_view_proj);

				aabb &= AABBox(float3(-1, -1, -1), float3(+1, +1, +1));

				aabb.Min() -= light_space_border;
				aabb.Max() += light_space_border;

				aabb.Min().x() = +aabb.Min().x() * 0.5f + 0.5f;
				aabb.Min().y() = -aabb.Min().y() * 0.5f + 0.5f;
				aabb.Max().x() = +aabb.Max().x() * 0.5f + 0.5f;
				aabb.Max().y() = -aabb.Max().y() * 0.5f + 0.5f;

				std::swap(aabb.Min().y(), aabb.Max().y());

				float3 const scale = float3(1.0f, 1.0f, 1.0f) / (aabb.Max() - aabb.Min());
				float3 const bias = -aabb.Min() * scale;

				intervals_[i] = interval;
				scales_[i] = scale;
				biases_[i] = bias;
			}
		}

		this->UpdateCropMats();

		++ frame_index_;
	}