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

gl_texture.cc « opengl « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c72dc6a5c38b8520869fa70fe83bf670a49fe30 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * 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) 2020 Blender Foundation.
 * All rights reserved.
 */

/** \file
 * \ingroup gpu
 */

#include "BKE_global.h"

#include "GPU_extensions.h"
#include "GPU_framebuffer.h"

#include "gl_backend.hh"
#include "gl_debug.hh"
#include "gl_state.hh"

#include "gl_texture.hh"

namespace blender::gpu {

/* -------------------------------------------------------------------- */
/** \name Creation & Deletion
 * \{ */

GLTexture::GLTexture(const char *name) : Texture(name)
{
  BLI_assert(GPU_context_active_get() != NULL);

  glGenTextures(1, &tex_id_);
}

GLTexture::~GLTexture()
{
  if (framebuffer_) {
    GPU_framebuffer_free(framebuffer_);
  }
  GPUContext *ctx = GPU_context_active_get();
  if (ctx != NULL && is_bound_) {
    /* This avoid errors when the texture is still inside the bound texture array. */
    ctx->state_manager->texture_unbind(this);
  }
  GLBackend::get()->tex_free(tex_id_);
}

/* Return true on success. */
bool GLTexture::init_internal(void)
{
  if ((format_ == GPU_DEPTH24_STENCIL8) && GPU_depth_blitting_workaround()) {
    /* MacOS + Radeon Pro fails to blit depth on GPU_DEPTH24_STENCIL8
     * but works on GPU_DEPTH32F_STENCIL8. */
    format_ = GPU_DEPTH32F_STENCIL8;
  }

  if ((type_ == GPU_TEXTURE_CUBE_ARRAY) && !GPU_arb_texture_cube_map_array_is_supported()) {
    debug::raise_gl_error("Attempt to create a cubemap array without hardware support!");
    return false;
  }

  target_ = to_gl_target(type_);

  /* TODO(fclem) Proxy check. */

  this->ensure_mipmaps(0);

  /* Avoid issue with incomplete textures. */
  if (false) {
    /* TODO(fclem) Direct State Access. */
  }
  else {
    GLContext::state_manager_active_get()->texture_bind_temp(this);
    glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  }

#ifndef __APPLE__
  if ((G.debug & G_DEBUG_GPU) && (GLEW_VERSION_4_3 || GLEW_KHR_debug)) {
    char sh_name[64];
    SNPRINTF(sh_name, "Texture-%s", name_);
    /* Binding before setting the label is needed on some drivers. */
    glObjectLabel(GL_TEXTURE, tex_id_, -1, sh_name);
  }
#endif

  GL_CHECK_ERROR("Post-texture creation");
  return true;
}

/* Return true on success. */
bool GLTexture::init_internal(GPUVertBuf *vbo)
{
  target_ = to_gl_target(type_);

  GLenum internal_format = to_gl_internal_format(format_);

  if (false) {
    /* TODO(fclem) Direct State Access. */
  }
  else {
    GLContext::state_manager_active_get()->texture_bind_temp(this);
    glTexBuffer(target_, internal_format, vbo->vbo_id);
  }

#ifndef __APPLE__
  if ((G.debug & G_DEBUG_GPU) && (GLEW_VERSION_4_3 || GLEW_KHR_debug)) {
    char sh_name[64];
    SNPRINTF(sh_name, "Texture-%s", name_);
    /* Binding before setting the label is needed on some drivers. */
    glObjectLabel(GL_TEXTURE, tex_id_, -1, sh_name);
  }
#endif

  GL_CHECK_ERROR("Post-texture buffer creation");
  return true;
}

/* Will create enough mipmaps up to get to the given level. */
void GLTexture::ensure_mipmaps(int miplvl)
{
  int effective_h = (type_ == GPU_TEXTURE_1D_ARRAY) ? 0 : h_;
  int effective_d = (type_ != GPU_TEXTURE_3D) ? 0 : d_;
  int max_dimension = max_iii(w_, effective_h, effective_d);
  int max_miplvl = floor(log2(max_dimension));
  miplvl = min_ii(miplvl, max_miplvl);

  while (mipmaps_ < miplvl) {
    int mip = ++mipmaps_;
    const int dimensions = this->dimensions_count();

    int w = mip_width_get(mip);
    int h = mip_height_get(mip);
    int d = mip_depth_get(mip);
    GLenum internal_format = to_gl_internal_format(format_);
    GLenum gl_format = to_gl_data_format(format_);
    GLenum gl_type = to_gl(to_data_format(format_));

    GLContext::state_manager_active_get()->texture_bind_temp(this);

    if (type_ == GPU_TEXTURE_CUBE) {
      for (int i = 0; i < d; i++) {
        GLenum target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
        glTexImage2D(target, mip, internal_format, w, h, 0, gl_format, gl_type, NULL);
      }
    }
    else if (format_flag_ & GPU_FORMAT_COMPRESSED) {
      size_t size = ((w + 3) / 4) * ((h + 3) / 4) * to_block_size(format_);
      switch (dimensions) {
        default:
        case 1:
          glCompressedTexImage1D(target_, mip, internal_format, w, 0, size, NULL);
          break;
        case 2:
          glCompressedTexImage2D(target_, mip, internal_format, w, h, 0, size, NULL);
          break;
        case 3:
          glCompressedTexImage3D(target_, mip, internal_format, w, h, d, 0, size, NULL);
          break;
      }
    }
    else {
      switch (dimensions) {
        default:
        case 1:
          glTexImage1D(target_, mip, internal_format, w, 0, gl_format, gl_type, NULL);
          break;
        case 2:
          glTexImage2D(target_, mip, internal_format, w, h, 0, gl_format, gl_type, NULL);
          break;
        case 3:
          glTexImage3D(target_, mip, internal_format, w, h, d, 0, gl_format, gl_type, NULL);
          break;
      }
    }

    GL_CHECK_ERROR("Post-mipmap creation");
  }

  this->mip_range_set(0, mipmaps_);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Operations
 * \{ */

void GLTexture::update_sub(
    int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data)
{
  BLI_assert(validate_data_format(format_, type));
  BLI_assert(data != NULL);

  this->ensure_mipmaps(mip);

  if (mip > mipmaps_) {
    debug::raise_gl_error("Updating a miplvl on a texture too small to have this many levels.");
    return;
  }

  const int dimensions = this->dimensions_count();
  GLenum gl_format = to_gl_data_format(format_);
  GLenum gl_type = to_gl(type);

  GLContext::state_manager_active_get()->texture_bind_temp(this);

  if (true && type_ == GPU_TEXTURE_CUBE) {
    /* TODO(fclem) bypass if direct state access is available. */
    /* Workaround when ARB_direct_state_access is not available. */
    for (int i = 0; i < extent[2]; i++) {
      GLenum target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + offset[2] + i;
      glTexSubImage2D(target, mip, UNPACK2(offset), UNPACK2(extent), gl_format, gl_type, data);
    }
  }
  else if (format_flag_ & GPU_FORMAT_COMPRESSED) {
    size_t size = ((extent[0] + 3) / 4) * ((extent[1] + 3) / 4) * to_block_size(format_);
    switch (dimensions) {
      default:
      case 1:
        glCompressedTexSubImage1D(target_, mip, offset[0], extent[0], gl_format, size, data);
        break;
      case 2:
        glCompressedTexSubImage2D(
            target_, mip, UNPACK2(offset), UNPACK2(extent), gl_format, size, data);
        break;
      case 3:
        glCompressedTexSubImage3D(
            target_, mip, UNPACK3(offset), UNPACK3(extent), gl_format, size, data);
        break;
    }
  }
  else {
    switch (dimensions) {
      default:
      case 1:
        glTexSubImage1D(target_, mip, offset[0], extent[0], gl_format, gl_type, data);
        break;
      case 2:
        glTexSubImage2D(target_, mip, UNPACK2(offset), UNPACK2(extent), gl_format, gl_type, data);
        break;
      case 3:
        glTexSubImage3D(target_, mip, UNPACK3(offset), UNPACK3(extent), gl_format, gl_type, data);
        break;
    }
  }

  GL_CHECK_ERROR("Post-update_sub");
}

/** This will create the mipmap images and populate them with filtered data from base level.
 * WARNING: Depth textures are not populated but they have their mips correctly defined.
 * WARNING: This resets the mipmap range.
 */
void GLTexture::generate_mipmap(void)
{
  this->ensure_mipmaps(9999);
  /* Some drivers have bugs when using glGenerateMipmap with depth textures (see T56789).
   * In this case we just create a complete texture with mipmaps manually without
   * down-sampling. You must initialize the texture levels using other methods like
   * GPU_framebuffer_recursive_downsample(). */
  if (format_flag_ & GPU_FORMAT_DEPTH) {
    return;
  }

  if (false) {
    /* TODO(fclem) Direct State Access. */
  }
  else {
    /* Downsample from mip 0 using implementation. */
    GLContext::state_manager_active_get()->texture_bind_temp(this);
    glGenerateMipmap(target_);
  }
}

void GLTexture::clear(eGPUDataFormat data_format, const void *data)
{
  BLI_assert(validate_data_format(format_, data_format));

  if (GLEW_ARB_clear_texture && !(G.debug & G_DEBUG_GPU_FORCE_WORKAROUNDS)) {
    int mip = 0;
    GLenum gl_format = to_gl_data_format(format_);
    GLenum gl_type = to_gl(data_format);
    glClearTexImage(tex_id_, mip, gl_format, gl_type, data);
  }
  else {
    /* Fallback for older GL. */
    GPUFrameBuffer *prev_fb = GPU_framebuffer_active_get();

    FrameBuffer *fb = reinterpret_cast<FrameBuffer *>(this->framebuffer_get());
    fb->bind(true);
    fb->clear_attachment(this->attachment_type(0), data_format, data);

    GPU_framebuffer_bind(prev_fb);
  }
}

void GLTexture::copy_to(Texture *dst_)
{
  GLTexture *dst = static_cast<GLTexture *>(dst_);
  GLTexture *src = this;

  BLI_assert((dst->w_ == src->w_) && (dst->h_ == src->h_) && (dst->d_ == src->d_));
  BLI_assert(dst->format_ == src->format_);
  BLI_assert(dst->type_ == src->type_);
  /* TODO support array / 3D textures. */
  BLI_assert(dst->d_ == 0);

  if (GLEW_ARB_copy_image && !GPU_texture_copy_workaround()) {
    /* Opengl 4.3 */
    int mip = 0;
    /* NOTE: mip_size_get() won't override any dimension that is equal to 0. */
    int extent[3] = {1, 1, 1};
    this->mip_size_get(mip, extent);
    glCopyImageSubData(
        src->tex_id_, target_, mip, 0, 0, 0, dst->tex_id_, target_, mip, 0, 0, 0, UNPACK3(extent));
  }
  else {
    /* Fallback for older GL. */
    GPU_framebuffer_blit(
        src->framebuffer_get(), 0, dst->framebuffer_get(), 0, to_framebuffer_bits(format_));
  }
}

void *GLTexture::read(int mip, eGPUDataFormat type)
{
  BLI_assert(!(format_flag_ & GPU_FORMAT_COMPRESSED));
  BLI_assert(mip <= mipmaps_);
  BLI_assert(validate_data_format(format_, type));

  /* NOTE: mip_size_get() won't override any dimension that is equal to 0. */
  int extent[3] = {1, 1, 1};
  this->mip_size_get(mip, extent);

  size_t sample_len = extent[0] * extent[1] * extent[2];
  size_t sample_size = to_bytesize(format_, type);
  size_t texture_size = sample_len * sample_size;

  /* AMD Pro driver have a bug that write 8 bytes past buffer size
   * if the texture is big. (see T66573) */
  void *data = MEM_mallocN(texture_size + 8, "GPU_texture_read");

  GLenum gl_format = to_gl_data_format(format_);
  GLenum gl_type = to_gl(type);

  if (false) {
    /* TODO(fclem) Direct State Access. */
    /* NOTE: DSA can read GL_TEXTURE_CUBE_MAP directly. */
  }
  else {
    GLContext::state_manager_active_get()->texture_bind_temp(this);
    if (type_ == GPU_TEXTURE_CUBE) {
      size_t cube_face_size = texture_size / 6;
      char *face_data = (char *)data;
      for (int i = 0; i < 6; i++, face_data += cube_face_size) {
        glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, mip, gl_format, gl_type, face_data);
      }
    }
    else {
      glGetTexImage(target_, mip, gl_format, gl_type, data);
    }
  }
  return data;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Getters & setters
 * \{ */

void GLTexture::swizzle_set(const char swizzle[4])
{
  GLint gl_swizzle[4] = {(GLint)swizzle_to_gl(swizzle[0]),
                         (GLint)swizzle_to_gl(swizzle[1]),
                         (GLint)swizzle_to_gl(swizzle[2]),
                         (GLint)swizzle_to_gl(swizzle[3])};
  if (false) {
    /* TODO(fclem) Direct State Access. */
  }
  else {
    GLContext::state_manager_active_get()->texture_bind_temp(this);
    glTexParameteriv(target_, GL_TEXTURE_SWIZZLE_RGBA, gl_swizzle);
  }
}

void GLTexture::mip_range_set(int min, int max)
{
  BLI_assert(min <= max && min >= 0 && max <= mipmaps_);
  if (false) {
    /* TODO(fclem) Direct State Access. */
  }
  else {
    GLContext::state_manager_active_get()->texture_bind_temp(this);
    glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, min);
    glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, max);
  }
}

struct GPUFrameBuffer *GLTexture::framebuffer_get(void)
{
  if (framebuffer_) {
    return framebuffer_;
  }
  BLI_assert(!(type_ & (GPU_TEXTURE_ARRAY | GPU_TEXTURE_CUBE | GPU_TEXTURE_1D | GPU_TEXTURE_3D)));
  /* TODO(fclem) cleanup this. Don't use GPU object but blender::gpu ones. */
  GPUTexture *gputex = reinterpret_cast<GPUTexture *>(static_cast<Texture *>(this));
  framebuffer_ = GPU_framebuffer_create(name_);
  GPU_framebuffer_texture_attach(framebuffer_, gputex, 0, 0);
  return framebuffer_;
}

/** \} */

/* TODO(fclem) Legacy. Should be removed at some point. */
uint GLTexture::gl_bindcode_get(void) const
{
  return tex_id_;
}

}  // namespace blender::gpu