aboutsummaryrefslogtreecommitdiff
path: root/src/clspv_helper.cc
blob: f4c48c97eb70a3108ee1e1b60806c4b2101760f8 (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
// 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/clspv_helper.h"

#include <utility>

#include "clspv/ArgKind.h"
#include "clspv/Compiler.h"

namespace amber {
namespace clspvhelper {

Result Compile(Pipeline::ShaderInfo* shader_info,
               std::vector<uint32_t>* generated_binary) {
  std::vector<clspv::version0::DescriptorMapEntry> entries;
  const auto& src_str = shader_info->GetShader()->GetData();
  std::string options;
  for (const auto& option : shader_info->GetCompileOptions()) {
    options += option + " ";
  }
  if (clspv::CompileFromSourceString(src_str, /* sampler map */ "", options,
                                     generated_binary, &entries)) {
    return Result("Clspv compile failed");
  }

  for (auto& entry : entries) {
    if (entry.kind != clspv::version0::DescriptorMapEntry::KernelArg) {
      return Result(
          "Only kernel argument descriptor entries are currently supported");
    }

    Pipeline::ShaderInfo::DescriptorMapEntry descriptor_entry;
    descriptor_entry.descriptor_set = entry.descriptor_set;
    descriptor_entry.binding = entry.binding;
    descriptor_entry.pod_offset = 0;
    descriptor_entry.pod_arg_size = 0;
    switch (entry.kernel_arg_data.arg_kind) {
      case clspv::ArgKind::Buffer:
        descriptor_entry.kind =
            Pipeline::ShaderInfo::DescriptorMapEntry::Kind::SSBO;
        break;
      case clspv::ArgKind::BufferUBO:
        descriptor_entry.kind =
            Pipeline::ShaderInfo::DescriptorMapEntry::Kind::UBO;
        break;
      case clspv::ArgKind::Pod:
        descriptor_entry.kind =
            Pipeline::ShaderInfo::DescriptorMapEntry::Kind::POD;
        break;
      case clspv::ArgKind::PodUBO:
        descriptor_entry.kind =
            Pipeline::ShaderInfo::DescriptorMapEntry::Kind::POD_UBO;
        break;
      case clspv::ArgKind::ReadOnlyImage:
        descriptor_entry.kind =
            Pipeline::ShaderInfo::DescriptorMapEntry::Kind::RO_IMAGE;
        break;
      case clspv::ArgKind::WriteOnlyImage:
        descriptor_entry.kind =
            Pipeline::ShaderInfo::DescriptorMapEntry::Kind::WO_IMAGE;
        break;
      case clspv::ArgKind::Sampler:
        descriptor_entry.kind =
            Pipeline::ShaderInfo::DescriptorMapEntry::Kind::SAMPLER;
        break;
      case clspv::ArgKind::Local:
        // Local arguments are handled via specialization constants.
        break;
      default:
        return Result("Unsupported kernel argument descriptor entry");
    }

    if (entry.kernel_arg_data.arg_kind == clspv::ArgKind::Pod ||
        entry.kernel_arg_data.arg_kind == clspv::ArgKind::PodUBO) {
      descriptor_entry.pod_offset = entry.kernel_arg_data.pod_offset;
      descriptor_entry.pod_arg_size = entry.kernel_arg_data.pod_arg_size;
    }

    descriptor_entry.arg_name = entry.kernel_arg_data.arg_name;
    descriptor_entry.arg_ordinal = entry.kernel_arg_data.arg_ordinal;

    shader_info->AddDescriptorEntry(entry.kernel_arg_data.kernel_name,
                                    std::move(descriptor_entry));
  }

  return Result();
}

}  // namespace clspvhelper
}  // namespace amber