1 #ifndef _VKPROGRAMS_HPP
2 #define _VKPROGRAMS_HPP
3 /*-------------------------------------------------------------------------
4 * Vulkan CTS Framework
5 * --------------------
6 *
7 * Copyright (c) 2015 Google Inc.
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Program utilities.
24 *//*--------------------------------------------------------------------*/
25
26 #include "vkDefs.hpp"
27 #include "vkRef.hpp"
28 #include "vkSpirVProgram.hpp"
29 #include "vkGlslProgram.hpp"
30
31 #include "deUniquePtr.hpp"
32 #include "deSTLUtil.hpp"
33
34 #include <vector>
35 #include <map>
36
37 namespace vk
38 {
39
40 enum ProgramFormat
41 {
42 PROGRAM_FORMAT_SPIRV = 0,
43
44 PROGRAM_FORMAT_LAST
45 };
46
47 class ProgramBinary
48 {
49 public:
50 ProgramBinary (ProgramFormat format, size_t binarySize, const deUint8* binary);
51
getFormat(void) const52 ProgramFormat getFormat (void) const { return m_format; }
getSize(void) const53 size_t getSize (void) const { return m_binary.size(); }
getBinary(void) const54 const deUint8* getBinary (void) const { return m_binary.empty() ? DE_NULL : &m_binary[0]; }
55
56 private:
57 const ProgramFormat m_format;
58 const std::vector<deUint8> m_binary;
59 };
60
61 template<typename Program>
62 class ProgramCollection
63 {
64 public:
65 ProgramCollection (void);
66 ~ProgramCollection (void);
67
68 void clear (void);
69
70 Program& add (const std::string& name);
71 void add (const std::string& name, de::MovePtr<Program>& program);
72
73 bool contains (const std::string& name) const;
74 const Program& get (const std::string& name) const;
75
76 class Iterator
77 {
78 private:
79 typedef typename std::map<std::string, Program*>::const_iterator IteratorImpl;
80
81 public:
Iterator(const IteratorImpl & i)82 explicit Iterator (const IteratorImpl& i) : m_impl(i) {}
83
operator ++(void)84 Iterator& operator++ (void) { ++m_impl; return *this; }
operator *(void) const85 const Program& operator* (void) const { return getProgram(); }
86
getName(void) const87 const std::string& getName (void) const { return m_impl->first; }
getProgram(void) const88 const Program& getProgram (void) const { return *m_impl->second; }
89
operator ==(const Iterator & other) const90 bool operator== (const Iterator& other) const { return m_impl == other.m_impl; }
operator !=(const Iterator & other) const91 bool operator!= (const Iterator& other) const { return m_impl != other.m_impl; }
92
93 private:
94
95 IteratorImpl m_impl;
96 };
97
begin(void) const98 Iterator begin (void) const { return Iterator(m_programs.begin()); }
end(void) const99 Iterator end (void) const { return Iterator(m_programs.end()); }
100
101 private:
102 typedef std::map<std::string, Program*> ProgramMap;
103
104 ProgramMap m_programs;
105 };
106
107 template<typename Program>
ProgramCollection(void)108 ProgramCollection<Program>::ProgramCollection (void)
109 {
110 }
111
112 template<typename Program>
~ProgramCollection(void)113 ProgramCollection<Program>::~ProgramCollection (void)
114 {
115 clear();
116 }
117
118 template<typename Program>
clear(void)119 void ProgramCollection<Program>::clear (void)
120 {
121 for (typename ProgramMap::const_iterator i = m_programs.begin(); i != m_programs.end(); ++i)
122 delete i->second;
123 m_programs.clear();
124 }
125
126 template<typename Program>
add(const std::string & name)127 Program& ProgramCollection<Program>::add (const std::string& name)
128 {
129 DE_ASSERT(!contains(name));
130 de::MovePtr<Program> prog = de::newMovePtr<Program>();
131 m_programs[name] = prog.get();
132 prog.release();
133 return *m_programs[name];
134 }
135
136 template<typename Program>
add(const std::string & name,de::MovePtr<Program> & program)137 void ProgramCollection<Program>::add (const std::string& name, de::MovePtr<Program>& program)
138 {
139 DE_ASSERT(!contains(name));
140 m_programs[name] = program.get();
141 program.release();
142 }
143
144 template<typename Program>
contains(const std::string & name) const145 bool ProgramCollection<Program>::contains (const std::string& name) const
146 {
147 return de::contains(m_programs, name);
148 }
149
150 template<typename Program>
get(const std::string & name) const151 const Program& ProgramCollection<Program>::get (const std::string& name) const
152 {
153 DE_ASSERT(contains(name));
154 return *m_programs.find(name)->second;
155 }
156
157 typedef ProgramCollection<GlslSource> GlslSourceCollection;
158 typedef ProgramCollection<SpirVAsmSource> SpirVAsmCollection;
159
160 struct SourceCollections
161 {
162 GlslSourceCollection glslSources;
163 SpirVAsmCollection spirvAsmSources;
164 };
165
166 typedef ProgramCollection<ProgramBinary> BinaryCollection;
167
168 ProgramBinary* buildProgram (const GlslSource& program, glu::ShaderProgramInfo* buildInfo);
169 ProgramBinary* assembleProgram (const vk::SpirVAsmSource& program, SpirVProgramInfo* buildInfo);
170 void disassembleProgram (const ProgramBinary& program, std::ostream* dst);
171 bool validateProgram (const ProgramBinary& program, std::ostream* dst);
172
173 Move<VkShaderModule> createShaderModule (const DeviceInterface& deviceInterface, VkDevice device, const ProgramBinary& binary, VkShaderModuleCreateFlags flags);
174
175 glu::ShaderType getGluShaderType (VkShaderStageFlagBits shaderStage);
176 VkShaderStageFlagBits getVkShaderStage (glu::ShaderType shaderType);
177
178 } // vk
179
180 #endif // _VKPROGRAMS_HPP
181