1 /*-------------------------------------------------------------------------
5 * Copyright (c) 2015 Google Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and/or associated documentation files (the
9 * "Materials"), to deal in the Materials without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Materials, and to
12 * permit persons to whom the Materials are furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice(s) and this permission notice shall be
16 * included in all copies or substantial portions of the Materials.
18 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
28 * \brief SPIR-V assembly to binary.
29 *//*--------------------------------------------------------------------*/
31 #include "vkSpirVAsm.hpp"
32 #include "vkSpirVProgram.hpp"
33 #include "deArrayUtil.hpp"
36 #include "qpDebugOut.h"
38 #if defined(DEQP_HAVE_SPIRV_TOOLS)
39 # include "deSingleton.h"
41 # include "libspirv/libspirv.h"
50 #if defined(DEQP_HAVE_SPIRV_TOOLS)
53 void assembleSpirV (const SpirVAsmSource* program, std::vector<deUint8>* dst, SpirVProgramInfo* buildInfo)
55 spv_context context = spvContextCreate();
57 const std::string& spvSource = program->program.str();
58 spv_binary binary = DE_NULL;
59 spv_diagnostic diagnostic = DE_NULL;
60 const deUint64 compileStartTime = deGetMicroseconds();
61 const spv_result_t compileOk = spvTextToBinary(context, spvSource.c_str(), spvSource.size(), &binary, &diagnostic);
64 buildInfo->source = program;
65 buildInfo->infoLog = diagnostic? diagnostic->error : ""; // \todo [2015-07-13 pyry] Include debug log?
66 buildInfo->compileTimeUs = deGetMicroseconds() - compileStartTime;
67 buildInfo->compileOk = (compileOk == SPV_SUCCESS);
70 if (compileOk != SPV_SUCCESS)
71 TCU_FAIL("Failed to compile shader");
73 dst->resize((int)binary->wordCount * sizeof(deUint32));
74 #if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
75 deMemcpy(&(*dst)[0], &binary->code[0], dst->size());
77 # error "Big-endian not supported"
79 spvBinaryDestroy(binary);
80 spvDiagnosticDestroy(diagnostic);
81 spvContextDestroy(context);
85 #else // defined(DEQP_HAVE_SPIRV_TOOLS)
87 void assembleSpirV (const SpirVAsmSource*, std::vector<deUint8>*, SpirVProgramInfo*)
89 TCU_THROW(NotSupportedError, "SPIR-V assembly not supported (DEQP_HAVE_SPIRV_TOOLS not defined)");