| 
									
										
										
										
											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"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-29 16:07:51 +02:00
										 |  |  | extern "C" char datatoc_gpu_shader_colorspace_lib_glsl[]; | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Convenience functions
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +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 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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Creation / Destruction
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  |     GPUShader *shader = (GPUShader *)MEM_callocN(sizeof(*shader), __func__); | 
					
						
							| 
									
										
										
										
											2019-06-05 13:06:11 -03:00
										 |  |  |     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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  |   shader = (GPUShader *)MEM_callocN(sizeof(GPUShader), "GPUShader"); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | void GPU_shader_free(GPUShader *shader) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #if 0 /* Would be nice to have, but for now the Deferred compilation \
 | 
					
						
							|  |  |  |        * does not have a GPUContext. */ | 
					
						
							|  |  |  |   BLI_assert(GPU_context_active_get() != NULL); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |   BLI_assert(shader); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (shader->vertex) { | 
					
						
							|  |  |  |     glDeleteShader(shader->vertex); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (shader->geometry) { | 
					
						
							|  |  |  |     glDeleteShader(shader->geometry); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (shader->fragment) { | 
					
						
							|  |  |  |     glDeleteShader(shader->fragment); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (shader->program) { | 
					
						
							|  |  |  |     glDeleteProgram(shader->program); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (shader->interface) { | 
					
						
							|  |  |  |     GPU_shaderinterface_discard(shader->interface); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   MEM_freeN(shader); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Binding
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Transform feedback
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | /** \} */ | 
					
						
							| 
									
										
										
										
											2017-03-02 21:46:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Uniforms / Resource location
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							| 
									
										
										
										
											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-07-28 20:04:15 +02:00
										 |  |  |   return GPU_shaderinterface_uniform_builtin(shader->interface, | 
					
						
							|  |  |  |                                              static_cast<GPUUniformBuiltin>(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-07-28 20:04:15 +02:00
										 |  |  |   return GPU_shaderinterface_block_builtin(shader->interface, | 
					
						
							|  |  |  |                                            static_cast<GPUUniformBlockBuiltin>(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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | int GPU_shader_get_attribute(GPUShader *shader, const char *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   BLI_assert(shader && shader->program); | 
					
						
							|  |  |  |   const GPUShaderInput *attr = GPU_shaderinterface_attr(shader->interface, name); | 
					
						
							|  |  |  |   return attr ? attr->location : -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Getters
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | char *GPU_shader_get_binary(GPUShader *shader, uint *r_binary_format, int *r_binary_len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   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 = (char *)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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Uniforms setters
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | void GPU_shader_uniform_int(GPUShader *UNUSED(shader), int location, int value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (location == -1) { | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   glUniform1i(location, value); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | /** \} */ | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							| 
									
										
										
										
											2020-07-30 08:31:25 +10:00
										 |  |  | /** \name sRGB Rendering Workaround
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-07-30 08:31:25 +10:00
										 |  |  |  * The viewport overlay frame-buffer is sRGB and will expect shaders to output display referred | 
					
						
							|  |  |  |  * Linear colors. But other frame-buffers (i.e: the area frame-buffers) are not sRGB and require | 
					
						
							|  |  |  |  * the shader output color to be in sRGB space | 
					
						
							|  |  |  |  * (assumed display encoded color-space as the time of writing). | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  |  * For this reason we have a uniform to switch the transform on and off depending on the current | 
					
						
							| 
									
										
										
										
											2020-07-30 08:31:25 +10:00
										 |  |  |  * frame-buffer color-space. | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int g_shader_builtin_srgb_transform = 0; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_shader_set_framebuffer_srgb_target(int use_srgb_to_linear) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_shader_builtin_srgb_transform = use_srgb_to_linear; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 20:04:15 +02:00
										 |  |  | /** \} */ |