20using namespace shader::parser;
21using namespace shader::parser::ast;
33void SourceProcessor::lower_srt_accessor_templates(
Parser &
parser)
44 if (attributes[1].
str() !=
"resource_table") {
45 if (type.str() ==
"srt_t") {
47 "The srt_t<T> template is only to be used with members declared with the "
48 "[[resource_table]] attribute.");
53 if (type.str() !=
"srt_t") {
55 "Members declared with the [[resource_table]] attribute must wrap their type "
56 "with the srt_t<T> template.");
59 if (array.is_valid()) {
60 report_error(name,
"[[resource_table]] members cannot be arrays.");
62 if (name.prev() ==
'&') {
63 report_error(name.prev(),
"[[resource_table]] members cannot be references.");
68 if (template_scope.is_valid()) {
69 parser.erase(template_scope.front());
70 parser.erase(template_scope.back());
74 parser.apply_mutations();
86void SourceProcessor::lower_srt_accessor_templates_ast(
Parser &
parser)
88 for (ClassDecl decl : parser.root().descendants_of_type<ClassDecl>()) {
89 for (VarDecl var : decl.body().children_of_type<VarDecl>()) {
91 bool is_resource_table = var.attributes().contains_attr(
"resource_table");
92 bool is_srt = type.identifier().name().str() ==
"srt_t";
97 if (!is_resource_table) {
99 "The srt_t<T> template is only to be used with members declared with the "
100 "[[resource_table]] attribute.");
104 for (Declarator decl : var.children_of_type<Declarator>()) {
105 if (decl.is_array()) {
106 report_error(decl,
"[[resource_table]] members cannot be arrays.");
108 if (decl.is_reference()) {
109 report_error(decl,
"[[resource_table]] members cannot be references.");
114 parser.erase(type.identifier().name());
116 if (list.is_valid()) {
117 parser.erase(list.front());
118 parser.erase(list.back());
123 parser.apply_mutations();
128void SourceProcessor::lower_srt_member_access(
Parser &
parser)
130 const string srt_attribute =
"resource_table";
137 const bool is_func_prototype_decl = body_scope.is_invalid();
138 const bool is_local_reference =
attribute.scope().type() != ScopeType::FunctionArgs &&
139 attribute.scope().type() != ScopeType::FunctionArg;
141 if (is_local_reference || is_func_prototype_decl) {
148 if (!is_local_reference && var.prev() ==
'&') {
149 parser.erase(var.prev());
152 string srt_type(type.str());
153 string srt_var(var.str());
155 body_scope.foreach_match(
"A.A", [&](
const vector<Token> toks) {
156 if (toks[0].
str() != srt_var || toks[0].
prev() ==
'.') {
160 toks[0], toks[2],
"srt_access(" + srt_type +
", " +
string(toks[2].
str()) +
")",
true);
164 parser().foreach_scope(ScopeType::FunctionArgs, [&](
const Scope fn_args) {
166 Scope fn_body = fn_args.
next().
type() == ScopeType::Function ? fn_args.next() :
Scope(parser);
168 fn_args.foreach_match(
"[[..]]c?A&A", [&](
const vector<Token> toks) {
169 memher_access_mutation(toks[0].scope(), toks[8], toks[10], fn_body);
171 fn_args.foreach_match(
"[[..]]c?AA", [&](
const vector<Token> toks) {
172 if (toks[1].next().
str() == srt_attribute) {
173 parser.erase(toks[0].scope());
174 report_error(toks[9],
"Shader Resource Table arguments must be references.");
179 parser().foreach_scope(ScopeType::Function, [&](
const Scope fn_body) {
181 fn_body.foreach_match(
"[[..]]c?A&A", [&](
const vector<Token> toks) {
182 memher_access_mutation(toks[0].scope(), toks[8], toks[10], toks[10].scope());
185 fn_body.foreach_match(
"[[..]]c?AA", [&](
const vector<Token> toks) {
186 memher_access_mutation(toks[0].scope(), toks[8], toks[9], toks[9].scope());
190 parser.apply_mutations();
195void SourceProcessor::lower_srt_arguments(
Parser &
parser)
200 fn_args.foreach_match(
"[[..]]c?A", [&](
const vector<Token> &tokens) {
201 if (tokens[1].next().
str() !=
"resource_table") {
205 tokens[1].scope().foreach_attribute([&](
Token attribute_name,
Scope attribute_parameters) {
206 if (attribute_name.str() ==
"condition") {
207 srt_cond =
"SRT_CONSTANT_" + string(attribute_parameters.str_exclusive());
212 if (!srt_cond.empty()) {
216 condition +=
"defined(CREATE_INFO_" + string(tokens[8].
str()) +
")";
217 if (!srt_cond.empty()) {
220 parser.replace(tokens[0].scope(),
"");
225 Token first_tok = fn_type.
prev() ==
']' ? fn_type.
prev().scope().front() : fn_type;
226 parser.insert_directive(first_tok.prev(),
"#if " +
condition.substr(4));
227 parser.insert_directive(fn_body.back(),
"#endif");
231 parser.apply_mutations();
235void SourceProcessor::lower_resource_access_functions(
Parser &
parser)
239 fn_body.foreach_match(
"A(", [&](
const vector<Token> &tokens) {
240 string_view func_name = tokens[0].str();
241 if (func_name !=
"specialization_constant_get" && func_name !=
"shared_variable_get" &&
242 func_name !=
"push_constant_get" && func_name !=
"interface_get" &&
243 func_name !=
"resource_table_get" && func_name !=
"attribute_get" &&
244 func_name !=
"buffer_get" && func_name !=
"sampler_get" && func_name !=
"image_get")
249 if (tokens[1].next() !=
Word) {
250 report_error(tokens[1].next(),
"Expecting symbol name");
254 string info_name(tokens[1].next().
str());
255 Scope scope = tokens[0].scope();
257 while (scope.type() != ScopeType::Function && scope.type() != ScopeType::Local) {
258 scope = scope.scope();
261 string condition =
"defined(CREATE_INFO_" + info_name +
")";
263 if (scope.type() == ScopeType::Function) {
272 parser.apply_mutations();
293 parser().foreach_match(
"un", [&](
const vector<Token> &tokens) {
294 report_error(tokens[0],
295 "Unsupported `using namespace`. "
296 "Add individual `using` directives for each needed symbol.");
299 auto process_using = [&](
const Token &using_tok,
301 const Token &to_start,
303 const Token &end_tok) {
304 string to = parser.substr_range_inclusive(to_start, to_end);
305 string namespace_prefix = parser.substr_range_inclusive(to_start, to_end.prev().prev().prev());
309 if (scope.type() == ScopeType::Global) {
310 report_error(using_tok,
"The `using` keyword is not allowed in global scope.");
313 if (scope.type() == ScopeType::Namespace) {
316 string namespace_name = scope.front().prev().full_symbol_name();
317 if (namespace_name != namespace_prefix) {
320 "The `using` keyword is only allowed in namespace scope to make visible symbols "
321 "from the same namespace declared in another scope, potentially from another "
328 const bool use_alias = from.
str() != to_end.str();
329 const bool replace_fn = !use_alias;
335 scope.foreach_token(
Word, [&](
const Token &token) {
337 if (token.index_ <= to_end.index_) {
341 if (token.prev() ==
':') {
344 if (!replace_fn && token.next() ==
'(') {
347 if (token.str() != from.str()) {
350 parser.replace(token, to,
true);
353 parser.erase(using_tok, end_tok);
356 parser().foreach_match(
"uA::A", [&](
const vector<Token> &tokens) {
358 process_using(tokens[0], end.prev(), tokens[1], end.prev(), end);
361 parser().foreach_match(
"uA=A::A", [&](
const vector<Token> &tokens) {
363 process_using(tokens[0], tokens[1], tokens[3], end.prev(), end);
366 parser.apply_mutations();
369 parser().foreach_token(
Using, [&](
const Token &token) {
370 report_error(token,
"Unsupported `using` keyword usage.");
375void SourceProcessor::lower_resource_table(
Parser &
parser)
390 Scope array_end = array;
391 while (array_end.next().type() == ScopeType::Subscript) {
392 array_end = array_end.next();
394 string_view array_str;
395 if (array.is_valid()) {
396 array_str = parser.substr(array.front(), array_end.back(),
true);
399 metadata::ParsedResource resource{
400 type.line_number(), string(type.str()), string(name.str()), string(array_str)};
403 if (type ==
"sampler") {
404 resource.res_type = type;
407 else if (type ==
"image") {
408 resource.res_type = type;
410 resource.res_qualifier =
attribute[4].str();
411 resource.res_format =
attribute[6].str();
413 else if (type ==
"uniform") {
414 resource.res_type = type;
417 else if (type ==
"storage") {
418 resource.res_type = type;
420 resource.res_qualifier =
attribute[4].str();
422 else if (type ==
"acceleration_structure") {
423 resource.res_type = type;
425 resource.res_qualifier =
attribute[4].str();
427 else if (type ==
"shared") {
428 resource.res_type = type;
430 else if (type ==
"push_constant") {
431 resource.res_type = type;
433 else if (type ==
"compilation_constant") {
434 resource.res_type = type;
436 else if (type ==
"specialization_constant") {
437 resource.res_type = type;
438 resource.res_value =
attribute[1].scope().str_exclusive();
440 else if (type ==
"condition") {
442 resource.res_condition +=
"int " + string(tok.str()) +
" = ";
443 resource.res_condition +=
"ShaderCreateInfo::find_constant(constants, \"" +
444 string(tok.str()) +
"\"); ";
446 resource.res_condition +=
"return " + string(
attribute[1].scope().
str()) +
";";
448 else if (type ==
"frequency") {
449 resource.res_frequency =
attribute[2].str();
451 else if (type ==
"resource_table") {
452 resource.res_type = type;
454 else if (type ==
"legacy_info") {
455 resource.res_type = type;
457 else if (type ==
"legacy_iface") {
458 resource.res_type = type;
461 report_error(
attribute[0],
"Invalid attribute in resource table");
468 if (array.is_valid()) {
469 report_error(array[0],
"Array are not supported as vertex attributes");
472 metadata::ParsedVertInput vert_in{type.line_number(), string(type.str()), string(name.str())};
474 if (vert_in.var_type ==
"float3x3" || vert_in.var_type ==
"float2x2" ||
475 vert_in.var_type ==
"float4x4" || vert_in.var_type ==
"float3x4")
477 report_error(name,
"Matrices are not supported as vertex attributes");
480 attributes.foreach_scope(ScopeType::Attribute, [&](
const Scope &
attribute) {
482 if (type ==
"attribute") {
485 else if (type ==
"condition") {
487 vert_in.res_condition +=
"int " + string(tok.str()) +
" = ";
488 vert_in.res_condition +=
"ShaderCreateInfo::find_constant(constants, \"" +
489 string(tok.str()) +
"\"); ";
491 vert_in.res_condition +=
"return " + string(
attribute[1].scope().
str()) +
";";
494 report_error(
attribute[0],
"Invalid attribute in vertex input interface");
500 auto parse_vertex_output =
502 if (array.is_valid()) {
503 report_error(array[0],
"Array are not supported in stage interface");
506 Token interpolation_mode = attributes[1];
508 metadata::ParsedAttribute attr{type.line_number(),
510 string(struct_name.str()) +
"_" + string(name.str()),
511 string(interpolation_mode.str())};
513 if (attr.var_type ==
"float3x3" || attr.var_type ==
"float2x2" ||
514 attr.var_type ==
"float4x4" || attr.var_type ==
"float3x4")
516 report_error(name,
"Matrices are not supported in stage interface");
519 if (attr.interpolation_mode !=
"smooth" && attr.interpolation_mode !=
"flat" &&
520 attr.interpolation_mode !=
"no_perspective")
522 report_error(attributes[0],
"Invalid attribute in shader stage interface");
527 auto parse_fragment_output =
529 metadata::ParsedFragOuput frag_out{tok_type.line_number(),
530 string(tok_type.str()),
531 string(struct_name.str()) +
"_" + string(name.str())};
533 attributes.foreach_scope(ScopeType::Attribute, [&](
const Scope &
attribute) {
535 if (type ==
"frag_color") {
538 else if (type ==
"raster_order_group") {
539 frag_out.raster_order_group =
attribute[2].str();
541 else if (type ==
"index") {
542 frag_out.dual_source =
attribute[2].str();
545 report_error(attributes[0],
"Invalid attribute in fragment output interface");
551 auto parse_fragment_input =
553 metadata::ParsedFragInput frag_in{tok_type.line_number(),
554 string(tok_type.str()),
555 string(struct_name.str()) +
"_" + string(name.str())};
557 attributes.foreach_scope(ScopeType::Attribute, [&](
const Scope &
attribute) {
559 if (type ==
"subpass_input") {
563 else if (type ==
"raster_order_group") {
564 frag_in.raster_order_group =
attribute[2].str();
567 report_error(attributes[0],
"Invalid attribute in fragment output interface");
573 auto is_resource_table_attribute = [](
Token attr) {
574 string_view type = attr.str();
575 return (type ==
"sampler" || type ==
"image" || type ==
"uniform" || type ==
"storage" ||
576 type ==
"acceleration_structure" || type ==
"shared" || type ==
"push_constant" ||
577 type ==
"compilation_constant" || type ==
"specialization_constant" ||
578 type ==
"legacy_info" || type ==
"legacy_iface" || type ==
"resource_table");
580 auto is_vertex_input_attribute = [](
Token attr) {
581 string_view type = attr.str();
582 return (type ==
"attribute");
584 auto is_vertex_output_attribute = [](
Token attr) {
585 string_view type = attr.str();
586 return (type ==
"flat" || type ==
"smooth" || type ==
"no_perspective");
588 auto is_fragment_output_attribute = [](
Token attr) {
589 string_view type = attr.str();
590 return (type ==
"frag_color" || type ==
"frag_depth" || type ==
"frag_stencil_ref");
592 auto is_fragment_input_attribute = [](
Token attr) {
593 string_view type = attr.str();
594 return (type ==
"subpass_input");
598 SrtType srt_type = SrtType::undefined;
599 bool has_srt_members =
false;
601 metadata::ResourceTable srt;
602 metadata::VertexInputs vertex_in;
603 metadata::StageInterface vertex_out;
604 metadata::FragmentOutputs fragment_out;
605 metadata::FragmentInputs fragment_in;
606 srt.name = struct_name.str();
607 vertex_in.name = struct_name.str();
608 vertex_out.name = struct_name.str();
609 fragment_out.name = struct_name.str();
610 fragment_in.name = struct_name.str();
612 body.foreach_declaration([&](
Scope attributes,
619 SrtType decl_type = SrtType::undefined;
620 if (attributes.is_invalid()) {
621 decl_type = SrtType::none;
623 else if (is_resource_table_attribute(attributes[1])) {
624 decl_type = SrtType::resource_table;
626 else if (is_vertex_input_attribute(attributes[1])) {
627 decl_type = SrtType::vertex_input;
629 else if (is_vertex_output_attribute(attributes[1])) {
630 decl_type = SrtType::vertex_output;
632 else if (is_fragment_output_attribute(attributes[1])) {
633 decl_type = SrtType::fragment_output;
635 else if (is_fragment_input_attribute(attributes[1])) {
636 decl_type = SrtType::fragment_input;
642 if (srt_type == SrtType::undefined) {
643 srt_type = decl_type;
645 else if (srt_type != decl_type) {
647 case SrtType::resource_table:
648 report_error(struct_name,
"Structure expected to contain resources...");
650 case SrtType::vertex_input:
651 report_error(struct_name,
"Structure expected to contain vertex inputs...");
653 case SrtType::vertex_output:
654 report_error(struct_name,
"Structure expected to contain vertex outputs...");
656 case SrtType::fragment_output:
657 report_error(struct_name,
"Structure expected to contain fragment outputs...");
659 case SrtType::fragment_input:
660 report_error(struct_name,
"Structure expected to contain fragment inputs...");
663 report_error(struct_name,
"Structure expected to contain plain data...");
665 case SrtType::undefined:
670 case SrtType::resource_table:
671 report_error(attributes[1],
"...but member declared as resource.");
673 case SrtType::vertex_input:
674 report_error(attributes[1],
"...but member declared as vertex input.");
676 case SrtType::vertex_output:
677 report_error(attributes[1],
"...but member declared as vertex output.");
679 case SrtType::fragment_output:
680 report_error(attributes[1],
"...but member declared as fragment output.");
682 case SrtType::fragment_input:
683 report_error(attributes[1],
"...but member declared as fragment input.");
686 report_error(name,
"...but member declared as plain data.");
688 case SrtType::undefined:
694 case SrtType::resource_table:
695 srt.emplace_back(parse_resource(attributes, type, name, array));
696 if (attributes[1].
str() ==
"resource_table") {
697 has_srt_members =
true;
698 parser.erase(attributes.scope());
699 parser.erase(const_tok);
702 parser.erase(attributes.front().line_start(), decl_end.line_end());
705 case SrtType::vertex_input:
706 vertex_in.emplace_back(parse_vertex_input(attributes, type, name, array));
707 parser.erase(attributes.scope());
709 case SrtType::vertex_output:
710 vertex_out.emplace_back(parse_vertex_output(struct_name, attributes, type, name, array));
711 parser.erase(attributes.scope());
713 case SrtType::fragment_output:
714 fragment_out.emplace_back(
715 parse_fragment_output(struct_name, attributes, type, name, array));
716 parser.erase(attributes.scope());
718 case SrtType::fragment_input:
719 fragment_in.emplace_back(
720 parse_fragment_input(struct_name, attributes, type, name, array));
721 parser.erase(attributes.scope());
723 case SrtType::undefined:
730 case SrtType::resource_table:
731 metadata_.resource_tables.emplace_back(srt);
733 case SrtType::vertex_input:
734 metadata_.vertex_inputs.emplace_back(vertex_in);
736 case SrtType::vertex_output:
737 metadata_.stage_interfaces.emplace_back(vertex_out);
739 case SrtType::fragment_output:
740 metadata_.fragment_outputs.emplace_back(fragment_out);
742 case SrtType::fragment_input:
743 metadata_.fragment_inputs.emplace_back(fragment_in);
745 case SrtType::undefined:
752 if (srt_type == SrtType::resource_table) {
756 ctor +=
"\nstatic " + srt.name +
" new_()\n";
758 ctor +=
" " + srt.name +
" result;\n";
759 if (has_srt_members ==
false) {
760 ctor +=
" result._pad = 0;\n";
762 for (
const auto &member : srt) {
763 if (member.res_type ==
"resource_table") {
764 ctor +=
" result." + member.var_name +
" = " + member.var_type +
"::new_();\n";
767 ctor +=
" return result;\n";
769 ctor +=
"#line " +
to_string(end_of_srt.line_number()) +
"\n";
771 parser.insert_after(end_of_srt, ctor);
774 string access_macros;
775 for (
const auto &member : srt) {
776 if (member.res_type ==
"resource_table") {
777 access_macros +=
"#define access_" + srt.name +
"_" + member.var_name +
"() ";
778 access_macros += member.var_type +
"_ctor_()\n";
781 access_macros +=
"#define access_" + srt.name +
"_" + member.var_name +
"() ";
782 access_macros += member.var_name +
"\n";
785 parser.insert_before(struct_tok, access_macros);
788 parser.insert_before(struct_tok,
"\n");
789 parser.insert_line_number(struct_tok.str_index_start() - 1, struct_tok.line_number());
794 parser.insert_after(struct_tok,
"[[resource_table]] ");
797 parser.insert_before(struct_tok,
"[[resource_table]] ");
802 parser.parse(error_handler);
805void SourceProcessor::lower_resource_macro_placeholder_ast(
Parser &
parser)
807 auto split_into_pairs = [](
const string &
str) {
808 vector<pair<string, string>>
result;
810 auto words_view =
str | std::views::split(
' ');
812 string first_of_pair;
813 for (
auto &&word_range : words_view) {
814 string word(word_range.begin(), word_range.end());
815 if (first_of_pair.empty()) {
816 first_of_pair = std::move(word);
819 result.emplace_back(std::move(first_of_pair), std::move(word));
825 for (Preprocessor directive : parser.root().descendants_of_type<Preprocessor>()) {
826 string_view dir_str = directive.str();
827 if (dir_str.starts_with(
"#pragma resource_access ")) {
828 auto pairs = split_into_pairs(
string(dir_str));
829 for (
auto pair : pairs) {
830 if (pair.first !=
"#pragma") {
831 parser.insert_before(directive.front(),
832 "#define " + pair.first +
" " + pair.second +
"\n");
835 parser.erase(directive);
837 else if (dir_str.starts_with(
"#pragma resource_defines ")) {
838 string_view str_name = directive.back().str();
static std::string get_create_info_placeholder(const std::string &name)
static void guarded_scope_mutation(Parser &parser, Scope scope, const std::string &condition, Token fn_type)
const char * to_string(ShaderStage stage)
SymEdge< T > * prev(const SymEdge< T > *se)
void foreach_declaration(Callback callback) const
std::string_view str() const
void foreach_scope(ScopeType type, CallbackFn callback) const
Token prev(int i=1) const
Token(const TokenBuffer *buf, int32_t index)