2015-12-06 21:20:19 +01:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2005 Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup gpu
|
2018-08-30 01:56:08 +10:00
|
|
|
*/
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "BLI_math_base.h"
|
|
|
|
#include "BLI_math_vector.h"
|
2017-09-27 18:03:00 +02:00
|
|
|
#include "BLI_path_util.h"
|
2018-08-02 18:31:38 +02:00
|
|
|
#include "BLI_string.h"
|
2019-02-06 09:15:16 +11:00
|
|
|
#include "BLI_string_utils.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2017-09-27 18:03:00 +02:00
|
|
|
#include "BKE_appdir.h"
|
2015-12-06 21:20:19 +01:00
|
|
|
#include "BKE_global.h"
|
|
|
|
|
2017-09-27 18:03:00 +02:00
|
|
|
#include "DNA_space_types.h"
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
#include "GPU_extensions.h"
|
2017-04-18 16:30:13 +02:00
|
|
|
#include "GPU_matrix.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "GPU_platform.h"
|
2015-12-06 21:20:19 +01:00
|
|
|
#include "GPU_shader.h"
|
|
|
|
#include "GPU_texture.h"
|
2017-04-18 16:30:13 +02:00
|
|
|
#include "GPU_uniformbuffer.h"
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2016-09-16 17:18:19 +02:00
|
|
|
#include "gpu_shader_private.h"
|
|
|
|
|
2017-04-07 13:28:42 -04:00
|
|
|
/* Adjust these constants as needed. */
|
|
|
|
#define MAX_DEFINE_LENGTH 256
|
2020-03-11 17:12:01 +01:00
|
|
|
#define MAX_EXT_DEFINE_LENGTH 512
|
2015-12-06 21:20:19 +01:00
|
|
|
|
|
|
|
/* Non-generated shaders */
|
2016-09-13 02:51:38 -04:00
|
|
|
extern char datatoc_gpu_shader_depth_only_frag_glsl[];
|
2016-08-16 14:58:53 -04:00
|
|
|
extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
|
2017-02-07 13:04:08 +01:00
|
|
|
extern char datatoc_gpu_shader_checker_frag_glsl[];
|
2017-02-16 21:49:20 -02:00
|
|
|
extern char datatoc_gpu_shader_diag_stripes_frag_glsl[];
|
2017-02-07 13:04:08 +01:00
|
|
|
extern char datatoc_gpu_shader_simple_lighting_frag_glsl[];
|
2016-08-16 14:58:53 -04:00
|
|
|
extern char datatoc_gpu_shader_flat_color_frag_glsl[];
|
2016-10-26 03:57:14 -04:00
|
|
|
extern char datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl[];
|
2018-04-17 13:00:09 +02:00
|
|
|
extern char datatoc_gpu_shader_flat_id_frag_glsl[];
|
2018-08-23 22:29:29 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_area_borders_vert_glsl[];
|
2018-12-01 19:49:31 +01:00
|
|
|
extern char datatoc_gpu_shader_2D_area_borders_frag_glsl[];
|
2016-10-15 02:30:59 -04:00
|
|
|
extern char datatoc_gpu_shader_2D_vert_glsl[];
|
2016-08-08 04:56:02 -04:00
|
|
|
extern char datatoc_gpu_shader_2D_flat_color_vert_glsl[];
|
2018-04-20 15:15:10 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_smooth_color_uniform_alpha_vert_glsl[];
|
2016-08-04 15:59:38 -04:00
|
|
|
extern char datatoc_gpu_shader_2D_smooth_color_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_2D_smooth_color_frag_glsl[];
|
2016-11-18 16:04:25 +01:00
|
|
|
extern char datatoc_gpu_shader_2D_image_vert_glsl[];
|
2018-03-27 23:57:29 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_image_rect_vert_glsl[];
|
2018-03-31 19:32:28 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_image_multi_rect_vert_glsl[];
|
2018-03-29 03:06:33 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_widget_base_vert_glsl[];
|
2018-04-06 23:54:24 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_widget_base_frag_glsl[];
|
2018-04-06 10:09:23 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_widget_shadow_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_2D_widget_shadow_frag_glsl[];
|
2018-04-05 15:27:53 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_nodelink_frag_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_2D_nodelink_vert_glsl[];
|
2016-11-18 16:04:25 +01:00
|
|
|
|
2016-10-18 00:08:34 -04:00
|
|
|
extern char datatoc_gpu_shader_3D_image_vert_glsl[];
|
2018-02-13 18:09:58 +01:00
|
|
|
extern char datatoc_gpu_shader_image_frag_glsl[];
|
2020-02-11 15:18:55 +01:00
|
|
|
extern char datatoc_gpu_shader_image_overlays_merge_frag_glsl[];
|
2020-03-19 08:06:49 +01:00
|
|
|
extern char datatoc_gpu_shader_image_overlays_stereo_merge_frag_glsl[];
|
2016-11-18 16:04:25 +01:00
|
|
|
extern char datatoc_gpu_shader_image_color_frag_glsl[];
|
2018-04-26 15:42:08 +02:00
|
|
|
extern char datatoc_gpu_shader_image_desaturate_frag_glsl[];
|
2018-03-31 19:32:28 +02:00
|
|
|
extern char datatoc_gpu_shader_image_varying_color_frag_glsl[];
|
2017-02-24 01:07:40 +01:00
|
|
|
extern char datatoc_gpu_shader_image_shuffle_color_frag_glsl[];
|
2016-10-15 02:30:59 -04:00
|
|
|
extern char datatoc_gpu_shader_3D_vert_glsl[];
|
2017-04-16 15:04:07 -04:00
|
|
|
extern char datatoc_gpu_shader_3D_normal_vert_glsl[];
|
2016-08-16 14:58:53 -04:00
|
|
|
extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[];
|
2020-04-23 23:05:39 +02:00
|
|
|
extern char datatoc_gpu_shader_3D_polyline_frag_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_3D_polyline_geom_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_3D_polyline_vert_glsl[];
|
2016-08-16 14:58:53 -04:00
|
|
|
extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
|
2017-02-11 21:15:46 +01:00
|
|
|
extern char datatoc_gpu_shader_3D_passthrough_vert_glsl[];
|
2017-04-05 01:28:48 +02:00
|
|
|
extern char datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl[];
|
2016-08-04 15:59:38 -04:00
|
|
|
|
2017-02-09 20:55:31 +01:00
|
|
|
extern char datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl[];
|
2017-02-07 11:20:15 +01:00
|
|
|
|
2016-09-30 20:44:22 -04:00
|
|
|
extern char datatoc_gpu_shader_point_uniform_color_frag_glsl[];
|
2017-02-28 02:18:52 -05:00
|
|
|
extern char datatoc_gpu_shader_point_uniform_color_aa_frag_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_point_varying_color_outline_aa_frag_glsl[];
|
2018-09-29 19:42:09 +02:00
|
|
|
extern char datatoc_gpu_shader_point_varying_color_varying_outline_aa_frag_glsl[];
|
2016-09-30 20:44:22 -04:00
|
|
|
extern char datatoc_gpu_shader_point_varying_color_frag_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl[];
|
2016-10-15 02:30:59 -04:00
|
|
|
extern char datatoc_gpu_shader_3D_point_varying_size_vert_glsl[];
|
2016-10-01 17:32:29 -04:00
|
|
|
extern char datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl[];
|
2017-02-28 02:18:52 -05:00
|
|
|
extern char datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_3D_point_uniform_size_outline_aa_vert_glsl[];
|
2016-10-06 16:31:49 -04:00
|
|
|
extern char datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl[];
|
2017-02-28 02:18:52 -05:00
|
|
|
extern char datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_vert_glsl[];
|
2016-09-30 20:44:22 -04:00
|
|
|
|
2018-09-29 19:42:09 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_edituvs_points_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_2D_edituvs_facedots_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_2D_edituvs_edges_vert_glsl[];
|
2020-06-22 17:30:45 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_edituvs_edges_frag_glsl[];
|
2018-09-29 19:42:09 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_edituvs_faces_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_2D_edituvs_stretch_vert_glsl[];
|
|
|
|
|
2017-07-13 16:44:02 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl[];
|
2017-02-24 15:33:09 -05:00
|
|
|
extern char datatoc_gpu_shader_2D_line_dashed_frag_glsl[];
|
2017-07-13 16:44:02 +02:00
|
|
|
extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl[];
|
2017-02-24 15:33:09 -05:00
|
|
|
|
2016-09-17 13:33:02 +02:00
|
|
|
extern char datatoc_gpu_shader_text_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_text_frag_glsl[];
|
2017-02-11 00:02:28 -05:00
|
|
|
extern char datatoc_gpu_shader_keyframe_diamond_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_keyframe_diamond_frag_glsl[];
|
2016-09-17 13:33:02 +02:00
|
|
|
|
2018-07-31 10:22:19 +02:00
|
|
|
extern char datatoc_gpu_shader_gpencil_stroke_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_gpencil_stroke_frag_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_gpencil_stroke_geom_glsl[];
|
|
|
|
|
|
|
|
extern char datatoc_gpu_shader_gpencil_fill_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_gpencil_fill_frag_glsl[];
|
2019-02-06 09:15:16 +11:00
|
|
|
extern char datatoc_gpu_shader_cfg_world_clip_lib_glsl[];
|
2018-07-31 10:22:19 +02:00
|
|
|
|
2020-04-14 20:44:45 +02:00
|
|
|
extern char datatoc_gpu_shader_colorspace_lib_glsl[];
|
|
|
|
|
2019-02-10 11:06:22 +11:00
|
|
|
const struct GPUShaderConfigData GPU_shader_cfg_data[GPU_SHADER_CFG_LEN] = {
|
2019-04-17 06:17:24 +02:00
|
|
|
[GPU_SHADER_CFG_DEFAULT] =
|
|
|
|
{
|
|
|
|
.lib = "",
|
2020-04-14 20:44:45 +02:00
|
|
|
.def = "#define blender_srgb_to_framebuffer_space(a) a\n",
|
2019-04-17 06:17:24 +02:00
|
|
|
},
|
|
|
|
[GPU_SHADER_CFG_CLIPPED] =
|
|
|
|
{
|
|
|
|
.lib = datatoc_gpu_shader_cfg_world_clip_lib_glsl,
|
2020-04-14 20:44:45 +02:00
|
|
|
.def = "#define USE_WORLD_CLIP_PLANES\n"
|
|
|
|
"#define blender_srgb_to_framebuffer_space(a) a\n",
|
2019-04-17 06:17:24 +02:00
|
|
|
},
|
2019-02-10 11:06:22 +11:00
|
|
|
};
|
|
|
|
|
2016-11-08 22:53:59 -06:00
|
|
|
/* cache of built-in shaders (each is created on first use) */
|
2019-03-16 19:48:28 +01:00
|
|
|
static GPUShader *builtin_shaders[GPU_SHADER_CFG_LEN][GPU_SHADER_BUILTIN_LEN] = {{NULL}};
|
2020-04-14 20:44:45 +02:00
|
|
|
static int g_shader_builtin_srgb_transform = 0;
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2018-08-03 14:34:05 +02:00
|
|
|
#ifndef NDEBUG
|
2018-08-02 18:31:38 +02:00
|
|
|
static uint g_shaderid = 0;
|
2018-08-03 14:34:05 +02:00
|
|
|
#endif
|
2018-08-02 18:31:38 +02:00
|
|
|
|
2016-11-08 22:53:59 -06:00
|
|
|
typedef struct {
|
2019-04-17 06:17:24 +02:00
|
|
|
const char *vert;
|
|
|
|
/** Optional. */
|
|
|
|
const char *geom;
|
|
|
|
const char *frag;
|
|
|
|
/** Optional. */
|
|
|
|
const char *defs;
|
2016-11-08 22:53:59 -06:00
|
|
|
} GPUShaderStages;
|
2015-12-06 21:20:19 +01:00
|
|
|
|
|
|
|
static void shader_print_errors(const char *task, const char *log, const char **code, int totcode)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
int line = 1;
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
fprintf(stderr, "GPUShader: %s error:\n", task);
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
for (int i = 0; i < totcode; i++) {
|
|
|
|
const char *c, *pos, *end = code[i] + strlen(code[i]);
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (G.debug & G_DEBUG) {
|
|
|
|
fprintf(stderr, "===== shader string %d ====\n", i + 1);
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
c = code[i];
|
|
|
|
while ((c < end) && (pos = strchr(c, '\n'))) {
|
|
|
|
fprintf(stderr, "%2d ", line);
|
|
|
|
fwrite(c, (pos + 1) - c, 1, stderr);
|
|
|
|
c = pos + 1;
|
|
|
|
line++;
|
|
|
|
}
|
2018-06-04 09:09:12 +02:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
fprintf(stderr, "%s", c);
|
|
|
|
}
|
|
|
|
}
|
2018-06-04 09:09:12 +02:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
fprintf(stderr, "%s\n", log);
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *gpu_shader_version(void)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return "#version 330\n";
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2017-04-08 02:21:13 -04:00
|
|
|
static void gpu_shader_standard_extensions(char defines[MAX_EXT_DEFINE_LENGTH])
|
2015-12-06 21:20:19 +01:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
/* enable extensions for features that are not part of our base GLSL version
|
|
|
|
* don't use an extension for something already available!
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (GLEW_ARB_texture_gather) {
|
|
|
|
/* There is a bug on older Nvidia GPU where GL_ARB_texture_gather
|
|
|
|
* is reported to be supported but yield a compile error (see T55802). */
|
|
|
|
if (!GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY) || GLEW_VERSION_4_0) {
|
|
|
|
strcat(defines, "#extension GL_ARB_texture_gather: enable\n");
|
|
|
|
|
|
|
|
/* Some drivers don't agree on GLEW_ARB_texture_gather and the actual support in the
|
|
|
|
* shader so double check the preprocessor define (see T56544). */
|
|
|
|
if (!GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY) && !GLEW_VERSION_4_0) {
|
|
|
|
strcat(defines, "#ifdef GL_ARB_texture_gather\n");
|
|
|
|
strcat(defines, "# define GPU_ARB_texture_gather\n");
|
|
|
|
strcat(defines, "#endif\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcat(defines, "#define GPU_ARB_texture_gather\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (GLEW_ARB_texture_query_lod) {
|
|
|
|
/* a #version 400 feature, but we use #version 330 maximum so use extension */
|
|
|
|
strcat(defines, "#extension GL_ARB_texture_query_lod: enable\n");
|
|
|
|
}
|
2019-05-31 01:45:41 +02:00
|
|
|
if (GLEW_ARB_shader_draw_parameters) {
|
|
|
|
strcat(defines, "#extension GL_ARB_shader_draw_parameters : enable\n");
|
|
|
|
}
|
2020-03-11 17:12:01 +01:00
|
|
|
if (GPU_arb_texture_cube_map_array_is_supported()) {
|
|
|
|
strcat(defines, "#extension GL_ARB_texture_cube_map_array : enable\n");
|
|
|
|
strcat(defines, "#define GPU_ARB_texture_cube_map_array\n");
|
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 14:30:28 +02:00
|
|
|
static void gpu_shader_standard_defines(char defines[MAX_DEFINE_LENGTH])
|
2015-12-06 21:20:19 +01:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
/* some useful defines to detect GPU type */
|
2019-04-22 09:32:37 +10:00
|
|
|
if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)) {
|
2019-04-17 06:17:24 +02:00
|
|
|
strcat(defines, "#define GPU_ATI\n");
|
2019-09-19 11:17:50 -03:00
|
|
|
if (GPU_crappy_amd_driver()) {
|
2019-12-05 12:02:24 +01:00
|
|
|
strcat(defines, "#define GPU_DEPRECATED_AMD_DRIVER\n");
|
2019-09-19 11:17:50 -03:00
|
|
|
}
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
else if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY)) {
|
2019-04-17 06:17:24 +02:00
|
|
|
strcat(defines, "#define GPU_NVIDIA\n");
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
else if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY)) {
|
2019-04-17 06:17:24 +02:00
|
|
|
strcat(defines, "#define GPU_INTEL\n");
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
/* some useful defines to detect OS type */
|
2019-04-22 09:32:37 +10:00
|
|
|
if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_WIN, GPU_DRIVER_ANY)) {
|
2019-04-17 06:17:24 +02:00
|
|
|
strcat(defines, "#define OS_WIN\n");
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
else if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_MAC, GPU_DRIVER_ANY)) {
|
2019-04-17 06:17:24 +02:00
|
|
|
strcat(defines, "#define OS_MAC\n");
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
else if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_UNIX, GPU_DRIVER_ANY)) {
|
2019-04-17 06:17:24 +02:00
|
|
|
strcat(defines, "#define OS_UNIX\n");
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-06 19:02:15 +02:00
|
|
|
float derivatives_factors[2];
|
|
|
|
GPU_get_dfdy_factors(derivatives_factors);
|
|
|
|
if (derivatives_factors[0] == 1.0f) {
|
|
|
|
strcat(defines, "#define DFDX_SIGN 1.0\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcat(defines, "#define DFDX_SIGN -1.0\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (derivatives_factors[1] == 1.0f) {
|
|
|
|
strcat(defines, "#define DFDY_SIGN 1.0\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcat(defines, "#define DFDY_SIGN -1.0\n");
|
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
GPUShader *GPU_shader_create(const char *vertexcode,
|
|
|
|
const char *fragcode,
|
|
|
|
const char *geocode,
|
|
|
|
const char *libcode,
|
|
|
|
const char *defines,
|
|
|
|
const char *shname)
|
2015-12-06 21:20:19 +01:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return GPU_shader_create_ex(
|
|
|
|
vertexcode, fragcode, geocode, libcode, defines, GPU_SHADER_TFB_NONE, NULL, 0, shname);
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-14 20:44:45 +02:00
|
|
|
GPUShader *GPU_shader_create_from_python(const char *vertexcode,
|
|
|
|
const char *fragcode,
|
|
|
|
const char *geocode,
|
|
|
|
const char *libcode,
|
|
|
|
const char *defines)
|
|
|
|
{
|
|
|
|
char *libcodecat = NULL;
|
|
|
|
|
|
|
|
if (libcode == NULL) {
|
|
|
|
libcode = datatoc_gpu_shader_colorspace_lib_glsl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
libcode = libcodecat = BLI_strdupcat(libcode, datatoc_gpu_shader_colorspace_lib_glsl);
|
|
|
|
}
|
|
|
|
|
|
|
|
GPUShader *sh = GPU_shader_create_ex(
|
|
|
|
vertexcode, fragcode, geocode, libcode, defines, GPU_SHADER_TFB_NONE, NULL, 0, NULL);
|
|
|
|
|
|
|
|
MEM_SAFE_FREE(libcodecat);
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
|
2019-06-05 13:06:11 -03:00
|
|
|
GPUShader *GPU_shader_load_from_binary(const char *binary,
|
|
|
|
const int binary_format,
|
|
|
|
const int binary_len,
|
|
|
|
const char *shname)
|
|
|
|
{
|
|
|
|
BLI_assert(GL_ARB_get_program_binary);
|
|
|
|
int success;
|
|
|
|
int program = glCreateProgram();
|
|
|
|
|
|
|
|
glProgramBinary(program, binary_format, binary, binary_len);
|
|
|
|
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
|
|
|
|
|
|
|
if (success) {
|
2020-06-02 17:29:48 +02:00
|
|
|
glUseProgram(program);
|
|
|
|
|
2019-06-05 13:06:11 -03:00
|
|
|
GPUShader *shader = MEM_callocN(sizeof(*shader), __func__);
|
|
|
|
shader->interface = GPU_shaderinterface_create(program);
|
|
|
|
shader->program = program;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
BLI_snprintf(shader->name, sizeof(shader->name), "%s_%u", shname, g_shaderid++);
|
|
|
|
#else
|
|
|
|
UNUSED_VARS(shname);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
glDeleteProgram(program);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-27 18:03:00 +02:00
|
|
|
#define DEBUG_SHADER_NONE ""
|
|
|
|
#define DEBUG_SHADER_VERTEX "vert"
|
|
|
|
#define DEBUG_SHADER_FRAGMENT "frag"
|
|
|
|
#define DEBUG_SHADER_GEOMETRY "geom"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump GLSL shaders to disk
|
|
|
|
*
|
|
|
|
* This is used for profiling shader performance externally and debug if shader code is correct.
|
|
|
|
* If called with no code, it simply bumps the shader index, so different shaders for the same
|
|
|
|
* program share the same index.
|
|
|
|
*/
|
|
|
|
static void gpu_dump_shaders(const char **code, const int num_shaders, const char *extension)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
if ((G.debug & G_DEBUG_GPU_SHADERS) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We use the same shader index for shaders in the same program.
|
|
|
|
* So we call this function once before calling for the individual shaders. */
|
|
|
|
static int shader_index = 0;
|
|
|
|
if (code == NULL) {
|
|
|
|
shader_index++;
|
|
|
|
BLI_assert(STREQ(DEBUG_SHADER_NONE, extension));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the full path of the new shader. */
|
|
|
|
char shader_path[FILE_MAX];
|
|
|
|
|
|
|
|
char file_name[512] = {'\0'};
|
|
|
|
sprintf(file_name, "%04d.%s", shader_index, extension);
|
|
|
|
|
|
|
|
BLI_join_dirfile(shader_path, sizeof(shader_path), BKE_tempdir_session(), file_name);
|
|
|
|
|
|
|
|
/* Write shader to disk. */
|
|
|
|
FILE *f = fopen(shader_path, "w");
|
|
|
|
if (f == NULL) {
|
|
|
|
printf("Error writing to file: %s\n", shader_path);
|
|
|
|
}
|
|
|
|
for (int j = 0; j < num_shaders; j++) {
|
|
|
|
fprintf(f, "%s", code[j]);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
printf("Shader file written to disk: %s\n", shader_path);
|
2017-09-27 18:03:00 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
GPUShader *GPU_shader_create_ex(const char *vertexcode,
|
|
|
|
const char *fragcode,
|
|
|
|
const char *geocode,
|
|
|
|
const char *libcode,
|
|
|
|
const char *defines,
|
|
|
|
const eGPUShaderTFBType tf_type,
|
|
|
|
const char **tf_names,
|
|
|
|
const int tf_count,
|
|
|
|
const char *shname)
|
2015-12-06 21:20:19 +01:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GLint status;
|
|
|
|
GLchar log[5000];
|
|
|
|
GLsizei length = 0;
|
|
|
|
GPUShader *shader;
|
|
|
|
char standard_defines[MAX_DEFINE_LENGTH] = "";
|
|
|
|
char standard_extensions[MAX_EXT_DEFINE_LENGTH] = "";
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
shader = MEM_callocN(sizeof(GPUShader), "GPUShader");
|
|
|
|
gpu_dump_shaders(NULL, 0, DEBUG_SHADER_NONE);
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2018-08-02 18:31:38 +02:00
|
|
|
#ifndef NDEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_snprintf(shader->name, sizeof(shader->name), "%s_%u", shname, g_shaderid++);
|
2018-08-02 18:31:38 +02:00
|
|
|
#else
|
2019-04-17 06:17:24 +02:00
|
|
|
UNUSED_VARS(shname);
|
2018-08-02 18:31:38 +02:00
|
|
|
#endif
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
/* At least a vertex shader and a fragment shader are required. */
|
|
|
|
BLI_assert((fragcode != NULL) && (vertexcode != NULL));
|
2018-12-07 04:07:56 +01:00
|
|
|
|
2019-04-22 09:32:37 +10:00
|
|
|
if (vertexcode) {
|
2019-04-17 06:17:24 +02:00
|
|
|
shader->vertex = glCreateShader(GL_VERTEX_SHADER);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
if (fragcode) {
|
2019-04-17 06:17:24 +02:00
|
|
|
shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
if (geocode) {
|
2019-04-17 06:17:24 +02:00
|
|
|
shader->geometry = glCreateShader(GL_GEOMETRY_SHADER);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
shader->program = glCreateProgram();
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (!shader->program || (vertexcode && !shader->vertex) || (fragcode && !shader->fragment) ||
|
|
|
|
(geocode && !shader->geometry)) {
|
|
|
|
fprintf(stderr, "GPUShader, object creation failed.\n");
|
|
|
|
GPU_shader_free(shader);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
gpu_shader_standard_defines(standard_defines);
|
|
|
|
gpu_shader_standard_extensions(standard_extensions);
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (vertexcode) {
|
|
|
|
const char *source[6];
|
|
|
|
/* custom limit, may be too small, beware */
|
|
|
|
int num_source = 0;
|
|
|
|
|
|
|
|
source[num_source++] = gpu_shader_version();
|
2020-07-15 14:59:52 +02:00
|
|
|
source[num_source++] =
|
|
|
|
"#define GPU_VERTEX_SHADER\n"
|
|
|
|
"#define IN_OUT out\n";
|
2019-04-17 06:17:24 +02:00
|
|
|
source[num_source++] = standard_extensions;
|
|
|
|
source[num_source++] = standard_defines;
|
|
|
|
|
2019-04-22 09:32:37 +10:00
|
|
|
if (defines) {
|
2019-04-17 06:17:24 +02:00
|
|
|
source[num_source++] = defines;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
source[num_source++] = vertexcode;
|
|
|
|
|
|
|
|
gpu_dump_shaders(source, num_source, DEBUG_SHADER_VERTEX);
|
|
|
|
|
|
|
|
glAttachShader(shader->program, shader->vertex);
|
|
|
|
glShaderSource(shader->vertex, num_source, source, NULL);
|
|
|
|
|
|
|
|
glCompileShader(shader->vertex);
|
|
|
|
glGetShaderiv(shader->vertex, GL_COMPILE_STATUS, &status);
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
glGetShaderInfoLog(shader->vertex, sizeof(log), &length, log);
|
|
|
|
shader_print_errors("compile", log, source, num_source);
|
|
|
|
|
|
|
|
GPU_shader_free(shader);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fragcode) {
|
|
|
|
const char *source[7];
|
|
|
|
int num_source = 0;
|
|
|
|
|
|
|
|
source[num_source++] = gpu_shader_version();
|
2020-07-15 14:59:52 +02:00
|
|
|
source[num_source++] =
|
|
|
|
"#define GPU_FRAGMENT_SHADER\n"
|
|
|
|
"#define IN_OUT in\n";
|
2019-04-17 06:17:24 +02:00
|
|
|
source[num_source++] = standard_extensions;
|
|
|
|
source[num_source++] = standard_defines;
|
|
|
|
|
2019-04-22 09:32:37 +10:00
|
|
|
if (defines) {
|
2019-04-17 06:17:24 +02:00
|
|
|
source[num_source++] = defines;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
if (libcode) {
|
2019-04-17 06:17:24 +02:00
|
|
|
source[num_source++] = libcode;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
source[num_source++] = fragcode;
|
|
|
|
|
|
|
|
gpu_dump_shaders(source, num_source, DEBUG_SHADER_FRAGMENT);
|
|
|
|
|
|
|
|
glAttachShader(shader->program, shader->fragment);
|
|
|
|
glShaderSource(shader->fragment, num_source, source, NULL);
|
|
|
|
|
|
|
|
glCompileShader(shader->fragment);
|
|
|
|
glGetShaderiv(shader->fragment, GL_COMPILE_STATUS, &status);
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
glGetShaderInfoLog(shader->fragment, sizeof(log), &length, log);
|
|
|
|
shader_print_errors("compile", log, source, num_source);
|
|
|
|
|
|
|
|
GPU_shader_free(shader);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (geocode) {
|
|
|
|
const char *source[6];
|
|
|
|
int num_source = 0;
|
|
|
|
|
|
|
|
source[num_source++] = gpu_shader_version();
|
|
|
|
source[num_source++] = "#define GPU_GEOMETRY_SHADER\n";
|
|
|
|
source[num_source++] = standard_extensions;
|
|
|
|
source[num_source++] = standard_defines;
|
|
|
|
|
2019-04-22 09:32:37 +10:00
|
|
|
if (defines) {
|
2019-04-17 06:17:24 +02:00
|
|
|
source[num_source++] = defines;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
source[num_source++] = geocode;
|
|
|
|
|
|
|
|
gpu_dump_shaders(source, num_source, DEBUG_SHADER_GEOMETRY);
|
|
|
|
|
|
|
|
glAttachShader(shader->program, shader->geometry);
|
|
|
|
glShaderSource(shader->geometry, num_source, source, NULL);
|
|
|
|
|
|
|
|
glCompileShader(shader->geometry);
|
|
|
|
glGetShaderiv(shader->geometry, GL_COMPILE_STATUS, &status);
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
glGetShaderInfoLog(shader->geometry, sizeof(log), &length, log);
|
|
|
|
shader_print_errors("compile", log, source, num_source);
|
|
|
|
|
|
|
|
GPU_shader_free(shader);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tf_names != NULL) {
|
|
|
|
glTransformFeedbackVaryings(shader->program, tf_count, tf_names, GL_INTERLEAVED_ATTRIBS);
|
|
|
|
/* Primitive type must be setup */
|
|
|
|
BLI_assert(tf_type != GPU_SHADER_TFB_NONE);
|
|
|
|
shader->feedback_transform_type = tf_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
glLinkProgram(shader->program);
|
|
|
|
glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
|
|
|
|
if (!status) {
|
|
|
|
glGetProgramInfoLog(shader->program, sizeof(log), &length, log);
|
|
|
|
/* print attached shaders in pipeline order */
|
2019-04-22 09:32:37 +10:00
|
|
|
if (vertexcode) {
|
2019-04-17 06:17:24 +02:00
|
|
|
shader_print_errors("linking", log, &vertexcode, 1);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
if (geocode) {
|
2019-04-17 06:17:24 +02:00
|
|
|
shader_print_errors("linking", log, &geocode, 1);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
if (libcode) {
|
2019-04-17 06:17:24 +02:00
|
|
|
shader_print_errors("linking", log, &libcode, 1);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
if (fragcode) {
|
2019-04-17 06:17:24 +02:00
|
|
|
shader_print_errors("linking", log, &fragcode, 1);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
GPU_shader_free(shader);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:29:48 +02:00
|
|
|
glUseProgram(shader->program);
|
2019-04-17 06:17:24 +02:00
|
|
|
shader->interface = GPU_shaderinterface_create(shader->program);
|
|
|
|
|
|
|
|
return shader;
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2017-09-27 18:03:00 +02:00
|
|
|
#undef DEBUG_SHADER_GEOMETRY
|
|
|
|
#undef DEBUG_SHADER_FRAGMENT
|
|
|
|
#undef DEBUG_SHADER_VERTEX
|
|
|
|
#undef DEBUG_SHADER_NONE
|
|
|
|
|
2019-02-06 09:15:16 +11:00
|
|
|
static const char *string_join_array_maybe_alloc(const char **str_arr, bool *r_is_alloc)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
bool is_alloc = false;
|
|
|
|
if (str_arr == NULL) {
|
|
|
|
*r_is_alloc = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Skip empty strings (avoid alloc if we can). */
|
|
|
|
while (str_arr[0] && str_arr[0][0] == '\0') {
|
|
|
|
str_arr++;
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for (i = 0; str_arr[i]; i++) {
|
|
|
|
if (i != 0 && str_arr[i][0] != '\0') {
|
|
|
|
is_alloc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*r_is_alloc = is_alloc;
|
|
|
|
if (is_alloc) {
|
|
|
|
return BLI_string_join_arrayN(str_arr, i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return str_arr[0];
|
|
|
|
}
|
2019-02-06 09:15:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use via #GPU_shader_create_from_arrays macro (avoids passing in param).
|
|
|
|
*
|
|
|
|
* Similar to #DRW_shader_create_with_lib with the ability to include libs for each type of shader.
|
|
|
|
*
|
|
|
|
* It has the advantage that each item can be conditionally included
|
|
|
|
* without having to build the string inline, then free it.
|
|
|
|
*
|
|
|
|
* \param params: NULL terminated arrays of strings.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* \code{.c}
|
|
|
|
* sh = GPU_shader_create_from_arrays({
|
2019-04-22 01:45:43 +10:00
|
|
|
* .vert = (const char *[]){shader_lib_glsl, shader_vert_glsl, NULL},
|
|
|
|
* .geom = (const char *[]){shader_geom_glsl, NULL},
|
|
|
|
* .frag = (const char *[]){shader_frag_glsl, NULL},
|
|
|
|
* .defs = (const char *[]){"#define DEFINE\n", test ? "#define OTHER_DEFINE\n" : "", NULL},
|
2019-02-06 09:15:16 +11:00
|
|
|
* });
|
|
|
|
* \endcode
|
|
|
|
*/
|
|
|
|
struct GPUShader *GPU_shader_create_from_arrays_impl(
|
2019-04-17 06:17:24 +02:00
|
|
|
const struct GPU_ShaderCreateFromArray_Params *params)
|
2019-02-06 09:15:16 +11:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
struct {
|
|
|
|
const char *str;
|
|
|
|
bool is_alloc;
|
|
|
|
} str_dst[4] = {{0}};
|
|
|
|
const char **str_src[4] = {params->vert, params->frag, params->geom, params->defs};
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(str_src); i++) {
|
|
|
|
str_dst[i].str = string_join_array_maybe_alloc(str_src[i], &str_dst[i].is_alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
GPUShader *sh = GPU_shader_create(
|
|
|
|
str_dst[0].str, str_dst[1].str, str_dst[2].str, NULL, str_dst[3].str, __func__);
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(str_dst); i++) {
|
|
|
|
if (str_dst[i].is_alloc) {
|
|
|
|
MEM_freeN((void *)str_dst[i].str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sh;
|
2019-02-06 09:15:16 +11:00
|
|
|
}
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
void GPU_shader_bind(GPUShader *shader)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(shader && shader->program);
|
2016-09-27 19:38:35 +02:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
glUseProgram(shader->program);
|
|
|
|
GPU_matrix_bind(shader->interface);
|
2020-04-14 20:44:45 +02:00
|
|
|
GPU_shader_set_srgb_uniform(shader->interface);
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_shader_unbind(void)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
glUseProgram(0);
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 12:18:35 +10:00
|
|
|
bool GPU_shader_transform_feedback_enable(GPUShader *shader, uint vbo_id)
|
2018-05-16 12:47:15 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
if (shader->feedback_transform_type == GPU_SHADER_TFB_NONE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vbo_id);
|
|
|
|
|
|
|
|
switch (shader->feedback_transform_type) {
|
|
|
|
case GPU_SHADER_TFB_POINTS:
|
|
|
|
glBeginTransformFeedback(GL_POINTS);
|
|
|
|
return true;
|
|
|
|
case GPU_SHADER_TFB_LINES:
|
|
|
|
glBeginTransformFeedback(GL_LINES);
|
|
|
|
return true;
|
|
|
|
case GPU_SHADER_TFB_TRIANGLES:
|
|
|
|
glBeginTransformFeedback(GL_TRIANGLES);
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-16 12:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_shader_transform_feedback_disable(GPUShader *UNUSED(shader))
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
glEndTransformFeedback();
|
2018-05-16 12:47:15 +02:00
|
|
|
}
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
void GPU_shader_free(GPUShader *shader)
|
|
|
|
{
|
2019-05-01 11:09:22 +10:00
|
|
|
#if 0 /* Would be nice to have, but for now the Deferred compilation \
|
2018-08-01 21:45:43 +02:00
|
|
|
* does not have a GPUContext. */
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(GPU_context_active_get() != NULL);
|
2018-08-01 21:45:43 +02:00
|
|
|
#endif
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(shader);
|
2016-09-27 19:38:35 +02:00
|
|
|
|
2019-04-22 09:32:37 +10:00
|
|
|
if (shader->vertex) {
|
2019-04-17 06:17:24 +02:00
|
|
|
glDeleteShader(shader->vertex);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
if (shader->geometry) {
|
2019-04-17 06:17:24 +02:00
|
|
|
glDeleteShader(shader->geometry);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
if (shader->fragment) {
|
2019-04-17 06:17:24 +02:00
|
|
|
glDeleteShader(shader->fragment);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
|
|
|
if (shader->program) {
|
2019-04-17 06:17:24 +02:00
|
|
|
glDeleteProgram(shader->program);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2016-03-21 00:53:48 +01:00
|
|
|
|
2019-04-22 09:32:37 +10:00
|
|
|
if (shader->interface) {
|
2019-04-17 06:17:24 +02:00
|
|
|
GPU_shaderinterface_discard(shader->interface);
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2017-03-02 21:46:54 -05:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
MEM_freeN(shader);
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int GPU_shader_get_uniform(GPUShader *shader, const char *name)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(shader && shader->program);
|
|
|
|
const GPUShaderInput *uniform = GPU_shaderinterface_uniform(shader->interface, name);
|
|
|
|
return uniform ? uniform->location : -1;
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2017-10-08 15:49:25 +02:00
|
|
|
int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(shader && shader->program);
|
2020-06-04 14:15:25 +02:00
|
|
|
return GPU_shaderinterface_uniform_builtin(shader->interface, builtin);
|
2020-06-04 13:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int GPU_shader_get_builtin_block(GPUShader *shader, int builtin)
|
|
|
|
{
|
|
|
|
BLI_assert(shader && shader->program);
|
2020-06-04 14:15:25 +02:00
|
|
|
return GPU_shaderinterface_block_builtin(shader->interface, builtin);
|
2017-10-08 15:49:25 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 11:20:15 +01:00
|
|
|
int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(shader && shader->program);
|
|
|
|
const GPUShaderInput *ubo = GPU_shaderinterface_ubo(shader->interface, name);
|
|
|
|
return ubo ? ubo->location : -1;
|
2017-02-07 11:20:15 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 18:14:28 +02:00
|
|
|
int GPU_shader_get_uniform_block_binding(GPUShader *shader, const char *name)
|
|
|
|
{
|
|
|
|
BLI_assert(shader && shader->program);
|
|
|
|
const GPUShaderInput *ubo = GPU_shaderinterface_ubo(shader->interface, name);
|
|
|
|
return ubo ? ubo->binding : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GPU_shader_get_texture_binding(GPUShader *shader, const char *name)
|
|
|
|
{
|
|
|
|
BLI_assert(shader && shader->program);
|
|
|
|
const GPUShaderInput *tex = GPU_shaderinterface_uniform(shader->interface, name);
|
|
|
|
return tex ? tex->binding : -1;
|
|
|
|
}
|
|
|
|
|
2017-03-02 21:46:54 -05:00
|
|
|
void *GPU_shader_get_interface(GPUShader *shader)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return shader->interface;
|
2017-03-02 21:46:54 -05:00
|
|
|
}
|
|
|
|
|
2017-02-07 11:20:15 +01:00
|
|
|
/* Clement : Temp */
|
|
|
|
int GPU_shader_get_program(GPUShader *shader)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return (int)shader->program;
|
2017-02-07 11:20:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 19:51:02 +00:00
|
|
|
void GPU_shader_uniform_float(GPUShader *UNUSED(shader), int location, float value)
|
|
|
|
{
|
2019-04-22 09:32:37 +10:00
|
|
|
if (location == -1) {
|
2019-04-17 06:17:24 +02:00
|
|
|
return;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2018-09-20 19:51:02 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
glUniform1f(location, value);
|
2018-09-20 19:51:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
void GPU_shader_uniform_vector(
|
|
|
|
GPUShader *UNUSED(shader), int location, int length, int arraysize, const float *value)
|
2015-12-06 21:20:19 +01:00
|
|
|
{
|
2019-04-22 09:32:37 +10:00
|
|
|
if (location == -1 || value == NULL) {
|
2019-04-17 06:17:24 +02:00
|
|
|
return;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-07 17:14:11 +02:00
|
|
|
switch (length) {
|
|
|
|
case 1:
|
|
|
|
glUniform1fv(location, arraysize, value);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
glUniform2fv(location, arraysize, value);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
glUniform3fv(location, arraysize, value);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
glUniform4fv(location, arraysize, value);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
glUniformMatrix3fv(location, arraysize, 0, value);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
glUniformMatrix4fv(location, arraysize, 0, value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BLI_assert(0);
|
|
|
|
break;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
void GPU_shader_uniform_vector_int(
|
|
|
|
GPUShader *UNUSED(shader), int location, int length, int arraysize, const int *value)
|
2015-12-06 21:20:19 +01:00
|
|
|
{
|
2019-04-22 09:32:37 +10:00
|
|
|
if (location == -1) {
|
2019-04-17 06:17:24 +02:00
|
|
|
return;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-07 17:14:11 +02:00
|
|
|
switch (length) {
|
|
|
|
case 1:
|
|
|
|
glUniform1iv(location, arraysize, value);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
glUniform2iv(location, arraysize, value);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
glUniform3iv(location, arraysize, value);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
glUniform4iv(location, arraysize, value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BLI_assert(0);
|
|
|
|
break;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_shader_uniform_int(GPUShader *UNUSED(shader), int location, int value)
|
|
|
|
{
|
2019-04-22 09:32:37 +10:00
|
|
|
if (location == -1) {
|
2019-04-17 06:17:24 +02:00
|
|
|
return;
|
2019-04-22 09:32:37 +10:00
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
glUniform1i(location, value);
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-14 20:44:45 +02:00
|
|
|
void GPU_shader_set_srgb_uniform(const GPUShaderInterface *interface)
|
|
|
|
{
|
2020-06-04 14:15:25 +02:00
|
|
|
int32_t loc = GPU_shaderinterface_uniform_builtin(interface, GPU_UNIFORM_SRGB_TRANSFORM);
|
|
|
|
if (loc != -1) {
|
|
|
|
glUniform1i(loc, g_shader_builtin_srgb_transform);
|
2020-04-14 20:44:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
int GPU_shader_get_attribute(GPUShader *shader, const char *name)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(shader && shader->program);
|
|
|
|
const GPUShaderInput *attr = GPU_shaderinterface_attr(shader->interface, name);
|
|
|
|
return attr ? attr->location : -1;
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2019-06-06 09:55:53 +10:00
|
|
|
char *GPU_shader_get_binary(GPUShader *shader, uint *r_binary_format, int *r_binary_len)
|
2019-06-05 13:06:11 -03:00
|
|
|
{
|
|
|
|
BLI_assert(GLEW_ARB_get_program_binary);
|
|
|
|
char *r_binary;
|
|
|
|
int binary_len = 0;
|
|
|
|
|
|
|
|
glGetProgramiv(shader->program, GL_PROGRAM_BINARY_LENGTH, &binary_len);
|
|
|
|
r_binary = MEM_mallocN(binary_len, __func__);
|
|
|
|
glGetProgramBinary(shader->program, binary_len, NULL, r_binary_format, r_binary);
|
|
|
|
|
|
|
|
if (r_binary_len) {
|
|
|
|
*r_binary_len = binary_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r_binary;
|
|
|
|
}
|
|
|
|
|
2020-04-14 20:44:45 +02:00
|
|
|
void GPU_shader_set_framebuffer_srgb_target(int use_srgb_to_linear)
|
|
|
|
{
|
|
|
|
g_shader_builtin_srgb_transform = use_srgb_to_linear;
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:15:16 +11:00
|
|
|
static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = {
|
2019-04-17 06:17:24 +02:00
|
|
|
[GPU_SHADER_TEXT] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_text_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_text_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_KEYFRAME_DIAMOND] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_keyframe_diamond_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_keyframe_diamond_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_SIMPLE_LIGHTING] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_normal_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_simple_lighting_frag_glsl,
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_2D_CHECKER] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_checker_frag_glsl,
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_2D_DIAG_STRIPES] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_diag_stripes_frag_glsl,
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_2D_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_uniform_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_FLAT_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_flat_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_flat_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_SMOOTH_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_smooth_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_smooth_color_frag_glsl,
|
|
|
|
},
|
2020-02-11 15:18:55 +01:00
|
|
|
[GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE] =
|
2019-04-17 06:17:24 +02:00
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_image_vert_glsl,
|
2020-02-11 15:18:55 +01:00
|
|
|
.frag = datatoc_gpu_shader_image_overlays_merge_frag_glsl,
|
2019-04-17 06:17:24 +02:00
|
|
|
},
|
2020-03-19 08:06:49 +01:00
|
|
|
[GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE] =
|
|
|
|
{
|
2020-03-23 09:09:33 +01:00
|
|
|
.vert = datatoc_gpu_shader_2D_vert_glsl,
|
2020-03-19 08:06:49 +01:00
|
|
|
.frag = datatoc_gpu_shader_image_overlays_stereo_merge_frag_glsl,
|
|
|
|
},
|
2019-04-17 06:17:24 +02:00
|
|
|
[GPU_SHADER_2D_IMAGE] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_image_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_IMAGE_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_image_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_IMAGE_DESATURATE_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_image_desaturate_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_image_shuffle_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_IMAGE_RECT_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_image_rect_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_image_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_image_multi_rect_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_image_varying_color_frag_glsl,
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_3D_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_uniform_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_FLAT_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_flat_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_flat_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_SMOOTH_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_smooth_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_3D_smooth_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_DEPTH_ONLY] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_depth_only_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_uniform_color_frag_glsl,
|
|
|
|
},
|
|
|
|
|
2020-04-23 23:05:39 +02:00
|
|
|
[GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_polyline_vert_glsl,
|
|
|
|
.geom = datatoc_gpu_shader_3D_polyline_geom_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_3D_polyline_frag_glsl,
|
|
|
|
.defs = "#define UNIFORM\n",
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_polyline_vert_glsl,
|
|
|
|
.geom = datatoc_gpu_shader_3D_polyline_geom_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_3D_polyline_frag_glsl,
|
|
|
|
.defs = "#define UNIFORM\n"
|
|
|
|
"#define CLIP\n",
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_POLYLINE_FLAT_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_polyline_vert_glsl,
|
|
|
|
.geom = datatoc_gpu_shader_3D_polyline_geom_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_3D_polyline_frag_glsl,
|
|
|
|
.defs = "#define FLAT\n",
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_polyline_vert_glsl,
|
|
|
|
.geom = datatoc_gpu_shader_3D_polyline_geom_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_3D_polyline_frag_glsl,
|
|
|
|
.defs = "#define SMOOTH\n",
|
|
|
|
},
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
[GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_line_dashed_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_line_dashed_frag_glsl,
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_uniform_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_varying_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_uniform_color_aa_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_POINT_UNIFORM_SIZE_VARYING_COLOR_OUTLINE_AA] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_varying_color_outline_aa_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_uniform_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_varying_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_point_varying_size_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_uniform_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_varying_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_uniform_color_aa_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_3D_point_uniform_size_outline_aa_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl,
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_flat_color_frag_glsl,
|
|
|
|
.defs = "#define UNIFORM_SCALE\n",
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_2D_AREA_EDGES] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_area_borders_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_area_borders_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_WIDGET_BASE] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_widget_base_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_widget_base_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_WIDGET_BASE_INST] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_widget_base_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_widget_base_frag_glsl,
|
|
|
|
.defs = "#define USE_INSTANCE\n",
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_WIDGET_SHADOW] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_widget_shadow_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_widget_shadow_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_NODELINK] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_nodelink_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_nodelink_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_NODELINK_INST] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_nodelink_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_nodelink_frag_glsl,
|
|
|
|
.defs = "#define USE_INSTANCE\n",
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_2D_UV_UNIFORM_COLOR] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_uniform_color_frag_glsl,
|
|
|
|
.defs = "#define UV_POS\n",
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_UV_VERTS] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_edituvs_points_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_varying_color_varying_outline_aa_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_UV_FACEDOTS] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_edituvs_facedots_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_point_varying_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_UV_EDGES] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_edituvs_edges_vert_glsl,
|
2020-06-22 17:30:45 +02:00
|
|
|
.frag = datatoc_gpu_shader_2D_edituvs_edges_frag_glsl,
|
2019-04-17 06:17:24 +02:00
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_UV_EDGES_SMOOTH] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_edituvs_edges_vert_glsl,
|
2020-06-22 17:30:45 +02:00
|
|
|
.frag = datatoc_gpu_shader_2D_edituvs_edges_frag_glsl,
|
2019-04-17 06:17:24 +02:00
|
|
|
.defs = "#define SMOOTH_COLOR\n",
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_UV_FACES] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_edituvs_faces_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_flat_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_UV_FACES_STRETCH_AREA] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_edituvs_stretch_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_smooth_color_frag_glsl,
|
|
|
|
},
|
|
|
|
[GPU_SHADER_2D_UV_FACES_STRETCH_ANGLE] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_2D_edituvs_stretch_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_2D_smooth_color_frag_glsl,
|
|
|
|
.defs = "#define STRETCH_ANGLE\n",
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_GPENCIL_STROKE] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_gpencil_stroke_vert_glsl,
|
|
|
|
.geom = datatoc_gpu_shader_gpencil_stroke_geom_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_gpencil_stroke_frag_glsl,
|
|
|
|
},
|
|
|
|
|
|
|
|
[GPU_SHADER_GPENCIL_FILL] =
|
|
|
|
{
|
|
|
|
.vert = datatoc_gpu_shader_gpencil_fill_vert_glsl,
|
|
|
|
.frag = datatoc_gpu_shader_gpencil_fill_frag_glsl,
|
|
|
|
},
|
2018-09-14 09:32:19 -03:00
|
|
|
};
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
GPUShader *GPU_shader_get_builtin_shader_with_config(eGPUBuiltinShader shader,
|
|
|
|
eGPUShaderConfig sh_cfg)
|
2015-12-06 21:20:19 +01:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(shader < GPU_SHADER_BUILTIN_LEN);
|
|
|
|
BLI_assert(sh_cfg < GPU_SHADER_CFG_LEN);
|
|
|
|
GPUShader **sh_p = &builtin_shaders[sh_cfg][shader];
|
|
|
|
|
|
|
|
if (*sh_p == NULL) {
|
|
|
|
const GPUShaderStages *stages = &builtin_shader_stages[shader];
|
|
|
|
|
|
|
|
/* common case */
|
|
|
|
if (sh_cfg == GPU_SHADER_CFG_DEFAULT) {
|
2020-04-14 20:44:45 +02:00
|
|
|
*sh_p = GPU_shader_create_from_arrays({
|
|
|
|
.vert = (const char *[]){stages->vert, NULL},
|
|
|
|
.geom = (const char *[]){stages->geom, NULL},
|
|
|
|
.frag = (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL},
|
|
|
|
.defs = (const char *[]){stages->defs, NULL},
|
|
|
|
});
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
else if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
|
|
|
|
/* Remove eventually, for now ensure support for each shader has been added. */
|
|
|
|
BLI_assert(ELEM(shader,
|
|
|
|
GPU_SHADER_3D_UNIFORM_COLOR,
|
|
|
|
GPU_SHADER_3D_SMOOTH_COLOR,
|
|
|
|
GPU_SHADER_3D_DEPTH_ONLY,
|
|
|
|
GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE,
|
|
|
|
GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA,
|
|
|
|
GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA,
|
2019-05-30 20:02:05 +10:00
|
|
|
GPU_SHADER_3D_FLAT_COLOR,
|
2019-12-02 01:40:58 +01:00
|
|
|
GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR));
|
2019-04-17 06:17:24 +02:00
|
|
|
const char *world_clip_lib = datatoc_gpu_shader_cfg_world_clip_lib_glsl;
|
|
|
|
const char *world_clip_def = "#define USE_WORLD_CLIP_PLANES\n";
|
|
|
|
/* In rare cases geometry shaders calculate clipping themselves. */
|
|
|
|
*sh_p = GPU_shader_create_from_arrays({
|
|
|
|
.vert = (const char *[]){world_clip_lib, stages->vert, NULL},
|
|
|
|
.geom = (const char *[]){stages->geom ? world_clip_lib : NULL, stages->geom, NULL},
|
2020-04-14 20:44:45 +02:00
|
|
|
.frag = (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL},
|
2019-04-17 06:17:24 +02:00
|
|
|
.defs = (const char *[]){world_clip_def, stages->defs, NULL},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BLI_assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *sh_p;
|
2019-02-06 09:15:16 +11:00
|
|
|
}
|
|
|
|
GPUShader *GPU_shader_get_builtin_shader(eGPUBuiltinShader shader)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return GPU_shader_get_builtin_shader_with_config(shader, GPU_SHADER_CFG_DEFAULT);
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
void GPU_shader_get_builtin_shader_code(eGPUBuiltinShader shader,
|
|
|
|
const char **r_vert,
|
|
|
|
const char **r_frag,
|
|
|
|
const char **r_geom,
|
|
|
|
const char **r_defines)
|
2018-09-14 09:32:19 -03:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const GPUShaderStages *stages = &builtin_shader_stages[shader];
|
|
|
|
*r_vert = stages->vert;
|
|
|
|
*r_frag = stages->frag;
|
|
|
|
*r_geom = stages->geom;
|
|
|
|
*r_defines = stages->defs;
|
2018-09-14 09:32:19 -03:00
|
|
|
}
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
void GPU_shader_free_builtin_shaders(void)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
for (int i = 0; i < GPU_SHADER_CFG_LEN; i++) {
|
|
|
|
for (int j = 0; j < GPU_SHADER_BUILTIN_LEN; j++) {
|
|
|
|
if (builtin_shaders[i][j]) {
|
|
|
|
GPU_shader_free(builtin_shaders[i][j]);
|
|
|
|
builtin_shaders[i][j] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|