Files
goo-engine/source/blender/draw/intern/shaders/goo_common_view_lib.glsl
T
codywinch 94e5855779 Cleanup: Remove old unused overlay functions
These functions were separated so no need to keep them for goo
2025-12-30 02:41:44 -06:00

188 lines
5.5 KiB
GLSL

/* SPDX-FileCopyrightText: 2018-2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/* WORKAROUND: to guard against double include in EEVEE. */
#ifndef GOO_COMMON_VIEW_LIB_GLSL
#define GOO_COMMON_VIEW_LIB_GLSL
#ifndef DRW_RESOURCE_CHUNK_LEN
# error Missing draw_view additional create info on shader create info
#endif
/* Not supported anymore. TODO(fclem): Add back support. */
// #define IS_DEBUG_MOUSE_FRAGMENT (ivec2(gl_FragCoord) == drw_view.mouse_pixel)
#define IS_FIRST_INVOCATION (gl_GlobalInvocationID == uvec3(0))
#define cameraForward ViewMatrixInverse[2].xyz
#define cameraPos ViewMatrixInverse[3].xyz
vec3 cameraVec(vec3 P)
{
return ((ProjectionMatrix[3][3] == 0.0) ? normalize(cameraPos - P) : cameraForward);
}
#define viewCameraVec(vP) ((ProjectionMatrix[3][3] == 0.0) ? normalize(-vP) : vec3(0.0, 0.0, 1.0))
/* Temporary until we fully make the switch. */
#ifndef USE_GPU_SHADER_CREATE_INFO
uniform int drw_resourceChunk;
#endif /* !USE_GPU_SHADER_CREATE_INFO */
/* Breaking this across multiple lines causes issues for some older GLSL compilers. */
/* clang-format off */
#if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && (!defined(OS_MAC) || defined(GPU_METAL)) && !defined(INSTANCED_ATTR) && !defined(DRW_LEGACY_MODEL_MATRIX)
/* clang-format on */
/* Temporary until we fully make the switch. */
# ifndef DRW_SHADER_SHARED_H
struct ObjectMatrices {
mat4 model;
mat4 model_inverse;
};
# endif /* !DRW_SHADER_SHARED_H */
# ifndef USE_GPU_SHADER_CREATE_INFO
layout(std140) uniform modelBlock
{
ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN];
};
# define ModelMatrix (drw_matrices[resource_id].model)
# define ModelMatrixInverse (drw_matrices[resource_id].model_inverse)
# endif /* USE_GPU_SHADER_CREATE_INFO */
#else /* GPU_INTEL */
/* Temporary until we fully make the switch. */
# ifndef USE_GPU_SHADER_CREATE_INFO
/* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage.
* So for now we just force using the legacy path. */
/* Note that this is also a workaround of a problem on OSX (AMD or NVIDIA)
* and older AMD driver on windows. */
uniform mat4 ModelMatrix;
uniform mat4 ModelMatrixInverse;
# endif /* !USE_GPU_SHADER_CREATE_INFO */
#endif
/* Temporary until we fully make the switch. */
#ifndef USE_GPU_SHADER_CREATE_INFO
# define resource_handle (drw_resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id)
#endif
/** Transform shortcuts. */
/* Rule of thumb: Try to reuse world positions and normals because converting through view-space
* will always be decomposed in at least 2 matrix operation. */
/**
* Some clarification:
* Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix))
*
* But since it is slow to multiply matrices we decompose it. Decomposing
* inversion and transposition both invert the product order leaving us with
* the same original order:
* transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse)
*
* Knowing that the view matrix is orthogonal, the transpose is also the inverse.
* NOTE: This is only valid because we are only using the mat3 of the ViewMatrixInverse.
* ViewMatrix * transpose(ModelMatrixInverse)
*/
#define NormalMatrix transpose(mat3(ModelMatrixInverse))
#define NormalMatrixInverse transpose(mat3(ModelMatrix))
#define normal_object_to_world(n) (NormalMatrix * n)
#define normal_world_to_view(n) (mat3(ViewMatrix) * n)
#define normal_view_to_world(n) (mat3(ViewMatrixInverse) * n)
#define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz)
#define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz)
vec3 point_view_to_world(vec3 p)
{
return (ViewMatrixInverse * vec4(p, 1.0)).xyz;
}
vec4 point_world_to_ndc(vec3 p)
{
return ProjectionMatrix * (ViewMatrix * vec4(p, 1.0));
}
vec3 point_world_to_view(vec3 p)
{
return (ViewMatrix * vec4(p, 1.0)).xyz;
}
/* Due to some shader compiler bug, we somewhat need to access gl_VertexID
* to make vertex shaders work. even if it's actually dead code. */
#if defined(GPU_INTEL) && defined(GPU_OPENGL)
# define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID);
#else
# define GPU_INTEL_VERTEX_SHADER_WORKAROUND
#endif
/* ---- Opengl Depth conversion ---- */
float linear_depth(bool is_persp, float z, float zf, float zn)
{
if (is_persp) {
return (zn * zf) / (z * (zn - zf) + zf);
}
else {
return (z * 2.0 - 1.0) * zf;
}
}
float buffer_depth(bool is_persp, float z, float zf, float zn)
{
if (is_persp) {
return (zf * (zn - z)) / (z * (zn - zf));
}
else {
return (z / (zf * 2.0)) + 0.5;
}
}
float get_view_z_from_depth(float depth)
{
float d = 2.0 * depth - 1.0;
if (ProjectionMatrix[3][3] == 0.0) {
d = -ProjectionMatrix[3][2] / (d + ProjectionMatrix[2][2]);
}
else {
d = (d - ProjectionMatrix[3][2]) / ProjectionMatrix[2][2];
}
return d;
}
float get_depth_from_view_z(float z)
{
float d;
if (ProjectionMatrix[3][3] == 0.0) {
d = (-ProjectionMatrix[3][2] / z) - ProjectionMatrix[2][2];
}
else {
d = ProjectionMatrix[2][2] * z + ProjectionMatrix[3][2];
}
return d * 0.5 + 0.5;
}
vec2 get_uvs_from_view(vec3 view)
{
vec4 ndc = ProjectionMatrix * vec4(view, 1.0);
return (ndc.xy / ndc.w) * 0.5 + 0.5;
}
vec3 get_view_space_from_depth(vec2 uvcoords, float depth)
{
vec3 ndc = vec3(uvcoords, depth) * 2.0 - 1.0;
vec4 p = ProjectionMatrixInverse * vec4(ndc, 1.0);
return p.xyz / p.w;
}
vec3 get_world_space_from_depth(vec2 uvcoords, float depth)
{
return (ViewMatrixInverse * vec4(get_view_space_from_depth(uvcoords, depth), 1.0)).xyz;
}
#endif /* GOO_COMMON_VIEW_LIB_GLSL */