1 // Copyright 2019 The Amber Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_TYPE_PARSER_H_ 16 #define SRC_TYPE_PARSER_H_ 17 18 #include <memory> 19 #include <string> 20 #include <vector> 21 22 #include "src/type.h" 23 24 namespace amber { 25 26 /// Parses a Vulkan image string into a type object. 27 class TypeParser { 28 public: 29 static FormatType NameToFormatType(const std::string& data); 30 31 TypeParser(); 32 ~TypeParser(); 33 34 /// Parses the |fmt| string and returns the associated type, nullptr if the 35 /// conversion is not possible. 36 /// 37 /// The format string can come in two different forms, it can be a vulkan 38 /// style format string (e.g. R32G32B32A32_SFLOAT) or it can be in the type 39 /// format (gl_type/glsl_type) specified by VkScript (e.g. byte/vec4). 40 std::unique_ptr<type::Type> Parse(const std::string& fmt); 41 42 private: 43 std::unique_ptr<type::Type> ParseGlslFormat(const std::string& fmt); 44 void ProcessChunk(const std::string&); 45 void AddPiece(FormatComponentType type, FormatMode mode, uint8_t bits); 46 void FlushPieces(type::Type* type, FormatMode mode); 47 48 struct Pieces { PiecesPieces49 Pieces(FormatComponentType t, FormatMode m, uint8_t bits) 50 : type(t), mode(m), num_bits(bits) {} 51 52 FormatComponentType type; 53 FormatMode mode; 54 uint8_t num_bits; 55 }; 56 57 FormatMode mode_ = FormatMode::kSInt; 58 uint32_t pack_size_ = 0; 59 std::vector<Pieces> pieces_; 60 }; 61 62 } // namespace amber 63 64 #endif // SRC_TYPE_PARSER_H_ 65