| 
									
										
										
										
											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 "DNA_image_types.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_blenlib.h"
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | #include "BLI_math_base.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-02 21:16:23 +02:00
										 |  |  | #include "BLI_listbase.h"
 | 
					
						
							|  |  |  | #include "BLI_threads.h"
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_global.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | #include "GPU_batch.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | #include "GPU_context.h"
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | #include "GPU_debug.h"
 | 
					
						
							|  |  |  | #include "GPU_draw.h"
 | 
					
						
							|  |  |  | #include "GPU_extensions.h"
 | 
					
						
							|  |  |  | #include "GPU_glew.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | #include "GPU_framebuffer.h"
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | #include "GPU_texture.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | #include "gpu_context_private.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | static struct GPUTextureGlobal { | 
					
						
							|  |  |  | 	GPUTexture *invalid_tex_1D; /* texture used in place of invalid textures (not loaded correctly, missing) */ | 
					
						
							|  |  |  | 	GPUTexture *invalid_tex_2D; | 
					
						
							|  |  |  | 	GPUTexture *invalid_tex_3D; | 
					
						
							|  |  |  | } GG = {NULL, NULL, NULL}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | /* Maximum number of FBOs a texture can be attached to. */ | 
					
						
							| 
									
										
										
										
											2018-09-10 16:27:01 +02:00
										 |  |  | #define GPU_TEX_MAX_FBO_ATTACHED 9
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-21 20:42:27 +02:00
										 |  |  | typedef enum GPUTextureFormatFlag { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	GPU_FORMAT_DEPTH     = (1 << 0), | 
					
						
							|  |  |  | 	GPU_FORMAT_STENCIL   = (1 << 1), | 
					
						
							|  |  |  | 	GPU_FORMAT_INTEGER   = (1 << 2), | 
					
						
							|  |  |  | 	GPU_FORMAT_FLOAT     = (1 << 3), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	GPU_FORMAT_1D        = (1 << 10), | 
					
						
							|  |  |  | 	GPU_FORMAT_2D        = (1 << 11), | 
					
						
							|  |  |  | 	GPU_FORMAT_3D        = (1 << 12), | 
					
						
							|  |  |  | 	GPU_FORMAT_CUBE      = (1 << 13), | 
					
						
							|  |  |  | 	GPU_FORMAT_ARRAY     = (1 << 14), | 
					
						
							|  |  |  | } GPUTextureFormatFlag; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | /* GPUTexture */ | 
					
						
							|  |  |  | struct GPUTexture { | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	int w, h, d;        /* width/height/depth */ | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	int number;         /* number for multitexture binding */ | 
					
						
							|  |  |  | 	int refcount;       /* reference count */ | 
					
						
							|  |  |  | 	GLenum target;      /* GL_TEXTURE_* */ | 
					
						
							| 
									
										
										
										
											2016-08-09 12:29:14 +03:00
										 |  |  | 	GLenum target_base; /* same as target, (but no multisample)
 | 
					
						
							|  |  |  | 	                     * use it for unbinding */ | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	GLuint bindcode;    /* opengl identifier for texture */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 	GPUTextureFormat format; | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	GPUTextureFormatFlag format_flag; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 12:18:35 +10:00
										 |  |  | 	uint bytesize;      /* number of byte for one pixel */ | 
					
						
							| 
									
										
										
										
											2017-08-22 10:22:11 +02:00
										 |  |  | 	int components;     /* number of color/alpha channels */ | 
					
						
							| 
									
										
										
										
											2018-03-14 03:20:39 +01:00
										 |  |  | 	int samples;        /* number of samples for multisamples textures. 0 if not multisample target */ | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	int fb_attachment[GPU_TEX_MAX_FBO_ATTACHED]; | 
					
						
							|  |  |  | 	GPUFrameBuffer *fb[GPU_TEX_MAX_FBO_ATTACHED]; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | /* ------ Memory Management ------- */ | 
					
						
							|  |  |  | /* Records every texture allocation / free
 | 
					
						
							|  |  |  |  * to estimate the Texture Pool Memory consumption */ | 
					
						
							| 
									
										
										
										
											2018-09-12 12:18:35 +10:00
										 |  |  | static uint memory_usage; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 12:18:35 +10:00
										 |  |  | static uint gpu_texture_memory_footprint_compute(GPUTexture *tex) | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-22 21:20:23 +02:00
										 |  |  | 	int samp = max_ii(tex->samples, 1); | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 	switch (tex->target) { | 
					
						
							|  |  |  | 		case GL_TEXTURE_1D: | 
					
						
							| 
									
										
										
										
											2018-04-22 21:20:23 +02:00
										 |  |  | 			return tex->bytesize * tex->w * samp; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GL_TEXTURE_1D_ARRAY: | 
					
						
							|  |  |  | 		case GL_TEXTURE_2D: | 
					
						
							| 
									
										
										
										
											2018-04-22 21:20:23 +02:00
										 |  |  | 			return tex->bytesize * tex->w * tex->h * samp; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GL_TEXTURE_2D_ARRAY: | 
					
						
							|  |  |  | 		case GL_TEXTURE_3D: | 
					
						
							| 
									
										
										
										
											2018-04-22 21:20:23 +02:00
										 |  |  | 			return tex->bytesize * tex->w * tex->h * tex->d * samp; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GL_TEXTURE_CUBE_MAP: | 
					
						
							| 
									
										
										
										
											2018-04-22 21:20:23 +02:00
										 |  |  | 			return tex->bytesize * 6 * tex->w * tex->h * samp; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GL_TEXTURE_CUBE_MAP_ARRAY: | 
					
						
							| 
									
										
										
										
											2018-04-22 21:20:23 +02:00
										 |  |  | 			return tex->bytesize * 6 * tex->w * tex->h * tex->d * samp; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		default: | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void gpu_texture_memory_footprint_add(GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	memory_usage += gpu_texture_memory_footprint_compute(tex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void gpu_texture_memory_footprint_remove(GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	memory_usage -= gpu_texture_memory_footprint_compute(tex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 12:18:35 +10:00
										 |  |  | uint GPU_texture_memory_usage_get(void) | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	return memory_usage; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | static int gpu_get_component_count(GPUTextureFormat format) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	switch (format) { | 
					
						
							|  |  |  | 		case GPU_RGBA8: | 
					
						
							| 
									
										
										
										
											2018-10-11 15:08:57 +02:00
										 |  |  | 		case GPU_RGBA8UI: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		case GPU_RGBA16F: | 
					
						
							|  |  |  | 		case GPU_RGBA16: | 
					
						
							|  |  |  | 		case GPU_RGBA32F: | 
					
						
							|  |  |  | 			return 4; | 
					
						
							|  |  |  | 		case GPU_RGB16F: | 
					
						
							|  |  |  | 		case GPU_R11F_G11F_B10F: | 
					
						
							|  |  |  | 			return 3; | 
					
						
							|  |  |  | 		case GPU_RG8: | 
					
						
							|  |  |  | 		case GPU_RG16: | 
					
						
							|  |  |  | 		case GPU_RG16F: | 
					
						
							|  |  |  | 		case GPU_RG16I: | 
					
						
							|  |  |  | 		case GPU_RG16UI: | 
					
						
							|  |  |  | 		case GPU_RG32F: | 
					
						
							|  |  |  | 			return 2; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Definitely not complete, edit according to the gl specification. */ | 
					
						
							|  |  |  | static void gpu_validate_data_format(GPUTextureFormat tex_format, GPUDataFormat data_format) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-27 14:15:45 +02:00
										 |  |  | 	(void)data_format; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 	if (ELEM(tex_format, | 
					
						
							|  |  |  | 	         GPU_DEPTH_COMPONENT24, | 
					
						
							|  |  |  | 	         GPU_DEPTH_COMPONENT16, | 
					
						
							|  |  |  | 	         GPU_DEPTH_COMPONENT32F)) | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		BLI_assert(data_format == GPU_DATA_FLOAT); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (tex_format == GPU_DEPTH24_STENCIL8) { | 
					
						
							|  |  |  | 		BLI_assert(data_format == GPU_DATA_UNSIGNED_INT_24_8); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		/* Integer formats */ | 
					
						
							|  |  |  | 		if (ELEM(tex_format, GPU_RG16I, GPU_R16I, GPU_RG16UI, GPU_R16UI, GPU_R32UI)) { | 
					
						
							|  |  |  | 			if (ELEM(tex_format, GPU_R16UI, GPU_RG16UI, GPU_R32UI)) { | 
					
						
							|  |  |  | 				BLI_assert(data_format == GPU_DATA_UNSIGNED_INT); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				BLI_assert(data_format == GPU_DATA_INT); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Byte formats */ | 
					
						
							| 
									
										
										
										
											2018-10-11 15:08:57 +02:00
										 |  |  | 		else if (ELEM(tex_format, GPU_R8, GPU_RG8, GPU_RGBA8, GPU_RGBA8UI)) { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			BLI_assert(ELEM(data_format, GPU_DATA_UNSIGNED_BYTE, GPU_DATA_FLOAT)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Special case */ | 
					
						
							|  |  |  | 		else if (ELEM(tex_format, GPU_R11F_G11F_B10F)) { | 
					
						
							|  |  |  | 			BLI_assert(ELEM(data_format, GPU_DATA_10_11_11_REV, GPU_DATA_FLOAT)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Float formats */ | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			BLI_assert(ELEM(data_format, GPU_DATA_FLOAT)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static GPUDataFormat gpu_get_data_format_from_tex_format(GPUTextureFormat tex_format) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 	if (ELEM(tex_format, | 
					
						
							|  |  |  | 	         GPU_DEPTH_COMPONENT24, | 
					
						
							|  |  |  | 	         GPU_DEPTH_COMPONENT16, | 
					
						
							|  |  |  | 	         GPU_DEPTH_COMPONENT32F)) | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return GPU_DATA_FLOAT; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (tex_format == GPU_DEPTH24_STENCIL8) { | 
					
						
							|  |  |  | 		return GPU_DATA_UNSIGNED_INT_24_8; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		/* Integer formats */ | 
					
						
							|  |  |  | 		if (ELEM(tex_format, GPU_RG16I, GPU_R16I, GPU_RG16UI, GPU_R16UI, GPU_R32UI)) { | 
					
						
							|  |  |  | 			if (ELEM(tex_format, GPU_R16UI, GPU_RG16UI, GPU_R32UI)) { | 
					
						
							|  |  |  | 				return GPU_DATA_UNSIGNED_INT; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				return GPU_DATA_INT; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Byte formats */ | 
					
						
							|  |  |  | 		else if (ELEM(tex_format, GPU_R8)) { | 
					
						
							|  |  |  | 			return GPU_DATA_UNSIGNED_BYTE; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Special case */ | 
					
						
							|  |  |  | 		else if (ELEM(tex_format, GPU_R11F_G11F_B10F)) { | 
					
						
							|  |  |  | 			return GPU_DATA_10_11_11_REV; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			return GPU_DATA_FLOAT; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Definitely not complete, edit according to the gl specification. */ | 
					
						
							|  |  |  | static GLenum gpu_get_gl_dataformat(GPUTextureFormat data_type, GPUTextureFormatFlag *format_flag) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 	if (ELEM(data_type, | 
					
						
							|  |  |  | 	         GPU_DEPTH_COMPONENT24, | 
					
						
							|  |  |  | 	         GPU_DEPTH_COMPONENT16, | 
					
						
							|  |  |  | 	         GPU_DEPTH_COMPONENT32F)) | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 		*format_flag |= GPU_FORMAT_DEPTH; | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		return GL_DEPTH_COMPONENT; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-18 01:26:08 +01:00
										 |  |  | 	else if (data_type == GPU_DEPTH24_STENCIL8) { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 		*format_flag |= GPU_FORMAT_DEPTH | GPU_FORMAT_STENCIL; | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		return GL_DEPTH_STENCIL; | 
					
						
							| 
									
										
										
										
											2017-03-18 01:26:08 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2018-01-13 17:14:01 +01:00
										 |  |  | 		/* Integer formats */ | 
					
						
							| 
									
										
										
										
											2018-05-15 15:19:57 +02:00
										 |  |  | 		if (ELEM(data_type, GPU_RG16I, GPU_R16I, GPU_RG16UI, GPU_R16UI, GPU_R32UI)) { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 			*format_flag |= GPU_FORMAT_INTEGER; | 
					
						
							| 
									
										
										
										
											2018-01-13 17:14:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			switch (gpu_get_component_count(data_type)) { | 
					
						
							|  |  |  | 				case 1: return GL_RED_INTEGER; break; | 
					
						
							|  |  |  | 				case 2: return GL_RG_INTEGER; break; | 
					
						
							|  |  |  | 				case 3: return GL_RGB_INTEGER; break; | 
					
						
							|  |  |  | 				case 4: return GL_RGBA_INTEGER; break; | 
					
						
							| 
									
										
										
										
											2018-01-13 17:14:01 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 		else if (ELEM(data_type, GPU_R8)) { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			*format_flag |= GPU_FORMAT_FLOAT; | 
					
						
							|  |  |  | 			return GL_RED; | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-13 17:14:01 +01:00
										 |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 			*format_flag |= GPU_FORMAT_FLOAT; | 
					
						
							| 
									
										
										
										
											2018-01-13 17:14:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			switch (gpu_get_component_count(data_type)) { | 
					
						
							|  |  |  | 				case 1: return GL_RED; break; | 
					
						
							|  |  |  | 				case 2: return GL_RG; break; | 
					
						
							|  |  |  | 				case 3: return GL_RGB; break; | 
					
						
							|  |  |  | 				case 4: return GL_RGBA; break; | 
					
						
							| 
									
										
										
										
											2018-01-13 17:14:01 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	BLI_assert(0); | 
					
						
							|  |  |  | 	*format_flag |= GPU_FORMAT_FLOAT; | 
					
						
							|  |  |  | 	return GL_RGBA; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 12:18:35 +10:00
										 |  |  | static uint gpu_get_bytesize(GPUTextureFormat data_type) | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 	switch (data_type) { | 
					
						
							| 
									
										
										
										
											2017-05-09 21:49:30 +02:00
										 |  |  | 		case GPU_RGBA32F: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			return 32; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GPU_RG32F: | 
					
						
							|  |  |  | 		case GPU_RGBA16F: | 
					
						
							| 
									
										
										
										
											2018-05-28 12:48:16 +02:00
										 |  |  | 		case GPU_RGBA16: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			return 16; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GPU_RGB16F: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			return 12; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GPU_RG16F: | 
					
						
							| 
									
										
										
										
											2018-01-13 17:14:01 +01:00
										 |  |  | 		case GPU_RG16I: | 
					
						
							| 
									
										
										
										
											2018-05-15 15:19:57 +02:00
										 |  |  | 		case GPU_RG16UI: | 
					
						
							| 
									
										
										
										
											2018-04-30 16:15:01 +02:00
										 |  |  | 		case GPU_RG16: | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GPU_DEPTH24_STENCIL8: | 
					
						
							|  |  |  | 		case GPU_DEPTH_COMPONENT32F: | 
					
						
							| 
									
										
										
										
											2018-10-11 15:08:57 +02:00
										 |  |  | 		case GPU_RGBA8UI: | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GPU_RGBA8: | 
					
						
							| 
									
										
										
										
											2017-05-16 20:18:57 +02:00
										 |  |  | 		case GPU_R11F_G11F_B10F: | 
					
						
							| 
									
										
										
										
											2017-05-20 13:11:21 +02:00
										 |  |  | 		case GPU_R32F: | 
					
						
							| 
									
										
										
										
											2018-04-25 10:59:48 +02:00
										 |  |  | 		case GPU_R32UI: | 
					
						
							| 
									
										
										
										
											2018-05-31 18:34:17 +02:00
										 |  |  | 		case GPU_R32I: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			return 4; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GPU_DEPTH_COMPONENT24: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			return 3; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GPU_DEPTH_COMPONENT16: | 
					
						
							|  |  |  | 		case GPU_R16F: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		case GPU_R16UI: | 
					
						
							| 
									
										
										
										
											2018-03-13 22:36:24 +01:00
										 |  |  | 		case GPU_R16I: | 
					
						
							| 
									
										
										
										
											2017-08-18 14:22:40 +02:00
										 |  |  | 		case GPU_RG8: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			return 2; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		case GPU_R8: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			return 1; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			BLI_assert(!"Texture format incorrect or unsupported\n"); | 
					
						
							|  |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | static GLenum gpu_get_gl_internalformat(GPUTextureFormat format) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	/* You can add any of the available type to this list
 | 
					
						
							|  |  |  | 	 * For available types see GPU_texture.h */ | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	switch (format) { | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		/* Formats texture & renderbuffer */ | 
					
						
							| 
									
										
										
										
											2017-05-09 21:49:30 +02:00
										 |  |  | 		case GPU_RGBA32F: return GL_RGBA32F; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		case GPU_RGBA16F: return GL_RGBA16F; | 
					
						
							| 
									
										
										
										
											2018-05-28 12:48:16 +02:00
										 |  |  | 		case GPU_RGBA16: return GL_RGBA16; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		case GPU_RG32F: return GL_RG32F; | 
					
						
							| 
									
										
										
										
											2017-04-18 12:50:09 +02:00
										 |  |  | 		case GPU_RGB16F: return GL_RGB16F; | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | 		case GPU_RG16F: return GL_RG16F; | 
					
						
							| 
									
										
										
										
											2018-01-13 17:14:01 +01:00
										 |  |  | 		case GPU_RG16I: return GL_RG16I; | 
					
						
							| 
									
										
										
										
											2018-04-30 16:15:01 +02:00
										 |  |  | 		case GPU_RG16: return GL_RG16; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		case GPU_RGBA8: return GL_RGBA8; | 
					
						
							| 
									
										
										
										
											2018-10-11 15:08:57 +02:00
										 |  |  | 		case GPU_RGBA8UI: return GL_RGBA8UI; | 
					
						
							| 
									
										
										
										
											2017-05-20 13:11:21 +02:00
										 |  |  | 		case GPU_R32F: return GL_R32F; | 
					
						
							| 
									
										
										
										
											2018-04-25 10:59:48 +02:00
										 |  |  | 		case GPU_R32UI: return GL_R32UI; | 
					
						
							| 
									
										
										
										
											2018-05-31 18:34:17 +02:00
										 |  |  | 		case GPU_R32I: return GL_R32I; | 
					
						
							| 
									
										
										
										
											2017-04-03 11:04:42 +02:00
										 |  |  | 		case GPU_R16F: return GL_R16F; | 
					
						
							| 
									
										
										
										
											2018-03-13 22:36:24 +01:00
										 |  |  | 		case GPU_R16I: return GL_R16I; | 
					
						
							| 
									
										
										
										
											2018-04-16 19:33:09 +02:00
										 |  |  | 		case GPU_R16UI: return GL_R16UI; | 
					
						
							| 
									
										
										
										
											2017-08-18 14:22:40 +02:00
										 |  |  | 		case GPU_RG8: return GL_RG8; | 
					
						
							| 
									
										
										
										
											2018-05-15 15:19:57 +02:00
										 |  |  | 		case GPU_RG16UI: return GL_RG16UI; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		case GPU_R8: return GL_R8; | 
					
						
							|  |  |  | 		/* Special formats texture & renderbuffer */ | 
					
						
							| 
									
										
										
										
											2017-05-16 20:18:57 +02:00
										 |  |  | 		case GPU_R11F_G11F_B10F: return GL_R11F_G11F_B10F; | 
					
						
							| 
									
										
										
										
											2017-03-18 01:26:08 +01:00
										 |  |  | 		case GPU_DEPTH24_STENCIL8: return GL_DEPTH24_STENCIL8; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		/* Texture only format */ | 
					
						
							|  |  |  | 		/* ** Add Format here **/ | 
					
						
							|  |  |  | 		/* Special formats texture only */ | 
					
						
							|  |  |  | 		/* ** Add Format here **/ | 
					
						
							|  |  |  | 		/* Depth Formats */ | 
					
						
							|  |  |  | 		case GPU_DEPTH_COMPONENT32F: return GL_DEPTH_COMPONENT32F; | 
					
						
							|  |  |  | 		case GPU_DEPTH_COMPONENT24: return GL_DEPTH_COMPONENT24; | 
					
						
							|  |  |  | 		case GPU_DEPTH_COMPONENT16: return GL_DEPTH_COMPONENT16; | 
					
						
							|  |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			BLI_assert(!"Texture format incorrect or unsupported\n"); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | static GLenum gpu_get_gl_datatype(GPUDataFormat format) | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	switch (format) { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		case GPU_DATA_FLOAT: | 
					
						
							|  |  |  | 			return GL_FLOAT; | 
					
						
							|  |  |  | 		case GPU_DATA_INT: | 
					
						
							|  |  |  | 			return GL_INT; | 
					
						
							|  |  |  | 		case GPU_DATA_UNSIGNED_INT: | 
					
						
							|  |  |  | 			return GL_UNSIGNED_INT; | 
					
						
							|  |  |  | 		case GPU_DATA_UNSIGNED_BYTE: | 
					
						
							|  |  |  | 			return GL_UNSIGNED_BYTE; | 
					
						
							|  |  |  | 		case GPU_DATA_UNSIGNED_INT_24_8: | 
					
						
							|  |  |  | 			return GL_UNSIGNED_INT_24_8; | 
					
						
							|  |  |  | 		case GPU_DATA_10_11_11_REV: | 
					
						
							|  |  |  | 			return GL_UNSIGNED_INT_10F_11F_11F_REV; | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			BLI_assert(!"Unhandled data format"); | 
					
						
							|  |  |  | 			return GL_FLOAT; | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | static float *GPU_texture_3D_rescale(GPUTexture *tex, int w, int h, int d, int channels, const float *fpixels) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-12 12:18:35 +10:00
										 |  |  | 	const uint xf = w / tex->w, yf = h / tex->h, zf = d / tex->d; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	float *nfpixels = MEM_mallocN(channels * sizeof(float) * tex->w * tex->h * tex->d, "GPUTexture Rescaled 3Dtex"); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	if (nfpixels) { | 
					
						
							|  |  |  | 		GPU_print_error_debug("You need to scale a 3D texture, feel the pain!"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 12:18:35 +10:00
										 |  |  | 		for (uint k = 0; k < tex->d; k++) { | 
					
						
							|  |  |  | 			for (uint j = 0; j < tex->h; j++) { | 
					
						
							|  |  |  | 				for (uint i = 0; i < tex->w; i++) { | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 					/* obviously doing nearest filtering here,
 | 
					
						
							|  |  |  | 					 * it's going to be slow in any case, let's not make it worse */ | 
					
						
							|  |  |  | 					float xb = i * xf; | 
					
						
							|  |  |  | 					float yb = j * yf; | 
					
						
							|  |  |  | 					float zb = k * zf; | 
					
						
							| 
									
										
										
										
											2018-09-12 12:18:35 +10:00
										 |  |  | 					uint offset = k * (tex->w * tex->h) + i * tex->h + j; | 
					
						
							|  |  |  | 					uint offset_orig = (zb) * (w * h) + (xb) * h + (yb); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					if (channels == 4) { | 
					
						
							|  |  |  | 						nfpixels[offset * 4] = fpixels[offset_orig * 4]; | 
					
						
							|  |  |  | 						nfpixels[offset * 4 + 1] = fpixels[offset_orig * 4 + 1]; | 
					
						
							|  |  |  | 						nfpixels[offset * 4 + 2] = fpixels[offset_orig * 4 + 2]; | 
					
						
							|  |  |  | 						nfpixels[offset * 4 + 3] = fpixels[offset_orig * 4 + 3]; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					else | 
					
						
							|  |  |  | 						nfpixels[offset] = fpixels[offset_orig]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nfpixels; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | /* This tries to allocate video memory for a given texture
 | 
					
						
							|  |  |  |  * If alloc fails, lower the resolution until it fits. */ | 
					
						
							| 
									
										
										
										
											2017-04-12 20:23:30 +10:00
										 |  |  | static bool gpu_texture_try_alloc( | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  |         GPUTexture *tex, GLenum proxy, GLenum internalformat, GLenum data_format, GLenum data_type, | 
					
						
							| 
									
										
										
										
											2017-03-18 01:26:08 +01:00
										 |  |  |         int channels, bool try_rescale, const float *fpixels, float **rescaled_fpixels) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	int r_width; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	switch (proxy) { | 
					
						
							|  |  |  | 		case GL_PROXY_TEXTURE_1D: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			glTexImage1D(proxy, 0, internalformat, tex->w, 0, data_format, data_type, NULL); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2017-04-10 12:06:17 +02:00
										 |  |  | 		case GL_PROXY_TEXTURE_1D_ARRAY: | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		case GL_PROXY_TEXTURE_2D: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			glTexImage2D(proxy, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, NULL); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2017-04-10 12:06:17 +02:00
										 |  |  | 		case GL_PROXY_TEXTURE_2D_ARRAY: | 
					
						
							| 
									
										
										
										
											2018-07-27 13:56:07 +02:00
										 |  |  | 			/* HACK: Some driver wrongly check GL_PROXY_TEXTURE_2D_ARRAY as a GL_PROXY_TEXTURE_3D
 | 
					
						
							|  |  |  | 			 * checking all dimensions against GPU_max_texture_layers (see T55888). */ | 
					
						
							| 
									
										
										
										
											2018-07-27 17:50:14 +02:00
										 |  |  | 			return (tex->w > 0) && (tex->w <= GPU_max_texture_size()) && | 
					
						
							|  |  |  | 			       (tex->h > 0) && (tex->h <= GPU_max_texture_size()) && | 
					
						
							|  |  |  | 			       (tex->d > 0) && (tex->d <= GPU_max_texture_layers()); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		case GL_PROXY_TEXTURE_3D: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			glTexImage3D(proxy, 0, internalformat, tex->w, tex->h, tex->d, 0, data_format, data_type, NULL); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glGetTexLevelParameteriv(proxy, 0, GL_TEXTURE_WIDTH, &r_width); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (r_width == 0 && try_rescale) { | 
					
						
							|  |  |  | 		const int w = tex->w, h = tex->h, d = tex->d; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Find largest texture possible */ | 
					
						
							|  |  |  | 		while (r_width == 0) { | 
					
						
							|  |  |  | 			tex->w /= 2; | 
					
						
							|  |  |  | 			tex->h /= 2; | 
					
						
							|  |  |  | 			tex->d /= 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* really unlikely to happen but keep this just in case */ | 
					
						
							|  |  |  | 			if (tex->w == 0) break; | 
					
						
							|  |  |  | 			if (tex->h == 0 && proxy != GL_PROXY_TEXTURE_1D) break; | 
					
						
							|  |  |  | 			if (tex->d == 0 && proxy == GL_PROXY_TEXTURE_3D) break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (proxy == GL_PROXY_TEXTURE_1D) | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 				glTexImage1D(proxy, 0, internalformat, tex->w, 0, data_format, data_type, NULL); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 			else if (proxy == GL_PROXY_TEXTURE_2D) | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 				glTexImage2D(proxy, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, NULL); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 			else if (proxy == GL_PROXY_TEXTURE_3D) | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 				glTexImage3D(proxy, 0, internalformat, tex->w, tex->h, tex->d, 0, data_format, data_type, NULL); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &r_width); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Rescale */ | 
					
						
							|  |  |  | 		if (r_width > 0) { | 
					
						
							|  |  |  | 			switch (proxy) { | 
					
						
							|  |  |  | 				case GL_PROXY_TEXTURE_1D: | 
					
						
							|  |  |  | 				case GL_PROXY_TEXTURE_2D: | 
					
						
							|  |  |  | 					/* Do nothing for now */ | 
					
						
							|  |  |  | 					return false; | 
					
						
							|  |  |  | 				case GL_PROXY_TEXTURE_3D: | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 					BLI_assert(data_type == GL_FLOAT); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 					*rescaled_fpixels = GPU_texture_3D_rescale(tex, w, h, d, channels, fpixels); | 
					
						
							|  |  |  | 					return (bool)*rescaled_fpixels; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (r_width > 0); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | GPUTexture *GPU_texture_create_nD( | 
					
						
							|  |  |  |         int w, int h, int d, int n, const void *pixels, | 
					
						
							|  |  |  |         GPUTextureFormat tex_format, GPUDataFormat gpu_data_format, int samples, | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  |         const bool can_rescale, char err_out[256]) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (samples) { | 
					
						
							|  |  |  | 		CLAMP_MAX(samples, GPU_max_color_texture_samples()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 16:32:02 -05:00
										 |  |  | 	GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture"); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	tex->w = w; | 
					
						
							|  |  |  | 	tex->h = h; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	tex->d = d; | 
					
						
							| 
									
										
										
										
											2018-03-14 03:20:39 +01:00
										 |  |  | 	tex->samples = samples; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	tex->number = -1; | 
					
						
							|  |  |  | 	tex->refcount = 1; | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	tex->format = tex_format; | 
					
						
							|  |  |  | 	tex->components = gpu_get_component_count(tex_format); | 
					
						
							|  |  |  | 	tex->bytesize = gpu_get_bytesize(tex_format); | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	tex->format_flag = 0; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-16 15:11:38 -04:00
										 |  |  | 	if (n == 2) { | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		if (d == 0) | 
					
						
							|  |  |  | 			tex->target_base = tex->target = GL_TEXTURE_2D; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			tex->target_base = tex->target = GL_TEXTURE_2D_ARRAY; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-16 15:11:38 -04:00
										 |  |  | 	else if (n == 1) { | 
					
						
							|  |  |  | 		if (h == 0) | 
					
						
							|  |  |  | 			tex->target_base = tex->target = GL_TEXTURE_1D; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			tex->target_base = tex->target = GL_TEXTURE_1D_ARRAY; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	else if (n == 3) { | 
					
						
							|  |  |  | 		tex->target_base = tex->target = GL_TEXTURE_3D; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-16 15:11:38 -04:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		/* should never happen */ | 
					
						
							|  |  |  | 		MEM_freeN(tex); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	gpu_validate_data_format(tex_format, gpu_data_format); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	if (samples && n == 2 && d == 0) | 
					
						
							|  |  |  | 		tex->target = GL_TEXTURE_2D_MULTISAMPLE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	GLenum internalformat = gpu_get_gl_internalformat(tex_format); | 
					
						
							|  |  |  | 	GLenum data_format = gpu_get_gl_dataformat(tex_format, &tex->format_flag); | 
					
						
							|  |  |  | 	GLenum data_type = gpu_get_gl_datatype(gpu_data_format); | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	gpu_texture_memory_footprint_add(tex); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Generate Texture object */ | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | 	tex->bindcode = GPU_tex_alloc(); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!tex->bindcode) { | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		if (err_out) | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			BLI_snprintf(err_out, 256, "GPUTexture: texture create failed\n"); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			fprintf(stderr, "GPUTexture: texture create failed\n"); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		GPU_texture_free(tex); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glBindTexture(tex->target, tex->bindcode); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	/* Check if texture fit in VRAM */ | 
					
						
							| 
									
										
										
										
											2017-04-16 15:11:38 -04:00
										 |  |  | 	GLenum proxy = GL_PROXY_TEXTURE_2D; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (n == 2) { | 
					
						
							|  |  |  | 		if (d > 0) | 
					
						
							|  |  |  | 			proxy = GL_PROXY_TEXTURE_2D_ARRAY; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (n == 1) { | 
					
						
							| 
									
										
										
										
											2017-04-10 12:06:17 +02:00
										 |  |  | 		if (h == 0) | 
					
						
							|  |  |  | 			proxy = GL_PROXY_TEXTURE_1D; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			proxy = GL_PROXY_TEXTURE_1D_ARRAY; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-10 12:06:17 +02:00
										 |  |  | 	else if (n == 3) { | 
					
						
							|  |  |  | 		proxy = GL_PROXY_TEXTURE_3D; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	float *rescaled_pixels = NULL; | 
					
						
							|  |  |  | 	bool valid = gpu_texture_try_alloc(tex, proxy, internalformat, data_format, data_type, tex->components, can_rescale, | 
					
						
							|  |  |  | 	                                   pixels, &rescaled_pixels); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	if (!valid) { | 
					
						
							|  |  |  | 		if (err_out) | 
					
						
							|  |  |  | 			BLI_snprintf(err_out, 256, "GPUTexture: texture alloc failed"); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			fprintf(stderr, "GPUTexture: texture alloc failed. Not enough Video Memory."); | 
					
						
							|  |  |  | 		GPU_texture_free(tex); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	/* Upload Texture */ | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	const float *pix = (rescaled_pixels) ? rescaled_pixels : pixels; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-16 15:11:38 -04:00
										 |  |  | 	if (tex->target == GL_TEXTURE_2D || | 
					
						
							|  |  |  | 	    tex->target == GL_TEXTURE_2D_MULTISAMPLE || | 
					
						
							|  |  |  | 	    tex->target == GL_TEXTURE_1D_ARRAY) | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		if (samples) { | 
					
						
							|  |  |  | 			glTexImage2DMultisample(tex->target, samples, internalformat, tex->w, tex->h, true); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 			if (pix) | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 				glTexSubImage2D(tex->target, 0, 0, 0, tex->w, tex->h, data_format, data_type, pix); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			glTexImage2D(tex->target, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, pix); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-16 15:11:38 -04:00
										 |  |  | 	else if (tex->target == GL_TEXTURE_1D) { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		glTexImage1D(tex->target, 0, internalformat, tex->w, 0, data_format, data_type, pix); | 
					
						
							| 
									
										
										
										
											2017-04-16 15:11:38 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		glTexImage3D(tex->target, 0, internalformat, tex->w, tex->h, tex->d, 0, data_format, data_type, pix); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	if (rescaled_pixels) | 
					
						
							|  |  |  | 		MEM_freeN(rescaled_pixels); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	/* Texture Parameters */ | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	if (GPU_texture_stencil(tex) || /* Does not support filtering */ | 
					
						
							| 
									
										
										
										
											2018-04-19 07:47:03 +02:00
										 |  |  | 	    GPU_texture_integer(tex) || /* Does not support filtering */ | 
					
						
							|  |  |  | 	    GPU_texture_depth(tex)) | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	if (GPU_texture_depth(tex)) { | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_MODE, GL_NONE); | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 
					
						
							| 
									
										
										
										
											2017-04-16 15:11:38 -04:00
										 |  |  | 	if (n > 1) { | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-16 15:11:38 -04:00
										 |  |  | 	if (n > 2) { | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	glBindTexture(tex->target, 0); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return tex; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | static GPUTexture *GPU_texture_cube_create( | 
					
						
							|  |  |  |         int w, int d, | 
					
						
							|  |  |  |         const float *fpixels_px, const float *fpixels_py, const float *fpixels_pz, | 
					
						
							|  |  |  |         const float *fpixels_nx, const float *fpixels_ny, const float *fpixels_nz, | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  |         GPUTextureFormat tex_format, GPUDataFormat gpu_data_format, | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  |         char err_out[256]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture"); | 
					
						
							|  |  |  | 	tex->w = w; | 
					
						
							|  |  |  | 	tex->h = w; | 
					
						
							|  |  |  | 	tex->d = d; | 
					
						
							| 
									
										
										
										
											2018-03-14 03:20:39 +01:00
										 |  |  | 	tex->samples = 0; | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 	tex->number = -1; | 
					
						
							|  |  |  | 	tex->refcount = 1; | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	tex->format = tex_format; | 
					
						
							|  |  |  | 	tex->components = gpu_get_component_count(tex_format); | 
					
						
							|  |  |  | 	tex->bytesize = gpu_get_bytesize(tex_format); | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	tex->format_flag = GPU_FORMAT_CUBE; | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (d == 0) { | 
					
						
							|  |  |  | 		tex->target_base = tex->target = GL_TEXTURE_CUBE_MAP; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		BLI_assert(false && "Cubemap array Not implemented yet"); | 
					
						
							|  |  |  | 		// tex->target_base = tex->target = GL_TEXTURE_CUBE_MAP_ARRAY;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	GLenum internalformat = gpu_get_gl_internalformat(tex_format); | 
					
						
							|  |  |  | 	GLenum data_format = gpu_get_gl_dataformat(tex_format, &tex->format_flag); | 
					
						
							|  |  |  | 	GLenum data_type = gpu_get_gl_datatype(gpu_data_format); | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	gpu_texture_memory_footprint_add(tex); | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Generate Texture object */ | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | 	tex->bindcode = GPU_tex_alloc(); | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!tex->bindcode) { | 
					
						
							|  |  |  | 		if (err_out) | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			BLI_snprintf(err_out, 256, "GPUTexture: texture create failed\n"); | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			fprintf(stderr, "GPUTexture: texture create failed\n"); | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 		GPU_texture_free(tex); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glBindTexture(tex->target, tex->bindcode); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Upload Texture */ | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, fpixels_px); | 
					
						
							|  |  |  | 	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, fpixels_py); | 
					
						
							|  |  |  | 	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, fpixels_pz); | 
					
						
							|  |  |  | 	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, fpixels_nx); | 
					
						
							|  |  |  | 	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, fpixels_ny); | 
					
						
							|  |  |  | 	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, fpixels_nz); | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Texture Parameters */ | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	if (GPU_texture_stencil(tex) || /* Does not support filtering */ | 
					
						
							|  |  |  | 	    GPU_texture_integer(tex) || /* Does not support filtering */ | 
					
						
							|  |  |  | 	    GPU_texture_depth(tex)) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	if (GPU_texture_depth(tex)) { | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_MODE, GL_NONE); | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 
					
						
							|  |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 
					
						
							|  |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	glBindTexture(tex->target, 0); | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return tex; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | /* Special buffer textures. tex_format must be compatible with the buffer content. */ | 
					
						
							|  |  |  | GPUTexture *GPU_texture_create_buffer(GPUTextureFormat tex_format, const GLuint buffer) | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture"); | 
					
						
							|  |  |  | 	tex->number = -1; | 
					
						
							|  |  |  | 	tex->refcount = 1; | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	tex->format = tex_format; | 
					
						
							|  |  |  | 	tex->components = gpu_get_component_count(tex_format); | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 	tex->format_flag = 0; | 
					
						
							|  |  |  | 	tex->target_base = tex->target = GL_TEXTURE_BUFFER; | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	tex->bytesize = gpu_get_bytesize(tex_format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	GLenum internalformat = gpu_get_gl_internalformat(tex_format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	gpu_get_gl_dataformat(tex_format, &tex->format_flag); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!(ELEM(tex_format, GPU_R8, GPU_R16) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_R16F, GPU_R32F) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_R8I, GPU_R16I, GPU_R32I) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_R8UI, GPU_R16UI, GPU_R32UI) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_RG8, GPU_RG16) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_RG16F, GPU_RG32F) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_RG8I, GPU_RG16I, GPU_RG32I) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_RG8UI, GPU_RG16UI, GPU_RG32UI) || | 
					
						
							|  |  |  | 	      //ELEM(tex_format, GPU_RGB32F, GPU_RGB32I, GPU_RGB32UI) || /* Not available until gl 4.0 */
 | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_RGBA8, GPU_RGBA16) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_RGBA16F, GPU_RGBA32F) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_RGBA8I, GPU_RGBA16I, GPU_RGBA32I) || | 
					
						
							|  |  |  | 	      ELEM(tex_format, GPU_RGBA8UI, GPU_RGBA16UI, GPU_RGBA32UI))) | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		fprintf(stderr, "GPUTexture: invalid format for texture buffer\n"); | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 		GPU_texture_free(tex); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Generate Texture object */ | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | 	tex->bindcode = GPU_tex_alloc(); | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!tex->bindcode) { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		fprintf(stderr, "GPUTexture: texture create failed\n"); | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 		GPU_texture_free(tex); | 
					
						
							| 
									
										
										
										
											2018-09-24 18:46:51 +02:00
										 |  |  | 		BLI_assert(0 && "glGenTextures failed: Are you sure a valid OGL context is active on this thread?\n"); | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glBindTexture(tex->target, tex->bindcode); | 
					
						
							|  |  |  | 	glTexBuffer(tex->target, internalformat, buffer); | 
					
						
							|  |  |  | 	glBindTexture(tex->target, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return tex; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-11 22:30:59 +02:00
										 |  |  | GPUTexture *GPU_texture_from_bindcode(int textarget, int bindcode) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-09 12:29:14 +03:00
										 |  |  | 	/* see GPUInput::textarget: it can take two values - GL_TEXTURE_2D and GL_TEXTURE_CUBE_MAP
 | 
					
						
							|  |  |  | 	 * these values are correct for glDisable, so textarget can be safely used in | 
					
						
							|  |  |  | 	 * GPU_texture_bind/GPU_texture_unbind through tex->target_base */ | 
					
						
							| 
									
										
										
										
											2017-04-27 14:32:47 -04:00
										 |  |  | 	/* (is any of this obsolete now that we don't glEnable/Disable textures?) */ | 
					
						
							| 
									
										
										
										
											2016-01-07 16:32:02 -05:00
										 |  |  | 	GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture"); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	tex->bindcode = bindcode; | 
					
						
							|  |  |  | 	tex->number = -1; | 
					
						
							|  |  |  | 	tex->refcount = 1; | 
					
						
							| 
									
										
										
											
												World textures displaying for viewport in BI.
This patch supports "Image or Movie" and "Environment map" types of world texture for the viewport.
It supports:
  - "View", "AngMap" and "Equirectangular" types of mapping.
  - Different types of texture blending (according to BI world render).
  - Same color blending as when it lacked textures (but render via glsl).
{F207734}
{F207735}
Example: {F275180}
Original author: @valentin_b4w
Regards,
Alexander (Blend4Web Team).
Reviewers: sergey, valentin_b4w, brecht, merwin
Reviewed By: merwin
Subscribers: campbellbarton, merwin, blueprintrandom, youle, a.romanov, yurikovelenov, AlexKowel, Evgeny_Rodygin
Projects: #rendering, #opengl_gfx, #bf_blender:_next
Differential Revision: https://developer.blender.org/D1414
											
										 
											2016-01-27 12:06:57 +03:00
										 |  |  | 	tex->target = textarget; | 
					
						
							| 
									
										
										
										
											2016-08-09 12:29:14 +03:00
										 |  |  | 	tex->target_base = textarget; | 
					
						
							| 
									
										
										
										
											2017-05-16 02:59:25 +02:00
										 |  |  | 	tex->format = -1; | 
					
						
							| 
									
										
										
										
											2017-08-22 10:22:11 +02:00
										 |  |  | 	tex->components = -1; | 
					
						
							| 
									
										
										
										
											2018-03-14 03:20:39 +01:00
										 |  |  | 	tex->samples = 0; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!glIsTexture(tex->bindcode)) { | 
					
						
							| 
									
										
										
										
											2018-06-11 22:34:35 +02:00
										 |  |  | 		GPU_print_error_debug("Blender Texture Not Loaded"); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2017-05-19 14:15:49 +02:00
										 |  |  | 		GLint w, h; | 
					
						
							| 
									
										
										
										
											2016-01-07 16:32:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
											
												World textures displaying for viewport in BI.
This patch supports "Image or Movie" and "Environment map" types of world texture for the viewport.
It supports:
  - "View", "AngMap" and "Equirectangular" types of mapping.
  - Different types of texture blending (according to BI world render).
  - Same color blending as when it lacked textures (but render via glsl).
{F207734}
{F207735}
Example: {F275180}
Original author: @valentin_b4w
Regards,
Alexander (Blend4Web Team).
Reviewers: sergey, valentin_b4w, brecht, merwin
Reviewed By: merwin
Subscribers: campbellbarton, merwin, blueprintrandom, youle, a.romanov, yurikovelenov, AlexKowel, Evgeny_Rodygin
Projects: #rendering, #opengl_gfx, #bf_blender:_next
Differential Revision: https://developer.blender.org/D1414
											
										 
											2016-01-27 12:06:57 +03:00
										 |  |  | 		GLenum gettarget; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (textarget == GL_TEXTURE_2D) | 
					
						
							|  |  |  | 			gettarget = GL_TEXTURE_2D; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			gettarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		glBindTexture(textarget, tex->bindcode); | 
					
						
							|  |  |  | 		glGetTexLevelParameteriv(gettarget, 0, GL_TEXTURE_WIDTH, &w); | 
					
						
							|  |  |  | 		glGetTexLevelParameteriv(gettarget, 0, GL_TEXTURE_HEIGHT, &h); | 
					
						
							| 
									
										
										
										
											2017-05-12 18:02:31 +02:00
										 |  |  | 		tex->w = w; | 
					
						
							|  |  |  | 		tex->h = h; | 
					
						
							| 
									
										
										
										
											2018-06-11 22:30:59 +02:00
										 |  |  | 		glBindTexture(textarget, 0); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return tex; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GPUTexture *GPU_texture_from_preview(PreviewImage *prv, int mipmap) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	GPUTexture *tex = prv->gputexture[0]; | 
					
						
							|  |  |  | 	GLuint bindcode = 0; | 
					
						
							| 
									
										
										
										
											2018-06-04 09:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	if (tex) | 
					
						
							|  |  |  | 		bindcode = tex->bindcode; | 
					
						
							| 
									
										
										
										
											2018-06-04 09:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	/* this binds a texture, so that's why we restore it to 0 */ | 
					
						
							|  |  |  | 	if (bindcode == 0) { | 
					
						
							| 
									
										
										
											
												World textures displaying for viewport in BI.
This patch supports "Image or Movie" and "Environment map" types of world texture for the viewport.
It supports:
  - "View", "AngMap" and "Equirectangular" types of mapping.
  - Different types of texture blending (according to BI world render).
  - Same color blending as when it lacked textures (but render via glsl).
{F207734}
{F207735}
Example: {F275180}
Original author: @valentin_b4w
Regards,
Alexander (Blend4Web Team).
Reviewers: sergey, valentin_b4w, brecht, merwin
Reviewed By: merwin
Subscribers: campbellbarton, merwin, blueprintrandom, youle, a.romanov, yurikovelenov, AlexKowel, Evgeny_Rodygin
Projects: #rendering, #opengl_gfx, #bf_blender:_next
Differential Revision: https://developer.blender.org/D1414
											
										 
											2016-01-27 12:06:57 +03:00
										 |  |  | 		GPU_create_gl_tex(&bindcode, prv->rect[0], NULL, prv->w[0], prv->h[0], GL_TEXTURE_2D, mipmap, 0, NULL); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (tex) { | 
					
						
							|  |  |  | 		tex->bindcode = bindcode; | 
					
						
							|  |  |  | 		glBindTexture(GL_TEXTURE_2D, 0); | 
					
						
							|  |  |  | 		return tex; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture"); | 
					
						
							|  |  |  | 	tex->bindcode = bindcode; | 
					
						
							|  |  |  | 	tex->number = -1; | 
					
						
							|  |  |  | 	tex->refcount = 1; | 
					
						
							|  |  |  | 	tex->target = GL_TEXTURE_2D; | 
					
						
							|  |  |  | 	tex->target_base = GL_TEXTURE_2D; | 
					
						
							| 
									
										
										
										
											2017-05-16 02:59:25 +02:00
										 |  |  | 	tex->format = -1; | 
					
						
							| 
									
										
										
										
											2017-08-22 10:22:11 +02:00
										 |  |  | 	tex->components = -1; | 
					
						
							| 
									
										
										
										
											2018-06-04 09:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	prv->gputexture[0] = tex; | 
					
						
							| 
									
										
										
										
											2018-06-04 09:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	if (!glIsTexture(tex->bindcode)) { | 
					
						
							| 
									
										
										
										
											2016-08-19 00:52:52 -04:00
										 |  |  | 		GPU_print_error_debug("Blender Texture Not Loaded"); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2016-01-07 16:32:02 -05:00
										 |  |  | 		GLint w, h; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		glBindTexture(GL_TEXTURE_2D, tex->bindcode); | 
					
						
							|  |  |  | 		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w); | 
					
						
							|  |  |  | 		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h); | 
					
						
							| 
									
										
										
										
											2018-06-04 09:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		tex->w = w; | 
					
						
							|  |  |  | 		tex->h = h; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-04 09:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	glBindTexture(GL_TEXTURE_2D, 0); | 
					
						
							| 
									
										
										
										
											2018-06-04 09:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	return tex; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | GPUTexture *GPU_texture_create_1D( | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  |         int w, GPUTextureFormat tex_format, const float *pixels, char err_out[256]) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	GPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); | 
					
						
							|  |  |  | 	return GPU_texture_create_nD(w, 0, 0, 1, pixels, tex_format, data_format, 0, false, err_out); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-10 15:13:39 +02:00
										 |  |  | GPUTexture *GPU_texture_create_1D_array( | 
					
						
							|  |  |  |         int w, int h, GPUTextureFormat tex_format, const float *pixels, char err_out[256]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	GPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); | 
					
						
							|  |  |  | 	return GPU_texture_create_nD(w, h, 0, 1, pixels, tex_format, data_format, 0, false, err_out); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | GPUTexture *GPU_texture_create_2D( | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  |         int w, int h, GPUTextureFormat tex_format, const float *pixels, char err_out[256]) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	GPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); | 
					
						
							|  |  |  | 	return GPU_texture_create_nD(w, h, 0, 2, pixels, tex_format, data_format, 0, false, err_out); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | GPUTexture *GPU_texture_create_2D_multisample( | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  |         int w, int h, GPUTextureFormat tex_format, const float *pixels, int samples, char err_out[256]) | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	GPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); | 
					
						
							|  |  |  | 	return GPU_texture_create_nD(w, h, 0, 2, pixels, tex_format, data_format, samples, false, err_out); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | GPUTexture *GPU_texture_create_2D_array( | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  |         int w, int h, int d, GPUTextureFormat tex_format, const float *pixels, char err_out[256]) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	GPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); | 
					
						
							|  |  |  | 	return GPU_texture_create_nD(w, h, d, 2, pixels, tex_format, data_format, 0, false, err_out); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | GPUTexture *GPU_texture_create_3D( | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  |         int w, int h, int d, GPUTextureFormat tex_format, const float *pixels, char err_out[256]) | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	GPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); | 
					
						
							|  |  |  | 	return GPU_texture_create_nD(w, h, d, 3, pixels, tex_format, data_format, 0, true, err_out); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | GPUTexture *GPU_texture_create_cube( | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  |         int w, GPUTextureFormat tex_format, const float *fpixels, char err_out[256]) | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	const float *fpixels_px, *fpixels_py, *fpixels_pz, *fpixels_nx, *fpixels_ny, *fpixels_nz; | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	const int channels = gpu_get_component_count(tex_format); | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (fpixels) { | 
					
						
							| 
									
										
										
										
											2018-10-22 12:58:11 +02:00
										 |  |  | 		int face_ofs = w * w * channels; | 
					
						
							|  |  |  | 		fpixels_px = fpixels + 0 * face_ofs; | 
					
						
							|  |  |  | 		fpixels_nx = fpixels + 1 * face_ofs; | 
					
						
							|  |  |  | 		fpixels_py = fpixels + 2 * face_ofs; | 
					
						
							|  |  |  | 		fpixels_ny = fpixels + 3 * face_ofs; | 
					
						
							|  |  |  | 		fpixels_pz = fpixels + 4 * face_ofs; | 
					
						
							|  |  |  | 		fpixels_nz = fpixels + 5 * face_ofs; | 
					
						
							| 
									
										
										
										
											2017-04-04 14:48:42 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		fpixels_px = fpixels_py = fpixels_pz = fpixels_nx = fpixels_ny = fpixels_nz = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 03:35:25 +02:00
										 |  |  | 	return GPU_texture_cube_create(w, 0, fpixels_px, fpixels_py, fpixels_pz, fpixels_nx, fpixels_ny, fpixels_nz, | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	                               tex_format, GPU_DATA_FLOAT, err_out); | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | GPUTexture *GPU_texture_create_from_vertbuf(GPUVertBuf *vert) | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPUVertFormat *format = &vert->format; | 
					
						
							|  |  |  | 	GPUVertAttr *attr = &format->attribs[0]; | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Detect incompatible cases (not supported by texture buffers) */ | 
					
						
							| 
									
										
										
										
											2018-07-08 13:05:41 +02:00
										 |  |  | 	BLI_assert(format->attr_len == 1 && vert->vbo_id != 0); | 
					
						
							|  |  |  | 	BLI_assert(attr->comp_len != 3); /* Not until OGL 4.0 */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	BLI_assert(attr->comp_type != GPU_COMP_I10); | 
					
						
							|  |  |  | 	BLI_assert(attr->fetch_mode != GPU_FETCH_INT_TO_FLOAT); | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 12:18:35 +10:00
										 |  |  | 	uint byte_per_comp = attr->sz / attr->comp_len; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	bool is_uint = ELEM(attr->comp_type, GPU_COMP_U8, GPU_COMP_U16, GPU_COMP_U32); | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Cannot fetch signed int or 32bit ints as normalized float. */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	if (attr->fetch_mode == GPU_FETCH_INT_TO_FLOAT_UNIT) { | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 		BLI_assert(is_uint || byte_per_comp <= 2); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	GPUTextureFormat data_type; | 
					
						
							|  |  |  | 	switch (attr->fetch_mode) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		case GPU_FETCH_FLOAT: | 
					
						
							| 
									
										
										
										
											2018-07-08 13:05:41 +02:00
										 |  |  | 			switch (attr->comp_len) { | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 				case 1: data_type = GPU_R32F; break; | 
					
						
							|  |  |  | 				case 2: data_type = GPU_RG32F; break; | 
					
						
							|  |  |  | 				// case 3: data_type = GPU_RGB32F; break; /* Not supported */
 | 
					
						
							|  |  |  | 				default: data_type = GPU_RGBA32F; break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		case GPU_FETCH_INT: | 
					
						
							| 
									
										
										
										
											2018-07-08 13:05:41 +02:00
										 |  |  | 			switch (attr->comp_len) { | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 				case 1: | 
					
						
							|  |  |  | 					switch (byte_per_comp) { | 
					
						
							|  |  |  | 						case 1: data_type = (is_uint) ? GPU_R8UI : GPU_R8I; break; | 
					
						
							|  |  |  | 						case 2: data_type = (is_uint) ? GPU_R16UI : GPU_R16I; break; | 
					
						
							|  |  |  | 						default: data_type = (is_uint) ? GPU_R32UI : GPU_R32I; break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case 2: | 
					
						
							|  |  |  | 					switch (byte_per_comp) { | 
					
						
							|  |  |  | 						case 1: data_type = (is_uint) ? GPU_RG8UI : GPU_RG8I; break; | 
					
						
							|  |  |  | 						case 2: data_type = (is_uint) ? GPU_RG16UI : GPU_RG16I; break; | 
					
						
							|  |  |  | 						default: data_type = (is_uint) ? GPU_RG32UI : GPU_RG32I; break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				default: | 
					
						
							|  |  |  | 					switch (byte_per_comp) { | 
					
						
							|  |  |  | 						case 1: data_type = (is_uint) ? GPU_RGBA8UI : GPU_RGBA8I; break; | 
					
						
							|  |  |  | 						case 2: data_type = (is_uint) ? GPU_RGBA16UI : GPU_RGBA16I; break; | 
					
						
							|  |  |  | 						default: data_type = (is_uint) ? GPU_RGBA32UI : GPU_RGBA32I; break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		case GPU_FETCH_INT_TO_FLOAT_UNIT: | 
					
						
							| 
									
										
										
										
											2018-07-08 13:05:41 +02:00
										 |  |  | 			switch (attr->comp_len) { | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 				case 1: data_type = (byte_per_comp == 1) ? GPU_R8 : GPU_R16; break; | 
					
						
							|  |  |  | 				case 2: data_type = (byte_per_comp == 1) ? GPU_RG8 : GPU_RG16; break; | 
					
						
							|  |  |  | 				default: data_type = (byte_per_comp == 1) ? GPU_RGBA8 : GPU_RGBA16; break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			BLI_assert(0); | 
					
						
							| 
									
										
										
										
											2018-05-22 10:54:31 +02:00
										 |  |  | 			return NULL; | 
					
						
							| 
									
										
										
										
											2018-05-13 22:12:32 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return GPU_texture_create_buffer(data_type, vert->vbo_id); | 
					
						
							| 
									
										
										
										
											2017-11-13 23:25:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | void GPU_texture_add_mipmap( | 
					
						
							|  |  |  |         GPUTexture *tex, GPUDataFormat gpu_data_format, int miplvl, const void *pixels) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BLI_assert((int)tex->format > -1); | 
					
						
							|  |  |  | 	BLI_assert(tex->components > -1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	gpu_validate_data_format(tex->format, gpu_data_format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	GLenum internalformat = gpu_get_gl_internalformat(tex->format); | 
					
						
							|  |  |  | 	GLenum data_format = gpu_get_gl_dataformat(tex->format, &tex->format_flag); | 
					
						
							|  |  |  | 	GLenum data_type = gpu_get_gl_datatype(gpu_data_format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glBindTexture(tex->target, tex->bindcode); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int size[3]; | 
					
						
							|  |  |  | 	GPU_texture_get_mipmap_size(tex, miplvl, size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (tex->target) { | 
					
						
							|  |  |  | 		case GL_TEXTURE_1D: | 
					
						
							|  |  |  | 			glTexImage1D(tex->target, miplvl, internalformat, size[0], 0, data_format, data_type, pixels); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case GL_TEXTURE_2D: | 
					
						
							|  |  |  | 		case GL_TEXTURE_1D_ARRAY: | 
					
						
							|  |  |  | 			glTexImage2D(tex->target, miplvl, internalformat, size[0], size[1], 0, data_format, data_type, pixels); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case GL_TEXTURE_3D: | 
					
						
							|  |  |  | 		case GL_TEXTURE_2D_ARRAY: | 
					
						
							|  |  |  | 			glTexImage3D(tex->target, miplvl, internalformat, size[0], size[1], size[2], 0, data_format, data_type, pixels); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case GL_TEXTURE_2D_MULTISAMPLE: | 
					
						
							|  |  |  | 			/* Multisample textures cannot have mipmaps. */ | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			BLI_assert(!"tex->target mode not supported"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glTexParameteri(GPU_texture_target(tex), GL_TEXTURE_MAX_LEVEL, miplvl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glBindTexture(tex->target, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  | void GPU_texture_update_sub( | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  |         GPUTexture *tex, GPUDataFormat gpu_data_format, const void *pixels, | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  |         int offset_x, int offset_y, int offset_z, int width, int height, int depth) | 
					
						
							| 
									
										
										
										
											2017-08-22 10:22:11 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-06-22 08:10:59 +02:00
										 |  |  | 	BLI_assert((int)tex->format > -1); | 
					
						
							| 
									
										
										
										
											2017-08-22 10:22:11 +02:00
										 |  |  | 	BLI_assert(tex->components > -1); | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	GLenum data_format = gpu_get_gl_dataformat(tex->format, &tex->format_flag); | 
					
						
							|  |  |  | 	GLenum data_type = gpu_get_gl_datatype(gpu_data_format); | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 	GLint alignment; | 
					
						
							| 
									
										
										
										
											2017-08-22 10:22:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 	/* The default pack size for textures is 4, which won't work for byte based textures */ | 
					
						
							|  |  |  | 	if (tex->bytesize == 1) { | 
					
						
							|  |  |  | 		glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment); | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  | 		glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-08-22 10:22:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 	glBindTexture(tex->target, tex->bindcode); | 
					
						
							| 
									
										
										
										
											2017-08-22 12:30:03 +02:00
										 |  |  | 	switch (tex->target) { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 		case GL_TEXTURE_1D: | 
					
						
							|  |  |  | 			glTexSubImage1D(tex->target, 0, offset_x, width, data_format, data_type, pixels); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2017-08-22 12:30:03 +02:00
										 |  |  | 		case GL_TEXTURE_2D: | 
					
						
							|  |  |  | 		case GL_TEXTURE_2D_MULTISAMPLE: | 
					
						
							|  |  |  | 		case GL_TEXTURE_1D_ARRAY: | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  | 			glTexSubImage2D( | 
					
						
							|  |  |  | 			        tex->target, 0, offset_x, offset_y, | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			        width, height, data_format, data_type, pixels); | 
					
						
							| 
									
										
										
										
											2017-08-22 12:30:03 +02:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		case GL_TEXTURE_3D: | 
					
						
							| 
									
										
										
										
											2017-08-22 14:33:57 +02:00
										 |  |  | 		case GL_TEXTURE_2D_ARRAY: | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  | 			glTexSubImage3D( | 
					
						
							|  |  |  | 			        tex->target, 0, offset_x, offset_y, offset_z, | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 			        width, height, depth, data_format, data_type, pixels); | 
					
						
							| 
									
										
										
										
											2017-08-22 12:30:03 +02:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			BLI_assert(!"tex->target mode not supported"); | 
					
						
							| 
									
										
										
										
											2018-06-25 12:02:57 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (tex->bytesize == 1) { | 
					
						
							|  |  |  | 		glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); | 
					
						
							| 
									
										
										
										
											2017-08-22 10:22:11 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glBindTexture(tex->target, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | void *GPU_texture_read(GPUTexture *tex, GPUDataFormat gpu_data_format, int miplvl) | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	int size[3] = {0, 0, 0}; | 
					
						
							|  |  |  | 	GPU_texture_get_mipmap_size(tex, miplvl, size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	gpu_validate_data_format(tex->format, gpu_data_format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size_t buf_size = gpu_texture_memory_footprint_compute(tex); | 
					
						
							|  |  |  | 	size_t samples_count = max_ii(1, tex->samples); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	samples_count *= size[0]; | 
					
						
							|  |  |  | 	samples_count *= max_ii(1, size[1]); | 
					
						
							|  |  |  | 	samples_count *= max_ii(1, size[2]); | 
					
						
							| 
									
										
										
										
											2018-10-22 12:58:11 +02:00
										 |  |  | 	samples_count *= (GPU_texture_cube(tex)) ? 6 : 1; | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch (gpu_data_format) { | 
					
						
							|  |  |  | 		case GPU_DATA_FLOAT: | 
					
						
							|  |  |  | 			buf_size = sizeof(float) * samples_count * tex->components; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case GPU_DATA_INT: | 
					
						
							|  |  |  | 		case GPU_DATA_UNSIGNED_INT: | 
					
						
							|  |  |  | 			buf_size = sizeof(int) * samples_count * tex->components; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case GPU_DATA_UNSIGNED_INT_24_8: | 
					
						
							|  |  |  | 		case GPU_DATA_10_11_11_REV: | 
					
						
							|  |  |  | 			buf_size = sizeof(int) * samples_count; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case GPU_DATA_UNSIGNED_BYTE: | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void *buf = MEM_mallocN(buf_size, "GPU_texture_read"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	GLenum data_format = gpu_get_gl_dataformat(tex->format, &tex->format_flag); | 
					
						
							|  |  |  | 	GLenum data_type = gpu_get_gl_datatype(gpu_data_format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glBindTexture(tex->target, tex->bindcode); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-22 12:58:11 +02:00
										 |  |  | 	if (GPU_texture_cube(tex)) { | 
					
						
							|  |  |  | 		int cube_face_size = buf_size / 6; | 
					
						
							|  |  |  | 		for (int i = 0; i < 6; ++i) { | 
					
						
							|  |  |  | 			glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, miplvl, data_format, data_type, | 
					
						
							|  |  |  | 			              ((char *)buf) + cube_face_size * i); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		glGetTexImage(tex->target, miplvl, data_format, data_type, buf); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	glBindTexture(tex->target, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return buf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_texture_update(GPUTexture *tex, GPUDataFormat data_format, const void *pixels) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	GPU_texture_update_sub(tex, data_format, pixels, 0, 0, 0, tex->w, tex->h, tex->d); | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | void GPU_invalid_tex_init(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 	memory_usage = 0; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	const float color[4] = {1.0f, 0.0f, 1.0f, 1.0f}; | 
					
						
							| 
									
										
										
										
											2018-04-30 16:02:24 +02:00
										 |  |  | 	GG.invalid_tex_1D = GPU_texture_create_1D(1, GPU_RGBA8, color, NULL); | 
					
						
							|  |  |  | 	GG.invalid_tex_2D = GPU_texture_create_2D(1, 1, GPU_RGBA8, color, NULL); | 
					
						
							|  |  |  | 	GG.invalid_tex_3D = GPU_texture_create_3D(1, 1, 1, GPU_RGBA8, color, NULL); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_invalid_tex_bind(int mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	switch (mode) { | 
					
						
							|  |  |  | 		case GL_TEXTURE_1D: | 
					
						
							|  |  |  | 			glBindTexture(GL_TEXTURE_1D, GG.invalid_tex_1D->bindcode); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case GL_TEXTURE_2D: | 
					
						
							|  |  |  | 			glBindTexture(GL_TEXTURE_2D, GG.invalid_tex_2D->bindcode); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case GL_TEXTURE_3D: | 
					
						
							|  |  |  | 			glBindTexture(GL_TEXTURE_3D, GG.invalid_tex_3D->bindcode); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_invalid_tex_free(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (GG.invalid_tex_1D) | 
					
						
							|  |  |  | 		GPU_texture_free(GG.invalid_tex_1D); | 
					
						
							|  |  |  | 	if (GG.invalid_tex_2D) | 
					
						
							|  |  |  | 		GPU_texture_free(GG.invalid_tex_2D); | 
					
						
							|  |  |  | 	if (GG.invalid_tex_3D) | 
					
						
							|  |  |  | 		GPU_texture_free(GG.invalid_tex_3D); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_texture_bind(GPUTexture *tex, int number) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	BLI_assert(number >= 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	if (number >= GPU_max_textures()) { | 
					
						
							|  |  |  | 		fprintf(stderr, "Not enough texture slots.\n"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((G.debug & G_DEBUG)) { | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 		for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; ++i) { | 
					
						
							|  |  |  | 			if (tex->fb[i] && GPU_framebuffer_bound(tex->fb[i])) { | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 				fprintf(stderr, | 
					
						
							|  |  |  | 				        "Feedback loop warning!: Attempting to bind " | 
					
						
							|  |  |  | 				        "texture attached to current framebuffer!\n"); | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 				BLI_assert(0); /* Should never happen! */ | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	glActiveTexture(GL_TEXTURE0 + number); | 
					
						
							| 
									
										
										
										
											2016-10-25 01:02:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (tex->bindcode != 0) | 
					
						
							| 
									
										
										
										
											2017-09-23 20:34:53 +02:00
										 |  |  | 		glBindTexture(tex->target, tex->bindcode); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2016-08-09 12:29:14 +03:00
										 |  |  | 		GPU_invalid_tex_bind(tex->target_base); | 
					
						
							| 
									
										
										
										
											2016-10-25 01:02:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	tex->number = number; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_texture_unbind(GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (tex->number == -1) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	glActiveTexture(GL_TEXTURE0 + tex->number); | 
					
						
							| 
									
										
										
										
											2017-09-23 20:34:53 +02:00
										 |  |  | 	glBindTexture(tex->target, 0); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	tex->number = -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int GPU_texture_bound_number(GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return tex->number; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | #define WARN_NOT_BOUND(_tex) do { \
 | 
					
						
							|  |  |  | 	if (_tex->number == -1) { \ | 
					
						
							|  |  |  | 		fprintf(stderr, "Warning : Trying to set parameter on a texture not bound.\n"); \ | 
					
						
							|  |  |  | 		BLI_assert(0); \ | 
					
						
							|  |  |  | 		return; \ | 
					
						
							|  |  |  | 	} \ | 
					
						
							|  |  |  | } while (0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-18 11:21:17 +02:00
										 |  |  | void GPU_texture_generate_mipmap(GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	WARN_NOT_BOUND(tex); | 
					
						
							| 
									
										
										
										
											2017-04-18 11:21:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	glActiveTexture(GL_TEXTURE0 + tex->number); | 
					
						
							| 
									
										
										
										
											2018-09-15 00:10:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (GPU_texture_depth(tex)) { | 
					
						
							|  |  |  | 		/* Some drivers have bugs when using glGenerateMipmap with depth textures (see T56789).
 | 
					
						
							|  |  |  | 		 * In this case we just create a complete texture with mipmaps manually without downsampling. | 
					
						
							|  |  |  | 		 * You must initialize the texture levels using other methods like GPU_framebuffer_recursive_downsample(). */ | 
					
						
							|  |  |  | 		int levels = 1 + floor(log2(max_ii(tex->w, tex->h))); | 
					
						
							|  |  |  | 		GPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex->format); | 
					
						
							|  |  |  | 		for (int i = 1; i < levels; ++i) { | 
					
						
							|  |  |  | 			GPU_texture_add_mipmap(tex, data_format, i, NULL); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		glBindTexture(tex->target, tex->bindcode); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		glGenerateMipmap(tex->target_base); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-18 11:21:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | void GPU_texture_compare_mode(GPUTexture *tex, bool use_compare) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	WARN_NOT_BOUND(tex); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	/* Could become an assertion ? (fclem) */ | 
					
						
							|  |  |  | 	if (!GPU_texture_depth(tex)) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	GLenum mode = (use_compare) ? GL_COMPARE_REF_TO_TEXTURE : GL_NONE; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	glActiveTexture(GL_TEXTURE0 + tex->number); | 
					
						
							|  |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_MODE, mode); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_texture_filter_mode(GPUTexture *tex, bool use_filter) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	WARN_NOT_BOUND(tex); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	/* Stencil and integer format does not support filtering. */ | 
					
						
							|  |  |  | 	BLI_assert(!use_filter || !(GPU_texture_stencil(tex) || GPU_texture_integer(tex))); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	GLenum filter = (use_filter) ? GL_LINEAR : GL_NEAREST; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	glActiveTexture(GL_TEXTURE0 + tex->number); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, filter); | 
					
						
							|  |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, filter); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 01:53:51 +02:00
										 |  |  | void GPU_texture_mipmap_mode(GPUTexture *tex, bool use_mipmap, bool use_filter) | 
					
						
							| 
									
										
										
										
											2017-04-18 11:21:17 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	WARN_NOT_BOUND(tex); | 
					
						
							| 
									
										
										
										
											2017-04-18 11:21:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	/* Stencil and integer format does not support filtering. */ | 
					
						
							|  |  |  | 	BLI_assert((!use_filter && !use_mipmap) || !(GPU_texture_stencil(tex) || GPU_texture_integer(tex))); | 
					
						
							| 
									
										
										
										
											2017-04-18 11:21:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	GLenum filter = (use_filter) ? GL_LINEAR : GL_NEAREST; | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  | 	GLenum mipmap = ( | 
					
						
							|  |  |  | 	        (use_filter) ? | 
					
						
							|  |  |  | 	        (use_mipmap) ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR : | 
					
						
							|  |  |  | 	        (use_mipmap) ? GL_NEAREST_MIPMAP_LINEAR : GL_NEAREST); | 
					
						
							| 
									
										
										
										
											2017-04-18 11:21:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	glActiveTexture(GL_TEXTURE0 + tex->number); | 
					
						
							|  |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, mipmap); | 
					
						
							| 
									
										
										
										
											2017-08-12 17:21:35 +02:00
										 |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, filter); | 
					
						
							| 
									
										
										
										
											2017-04-18 11:21:17 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | void GPU_texture_wrap_mode(GPUTexture *tex, bool use_repeat) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	WARN_NOT_BOUND(tex); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	GLenum repeat = (use_repeat) ? GL_REPEAT : GL_CLAMP_TO_EDGE; | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	glActiveTexture(GL_TEXTURE0 + tex->number); | 
					
						
							| 
									
										
										
										
											2017-02-03 16:01:32 +01:00
										 |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, repeat); | 
					
						
							|  |  |  | 	if (tex->target_base != GL_TEXTURE_1D) | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_T, repeat); | 
					
						
							|  |  |  | 	if (tex->target_base == GL_TEXTURE_3D) | 
					
						
							|  |  |  | 		glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_R, repeat); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | static GLenum gpu_get_gl_filterfunction(GPUFilterFunction filter) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  | 	switch (filter) { | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 		case GPU_NEAREST: | 
					
						
							|  |  |  | 			return GL_NEAREST; | 
					
						
							|  |  |  | 		case GPU_LINEAR: | 
					
						
							|  |  |  | 			return GL_LINEAR; | 
					
						
							|  |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  | 			BLI_assert(!"Unhandled filter mode"); | 
					
						
							|  |  |  | 			return GL_NEAREST; | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_texture_filters(GPUTexture *tex, GPUFilterFunction min_filter, GPUFilterFunction mag_filter) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	WARN_NOT_BOUND(tex); | 
					
						
							| 
									
										
										
										
											2018-06-22 08:18:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:35:37 -06:00
										 |  |  | 	/* Stencil and integer format does not support filtering. */ | 
					
						
							|  |  |  | 	BLI_assert(!(GPU_texture_stencil(tex) || GPU_texture_integer(tex))); | 
					
						
							|  |  |  | 	BLI_assert(mag_filter == GPU_NEAREST || mag_filter == GPU_LINEAR); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glActiveTexture(GL_TEXTURE0 + tex->number); | 
					
						
							|  |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, gpu_get_gl_filterfunction(min_filter)); | 
					
						
							|  |  |  | 	glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, gpu_get_gl_filterfunction(mag_filter)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | void GPU_texture_free(GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	tex->refcount--; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (tex->refcount < 0) | 
					
						
							|  |  |  | 		fprintf(stderr, "GPUTexture: negative refcount\n"); | 
					
						
							| 
									
										
										
										
											2018-06-04 09:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	if (tex->refcount == 0) { | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 		for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; ++i) { | 
					
						
							|  |  |  | 			if (tex->fb[i] != NULL) { | 
					
						
							|  |  |  | 				GPU_framebuffer_texture_detach_slot(tex->fb[i], tex, tex->fb_attachment[i]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | 		if (tex->bindcode) | 
					
						
							|  |  |  | 			GPU_tex_free(tex->bindcode); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | 		gpu_texture_memory_footprint_remove(tex); | 
					
						
							| 
									
										
										
										
											2017-05-04 21:22:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | 		MEM_freeN(tex); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_texture_ref(GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	tex->refcount++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int GPU_texture_target(const GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return tex->target; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int GPU_texture_width(const GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return tex->w; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int GPU_texture_height(const GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return tex->h; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | int GPU_texture_layers(const GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return tex->d; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | GPUTextureFormat GPU_texture_format(const GPUTexture *tex) | 
					
						
							| 
									
										
										
										
											2017-05-16 02:59:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	return tex->format; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-14 03:20:39 +01:00
										 |  |  | int GPU_texture_samples(const GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return tex->samples; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-18 01:26:08 +01:00
										 |  |  | bool GPU_texture_depth(const GPUTexture *tex) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	return (tex->format_flag & GPU_FORMAT_DEPTH) != 0; | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-18 01:26:08 +01:00
										 |  |  | bool GPU_texture_stencil(const GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-24 20:27:39 +01:00
										 |  |  | 	return (tex->format_flag & GPU_FORMAT_STENCIL) != 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool GPU_texture_integer(const GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return (tex->format_flag & GPU_FORMAT_INTEGER) != 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool GPU_texture_cube(const GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return (tex->format_flag & GPU_FORMAT_CUBE) != 0; | 
					
						
							| 
									
										
										
										
											2017-03-18 01:26:08 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | int GPU_texture_opengl_bindcode(const GPUTexture *tex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return tex->bindcode; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | void GPU_texture_attach_framebuffer(GPUTexture *tex, GPUFrameBuffer *fb, int attachment) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 	for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; ++i) { | 
					
						
							|  |  |  | 		if (tex->fb[i] == NULL) { | 
					
						
							|  |  |  | 			tex->fb[i] = fb; | 
					
						
							|  |  |  | 			tex->fb_attachment[i] = attachment; | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 	BLI_assert(!"Error: Texture: Not enough Framebuffer slots"); | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | /* Return previous attachment point */ | 
					
						
							|  |  |  | int GPU_texture_detach_framebuffer(GPUTexture *tex, GPUFrameBuffer *fb) | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 	for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; ++i) { | 
					
						
							|  |  |  | 		if (tex->fb[i] == fb) { | 
					
						
							|  |  |  | 			tex->fb[i] = NULL; | 
					
						
							|  |  |  | 			return tex->fb_attachment[i]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-06 21:20:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | 	BLI_assert(!"Error: Texture: Framebuffer is not attached"); | 
					
						
							| 
									
										
										
										
											2018-03-26 09:59:12 +02:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2018-03-25 17:46:48 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GPU_texture_get_mipmap_size(GPUTexture *tex, int lvl, int *size) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-24 18:46:51 +02:00
										 |  |  | 	/* TODO assert if lvl is below the limit of 1px in each dimension. */ | 
					
						
							| 
									
										
										
										
											2018-07-10 13:17:32 +02:00
										 |  |  | 	int div = 1 << lvl; | 
					
						
							|  |  |  | 	size[0] = max_ii(1, tex->w / div); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (tex->target == GL_TEXTURE_1D_ARRAY) { | 
					
						
							|  |  |  | 		size[1] = tex->h; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (tex->h > 0) { | 
					
						
							|  |  |  | 		size[1] = max_ii(1, tex->h / div); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (tex->target == GL_TEXTURE_2D_ARRAY) { | 
					
						
							|  |  |  | 		size[2] = tex->d; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (tex->d > 0) { | 
					
						
							|  |  |  | 		size[2] = max_ii(1, tex->d / div); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |