Debian Codegraph @

mojoshader_common.c

raw symbols SCIP
1 #define __MOJOSHADER_INTERNAL__ 1
2 #include "mojoshader_internal.h"
3 #ifndef MOJOSHADER_USE_SDL_STDLIB
4 #include <math.h>
5 #endif /* MOJOSHADER_USE_SDL_STDLIB */
6
7 // Convenience functions for allocators...
8 #if !MOJOSHADER_FORCE_ALLOCATOR
9 static char zeromalloc = 0;
10 void * MOJOSHADERCALL MOJOSHADER_internal_malloc(int bytes, void *d)
11 {
12 return (bytes == 0) ? &zeromalloc : malloc(bytes);
13 } // MOJOSHADER_internal_malloc
14 void MOJOSHADERCALL MOJOSHADER_internal_free(void *ptr, void *d)
15 {
16 if ((ptr != &zeromalloc) && (ptr != NULL))
17 free(ptr);
18 } // MOJOSHADER_internal_free
19 #endif
20
21 MOJOSHADER_error MOJOSHADER_out_of_mem_error = {
22 "Out of memory", NULL, MOJOSHADER_POSITION_NONE
23 };
24
25 MOJOSHADER_parseData MOJOSHADER_out_of_mem_data = {
26 1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0,
27 MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0, 0, 0, 0,
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
29 };
30
31
32 typedef struct HashItem
33 {
34 const void *key;
35 const void *value;
36 struct HashItem *next;
37 } HashItem;
38
39 struct HashTable
40 {
41 HashItem **table;
42 uint32 table_len;
43 int stackable;
44 void *data;
45 HashTable_HashFn hash;
46 HashTable_KeyMatchFn keymatch;
47 HashTable_NukeFn nuke;
48 MOJOSHADER_malloc m;
49 MOJOSHADER_free f;
50 void *d;
51 };
52
53 static inline uint32 calc_hash(const HashTable *table, const void *key)
54 {
55 return table->hash(key, table->data) & (table->table_len-1);
56 } // calc_hash
57
58 int hash_find(const HashTable *table, const void *key, const void **_value)
59 {
60 HashItem *i;
61 void *data = table->data;
62 const uint32 hash = calc_hash(table, key);
63 HashItem *prev = NULL;
64 for (i = table->table[hash]; i != NULL; i = i->next)
65 {
66 if (table->keymatch(key, i->key, data))
67 {
68 if (_value != NULL)
69 *_value = i->value;
70
71 // Matched! Move to the front of list for faster lookup next time.
72 // (stackable tables have to remain in the same order, though!)
73 if ((!table->stackable) && (prev != NULL))
74 {
75 assert(prev->next == i);
76 prev->next = i->next;
77 i->next = table->table[hash];
78 table->table[hash] = i;
79 } // if
80
81 return 1;
82 } // if
83
84 prev = i;
85 } // for
86
87 return 0;
88 } // hash_find
89
90 int hash_iter(const HashTable *table, const void *key,
91 const void **_value, void **iter)
92 {
93 HashItem *item = (HashItem *) *iter;
94 if (item == NULL)
95 item = table->table[calc_hash(table, key)];
96 else
97 item = item->next;
98
99 while (item != NULL)
100 {
101 if (table->keymatch(key, item->key, table->data))
102 {
103 *_value = item->value;
104 *iter = item;
105 return 1;
106 } // if
107 item = item->next;
108 } // while
109
110 // no more matches.
111 *_value = NULL;
112 *iter = NULL;
113 return 0;
114 } // hash_iter
115
116 int hash_iter_keys(const HashTable *table, const void **_key, void **iter)
117 {
118 HashItem *item = (HashItem *) *iter;
119 uint32 idx = 0;
120
121 if (item != NULL)
122 {
123 const HashItem *orig = item;
124 item = item->next;
125 if (item == NULL)
126 idx = calc_hash(table, orig->key) + 1;
127 } // if
128
129 while (!item && (idx < table->table_len))
130 item = table->table[idx++]; // skip empty buckets...
131
132 if (item == NULL) // no more matches?
133 {
134 *_key = NULL;
135 *iter = NULL;
136 return 0;
137 } // if
138
139 *_key = item->key;
140 *iter = item;
141 return 1;
142 } // hash_iter_keys
143
144 int hash_insert(HashTable *table, const void *key, const void *value)
145 {
146 HashItem *item = NULL;
147 const uint32 hash = calc_hash(table, key);
148 if ( (!table->stackable) && (hash_find(table, key, NULL)) )
149 return 0;
150
151 // !!! FIXME: grow and rehash table if it gets too saturated.
152 item = (HashItem *) table->m(sizeof (HashItem), table->d);
153 if (item == NULL)
154 return -1;
155
156 item->key = key;
157 item->value = value;
158 item->next = table->table[hash];
159 table->table[hash] = item;
160
161 return 1;
162 } // hash_insert
163
164 HashTable *hash_create(void *data, const HashTable_HashFn hashfn,
165 const HashTable_KeyMatchFn keymatchfn,
166 const HashTable_NukeFn nukefn,
167 const int stackable,
168 MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
169 {
170 const uint32 initial_table_size = 256;
171 const uint32 alloc_len = sizeof (HashItem *) * initial_table_size;
172 HashTable *table = (HashTable *) m(sizeof (HashTable), d);
173 if (table == NULL)
174 return NULL;
175 memset(table, '\0', sizeof (HashTable));
176
177 table->table = (HashItem **) m(alloc_len, d);
178 if (table->table == NULL)
179 {
180 f(table, d);
181 return NULL;
182 } // if
183
184 memset(table->table, '\0', alloc_len);
185 table->table_len = initial_table_size;
186 table->stackable = stackable;
187 table->data = data;
188 table->hash = hashfn;
189 table->keymatch = keymatchfn;
190 table->nuke = nukefn;
191 table->m = m;
192 table->f = f;
193 table->d = d;
194 return table;
195 } // hash_create
196
197 void hash_destroy(HashTable *table, const void *ctx)
198 {
199 uint32 i;
200 void *data = table->data;
201 MOJOSHADER_free f = table->f;
202 void *d = table->d;
203 for (i = 0; i < table->table_len; i++)
204 {
205 HashItem *item = table->table[i];
206 while (item != NULL)
207 {
208 HashItem *next = item->next;
209 table->nuke(ctx, item->key, item->value, data);
210 f(item, d);
211 item = next;
212 } // while
213 } // for
214
215 f(table->table, d);
216 f(table, d);
217 } // hash_destroy
218
219 int hash_remove(HashTable *table, const void *key, const void *ctx)
220 {
221 HashItem *item = NULL;
222 HashItem *prev = NULL;
223 void *data = table->data;
224 const uint32 hash = calc_hash(table, key);
225 for (item = table->table[hash]; item != NULL; item = item->next)
226 {
227 if (table->keymatch(key, item->key, data))
228 {
229 if (prev != NULL)
230 prev->next = item->next;
231 else
232 table->table[hash] = item->next;
233
234 table->nuke(ctx, item->key, item->value, data);
235 table->f(item, table->d);
236 return 1;
237 } // if
238
239 prev = item;
240 } // for
241
242 return 0;
243 } // hash_remove
244
245
246 // this is djb's xor hashing function.
247 static inline uint32 hash_string_djbxor(const char *str, size_t len)
248 {
249 register uint32 hash = 5381;
250 while (len--)
251 hash = ((hash << 5) + hash) ^ *(str++);
252 return hash;
253 } // hash_string_djbxor
254
255 static inline uint32 hash_string(const char *str, size_t len)
256 {
257 return hash_string_djbxor(str, len);
258 } // hash_string
259
260 uint32 hash_hash_string(const void *sym, void *data)
261 {
262 (void) data;
263 return hash_string((const char*) sym, strlen((const char *) sym));
264 } // hash_hash_string
265
266 int hash_keymatch_string(const void *a, const void *b, void *data)
267 {
268 (void) data;
269 return (strcmp((const char *) a, (const char *) b) == 0);
270 } // hash_keymatch_string
271
272
273 // string -> string map...
274
275 static void stringmap_nuke_noop(const void *ctx, const void *key, const void *val, void *d) {}
276
277 static void stringmap_nuke(const void *ctx, const void *key, const void *val, void *d)
278 {
279 StringMap *smap = (StringMap *) d;
280 smap->f((void *) key, smap->d);
281 smap->f((void *) val, smap->d);
282 } // stringmap_nuke
283
284 StringMap *stringmap_create(const int copy, MOJOSHADER_malloc m,
285 MOJOSHADER_free f, void *d)
286 {
287 HashTable_NukeFn nuke = copy ? stringmap_nuke : stringmap_nuke_noop;
288 StringMap *smap;
289 smap = hash_create(0,hash_hash_string,hash_keymatch_string,nuke,0,m,f,d);
290 if (smap != NULL)
291 smap->data = smap;
292 return smap;
293 } // stringmap_create
294
295 void stringmap_destroy(StringMap *smap)
296 {
297 hash_destroy(smap, NULL);
298 } // stringmap_destroy
299
300 int stringmap_insert(StringMap *smap, const char *key, const char *value)
301 {
302 assert(key != NULL);
303 if (smap->nuke == stringmap_nuke_noop) // no copy?
304 return hash_insert(smap, key, value);
305
306 int rc = -1;
307 char *k = (char *) smap->m(strlen(key) + 1, smap->d);
308 char *v = (char *) (value ? smap->m(strlen(value) + 1, smap->d) : NULL);
309 int failed = ( (!k) || ((!v) && (value)) );
310
311 if (!failed)
312 {
313 strcpy(k, key);
314 if (value != NULL)
315 strcpy(v, value);
316 failed = ((rc = hash_insert(smap, k, v)) <= 0);
317 } // if
318
319 if (failed)
320 {
321 smap->f(k, smap->d);
322 smap->f(v, smap->d);
323 } // if
324
325 return rc;
326 } // stringmap_insert
327
328 int stringmap_remove(StringMap *smap, const char *key)
329 {
330 return hash_remove(smap, key, NULL);
331 } // stringmap_remove
332
333 int stringmap_find(const StringMap *smap, const char *key, const char **_value)
334 {
335 const void *value = NULL;
336 const int retval = hash_find(smap, key, &value);
337 *_value = (const char *) value;
338 return retval;
339 } // stringmap_find
340
341
342 // The string cache... !!! FIXME: use StringMap internally for this.
343
344 typedef struct StringBucket
345 {
346 char *string;
347 struct StringBucket *next;
348 } StringBucket;
349
350 struct StringCache
351 {
352 StringBucket **hashtable;
353 uint32 table_size;
354 MOJOSHADER_malloc m;
355 MOJOSHADER_free f;
356 void *d;
357 };
358
359
360 const char *stringcache(StringCache *cache, const char *str)
361 {
362 return stringcache_len(cache, str, strlen(str));
363 } // stringcache
364
365 static const char *stringcache_len_internal(StringCache *cache,
366 const char *str,
367 const unsigned int len,
368 const int addmissing)
369 {
370 const uint8 hash = hash_string(str, len) & (cache->table_size-1);
371 StringBucket *bucket = cache->hashtable[hash];
372 StringBucket *prev = NULL;
373 while (bucket)
374 {
375 const char *bstr = bucket->string;
376 if ((strncmp(bstr, str, len) == 0) && (bstr[len] == 0))
377 {
378 // Matched! Move this to the front of the list.
379 if (prev != NULL)
380 {
381 assert(prev->next == bucket);
382 prev->next = bucket->next;
383 bucket->next = cache->hashtable[hash];
384 cache->hashtable[hash] = bucket;
385 } // if
386 return bstr; // already cached
387 } // if
388 prev = bucket;
389 bucket = bucket->next;
390 } // while
391
392 // no match!
393 if (!addmissing)
394 return NULL;
395
396 // add to the table.
397 bucket = (StringBucket *) cache->m(sizeof (StringBucket) + len + 1, cache->d);
398 if (bucket == NULL)
399 return NULL;
400 bucket->string = (char *)(bucket + 1);
401 memcpy(bucket->string, str, len);
402 bucket->string[len] = '\0';
403 bucket->next = cache->hashtable[hash];
404 cache->hashtable[hash] = bucket;
405 return bucket->string;
406 } // stringcache_len_internal
407
408 const char *stringcache_len(StringCache *cache, const char *str,
409 const unsigned int len)
410 {
411 return stringcache_len_internal(cache, str, len, 1);
412 } // stringcache_len
413
414 int stringcache_iscached(StringCache *cache, const char *str)
415 {
416 return (stringcache_len_internal(cache, str, strlen(str), 0) != NULL);
417 } // stringcache_iscached
418
419 const char *stringcache_fmt(StringCache *cache, const char *fmt, ...)
420 {
421 char buf[128]; // use the stack if reasonable.
422 char *ptr = NULL;
423 int len = 0; // number of chars, NOT counting null-terminator!
424 va_list ap;
425
426 va_start(ap, fmt);
427 len = vsnprintf(buf, sizeof (buf), fmt, ap);
428 va_end(ap);
429
430 if (len > sizeof (buf))
431 {
432 ptr = (char *) cache->m(len, cache->d);
433 if (ptr == NULL)
434 return NULL;
435
436 va_start(ap, fmt);
437 vsnprintf(ptr, len, fmt, ap);
438 va_end(ap);
439 } // if
440
441 const char *retval = stringcache_len(cache, ptr ? ptr : buf, len);
442 if (ptr != NULL)
443 cache->f(ptr, cache->d);
444
445 return retval;
446 } // stringcache_fmt
447
448 StringCache *stringcache_create(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
449 {
450 const uint32 initial_table_size = 256;
451 const size_t tablelen = sizeof (StringBucket *) * initial_table_size;
452 StringCache *cache = (StringCache *) m(sizeof (StringCache), d);
453 if (!cache)
454 return NULL;
455 memset(cache, '\0', sizeof (StringCache));
456
457 cache->hashtable = (StringBucket **) m(tablelen, d);
458 if (!cache->hashtable)
459 {
460 f(cache, d);
461 return NULL;
462 } // if
463 memset(cache->hashtable, '\0', tablelen);
464
465 cache->table_size = initial_table_size;
466 cache->m = m;
467 cache->f = f;
468 cache->d = d;
469 return cache;
470 } // stringcache_create
471
472 void stringcache_destroy(StringCache *cache)
473 {
474 if (cache == NULL)
475 return;
476
477 MOJOSHADER_free f = cache->f;
478 void *d = cache->d;
479 size_t i;
480
481 for (i = 0; i < cache->table_size; i++)
482 {
483 StringBucket *bucket = cache->hashtable[i];
484 cache->hashtable[i] = NULL;
485 while (bucket)
486 {
487 StringBucket *next = bucket->next;
488 f(bucket, d);
489 bucket = next;
490 } // while
491 } // for
492
493 f(cache->hashtable, d);
494 f(cache, d);
495 } // stringcache_destroy
496
497
498 // We chain errors as a linked list with a head/tail for easy appending.
499 // These get flattened before passing to the application.
500 typedef struct ErrorItem
501 {
502 MOJOSHADER_error error;
503 struct ErrorItem *next;
504 } ErrorItem;
505
506 struct ErrorList
507 {
508 ErrorItem head;
509 ErrorItem *tail;
510 int count;
511 MOJOSHADER_malloc m;
512 MOJOSHADER_free f;
513 void *d;
514 };
515
516 ErrorList *errorlist_create(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
517 {
518 ErrorList *retval = (ErrorList *) m(sizeof (ErrorList), d);
519 if (retval != NULL)
520 {
521 memset(retval, '\0', sizeof (ErrorList));
522 retval->tail = &retval->head;
523 retval->m = m;
524 retval->f = f;
525 retval->d = d;
526 } // if
527
528 return retval;
529 } // errorlist_create
530
531
532 int errorlist_add(ErrorList *list, const char *fname,
533 const int errpos, const char *str)
534 {
535 return errorlist_add_fmt(list, fname, errpos, "%s", str);
536 } // errorlist_add
537
538
539 int errorlist_add_fmt(ErrorList *list, const char *fname,
540 const int errpos, const char *fmt, ...)
541 {
542 va_list ap;
543 va_start(ap, fmt);
544 const int retval = errorlist_add_va(list, fname, errpos, fmt, ap);
545 va_end(ap);
546 return retval;
547 } // errorlist_add_fmt
548
549
550 int errorlist_add_va(ErrorList *list, const char *_fname,
551 const int errpos, const char *fmt, va_list va)
552 {
553 ErrorItem *error = (ErrorItem *) list->m(sizeof (ErrorItem), list->d);
554 if (error == NULL)
555 return 0;
556
557 char *fname = NULL;
558 if (_fname != NULL)
559 {
560 fname = (char *) list->m(strlen(_fname) + 1, list->d);
561 if (fname == NULL)
562 {
563 list->f(error, list->d);
564 return 0;
565 } // if
566 strcpy(fname, _fname);
567 } // if
568
569 char scratch[128];
570 va_list ap;
571 va_copy(ap, va);
572 int len = vsnprintf(scratch, sizeof (scratch), fmt, ap);
573 va_end(ap);
574
575 // on some versions of the windows C runtime, vsnprintf() returns -1
576 // if the buffer overflows instead of the length the string would have
577 // been as expected.
578 // In this case we make another copy of va and fetch the length only
579 // with another call to _vscprintf
580
581 #if defined(_WIN32) && !defined(MOJOSHADER_USE_SDL_STDLIB)
582 if (len == -1)
583 {
584 va_copy(ap, va);
585 len = _vscprintf(fmt, ap);
586 va_end(ap);
587 }
588 #endif
589
590 char *failstr = (char *) list->m(len + 1, list->d);
591 if (failstr == NULL)
592 {
593 list->f(error, list->d);
594 list->f(fname, list->d);
595 return 0;
596 } // if
597
598 // If we overflowed our scratch buffer, that's okay. We were going to
599 // allocate anyhow...the scratch buffer just lets us avoid a second
600 // run of vsnprintf().
601 if (len < sizeof (scratch))
602 strcpy(failstr, scratch); // copy it over.
603 else
604 {
605 va_copy(ap, va);
606 vsnprintf(failstr, len + 1, fmt, ap); // rebuild it.
607 va_end(ap);
608 } // else
609
610 error->error.error = failstr;
611 error->error.filename = fname;
612 error->error.error_position = errpos;
613 error->next = NULL;
614
615 list->tail->next = error;
616 list->tail = error;
617
618 list->count++;
619 return 1;
620 } // errorlist_add_va
621
622
623 int errorlist_count(ErrorList *list)
624 {
625 return list->count;
626 } // errorlist_count
627
628
629 MOJOSHADER_error *errorlist_flatten(ErrorList *list)
630 {
631 if (list->count == 0)
632 return NULL;
633
634 int total = 0;
635 MOJOSHADER_error *retval = (MOJOSHADER_error *)
636 list->m(sizeof (MOJOSHADER_error) * list->count, list->d);
637 if (retval == NULL)
638 return NULL;
639
640 ErrorItem *item = list->head.next;
641 while (item != NULL)
642 {
643 ErrorItem *next = item->next;
644 // reuse the string allocations
645 memcpy(&retval[total], &item->error, sizeof (MOJOSHADER_error));
646 list->f(item, list->d);
647 item = next;
648 total++;
649 } // while
650
651 assert(total == list->count);
652 list->count = 0;
653 list->head.next = NULL;
654 list->tail = &list->head;
655 return retval;
656 } // errorlist_flatten
657
658
659 void errorlist_destroy(ErrorList *list)
660 {
661 if (list == NULL)
662 return;
663
664 MOJOSHADER_free f = list->f;
665 void *d = list->d;
666 ErrorItem *item = list->head.next;
667 while (item != NULL)
668 {
669 ErrorItem *next = item->next;
670 f((void *) item->error.error, d);
671 f((void *) item->error.filename, d);
672 f(item, d);
673 item = next;
674 } // while
675 f(list, d);
676 } // errorlist_destroy
677
678
679 Buffer *buffer_create(size_t blksz, MOJOSHADER_malloc m,
680 MOJOSHADER_free f, void *d)
681 {
682 Buffer *buffer = (Buffer *) m(sizeof (Buffer), d);
683 if (buffer != NULL)
684 {
685 memset(buffer, '\0', sizeof (Buffer));
686 buffer->block_size = blksz;
687 buffer->m = m;
688 buffer->f = f;
689 buffer->d = d;
690 } // if
691 return buffer;
692 } // buffer_create
693
694 char *buffer_reserve(Buffer *buffer, const size_t len)
695 {
696 // note that we make the blocks bigger than blocksize when we have enough
697 // data to overfill a fresh block, to reduce allocations.
698 const size_t blocksize = buffer->block_size;
699
700 if (len == 0)
701 return NULL;
702
703 if (buffer->tail != NULL)
704 {
705 const size_t tailbytes = buffer->tail->bytes;
706 const size_t avail = (tailbytes >= blocksize) ? 0 : blocksize - tailbytes;
707 if (len <= avail)
708 {
709 buffer->tail->bytes += len;
710 buffer->total_bytes += len;
711 assert(buffer->tail->bytes <= blocksize);
712 return (char *) buffer->tail->data + tailbytes;
713 } // if
714 } // if
715
716 // need to allocate a new block (even if a previous block wasn't filled,
717 // so this buffer is contiguous).
718 const size_t bytecount = len > blocksize ? len : blocksize;
719 const size_t malloc_len = sizeof (BufferBlock) + bytecount;
720 BufferBlock *item = (BufferBlock *) buffer->m(malloc_len, buffer->d);
721 if (item == NULL)
722 return NULL;
723
724 item->data = ((uint8 *) item) + sizeof (BufferBlock);
725 item->bytes = len;
726 item->next = NULL;
727 if (buffer->tail != NULL)
728 buffer->tail->next = item;
729 else
730 buffer->head = item;
731 buffer->tail = item;
732
733 buffer->total_bytes += len;
734
735 return (char *) item->data;
736 } // buffer_reserve
737
738 int buffer_append(Buffer *buffer, const void *_data, size_t len)
739 {
740 const uint8 *data = (const uint8 *) _data;
741
742 // note that we make the blocks bigger than blocksize when we have enough
743 // data to overfill a fresh block, to reduce allocations.
744 const size_t blocksize = buffer->block_size;
745
746 if (len == 0)
747 return 1;
748
749 if (buffer->tail != NULL)
750 {
751 const size_t tailbytes = buffer->tail->bytes;
752 const size_t avail = (tailbytes >= blocksize) ? 0 : blocksize - tailbytes;
753 const size_t cpy = (avail > len) ? len : avail;
754 if (cpy > 0)
755 {
756 memcpy(buffer->tail->data + tailbytes, data, cpy);
757 len -= cpy;
758 data += cpy;
759 buffer->tail->bytes += cpy;
760 buffer->total_bytes += cpy;
761 assert(buffer->tail->bytes <= blocksize);
762 } // if
763 } // if
764
765 if (len > 0)
766 {
767 assert((!buffer->tail) || (buffer->tail->bytes >= blocksize));
768 const size_t bytecount = len > blocksize ? len : blocksize;
769 const size_t malloc_len = sizeof (BufferBlock) + bytecount;
770 BufferBlock *item = (BufferBlock *) buffer->m(malloc_len, buffer->d);
771 if (item == NULL)
772 return 0;
773
774 item->data = ((uint8 *) item) + sizeof (BufferBlock);
775 item->bytes = len;
776 item->next = NULL;
777 if (buffer->tail != NULL)
778 buffer->tail->next = item;
779 else
780 buffer->head = item;
781 buffer->tail = item;
782
783 memcpy(item->data, data, len);
784 buffer->total_bytes += len;
785 } // if
786
787 return 1;
788 } // buffer_append
789
790 int buffer_append_fmt(Buffer *buffer, const char *fmt, ...)
791 {
792 va_list ap;
793 va_start(ap, fmt);
794 const int retval = buffer_append_va(buffer, fmt, ap);
795 va_end(ap);
796 return retval;
797 } // buffer_append_fmt
798
799 int buffer_append_va(Buffer *buffer, const char *fmt, va_list va)
800 {
801 char scratch[256];
802
803 va_list ap;
804 va_copy(ap, va);
805 const int len = vsnprintf(scratch, sizeof (scratch), fmt, ap);
806 va_end(ap);
807
808 // If we overflowed our scratch buffer, heap allocate and try again.
809
810 if (len == 0)
811 return 1; // nothing to do.
812 else if (len < sizeof (scratch))
813 return buffer_append(buffer, scratch, len);
814
815 char *buf = (char *) buffer->m(len + 1, buffer->d);
816 if (buf == NULL)
817 return 0;
818 va_copy(ap, va);
819 vsnprintf(buf, len + 1, fmt, ap); // rebuild it.
820 va_end(ap);
821 const int retval = buffer_append(buffer, buf, len);
822 buffer->f(buf, buffer->d);
823 return retval;
824 } // buffer_append_va
825
826 size_t buffer_size(Buffer *buffer)
827 {
828 return buffer->total_bytes;
829 } // buffer_size
830
831 void buffer_empty(Buffer *buffer)
832 {
833 BufferBlock *item = buffer->head;
834 while (item != NULL)
835 {
836 BufferBlock *next = item->next;
837 buffer->f(item, buffer->d);
838 item = next;
839 } // while
840 buffer->head = buffer->tail = NULL;
841 buffer->total_bytes = 0;
842 } // buffer_empty
843
844 char *buffer_flatten(Buffer *buffer)
845 {
846 char *retval = (char *) buffer->m(buffer->total_bytes + 1, buffer->d);
847 if (retval == NULL)
848 return NULL;
849 BufferBlock *item = buffer->head;
850 char *ptr = retval;
851 while (item != NULL)
852 {
853 BufferBlock *next = item->next;
854 memcpy(ptr, item->data, item->bytes);
855 ptr += item->bytes;
856 buffer->f(item, buffer->d);
857 item = next;
858 } // while
859 *ptr = '\0';
860
861 assert(ptr == (retval + buffer->total_bytes));
862
863 buffer->head = buffer->tail = NULL;
864 buffer->total_bytes = 0;
865
866 return retval;
867 } // buffer_flatten
868
869 char *buffer_merge(Buffer **buffers, const size_t n, size_t *_len)
870 {
871 Buffer *first = NULL;
872 size_t len = 0;
873 size_t i;
874 for (i = 0; i < n; i++)
875 {
876 Buffer *buffer = buffers[i];
877 if (buffer == NULL)
878 continue;
879 if (first == NULL)
880 first = buffer;
881 len += buffer->total_bytes;
882 } // for
883
884 char *retval = (char *) (first ? first->m(len + 1, first->d) : NULL);
885 if (retval == NULL)
886 {
887 *_len = 0;
888 return NULL;
889 } // if
890
891 *_len = len;
892 char *ptr = retval;
893 for (i = 0; i < n; i++)
894 {
895 Buffer *buffer = buffers[i];
896 if (buffer == NULL)
897 continue;
898 BufferBlock *item = buffer->head;
899 while (item != NULL)
900 {
901 BufferBlock *next = item->next;
902 memcpy(ptr, item->data, item->bytes);
903 ptr += item->bytes;
904 buffer->f(item, buffer->d);
905 item = next;
906 } // while
907
908 buffer->head = buffer->tail = NULL;
909 buffer->total_bytes = 0;
910 } // for
911 *ptr = '\0';
912
913 assert(ptr == (retval + len));
914
915 return retval;
916 } // buffer_merge
917
918 void buffer_destroy(Buffer *buffer)
919 {
920 if (buffer != NULL)
921 {
922 MOJOSHADER_free f = buffer->f;
923 void *d = buffer->d;
924 buffer_empty(buffer);
925 f(buffer, d);
926 } // if
927 } // buffer_destroy
928
929 void buffer_patch(Buffer *buffer, const size_t start,
930 const void *_data, const size_t len)
931 {
932 if (len == 0)
933 return; // Nothing to do.
934
935 if ((start + len) > buffer->total_bytes)
936 return; // definitely can't patch.
937
938 // Find the start point somewhere in the center of a buffer.
939 BufferBlock *item = buffer->head;
940 size_t pos = 0;
941 if (start > 0)
942 {
943 while (1)
944 {
945 assert(item != NULL);
946 if ((pos + item->bytes) > start) // start is in this block.
947 break;
948
949 pos += item->bytes;
950 item = item->next;
951 } // while
952 } // if
953
954 const uint8 *data = (const uint8 *) _data;
955 size_t write_pos = start - pos;
956 size_t write_remain = len;
957 size_t written = 0;
958 while (write_remain)
959 {
960 size_t write_end = write_pos + write_remain;
961 if (write_end > item->bytes)
962 write_end = item->bytes;
963
964 size_t to_write = write_end - write_pos;
965 memcpy(item->data + write_pos, data + written, to_write);
966 write_remain -= to_write;
967 written += to_write;
968 write_pos = 0;
969 item = item->next;
970 } // while
971 } // buffer_patch
972
973 // Based on SDL_string.c's SDL_PrintFloat function
974 size_t MOJOSHADER_printFloat(char *text, size_t maxlen, float arg)
975 {
976 size_t len;
977 size_t left = maxlen;
978 char *textstart = text;
979
980 int precision = 9;
981
982 if (isnan(arg))
983 {
984 if (left > 3)
985 {
986 snprintf(text, left, "NaN");
987 left -= 3;
988 } // if
989 text += 3;
990 } // if
991 else if (isinf(arg))
992 {
993 if (left > 3)
994 {
995 snprintf(text, left, "inf");
996 left -= 3;
997 } // if
998 text += 3;
999 } // else if
1000 else if (arg)
1001 {
1002 /* This isn't especially accurate, but hey, it's easy. :) */
1003 unsigned long value;
1004
1005 if (arg < 0)
1006 {
1007 if (left > 1)
1008 {
1009 *text = '-';
1010 --left;
1011 } // if
1012 ++text;
1013 arg = -arg;
1014 } // if
1015 value = (unsigned long) arg;
1016 len = snprintf(text, left, "%lu", value);
1017 text += len;
1018 if (len >= left)
1019 left = (left < 1) ? left : 1;
1020 else
1021 left -= len;
1022 arg -= value;
1023
1024 int mult = 10;
1025 if (left > 1)
1026 {
1027 *text = '.';
1028 --left;
1029 } // if
1030 ++text;
1031 while (precision-- > 0)
1032 {
1033 value = (unsigned long) (arg * mult);
1034 len = snprintf(text, left, "%lu", value);
1035 text += len;
1036 if (len >= left)
1037 left = (left < 1) ? left : 1;
1038 else
1039 left -= len;
1040 arg -= (double) value / mult;
1041 if (arg < 0) arg = -arg; // Sometimes that bit gets flipped...
1042 mult *= 10;
1043 } // while
1044 } // if
1045 else
1046 {
1047 if (left > 3)
1048 {
1049 snprintf(text, left, "0.0");
1050 left -= 3;
1051 } // if
1052 text += 3;
1053 } // else
1054
1055 return (text - textstart);
1056 } // MOJOSHADER_printFloat
1057
1058 #if SUPPORT_PROFILE_SPIRV
1059 #include "spirv/spirv.h"
1060 #include "spirv/GLSL.std.450.h"
1061 void MOJOSHADER_spirv_link_attributes(const MOJOSHADER_parseData *vertex,
1062 const MOJOSHADER_parseData *pixel,
1063 int is_glspirv)
1064 {
1065 int i;
1066 uint32 attr_loc = 0;
1067 uint32 vOffset, pOffset;
1068 int vDataLen = vertex->output_len - sizeof(SpirvPatchTable);
1069 int pDataLen = pixel->output_len - sizeof(SpirvPatchTable);
1070 SpirvPatchTable *vTable = (SpirvPatchTable *) &vertex->output[vDataLen];
1071 SpirvPatchTable *pTable = (SpirvPatchTable *) &pixel->output[pDataLen];
1072 const uint32 texcoord0Loc = pTable->attrib_offsets[MOJOSHADER_USAGE_TEXCOORD][0];
1073
1074 if (is_glspirv)
1075 {
1076 // We need locations for color outputs first!
1077 for (i = 0; i < pixel->output_count; i++)
1078 {
1079 const MOJOSHADER_attribute* pAttr = &pixel->outputs[i];
1080 if (pAttr->usage != MOJOSHADER_USAGE_COLOR)
1081 {
1082 // This should be FragDepth, which is builtin
1083 assert(pAttr->usage == MOJOSHADER_USAGE_DEPTH);
1084 continue;
1085 } // if
1086
1087 // Set the loc for the output declaration...
1088 pOffset = pTable->output_offsets[pAttr->index];
1089 assert(pOffset > 0);
1090 ((uint32*)pixel->output)[pOffset] = attr_loc;
1091
1092 // Set the same value for the vertex output/pixel input...
1093 pOffset = pTable->attrib_offsets[pAttr->usage][pAttr->index];
1094 if (pOffset)
1095 ((uint32*)pixel->output)[pOffset] = attr_loc;
1096 vOffset = vTable->attrib_offsets[pAttr->usage][pAttr->index];
1097 if (vOffset)
1098 ((uint32*)vertex->output)[vOffset] = attr_loc;
1099
1100 // ... increment location index, finally.
1101 attr_loc++;
1102 } // for
1103 }
1104
1105 // Okay, now we can start linking pixel/vertex attributes
1106 for (i = 0; i < pixel->attribute_count; i++)
1107 {
1108 const MOJOSHADER_attribute *pAttr = &pixel->attributes[i];
1109 if (pAttr->usage == MOJOSHADER_USAGE_UNKNOWN)
1110 continue; // Probably something like VPOS, ignore!
1111 if (pAttr->usage == MOJOSHADER_USAGE_DEPTH)
1112 continue; // This should be FragDepth, which is builtin
1113 if (is_glspirv && pAttr->usage == MOJOSHADER_USAGE_COLOR && pTable->output_offsets[pAttr->index])
1114 continue;
1115
1116 // The input may not exist in the output list!
1117 pOffset = pTable->attrib_offsets[pAttr->usage][pAttr->index];
1118 vOffset = vTable->attrib_offsets[pAttr->usage][pAttr->index];
1119 ((uint32 *) pixel->output)[pOffset] = attr_loc;
1120 if (vOffset)
1121 ((uint32 *) vertex->output)[vOffset] = attr_loc;
1122 attr_loc++;
1123 } // for
1124
1125 // There may be outputs not present in the input list!
1126 for (i = 0; i < vertex->output_count; i++)
1127 {
1128 const MOJOSHADER_attribute *vAttr = &vertex->outputs[i];
1129 assert(vAttr->usage != MOJOSHADER_USAGE_UNKNOWN);
1130 if (vAttr->usage == MOJOSHADER_USAGE_POSITION && vAttr->index == 0)
1131 continue;
1132 if (vAttr->usage == MOJOSHADER_USAGE_POINTSIZE && vAttr->index == 0)
1133 continue;
1134 if (is_glspirv && vAttr->usage == MOJOSHADER_USAGE_COLOR && pTable->output_offsets[vAttr->index])
1135 continue;
1136
1137 if (!pTable->attrib_offsets[vAttr->usage][vAttr->index])
1138 {
1139 vOffset = vTable->attrib_offsets[vAttr->usage][vAttr->index];
1140 ((uint32 *) vertex->output)[vOffset] = attr_loc++;
1141 } // if
1142 } // for
1143
1144 // gl_PointCoord support
1145 if (texcoord0Loc)
1146 {
1147 if (vTable->attrib_offsets[MOJOSHADER_USAGE_POINTSIZE][0] > 0)
1148 {
1149 ((uint32 *) pixel->output)[pTable->pointcoord_var_offset + 1] = pTable->tid_pvec2i;
1150 ((uint32 *) pixel->output)[pTable->pointcoord_load_offset + 1] = pTable->tid_vec2;
1151 ((uint32 *) pixel->output)[texcoord0Loc - 1] = SpvDecorationBuiltIn;
1152 ((uint32 *) pixel->output)[texcoord0Loc] = SpvBuiltInPointCoord;
1153 } // if
1154 else
1155 {
1156 ((uint32 *) pixel->output)[pTable->pointcoord_var_offset + 1] = pTable->tid_pvec4i;
1157 ((uint32 *) pixel->output)[pTable->pointcoord_load_offset + 1] = pTable->tid_vec4;
1158 ((uint32 *) pixel->output)[texcoord0Loc - 1] = SpvDecorationLocation;
1159 // texcoord0Loc should already have attr_loc from the above work!
1160 } // else
1161 } // if
1162 } // MOJOSHADER_spirv_link_attributes
1163 #endif
1164
1165 // end of mojoshader_common.c ...
1166