aboutsummaryrefslogtreecommitdiff
path: root/src/vulkan/descriptor.cc
blob: 5f2380b777a02e11be78b3e52e1fa8842134b922 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
// 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/vulkan/descriptor.h"

#include <cassert>

#include "src/vulkan/device.h"

namespace amber {
namespace vulkan {

VkDescriptorType ToVkDescriptorType(DescriptorType type) {
  switch (type) {
    case DescriptorType::kStorageImage:
      return VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
    case DescriptorType::kSampler:
      return VK_DESCRIPTOR_TYPE_SAMPLER;
    case DescriptorType::kSampledImage:
      return VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
    case DescriptorType::kCombinedImageSampler:
      return VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    case DescriptorType::kUniformTexelBuffer:
      return VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
    case DescriptorType::kStorageTexelBuffer:
      return VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
    case DescriptorType::kStorageBuffer:
      return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
    case DescriptorType::kUniformBuffer:
      return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
    case DescriptorType::kDynamicUniformBuffer:
      return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
    case DescriptorType::kDynamicStorageBuffer:
      return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
    case DescriptorType::kInputAttachment:
      return VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
  }

  assert(false && "Unknown resource type");
  return VK_DESCRIPTOR_TYPE_SAMPLER;
}

Descriptor::Descriptor(DescriptorType type,
                       Device* device,
                       uint32_t desc_set,
                       uint32_t binding)
    : descriptor_set_(desc_set),
      binding_(binding),
      device_(device),
      type_(type) {}

Descriptor::~Descriptor() = default;

VkWriteDescriptorSet Descriptor::GetWriteDescriptorSet(
    VkDescriptorSet descriptor_set,
    VkDescriptorType descriptor_type) const {
  VkWriteDescriptorSet write = VkWriteDescriptorSet();
  write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  write.dstSet = descriptor_set;
  write.dstBinding = binding_;
  write.dstArrayElement = 0;
  write.descriptorCount = 1;
  write.descriptorType = descriptor_type;
  return write;
}

Result Descriptor::UpdateDescriptorSetForBuffer(
    VkDescriptorSet descriptor_set,
    VkDescriptorType descriptor_type,
    const VkDescriptorBufferInfo& buffer_info) {
  VkWriteDescriptorSet write =
      GetWriteDescriptorSet(descriptor_set, descriptor_type);
  switch (descriptor_type) {
    case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
    case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
    case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
    case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
      write.pBufferInfo = &buffer_info;
      break;
    default:
      return Result(
          "Vulkan::UpdateDescriptorSetForBuffer not descriptor based on "
          "buffer");
  }

  UpdateVkDescriptorSet(write);
  return {};
}

Result Descriptor::UpdateDescriptorSetForImage(
    VkDescriptorSet descriptor_set,
    VkDescriptorType descriptor_type,
    const VkDescriptorImageInfo& image_info) {
  VkWriteDescriptorSet write =
      GetWriteDescriptorSet(descriptor_set, descriptor_type);
  switch (descriptor_type) {
    case VK_DESCRIPTOR_TYPE_SAMPLER:
    case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
    case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
    case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
    case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
      write.pImageInfo = &image_info;
      break;
    default:
      return Result(
          "Vulkan::UpdateDescriptorSetForImage not descriptor based on image");
  }

  UpdateVkDescriptorSet(write);
  return {};
}

Result Descriptor::UpdateDescriptorSetForBufferView(
    VkDescriptorSet descriptor_set,
    VkDescriptorType descriptor_type,
    const VkBufferView& texel_view) {
  VkWriteDescriptorSet write =
      GetWriteDescriptorSet(descriptor_set, descriptor_type);
  switch (descriptor_type) {
    case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
    case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
      write.pTexelBufferView = &texel_view;
      break;
    default:
      return Result(
          "Vulkan::UpdateDescriptorSetForImage not descriptor based on buffer "
          "view");
  }

  UpdateVkDescriptorSet(write);
  return {};
}

void Descriptor::UpdateVkDescriptorSet(const VkWriteDescriptorSet& write) {
  device_->GetPtrs()->vkUpdateDescriptorSets(device_->GetDevice(), 1, &write, 0,
                                             nullptr);
  is_descriptor_set_update_needed_ = false;
}

}  // namespace vulkan
}  // namespace amber