Blender V5.3
eevee_ray_generate.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "draw_view.bsl.hh"
8#include "eevee_bxdf.bsl.hh"
11#include "eevee_uniform.bsl.hh"
12#include "gpu_shader_codegen_lib.glsl"
14
15namespace eevee {
16
17/* Returns view-space ray. */
19 ClosureUndetermined cl,
20 float3 V,
22{
23 float3 random_point_on_cylinder = sample_cylinder(noise);
24 /* Bias the rays so we never get really high energy rays almost parallel to the surface. */
25 constexpr float rng_bias = 0.08f;
26 /* When modeling object thickness as a sphere, the outgoing rays are distributed uniformly
27 * over the sphere. We don't want the RAY_BIAS in this case. */
28 if (cl.type != CLOSURE_BSDF_TRANSLUCENT_ID || thickness.mode() == ThicknessMode::Slab) {
29 random_point_on_cylinder.x = 1.0f - random_point_on_cylinder.x * (1.0f - rng_bias);
30 }
31
32 switch (cl.type) {
35 break;
36 case CLOSURE_BSDF_MICROFACET_GGX_REFLECTION_ID:
41 break;
42 case CLOSURE_NONE_ID:
43 assert(false);
44 break;
45 }
46
47 float3x3 tangent_to_world = from_up_axis(cl.N);
48
49 BsdfSample samp;
50 samp.pdf = 0.0f;
51 samp.direction = float3(0.0f);
52 switch (cl.type) {
54 samp = bxdf_translucent_sample(random_point_on_cylinder, thickness);
55 break;
58 samp = bxdf_diffuse_sample(random_point_on_cylinder);
59 break;
60 case CLOSURE_BSDF_MICROFACET_GGX_REFLECTION_ID: {
61 samp = bxdf_ggx_sample_reflection(random_point_on_cylinder,
62 V * tangent_to_world,
63 square(to_closure_reflection(cl).roughness),
64 true);
65 break;
66 }
68 samp = bxdf_ggx_sample_refraction(random_point_on_cylinder,
69 V * tangent_to_world,
70 square(to_closure_refraction(cl).roughness),
71 to_closure_refraction(cl).ior,
73 true);
74 break;
75 }
77 samp = bxdf_ggx_sample_reflection(random_point_on_cylinder,
78 V * tangent_to_world,
79 square(to_closure_thin_refraction(cl).roughness),
80 true);
81 samp.direction.z = -samp.direction.z;
82 break;
83 }
84 case CLOSURE_NONE_ID:
85 assert(false);
86 break;
87 }
88 samp.direction = tangent_to_world * float3(samp.direction);
89
90 return samp;
91}
92
93} // namespace eevee
94
95namespace eevee::raytracing {
96
103
108[[compute, local_size(RAYTRACE_GROUP_SIZE, RAYTRACE_GROUP_SIZE)]]
110 [[resource_table]] const Uniform &uni,
111 [[resource_table]] const Sampling &sampling,
112 [[resource_table]] const UtilityTexture &util_tx,
113 [[resource_table]] const draw::View &views,
114 [[resource_table]] const gbuffer::Reader &reader,
115 [[work_group_id]] const uint3 group_id,
116 [[local_invocation_id]] const uint3 local_id)
117{
118 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
119 uint2 tile_coord = unpackUvec2x16(srt.tiles_coord_buf[group_id.x]);
120 int2 texel = int2(local_id.xy + tile_coord * tile_size);
121
122 int2 texel_fullres = texel * uni.raytrace_buf.trace_pixel_scale +
123 uni.raytrace_buf.trace_pixel_offset;
124
125 gbuffer::Header gbuf_header = reader.read_header(texel_fullres);
126 ClosureUndetermined closure = reader.read_bin(texel_fullres, srt.closure_index);
127
128 if (closure.type == CLOSURE_NONE_ID) {
129 imageStore(srt.out_ray_data_img, texel, float4(0.0f));
130 return;
131 }
132
133 const ViewMatrices view = views.get(0);
134
135 float2 uv = (float2(texel_fullres) + 0.5f) / float2(textureSize(reader.gbuf_header_tx, 0).xy);
136 float3 P = view.point_screen_to_world(float3(uv, 0.5f));
138 float2 noise = util_tx.fetch(float2(texel), UTIL_BLUE_NOISE_LAYER).rg;
139 noise = fract(noise + sampling.rng_2D_get(SAMPLING_RAYTRACE_U));
140
141 Thickness thickness = reader.read_thickness(gbuf_header, texel_fullres);
142
143 BsdfSample samp = ray_generate_direction(noise.xy, closure, V, thickness);
144
145 /* Store inverse pdf to speedup denoising.
146 * Limit to the smallest non-0 value that the format can encode.
147 * Strangely it does not correspond to the IEEE spec. */
148 float inv_pdf = (samp.pdf == 0.0f) ? 0.0f : max(6e-8f, 1.0f / samp.pdf);
149 imageStoreFast(srt.out_ray_data_img, texel, float4(samp.direction, inv_pdf));
150}
151
152} // namespace eevee::raytracing
153
btScalar max
BsdfSample bxdf_translucent_sample(float3 rand, Thickness thickness)
BsdfSample bxdf_diffuse_sample(const float3 rand_cylinder)
BsdfSample bxdf_ggx_sample_refraction(float3 rand, float3 Vt, float alpha, float ior, Thickness thickness, const bool do_clamp)
void bxdf_ggx_context_amend_transmission(ClosureUndetermined &cl, float3 &V, Thickness thickness)
BsdfSample bxdf_ggx_sample_reflection(float3 rand, float3 Vt, float alpha, bool do_clamp)
#define UTIL_BLUE_NOISE_LAYER
PipelineCompute eevee_ray_generate(eevee::raytracing::generate_rays)
float3 sample_cylinder(float2 rand)
@ SFLOAT_16_16_16_16
#define assert(assertion)
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define work_group_id
#define resource_table
#define specialization_constant(default_value)
#define local_invocation_id
#define compute
#define local_size(...)
T fract(T) RET
ImageBase< float, 2 > image2D
#define imageStoreFast
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
TEX_TEMPLATE SizeVec textureSize(T, int) RET
float noise(float p)
float square(float v)
float3x3 from_up_axis(float3 up)
uint2 unpackUvec2x16(uint a)
@ CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID
@ CLOSURE_BSDF_DIFFUSE_ID
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_NONE_ID
@ CLOSURE_BSDF_THIN_GLASS_TRANSMISSION_ID
@ CLOSURE_BSDF_TRANSLUCENT_ID
void generate_rays(RayGenerate &srt, const Uniform &uni, const Sampling &sampling, const UtilityTexture &util_tx, const draw::View &views, const gbuffer::Reader &reader, const uint3 group_id, const uint3 local_id)
BsdfSample ray_generate_direction(float2 noise, ClosureUndetermined cl, float3 V, Thickness thickness)
packed_float3 direction
float3 point_screen_to_world(float3 ssP) const
float3 world_incident_vector(float3 P) const
float x
Definition sky_math.h:136
CCL_NAMESPACE_BEGIN struct Window V