Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 460
  2. #extension GL_EXT_nonuniform_qualifier : enable
  3. #extension GL_EXT_scalar_block_layout : enable
  4. #extension GL_EXT_ray_tracing : enable
  5. #extension GL_EXT_ray_query : enable
  6.  
  7.  
  8. layout (local_size_x = 32, local_size_y = 32) in;
  9. layout(binding = 7, set = 0) uniform accelerationStructureEXT topLevelAS;
  10.  
  11. layout (set = 0,binding = 0) uniform sampler2D samplerPositionDepth;
  12. layout (set = 0,binding = 1) uniform sampler2D samplerNormal;
  13. layout (set = 0,binding = 2) uniform sampler2D ssaoNoise;
  14.  
  15. layout (set = 0,binding = 3, r8) uniform writeonly image2D resultImage;
  16.  
  17. layout(set = 0, binding = 5) uniform UniformBufferObject {
  18.     mat4 model;
  19.     mat4 view;
  20.     mat4 projection;
  21.     mat4 inv_model;
  22.     mat4 inv_view;
  23.     mat4 inv_proj;
  24.     vec4 eye;
  25. } ubo;
  26. #define M1 1597334677U     //1719413*929
  27. #define M2 3812015801U     //140473*2467*11
  28. #define M3 3299493293U     //467549*7057
  29.  
  30. #define F0 (1.0/float(0xffffffffU))
  31.  
  32. #define hash(n) n*(n^(n>>15))
  33.  
  34. #define coord1(p) (p*M1)
  35. #define coord2(p) (p.x*M1^p.y*M2)
  36. #define coord3(p) (p.x*M1^p.y*M2^p.z*M3)
  37.  
  38. float hash1(uint n){return float(hash(n))*F0;}
  39. vec2 hash2(uint n){return vec2(hash(n)*uvec2(0x1U,0x3fffU))*F0;}
  40. vec3 hash3(uint n){return vec3(hash(n)*uvec3(0x1U,0x1ffU,0x3ffffU))*F0;}
  41. vec4 hash4(uint n){return vec4(hash(n)*uvec4(0x1U,0x7fU,0x3fffU,0x1fffffU))*F0;}
  42. vec3 screenBluenoise(int seed)
  43. {
  44.     // Get a random vector using a noise lookup
  45.     ivec2 noiseDim = textureSize(ssaoNoise, 0);
  46.     const vec2 noiseUV = vec2(gl_GlobalInvocationID.xy) / (noiseDim*seed);  
  47.  
  48.     vec3 randomVec = texture(ssaoNoise, noiseUV).xyz * 2.0 - 1.0;
  49.     return randomVec;
  50. }
  51. // clang-format on
  52. vec3 CosineSampleHemisphere(float u1, float u2)
  53. {
  54.     const float r = sqrt(u1);
  55.     const float theta = 2 * 3.1416f * u2;
  56.  
  57.     const float x = r * cos(theta);
  58.     const float y = r * sin(theta);
  59.  
  60.     return vec3(x, y, sqrt(max(0.0f, 1 - u1)));
  61. }
  62.  
  63. void main()
  64. {
  65.     ivec2 dim = imageSize(resultImage);
  66.     float outFragColor = 0;
  67.     if(gl_GlobalInvocationID.x < dim.x && gl_GlobalInvocationID.y < dim.y){    
  68.        
  69.         vec2 inUV = vec2(gl_GlobalInvocationID.xy) / dim;
  70.    
  71.         vec2 d = inUV * 2.0 - 1.0;
  72.  
  73.          // Ray Query for shadow
  74.          vec3  origin    = texture(samplerPositionDepth, inUV).xyz;
  75.  
  76.          vec3 normal = normalize(texture(samplerNormal, inUV).rgb * 2.0 - 1.0);
  77.         int samples = 16;
  78.         for(int i = 0; i < samples;i++){
  79.             vec3 rng = normalize(hash3(coord3(ivec3(gl_GlobalInvocationID.x,gl_GlobalInvocationID.y,i))) * 2.0 - 1.0);//screenBluenoise(i);          
  80.  
  81.             vec3 samplePos = rng;//TBN*hemisphere;
  82.             if(dot(rng,normal) < 0){
  83.                 samplePos -= samplePos;
  84.             }
  85.  
  86.             float rayLen  =500.f;
  87.             // vec3 target = origin + samplePos;// * 5 + screenBluenoise()*5;
  88.             vec3  direction = samplePos;//normalize(target - origin);//vec3(0,1,1);  // vector to light
  89.             float tMin      = 0.1f;
  90.             float tMax      = rayLen;
  91.  
  92.             // Initializes a ray query object but does not start traversal
  93.             rayQueryEXT rayQuery;
  94.        
  95.             rayQueryInitializeEXT(rayQuery, topLevelAS, gl_RayFlagsNoneEXT , 0xFF, origin.xyz, tMin,
  96.                                 direction.xyz, tMax);
  97.            
  98.                     // Start traversal: return false if traversal is complete
  99.             while(rayQueryProceedEXT(rayQuery))
  100.             {
  101.                
  102.             }
  103.            
  104.             // Returns type of committed (true) intersection
  105.             if(rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT)
  106.             {
  107.                 outFragColor += rayQueryGetIntersectionTEXT(rayQuery,true)/rayLen;
  108.             }
  109.             else{
  110.                 outFragColor += 1;
  111.             }
  112.         }
  113.          
  114.  
  115.         imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), vec4(outFragColor/samples));
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement