1e5c31af7Sopenharmony_ci// Copyright 2018 The Amber Authors.
2e5c31af7Sopenharmony_ci//
3e5c31af7Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4e5c31af7Sopenharmony_ci// you may not use this file except in compliance with the License.
5e5c31af7Sopenharmony_ci// You may obtain a copy of the License at
6e5c31af7Sopenharmony_ci//
7e5c31af7Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8e5c31af7Sopenharmony_ci//
9e5c31af7Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10e5c31af7Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11e5c31af7Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e5c31af7Sopenharmony_ci// See the License for the specific language governing permissions and
13e5c31af7Sopenharmony_ci// limitations under the License.
14e5c31af7Sopenharmony_ci
15e5c31af7Sopenharmony_ci#ifndef SRC_SHADER_COMPILER_H_
16e5c31af7Sopenharmony_ci#define SRC_SHADER_COMPILER_H_
17e5c31af7Sopenharmony_ci
18e5c31af7Sopenharmony_ci#include <string>
19e5c31af7Sopenharmony_ci#include <utility>
20e5c31af7Sopenharmony_ci#include <vector>
21e5c31af7Sopenharmony_ci
22e5c31af7Sopenharmony_ci#include "amber/amber.h"
23e5c31af7Sopenharmony_ci#include "amber/result.h"
24e5c31af7Sopenharmony_ci#if AMBER_ENABLE_CLSPV
25e5c31af7Sopenharmony_ci#include "spirv-tools/libspirv.h"
26e5c31af7Sopenharmony_ci#endif
27e5c31af7Sopenharmony_ci#include "src/pipeline.h"
28e5c31af7Sopenharmony_ci#include "src/shader.h"
29e5c31af7Sopenharmony_ci#include "src/virtual_file_store.h"
30e5c31af7Sopenharmony_ci
31e5c31af7Sopenharmony_cinamespace amber {
32e5c31af7Sopenharmony_ci
33e5c31af7Sopenharmony_ci/// Class to wrap the compilation of shaders.
34e5c31af7Sopenharmony_ciclass ShaderCompiler {
35e5c31af7Sopenharmony_ci public:
36e5c31af7Sopenharmony_ci  ShaderCompiler();
37e5c31af7Sopenharmony_ci  ShaderCompiler(const std::string& env,
38e5c31af7Sopenharmony_ci                 bool disable_spirv_validation,
39e5c31af7Sopenharmony_ci                 VirtualFileStore* virtual_files);
40e5c31af7Sopenharmony_ci  ~ShaderCompiler();
41e5c31af7Sopenharmony_ci
42e5c31af7Sopenharmony_ci  /// Returns a result code and a compilation of the given shader.
43e5c31af7Sopenharmony_ci  /// If the shader in |shader_info| has a corresponding entry in the
44e5c31af7Sopenharmony_ci  /// |shader_map|, then the compilation result is copied from that entry.
45e5c31af7Sopenharmony_ci  /// Otherwise a compiler is invoked to produce the compilation result.
46e5c31af7Sopenharmony_ci  ///
47e5c31af7Sopenharmony_ci  /// If |shader_info| specifies shader optimizations to run and there is no
48e5c31af7Sopenharmony_ci  /// entry in |shader_map| for that shader, then the SPIRV-Tools optimizer will
49e5c31af7Sopenharmony_ci  /// be invoked to produce the shader binary.
50e5c31af7Sopenharmony_ci  ///
51e5c31af7Sopenharmony_ci  /// |pipeline| is the pipeline containing |shader_info|. The name is used to
52e5c31af7Sopenharmony_ci  /// prefix shaders used in multiple pipelines with different optimization
53e5c31af7Sopenharmony_ci  /// flags. The pipeline is used in OPENCL-C compiles to create the literal
54e5c31af7Sopenharmony_ci  /// sampler bindings.
55e5c31af7Sopenharmony_ci  std::pair<Result, std::vector<uint32_t>> Compile(
56e5c31af7Sopenharmony_ci      Pipeline* pipeline,
57e5c31af7Sopenharmony_ci      Pipeline::ShaderInfo* shader_info,
58e5c31af7Sopenharmony_ci      const ShaderMap& shader_map) const;
59e5c31af7Sopenharmony_ci
60e5c31af7Sopenharmony_ci private:
61e5c31af7Sopenharmony_ci  Result ParseHex(const std::string& data, std::vector<uint32_t>* result) const;
62e5c31af7Sopenharmony_ci  Result CompileGlsl(const Shader* shader, std::vector<uint32_t>* result) const;
63e5c31af7Sopenharmony_ci  Result CompileHlsl(const Shader* shader, std::vector<uint32_t>* result) const;
64e5c31af7Sopenharmony_ci#if AMBER_ENABLE_CLSPV
65e5c31af7Sopenharmony_ci  Result CompileOpenCLC(Pipeline::ShaderInfo* shader,
66e5c31af7Sopenharmony_ci                        Pipeline* pipeline,
67e5c31af7Sopenharmony_ci                        spv_target_env env,
68e5c31af7Sopenharmony_ci                        std::vector<uint32_t>* result) const;
69e5c31af7Sopenharmony_ci#endif
70e5c31af7Sopenharmony_ci
71e5c31af7Sopenharmony_ci  std::string spv_env_;
72e5c31af7Sopenharmony_ci  bool disable_spirv_validation_ = false;
73e5c31af7Sopenharmony_ci  VirtualFileStore* virtual_files_ = nullptr;
74e5c31af7Sopenharmony_ci};
75e5c31af7Sopenharmony_ci
76e5c31af7Sopenharmony_ci// Parses the SPIR-V environment string, and returns the corresponding
77e5c31af7Sopenharmony_ci// |target_env|, |target_env_version|, and |spirv_versoin|. Returns a failure
78e5c31af7Sopenharmony_ci// value if the |spv_env| is invalid.
79e5c31af7Sopenharmony_ciResult ParseSpvEnv(const std::string& spv_env,
80e5c31af7Sopenharmony_ci                   uint32_t* target_env,
81e5c31af7Sopenharmony_ci                   uint32_t* target_env_version,
82e5c31af7Sopenharmony_ci                   uint32_t* spirv_version);
83e5c31af7Sopenharmony_ci
84e5c31af7Sopenharmony_ci}  // namespace amber
85e5c31af7Sopenharmony_ci
86e5c31af7Sopenharmony_ci#endif  // SRC_SHADER_COMPILER_H_
87