Blender  V2.93
gl_vertex_array.cc
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  * The Original Code is Copyright (C) 2016 by Mike Erwin.
17  * All rights reserved.
18  */
19 
24 #include "gpu_shader_interface.hh"
27 
28 #include "gl_batch.hh"
29 #include "gl_context.hh"
30 #include "gl_index_buffer.hh"
31 #include "gl_vertex_buffer.hh"
32 
33 #include "gl_vertex_array.hh"
34 
35 namespace blender::gpu {
36 
37 /* -------------------------------------------------------------------- */
41 /* Returns enabled vertex pointers as a bitflag (one bit per attrib). */
42 static uint16_t vbo_bind(const ShaderInterface *interface,
43  const GPUVertFormat *format,
44  uint v_first,
45  uint v_len,
46  const bool use_instancing)
47 {
48  uint16_t enabled_attrib = 0;
49  const uint attr_len = format->attr_len;
50  uint stride = format->stride;
51  uint offset = 0;
52  GLuint divisor = (use_instancing) ? 1 : 0;
53 
54  for (uint a_idx = 0; a_idx < attr_len; a_idx++) {
55  const GPUVertAttr *a = &format->attrs[a_idx];
56 
57  if (format->deinterleaved) {
58  offset += ((a_idx == 0) ? 0 : format->attrs[a_idx - 1].sz) * v_len;
59  stride = a->sz;
60  }
61  else {
62  offset = a->offset;
63  }
64 
65  /* This is in fact an offset in memory. */
66  const GLvoid *pointer = (const GLubyte *)(intptr_t)(offset + v_first * stride);
67  const GLenum type = to_gl(static_cast<GPUVertCompType>(a->comp_type));
68 
69  for (uint n_idx = 0; n_idx < a->name_len; n_idx++) {
70  const char *name = GPU_vertformat_attr_name_get(format, a, n_idx);
71  const ShaderInput *input = interface->attr_get(name);
72 
73  if (input == nullptr) {
74  continue;
75  }
76 
77  enabled_attrib |= (1 << input->location);
78 
79  if (ELEM(a->comp_len, 16, 12, 8)) {
80  BLI_assert(a->fetch_mode == GPU_FETCH_FLOAT);
81  BLI_assert(a->comp_type == GPU_COMP_F32);
82  for (int i = 0; i < a->comp_len / 4; i++) {
83  glEnableVertexAttribArray(input->location + i);
84  glVertexAttribDivisor(input->location + i, divisor);
85  glVertexAttribPointer(
86  input->location + i, 4, type, GL_FALSE, stride, (const GLubyte *)pointer + i * 16);
87  }
88  }
89  else {
90  glEnableVertexAttribArray(input->location);
91  glVertexAttribDivisor(input->location, divisor);
92 
93  switch (a->fetch_mode) {
94  case GPU_FETCH_FLOAT:
96  glVertexAttribPointer(input->location, a->comp_len, type, GL_FALSE, stride, pointer);
97  break;
99  glVertexAttribPointer(input->location, a->comp_len, type, GL_TRUE, stride, pointer);
100  break;
101  case GPU_FETCH_INT:
102  glVertexAttribIPointer(input->location, a->comp_len, type, stride, pointer);
103  break;
104  }
105  }
106  }
107  }
108  return enabled_attrib;
109 }
110 
111 /* Update the Attrib Binding of the currently bound VAO. */
112 void GLVertArray::update_bindings(const GLuint vao,
113  const GPUBatch *batch_, /* Should be GLBatch. */
114  const ShaderInterface *interface,
115  const int base_instance)
116 {
117  const GLBatch *batch = static_cast<const GLBatch *>(batch_);
118  uint16_t attr_mask = interface->enabled_attr_mask_;
119 
120  glBindVertexArray(vao);
121 
122  /* Reverse order so first VBO'S have more prevalence (in term of attribute override). */
123  for (int v = GPU_BATCH_VBO_MAX_LEN - 1; v > -1; v--) {
124  GLVertBuf *vbo = batch->verts_(v);
125  if (vbo) {
126  vbo->bind();
127  attr_mask &= ~vbo_bind(interface, &vbo->format, 0, vbo->vertex_len, false);
128  }
129  }
130 
131  for (int v = GPU_BATCH_INST_VBO_MAX_LEN - 1; v > -1; v--) {
132  GLVertBuf *vbo = batch->inst_(v);
133  if (vbo) {
134  vbo->bind();
135  attr_mask &= ~vbo_bind(interface, &vbo->format, base_instance, vbo->vertex_len, true);
136  }
137  }
138 
139  if (attr_mask != 0 && GLContext::vertex_attrib_binding_support) {
140  for (uint16_t mask = 1, a = 0; a < 16; a++, mask <<= 1) {
141  if (attr_mask & mask) {
142  GLContext *ctx = GLContext::get();
143  /* This replaces glVertexAttrib4f(a, 0.0f, 0.0f, 0.0f, 1.0f); with a more modern style.
144  * Fix issues for some drivers (see T75069). */
145  glBindVertexBuffer(a, ctx->default_attr_vbo_, (intptr_t)0, (intptr_t)0);
146  glEnableVertexAttribArray(a);
147  glVertexAttribFormat(a, 4, GL_FLOAT, GL_FALSE, 0);
148  glVertexAttribBinding(a, a);
149  }
150  }
151  }
152 
153  if (batch->elem) {
154  /* Binds the index buffer. This state is also saved in the VAO. */
155  static_cast<GLIndexBuf *>(unwrap(batch->elem))->bind();
156  }
157 }
158 
159 /* Another version of update_bindings for Immediate mode. */
160 void GLVertArray::update_bindings(const GLuint vao,
161  const uint v_first,
162  const GPUVertFormat *format,
163  const ShaderInterface *interface)
164 {
165  glBindVertexArray(vao);
166 
167  vbo_bind(interface, format, v_first, 0, false);
168 }
169 
172 } // namespace blender::gpu
#define BLI_assert(a)
Definition: BLI_assert.h:58
unsigned int uint
Definition: BLI_sys_types.h:83
#define ELEM(...)
GPUBatch
Definition: GPU_batch.h:93
#define GPU_BATCH_INST_VBO_MAX_LEN
Definition: GPU_batch.h:36
#define GPU_BATCH_VBO_MAX_LEN
Definition: GPU_batch.h:35
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei stride
@ GPU_FETCH_FLOAT
@ GPU_FETCH_INT_TO_FLOAT_UNIT
@ GPU_FETCH_INT
@ GPU_FETCH_INT_TO_FLOAT
BLI_INLINE const char * GPU_vertformat_attr_name_get(const GPUVertFormat *format, const GPUVertAttr *attr, uint n_idx)
GPUVertCompType
@ GPU_COMP_F32
ATTR_WARN_UNUSED_RESULT const BMVert * v
static bool vertex_attrib_binding_support
Definition: gl_context.hh:76
static GLContext * get()
Definition: gl_context.hh:118
const ShaderInput * attr_get(const char *name) const
GPUBatch * batch
Definition: drawnode.c:3779
format
Definition: logImageCore.h:47
static unsigned a[3]
Definition: RandGen.cpp:92
void update_bindings(const GLuint vao, const GPUBatch *batch, const ShaderInterface *interface, const int base_instance)
static Context * unwrap(GPUContext *ctx)
static uint16_t vbo_bind(const ShaderInterface *interface, const GPUVertFormat *format, uint v_first, uint v_len, const bool use_instancing)
static GLenum to_gl(const GPUAttachmentType type)
unsigned short uint16_t
Definition: stdint.h:82
_W64 int intptr_t
Definition: stdint.h:121
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)