• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/descriptor_set_and_binding_parser.h"
16 
17 #include <iostream>
18 #include <limits>
19 
20 #include "src/tokenizer.h"
21 
22 namespace amber {
23 
24 DescriptorSetAndBindingParser::DescriptorSetAndBindingParser() = default;
25 
26 DescriptorSetAndBindingParser::~DescriptorSetAndBindingParser() = default;
27 
Parse(const std::string & buffer_id)28 Result DescriptorSetAndBindingParser::Parse(const std::string& buffer_id) {
29   Tokenizer t(buffer_id);
30   auto token = t.NextToken();
31   if (token->IsInteger()) {
32     if (token->AsInt32() < 0) {
33       return Result(
34           "Descriptor set and binding for a buffer must be non-negative "
35           "integer, but you gave: " +
36           token->ToOriginalString());
37     }
38 
39     uint32_t val = token->AsUint32();
40     token = t.NextToken();
41     if (token->IsEOS() || token->IsEOL()) {
42       descriptor_set_ = 0;
43       binding_ = val;
44       return {};
45     }
46 
47     descriptor_set_ = val;
48   } else {
49     descriptor_set_ = 0;
50   }
51 
52   if (!token->IsString())
53     return Result("Invalid buffer id: " + buffer_id);
54 
55   auto& str = token->AsString();
56   if (str.size() < 2 || str[0] != ':')
57     return Result("Invalid buffer id: " + buffer_id);
58 
59   auto substr = str.substr(1, str.size());
60   // Validate all characters are integers.
61   for (size_t i = 0; i < substr.size(); ++i) {
62     if (substr[i] < '0' || substr[i] > '9') {
63       return Result(
64           "Binding for a buffer must be non-negative integer, "
65           "but you gave: " +
66           substr);
67     }
68   }
69 
70   uint64_t binding_val = strtoul(substr.c_str(), nullptr, 10);
71   if (binding_val > std::numeric_limits<uint32_t>::max())
72     return Result("binding value too large in probe ssbo command: " +
73                   token->ToOriginalString());
74   if (static_cast<int32_t>(binding_val) < 0) {
75     return Result(
76         "Binding for a buffer must be non-negative integer, but you gave: " +
77         token->ToOriginalString());
78   }
79 
80   binding_ = static_cast<uint32_t>(binding_val);
81   return {};
82 }
83 
84 }  // namespace amber
85