1 // Copyright 2018 The Amber Authors. 2 // Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #ifndef SRC_AMBERSCRIPT_PARSER_H_ 17 #define SRC_AMBERSCRIPT_PARSER_H_ 18 19 #include <memory> 20 #include <string> 21 #include <utility> 22 #include <vector> 23 24 #include "amber/result.h" 25 #include "src/parser.h" 26 #include "src/script.h" 27 28 namespace amber { 29 30 class Tokenizer; 31 class Token; 32 33 namespace amberscript { 34 35 /// Parser for the `AmberScript` format. 36 class Parser : public amber::Parser { 37 public: 38 Parser(); 39 explicit Parser(Delegate* delegate); 40 ~Parser() override; 41 42 // amber::Parser 43 Result Parse(const std::string& data) override; 44 45 private: 46 std::string make_error(const std::string& err); 47 Result ToShaderType(const std::string& str, ShaderType* type); 48 Result ToBufferType(const std::string& str, BufferType* type); 49 Result ToShaderFormat(const std::string& str, ShaderFormat* fmt); 50 Result ToPipelineType(const std::string& str, PipelineType* type); 51 Result ValidateEndOfStatement(const std::string& name); 52 53 Result ParseStruct(); 54 Result ParseBuffer(); 55 Result ParseImage(); 56 Result ParseBufferInitializer(Buffer*); 57 Result ParseBufferInitializerSize(Buffer*); 58 Result ParseBufferInitializerFill(Buffer*, uint32_t); 59 Result ParseBufferInitializerSeries(Buffer*, uint32_t); 60 Result ParseBufferInitializerData(Buffer*); 61 Result ParseBufferInitializerFile(Buffer*); 62 Result ParseShaderBlock(); 63 Result ParsePipelineBlock(); 64 Result ParsePipelineAttach(Pipeline*); 65 Result ParsePipelineShaderOptimizations(Pipeline*); 66 Result ParsePipelineShaderCompileOptions(Pipeline*); 67 Result ParsePipelineSubgroup(Pipeline* pipeline); 68 Result ParsePipelinePatchControlPoints(Pipeline* pipeline); 69 Result ParsePipelineFramebufferSize(Pipeline*); 70 Result ParsePipelineViewport(Pipeline*); 71 Result ParsePipelineBind(Pipeline*); 72 Result ParsePipelineVertexData(Pipeline*); 73 Result ParsePipelineIndexData(Pipeline*); 74 Result ParsePipelineSet(Pipeline*); 75 Result ParsePipelinePolygonMode(Pipeline*); 76 Result ParsePipelineDepth(Pipeline* pipeline); 77 Result ParsePipelineStencil(Pipeline* pipeline); 78 Result ParsePipelineBlend(Pipeline* pipeline); 79 Result ParsePipelineShaderGroup(Pipeline* pipeline); 80 Result ParseRun(); 81 Result ParseClear(); 82 Result ParseClearColor(); 83 Result ParseClearDepth(); 84 Result ParseClearStencil(); 85 Result ParseExpect(); 86 Result ParseCopy(); 87 Result ParseDeviceFeature(); 88 Result ParseDeviceExtension(); 89 Result ParseDeviceProperty(); 90 Result ParseInstanceExtension(); 91 Result ParseRepeat(); 92 Result ParseSet(); 93 bool IsRepeatable(const std::string& name) const; 94 Result ParseRepeatableCommand(const std::string& name); 95 Result ParseDerivePipelineBlock(); 96 Result ParsePipelineBody(const std::string& cmd_name, 97 std::unique_ptr<Pipeline> pipeline); 98 Result ParseShaderSpecialization(Pipeline* pipeline); 99 Result ParseSampler(); 100 bool IsRayTracingShader(ShaderType type); 101 Result ParseAS(); 102 Result ParseBLAS(); 103 Result ParseBLASTriangle(BLAS* blas); 104 Result ParseBLASAABB(BLAS* blas); 105 Result ParseGeometryFlags(uint32_t* flags); 106 Result ParseTLAS(); 107 Result ParseBLASInstance(TLAS* tlas); 108 Result ParseBLASInstanceTransform(BLASInstance* instance); 109 Result ParseBLASInstanceFlags(BLASInstance* instance); 110 Result ParseSBT(Pipeline* pipeline); 111 Result ParseMaxRayPayloadSize(Pipeline* pipeline); 112 Result ParseMaxRayHitAttributeSize(Pipeline* pipeline); 113 Result ParseMaxRayRecursionDepth(Pipeline* pipeline); 114 Result ParseFlags(Pipeline* pipeline); 115 Result ParseUseLibrary(Pipeline* pipeline); 116 Result ParseTolerances(std::vector<Probe::Tolerance>* tolerances); 117 118 /// Parses a set of values out of the token stream. |name| is the name of the 119 /// current command we're parsing for error purposes. The |type| is the type 120 /// of data we expect for the current buffer. |values| will be appended to 121 /// with the parsed values. 122 Result ParseValues(const std::string& name, 123 Format* fmt, 124 std::vector<Value>* values); 125 126 Result ParseVirtualFile(); 127 128 std::unique_ptr<Tokenizer> tokenizer_; 129 std::vector<std::unique_ptr<Command>> command_list_; 130 }; 131 132 } // namespace amberscript 133 } // namespace amber 134 135 #endif // SRC_AMBERSCRIPT_PARSER_H_ 136