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")) + "/libs/rgbd_render;"+
66 std::string(getenv("CGV_DIR")) + "/plugins/examples";
67 }
68 return config;
69}
70
71void shader_code::decode_if_base64(std::string& content) {
72//#ifdef _WIN32
73 if (!content.empty()) {
74 // test if the first character is equal to the base64 prefix (ANSI 'paragraph' char with hexcode A7)
75 if(content[0] == char(0xA7))
76 content = decode_base64(content.substr(1));
77 }
78/*#else
79 if (content.size() > 1) {
80 if ((uint8_t&)content[0]==0xC2 && (uint8_t&)content[1]==0xA7) // UTF-8 for '�'
81 content = decode_base64(content.substr(2));
82 else if (
83 content.size() > 2 &&
84 (int)content[0] == -17 &&
85 (int)content[1] == -65 &&
86 (int)content[2] == -67) {
87 content = decode_base64(content.substr(3));
88 }
89 }
90#endif*/
91}
92
95std::string shader_code::get_last_error(const std::string& file_name, const std::string& last_error)
96{
97 std::string fn = shader_code::find_file(file_name);
98 std::vector<line> lines;
100 std::string formated_error;
101 for (unsigned int i = 0; i<lines.size(); ++i) {
102 std::string l = to_string((const token&)(lines[i]));
103 if (to_upper(l.substr(0, 5)) == "ERROR") {
104 std::vector<token> toks;
105 tokenizer(l).set_sep(":").set_ws("").bite_all(toks);
106 formated_error += fn + "(";
107 if (toks.size() > 4) {
108 formated_error += to_string(toks[4])+") : error G0001: ";
109 if (toks.size() > 6)
110 formated_error += l.substr(toks[6].begin-&l[0]);
111 }
112 else {
113 formated_error += "1) : error G0000: ";
114 if (toks.size() > 2)
115 formated_error += l.substr(toks[2].begin-&l[0]);
116 }
117 formated_error += "\n";
118 }
119 else {
120 std::vector<token> toks;
121 tokenizer(l).set_sep(":()").set_ws("").bite_all(toks);
122 unsigned int i, j, k; int n;
123 for (i=0; i<toks.size(); ++i) {
124 if (to_string(toks[i]) == "(")
125 break;
126 }
127 for (j=0; j<toks.size(); ++j) {
128 if (to_string(toks[j]) == ")")
129 break;
130 }
131 for (k=0; k<toks.size(); ++k) {
132 if (to_string(toks[k]) == ":")
133 break;
134 }
135 if (k < toks.size() && i < j && j < k && is_integer(toks[i].end, toks[j].begin, n)) {
136 std::string _fn = fn;
137 int shader_id;
138 if (i > 0 && is_integer(toks[i-1].begin, toks[i-1].end, shader_id)) {
139 if (shader_id >= 1000) {
140 unsigned si = shader_id-1000;
141 if (si < get_shader_config()->inserted_shader_file_names.size())
142 _fn = get_shader_config()->inserted_shader_file_names[si];
143 }
144 else {
145 if ((unsigned)shader_id < get_shader_config()->shader_file_names.size())
146 _fn = get_shader_config()->shader_file_names[shader_id];
147 }
148 }
149 formated_error += _fn + l.substr(toks[i].begin - &l[0])+"\n";
150 }
151 else
152 formated_error += l+"\n";
153 }
154 }
155 return formated_error;
156}
157
160{
161 st = ST_DETECT;
162}
165{
166 if (ctx_ptr && ctx_ptr->make_current())
168 else
169 if (handle)
170 std::cerr << "shader code not destructed correctly" << std::endl;
171}
172
175{
176 if (!handle)
177 return;
178 ctx.shader_code_destruct(*this);
179 handle = 0;
180}
181
182std::string shader_code::find_file(const std::string& file_name, bool search_exhaustive)
183{
184 if (file_name.substr(0, 6) == "str://" || file_name.substr(0, 6) == "res://") {
185 std::map<std::string, resource_file_info>::const_iterator it = ref_resource_file_map().find(file_name.substr(6));
186 if (it != ref_resource_file_map().end())
187 return file_name;
188 else
189 return "";
190 }
191 if (file::exists(file_name))
192 return file_name;
193
194 std::string try_name = file::get_path(ref_prog_name()) + "/" + file_name;
195 if (file::exists(try_name))
196 return try_name;
197
198 std::map<std::string, resource_file_info>::const_iterator it =
199 ref_resource_file_map().find(file_name);
200 if (it != ref_resource_file_map().end()) {
201 if (it->second.file_offset == -1)
202 return std::string("str://") + file_name;
203 else
204 return std::string("res://") + file_name;
205 }
206 if (get_shader_config()->shader_path.empty()) {
207 try_name = std::string("glsl/") + file_name;
208 if (file::exists(try_name))
209 return try_name;
210 try_name = file::get_path(ref_prog_name()) + "/glsl/" + file_name;
211 if (file::exists(try_name))
212 return try_name;
213 return "";
214 }
215
217 std::string path_list = get_shader_config()->shader_path;
218
219 size_t pos = 0;
220 do {
221 size_t end_pos = path_list.find_first_of(';', pos);
222 std::string path;
223 if(end_pos == std::string::npos) {
224 path = path_list.substr(pos);
225 pos = path_list.length();
226 } else {
227 path = path_list.substr(pos, end_pos - pos);
228 pos = end_pos + 1;
229 }
230
231 std::vector<std::string> file_names;
232 dir::glob(path, file_names, "*gl*", true);
233
234 for(const auto& file_name : file_names) {
235 std::string ext = file::get_extension(file_name);
236 if(ext.length() > 2) {
237 if(ext[0] == 'g' && ext[1] == 'l' ||
238 ext[0] == 'p' && ext[1] == 'g' && ext[2] == 'l')
239 shader_file_name_map.emplace(file::get_file_name(file_name), file_name);
240 }
241 }
242 } while(pos < path_list.length());
243
245 }
246
247 std::map<std::string, std::string>::const_iterator file_name_map_it = shader_file_name_map.find(file_name);
249 try_name = file_name_map_it->second;
250 if(file::exists(try_name))
251 return try_name;
252 } else if(!search_exhaustive) {
253 return "";
254 }
255
256 return file::find_in_paths(file_name, get_shader_config()->shader_path, true);
257}
258
259std::string shader_code::retrieve_code(const std::string& file_name, bool use_cache, std::string* _last_error) {
260
261 std::string source = "";
262
263 if(use_cache) {
264 auto it = code_cache.find(file_name);
265 if(it != code_cache.end()) {
266 source = it->second;
267 }
268 } else {
269 code_cache.clear();
270 }
271
272 if(source.empty())
273 source = read_code_file(file_name, _last_error);
274
275 if(use_cache)
276 code_cache.emplace(file_name, source);
277
278 return source;
279}
280
281ShaderType shader_code::detect_shader_type(const std::string& file_name)
282{
283 std::string ext = to_lower(file::get_extension(file_name));
284 ShaderType st = ST_VERTEX;
285 if (ext == "glfs" || ext == "pglfs")
286 st = ST_FRAGMENT;
287 else if (ext == "glgs" || ext == "pglgs")
288 st = ST_GEOMETRY;
289 else if (ext == "glcs" || ext == "pglcs")
290 st = ST_COMPUTE;
291 else if (ext == "gltc" || ext == "pgltc")
292 st = ST_TESS_CONTROL;
293 else if (ext == "glte" || ext == "pglte")
294 st = ST_TESS_EVALUATION;
295 return st;
296}
297
298std::string shader_code::resolve_includes(const std::string& source, bool use_cache, std::set<std::string>& included_file_names, std::string* _last_error)
299{
300 const std::string identifier = "#include ";
301
302 std::string resolved_source = "";
303
304 std::vector<line> lines;
305 split_to_lines(source, lines);
306
307 for(size_t i = 0; i < lines.size(); ++i) {
308 std::string current_line = to_string(lines[i]);
309
310 // search for the include identifier
312 if(identifier_pos != std::string::npos) {
313 // remove identifier and all content before; an include directive must be the first and only statement on a line
314 current_line.erase(0, identifier_pos + identifier.length());
315
316 // trim whitespace
318
319 if(current_line.length() > 0) {
320 // remove quotation marks, leaving only the include path
321 std::string include_file_name = current_line.substr(1, current_line.length() - 2);
323
324 // check whether this file was already included and skip if this is the case
328 } else {
329 current_line = "";
330 }
331 }
332
333 // skip this line if nothing needs to be included (removes the include statement from the code)
334 if(current_line == "")
335 continue;
336 }
337
339 }
340
341 return resolved_source;
342}
343
344std::string shader_code::resolve_includes(const std::string& source, bool use_cache, std::string* _last_error) {
345 std::set<std::string> included_file_names;
346 return resolve_includes(source, use_cache, included_file_names, _last_error);
347}
348
349void shader_code::resolve_version_and_extensions(std::string& source) {
350 struct index_range {
351 size_t begin = 0;
352 size_t end = 0;
353
354 bool empty() const {
355 return begin >= end;
356 }
357
358 size_t length() const {
359 return end - begin;
360 }
361 };
362
364 enum Type {
366 kVersion,
368 };
369
370 Type type = kUndefined;
373 // TODO: Replace with semantics for replace comment //! and conditional expansion //?
374 bool is_special_comment = false;
375 };
376
377 static const std::map<std::string, preprocessor_directive_t::Type> identifier_to_type = {
378 { "#version", preprocessor_directive_t::Type::kVersion },
379 { "#extension", preprocessor_directive_t::Type::kExtension },
380 };
381
382 enum class CommentType {
383 kNone,
384 kLine,
385 kBlock,
387 };
388
389 std::vector<preprocessor_directive_t> directives;
390
391 // read source string and collect all known directives in source
392 size_t i = 0;
393
394 CommentType comment = CommentType::kNone;
395 while(i < source.length()) {
396 char c = source[i];
397
398 switch(c) {
399 case '#':
400 if(comment == CommentType::kNone || comment == CommentType::kSpecial) {
401 // search for the next non-escaped newline which is not preceded by a backslash
402 size_t end = i;
403 do {
404 end = source.find('\n', end + 1);
405 } while(end != std::string::npos && source[end - 1] == '\\');
406
407 if(end == std::string::npos)
408 end = source.length();
409
410 // There must be at least one space that separates the directive type and its value/content between indices i and end.
411 // A valid directive also must have a value/content.
412
413 // TODO: support space between # and directive type
414
415 size_t space = source.find(' ', i + 1);
416 if(space < end) {
418 std::string type_string = source.substr(type_range.begin, type_range.length());
419 auto it = identifier_to_type.find(type_string);
420 if(it != identifier_to_type.end()) {
422 directive.type = it->second;
423 directive.type_range = { i, space };
424 directive.content_range = { space + 1, end };
425 directive.is_special_comment = comment == CommentType::kSpecial;
426 directives.push_back(directive);
427 }
428 }
429 comment = CommentType::kNone;
430 i = end;
431 }
432 break;
433 case '/':
434 if(comment == CommentType::kNone && (i + 1) < source.length()) {
435 if(source[i + 1] == '/')
436 comment = CommentType::kLine;
437 else if(source[i + 1] == '*')
438 comment = CommentType::kBlock;
439 ++i;
440 }
441 break;
442 case '*':
443 if(comment == CommentType::kBlock && (i + 1) < source.length()) {
444 if(source[i + 1] == '/')
445 comment = CommentType::kNone;
446 ++i;
447 }
448 break;
449 case '!':
450 case '?':
451 if(comment == CommentType::kLine)
452 comment = CommentType::kSpecial;
453 break;
454 case '\n':
455 if(comment != CommentType::kBlock)
456 comment = CommentType::kNone;
457 break;
458 default:
459 break;
460 }
461
462 ++i;
463 }
464
465 struct version_t {
466 size_t number = 0;
467 bool has_profile = false;
468 bool is_core_profile = false;
469 };
470
471 // find the highest version number and profile
474 if(directive.type != preprocessor_directive_t::Type::kVersion)
475 continue;
476
477 std::string content = cgv::utils::trim(source.substr(directive.content_range.begin, directive.content_range.length()));
478
479 std::vector<cgv::utils::token> tokens;
480 cgv::utils::split_to_tokens(content, tokens, "", true, "", "", " \t");
481
482 version_t version;
483
484 if(!tokens.empty()) {
485 if(!cgv::utils::from_string(version.number, to_string(tokens[0])))
486 version.number = 0;
487 }
488
489 if(tokens.size() > 1) {
490 std::string profile = to_string(tokens[1]);
491 if(profile == "core") {
492 version.has_profile = true;
493 version.is_core_profile = true;
494 } else if(profile == "compatibility") {
495 version.has_profile = true;
496 version.is_core_profile = false;
497 }
498 }
499
500 if(version.number > highest_version.number)
501 highest_version.number = version.number;
502
503 if(version.has_profile) {
504 if(highest_version.has_profile) {
505 if(version.is_core_profile)
506 highest_version.is_core_profile = true;
507 } else {
508 highest_version.has_profile = true;
509 highest_version.is_core_profile = version.is_core_profile;
510 }
511 }
512 }
513
514 // disable all non-commented version and extension directives by changing them to comments
516 if(!directive.is_special_comment && (directive.type == preprocessor_directive_t::Type::kVersion || directive.type == preprocessor_directive_t::Type::kExtension)) {
517 // Prevent expensive insertion operatons by simply overwriting the first two characters of the directive, since it is no longer needed anyway.
518 // This also has the advantage that the indices stay valid.
519 source[directive.type_range.begin] = '/';
520 source[directive.type_range.begin + 1] = '/';
521 }
522 }
523
524 // build a new header for the shader by first adding the version directive using the highest version
525 std::string header = "#version " + std::to_string(highest_version.number);
526 if(highest_version.has_profile) {
527 header += " ";
528 header += highest_version.is_core_profile ? "core" : "compatibility";
529 }
530 header += "\n";
531
532 // next add all extensions directly after the version to comply with the specification of some newer shader versions
534 if(directive.type == preprocessor_directive_t::Type::kExtension)
535 header += "#extension " + source.substr(directive.content_range.begin, directive.content_range.length()) + "\n";
536 }
537 header += '\n';
538
539 source.insert(0, header);
540}
541
544{
545 return st;
546}
547
549std::string shader_code::read_code_file(const std::string &file_name, std::string* last_error)
550{
551 std::string source;
552 std::string fn = find_file(file_name);
553 if (fn.empty()) {
554 if (last_error) {
555 *last_error = "could not find shader file ";
556 *last_error += file_name;
557 }
558 return "";
559 }
560 if (!cgv::base::read_data_file(fn, source, true)) {
561 if (last_error) {
562 *last_error = "could not read shader file ";
563 *last_error += fn;
564 }
565 return "";
566 }
567 if (get_shader_config()->show_file_paths)
568 std::cout << "read shader code <" << fn << ">" << std::endl;
569
570 decode_if_base64(source);
571
572 if (file::get_extension(file_name)[0] == 'p') {
573 std::string code;
574 get_shader_config()->inserted_shader_file_names.clear();
575 std::string paths = file::get_path(fn);
576 if (!get_shader_config()->shader_path.empty())
577 paths = paths+";"+get_shader_config()->shader_path;
578
580 php.configure_insert_to_shader(&get_shader_config()->inserted_shader_file_names);
581 if (!php.parse_string(source))
582 return "";
583 if (!php.process_to_string(code))
584 return "";
585 cgv::ppp::clear_variables();
586 source = code;
587 }
588 return source;
589}
590
592bool shader_code::read_code(const context& ctx, const std::string &file_name, ShaderType st, const shader_compile_options& options)
593{
594 if (st == ST_DETECT)
595 st = detect_shader_type(file_name);
596
597 // get source code from cache or read file
598 std::string source = retrieve_code(file_name, ctx.is_shader_file_cache_enabled(), &last_error);
599
600 source = resolve_includes(source, ctx.is_shader_file_cache_enabled());
601
602 resolve_version_and_extensions(source);
603
604 set_defines_and_snippets(source, options);
605
606 if (st == ST_VERTEX && ctx.get_gpu_vendor_id() == GPUVendorID::GPU_VENDOR_AMD)
607 set_vertex_attrib_locations(source);
608
609 if (source.empty())
610 return false;
611
612 return set_code(ctx, source, st);
613}
614
616bool shader_code::set_code(const context& ctx, const std::string &source, ShaderType _st)
617{
618 st = _st;
619 destruct(ctx);
620 ctx_ptr = &ctx;
621 return ctx.shader_code_create(*this, st, source);
622}
623
625void shader_code::set_defines_and_snippets(std::string& source, const shader_compile_options& options)
626{
627 enum class DirectiveType {
629 kDefine,
631 };
632
633 struct directive_t {
634 DirectiveType type = DirectiveType::kUndefined;
635 std::string identifier {};
636 std::string replacement_list {};
637 };
638
639 if(options.empty())
640 return;
641
642 std::map<std::string, directive_t*> directives;
643 std::map<size_t, directive_t*> directives_per_line;
644 size_t version_line_index = std::numeric_limits<size_t>::max();
645
646 std::vector<cgv::utils::line> lines;
647 split_to_lines(source, lines);
648 for(size_t i = 0; i < lines.size(); ++i) {
649 const cgv::utils::line& line = lines[i];
650
651 if(line.empty())
652 continue;
653
654 DirectiveType directive_type = DirectiveType::kUndefined;
655
656 // Preprocessor directives and snippet markers must appear on separate lines. Only whitespace characters are allowed to appear before them.
657 const char* ptr = line.begin;
658 for(; ptr < line.end; ++ptr) {
659 char c = *ptr;
660
661 if(c == '#') {
662 directive_type = DirectiveType::kDefine;
663 break;
664 } else if(c == '/') {
665 if(line.end - ptr > 7) {
666 if(cgv::utils::token(ptr, ptr + 8) == "//$cgv::") {
667 directive_type = DirectiveType::kSnippet;
668 break;
669 }
670 }
671 }
672
673 if(!is_space(c))
674 break;
675 }
676
677 if(directive_type != DirectiveType::kUndefined) {
678 std::vector<cgv::utils::token> tokens;
679 split_to_tokens(cgv::utils::token(ptr, line.end), tokens, "");
680
681 directive_t* directive = nullptr;
682
683 switch(directive_type) {
684 case DirectiveType::kDefine:
685 // A valid preprocessor directive will have at least two tokens (directive type and identifier)
686 if(tokens.size() > 1) {
687 std::string first_token_str = to_string(tokens.front());
688
689 if(first_token_str == "#version") {
691 } else if(first_token_str == "#define" && !tokens[1].empty()) {
693 directive->identifier = to_string(tokens[1]);
694
695 // Any following tokens are considered to be the value (replacement_list) of the define.
696 if(tokens.size() > 2) {
697 cgv::utils::token value_token(tokens[2].begin, tokens.back().end);
698 directive->replacement_list = to_string(value_token);
699 }
700 }
701 }
702 break;
703 case DirectiveType::kSnippet:
704 // A valid snippet directive will have one token (identifier).
705 if(tokens.size() == 1) {
706 tokens.front().begin += 3;
707 if(!tokens.front().empty())
708 directive = new directive_t{ directive_type, to_string(tokens.front()), "" };
709 }
710 break;
711 default:
712 break;
713 }
714
715 if(directive) {
716 directives[directive->identifier] = directive;
718 }
719 }
720 }
721
722 std::vector<std::pair<std::string, std::string>> additional_defines;
723
724 for(const auto& define : options.get_macros()) {
725 auto it = directives.find(define.first);
726 if(it != directives.end() && it->second->type == DirectiveType::kDefine) {
727 it->second->replacement_list = define.second;
728 } else {
729 additional_defines.push_back({ define.first, define.second });
730 }
731 }
732
733 const shader_compile_options::string_map& snippets = options.get_snippets();
734 if(!snippets.empty())
735 additional_defines.push_back({ "CGV_USE_SNIPPETS", "" });
736
737 std::string out;
738 // The output string is going to have a similar length as the input.
739 out.reserve(source.length());
740
741 for(size_t i = 0; i < lines.size(); ++i) {
742 const cgv::utils::line& line = lines[i];
743
744 auto it = directives_per_line.find(i);
745 if(it != directives_per_line.end()) {
746 const directive_t* directive = it->second;
747
748 switch(directive->type) {
749 case DirectiveType::kDefine:
750 out += "#define " + directive->identifier + " " + directive->replacement_list;
751 break;
752 case DirectiveType::kSnippet:
753 if(!snippets.empty()) {
754 auto it = std::find_if(snippets.begin(), snippets.end(), [directive](const std::pair<std::string, std::string>& snippet) {
755 return directive->identifier == "cgv::" + snippet.first;
756 });
757 if(it != snippets.end())
758 out += it->second;
759 else
760 out += to_string(line);
761 }
762 break;
763 default:
764 break;
765 }
766 } else {
767 out += to_string(line);
768 }
769
770 out += '\n';
771
772 // Put additional defines after version directive.
773 if(version_line_index == i) {
774 out += '\n';
775 for(const auto& define : additional_defines)
776 out += "#define " + define.first + " " + define.second + '\n';
777 }
778 }
779
780 for(const auto& pair : directives)
782
783 directives.clear();
784 directives_per_line.clear();
785
786 source = std::move(out);
787}
788
790void shader_code::set_vertex_attrib_locations(std::string& source)
791{
792 struct vertex_attribute {
793 token tok;
794 int location = -1;
795 std::string type = "";
796 std::string name = "";
797
798 vertex_attribute(const token& tok) : tok(tok) {}
799
800 std::string to_string() {
801 std::string str = "layout (location = ";
802 str += std::to_string(location);
803 str += ") in ";
804 str += type + " ";
805 str += name + ";";
806 return str;
807 }
808 };
809
810 std::vector<token> parts;
811 std::vector<token> tokens;
812 std::vector<vertex_attribute> attribs;
813 std::vector<bool> attrib_flags;
814 size_t version_idx = 0;
815 int version_number = 0;
816 bool is_core = false;
817 bool no_upgrade = false;
818
819 source = strip_cpp_comments(source);
820
821 split_to_tokens(source, parts, "", true, "", "", ";\n");
822
823 attrib_flags.resize(parts.size(), false);
824
825 size_t part_idx = 0;
826
827 // First read the version. The only thing allowed before the version statement is comments and empty lines.
828 // Both get removed bevore splitting the source into parts, so the first part must be the version.
829 split_to_tokens(parts[part_idx], tokens, "", true, "", "", " \t");
830
831 if(tokens.size() > 1 && tokens[0] == "#version") {
833
834 std::string number_str = to_string(tokens[1]);
835 char* p_end;
836 const long num = std::strtol(number_str.c_str(), &p_end, 10);
837 if(number_str.c_str() != p_end)
838 version_number = static_cast<int>(num);
839
840 if(tokens.size() == 3 && tokens[2] == "core")
841 is_core = true;
842 }
843
844 ++part_idx;
845
846 // Search for the optional NO_UPGRADE define, which must come directly after the version statement.
847 tokens.clear();
848 split_to_tokens(parts[part_idx], tokens, "", true, "", "", " \t");
849
850 if(tokens.size() > 1 && tokens[0] == "#define") {
851 if(tokens[1] == "NO_UPGRADE") {
852 no_upgrade = true;
853 ++part_idx;
854 }
855 }
856
857 // return if the shader should not be upgraded
858 if(no_upgrade)
859 return;
860
861 // now get all vertex attributes
862 for(; part_idx < parts.size(); ++part_idx) {
863 auto& tok = parts[part_idx];
864
865 while(tok.begin < tok.end && is_space(*tok.begin))
866 ++tok.begin;
867
868 if(tok.size() > 2) {
869 int parentheses_count = 0;
870 bool is_new_word = true;
871 bool was_new_word = true;
872
873 for(unsigned i = 0; i < tok.size() - 2; ++i) {
874 char c = tok[i];
875
876 if(c == '(')
878
879 if(c == ')')
881
882 is_new_word = is_space(c) || c == ')';
883
884 if(c == 'i' && tok[i + 1] == 'n' && tok[i + 2] == ' ') {
885 if(was_new_word && parentheses_count == 0) {
886 attribs.push_back(vertex_attribute(tok));
887 attrib_flags[part_idx] = true;
888 break;
889 }
890 }
891
893 }
894 }
895 }
896
897 // return if no vertex attributes were found
898 if(attribs.size() == 0)
899 return;
900
901 int max_location = -1;
902 for(size_t i = 0; i < attribs.size(); ++i) {
903 auto& attrib = attribs[i];
904
905 tokens.clear();
906 split_to_tokens(attrib.tok, tokens, "", true, "", "", " \t()");
907
908 size_t size = tokens.size();
909
910 if(tokens.size() > 2) {
911 // last two entries must be type and name
912 attrib.type = to_string(tokens[size - 2]);
913 attrib.name = to_string(tokens[size - 1]);
914
915 // find location if present
916 size_t equals_idx = -1;
917 for(size_t j = 0; j < size; ++j) {
918 auto& tok = tokens[j];
919 if(tok.size() == 1 && tok[0] == '=')
920 equals_idx = j;
921 }
922
923 if(equals_idx != -1 && equals_idx > 0 && equals_idx < size - 1) {
924 if(to_string(tokens[equals_idx - 1]) == "location") {
925 std::string val_str = to_string(tokens[equals_idx + 1]);
926 char* p_end;
927 const long num = std::strtol(val_str.c_str(), &p_end, 10);
928 if(val_str.c_str() != p_end) {
929 attrib.location = static_cast<int>(num);
930 max_location = std::max(max_location, attrib.location);
931 }
932 }
933 }
934 }
935 }
936
937 for(size_t i = 0; i < attribs.size(); ++i) {
938 auto& attrib = attribs[i];
939 if(attrib.location < 0)
940 attrib.location = ++max_location;
941 }
942
943 size_t attrib_idx = 0;
944 size_t accumulate_offset = 0;
945 size_t content_offset = reinterpret_cast<size_t>(source.data());
946
947 for(size_t i = 0; i < parts.size(); ++i) {
948 auto& tok = parts[i];
949
950 if(i == version_idx || attrib_flags[i]) {
951 size_t token_begin = reinterpret_cast<size_t>(tok.begin);
952 size_t token_end = reinterpret_cast<size_t>(tok.end);
953
954 size_t offset = token_begin - content_offset;
955 size_t length = token_end - token_begin;
956
957 std::string str = "";
958
959 if(attrib_flags[i]) {
960 auto& attrib = attribs[attrib_idx];
961 ++attrib_idx;
962 str = attrib.to_string() + "\n";
963 } else {
964 version_number = std::max(version_number, 330);
965 str = "#version " + std::to_string(version_number) + (is_core ? " core" : "");
966 }
967
968 offset += accumulate_offset;
969 accumulate_offset += str.length() - length - 1;
970
971 std::string first_part = source.substr(0, offset);
972 std::string second_part = source.substr(offset + length + 1);
973
974 source = first_part + str + second_part;
975 }
976 }
977}
978
981{
982 if (!ctx.shader_code_compile(*this)) {
983 user_data = 0;
984 return false;
985 }
986 int id = 1;
987 user_data = (void*&) id;
988 return true;
989}
990
992bool shader_code::read_and_compile(const context& ctx, const std::string &file_name, ShaderType st, const shader_compile_options& options, bool show_error)
993{
994 if (!read_code(ctx, file_name, st, options))
995 return false;
996
997 if (!compile(ctx)) {
998 if (show_error)
999 std::cerr << get_last_error(file_name, last_error).c_str() << std::endl;
1000 return false;
1001 }
1002
1003 return true;
1004}
1005
1008{
1009 return user_data != 0;
1010}
1011
1012
1013 }
1014}
1015
1017{
1019 {
1020 register_object(cgv::render::get_shader_config(), "register global shader config");
1021 }
1022};
1023
1024shader_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