7b9db4de63feff62c2099c2c6af0326d8dc5df30
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / synchronization / vktSynchronizationOperation.hpp
1 #ifndef _VKTSYNCHRONIZATIONOPERATION_HPP
2 #define _VKTSYNCHRONIZATIONOPERATION_HPP
3 /*------------------------------------------------------------------------
4  * Vulkan Conformance Tests
5  * ------------------------
6  *
7  * Copyright (c) 2016 The Khronos Group 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 Synchronization operation abstraction
24  *//*--------------------------------------------------------------------*/
25
26 #include "tcuDefs.hpp"
27 #include "vkDefs.hpp"
28 #include "vkPrograms.hpp"
29 #include "vktTestCase.hpp"
30 #include "vktSynchronizationUtil.hpp"
31 #include "tcuVector.hpp"
32 #include "deUniquePtr.hpp"
33 #include <string>
34
35 namespace vkt
36 {
37 namespace synchronization
38 {
39
40 enum OperationName
41 {
42         // Write operations
43         OPERATION_NAME_WRITE_FILL_BUFFER,
44         OPERATION_NAME_WRITE_UPDATE_BUFFER,
45         OPERATION_NAME_WRITE_COPY_BUFFER,
46         OPERATION_NAME_WRITE_COPY_BUFFER_TO_IMAGE,
47         OPERATION_NAME_WRITE_COPY_IMAGE_TO_BUFFER,
48         OPERATION_NAME_WRITE_COPY_IMAGE,
49         OPERATION_NAME_WRITE_BLIT_IMAGE,
50         OPERATION_NAME_WRITE_SSBO_VERTEX,
51         OPERATION_NAME_WRITE_SSBO_TESSELLATION_CONTROL,
52         OPERATION_NAME_WRITE_SSBO_TESSELLATION_EVALUATION,
53         OPERATION_NAME_WRITE_SSBO_GEOMETRY,
54         OPERATION_NAME_WRITE_SSBO_FRAGMENT,
55         OPERATION_NAME_WRITE_SSBO_COMPUTE,
56         OPERATION_NAME_WRITE_SSBO_COMPUTE_INDIRECT,
57         OPERATION_NAME_WRITE_IMAGE_VERTEX,
58         OPERATION_NAME_WRITE_IMAGE_TESSELLATION_CONTROL,
59         OPERATION_NAME_WRITE_IMAGE_TESSELLATION_EVALUATION,
60         OPERATION_NAME_WRITE_IMAGE_GEOMETRY,
61         OPERATION_NAME_WRITE_IMAGE_FRAGMENT,
62         OPERATION_NAME_WRITE_IMAGE_COMPUTE,
63         OPERATION_NAME_WRITE_IMAGE_COMPUTE_INDIRECT,
64         OPERATION_NAME_WRITE_CLEAR_COLOR_IMAGE,
65         OPERATION_NAME_WRITE_CLEAR_DEPTH_STENCIL_IMAGE,
66         OPERATION_NAME_WRITE_DRAW,
67         OPERATION_NAME_WRITE_DRAW_INDEXED,
68         OPERATION_NAME_WRITE_DRAW_INDIRECT,
69         OPERATION_NAME_WRITE_DRAW_INDEXED_INDIRECT,
70         OPERATION_NAME_WRITE_CLEAR_ATTACHMENTS,
71         OPERATION_NAME_WRITE_INDIRECT_BUFFER_DRAW,
72         OPERATION_NAME_WRITE_INDIRECT_BUFFER_DRAW_INDEXED,
73         OPERATION_NAME_WRITE_INDIRECT_BUFFER_DISPATCH,
74
75         // Read operations
76         OPERATION_NAME_READ_COPY_BUFFER,
77         OPERATION_NAME_READ_COPY_BUFFER_TO_IMAGE,
78         OPERATION_NAME_READ_COPY_IMAGE_TO_BUFFER,
79         OPERATION_NAME_READ_COPY_IMAGE,
80         OPERATION_NAME_READ_BLIT_IMAGE,
81         OPERATION_NAME_READ_UBO_VERTEX,
82         OPERATION_NAME_READ_UBO_TESSELLATION_CONTROL,
83         OPERATION_NAME_READ_UBO_TESSELLATION_EVALUATION,
84         OPERATION_NAME_READ_UBO_GEOMETRY,
85         OPERATION_NAME_READ_UBO_FRAGMENT,
86         OPERATION_NAME_READ_UBO_COMPUTE,
87         OPERATION_NAME_READ_UBO_COMPUTE_INDIRECT,
88         OPERATION_NAME_READ_SSBO_VERTEX,
89         OPERATION_NAME_READ_SSBO_TESSELLATION_CONTROL,
90         OPERATION_NAME_READ_SSBO_TESSELLATION_EVALUATION,
91         OPERATION_NAME_READ_SSBO_GEOMETRY,
92         OPERATION_NAME_READ_SSBO_FRAGMENT,
93         OPERATION_NAME_READ_SSBO_COMPUTE,
94         OPERATION_NAME_READ_SSBO_COMPUTE_INDIRECT,
95         OPERATION_NAME_READ_IMAGE_VERTEX,
96         OPERATION_NAME_READ_IMAGE_TESSELLATION_CONTROL,
97         OPERATION_NAME_READ_IMAGE_TESSELLATION_EVALUATION,
98         OPERATION_NAME_READ_IMAGE_GEOMETRY,
99         OPERATION_NAME_READ_IMAGE_FRAGMENT,
100         OPERATION_NAME_READ_IMAGE_COMPUTE,
101         OPERATION_NAME_READ_IMAGE_COMPUTE_INDIRECT,
102         OPERATION_NAME_READ_INDIRECT_BUFFER_DRAW,
103         OPERATION_NAME_READ_INDIRECT_BUFFER_DRAW_INDEXED,
104         OPERATION_NAME_READ_INDIRECT_BUFFER_DISPATCH,
105         OPERATION_NAME_READ_VERTEX_INPUT,
106
107         // Copy operations
108         OPERATION_NAME_COPY_BUFFER,
109         OPERATION_NAME_COPY_IMAGE,
110         OPERATION_NAME_BLIT_IMAGE,
111         OPERATION_NAME_COPY_SSBO_VERTEX,
112         OPERATION_NAME_COPY_SSBO_TESSELLATION_CONTROL,
113         OPERATION_NAME_COPY_SSBO_TESSELLATION_EVALUATION,
114         OPERATION_NAME_COPY_SSBO_GEOMETRY,
115         OPERATION_NAME_COPY_SSBO_FRAGMENT,
116         OPERATION_NAME_COPY_SSBO_COMPUTE,
117         OPERATION_NAME_COPY_SSBO_COMPUTE_INDIRECT,
118         OPERATION_NAME_COPY_IMAGE_VERTEX,
119         OPERATION_NAME_COPY_IMAGE_TESSELLATION_CONTROL,
120         OPERATION_NAME_COPY_IMAGE_TESSELLATION_EVALUATION,
121         OPERATION_NAME_COPY_IMAGE_GEOMETRY,
122         OPERATION_NAME_COPY_IMAGE_FRAGMENT,
123         OPERATION_NAME_COPY_IMAGE_COMPUTE,
124         OPERATION_NAME_COPY_IMAGE_COMPUTE_INDIRECT,
125 };
126
127 // Similar to Context, but allows test instance to decide which resources are used by the operation.
128 // E.g. this is needed when we want operation to work on a particular queue instead of the universal queue.
129 class OperationContext
130 {
131 public:
132                                                                         OperationContext                (Context&                       context,
133                                                                                                                          PipelineCacheData&     pipelineCacheData);
134
135                                                                         OperationContext                (Context&                                       context,
136                                                                                                                          PipelineCacheData&                     pipelineCacheData,
137                                                                                                                          const vk::DeviceInterface&     vk,
138                                                                                                                          const vk::VkDevice                     device,
139                                                                                                                          vk::Allocator&                         allocator);
140
141                                                                         OperationContext                (Context&                                                       context,
142                                                                                                                          const vk::InstanceInterface&           vki,
143                                                                                                                          const vk::DeviceInterface&                     vkd,
144                                                                                                                          vk::VkPhysicalDevice                           physicalDevice,
145                                                                                                                          vk::VkDevice                                           device,
146                                                                                                                          vk::Allocator&                                         allocator,
147                                                                                                                          const std::vector<std::string>&        deviceExtensions,
148                                                                                                                          vk::BinaryCollection&                          programCollection,
149                                                                                                                          PipelineCacheData&                                     pipelineCacheData);
150
151         const vk::InstanceInterface&    getInstanceInterface    (void) const { return m_vki; }
152         const vk::DeviceInterface&              getDeviceInterface              (void) const { return m_vk; }
153         vk::VkPhysicalDevice                    getPhysicalDevice               (void) const { return m_physicalDevice; }
154         vk::VkDevice                                    getDevice                               (void) const { return m_device; }
155         vk::Allocator&                                  getAllocator                    (void) const { return m_allocator; }
156         vk::BinaryCollection&                   getBinaryCollection             (void) const { return m_progCollection; }
157         PipelineCacheData&                              getPipelineCacheData    (void) const { return m_pipelineCacheData; }
158         const std::vector<std::string>& getDeviceExtensions             (void) const { return m_deviceExtensions;}
159         deUint32                                                getUsedApiVersion               (void) const { return m_usedApiVersion; }
160
161         bool isDeviceFunctionalitySupported(const std::string& extension) const
162         {
163                 return m_context.isDeviceFunctionalitySupported(extension);
164         }
165
166 private:
167         const vkt::Context&                             m_context;
168         const vk::InstanceInterface&    m_vki;
169         const vk::DeviceInterface&              m_vk;
170         const vk::VkPhysicalDevice              m_physicalDevice;
171         const vk::VkDevice                              m_device;
172         vk::Allocator&                                  m_allocator;
173         vk::BinaryCollection&                   m_progCollection;
174         PipelineCacheData&                              m_pipelineCacheData;
175         const std::vector<std::string>& m_deviceExtensions;
176         const deUint32                                  m_usedApiVersion;
177
178         // Disabled
179                                                                         OperationContext                (const OperationContext&);
180         OperationContext&                               operator=                               (const OperationContext&);
181 };
182
183 // Common interface to images and buffers used by operations.
184 class Resource
185 {
186 public:
187                                                         Resource        (OperationContext&                              context,
188                                                                                  const ResourceDescription&             desc,
189                                                                                  const deUint32                                 usage,
190                                                                                  const vk::VkSharingMode                sharingMode = vk::VK_SHARING_MODE_EXCLUSIVE,
191                                                                                  const std::vector<deUint32>&   queueFamilyIndex = std::vector<deUint32>());
192
193                                                         Resource        (ResourceType                                   type,
194                                                                                  vk::Move<vk::VkBuffer>                 buffer,
195                                                                                  de::MovePtr<vk::Allocation>    allocation,
196                                                                                  vk::VkDeviceSize                               offset,
197                                                                                  vk::VkDeviceSize                               size);
198
199                                                         Resource        (vk::Move<vk::VkImage>                  image,
200                                                                                  de::MovePtr<vk::Allocation>    allocation,
201                                                                                  const vk::VkExtent3D&                  extent,
202                                                                                  vk::VkImageType                                imageType,
203                                                                                  vk::VkFormat                                   format,
204                                                                                  vk::VkImageSubresourceRange    subresourceRange,
205                                                                                  vk::VkImageSubresourceLayers   subresourceLayers);
206
207         ResourceType                    getType         (void) const { return m_type; }
208         const BufferResource&   getBuffer       (void) const { return m_bufferData; }
209         const ImageResource&    getImage        (void) const { return m_imageData; }
210
211         vk::VkDeviceMemory              getMemory       (void) const;
212
213 private:
214         const ResourceType              m_type;
215         de::MovePtr<Buffer>             m_buffer;
216         BufferResource                  m_bufferData;
217         de::MovePtr<Image>              m_image;
218         ImageResource                   m_imageData;
219 };
220
221 // \note Meaning of image layout is different for read and write types of operations:
222 //       read  - the layout image must be in before being passed to the read operation
223 //       write - the layout image will be in after the write operation has finished
224 struct SyncInfo
225 {
226         vk::VkPipelineStageFlags        stageMask;              // pipeline stage where read/write takes place
227         vk::VkAccessFlags                       accessMask;             // type of access that is performed
228         vk::VkImageLayout                       imageLayout;    // src (for reads) or dst (for writes) image layout
229 };
230
231 struct Data
232 {
233         std::size_t                                     size;
234         const deUint8*                          data;
235 };
236
237 // Abstract operation on a resource
238 // \note Meaning of getData is different for read and write operations:
239 //       read  - data actually read by the operation
240 //       write - expected data that operation was supposed to write
241 // \note It's assumed that recordCommands is called only once (i.e. no multiple command buffers are using these commands).
242 class Operation
243 {
244 public:
245                                                 Operation               (void) {}
246         virtual                         ~Operation              (void) {}
247
248         virtual void            recordCommands  (const vk::VkCommandBuffer cmdBuffer) = 0;      // commands that carry out this operation
249         virtual SyncInfo        getInSyncInfo   (void) const = 0;                                                       // data required to properly synchronize this operation
250         virtual SyncInfo        getOutSyncInfo  (void) const = 0;                                                       // data required to properly synchronize this operation
251         virtual Data            getData                 (void) const = 0;                                                       // get raw data that was written to or read from actual resource
252         virtual void            setData                 (const Data& data) = 0;                                         // set raw data to be read from actual resource
253
254 private:
255                                                 Operation               (const Operation&);
256         Operation&                      operator=               (const Operation&);
257 };
258
259 // A helper class to init programs and create the operation when context becomes available.
260 // Throws OperationInvalidResourceError when resource and operation combination is not possible (e.g. buffer-specific op on an image).
261 class OperationSupport
262 {
263 public:
264                                                                         OperationSupport                        (void) {}
265         virtual                                                 ~OperationSupport                       (void) {}
266
267         virtual deUint32                                getInResourceUsageFlags         (void) const = 0;
268         virtual deUint32                                getOutResourceUsageFlags        (void) const = 0;
269         virtual vk::VkQueueFlags                getQueueFlags                           (const OperationContext& context) const = 0;
270         virtual void                                    initPrograms                            (vk::SourceCollections&) const {}       //!< empty by default
271
272         virtual de::MovePtr<Operation>  build                                           (OperationContext& context, Resource& resource) const = 0;
273         virtual de::MovePtr<Operation>  build                                           (OperationContext& context, Resource& inResource, Resource& outResource) const = 0;
274
275 private:
276                                                                         OperationSupport                        (const OperationSupport&);
277         OperationSupport&                               operator=                                       (const OperationSupport&);
278 };
279
280 bool                                                    isResourceSupported             (const OperationName opName, const ResourceDescription& resourceDesc);
281 de::MovePtr<OperationSupport>   makeOperationSupport    (const OperationName opName, const ResourceDescription& resourceDesc);
282 std::string                                             getOperationName                (const OperationName opName);
283
284 } // synchronization
285 } // vkt
286
287 #endif // _VKTSYNCHRONIZATIONOPERATION_HPP