Blender V5.3
text_format_glsl.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <cstring>
13
14#include "BLI_string_utf8.hh"
15
16#include "DNA_space_types.h"
17#include "DNA_text_types.h"
18
19#include "BKE_text.h"
20
21#include "text_format.hh"
22
23namespace blender {
24
25/* -------------------------------------------------------------------- */
28
34 /* Force single column, sorted list. */
35 /* clang-format off */
36 "EmitStreamVertex",
37 "EmitVertex",
38 "EndPrimitive",
39 "EndStreamPrimitive",
40 "abs",
41 "acos",
42 "acosh",
43 "all",
44 "any",
45 "asin",
46 "asinh",
47 "atan",
48 "atanh",
49 "atomicAdd",
50 "atomicAnd",
51 "atomicCompSwap",
52 "atomicCounter",
53 "atomicCounterDecrement",
54 "atomicCounterIncrement",
55 "atomicExchange",
56 "atomicMax",
57 "atomicMin",
58 "atomicOr",
59 "atomicXor",
60 "barrier",
61 "bitCount",
62 "bitfieldExtract",
63 "bitfieldInsert",
64 "bitfieldReverse",
65 "bool",
66 "break",
67 "bvec2",
68 "bvec3",
69 "bvec4",
70 "case",
71 "ceil",
72 "clamp",
73 "continue",
74 "cos",
75 "cosh",
76 "cross",
77 "dFdx",
78 "dFdxCoarse",
79 "dFdxFine",
80 "dFdy",
81 "dFdyCoarse",
82 "dFdyFine",
83 "degrees",
84 "determinant",
85 "discard",
86 "distance",
87 "dmat2",
88 "dmat2x2",
89 "dmat2x3",
90 "dmat2x4",
91 "dmat3",
92 "dmat3x2",
93 "dmat3x3",
94 "dmat3x4",
95 "dmat4",
96 "dmat4x2",
97 "dmat4x3",
98 "dmat4x4",
99 "do",
100 "dot",
101 "double",
102 "else",
103 "equal",
104 "exp",
105 "exp2",
106 "faceforward",
107 "findLSB",
108 "findMSB",
109 "float",
110 "floatBitsToInt",
111 "floatBitsToUint",
112 "floor",
113 "fma",
114 "for",
115 "fract",
116 "frexp",
117 "fwidth",
118 "greaterThan",
119 "greaterThanEqual",
120 "groupMemoryBarrier",
121 "if",
122 "imageAtomicAdd",
123 "imageAtomicAnd",
124 "imageAtomicCompSwap",
125 "imageAtomicExchange",
126 "imageAtomicMax",
127 "imageAtomicMin",
128 "imageAtomicOr",
129 "imageAtomicXor",
130 "imageLoad",
131 "imageSamples",
132 "imageSize",
133 "imageStore",
134 "int",
135 "intBitsToFloat",
136 "interpolateAtCentriod",
137 "interpolateAtOffset",
138 "interpolateAtSample",
139 "inverse",
140 "inversesqrt",
141 "isinf",
142 "isnan",
143 "ivec2",
144 "ivec3",
145 "ivec4",
146 "ldexp",
147 "length",
148 "lessThan",
149 "lessThanEqual",
150 "log",
151 "log2",
152 "mat2",
153 "mat2x2",
154 "mat2x3",
155 "mat2x4",
156 "mat3",
157 "mat3x2",
158 "mat3x3",
159 "mat3x4",
160 "mat4",
161 "mat4x2",
162 "mat4x3",
163 "mat4x4",
164 "matrixCompMult",
165 "max",
166 "memoryBarrier",
167 "memoryBarrierAtomicCounter",
168 "memoryBarrierBuffer",
169 "memoryBarrierImage",
170 "memoryBarrierShared",
171 "min",
172 "mix",
173 "mod",
174 "modf",
175 "noise",
176 "normalize",
177 "not",
178 "notEqual",
179 "outerProduct",
180 "packDouble2x32",
181 "packHalf2x16",
182 "packUnorm",
183 "pow",
184 "radians",
185 "reflect",
186 "refract",
187 "return",
188 "round",
189 "roundEven",
190 "sampler1D",
191 "sampler1DArray",
192 "sampler1DArrayShadow",
193 "sampler1DShadow",
194 "sampler2D",
195 "sampler2DArray",
196 "sampler2DArrayShadow",
197 "sampler2DMS",
198 "sampler2DMSArray",
199 "sampler2DRect",
200 "sampler2DShadow",
201 "sampler3D",
202 "samplerBuffer",
203 "samplerCube",
204 "samplerCubeArray",
205 "samplerCubeArrayShadow",
206 "samplerCubeShadow",
207 "sign",
208 "sin",
209 "sinh",
210 "smoothstep",
211 "sqrt",
212 "step",
213 "struct",
214 "switch",
215 "tan",
216 "tanh",
217 "texelFetch",
218 "texelFetchOffset",
219 "texture",
220 "textureGather",
221 "textureGatherOffset",
222 "textureGatherOffsets",
223 "textureGrad",
224 "textureGradOffset",
225 "textureLod",
226 "textureLodOffset",
227 "textureOffset",
228 "textureProj",
229 "textureProjGrad",
230 "textureProjGradOffset",
231 "textureProjLod",
232 "textureProjLodOffset",
233 "textureProjOffset",
234 "textureQueryLevels",
235 "textureQueryLod",
236 "textureSamples",
237 "textureSize",
238 "transpose",
239 "trunc",
240 "uaddCarry",
241 "uint",
242 "uintBitsToFloat",
243 "umulExtended",
244 "unpackDouble2x32",
245 "unpackHalf2x16",
246 "unpackUnorm2x16",
247 "unpackUnorm4x8",
248 "usubBorrow",
249 "uvec2",
250 "uvec3",
251 "uvec4",
252 "vec2",
253 "vec3",
254 "vec4",
255 "void",
256 "while",
257 /* clang-format on */
258};
262
268 /* Force single column, sorted list. */
269 /* clang-format off */
270 "buffer",
271 "coherent",
272 "default",
273 "false",
274 "flat",
275 "in",
276 "inout",
277 "layout",
278 "out",
279 "readonly",
280 "restrict",
281 "sampler",
282 "smooth",
283 "true",
284 "uniform",
285 "varying",
286 "volatile",
287 "writeonly",
288 /* clang-format on */
289};
292
298 /* Force single column, sorted list. */
299 /* clang-format off */
300 "gl_ClipDistance",
301 "gl_FragCoord",
302 "gl_FragDepth",
303 "gl_FrontFacing",
304 "gl_GlobalInvocationID",
305 "gl_InstanceID",
306 "gl_InvocationID",
307 "gl_Layer",
308 "gl_LocalInvocationID",
309 "gl_LocalInvocationIndex",
310 "gl_NumSamples",
311 "gl_NumWorkGroups",
312 "gl_PatchVerticesIn",
313 "gl_PointCoord",
314 "gl_PointSize",
315 "gl_Position",
316 "gl_PrimitiveID",
317 "gl_PrimitiveIDIn",
318 "gl_SampleID",
319 "gl_SampleMask",
320 "gl_SampleMaskIn",
321 "gl_SamplePosition",
322 "gl_TessCoord",
323 "gl_TessLevelInner",
324 "gl_TessLevelOuter",
325 "gl_VertexID",
326 "gl_ViewportIndex",
327 "gl_WorkGroupID",
328 "gl_WorkGroupSize",
329 /* clang-format on */
330};
334
336
337/* -------------------------------------------------------------------- */
340
341static int txtfmt_glsl_find_builtinfunc(const char *string)
342{
344
345 if (i == 0 || text_check_identifier(string[i])) {
346 return -1;
347 }
348 return i;
349}
350
351static int txtfmt_glsl_find_reserved(const char *string)
352{
354
355 if (i == 0 || text_check_identifier(string[i])) {
356 return -1;
357 }
358 return i;
359}
360static int txtfmt_glsl_find_specialvar(const char *string)
361{
363
364 if (i == 0 || text_check_identifier(string[i])) {
365 return -1;
366 }
367 return i;
368}
369static int txtfmt_glsl_find_preprocessor(const char *string)
370{
371 if (string[0] == '#') {
372 int i = 1;
373 /* White-space is ok `# foo`. */
374 while (text_check_whitespace(string[i])) {
375 i++;
376 }
377 while (text_check_identifier(string[i])) {
378 i++;
379 }
380 return i;
381 }
382 return -1;
383}
384static char txtfmt_glsl_format_identifier(const char *str)
385{
386 char fmt;
387
388 /* clang-format off */
389
391 } else if (txtfmt_glsl_find_builtinfunc(str) != -1) {fmt = FMT_TYPE_KEYWORD;
392 } else if (txtfmt_glsl_find_reserved(str) != -1) {fmt = FMT_TYPE_RESERVED;
393 } else if (txtfmt_glsl_find_preprocessor(str) != -1) {fmt = FMT_TYPE_DIRECTIVE;
394 } else {fmt = FMT_TYPE_DEFAULT;
395 }
396
397 /* clang-format on */
398
399 return fmt;
400}
401
403
404/* -------------------------------------------------------------------- */
407
408static void txtfmt_glsl_format_line(SpaceText *st, TextLine *line, const bool do_next)
409{
410 FlattenString fs;
411 const char *str;
412 char *fmt;
413 char cont_orig, cont, find, prev = ' ';
414 int len, i;
415
416 /* Get continuation from previous line. */
417 if (line->prev && line->prev->format != nullptr) {
418 fmt = line->prev->format;
419 cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator. */
420 BLI_assert((FMT_CONT_ALL & cont) == cont);
421 }
422 else {
423 cont = FMT_CONT_NOP;
424 }
425
426 /* Get original continuation from this line. */
427 if (line->format != nullptr) {
428 fmt = line->format;
429 cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator. */
430 BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig);
431 }
432 else {
433 cont_orig = 0xFF;
434 }
435
436 len = flatten_string(st, &fs, line->line);
437 str = fs.buf;
438 if (!text_check_format_len(line, len)) {
440 return;
441 }
442 fmt = line->format;
443
444 while (*str) {
445 /* Handle escape sequences by skipping both \ and next char. */
446 if (*str == '\\') {
447 *fmt = prev;
448 fmt++;
449 str++;
450 if (*str == '\0') {
451 break;
452 }
453 *fmt = prev;
454 fmt++;
456 continue;
457 }
458 /* Handle continuations. */
459 if (cont) {
460 /* C-Style comments. */
461 if (cont & FMT_CONT_COMMENT_C) {
462 if (*str == '*' && *(str + 1) == '/') {
463 *fmt = FMT_TYPE_COMMENT;
464 fmt++;
465 str++;
466 *fmt = FMT_TYPE_COMMENT;
467 cont = FMT_CONT_NOP;
468 }
469 else {
470 *fmt = FMT_TYPE_COMMENT;
471 }
472 /* Handle other comments. */
473 }
474 else {
475 find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\'';
476 if (*str == find) {
477 cont = 0;
478 }
479 *fmt = FMT_TYPE_STRING;
480 }
481
483 }
484 /* Not in a string... */
485 else {
486 /* Deal with comments first. */
487 if (*str == '/' && *(str + 1) == '/') {
488 /* Fill the remaining line. */
489 text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - int(fmt - line->format));
490 }
491 /* C-Style (multi-line) comments. */
492 else if (*str == '/' && *(str + 1) == '*') {
493 cont = FMT_CONT_COMMENT_C;
494 *fmt = FMT_TYPE_COMMENT;
495 fmt++;
496 str++;
497 *fmt = FMT_TYPE_COMMENT;
498 }
499 else if (ELEM(*str, '"', '\'')) {
500 /* Strings. */
501 find = *str;
502 cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE;
503 *fmt = FMT_TYPE_STRING;
504 }
505 /* White-space (all white-space has been converted to spaces). */
506 else if (*str == ' ') {
507 *fmt = FMT_TYPE_WHITESPACE;
508 }
509 /* Numbers (digits not part of an identifier and periods followed by digits). */
510 else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) ||
511 (*str == '.' && text_check_digit(*(str + 1))))
512 {
513 *fmt = FMT_TYPE_NUMERAL;
514 }
515 /* Punctuation. */
516 else if ((*str != '#') && text_check_delim(*str)) {
517 *fmt = FMT_TYPE_SYMBOL;
518 }
519 /* Identifiers and other text (no previous white-space or delimiters. so text continues). */
520 else if (prev == FMT_TYPE_DEFAULT) {
522 *fmt = FMT_TYPE_DEFAULT;
523 }
524 /* Not white-space, a digit, punctuation, or continuing text.
525 * Must be new, check for special words. */
526 else {
527 /* Keep aligned arguments for readability. */
528 /* clang-format off */
529
530 /* Special vars(v) or built-in keywords(b). */
531 /* keep in sync with `txtfmt_glsl_format_identifier()`. */
532 if ((i = txtfmt_glsl_find_specialvar(str)) != -1) { prev = FMT_TYPE_SPECIAL;
533 } else if ((i = txtfmt_glsl_find_builtinfunc(str)) != -1) { prev = FMT_TYPE_KEYWORD;
534 } else if ((i = txtfmt_glsl_find_reserved(str)) != -1) { prev = FMT_TYPE_RESERVED;
535 } else if ((i = txtfmt_glsl_find_preprocessor(str)) != -1) { prev = FMT_TYPE_DIRECTIVE;
536 }
537 /* clang-format on */
538
539 if (i > 0) {
540 if (prev == FMT_TYPE_DIRECTIVE) { /* May contain UTF8. */
541 text_format_fill(&str, &fmt, prev, i);
542 }
543 else {
544 text_format_fill_ascii(&str, &fmt, prev, i);
545 }
546 }
547 else {
549 *fmt = FMT_TYPE_DEFAULT;
550 }
551 }
552 }
553 prev = *fmt;
554 fmt++;
555 str++;
556 }
557
558 /* Terminate and add continuation char. */
559 *fmt = '\0';
560 fmt++;
561 *fmt = cont;
562
563 /* If continuation has changed and we're allowed, process the next line. */
564 if (cont != cont_orig && do_next && line->next) {
565 txtfmt_glsl_format_line(st, line->next, do_next);
566 }
567
569}
570
572
573/* -------------------------------------------------------------------- */
576
594
596
597} // namespace blender
#define ELEM(...)
#define str(s)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
bool text_check_identifier(char ch)
Definition text.cc:2323
bool text_check_digit(char ch)
Definition text.cc:2312
static int txtfmt_glsl_find_builtinfunc(const char *string)
int text_check_format_len(TextLine *line, uint len)
bool text_format_string_literals_check_sorted_array(const Span< const char * > string_literals)
static const char * text_format_glsl_literals_reserved_data[]
static void txtfmt_glsl_format_line(SpaceText *st, TextLine *line, const bool do_next)
static const Span< const char * > text_format_glsl_literals_specialvar(text_format_glsl_literals_specialvar_data, ARRAY_SIZE(text_format_glsl_literals_specialvar_data))
int text_format_string_literal_find(const Span< const char * > string_literals, const char *text)
bool text_check_delim(char ch)
Definition text.cc:2296
static const char * text_format_glsl_literals_specialvar_data[]
void flatten_string_free(FlattenString *fs)
static const Span< const char * > text_format_glsl_literals_builtinfunc(text_format_glsl_literals_builtinfunc_data, ARRAY_SIZE(text_format_glsl_literals_builtinfunc_data))
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool text_check_whitespace(char ch)
Definition text.cc:2378
@ FMT_CONT_COMMENT_C
@ FMT_CONT_QUOTEDOUBLE
@ FMT_CONT_QUOTESINGLE
static char txtfmt_glsl_format_identifier(const char *str)
static const Span< const char * > text_format_glsl_literals_reserved(text_format_glsl_literals_reserved_data, ARRAY_SIZE(text_format_glsl_literals_reserved_data))
static int txtfmt_glsl_find_specialvar(const char *string)
static const char * text_format_glsl_literals_builtinfunc_data[]
static int txtfmt_glsl_find_preprocessor(const char *string)
void ED_text_format_register(TextFormatType *tft)
void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
void ED_text_format_register_glsl()
@ FMT_TYPE_KEYWORD
@ FMT_TYPE_RESERVED
@ FMT_TYPE_WHITESPACE
@ FMT_TYPE_COMMENT
@ FMT_TYPE_SPECIAL
@ FMT_TYPE_DIRECTIVE
@ FMT_TYPE_STRING
@ FMT_TYPE_NUMERAL
@ FMT_TYPE_SYMBOL
@ FMT_TYPE_DEFAULT
static int txtfmt_glsl_find_reserved(const char *string)
void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
const char * comment_line
char(*) format_identifier(const char *string)
void(*) format_line(SpaceText *st, TextLine *line, bool do_next)
struct TextLine * prev
struct TextLine * next
#define FMT_CONT_ALL