| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |  * The Original Code is Copyright (C) 2020 Blender Foundation. | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup gpu | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_blenlib.h"
 | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  | #include "BLI_math_base.h"
 | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | #include "gpu_backend.hh"
 | 
					
						
							| 
									
										
										
										
											2020-02-12 12:48:44 +01:00
										 |  |  | #include "gpu_node_graph.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-14 17:40:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "GPU_material.h"
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "GPU_extensions.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 23:09:37 +02:00
										 |  |  | #include "GPU_uniform_buffer.h"
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | #include "gpu_uniform_buffer_private.hh"
 | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Creation & Deletion
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							| 
									
										
										
										
											2020-08-20 23:09:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | namespace blender::gpu { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | UniformBuf::UniformBuf(size_t size, const char *name) | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-29 16:57:09 +02:00
										 |  |  |   /* Make sure that UBO is padded to size of vec4 */ | 
					
						
							|  |  |  |   BLI_assert((size % 16) == 0); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   BLI_assert(size <= GPU_max_ubo_size()); | 
					
						
							| 
									
										
										
										
											2019-05-29 16:57:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   size_in_bytes_ = size; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   BLI_strncpy(name_, name, sizeof(name_)); | 
					
						
							| 
									
										
										
										
											2017-07-14 17:40:54 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | UniformBuf::~UniformBuf() | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   MEM_SAFE_FREE(data_); | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | }  // namespace blender::gpu
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Uniform buffer from GPUInput list
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:00:30 +02:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * We need to pad some data types (vec3) on the C side | 
					
						
							|  |  |  |  * To match the GPU expected memory block alignment. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-01-23 14:15:43 +11:00
										 |  |  | static eGPUType get_padded_gpu_type(LinkData *link) | 
					
						
							| 
									
										
										
										
											2017-08-18 12:00:30 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  |   GPUInput *input = (GPUInput *)link->data; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   eGPUType gputype = input->type; | 
					
						
							|  |  |  |   /* Unless the vec3 is followed by a float we need to treat it as a vec4. */ | 
					
						
							|  |  |  |   if (gputype == GPU_VEC3 && (link->next != NULL) && | 
					
						
							|  |  |  |       (((GPUInput *)link->next->data)->type != GPU_FLOAT)) { | 
					
						
							|  |  |  |     gputype = GPU_VEC4; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return gputype; | 
					
						
							| 
									
										
										
										
											2017-08-18 12:00:30 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-14 17:40:54 +02:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2019-04-29 14:14:14 +10:00
										 |  |  |  * Returns 1 if the first item should be after second item. | 
					
						
							| 
									
										
										
										
											2017-07-14 17:40:54 +02:00
										 |  |  |  * We make sure the vec4 uniforms come first. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int inputs_cmp(const void *a, const void *b) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  |   const LinkData *link_a = (const LinkData *)a, *link_b = (const LinkData *)b; | 
					
						
							|  |  |  |   const GPUInput *input_a = (const GPUInput *)link_a->data; | 
					
						
							|  |  |  |   const GPUInput *input_b = (const GPUInput *)link_b->data; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   return input_a->type < input_b->type ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2017-07-14 17:40:54 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Make sure we respect the expected alignment of UBOs. | 
					
						
							| 
									
										
										
										
											2019-04-25 12:11:55 +02:00
										 |  |  |  * mat4, vec4, pad vec3 as vec4, then vec2, then floats. | 
					
						
							| 
									
										
										
										
											2017-07-14 17:40:54 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | static void buffer_from_list_inputs_sort(ListBase *inputs) | 
					
						
							| 
									
										
										
										
											2017-07-14 17:40:54 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-22 00:09:17 +10:00
										 |  |  | /* Only support up to this type, if you want to extend it, make sure static void
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |  * inputs_sobuffer_size_compute *inputs) padding logic is correct for the new types. */ | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  | #define MAX_UBO_GPU_TYPE GPU_MAT4
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-25 12:11:55 +02:00
										 |  |  |   /* Order them as mat4, vec4, vec3, vec2, float. */ | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   BLI_listbase_sort(inputs, inputs_cmp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Creates a lookup table for the different types; */ | 
					
						
							|  |  |  |   LinkData *inputs_lookup[MAX_UBO_GPU_TYPE + 1] = {NULL}; | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  |   eGPUType cur_type = static_cast<eGPUType>(MAX_UBO_GPU_TYPE + 1); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 19:15:01 +02:00
										 |  |  |   LISTBASE_FOREACH (LinkData *, link, inputs) { | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  |     GPUInput *input = (GPUInput *)link->data; | 
					
						
							| 
									
										
										
										
											2019-04-25 12:11:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (input->type == GPU_MAT3) { | 
					
						
							|  |  |  |       /* Alignment for mat3 is not handled currently, so not supported */ | 
					
						
							|  |  |  |       BLI_assert(!"mat3 not supported in UBO"); | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-07 12:39:35 +02:00
										 |  |  |     if (input->type > MAX_UBO_GPU_TYPE) { | 
					
						
							| 
									
										
										
										
											2019-04-25 12:11:55 +02:00
										 |  |  |       BLI_assert(!"GPU type not supported in UBO"); | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     if (input->type == cur_type) { | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-07 12:39:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     inputs_lookup[input->type] = link; | 
					
						
							|  |  |  |     cur_type = input->type; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* If there is no GPU_VEC3 there is no need for alignment. */ | 
					
						
							|  |  |  |   if (inputs_lookup[GPU_VEC3] == NULL) { | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   LinkData *link = inputs_lookup[GPU_VEC3]; | 
					
						
							|  |  |  |   while (link != NULL && ((GPUInput *)link->data)->type == GPU_VEC3) { | 
					
						
							|  |  |  |     LinkData *link_next = link->next; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* If GPU_VEC3 is followed by nothing or a GPU_FLOAT, no need for alignment. */ | 
					
						
							|  |  |  |     if ((link_next == NULL) || ((GPUInput *)link_next->data)->type == GPU_FLOAT) { | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* If there is a float, move it next to current vec3. */ | 
					
						
							|  |  |  |     if (inputs_lookup[GPU_FLOAT] != NULL) { | 
					
						
							|  |  |  |       LinkData *float_input = inputs_lookup[GPU_FLOAT]; | 
					
						
							|  |  |  |       inputs_lookup[GPU_FLOAT] = float_input->next; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       BLI_remlink(inputs, float_input); | 
					
						
							|  |  |  |       BLI_insertlinkafter(inputs, link, float_input); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     link = link_next; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  | #undef MAX_UBO_GPU_TYPE
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | static inline size_t buffer_size_from_list(ListBase *inputs) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   size_t buffer_size = 0; | 
					
						
							|  |  |  |   LISTBASE_FOREACH (LinkData *, link, inputs) { | 
					
						
							|  |  |  |     const eGPUType gputype = get_padded_gpu_type(link); | 
					
						
							|  |  |  |     buffer_size += gputype * sizeof(float); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   /* Round up to size of vec4. (Opengl Requirement) */ | 
					
						
							|  |  |  |   size_t alignment = sizeof(float[4]); | 
					
						
							|  |  |  |   buffer_size = divide_ceil_u(buffer_size, alignment) * alignment; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   return buffer_size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline void buffer_fill_from_list(void *data, ListBase *inputs) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  |   /* Now that we know the total ubo size we can start populating it. */ | 
					
						
							|  |  |  |   float *offset = (float *)data; | 
					
						
							|  |  |  |   LISTBASE_FOREACH (LinkData *, link, inputs) { | 
					
						
							|  |  |  |     GPUInput *input = (GPUInput *)link->data; | 
					
						
							|  |  |  |     memcpy(offset, input->vec, input->type * sizeof(float)); | 
					
						
							|  |  |  |     offset += get_padded_gpu_type(link); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | /** \} */ | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name C-API
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | using namespace blender::gpu; | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | GPUUniformBuf *GPU_uniformbuf_create_ex(size_t size, const void *data, const char *name) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   UniformBuf *ubo = GPUBackend::get()->uniformbuf_alloc(size, name); | 
					
						
							|  |  |  |   /* Direct init. */ | 
					
						
							|  |  |  |   if (data != NULL) { | 
					
						
							|  |  |  |     ubo->update(data); | 
					
						
							| 
									
										
										
										
											2020-07-28 15:38:46 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   return reinterpret_cast<GPUUniformBuf *>(ubo); | 
					
						
							| 
									
										
										
										
											2017-07-14 17:40:54 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Create UBO from inputs list. | 
					
						
							|  |  |  |  * Return NULL if failed to create or if \param inputs: is empty. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \param inputs: ListBase of #BLI_genericNodeN(#GPUInput). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | GPUUniformBuf *GPU_uniformbuf_create_from_list(ListBase *inputs, const char *name) | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   /* There is no point on creating an UBO if there is no arguments. */ | 
					
						
							|  |  |  |   if (BLI_listbase_is_empty(inputs)) { | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   buffer_from_list_inputs_sort(inputs); | 
					
						
							|  |  |  |   size_t buffer_size = buffer_size_from_list(inputs); | 
					
						
							|  |  |  |   void *data = MEM_mallocN(buffer_size, __func__); | 
					
						
							|  |  |  |   buffer_fill_from_list(data, inputs); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   UniformBuf *ubo = GPUBackend::get()->uniformbuf_alloc(buffer_size, name); | 
					
						
							|  |  |  |   /* Defer data upload. */ | 
					
						
							|  |  |  |   ubo->attach_data(data); | 
					
						
							|  |  |  |   return reinterpret_cast<GPUUniformBuf *>(ubo); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_uniformbuf_free(GPUUniformBuf *ubo) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   delete reinterpret_cast<UniformBuf *>(ubo); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_uniformbuf_update(GPUUniformBuf *ubo, const void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   reinterpret_cast<UniformBuf *>(ubo)->update(data); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | void GPU_uniformbuf_bind(GPUUniformBuf *ubo, int slot) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   reinterpret_cast<UniformBuf *>(ubo)->bind(slot); | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 23:09:37 +02:00
										 |  |  | void GPU_uniformbuf_unbind(GPUUniformBuf *ubo) | 
					
						
							| 
									
										
										
										
											2017-09-11 23:15:29 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   reinterpret_cast<UniformBuf *>(ubo)->unbind(); | 
					
						
							| 
									
										
										
										
											2017-09-11 23:15:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 23:09:37 +02:00
										 |  |  | void GPU_uniformbuf_unbind_all(void) | 
					
						
							| 
									
										
										
										
											2017-02-07 11:20:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   /* FIXME */ | 
					
						
							| 
									
										
										
										
											2020-08-02 18:43:17 +10:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** \} */ |