Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. #pragma pack_matrix( row_major )
  2.  
  3. #include "../LogDepth.fx"
  4.  
  5. #define SM4
  6.  
  7. cbuffer MainBuf
  8. {
  9. float4x4 ViewProj;
  10. float2 VPDims;
  11. float CamFar;
  12. float NumBounds;
  13. };
  14.  
  15.  
  16. struct BBox
  17. {
  18. float3 InstPos;
  19. float3 Extent;
  20. };
  21.  
  22.  
  23. StructuredBuffer<BBox> Buffer0;
  24.  
  25. // Is Visible 1 (Visible) 0 (Culled)
  26. RWStructuredBuffer<float> BufferOut;
  27.  
  28. Texture2D<float> HizMap,Hiz0;// : register(t1);
  29. SamplerState HizMapSampler;// : register(s0);
  30.  
  31. //#define TEX_WIDTH 200
  32. //#define TEX_HEIGHT 150
  33. //#define CAM_MAX 1000
  34. //CAN BE GET AUTOMATICLY, GET DIMENSIONS!
  35. //but still need to try with array somehow!
  36.  
  37. //new version of alghorytm :D lol
  38.  
  39. void ToNDC(inout float2 v)
  40. {
  41. v.x*=0.5f;
  42. v.y*=-0.5f;
  43. v.x+=0.5f;
  44. v.y+=0.5f;
  45. }
  46.  
  47. static const float LODS[10] = {512,256,128,64,32,16,8,4,2,1};
  48.  
  49. [numthreads(1, 1, 1)]
  50. void CSMain( uint3 GroupId : SV_GroupID,
  51. uint3 DispatchThreadId : SV_DispatchThreadID,
  52. uint GroupIndex : SV_GroupIndex)
  53. {
  54. // Calculate the actual index this thread in this group will be reading from.
  55. //int index = GroupIndex + (GroupId.x * NUM_THREADS_X);
  56. int index = DispatchThreadId.x;
  57.  
  58. // Bounding sphere center (XYZ) and radius (W), world space
  59. BBox EntBox = Buffer0[index];
  60. float3 InstPos=EntBox.InstPos;
  61. float3 Extent=EntBox.Extent;
  62.  
  63. float4 BoundingBox[8];
  64. BoundingBox[0] = float4( InstPos + float3( Extent.x, Extent.y, Extent.z), 1.0 );
  65. BoundingBox[1] = float4( InstPos + float3(-Extent.x, Extent.y, Extent.z), 1.0 );
  66. BoundingBox[2] = float4( InstPos + float3( Extent.x,-Extent.y, Extent.z), 1.0 );
  67. BoundingBox[3] = float4( InstPos + float3(-Extent.x,-Extent.y, Extent.z), 1.0 );
  68. BoundingBox[4] = float4( InstPos + float3( Extent.x, Extent.y,-Extent.z), 1.0 );
  69. BoundingBox[5] = float4( InstPos + float3(-Extent.x, Extent.y,-Extent.z), 1.0 );
  70. BoundingBox[6] = float4( InstPos + float3( Extent.x,-Extent.y,-Extent.z), 1.0 );
  71. BoundingBox[7] = float4( InstPos + float3(-Extent.x,-Extent.y,-Extent.z), 1.0 );
  72.  
  73. /* check how the bounding box resides regarding to the view frustum */
  74. int outOfBound[6] = { 0, 0, 0, 0, 0, 0 };
  75.  
  76. int i;
  77.  
  78. for (i=0; i<8; i++)
  79. {
  80. BoundingBox[i]=mul(BoundingBox[i],ViewProj);
  81. if ( BoundingBox[i].x > BoundingBox[i].w ) outOfBound[0]++;
  82. if ( BoundingBox[i].x < -BoundingBox[i].w ) outOfBound[1]++;
  83. if ( BoundingBox[i].y > BoundingBox[i].w ) outOfBound[2]++;
  84. if ( BoundingBox[i].y < -BoundingBox[i].w ) outOfBound[3]++;
  85. if ( BoundingBox[i].z > BoundingBox[i].w ) outOfBound[4]++;
  86. if ( BoundingBox[i].z < -BoundingBox[i].w ) outOfBound[5]++;
  87. }
  88.  
  89. int inFrustum = 1;
  90.  
  91. for (i=0; i<6; i++)
  92. if ( outOfBound[i] == 8 ) inFrustum = 0;
  93.  
  94. // float test=BufferOut[NumBounds];
  95.  
  96. [branch]
  97. if(inFrustum==1)
  98. {
  99. /* perform perspective division for the bounding box */
  100. [unroll(8)]for (int i=0; i<8; i++)
  101. {
  102. BoundingBox[i].xy /= BoundingBox[i].w;
  103. BoundingBox[i].z/=CamFar;
  104. }
  105.  
  106. float InstanceDepth = min( min( min( BoundingBox[0].z, BoundingBox[1].z ),
  107. min( BoundingBox[2].z, BoundingBox[3].z ) ),
  108. min( min( BoundingBox[4].z, BoundingBox[5].z ),
  109. min( BoundingBox[6].z, BoundingBox[7].z ) ) );
  110.  
  111. //fast check goes here somehow :D
  112.  
  113. [branch]
  114. if(InstanceDepth>HizMap.Load( int3(0,0,9) ))
  115. {
  116. BufferOut[index]=-2;
  117. }
  118. else
  119. {
  120. float2 BoundingRect[2];
  121. BoundingRect[0].x = min( min( min( BoundingBox[0].x, BoundingBox[1].x ),
  122. min( BoundingBox[2].x, BoundingBox[3].x ) ),
  123. min( min( BoundingBox[4].x, BoundingBox[5].x ),
  124. min( BoundingBox[6].x, BoundingBox[7].x ) ) );
  125. BoundingRect[0].y = min( min( min( BoundingBox[0].y, BoundingBox[1].y ),
  126. min( BoundingBox[2].y, BoundingBox[3].y ) ),
  127. min( min( BoundingBox[4].y, BoundingBox[5].y ),
  128. min( BoundingBox[6].y, BoundingBox[7].y ) ) );
  129. BoundingRect[1].x = max( max( max( BoundingBox[0].x, BoundingBox[1].x ),
  130. max( BoundingBox[2].x, BoundingBox[3].x ) ),
  131. max( max( BoundingBox[4].x, BoundingBox[5].x ),
  132. max( BoundingBox[6].x, BoundingBox[7].x ) ) );
  133. BoundingRect[1].y = max( max( max( BoundingBox[0].y, BoundingBox[1].y ),
  134. max( BoundingBox[2].y, BoundingBox[3].y ) ),
  135. max( max( BoundingBox[4].y, BoundingBox[5].y ),
  136. max( BoundingBox[6].y, BoundingBox[7].y ) ) );
  137.  
  138.  
  139. //convert bounding rect to ndc
  140. [unroll(2)]for(i=0;i<2;i++)
  141. {
  142. ToNDC(BoundingRect[i]);
  143. BoundingRect[i]=clamp(BoundingRect[i],0,1);
  144. }
  145.  
  146. float2 fBoundSize;
  147. fBoundSize.x=BoundingRect[1].x-BoundingRect[0].x;
  148. fBoundSize.y=BoundingRect[1].y-BoundingRect[0].y;
  149.  
  150. #define MAXX 512
  151. #define MAXY 512
  152.  
  153. fBoundSize.x*=MAXX;
  154. fBoundSize.y*=MAXY;
  155. int fLOD = 7;//(int)ceil(log2( max(fBoundSize.x,fBoundSize.y)*0.25f ));
  156. fLOD=min(fLOD,8);
  157. fLOD=max(fLOD,1);
  158.  
  159. float2 TexSize=float2(LODS[fLOD],LODS[fLOD]);
  160.  
  161. //convert to texture space(here's catch we must multiply by size array
  162. [unroll(2)]for(int i=0;i<2;i++)
  163. {
  164. //float2 &brect=BoundingRect[i];
  165. //brect.x*=curVp.Width;
  166. //brect.y*=curVp.Height;
  167. BoundingRect[i]*=TexSize;
  168. }
  169.  
  170. int2 SSMin,SSMax;
  171. SSMin.x=round( min(BoundingRect[0].x,BoundingRect[1].x) );
  172. SSMin.y=round( min(BoundingRect[0].y,BoundingRect[1].y) );
  173. SSMax.x=round( max(BoundingRect[0].x,BoundingRect[1].x) );
  174. SSMax.y=round( max(BoundingRect[0].y,BoundingRect[1].y) );
  175.  
  176. float2 BoundSize;
  177. BoundSize.x=SSMax.x-SSMin.x;
  178. BoundSize.y=SSMax.y-SSMin.y;
  179.  
  180. //[branch]
  181. if((BoundSize.x>MAXX/2) || (BoundSize.y>MAXY/2))//many pixels[something extra big]
  182. {
  183. BufferOut[index]=2;
  184. }
  185. else if((BoundSize.x<=0) || (BoundSize.y<=0))//bad case
  186. {
  187. BufferOut[index]=3;
  188. }
  189. else
  190. {
  191. int x,y;
  192. int incX=1;
  193. int incY=1;
  194.  
  195. bool AllBad=false;
  196.  
  197. for(y=SSMin.y;y<SSMax.y && !AllBad;y+=incY)
  198. {
  199. for(x=SSMin.x;x<SSMax.x && !AllBad;x+=incX)
  200. {
  201. if(InstanceDepth<HizMap.Load(uint3(x,y,fLOD)))
  202. {
  203. AllBad=true;
  204. break;
  205. }
  206. }
  207. }
  208. BufferOut[index]=AllBad?1:0;
  209. }
  210.  
  211. }//fast cull brace
  212. }
  213. else
  214. {
  215. BufferOut[index]=-1;
  216. }
  217. }
  218.  
  219. technique10 Render
  220. {
  221. pass P0
  222. {
  223. SetVertexShader( 0 );
  224. SetGeometryShader( 0 );
  225. SetPixelShader( 0 );
  226. SetComputeShader( CompileShader( cs_4_0, CSMain() ) );
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement