aboutsummaryrefslogtreecommitdiff
path: root/src/vulkan/buffer_backed_descriptor.cc
blob: 2f7aa988c0ea4394b53c4be4ad9e872f500422a5 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2019 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/vulkan/buffer_backed_descriptor.h"

#include <cstring>

#include "src/vulkan/command_buffer.h"
#include "src/vulkan/device.h"

namespace amber {
namespace vulkan {

BufferBackedDescriptor::BufferBackedDescriptor(Buffer* buffer,
                                               DescriptorType type,
                                               Device* device,
                                               uint32_t desc_set,
                                               uint32_t binding)
    : Descriptor(type, device, desc_set, binding) {
  AddAmberBuffer(buffer);
}

BufferBackedDescriptor::~BufferBackedDescriptor() = default;

Result BufferBackedDescriptor::RecordCopyDataToResourceIfNeeded(
    CommandBuffer* command) {
  auto resources = GetResources();
  auto buffers = GetAmberBuffers();
  if (resources.size() != buffers.size())
    return Result(
        "Vulkan: BufferBackedDescriptor::RecordCopyDataToResourceIfNeeded() "
        "resource and buffer vector sizes are not matching");

  for (size_t i = 0; i < resources.size(); i++) {
    if (!buffers[i]->ValuePtr()->empty()) {
      resources[i]->UpdateMemoryWithRawData(*buffers[i]->ValuePtr());
      // If the resource is read-only, keep the buffer data; Amber won't copy
      // read-only resources back into the host buffers, so it makes sense to
      // leave the buffer intact.
      if (!IsReadOnly())
        buffers[i]->ValuePtr()->clear();
    }

    resources[i]->CopyToDevice(command);
  }

  return {};
}

Result BufferBackedDescriptor::RecordCopyDataToHost(CommandBuffer* command) {
  if (!IsReadOnly()) {
    if (GetResources().empty()) {
      return Result(
          "Vulkan: BufferBackedDescriptor::RecordCopyDataToHost() no transfer "
          "resources");
    }

    for (const auto& r : GetResources())
      r->CopyToHost(command);
  }

  return {};
}

Result BufferBackedDescriptor::MoveResourceToBufferOutput() {
  // No need to copy results of read only resources.
  if (IsReadOnly())
    return {};

  auto resources = GetResources();
  auto buffers = GetAmberBuffers();
  if (resources.size() != buffers.size())
    return Result(
        "Vulkan: BufferBackedDescriptor::MoveResourceToBufferOutput() resource "
        "and buffer vector sizes are not matching");

  if (resources.empty()) {
    return Result(
        "Vulkan: BufferBackedDescriptor::MoveResourceToBufferOutput() no "
        "transfer resource");
  }

  for (size_t i = 0; i < resources.size(); i++) {
    void* resource_memory_ptr = resources[i]->HostAccessibleMemoryPtr();
    if (!resource_memory_ptr) {
      return Result(
          "Vulkan: BufferBackedDescriptor::MoveResourceToBufferOutput() "
          "no host accessible memory pointer");
    }

    if (!buffers[i]->ValuePtr()->empty()) {
      return Result(
          "Vulkan: BufferBackedDescriptor::MoveResourceToBufferOutput() "
          "output buffer is not empty");
    }

    auto size_in_bytes = resources[i]->GetSizeInBytes();
    buffers[i]->SetElementCount(size_in_bytes /
                                buffers[i]->GetFormat()->SizeInBytes());
    buffers[i]->ValuePtr()->resize(size_in_bytes);
    std::memcpy(buffers[i]->ValuePtr()->data(), resource_memory_ptr,
                size_in_bytes);
  }

  return {};
}

bool BufferBackedDescriptor::IsReadOnly() const {
  switch (type_) {
    case DescriptorType::kUniformBuffer:
    case DescriptorType::kUniformBufferDynamic:
    case DescriptorType::kUniformTexelBuffer:
    case DescriptorType::kSampledImage:
    case DescriptorType::kCombinedImageSampler:
      return true;
    case DescriptorType::kStorageBuffer:
    case DescriptorType::kStorageBufferDynamic:
    case DescriptorType::kStorageTexelBuffer:
    case DescriptorType::kStorageImage:
      return false;
    default:
      assert(false && "Unexpected descriptor type");
      return false;
  }
}

}  // namespace vulkan
}  // namespace amber