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

gpu_shader_interface.cc « intern « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae94112b17b67cc892cd30bdd7dfc7a7117357a1 (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
/*
 * 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
 *
 * GPU shader interface (C --> GLSL)
 */

#include "MEM_guardedalloc.h"

#include "BLI_span.hh"
#include "BLI_vector.hh"

#include "gpu_shader_interface.hh"

namespace blender::gpu {

/* TODO(fclem): add unique ID for debugging. */
ShaderInterface::ShaderInterface() = default;

ShaderInterface::~ShaderInterface()
{
  /* Free memory used by name_buffer. */
  MEM_freeN(name_buffer_);
  MEM_freeN(inputs_);
}

static void sort_input_list(MutableSpan<ShaderInput> dst)
{
  if (dst.size() == 0) {
    return;
  }

  Vector<ShaderInput> inputs_vec = Vector<ShaderInput>(dst.size());
  MutableSpan<ShaderInput> src = inputs_vec.as_mutable_span();
  src.copy_from(dst);

  /* Simple sorting by going through the array and selecting the biggest element each time. */
  for (uint i = 0; i < dst.size(); i++) {
    ShaderInput *input_src = &src[0];
    for (uint j = 1; j < src.size(); j++) {
      if (src[j].name_hash > input_src->name_hash) {
        input_src = &src[j];
      }
    }
    dst[i] = *input_src;
    input_src->name_hash = 0;
  }
}

/* Sorts all inputs inside their respective array.
 * This is to allow fast hash collision detection.
 * See ShaderInterface::input_lookup for more details. */
void ShaderInterface::sort_inputs()
{
  sort_input_list(MutableSpan<ShaderInput>(inputs_, attr_len_));
  sort_input_list(MutableSpan<ShaderInput>(inputs_ + attr_len_, ubo_len_));
  sort_input_list(MutableSpan<ShaderInput>(inputs_ + attr_len_ + ubo_len_, uniform_len_));
}

void ShaderInterface::debug_print()
{
  Span<ShaderInput> attrs = Span<ShaderInput>(inputs_, attr_len_);
  Span<ShaderInput> ubos = Span<ShaderInput>(inputs_ + attr_len_, ubo_len_);
  Span<ShaderInput> uniforms = Span<ShaderInput>(inputs_ + attr_len_ + ubo_len_, uniform_len_);
  Span<ShaderInput> ssbos = Span<ShaderInput>(inputs_ + attr_len_ + ubo_len_ + uniform_len_,
                                              ssbo_len_);
  char *name_buf = name_buffer_;
  const char format[] = "      | %.8x : %4d : %s\n";

  if (attrs.size() > 0) {
    printf("\n    Attributes :\n");
  }
  for (const ShaderInput &attr : attrs) {
    printf(format, attr.name_hash, attr.location, name_buf + attr.name_offset);
  }

  if (uniforms.size() > 0) {
    printf("\n    Uniforms :\n");
  }
  for (const ShaderInput &uni : uniforms) {
    /* Bypass samplers. */
    if (uni.binding == -1) {
      printf(format, uni.name_hash, uni.location, name_buf + uni.name_offset);
    }
  }

  if (ubos.size() > 0) {
    printf("\n    Uniform Buffer Objects :\n");
  }
  for (const ShaderInput &ubo : ubos) {
    printf(format, ubo.name_hash, ubo.binding, name_buf + ubo.name_offset);
  }

  if (enabled_tex_mask_ > 0) {
    printf("\n    Samplers :\n");
  }
  for (const ShaderInput &samp : uniforms) {
    /* Bypass uniforms. */
    if (samp.binding != -1) {
      printf(format, samp.name_hash, samp.binding, name_buf + samp.name_offset);
    }
  }

  if (ssbos.size() > 0) {
    printf("\n    Shader Storage Objects :\n");
  }
  for (const ShaderInput &ssbo : ssbos) {
    printf(format, ssbo.name_hash, ssbo.binding, name_buf + ssbo.name_offset);
  }

  printf("\n");
}

}  // namespace blender::gpu