Workbench: Replace viewvecs caculation by DRWView

This commit is contained in:
Clément Foucault 2020-07-15 16:41:30 +02:00
parent cd8f3c9ee7
commit 47885abbe6
8 changed files with 12 additions and 55 deletions

View File

@ -28,7 +28,7 @@ void cavity_compute(vec2 screenco,
return;
}
vec3 position = view_position_from_depth(screenco, depth, world_data.viewvecs, ProjectionMatrix);
vec3 position = view_position_from_depth(screenco, depth, ViewVecs, ProjectionMatrix);
vec3 normal = workbench_normal_decode(texture(normalBuffer, screenco));
vec2 jitter_co = (screenco * world_data.viewport_size.xy) * world_data.cavity_jitter_scale;
@ -68,8 +68,7 @@ void cavity_compute(vec2 screenco,
bool is_background = (s_depth == 1.0);
/* This trick provide good edge effect even if no neighbor is found. */
s_depth = (is_background) ? depth : s_depth;
vec3 s_pos = view_position_from_depth(
uvcoords, s_depth, world_data.viewvecs, ProjectionMatrix);
vec3 s_pos = view_position_from_depth(uvcoords, s_depth, ViewVecs, ProjectionMatrix);
if (is_background) {
s_pos.z -= world_data.cavity_distance;

View File

@ -68,17 +68,17 @@ void workbench_float_pair_decode(float data, out float v1, out float v2)
v2 = float(idata >> int(ROUGHNESS_BITS)) * (1.0 / float(v2_mask));
}
vec3 view_vector_from_screen_uv(vec2 uv, vec4 viewvecs[3], mat4 proj_mat)
vec3 view_vector_from_screen_uv(vec2 uv, vec4 viewvecs[2], mat4 proj_mat)
{
if (proj_mat[3][3] == 0.0) {
return normalize(viewvecs[0].xyz + vec3(uv, 0.0) * viewvecs[1].xyz);
return normalize(vec3(viewvecs[0].xy + uv * viewvecs[1].xy, 1.0));
}
else {
return vec3(0.0, 0.0, 1.0);
}
}
vec3 view_position_from_depth(vec2 uvcoords, float depth, vec4 viewvecs[3], mat4 proj_mat)
vec3 view_position_from_depth(vec2 uvcoords, float depth, vec4 viewvecs[2], mat4 proj_mat)
{
if (proj_mat[3][3] == 0.0) {
/* Perspective */
@ -86,7 +86,7 @@ vec3 view_position_from_depth(vec2 uvcoords, float depth, vec4 viewvecs[3], mat4
float zview = -proj_mat[3][2] / (d + proj_mat[2][2]);
return zview * (viewvecs[0].xyz + vec3(uvcoords, 0.0) * viewvecs[1].xyz);
return zview * vec3(viewvecs[0].xy + uvcoords * viewvecs[1].xy, 1.0);
}
else {
/* Orthographic */

View File

@ -14,7 +14,7 @@ out vec4 fragColor;
void main()
{
/* Normal and Incident vector are in viewspace. Lighting is evaluated in viewspace. */
vec3 I = view_vector_from_screen_uv(uvcoordsvar.st, world_data.viewvecs, ProjectionMatrix);
vec3 I = view_vector_from_screen_uv(uvcoordsvar.st, ViewVecs, ProjectionMatrix);
vec3 N = workbench_normal_decode(texture(normalBuffer, uvcoordsvar.st));
vec4 mat_data = texture(materialBuffer, uvcoordsvar.st);

View File

@ -5,7 +5,6 @@ struct LightData {
};
struct WorldData {
vec4 viewvecs[3];
vec4 viewport_size;
vec4 object_outline_color;
vec4 shadow_direction_vs;

View File

@ -15,7 +15,7 @@ layout(location = 1) out vec4 revealageAccum;
layout(location = 2) out uint objectId;
/* Special function only to be used with calculate_transparent_weight(). */
float linear_zdepth(float depth, vec4 viewvecs[3], mat4 proj_mat)
float linear_zdepth(float depth, vec4 viewvecs[2], mat4 proj_mat)
{
if (proj_mat[3][3] == 0.0) {
float d = 2.0 * depth - 1.0;
@ -33,7 +33,7 @@ float linear_zdepth(float depth, vec4 viewvecs[3], mat4 proj_mat)
*/
float calculate_transparent_weight(void)
{
float z = linear_zdepth(gl_FragCoord.z, world_data.viewvecs, ProjectionMatrix);
float z = linear_zdepth(gl_FragCoord.z, ViewVecs, ProjectionMatrix);
#if 0
/* Eq 10 : Good for surfaces with varying opacity (like particles) */
float a = min(1.0, alpha * 10.0) + 0.01;
@ -57,7 +57,7 @@ void main()
{
/* Normal and Incident vector are in viewspace. Lighting is evaluated in viewspace. */
vec2 uv_viewport = gl_FragCoord.xy * world_data.viewport_size_inv;
vec3 I = view_vector_from_screen_uv(uv_viewport, world_data.viewvecs, ProjectionMatrix);
vec3 I = view_vector_from_screen_uv(uv_viewport, ViewVecs, ProjectionMatrix);
vec3 N = normalize(normal_interp);
vec3 color = color_interp;

View File

@ -194,10 +194,8 @@ void main()
float depth = texelFetch(depthBuffer, ivec2(gl_FragCoord.xy), 0).r;
float depth_end = min(depth, gl_FragCoord.z);
vec3 vs_ray_end = view_position_from_depth(
screen_uv, depth_end, world_data.viewvecs, ProjectionMatrix);
vec3 vs_ray_ori = view_position_from_depth(
screen_uv, 0.0, world_data.viewvecs, ProjectionMatrix);
vec3 vs_ray_end = view_position_from_depth(screen_uv, depth_end, ViewVecs, ProjectionMatrix);
vec3 vs_ray_ori = view_position_from_depth(screen_uv, 0.0, ViewVecs, ProjectionMatrix);
vec3 vs_ray_dir = (is_persp) ? (vs_ray_end - vs_ray_ori) : vec3(0.0, 0.0, -1.0);
vs_ray_dir /= abs(vs_ray_dir.z);

View File

@ -86,43 +86,6 @@ static WORKBENCH_ViewLayerData *workbench_view_layer_data_ensure_ex(struct ViewL
/* \} */
static void workbench_viewvecs_update(float r_viewvecs[3][4])
{
float invproj[4][4];
const bool is_persp = DRW_view_is_persp_get(NULL);
DRW_view_winmat_get(NULL, invproj, true);
/* view vectors for the corners of the view frustum.
* Can be used to recreate the world space position easily */
copy_v4_fl4(r_viewvecs[0], -1.0f, -1.0f, -1.0f, 1.0f);
copy_v4_fl4(r_viewvecs[1], 1.0f, -1.0f, -1.0f, 1.0f);
copy_v4_fl4(r_viewvecs[2], -1.0f, 1.0f, -1.0f, 1.0f);
/* convert the view vectors to view space */
for (int i = 0; i < 3; i++) {
mul_m4_v4(invproj, r_viewvecs[i]);
/* normalized trick see:
* http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer */
mul_v3_fl(r_viewvecs[i], 1.0f / r_viewvecs[i][3]);
if (is_persp) {
mul_v3_fl(r_viewvecs[i], 1.0f / r_viewvecs[i][2]);
}
r_viewvecs[i][3] = 1.0;
}
/* we need to store the differences */
r_viewvecs[1][0] -= r_viewvecs[0][0];
r_viewvecs[1][1] = r_viewvecs[2][1] - r_viewvecs[0][1];
/* calculate a depth offset as well */
if (!is_persp) {
float vec_far[] = {-1.0f, -1.0f, 1.0f, 1.0f};
mul_m4_v4(invproj, vec_far);
mul_v3_fl(vec_far, 1.0f / vec_far[3]);
r_viewvecs[1][2] = vec_far[2] - r_viewvecs[0][2];
}
}
static void workbench_studiolight_data_update(WORKBENCH_PrivateData *wpd, WORKBENCH_UBO_World *wd)
{
StudioLight *studiolight = wpd->studio_light;
@ -311,7 +274,6 @@ void workbench_update_world_ubo(WORKBENCH_PrivateData *wpd)
workbench_studiolight_data_update(wpd, &wd);
workbench_shadow_data_update(wpd, &wd);
workbench_cavity_data_update(wpd, &wd);
workbench_viewvecs_update(wd.viewvecs);
DRW_uniformbuffer_update(wpd->world_ubo, &wd);
}

View File

@ -168,7 +168,6 @@ typedef struct WORKBENCH_UBO_Material {
} WORKBENCH_UBO_Material;
typedef struct WORKBENCH_UBO_World {
float viewvecs[3][4];
float viewport_size[2], viewport_size_inv[2];
float object_outline_color[4];
float shadow_direction_vs[4];