blob: a129ba54da57b6bf977d26951539649a09fa7198 [file] [log] [blame]
Brad Davis709d3c62018-06-03 11:16:37 -07001/*
Hans-Kristian Arntzen47044822021-01-14 16:07:49 +01002 * Copyright 2018-2021 Bradley Austin Davis
Jon Leechf2a65542021-05-08 01:47:48 -07003 * SPDX-License-Identifier: Apache-2.0 OR MIT
Brad Davis709d3c62018-06-03 11:16:37 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Hans-Kristian Arntzencf1e9e02020-11-25 15:22:08 +010018/*
19 * At your option, you may choose to accept this material under either:
20 * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
21 * 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
Hans-Kristian Arntzencf1e9e02020-11-25 15:22:08 +010022 */
23
Brad Davisee860002018-06-14 21:53:55 -070024#ifndef SPIRV_CROSS_REFLECT_HPP
25#define SPIRV_CROSS_REFLECT_HPP
Brad Davis709d3c62018-06-03 11:16:37 -070026
Brad Davis76204002018-06-20 10:25:38 -070027#include "spirv_glsl.hpp"
Brad Davis709d3c62018-06-03 11:16:37 -070028#include <utility>
Brad Davis709d3c62018-06-03 11:16:37 -070029
Brad Davis6c88b002018-06-18 09:30:16 -070030namespace simple_json
31{
32class Stream;
33}
34
Hans-Kristian Arntzen9b92e682019-03-29 10:29:44 +010035namespace SPIRV_CROSS_NAMESPACE
Brad Davis709d3c62018-06-03 11:16:37 -070036{
Brad Davis76204002018-06-20 10:25:38 -070037class CompilerReflection : public CompilerGLSL
Brad Davis709d3c62018-06-03 11:16:37 -070038{
Brad Davis76204002018-06-20 10:25:38 -070039 using Parent = CompilerGLSL;
Brad Davis709d3c62018-06-03 11:16:37 -070040
41public:
Hans-Kristian Arntzen3fe57d32019-04-09 12:46:23 +020042 explicit CompilerReflection(std::vector<uint32_t> spirv_)
Hans-Kristian Arntzena489ba72019-04-02 11:19:03 +020043 : Parent(std::move(spirv_))
Brad Davis6c88b002018-06-18 09:30:16 -070044 {
Brad Davis76204002018-06-20 10:25:38 -070045 options.vulkan_semantics = true;
Brad Davis6c88b002018-06-18 09:30:16 -070046 }
Brad Davis709d3c62018-06-03 11:16:37 -070047
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020048 CompilerReflection(const uint32_t *ir_, size_t word_count)
49 : Parent(ir_, word_count)
50 {
51 options.vulkan_semantics = true;
52 }
53
54 explicit CompilerReflection(const ParsedIR &ir_)
55 : CompilerGLSL(ir_)
56 {
57 options.vulkan_semantics = true;
58 }
59
60 explicit CompilerReflection(ParsedIR &&ir_)
61 : CompilerGLSL(std::move(ir_))
Brad Davis6c88b002018-06-18 09:30:16 -070062 {
Brad Davis76204002018-06-20 10:25:38 -070063 options.vulkan_semantics = true;
Brad Davis6c88b002018-06-18 09:30:16 -070064 }
Brad Davis709d3c62018-06-03 11:16:37 -070065
Brad Davis6c88b002018-06-18 09:30:16 -070066 void set_format(const std::string &format);
67 std::string compile() override;
Brad Davis709d3c62018-06-03 11:16:37 -070068
69private:
Brad Davis76204002018-06-20 10:25:38 -070070 static std::string execution_model_to_str(spv::ExecutionModel model);
Brad Davis709d3c62018-06-03 11:16:37 -070071
Brad Davis6c88b002018-06-18 09:30:16 -070072 void emit_entry_points();
73 void emit_types();
74 void emit_resources();
Brad Davis8d84a542018-06-20 11:47:31 -070075 void emit_specialization_constants();
Brad Davis709d3c62018-06-03 11:16:37 -070076
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +020077 void emit_type(uint32_t type_id, bool &emitted_open_tag);
Brad Davis6c88b002018-06-18 09:30:16 -070078 void emit_type_member(const SPIRType &type, uint32_t index);
79 void emit_type_member_qualifiers(const SPIRType &type, uint32_t index);
80 void emit_type_array(const SPIRType &type);
Hans-Kristian Arntzena489ba72019-04-02 11:19:03 +020081 void emit_resources(const char *tag, const SmallVector<Resource> &resources);
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +020082 bool type_is_reference(const SPIRType &type) const;
Brad Davis709d3c62018-06-03 11:16:37 -070083
Brad Davis6c88b002018-06-18 09:30:16 -070084 std::string to_member_name(const SPIRType &type, uint32_t index) const;
Brad Davis709d3c62018-06-03 11:16:37 -070085
Brad Davis6c88b002018-06-18 09:30:16 -070086 std::shared_ptr<simple_json::Stream> json_stream;
Brad Davis709d3c62018-06-03 11:16:37 -070087};
88
Hans-Kristian Arntzena489ba72019-04-02 11:19:03 +020089} // namespace SPIRV_CROSS_NAMESPACE
Brad Davis709d3c62018-06-03 11:16:37 -070090
91#endif