DO NOT MERGE: Apply fix for tessellation fractional even test verification. am: a7716...
[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 Confidential Information as defined by the
19  * Khronos Membership Agreement until designated non-confidential by
20  * Khronos, at which point this condition clause shall be removed.
21  *
22  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
29  *
30  *//*!
31  * \file
32  * \brief SPIR-V assembly to binary.
33  *//*--------------------------------------------------------------------*/
34
35 #include "vkSpirVAsm.hpp"
36 #include "vkSpirVProgram.hpp"
37 #include "deArrayUtil.hpp"
38 #include "deMemory.h"
39 #include "deClock.h"
40 #include "qpDebugOut.h"
41
42 #if defined(DEQP_HAVE_SPIRV_TOOLS)
43 #       include "deSingleton.h"
44
45 #       include "libspirv/libspirv.h"
46 #endif
47
48 namespace vk
49 {
50
51 using std::string;
52 using std::vector;
53
54 #if defined(DEQP_HAVE_SPIRV_TOOLS)
55
56
57 void assembleSpirV (const SpirVAsmSource* program, std::vector<deUint8>* dst, SpirVProgramInfo* buildInfo)
58 {
59         spv_context context = spvContextCreate();
60
61         const std::string&      spvSource                       = program->program.str();
62         spv_binary                      binary                          = DE_NULL;
63         spv_diagnostic          diagnostic                      = DE_NULL;
64         const deUint64          compileStartTime        = deGetMicroseconds();
65         const spv_result_t      compileOk                       = spvTextToBinary(context, spvSource.c_str(), spvSource.size(), &binary, &diagnostic);
66
67         {
68                 buildInfo->source                       = program;
69                 buildInfo->infoLog                      = diagnostic? diagnostic->error : ""; // \todo [2015-07-13 pyry] Include debug log?
70                 buildInfo->compileTimeUs        = deGetMicroseconds() - compileStartTime;
71                 buildInfo->compileOk            = (compileOk == SPV_SUCCESS);
72         }
73
74         if (compileOk != SPV_SUCCESS)
75                 TCU_FAIL("Failed to compile shader");
76
77         dst->resize((int)binary->wordCount * sizeof(deUint32));
78 #if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
79         deMemcpy(&(*dst)[0], &binary->code[0], dst->size());
80 #else
81 #       error "Big-endian not supported"
82 #endif
83         spvBinaryDestroy(binary);
84         spvDiagnosticDestroy(diagnostic);
85         spvContextDestroy(context);
86         return;
87 }
88
89 #else // defined(DEQP_HAVE_SPIRV_TOOLS)
90
91 void assembleSpirV (const SpirVAsmSource*, std::vector<deUint8>*, SpirVProgramInfo*)
92 {
93         TCU_THROW(NotSupportedError, "SPIR-V assembly not supported (DEQP_HAVE_SPIRV_TOOLS not defined)");
94 }
95
96 #endif
97
98 } // vk