| 
									
										
										
										
											2020-08-16 14:01:07 +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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup gpu | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:29:51 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 20:56:39 +02:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 14:01:07 +02:00
										 |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gpu_state_private.hh"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "glew-mx.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace blender { | 
					
						
							|  |  |  | namespace gpu { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 15:17:13 +02:00
										 |  |  | class GLFrameBuffer; | 
					
						
							| 
									
										
										
										
											2020-09-05 17:29:51 +02:00
										 |  |  | class GLTexture; | 
					
						
							| 
									
										
										
										
											2020-08-29 15:17:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 14:14:56 +02:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * State manager keeping track of the draw state and applying it before drawing. | 
					
						
							|  |  |  |  * Opengl Implementation. | 
					
						
							| 
									
										
										
										
											2021-01-04 12:00:18 +11:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-12 16:22:34 +02:00
										 |  |  | class GLStateManager : public StateManager { | 
					
						
							| 
									
										
										
										
											2020-08-29 15:17:13 +02:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2021-02-05 16:23:34 +11:00
										 |  |  |   /** Another reference to the active frame-buffer. */ | 
					
						
							| 
									
										
										
										
											2020-09-09 00:47:59 +02:00
										 |  |  |   GLFrameBuffer *active_fb = nullptr; | 
					
						
							| 
									
										
										
										
											2020-08-29 15:17:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 14:01:07 +02:00
										 |  |  |  private: | 
					
						
							|  |  |  |   /** Current state of the GL implementation. Avoids resetting the whole state for every change. */ | 
					
						
							|  |  |  |   GPUState current_; | 
					
						
							|  |  |  |   GPUStateMutable current_mutable_; | 
					
						
							| 
									
										
										
										
											2020-08-25 18:18:43 +02:00
										 |  |  |   /** Limits. */ | 
					
						
							|  |  |  |   float line_width_range_[2]; | 
					
						
							| 
									
										
										
										
											2020-08-16 14:01:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:29:51 +02:00
										 |  |  |   /** Texture state:
 | 
					
						
							|  |  |  |    * We keep the full stack of textures and sampler bounds to use multi bind, and to be able to | 
					
						
							|  |  |  |    * edit and restore texture binds on the fly without querying the context. | 
					
						
							|  |  |  |    * Also this allows us to keep track of textures bounds to many texture units. | 
					
						
							|  |  |  |    * Keep the targets to know what target to set to 0 for unbinding (legacy). | 
					
						
							|  |  |  |    * Init first target to GL_TEXTURE_2D for texture_bind_temp to work. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   GLuint targets_[64] = {GL_TEXTURE_2D}; | 
					
						
							|  |  |  |   GLuint textures_[64] = {0}; | 
					
						
							|  |  |  |   GLuint samplers_[64] = {0}; | 
					
						
							|  |  |  |   uint64_t dirty_texture_binds_ = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 06:10:11 +02:00
										 |  |  |   GLuint images_[8] = {0}; | 
					
						
							|  |  |  |   GLenum formats_[8] = {0}; | 
					
						
							|  |  |  |   uint8_t dirty_image_binds_ = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 14:01:07 +02:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2020-08-17 20:32:20 +02:00
										 |  |  |   GLStateManager(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-05 21:44:03 -05:00
										 |  |  |   void apply_state() override; | 
					
						
							| 
									
										
										
										
											2021-12-09 20:01:47 +11:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Will set all the states regardless of the current ones. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-01-05 21:44:03 -05:00
										 |  |  |   void force_state() override; | 
					
						
							| 
									
										
										
										
											2020-08-16 14:01:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 06:10:11 +02:00
										 |  |  |   void issue_barrier(eGPUBarrier barrier_bits) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 17:29:51 +02:00
										 |  |  |   void texture_bind(Texture *tex, eGPUSamplerState sampler, int unit) override; | 
					
						
							| 
									
										
										
										
											2021-12-09 20:01:47 +11:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Bind the texture to slot 0 for editing purpose. Used by legacy pipeline. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-09-05 17:29:51 +02:00
										 |  |  |   void texture_bind_temp(GLTexture *tex); | 
					
						
							|  |  |  |   void texture_unbind(Texture *tex) override; | 
					
						
							| 
									
										
										
										
											2022-01-05 21:44:03 -05:00
										 |  |  |   void texture_unbind_all() override; | 
					
						
							| 
									
										
										
										
											2020-09-05 17:29:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 06:10:11 +02:00
										 |  |  |   void image_bind(Texture *tex, int unit) override; | 
					
						
							|  |  |  |   void image_unbind(Texture *tex) override; | 
					
						
							| 
									
										
										
										
											2022-01-05 21:44:03 -05:00
										 |  |  |   void image_unbind_all() override; | 
					
						
							| 
									
										
										
										
											2020-09-12 06:10:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-08 00:30:38 +02:00
										 |  |  |   void texture_unpack_row_length_set(uint len) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-05 21:44:03 -05:00
										 |  |  |   uint64_t bound_texture_slots(); | 
					
						
							|  |  |  |   uint8_t bound_image_slots(); | 
					
						
							| 
									
										
										
										
											2020-09-04 22:19:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 00:34:06 +02:00
										 |  |  |  private: | 
					
						
							| 
									
										
										
										
											2022-01-07 11:38:08 +11:00
										 |  |  |   static void set_write_mask(eGPUWriteMask value); | 
					
						
							|  |  |  |   static void set_depth_test(eGPUDepthTest value); | 
					
						
							|  |  |  |   static void set_stencil_test(eGPUStencilTest test, eGPUStencilOp operation); | 
					
						
							|  |  |  |   static void set_stencil_mask(eGPUStencilTest test, const GPUStateMutable state); | 
					
						
							|  |  |  |   static void set_clip_distances(int new_dist_len, int old_dist_len); | 
					
						
							|  |  |  |   static void set_logic_op(bool enable); | 
					
						
							|  |  |  |   static void set_facing(bool invert); | 
					
						
							|  |  |  |   static void set_backface_culling(eGPUFaceCullTest test); | 
					
						
							|  |  |  |   static void set_provoking_vert(eGPUProvokingVertex vert); | 
					
						
							|  |  |  |   static void set_shadow_bias(bool enable); | 
					
						
							|  |  |  |   static void set_blend(eGPUBlend value); | 
					
						
							| 
									
										
										
										
											2020-08-16 20:56:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 15:17:13 +02:00
										 |  |  |   void set_state(const GPUState &state); | 
					
						
							|  |  |  |   void set_mutable_state(const GPUStateMutable &state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-05 21:44:03 -05:00
										 |  |  |   void texture_bind_apply(); | 
					
						
							|  |  |  |   void image_bind_apply(); | 
					
						
							| 
									
										
										
										
											2020-09-05 17:29:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 00:34:06 +02:00
										 |  |  |   MEM_CXX_CLASS_ALLOC_FUNCS("GLStateManager") | 
					
						
							| 
									
										
										
										
											2020-08-16 14:01:07 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 06:10:11 +02:00
										 |  |  | static inline GLbitfield to_gl(eGPUBarrier barrier_bits) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GLbitfield barrier = 0; | 
					
						
							|  |  |  |   if (barrier_bits & GPU_BARRIER_SHADER_IMAGE_ACCESS) { | 
					
						
							|  |  |  |     barrier |= GL_SHADER_IMAGE_ACCESS_BARRIER_BIT; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (barrier_bits & GPU_BARRIER_TEXTURE_FETCH) { | 
					
						
							|  |  |  |     barrier |= GL_TEXTURE_FETCH_BARRIER_BIT; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-05-26 16:49:17 +02:00
										 |  |  |   if (barrier_bits & GPU_BARRIER_SHADER_STORAGE) { | 
					
						
							|  |  |  |     barrier |= GL_SHADER_STORAGE_BARRIER_BIT; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-12-18 00:55:22 +01:00
										 |  |  |   if (barrier_bits & GPU_BARRIER_VERTEX_ATTRIB_ARRAY) { | 
					
						
							|  |  |  |     barrier |= GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (barrier_bits & GPU_BARRIER_ELEMENT_ARRAY) { | 
					
						
							|  |  |  |     barrier |= GL_ELEMENT_ARRAY_BARRIER_BIT; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-09-12 06:10:11 +02:00
										 |  |  |   return barrier; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 14:01:07 +02:00
										 |  |  | }  // namespace gpu
 | 
					
						
							|  |  |  | }  // namespace blender
 |