| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2020 Blender Foundation. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup gpu | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_global.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_string.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gpu_backend.hh"
 | 
					
						
							|  |  |  | #include "gpu_context_private.hh"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gl_backend.hh"
 | 
					
						
							| 
									
										
										
										
											2020-09-09 00:47:59 +02:00
										 |  |  | #include "gl_debug.hh"
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | #include "gl_uniform_buffer.hh"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace blender::gpu { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Creation & Deletion
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GLUniformBuf::GLUniformBuf(size_t size, const char *name) : UniformBuf(size, name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* Do not create ubo GL buffer here to allow allocation from any thread. */ | 
					
						
							| 
									
										
										
										
											2020-09-07 18:52:30 +02:00
										 |  |  |   BLI_assert(size <= GLContext::max_ubo_size); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GLUniformBuf::~GLUniformBuf() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-07 20:08:25 +02:00
										 |  |  |   GLContext::buf_free(ubo_id_); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Data upload / update
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 13:18:48 +01:00
										 |  |  | void GLUniformBuf::init() | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-08 04:12:12 +02:00
										 |  |  |   BLI_assert(GLContext::get()); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   glGenBuffers(1, &ubo_id_); | 
					
						
							|  |  |  |   glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_); | 
					
						
							| 
									
										
										
										
											2020-11-06 17:49:09 +01:00
										 |  |  |   glBufferData(GL_UNIFORM_BUFFER, size_in_bytes_, nullptr, GL_DYNAMIC_DRAW); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-09 00:47:59 +02:00
										 |  |  |   debug::object_label(GL_UNIFORM_BUFFER, ubo_id_, name_); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GLUniformBuf::update(const void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (ubo_id_ == 0) { | 
					
						
							|  |  |  |     this->init(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_); | 
					
						
							|  |  |  |   glBufferSubData(GL_UNIFORM_BUFFER, 0, size_in_bytes_, data); | 
					
						
							|  |  |  |   glBindBuffer(GL_UNIFORM_BUFFER, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Usage
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GLUniformBuf::bind(int slot) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-07 18:52:30 +02:00
										 |  |  |   if (slot >= GLContext::max_ubo_binds) { | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |     fprintf(stderr, | 
					
						
							|  |  |  |             "Error: Trying to bind \"%s\" ubo to slot %d which is above the reported limit of %d.", | 
					
						
							|  |  |  |             name_, | 
					
						
							|  |  |  |             slot, | 
					
						
							| 
									
										
										
										
											2020-09-07 18:52:30 +02:00
										 |  |  |             GLContext::max_ubo_binds); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ubo_id_ == 0) { | 
					
						
							|  |  |  |     this->init(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 17:49:09 +01:00
										 |  |  |   if (data_ != nullptr) { | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |     this->update(data_); | 
					
						
							|  |  |  |     MEM_SAFE_FREE(data_); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   slot_ = slot; | 
					
						
							|  |  |  |   glBindBufferBase(GL_UNIFORM_BUFFER, slot_, ubo_id_); | 
					
						
							| 
									
										
										
										
											2020-09-01 02:41:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef DEBUG
 | 
					
						
							|  |  |  |   BLI_assert(slot < 16); | 
					
						
							| 
									
										
										
										
											2020-09-08 04:12:12 +02:00
										 |  |  |   GLContext::get()->bound_ubo_slots |= 1 << slot; | 
					
						
							| 
									
										
										
										
											2020-09-01 02:41:29 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 13:18:48 +01:00
										 |  |  | void GLUniformBuf::unbind() | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | { | 
					
						
							|  |  |  | #ifdef DEBUG
 | 
					
						
							|  |  |  |   /* NOTE: This only unbinds the last bound slot. */ | 
					
						
							|  |  |  |   glBindBufferBase(GL_UNIFORM_BUFFER, slot_, 0); | 
					
						
							| 
									
										
										
										
											2020-09-01 02:41:29 +02:00
										 |  |  |   /* Hope that the context did not change. */ | 
					
						
							| 
									
										
										
										
											2020-09-08 04:12:12 +02:00
										 |  |  |   GLContext::get()->bound_ubo_slots &= ~(1 << slot_); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |   slot_ = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-02 09:48:41 +10:00
										 |  |  | }  // namespace blender::gpu
 |