| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qspirvshaderremap_p.h" |
| 5 | |
| 6 | #include <SPIRV/SPVRemapper.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | void QSpirvShaderRemapper::remapErrorHandler(const std::string &s) |
| 11 | { |
| 12 | if (!remapErrorMsg.isEmpty()) |
| 13 | remapErrorMsg.append(c: QLatin1Char('\n')); |
| 14 | remapErrorMsg.append(s: QString::fromStdString(s)); |
| 15 | } |
| 16 | |
| 17 | void QSpirvShaderRemapper::remapLogHandler(const std::string &) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | QByteArray QSpirvShaderRemapper::remap(const QByteArray &ir, QSpirvShader::RemapFlags flags) |
| 22 | { |
| 23 | if (ir.isEmpty()) |
| 24 | return QByteArray(); |
| 25 | |
| 26 | remapErrorMsg.clear(); |
| 27 | |
| 28 | spv::spirvbin_t b; |
| 29 | b.registerErrorHandler(handler: std::bind(f: &QSpirvShaderRemapper::remapErrorHandler, args: this, args: std::placeholders::_1)); |
| 30 | b.registerLogHandler(handler: std::bind(f: &QSpirvShaderRemapper::remapLogHandler, args: this, args: std::placeholders::_1)); |
| 31 | |
| 32 | const uint32_t opts = flags.testFlag(flag: QSpirvShader::RemapFlag::StripOnly) ? spv::spirvbin_t::STRIP |
| 33 | : spv::spirvbin_t::DO_EVERYTHING; |
| 34 | |
| 35 | std::vector<uint32_t> v; |
| 36 | v.resize(new_size: ir.size() / 4); |
| 37 | memcpy(dest: v.data(), src: ir.constData(), n: v.size() * 4); |
| 38 | |
| 39 | b.remap(spv&: v, opts); |
| 40 | |
| 41 | if (!remapErrorMsg.isEmpty()) |
| 42 | return QByteArray(); |
| 43 | |
| 44 | return QByteArray(reinterpret_cast<const char *>(v.data()), int(v.size()) * 4); |
| 45 | } |
| 46 | |
| 47 | QT_END_NAMESPACE |
| 48 |
