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

common_debug_print_lib.glsl « shaders « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89d1729b52d1e77c85fc1153e3a457b248ca716d (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

/**
 * Debug print implementation for shaders.
 *
 * `print()`:
 *   Log variable or strings inside the viewport.
 *   Using a unique non string argument will print the variable name with it.
 *   Concatenate by using multiple arguments. i.e: `print("Looped ", n, "times.")`.
 * `drw_print_no_endl()`:
 *   Same as `print()` but does not finish the line.
 * `drw_print_value()`:
 *   Display only the value of a variable. Does not finish the line.
 * `drw_print_value_hex()`:
 *   Display only the hex representation of a variable. Does not finish the line.
 * `drw_print_value_binary()`: Display only the binary representation of a
 * variable. Does not finish the line.
 *
 * IMPORTANT: As it is now, it is not yet thread safe. Only print from one thread. You can use the
 * IS_DEBUG_MOUSE_FRAGMENT macro in fragment shader to filter using mouse position or
 * IS_FIRST_INVOCATION in compute shaders.
 *
 * NOTE: Floating point representation might not be very precise (see drw_print_value(float)).
 *
 * IMPORTANT: Multipler drawcalls can write to the buffer in sequence (if they are from different
 * shgroups). However, we add barriers to support this case and it might change the application
 * behavior. Uncomment DISABLE_DEBUG_SHADER_drw_print_BARRIER to remove the barriers if that
 * happens. But then you are limited to a single invocation output.
 *
 * IMPORTANT: All of these are copied to the CPU debug libs (draw_debug.cc). They need to be kept
 * in sync to write the same data.
 */

/** Global switch option when you want to silence all prints from all shaders at once. */
bool drw_debug_print_enable = true;

/* Set drw_print_col to max value so we will start by creating a new line and get the correct
 * threadsafe row. */
uint drw_print_col = DRW_DEBUG_PRINT_WORD_WRAP_COLUMN;
uint drw_print_row = 0u;

void drw_print_newline()
{
  if (!drw_debug_print_enable) {
    return;
  }
  drw_print_col = 0u;
  drw_print_row = atomicAdd(drw_debug_print_row_shared, 1u) + 1u;
}

void drw_print_string_start(uint len)
{
  if (!drw_debug_print_enable) {
    return;
  }
  /* Break before word. */
  if (drw_print_col + len > DRW_DEBUG_PRINT_WORD_WRAP_COLUMN) {
    drw_print_newline();
  }
}

void drw_print_char4(uint data)
{
  if (!drw_debug_print_enable) {
    return;
  }
  /* Convert into char stream. */
  for (; data != 0u; data >>= 8u) {
    uint char1 = data & 0xFFu;
    /* Check for null terminator. */
    if (char1 == 0x00) {
      break;
    }
    uint cursor = atomicAdd(drw_debug_print_cursor, 1u);
    cursor += drw_debug_print_offset;
    if (cursor < DRW_DEBUG_PRINT_MAX) {
      /* For future usage. (i.e: Color) */
      uint flags = 0u;
      uint col = drw_print_col++;
      uint drw_print_header = (flags << 24u) | (drw_print_row << 16u) | (col << 8u);
      drw_debug_print_buf[cursor] = drw_print_header | char1;
      /* Break word. */
      if (drw_print_col > DRW_DEBUG_PRINT_WORD_WRAP_COLUMN) {
        drw_print_newline();
      }
    }
  }
}

/**
 * NOTE(fclem): Strange behavior emerge when trying to increment the digit
 * counter inside the append function. It looks like the compiler does not see
 * it is referenced as an index for char4 and thus do not capture the right
 * reference. I do not know if this is undefined behavior. As a matter of
 * precaution, we implement all the append function separately. This behavior
 * was observed on both Mesa & amdgpu-pro.
 */
/* Using ascii char code. Expect char1 to be less or equal to 0xFF. Appends chars to the right. */
void drw_print_append_char(uint char1, inout uint char4)
{
  char4 = (char4 << 8u) | char1;
}

void drw_print_append_digit(uint digit, inout uint char4)
{
  const uint char_A = 0x41u;
  const uint char_0 = 0x30u;
  bool is_hexadecimal = digit > 9u;
  char4 = (char4 << 8u) | (is_hexadecimal ? (char_A + digit - 10u) : (char_0 + digit));
}

void drw_print_append_space(inout uint char4)
{
  char4 = (char4 << 8u) | 0x20u;
}

void drw_print_value_binary(uint value)
{
  drw_print_no_endl("0b");
  drw_print_string_start(10u * 4u);
  uint digits[10] = uint[10](0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u);
  uint digit = 0u;
  for (uint i = 0u; i < 32u; i++) {
    drw_print_append_digit(((value >> i) & 1u), digits[digit / 4u]);
    digit++;
    if ((i % 4u) == 3u) {
      drw_print_append_space(digits[digit / 4u]);
      digit++;
    }
  }
  /* Numbers are written from right to left. So we need to reverse the order. */
  for (int j = 9; j >= 0; j--) {
    drw_print_char4(digits[j]);
  }
}

void drw_print_value_binary(int value)
{
  drw_print_value_binary(uint(value));
}

void drw_print_value_binary(float value)
{
  drw_print_value_binary(floatBitsToUint(value));
}

void drw_print_value_uint(uint value, const bool hex, bool is_negative, const bool is_unsigned)
{
  drw_print_string_start(3u * 4u);
  const uint blank_value = hex ? 0x30303030u : 0x20202020u;
  const uint prefix = hex ? 0x78302020u : 0x20202020u;
  uint digits[3] = uint[3](blank_value, blank_value, prefix);
  const uint base = hex ? 16u : 10u;
  uint digit = 0u;
  /* Add `u` suffix. */
  if (is_unsigned) {
    drw_print_append_char('u', digits[digit / 4u]);
    digit++;
  }
  /* Number's digits. */
  for (; value != 0u || digit == uint(is_unsigned); value /= base) {
    drw_print_append_digit(value % base, digits[digit / 4u]);
    digit++;
  }
  /* Add negative sign. */
  if (is_negative) {
    drw_print_append_char('-', digits[digit / 4u]);
    digit++;
  }
  /* Need to pad to uint alignment because we are issuing chars in "reverse". */
  for (uint i = digit % 4u; i < 4u && i > 0u; i++) {
    drw_print_append_space(digits[digit / 4u]);
    digit++;
  }
  /* Numbers are written from right to left. So we need to reverse the order. */
  for (int j = 2; j >= 0; j--) {
    drw_print_char4(digits[j]);
  }
}

void drw_print_value_hex(uint value)
{
  drw_print_value_uint(value, true, false, false);
}

void drw_print_value_hex(int value)
{
  drw_print_value_uint(uint(value), true, false, false);
}

void drw_print_value_hex(float value)
{
  drw_print_value_uint(floatBitsToUint(value), true, false, false);
}

void drw_print_value(uint value)
{
  drw_print_value_uint(value, false, false, true);
}

void drw_print_value(int value)
{
  drw_print_value_uint(uint(abs(value)), false, (value < 0), false);
}

void drw_print_value(bool value)
{
  if (value) {
    drw_print_no_endl("true ");
  }
  else {
    drw_print_no_endl("false");
  }
}

/* NOTE(@fclem): This is homebrew and might not be 100% accurate (accuracy has
 * not been tested and might dependent on compiler implementation). If unsure,
 * use drw_print_value_hex and transcribe the value manually with another tool. */
void drw_print_value(float val)
{
  /* We pad the string to match normal float values length. */
  if (isnan(val)) {
    drw_print_no_endl("         NaN");
    return;
  }
  if (isinf(val)) {
    if (sign(val) < 0.0) {
      drw_print_no_endl("        -Inf");
    }
    else {
      drw_print_no_endl("         Inf");
    }
    return;
  }

  /* Adjusted for significant digits (6) with sign (1), decimal separator (1)
   * and exponent (4). */
  const float significant_digits = 6.0;
  drw_print_string_start(3u * 4u);
  uint digits[3] = uint[3](0x20202020u, 0x20202020u, 0x20202020u);

  float exponent = floor(log(abs(val)) / log(10.0));
  bool display_exponent = exponent >= (significant_digits) ||
                          exponent <= (-significant_digits + 1.0);

  float int_significant_digits = min(exponent + 1.0, significant_digits);
  float dec_significant_digits = max(0.0, significant_digits - int_significant_digits);
  /* Power to get to the rounding point. */
  float rounding_power = dec_significant_digits;

  if (val == 0.0 || isinf(exponent)) {
    display_exponent = false;
    int_significant_digits = dec_significant_digits = 1.0;
  }
  /* Remap to keep significant numbers count. */
  if (display_exponent) {
    int_significant_digits = 1.0;
    dec_significant_digits = significant_digits - int_significant_digits;
    rounding_power = -exponent + dec_significant_digits;
  }
  /* Round at the last significant digit. */
  val = round(val * pow(10.0, rounding_power));
  /* Get back to final exponent. */
  val *= pow(10.0, -dec_significant_digits);

  float int_part;
  float dec_part = modf(val, int_part);

  dec_part *= pow(10.0, dec_significant_digits);

  const uint base = 10u;
  uint digit = 0u;
  /* Exponent */
  uint value = uint(abs(exponent));
  if (display_exponent) {
    for (int i = 0; value != 0u || i == 0; i++, value /= base) {
      drw_print_append_digit(value % base, digits[digit / 4u]);
      digit++;
    }
    /* Exponent sign. */
    uint sign_char = (exponent < 0.0) ? '-' : '+';
    drw_print_append_char(sign_char, digits[digit / 4u]);
    digit++;
    /* Exponent `e` suffix. */
    drw_print_append_char(0x65u, digits[digit / 4u]);
    digit++;
  }
  /* Decimal part. */
  value = uint(abs(dec_part));
#if 0 /* We don't do that because it makes unstable values really hard to \
         read. */
  /* Trim trailing zeros. */
  while ((value % base) == 0u) {
    value /= base;
    if (value == 0u) {
      break;
    }
  }
#endif
  if (value != 0u) {
    for (int i = 0; value != 0u || i == 0; i++, value /= base) {
      drw_print_append_digit(value % base, digits[digit / 4u]);
      digit++;
    }
    /* Point separator. */
    drw_print_append_char('.', digits[digit / 4u]);
    digit++;
  }
  /* Integer part. */
  value = uint(abs(int_part));
  for (int i = 0; value != 0u || i == 0; i++, value /= base) {
    drw_print_append_digit(value % base, digits[digit / 4u]);
    digit++;
  }
  /* Negative sign. */
  if (val < 0.0) {
    drw_print_append_char('-', digits[digit / 4u]);
    digit++;
  }
  /* Need to pad to uint alignment because we are issuing chars in "reverse". */
  for (uint i = digit % 4u; i < 4u && i > 0u; i++) {
    drw_print_append_space(digits[digit / 4u]);
    digit++;
  }
  /* Numbers are written from right to left. So we need to reverse the order. */
  for (int j = 2; j >= 0; j--) {
    drw_print_char4(digits[j]);
  }
}

void drw_print_value(vec2 value)
{
  drw_print_no_endl("vec2(", value[0], ", ", value[1], ")");
}

void drw_print_value(vec3 value)
{
  drw_print_no_endl("vec3(", value[0], ", ", value[1], ", ", value[1], ")");
}

void drw_print_value(vec4 value)
{
  drw_print_no_endl("vec4(", value[0], ", ", value[1], ", ", value[2], ", ", value[3], ")");
}

void drw_print_value(ivec2 value)
{
  drw_print_no_endl("ivec2(", value[0], ", ", value[1], ")");
}

void drw_print_value(ivec3 value)
{
  drw_print_no_endl("ivec3(", value[0], ", ", value[1], ", ", value[1], ")");
}

void drw_print_value(ivec4 value)
{
  drw_print_no_endl("ivec4(", value[0], ", ", value[1], ", ", value[2], ", ", value[3], ")");
}

void drw_print_value(uvec2 value)
{
  drw_print_no_endl("uvec2(", value[0], ", ", value[1], ")");
}

void drw_print_value(uvec3 value)
{
  drw_print_no_endl("uvec3(", value[0], ", ", value[1], ", ", value[1], ")");
}

void drw_print_value(uvec4 value)
{
  drw_print_no_endl("uvec4(", value[0], ", ", value[1], ", ", value[2], ", ", value[3], ")");
}

void drw_print_value(bvec2 value)
{
  drw_print_no_endl("bvec2(", value[0], ", ", value[1], ")");
}

void drw_print_value(bvec3 value)
{
  drw_print_no_endl("bvec3(", value[0], ", ", value[1], ", ", value[1], ")");
}

void drw_print_value(bvec4 value)
{
  drw_print_no_endl("bvec4(", value[0], ", ", value[1], ", ", value[2], ", ", value[3], ")");
}