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

gpu_shader_block.cc « intern « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c78e73c20571b6cd782f45f80c2f6566dca062a6 (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
/*
 * 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) 2021 Blender Foundation.
 * All rights reserved.
 */

/** \file
 * \ingroup gpu
 */

#include "gpu_uniform_buffer_private.hh"

#include "MEM_guardedalloc.h"

#include "GPU_shader.h"
#include "gpu_shader_block.hh"
#include "gpu_shader_interface.hh"

namespace blender::gpu {

/* -------------------------------------------------------------------- */
/** \name Attribute bindings
 * \{ */
const int BUILTIN_BINDING_LOCATION_OFFSET = 1000;

static constexpr int to_binding_location(const GPUUniformBuiltin builtin_uniform)
{
  return BUILTIN_BINDING_LOCATION_OFFSET + builtin_uniform;
}

static GPUUniformBuiltin to_builtin_uniform(int location)
{
  return static_cast<GPUUniformBuiltin>(location - BUILTIN_BINDING_LOCATION_OFFSET);
}

static bool is_valid_location(int location)
{
  int builtin_uniform = location - BUILTIN_BINDING_LOCATION_OFFSET;
  if (builtin_uniform < 0 || builtin_uniform >= GPU_NUM_UNIFORMS) {
    return false;
  }
  return true;
}

static constexpr ShaderBlockType::AttributeBinding determine_location_3d_color(
    const GPUUniformBuiltin builtin_uniform)
{
  ShaderBlockType::AttributeBinding result = {-1, 0};

  switch (builtin_uniform) {
    case GPU_UNIFORM_MODEL:
      result.location = to_binding_location(builtin_uniform);
      result.offset = offsetof(GPUShaderBlock3dColor, ModelMatrix);
      break;
    case GPU_UNIFORM_MVP:
      result.location = to_binding_location(builtin_uniform);
      result.offset = offsetof(GPUShaderBlock3dColor, ModelViewProjectionMatrix);
      break;
    case GPU_UNIFORM_COLOR:
      result.location = to_binding_location(builtin_uniform);
      result.offset = offsetof(GPUShaderBlock3dColor, color);
      break;
    case GPU_UNIFORM_CLIPPLANES:
      result.location = to_binding_location(builtin_uniform);
      result.offset = offsetof(GPUShaderBlock3dColor, WorldClipPlanes);
      break;
    case GPU_UNIFORM_SRGB_TRANSFORM:
      result.location = to_binding_location(builtin_uniform);
      result.offset = offsetof(GPUShaderBlock3dColor, SrgbTransform);
      break;

    default:
      break;
  };

  return result;
}

static constexpr ShaderBlockType::AttributeBinding determine_binding(
    const GPUShaderBlockType block_type, const GPUUniformBuiltin builtin_uniform)
{

  switch (block_type) {
    case GPU_SHADER_BLOCK_CUSTOM:
    case GPU_NUM_SHADER_BLOCK_TYPES:
      return {};

    case GPU_SHADER_BLOCK_3D_COLOR:
      return determine_location_3d_color(builtin_uniform);
  };
  return {};
}

static constexpr std::array<const ShaderBlockType::AttributeBinding, GPU_NUM_UNIFORMS>
builtin_uniforms_for_struct_type(const GPUShaderBlockType block_type)
{
  return {
      determine_binding(block_type, GPU_UNIFORM_MODEL),
      determine_binding(block_type, GPU_UNIFORM_VIEW),
      determine_binding(block_type, GPU_UNIFORM_MODELVIEW),
      determine_binding(block_type, GPU_UNIFORM_PROJECTION),
      determine_binding(block_type, GPU_UNIFORM_VIEWPROJECTION),
      determine_binding(block_type, GPU_UNIFORM_MVP),
      determine_binding(block_type, GPU_UNIFORM_MODEL_INV),
      determine_binding(block_type, GPU_UNIFORM_VIEW_INV),
      determine_binding(block_type, GPU_UNIFORM_MODELVIEW_INV),
      determine_binding(block_type, GPU_UNIFORM_PROJECTION_INV),
      determine_binding(block_type, GPU_UNIFORM_VIEWPROJECTION_INV),
      determine_binding(block_type, GPU_UNIFORM_NORMAL),
      determine_binding(block_type, GPU_UNIFORM_ORCO),
      determine_binding(block_type, GPU_UNIFORM_CLIPPLANES),
      determine_binding(block_type, GPU_UNIFORM_COLOR),
      determine_binding(block_type, GPU_UNIFORM_BASE_INSTANCE),
      determine_binding(block_type, GPU_UNIFORM_RESOURCE_CHUNK),
      determine_binding(block_type, GPU_UNIFORM_RESOURCE_ID),
      determine_binding(block_type, GPU_UNIFORM_SRGB_TRANSFORM),
  };
}

static constexpr std::array<
    const std::array<const ShaderBlockType::AttributeBinding, GPU_NUM_UNIFORMS>,
    GPU_NUM_SHADER_BLOCK_TYPES>
    ATTRIBUTE_BINDINGS = {
        builtin_uniforms_for_struct_type(GPU_SHADER_BLOCK_CUSTOM),
        builtin_uniforms_for_struct_type(GPU_SHADER_BLOCK_3D_COLOR),
};

static constexpr size_t data_size_for(const GPUShaderBlockType block_type)
{

  switch (block_type) {
    case GPU_SHADER_BLOCK_CUSTOM:
    case GPU_NUM_SHADER_BLOCK_TYPES:
      return 0;

    case GPU_SHADER_BLOCK_3D_COLOR:
      return sizeof(GPUShaderBlock3dColor);
  };
  return 0;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Struct type
 * \{ */

constexpr ShaderBlockType::ShaderBlockType(const GPUShaderBlockType type)
    : type(type), m_attribute_bindings(ATTRIBUTE_BINDINGS[type]), m_data_size(data_size_for(type))
{
}

bool ShaderBlockType::AttributeBinding::has_binding() const
{
  return location != -1;
}

bool ShaderBlockType::has_all_builtin_uniforms(const ShaderInterface &interface) const
{
  for (int i = 0; i < GPU_NUM_UNIFORMS; i++) {
    const GPUUniformBuiltin builtin_uniform = static_cast<const GPUUniformBuiltin>(i);
    const AttributeBinding &binding = attribute_binding(builtin_uniform);
    const bool builtin_is_used = interface.builtins_[i] != -1;
    const bool has_attribute = binding.has_binding();
    if (builtin_is_used && !has_attribute) {
      return false;
    }
  }
  return true;
}

static constexpr std::array<ShaderBlockType, GPU_NUM_SHADER_BLOCK_TYPES> STRUCT_TYPE_INFOS = {
    ShaderBlockType(GPU_SHADER_BLOCK_CUSTOM),
    ShaderBlockType(GPU_SHADER_BLOCK_3D_COLOR),
};

const ShaderBlockType &ShaderBlockType::get(const GPUShaderBlockType type)
{
  return STRUCT_TYPE_INFOS[type];
}

std::optional<const GPUShaderBlockType> find_smallest_uniform_builtin_struct(
    const ShaderInterface &interface)
{
  if (!interface.has_builtin_uniforms()) {
    return std::nullopt;
  }

  if (ShaderBlockType::get(GPU_SHADER_BLOCK_3D_COLOR).has_all_builtin_uniforms(interface)) {
    return std::make_optional(GPU_SHADER_BLOCK_3D_COLOR);
  }

  return std::nullopt;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Struct type
 * \{ */

ShaderBlockBuffer::ShaderBlockBuffer(const GPUShaderBlockType type)
    : m_type_info(ShaderBlockType::get(type))
{
  m_data = MEM_mallocN(m_type_info.data_size(), __func__);
}

ShaderBlockBuffer::~ShaderBlockBuffer()
{
  MEM_freeN(m_data);
  m_data = nullptr;
}

bool ShaderBlockBuffer::uniform_int(int location, int comp_len, int array_size, const int *data)
{
  if (!is_valid_location(location)) {
    return false;
  }
  const GPUUniformBuiltin builtin_uniform = to_builtin_uniform(location);
  const ShaderBlockType::AttributeBinding &attribute = m_type_info.attribute_binding(
      builtin_uniform);

  if (!attribute.has_binding()) {
    return false;
  }
  const size_t attribute_len = comp_len * array_size * sizeof(int);
  memcpy(((uint8_t *)m_data) + attribute.offset, data, attribute_len);
  m_flags.is_dirty = true;

  return true;
}

bool ShaderBlockBuffer::uniform_float(int location,
                                      int comp_len,
                                      int array_size,
                                      const float *data)
{
  if (!is_valid_location(location)) {
    return false;
  }
  const GPUUniformBuiltin builtin_uniform = to_builtin_uniform(location);
  const ShaderBlockType::AttributeBinding &attribute = m_type_info.attribute_binding(
      builtin_uniform);

  if (!attribute.has_binding()) {
    return false;
  }
  const size_t attribute_len = comp_len * array_size * sizeof(float);
  memcpy(((uint8_t *)m_data) + attribute.offset, data, attribute_len);
  m_flags.is_dirty = true;

  return true;
}

/** \} */

}  // namespace blender::gpu