1 // Copyright (c) 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // MARK-V is a compression format for SPIR-V binaries. It strips away
16 // non-essential information (such as result ids which can be regenerated) and
17 // uses various bit reduction techiniques to reduce the size of the binary and
18 // make it more similar to other compressed SPIR-V files to further improve
19 // compression of the dataset.
20 
21 #ifndef SOURCE_COMP_MARKV_H_
22 #define SOURCE_COMP_MARKV_H_
23 
24 #include "spirv-tools/libspirv.hpp"
25 
26 namespace spvtools {
27 namespace comp {
28 
29 class MarkvModel;
30 
31 struct MarkvCodecOptions {
32   bool validate_spirv_binary = false;
33 };
34 
35 // Debug callback. Called once per instruction.
36 // |words| is instruction SPIR-V words.
37 // |bits| is a textual representation of the MARK-V bit sequence used to encode
38 // the instruction (char '0' for 0, char '1' for 1).
39 // |comment| contains all logs generated while processing the instruction.
40 using MarkvDebugConsumer =
41     std::function<bool(const std::vector<uint32_t>& words,
42                        const std::string& bits, const std::string& comment)>;
43 
44 // Logging callback. Called often (if decoder reads a single bit, the log
45 // consumer will receive 1 character string with that bit).
46 // This callback is more suitable for continous output than MarkvDebugConsumer,
47 // for example if the codec crashes it would allow to pinpoint on which operand
48 // or bit the crash happened.
49 // |snippet| could be any atomic fragment of text logged by the codec. It can
50 // contain a paragraph of text with newlines, or can be just one character.
51 using MarkvLogConsumer = std::function<void(const std::string& snippet)>;
52 
53 // Encodes the given SPIR-V binary to MARK-V binary.
54 // |log_consumer| is optional (pass MarkvLogConsumer() to disable).
55 // |debug_consumer| is optional (pass MarkvDebugConsumer() to disable).
56 spv_result_t SpirvToMarkv(
57     spv_const_context context, const std::vector<uint32_t>& spirv,
58     const MarkvCodecOptions& options, const MarkvModel& markv_model,
59     MessageConsumer message_consumer, MarkvLogConsumer log_consumer,
60     MarkvDebugConsumer debug_consumer, std::vector<uint8_t>* markv);
61 
62 // Decodes a SPIR-V binary from the given MARK-V binary.
63 // |log_consumer| is optional (pass MarkvLogConsumer() to disable).
64 // |debug_consumer| is optional (pass MarkvDebugConsumer() to disable).
65 spv_result_t MarkvToSpirv(
66     spv_const_context context, const std::vector<uint8_t>& markv,
67     const MarkvCodecOptions& options, const MarkvModel& markv_model,
68     MessageConsumer message_consumer, MarkvLogConsumer log_consumer,
69     MarkvDebugConsumer debug_consumer, std::vector<uint32_t>* spirv);
70 
71 }  // namespace comp
72 }  // namespace spvtools
73 
74 #endif  // SOURCE_COMP_MARKV_H_
75