Home | History | Annotate | Download | only in src
      1 // Copyright 2016 The Shaderc Authors. All rights reserved.
      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 #include "libshaderc_util/spirv_tools_wrapper.h"
     16 
     17 #include <cassert>
     18 #include <sstream>
     19 
     20 namespace {
     21 
     22 // Writes the message contained in the diagnostic parameter to *dest. Assumes
     23 // the diagnostic message is reported for a binary location.
     24 void SpirvToolsOutputDiagnostic(spv_diagnostic diagnostic, std::string* dest) {
     25   std::ostringstream os;
     26   if (diagnostic->isTextSource) {
     27     os << diagnostic->position.line + 1 << ":"
     28        << diagnostic->position.column + 1;
     29   } else {
     30     os << diagnostic->position.index;
     31   }
     32   os << ": " << diagnostic->error;
     33   *dest = os.str();
     34 }
     35 
     36 }  // anonymous namespace
     37 
     38 namespace shaderc_util {
     39 
     40 bool SpirvToolsDisassemble(const std::vector<uint32_t>& binary,
     41                            std::string* text_or_error) {
     42   auto spvtools_context = spvContextCreate(SPV_ENV_VULKAN_1_0);
     43   spv_text disassembled_text = nullptr;
     44   spv_diagnostic spvtools_diagnostic = nullptr;
     45 
     46   const bool result =
     47       spvBinaryToText(spvtools_context, binary.data(), binary.size(),
     48                       SPV_BINARY_TO_TEXT_OPTION_INDENT, &disassembled_text,
     49                       &spvtools_diagnostic) == SPV_SUCCESS;
     50   if (result) {
     51     text_or_error->assign(disassembled_text->str, disassembled_text->length);
     52   } else {
     53     SpirvToolsOutputDiagnostic(spvtools_diagnostic, text_or_error);
     54   }
     55 
     56   spvDiagnosticDestroy(spvtools_diagnostic);
     57   spvTextDestroy(disassembled_text);
     58   spvContextDestroy(spvtools_context);
     59 
     60   return result;
     61 }
     62 
     63 bool SpirvToolsAssemble(const string_piece assembly, spv_binary* binary,
     64                         std::string* errors) {
     65   auto spvtools_context = spvContextCreate(SPV_ENV_VULKAN_1_0);
     66   spv_diagnostic spvtools_diagnostic = nullptr;
     67 
     68   *binary = nullptr;
     69   errors->clear();
     70 
     71   const bool result =
     72       spvTextToBinary(spvtools_context, assembly.data(), assembly.size(),
     73                       binary, &spvtools_diagnostic) == SPV_SUCCESS;
     74   if (!result) SpirvToolsOutputDiagnostic(spvtools_diagnostic, errors);
     75 
     76   spvDiagnosticDestroy(spvtools_diagnostic);
     77   spvContextDestroy(spvtools_context);
     78 
     79   return result;
     80 }
     81 
     82 }  // namespace shaderc_util
     83