游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1011|回复: 0

Queried Virtual Shadow Maps problem

[复制链接]

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
发表于 2008-3-12 00:13:00 | 显示全部楼层 |阅读模式
I am implementing the QVSM.
I have a question how to calculate the matrix of m4x4_screen_to_shadowmap in vsmshadow_sm_helper2.hlsl(in Book sample of ShaderX5 chapter4.5).


  1. //vsmshadow_sm_helper2.hlsl

  2. float4x4 m4x4_screen_to_shadowmap; // The uniform part of the transformation from screenspace to shadowmapspace


  3. //-------------------------------------------------------------------------------------------------
  4. // ScreenspaceToShadowmapspace
  5. //-------------------------------------------------------------------------------------------------

  6. inline float4 ScreenspaceToShadowmapspace(float screen_x, float screen_y, float z_view)
  7. {
  8.         float4 v4_pos_screen_reprojected = float4( screen_x * z_view, screen_y * z_view, z_view, 1 );
  9.         float4 v4_pos_lightspace        = mul(v4_pos_screen_reprojected, m4x4_screen_to_shadowmap);
  10.         return v4_pos_lightspace;
  11. };


  12. //-------------------------------------------------------------------------------------------------
  13. // ScreenspaceToShadowmapCoordinatesAndLightspaceDepth
  14. //-------------------------------------------------------------------------------------------------

  15. inline float3 ScreenspaceToShadowmapCoordinatesAndLightspaceDepth(float screen_x, float screen_y, float z_view)
  16. {
  17.         float4 pos_light = ScreenspaceToShadowmapspace(screen_x, screen_y, z_view);

  18.         // Project result into shadowmap coordinates

  19.         double pos_light_w_inv = 1.0 / pos_light.w;

  20.         float3 pos_shadowmap =
  21.                 float3(
  22.                         0.5 + 0.5 * pos_light_w_inv * pos_light.x, // SM texture x
  23.                         0.5 - 0.5 * pos_light_w_inv * pos_light.y, // SM texture y
  24.                         pos_light_w_inv * pos_light.z  // depth in SM space
  25.                 );

  26.         return pos_shadowmap;
  27. };
复制代码


  1. //vsmshadow_deferred.hlsl

  2. //-------------------------------------------------------------------------------------------------
  3. // PsApplySmTileToFramebufferUsingShadowDepthBuffer
  4. // Apply shadow map tile to the frame buffer using the linear depth values stored in the
  5. // Shadow Depth Buffer
  6. //-------------------------------------------------------------------------------------------------

  7. Ps_DEFAULT_OUT PsApplySmTileToFramebufferUsingShadowDepthBuffer(Ps_TC_IN IN)
  8. {
  9.         Ps_DEFAULT_OUT OUT;
  10.        
  11.         float shadow_term = 1.0f;
  12.         const float shadow_ambient = 0.7f;
  13.         //float shadow_ambient = UNIFORM_shadow_map_ambient;
  14.        
  15.         float z_view_center = tex2D(tex_shadow_depth_buffer, IN.v2_tc.xy).w;

  16.         float3 pos_shadowmap =
  17.                 ScreenspaceToShadowmapCoordinatesAndLightspaceDepth( IN.v2_tc.x, IN.v2_tc.y, z_view_center );
  18.         float2 sm_coords = pos_shadowmap.xy;
  19.         float depth_lightspace = pos_shadowmap.z;
  20.        
  21.         // Pixel lies on backplane or query does not concern current tile => Skip Fragment
  22.         if(
  23.                 (z_view_center == 0.0f) ||  // skip pixels lying on the backplane (D3D9 only allows DWORDs for Clear, so we have to use 0 here).
  24.                 (sm_coords.x < 0) || (sm_coords.x > 1) ||  // pixel lies outside of current shdow map tile
  25.                 (sm_coords.y < 0) || (sm_coords.y > 1)
  26.         )
  27.         {
  28.                 clip(-1);
  29.         }

  30.        
  31. #if(1)

  32.         shadow_term = (tex2D(tex_shadowmap_tile, sm_coords).x < (depth_lightspace)) ? shadow_ambient : 1.0f;

  33. #else

  34.         #include "vsmshadow_adaptive_sm_filtering_inc.hlsl"

  35. #endif

  36.         OUT.color = tex2D(tex_shadow_depth_buffer, IN.v2_tc.xy) * shadow_term;

  37.         return OUT;
  38. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-20 09:33

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表