cgv
Loading...
Searching...
No Matches
shader_code.cxx
1#include <cgv/base/base.h>
2#include "shader_code.h"
3#include <cgv/base/register.h>
4#include <cgv/base/import.h>
6#include <cgv/utils/tokenizer.h>
7#include <cgv/ppp/ph_processor.h>
8#include <cgv/utils/dir.h>
9#include <cgv/utils/file.h>
10#include <cgv/type/variant.h>
11
12#ifdef WIN32
13#pragma warning(disable:4996)
14#endif
15
16using namespace cgv::base;
17using namespace cgv::type;
18using namespace cgv::utils;
19
20namespace cgv {
21 namespace render {
22
23std::map<std::string, std::string> shader_code::code_cache;
24
25std::map<std::string, std::string> shader_code::shader_file_name_map;
26
28
34
36{
37 return "shader_config";
38}
39
42{
43 return
44 rh.reflect_member("shader_path", shader_path) &&
45 rh.reflect_member("show_file_paths", show_file_paths);
46}
47
50{
52 if (!config) {
54 if (getenv("CGV_SHADER_PATH"))
55 config->shader_path = getenv("CGV_SHADER_PATH");
56 else if (getenv("CGV_DIR"))
57 config->shader_path =
58 std::string(getenv("CGV_DIR")) + "/libs/cgv_g2d/glsl;" +
59 std::string(getenv("CGV_DIR")) + "/libs/cgv_gl/glsl;"+
60 std::string(getenv("CGV_DIR")) + "/libs/cgv_gpgpu/glsl;" +
61 std::string(getenv("CGV_DIR")) + "/libs/cgv_overlay/glsl;" +
62 std::string(getenv("CGV_DIR")) + "/libs/holo_disp;" +
63 std::string(getenv("CGV_DIR")) + "/libs/plot/glsl;"+
64 std::string(getenv("CGV_DIR")) + "/libs/cgv_proc/glsl;"+
65 std::string(getenv("CGV_DIR")) + "/plugins/examples";
66 }
67 return config;
68}
69
70void shader_code::decode_if_base64(std::string& content) {
71//#ifdef _WIN32
72 if (!content.empty()) {
73 // test if the first character is equal to the base64 prefix (ANSI 'paragraph' char with hexcode A7)
74 if(content[0] == char(0xA7))
75 content = decode_base64(content.substr(1));
76 }
77/*#else
78 if (content.size() > 1) {
79 if ((uint8_t&)content[0]==0xC2 && (uint8_t&)content[1]==0xA7) // UTF-8 for '�'
80 content = decode_base64(content.substr(2));
81 else if (
82 content.size() > 2 &&
83 (int)content[0] == -17 &&
84 (int)content[1] == -65 &&
85 (int)content[2] == -67) {
86 content = decode_base64(content.substr(3));
87 }
88 }
89#endif*/
90}
91
94std::string shader_code::get_last_error(const std::string& file_name, const std::string& last_error)
95{
96 std::string fn = shader_code::find_file(file_name);
97 std::vector<line> lines;
99 std::string formated_error;
100 for (unsigned int i = 0; i<lines.size(); ++i) {
101 std::string l = to_string((const token&)(lines[i]));
102 if (to_upper(l.substr(0, 5)) == "ERROR") {
103 std::vector<token> toks;
104 tokenizer(l).set_sep(":").set_ws("").bite_all(toks);
105 formated_error += fn + "(";
106 if (toks.size() > 4) {
107 formated_error += to_string(toks[4])+") : error G0001: ";
108 if (toks.size() > 6)
109 formated_error += l.substr(toks[6].begin-&l[0]);
110 }
111 else {
112 formated_error += "1) : error G0000: ";
113 if (toks.size() > 2)
114 formated_error += l.substr(toks[2].begin-&l[0]);
115 }
116 formated_error += "\n";
117 }
118 else {
119 std::vector<token> toks;
120 tokenizer(l).set_sep(":()").set_ws("").bite_all(toks);
121 unsigned int i, j, k; int n;
122 for (i=0; i<toks.size(); ++i) {
123 if (to_string(toks[i]) == "(")
124 break;
125 }
126 for (j=0; j<toks.size(); ++j) {
127 if (to_string(toks[j]) == ")")
128 break;
129 }
130 for (k=0; k<toks.size(); ++k) {
131 if (to_string(toks[k]) == ":")
132 break;
133 }
134 if (k < toks.size() && i < j && j < k && is_integer(toks[i].end, toks[j].begin, n)) {
135 std::string _fn = fn;
136 int shader_id;
137 if (i > 0 && is_integer(toks[i-1].begin, toks[i-1].end, shader_id)) {
138 if (shader_id >= 1000) {
139 unsigned si = shader_id-1000;
140 if (si < get_shader_config()->inserted_shader_file_names.size())
141 _fn = get_shader_config()->inserted_shader_file_names[si];
142 }
143 else {
144 if ((unsigned)shader_id < get_shader_config()->shader_file_names.size())
145 _fn = get_shader_config()->shader_file_names[shader_id];
146 }
147 }
148 formated_error += _fn + l.substr(toks[i].begin - &l[0])+"\n";
149 }
150 else
151 formated_error += l+"\n";
152 }
153 }
154 return formated_error;
155}
156
159{
160 st = ST_DETECT;
161}
164{
165 if (ctx_ptr && ctx_ptr->make_current())
167 else
168 if (handle)
169 std::cerr << "shader code not destructed correctly" << std::endl;
170}
171
174{
175 if (!handle)
176 return;
177 ctx.shader_code_destruct(*this);
178 handle = 0;
179}
180
181std::string shader_code::find_file(const std::string& file_name, bool search_exhaustive)
182{
183 if (file_name.substr(0, 6) == "str://" || file_name.substr(0, 6) == "res://") {
184 std::map<std::string, resource_file_info>::const_iterator it = ref_resource_file_map().find(file_name.substr(6));
185 if (it != ref_resource_file_map().end())
186 return file_name;
187 else
188 return "";
189 }
190 if (file::exists(file_name))
191 return file_name;
192
193 std::string try_name = file::get_path(ref_prog_name()) + "/" + file_name;
194 if (file::exists(try_name))
195 return try_name;
196
197 std::map<std::string, resource_file_info>::const_iterator it =
198 ref_resource_file_map().find(file_name);
199 if (it != ref_resource_file_map().end()) {
200 if (it->second.file_offset == -1)
201 return std::string("str://") + file_name;
202 else
203 return std::string("res://") + file_name;
204 }
205 if (get_shader_config()->shader_path.empty()) {
206 try_name = std::string("glsl/") + file_name;
207 if (file::exists(try_name))
208 return try_name;
209 try_name = file::get_path(ref_prog_name()) + "/glsl/" + file_name;
210 if (file::exists(try_name))
211 return try_name;
212 return "";
213 }
214
216 std::string path_list = get_shader_config()->shader_path;
217
218 size_t pos = 0;
219 do {
220 size_t end_pos = path_list.find_first_of(';', pos);
221 std::string path;
222 if(end_pos == std::string::npos) {
223 path = path_list.substr(pos);
224 pos = path_list.length();
225 } else {
226 path = path_list.substr(pos, end_pos - pos);
227 pos = end_pos + 1;
228 }
229
230 std::vector<std::string> file_names;
231 dir::glob(path, file_names, "*gl*", true);
232
233 for(const auto& file_name : file_names) {
234 std::string ext = file::get_extension(file_name);
235 if(ext.length() > 2) {
236 if(ext[0] == 'g' && ext[1] == 'l' ||
237 ext[0] == 'p' && ext[1] == 'g' && ext[2] == 'l')
238 shader_file_name_map.emplace(file::get_file_name(file_name), file_name);
239 }
240 }
241 } while(pos < path_list.length());
242
244 }
245
246 std::map<std::string, std::string>::const_iterator file_name_map_it = shader_file_name_map.find(file_name);
248 try_name = file_name_map_it->second;
249 if(file::exists(try_name))
250 return try_name;
251 } else if(!search_exhaustive) {
252 return "";
253 }
254
255 return file::find_in_paths(file_name, get_shader_config()->shader_path, true);
256}
257
258std::string shader_code::retrieve_code(const std::string& file_name, bool use_cache, std::string* _last_error) {
259
260 std::string source = "";
261
262 if(use_cache) {
263 auto it = code_cache.find(file_name);
264 if(it != code_cache.end()) {
265 source = it->second;
266 }
267 } else {
268 code_cache.clear();
269 }
270
271 if(source.empty())
272 source = read_code_file(file_name, _last_error);
273
274 if(use_cache)
275 code_cache.emplace(file_name, source);
276
277 return source;
278}
279
280ShaderType shader_code::detect_shader_type(const std::string& file_name)
281{
282 std::string ext = to_lower(file::get_extension(file_name));
283 ShaderType st = ST_VERTEX;
284 if (ext == "glfs" || ext == "pglfs")
285 st = ST_FRAGMENT;
286 else if (ext == "glgs" || ext == "pglgs")
287 st = ST_GEOMETRY;
288 else if (ext == "glcs" || ext == "pglcs")
289 st = ST_COMPUTE;
290 else if (ext == "gltc" || ext == "pgltc")
291 st = ST_TESS_CONTROL;
292 else if (ext == "glte" || ext == "pglte")
293 st = ST_TESS_EVALUATION;
294 return st;
295}
296
297std::string shader_code::resolve_includes(const std::string& source, bool use_cache, std::set<std::string>& included_file_names, std::string* _last_error)
298{
299 const std::string identifier = "#include ";
300
301 std::string resolved_source = "";
302
303 std::vector<line> lines;
304 split_to_lines(source, lines);
305
306 for(size_t i = 0; i < lines.size(); ++i) {
307 std::string current_line = to_string(lines[i]);
308
309 // search for the include identifier
311 if(identifier_pos != std::string::npos) {
312 // remove identifier and all content before; an include directive must be the first and only statement on a line
313 current_line.erase(0, identifier_pos + identifier.length());
314
315 // trim whitespace
317
318 if(current_line.length() > 0) {
319 // remove quotation marks, leaving only the include path
320 std::string include_file_name = current_line.substr(1, current_line.length() - 2);
322
323 // check whether this file was already included and skip if this is the case
327 } else {
328 current_line = "";
329 }
330 }
331
332 // skip this line if nothing needs to be included (removes the include statement from the code)
333 if(current_line == "")
334 continue;
335 }
336
338 }
339
340 return resolved_source;
341}
342
343std::string shader_code::resolve_includes(const std::string& source, bool use_cache, std::string* _last_error) {
344 std::set<std::string> included_file_names;
345 return resolve_includes(source, use_cache, included_file_names, _last_error);
346}
347
348void shader_code::resolve_version_and_extensions(std::string& source) {
349 struct index_range {
350 size_t begin = 0;
351 size_t end = 0;
352
353 bool empty() const {
354 return begin >= end;
355 }
356
357 size_t length() const {
358 return end - begin;
359 }
360 };
361
363 enum Type {
364 kUndefined,
365 kVersion,
367 };
368
369 Type type = kUndefined;
372 // TODO: Replace with semantics for replace comment //! and conditional expansion //?
373 bool is_special_comment = false;
374 };
375
376 static const std::map<std::string, preprocessor_directive_t::Type> identifier_to_type = {
377 { "#version", preprocessor_directive_t::Type::kVersion },
378 { "#extension", preprocessor_directive_t::Type::kExtension },
379 };
380
381 enum class CommentType {
382 kNone,
383 kLine,
384 kBlock,
386 };
387
388 std::vector<preprocessor_directive_t> directives;
389
390 // read source string and collect all known directives in source
391 size_t i = 0;
392
393 CommentType comment = CommentType::kNone;
394 while(i < source.length()) {
395 char c = source[i];
396
397 switch(c) {
398 case '#':
399 if(comment == CommentType::kNone || comment == CommentType::kSpecial) {
400 // search for the next non-escaped newline which is not preceded by a backslash
401 size_t end = i;
402 do {
403 end = source.find('\n', end + 1);
404 } while(end != std::string::npos && source[end - 1] == '\\');
405
406 if(end == std::string::npos)
407 end = source.length();
408
409 // There must be at least one space that separates the directive type and its value/content between indices i and end.
410 // A valid directive also must have a value/content.
411
412 // TODO: support space between # and directive type
413
414 size_t space = source.find(' ', i + 1);
415 if(space < end) {
417 std::string type_string = source.substr(type_range.begin, type_range.length());
418 auto it = identifier_to_type.find(type_string);
419 if(it != identifier_to_type.end()) {
421 directive.type = it->second;
422 directive.type_range = { i, space };
423 directive.content_range = { space + 1, end };
424 directive.is_special_comment = comment == CommentType::kSpecial;
425 directives.push_back(directive);
426 }
427 }
428 comment = CommentType::kNone;
429 i = end;
430 }
431 break;
432 case '/':
433 if(comment == CommentType::kNone && (i + 1) < source.length()) {
434 if(source[i + 1] == '/')
435 comment = CommentType::kLine;
436 else if(source[i + 1] == '*')
437 comment = CommentType::kBlock;
438 ++i;
439 }
440 break;
441 case '*':
442 if(comment == CommentType::kBlock && (i + 1) < source.length()) {
443 if(source[i + 1] == '/')
444 comment = CommentType::kNone;
445 ++i;
446 }
447 break;
448 case '!':
449 case '?':
450 if(comment == CommentType::kLine)
451 comment = CommentType::kSpecial;
452 break;
453 case '\n':
454 if(comment != CommentType::kBlock)
455 comment = CommentType::kNone;
456 break;
457 default:
458 break;
459 }
460
461 ++i;
462 }
463
464 struct version_t {
465 size_t number = 0;
466 bool has_profile = false;
467 bool is_core_profile = false;
468 };
469
470 // find the highest version number and profile
473 if(directive.type != preprocessor_directive_t::Type::kVersion)
474 continue;
475
476 std::string content = cgv::utils::trim(source.substr(directive.content_range.begin, directive.content_range.length()));
477
478 std::vector<cgv::utils::token> tokens;
479 cgv::utils::split_to_tokens(content, tokens, "", true, "", "", " \t");
480
481 version_t version;
482
483 if(!tokens.empty()) {
484 if(!cgv::utils::from_string(version.number, to_string(tokens[0])))
485 version.number = 0;
486 }
487
488 if(tokens.size() > 1) {
489 std::string profile = to_string(tokens[1]);
490 if(profile == "core") {
491 version.has_profile = true;
492 version.is_core_profile = true;
493 } else if(profile == "compatibility") {
494 version.has_profile = true;
495 version.is_core_profile = false;
496 }
497 }
498
499 if(version.number > highest_version.number)
500 highest_version.number = version.number;
501
502 if(version.has_profile) {
503 if(highest_version.has_profile) {
504 if(version.is_core_profile)
505 highest_version.is_core_profile = true;
506 } else {
507 highest_version.has_profile = true;
508 highest_version.is_core_profile = version.is_core_profile;
509 }
510 }
511 }
512
513 // disable all non-commented version and extension directives by changing them to comments
515 if(!directive.is_special_comment && (directive.type == preprocessor_directive_t::Type::kVersion || directive.type == preprocessor_directive_t::Type::kExtension)) {
516 // Prevent expensive insertion operatons by simply overwriting the first two characters of the directive, since it is no longer needed anyway.
517 // This also has the advantage that the indices stay valid.
518 source[directive.type_range.begin] = '/';
519 source[directive.type_range.begin + 1] = '/';
520 }
521 }
522
523 // build a new header for the shader by first adding the version directive using the highest version
524 std::string header = "#version " + std::to_string(highest_version.number);
525 if(highest_version.has_profile) {
526 header += " ";
527 header += highest_version.is_core_profile ? "core" : "compatibility";
528 }
529 header += "\n";
530
531 // next add all extensions directly after the version to comply with the specification of some newer shader versions
533 if(directive.type == preprocessor_directive_t::Type::kExtension)
534 header += "#extension " + source.substr(directive.content_range.begin, directive.content_range.length()) + "\n";
535 }
536 header += '\n';
537
538 source.insert(0, header);
539}
540
543{
544 return st;
545}
546
548std::string shader_code::read_code_file(const std::string &file_name, std::string* last_error)
549{
550 std::string source;
551 std::string fn = find_file(file_name);
552 if (fn.empty()) {
553 if (last_error) {
554 *last_error = "could not find shader file ";
555 *last_error += file_name;
556 }
557 return "";
558 }
559 if (!cgv::base::read_data_file(fn, source, true)) {
560 if (last_error) {
561 *last_error = "could not read shader file ";
562 *last_error += fn;
563 }
564 return "";
565 }
566 if (get_shader_config()->show_file_paths)
567 std::cout << "read shader code <" << fn << ">" << std::endl;
568
569 decode_if_base64(source);
570
571 if (file::get_extension(file_name)[0] == 'p') {
572 std::string code;
573 get_shader_config()->inserted_shader_file_names.clear();
574 std::string paths = file::get_path(fn);
575 if (!get_shader_config()->shader_path.empty())
576 paths = paths+";"+get_shader_config()->shader_path;
577
579 php.configure_insert_to_shader(&get_shader_config()->inserted_shader_file_names);
580 if (!php.parse_string(source))
581 return "";
582 if (!php.process_to_string(code))
583 return "";
584 cgv::ppp::clear_variables();
585 source = code;
586 }
587 return source;
588}
589
591bool shader_code::read_code(const context& ctx, const std::string &file_name, ShaderType st, const shader_compile_options& options)
592{
593 if (st == ST_DETECT)
594 st = detect_shader_type(file_name);
595
596 // get source code from cache or read file
597 std::string source = retrieve_code(file_name, ctx.is_shader_file_cache_enabled(), &last_error);
598
599 source = resolve_includes(source, ctx.is_shader_file_cache_enabled());
600
601 resolve_version_and_extensions(source);
602
603 set_defines_and_snippets(source, options);
604
605 if (st == ST_VERTEX && ctx.get_gpu_vendor_id() == GPUVendorID::GPU_VENDOR_AMD)
606 set_vertex_attrib_locations(source);
607
608 if (source.empty())
609 return false;
610
611 return set_code(ctx, source, st);
612}
613
615bool shader_code::set_code(const context& ctx, const std::string &source, ShaderType _st)
616{
617 st = _st;
618 destruct(ctx);
619 ctx_ptr = &ctx;
620 return ctx.shader_code_create(*this, st, source);
621}
622
624void shader_code::set_defines_and_snippets(std::string& source, const shader_compile_options& options)
625{
626 enum class DirectiveType {
627 kUndefined,
628 kDefine,
630 };
631
632 struct directive_t {
633 DirectiveType type = DirectiveType::kUndefined;
634 std::string identifier {};
635 std::string replacement_list {};
636 };
637
638 if(options.empty())
639 return;
640
641 std::map<std::string, directive_t*> directives;
642 std::map<size_t, directive_t*> directives_per_line;
643 size_t version_line_index = std::numeric_limits<size_t>::max();
644
645 std::vector<cgv::utils::line> lines;
646 split_to_lines(source, lines);
647 for(size_t i = 0; i < lines.size(); ++i) {
648 const cgv::utils::line& line = lines[i];
649
650 if(line.empty())
651 continue;
652
653 DirectiveType directive_type = DirectiveType::kUndefined;
654
655 // Preprocessor directives and snippet markers must appear on separate lines. Only whitespace characters are allowed to appear before them.
656 const char* ptr = line.begin;
657 for(; ptr < line.end; ++ptr) {
658 char c = *ptr;
659
660 if(c == '#') {
661 directive_type = DirectiveType::kDefine;
662 break;
663 } else if(c == '/') {
664 if(line.end - ptr > 7) {
665 if(cgv::utils::token(ptr, ptr + 8) == "//$cgv::") {
666 directive_type = DirectiveType::kSnippet;
667 break;
668 }
669 }
670 }
671
672 if(!is_space(c))
673 break;
674 }
675
676 if(directive_type != DirectiveType::kUndefined) {
677 std::vector<cgv::utils::token> tokens;
678 split_to_tokens(cgv::utils::token(ptr, line.end), tokens, "");
679
680 directive_t* directive = nullptr;
681
682 switch(directive_type) {
683 case DirectiveType::kDefine:
684 // A valid preprocessor directive will have at least two tokens (directive type and identifier)
685 if(tokens.size() > 1) {
686 std::string first_token_str = to_string(tokens.front());
687
688 if(first_token_str == "#version") {
690 } else if(first_token_str == "#define" && !tokens[1].empty()) {
692 directive->identifier = to_string(tokens[1]);
693
694 // Any following tokens are considered to be the value (replacement_list) of the define.
695 if(tokens.size() > 2) {
696 cgv::utils::token value_token(tokens[2].begin, tokens.back().end);
697 directive->replacement_list = to_string(value_token);
698 }
699 }
700 }
701 break;
702 case DirectiveType::kSnippet:
703 // A valid snippet directive will have one token (identifier).
704 if(tokens.size() == 1) {
705 tokens.front().begin += 3;
706 if(!tokens.front().empty())
707 directive = new directive_t{ directive_type, to_string(tokens.front()), "" };
708 }
709 break;
710 default:
711 break;
712 }
713
714 if(directive) {
715 directives[directive->identifier] = directive;
717 }
718 }
719 }
720
721 std::vector<std::pair<std::string, std::string>> additional_defines;
722
723 for(const auto& define : options.get_macros()) {
724 auto it = directives.find(define.first);
725 if(it != directives.end() && it->second->type == DirectiveType::kDefine) {
726 it->second->replacement_list = define.second;
727 } else {
728 additional_defines.push_back({ define.first, define.second });
729 }
730 }
731
732 const shader_compile_options::string_map& snippets = options.get_snippets();
733 if(!snippets.empty())
734 additional_defines.push_back({ "CGV_USE_SNIPPETS", "" });
735
736 std::string out;
737 // The output string is going to have a similar length as the input.
738 out.reserve(source.length());
739
740 for(size_t i = 0; i < lines.size(); ++i) {
741 const cgv::utils::line& line = lines[i];
742
743 auto it = directives_per_line.find(i);
744 if(it != directives_per_line.end()) {
745 const directive_t* directive = it->second;
746
747 switch(directive->type) {
748 case DirectiveType::kDefine:
749 out += "#define " + directive->identifier + " " + directive->replacement_list;
750 break;
751 case DirectiveType::kSnippet:
752 if(!snippets.empty()) {
753 auto it = std::find_if(snippets.begin(), snippets.end(), [directive](const std::pair<std::string, std::string>& snippet) {
754 return directive->identifier == "cgv::" + snippet.first;
755 });
756 if(it != snippets.end())
757 out += it->second;
758 else
759 out += to_string(line);
760 }
761 break;
762 default:
763 break;
764 }
765 } else {
766 out += to_string(line);
767 }
768
769 out += '\n';
770
771 // Put additional defines after version directive.
772 if(version_line_index == i) {
773 out += '\n';
774 for(const auto& define : additional_defines)
775 out += "#define " + define.first + " " + define.second + '\n';
776 }
777 }
778
779 for(const auto& pair : directives)
781
782 directives.clear();
783 directives_per_line.clear();
784
785 source = std::move(out);
786}
787
789void shader_code::set_vertex_attrib_locations(std::string& source)
790{
791 struct vertex_attribute {
792 token tok;
793 int location = -1;
794 std::string type = "";
795 std::string name = "";
796
797 vertex_attribute(const token& tok) : tok(tok) {}
798
799 std::string to_string() {
800 std::string str = "layout (location = ";
801 str += std::to_string(location);
802 str += ") in ";
803 str += type + " ";
804 str += name + ";";
805 return str;
806 }
807 };
808
809 std::vector<token> parts;
810 std::vector<token> tokens;
811 std::vector<vertex_attribute> attribs;
812 std::vector<bool> attrib_flags;
813 size_t version_idx = 0;
814 int version_number = 0;
815 bool is_core = false;
816 bool no_upgrade = false;
817
818 source = strip_cpp_comments(source);
819
820 split_to_tokens(source, parts, "", true, "", "", ";\n");
821
822 attrib_flags.resize(parts.size(), false);
823
824 size_t part_idx = 0;
825
826 // First read the version. The only thing allowed before the version statement is comments and empty lines.
827 // Both get removed bevore splitting the source into parts, so the first part must be the version.
828 split_to_tokens(parts[part_idx], tokens, "", true, "", "", " \t");
829
830 if(tokens.size() > 1 && tokens[0] == "#version") {
832
833 std::string number_str = to_string(tokens[1]);
834 char* p_end;
835 const long num = std::strtol(number_str.c_str(), &p_end, 10);
836 if(number_str.c_str() != p_end)
837 version_number = static_cast<int>(num);
838
839 if(tokens.size() == 3 && tokens[2] == "core")
840 is_core = true;
841 }
842
843 ++part_idx;
844
845 // Search for the optional NO_UPGRADE define, which must come directly after the version statement.
846 tokens.clear();
847 split_to_tokens(parts[part_idx], tokens, "", true, "", "", " \t");
848
849 if(tokens.size() > 1 && tokens[0] == "#define") {
850 if(tokens[1] == "NO_UPGRADE") {
851 no_upgrade = true;
852 ++part_idx;
853 }
854 }
855
856 // return if the shader should not be upgraded
857 if(no_upgrade)
858 return;
859
860 // now get all vertex attributes
861 for(; part_idx < parts.size(); ++part_idx) {
862 auto& tok = parts[part_idx];
863
864 while(tok.begin < tok.end && is_space(*tok.begin))
865 ++tok.begin;
866
867 if(tok.size() > 2) {
868 int parentheses_count = 0;
869 bool is_new_word = true;
870 bool was_new_word = true;
871
872 for(unsigned i = 0; i < tok.size() - 2; ++i) {
873 char c = tok[i];
874
875 if(c == '(')
877
878 if(c == ')')
880
881 is_new_word = is_space(c) || c == ')';
882
883 if(c == 'i' && tok[i + 1] == 'n' && tok[i + 2] == ' ') {
884 if(was_new_word && parentheses_count == 0) {
885 attribs.push_back(vertex_attribute(tok));
886 attrib_flags[part_idx] = true;
887 break;
888 }
889 }
890
892 }
893 }
894 }
895
896 // return if no vertex attributes were found
897 if(attribs.size() == 0)
898 return;
899
900 int max_location = -1;
901 for(size_t i = 0; i < attribs.size(); ++i) {
902 auto& attrib = attribs[i];
903
904 tokens.clear();
905 split_to_tokens(attrib.tok, tokens, "", true, "", "", " \t()");
906
907 size_t size = tokens.size();
908
909 if(tokens.size() > 2) {
910 // last two entries must be type and name
911 attrib.type = to_string(tokens[size - 2]);
912 attrib.name = to_string(tokens[size - 1]);
913
914 // find location if present
915 size_t equals_idx = -1;
916 for(size_t j = 0; j < size; ++j) {
917 auto& tok = tokens[j];
918 if(tok.size() == 1 && tok[0] == '=')
919 equals_idx = j;
920 }
921
922 if(equals_idx != -1 && equals_idx > 0 && equals_idx < size - 1) {
923 if(to_string(tokens[equals_idx - 1]) == "location") {
924 std::string val_str = to_string(tokens[equals_idx + 1]);
925 char* p_end;
926 const long num = std::strtol(val_str.c_str(), &p_end, 10);
927 if(val_str.c_str() != p_end) {
928 attrib.location = static_cast<int>(num);
929 max_location = std::max(max_location, attrib.location);
930 }
931 }
932 }
933 }
934 }
935
936 for(size_t i = 0; i < attribs.size(); ++i) {
937 auto& attrib = attribs[i];
938 if(attrib.location < 0)
939 attrib.location = ++max_location;
940 }
941
942 size_t attrib_idx = 0;
943 size_t accumulate_offset = 0;
944 size_t content_offset = reinterpret_cast<size_t>(source.data());
945
946 for(size_t i = 0; i < parts.size(); ++i) {
947 auto& tok = parts[i];
948
949 if(i == version_idx || attrib_flags[i]) {
950 size_t token_begin = reinterpret_cast<size_t>(tok.begin);
951 size_t token_end = reinterpret_cast<size_t>(tok.end);
952
953 size_t offset = token_begin - content_offset;
954 size_t length = token_end - token_begin;
955
956 std::string str = "";
957
958 if(attrib_flags[i]) {
959 auto& attrib = attribs[attrib_idx];
960 ++attrib_idx;
961 str = attrib.to_string() + "\n";
962 } else {
963 version_number = std::max(version_number, 330);
964 str = "#version " + std::to_string(version_number) + (is_core ? " core" : "");
965 }
966
967 offset += accumulate_offset;
968 accumulate_offset += str.length() - length - 1;
969
970 std::string first_part = source.substr(0, offset);
971 std::string second_part = source.substr(offset + length + 1);
972
973 source = first_part + str + second_part;
974 }
975 }
976}
977
980{
981 if (!ctx.shader_code_compile(*this)) {
982 user_data = 0;
983 return false;
984 }
985 int id = 1;
986 user_data = (void*&) id;
987 return true;
988}
989
991bool shader_code::read_and_compile(const context& ctx, const std::string &file_name, ShaderType st, const shader_compile_options& options, bool show_error)
992{
993 if (!read_code(ctx, file_name, st, options))
994 return false;
995
996 if (!compile(ctx)) {
997 if (show_error)
998 std::cerr << get_last_error(file_name, last_error).c_str() << std::endl;
999 return false;
1000 }
1001
1002 return true;
1003}
1004
1007{
1008 return user_data != 0;
1009}
1010
1011
1012 }
1013}
1014
1016{
1018 {
1019 register_object(cgv::render::get_shader_config(), "register global shader config");
1020 }
1021};
1022
1023shader_config_registration shader_config_registration_instance;
More advanced text processing for splitting text into lines or tokens.
virtual bool end()
perform the leave part of the action on the current object
Definition action.cxx:48
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
bool begin()
uses call_method of base class method_action to call the method refered to by the stored method point...
Definition action.h:122
reference counted pointer, which can work together with types that are derived from ref_counted,...
Definition ref_ptr.h:160
bool empty() const
check if pointer is not yet set
Definition ref_ptr.h:230
the pre header processor parses a pre header file and converts it to a header file or uses the inform...
the self reflection handler is passed to the virtual self_reflect() method of cgv::base::base.
bool reflect_member(const std::string &member_name, T &member_ref, bool hard_cast=false)
call this to reflect a member by member name and reference to the member.
base class for all drawables, which is independent of the used rendering API.
Definition context.h:670
virtual GPUVendorID get_gpu_vendor_id() const
device information
Definition context.cxx:318
bool is_shader_file_cache_enabled() const
whether the shader file caches are enabled
Definition context.cxx:569
const context * ctx_ptr
keep pointer to my context
Definition context.h:363
std::string last_error
a string that contains the last error
Definition context.h:365
static bool shader_file_name_map_initialized
whether the shader file name map is initialized
static std::string find_file(const std::string &file_name, bool search_exhaustive=false)
Find the full path to a shader by its file name.
shader_code()
create shader a shader code object
static std::string read_code_file(const std::string &file_name, std::string *_last_error=0)
read shader code from file and return string with content or empty string if read failed
static std::map< std::string, std::string > code_cache
map that caches shader file contents indexed by their file name
ShaderType get_shader_type() const
return the shader type of this code
bool is_compiled() const
return whether shader has been compiled successfully
void destruct(const context &ctx)
destruct shader code
~shader_code()
calls the destruct method
static void decode_if_base64(std::string &content)
decode a string if it is base64 encoded
static std::string retrieve_code(const std::string &file_name, bool use_cache, std::string *_last_error)
retreive shader code either by reading the file from disk or from the cache if enabled
ShaderType st
store the shader type
static std::string get_last_error(const std::string &file_name, const std::string &last_error)
format given last error in a way that developer environments can locate errors in the source file
static std::map< std::string, std::string > shader_file_name_map
map that caches full shader file paths indexed by the shader file name
bool compile(const context &ctx)
compile attached source; returns true if successful
bool set_code(const context &ctx, const std::string &source, ShaderType st)
set shader code from string
bool read_code(const context &ctx, const std::string &file_name, ShaderType st=ST_DETECT, const shader_compile_options &options={})
read shader code from file that is searched for with find_file.
bool read_and_compile(const context &ctx, const std::string &file_name, ShaderType st=ST_DETECT, const shader_compile_options &options={}, bool show_error=true)
read shader code with read_code and compile.
static ShaderType detect_shader_type(const std::string &file_name)
detect the shader type from the extension of the given file_name, i.e.
Stores preprocessor options used for conditionally compiling shader programs.
Definition shader_code.h:73
bool empty() const
Return true if no options are set.
Definition shader_code.h:78
const string_map & get_snippets() const
Return const reference to defined snippets.
Definition shader_code.h:94
the tokenizer allows to split text into tokens in a convenient way.
Definition tokenizer.h:68
tokenizer & set_sep(const std::string &sep, bool merge)
set the list of separators and specify whether succeeding separators are merged into single tokens
Definition tokenizer.cxx:59
tokenizer & set_ws(const std::string &ws)
set the list of white spaces, that separate tokens and are skipped
Definition tokenizer.cxx:38
the base namespace holds the base hierarchy, support for plugin registration and signals
Definition action.cxx:4
bool read_data_file(const std::string &file_name, std::string &content, bool ascii)
read ascii file into a string
Definition import.cxx:388
std::map< std::string, resource_file_info > & ref_resource_file_map()
return a reference to a mapping of resource file names to resource file infos
Definition register.cxx:143
void register_object(base_ptr object, const std::string &options)
register an object and send event to all current registration ref_listeners()
Definition register.cxx:581
std::string & ref_prog_name()
return a refence to the name of the started executable
Definition register.cxx:125
shader_config_ptr get_shader_config()
return a reference to the current shader configuration
cgv::data::ref_ptr< shader_config > shader_config_ptr
type of ref counted pointer to shader configuration
Definition shader_code.h:46
ShaderType
different shader types
Definition context.h:543
namespace for compile time type information
Definition bool32_t.h:7
namespace that holds tools that dont fit any other namespace
void split_to_tokens(const char *begin, const char *end, std::vector< token > &tokens, const std::string &separators, bool merge_separators, const std::string &open_parenthesis, const std::string &close_parenthesis, const std::string &whitespaces, unsigned int max_nr_tokens)
this function splits a text range into tokens.
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
bool is_integer(const char *begin, const char *end, int &value)
check if the text range (begin,end( defines an integer value. If yes, store the value in the passed r...
Definition scan.cxx:449
std::string & trim(std::string &str, const std::string &chars)
trim white space or other characters from start and end of string
Definition scan.cxx:790
void split_to_lines(const char *global_begin, const char *global_end, std::vector< line > &lines, bool truncate_trailing_spaces)
this function splits a text range at the newline characters into single lines.
char to_lower(char c)
convert char to lower case
Definition scan.cxx:39
char to_upper(char c)
convert char to upper case
Definition scan.cxx:106
bool is_space(char c)
check if char is a whitespace
Definition scan.cxx:12
std::string decode_base64(std::string const &encoded_string)
decode a base64 encoded string
Definition convert.cxx:105
std::string strip_cpp_comments(const std::string &source, bool correct_new_lines)
remove cpp-style comments from string
bool from_string(std::string &v, const std::string &s)
specialization to extract string value from string
this header is dependency free
Definition print.h:11
a globally unique shader config is registered by default when the cgv library is used.
Definition shader_code.h:26
std::string get_type_name() const
return "shader_config"
bool show_file_paths
whether to output full paths of read shaders
Definition shader_code.h:32
shader_config()
construct config without file name tracing
bool trace_file_names
whether to keep track of file names
Definition shader_code.h:30
bool self_reflect(cgv::reflect::reflection_handler &srh)
reflect the shader_path member
std::string shader_path
the path used to find shaders with the cgv::utils::file::find_in_paths function
Definition shader_code.h:28
a line in a text is simply represented as a token
representation of a token in a text by two pointers begin and end, that point to the first character ...
Definition token.h:18
bool empty() const
return whether the token is empty
Definition token.h:56
const char * begin
pointers that define the range of characters
Definition token.h:20