Merge branch '286-extend-device-feature-limit-validation' into 'master'
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / framework / vulkan / vkSpirVAsm.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan CTS Framework
3  * --------------------
4  *
5  * Copyright (c) 2015 Google Inc.
6  *
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:
14  *
15  * The above copyright notice(s) and this permission notice shall be
16  * included in all copies or substantial portions of the Materials.
17  *
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.
25  *
26  *//*!
27  * \file
28  * \brief SPIR-V assembly to binary.
29  *//*--------------------------------------------------------------------*/
30
31 #include "vkSpirVAsm.hpp"
32 #include "vkSpirVProgram.hpp"
33 #include "deArrayUtil.hpp"
34 #include "deMemory.h"
35 #include "deClock.h"
36 #include "qpDebugOut.h"
37
38 #if defined(DEQP_HAVE_SPIRV_TOOLS)
39 #       include "deSingleton.h"
40
41 #       include "libspirv/libspirv.h"
42 #endif
43
44 namespace vk
45 {
46
47 using std::string;
48 using std::vector;
49
50 #if defined(DEQP_HAVE_SPIRV_TOOLS)
51
52
53 void assembleSpirV (const SpirVAsmSource* program, std::vector<deUint8>* dst, SpirVProgramInfo* buildInfo)
54 {
55         spv_context context = spvContextCreate();
56
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);
62
63         {
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);
68         }
69
70         if (compileOk != SPV_SUCCESS)
71                 TCU_FAIL("Failed to compile shader");
72
73         dst->resize((int)binary->wordCount * sizeof(deUint32));
74 #if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
75         deMemcpy(&(*dst)[0], &binary->code[0], dst->size());
76 #else
77 #       error "Big-endian not supported"
78 #endif
79         spvBinaryDestroy(binary);
80         spvDiagnosticDestroy(diagnostic);
81         spvContextDestroy(context);
82         return;
83 }
84
85 #else // defined(DEQP_HAVE_SPIRV_TOOLS)
86
87 void assembleSpirV (const SpirVAsmSource*, std::vector<deUint8>*, SpirVProgramInfo*)
88 {
89         TCU_THROW(NotSupportedError, "SPIR-V assembly not supported (DEQP_HAVE_SPIRV_TOOLS not defined)");
90 }
91
92 #endif
93
94 } // vk