aboutsummaryrefslogtreecommitdiff
path: root/src/format.cc
blob: de2ef06311a21d826f5727c6c16a9e10850b48a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2018 The Amber Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/format.h"

#include "src/make_unique.h"

namespace amber {

Format::Format() = default;

Format::Format(const Format&) = default;

Format::~Format() = default;

uint32_t Format::SizeInBytesPerRow() const {
  uint32_t bits = 0;
  for (const auto& comp : components_)
    bits += comp.num_bits;

  uint32_t inflate = 0;
  // Std140 always has 4 elements. std430 expands 3 elements to 4.
  if ((is_std140_ && column_count_ > 1) || components_.size() == 3)
    inflate = 4U - static_cast<uint32_t>(components_.size());

  for (uint32_t i = 0; i < inflate; ++i)
    bits += components_[0].num_bits;

  uint32_t bytes_per_element = bits / 8;
  // Odd number of bits, inflate byte count to accommodate
  if ((bits % 8) != 0)
    bytes_per_element += 1;

  return bytes_per_element;
}

uint32_t Format::SizeInBytes() const {
  return SizeInBytesPerRow() * column_count_;
}

bool Format::AreAllComponents(FormatMode mode, uint32_t bits) const {
  for (const auto& comp : components_) {
    if (comp.mode != mode || comp.num_bits != bits)
      return false;
  }
  return true;
}

bool Format::Equal(const Format* b) const {
  if (type_ != b->type_ || is_std140_ != b->is_std140_ ||
      pack_size_in_bytes_ != b->pack_size_in_bytes_ ||
      column_count_ != b->column_count_) {
    return false;
  }
  if (components_.size() != b->components_.size())
    return false;

  for (uint32_t i = 0; i < components_.size(); ++i) {
    if (components_[i].type != b->components_[i].type ||
        components_[i].mode != b->components_[i].mode ||
        components_[i].num_bits != b->components_[i].num_bits) {
      return false;
    }
  }
  return true;
}

}  // namespace amber