Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gl_drawlist.cc « opengl « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50ab5574cd2da6f1a008cf523ae7452f2c4fb873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * 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
 *
 * Implementation of Multi Draw Indirect using OpenGL.
 * Fallback if the needed extensions are not supported.
 */

#include "BLI_assert.h"

#include "GPU_batch.h"
#include "GPU_capabilities.h"

#include "glew-mx.h"

#include "gpu_context_private.hh"
#include "gpu_drawlist_private.hh"
#include "gpu_vertex_buffer_private.hh"

#include "gl_backend.hh"
#include "gl_drawlist.hh"
#include "gl_primitive.hh"

#include <climits>

using namespace blender::gpu;

struct GLDrawCommand {
  GLuint v_count;
  GLuint i_count;
  GLuint v_first;
  GLuint i_first;
};

struct GLDrawCommandIndexed {
  GLuint v_count;
  GLuint i_count;
  GLuint v_first;
  GLuint base_index;
  GLuint i_first;
};

#define MDI_ENABLED (buffer_size_ != 0)
#define MDI_DISABLED (buffer_size_ == 0)
#define MDI_INDEXED (base_index_ != UINT_MAX)

GLDrawList::GLDrawList(int length)
{
  BLI_assert(length > 0);
  batch_ = nullptr;
  buffer_id_ = 0;
  command_len_ = 0;
  base_index_ = 0;
  command_offset_ = 0;
  data_size_ = 0;
  data_ = nullptr;

  if (GLContext::multi_draw_indirect_support) {
    /* Alloc the biggest possible command list, which is indexed. */
    buffer_size_ = sizeof(GLDrawCommandIndexed) * length;
  }
  else {
    /* Indicates MDI is not supported. */
    buffer_size_ = 0;
  }
  /* Force buffer specification on first init. */
  data_offset_ = buffer_size_;
}

GLDrawList::~GLDrawList()
{
  GLContext::buf_free(buffer_id_);
}

void GLDrawList::init()
{
  BLI_assert(GLContext::get());
  BLI_assert(MDI_ENABLED);
  BLI_assert(data_ == nullptr);
  batch_ = nullptr;
  command_len_ = 0;

  if (buffer_id_ == 0) {
    /* Allocate on first use. */
    glGenBuffers(1, &buffer_id_);
    context_ = GLContext::get();
  }

  glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer_id_);
  /* If buffer is full, orphan buffer data and start fresh. */
  size_t command_size = MDI_INDEXED ? sizeof(GLDrawCommandIndexed) : sizeof(GLDrawCommand);
  if (data_offset_ + command_size > buffer_size_) {
    glBufferData(GL_DRAW_INDIRECT_BUFFER, buffer_size_, nullptr, GL_DYNAMIC_DRAW);
    data_offset_ = 0;
  }
  /* Map the remaining range. */
  GLbitfield flag = GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
  data_size_ = buffer_size_ - data_offset_;
  data_ = (GLbyte *)glMapBufferRange(GL_DRAW_INDIRECT_BUFFER, data_offset_, data_size_, flag);
  command_offset_ = 0;
}

