1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2019 Advanced Micro Devices, Inc.
6  * Copyright (c) 2019 The Khronos Group Inc.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *//*!
21  * \file
22  * \brief Utilities for VK_EXT_sample_locations
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktPipelineSampleLocationsUtil.hpp"
26 #include "deRandom.hpp"
27 #include <set>
28 
29 namespace vkt
30 {
31 namespace pipeline
32 {
33 using namespace vk;
34 using tcu::UVec2;
35 using tcu::Vec2;
36 
37 //! Order a Vector by X, Y, Z, and W
38 template <typename VectorT>
39 struct LessThan
40 {
operator ()vkt::pipeline::LessThan41     bool operator()(const VectorT &v1, const VectorT &v2) const
42     {
43         for (int i = 0; i < VectorT::SIZE; ++i)
44         {
45             if (v1[i] == v2[i])
46                 continue;
47             else
48                 return v1[i] < v2[i];
49         }
50 
51         return false;
52     }
53 };
54 
numSamplesPerPixel(const MultisamplePixelGrid & pixelGrid)55 static inline uint32_t numSamplesPerPixel(const MultisamplePixelGrid &pixelGrid)
56 {
57     return static_cast<uint32_t>(pixelGrid.samplesPerPixel());
58 }
59 
60 //! Fill each grid pixel with a distinct samples pattern, rounding locations based on subPixelBits
fillSampleLocationsRandom(MultisamplePixelGrid & grid,const uint32_t subPixelBits,const uint32_t seed)61 void fillSampleLocationsRandom(MultisamplePixelGrid &grid, const uint32_t subPixelBits, const uint32_t seed)
62 {
63     const uint32_t guardOffset      = 1u; // don't put samples on the right or the bottom edge of the pixel
64     const uint32_t maxLocationIndex = 1u << subPixelBits;
65     de::Random rng(seed);
66 
67     for (uint32_t gridY = 0; gridY < grid.size().y(); ++gridY)
68         for (uint32_t gridX = 0; gridX < grid.size().x(); ++gridX)
69         {
70             std::set<UVec2, LessThan<UVec2>> takenLocationIndices;
71             for (uint32_t sampleNdx = 0; sampleNdx < numSamplesPerPixel(grid); /* no increment */)
72             {
73                 const UVec2 locationNdx(rng.getUint32() % (maxLocationIndex + 1 - guardOffset),
74                                         rng.getUint32() % (maxLocationIndex + 1 - guardOffset));
75 
76                 if (takenLocationIndices.find(locationNdx) == takenLocationIndices.end())
77                 {
78                     const VkSampleLocationEXT location = {
79                         static_cast<float>(locationNdx.x()) / static_cast<float>(maxLocationIndex), // float x;
80                         static_cast<float>(locationNdx.y()) / static_cast<float>(maxLocationIndex), // float y;
81                     };
82 
83                     grid.setSample(gridX, gridY, sampleNdx, location);
84                     takenLocationIndices.insert(locationNdx);
85 
86                     ++sampleNdx; // next sample
87                 }
88             }
89         }
90 }
91 
92 } // namespace pipeline
93 } // namespace vkt
94