Blender  V2.93
gl_batch.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2020, Blender Foundation.
17  * All rights reserved.
18  */
19 
27 #pragma once
28 
29 #include "MEM_guardedalloc.h"
30 
31 #include "gpu_batch_private.hh"
32 
33 #include "gl_index_buffer.hh"
34 #include "gl_vertex_buffer.hh"
35 
36 #include "glew-mx.h"
37 
38 namespace blender {
39 namespace gpu {
40 
41 class GLContext;
42 class GLShaderInterface;
43 
44 #define GPU_VAO_STATIC_LEN 3
45 
46 /* Vao management: remembers all geometry state (vertex attribute bindings & element buffer)
47  * for each shader interface. Start with a static number of vaos and fallback to dynamic count
48  * if necessary. Once a batch goes dynamic it does not go back. */
49 class GLVaoCache {
50  private:
52  GLContext *context_ = NULL;
54  GLShaderInterface *interface_ = NULL;
56  GLuint vao_id_ = 0;
58  GLuint vao_base_instance_ = 0;
59  int base_instance_ = 0;
60 
61  bool is_dynamic_vao_count = false;
62  union {
64  struct {
69  struct {
72  GLuint *vao_ids;
74  };
75 
76  public:
77  GLVaoCache();
78  ~GLVaoCache();
79 
80  GLuint vao_get(GPUBatch *batch);
81  GLuint base_instance_vao_get(GPUBatch *batch, int i_first);
82 
83  GLuint lookup(const GLShaderInterface *interface);
84  void insert(const GLShaderInterface *interface, GLuint vao_id);
85  void remove(const GLShaderInterface *interface);
86  void clear(void);
87 
88  private:
89  void init(void);
90  void context_check(void);
91 };
92 
93 class GLBatch : public Batch {
94  public:
97 
98  public:
99  void draw(int v_first, int v_count, int i_first, int i_count) override;
100  void bind(int i_first);
101 
102  /* Convenience getters. */
103  GLIndexBuf *elem_(void) const
104  {
105  return static_cast<GLIndexBuf *>(unwrap(elem));
106  }
107  GLVertBuf *verts_(const int index) const
108  {
109  return static_cast<GLVertBuf *>(unwrap(verts[index]));
110  }
111  GLVertBuf *inst_(const int index) const
112  {
113  return static_cast<GLVertBuf *>(unwrap(inst[index]));
114  }
115 
117 };
118 
119 } // namespace gpu
120 } // namespace blender
unsigned int uint
Definition: BLI_sys_types.h:83
GPUBatch
Definition: GPU_batch.h:93
Read Guarded memory(de)allocation.
GLVertBuf * verts_(const int index) const
Definition: gl_batch.hh:107
MEM_CXX_CLASS_ALLOC_FUNCS("GLBatch")
void bind(int i_first)
Definition: gl_batch.cc:285
void draw(int v_first, int v_count, int i_first, int i_count) override
Definition: gl_batch.cc:310
GLVertBuf * inst_(const int index) const
Definition: gl_batch.hh:111
GLIndexBuf * elem_(void) const
Definition: gl_batch.hh:103
GLVaoCache vao_cache_
Definition: gl_batch.hh:96
GLuint base_instance_vao_get(GPUBatch *batch, int i_first)
Definition: gl_batch.cc:227
struct blender::gpu::GLVaoCache::@645::@647 static_vaos
struct blender::gpu::GLVaoCache::@645::@648 dynamic_vaos
GLuint lookup(const GLShaderInterface *interface)
Definition: gl_batch.cc:195
const GLShaderInterface ** interfaces
Definition: gl_batch.hh:71
const GLShaderInterface * interfaces[GPU_VAO_STATIC_LEN]
Definition: gl_batch.hh:65
void insert(const GLShaderInterface *interface, GLuint vao_id)
Definition: gl_batch.cc:78
GLuint vao_get(GPUBatch *batch)
Definition: gl_batch.cc:259
GLuint vao_ids[GPU_VAO_STATIC_LEN]
Definition: gl_batch.hh:66
void remove(const GLShaderInterface *interface)
Definition: gl_batch.cc:136
GPUBatch * batch
Definition: drawnode.c:3779
static float verts[][3]
#define GPU_VAO_STATIC_LEN
Definition: gl_batch.hh:44
static Context * unwrap(GPUContext *ctx)