void GLDrawList::append(GPUBatch *gpu_batch, int i_first, int i_count)
{
  /* Fallback when MultiDrawIndirect is not supported/enabled. */
  if (MDI_DISABLED) {
    GPU_batch_draw_advanced(gpu_batch, 0, 0, i_first, i_count);
    return;
  }

  if (data_ == nullptr) {
    this->init();
  }

  GLBatch *batch = static_cast<GLBatch *>(gpu_batch);
  if (batch != batch_) {
    // BLI_assert(batch->flag | GPU_BATCH_INIT);
    this->submit();
    batch_ = batch;
    /* Cached for faster access. */
    GLIndexBuf *el = batch_->elem_();
    base_index_ = el ? el->index_base_ : UINT_MAX;
    v_first_ = el ? el->index_start_ : 0;
    v_count_ = el ? el->index_len_ : batch->verts_(0)->vertex_len;
  }

  if (v_count_ == 0) {
    /* Nothing to draw. */
    return;
  }

  if (MDI_INDEXED) {
    GLDrawCommandIndexed *cmd = reinterpret_cast<GLDrawCommandIndexed *>(data_ + command_offset_);
    cmd->v_first = v_first_;
    cmd->v_count = v_count_;
    cmd->i_count = i_count;
    cmd->base_index = base_index_;
    cmd->i_first = i_first;
  }
  else {
    GLDrawCommand *cmd = reinterpret_cast<GLDrawCommand *>(data_ + command_offset_);
    cmd->v_first = v_first_;
    cmd->v_count = v_count_;
    cmd->i_count = i_count;
    cmd->i_first = i_first;
  }

  size_t command_size = MDI_INDEXED ? sizeof(GLDrawCommandIndexed) : sizeof(GLDrawCommand);

  command_offset_ += command_size;
  command_len_++;

  /* Check if we can fit at least one other command. */
  if (command_offset_ + command_size > data_size_) {
    this->submit();
  }
}

void GLDrawList::submit()
{
  if (command_len_ == 0) {
    return;
  }
  /* Something's wrong if we get here without MDI support. */
  BLI_assert(MDI_ENABLED);
  BLI_assert(data_);
  BLI_assert(GLContext::get()->shader != nullptr);

  size_t command_size = MDI_INDEXED ? sizeof(GLDrawCommandIndexed) : sizeof(GLDrawCommand);

  /* Only do multi-draw indirect if doing more than 2 drawcall. This avoids the overhead of
   * buffer mapping if scene is not very instance friendly. BUT we also need to take into
   * account the case where only a few instances are needed to finish filling a call buffer. */
  const bool is_finishing_a_buffer = (command_offset_ + command_size > data_size_);
  if (command_len_ > 2 || is_finishing_a_buffer) {
    GLenum prim = to_gl(batch_->prim_type);
    void *offset = (void *)data_offset_;

    glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer_id_);
    glFlushMappedBufferRange(GL_DRAW_INDIRECT_BUFFER, 0, command_offset_);
    glUnmapBuffer(GL_DRAW_INDIRECT_BUFFER);
    data_ = nullptr; /* Unmapped */
    data_offset_ += command_offset_;

    batch_->bind(0);

    if (MDI_INDEXED) {
      GLenum gl_type = to_gl(batch_->elem_()->index_type_);
      glMultiDrawElementsIndirect(prim, gl_type, offset, command_len_, 0);
    }
    else {
      glMultiDrawArraysIndirect(prim, offset, command_len_, 0);
    }
  }
  else {
    /* Fallback do simple drawcalls, and don't unmap the buffer. */
    if (MDI_INDEXED) {
      GLDrawCommandIndexed *cmd = (GLDrawCommandIndexed *)data_;
      for (int i = 0; i < command_len_; i++, cmd++) {
        /* Index start was already added. Avoid counting it twice. */
        cmd->v_first -= v_first_;
        batch_->draw(cmd->v_first, cmd->v_count, cmd->i_first, cmd->i_count);
      }
      /* Reuse the same data. */
      command_offset_ -= command_len_ * sizeof(GLDrawCommandIndexed);
    }
    else {
      GLDrawCommand *cmd = (GLDrawCommand *)data_;
      for (int i = 0; i < command_len_; i++, cmd++) {
        batch_->draw(cmd->v_first, cmd->v_count, cmd->i_first, cmd->i_count);
      }
      /* Reuse the same data. */
      command_offset_ -= command_len_ * sizeof(GLDrawCommand);
    }
  }
  /* Do not submit this buffer again. */
  command_len_ = 0;
  /* Avoid keeping reference to the batch. */
  batch_ = nullptr;
}

/** \} */