cubemap_roughness_raster.glsl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* clang-format off */
  2. #[vertex]
  3. #version 450
  4. #VERSION_DEFINES
  5. #include "cubemap_roughness_inc.glsl"
  6. layout(location = 0) out vec2 uv_interp;
  7. /* clang-format on */
  8. void main() {
  9. vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
  10. gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
  11. uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
  12. }
  13. /* clang-format off */
  14. #[fragment]
  15. #version 450
  16. #VERSION_DEFINES
  17. #include "cubemap_roughness_inc.glsl"
  18. layout(location = 0) in vec2 uv_interp;
  19. layout(set = 0, binding = 0) uniform samplerCube source_cube;
  20. layout(location = 0) out vec4 frag_color;
  21. /* clang-format on */
  22. void main() {
  23. vec3 N = texelCoordToVec(uv_interp * 2.0 - 1.0, params.face_id);
  24. //vec4 color = color_interp;
  25. if (params.use_direct_write) {
  26. frag_color = vec4(texture(source_cube, N).rgb, 1.0);
  27. } else {
  28. vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);
  29. float solid_angle_texel = 4.0 * M_PI / (6.0 * params.face_size * params.face_size);
  30. float roughness2 = params.roughness * params.roughness;
  31. float roughness4 = roughness2 * roughness2;
  32. vec3 UpVector = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
  33. mat3 T;
  34. T[0] = normalize(cross(UpVector, N));
  35. T[1] = cross(N, T[0]);
  36. T[2] = N;
  37. for (uint sampleNum = 0u; sampleNum < params.sample_count; sampleNum++) {
  38. vec2 xi = Hammersley(sampleNum, params.sample_count);
  39. vec3 H = T * ImportanceSampleGGX(xi, roughness4);
  40. float NdotH = dot(N, H);
  41. vec3 L = (2.0 * NdotH * H - N);
  42. float ndotl = clamp(dot(N, L), 0.0, 1.0);
  43. if (ndotl > 0.0) {
  44. float D = DistributionGGX(NdotH, roughness4);
  45. float pdf = D * NdotH / (4.0 * NdotH) + 0.0001;
  46. float solid_angle_sample = 1.0 / (float(params.sample_count) * pdf + 0.0001);
  47. float mipLevel = params.roughness == 0.0 ? 0.0 : 0.5 * log2(solid_angle_sample / solid_angle_texel);
  48. sum.rgb += textureLod(source_cube, L, mipLevel).rgb * ndotl;
  49. sum.a += ndotl;
  50. }
  51. }
  52. sum /= sum.a;
  53. frag_color = vec4(sum.rgb, 1.0);
  54. }
  55. }