R2 Core Shaders

R2ReconstructedSurface.h
Go to the documentation of this file.
1 #ifndef R2_RECONSTRUCTED_SURFACE_H
2 #define R2_RECONSTRUCTED_SURFACE_H
3
4 /// \file R2ReconstructedSurface.h
5 /// \brief Surface data reconstructed from the G-Buffer
6
7 #include "R2GBufferInput.h"
8 #include "R2LogDepth.h"
9 #include "R2Normals.h"
10 #include "R2PositionReconstruction.h"
11 #include "R2Viewport.h"
12 #include "R2ViewRays.h"
13
14 /// Reconstructed surface data, taken from the G-Buffer
15
16 struct R2_reconstructed_surface_t {
17 /// The sampled surface albedo
18 vec3 albedo;
19 /// The sampled surface emission level
20 float emission;
21
22 /// The sampled surface specular color
23 vec3 specular;
24 /// The sampled surface specular exponent in the range `[0, 256]`
25 float specular_exponent;
26
27 /// The eye-space normal vector of the surface
28 vec3 normal;
29 /// The eye-space position of the surface
30 vec4 position;
31 /// The UV coordinates of the current screen fragment
32 vec2 uv;
33 };
34
35 ///
36 /// Perform a full reconstruction of the surface in the given G-Buffer.
37 ///
38 /// The implementation of this function is affected by the following preprocessor
39 /// defines:
40 ///
41 /// If `R2_RECONSTRUCT_DIFFUSE_ONLY` is defined, the function only samples those parts
42 /// of the geometry buffer that are required for diffuse lighting (specifically depth
43 /// and normals).
44 ///
45 /// If `R2_RECONSTRUCT_DIFFUSE_SPECULAR_ONLY` is defined, the function only samples
46 /// those parts of the geometry buffer that are required for diffuse lighting
47 /// (specifically depth, normals, and specular).
48 ///
49 /// Otherwise, the surface is fully reconstructed.
50 ///
51 /// @param gbuffer The G-Buffer that will be sampled
52 /// @param viewport The current viewport
53 /// @param view_rays The current view rays
54 /// @param screen_position The current screen position (typically `gl_FragCoord.xy`).
55 ///
56 /// @return The fully reconstructed surface
57 ///
58
59 R2_reconstructed_surface_t
60 R2_deferredSurfaceReconstruct(
61 const R2_gbuffer_input_t gbuffer,
62 const R2_viewport_t viewport,
63 const R2_view_rays_t view_rays,
64 const float depth_coefficient,
65 const vec2 screen_position)
66 {
67 // Get the current screen coordinates in UV coordinate form
68 vec2 screen_uv =
69 R2_viewportFragmentPositionToUV (viewport, screen_position);
70
71 // Reconstruct the eye-space Z from the depth texture
72 float log_depth =
73 texture (gbuffer.depth, screen_uv).x;
74 float eye_z_positive =
75 R2_logDepthDecode (log_depth, depth_coefficient);
76 float eye_z =
77 -eye_z_positive;
78
79 // Reconstruct the full eye-space position
80 vec4 position_eye =
81 R2_positionReconstructFromEyeZ (eye_z, screen_uv, view_rays);
82
83 #if defined(R2_RECONSTRUCT_DIFFUSE_ONLY) || defined(R2_RECONSTRUCT_DIFFUSE_SPECULAR_ONLY)
84 vec3 albedo = vec3 (0.0);
85 float emission = 0.0;
86 #else
87 // Sample albedo/emission
88 vec4 albedo_raw =
89 texture (gbuffer.albedo, screen_uv);
90 vec3 albedo =
91 albedo_raw.rgb;
92 float emission =
93 albedo_raw.a;
94 #endif
95
96 // Sample normals
97 vec2 normal_compressed =
98 texture (gbuffer.normal, screen_uv).xy;
99 vec3 normal =
100 R2_normalsDecompress (normal_compressed);
101
102 #if !defined(R2_RECONSTRUCT_DIFFUSE_ONLY)
103 // Sample specular
104 vec4 specular_raw =
105 texture (gbuffer.specular, screen_uv);
106 vec3 specular_color =
107 specular_raw.rgb;
108 float specular_exponent =
109 specular_raw.a * 256.0;
110 #else
111 vec3 specular_color = vec3 (0.0);
112 float specular_exponent = 0.0;
113 #endif
114
115 // Construct surface data
116 R2_reconstructed_surface_t surface =
117 R2_reconstructed_surface_t (
118 albedo,
119 emission,
120 specular_color,
121 specular_exponent,
122 normal,
123 position_eye,
124 screen_uv
125 );
126
127 return surface;
128 }
129
130 #endif // R2_RECONSTRUCTED_SURFACE_H
R2ViewRays.h
View ray types.
R2_gbuffer_input_t::normal
sampler2D normal
The normal texture containing compressed normals (see R2_normalsDecompress).
Definition: R2GBufferInput.h:13
R2Normals.h
Functions for transforming normal vectors.
R2_reconstructed_surface_t::position
vec4 position
The eye-space position of the surface.
Definition: R2ReconstructedSurface.h:30
R2_gbuffer_input_t::albedo
sampler2D albedo
The albedo texture, with the emission level stored in the a component.
Definition: R2GBufferInput.h:11
R2Viewport.h
Viewport types and functions.
R2_reconstructed_surface_t::uv
vec2 uv
The UV coordinates of the current screen fragment.
Definition: R2ReconstructedSurface.h:32
R2_reconstructed_surface_t::specular_exponent
float specular_exponent
The sampled surface specular exponent in the range [0, 256]
Definition: R2ReconstructedSurface.h:25
R2_viewport_t
The type of viewports.
Definition: R2Viewport.h:9
R2_reconstructed_surface_t
Reconstructed surface data, taken from the G-Buffer.
Definition: R2ReconstructedSurface.h:16
R2_reconstructed_surface_t::emission
float emission
The sampled surface emission level.
Definition: R2ReconstructedSurface.h:20
R2_gbuffer_input_t::specular
sampler2D specular
The specular texture, with the exponent stored in the a component.
Definition: R2GBufferInput.h:15
R2_reconstructed_surface_t::albedo
vec3 albedo
The sampled surface albedo.
Definition: R2ReconstructedSurface.h:18
R2_gbuffer_input_t::depth
sampler2D depth
The logarithmic depth texture (see R2_logDepthDecode).
Definition: R2GBufferInput.h:17
R2_reconstructed_surface_t::normal
vec3 normal
The eye-space normal vector of the surface.
Definition: R2ReconstructedSurface.h:28
R2_logDepthDecode
float R2_logDepthDecode(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:72
R2GBufferInput.h
The textures that make up the G-Buffer.
R2_reconstructed_surface_t::specular
vec3 specular
The sampled surface specular color.
Definition: R2ReconstructedSurface.h:23
R2_gbuffer_input_t
A type representing the set of bound textures that make up the G-Buffer.
Definition: R2GBufferInput.h:9
R2_normalsDecompress
vec3 R2_normalsDecompress(const vec2 n)
Definition: R2Normals.h:34
R2_viewportFragmentPositionToUV
vec2 R2_viewportFragmentPositionToUV(const R2_viewport_t v, const vec2 f_pos)
Definition: R2Viewport.h:24
R2_view_rays_t
The type of view rays used to reconstruct positions during deferred rendering.
Definition: R2ViewRays.h:9
R2LogDepth.h
Logarithmic depth functions.
R2_positionReconstructFromEyeZ
vec4 R2_positionReconstructFromEyeZ(const float eye_z, const vec2 uv, const R2_view_rays_t view_rays)
Definition: R2PositionReconstruction.h:19
R2PositionReconstruction.h
Functions for performing position reconstruction during deferred rendering.
R2_deferredSurfaceReconstruct
R2_reconstructed_surface_t R2_deferredSurfaceReconstruct(const R2_gbuffer_input_t gbuffer, const R2_viewport_t viewport, const R2_view_rays_t view_rays, const float depth_coefficient, const vec2 screen_position)
Definition: R2ReconstructedSurface.h:60