cubemap_roughness_raster.glsl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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[4] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0));
  10. uv_interp = base_arr[gl_VertexIndex];
  11. gl_Position = vec4(uv_interp * 2.0 - 1.0, 0.0, 1.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. for (uint sampleNum = 0u; sampleNum < params.sample_count; sampleNum++) {
  30. vec2 xi = Hammersley(sampleNum, params.sample_count);
  31. vec3 H = ImportanceSampleGGX(xi, params.roughness, N);
  32. vec3 V = N;
  33. vec3 L = (2.0 * dot(V, H) * H - V);
  34. float ndotl = clamp(dot(N, L), 0.0, 1.0);
  35. if (ndotl > 0.0) {
  36. sum.rgb += textureLod(source_cube, L, 0.0).rgb * ndotl;
  37. sum.a += ndotl;
  38. }
  39. }
  40. sum /= sum.a;
  41. frag_color = vec4(sum.rgb, 1.0);
  42. }
  43. }