blob: 66d10be13a5297ba97928320a584c615ecdb649f [file] [log] [blame]
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001// VK tests
Chia-I Wucb67c652014-10-21 11:06:26 +08002//
Chia-I Wu6170c9e2014-12-08 14:30:10 +08003// Copyright (C) 2014 LunarG, Inc.
Chia-I Wucb67c652014-10-21 11:06:26 +08004//
Chia-I Wu6170c9e2014-12-08 14:30:10 +08005// Permission is hereby granted, free of charge, to any person obtaining a
6// copy of this software and associated documentation files (the "Software"),
7// to deal in the Software without restriction, including without limitation
8// the rights to use, copy, modify, merge, publish, distribute, sublicense,
9// and/or sell copies of the Software, and to permit persons to whom the
10// Software is furnished to do so, subject to the following conditions:
Chia-I Wucb67c652014-10-21 11:06:26 +080011//
Chia-I Wu6170c9e2014-12-08 14:30:10 +080012// The above copyright notice and this permission notice shall be included
13// in all copies or substantial portions of the Software.
Chia-I Wucb67c652014-10-21 11:06:26 +080014//
Chia-I Wu6170c9e2014-12-08 14:30:10 +080015// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21// DEALINGS IN THE SOFTWARE.
Chia-I Wucb67c652014-10-21 11:06:26 +080022
23// Blit (copy, clear, and resolve) tests
24
Tony Barbourb223d482015-06-09 13:40:53 -060025#include "math.h"
Chia-I Wu9dac52d2014-12-27 22:04:00 +080026#include "test_common.h"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060027#include "vktestbinding.h"
Tony Barbour34888cf2015-03-02 16:38:52 -070028#include "test_environment.h"
Chia-I Wucb67c652014-10-21 11:06:26 +080029
30#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060032namespace vk_testing {
Chia-I Wucb67c652014-10-21 11:06:26 +080033
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060034size_t get_format_size(VkFormat format);
Chia-I Wu6170c9e2014-12-08 14:30:10 +080035
Chia-I Wu6170c9e2014-12-08 14:30:10 +080036class ImageChecker {
37public:
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060038 explicit ImageChecker(const VkImageCreateInfo &info, const std::vector<VkBufferImageCopy> &regions)
Chia-I Wu6170c9e2014-12-08 14:30:10 +080039 : info_(info), regions_(regions), pattern_(HASH) {}
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060040 explicit ImageChecker(const VkImageCreateInfo &info, const std::vector<VkImageSubresourceRange> &ranges);
41 explicit ImageChecker(const VkImageCreateInfo &info);
Chia-I Wu6170c9e2014-12-08 14:30:10 +080042
43 void set_solid_pattern(const std::vector<uint8_t> &solid);
44
Tony Barbour8205d902015-04-16 15:59:00 -060045 VkDeviceSize buffer_size() const;
Chia-I Wu6170c9e2014-12-08 14:30:10 +080046 bool fill(Buffer &buf) const { return walk(FILL, buf); }
47 bool fill(Image &img) const { return walk(FILL, img); }
48 bool check(Buffer &buf) const { return walk(CHECK, buf); }
49 bool check(Image &img) const { return walk(CHECK, img); }
50
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060051 const std::vector<VkBufferImageCopy> &regions() const { return regions_; }
Chia-I Wu6170c9e2014-12-08 14:30:10 +080052
53 static void hash_salt_generate() { hash_salt_++; }
54
55private:
56 enum Action {
57 FILL,
58 CHECK,
59 };
60
61 enum Pattern {
62 HASH,
63 SOLID,
64 };
65
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060066 size_t buffer_cpp() const;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060067 VkSubresourceLayout buffer_layout(const VkBufferImageCopy &region) const;
Chia-I Wu6170c9e2014-12-08 14:30:10 +080068
69 bool walk(Action action, Buffer &buf) const;
70 bool walk(Action action, Image &img) const;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060071 bool walk_region(Action action, const VkBufferImageCopy &region, const VkSubresourceLayout &layout, void *data) const;
Chia-I Wu6170c9e2014-12-08 14:30:10 +080072
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060073 std::vector<uint8_t> pattern_hash(const VkImageSubresource &subres, const VkOffset3D &offset) const;
Chia-I Wu6170c9e2014-12-08 14:30:10 +080074
75 static uint32_t hash_salt_;
76
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060077 VkImageCreateInfo info_;
78 std::vector<VkBufferImageCopy> regions_;
Chia-I Wu6170c9e2014-12-08 14:30:10 +080079
80 Pattern pattern_;
81 std::vector<uint8_t> pattern_solid_;
82};
83
Chia-I Wucb67c652014-10-21 11:06:26 +080084
Chia-I Wu6170c9e2014-12-08 14:30:10 +080085uint32_t ImageChecker::hash_salt_;
86
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060087ImageChecker::ImageChecker(const VkImageCreateInfo &info)
Chia-I Wu6170c9e2014-12-08 14:30:10 +080088 : info_(info), regions_(), pattern_(HASH)
89{
90 // create a region for every mip level in array slice 0
Tony Barbour8205d902015-04-16 15:59:00 -060091 VkDeviceSize offset = 0;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060092 for (uint32_t lv = 0; lv < info_.mipLevels; lv++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060093 VkBufferImageCopy region = {};
Chia-I Wu6170c9e2014-12-08 14:30:10 +080094
Chia-I Wu714df452015-01-01 07:55:04 +080095 region.bufferOffset = offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +080096 region.imageSubresource.mipLevel = lv;
97 region.imageSubresource.arraySlice = 0;
Chia-I Wu9dac52d2014-12-27 22:04:00 +080098 region.imageExtent = Image::extent(info_.extent, lv);
Chia-I Wu6170c9e2014-12-08 14:30:10 +080099
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600100 if (info_.usage & VK_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
Tony Barbour8205d902015-04-16 15:59:00 -0600101 if (info_.format != VK_FORMAT_S8_UINT) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600102 region.imageSubresource.aspect = VK_IMAGE_ASPECT_DEPTH;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800103 regions_.push_back(region);
104 }
105
Tony Barbour8205d902015-04-16 15:59:00 -0600106 if (info_.format == VK_FORMAT_D16_UNORM_S8_UINT ||
107 info_.format == VK_FORMAT_D32_SFLOAT_S8_UINT ||
108 info_.format == VK_FORMAT_S8_UINT) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600109 region.imageSubresource.aspect = VK_IMAGE_ASPECT_STENCIL;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800110 regions_.push_back(region);
111 }
Chia-I Wucb67c652014-10-21 11:06:26 +0800112 } else {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600113 region.imageSubresource.aspect = VK_IMAGE_ASPECT_COLOR;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800114 regions_.push_back(region);
115 }
116
117 offset += buffer_layout(region).size;
118 }
119
120 // arraySize should be limited in our tests. If this proves to be an
121 // issue, we can store only the regions for array slice 0 and be smart.
122 if (info_.arraySize > 1) {
Tony Barbour8205d902015-04-16 15:59:00 -0600123 const VkDeviceSize slice_pitch = offset;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600124 const uint32_t slice_region_count = regions_.size();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800125
126 regions_.reserve(slice_region_count * info_.arraySize);
127
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600128 for (uint32_t slice = 1; slice < info_.arraySize; slice++) {
129 for (uint32_t i = 0; i < slice_region_count; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600130 VkBufferImageCopy region = regions_[i];
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800131
Chia-I Wu714df452015-01-01 07:55:04 +0800132 region.bufferOffset += slice_pitch * slice;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800133 region.imageSubresource.arraySlice = slice;
134 regions_.push_back(region);
135 }
136 }
137 }
138}
139
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600140ImageChecker::ImageChecker(const VkImageCreateInfo &info, const std::vector<VkImageSubresourceRange> &ranges)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800141 : info_(info), regions_(), pattern_(HASH)
142{
Tony Barbour8205d902015-04-16 15:59:00 -0600143 VkDeviceSize offset = 0;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600144 for (std::vector<VkImageSubresourceRange>::const_iterator it = ranges.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800145 it != ranges.end(); it++) {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600146 for (uint32_t lv = 0; lv < it->mipLevels; lv++) {
147 for (uint32_t slice = 0; slice < it->arraySize; slice++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600148 VkBufferImageCopy region = {};
Chia-I Wu714df452015-01-01 07:55:04 +0800149 region.bufferOffset = offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800150 region.imageSubresource = Image::subresource(*it, lv, slice);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800151 region.imageExtent = Image::extent(info_.extent, lv);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800152
153 regions_.push_back(region);
154
155 offset += buffer_layout(region).size;
156 }
157 }
158 }
159}
160
161void ImageChecker::set_solid_pattern(const std::vector<uint8_t> &solid)
162{
163 pattern_ = SOLID;
Chia-I Wu4dce6af2014-12-21 14:59:04 +0800164 pattern_solid_.clear();
165 pattern_solid_.reserve(buffer_cpp());
Tony Barbourae2d5f42015-05-14 17:15:33 -0600166 for (size_t i = 0; i < buffer_cpp(); i++)
Chia-I Wu4dce6af2014-12-21 14:59:04 +0800167 pattern_solid_.push_back(solid[i % solid.size()]);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800168}
169
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600170size_t ImageChecker::buffer_cpp() const
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800171{
172 return get_format_size(info_.format);
173}
174
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600175VkSubresourceLayout ImageChecker::buffer_layout(const VkBufferImageCopy &region) const
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800176{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600177 VkSubresourceLayout layout = {};
Chia-I Wu714df452015-01-01 07:55:04 +0800178 layout.offset = region.bufferOffset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800179 layout.rowPitch = buffer_cpp() * region.imageExtent.width;
180 layout.depthPitch = layout.rowPitch * region.imageExtent.height;
181 layout.size = layout.depthPitch * region.imageExtent.depth;
182
183 return layout;
184}
185
Tony Barbour8205d902015-04-16 15:59:00 -0600186VkDeviceSize ImageChecker::buffer_size() const
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800187{
Tony Barbour8205d902015-04-16 15:59:00 -0600188 VkDeviceSize size = 0;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800189
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600190 for (std::vector<VkBufferImageCopy>::const_iterator it = regions_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800191 it != regions_.end(); it++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600192 const VkSubresourceLayout layout = buffer_layout(*it);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800193 if (size < layout.offset + layout.size)
194 size = layout.offset + layout.size;
195 }
196
197 return size;
198}
199
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600200bool ImageChecker::walk_region(Action action, const VkBufferImageCopy &region,
201 const VkSubresourceLayout &layout, void *data) const
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800202{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600203 for (int32_t z = 0; z < region.imageExtent.depth; z++) {
204 for (int32_t y = 0; y < region.imageExtent.height; y++) {
205 for (int32_t x = 0; x < region.imageExtent.width; x++) {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800206 uint8_t *dst = static_cast<uint8_t *>(data);
207 dst += layout.offset + layout.depthPitch * z +
208 layout.rowPitch * y + buffer_cpp() * x;
209
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600210 VkOffset3D offset = region.imageOffset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800211 offset.x += x;
212 offset.y += y;
213 offset.z += z;
214
215 const std::vector<uint8_t> &val = (pattern_ == HASH) ?
216 pattern_hash(region.imageSubresource, offset) :
217 pattern_solid_;
218 assert(val.size() == buffer_cpp());
219
220 if (action == FILL) {
221 memcpy(dst, &val[0], val.size());
222 } else {
Tony Barbourae2d5f42015-05-14 17:15:33 -0600223 for (size_t i = 0; i < val.size(); i++) {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800224 EXPECT_EQ(val[i], dst[i]) <<
225 "Offset is: (" << x << ", " << y << ", " << z << ")";
226 if (val[i] != dst[i])
227 return false;
228 }
229 }
230 }
Chia-I Wucb67c652014-10-21 11:06:26 +0800231 }
232 }
233
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800234 return true;
Chia-I Wucb67c652014-10-21 11:06:26 +0800235}
236
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800237bool ImageChecker::walk(Action action, Buffer &buf) const
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800238{
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800239 void *data = buf.map();
240 if (!data)
241 return false;
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800242
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600243 std::vector<VkBufferImageCopy>::const_iterator it;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800244 for (it = regions_.begin(); it != regions_.end(); it++) {
245 if (!walk_region(action, *it, buffer_layout(*it), data))
246 break;
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800247 }
248
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800249 buf.unmap();
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800250
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800251 return (it == regions_.end());
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800252}
253
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800254bool ImageChecker::walk(Action action, Image &img) const
255{
256 void *data = img.map();
257 if (!data)
258 return false;
259
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600260 std::vector<VkBufferImageCopy>::const_iterator it;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800261 for (it = regions_.begin(); it != regions_.end(); it++) {
262 if (!walk_region(action, *it, img.subresource_layout(it->imageSubresource), data))
263 break;
264 }
265
266 img.unmap();
267
268 return (it == regions_.end());
269}
270
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600271std::vector<uint8_t> ImageChecker::pattern_hash(const VkImageSubresource &subres, const VkOffset3D &offset) const
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800272{
273#define HASH_BYTE(val, b) static_cast<uint8_t>((static_cast<uint32_t>(val) >> (b * 8)) & 0xff)
274#define HASH_BYTES(val) HASH_BYTE(val, 0), HASH_BYTE(val, 1), HASH_BYTE(val, 2), HASH_BYTE(val, 3)
275 const unsigned char input[] = {
276 HASH_BYTES(hash_salt_),
277 HASH_BYTES(subres.mipLevel),
278 HASH_BYTES(subres.arraySlice),
279 HASH_BYTES(offset.x),
280 HASH_BYTES(offset.y),
281 HASH_BYTES(offset.z),
282 };
283 unsigned long hash = 5381;
284
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600285 for (int32_t i = 0; i < ARRAY_SIZE(input); i++)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800286 hash = ((hash << 5) + hash) + input[i];
287
288 const uint8_t output[4] = { HASH_BYTES(hash) };
289#undef HASH_BYTES
290#undef HASH_BYTE
291
Chia-I Wu4dce6af2014-12-21 14:59:04 +0800292 std::vector<uint8_t> val;
293 val.reserve(buffer_cpp());
Tony Barbourae2d5f42015-05-14 17:15:33 -0600294 for (size_t i = 0; i < buffer_cpp(); i++)
Chia-I Wu4dce6af2014-12-21 14:59:04 +0800295 val.push_back(output[i % 4]);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800296
297 return val;
298}
299
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600300size_t get_format_size(VkFormat format)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800301{
Tony Barbourae2d5f42015-05-14 17:15:33 -0600302 static bool format_table_unverified = true;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800303 static const struct format_info {
Tony Barbourae2d5f42015-05-14 17:15:33 -0600304 VkFormat_ format;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600305 size_t size;
306 uint32_t channel_count;
Tony Barbour8205d902015-04-16 15:59:00 -0600307 } format_table[VK_NUM_FORMAT] = {
Tony Barbourae2d5f42015-05-14 17:15:33 -0600308 { VK_FORMAT_UNDEFINED, 0, 0 },
309 { VK_FORMAT_R4G4_UNORM, 1, 2 },
310 { VK_FORMAT_R4G4_USCALED, 1, 2 },
311 { VK_FORMAT_R4G4B4A4_UNORM, 2, 4 },
312 { VK_FORMAT_R4G4B4A4_USCALED, 2, 4 },
313 { VK_FORMAT_R5G6B5_UNORM, 2, 3 },
314 { VK_FORMAT_R5G6B5_USCALED, 2, 3 },
315 { VK_FORMAT_R5G5B5A1_UNORM, 2, 4 },
316 { VK_FORMAT_R5G5B5A1_USCALED, 2, 4 },
317 { VK_FORMAT_R8_UNORM, 1, 1 },
318 { VK_FORMAT_R8_SNORM, 1, 1 },
319 { VK_FORMAT_R8_USCALED, 1, 1 },
320 { VK_FORMAT_R8_SSCALED, 1, 1 },
321 { VK_FORMAT_R8_UINT, 1, 1 },
322 { VK_FORMAT_R8_SINT, 1, 1 },
323 { VK_FORMAT_R8_SRGB, 1, 1 },
324 { VK_FORMAT_R8G8_UNORM, 2, 2 },
325 { VK_FORMAT_R8G8_SNORM, 2, 2 },
326 { VK_FORMAT_R8G8_USCALED, 2, 2 },
327 { VK_FORMAT_R8G8_SSCALED, 2, 2 },
328 { VK_FORMAT_R8G8_UINT, 2, 2 },
329 { VK_FORMAT_R8G8_SINT, 2, 2 },
330 { VK_FORMAT_R8G8_SRGB, 2, 2 },
331 { VK_FORMAT_R8G8B8_UNORM, 3, 3 },
332 { VK_FORMAT_R8G8B8_SNORM, 3, 3 },
333 { VK_FORMAT_R8G8B8_USCALED, 3, 3 },
334 { VK_FORMAT_R8G8B8_SSCALED, 3, 3 },
335 { VK_FORMAT_R8G8B8_UINT, 3, 3 },
336 { VK_FORMAT_R8G8B8_SINT, 3, 3 },
337 { VK_FORMAT_R8G8B8_SRGB, 3, 3 },
338 { VK_FORMAT_R8G8B8A8_UNORM, 4, 4 },
339 { VK_FORMAT_R8G8B8A8_SNORM, 4, 4 },
340 { VK_FORMAT_R8G8B8A8_USCALED, 4, 4 },
341 { VK_FORMAT_R8G8B8A8_SSCALED, 4, 4 },
342 { VK_FORMAT_R8G8B8A8_UINT, 4, 4 },
343 { VK_FORMAT_R8G8B8A8_SINT, 4, 4 },
344 { VK_FORMAT_R8G8B8A8_SRGB, 4, 4 },
345 { VK_FORMAT_R10G10B10A2_UNORM, 4, 4 },
346 { VK_FORMAT_R10G10B10A2_SNORM, 4, 4 },
347 { VK_FORMAT_R10G10B10A2_USCALED, 4, 4 },
348 { VK_FORMAT_R10G10B10A2_SSCALED, 4, 4 },
349 { VK_FORMAT_R10G10B10A2_UINT, 4, 4 },
350 { VK_FORMAT_R10G10B10A2_SINT, 4, 4 },
351 { VK_FORMAT_R16_UNORM, 2, 1 },
352 { VK_FORMAT_R16_SNORM, 2, 1 },
353 { VK_FORMAT_R16_USCALED, 2, 1 },
354 { VK_FORMAT_R16_SSCALED, 2, 1 },
355 { VK_FORMAT_R16_UINT, 2, 1 },
356 { VK_FORMAT_R16_SINT, 2, 1 },
357 { VK_FORMAT_R16_SFLOAT, 2, 1 },
358 { VK_FORMAT_R16G16_UNORM, 4, 2 },
359 { VK_FORMAT_R16G16_SNORM, 4, 2 },
360 { VK_FORMAT_R16G16_USCALED, 4, 2 },
361 { VK_FORMAT_R16G16_SSCALED, 4, 2 },
362 { VK_FORMAT_R16G16_UINT, 4, 2 },
363 { VK_FORMAT_R16G16_SINT, 4, 2 },
364 { VK_FORMAT_R16G16_SFLOAT, 4, 2 },
365 { VK_FORMAT_R16G16B16_UNORM, 6, 3 },
366 { VK_FORMAT_R16G16B16_SNORM, 6, 3 },
367 { VK_FORMAT_R16G16B16_USCALED, 6, 3 },
368 { VK_FORMAT_R16G16B16_SSCALED, 6, 3 },
369 { VK_FORMAT_R16G16B16_UINT, 6, 3 },
370 { VK_FORMAT_R16G16B16_SINT, 6, 3 },
371 { VK_FORMAT_R16G16B16_SFLOAT, 6, 3 },
372 { VK_FORMAT_R16G16B16A16_UNORM, 8, 4 },
373 { VK_FORMAT_R16G16B16A16_SNORM, 8, 4 },
374 { VK_FORMAT_R16G16B16A16_USCALED, 8, 4 },
375 { VK_FORMAT_R16G16B16A16_SSCALED, 8, 4 },
376 { VK_FORMAT_R16G16B16A16_UINT, 8, 4 },
377 { VK_FORMAT_R16G16B16A16_SINT, 8, 4 },
378 { VK_FORMAT_R16G16B16A16_SFLOAT, 8, 4 },
379 { VK_FORMAT_R32_UINT, 4, 1 },
380 { VK_FORMAT_R32_SINT, 4, 1 },
381 { VK_FORMAT_R32_SFLOAT, 4, 1 },
382 { VK_FORMAT_R32G32_UINT, 8, 2 },
383 { VK_FORMAT_R32G32_SINT, 8, 2 },
384 { VK_FORMAT_R32G32_SFLOAT, 8, 2 },
385 { VK_FORMAT_R32G32B32_UINT, 12, 3 },
386 { VK_FORMAT_R32G32B32_SINT, 12, 3 },
387 { VK_FORMAT_R32G32B32_SFLOAT, 12, 3 },
388 { VK_FORMAT_R32G32B32A32_UINT, 16, 4 },
389 { VK_FORMAT_R32G32B32A32_SINT, 16, 4 },
390 { VK_FORMAT_R32G32B32A32_SFLOAT, 16, 4 },
391 { VK_FORMAT_R64_SFLOAT, 8, 1 },
392 { VK_FORMAT_R64G64_SFLOAT, 16, 2 },
393 { VK_FORMAT_R64G64B64_SFLOAT, 24, 3 },
394 { VK_FORMAT_R64G64B64A64_SFLOAT, 32, 4 },
395 { VK_FORMAT_R11G11B10_UFLOAT, 4, 3 },
396 { VK_FORMAT_R9G9B9E5_UFLOAT, 4, 3 },
397 { VK_FORMAT_D16_UNORM, 2, 1 },
398 { VK_FORMAT_D24_UNORM, 3, 1 },
399 { VK_FORMAT_D32_SFLOAT, 4, 1 },
400 { VK_FORMAT_S8_UINT, 1, 1 },
401 { VK_FORMAT_D16_UNORM_S8_UINT, 3, 2 },
402 { VK_FORMAT_D24_UNORM_S8_UINT, 4, 2 },
Tony Barbourb223d482015-06-09 13:40:53 -0600403 { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 2 },
Tony Barbourae2d5f42015-05-14 17:15:33 -0600404 { VK_FORMAT_BC1_RGB_UNORM, 8, 4 },
405 { VK_FORMAT_BC1_RGB_SRGB, 8, 4 },
406 { VK_FORMAT_BC1_RGBA_UNORM, 8, 4 },
407 { VK_FORMAT_BC1_RGBA_SRGB, 8, 4 },
408 { VK_FORMAT_BC2_UNORM, 16, 4 },
409 { VK_FORMAT_BC2_SRGB, 16, 4 },
410 { VK_FORMAT_BC3_UNORM, 16, 4 },
411 { VK_FORMAT_BC3_SRGB, 16, 4 },
412 { VK_FORMAT_BC4_UNORM, 8, 4 },
413 { VK_FORMAT_BC4_SNORM, 8, 4 },
414 { VK_FORMAT_BC5_UNORM, 16, 4 },
415 { VK_FORMAT_BC5_SNORM, 16, 4 },
416 { VK_FORMAT_BC6H_UFLOAT, 16, 4 },
417 { VK_FORMAT_BC6H_SFLOAT, 16, 4 },
418 { VK_FORMAT_BC7_UNORM, 16, 4 },
419 { VK_FORMAT_BC7_SRGB, 16, 4 },
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700420 // TODO: Initialize remaining compressed formats.
Tony Barbourae2d5f42015-05-14 17:15:33 -0600421 { VK_FORMAT_ETC2_R8G8B8_UNORM, 0, 0 },
422 { VK_FORMAT_ETC2_R8G8B8_SRGB, 0, 0 },
423 { VK_FORMAT_ETC2_R8G8B8A1_UNORM, 0, 0 },
424 { VK_FORMAT_ETC2_R8G8B8A1_SRGB, 0, 0 },
425 { VK_FORMAT_ETC2_R8G8B8A8_UNORM, 0, 0 },
426 { VK_FORMAT_ETC2_R8G8B8A8_SRGB, 0, 0 },
427 { VK_FORMAT_EAC_R11_UNORM, 0, 0 },
428 { VK_FORMAT_EAC_R11_SNORM, 0, 0 },
429 { VK_FORMAT_EAC_R11G11_UNORM, 0, 0 },
430 { VK_FORMAT_EAC_R11G11_SNORM, 0, 0 },
431 { VK_FORMAT_ASTC_4x4_UNORM, 0, 0 },
432 { VK_FORMAT_ASTC_4x4_SRGB, 0, 0 },
433 { VK_FORMAT_ASTC_5x4_UNORM, 0, 0 },
434 { VK_FORMAT_ASTC_5x4_SRGB, 0, 0 },
435 { VK_FORMAT_ASTC_5x5_UNORM, 0, 0 },
436 { VK_FORMAT_ASTC_5x5_SRGB, 0, 0 },
437 { VK_FORMAT_ASTC_6x5_UNORM, 0, 0 },
438 { VK_FORMAT_ASTC_6x5_SRGB, 0, 0 },
439 { VK_FORMAT_ASTC_6x6_UNORM, 0, 0 },
440 { VK_FORMAT_ASTC_6x6_SRGB, 0, 0 },
441 { VK_FORMAT_ASTC_8x5_UNORM, 0, 0 },
442 { VK_FORMAT_ASTC_8x5_SRGB, 0, 0 },
443 { VK_FORMAT_ASTC_8x6_UNORM, 0, 0 },
444 { VK_FORMAT_ASTC_8x6_SRGB, 0, 0 },
445 { VK_FORMAT_ASTC_8x8_UNORM, 0, 0 },
446 { VK_FORMAT_ASTC_8x8_SRGB, 0, 0 },
447 { VK_FORMAT_ASTC_10x5_UNORM, 0, 0 },
448 { VK_FORMAT_ASTC_10x5_SRGB, 0, 0 },
449 { VK_FORMAT_ASTC_10x6_UNORM, 0, 0 },
450 { VK_FORMAT_ASTC_10x6_SRGB, 0, 0 },
451 { VK_FORMAT_ASTC_10x8_UNORM, 0, 0 },
452 { VK_FORMAT_ASTC_10x8_SRGB, 0, 0 },
453 { VK_FORMAT_ASTC_10x10_UNORM, 0, 0 },
454 { VK_FORMAT_ASTC_10x10_SRGB, 0, 0 },
455 { VK_FORMAT_ASTC_12x10_UNORM, 0, 0 },
456 { VK_FORMAT_ASTC_12x10_SRGB, 0, 0 },
457 { VK_FORMAT_ASTC_12x12_UNORM, 0, 0 },
458 { VK_FORMAT_ASTC_12x12_SRGB, 0, 0 },
459 { VK_FORMAT_B4G4R4A4_UNORM, 2, 4 },
460 { VK_FORMAT_B5G5R5A1_UNORM, 2, 4 },
461 { VK_FORMAT_B5G6R5_UNORM, 2, 3 },
462 { VK_FORMAT_B5G6R5_USCALED, 2, 3 },
463 { VK_FORMAT_B8G8R8_UNORM, 3, 3 },
464 { VK_FORMAT_B8G8R8_SNORM, 3, 3 },
465 { VK_FORMAT_B8G8R8_USCALED, 3, 3 },
466 { VK_FORMAT_B8G8R8_SSCALED, 3, 3 },
467 { VK_FORMAT_B8G8R8_UINT, 3, 3 },
468 { VK_FORMAT_B8G8R8_SINT, 3, 3 },
469 { VK_FORMAT_B8G8R8_SRGB, 3, 3 },
470 { VK_FORMAT_B8G8R8A8_UNORM, 4, 4 },
471 { VK_FORMAT_B8G8R8A8_SNORM, 4, 4 },
472 { VK_FORMAT_B8G8R8A8_USCALED, 4, 4 },
473 { VK_FORMAT_B8G8R8A8_SSCALED, 4, 4 },
474 { VK_FORMAT_B8G8R8A8_UINT, 4, 4 },
475 { VK_FORMAT_B8G8R8A8_SINT, 4, 4 },
476 { VK_FORMAT_B8G8R8A8_SRGB, 4, 4 },
477 { VK_FORMAT_B10G10R10A2_UNORM, 4, 4 },
478 { VK_FORMAT_B10G10R10A2_SNORM, 4, 4 },
479 { VK_FORMAT_B10G10R10A2_USCALED, 4, 4 },
480 { VK_FORMAT_B10G10R10A2_SSCALED, 4, 4 },
481 { VK_FORMAT_B10G10R10A2_UINT, 4, 4 },
482 { VK_FORMAT_B10G10R10A2_SINT, 4, 4 },
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800483 };
Tony Barbourae2d5f42015-05-14 17:15:33 -0600484 if (format_table_unverified)
485 {
486 for (unsigned int i = 0; i < VK_NUM_FORMAT; i++)
487 {
488 assert(format_table[i].format == i);
489 }
490 format_table_unverified = false;
491 }
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800492
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700493 return format_table[format].size;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800494}
495
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600496VkExtent3D get_mip_level_extent(const VkExtent3D &extent, uint32_t mip_level)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800497{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600498 const VkExtent3D ext = {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800499 (extent.width >> mip_level) ? extent.width >> mip_level : 1,
500 (extent.height >> mip_level) ? extent.height >> mip_level : 1,
501 (extent.depth >> mip_level) ? extent.depth >> mip_level : 1,
502 };
503
504 return ext;
505}
506
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600507}; // namespace vk_testing
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800508
509namespace {
510
511#define DO(action) ASSERT_EQ(true, action);
512
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600513static vk_testing::Environment *environment;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800514
Tony Barbour01999182015-04-09 12:58:51 -0600515class VkCmdBlitTest : public ::testing::Test {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800516protected:
Tony Barbour01999182015-04-09 12:58:51 -0600517 VkCmdBlitTest() :
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800518 dev_(environment->default_device()),
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800519 queue_(*dev_.graphics_queues()[0]),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600520 cmd_(dev_, vk_testing::CmdBuffer::create_info(dev_.graphics_queue_node_index_))
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800521 {
522 // make sure every test uses a different pattern
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600523 vk_testing::ImageChecker::hash_salt_generate();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800524 }
525
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800526 bool submit_and_done()
527 {
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600528 queue_.submit(cmd_);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800529 queue_.wait();
530 mem_refs_.clear();
531 return true;
532 }
533
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600534 vk_testing::Device &dev_;
535 vk_testing::Queue &queue_;
536 vk_testing::CmdBuffer cmd_;
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800537
Tony Barbour8205d902015-04-16 15:59:00 -0600538 std::vector<VkDeviceMemory> mem_refs_;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800539};
540
Tony Barbour01999182015-04-09 12:58:51 -0600541typedef VkCmdBlitTest VkCmdFillBufferTest;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800542
Tony Barbour01999182015-04-09 12:58:51 -0600543TEST_F(VkCmdFillBufferTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800544{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600545 vk_testing::Buffer buf;
Tony Barbour94310562015-04-22 15:10:33 -0600546 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800547
Cody Northropd0e7d222015-06-22 14:56:14 -0600548 buf.init_as_dst(dev_, 20, reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800549
550 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +0800551 vkCmdFillBuffer(cmd_.handle(), buf.obj(), 0, 4, 0x11111111);
552 vkCmdFillBuffer(cmd_.handle(), buf.obj(), 4, 16, 0x22222222);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800553 cmd_.end();
554
555 submit_and_done();
556
557 const uint32_t *data = static_cast<const uint32_t *>(buf.map());
558 EXPECT_EQ(0x11111111, data[0]);
559 EXPECT_EQ(0x22222222, data[1]);
560 EXPECT_EQ(0x22222222, data[2]);
561 EXPECT_EQ(0x22222222, data[3]);
562 EXPECT_EQ(0x22222222, data[4]);
563 buf.unmap();
564}
565
Tony Barbour01999182015-04-09 12:58:51 -0600566TEST_F(VkCmdFillBufferTest, Large)
Chia-I Wuea9367f2014-11-23 02:16:45 +0800567{
Tony Barbour8205d902015-04-16 15:59:00 -0600568 const VkDeviceSize size = 32 * 1024 * 1024;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600569 vk_testing::Buffer buf;
Tony Barbour94310562015-04-22 15:10:33 -0600570 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wuea9367f2014-11-23 02:16:45 +0800571
Cody Northropd0e7d222015-06-22 14:56:14 -0600572 buf.init_as_dst(dev_, size, reqs);
Chia-I Wuea9367f2014-11-23 02:16:45 +0800573
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800574 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +0800575 vkCmdFillBuffer(cmd_.handle(), buf.obj(), 0, size / 2, 0x11111111);
576 vkCmdFillBuffer(cmd_.handle(), buf.obj(), size / 2, size / 2, 0x22222222);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800577 cmd_.end();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800578
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800579 submit_and_done();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800580
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800581 const uint32_t *data = static_cast<const uint32_t *>(buf.map());
Tony Barbour8205d902015-04-16 15:59:00 -0600582 VkDeviceSize offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800583 for (offset = 0; offset < size / 2; offset += 4)
584 EXPECT_EQ(0x11111111, data[offset / 4]) << "Offset is: " << offset;
585 for (; offset < size; offset += 4)
586 EXPECT_EQ(0x22222222, data[offset / 4]) << "Offset is: " << offset;
587 buf.unmap();
588}
Chia-I Wuea9367f2014-11-23 02:16:45 +0800589
Tony Barbour01999182015-04-09 12:58:51 -0600590TEST_F(VkCmdFillBufferTest, Overlap)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800591{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600592 vk_testing::Buffer buf;
Tony Barbour94310562015-04-22 15:10:33 -0600593 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800594
Cody Northropd0e7d222015-06-22 14:56:14 -0600595 buf.init_as_dst(dev_, 64, reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800596
597 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +0800598 vkCmdFillBuffer(cmd_.handle(), buf.obj(), 0, 48, 0x11111111);
599 vkCmdFillBuffer(cmd_.handle(), buf.obj(), 32, 32, 0x22222222);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800600 cmd_.end();
601
602 submit_and_done();
603
604 const uint32_t *data = static_cast<const uint32_t *>(buf.map());
Tony Barbour8205d902015-04-16 15:59:00 -0600605 VkDeviceSize offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800606 for (offset = 0; offset < 32; offset += 4)
607 EXPECT_EQ(0x11111111, data[offset / 4]) << "Offset is: " << offset;
608 for (; offset < 64; offset += 4)
609 EXPECT_EQ(0x22222222, data[offset / 4]) << "Offset is: " << offset;
610 buf.unmap();
611}
612
Tony Barbour01999182015-04-09 12:58:51 -0600613TEST_F(VkCmdFillBufferTest, MultiAlignments)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800614{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600615 vk_testing::Buffer bufs[9];
Tony Barbour8205d902015-04-16 15:59:00 -0600616 VkDeviceSize size = 4;
Tony Barbour94310562015-04-22 15:10:33 -0600617 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800618
619 cmd_.begin();
620 for (int i = 0; i < ARRAY_SIZE(bufs); i++) {
Cody Northropd0e7d222015-06-22 14:56:14 -0600621 bufs[i].init_as_dst(dev_, size, reqs);
Chia-I Wu78c2a352015-07-03 11:49:42 +0800622 vkCmdFillBuffer(cmd_.handle(), bufs[i].obj(), 0, size, 0x11111111);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800623 size <<= 1;
624 }
625 cmd_.end();
626
627 submit_and_done();
628
629 size = 4;
630 for (int i = 0; i < ARRAY_SIZE(bufs); i++) {
631 const uint32_t *data = static_cast<const uint32_t *>(bufs[i].map());
Tony Barbour8205d902015-04-16 15:59:00 -0600632 VkDeviceSize offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800633 for (offset = 0; offset < size; offset += 4)
634 EXPECT_EQ(0x11111111, data[offset / 4]) << "Buffser is: " << i << "\n" <<
635 "Offset is: " << offset;
636 bufs[i].unmap();
637
638 size <<= 1;
639 }
640}
641
Tony Barbour01999182015-04-09 12:58:51 -0600642typedef VkCmdBlitTest VkCmdCopyBufferTest;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800643
Tony Barbour01999182015-04-09 12:58:51 -0600644TEST_F(VkCmdCopyBufferTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800645{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600646 vk_testing::Buffer src, dst;
Tony Barbour94310562015-04-22 15:10:33 -0600647 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800648
Cody Northropd0e7d222015-06-22 14:56:14 -0600649 src.init_as_src(dev_, 4, reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800650 uint32_t *data = static_cast<uint32_t *>(src.map());
651 data[0] = 0x11111111;
652 src.unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800653
Cody Northropd0e7d222015-06-22 14:56:14 -0600654 dst.init_as_dst(dev_, 4, reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800655
656 cmd_.begin();
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600657 VkBufferCopy region = {};
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800658 region.copySize = 4;
Chia-I Wu78c2a352015-07-03 11:49:42 +0800659 vkCmdCopyBuffer(cmd_.handle(), src.obj(), dst.obj(), 1, &region);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800660 cmd_.end();
661
662 submit_and_done();
663
664 data = static_cast<uint32_t *>(dst.map());
665 EXPECT_EQ(0x11111111, data[0]);
666 dst.unmap();
667}
668
Tony Barbour01999182015-04-09 12:58:51 -0600669TEST_F(VkCmdCopyBufferTest, Large)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800670{
Tony Barbour8205d902015-04-16 15:59:00 -0600671 const VkDeviceSize size = 32 * 1024 * 1024;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600672 vk_testing::Buffer src, dst;
Tony Barbour94310562015-04-22 15:10:33 -0600673 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800674
Cody Northropd0e7d222015-06-22 14:56:14 -0600675 src.init_as_src(dev_, size, reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800676 uint32_t *data = static_cast<uint32_t *>(src.map());
Tony Barbour8205d902015-04-16 15:59:00 -0600677 VkDeviceSize offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800678 for (offset = 0; offset < size; offset += 4)
679 data[offset / 4] = offset;
680 src.unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800681
Cody Northropd0e7d222015-06-22 14:56:14 -0600682 dst.init_as_dst(dev_, size, reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800683
684 cmd_.begin();
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600685 VkBufferCopy region = {};
Chia-I Wuea9367f2014-11-23 02:16:45 +0800686 region.copySize = size;
Chia-I Wu78c2a352015-07-03 11:49:42 +0800687 vkCmdCopyBuffer(cmd_.handle(), src.obj(), dst.obj(), 1, &region);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800688 cmd_.end();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800689
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800690 submit_and_done();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800691
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800692 data = static_cast<uint32_t *>(dst.map());
693 for (offset = 0; offset < size; offset += 4)
694 EXPECT_EQ(offset, data[offset / 4]);
695 dst.unmap();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800696}
697
Tony Barbour01999182015-04-09 12:58:51 -0600698TEST_F(VkCmdCopyBufferTest, MultiAlignments)
Chia-I Wucb67c652014-10-21 11:06:26 +0800699{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600700 const VkBufferCopy regions[] = {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800701 /* well aligned */
702 { 0, 0, 256 },
703 { 0, 256, 128 },
704 { 0, 384, 64 },
705 { 0, 448, 32 },
706 { 0, 480, 16 },
707 { 0, 496, 8 },
Chia-I Wucb67c652014-10-21 11:06:26 +0800708
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800709 /* ill aligned */
710 { 7, 510, 16 },
711 { 16, 530, 13 },
712 { 32, 551, 16 },
713 { 45, 570, 15 },
714 { 50, 590, 1 },
715 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600716 vk_testing::Buffer src, dst;
Tony Barbour94310562015-04-22 15:10:33 -0600717 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Chia-I Wucb67c652014-10-21 11:06:26 +0800718
Cody Northropd0e7d222015-06-22 14:56:14 -0600719 src.init_as_src(dev_, 256, reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800720 uint8_t *data = static_cast<uint8_t *>(src.map());
721 for (int i = 0; i < 256; i++)
722 data[i] = i;
723 src.unmap();
Chia-I Wucb67c652014-10-21 11:06:26 +0800724
Cody Northropd0e7d222015-06-22 14:56:14 -0600725 dst.init_as_dst(dev_, 1024, reqs);
Chia-I Wucb67c652014-10-21 11:06:26 +0800726
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800727 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +0800728 vkCmdCopyBuffer(cmd_.handle(), src.obj(), dst.obj(), ARRAY_SIZE(regions), regions);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800729 cmd_.end();
Chia-I Wucb67c652014-10-21 11:06:26 +0800730
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800731 submit_and_done();
Chia-I Wucb67c652014-10-21 11:06:26 +0800732
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800733 data = static_cast<uint8_t *>(dst.map());
734 for (int i = 0; i < ARRAY_SIZE(regions); i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600735 const VkBufferCopy &r = regions[i];
Chia-I Wucb67c652014-10-21 11:06:26 +0800736
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800737 for (int j = 0; j < r.copySize; j++) {
738 EXPECT_EQ(r.srcOffset + j, data[r.destOffset + j]) <<
739 "Region is: " << i << "\n" <<
740 "Offset is: " << r.destOffset + j;
741 }
742 }
743 dst.unmap();
744}
Chia-I Wucb67c652014-10-21 11:06:26 +0800745
Tony Barbour01999182015-04-09 12:58:51 -0600746TEST_F(VkCmdCopyBufferTest, RAWHazard)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800747{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600748 vk_testing::Buffer bufs[3];
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600749 VkEventCreateInfo event_info;
750 VkEvent event;
751 VkMemoryRequirements mem_req;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600752 VkResult err;
Tony Barbour94310562015-04-22 15:10:33 -0600753 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Tony Barbour43cdc952015-05-21 13:26:05 -0600754 VkMemoryAllocInfo mem_info;
755 VkDeviceMemory event_mem;
Mike Stroyan55658c22014-12-04 11:08:39 +0000756
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600757 // typedef struct VkEventCreateInfo_
Mike Stroyan55658c22014-12-04 11:08:39 +0000758 // {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600759 // VkStructureType sType; // Must be VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
Mark Lobodzinski72346292015-07-02 16:49:40 -0600760 // const void* pNext; // Pointer to next structure
761 // VkFlags flags; // Reserved
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600762 // } VkEventCreateInfo;
Mike Stroyan55658c22014-12-04 11:08:39 +0000763 memset(&event_info, 0, sizeof(event_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600764 event_info.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
Mike Stroyan55658c22014-12-04 11:08:39 +0000765
Chia-I Wua2636292015-07-03 10:41:20 +0800766 err = vkCreateEvent(dev_.handle(), &event_info, &event);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600767 ASSERT_VK_SUCCESS(err);
Mike Stroyan55658c22014-12-04 11:08:39 +0000768
Chia-I Wua2636292015-07-03 10:41:20 +0800769 err = vkGetObjectMemoryRequirements(dev_.handle(), VK_OBJECT_TYPE_EVENT, event, &mem_req);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600770 ASSERT_VK_SUCCESS(err);
Mike Stroyan55658c22014-12-04 11:08:39 +0000771
Tony Barbour43cdc952015-05-21 13:26:05 -0600772 if (mem_req.size) {
Mike Stroyan55658c22014-12-04 11:08:39 +0000773
Tony Barbour43cdc952015-05-21 13:26:05 -0600774 memset(&mem_info, 0, sizeof(mem_info));
775 mem_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
776 mem_info.allocationSize = mem_req.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600777 mem_info.memoryTypeIndex = 0;
778
Chia-I Wuf5fb1092015-07-03 10:32:05 +0800779 err = dev_.phy().set_memory_type(mem_req.memoryTypeBits, &mem_info, 0);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600780 ASSERT_VK_SUCCESS(err);
781
Chia-I Wua2636292015-07-03 10:41:20 +0800782 err = vkAllocMemory(dev_.handle(), &mem_info, &event_mem);
Tony Barbour43cdc952015-05-21 13:26:05 -0600783 ASSERT_VK_SUCCESS(err);
784
Chia-I Wua2636292015-07-03 10:41:20 +0800785 err = vkBindObjectMemory(dev_.handle(), VK_OBJECT_TYPE_EVENT, event, event_mem, 0);
Tony Barbour43cdc952015-05-21 13:26:05 -0600786 ASSERT_VK_SUCCESS(err);
787 }
Mike Stroyan55658c22014-12-04 11:08:39 +0000788
Chia-I Wua2636292015-07-03 10:41:20 +0800789 err = vkResetEvent(dev_.handle(), event);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600790 ASSERT_VK_SUCCESS(err);
Chia-I Wucb67c652014-10-21 11:06:26 +0800791
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800792 for (int i = 0; i < ARRAY_SIZE(bufs); i++) {
Cody Northropd0e7d222015-06-22 14:56:14 -0600793 bufs[i].init_as_src_and_dst(dev_, 4, reqs);
Chia-I Wucb67c652014-10-21 11:06:26 +0800794
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800795 uint32_t *data = static_cast<uint32_t *>(bufs[i].map());
796 data[0] = 0x22222222 * (i + 1);
797 bufs[i].unmap();
798 }
799
800 cmd_.begin();
801
Chia-I Wu78c2a352015-07-03 11:49:42 +0800802 vkCmdFillBuffer(cmd_.handle(), bufs[0].obj(), 0, 4, 0x11111111);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800803 // is this necessary?
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600804 VkBufferMemoryBarrier memory_barrier = bufs[0].buffer_memory_barrier(
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600805 VK_MEMORY_OUTPUT_TRANSFER_BIT, VK_MEMORY_INPUT_TRANSFER_BIT, 0, 4);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600806 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyan55658c22014-12-04 11:08:39 +0000807
Tony Barbourc2e987e2015-06-29 16:20:35 -0600808 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
809 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Chia-I Wu78c2a352015-07-03 11:49:42 +0800810 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800811
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600812 VkBufferCopy region = {};
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800813 region.copySize = 4;
Chia-I Wu78c2a352015-07-03 11:49:42 +0800814 vkCmdCopyBuffer(cmd_.handle(), bufs[0].obj(), bufs[1].obj(), 1, &region);
Mike Stroyan55658c22014-12-04 11:08:39 +0000815
816 memory_barrier = bufs[1].buffer_memory_barrier(
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600817 VK_MEMORY_OUTPUT_TRANSFER_BIT, VK_MEMORY_INPUT_TRANSFER_BIT, 0, 4);
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -0600818 pmemory_barrier = &memory_barrier;
Chia-I Wu78c2a352015-07-03 11:49:42 +0800819 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800820
Chia-I Wu78c2a352015-07-03 11:49:42 +0800821 vkCmdCopyBuffer(cmd_.handle(), bufs[1].obj(), bufs[2].obj(), 1, &region);
Mike Stroyan55658c22014-12-04 11:08:39 +0000822
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600823 /* Use vkCmdSetEvent and vkCmdWaitEvents to test them.
824 * This could be vkCmdPipelineBarrier.
Mike Stroyan55658c22014-12-04 11:08:39 +0000825 */
Chia-I Wu78c2a352015-07-03 11:49:42 +0800826 vkCmdSetEvent(cmd_.handle(), event, VK_PIPELINE_STAGE_TRANSFER_BIT);
Mike Stroyan55658c22014-12-04 11:08:39 +0000827
828 // Additional commands could go into the buffer here before the wait.
829
830 memory_barrier = bufs[1].buffer_memory_barrier(
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600831 VK_MEMORY_OUTPUT_TRANSFER_BIT, VK_MEMORY_INPUT_HOST_READ_BIT, 0, 4);
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -0600832 pmemory_barrier = &memory_barrier;
Chia-I Wu78c2a352015-07-03 11:49:42 +0800833 vkCmdWaitEvents(cmd_.handle(), 1, &event, src_stages, dest_stages, 1, (const void **)&pmemory_barrier);
Mike Stroyan55658c22014-12-04 11:08:39 +0000834
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800835 cmd_.end();
836
837 submit_and_done();
838
839 const uint32_t *data = static_cast<const uint32_t *>(bufs[2].map());
840 EXPECT_EQ(0x11111111, data[0]);
841 bufs[2].unmap();
Mike Stroyan55658c22014-12-04 11:08:39 +0000842
Chia-I Wua2636292015-07-03 10:41:20 +0800843 err = vkDestroyObject(dev_.handle(), VK_OBJECT_TYPE_EVENT, event);
Tony Barbour4003ae82015-06-26 11:49:05 -0600844 ASSERT_VK_SUCCESS(err);
845
Tony Barbour43cdc952015-05-21 13:26:05 -0600846 if (mem_req.size) {
847 // All done with event memory, clean up
Chia-I Wua2636292015-07-03 10:41:20 +0800848 err = vkFreeMemory(dev_.handle(), event_mem);
Tony Barbour43cdc952015-05-21 13:26:05 -0600849 ASSERT_VK_SUCCESS(err);
850 }
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800851}
852
Tony Barbour01999182015-04-09 12:58:51 -0600853class VkCmdBlitImageTest : public VkCmdBlitTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800854protected:
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600855 void init_test_formats(VkFlags features)
Chia-I Wucb67c652014-10-21 11:06:26 +0800856 {
Tony Barbour8205d902015-04-16 15:59:00 -0600857 first_linear_format_ = VK_FORMAT_UNDEFINED;
858 first_optimal_format_ = VK_FORMAT_UNDEFINED;
Chia-I Wucb67c652014-10-21 11:06:26 +0800859
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600860 for (std::vector<vk_testing::Device::Format>::const_iterator it = dev_.formats().begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800861 it != dev_.formats().end(); it++) {
862 if (it->features & features) {
863 test_formats_.push_back(*it);
Chia-I Wucb67c652014-10-21 11:06:26 +0800864
Tony Barbour8205d902015-04-16 15:59:00 -0600865 if (it->tiling == VK_IMAGE_TILING_LINEAR &&
866 first_linear_format_ == VK_FORMAT_UNDEFINED)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800867 first_linear_format_ = it->format;
Tony Barbour8205d902015-04-16 15:59:00 -0600868 if (it->tiling == VK_IMAGE_TILING_OPTIMAL &&
869 first_optimal_format_ == VK_FORMAT_UNDEFINED)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800870 first_optimal_format_ = it->format;
871 }
872 }
873 }
Chia-I Wucb67c652014-10-21 11:06:26 +0800874
Chia-I Wu9feab842014-12-22 13:19:08 +0800875 void init_test_formats()
876 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600877 init_test_formats(static_cast<VkFlags>(-1));
Chia-I Wu9feab842014-12-22 13:19:08 +0800878 }
879
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600880 void fill_src(vk_testing::Image &img, const vk_testing::ImageChecker &checker)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800881 {
882 if (img.transparent()) {
883 checker.fill(img);
884 return;
Chia-I Wucb67c652014-10-21 11:06:26 +0800885 }
886
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800887 ASSERT_EQ(true, img.copyable());
888
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600889 vk_testing::Buffer in_buf;
Tony Barbour94310562015-04-22 15:10:33 -0600890 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
891
Cody Northropd0e7d222015-06-22 14:56:14 -0600892 in_buf.init_as_src(dev_, checker.buffer_size(), reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800893 checker.fill(in_buf);
894
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800895 // copy in and tile
896 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +0800897 vkCmdCopyBufferToImage(cmd_.handle(), in_buf.obj(),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600898 img.obj(), VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800899 checker.regions().size(), &checker.regions()[0]);
900 cmd_.end();
901
902 submit_and_done();
Chia-I Wucb67c652014-10-21 11:06:26 +0800903 }
904
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600905 void check_dst(vk_testing::Image &img, const vk_testing::ImageChecker &checker)
Chia-I Wu86822632014-11-22 15:09:42 +0800906 {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800907 if (img.transparent()) {
908 DO(checker.check(img));
909 return;
Chia-I Wu86822632014-11-22 15:09:42 +0800910 }
911
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800912 ASSERT_EQ(true, img.copyable());
913
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600914 vk_testing::Buffer out_buf;
Tony Barbour94310562015-04-22 15:10:33 -0600915 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northropd0e7d222015-06-22 14:56:14 -0600916 out_buf.init_as_dst(dev_, checker.buffer_size(), reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800917
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800918 // copy out and linearize
919 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +0800920 vkCmdCopyImageToBuffer(cmd_.handle(),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600921 img.obj(), VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600922 out_buf.obj(),
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800923 checker.regions().size(), &checker.regions()[0]);
924 cmd_.end();
925
926 submit_and_done();
927
928 DO(checker.check(out_buf));
Chia-I Wu86822632014-11-22 15:09:42 +0800929 }
930
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600931 std::vector<vk_testing::Device::Format> test_formats_;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600932 VkFormat first_linear_format_;
933 VkFormat first_optimal_format_;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800934};
935
Tony Barbour01999182015-04-09 12:58:51 -0600936class VkCmdCopyBufferToImageTest : public VkCmdBlitImageTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800937protected:
938 virtual void SetUp()
939 {
Tony Barbour01999182015-04-09 12:58:51 -0600940 VkCmdBlitTest::SetUp();
Tony Barbour8205d902015-04-16 15:59:00 -0600941 init_test_formats(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800942 ASSERT_NE(true, test_formats_.empty());
943 }
944
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600945 void test_copy_memory_to_image(const VkImageCreateInfo &img_info, const vk_testing::ImageChecker &checker)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800946 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600947 vk_testing::Buffer buf;
948 vk_testing::Image img;
Tony Barbour43cdc952015-05-21 13:26:05 -0600949 VkMemoryPropertyFlags buffer_reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
950 VkMemoryPropertyFlags image_reqs =
951 (img_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800952
Cody Northropd0e7d222015-06-22 14:56:14 -0600953 buf.init_as_src(dev_, checker.buffer_size(), buffer_reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800954 checker.fill(buf);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800955
Tony Barbour43cdc952015-05-21 13:26:05 -0600956 img.init(dev_, img_info, image_reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800957
958 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +0800959 vkCmdCopyBufferToImage(cmd_.handle(),
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600960 buf.obj(),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600961 img.obj(), VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800962 checker.regions().size(), &checker.regions()[0]);
963 cmd_.end();
964
965 submit_and_done();
966
967 check_dst(img, checker);
968 }
969
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600970 void test_copy_memory_to_image(const VkImageCreateInfo &img_info, const std::vector<VkBufferImageCopy> &regions)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800971 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600972 vk_testing::ImageChecker checker(img_info, regions);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800973 test_copy_memory_to_image(img_info, checker);
974 }
975
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600976 void test_copy_memory_to_image(const VkImageCreateInfo &img_info)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800977 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600978 vk_testing::ImageChecker checker(img_info);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800979 test_copy_memory_to_image(img_info, checker);
980 }
981};
982
Tony Barbour01999182015-04-09 12:58:51 -0600983TEST_F(VkCmdCopyBufferToImageTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800984{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600985 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800986 it != test_formats_.end(); it++) {
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700987
988 // not sure what to do here
Tony Barbour8205d902015-04-16 15:59:00 -0600989 if (it->format == VK_FORMAT_UNDEFINED ||
990 (it->format >= VK_FORMAT_B8G8R8_UNORM &&
991 it->format <= VK_FORMAT_B8G8R8_SRGB))
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700992 continue;
993
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600994 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -0600995 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800996 img_info.format = it->format;
997 img_info.extent.width = 64;
998 img_info.extent.height = 64;
999 img_info.tiling = it->tiling;
Cody Northrop025cbb12015-06-18 17:05:15 -06001000 img_info.usage = VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001001
1002 test_copy_memory_to_image(img_info);
1003 }
Chia-I Wu86822632014-11-22 15:09:42 +08001004}
1005
Tony Barbour01999182015-04-09 12:58:51 -06001006class VkCmdCopyImageToBufferTest : public VkCmdBlitImageTest {
Chia-I Wu830fb332014-12-13 15:28:20 +08001007protected:
1008 virtual void SetUp()
1009 {
Tony Barbour01999182015-04-09 12:58:51 -06001010 VkCmdBlitTest::SetUp();
Tony Barbour8205d902015-04-16 15:59:00 -06001011 init_test_formats(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT);
Chia-I Wu830fb332014-12-13 15:28:20 +08001012 ASSERT_NE(true, test_formats_.empty());
1013 }
1014
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001015 void test_copy_image_to_memory(const VkImageCreateInfo &img_info, const vk_testing::ImageChecker &checker)
Chia-I Wu830fb332014-12-13 15:28:20 +08001016 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001017 vk_testing::Image img;
1018 vk_testing::Buffer buf;
Tony Barbour43cdc952015-05-21 13:26:05 -06001019 VkMemoryPropertyFlags buffer_reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
1020 VkMemoryPropertyFlags image_reqs =
1021 (img_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
Chia-I Wu830fb332014-12-13 15:28:20 +08001022
Tony Barbour43cdc952015-05-21 13:26:05 -06001023 img.init(dev_, img_info, image_reqs);
Chia-I Wu830fb332014-12-13 15:28:20 +08001024 fill_src(img, checker);
Chia-I Wu830fb332014-12-13 15:28:20 +08001025
Cody Northropd0e7d222015-06-22 14:56:14 -06001026 buf.init_as_dst(dev_, checker.buffer_size(), buffer_reqs);
Chia-I Wu830fb332014-12-13 15:28:20 +08001027
1028 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +08001029 vkCmdCopyImageToBuffer(cmd_.handle(),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001030 img.obj(), VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001031 buf.obj(),
Chia-I Wu830fb332014-12-13 15:28:20 +08001032 checker.regions().size(), &checker.regions()[0]);
1033 cmd_.end();
1034
1035 submit_and_done();
1036
1037 checker.check(buf);
1038 }
1039
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001040 void test_copy_image_to_memory(const VkImageCreateInfo &img_info, const std::vector<VkBufferImageCopy> &regions)
Chia-I Wu830fb332014-12-13 15:28:20 +08001041 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001042 vk_testing::ImageChecker checker(img_info, regions);
Chia-I Wu830fb332014-12-13 15:28:20 +08001043 test_copy_image_to_memory(img_info, checker);
1044 }
1045
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001046 void test_copy_image_to_memory(const VkImageCreateInfo &img_info)
Chia-I Wu830fb332014-12-13 15:28:20 +08001047 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001048 vk_testing::ImageChecker checker(img_info);
Chia-I Wu830fb332014-12-13 15:28:20 +08001049 test_copy_image_to_memory(img_info, checker);
1050 }
1051};
1052
Tony Barbour01999182015-04-09 12:58:51 -06001053TEST_F(VkCmdCopyImageToBufferTest, Basic)
Chia-I Wu830fb332014-12-13 15:28:20 +08001054{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001055 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu830fb332014-12-13 15:28:20 +08001056 it != test_formats_.end(); it++) {
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001057
1058 // not sure what to do here
Tony Barbour8205d902015-04-16 15:59:00 -06001059 if (it->format == VK_FORMAT_UNDEFINED ||
1060 (it->format >= VK_FORMAT_B8G8R8_UNORM &&
1061 it->format <= VK_FORMAT_B8G8R8_SRGB))
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001062 continue;
1063
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001064 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -06001065 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu830fb332014-12-13 15:28:20 +08001066 img_info.format = it->format;
1067 img_info.extent.width = 64;
1068 img_info.extent.height = 64;
1069 img_info.tiling = it->tiling;
Cody Northrop025cbb12015-06-18 17:05:15 -06001070 img_info.usage = VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT;
Chia-I Wu830fb332014-12-13 15:28:20 +08001071
1072 test_copy_image_to_memory(img_info);
1073 }
1074}
1075
Tony Barbour01999182015-04-09 12:58:51 -06001076class VkCmdCopyImageTest : public VkCmdBlitImageTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001077protected:
1078 virtual void SetUp()
1079 {
Tony Barbour01999182015-04-09 12:58:51 -06001080 VkCmdBlitTest::SetUp();
Tony Barbour8205d902015-04-16 15:59:00 -06001081 init_test_formats(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001082 ASSERT_NE(true, test_formats_.empty());
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001083 }
1084
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001085 void test_copy_image(const VkImageCreateInfo &src_info, const VkImageCreateInfo &dst_info,
1086 const std::vector<VkImageCopy> &copies)
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001087 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001088 // convert VkImageCopy to two sets of VkBufferImageCopy
1089 std::vector<VkBufferImageCopy> src_regions, dst_regions;
Tony Barbour8205d902015-04-16 15:59:00 -06001090 VkDeviceSize src_offset = 0, dst_offset = 0;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001091 for (std::vector<VkImageCopy>::const_iterator it = copies.begin(); it != copies.end(); it++) {
1092 VkBufferImageCopy src_region = {}, dst_region = {};
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001093
Chia-I Wu714df452015-01-01 07:55:04 +08001094 src_region.bufferOffset = src_offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001095 src_region.imageSubresource = it->srcSubresource;
1096 src_region.imageOffset = it->srcOffset;
1097 src_region.imageExtent = it->extent;
1098 src_regions.push_back(src_region);
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001099
Chia-I Wu714df452015-01-01 07:55:04 +08001100 dst_region.bufferOffset = src_offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001101 dst_region.imageSubresource = it->destSubresource;
1102 dst_region.imageOffset = it->destOffset;
1103 dst_region.imageExtent = it->extent;
1104 dst_regions.push_back(dst_region);
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001105
Tony Barbour8205d902015-04-16 15:59:00 -06001106 const VkDeviceSize size = it->extent.width * it->extent.height * it->extent.depth;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001107 src_offset += vk_testing::get_format_size(src_info.format) * size;
1108 dst_offset += vk_testing::get_format_size(dst_info.format) * size;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001109 }
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001110
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001111 vk_testing::ImageChecker src_checker(src_info, src_regions);
1112 vk_testing::ImageChecker dst_checker(dst_info, dst_regions);
Tony Barbour43cdc952015-05-21 13:26:05 -06001113 VkMemoryPropertyFlags src_reqs =
1114 (src_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
1115 VkMemoryPropertyFlags dst_reqs =
1116 (dst_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
1117
1118
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001119
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001120 vk_testing::Image src;
Tony Barbour43cdc952015-05-21 13:26:05 -06001121 src.init(dev_, src_info, src_reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001122 fill_src(src, src_checker);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001123
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001124 vk_testing::Image dst;
Tony Barbour43cdc952015-05-21 13:26:05 -06001125 dst.init(dev_, dst_info, dst_reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001126
1127 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +08001128 vkCmdCopyImage(cmd_.handle(),
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001129 src.obj(), VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1130 dst.obj(), VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001131 copies.size(), &copies[0]);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001132 cmd_.end();
1133
1134 submit_and_done();
1135
1136 check_dst(dst, dst_checker);
1137 }
1138};
1139
Tony Barbour01999182015-04-09 12:58:51 -06001140TEST_F(VkCmdCopyImageTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001141{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001142 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001143 it != test_formats_.end(); it++) {
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001144
1145 // not sure what to do here
Tony Barbour8205d902015-04-16 15:59:00 -06001146 if (it->format == VK_FORMAT_UNDEFINED ||
1147 (it->format >= VK_FORMAT_B8G8R8_UNORM &&
1148 it->format <= VK_FORMAT_B8G8R8_SRGB))
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001149 continue;
1150
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001151 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -06001152 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001153 img_info.format = it->format;
1154 img_info.extent.width = 64;
1155 img_info.extent.height = 64;
1156 img_info.tiling = it->tiling;
Cody Northrop025cbb12015-06-18 17:05:15 -06001157 img_info.usage = VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT | VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001158
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001159 VkImageCopy copy = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001160 copy.srcSubresource = vk_testing::Image::subresource(VK_IMAGE_ASPECT_COLOR, 0, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001161 copy.destSubresource = copy.srcSubresource;
1162 copy.extent = img_info.extent;
1163
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001164 test_copy_image(img_info, img_info, std::vector<VkImageCopy>(&copy, &copy + 1));
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001165 }
1166}
1167
Tony Barbour01999182015-04-09 12:58:51 -06001168class VkCmdClearColorImageTest : public VkCmdBlitImageTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001169protected:
Chris Forbese3105972015-06-24 14:34:53 +12001170 VkCmdClearColorImageTest() {}
Chia-I Wu9feab842014-12-22 13:19:08 +08001171
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001172 virtual void SetUp()
1173 {
Tony Barbour01999182015-04-09 12:58:51 -06001174 VkCmdBlitTest::SetUp();
Chia-I Wu9feab842014-12-22 13:19:08 +08001175
Chris Forbese3105972015-06-24 14:34:53 +12001176 init_test_formats(VK_FORMAT_FEATURE_CONVERSION_BIT);
Chia-I Wu9feab842014-12-22 13:19:08 +08001177
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001178 ASSERT_NE(true, test_formats_.empty());
1179 }
1180
Chia-I Wu9feab842014-12-22 13:19:08 +08001181 union Color {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001182 float color[4];
1183 uint32_t raw[4];
Chia-I Wu9feab842014-12-22 13:19:08 +08001184 };
1185
Chris Forbese3105972015-06-24 14:34:53 +12001186 std::vector<uint8_t> color_to_raw(VkFormat format, const VkClearColorValue &color)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001187 {
1188 std::vector<uint8_t> raw;
1189
1190 // TODO support all formats
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001191 switch (format) {
Tony Barbour8205d902015-04-16 15:59:00 -06001192 case VK_FORMAT_R8G8B8A8_UNORM:
Chris Forbese3105972015-06-24 14:34:53 +12001193 raw.push_back((uint8_t)(color.f32[0] * 255.0f));
1194 raw.push_back((uint8_t)(color.f32[1] * 255.0f));
1195 raw.push_back((uint8_t)(color.f32[2] * 255.0f));
1196 raw.push_back((uint8_t)(color.f32[3] * 255.0f));
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001197 break;
Tony Barbour8205d902015-04-16 15:59:00 -06001198 case VK_FORMAT_B8G8R8A8_UNORM:
Chris Forbese3105972015-06-24 14:34:53 +12001199 raw.push_back((uint8_t)(color.f32[2] * 255.0f));
1200 raw.push_back((uint8_t)(color.f32[1] * 255.0f));
1201 raw.push_back((uint8_t)(color.f32[0] * 255.0f));
1202 raw.push_back((uint8_t)(color.f32[3] * 255.0f));
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001203 break;
1204 default:
1205 break;
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001206 }
1207
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001208 return raw;
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001209 }
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001210
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001211 void test_clear_color_image(const VkImageCreateInfo &img_info,
Chris Forbese3105972015-06-24 14:34:53 +12001212 const VkClearColorValue &clear_color,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001213 const std::vector<VkImageSubresourceRange> &ranges)
Chia-I Wucb67c652014-10-21 11:06:26 +08001214 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001215 vk_testing::Image img;
Tony Barbour43cdc952015-05-21 13:26:05 -06001216 VkMemoryPropertyFlags image_reqs =
1217 (img_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
Tony Barbour94310562015-04-22 15:10:33 -06001218
Tony Barbour43cdc952015-05-21 13:26:05 -06001219 img.init(dev_, img_info, image_reqs);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001220 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001221 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001222 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1223 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1224 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001225 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001226 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001227 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001228 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1229 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1230 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1231 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1232 VK_MEMORY_INPUT_SHADER_READ_BIT |
1233 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1234 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001235 VK_MEMORY_INPUT_TRANSFER_BIT;
Chia-I Wucb67c652014-10-21 11:06:26 +08001236
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001237 std::vector<VkImageMemoryBarrier> to_clear;
1238 std::vector<VkImageMemoryBarrier *> p_to_clear;
1239 std::vector<VkImageMemoryBarrier> to_xfer;
1240 std::vector<VkImageMemoryBarrier *> p_to_xfer;
Chia-I Wucb67c652014-10-21 11:06:26 +08001241
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001242 for (std::vector<VkImageSubresourceRange>::const_iterator it = ranges.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001243 it != ranges.end(); it++) {
Mike Stroyan55658c22014-12-04 11:08:39 +00001244 to_clear.push_back(img.image_memory_barrier(all_cache_outputs, all_cache_inputs,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001245 VK_IMAGE_LAYOUT_GENERAL,
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001246 VK_IMAGE_LAYOUT_GENERAL,
Mike Stroyan55658c22014-12-04 11:08:39 +00001247 *it));
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -06001248 p_to_clear.push_back(&to_clear.back());
Mike Stroyan55658c22014-12-04 11:08:39 +00001249 to_xfer.push_back(img.image_memory_barrier(all_cache_outputs, all_cache_inputs,
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001250 VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001251 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, *it));
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -06001252 p_to_xfer.push_back(&to_xfer.back());
Chia-I Wucb67c652014-10-21 11:06:26 +08001253 }
1254
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001255 cmd_.begin();
Chia-I Wu9feab842014-12-22 13:19:08 +08001256
Tony Barbourc2e987e2015-06-29 16:20:35 -06001257 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1258 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Chia-I Wu78c2a352015-07-03 11:49:42 +08001259 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, 1, (const void **)&p_to_clear[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001260
Chia-I Wu78c2a352015-07-03 11:49:42 +08001261 vkCmdClearColorImage(cmd_.handle(),
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001262 img.obj(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterda4a99e2015-04-23 17:49:22 -06001263 &clear_color, ranges.size(), &ranges[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001264
Chia-I Wu78c2a352015-07-03 11:49:42 +08001265 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, 1, (const void **)&p_to_xfer[0]);
Chia-I Wu9feab842014-12-22 13:19:08 +08001266
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001267 cmd_.end();
1268
1269 submit_and_done();
1270
1271 // cannot verify
1272 if (!img.transparent() && !img.copyable())
1273 return;
1274
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001275 vk_testing::ImageChecker checker(img_info, ranges);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001276
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -06001277 const std::vector<uint8_t> solid_pattern = color_to_raw(img_info.format, clear_color);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001278 if (solid_pattern.empty())
1279 return;
1280
1281 checker.set_solid_pattern(solid_pattern);
1282 check_dst(img, checker);
1283 }
Chia-I Wu9feab842014-12-22 13:19:08 +08001284
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001285 void test_clear_color_image(const VkImageCreateInfo &img_info,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001286 const float color[4],
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001287 const std::vector<VkImageSubresourceRange> &ranges)
Chia-I Wu9feab842014-12-22 13:19:08 +08001288 {
Chris Forbese3105972015-06-24 14:34:53 +12001289 VkClearColorValue c = {};
1290 memcpy(c.f32, color, sizeof(c.f32));
Chia-I Wu9feab842014-12-22 13:19:08 +08001291 test_clear_color_image(img_info, c, ranges);
1292 }
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001293};
1294
Tony Barbour01999182015-04-09 12:58:51 -06001295TEST_F(VkCmdClearColorImageTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001296{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001297 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001298 it != test_formats_.end(); it++) {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001299 const float color[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
Tony Barbour2275eb42015-06-05 12:53:55 -06001300 VkFormatProperties props;
Tony Barbour2275eb42015-06-05 12:53:55 -06001301 VkResult err;
1302
Chia-I Wuf5fb1092015-07-03 10:32:05 +08001303 err = vkGetPhysicalDeviceFormatInfo(dev_.phy().handle(), it->format, &props);
Tony Barbour2275eb42015-06-05 12:53:55 -06001304 ASSERT_EQ(err, VK_SUCCESS);
Chris Forbesd7576302015-06-21 22:55:02 +12001305
Tony Barbour2275eb42015-06-05 12:53:55 -06001306 if (it->tiling == VK_IMAGE_TILING_LINEAR && !(props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))
1307 continue;
1308
1309 if (it->tiling == VK_IMAGE_TILING_OPTIMAL && !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))
1310 continue;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001311
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001312 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -06001313 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001314 img_info.format = it->format;
1315 img_info.extent.width = 64;
1316 img_info.extent.height = 64;
1317 img_info.tiling = it->tiling;
Tony Barbour2275eb42015-06-05 12:53:55 -06001318 img_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001319
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001320 const VkImageSubresourceRange range =
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001321 vk_testing::Image::subresource_range(img_info, VK_IMAGE_ASPECT_COLOR);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001322 std::vector<VkImageSubresourceRange> ranges(&range, &range + 1);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001323
1324 test_clear_color_image(img_info, color, ranges);
Chia-I Wucb67c652014-10-21 11:06:26 +08001325 }
1326}
1327
Tony Barbour01999182015-04-09 12:58:51 -06001328class VkCmdClearDepthStencilTest : public VkCmdBlitImageTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001329protected:
1330 virtual void SetUp()
1331 {
Tony Barbour01999182015-04-09 12:58:51 -06001332 VkCmdBlitTest::SetUp();
Tony Barbour8205d902015-04-16 15:59:00 -06001333 init_test_formats(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001334 ASSERT_NE(true, test_formats_.empty());
1335 }
1336
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001337 std::vector<uint8_t> ds_to_raw(VkFormat format, float depth, uint32_t stencil)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001338 {
1339 std::vector<uint8_t> raw;
1340
1341 // depth
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001342 switch (format) {
Tony Barbour8205d902015-04-16 15:59:00 -06001343 case VK_FORMAT_D16_UNORM:
1344 case VK_FORMAT_D16_UNORM_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001345 {
Tony Barbourb223d482015-06-09 13:40:53 -06001346 const uint16_t unorm = (uint16_t)roundf(depth * 65535.0f);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001347 raw.push_back(unorm & 0xff);
1348 raw.push_back(unorm >> 8);
1349 }
1350 break;
Tony Barbour8205d902015-04-16 15:59:00 -06001351 case VK_FORMAT_D32_SFLOAT:
1352 case VK_FORMAT_D32_SFLOAT_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001353 {
1354 const union {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001355 float depth;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001356 uint32_t u32;
1357 } u = { depth };
1358
1359 raw.push_back((u.u32 ) & 0xff);
1360 raw.push_back((u.u32 >> 8) & 0xff);
1361 raw.push_back((u.u32 >> 16) & 0xff);
1362 raw.push_back((u.u32 >> 24) & 0xff);
1363 }
1364 break;
1365 default:
1366 break;
1367 }
1368
1369 // stencil
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001370 switch (format) {
Tony Barbour8205d902015-04-16 15:59:00 -06001371 case VK_FORMAT_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001372 raw.push_back(stencil);
1373 break;
Tony Barbour8205d902015-04-16 15:59:00 -06001374 case VK_FORMAT_D16_UNORM_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001375 raw.push_back(stencil);
1376 raw.push_back(0);
1377 break;
Tony Barbour8205d902015-04-16 15:59:00 -06001378 case VK_FORMAT_D32_SFLOAT_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001379 raw.push_back(stencil);
1380 raw.push_back(0);
1381 raw.push_back(0);
1382 raw.push_back(0);
1383 break;
1384 default:
1385 break;
1386 }
1387
1388 return raw;
1389 }
1390
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001391 void test_clear_depth_stencil(const VkImageCreateInfo &img_info,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001392 float depth, uint32_t stencil,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001393 const std::vector<VkImageSubresourceRange> &ranges)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001394 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001395 vk_testing::Image img;
Tony Barbour43cdc952015-05-21 13:26:05 -06001396 VkMemoryPropertyFlags image_reqs =
1397 (img_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
Tony Barbour94310562015-04-22 15:10:33 -06001398
Tony Barbour43cdc952015-05-21 13:26:05 -06001399 img.init(dev_, img_info, image_reqs);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001400 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001401 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001402 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1403 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1404 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001405 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001406 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001407 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001408 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1409 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1410 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1411 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1412 VK_MEMORY_INPUT_SHADER_READ_BIT |
1413 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1414 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001415 VK_MEMORY_INPUT_TRANSFER_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001416
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001417 std::vector<VkImageMemoryBarrier> to_clear;
1418 std::vector<VkImageMemoryBarrier *> p_to_clear;
1419 std::vector<VkImageMemoryBarrier> to_xfer;
1420 std::vector<VkImageMemoryBarrier *> p_to_xfer;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001421
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001422 for (std::vector<VkImageSubresourceRange>::const_iterator it = ranges.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001423 it != ranges.end(); it++) {
Mike Stroyan55658c22014-12-04 11:08:39 +00001424 to_clear.push_back(img.image_memory_barrier(all_cache_outputs, all_cache_inputs,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001425 VK_IMAGE_LAYOUT_GENERAL,
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001426 VK_IMAGE_LAYOUT_GENERAL,
Mike Stroyan55658c22014-12-04 11:08:39 +00001427 *it));
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -06001428 p_to_clear.push_back(&to_clear.back());
Mike Stroyan55658c22014-12-04 11:08:39 +00001429 to_xfer.push_back(img.image_memory_barrier(all_cache_outputs, all_cache_inputs,
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001430 VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001431 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, *it));
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -06001432 p_to_xfer.push_back(&to_xfer.back());
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001433 }
1434
1435 cmd_.begin();
Mike Stroyan55658c22014-12-04 11:08:39 +00001436
Tony Barbourc2e987e2015-06-29 16:20:35 -06001437 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1438 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Chia-I Wu78c2a352015-07-03 11:49:42 +08001439 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, to_clear.size(), (const void **)&p_to_clear[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001440
Chia-I Wu78c2a352015-07-03 11:49:42 +08001441 vkCmdClearDepthStencilImage(cmd_.handle(),
Chris Forbes2951d7d2015-06-22 17:21:59 +12001442 img.obj(), VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1443 depth, stencil,
1444 ranges.size(), &ranges[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001445
Chia-I Wu78c2a352015-07-03 11:49:42 +08001446 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, to_xfer.size(), (const void **)&p_to_xfer[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001447
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001448 cmd_.end();
1449
1450 submit_and_done();
1451
1452 // cannot verify
1453 if (!img.transparent() && !img.copyable())
1454 return;
1455
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001456 vk_testing::ImageChecker checker(img_info, ranges);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001457
1458 checker.set_solid_pattern(ds_to_raw(img_info.format, depth, stencil));
1459 check_dst(img, checker);
1460 }
1461};
1462
Tony Barbour01999182015-04-09 12:58:51 -06001463TEST_F(VkCmdClearDepthStencilTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001464{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001465 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001466 it != test_formats_.end(); it++) {
Tony Barbour2275eb42015-06-05 12:53:55 -06001467 VkFormatProperties props;
Tony Barbour2275eb42015-06-05 12:53:55 -06001468 VkResult err;
1469
Chia-I Wuf5fb1092015-07-03 10:32:05 +08001470 err = vkGetPhysicalDeviceFormatInfo(dev_.phy().handle(), it->format, &props);
Tony Barbour2275eb42015-06-05 12:53:55 -06001471 ASSERT_EQ(err, VK_SUCCESS);
Chris Forbesd7576302015-06-21 22:55:02 +12001472
Tony Barbour2275eb42015-06-05 12:53:55 -06001473 if (it->tiling == VK_IMAGE_TILING_LINEAR && !(props.linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))
1474 continue;
1475
1476 if (it->tiling == VK_IMAGE_TILING_OPTIMAL && !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))
1477 continue;
1478
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001479 // known driver issues
Tony Barbour8205d902015-04-16 15:59:00 -06001480 if (it->format == VK_FORMAT_S8_UINT ||
1481 it->format == VK_FORMAT_D24_UNORM ||
1482 it->format == VK_FORMAT_D16_UNORM_S8_UINT ||
1483 it->format == VK_FORMAT_D24_UNORM_S8_UINT)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001484 continue;
1485
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001486 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -06001487 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001488 img_info.format = it->format;
1489 img_info.extent.width = 64;
1490 img_info.extent.height = 64;
1491 img_info.tiling = it->tiling;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001492 img_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001493
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001494 const VkImageSubresourceRange range =
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001495 vk_testing::Image::subresource_range(img_info, VK_IMAGE_ASPECT_DEPTH);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001496 std::vector<VkImageSubresourceRange> ranges(&range, &range + 1);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001497
Tony Barbourb223d482015-06-09 13:40:53 -06001498 if (it->format == VK_FORMAT_D32_SFLOAT_S8_UINT ||
1499 it->format == VK_FORMAT_D16_UNORM_S8_UINT ||
1500 it->format == VK_FORMAT_D24_UNORM_S8_UINT) {
1501 const VkImageSubresourceRange range2 =
1502 vk_testing::Image::subresource_range(img_info, VK_IMAGE_ASPECT_STENCIL);
1503 ranges.push_back(range2);
1504 }
1505
1506
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001507 test_clear_depth_stencil(img_info, 0.25f, 63, ranges);
1508 }
1509}
1510
1511}; // namespace
1512
Chia-I Wucb67c652014-10-21 11:06:26 +08001513int main(int argc, char **argv)
1514{
Chia-I Wucb67c652014-10-21 11:06:26 +08001515 ::testing::InitGoogleTest(&argc, argv);
Chia-I Wucb67c652014-10-21 11:06:26 +08001516
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001517 vk_testing::set_error_callback(test_error_callback);
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001518
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001519 environment = new vk_testing::Environment();
Chia-I Wucb67c652014-10-21 11:06:26 +08001520
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001521 if (!environment->parse_args(argc, argv))
1522 return -1;
Chia-I Wucb67c652014-10-21 11:06:26 +08001523
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001524 ::testing::AddGlobalTestEnvironment(environment);
1525
1526 return RUN_ALL_TESTS();
Chia-I Wucb67c652014-10-21 11:06:26 +08001527}