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_gl/glsl;"+
59 std::string(getenv("CGV_DIR"))+"/libs/plot/glsl;"+
60 std::string(getenv("CGV_DIR")) + "/libs/cgv_app/glsl;" +
61 std::string(getenv("CGV_DIR")) + "/libs/cgv_g2d/glsl;" +
62 std::string(getenv("CGV_DIR")) + "/libs/cgv_gpgpu/glsl;" +
63 std::string(getenv("CGV_DIR")) + "/libs/holo_disp;" +
64 std::string(getenv("CGV_DIR")) + "/plugins/examples";
65 }
66 return config;
67}
68
69void shader_code::decode_if_base64(std::string& content) {
70//#ifdef _WIN32
71 if (!content.empty()) {
72 // test if the first character is equal to the base64 prefix (ANSI 'paragraph' char with hexcode A7)
73 if(content[0] == char(0xA7))
74 content = decode_base64(content.substr(1));
75 }
76/*#else
77 if (content.size() > 1) {
78 if ((uint8_t&)content[0]==0xC2 && (uint8_t&)content[1]==0xA7) // UTF-8 for '�'
79 content = decode_base64(content.substr(2));
80 else if (
81 content.size() > 2 &&
82 (int)content[0] == -17 &&
83 (int)content[1] == -65 &&
84 (int)content[2] == -67) {
85 content = decode_base64(content.substr(3));
86 }
87 }
88#endif*/
89}
90
93std::string shader_code::get_last_error(const std::string& file_name, const std::string& last_error)
94{
95 std::string fn = shader_code::find_file(file_name);
96 std::vector<line> lines;
98 std::string formated_error;
99 for (unsigned int i = 0; i<lines.size(); ++i) {
100 std::string l = to_string((const token&)(lines[i]));
101 if (to_upper(l.substr(0, 5)) == "ERROR") {
102 std::vector<token> toks;
103 tokenizer(l).set_sep(":").set_ws("").bite_all(toks);
104 formated_error += fn + "(";
105 if (toks.size() > 4) {
106 formated_error += to_string(toks[4])+") : error G0001: ";
107 if (toks.size() > 6)
108 formated_error += l.substr(toks[6].begin-&l[0]);
109 }
110 else {
111 formated_error += "1) : error G0000: ";
112 if (toks.size() > 2)
113 formated_error += l.substr(toks[2].begin-&l[0]);
114 }
115 formated_error += "\n";
116 }
117 else {
118 std::vector<token> toks;
119 tokenizer(l).set_sep(":()").set_ws("").bite_all(toks);
120 unsigned int i, j, k; int n;
121 for (i=0; i<toks.size(); ++i) {
122 if (to_string(toks[i]) == "(")
123 break;
124 }
125 for (j=0; j<toks.size(); ++j) {
126 if (to_string(toks[j]) == ")")
127 break;
128 }
129 for (k=0; k<toks.size(); ++k) {
130 if (to_string(toks[k]) == ":")
131 break;
132 }
133 if (k < toks.size() && i < j && j < k && is_integer(toks[i].end, toks[j].begin, n)) {
134 std::string _fn = fn;
135 int shader_id;
136 if (i > 0 && is_integer(toks[i-1].begin, toks[i-1].end, shader_id)) {
137 if (shader_id >= 1000) {
138 unsigned si = shader_id-1000;
139 if (si < get_shader_config()->inserted_shader_file_names.size())
140 _fn = get_shader_config()->inserted_shader_file_names[si];
141 }
142 else {
143 if ((unsigned)shader_id < get_shader_config()->shader_file_names.size())
144 _fn = get_shader_config()->shader_file_names[shader_id];
145 }
146 }
147 formated_error += _fn + l.substr(toks[i].begin - &l[0])+"\n";
148 }
149 else
150 formated_error += l+"\n";
151 }
152 }
153 return formated_error;
154}
155
158{
159 st = ST_DETECT;
160}
163{
164 if (ctx_ptr && ctx_ptr->make_current())
166 else
167 if (handle)
168 std::cerr << "shader code not destructed correctly" << std::endl;
169}
170
173{
174 if (!handle)
175 return;
176 ctx.shader_code_destruct(*this);
177 handle = 0;
178}
179
180std::string shader_code::find_file(const std::string& file_name, bool search_exhaustive)
181{
182 if (file_name.substr(0, 6) == "str://" || file_name.substr(0, 6) == "res://") {
183 std::map<std::string, resource_file_info>::const_iterator it = ref_resource_file_map().find(file_name.substr(6));
184 if (it != ref_resource_file_map().end())
185 return file_name;
186 else
187 return "";
188 }
189 if (file::exists(file_name))
190 return file_name;
191
192 std::string try_name = file::get_path(ref_prog_name()) + "/" + file_name;
193 if (file::exists(try_name))
194 return try_name;
195
196 std::map<std::string, resource_file_info>::const_iterator it =
197 ref_resource_file_map().find(file_name);
198 if (it != ref_resource_file_map().end()) {
199 if (it->second.file_offset == -1)
200 return std::string("str://") + file_name;
201 else
202 return std::string("res://") + file_name;
203 }
204 if (get_shader_config()->shader_path.empty()) {
205 try_name = std::string("glsl/") + file_name;
206 if (file::exists(try_name))
207 return try_name;
208 try_name = file::get_path(ref_prog_name()) + "/glsl/" + file_name;
209 if (file::exists(try_name))
210 return try_name;
211 return "";
212 }
213
215 std::string path_list = get_shader_config()->shader_path;
216
217 size_t pos = 0;
218 do {
219 size_t end_pos = path_list.find_first_of(';', pos);
220 std::string path;
221 if(end_pos == std::string::npos) {
222 path = path_list.substr(pos);
223 pos = path_list.length();
224 } else {
225 path = path_list.substr(pos, end_pos - pos);
226 pos = end_pos + 1;
227 }
228
229 std::vector<std::string> file_names;
230 dir::glob(path, file_names, "*gl*", true);
231
232 for(const auto& file_name : file_names) {
233 std::string ext = file::get_extension(file_name);
234 if(ext.length() > 2) {
235 if(ext[0] == 'g' && ext[1] == 'l' ||
236 ext[0] == 'p' && ext[1] == 'g' && ext[2] == 'l')
237 shader_file_name_map.emplace(file::get_file_name(file_name), file_name);
238 }
239 }
240 } while(pos < path_list.length());
241
243 }
244
245 std::map<std::string, std::string>::const_iterator file_name_map_it = shader_file_name_map.find(file_name);
247 try_name = file_name_map_it->second;
248 if(file::exists(try_name))
249 return try_name;
250 } else if(!search_exhaustive) {
251 return "";
252 }
253
254 return file::find_in_paths(file_name, get_shader_config()->shader_path, true);
255}
256
257std::string shader_code::retrieve_code(const std::string& file_name, bool use_cache, std::string* _last_error) {
258
259 std::string source = "";
260
261 if(use_cache) {
262 auto it = code_cache.find(file_name);
263 if(it != code_cache.end()) {
264 source = it->second;
265 }
266 } else {
267 code_cache.clear();
268 }
269
270 if(source.empty())
271 source = read_code_file(file_name, _last_error);
272
273 if(use_cache)
274 code_cache.emplace(file_name, source);
275
276 return source;
277}
278
279ShaderType shader_code::detect_shader_type(const std::string& file_name)
280{
281 std::string ext = to_lower(file::get_extension(file_name));
282 ShaderType st = ST_VERTEX;
283 if (ext == "glfs" || ext == "pglfs")
284 st = ST_FRAGMENT;
285 else if (ext == "glgs" || ext == "pglgs")
286 st = ST_GEOMETRY;
287 else if (ext == "glcs" || ext == "pglcs")
288 st = ST_COMPUTE;
289 else if (ext == "gltc" || ext == "pgltc")
290 st = ST_TESS_CONTROL;
291 else if (ext == "glte" || ext == "pglte")
292 st = ST_TESS_EVALUATION;
293 return st;
294}
295
296std::string shader_code::resolve_includes(const std::string& source, bool use_cache, std::set<std::string>& included_file_names, std::string* _last_error)
297{
298 const std::string identifier = "#include ";
299
300 std::string resolved_source = "";
301
302 std::vector<line> lines;
303 split_to_lines(source, lines);
304
305 for(size_t i = 0; i < lines.size(); ++i) {
306 std::string current_line = to_string(lines[i]);
307
308 // search for the include identifier
310 if(identifier_pos != std::string::npos) {
311 // remove identifier and all content before; an include directive must be the first and only statement on a line
312 current_line.erase(0, identifier_pos + identifier.length());
313
314 // trim whitespace
316
317 if(current_line.length() > 0) {
318 // remove quotation marks, leaving only the include path
319 std::string include_file_name = current_line.substr(1, current_line.length() - 2);
321
322 // check whether this file was already included and skip if this is the case
326 } else {
327 current_line = "";
328 }
329 }
330
331 // skip this line if nothing needs to be included (removes the include statement from the code)
332 if(current_line == "")
333 continue;
334 }
335
337 }
338
339 return resolved_source;
340}
341
342std::string shader_code::resolve_includes(const std::string& source, bool use_cache, std::string* _last_error) {
343 std::set<std::string> included_file_names;
344 return resolve_includes(source, use_cache, included_file_names, _last_error);
345}
346
347void shader_code::resolve_version_and_extensions(std::string& source) {
348 struct index_range {
349 size_t begin = 0;
350 size_t end = 0;
351
352 bool empty() const {
353 return begin >= end;
354 }
355
356 size_t length() const {
357 return end - begin;
358 }
359 };
360
362 enum Type {
364 kVersion,
366 };
367
368 Type type = kUndefined;
371 // TODO: Replace with semantics for replace comment //! and conditional expansion //?
372 bool is_special_comment = false;
373 };
374
375 static const std::map<std::string, preprocessor_directive_t::Type> identifier_to_type = {
376 { "#version", preprocessor_directive_t::Type::kVersion },
377 { "#extension", preprocessor_directive_t::Type::kExtension },
378 };
379
380 enum class CommentType {
381 kNone,
382 kLine,
383 kBlock,
385 };
386
387 std::vector<preprocessor_directive_t> directives;
388
389 // read source string and collect all known directives in source
390 size_t i = 0;
391
392 CommentType comment = CommentType::kNone;
393 while(i < source.length()) {
394 char c = source[i];
395
396 switch(c) {
397 case '#':
398 if(comment == CommentType::kNone || comment == CommentType::kSpecial) {
399 // search for the next non-escaped newline which is not preceded by a backslash
400 size_t end = i;
401 do {
402 end = source.find('\n', end + 1);
403 } while(end != std::string::npos && source[end - 1] == '\\');
404
405 if(end == std::string::npos)
406 end = source.length();
407
408 // There must be at least one space that separates the directive type and its value/content between indices i and end.
409 // A valid directive also must have a value/content.
410
411 // TODO: support space between # and directive type
412
413 size_t space = source.find(' ', i + 1);
414 if(space < end) {
416 std::string type_string = source.substr(type_range.begin, type_range.length());
417 auto it = identifier_to_type.find(type_string);
418 if(it != identifier_to_type.end()) {
420 directive.type = it->second;
421 directive.type_range = { i, space };
422 directive.content_range = { space + 1, end };
423 directive.is_special_comment = comment == CommentType::kSpecial;
424 directives.push_back(directive);
425 }
426 }
427 comment = CommentType::kNone;
428 i = end;
429 }
430 break;
431 case '/':
432 if(comment == CommentType::kNone && (i + 1) < source.length()) {
433 if(source[i + 1] == '/')
434 comment = CommentType::kLine;
435 else if(source[i + 1] == '*')
436 comment = CommentType::kBlock;
437 ++i;
438 }
439 break;
440 case '*':
441 if(comment == CommentType::kBlock && (i + 1) < source.length()) {
442 if(source[i + 1] == '/')
443 comment = CommentType::kNone;
444 ++i;
445 }
446 break;
447 case '!':
448 case '?':
449 if(comment == CommentType::kLine)
450 comment = CommentType::kSpecial;
451 break;
452 case '\n':
453 if(comment != CommentType::kBlock)
454 comment = CommentType::kNone;
455 break;
456 default:
457 break;
458 }
459
460 ++i;
461 }
462
463 struct version_t {
464 size_t number = 0;
465 bool has_profile = false;
466 bool is_core_profile = false;
467 };
468
469 // find the highest version number and profile
472 if(directive.type != preprocessor_directive_t::Type::kVersion)
473 continue;
474
475 std::string content = cgv::utils::trim(source.substr(directive.content_range.begin, directive.content_range.length()));
476
477 std::vector<cgv::utils::token> tokens;
478 cgv::utils::split_to_tokens(content, tokens, "", true, "", "", " \t");
479
480 version_t version;
481
482 if(!tokens.empty()) {
483 if(!cgv::utils::from_string(version.number, to_string(tokens[0])))
484 version.number = 0;
485 }
486
487 if(tokens.size() > 1) {
488 std::string profile = to_string(tokens[1]);
489 if(profile == "core") {
490 version.has_profile = true;
491 version.is_core_profile = true;
492 } else if(profile == "compatibility") {
493 version.has_profile = true;
494 version.is_core_profile = false;
495 }
496 }
497
498 if(version.number > highest_version.number)
499 highest_version.number = version.number;
500
501 if(version.has_profile) {
502 if(highest_version.has_profile) {
503 if(version.is_core_profile)
504 highest_version.is_core_profile = true;
505 } else {
506 highest_version.has_profile = true;
507 highest_version.is_core_profile = version.is_core_profile;
508 }
509 }
510 }
511
512 // disable all non-commented version and extension directives by changing them to comments
514 if(!directive.is_special_comment && (directive.type == preprocessor_directive_t::Type::kVersion || directive.type == preprocessor_directive_t::Type::kExtension)) {
515 // Prevent expensive insertion operatons by simply overwriting the first two characters of the directive, since it is no longer needed anyway.
516 // This also has the advantage that the indices stay valid.
517 source[directive.type_range.begin] = '/';
518 source[directive.type_range.begin + 1] = '/';
519 }
520 }
521
522 // build a new header for the shader by first adding the version directive using the highest version
523 std::string header = "#version " + std::to_string(highest_version.number);
524 if(highest_version.has_profile) {
525 header += " ";
526 header += highest_version.is_core_profile ? "core" : "compatibility";
527 }
528 header += "\n";
529
530 // next add all extensions directly after the version to comply with the specification of some newer shader versions
532 if(directive.type == preprocessor_directive_t::Type::kExtension)
533 header += "#extension " + source.substr(directive.content_range.begin, directive.content_range.length()) + "\n";
534 }
535 header += '\n';
536
537 source.insert(0, header);
538}
539
542{
543 return st;
544}
545
547std::string shader_code::read_code_file(const std::string &file_name, std::string* last_error)
548{
549 std::string source;
550 std::string fn = find_file(file_name);
551 if (fn.empty()) {
552 if (last_error) {
553 *last_error = "could not find shader file ";
554 *last_error += file_name;
555 }
556 return "";
557 }
558 if (!cgv::base::read_data_file(fn, source, true)) {
559 if (last_error) {
560 *last_error = "could not read shader file ";
561 *last_error += fn;
562 }
563 return "";
564 }
565 if (get_shader_config()->show_file_paths)
566 std::cout << "read shader code <" << fn << ">" << std::endl;
567
568 decode_if_base64(source);
569
570 if (file::get_extension(file_name)[0] == 'p') {
571 std::string code;
572 get_shader_config()->inserted_shader_file_names.clear();
573 std::string paths = file::get_path(fn);
574 if (!get_shader_config()->shader_path.empty())
575 paths = paths+";"+get_shader_config()->shader_path;
576
578 php.configure_insert_to_shader(&get_shader_config()->inserted_shader_file_names);
579 if (!php.parse_string(source))
580 return "";
581 if (!php.process_to_string(code))
582 return "";
583 cgv::ppp::clear_variables();
584 source = code;
585 }
586 return source;
587}
588
590bool shader_code::read_code(const context& ctx, const std::string &file_name, ShaderType st, const shader_compile_options& options)
591{
592 if (st == ST_DETECT)
593 st = detect_shader_type(file_name);
594
595 // get source code from cache or read file
596 std::string source = retrieve_code(file_name, ctx.is_shader_file_cache_enabled(), &last_error);
597
598 source = resolve_includes(source, ctx.is_shader_file_cache_enabled());
599
600 resolve_version_and_extensions(source);
601
602 set_defines_and_snippets(source, options);
603
604 if (st == ST_VERTEX && ctx.get_gpu_vendor_id() == GPUVendorID::GPU_VENDOR_AMD)
605 set_vertex_attrib_locations(source);
606
607 if (source.empty())
608 return false;
609
610 return set_code(ctx, source, st);
611}
612
614bool shader_code::set_code(const context& ctx, const std::string &source, ShaderType _st)
615{
616 st = _st;
617 destruct(ctx);
618 ctx_ptr = &ctx;
619 return ctx.shader_code_create(*this, st, source);
620}
621
623void shader_code::set_defines_and_snippets(std::string& source, const shader_compile_options& options)
624{
625 enum class DirectiveType {
627 kDefine,
629 };
630
631 struct directive_t {
632 DirectiveType type = DirectiveType::kUndefined;
633 std::string identifier;
634 std::string value;
635 };
636
637 if(options.defines.empty() && options.snippets.empty())
638 return;
639
640 std::map<std::string, directive_t*> directives;
641 std::map<size_t, directive_t*> directives_per_line;
642 size_t version_line_index = std::numeric_limits<size_t>::max();
643
644 std::vector<cgv::utils::line> lines;
645 split_to_lines(source, lines);
646 for(size_t i = 0; i < lines.size(); ++i) {
647 const cgv::utils::line& line = lines[i];
648
649 if(line.empty())
650 continue;
651
652 DirectiveType directive_type = DirectiveType::kUndefined;
653
654 // Pre-processor directives and snippet markers must appear on separate lines. Only whitespace characters are allowed to appear before them.
655 const char* ptr = line.begin;
656 for(; ptr < line.end; ++ptr) {
657 char c = *ptr;
658
659 if(c == '#') {
660 directive_type = DirectiveType::kDefine;
661 break;
662 } else if(c == '/') {
663 if(line.end - ptr > 7) {
664 if(cgv::utils::token(ptr, ptr + 8) == "//$cgv::") {
665 directive_type = DirectiveType::kSnippet;
666 break;
667 }
668 }
669 }
670
671 if(!is_space(c))
672 break;
673 }
674
675 if(directive_type != DirectiveType::kUndefined) {
676 std::vector<cgv::utils::token> tokens;
677 split_to_tokens(cgv::utils::token(ptr, line.end), tokens, "");
678
679 directive_t* directive = nullptr;
680
681 switch(directive_type) {
682 case DirectiveType::kDefine:
683 // A valid define directive will have at least two tokens (directive and name)
684 if(tokens.size() > 1) {
685 std::string first_token_str = to_string(tokens.front());
686
687 if(first_token_str == "#version") {
689 } else if(first_token_str == "#define" && !tokens[1].empty()) {
691 directive->identifier = to_string(tokens[1]);
692
693 // Any following tokens are considered to be the value of the define.
694 if(tokens.size() > 2) {
695 cgv::utils::token value_token(tokens[2].begin, tokens.back().end);
697 }
698 }
699 }
700 break;
701 case DirectiveType::kSnippet:
702 // A valid snippet directive will have one token (identifier).
703 if(tokens.size() == 1) {
704 tokens.front().begin += 3;
705 if(!tokens.front().empty())
706 directive = new directive_t{ directive_type, to_string(tokens.front()), "" };
707 }
708 break;
709 default:
710 break;
711 }
712
713 if(directive) {
714 directives[directive->identifier] = directive;
716 }
717 }
718 }
719
720 std::vector<std::pair<std::string, std::string>> additional_defines;
721
722 for(const auto& define : options.defines) {
723 auto it = directives.find(define.first);
724 if(it != directives.end() && it->second->type == DirectiveType::kDefine)
725 it->second->value = define.second;
726 else
727 additional_defines.push_back(define);
728 }
729
730 bool use_snippets = false;
731 if(!options.snippets.empty()) {
732 additional_defines.push_back({ "CGV_USE_SNIPPETS", "" });
733 use_snippets = true;
734 }
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->value;
750 break;
751 case DirectiveType::kSnippet:
752 if(use_snippets) {
753 auto it = std::find_if(options.snippets.begin(), options.snippets.end(), [directive](const shader_code_snippet& snippet) {
754 return directive->identifier == "cgv::" + snippet.id;
755 });
756 if(it != options.snippets.end())
757 out += it->content;
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:627
virtual GPUVendorID get_gpu_vendor_id() const
device information
Definition context.cxx:230
bool is_shader_file_cache_enabled() const
whether the shader file caches are enabled
Definition context.cxx:467
const context * ctx_ptr
keep pointer to my context
Definition context.h:316
std::string last_error
a string that contains the last error
Definition context.h:318
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.
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:496
namespace for compile time type information
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:367
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:709
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
the cgv namespace
Definition print.h:11
holds options applied before and during shader compilation, such as preprocessor defines and code sni...
Definition shader_code.h:87
std::vector< shader_code_snippet > snippets
shader code snippets
Definition shader_code.h:91
shader_define_map defines
map of pre-processor define names to values
Definition shader_code.h:89
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