1 #ifndef _VKTMEMORYMODELSHAREDLAYOUTCASE_HPP
2 #define _VKTMEMORYMODELSHAREDLAYOUTCASE_HPP
3 /*------------------------------------------------------------------------
4  * Vulkan Conformance Tests
5  * ------------------------
6  *
7  * Copyright (c) 2021 The Khronos Group Inc.
8  * Copyright (c) 2021 Google LLC.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  *//*!
23  * \file
24  * \brief Shared memory layout tests.
25  *//*--------------------------------------------------------------------*/
26 
27 #include "vktTestCase.hpp"
28 #include "tcuDefs.hpp"
29 #include "gluShaderUtil.hpp"
30 #include "gluVarType.hpp"
31 
32 #include "deRandom.hpp"
33 #include "deSharedPtr.hpp"
34 
35 #include <vector>
36 
37 namespace vkt
38 {
39 namespace MemoryModel
40 {
41 typedef de::SharedPtr<glu::StructType> NamedStructSP;
42 
43 struct SharedStructVarEntry
44 {
SharedStructVarEntryvkt::MemoryModel::SharedStructVarEntry45     SharedStructVarEntry(glu::DataType type_, int arraySize_) : type(type_), arraySize(arraySize_)
46     {
47     }
48 
49     glu::DataType type;
50     int arraySize;
51 };
52 
53 struct SharedStructVar
54 {
55     std::string name;
56     glu::VarType type;
57     int arraySize;
58     int topLevelArraySize;
59     std::vector<SharedStructVarEntry> entries;
60 
61     // Contains all the values assigned to the variable.
62     std::vector<std::string> entryValues;
63 };
64 
65 class SharedStruct
66 {
67 public:
68     typedef std::vector<SharedStructVar>::iterator iterator;
69     typedef std::vector<SharedStructVar>::const_iterator const_iterator;
70 
SharedStruct(const std::string name,const std::string instanceName)71     SharedStruct(const std::string name, const std::string instanceName) : m_name(name), m_instanceName(instanceName)
72     {
73     }
74 
getName(void) const75     const std::string getName(void) const
76     {
77         return m_name;
78     }
getInstanceName(void) const79     const std::string getInstanceName(void) const
80     {
81         return m_instanceName;
82     }
83 
addMember(SharedStructVar var)84     void addMember(SharedStructVar var)
85     {
86         m_members.push_back(var);
87     }
getNumMembers(void)88     int getNumMembers(void)
89     {
90         return static_cast<int>(m_members.size());
91     }
92 
begin(void)93     inline iterator begin(void)
94     {
95         return m_members.begin();
96     }
begin(void) const97     inline const_iterator begin(void) const
98     {
99         return m_members.begin();
100     }
end(void)101     inline iterator end(void)
102     {
103         return m_members.end();
104     }
end(void) const105     inline const_iterator end(void) const
106     {
107         return m_members.end();
108     }
109 
110 private:
111     // Shared struct name
112     std::string m_name;
113 
114     // Shared struct instance name
115     std::string m_instanceName;
116 
117     // Contains the members of this struct.
118     std::vector<SharedStructVar> m_members;
119 };
120 
121 class ShaderInterface
122 {
123 public:
ShaderInterface(void)124     ShaderInterface(void)
125     {
126     }
~ShaderInterface(void)127     ~ShaderInterface(void)
128     {
129     }
130 
131     SharedStruct &allocSharedObject(const std::string &name, const std::string &instanceName);
132     NamedStructSP allocStruct(const std::string &name);
133 
getStructs(void)134     std::vector<NamedStructSP> &getStructs(void)
135     {
136         return m_structs;
137     }
getNumStructs(void)138     int getNumStructs(void)
139     {
140         return static_cast<int>(m_structs.size());
141     }
142 
getNumSharedObjects(void) const143     int getNumSharedObjects(void) const
144     {
145         return static_cast<int>(m_sharedMemoryObjects.size());
146     }
getSharedObjects(void)147     std::vector<SharedStruct> &getSharedObjects(void)
148     {
149         return m_sharedMemoryObjects;
150     }
getSharedObjects(void) const151     const std::vector<SharedStruct> &getSharedObjects(void) const
152     {
153         return m_sharedMemoryObjects;
154     }
155 
enable8BitTypes(bool enabled)156     void enable8BitTypes(bool enabled)
157     {
158         m_8BitTypesEnabled = enabled;
159     }
enable16BitTypes(bool enabled)160     void enable16BitTypes(bool enabled)
161     {
162         m_16BitTypesEnabled = enabled;
163     }
is8BitTypesEnabled(void) const164     bool is8BitTypesEnabled(void) const
165     {
166         return m_8BitTypesEnabled;
167     }
is16BitTypesEnabled(void) const168     bool is16BitTypesEnabled(void) const
169     {
170         return m_16BitTypesEnabled;
171     }
172 
173 private:
174     ShaderInterface(const ShaderInterface &);
175     ShaderInterface &operator=(const ShaderInterface &);
176 
177     std::vector<NamedStructSP> m_structs;
178     std::vector<SharedStruct> m_sharedMemoryObjects;
179     bool m_8BitTypesEnabled;
180     bool m_16BitTypesEnabled;
181 };
182 
183 class SharedLayoutCaseInstance : public TestInstance
184 {
185 public:
SharedLayoutCaseInstance(Context & context)186     SharedLayoutCaseInstance(Context &context) : TestInstance(context)
187     {
188     }
~SharedLayoutCaseInstance(void)189     virtual ~SharedLayoutCaseInstance(void)
190     {
191     }
192     virtual tcu::TestStatus iterate(void);
193 };
194 
195 class SharedLayoutCase : public vkt::TestCase
196 {
197 public:
SharedLayoutCase(tcu::TestContext & testCtx,const char * name)198     SharedLayoutCase(tcu::TestContext &testCtx, const char *name) : TestCase(testCtx, name)
199     {
200     }
~SharedLayoutCase(void)201     virtual ~SharedLayoutCase(void)
202     {
203     }
204     virtual void delayedInit(void);
205     virtual void initPrograms(vk::SourceCollections &programCollection) const;
206     virtual TestInstance *createInstance(Context &context) const;
207     virtual void checkSupport(Context &context) const;
208 
209 protected:
210     ShaderInterface m_interface;
211     std::string m_computeShaderSrc;
212 
213 private:
214     SharedLayoutCase(const SharedLayoutCase &);
215     SharedLayoutCase &operator=(const SharedLayoutCase &);
216 };
217 
218 class RandomSharedLayoutCase : public SharedLayoutCase
219 {
220 public:
221     RandomSharedLayoutCase(tcu::TestContext &testCtx, const char *name, uint32_t features, uint32_t seed);
222 
223 private:
224     void generateSharedMemoryObject(de::Random &rnd);
225     void generateSharedMemoryVar(de::Random &rnd, SharedStruct &object);
226     glu::VarType generateType(de::Random &rnd, int typeDepth, bool arrayOk);
227 
228     uint32_t m_features;
229     int m_maxArrayLength;
230     uint32_t m_seed;
231 
232     const int m_maxSharedObjects       = 3;
233     const int m_maxSharedObjectMembers = 4;
234     const int m_maxStructMembers       = 3;
235 };
236 
237 } // namespace MemoryModel
238 } // namespace vkt
239 
240 #endif // _VKTMEMORYMODELSHAREDLAYOUTCASE_HPP
241