2015-12-06 21:20:19 +01:00
|
|
|
/*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): Brecht Van Lommel.
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
#include "BLI_math_base.h"
|
|
|
|
#include "BLI_math_vector.h"
|
2017-09-27 18:03:00 +02:00
|
|
|
#include "BLI_path_util.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"
|
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
|
|
|
|
#define MAX_EXT_DEFINE_LENGTH 256
|
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[];
|
2017-05-10 20:14:52 +10:00
|
|
|
extern char datatoc_gpu_shader_simple_lighting_flat_color_frag_glsl[];
|
2017-03-11 16:10:03 +01:00
|
|
|
extern char datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl[];
|
2017-05-03 18:55:40 +02:00
|
|
|
extern char datatoc_gpu_shader_simple_lighting_smooth_color_alpha_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[];
|
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[];
|
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[];
|
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[];
|
2017-05-11 16:20:10 +02:00
|
|
|
extern char datatoc_gpu_shader_image_linear_frag_glsl[];
|
2016-11-18 16:04:25 +01:00
|
|
|
extern char datatoc_gpu_shader_image_color_frag_glsl[];
|
2017-04-19 16:52:47 +02:00
|
|
|
extern char datatoc_gpu_shader_image_alpha_color_frag_glsl[];
|
2017-02-24 01:07:40 +01:00
|
|
|
extern char datatoc_gpu_shader_image_shuffle_color_frag_glsl[];
|
2017-01-09 17:58:13 +01:00
|
|
|
extern char datatoc_gpu_shader_image_interlace_frag_glsl[];
|
2016-11-08 10:51:42 -05:00
|
|
|
extern char datatoc_gpu_shader_image_mask_uniform_color_frag_glsl[];
|
2016-10-18 00:08:34 -04:00
|
|
|
extern char datatoc_gpu_shader_image_modulate_alpha_frag_glsl[];
|
2016-10-21 20:48:08 +00:00
|
|
|
extern char datatoc_gpu_shader_image_depth_linear_frag_glsl[];
|
2018-02-26 19:41:17 +01:00
|
|
|
extern char datatoc_gpu_shader_image_depth_copy_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[];
|
|
|
|
extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[];
|
2017-05-10 20:14:52 +10:00
|
|
|
extern char datatoc_gpu_shader_3D_normal_flat_color_vert_glsl[];
|
2017-04-16 15:04:07 -04:00
|
|
|
extern char datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl[];
|
2016-08-16 14:58:53 -04:00
|
|
|
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_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl[];
|
2017-02-22 12:46:27 +01:00
|
|
|
extern char datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl[];
|
2017-02-11 21:15:46 +01:00
|
|
|
extern char datatoc_gpu_shader_instance_screenspace_variying_color_vert_glsl[];
|
2017-04-10 22:22:37 +02:00
|
|
|
extern char datatoc_gpu_shader_instance_screen_aligned_vert_glsl[];
|
2017-03-04 00:09:22 +01:00
|
|
|
extern char datatoc_gpu_shader_instance_camera_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_instance_distance_line_vert_glsl[];
|
2017-03-05 05:22:40 +01:00
|
|
|
extern char datatoc_gpu_shader_instance_edges_variying_color_geom_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_instance_edges_variying_color_vert_glsl[];
|
2017-05-18 11:41:59 +02:00
|
|
|
extern char datatoc_gpu_shader_instance_bone_envelope_solid_vert_glsl[];
|
2017-05-18 12:10:10 +02:00
|
|
|
extern char datatoc_gpu_shader_instance_bone_envelope_wire_vert_glsl[];
|
2018-02-16 02:01:09 -02:00
|
|
|
extern char datatoc_gpu_shader_instance_mball_handles_vert_glsl[];
|
2017-02-09 20:55:31 +01:00
|
|
|
|
2017-02-07 11:20:15 +01:00
|
|
|
extern char datatoc_gpu_shader_3D_groundpoint_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_3D_groundline_geom_glsl[];
|
|
|
|
|
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[];
|
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
|
|
|
|
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[];
|
Reworked version of dashed line shader.
Using geometry shader allows us to get rid of the 'line origin' extra
vertex attribute, which means dashed shader no longer requires fiddling
with those vertex attributes definition, and, most importantly, does not
require anymore special drawing code!
As you can see, this makes code much simpler, and much less verbose,
especially in complex cases.
In addition, changed how dashes are handled, to have two 'modes', a
simple one with single color (using default "color" uniform name), and a
more advanced one allowing more complex and multi-color patterns.
Note that since GLSL 1.2 does not support geometry shaders, a hack was
added for now (which gives solid lines, but at least does not make
Blender crash).
2017-05-01 16:21:53 +02:00
|
|
|
extern char datatoc_gpu_shader_2D_line_dashed_geom_glsl[];
|
2017-07-13 16:44:02 +02:00
|
|
|
extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_legacy_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl[];
|
2017-02-24 15:33:09 -05:00
|
|
|
|
2016-10-25 03:19:10 -04:00
|
|
|
extern char datatoc_gpu_shader_edges_front_back_persp_vert_glsl[];
|
2016-11-05 18:56:57 +01:00
|
|
|
extern char datatoc_gpu_shader_edges_front_back_persp_geom_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_edges_front_back_persp_legacy_vert_glsl[];
|
2016-10-25 03:19:10 -04:00
|
|
|
extern char datatoc_gpu_shader_edges_front_back_ortho_vert_glsl[];
|
2016-11-05 19:26:13 +01:00
|
|
|
extern char datatoc_gpu_shader_edges_overlay_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_edges_overlay_geom_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_edges_overlay_simple_geom_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_edges_overlay_frag_glsl[];
|
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
|
|
|
|
2016-08-28 16:50:49 +02:00
|
|
|
extern char datatoc_gpu_shader_fire_frag_glsl[];
|
2016-01-10 07:36:23 +01:00
|
|
|
extern char datatoc_gpu_shader_smoke_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_smoke_frag_glsl[];
|
2015-12-06 21:20:19 +01:00
|
|
|
extern char datatoc_gpu_shader_vsm_store_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_vsm_store_frag_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_sep_gaussian_blur_vert_glsl[];
|
|
|
|
extern char datatoc_gpu_shader_sep_gaussian_blur_frag_glsl[];
|
|
|
|
|
2016-11-08 22:53:59 -06:00
|
|
|
/* cache of built-in shaders (each is created on first use) */
|
|
|
|
static GPUShader *builtin_shaders[GPU_NUM_BUILTIN_SHADERS] = { NULL };
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2016-11-08 22:53:59 -06:00
|
|
|
typedef struct {
|
|
|
|
const char *vert;
|
|
|
|
const char *frag;
|
|
|
|
const char *geom; /* geometry stage runs between vert & frag, but is less common, so it goes last */
|
|
|
|
} 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)
|
|
|
|
{
|
|
|
|
int line = 1;
|
|
|
|
|
|
|
|
fprintf(stderr, "GPUShader: %s error:\n", task);
|
|
|
|
|
2016-10-16 03:18:17 -04:00
|
|
|
for (int i = 0; i < totcode; i++) {
|
2015-12-06 21:20:19 +01:00
|
|
|
const char *c, *pos, *end = code[i] + strlen(code[i]);
|
|
|
|
|
|
|
|
if (G.debug & G_DEBUG) {
|
|
|
|
fprintf(stderr, "===== shader string %d ====\n", i + 1);
|
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s\n", log);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *gpu_shader_version(void)
|
|
|
|
{
|
2017-04-07 12:50:44 -04: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
|
|
|
{
|
|
|
|
/* 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_query_lod) {
|
2017-05-18 15:47:32 -04:00
|
|
|
/* a #version 400 feature, but we use #version 330 maximum so use extension */
|
2015-12-06 21:20:19 +01:00
|
|
|
strcat(defines, "#extension GL_ARB_texture_query_lod: enable\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 12:48:18 +01:00
|
|
|
static void gpu_shader_standard_defines(char defines[MAX_DEFINE_LENGTH],
|
|
|
|
bool use_opensubdiv,
|
|
|
|
bool use_new_shading)
|
2015-12-06 21:20:19 +01:00
|
|
|
{
|
|
|
|
/* some useful defines to detect GPU type */
|
|
|
|
if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)) {
|
|
|
|
strcat(defines, "#define GPU_ATI\n");
|
|
|
|
if (GLEW_VERSION_3_0) {
|
|
|
|
/* TODO(merwin): revisit this version check; GLEW_VERSION_3_0 means GL 3.0 or newer */
|
|
|
|
strcat(defines, "#define CLIP_WORKAROUND\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY))
|
|
|
|
strcat(defines, "#define GPU_NVIDIA\n");
|
|
|
|
else if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY))
|
|
|
|
strcat(defines, "#define GPU_INTEL\n");
|
|
|
|
|
|
|
|
if (GPU_bicubic_bump_support())
|
|
|
|
strcat(defines, "#define BUMP_BICUBIC\n");
|
|
|
|
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
/* TODO(sergey): Check whether we actually compiling shader for
|
|
|
|
* the OpenSubdiv mesh.
|
|
|
|
*/
|
|
|
|
if (use_opensubdiv) {
|
|
|
|
strcat(defines, "#define USE_OPENSUBDIV\n");
|
|
|
|
|
|
|
|
/* TODO(sergey): not strictly speaking a define, but this is
|
|
|
|
* a global typedef which we don't have better place to define
|
|
|
|
* in yet.
|
|
|
|
*/
|
|
|
|
strcat(defines, "struct VertexData {\n"
|
|
|
|
" vec4 position;\n"
|
|
|
|
" vec3 normal;\n"
|
|
|
|
" vec2 uv;"
|
|
|
|
"};\n");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
UNUSED_VARS(use_opensubdiv);
|
|
|
|
#endif
|
|
|
|
|
2016-02-02 12:48:18 +01:00
|
|
|
if (use_new_shading) {
|
|
|
|
strcat(defines, "#define USE_NEW_SHADING\n");
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GPUShader *GPU_shader_create(const char *vertexcode,
|
|
|
|
const char *fragcode,
|
|
|
|
const char *geocode,
|
|
|
|
const char *libcode,
|
2017-04-08 02:21:13 -04:00
|
|
|
const char *defines)
|
2015-12-06 21:20:19 +01:00
|
|
|
{
|
|
|
|
return GPU_shader_create_ex(vertexcode,
|
|
|
|
fragcode,
|
|
|
|
geocode,
|
|
|
|
libcode,
|
|
|
|
defines,
|
|
|
|
GPU_SHADER_FLAGS_NONE);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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 invidual 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);
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
GPUShader *GPU_shader_create_ex(const char *vertexcode,
|
|
|
|
const char *fragcode,
|
|
|
|
const char *geocode,
|
|
|
|
const char *libcode,
|
|
|
|
const char *defines,
|
|
|
|
const int flags)
|
|
|
|
{
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
bool use_opensubdiv = (flags & GPU_SHADER_FLAGS_SPECIAL_OPENSUBDIV) != 0;
|
|
|
|
#else
|
|
|
|
UNUSED_VARS(flags);
|
|
|
|
bool use_opensubdiv = false;
|
|
|
|
#endif
|
|
|
|
GLint status;
|
|
|
|
GLchar log[5000];
|
|
|
|
GLsizei length = 0;
|
|
|
|
GPUShader *shader;
|
|
|
|
char standard_defines[MAX_DEFINE_LENGTH] = "";
|
|
|
|
char standard_extensions[MAX_EXT_DEFINE_LENGTH] = "";
|
|
|
|
|
|
|
|
shader = MEM_callocN(sizeof(GPUShader), "GPUShader");
|
2017-09-27 18:03:00 +02:00
|
|
|
gpu_dump_shaders(NULL, 0, DEBUG_SHADER_NONE);
|
2015-12-06 21:20:19 +01:00
|
|
|
|
|
|
|
if (vertexcode)
|
|
|
|
shader->vertex = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
if (fragcode)
|
|
|
|
shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
if (geocode)
|
|
|
|
shader->geometry = glCreateShader(GL_GEOMETRY_SHADER_EXT);
|
|
|
|
|
|
|
|
shader->program = glCreateProgram();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-02 12:48:18 +01:00
|
|
|
gpu_shader_standard_defines(standard_defines,
|
|
|
|
use_opensubdiv,
|
|
|
|
(flags & GPU_SHADER_FLAGS_NEW_SHADING) != 0);
|
2017-04-08 02:21:13 -04:00
|
|
|
gpu_shader_standard_extensions(standard_extensions);
|
2015-12-06 21:20:19 +01:00
|
|
|
|
|
|
|
if (vertexcode) {
|
|
|
|
const char *source[5];
|
|
|
|
/* custom limit, may be too small, beware */
|
|
|
|
int num_source = 0;
|
|
|
|
|
|
|
|
source[num_source++] = gpu_shader_version();
|
|
|
|
source[num_source++] = standard_extensions;
|
|
|
|
source[num_source++] = standard_defines;
|
|
|
|
|
|
|
|
if (defines) source[num_source++] = defines;
|
|
|
|
source[num_source++] = vertexcode;
|
|
|
|
|
2017-09-27 18:03:00 +02:00
|
|
|
gpu_dump_shaders(source, num_source, DEBUG_SHADER_VERTEX);
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
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();
|
|
|
|
source[num_source++] = standard_extensions;
|
|
|
|
source[num_source++] = standard_defines;
|
|
|
|
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
/* TODO(sergey): Move to fragment shader source code generation. */
|
|
|
|
if (use_opensubdiv) {
|
|
|
|
source[num_source++] =
|
|
|
|
"#ifdef USE_OPENSUBDIV\n"
|
|
|
|
"in block {\n"
|
|
|
|
" VertexData v;\n"
|
|
|
|
"} inpt;\n"
|
|
|
|
"#endif\n";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (defines) source[num_source++] = defines;
|
|
|
|
if (libcode) source[num_source++] = libcode;
|
|
|
|
source[num_source++] = fragcode;
|
|
|
|
|
2017-09-27 18:03:00 +02:00
|
|
|
gpu_dump_shaders(source, num_source, DEBUG_SHADER_FRAGMENT);
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
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++] = standard_extensions;
|
|
|
|
source[num_source++] = standard_defines;
|
|
|
|
|
|
|
|
if (defines) source[num_source++] = defines;
|
|
|
|
source[num_source++] = geocode;
|
|
|
|
|
2017-09-27 18:03:00 +02:00
|
|
|
gpu_dump_shaders(source, num_source, DEBUG_SHADER_GEOMETRY);
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
if (use_opensubdiv) {
|
|
|
|
glBindAttribLocation(shader->program, 0, "position");
|
|
|
|
glBindAttribLocation(shader->program, 1, "normal");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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 */
|
|
|
|
if (vertexcode) shader_print_errors("linking", log, &vertexcode, 1);
|
|
|
|
if (geocode) shader_print_errors("linking", log, &geocode, 1);
|
|
|
|
if (libcode) shader_print_errors("linking", log, &libcode, 1);
|
|
|
|
if (fragcode) shader_print_errors("linking", log, &fragcode, 1);
|
|
|
|
|
|
|
|
GPU_shader_free(shader);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:18:04 +10:00
|
|
|
shader->interface = GWN_shaderinterface_create(shader->program);
|
2017-04-13 18:37:26 -04:00
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
/* TODO(sergey): Find a better place for this. */
|
2017-05-18 18:30:09 -04:00
|
|
|
if (use_opensubdiv) {
|
|
|
|
if (GLEW_VERSION_4_1) {
|
|
|
|
glProgramUniform1i(shader->program,
|
2017-06-19 20:18:04 +10:00
|
|
|
GWN_shaderinterface_uniform(shader->interface, "FVarDataOffsetBuffer")->location,
|
2017-05-18 18:30:09 -04:00
|
|
|
30); /* GL_TEXTURE30 */
|
|
|
|
|
|
|
|
glProgramUniform1i(shader->program,
|
2017-06-19 20:18:04 +10:00
|
|
|
GWN_shaderinterface_uniform(shader->interface, "FVarDataBuffer")->location,
|
2017-05-18 18:30:09 -04:00
|
|
|
31); /* GL_TEXTURE31 */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
glUseProgram(shader->program);
|
2017-06-19 20:18:04 +10:00
|
|
|
glUniform1i(GWN_shaderinterface_uniform(shader->interface, "FVarDataOffsetBuffer")->location, 30);
|
|
|
|
glUniform1i(GWN_shaderinterface_uniform(shader->interface, "FVarDataBuffer")->location, 31);
|
2017-05-18 18:30:09 -04:00
|
|
|
glUseProgram(0);
|
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2017-09-27 18:03:00 +02:00
|
|
|
#undef DEBUG_SHADER_GEOMETRY
|
|
|
|
#undef DEBUG_SHADER_FRAGMENT
|
|
|
|
#undef DEBUG_SHADER_VERTEX
|
|
|
|
#undef DEBUG_SHADER_NONE
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
void GPU_shader_bind(GPUShader *shader)
|
|
|
|
{
|
2016-09-27 19:38:35 +02:00
|
|
|
BLI_assert(shader && shader->program);
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
glUseProgram(shader->program);
|
2017-04-12 18:55:32 -04:00
|
|
|
gpuBindMatrices(shader->interface);
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_shader_unbind(void)
|
|
|
|
{
|
|
|
|
glUseProgram(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_shader_free(GPUShader *shader)
|
|
|
|
{
|
2016-09-27 19:38:35 +02:00
|
|
|
BLI_assert(shader);
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
if (shader->vertex)
|
|
|
|
glDeleteShader(shader->vertex);
|
|
|
|
if (shader->geometry)
|
|
|
|
glDeleteShader(shader->geometry);
|
|
|
|
if (shader->fragment)
|
|
|
|
glDeleteShader(shader->fragment);
|
|
|
|
if (shader->program)
|
|
|
|
glDeleteProgram(shader->program);
|
2016-03-21 00:53:48 +01:00
|
|
|
|
2017-03-02 21:46:54 -05:00
|
|
|
if (shader->interface)
|
2017-06-19 20:18:04 +10:00
|
|
|
GWN_shaderinterface_discard(shader->interface);
|
2017-03-02 21:46:54 -05:00
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
MEM_freeN(shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GPU_shader_get_uniform(GPUShader *shader, const char *name)
|
|
|
|
{
|
2016-09-27 19:38:35 +02:00
|
|
|
BLI_assert(shader && shader->program);
|
2017-06-19 20:18:04 +10:00
|
|
|
const Gwn_ShaderInput *uniform = GWN_shaderinterface_uniform(shader->interface, name);
|
2017-04-13 18:37:26 -04:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
BLI_assert(shader && shader->program);
|
|
|
|
const Gwn_ShaderInput *uniform = GWN_shaderinterface_uniform_builtin(shader->interface, builtin);
|
|
|
|
return uniform ? uniform->location : -1;
|
|
|
|
}
|
|
|
|
|
2017-02-07 11:20:15 +01:00
|
|
|
int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
|
|
|
|
{
|
|
|
|
BLI_assert(shader && shader->program);
|
|
|
|
|
2017-10-06 14:57:21 +02:00
|
|
|
const Gwn_ShaderInput *ubo = GWN_shaderinterface_ubo(shader->interface, name);
|
|
|
|
return ubo ? ubo->location : -1;
|
2017-02-07 11:20:15 +01:00
|
|
|
}
|
|
|
|
|
2017-03-02 21:46:54 -05:00
|
|
|
void *GPU_shader_get_interface(GPUShader *shader)
|
|
|
|
{
|
|
|
|
return shader->interface;
|
|
|
|
}
|
|
|
|
|
2017-02-07 11:20:15 +01:00
|
|
|
/* Clement : Temp */
|
|
|
|
int GPU_shader_get_program(GPUShader *shader)
|
|
|
|
{
|
|
|
|
return (int)shader->program;
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
void GPU_shader_uniform_vector(GPUShader *UNUSED(shader), int location, int length, int arraysize, const float *value)
|
|
|
|
{
|
|
|
|
if (location == -1 || value == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (length == 1) glUniform1fv(location, arraysize, value);
|
|
|
|
else if (length == 2) glUniform2fv(location, arraysize, value);
|
|
|
|
else if (length == 3) glUniform3fv(location, arraysize, value);
|
|
|
|
else if (length == 4) glUniform4fv(location, arraysize, value);
|
|
|
|
else if (length == 9) glUniformMatrix3fv(location, arraysize, 0, value);
|
|
|
|
else if (length == 16) glUniformMatrix4fv(location, arraysize, 0, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_shader_uniform_vector_int(GPUShader *UNUSED(shader), int location, int length, int arraysize, const int *value)
|
|
|
|
{
|
|
|
|
if (location == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (length == 1) glUniform1iv(location, arraysize, value);
|
|
|
|
else if (length == 2) glUniform2iv(location, arraysize, value);
|
|
|
|
else if (length == 3) glUniform3iv(location, arraysize, value);
|
|
|
|
else if (length == 4) glUniform4iv(location, arraysize, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_shader_uniform_int(GPUShader *UNUSED(shader), int location, int value)
|
|
|
|
{
|
|
|
|
if (location == -1)
|
|
|
|
return;
|
|
|
|
|
2016-08-19 00:52:52 -04:00
|
|
|
glUniform1i(location, value);
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
2017-02-07 11:20:15 +01:00
|
|
|
void GPU_shader_uniform_buffer(GPUShader *shader, int location, GPUUniformBuffer *ubo)
|
|
|
|
{
|
|
|
|
int bindpoint = GPU_uniformbuffer_bindpoint(ubo);
|
|
|
|
|
|
|
|
if (location == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
glUniformBlockBinding(shader->program, location, bindpoint);
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:20:19 +01:00
|
|
|
void GPU_shader_uniform_texture(GPUShader *UNUSED(shader), int location, GPUTexture *tex)
|
|
|
|
{
|
|
|
|
int number = GPU_texture_bound_number(tex);
|
|
|
|
|
2018-03-22 16:10:54 +01:00
|
|
|
if (number == -1) {
|
|
|
|
fprintf(stderr, "Texture is not bound.\n");
|
|
|
|
BLI_assert(0);
|
2015-12-06 21:20:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (location == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glUniform1i(location, number);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GPU_shader_get_attribute(GPUShader *shader, const char *name)
|
|
|
|
{
|
2016-09-27 19:38:35 +02:00
|
|
|
BLI_assert(shader && shader->program);
|
2017-06-19 20:18:04 +10:00
|
|
|
const Gwn_ShaderInput *attrib = GWN_shaderinterface_attr(shader->interface, name);
|
2017-04-15 18:06:54 -04:00
|
|
|
return attrib ? attrib->location : -1;
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
|
|
|
|
{
|
2016-11-08 22:53:59 -06:00
|
|
|
BLI_assert(shader != GPU_NUM_BUILTIN_SHADERS); /* don't be a troll */
|
|
|
|
|
|
|
|
static const GPUShaderStages builtin_shader_stages[GPU_NUM_BUILTIN_SHADERS] = {
|
|
|
|
[GPU_SHADER_VSM_STORE] = { datatoc_gpu_shader_vsm_store_vert_glsl, datatoc_gpu_shader_vsm_store_frag_glsl },
|
|
|
|
[GPU_SHADER_SEP_GAUSSIAN_BLUR] = { datatoc_gpu_shader_sep_gaussian_blur_vert_glsl,
|
|
|
|
datatoc_gpu_shader_sep_gaussian_blur_frag_glsl },
|
|
|
|
[GPU_SHADER_SMOKE] = { datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl },
|
|
|
|
[GPU_SHADER_SMOKE_FIRE] = { datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl },
|
|
|
|
[GPU_SHADER_SMOKE_COBA] = { datatoc_gpu_shader_smoke_vert_glsl, datatoc_gpu_shader_smoke_frag_glsl },
|
|
|
|
|
|
|
|
[GPU_SHADER_TEXT] = { datatoc_gpu_shader_text_vert_glsl, datatoc_gpu_shader_text_frag_glsl },
|
2017-02-11 00:02:28 -05:00
|
|
|
[GPU_SHADER_KEYFRAME_DIAMOND] = { datatoc_gpu_shader_keyframe_diamond_vert_glsl,
|
|
|
|
datatoc_gpu_shader_keyframe_diamond_frag_glsl },
|
2016-11-08 22:53:59 -06:00
|
|
|
[GPU_SHADER_EDGES_FRONT_BACK_PERSP] = { datatoc_gpu_shader_edges_front_back_persp_vert_glsl,
|
|
|
|
/* this version is */ datatoc_gpu_shader_flat_color_frag_glsl,
|
|
|
|
/* magical but slooow */ datatoc_gpu_shader_edges_front_back_persp_geom_glsl },
|
|
|
|
[GPU_SHADER_EDGES_FRONT_BACK_ORTHO] = { datatoc_gpu_shader_edges_front_back_ortho_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl },
|
|
|
|
[GPU_SHADER_EDGES_OVERLAY_SIMPLE] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_edges_overlay_frag_glsl,
|
|
|
|
datatoc_gpu_shader_edges_overlay_simple_geom_glsl },
|
|
|
|
[GPU_SHADER_EDGES_OVERLAY] = { datatoc_gpu_shader_edges_overlay_vert_glsl,
|
|
|
|
datatoc_gpu_shader_edges_overlay_frag_glsl,
|
|
|
|
datatoc_gpu_shader_edges_overlay_geom_glsl },
|
2017-04-16 15:04:07 -04:00
|
|
|
[GPU_SHADER_SIMPLE_LIGHTING] = { datatoc_gpu_shader_3D_normal_vert_glsl, datatoc_gpu_shader_simple_lighting_frag_glsl },
|
2017-05-16 16:57:26 +10:00
|
|
|
/* Use 'USE_FLAT_NORMAL' to make flat shader from smooth */
|
|
|
|
[GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR] = { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl, datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl },
|
2017-04-16 15:04:07 -04:00
|
|
|
[GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR] = { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl, datatoc_gpu_shader_simple_lighting_smooth_color_frag_glsl },
|
2017-05-03 18:55:40 +02:00
|
|
|
[GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA] = { datatoc_gpu_shader_3D_normal_smooth_color_vert_glsl, datatoc_gpu_shader_simple_lighting_smooth_color_alpha_frag_glsl },
|
2016-11-08 22:53:59 -06:00
|
|
|
|
|
|
|
[GPU_SHADER_2D_IMAGE_MASK_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_mask_uniform_color_frag_glsl },
|
|
|
|
[GPU_SHADER_3D_IMAGE_MODULATE_ALPHA] = { datatoc_gpu_shader_3D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_modulate_alpha_frag_glsl },
|
|
|
|
[GPU_SHADER_3D_IMAGE_DEPTH] = { datatoc_gpu_shader_3D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_depth_linear_frag_glsl },
|
2018-02-26 19:41:17 +01:00
|
|
|
[GPU_SHADER_3D_IMAGE_DEPTH_COPY] = { datatoc_gpu_shader_3D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_depth_copy_frag_glsl },
|
2016-11-08 22:53:59 -06:00
|
|
|
|
2017-02-11 00:02:28 -05:00
|
|
|
[GPU_SHADER_2D_IMAGE_INTERLACE] = { datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_interlace_frag_glsl },
|
2017-02-07 13:04:08 +01:00
|
|
|
[GPU_SHADER_2D_CHECKER] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_checker_frag_glsl },
|
2017-01-09 17:58:13 +01:00
|
|
|
|
2017-02-16 21:49:20 -02:00
|
|
|
[GPU_SHADER_2D_DIAG_STRIPES] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_diag_stripes_frag_glsl },
|
|
|
|
|
2016-11-08 22:53:59 -06:00
|
|
|
[GPU_SHADER_2D_UNIFORM_COLOR] = { datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
|
|
|
|
[GPU_SHADER_2D_FLAT_COLOR] = { datatoc_gpu_shader_2D_flat_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl },
|
|
|
|
[GPU_SHADER_2D_SMOOTH_COLOR] = { datatoc_gpu_shader_2D_smooth_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_2D_smooth_color_frag_glsl },
|
2017-05-11 16:20:10 +02:00
|
|
|
[GPU_SHADER_2D_IMAGE_LINEAR_TO_SRGB] = { datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_linear_frag_glsl },
|
2018-02-13 18:09:58 +01:00
|
|
|
[GPU_SHADER_2D_IMAGE] = { datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_frag_glsl },
|
2016-11-18 16:04:25 +01:00
|
|
|
[GPU_SHADER_2D_IMAGE_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_color_frag_glsl },
|
2017-04-19 16:52:47 +02:00
|
|
|
[GPU_SHADER_2D_IMAGE_ALPHA_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_alpha_color_frag_glsl },
|
2018-02-13 18:09:58 +01:00
|
|
|
[GPU_SHADER_2D_IMAGE_ALPHA] = { datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_modulate_alpha_frag_glsl },
|
2017-02-24 01:07:40 +01:00
|
|
|
[GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR] = { datatoc_gpu_shader_2D_image_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_shuffle_color_frag_glsl },
|
2018-03-27 23:57:29 +02:00
|
|
|
[GPU_SHADER_2D_IMAGE_RECT_COLOR] = { datatoc_gpu_shader_2D_image_rect_vert_glsl,
|
|
|
|
datatoc_gpu_shader_image_color_frag_glsl },
|
|
|
|
|
2016-11-08 22:53:59 -06:00
|
|
|
[GPU_SHADER_3D_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
|
2017-10-07 15:57:14 +11:00
|
|
|
[GPU_SHADER_3D_UNIFORM_COLOR_U32] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
|
2016-11-08 22:53:59 -06:00
|
|
|
[GPU_SHADER_3D_FLAT_COLOR] = { datatoc_gpu_shader_3D_flat_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl },
|
2017-05-05 05:17:31 +10:00
|
|
|
[GPU_SHADER_3D_FLAT_COLOR_U32] = { datatoc_gpu_shader_3D_flat_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl },
|
2016-11-08 22:53:59 -06:00
|
|
|
[GPU_SHADER_3D_SMOOTH_COLOR] = { datatoc_gpu_shader_3D_smooth_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_3D_smooth_color_frag_glsl },
|
|
|
|
[GPU_SHADER_3D_DEPTH_ONLY] = { datatoc_gpu_shader_3D_vert_glsl, datatoc_gpu_shader_depth_only_frag_glsl },
|
2017-04-05 01:28:48 +02:00
|
|
|
[GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_uniform_color_frag_glsl },
|
2016-11-08 22:53:59 -06:00
|
|
|
|
2017-02-07 11:20:15 +01:00
|
|
|
[GPU_SHADER_3D_GROUNDPOINT] = { datatoc_gpu_shader_3D_groundpoint_vert_glsl, datatoc_gpu_shader_point_uniform_color_frag_glsl },
|
2017-02-11 21:15:46 +01:00
|
|
|
[GPU_SHADER_3D_GROUNDLINE] = { datatoc_gpu_shader_3D_passthrough_vert_glsl,
|
2017-02-07 11:20:15 +01:00
|
|
|
datatoc_gpu_shader_uniform_color_frag_glsl,
|
|
|
|
datatoc_gpu_shader_3D_groundline_geom_glsl },
|
|
|
|
|
2017-07-13 16:44:02 +02:00
|
|
|
[GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] = { datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_2D_line_dashed_frag_glsl,
|
|
|
|
datatoc_gpu_shader_2D_line_dashed_geom_glsl },
|
|
|
|
[GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_2D_line_dashed_frag_glsl,
|
|
|
|
datatoc_gpu_shader_2D_line_dashed_geom_glsl },
|
2017-02-24 15:33:09 -05:00
|
|
|
|
2017-02-22 12:46:27 +01:00
|
|
|
[GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR] =
|
|
|
|
{ datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_simple_lighting_frag_glsl},
|
|
|
|
[GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR] = { datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl},
|
2017-02-11 21:15:46 +01:00
|
|
|
[GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR] = { datatoc_gpu_shader_instance_screenspace_variying_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl},
|
2017-04-10 22:22:37 +02:00
|
|
|
[GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS] = { datatoc_gpu_shader_instance_screen_aligned_vert_glsl,
|
2017-03-04 00:09:22 +01:00
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl},
|
2017-04-10 22:22:37 +02:00
|
|
|
[GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED] = { datatoc_gpu_shader_instance_screen_aligned_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl},
|
2017-03-04 00:09:22 +01:00
|
|
|
|
|
|
|
[GPU_SHADER_CAMERA] = { datatoc_gpu_shader_instance_camera_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl},
|
|
|
|
[GPU_SHADER_DISTANCE_LINES] = { datatoc_gpu_shader_instance_distance_line_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl},
|
2017-02-07 11:20:15 +01:00
|
|
|
|
2016-11-08 22:53:59 -06:00
|
|
|
[GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR] =
|
|
|
|
{ datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_point_uniform_color_frag_glsl },
|
|
|
|
[GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR] =
|
|
|
|
{ datatoc_gpu_shader_2D_point_varying_size_varying_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_varying_color_frag_glsl },
|
2017-02-28 02:18:52 -05:00
|
|
|
[GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
|
|
|
|
{ datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_uniform_color_aa_frag_glsl },
|
|
|
|
[GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
|
|
|
|
{ datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl },
|
|
|
|
[GPU_SHADER_2D_POINT_UNIFORM_SIZE_VARYING_COLOR_OUTLINE_AA] =
|
|
|
|
{ datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_varying_color_outline_aa_frag_glsl },
|
2016-11-08 22:53:59 -06:00
|
|
|
[GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_uniform_color_frag_glsl },
|
|
|
|
[GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR] = { datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_varying_color_frag_glsl },
|
|
|
|
[GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_point_varying_size_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_uniform_color_frag_glsl },
|
|
|
|
[GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR] =
|
|
|
|
{ datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_varying_color_frag_glsl },
|
2017-02-28 02:18:52 -05:00
|
|
|
[GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] =
|
|
|
|
{ datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_uniform_color_aa_frag_glsl },
|
|
|
|
[GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA] =
|
|
|
|
{ datatoc_gpu_shader_3D_point_uniform_size_outline_aa_vert_glsl,
|
|
|
|
datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl },
|
2017-02-09 20:55:31 +01:00
|
|
|
|
|
|
|
[GPU_SHADER_INSTANCE_UNIFORM_COLOR] = { datatoc_gpu_shader_instance_vert_glsl, datatoc_gpu_shader_uniform_color_frag_glsl },
|
2017-11-16 15:12:32 -02:00
|
|
|
[GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE] =
|
2017-02-09 20:55:31 +01:00
|
|
|
{ datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl },
|
2017-04-10 22:22:37 +02:00
|
|
|
[GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE] =
|
|
|
|
{ datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl },
|
2017-03-05 05:22:40 +01:00
|
|
|
[GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR] = { datatoc_gpu_shader_instance_edges_variying_color_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl,
|
|
|
|
datatoc_gpu_shader_instance_edges_variying_color_geom_glsl},
|
2017-05-17 12:34:06 +02:00
|
|
|
|
2017-05-18 11:41:59 +02:00
|
|
|
[GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_SOLID] = { datatoc_gpu_shader_instance_bone_envelope_solid_vert_glsl,
|
2017-07-13 16:44:02 +02:00
|
|
|
datatoc_gpu_shader_simple_lighting_frag_glsl },
|
2017-05-18 12:10:10 +02:00
|
|
|
[GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_WIRE] = { datatoc_gpu_shader_instance_bone_envelope_wire_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl },
|
2017-11-16 15:12:32 -02:00
|
|
|
|
2018-02-16 02:01:09 -02:00
|
|
|
[GPU_SHADER_3D_INSTANCE_MBALL_HANDLES] = { datatoc_gpu_shader_instance_mball_handles_vert_glsl,
|
2017-11-16 15:12:32 -02:00
|
|
|
datatoc_gpu_shader_flat_color_frag_glsl },
|
2016-11-08 22:53:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if (builtin_shaders[shader] == NULL) {
|
|
|
|
/* just a few special cases */
|
2017-05-05 05:17:31 +10:00
|
|
|
const char *defines = NULL;
|
|
|
|
switch (shader) {
|
|
|
|
case GPU_SHADER_SMOKE_COBA:
|
|
|
|
defines = "#define USE_COBA;\n";
|
|
|
|
break;
|
|
|
|
case GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE:
|
|
|
|
defines = "#define UNIFORM_SCALE;\n";
|
|
|
|
break;
|
|
|
|
case GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS:
|
|
|
|
defines = "#define AXIS_NAME;\n";
|
|
|
|
break;
|
|
|
|
case GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR:
|
2017-05-18 11:41:59 +02:00
|
|
|
case GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_SOLID:
|
2017-05-05 05:17:31 +10:00
|
|
|
defines = "#define USE_INSTANCE_COLOR;\n";
|
|
|
|
break;
|
|
|
|
case GPU_SHADER_3D_FLAT_COLOR_U32:
|
2017-08-17 01:38:07 +10:00
|
|
|
case GPU_SHADER_3D_UNIFORM_COLOR_U32:
|
2017-05-05 05:17:31 +10:00
|
|
|
defines = "#define USE_COLOR_U32;\n";
|
|
|
|
break;
|
2017-05-16 16:57:26 +10:00
|
|
|
case GPU_SHADER_SIMPLE_LIGHTING_FLAT_COLOR:
|
|
|
|
defines = "#define USE_FLAT_NORMAL;\n";
|
|
|
|
break;
|
2017-05-05 05:17:31 +10:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-11-08 22:53:59 -06:00
|
|
|
|
|
|
|
const GPUShaderStages *stages = builtin_shader_stages + shader;
|
|
|
|
|
|
|
|
if (shader == GPU_SHADER_EDGES_FRONT_BACK_PERSP && !GLEW_VERSION_3_2) {
|
|
|
|
/* TODO: remove after switch to core profile (maybe) */
|
|
|
|
static const GPUShaderStages legacy_fancy_edges =
|
|
|
|
{ datatoc_gpu_shader_edges_front_back_persp_legacy_vert_glsl,
|
|
|
|
datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl };
|
|
|
|
stages = &legacy_fancy_edges;
|
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2017-07-13 16:44:02 +02:00
|
|
|
if (shader == GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR && !GLEW_VERSION_3_2) {
|
Reworked version of dashed line shader.
Using geometry shader allows us to get rid of the 'line origin' extra
vertex attribute, which means dashed shader no longer requires fiddling
with those vertex attributes definition, and, most importantly, does not
require anymore special drawing code!
As you can see, this makes code much simpler, and much less verbose,
especially in complex cases.
In addition, changed how dashes are handled, to have two 'modes', a
simple one with single color (using default "color" uniform name), and a
more advanced one allowing more complex and multi-color patterns.
Note that since GLSL 1.2 does not support geometry shaders, a hack was
added for now (which gives solid lines, but at least does not make
Blender crash).
2017-05-01 16:21:53 +02:00
|
|
|
/* Dashed need geometry shader, which are not supported by legacy OpenGL, fallback to solid lines. */
|
|
|
|
/* TODO: remove after switch to core profile (maybe) */
|
2017-07-13 16:44:02 +02:00
|
|
|
static const GPUShaderStages legacy_dashed_lines = { datatoc_gpu_shader_3D_line_dashed_uniform_color_legacy_vert_glsl,
|
Reworked version of dashed line shader.
Using geometry shader allows us to get rid of the 'line origin' extra
vertex attribute, which means dashed shader no longer requires fiddling
with those vertex attributes definition, and, most importantly, does not
require anymore special drawing code!
As you can see, this makes code much simpler, and much less verbose,
especially in complex cases.
In addition, changed how dashes are handled, to have two 'modes', a
simple one with single color (using default "color" uniform name), and a
more advanced one allowing more complex and multi-color patterns.
Note that since GLSL 1.2 does not support geometry shaders, a hack was
added for now (which gives solid lines, but at least does not make
Blender crash).
2017-05-01 16:21:53 +02:00
|
|
|
datatoc_gpu_shader_2D_line_dashed_frag_glsl };
|
|
|
|
stages = &legacy_dashed_lines;
|
|
|
|
}
|
|
|
|
|
2016-11-08 22:53:59 -06:00
|
|
|
/* common case */
|
2017-04-08 02:21:13 -04:00
|
|
|
builtin_shaders[shader] = GPU_shader_create(stages->vert, stages->frag, stages->geom, NULL, defines);
|
2016-11-08 22:53:59 -06:00
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
|
2016-11-08 22:53:59 -06:00
|
|
|
return builtin_shaders[shader];
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_DEFINES 100
|
|
|
|
|
|
|
|
void GPU_shader_free_builtin_shaders(void)
|
|
|
|
{
|
2016-11-08 22:53:59 -06:00
|
|
|
for (int i = 0; i < GPU_NUM_BUILTIN_SHADERS; ++i) {
|
|
|
|
if (builtin_shaders[i]) {
|
|
|
|
GPU_shader_free(builtin_shaders[i]);
|
|
|
|
builtin_shaders[i] = NULL;
|
|
|
|
}
|
2016-10-16 18:43:24 -04:00
|
|
|
}
|
2015-12-06 21:20:19 +01:00
|
|
|
}
|