diff options
-rw-r--r-- | source/blender/gpu/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/gpu/GPU_shader.h | 1 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader.c | 15 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_image_depth_linear_frag.glsl | 22 |
4 files changed, 39 insertions, 0 deletions
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index d8925b17924..0bb383af430 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -135,6 +135,7 @@ data_to_c_simple(shaders/gpu_shader_2D_smooth_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_2D_smooth_color_frag.glsl SRC) data_to_c_simple(shaders/gpu_shader_image_modulate_alpha_frag.glsl SRC) data_to_c_simple(shaders/gpu_shader_image_rect_modulate_alpha_frag.glsl SRC) +data_to_c_simple(shaders/gpu_shader_image_depth_linear_frag.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_image_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_flat_color_vert.glsl SRC) diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h index 7a47b5ae331..e19588a5b1b 100644 --- a/source/blender/gpu/GPU_shader.h +++ b/source/blender/gpu/GPU_shader.h @@ -104,6 +104,7 @@ typedef enum GPUBuiltinShader { /* basic image drawing */ GPU_SHADER_3D_IMAGE_MODULATE_ALPHA, GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA, + GPU_SHADER_3D_IMAGE_DEPTH, /* points */ GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR, GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_SMOOTH, diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c index db133fd7c29..f84b2bea99b 100644 --- a/source/blender/gpu/intern/gpu_shader.c +++ b/source/blender/gpu/intern/gpu_shader.c @@ -56,6 +56,7 @@ extern char datatoc_gpu_shader_2D_smooth_color_frag_glsl[]; extern char datatoc_gpu_shader_3D_image_vert_glsl[]; extern char datatoc_gpu_shader_image_modulate_alpha_frag_glsl[]; extern char datatoc_gpu_shader_image_rect_modulate_alpha_frag_glsl[]; +extern char datatoc_gpu_shader_image_depth_linear_frag_glsl[]; extern char datatoc_gpu_shader_3D_vert_glsl[]; extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[]; extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[]; @@ -109,6 +110,7 @@ static struct GPUShadersGlobal { /* for drawing images */ GPUShader *image_modulate_alpha_3D; GPUShader *image_rect_modulate_alpha_3D; + GPUShader *image_depth_3D; /* for simple 2D drawing */ GPUShader *uniform_color_2D; GPUShader *flat_color_2D; @@ -669,6 +671,14 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader) NULL, NULL, NULL, 0, 0, 0); retval = GG.shaders.image_rect_modulate_alpha_3D; break; + case GPU_SHADER_3D_IMAGE_DEPTH: + if (!GG.shaders.image_depth_3D) + GG.shaders.image_depth_3D = GPU_shader_create( + datatoc_gpu_shader_3D_image_vert_glsl, + datatoc_gpu_shader_image_depth_linear_frag_glsl, + NULL, NULL, NULL, 0, 0, 0); + retval = GG.shaders.image_depth_3D; + break; case GPU_SHADER_2D_UNIFORM_COLOR: if (!GG.shaders.uniform_color_2D) GG.shaders.uniform_color_2D = GPU_shader_create( @@ -937,6 +947,11 @@ void GPU_shader_free_builtin_shaders(void) GG.shaders.image_rect_modulate_alpha_3D = NULL; } + if (GG.shaders.image_depth_3D) { + GPU_shader_free(GG.shaders.image_depth_3D); + GG.shaders.image_depth_3D = NULL; + } + if (GG.shaders.uniform_color_2D) { GPU_shader_free(GG.shaders.uniform_color_2D); GG.shaders.uniform_color_2D = NULL; diff --git a/source/blender/gpu/shaders/gpu_shader_image_depth_linear_frag.glsl b/source/blender/gpu/shaders/gpu_shader_image_depth_linear_frag.glsl new file mode 100644 index 00000000000..7f76fbf03be --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_image_depth_linear_frag.glsl @@ -0,0 +1,22 @@ + +#if __VERSION__ == 120 + varying vec2 texCoord_interp; + #define fragColor gl_FragColor +#else + in vec2 texCoord_interp; + out vec4 fragColor; + #define texture2D texture +#endif + +uniform float znear; +uniform float zfar; +uniform sampler2D image; + +void main() +{ + float depth = texture2D(image, texCoord_interp).r; + + /* normalize */ + fragColor.rgb = vec3((2.0f * znear) / (zfar + znear - (depth * (zfar - znear)))); + fragColor.a = 1.0f; +} |