| 
									
										
										
										
											2020-09-06 23:45:51 +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) 2016 by Mike Erwin. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup gpu | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 20:08:25 +02:00
										 |  |  | #include "gl_context.hh"
 | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "gl_vertex_buffer.hh"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace blender::gpu { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  | void GLVertBuf::acquire_data() | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  |   /* Discard previous data if any. */ | 
					
						
							|  |  |  |   MEM_SAFE_FREE(data); | 
					
						
							|  |  |  |   data = (uchar *)MEM_mallocN(sizeof(uchar) * this->size_alloc_get(), __func__); | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  | void GLVertBuf::resize_data() | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  |   data = (uchar *)MEM_reallocN(data, sizeof(uchar) * this->size_alloc_get()); | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  | void GLVertBuf::release_data() | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  |   if (vbo_id_ != 0) { | 
					
						
							| 
									
										
										
										
											2020-09-07 20:08:25 +02:00
										 |  |  |     GLContext::buf_free(vbo_id_); | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  |     vbo_id_ = 0; | 
					
						
							|  |  |  |     memory_usage -= vbo_size_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   MEM_SAFE_FREE(data); | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  | void GLVertBuf::duplicate_data(VertBuf *dst_) | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-08 04:12:12 +02:00
										 |  |  |   BLI_assert(GLContext::get() != nullptr); | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  |   GLVertBuf *src = this; | 
					
						
							|  |  |  |   GLVertBuf *dst = static_cast<GLVertBuf *>(dst_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (src->vbo_id_ != 0) { | 
					
						
							|  |  |  |     dst->vbo_size_ = src->size_used_get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     glGenBuffers(1, &dst->vbo_id_); | 
					
						
							|  |  |  |     glBindBuffer(GL_COPY_WRITE_BUFFER, dst->vbo_id_); | 
					
						
							|  |  |  |     glBufferData(GL_COPY_WRITE_BUFFER, dst->vbo_size_, nullptr, to_gl(dst->usage_)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     glBindBuffer(GL_COPY_READ_BUFFER, src->vbo_id_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, dst->vbo_size_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memory_usage += dst->vbo_size_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (data != nullptr) { | 
					
						
							|  |  |  |     dst->data = (uchar *)MEM_dupallocN(src->data); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  | void GLVertBuf::upload_data() | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  |   this->bind(); | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  | void GLVertBuf::bind() | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-08 04:12:12 +02:00
										 |  |  |   BLI_assert(GLContext::get() != nullptr); | 
					
						
							| 
									
										
										
										
											2020-09-07 01:20:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (vbo_id_ == 0) { | 
					
						
							|  |  |  |     glGenBuffers(1, &vbo_id_); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   glBindBuffer(GL_ARRAY_BUFFER, vbo_id_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (flag & GPU_VERTBUF_DATA_DIRTY) { | 
					
						
							|  |  |  |     vbo_size_ = this->size_used_get(); | 
					
						
							|  |  |  |     /* Orphan the vbo to avoid sync then upload data. */ | 
					
						
							|  |  |  |     glBufferData(GL_ARRAY_BUFFER, vbo_size_, nullptr, to_gl(usage_)); | 
					
						
							|  |  |  |     glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_size_, data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memory_usage += vbo_size_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (usage_ == GPU_USAGE_STATIC) { | 
					
						
							|  |  |  |       MEM_SAFE_FREE(data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     flag &= ~GPU_VERTBUF_DATA_DIRTY; | 
					
						
							|  |  |  |     flag |= GPU_VERTBUF_DATA_UPLOADED; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 19:48:52 +02:00
										 |  |  | void GLVertBuf::update_sub(uint start, uint len, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   glBufferSubData(GL_ARRAY_BUFFER, start, len, data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-02 09:48:41 +10:00
										 |  |  | }  // namespace blender::gpu
 |