blob: d7d0762b9512fc83dc6cec616f5108fbb3a49657 [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 Wuf4aed6c2015-07-03 13:44:34 +0800239 void *data = buf.memory().map();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800240 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 Wuf4aed6c2015-07-03 13:44:34 +0800249 buf.memory().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{
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800256 void *data = img.memory().map();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800257 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
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800266 img.memory().unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800267
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 Wuf4aed6c2015-07-03 13:44:34 +0800551 vkCmdFillBuffer(cmd_.handle(), buf.handle(), 0, 4, 0x11111111);
552 vkCmdFillBuffer(cmd_.handle(), buf.handle(), 4, 16, 0x22222222);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800553 cmd_.end();
554
555 submit_and_done();
556
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800557 const uint32_t *data = static_cast<const uint32_t *>(buf.memory().map());
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800558 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]);
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800563 buf.memory().unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800564}
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 Wuf4aed6c2015-07-03 13:44:34 +0800575 vkCmdFillBuffer(cmd_.handle(), buf.handle(), 0, size / 2, 0x11111111);
576 vkCmdFillBuffer(cmd_.handle(), buf.handle(), 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 Wuf4aed6c2015-07-03 13:44:34 +0800581 const uint32_t *data = static_cast<const uint32_t *>(buf.memory().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;
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800587 buf.memory().unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800588}
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 Wuf4aed6c2015-07-03 13:44:34 +0800598 vkCmdFillBuffer(cmd_.handle(), buf.handle(), 0, 48, 0x11111111);
599 vkCmdFillBuffer(cmd_.handle(), buf.handle(), 32, 32, 0x22222222);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800600 cmd_.end();
601
602 submit_and_done();
603
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800604 const uint32_t *data = static_cast<const uint32_t *>(buf.memory().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;
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800610 buf.memory().unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800611}
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 Wuf4aed6c2015-07-03 13:44:34 +0800622 vkCmdFillBuffer(cmd_.handle(), bufs[i].handle(), 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++) {
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800631 const uint32_t *data = static_cast<const uint32_t *>(bufs[i].memory().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;
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800636 bufs[i].memory().unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800637
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 Wuf4aed6c2015-07-03 13:44:34 +0800650 uint32_t *data = static_cast<uint32_t *>(src.memory().map());
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800651 data[0] = 0x11111111;
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800652 src.memory().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 Wuf4aed6c2015-07-03 13:44:34 +0800659 vkCmdCopyBuffer(cmd_.handle(), src.handle(), dst.handle(), 1, &region);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800660 cmd_.end();
661
662 submit_and_done();
663
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800664 data = static_cast<uint32_t *>(dst.memory().map());
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800665 EXPECT_EQ(0x11111111, data[0]);
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800666 dst.memory().unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800667}
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 Wuf4aed6c2015-07-03 13:44:34 +0800676 uint32_t *data = static_cast<uint32_t *>(src.memory().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;
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800680 src.memory().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 Wuf4aed6c2015-07-03 13:44:34 +0800687 vkCmdCopyBuffer(cmd_.handle(), src.handle(), dst.handle(), 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 Wuf4aed6c2015-07-03 13:44:34 +0800692 data = static_cast<uint32_t *>(dst.memory().map());
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800693 for (offset = 0; offset < size; offset += 4)
694 EXPECT_EQ(offset, data[offset / 4]);
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800695 dst.memory().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 Wuf4aed6c2015-07-03 13:44:34 +0800720 uint8_t *data = static_cast<uint8_t *>(src.memory().map());
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800721 for (int i = 0; i < 256; i++)
722 data[i] = i;
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800723 src.memory().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 Wuf4aed6c2015-07-03 13:44:34 +0800728 vkCmdCopyBuffer(cmd_.handle(), src.handle(), dst.handle(), 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 Wuf4aed6c2015-07-03 13:44:34 +0800733 data = static_cast<uint8_t *>(dst.memory().map());
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800734 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 }
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800743 dst.memory().unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800744}
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;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600751 VkResult err;
Tony Barbour94310562015-04-22 15:10:33 -0600752 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Mike Stroyan55658c22014-12-04 11:08:39 +0000753
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600754 // typedef struct VkEventCreateInfo_
Mike Stroyan55658c22014-12-04 11:08:39 +0000755 // {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600756 // VkStructureType sType; // Must be VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
Mark Lobodzinski72346292015-07-02 16:49:40 -0600757 // const void* pNext; // Pointer to next structure
758 // VkFlags flags; // Reserved
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600759 // } VkEventCreateInfo;
Mike Stroyan55658c22014-12-04 11:08:39 +0000760 memset(&event_info, 0, sizeof(event_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600761 event_info.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
Mike Stroyan55658c22014-12-04 11:08:39 +0000762
Chia-I Wua2636292015-07-03 10:41:20 +0800763 err = vkCreateEvent(dev_.handle(), &event_info, &event);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600764 ASSERT_VK_SUCCESS(err);
Mike Stroyan55658c22014-12-04 11:08:39 +0000765
Chia-I Wua2636292015-07-03 10:41:20 +0800766 err = vkResetEvent(dev_.handle(), event);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600767 ASSERT_VK_SUCCESS(err);
Chia-I Wucb67c652014-10-21 11:06:26 +0800768
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800769 for (int i = 0; i < ARRAY_SIZE(bufs); i++) {
Cody Northropd0e7d222015-06-22 14:56:14 -0600770 bufs[i].init_as_src_and_dst(dev_, 4, reqs);
Chia-I Wucb67c652014-10-21 11:06:26 +0800771
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800772 uint32_t *data = static_cast<uint32_t *>(bufs[i].memory().map());
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800773 data[0] = 0x22222222 * (i + 1);
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800774 bufs[i].memory().unmap();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800775 }
776
777 cmd_.begin();
778
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800779 vkCmdFillBuffer(cmd_.handle(), bufs[0].handle(), 0, 4, 0x11111111);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800780 // is this necessary?
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600781 VkBufferMemoryBarrier memory_barrier = bufs[0].buffer_memory_barrier(
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600782 VK_MEMORY_OUTPUT_TRANSFER_BIT, VK_MEMORY_INPUT_TRANSFER_BIT, 0, 4);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600783 VkBufferMemoryBarrier *pmemory_barrier = &memory_barrier;
Mike Stroyan55658c22014-12-04 11:08:39 +0000784
Tony Barbourc2e987e2015-06-29 16:20:35 -0600785 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
786 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Chia-I Wu78c2a352015-07-03 11:49:42 +0800787 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800788
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600789 VkBufferCopy region = {};
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800790 region.copySize = 4;
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800791 vkCmdCopyBuffer(cmd_.handle(), bufs[0].handle(), bufs[1].handle(), 1, &region);
Mike Stroyan55658c22014-12-04 11:08:39 +0000792
793 memory_barrier = bufs[1].buffer_memory_barrier(
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600794 VK_MEMORY_OUTPUT_TRANSFER_BIT, VK_MEMORY_INPUT_TRANSFER_BIT, 0, 4);
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -0600795 pmemory_barrier = &memory_barrier;
Chia-I Wu78c2a352015-07-03 11:49:42 +0800796 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800797
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800798 vkCmdCopyBuffer(cmd_.handle(), bufs[1].handle(), bufs[2].handle(), 1, &region);
Mike Stroyan55658c22014-12-04 11:08:39 +0000799
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600800 /* Use vkCmdSetEvent and vkCmdWaitEvents to test them.
801 * This could be vkCmdPipelineBarrier.
Mike Stroyan55658c22014-12-04 11:08:39 +0000802 */
Chia-I Wu78c2a352015-07-03 11:49:42 +0800803 vkCmdSetEvent(cmd_.handle(), event, VK_PIPELINE_STAGE_TRANSFER_BIT);
Mike Stroyan55658c22014-12-04 11:08:39 +0000804
805 // Additional commands could go into the buffer here before the wait.
806
807 memory_barrier = bufs[1].buffer_memory_barrier(
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600808 VK_MEMORY_OUTPUT_TRANSFER_BIT, VK_MEMORY_INPUT_HOST_READ_BIT, 0, 4);
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -0600809 pmemory_barrier = &memory_barrier;
Chia-I Wu78c2a352015-07-03 11:49:42 +0800810 vkCmdWaitEvents(cmd_.handle(), 1, &event, src_stages, dest_stages, 1, (const void **)&pmemory_barrier);
Mike Stroyan55658c22014-12-04 11:08:39 +0000811
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800812 cmd_.end();
813
814 submit_and_done();
815
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800816 const uint32_t *data = static_cast<const uint32_t *>(bufs[2].memory().map());
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800817 EXPECT_EQ(0x11111111, data[0]);
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800818 bufs[2].memory().unmap();
Mike Stroyan55658c22014-12-04 11:08:39 +0000819
Tony Barboure84a8d62015-07-10 14:10:27 -0600820 err = vkDestroyEvent(dev_.handle(), event);
Tony Barbour4003ae82015-06-26 11:49:05 -0600821 ASSERT_VK_SUCCESS(err);
822
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800823}
824
Tony Barbour01999182015-04-09 12:58:51 -0600825class VkCmdBlitImageTest : public VkCmdBlitTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800826protected:
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600827 void init_test_formats(VkFlags features)
Chia-I Wucb67c652014-10-21 11:06:26 +0800828 {
Tony Barbour8205d902015-04-16 15:59:00 -0600829 first_linear_format_ = VK_FORMAT_UNDEFINED;
830 first_optimal_format_ = VK_FORMAT_UNDEFINED;
Chia-I Wucb67c652014-10-21 11:06:26 +0800831
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600832 for (std::vector<vk_testing::Device::Format>::const_iterator it = dev_.formats().begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800833 it != dev_.formats().end(); it++) {
834 if (it->features & features) {
835 test_formats_.push_back(*it);
Chia-I Wucb67c652014-10-21 11:06:26 +0800836
Tony Barbour8205d902015-04-16 15:59:00 -0600837 if (it->tiling == VK_IMAGE_TILING_LINEAR &&
838 first_linear_format_ == VK_FORMAT_UNDEFINED)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800839 first_linear_format_ = it->format;
Tony Barbour8205d902015-04-16 15:59:00 -0600840 if (it->tiling == VK_IMAGE_TILING_OPTIMAL &&
841 first_optimal_format_ == VK_FORMAT_UNDEFINED)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800842 first_optimal_format_ = it->format;
843 }
844 }
845 }
Chia-I Wucb67c652014-10-21 11:06:26 +0800846
Chia-I Wu9feab842014-12-22 13:19:08 +0800847 void init_test_formats()
848 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600849 init_test_formats(static_cast<VkFlags>(-1));
Chia-I Wu9feab842014-12-22 13:19:08 +0800850 }
851
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600852 void fill_src(vk_testing::Image &img, const vk_testing::ImageChecker &checker)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800853 {
854 if (img.transparent()) {
855 checker.fill(img);
856 return;
Chia-I Wucb67c652014-10-21 11:06:26 +0800857 }
858
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800859 ASSERT_EQ(true, img.copyable());
860
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600861 vk_testing::Buffer in_buf;
Tony Barbour94310562015-04-22 15:10:33 -0600862 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
863
Cody Northropd0e7d222015-06-22 14:56:14 -0600864 in_buf.init_as_src(dev_, checker.buffer_size(), reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800865 checker.fill(in_buf);
866
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800867 // copy in and tile
868 cmd_.begin();
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800869 vkCmdCopyBufferToImage(cmd_.handle(), in_buf.handle(),
870 img.handle(), VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800871 checker.regions().size(), &checker.regions()[0]);
872 cmd_.end();
873
874 submit_and_done();
Chia-I Wucb67c652014-10-21 11:06:26 +0800875 }
876
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600877 void check_dst(vk_testing::Image &img, const vk_testing::ImageChecker &checker)
Chia-I Wu86822632014-11-22 15:09:42 +0800878 {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800879 if (img.transparent()) {
880 DO(checker.check(img));
881 return;
Chia-I Wu86822632014-11-22 15:09:42 +0800882 }
883
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800884 ASSERT_EQ(true, img.copyable());
885
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600886 vk_testing::Buffer out_buf;
Tony Barbour94310562015-04-22 15:10:33 -0600887 VkMemoryPropertyFlags reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Cody Northropd0e7d222015-06-22 14:56:14 -0600888 out_buf.init_as_dst(dev_, checker.buffer_size(), reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800889
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800890 // copy out and linearize
891 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +0800892 vkCmdCopyImageToBuffer(cmd_.handle(),
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800893 img.handle(), VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
894 out_buf.handle(),
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800895 checker.regions().size(), &checker.regions()[0]);
896 cmd_.end();
897
898 submit_and_done();
899
900 DO(checker.check(out_buf));
Chia-I Wu86822632014-11-22 15:09:42 +0800901 }
902
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600903 std::vector<vk_testing::Device::Format> test_formats_;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600904 VkFormat first_linear_format_;
905 VkFormat first_optimal_format_;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800906};
907
Tony Barbour01999182015-04-09 12:58:51 -0600908class VkCmdCopyBufferToImageTest : public VkCmdBlitImageTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800909protected:
910 virtual void SetUp()
911 {
Tony Barbour01999182015-04-09 12:58:51 -0600912 VkCmdBlitTest::SetUp();
Tony Barbour8205d902015-04-16 15:59:00 -0600913 init_test_formats(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800914 ASSERT_NE(true, test_formats_.empty());
915 }
916
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600917 void test_copy_memory_to_image(const VkImageCreateInfo &img_info, const vk_testing::ImageChecker &checker)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800918 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600919 vk_testing::Buffer buf;
920 vk_testing::Image img;
Tony Barbour43cdc952015-05-21 13:26:05 -0600921 VkMemoryPropertyFlags buffer_reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
922 VkMemoryPropertyFlags image_reqs =
923 (img_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800924
Cody Northropd0e7d222015-06-22 14:56:14 -0600925 buf.init_as_src(dev_, checker.buffer_size(), buffer_reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800926 checker.fill(buf);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800927
Tony Barbour43cdc952015-05-21 13:26:05 -0600928 img.init(dev_, img_info, image_reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800929
930 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +0800931 vkCmdCopyBufferToImage(cmd_.handle(),
Chia-I Wuf4aed6c2015-07-03 13:44:34 +0800932 buf.handle(),
933 img.handle(), VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800934 checker.regions().size(), &checker.regions()[0]);
935 cmd_.end();
936
937 submit_and_done();
938
939 check_dst(img, checker);
940 }
941
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600942 void test_copy_memory_to_image(const VkImageCreateInfo &img_info, const std::vector<VkBufferImageCopy> &regions)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800943 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600944 vk_testing::ImageChecker checker(img_info, regions);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800945 test_copy_memory_to_image(img_info, checker);
946 }
947
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600948 void test_copy_memory_to_image(const VkImageCreateInfo &img_info)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800949 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600950 vk_testing::ImageChecker checker(img_info);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800951 test_copy_memory_to_image(img_info, checker);
952 }
953};
954
Tony Barbour01999182015-04-09 12:58:51 -0600955TEST_F(VkCmdCopyBufferToImageTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800956{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600957 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800958 it != test_formats_.end(); it++) {
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700959
960 // not sure what to do here
Tony Barbour8205d902015-04-16 15:59:00 -0600961 if (it->format == VK_FORMAT_UNDEFINED ||
962 (it->format >= VK_FORMAT_B8G8R8_UNORM &&
963 it->format <= VK_FORMAT_B8G8R8_SRGB))
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700964 continue;
965
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600966 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -0600967 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800968 img_info.format = it->format;
969 img_info.extent.width = 64;
970 img_info.extent.height = 64;
971 img_info.tiling = it->tiling;
Cody Northrop025cbb12015-06-18 17:05:15 -0600972 img_info.usage = VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800973
974 test_copy_memory_to_image(img_info);
975 }
Chia-I Wu86822632014-11-22 15:09:42 +0800976}
977
Tony Barbour01999182015-04-09 12:58:51 -0600978class VkCmdCopyImageToBufferTest : public VkCmdBlitImageTest {
Chia-I Wu830fb332014-12-13 15:28:20 +0800979protected:
980 virtual void SetUp()
981 {
Tony Barbour01999182015-04-09 12:58:51 -0600982 VkCmdBlitTest::SetUp();
Tony Barbour8205d902015-04-16 15:59:00 -0600983 init_test_formats(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT);
Chia-I Wu830fb332014-12-13 15:28:20 +0800984 ASSERT_NE(true, test_formats_.empty());
985 }
986
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600987 void test_copy_image_to_memory(const VkImageCreateInfo &img_info, const vk_testing::ImageChecker &checker)
Chia-I Wu830fb332014-12-13 15:28:20 +0800988 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600989 vk_testing::Image img;
990 vk_testing::Buffer buf;
Tony Barbour43cdc952015-05-21 13:26:05 -0600991 VkMemoryPropertyFlags buffer_reqs = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
992 VkMemoryPropertyFlags image_reqs =
993 (img_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
Chia-I Wu830fb332014-12-13 15:28:20 +0800994
Tony Barbour43cdc952015-05-21 13:26:05 -0600995 img.init(dev_, img_info, image_reqs);
Chia-I Wu830fb332014-12-13 15:28:20 +0800996 fill_src(img, checker);
Chia-I Wu830fb332014-12-13 15:28:20 +0800997
Cody Northropd0e7d222015-06-22 14:56:14 -0600998 buf.init_as_dst(dev_, checker.buffer_size(), buffer_reqs);
Chia-I Wu830fb332014-12-13 15:28:20 +0800999
1000 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +08001001 vkCmdCopyImageToBuffer(cmd_.handle(),
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001002 img.handle(), VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1003 buf.handle(),
Chia-I Wu830fb332014-12-13 15:28:20 +08001004 checker.regions().size(), &checker.regions()[0]);
1005 cmd_.end();
1006
1007 submit_and_done();
1008
1009 checker.check(buf);
1010 }
1011
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001012 void test_copy_image_to_memory(const VkImageCreateInfo &img_info, const std::vector<VkBufferImageCopy> &regions)
Chia-I Wu830fb332014-12-13 15:28:20 +08001013 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001014 vk_testing::ImageChecker checker(img_info, regions);
Chia-I Wu830fb332014-12-13 15:28:20 +08001015 test_copy_image_to_memory(img_info, checker);
1016 }
1017
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001018 void test_copy_image_to_memory(const VkImageCreateInfo &img_info)
Chia-I Wu830fb332014-12-13 15:28:20 +08001019 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001020 vk_testing::ImageChecker checker(img_info);
Chia-I Wu830fb332014-12-13 15:28:20 +08001021 test_copy_image_to_memory(img_info, checker);
1022 }
1023};
1024
Tony Barbour01999182015-04-09 12:58:51 -06001025TEST_F(VkCmdCopyImageToBufferTest, Basic)
Chia-I Wu830fb332014-12-13 15:28:20 +08001026{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001027 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu830fb332014-12-13 15:28:20 +08001028 it != test_formats_.end(); it++) {
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001029
1030 // not sure what to do here
Tony Barbour8205d902015-04-16 15:59:00 -06001031 if (it->format == VK_FORMAT_UNDEFINED ||
1032 (it->format >= VK_FORMAT_B8G8R8_UNORM &&
1033 it->format <= VK_FORMAT_B8G8R8_SRGB))
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001034 continue;
1035
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001036 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -06001037 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu830fb332014-12-13 15:28:20 +08001038 img_info.format = it->format;
1039 img_info.extent.width = 64;
1040 img_info.extent.height = 64;
1041 img_info.tiling = it->tiling;
Cody Northrop025cbb12015-06-18 17:05:15 -06001042 img_info.usage = VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT;
Chia-I Wu830fb332014-12-13 15:28:20 +08001043
1044 test_copy_image_to_memory(img_info);
1045 }
1046}
1047
Tony Barbour01999182015-04-09 12:58:51 -06001048class VkCmdCopyImageTest : public VkCmdBlitImageTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001049protected:
1050 virtual void SetUp()
1051 {
Tony Barbour01999182015-04-09 12:58:51 -06001052 VkCmdBlitTest::SetUp();
Tony Barbour8205d902015-04-16 15:59:00 -06001053 init_test_formats(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001054 ASSERT_NE(true, test_formats_.empty());
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001055 }
1056
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001057 void test_copy_image(const VkImageCreateInfo &src_info, const VkImageCreateInfo &dst_info,
1058 const std::vector<VkImageCopy> &copies)
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001059 {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001060 // convert VkImageCopy to two sets of VkBufferImageCopy
1061 std::vector<VkBufferImageCopy> src_regions, dst_regions;
Tony Barbour8205d902015-04-16 15:59:00 -06001062 VkDeviceSize src_offset = 0, dst_offset = 0;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001063 for (std::vector<VkImageCopy>::const_iterator it = copies.begin(); it != copies.end(); it++) {
1064 VkBufferImageCopy src_region = {}, dst_region = {};
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001065
Chia-I Wu714df452015-01-01 07:55:04 +08001066 src_region.bufferOffset = src_offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001067 src_region.imageSubresource = it->srcSubresource;
1068 src_region.imageOffset = it->srcOffset;
1069 src_region.imageExtent = it->extent;
1070 src_regions.push_back(src_region);
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001071
Chia-I Wu714df452015-01-01 07:55:04 +08001072 dst_region.bufferOffset = src_offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001073 dst_region.imageSubresource = it->destSubresource;
1074 dst_region.imageOffset = it->destOffset;
1075 dst_region.imageExtent = it->extent;
1076 dst_regions.push_back(dst_region);
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001077
Tony Barbour8205d902015-04-16 15:59:00 -06001078 const VkDeviceSize size = it->extent.width * it->extent.height * it->extent.depth;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001079 src_offset += vk_testing::get_format_size(src_info.format) * size;
1080 dst_offset += vk_testing::get_format_size(dst_info.format) * size;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001081 }
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001082
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001083 vk_testing::ImageChecker src_checker(src_info, src_regions);
1084 vk_testing::ImageChecker dst_checker(dst_info, dst_regions);
Tony Barbour43cdc952015-05-21 13:26:05 -06001085 VkMemoryPropertyFlags src_reqs =
1086 (src_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
1087 VkMemoryPropertyFlags dst_reqs =
1088 (dst_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
1089
1090
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001091
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001092 vk_testing::Image src;
Tony Barbour43cdc952015-05-21 13:26:05 -06001093 src.init(dev_, src_info, src_reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001094 fill_src(src, src_checker);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001095
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001096 vk_testing::Image dst;
Tony Barbour43cdc952015-05-21 13:26:05 -06001097 dst.init(dev_, dst_info, dst_reqs);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001098
1099 cmd_.begin();
Chia-I Wu78c2a352015-07-03 11:49:42 +08001100 vkCmdCopyImage(cmd_.handle(),
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001101 src.handle(), VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1102 dst.handle(), VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001103 copies.size(), &copies[0]);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001104 cmd_.end();
1105
1106 submit_and_done();
1107
1108 check_dst(dst, dst_checker);
1109 }
1110};
1111
Tony Barbour01999182015-04-09 12:58:51 -06001112TEST_F(VkCmdCopyImageTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001113{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001114 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001115 it != test_formats_.end(); it++) {
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001116
1117 // not sure what to do here
Tony Barbour8205d902015-04-16 15:59:00 -06001118 if (it->format == VK_FORMAT_UNDEFINED ||
1119 (it->format >= VK_FORMAT_B8G8R8_UNORM &&
1120 it->format <= VK_FORMAT_B8G8R8_SRGB))
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001121 continue;
1122
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001123 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -06001124 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001125 img_info.format = it->format;
1126 img_info.extent.width = 64;
1127 img_info.extent.height = 64;
1128 img_info.tiling = it->tiling;
Cody Northrop025cbb12015-06-18 17:05:15 -06001129 img_info.usage = VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT | VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001130
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001131 VkImageCopy copy = {};
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001132 copy.srcSubresource = vk_testing::Image::subresource(VK_IMAGE_ASPECT_COLOR, 0, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001133 copy.destSubresource = copy.srcSubresource;
1134 copy.extent = img_info.extent;
1135
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001136 test_copy_image(img_info, img_info, std::vector<VkImageCopy>(&copy, &copy + 1));
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001137 }
1138}
1139
Tony Barbour01999182015-04-09 12:58:51 -06001140class VkCmdClearColorImageTest : public VkCmdBlitImageTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001141protected:
Chris Forbese3105972015-06-24 14:34:53 +12001142 VkCmdClearColorImageTest() {}
Chia-I Wu9feab842014-12-22 13:19:08 +08001143
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001144 virtual void SetUp()
1145 {
Tony Barbour01999182015-04-09 12:58:51 -06001146 VkCmdBlitTest::SetUp();
Chia-I Wu9feab842014-12-22 13:19:08 +08001147
Chris Forbese3105972015-06-24 14:34:53 +12001148 init_test_formats(VK_FORMAT_FEATURE_CONVERSION_BIT);
Chia-I Wu9feab842014-12-22 13:19:08 +08001149
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001150 ASSERT_NE(true, test_formats_.empty());
1151 }
1152
Chia-I Wu9feab842014-12-22 13:19:08 +08001153 union Color {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001154 float color[4];
1155 uint32_t raw[4];
Chia-I Wu9feab842014-12-22 13:19:08 +08001156 };
1157
Chris Forbese3105972015-06-24 14:34:53 +12001158 std::vector<uint8_t> color_to_raw(VkFormat format, const VkClearColorValue &color)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001159 {
1160 std::vector<uint8_t> raw;
1161
1162 // TODO support all formats
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001163 switch (format) {
Tony Barbour8205d902015-04-16 15:59:00 -06001164 case VK_FORMAT_R8G8B8A8_UNORM:
Chris Forbese3105972015-06-24 14:34:53 +12001165 raw.push_back((uint8_t)(color.f32[0] * 255.0f));
1166 raw.push_back((uint8_t)(color.f32[1] * 255.0f));
1167 raw.push_back((uint8_t)(color.f32[2] * 255.0f));
1168 raw.push_back((uint8_t)(color.f32[3] * 255.0f));
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001169 break;
Tony Barbour8205d902015-04-16 15:59:00 -06001170 case VK_FORMAT_B8G8R8A8_UNORM:
Chris Forbese3105972015-06-24 14:34:53 +12001171 raw.push_back((uint8_t)(color.f32[2] * 255.0f));
1172 raw.push_back((uint8_t)(color.f32[1] * 255.0f));
1173 raw.push_back((uint8_t)(color.f32[0] * 255.0f));
1174 raw.push_back((uint8_t)(color.f32[3] * 255.0f));
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001175 break;
1176 default:
1177 break;
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001178 }
1179
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001180 return raw;
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001181 }
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001182
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001183 void test_clear_color_image(const VkImageCreateInfo &img_info,
Chris Forbese3105972015-06-24 14:34:53 +12001184 const VkClearColorValue &clear_color,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001185 const std::vector<VkImageSubresourceRange> &ranges)
Chia-I Wucb67c652014-10-21 11:06:26 +08001186 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001187 vk_testing::Image img;
Tony Barbour43cdc952015-05-21 13:26:05 -06001188 VkMemoryPropertyFlags image_reqs =
1189 (img_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
Tony Barbour94310562015-04-22 15:10:33 -06001190
Tony Barbour43cdc952015-05-21 13:26:05 -06001191 img.init(dev_, img_info, image_reqs);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001192 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001193 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001194 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1195 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1196 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001197 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001198 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001199 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001200 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1201 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1202 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1203 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1204 VK_MEMORY_INPUT_SHADER_READ_BIT |
1205 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1206 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001207 VK_MEMORY_INPUT_TRANSFER_BIT;
Chia-I Wucb67c652014-10-21 11:06:26 +08001208
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001209 std::vector<VkImageMemoryBarrier> to_clear;
1210 std::vector<VkImageMemoryBarrier *> p_to_clear;
1211 std::vector<VkImageMemoryBarrier> to_xfer;
1212 std::vector<VkImageMemoryBarrier *> p_to_xfer;
Chia-I Wucb67c652014-10-21 11:06:26 +08001213
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001214 for (std::vector<VkImageSubresourceRange>::const_iterator it = ranges.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001215 it != ranges.end(); it++) {
Mike Stroyan55658c22014-12-04 11:08:39 +00001216 to_clear.push_back(img.image_memory_barrier(all_cache_outputs, all_cache_inputs,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001217 VK_IMAGE_LAYOUT_GENERAL,
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001218 VK_IMAGE_LAYOUT_GENERAL,
Mike Stroyan55658c22014-12-04 11:08:39 +00001219 *it));
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -06001220 p_to_clear.push_back(&to_clear.back());
Mike Stroyan55658c22014-12-04 11:08:39 +00001221 to_xfer.push_back(img.image_memory_barrier(all_cache_outputs, all_cache_inputs,
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001222 VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001223 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, *it));
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -06001224 p_to_xfer.push_back(&to_xfer.back());
Chia-I Wucb67c652014-10-21 11:06:26 +08001225 }
1226
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001227 cmd_.begin();
Chia-I Wu9feab842014-12-22 13:19:08 +08001228
Tony Barbourc2e987e2015-06-29 16:20:35 -06001229 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1230 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Chia-I Wu78c2a352015-07-03 11:49:42 +08001231 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, 1, (const void **)&p_to_clear[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001232
Chia-I Wu78c2a352015-07-03 11:49:42 +08001233 vkCmdClearColorImage(cmd_.handle(),
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001234 img.handle(), VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchterda4a99e2015-04-23 17:49:22 -06001235 &clear_color, ranges.size(), &ranges[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001236
Chia-I Wu78c2a352015-07-03 11:49:42 +08001237 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, 1, (const void **)&p_to_xfer[0]);
Chia-I Wu9feab842014-12-22 13:19:08 +08001238
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001239 cmd_.end();
1240
1241 submit_and_done();
1242
1243 // cannot verify
1244 if (!img.transparent() && !img.copyable())
1245 return;
1246
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001247 vk_testing::ImageChecker checker(img_info, ranges);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001248
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -06001249 const std::vector<uint8_t> solid_pattern = color_to_raw(img_info.format, clear_color);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001250 if (solid_pattern.empty())
1251 return;
1252
1253 checker.set_solid_pattern(solid_pattern);
1254 check_dst(img, checker);
1255 }
Chia-I Wu9feab842014-12-22 13:19:08 +08001256
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001257 void test_clear_color_image(const VkImageCreateInfo &img_info,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001258 const float color[4],
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001259 const std::vector<VkImageSubresourceRange> &ranges)
Chia-I Wu9feab842014-12-22 13:19:08 +08001260 {
Chris Forbese3105972015-06-24 14:34:53 +12001261 VkClearColorValue c = {};
1262 memcpy(c.f32, color, sizeof(c.f32));
Chia-I Wu9feab842014-12-22 13:19:08 +08001263 test_clear_color_image(img_info, c, ranges);
1264 }
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001265};
1266
Tony Barbour01999182015-04-09 12:58:51 -06001267TEST_F(VkCmdClearColorImageTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001268{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001269 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001270 it != test_formats_.end(); it++) {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001271 const float color[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
Tony Barbour2275eb42015-06-05 12:53:55 -06001272 VkFormatProperties props;
Tony Barbour2275eb42015-06-05 12:53:55 -06001273 VkResult err;
1274
Chia-I Wuf5fb1092015-07-03 10:32:05 +08001275 err = vkGetPhysicalDeviceFormatInfo(dev_.phy().handle(), it->format, &props);
Tony Barbour2275eb42015-06-05 12:53:55 -06001276 ASSERT_EQ(err, VK_SUCCESS);
Chris Forbesd7576302015-06-21 22:55:02 +12001277
Tony Barbour2275eb42015-06-05 12:53:55 -06001278 if (it->tiling == VK_IMAGE_TILING_LINEAR && !(props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))
1279 continue;
1280
1281 if (it->tiling == VK_IMAGE_TILING_OPTIMAL && !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))
1282 continue;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001283
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001284 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -06001285 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001286 img_info.format = it->format;
1287 img_info.extent.width = 64;
1288 img_info.extent.height = 64;
1289 img_info.tiling = it->tiling;
Tony Barbour2275eb42015-06-05 12:53:55 -06001290 img_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001291
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001292 const VkImageSubresourceRange range =
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001293 vk_testing::Image::subresource_range(img_info, VK_IMAGE_ASPECT_COLOR);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001294 std::vector<VkImageSubresourceRange> ranges(&range, &range + 1);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001295
1296 test_clear_color_image(img_info, color, ranges);
Chia-I Wucb67c652014-10-21 11:06:26 +08001297 }
1298}
1299
Tony Barbour01999182015-04-09 12:58:51 -06001300class VkCmdClearDepthStencilTest : public VkCmdBlitImageTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001301protected:
1302 virtual void SetUp()
1303 {
Tony Barbour01999182015-04-09 12:58:51 -06001304 VkCmdBlitTest::SetUp();
Tony Barbour8205d902015-04-16 15:59:00 -06001305 init_test_formats(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001306 ASSERT_NE(true, test_formats_.empty());
1307 }
1308
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001309 std::vector<uint8_t> ds_to_raw(VkFormat format, float depth, uint32_t stencil)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001310 {
1311 std::vector<uint8_t> raw;
1312
1313 // depth
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001314 switch (format) {
Tony Barbour8205d902015-04-16 15:59:00 -06001315 case VK_FORMAT_D16_UNORM:
1316 case VK_FORMAT_D16_UNORM_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001317 {
Tony Barbourb223d482015-06-09 13:40:53 -06001318 const uint16_t unorm = (uint16_t)roundf(depth * 65535.0f);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001319 raw.push_back(unorm & 0xff);
1320 raw.push_back(unorm >> 8);
1321 }
1322 break;
Tony Barbour8205d902015-04-16 15:59:00 -06001323 case VK_FORMAT_D32_SFLOAT:
1324 case VK_FORMAT_D32_SFLOAT_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001325 {
1326 const union {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001327 float depth;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001328 uint32_t u32;
1329 } u = { depth };
1330
1331 raw.push_back((u.u32 ) & 0xff);
1332 raw.push_back((u.u32 >> 8) & 0xff);
1333 raw.push_back((u.u32 >> 16) & 0xff);
1334 raw.push_back((u.u32 >> 24) & 0xff);
1335 }
1336 break;
1337 default:
1338 break;
1339 }
1340
1341 // stencil
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001342 switch (format) {
Tony Barbour8205d902015-04-16 15:59:00 -06001343 case VK_FORMAT_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001344 raw.push_back(stencil);
1345 break;
Tony Barbour8205d902015-04-16 15:59:00 -06001346 case VK_FORMAT_D16_UNORM_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001347 raw.push_back(stencil);
1348 raw.push_back(0);
1349 break;
Tony Barbour8205d902015-04-16 15:59:00 -06001350 case VK_FORMAT_D32_SFLOAT_S8_UINT:
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001351 raw.push_back(stencil);
1352 raw.push_back(0);
1353 raw.push_back(0);
1354 raw.push_back(0);
1355 break;
1356 default:
1357 break;
1358 }
1359
1360 return raw;
1361 }
1362
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001363 void test_clear_depth_stencil(const VkImageCreateInfo &img_info,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001364 float depth, uint32_t stencil,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001365 const std::vector<VkImageSubresourceRange> &ranges)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001366 {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001367 vk_testing::Image img;
Tony Barbour43cdc952015-05-21 13:26:05 -06001368 VkMemoryPropertyFlags image_reqs =
1369 (img_info.tiling == VK_IMAGE_TILING_LINEAR)?VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT:0;
Tony Barbour94310562015-04-22 15:10:33 -06001370
Tony Barbour43cdc952015-05-21 13:26:05 -06001371 img.init(dev_, img_info, image_reqs);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001372 const VkFlags all_cache_outputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001373 VK_MEMORY_OUTPUT_HOST_WRITE_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001374 VK_MEMORY_OUTPUT_SHADER_WRITE_BIT |
1375 VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT |
1376 VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001377 VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001378 const VkFlags all_cache_inputs =
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -06001379 VK_MEMORY_INPUT_HOST_READ_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001380 VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT |
1381 VK_MEMORY_INPUT_INDEX_FETCH_BIT |
1382 VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT |
1383 VK_MEMORY_INPUT_UNIFORM_READ_BIT |
1384 VK_MEMORY_INPUT_SHADER_READ_BIT |
1385 VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT |
1386 VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT |
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001387 VK_MEMORY_INPUT_TRANSFER_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001388
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001389 std::vector<VkImageMemoryBarrier> to_clear;
1390 std::vector<VkImageMemoryBarrier *> p_to_clear;
1391 std::vector<VkImageMemoryBarrier> to_xfer;
1392 std::vector<VkImageMemoryBarrier *> p_to_xfer;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001393
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001394 for (std::vector<VkImageSubresourceRange>::const_iterator it = ranges.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001395 it != ranges.end(); it++) {
Mike Stroyan55658c22014-12-04 11:08:39 +00001396 to_clear.push_back(img.image_memory_barrier(all_cache_outputs, all_cache_inputs,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001397 VK_IMAGE_LAYOUT_GENERAL,
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001398 VK_IMAGE_LAYOUT_GENERAL,
Mike Stroyan55658c22014-12-04 11:08:39 +00001399 *it));
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -06001400 p_to_clear.push_back(&to_clear.back());
Mike Stroyan55658c22014-12-04 11:08:39 +00001401 to_xfer.push_back(img.image_memory_barrier(all_cache_outputs, all_cache_inputs,
Mark Lobodzinski4e97c452015-07-01 15:18:26 -06001402 VK_IMAGE_LAYOUT_GENERAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001403 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, *it));
Mark Lobodzinskid3eabd72015-01-29 14:24:14 -06001404 p_to_xfer.push_back(&to_xfer.back());
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001405 }
1406
1407 cmd_.begin();
Mike Stroyan55658c22014-12-04 11:08:39 +00001408
Tony Barbourc2e987e2015-06-29 16:20:35 -06001409 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
1410 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_GPU_COMMANDS;
Chia-I Wu78c2a352015-07-03 11:49:42 +08001411 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, to_clear.size(), (const void **)&p_to_clear[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001412
Chia-I Wu78c2a352015-07-03 11:49:42 +08001413 vkCmdClearDepthStencilImage(cmd_.handle(),
Chia-I Wuf4aed6c2015-07-03 13:44:34 +08001414 img.handle(), VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Chris Forbes2951d7d2015-06-22 17:21:59 +12001415 depth, stencil,
1416 ranges.size(), &ranges[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001417
Chia-I Wu78c2a352015-07-03 11:49:42 +08001418 vkCmdPipelineBarrier(cmd_.handle(), src_stages, dest_stages, false, to_xfer.size(), (const void **)&p_to_xfer[0]);
Mike Stroyan55658c22014-12-04 11:08:39 +00001419
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001420 cmd_.end();
1421
1422 submit_and_done();
1423
1424 // cannot verify
1425 if (!img.transparent() && !img.copyable())
1426 return;
1427
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001428 vk_testing::ImageChecker checker(img_info, ranges);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001429
1430 checker.set_solid_pattern(ds_to_raw(img_info.format, depth, stencil));
1431 check_dst(img, checker);
1432 }
1433};
1434
Tony Barbour01999182015-04-09 12:58:51 -06001435TEST_F(VkCmdClearDepthStencilTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001436{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001437 for (std::vector<vk_testing::Device::Format>::const_iterator it = test_formats_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001438 it != test_formats_.end(); it++) {
Tony Barbour2275eb42015-06-05 12:53:55 -06001439 VkFormatProperties props;
Tony Barbour2275eb42015-06-05 12:53:55 -06001440 VkResult err;
1441
Chia-I Wuf5fb1092015-07-03 10:32:05 +08001442 err = vkGetPhysicalDeviceFormatInfo(dev_.phy().handle(), it->format, &props);
Tony Barbour2275eb42015-06-05 12:53:55 -06001443 ASSERT_EQ(err, VK_SUCCESS);
Chris Forbesd7576302015-06-21 22:55:02 +12001444
Tony Barbour2275eb42015-06-05 12:53:55 -06001445 if (it->tiling == VK_IMAGE_TILING_LINEAR && !(props.linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))
1446 continue;
1447
1448 if (it->tiling == VK_IMAGE_TILING_OPTIMAL && !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))
1449 continue;
1450
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001451 // known driver issues
Tony Barbour8205d902015-04-16 15:59:00 -06001452 if (it->format == VK_FORMAT_S8_UINT ||
1453 it->format == VK_FORMAT_D24_UNORM ||
1454 it->format == VK_FORMAT_D16_UNORM_S8_UINT ||
1455 it->format == VK_FORMAT_D24_UNORM_S8_UINT)
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001456 continue;
1457
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001458 VkImageCreateInfo img_info = vk_testing::Image::create_info();
Tony Barbour8205d902015-04-16 15:59:00 -06001459 img_info.imageType = VK_IMAGE_TYPE_2D;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001460 img_info.format = it->format;
1461 img_info.extent.width = 64;
1462 img_info.extent.height = 64;
1463 img_info.tiling = it->tiling;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001464 img_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT;
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001465
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001466 const VkImageSubresourceRange range =
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001467 vk_testing::Image::subresource_range(img_info, VK_IMAGE_ASPECT_DEPTH);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001468 std::vector<VkImageSubresourceRange> ranges(&range, &range + 1);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001469
Tony Barbourb223d482015-06-09 13:40:53 -06001470 if (it->format == VK_FORMAT_D32_SFLOAT_S8_UINT ||
1471 it->format == VK_FORMAT_D16_UNORM_S8_UINT ||
1472 it->format == VK_FORMAT_D24_UNORM_S8_UINT) {
1473 const VkImageSubresourceRange range2 =
1474 vk_testing::Image::subresource_range(img_info, VK_IMAGE_ASPECT_STENCIL);
1475 ranges.push_back(range2);
1476 }
1477
1478
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001479 test_clear_depth_stencil(img_info, 0.25f, 63, ranges);
1480 }
1481}
1482
1483}; // namespace
1484
Chia-I Wucb67c652014-10-21 11:06:26 +08001485int main(int argc, char **argv)
1486{
Chia-I Wucb67c652014-10-21 11:06:26 +08001487 ::testing::InitGoogleTest(&argc, argv);
Chia-I Wucb67c652014-10-21 11:06:26 +08001488
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001489 vk_testing::set_error_callback(test_error_callback);
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001490
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001491 environment = new vk_testing::Environment();
Chia-I Wucb67c652014-10-21 11:06:26 +08001492
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001493 if (!environment->parse_args(argc, argv))
1494 return -1;
Chia-I Wucb67c652014-10-21 11:06:26 +08001495
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001496 ::testing::AddGlobalTestEnvironment(environment);
1497
1498 return RUN_ALL_TESTS();
Chia-I Wucb67c652014-10-21 11:06:26 +08001499}