| 
									
										
										
										
											2020-08-10 11:41:22 +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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright 2020, Blender Foundation. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup gpu | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * GPU geometry batch | 
					
						
							|  |  |  |  * Contains VAOs + VBOs + Shader representing a drawable entity. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gpu_batch_private.hh"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 02:46:51 +02:00
										 |  |  | #include "gl_index_buffer.hh"
 | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  | #include "gl_vertex_buffer.hh"
 | 
					
						
							| 
									
										
										
										
											2020-09-06 02:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  | #include "glew-mx.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace blender { | 
					
						
							|  |  |  | namespace gpu { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  | class GLContext; | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | class GLShaderInterface; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  | #define GPU_VAO_STATIC_LEN 3
 | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 20:01:47 +11:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * VAO management: remembers all geometry state (vertex attribute bindings & element buffer) | 
					
						
							|  |  |  |  * for each shader interface. Start with a static number of VAO's and fallback to dynamic count | 
					
						
							|  |  |  |  * if necessary. Once a batch goes dynamic it does not go back. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  | class GLVaoCache { | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |  private: | 
					
						
							|  |  |  |   /** Context for which the vao_cache_ was generated. */ | 
					
						
							| 
									
										
										
										
											2020-08-21 12:30:55 +02:00
										 |  |  |   GLContext *context_ = NULL; | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |   /** Last interface this batch was drawn with. */ | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   GLShaderInterface *interface_ = NULL; | 
					
						
							| 
									
										
										
										
											2021-02-05 16:23:34 +11:00
										 |  |  |   /** Cached VAO for the last interface. */ | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |   GLuint vao_id_ = 0; | 
					
						
							| 
									
										
										
										
											2021-02-05 16:23:34 +11:00
										 |  |  |   /** Used when arb_base_instance is not supported. */ | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |   GLuint vao_base_instance_ = 0; | 
					
						
							|  |  |  |   int base_instance_ = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  |   bool is_dynamic_vao_count = false; | 
					
						
							|  |  |  |   union { | 
					
						
							|  |  |  |     /** Static handle count */ | 
					
						
							|  |  |  |     struct { | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |       const GLShaderInterface *interfaces[GPU_VAO_STATIC_LEN]; | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |       GLuint vao_ids[GPU_VAO_STATIC_LEN]; | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  |     } static_vaos; | 
					
						
							|  |  |  |     /** Dynamic handle count */ | 
					
						
							|  |  |  |     struct { | 
					
						
							|  |  |  |       uint count; | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |       const GLShaderInterface **interfaces; | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  |       GLuint *vao_ids; | 
					
						
							|  |  |  |     } dynamic_vaos; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |  public: | 
					
						
							|  |  |  |   GLVaoCache(); | 
					
						
							|  |  |  |   ~GLVaoCache(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GLuint vao_get(GPUBatch *batch); | 
					
						
							|  |  |  |   GLuint base_instance_vao_get(GPUBatch *batch, int i_first); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 20:01:47 +11:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Return 0 on cache miss (invalid VAO). | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   GLuint lookup(const GLShaderInterface *interface); | 
					
						
							| 
									
										
										
										
											2021-12-09 20:01:47 +11:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Create a new VAO object and store it in the cache. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   void insert(const GLShaderInterface *interface, GLuint vao_id); | 
					
						
							|  |  |  |   void remove(const GLShaderInterface *interface); | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  |   void clear(void); | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |  private: | 
					
						
							|  |  |  |   void init(void); | 
					
						
							| 
									
										
										
										
											2021-12-09 20:01:47 +11:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * The #GLVaoCache object is only valid for one #GLContext. | 
					
						
							|  |  |  |    * Reset the cache if trying to draw in another context;. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |   void context_check(void); | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class GLBatch : public Batch { | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  |   /** All vaos corresponding to all the GPUShaderInterface this batch was drawn with. */ | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |   GLVaoCache vao_cache_; | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |  public: | 
					
						
							|  |  |  |   void draw(int v_first, int v_count, int i_first, int i_count) override; | 
					
						
							| 
									
										
										
										
											2020-08-11 01:31:40 +02:00
										 |  |  |   void bind(int i_first); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 02:46:51 +02:00
										 |  |  |   /* Convenience getters. */ | 
					
						
							| 
									
										
										
										
											2021-12-09 20:01:47 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  |   GLIndexBuf *elem_(void) const | 
					
						
							| 
									
										
										
										
											2020-09-06 02:46:51 +02:00
										 |  |  |   { | 
					
						
							|  |  |  |     return static_cast<GLIndexBuf *>(unwrap(elem)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-09-06 23:45:51 +02:00
										 |  |  |   GLVertBuf *verts_(const int index) const | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return static_cast<GLVertBuf *>(unwrap(verts[index])); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   GLVertBuf *inst_(const int index) const | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return static_cast<GLVertBuf *>(unwrap(inst[index])); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-09-06 02:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-10 11:41:22 +02:00
										 |  |  |   MEM_CXX_CLASS_ALLOC_FUNCS("GLBatch"); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace gpu
 | 
					
						
							|  |  |  | }  // namespace blender
 |