R2 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"
11 #include "R2Viewport.h"
12 #include "R2ViewRays.h"
13 
14 /// Reconstructed surface data, taken from the G-Buffer
15 
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]`
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 
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
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
View ray types.
sampler2D normal
The normal texture containing compressed normals (see R2_normalsDecompress).
Functions for transforming normal vectors.
vec4 position
The eye-space position of the surface.
sampler2D albedo
The albedo texture, with the emission level stored in the a component.
Viewport types and functions.
vec2 uv
The UV coordinates of the current screen fragment.
float specular_exponent
The sampled surface specular exponent in the range [0, 256]
The type of viewports.
Definition: R2Viewport.h:9
Reconstructed surface data, taken from the G-Buffer.
float emission
The sampled surface emission level.
sampler2D specular
The specular texture, with the exponent stored in the a component.
vec3 albedo
The sampled surface albedo.
sampler2D depth
The logarithmic depth texture (see R2_logDepthDecode).
vec3 normal
The eye-space normal vector of the surface.
float R2_logDepthDecode(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:72
The textures that make up the G-Buffer.
vec3 specular
The sampled surface specular color.
A type representing the set of bound textures that make up the G-Buffer.
Definition: R2GBufferInput.h:9
vec3 R2_normalsDecompress(const vec2 n)
Definition: R2Normals.h:34
vec2 R2_viewportFragmentPositionToUV(const R2_viewport_t v, const vec2 f_pos)
Definition: R2Viewport.h:24
The type of view rays used to reconstruct positions during deferred rendering.
Definition: R2ViewRays.h:9
Logarithmic depth functions.
vec4 R2_positionReconstructFromEyeZ(const float eye_z, const vec2 uv, const R2_view_rays_t view_rays)
Functions for performing position reconstruction during deferred rendering.
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)