blob: 3fbc7126d39b21438f28bfbc5ff16f5986f6ef6f [file] [log] [blame]
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001// XGL 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
Chia-I Wu9dac52d2014-12-27 22:04:00 +080025#include "test_common.h"
26#include "xgltestbinding.h"
Chia-I Wucb67c652014-10-21 11:06:26 +080027
28#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
29
Chia-I Wu6170c9e2014-12-08 14:30:10 +080030namespace xgl_testing {
Chia-I Wucb67c652014-10-21 11:06:26 +080031
Chia-I Wu6170c9e2014-12-08 14:30:10 +080032XGL_SIZE get_format_size(XGL_FORMAT format);
Chia-I Wu6170c9e2014-12-08 14:30:10 +080033
34class Environment : public ::testing::Environment {
35public:
36 Environment();
37
38 bool parse_args(int argc, char **argv);
Chia-I Wucb67c652014-10-21 11:06:26 +080039
40 virtual void SetUp();
41 virtual void TearDown();
42
Chia-I Wu6170c9e2014-12-08 14:30:10 +080043 const std::vector<Device *> &devices() { return devs_; }
44 Device &default_device() { return *(devs_[default_dev_]); }
Chia-I Wucb67c652014-10-21 11:06:26 +080045
Chia-I Wu6170c9e2014-12-08 14:30:10 +080046private:
47 XGL_APPLICATION_INFO app_;
48 int default_dev_;
Chia-I Wucb67c652014-10-21 11:06:26 +080049
Chia-I Wu6170c9e2014-12-08 14:30:10 +080050 std::vector<Device *> devs_;
Chia-I Wucb67c652014-10-21 11:06:26 +080051};
52
Chia-I Wu6170c9e2014-12-08 14:30:10 +080053class ImageChecker {
54public:
Chia-I Wu714df452015-01-01 07:55:04 +080055 explicit ImageChecker(const XGL_IMAGE_CREATE_INFO &info, const std::vector<XGL_BUFFER_IMAGE_COPY> &regions)
Chia-I Wu6170c9e2014-12-08 14:30:10 +080056 : info_(info), regions_(regions), pattern_(HASH) {}
57 explicit ImageChecker(const XGL_IMAGE_CREATE_INFO &info, const std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> &ranges);
58 explicit ImageChecker(const XGL_IMAGE_CREATE_INFO &info);
59
60 void set_solid_pattern(const std::vector<uint8_t> &solid);
61
62 XGL_GPU_SIZE buffer_size() const;
63 bool fill(Buffer &buf) const { return walk(FILL, buf); }
64 bool fill(Image &img) const { return walk(FILL, img); }
65 bool check(Buffer &buf) const { return walk(CHECK, buf); }
66 bool check(Image &img) const { return walk(CHECK, img); }
67
Chia-I Wu714df452015-01-01 07:55:04 +080068 const std::vector<XGL_BUFFER_IMAGE_COPY> &regions() const { return regions_; }
Chia-I Wu6170c9e2014-12-08 14:30:10 +080069
70 static void hash_salt_generate() { hash_salt_++; }
71
72private:
73 enum Action {
74 FILL,
75 CHECK,
76 };
77
78 enum Pattern {
79 HASH,
80 SOLID,
81 };
82
83 XGL_SIZE buffer_cpp() const;
Chia-I Wu714df452015-01-01 07:55:04 +080084 XGL_SUBRESOURCE_LAYOUT buffer_layout(const XGL_BUFFER_IMAGE_COPY &region) const;
Chia-I Wu6170c9e2014-12-08 14:30:10 +080085
86 bool walk(Action action, Buffer &buf) const;
87 bool walk(Action action, Image &img) const;
Chia-I Wu714df452015-01-01 07:55:04 +080088 bool walk_region(Action action, const XGL_BUFFER_IMAGE_COPY &region, const XGL_SUBRESOURCE_LAYOUT &layout, void *data) const;
Chia-I Wu6170c9e2014-12-08 14:30:10 +080089
90 std::vector<uint8_t> pattern_hash(const XGL_IMAGE_SUBRESOURCE &subres, const XGL_OFFSET3D &offset) const;
91
92 static uint32_t hash_salt_;
93
94 XGL_IMAGE_CREATE_INFO info_;
Chia-I Wu714df452015-01-01 07:55:04 +080095 std::vector<XGL_BUFFER_IMAGE_COPY> regions_;
Chia-I Wu6170c9e2014-12-08 14:30:10 +080096
97 Pattern pattern_;
98 std::vector<uint8_t> pattern_solid_;
99};
100
101Environment::Environment() :
102 default_dev_(0)
Chia-I Wucb67c652014-10-21 11:06:26 +0800103{
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800104 app_.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800105 app_.pAppName = "xgl_testing";
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800106 app_.appVersion = 1;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800107 app_.pEngineName = "xgl_testing";
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800108 app_.engineVersion = 1;
109 app_.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
110}
111
112bool Environment::parse_args(int argc, char **argv)
113{
114 int i;
115
116 for (i = 1; i < argc; i++) {
117#define ARG(name) (strcmp(argv[i], name) == 0)
118#define ARG_P(name) (i < argc - 1 && ARG(name))
119 if (ARG_P("--gpu")) {
120 default_dev_ = atoi(argv[++i]);
121 } else {
122 break;
123 }
124#undef ARG
125#undef ARG_P
126 }
127
128 if (i < argc) {
129 std::cout <<
130 "invalid argument: " << argv[i] << "\n\n" <<
131 "Usage: " << argv[0] << " <options>\n\n" <<
132 "Options:\n"
133 " --gpu <n> Use GPU<n> as the default GPU\n";
134
135 return false;
136 }
137
138 return true;
139}
140
141void Environment::SetUp()
142{
143 XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS];
Chia-I Wucb67c652014-10-21 11:06:26 +0800144 XGL_UINT count;
145 XGL_RESULT err;
146
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800147 err = xglInitAndEnumerateGpus(&app_, NULL, ARRAY_SIZE(gpus), &count, gpus);
148 ASSERT_EQ(XGL_SUCCESS, err);
149 ASSERT_GT(count, default_dev_);
Chia-I Wucb67c652014-10-21 11:06:26 +0800150
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800151 devs_.reserve(count);
152 for (XGL_UINT i = 0; i < count; i++) {
153 devs_.push_back(new Device(gpus[i]));
154 if (i == default_dev_) {
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800155 devs_[i]->init();
156 ASSERT_NE(true, devs_[i]->graphics_queues().empty());
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800157 }
158 }
Chia-I Wucb67c652014-10-21 11:06:26 +0800159}
160
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800161void Environment::TearDown()
Chia-I Wucb67c652014-10-21 11:06:26 +0800162{
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800163 // destroy devices first
164 for (std::vector<Device *>::iterator it = devs_.begin(); it != devs_.end(); it++)
165 delete *it;
166 devs_.clear();
Chia-I Wucb67c652014-10-21 11:06:26 +0800167
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800168 XGL_UINT dummy_count;
169 xglInitAndEnumerateGpus(&app_, NULL, 0, &dummy_count, NULL);
Chia-I Wucb67c652014-10-21 11:06:26 +0800170}
171
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800172uint32_t ImageChecker::hash_salt_;
173
174ImageChecker::ImageChecker(const XGL_IMAGE_CREATE_INFO &info)
175 : info_(info), regions_(), pattern_(HASH)
176{
177 // create a region for every mip level in array slice 0
178 XGL_GPU_SIZE offset = 0;
179 for (XGL_UINT lv = 0; lv < info_.mipLevels; lv++) {
Chia-I Wu714df452015-01-01 07:55:04 +0800180 XGL_BUFFER_IMAGE_COPY region = {};
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800181
Chia-I Wu714df452015-01-01 07:55:04 +0800182 region.bufferOffset = offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800183 region.imageSubresource.mipLevel = lv;
184 region.imageSubresource.arraySlice = 0;
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800185 region.imageExtent = Image::extent(info_.extent, lv);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800186
187 if (info_.usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
188 if (info_.format.channelFormat != XGL_CH_FMT_R8) {
189 region.imageSubresource.aspect = XGL_IMAGE_ASPECT_DEPTH;
190 regions_.push_back(region);
191 }
192
193 if (info_.format.channelFormat == XGL_CH_FMT_R16G8 ||
194 info_.format.channelFormat == XGL_CH_FMT_R32G8 ||
195 info_.format.channelFormat == XGL_CH_FMT_R8) {
196 region.imageSubresource.aspect = XGL_IMAGE_ASPECT_STENCIL;
197 regions_.push_back(region);
198 }
Chia-I Wucb67c652014-10-21 11:06:26 +0800199 } else {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800200 region.imageSubresource.aspect = XGL_IMAGE_ASPECT_COLOR;
201 regions_.push_back(region);
202 }
203
204 offset += buffer_layout(region).size;
205 }
206
207 // arraySize should be limited in our tests. If this proves to be an
208 // issue, we can store only the regions for array slice 0 and be smart.
209 if (info_.arraySize > 1) {
210 const XGL_GPU_SIZE slice_pitch = offset;
211 const XGL_UINT slice_region_count = regions_.size();
212
213 regions_.reserve(slice_region_count * info_.arraySize);
214
215 for (XGL_UINT slice = 1; slice < info_.arraySize; slice++) {
216 for (XGL_UINT i = 0; i < slice_region_count; i++) {
Chia-I Wu714df452015-01-01 07:55:04 +0800217 XGL_BUFFER_IMAGE_COPY region = regions_[i];
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800218
Chia-I Wu714df452015-01-01 07:55:04 +0800219 region.bufferOffset += slice_pitch * slice;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800220 region.imageSubresource.arraySlice = slice;
221 regions_.push_back(region);
222 }
223 }
224 }
225}
226
227ImageChecker::ImageChecker(const XGL_IMAGE_CREATE_INFO &info, const std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> &ranges)
228 : info_(info), regions_(), pattern_(HASH)
229{
230 XGL_GPU_SIZE offset = 0;
231 for (std::vector<XGL_IMAGE_SUBRESOURCE_RANGE>::const_iterator it = ranges.begin();
232 it != ranges.end(); it++) {
233 for (XGL_UINT lv = 0; lv < it->mipLevels; lv++) {
234 for (XGL_UINT slice = 0; slice < it->arraySize; slice++) {
Chia-I Wu714df452015-01-01 07:55:04 +0800235 XGL_BUFFER_IMAGE_COPY region = {};
236 region.bufferOffset = offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800237 region.imageSubresource = Image::subresource(*it, lv, slice);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800238 region.imageExtent = Image::extent(info_.extent, lv);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800239
240 regions_.push_back(region);
241
242 offset += buffer_layout(region).size;
243 }
244 }
245 }
246}
247
248void ImageChecker::set_solid_pattern(const std::vector<uint8_t> &solid)
249{
250 pattern_ = SOLID;
Chia-I Wu4dce6af2014-12-21 14:59:04 +0800251 pattern_solid_.clear();
252 pattern_solid_.reserve(buffer_cpp());
253 for (int i = 0; i < buffer_cpp(); i++)
254 pattern_solid_.push_back(solid[i % solid.size()]);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800255}
256
257XGL_SIZE ImageChecker::buffer_cpp() const
258{
259 return get_format_size(info_.format);
260}
261
Chia-I Wu714df452015-01-01 07:55:04 +0800262XGL_SUBRESOURCE_LAYOUT ImageChecker::buffer_layout(const XGL_BUFFER_IMAGE_COPY &region) const
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800263{
264 XGL_SUBRESOURCE_LAYOUT layout = {};
Chia-I Wu714df452015-01-01 07:55:04 +0800265 layout.offset = region.bufferOffset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800266 layout.rowPitch = buffer_cpp() * region.imageExtent.width;
267 layout.depthPitch = layout.rowPitch * region.imageExtent.height;
268 layout.size = layout.depthPitch * region.imageExtent.depth;
269
270 return layout;
271}
272
273XGL_GPU_SIZE ImageChecker::buffer_size() const
274{
275 XGL_GPU_SIZE size = 0;
276
Chia-I Wu714df452015-01-01 07:55:04 +0800277 for (std::vector<XGL_BUFFER_IMAGE_COPY>::const_iterator it = regions_.begin();
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800278 it != regions_.end(); it++) {
279 const XGL_SUBRESOURCE_LAYOUT layout = buffer_layout(*it);
280 if (size < layout.offset + layout.size)
281 size = layout.offset + layout.size;
282 }
283
284 return size;
285}
286
Chia-I Wu714df452015-01-01 07:55:04 +0800287bool ImageChecker::walk_region(Action action, const XGL_BUFFER_IMAGE_COPY &region,
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800288 const XGL_SUBRESOURCE_LAYOUT &layout, void *data) const
289{
290 for (XGL_INT z = 0; z < region.imageExtent.depth; z++) {
291 for (XGL_INT y = 0; y < region.imageExtent.height; y++) {
292 for (XGL_INT x = 0; x < region.imageExtent.width; x++) {
293 uint8_t *dst = static_cast<uint8_t *>(data);
294 dst += layout.offset + layout.depthPitch * z +
295 layout.rowPitch * y + buffer_cpp() * x;
296
297 XGL_OFFSET3D offset = region.imageOffset;
298 offset.x += x;
299 offset.y += y;
300 offset.z += z;
301
302 const std::vector<uint8_t> &val = (pattern_ == HASH) ?
303 pattern_hash(region.imageSubresource, offset) :
304 pattern_solid_;
305 assert(val.size() == buffer_cpp());
306
307 if (action == FILL) {
308 memcpy(dst, &val[0], val.size());
309 } else {
310 for (int i = 0; i < val.size(); i++) {
311 EXPECT_EQ(val[i], dst[i]) <<
312 "Offset is: (" << x << ", " << y << ", " << z << ")";
313 if (val[i] != dst[i])
314 return false;
315 }
316 }
317 }
Chia-I Wucb67c652014-10-21 11:06:26 +0800318 }
319 }
320
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800321 return true;
Chia-I Wucb67c652014-10-21 11:06:26 +0800322}
323
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800324bool ImageChecker::walk(Action action, Buffer &buf) const
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800325{
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800326 void *data = buf.map();
327 if (!data)
328 return false;
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800329
Chia-I Wu714df452015-01-01 07:55:04 +0800330 std::vector<XGL_BUFFER_IMAGE_COPY>::const_iterator it;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800331 for (it = regions_.begin(); it != regions_.end(); it++) {
332 if (!walk_region(action, *it, buffer_layout(*it), data))
333 break;
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800334 }
335
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800336 buf.unmap();
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800337
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800338 return (it == regions_.end());
Chia-I Wu3c3fd122014-11-22 02:51:25 +0800339}
340
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800341bool ImageChecker::walk(Action action, Image &img) const
342{
343 void *data = img.map();
344 if (!data)
345 return false;
346
Chia-I Wu714df452015-01-01 07:55:04 +0800347 std::vector<XGL_BUFFER_IMAGE_COPY>::const_iterator it;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800348 for (it = regions_.begin(); it != regions_.end(); it++) {
349 if (!walk_region(action, *it, img.subresource_layout(it->imageSubresource), data))
350 break;
351 }
352
353 img.unmap();
354
355 return (it == regions_.end());
356}
357
358std::vector<uint8_t> ImageChecker::pattern_hash(const XGL_IMAGE_SUBRESOURCE &subres, const XGL_OFFSET3D &offset) const
359{
360#define HASH_BYTE(val, b) static_cast<uint8_t>((static_cast<uint32_t>(val) >> (b * 8)) & 0xff)
361#define HASH_BYTES(val) HASH_BYTE(val, 0), HASH_BYTE(val, 1), HASH_BYTE(val, 2), HASH_BYTE(val, 3)
362 const unsigned char input[] = {
363 HASH_BYTES(hash_salt_),
364 HASH_BYTES(subres.mipLevel),
365 HASH_BYTES(subres.arraySlice),
366 HASH_BYTES(offset.x),
367 HASH_BYTES(offset.y),
368 HASH_BYTES(offset.z),
369 };
370 unsigned long hash = 5381;
371
372 for (XGL_INT i = 0; i < ARRAY_SIZE(input); i++)
373 hash = ((hash << 5) + hash) + input[i];
374
375 const uint8_t output[4] = { HASH_BYTES(hash) };
376#undef HASH_BYTES
377#undef HASH_BYTE
378
Chia-I Wu4dce6af2014-12-21 14:59:04 +0800379 std::vector<uint8_t> val;
380 val.reserve(buffer_cpp());
381 for (int i = 0; i < buffer_cpp(); i++)
382 val.push_back(output[i % 4]);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800383
384 return val;
385}
386
387XGL_SIZE get_format_size(XGL_FORMAT format)
388{
389 static const struct format_info {
390 XGL_SIZE size;
391 XGL_UINT channel_count;
392 } format_table[XGL_MAX_CH_FMT + 1] = {
393 [XGL_CH_FMT_UNDEFINED] = { 0, 0 },
394 [XGL_CH_FMT_R4G4] = { 1, 2 },
395 [XGL_CH_FMT_R4G4B4A4] = { 2, 4 },
396 [XGL_CH_FMT_R5G6B5] = { 2, 3 },
397 [XGL_CH_FMT_B5G6R5] = { 2, 3 },
398 [XGL_CH_FMT_R5G5B5A1] = { 2, 4 },
399 [XGL_CH_FMT_R8] = { 1, 1 },
400 [XGL_CH_FMT_R8G8] = { 2, 2 },
401 [XGL_CH_FMT_R8G8B8A8] = { 4, 4 },
402 [XGL_CH_FMT_B8G8R8A8] = { 4, 4 },
403 [XGL_CH_FMT_R10G11B11] = { 4, 3 },
404 [XGL_CH_FMT_R11G11B10] = { 4, 3 },
405 [XGL_CH_FMT_R10G10B10A2] = { 4, 4 },
406 [XGL_CH_FMT_R16] = { 2, 1 },
407 [XGL_CH_FMT_R16G16] = { 4, 2 },
408 [XGL_CH_FMT_R16G16B16A16] = { 8, 4 },
409 [XGL_CH_FMT_R32] = { 4, 1 },
410 [XGL_CH_FMT_R32G32] = { 8, 2 },
411 [XGL_CH_FMT_R32G32B32] = { 12, 3 },
412 [XGL_CH_FMT_R32G32B32A32] = { 16, 4 },
413 [XGL_CH_FMT_R16G8] = { 3, 2 },
414 [XGL_CH_FMT_R32G8] = { 5, 2 },
415 [XGL_CH_FMT_R9G9B9E5] = { 4, 3 },
416 [XGL_CH_FMT_BC1] = { 8, 4 },
417 [XGL_CH_FMT_BC2] = { 16, 4 },
418 [XGL_CH_FMT_BC3] = { 16, 4 },
419 [XGL_CH_FMT_BC4] = { 8, 4 },
420 [XGL_CH_FMT_BC5] = { 16, 4 },
421 [XGL_CH_FMT_BC6U] = { 16, 4 },
422 [XGL_CH_FMT_BC6S] = { 16, 4 },
423 [XGL_CH_FMT_BC7] = { 16, 4 },
424 };
425
426 return format_table[format.channelFormat].size;
427}
428
429XGL_EXTENT3D get_mip_level_extent(const XGL_EXTENT3D &extent, XGL_UINT mip_level)
430{
431 const XGL_EXTENT3D ext = {
432 (extent.width >> mip_level) ? extent.width >> mip_level : 1,
433 (extent.height >> mip_level) ? extent.height >> mip_level : 1,
434 (extent.depth >> mip_level) ? extent.depth >> mip_level : 1,
435 };
436
437 return ext;
438}
439
440}; // namespace xgl_testing
441
442namespace {
443
444#define DO(action) ASSERT_EQ(true, action);
445
446xgl_testing::Environment *environment;
447
448class XglCmdBlitTest : public ::testing::Test {
449protected:
450 XglCmdBlitTest() :
451 dev_(environment->default_device()),
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800452 queue_(*dev_.graphics_queues()[0]),
453 cmd_(dev_, xgl_testing::CmdBuffer::create_info(XGL_QUEUE_TYPE_GRAPHICS))
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800454 {
455 // make sure every test uses a different pattern
456 xgl_testing::ImageChecker::hash_salt_generate();
457 }
458
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800459 bool submit_and_done()
460 {
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800461 queue_.submit(cmd_, mem_refs_);
462 queue_.wait();
463 mem_refs_.clear();
464 return true;
465 }
466
467 void add_memory_ref(const xgl_testing::Object &obj, XGL_FLAGS flags)
468 {
469 const std::vector<XGL_GPU_MEMORY> mems = obj.memories();
470 for (std::vector<XGL_GPU_MEMORY>::const_iterator it = mems.begin(); it != mems.end(); it++) {
471 std::vector<XGL_MEMORY_REF>::iterator ref;
472 for (ref = mem_refs_.begin(); ref != mem_refs_.end(); ref++) {
473 if (ref->mem == *it)
474 break;
475 }
476
477 if (ref == mem_refs_.end()) {
478 XGL_MEMORY_REF tmp = {};
479 tmp.mem = *it;
480 tmp.flags = flags;
481 mem_refs_.push_back(tmp);
482 } else {
483 ref->flags &= flags;
484 }
485 }
486 }
487
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800488 xgl_testing::Device &dev_;
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800489 xgl_testing::Queue &queue_;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800490 xgl_testing::CmdBuffer cmd_;
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800491
492 std::vector<XGL_MEMORY_REF> mem_refs_;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800493};
494
Chia-I Wu714df452015-01-01 07:55:04 +0800495typedef XglCmdBlitTest XglCmdFillBufferTest;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800496
Chia-I Wu714df452015-01-01 07:55:04 +0800497TEST_F(XglCmdFillBufferTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800498{
499 xgl_testing::Buffer buf;
500
501 buf.init(dev_, 20);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800502 add_memory_ref(buf, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800503
504 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800505 xglCmdFillBuffer(cmd_.obj(), buf.obj(), 0, 4, 0x11111111);
506 xglCmdFillBuffer(cmd_.obj(), buf.obj(), 4, 16, 0x22222222);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800507 cmd_.end();
508
509 submit_and_done();
510
511 const uint32_t *data = static_cast<const uint32_t *>(buf.map());
512 EXPECT_EQ(0x11111111, data[0]);
513 EXPECT_EQ(0x22222222, data[1]);
514 EXPECT_EQ(0x22222222, data[2]);
515 EXPECT_EQ(0x22222222, data[3]);
516 EXPECT_EQ(0x22222222, data[4]);
517 buf.unmap();
518}
519
Chia-I Wu714df452015-01-01 07:55:04 +0800520TEST_F(XglCmdFillBufferTest, Large)
Chia-I Wuea9367f2014-11-23 02:16:45 +0800521{
522 const XGL_GPU_SIZE size = 32 * 1024 * 1024;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800523 xgl_testing::Buffer buf;
Chia-I Wuea9367f2014-11-23 02:16:45 +0800524
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800525 buf.init(dev_, size);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800526 add_memory_ref(buf, 0);
Chia-I Wuea9367f2014-11-23 02:16:45 +0800527
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800528 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800529 xglCmdFillBuffer(cmd_.obj(), buf.obj(), 0, size / 2, 0x11111111);
530 xglCmdFillBuffer(cmd_.obj(), buf.obj(), size / 2, size / 2, 0x22222222);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800531 cmd_.end();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800532
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800533 submit_and_done();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800534
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800535 const uint32_t *data = static_cast<const uint32_t *>(buf.map());
536 XGL_GPU_SIZE offset;
537 for (offset = 0; offset < size / 2; offset += 4)
538 EXPECT_EQ(0x11111111, data[offset / 4]) << "Offset is: " << offset;
539 for (; offset < size; offset += 4)
540 EXPECT_EQ(0x22222222, data[offset / 4]) << "Offset is: " << offset;
541 buf.unmap();
542}
Chia-I Wuea9367f2014-11-23 02:16:45 +0800543
Chia-I Wu714df452015-01-01 07:55:04 +0800544TEST_F(XglCmdFillBufferTest, Overlap)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800545{
546 xgl_testing::Buffer buf;
547
548 buf.init(dev_, 64);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800549 add_memory_ref(buf, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800550
551 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800552 xglCmdFillBuffer(cmd_.obj(), buf.obj(), 0, 48, 0x11111111);
553 xglCmdFillBuffer(cmd_.obj(), buf.obj(), 32, 32, 0x22222222);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800554 cmd_.end();
555
556 submit_and_done();
557
558 const uint32_t *data = static_cast<const uint32_t *>(buf.map());
559 XGL_GPU_SIZE offset;
560 for (offset = 0; offset < 32; offset += 4)
561 EXPECT_EQ(0x11111111, data[offset / 4]) << "Offset is: " << offset;
562 for (; offset < 64; offset += 4)
563 EXPECT_EQ(0x22222222, data[offset / 4]) << "Offset is: " << offset;
564 buf.unmap();
565}
566
Chia-I Wu714df452015-01-01 07:55:04 +0800567TEST_F(XglCmdFillBufferTest, MultiAlignments)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800568{
569 xgl_testing::Buffer bufs[9];
570 XGL_GPU_SIZE size = 4;
571
572 cmd_.begin();
573 for (int i = 0; i < ARRAY_SIZE(bufs); i++) {
574 bufs[i].init(dev_, size);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800575 add_memory_ref(bufs[i], 0);
Chia-I Wu714df452015-01-01 07:55:04 +0800576 xglCmdFillBuffer(cmd_.obj(), bufs[i].obj(), 0, size, 0x11111111);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800577 size <<= 1;
578 }
579 cmd_.end();
580
581 submit_and_done();
582
583 size = 4;
584 for (int i = 0; i < ARRAY_SIZE(bufs); i++) {
585 const uint32_t *data = static_cast<const uint32_t *>(bufs[i].map());
586 XGL_GPU_SIZE offset;
587 for (offset = 0; offset < size; offset += 4)
588 EXPECT_EQ(0x11111111, data[offset / 4]) << "Buffser is: " << i << "\n" <<
589 "Offset is: " << offset;
590 bufs[i].unmap();
591
592 size <<= 1;
593 }
594}
595
Chia-I Wu714df452015-01-01 07:55:04 +0800596typedef XglCmdBlitTest XglCmdCopyBufferTest;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800597
Chia-I Wu714df452015-01-01 07:55:04 +0800598TEST_F(XglCmdCopyBufferTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800599{
600 xgl_testing::Buffer src, dst;
601
602 src.init(dev_, 4);
603 uint32_t *data = static_cast<uint32_t *>(src.map());
604 data[0] = 0x11111111;
605 src.unmap();
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800606 add_memory_ref(src, XGL_MEMORY_REF_READ_ONLY_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800607
608 dst.init(dev_, 4);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800609 add_memory_ref(dst, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800610
611 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800612 XGL_BUFFER_COPY region = {};
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800613 region.copySize = 4;
Chia-I Wu714df452015-01-01 07:55:04 +0800614 xglCmdCopyBuffer(cmd_.obj(), src.obj(), dst.obj(), 1, &region);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800615 cmd_.end();
616
617 submit_and_done();
618
619 data = static_cast<uint32_t *>(dst.map());
620 EXPECT_EQ(0x11111111, data[0]);
621 dst.unmap();
622}
623
Chia-I Wu714df452015-01-01 07:55:04 +0800624TEST_F(XglCmdCopyBufferTest, Large)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800625{
626 const XGL_GPU_SIZE size = 32 * 1024 * 1024;
627 xgl_testing::Buffer src, dst;
628
629 src.init(dev_, size);
630 uint32_t *data = static_cast<uint32_t *>(src.map());
631 XGL_GPU_SIZE offset;
632 for (offset = 0; offset < size; offset += 4)
633 data[offset / 4] = offset;
634 src.unmap();
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800635 add_memory_ref(src, XGL_MEMORY_REF_READ_ONLY_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800636
637 dst.init(dev_, size);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800638 add_memory_ref(dst, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800639
640 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800641 XGL_BUFFER_COPY region = {};
Chia-I Wuea9367f2014-11-23 02:16:45 +0800642 region.copySize = size;
Chia-I Wu714df452015-01-01 07:55:04 +0800643 xglCmdCopyBuffer(cmd_.obj(), src.obj(), dst.obj(), 1, &region);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800644 cmd_.end();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800645
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800646 submit_and_done();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800647
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800648 data = static_cast<uint32_t *>(dst.map());
649 for (offset = 0; offset < size; offset += 4)
650 EXPECT_EQ(offset, data[offset / 4]);
651 dst.unmap();
Chia-I Wuea9367f2014-11-23 02:16:45 +0800652}
653
Chia-I Wu714df452015-01-01 07:55:04 +0800654TEST_F(XglCmdCopyBufferTest, MultiAlignments)
Chia-I Wucb67c652014-10-21 11:06:26 +0800655{
Chia-I Wu714df452015-01-01 07:55:04 +0800656 const XGL_BUFFER_COPY regions[] = {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800657 /* well aligned */
658 { 0, 0, 256 },
659 { 0, 256, 128 },
660 { 0, 384, 64 },
661 { 0, 448, 32 },
662 { 0, 480, 16 },
663 { 0, 496, 8 },
Chia-I Wucb67c652014-10-21 11:06:26 +0800664
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800665 /* ill aligned */
666 { 7, 510, 16 },
667 { 16, 530, 13 },
668 { 32, 551, 16 },
669 { 45, 570, 15 },
670 { 50, 590, 1 },
671 };
672 xgl_testing::Buffer src, dst;
Chia-I Wucb67c652014-10-21 11:06:26 +0800673
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800674 src.init(dev_, 256);
675 uint8_t *data = static_cast<uint8_t *>(src.map());
676 for (int i = 0; i < 256; i++)
677 data[i] = i;
678 src.unmap();
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800679 add_memory_ref(src, XGL_MEMORY_REF_READ_ONLY_BIT);
Chia-I Wucb67c652014-10-21 11:06:26 +0800680
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800681 dst.init(dev_, 1024);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800682 add_memory_ref(dst, 0);
Chia-I Wucb67c652014-10-21 11:06:26 +0800683
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800684 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800685 xglCmdCopyBuffer(cmd_.obj(), src.obj(), dst.obj(), ARRAY_SIZE(regions), regions);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800686 cmd_.end();
Chia-I Wucb67c652014-10-21 11:06:26 +0800687
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800688 submit_and_done();
Chia-I Wucb67c652014-10-21 11:06:26 +0800689
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800690 data = static_cast<uint8_t *>(dst.map());
691 for (int i = 0; i < ARRAY_SIZE(regions); i++) {
Chia-I Wu714df452015-01-01 07:55:04 +0800692 const XGL_BUFFER_COPY &r = regions[i];
Chia-I Wucb67c652014-10-21 11:06:26 +0800693
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800694 for (int j = 0; j < r.copySize; j++) {
695 EXPECT_EQ(r.srcOffset + j, data[r.destOffset + j]) <<
696 "Region is: " << i << "\n" <<
697 "Offset is: " << r.destOffset + j;
698 }
699 }
700 dst.unmap();
701}
Chia-I Wucb67c652014-10-21 11:06:26 +0800702
Chia-I Wu714df452015-01-01 07:55:04 +0800703TEST_F(XglCmdCopyBufferTest, RAWHazard)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800704{
705 xgl_testing::Buffer bufs[3];
Chia-I Wucb67c652014-10-21 11:06:26 +0800706
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800707 for (int i = 0; i < ARRAY_SIZE(bufs); i++) {
708 bufs[i].init(dev_, 4);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800709 add_memory_ref(bufs[i], 0);
Chia-I Wucb67c652014-10-21 11:06:26 +0800710
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800711 uint32_t *data = static_cast<uint32_t *>(bufs[i].map());
712 data[0] = 0x22222222 * (i + 1);
713 bufs[i].unmap();
714 }
715
716 cmd_.begin();
717
Chia-I Wu714df452015-01-01 07:55:04 +0800718 xglCmdFillBuffer(cmd_.obj(), bufs[0].obj(), 0, 4, 0x11111111);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800719 // is this necessary?
Chia-I Wu714df452015-01-01 07:55:04 +0800720 XGL_BUFFER_STATE_TRANSITION transition = bufs[0].state_transition(
721 XGL_BUFFER_STATE_DATA_TRANSFER, XGL_BUFFER_STATE_DATA_TRANSFER, 0, 4);
722 xglCmdPrepareBufferRegions(cmd_.obj(), 1, &transition);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800723
Chia-I Wu714df452015-01-01 07:55:04 +0800724 XGL_BUFFER_COPY region = {};
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800725 region.copySize = 4;
Chia-I Wu714df452015-01-01 07:55:04 +0800726 xglCmdCopyBuffer(cmd_.obj(), bufs[0].obj(), bufs[1].obj(), 1, &region);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800727 // is this necessary?
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800728 transition = bufs[1].state_transition(
Chia-I Wu714df452015-01-01 07:55:04 +0800729 XGL_BUFFER_STATE_DATA_TRANSFER, XGL_BUFFER_STATE_DATA_TRANSFER, 0, 4);
730 xglCmdPrepareBufferRegions(cmd_.obj(), 1, &transition);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800731
Chia-I Wu714df452015-01-01 07:55:04 +0800732 xglCmdCopyBuffer(cmd_.obj(), bufs[1].obj(), bufs[2].obj(), 1, &region);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800733 cmd_.end();
734
735 submit_and_done();
736
737 const uint32_t *data = static_cast<const uint32_t *>(bufs[2].map());
738 EXPECT_EQ(0x11111111, data[0]);
739 bufs[2].unmap();
740}
741
742class XglCmdBlitImageTest : public XglCmdBlitTest {
743protected:
744 void init_test_formats(XGL_FLAGS features)
Chia-I Wucb67c652014-10-21 11:06:26 +0800745 {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800746 first_linear_format_.channelFormat = XGL_CH_FMT_UNDEFINED;
747 first_linear_format_.numericFormat = XGL_NUM_FMT_UNDEFINED;
748 first_optimal_format_.channelFormat = XGL_CH_FMT_UNDEFINED;
749 first_optimal_format_.numericFormat = XGL_NUM_FMT_UNDEFINED;
Chia-I Wucb67c652014-10-21 11:06:26 +0800750
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800751 for (std::vector<xgl_testing::Device::Format>::const_iterator it = dev_.formats().begin();
752 it != dev_.formats().end(); it++) {
753 if (it->features & features) {
754 test_formats_.push_back(*it);
Chia-I Wucb67c652014-10-21 11:06:26 +0800755
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800756 if (it->tiling == XGL_LINEAR_TILING &&
757 first_linear_format_.channelFormat == XGL_CH_FMT_UNDEFINED)
758 first_linear_format_ = it->format;
759 if (it->tiling == XGL_OPTIMAL_TILING &&
760 first_optimal_format_.channelFormat == XGL_CH_FMT_UNDEFINED)
761 first_optimal_format_ = it->format;
762 }
763 }
764 }
Chia-I Wucb67c652014-10-21 11:06:26 +0800765
Chia-I Wu9feab842014-12-22 13:19:08 +0800766 void init_test_formats()
767 {
768 init_test_formats(static_cast<XGL_FLAGS>(-1));
769 }
770
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800771 void fill_src(xgl_testing::Image &img, const xgl_testing::ImageChecker &checker)
772 {
773 if (img.transparent()) {
774 checker.fill(img);
775 return;
Chia-I Wucb67c652014-10-21 11:06:26 +0800776 }
777
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800778 ASSERT_EQ(true, img.copyable());
779
780 xgl_testing::Buffer in_buf;
781 in_buf.init(dev_, checker.buffer_size());
782 checker.fill(in_buf);
783
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800784 add_memory_ref(in_buf, XGL_MEMORY_REF_READ_ONLY_BIT);
785 add_memory_ref(img, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800786
787 // copy in and tile
788 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800789 xglCmdCopyBufferToImage(cmd_.obj(), in_buf.obj(), img.obj(),
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800790 checker.regions().size(), &checker.regions()[0]);
791 cmd_.end();
792
793 submit_and_done();
Chia-I Wucb67c652014-10-21 11:06:26 +0800794 }
795
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800796 void check_dst(xgl_testing::Image &img, const xgl_testing::ImageChecker &checker)
Chia-I Wu86822632014-11-22 15:09:42 +0800797 {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800798 if (img.transparent()) {
799 DO(checker.check(img));
800 return;
Chia-I Wu86822632014-11-22 15:09:42 +0800801 }
802
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800803 ASSERT_EQ(true, img.copyable());
804
805 xgl_testing::Buffer out_buf;
806 out_buf.init(dev_, checker.buffer_size());
807
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800808 add_memory_ref(img, XGL_MEMORY_REF_READ_ONLY_BIT);
809 add_memory_ref(out_buf, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800810
811 // copy out and linearize
812 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800813 xglCmdCopyImageToBuffer(cmd_.obj(), img.obj(), out_buf.obj(),
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800814 checker.regions().size(), &checker.regions()[0]);
815 cmd_.end();
816
817 submit_and_done();
818
819 DO(checker.check(out_buf));
Chia-I Wu86822632014-11-22 15:09:42 +0800820 }
821
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800822 std::vector<xgl_testing::Device::Format> test_formats_;
823 XGL_FORMAT first_linear_format_;
824 XGL_FORMAT first_optimal_format_;
825};
826
Chia-I Wu714df452015-01-01 07:55:04 +0800827class XglCmdCopyBufferToImageTest : public XglCmdBlitImageTest {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800828protected:
829 virtual void SetUp()
830 {
831 XglCmdBlitTest::SetUp();
832 init_test_formats(XGL_FORMAT_IMAGE_COPY_BIT);
833 ASSERT_NE(true, test_formats_.empty());
834 }
835
836 void test_copy_memory_to_image(const XGL_IMAGE_CREATE_INFO &img_info, const xgl_testing::ImageChecker &checker)
837 {
838 xgl_testing::Buffer buf;
839 xgl_testing::Image img;
840
841 buf.init(dev_, checker.buffer_size());
842 checker.fill(buf);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800843 add_memory_ref(buf, XGL_MEMORY_REF_READ_ONLY_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800844
845 img.init(dev_, img_info);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800846 add_memory_ref(img, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800847
848 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800849 xglCmdCopyBufferToImage(cmd_.obj(), buf.obj(), img.obj(),
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800850 checker.regions().size(), &checker.regions()[0]);
851 cmd_.end();
852
853 submit_and_done();
854
855 check_dst(img, checker);
856 }
857
Chia-I Wu714df452015-01-01 07:55:04 +0800858 void test_copy_memory_to_image(const XGL_IMAGE_CREATE_INFO &img_info, const std::vector<XGL_BUFFER_IMAGE_COPY> &regions)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800859 {
860 xgl_testing::ImageChecker checker(img_info, regions);
861 test_copy_memory_to_image(img_info, checker);
862 }
863
864 void test_copy_memory_to_image(const XGL_IMAGE_CREATE_INFO &img_info)
865 {
866 xgl_testing::ImageChecker checker(img_info);
867 test_copy_memory_to_image(img_info, checker);
868 }
869};
870
Chia-I Wu714df452015-01-01 07:55:04 +0800871TEST_F(XglCmdCopyBufferToImageTest, Basic)
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800872{
873 for (std::vector<xgl_testing::Device::Format>::const_iterator it = test_formats_.begin();
874 it != test_formats_.end(); it++) {
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800875 XGL_IMAGE_CREATE_INFO img_info = xgl_testing::Image::create_info();
876 img_info.imageType = XGL_IMAGE_2D;
877 img_info.format = it->format;
878 img_info.extent.width = 64;
879 img_info.extent.height = 64;
880 img_info.tiling = it->tiling;
881
882 test_copy_memory_to_image(img_info);
883 }
Chia-I Wu86822632014-11-22 15:09:42 +0800884}
885
Chia-I Wu714df452015-01-01 07:55:04 +0800886class XglCmdCopyImageToBufferTest : public XglCmdBlitImageTest {
Chia-I Wu830fb332014-12-13 15:28:20 +0800887protected:
888 virtual void SetUp()
889 {
890 XglCmdBlitTest::SetUp();
891 init_test_formats(XGL_FORMAT_IMAGE_COPY_BIT);
892 ASSERT_NE(true, test_formats_.empty());
893 }
894
895 void test_copy_image_to_memory(const XGL_IMAGE_CREATE_INFO &img_info, const xgl_testing::ImageChecker &checker)
896 {
897 xgl_testing::Image img;
898 xgl_testing::Buffer buf;
899
900 img.init(dev_, img_info);
901 fill_src(img, checker);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800902 add_memory_ref(img, XGL_MEMORY_REF_READ_ONLY_BIT);
Chia-I Wu830fb332014-12-13 15:28:20 +0800903
904 buf.init(dev_, checker.buffer_size());
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800905 add_memory_ref(buf, 0);
Chia-I Wu830fb332014-12-13 15:28:20 +0800906
907 cmd_.begin();
Chia-I Wu714df452015-01-01 07:55:04 +0800908 xglCmdCopyImageToBuffer(cmd_.obj(), img.obj(), buf.obj(),
Chia-I Wu830fb332014-12-13 15:28:20 +0800909 checker.regions().size(), &checker.regions()[0]);
910 cmd_.end();
911
912 submit_and_done();
913
914 checker.check(buf);
915 }
916
Chia-I Wu714df452015-01-01 07:55:04 +0800917 void test_copy_image_to_memory(const XGL_IMAGE_CREATE_INFO &img_info, const std::vector<XGL_BUFFER_IMAGE_COPY> &regions)
Chia-I Wu830fb332014-12-13 15:28:20 +0800918 {
919 xgl_testing::ImageChecker checker(img_info, regions);
920 test_copy_image_to_memory(img_info, checker);
921 }
922
923 void test_copy_image_to_memory(const XGL_IMAGE_CREATE_INFO &img_info)
924 {
925 xgl_testing::ImageChecker checker(img_info);
926 test_copy_image_to_memory(img_info, checker);
927 }
928};
929
Chia-I Wu714df452015-01-01 07:55:04 +0800930TEST_F(XglCmdCopyImageToBufferTest, Basic)
Chia-I Wu830fb332014-12-13 15:28:20 +0800931{
932 for (std::vector<xgl_testing::Device::Format>::const_iterator it = test_formats_.begin();
933 it != test_formats_.end(); it++) {
934 XGL_IMAGE_CREATE_INFO img_info = xgl_testing::Image::create_info();
935 img_info.imageType = XGL_IMAGE_2D;
936 img_info.format = it->format;
937 img_info.extent.width = 64;
938 img_info.extent.height = 64;
939 img_info.tiling = it->tiling;
940
941 test_copy_image_to_memory(img_info);
942 }
943}
944
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800945class XglCmdCopyImageTest : public XglCmdBlitImageTest {
946protected:
947 virtual void SetUp()
948 {
949 XglCmdBlitTest::SetUp();
950 init_test_formats(XGL_FORMAT_IMAGE_COPY_BIT);
951 ASSERT_NE(true, test_formats_.empty());
Chia-I Wuf7ff6b42014-11-22 16:24:41 +0800952 }
953
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800954 void test_copy_image(const XGL_IMAGE_CREATE_INFO &src_info, const XGL_IMAGE_CREATE_INFO &dst_info,
955 const std::vector<XGL_IMAGE_COPY> &copies)
Chia-I Wuf7ff6b42014-11-22 16:24:41 +0800956 {
Chia-I Wu714df452015-01-01 07:55:04 +0800957 // convert XGL_IMAGE_COPY to two sets of XGL_BUFFER_IMAGE_COPY
958 std::vector<XGL_BUFFER_IMAGE_COPY> src_regions, dst_regions;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800959 XGL_GPU_SIZE src_offset = 0, dst_offset = 0;
960 for (std::vector<XGL_IMAGE_COPY>::const_iterator it = copies.begin(); it != copies.end(); it++) {
Chia-I Wu714df452015-01-01 07:55:04 +0800961 XGL_BUFFER_IMAGE_COPY src_region = {}, dst_region = {};
Chia-I Wuf7ff6b42014-11-22 16:24:41 +0800962
Chia-I Wu714df452015-01-01 07:55:04 +0800963 src_region.bufferOffset = src_offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800964 src_region.imageSubresource = it->srcSubresource;
965 src_region.imageOffset = it->srcOffset;
966 src_region.imageExtent = it->extent;
967 src_regions.push_back(src_region);
Chia-I Wuf7ff6b42014-11-22 16:24:41 +0800968
Chia-I Wu714df452015-01-01 07:55:04 +0800969 dst_region.bufferOffset = src_offset;
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800970 dst_region.imageSubresource = it->destSubresource;
971 dst_region.imageOffset = it->destOffset;
972 dst_region.imageExtent = it->extent;
973 dst_regions.push_back(dst_region);
Chia-I Wuf7ff6b42014-11-22 16:24:41 +0800974
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800975 const XGL_GPU_SIZE size = it->extent.width * it->extent.height * it->extent.depth;
976 src_offset += xgl_testing::get_format_size(src_info.format) * size;
977 dst_offset += xgl_testing::get_format_size(dst_info.format) * size;
978 }
Chia-I Wuf7ff6b42014-11-22 16:24:41 +0800979
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800980 xgl_testing::ImageChecker src_checker(src_info, src_regions);
981 xgl_testing::ImageChecker dst_checker(dst_info, dst_regions);
982
983 xgl_testing::Image src;
984 src.init(dev_, src_info);
985 fill_src(src, src_checker);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800986 add_memory_ref(src, XGL_MEMORY_REF_READ_ONLY_BIT);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800987
988 xgl_testing::Image dst;
989 dst.init(dev_, dst_info);
Chia-I Wu9dac52d2014-12-27 22:04:00 +0800990 add_memory_ref(dst, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +0800991
992 cmd_.begin();
993 xglCmdCopyImage(cmd_.obj(), src.obj(), dst.obj(), copies.size(), &copies[0]);
994 cmd_.end();
995
996 submit_and_done();
997
998 check_dst(dst, dst_checker);
999 }
1000};
1001
1002TEST_F(XglCmdCopyImageTest, Basic)
1003{
1004 for (std::vector<xgl_testing::Device::Format>::const_iterator it = test_formats_.begin();
1005 it != test_formats_.end(); it++) {
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001006 XGL_IMAGE_CREATE_INFO img_info = xgl_testing::Image::create_info();
1007 img_info.imageType = XGL_IMAGE_2D;
1008 img_info.format = it->format;
1009 img_info.extent.width = 64;
1010 img_info.extent.height = 64;
1011 img_info.tiling = it->tiling;
1012
1013 XGL_IMAGE_COPY copy = {};
1014 copy.srcSubresource = xgl_testing::Image::subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0);
1015 copy.destSubresource = copy.srcSubresource;
1016 copy.extent = img_info.extent;
1017
1018 test_copy_image(img_info, img_info, std::vector<XGL_IMAGE_COPY>(&copy, &copy + 1));
1019 }
1020}
1021
Chia-I Wub75939f2014-12-22 14:46:56 +08001022class XglCmdCloneImageDataTest : public XglCmdBlitImageTest {
1023protected:
1024 virtual void SetUp()
1025 {
1026 XglCmdBlitTest::SetUp();
1027 init_test_formats();
1028 ASSERT_NE(true, test_formats_.empty());
1029 }
1030
1031 void test_clone_image_data(const XGL_IMAGE_CREATE_INFO &img_info)
1032 {
1033 xgl_testing::ImageChecker checker(img_info);
1034 xgl_testing::Image src, dst;
1035
1036 src.init(dev_, img_info);
1037 if (src.transparent() || src.copyable())
1038 fill_src(src, checker);
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001039 add_memory_ref(src, XGL_MEMORY_REF_READ_ONLY_BIT);
Chia-I Wub75939f2014-12-22 14:46:56 +08001040
1041 dst.init(dev_, img_info);
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001042 add_memory_ref(dst, 0);
Chia-I Wub75939f2014-12-22 14:46:56 +08001043
1044 const XGL_IMAGE_STATE state =
1045 (img_info.usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) ?
1046 XGL_IMAGE_STATE_UNINITIALIZED_TARGET : XGL_IMAGE_STATE_DATA_TRANSFER;
1047
1048 cmd_.begin();
1049 xglCmdCloneImageData(cmd_.obj(), src.obj(), state, dst.obj(), state);
1050 cmd_.end();
1051
1052 submit_and_done();
1053
1054 // cannot verify
1055 if (!dst.transparent() && !dst.copyable())
1056 return;
1057
1058 check_dst(dst, checker);
1059 }
1060};
1061
1062TEST_F(XglCmdCloneImageDataTest, Basic)
1063{
1064 for (std::vector<xgl_testing::Device::Format>::const_iterator it = test_formats_.begin();
1065 it != test_formats_.end(); it++) {
1066 // not sure what to do here
1067 if (it->format.channelFormat == XGL_CH_FMT_UNDEFINED ||
1068 (it->format.channelFormat >= XGL_CH_FMT_BC1 &&
1069 it->format.channelFormat <= XGL_CH_FMT_BC7) ||
1070 it->format.numericFormat == XGL_NUM_FMT_DS)
1071 continue;
1072
1073 XGL_IMAGE_CREATE_INFO img_info = xgl_testing::Image::create_info();
1074 img_info.imageType = XGL_IMAGE_2D;
1075 img_info.format = it->format;
1076 img_info.extent.width = 64;
1077 img_info.extent.height = 64;
1078 img_info.tiling = it->tiling;
1079 img_info.flags = XGL_IMAGE_CREATE_CLONEABLE_BIT;
1080
1081 const XGL_IMAGE_SUBRESOURCE_RANGE range =
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001082 xgl_testing::Image::subresource_range(img_info, XGL_IMAGE_ASPECT_COLOR);
Chia-I Wub75939f2014-12-22 14:46:56 +08001083 std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> ranges(&range, &range + 1);
1084
1085 test_clone_image_data(img_info);
1086 }
1087}
1088
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001089class XglCmdClearColorImageTest : public XglCmdBlitImageTest {
1090protected:
Chia-I Wu9feab842014-12-22 13:19:08 +08001091 XglCmdClearColorImageTest() : test_raw_(false) {}
1092 XglCmdClearColorImageTest(bool test_raw) : test_raw_(test_raw) {}
1093
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001094 virtual void SetUp()
1095 {
1096 XglCmdBlitTest::SetUp();
Chia-I Wu9feab842014-12-22 13:19:08 +08001097
1098 if (test_raw_)
1099 init_test_formats();
1100 else
1101 init_test_formats(XGL_FORMAT_CONVERSION_BIT);
1102
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001103 ASSERT_NE(true, test_formats_.empty());
1104 }
1105
Chia-I Wu9feab842014-12-22 13:19:08 +08001106 union Color {
1107 XGL_FLOAT color[4];
1108 XGL_UINT32 raw[4];
1109 };
1110
1111 bool test_raw_;
1112
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001113 std::vector<uint8_t> color_to_raw(XGL_FORMAT format, const XGL_FLOAT color[4])
1114 {
1115 std::vector<uint8_t> raw;
1116
1117 // TODO support all formats
1118 if (format.numericFormat == XGL_NUM_FMT_UNORM) {
1119 switch (format.channelFormat) {
1120 case XGL_CH_FMT_R8G8B8A8:
1121 raw.push_back(color[0] * 255.0f);
1122 raw.push_back(color[1] * 255.0f);
1123 raw.push_back(color[2] * 255.0f);
1124 raw.push_back(color[3] * 255.0f);
1125 break;
1126 case XGL_CH_FMT_B8G8R8A8:
1127 raw.push_back(color[2] * 255.0f);
1128 raw.push_back(color[1] * 255.0f);
1129 raw.push_back(color[0] * 255.0f);
1130 raw.push_back(color[3] * 255.0f);
1131 break;
1132 default:
1133 break;
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001134 }
1135 }
1136
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001137 return raw;
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001138 }
Chia-I Wuf7ff6b42014-11-22 16:24:41 +08001139
Chia-I Wu9feab842014-12-22 13:19:08 +08001140 std::vector<uint8_t> color_to_raw(XGL_FORMAT format, const XGL_UINT32 color[4])
1141 {
1142 std::vector<uint8_t> raw;
1143
1144 // TODO support all formats
1145 if (format.numericFormat == XGL_NUM_FMT_UNORM) {
1146 switch (format.channelFormat) {
1147 case XGL_CH_FMT_R8G8B8A8:
1148 raw.push_back(static_cast<uint8_t>(color[0]));
1149 raw.push_back(static_cast<uint8_t>(color[1]));
1150 raw.push_back(static_cast<uint8_t>(color[2]));
1151 raw.push_back(static_cast<uint8_t>(color[3]));
1152 break;
1153 case XGL_CH_FMT_B8G8R8A8:
1154 raw.push_back(static_cast<uint8_t>(color[2]));
1155 raw.push_back(static_cast<uint8_t>(color[1]));
1156 raw.push_back(static_cast<uint8_t>(color[0]));
1157 raw.push_back(static_cast<uint8_t>(color[3]));
1158 break;
1159 default:
1160 break;
1161 }
1162 }
1163
1164 return raw;
1165 }
1166
1167 std::vector<uint8_t> color_to_raw(XGL_FORMAT format, const Color &color)
1168 {
1169 if (test_raw_)
1170 return color_to_raw(format, color.raw);
1171 else
1172 return color_to_raw(format, color.color);
1173 }
1174
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001175 void test_clear_color_image(const XGL_IMAGE_CREATE_INFO &img_info,
Chia-I Wu9feab842014-12-22 13:19:08 +08001176 const Color &color,
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001177 const std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> &ranges)
Chia-I Wucb67c652014-10-21 11:06:26 +08001178 {
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001179 xgl_testing::Image img;
1180 img.init(dev_, img_info);
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001181 add_memory_ref(img, 0);
Chia-I Wucb67c652014-10-21 11:06:26 +08001182
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001183 std::vector<XGL_IMAGE_STATE_TRANSITION> to_clear;
1184 std::vector<XGL_IMAGE_STATE_TRANSITION> to_xfer;
Chia-I Wucb67c652014-10-21 11:06:26 +08001185
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001186 const XGL_IMAGE_STATE initial_state =
1187 (img_info.usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) ?
1188 XGL_IMAGE_STATE_UNINITIALIZED_TARGET : XGL_IMAGE_STATE_DATA_TRANSFER;
1189 for (std::vector<XGL_IMAGE_SUBRESOURCE_RANGE>::const_iterator it = ranges.begin();
1190 it != ranges.end(); it++) {
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001191 to_clear.push_back(img.state_transition(initial_state, XGL_IMAGE_STATE_CLEAR, *it));
1192 to_xfer.push_back(img.state_transition(XGL_IMAGE_STATE_CLEAR, XGL_IMAGE_STATE_DATA_TRANSFER, *it));
Chia-I Wucb67c652014-10-21 11:06:26 +08001193 }
1194
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001195 cmd_.begin();
Chia-I Wu9feab842014-12-22 13:19:08 +08001196
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001197 xglCmdPrepareImages(cmd_.obj(), to_clear.size(), &to_clear[0]);
Chia-I Wu9feab842014-12-22 13:19:08 +08001198 if (test_raw_)
1199 xglCmdClearColorImageRaw(cmd_.obj(), img.obj(), color.raw, ranges.size(), &ranges[0]);
1200 else
1201 xglCmdClearColorImage(cmd_.obj(), img.obj(), color.color, ranges.size(), &ranges[0]);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001202 xglCmdPrepareImages(cmd_.obj(), to_xfer.size(), &to_xfer[0]);
Chia-I Wu9feab842014-12-22 13:19:08 +08001203
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001204 cmd_.end();
1205
1206 submit_and_done();
1207
1208 // cannot verify
1209 if (!img.transparent() && !img.copyable())
1210 return;
1211
1212 xgl_testing::ImageChecker checker(img_info, ranges);
1213
1214 const std::vector<uint8_t> solid_pattern = color_to_raw(img_info.format, color);
1215 if (solid_pattern.empty())
1216 return;
1217
1218 checker.set_solid_pattern(solid_pattern);
1219 check_dst(img, checker);
1220 }
Chia-I Wu9feab842014-12-22 13:19:08 +08001221
1222 void test_clear_color_image(const XGL_IMAGE_CREATE_INFO &img_info,
1223 const XGL_FLOAT color[4],
1224 const std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> &ranges)
1225 {
1226 Color c;
1227 memcpy(c.color, color, sizeof(c.color));
1228 test_clear_color_image(img_info, c, ranges);
1229 }
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001230};
1231
1232TEST_F(XglCmdClearColorImageTest, Basic)
1233{
1234 for (std::vector<xgl_testing::Device::Format>::const_iterator it = test_formats_.begin();
1235 it != test_formats_.end(); it++) {
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001236 const XGL_FLOAT color[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
1237
1238 XGL_IMAGE_CREATE_INFO img_info = xgl_testing::Image::create_info();
1239 img_info.imageType = XGL_IMAGE_2D;
1240 img_info.format = it->format;
1241 img_info.extent.width = 64;
1242 img_info.extent.height = 64;
1243 img_info.tiling = it->tiling;
1244
1245 const XGL_IMAGE_SUBRESOURCE_RANGE range =
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001246 xgl_testing::Image::subresource_range(img_info, XGL_IMAGE_ASPECT_COLOR);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001247 std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> ranges(&range, &range + 1);
1248
1249 test_clear_color_image(img_info, color, ranges);
Chia-I Wucb67c652014-10-21 11:06:26 +08001250 }
1251}
1252
Chia-I Wu8dbe8562014-12-22 13:44:24 +08001253class XglCmdClearColorImageRawTest : public XglCmdClearColorImageTest {
1254protected:
1255 XglCmdClearColorImageRawTest() : XglCmdClearColorImageTest(true) {}
1256
1257 void test_clear_color_image_raw(const XGL_IMAGE_CREATE_INFO &img_info,
1258 const XGL_UINT32 color[4],
1259 const std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> &ranges)
1260 {
1261 Color c;
1262 memcpy(c.raw, color, sizeof(c.raw));
1263 test_clear_color_image(img_info, c, ranges);
1264 }
1265};
1266
1267TEST_F(XglCmdClearColorImageRawTest, Basic)
1268{
1269 for (std::vector<xgl_testing::Device::Format>::const_iterator it = test_formats_.begin();
1270 it != test_formats_.end(); it++) {
1271 const XGL_UINT32 color[4] = { 0x11111111, 0x22222222, 0x33333333, 0x44444444 };
1272
1273 // not sure what to do here
1274 if (it->format.channelFormat == XGL_CH_FMT_UNDEFINED ||
1275 it->format.channelFormat == XGL_CH_FMT_R32G32B32 ||
1276 it->format.numericFormat == XGL_NUM_FMT_DS)
1277 continue;
1278
1279 XGL_IMAGE_CREATE_INFO img_info = xgl_testing::Image::create_info();
1280 img_info.imageType = XGL_IMAGE_2D;
1281 img_info.format = it->format;
1282 img_info.extent.width = 64;
1283 img_info.extent.height = 64;
1284 img_info.tiling = it->tiling;
1285
1286 const XGL_IMAGE_SUBRESOURCE_RANGE range =
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001287 xgl_testing::Image::subresource_range(img_info, XGL_IMAGE_ASPECT_COLOR);
Chia-I Wu8dbe8562014-12-22 13:44:24 +08001288 std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> ranges(&range, &range + 1);
1289
1290 test_clear_color_image_raw(img_info, color, ranges);
1291 }
1292}
1293
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001294class XglCmdClearDepthStencilTest : public XglCmdBlitImageTest {
1295protected:
1296 virtual void SetUp()
1297 {
1298 XglCmdBlitTest::SetUp();
1299 init_test_formats(XGL_FORMAT_DEPTH_ATTACHMENT_BIT |
1300 XGL_FORMAT_STENCIL_ATTACHMENT_BIT);
1301 ASSERT_NE(true, test_formats_.empty());
1302 }
1303
1304 std::vector<uint8_t> ds_to_raw(XGL_FORMAT format, XGL_FLOAT depth, XGL_UINT32 stencil)
1305 {
1306 std::vector<uint8_t> raw;
1307
1308 // depth
1309 switch (format.channelFormat) {
1310 case XGL_CH_FMT_R16:
1311 case XGL_CH_FMT_R16G8:
1312 {
1313 const uint16_t unorm = depth * 65535.0f;
1314 raw.push_back(unorm & 0xff);
1315 raw.push_back(unorm >> 8);
1316 }
1317 break;
1318 case XGL_CH_FMT_R32:
1319 case XGL_CH_FMT_R32G8:
1320 {
1321 const union {
1322 XGL_FLOAT depth;
1323 uint32_t u32;
1324 } u = { depth };
1325
1326 raw.push_back((u.u32 ) & 0xff);
1327 raw.push_back((u.u32 >> 8) & 0xff);
1328 raw.push_back((u.u32 >> 16) & 0xff);
1329 raw.push_back((u.u32 >> 24) & 0xff);
1330 }
1331 break;
1332 default:
1333 break;
1334 }
1335
1336 // stencil
1337 switch (format.channelFormat) {
1338 case XGL_CH_FMT_R8:
1339 raw.push_back(stencil);
1340 break;
1341 case XGL_CH_FMT_R16G8:
1342 raw.push_back(stencil);
1343 raw.push_back(0);
1344 break;
1345 case XGL_CH_FMT_R32G8:
1346 raw.push_back(stencil);
1347 raw.push_back(0);
1348 raw.push_back(0);
1349 raw.push_back(0);
1350 break;
1351 default:
1352 break;
1353 }
1354
1355 return raw;
1356 }
1357
1358 void test_clear_depth_stencil(const XGL_IMAGE_CREATE_INFO &img_info,
1359 XGL_FLOAT depth, XGL_UINT32 stencil,
1360 const std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> &ranges)
1361 {
1362 xgl_testing::Image img;
1363 img.init(dev_, img_info);
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001364 add_memory_ref(img, 0);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001365
1366 std::vector<XGL_IMAGE_STATE_TRANSITION> to_clear;
1367 std::vector<XGL_IMAGE_STATE_TRANSITION> to_xfer;
1368
1369 for (std::vector<XGL_IMAGE_SUBRESOURCE_RANGE>::const_iterator it = ranges.begin();
1370 it != ranges.end(); it++) {
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001371 to_clear.push_back(img.state_transition(XGL_IMAGE_STATE_UNINITIALIZED_TARGET, XGL_IMAGE_STATE_CLEAR, *it));
1372 to_xfer.push_back(img.state_transition(XGL_IMAGE_STATE_CLEAR, XGL_IMAGE_STATE_DATA_TRANSFER, *it));
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001373 }
1374
1375 cmd_.begin();
1376 xglCmdPrepareImages(cmd_.obj(), to_clear.size(), &to_clear[0]);
1377 xglCmdClearDepthStencil(cmd_.obj(), img.obj(), depth, stencil, ranges.size(), &ranges[0]);
1378 xglCmdPrepareImages(cmd_.obj(), to_xfer.size(), &to_xfer[0]);
1379 cmd_.end();
1380
1381 submit_and_done();
1382
1383 // cannot verify
1384 if (!img.transparent() && !img.copyable())
1385 return;
1386
1387 xgl_testing::ImageChecker checker(img_info, ranges);
1388
1389 checker.set_solid_pattern(ds_to_raw(img_info.format, depth, stencil));
1390 check_dst(img, checker);
1391 }
1392};
1393
1394TEST_F(XglCmdClearDepthStencilTest, Basic)
1395{
1396 for (std::vector<xgl_testing::Device::Format>::const_iterator it = test_formats_.begin();
1397 it != test_formats_.end(); it++) {
1398 // known driver issues
1399 if (it->format.channelFormat == XGL_CH_FMT_R8)
1400 continue;
1401
1402 XGL_IMAGE_CREATE_INFO img_info = xgl_testing::Image::create_info();
1403 img_info.imageType = XGL_IMAGE_2D;
1404 img_info.format = it->format;
1405 img_info.extent.width = 64;
1406 img_info.extent.height = 64;
1407 img_info.tiling = it->tiling;
1408 img_info.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
1409
1410 const XGL_IMAGE_SUBRESOURCE_RANGE range =
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001411 xgl_testing::Image::subresource_range(img_info, XGL_IMAGE_ASPECT_DEPTH);
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001412 std::vector<XGL_IMAGE_SUBRESOURCE_RANGE> ranges(&range, &range + 1);
1413
1414 test_clear_depth_stencil(img_info, 0.25f, 63, ranges);
1415 }
1416}
1417
1418}; // namespace
1419
Chia-I Wucb67c652014-10-21 11:06:26 +08001420int main(int argc, char **argv)
1421{
Chia-I Wucb67c652014-10-21 11:06:26 +08001422 ::testing::InitGoogleTest(&argc, argv);
Chia-I Wucb67c652014-10-21 11:06:26 +08001423
Chia-I Wu9dac52d2014-12-27 22:04:00 +08001424 xgl_testing::set_error_callback(test_error_callback);
1425
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001426 environment = new xgl_testing::Environment();
Chia-I Wucb67c652014-10-21 11:06:26 +08001427
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001428 if (!environment->parse_args(argc, argv))
1429 return -1;
Chia-I Wucb67c652014-10-21 11:06:26 +08001430
Chia-I Wu6170c9e2014-12-08 14:30:10 +08001431 ::testing::AddGlobalTestEnvironment(environment);
1432
1433 return RUN_ALL_TESTS();
Chia-I Wucb67c652014-10-21 11:06:26 +08001434}