Blender V5.3
workbench_shadow_raytrace.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "draw_view.bsl.hh"
11
13
20
21[[vertex]] void vert([[vertex_id]] const int &vert_id, [[position]] float4 &out_pos)
22{
23 fullscreen_vertex(vert_id, out_pos);
24}
25
28 [[resource_table]] const draw::View &views)
29{
30 const float2 resolution = float2(textureSize(srt.depth_tx, 0).xy);
31 const float2 screen_uv = frag_coord.xy / resolution;
32
33 const float depth = texture(srt.depth_tx, screen_uv).r;
34 if (depth == 1.0f) {
36 return;
37 }
38
39 ViewMatrices view = views.get(0);
40
41 const float3 N = view.normal_view_to_world(
42 workbench::normal_decode(texture(srt.normal_tx, screen_uv)));
43 if (dot(N, srt.pass_data.light_direction_ws) >= 0.0f) {
44 /* We already know the fragment is in shadow. No need to query. */
45 return;
46 }
47
48 /* Offset depth to compensate for depth buffer precision. */
50 float3(screen_uv, intBitsToFloat(floatBitsToInt(depth) - 2)));
51
52 const float pixel_size = srt.pass_data.pixel_size *
53 (view.is_perspective() ? view.z_distance(P) : 1.0f);
54
55 /* Offset by pixel size to compensate for floating point precision. */
56 const float tMin = pixel_size / max(dot(N, -srt.pass_data.light_direction_ws), 0.1f);
57
58 rayQuery query;
60 srt.shadow_as,
62 0xFF,
63 P,
64 tMin,
65 -srt.pass_data.light_direction_ws,
66 FLT_MAX);
67 rayQueryProceed(query);
68
69 const bool is_light_occluded = rayQueryGetIntersectionType(query, true) !=
71
72 if (!is_light_occluded) {
73 /* Writing the stencil means the fragment is in shadow. */
75 return;
76 }
77}
78
80
81} // namespace workbench::shadow::rt
btScalar max
#define FLT_MAX
#define uniform(slot)
#define position
#define acceleration_structure(slot)
#define frag_coord
#define resource_table
#define sampler(slot)
#define fragment
#define vertex_id
#define vertex
void gpu_discard_fragment()
bool rayQueryProceed(rayQuery) RET
VecBase< int, D > floatBitsToInt(VecOp< float, D >) RET
const uint gpu_RayFlagsTerminateOnFirstHit
VecBase< float, D > intBitsToFloat(VecOp< int, D >) RET
uint rayQueryGetIntersectionType(rayQuery, bool) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
void rayQueryInitialize(rayQuery, accelerationStructure, uint, uint, float3, float, float3, float)
const uint gpu_RayQueryCommittedIntersectionNone
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
void fullscreen_vertex(int vertex_id, float4 &out_position)
#define N
void vert(const int &vert_id, float4 &out_pos)
PipelineGraphic raytrace(vert, frag)
void frag(const float4 &frag_coord, Resources &srt, const draw::View &views)
float3 normal_decode(float4 enc)
bool is_perspective() const
float3 point_screen_to_world(float3 ssP) const
float z_distance(float3 P) const