Chia-I Wu | cb67c65 | 2014-10-21 11:06:26 +0800 | [diff] [blame] | 1 | // Copyright 2005, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | |
| 31 | // XGL tests |
| 32 | // |
| 33 | // Copyright (C) 2014 LunarG, Inc. |
| 34 | // |
| 35 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 36 | // copy of this software and associated documentation files (the "Software"), |
| 37 | // to deal in the Software without restriction, including without limitation |
| 38 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 39 | // and/or sell copies of the Software, and to permit persons to whom the |
| 40 | // Software is furnished to do so, subject to the following conditions: |
| 41 | // |
| 42 | // The above copyright notice and this permission notice shall be included |
| 43 | // in all copies or substantial portions of the Software. |
| 44 | // |
| 45 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 46 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 47 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 48 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 49 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 50 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 51 | // DEALINGS IN THE SOFTWARE. |
| 52 | |
| 53 | // Blit (copy, clear, and resolve) tests |
| 54 | |
| 55 | #include <string.h> |
| 56 | #include <xgl.h> |
| 57 | #include "gtest-1.7.0/include/gtest/gtest.h" |
| 58 | #include "xgldevice.h" |
| 59 | #include "xglimage.h" |
| 60 | #include "xgltestframework.h" |
| 61 | |
| 62 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 63 | |
| 64 | class XglBlitTest : public XglTestFramework |
| 65 | { |
| 66 | protected: |
| 67 | XGL_APPLICATION_INFO app_info; |
| 68 | XglDevice *m_device; |
| 69 | |
| 70 | XGL_CMD_BUFFER m_cmd; |
| 71 | |
| 72 | XGL_MEMORY_REF m_mem_refs[8]; |
| 73 | XGL_UINT m_mem_ref_count; |
| 74 | |
| 75 | virtual void SetUp(); |
| 76 | virtual void TearDown(); |
| 77 | |
| 78 | XGL_DEVICE device() { return m_device->device(); } |
| 79 | |
| 80 | void ClearMemoryRefs() |
| 81 | { |
| 82 | m_mem_ref_count = 0; |
| 83 | } |
| 84 | |
| 85 | void AddMemoryRef(XGL_GPU_MEMORY mem, bool readonly) |
| 86 | { |
| 87 | ASSERT_LE(m_mem_ref_count, ARRAY_SIZE(m_mem_refs)); |
| 88 | |
| 89 | m_mem_refs[m_mem_ref_count].mem = mem; |
| 90 | m_mem_refs[m_mem_ref_count].flags = |
| 91 | (readonly) ? XGL_MEMORY_REF_READ_ONLY_BIT : 0; |
| 92 | m_mem_ref_count++; |
| 93 | } |
| 94 | |
| 95 | XGL_GPU_MEMORY AllocMemory(XGL_GPU_SIZE size) |
| 96 | { |
| 97 | XGL_MEMORY_ALLOC_INFO info; |
| 98 | XGL_GPU_MEMORY mem; |
| 99 | XGL_RESULT err; |
| 100 | |
| 101 | memset(&info, 0, sizeof(info)); |
| 102 | info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 103 | info.allocationSize = size; |
| 104 | info.alignment = 1; |
| 105 | info.heapCount = 1; |
| 106 | info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 107 | |
| 108 | err = xglAllocMemory(device(), &info, &mem); |
| 109 | if (err) |
| 110 | mem = XGL_NULL_HANDLE; |
| 111 | |
| 112 | return mem; |
| 113 | } |
| 114 | |
| 115 | XGL_GPU_MEMORY AddMemory(XGL_GPU_SIZE size, bool readonly) |
| 116 | { |
| 117 | XGL_GPU_MEMORY mem; |
| 118 | |
| 119 | mem = AllocMemory(size); |
| 120 | if (mem) |
| 121 | AddMemoryRef(mem, readonly); |
| 122 | |
| 123 | return mem; |
| 124 | } |
| 125 | |
| 126 | void BeginCmd() |
| 127 | { |
| 128 | XGL_RESULT err; |
| 129 | |
| 130 | err = xglBeginCommandBuffer(m_cmd, |
| 131 | XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT | |
| 132 | XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT); |
| 133 | ASSERT_XGL_SUCCESS(err); |
| 134 | } |
| 135 | |
| 136 | void EndAndSubmitCmd() |
| 137 | { |
| 138 | XGL_RESULT err; |
| 139 | |
| 140 | err = xglEndCommandBuffer(m_cmd); |
| 141 | ASSERT_XGL_SUCCESS(err); |
| 142 | |
| 143 | err = xglQueueSubmit(m_device->m_queue, 1, &m_cmd, m_mem_ref_count, m_mem_refs, NULL ); |
| 144 | ASSERT_XGL_SUCCESS(err); |
| 145 | |
| 146 | err = xglQueueWaitIdle(m_device->m_queue); |
| 147 | ASSERT_XGL_SUCCESS(err); |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | void XglBlitTest::SetUp() |
| 152 | { |
| 153 | XGL_CMD_BUFFER_CREATE_INFO cmd_info; |
| 154 | XGL_PHYSICAL_GPU gpu; |
| 155 | XGL_UINT count; |
| 156 | XGL_RESULT err; |
| 157 | |
| 158 | memset(&app_info, 0, sizeof(app_info)); |
| 159 | app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO; |
| 160 | app_info.pNext = NULL; |
| 161 | app_info.pAppName = (const XGL_CHAR *) "blit tests"; |
| 162 | app_info.appVersion = 1; |
| 163 | app_info.pEngineName = (const XGL_CHAR *) "unittest"; |
| 164 | app_info.engineVersion = 1; |
| 165 | app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0); |
| 166 | |
| 167 | err = xglInitAndEnumerateGpus(&app_info, NULL, 1, &count, &gpu); |
| 168 | ASSERT_XGL_SUCCESS(err); |
Jon Ashburn | 19733c9 | 2014-11-26 11:06:49 -0700 | [diff] [blame] | 169 | ASSERT_GE(count, 1) << "No GPU available"; |
Chia-I Wu | cb67c65 | 2014-10-21 11:06:26 +0800 | [diff] [blame] | 170 | |
| 171 | m_device = new XglDevice(0, gpu); |
| 172 | m_device->get_device_queue(); |
| 173 | |
| 174 | memset(&cmd_info, 0, sizeof(cmd_info)); |
| 175 | cmd_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO; |
| 176 | cmd_info.queueType = XGL_QUEUE_TYPE_GRAPHICS; |
| 177 | |
| 178 | err = xglCreateCommandBuffer(device(), &cmd_info, &m_cmd); |
| 179 | ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed"; |
| 180 | } |
| 181 | |
| 182 | void XglBlitTest::TearDown() |
| 183 | { |
| 184 | XGL_UINT dummy_count; |
| 185 | |
| 186 | xglDestroyObject(m_cmd); |
| 187 | xglInitAndEnumerateGpus(&app_info, NULL, 0, &dummy_count, NULL); |
| 188 | } |
| 189 | |
| 190 | TEST_F(XglBlitTest, FillMemory) |
| 191 | { |
| 192 | const struct { |
| 193 | XGL_GPU_SIZE offset; |
| 194 | XGL_GPU_SIZE size; |
| 195 | XGL_UINT value; |
| 196 | } ranges[] = { |
| 197 | { 0, 64, 0x11111111 }, // 16-byte aligned |
| 198 | { 64, 12, 0x22222222 }, // 4-byte aligned |
| 199 | { 76, 4, 0x33333333 }, // min. fill size |
| 200 | { 80, 20, 0x44444444 }, |
| 201 | { 92, 8, 0x55555555 }, // overlapped |
| 202 | }; |
| 203 | XGL_GPU_MEMORY mem; |
| 204 | XGL_RESULT err; |
| 205 | void *data; |
| 206 | XGL_UINT i; |
| 207 | |
| 208 | ClearMemoryRefs(); |
| 209 | |
| 210 | mem = AddMemory(256, false); |
| 211 | ASSERT_NE((XGL_GPU_MEMORY) XGL_NULL_HANDLE, mem); |
| 212 | |
| 213 | BeginCmd(); |
| 214 | for (i = 0; i < ARRAY_SIZE(ranges); i++) { |
| 215 | xglCmdFillMemory(m_cmd, mem, ranges[i].offset, |
| 216 | ranges[i].size, ranges[i].value); |
| 217 | } |
| 218 | EndAndSubmitCmd(); |
| 219 | |
| 220 | err = xglMapMemory(mem, 0, &data); |
| 221 | ASSERT_XGL_SUCCESS(err); |
| 222 | |
| 223 | for (i = 0; i < ARRAY_SIZE(ranges); i++) { |
| 224 | const XGL_UINT expected = ranges[i].value; |
| 225 | const XGL_UINT *real = (const XGL_UINT *) |
| 226 | ((char *) data + ranges[i].offset); |
| 227 | XGL_UINT count, j; |
| 228 | |
| 229 | count = ranges[i].size / 4; |
| 230 | |
| 231 | /* check if the next range overlaps */ |
| 232 | if (i + 1 < ARRAY_SIZE(ranges)) { |
| 233 | if (ranges[i].offset + ranges[i].size > ranges[i + 1].offset) |
| 234 | count = (ranges[i + 1].offset - ranges[i].offset) / 4; |
| 235 | } |
| 236 | |
| 237 | for (j = 0; j < count; j++) |
| 238 | ASSERT_EQ(expected, real[j]); |
| 239 | } |
| 240 | |
| 241 | xglUnmapMemory(mem); |
| 242 | xglFreeMemory(mem); |
| 243 | } |
| 244 | |
Chia-I Wu | ea9367f | 2014-11-23 02:16:45 +0800 | [diff] [blame] | 245 | TEST_F(XglBlitTest, FillMemoryLarge) |
| 246 | { |
| 247 | const XGL_GPU_SIZE size = 32 * 1024 * 1024; |
| 248 | XGL_GPU_MEMORY mem; |
| 249 | XGL_RESULT err; |
| 250 | XGL_GPU_SIZE offset; |
| 251 | const char *data; |
| 252 | |
| 253 | ClearMemoryRefs(); |
| 254 | |
| 255 | mem = AddMemory(size, false); |
| 256 | ASSERT_NE((XGL_GPU_MEMORY) XGL_NULL_HANDLE, mem); |
| 257 | |
| 258 | BeginCmd(); |
| 259 | xglCmdFillMemory(m_cmd, mem, 0, size / 2, 0x01234567); |
| 260 | xglCmdFillMemory(m_cmd, mem, size / 2, size / 2, 0x89abcdef); |
| 261 | EndAndSubmitCmd(); |
| 262 | |
| 263 | err = xglMapMemory(mem, 0, (void **) &data); |
| 264 | ASSERT_XGL_SUCCESS(err); |
| 265 | |
| 266 | for (offset = 0; offset < size / 2; offset += 4) { |
| 267 | ASSERT_EQ(0x01234567, *((const XGL_UINT32 *) (data + offset))) << |
| 268 | "Offset is: " << offset; |
| 269 | } |
| 270 | for (; offset < size; offset += 4) { |
| 271 | ASSERT_EQ(0x89abcdef, *((const XGL_UINT32 *) (data + offset))) << |
| 272 | "Offset is: " << offset; |
| 273 | } |
| 274 | |
| 275 | xglUnmapMemory(mem); |
| 276 | xglFreeMemory(mem); |
| 277 | } |
| 278 | |
Chia-I Wu | cb67c65 | 2014-10-21 11:06:26 +0800 | [diff] [blame] | 279 | TEST_F(XglBlitTest, CopyMemory) |
| 280 | { |
| 281 | XGL_GPU_MEMORY src, dst; |
| 282 | XGL_MEMORY_COPY regions[17]; |
| 283 | XGL_RESULT err; |
| 284 | void *data; |
| 285 | XGL_UINT i; |
| 286 | |
| 287 | ClearMemoryRefs(); |
| 288 | |
| 289 | src = AddMemory(256, false); |
| 290 | ASSERT_NE((XGL_GPU_MEMORY) XGL_NULL_HANDLE, src); |
| 291 | |
| 292 | err = xglMapMemory(src, 0, &data); |
| 293 | ASSERT_XGL_SUCCESS(err); |
| 294 | for (i = 0; i < 256; i++) |
| 295 | ((char *) data)[i] = i; |
| 296 | xglUnmapMemory(src); |
| 297 | |
| 298 | dst = AddMemory(256, false); |
| 299 | ASSERT_NE((XGL_GPU_MEMORY) XGL_NULL_HANDLE, dst); |
| 300 | |
| 301 | /* copy with various alignments */ |
| 302 | for (i = 0; i < 16; i++) { |
| 303 | regions[i].copySize = i + 1; |
| 304 | regions[i].srcOffset = i * 8; |
| 305 | |
| 306 | if (i > 0) { |
| 307 | regions[i].destOffset = regions[i - 1].destOffset + |
| 308 | regions[i - 1].copySize; |
| 309 | } else { |
| 310 | regions[i].destOffset = 0; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | regions[i].srcOffset = 192; |
| 315 | regions[i].destOffset = 192; |
| 316 | regions[i].copySize = 64; |
| 317 | |
| 318 | BeginCmd(); |
| 319 | |
| 320 | xglCmdCopyMemory(m_cmd, src, dst, 16, regions); |
| 321 | xglCmdCopyMemory(m_cmd, src, dst, 1, ®ions[16]); |
| 322 | |
| 323 | EndAndSubmitCmd(); |
| 324 | |
| 325 | err = xglMapMemory(dst, 0, &data); |
| 326 | ASSERT_XGL_SUCCESS(err); |
| 327 | |
| 328 | for (i = 0; i < ARRAY_SIZE(regions); i++) { |
| 329 | const unsigned char *real = (const unsigned char *) data + |
| 330 | regions[i].destOffset; |
| 331 | XGL_UINT j; |
| 332 | |
| 333 | for (j = 0; j < regions[i].copySize; j++) |
| 334 | ASSERT_EQ(regions[i].srcOffset + j, real[j]); |
| 335 | } |
| 336 | |
| 337 | xglUnmapMemory(dst); |
| 338 | xglFreeMemory(src); |
| 339 | xglFreeMemory(dst); |
| 340 | } |
| 341 | |
Chia-I Wu | 3c3fd12 | 2014-11-22 02:51:25 +0800 | [diff] [blame] | 342 | TEST_F(XglBlitTest, CopyMemoryHazard) |
| 343 | { |
| 344 | XGL_GPU_MEMORY mem[3]; |
| 345 | XGL_MEMORY_COPY region; |
| 346 | XGL_MEMORY_STATE_TRANSITION transition; |
| 347 | XGL_RESULT err; |
| 348 | void *data; |
| 349 | XGL_UINT i; |
| 350 | |
| 351 | ClearMemoryRefs(); |
| 352 | |
| 353 | for (i = 0; i < 3; i++) { |
| 354 | mem[i] = AddMemory(256, false); |
| 355 | ASSERT_NE((XGL_GPU_MEMORY) XGL_NULL_HANDLE, mem[i]); |
| 356 | |
| 357 | err = xglMapMemory(mem[i], 0, &data); |
| 358 | ASSERT_XGL_SUCCESS(err); |
| 359 | ((char *) data)[0] = 5 * (i + 1); |
| 360 | xglUnmapMemory(mem[i]); |
| 361 | } |
| 362 | |
| 363 | region.srcOffset = 0; |
| 364 | region.destOffset = 0; |
| 365 | region.copySize = 1; |
| 366 | |
| 367 | BeginCmd(); |
| 368 | |
| 369 | xglCmdCopyMemory(m_cmd, mem[0], mem[1], 1, ®ion); |
| 370 | |
| 371 | memset(&transition, 0, sizeof(transition)); |
| 372 | transition.sType = XGL_STRUCTURE_TYPE_MEMORY_STATE_TRANSITION; |
| 373 | transition.mem = mem[1]; |
| 374 | transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER; |
| 375 | transition.newState = XGL_MEMORY_STATE_DATA_TRANSFER; |
| 376 | transition.offset = region.destOffset; |
| 377 | transition.regionSize = region.copySize; |
| 378 | xglCmdPrepareMemoryRegions(m_cmd, 1, &transition); |
| 379 | |
| 380 | xglCmdCopyMemory(m_cmd, mem[1], mem[2], 1, ®ion); |
| 381 | |
| 382 | EndAndSubmitCmd(); |
| 383 | |
| 384 | err = xglMapMemory(mem[2], 0, &data); |
| 385 | ASSERT_XGL_SUCCESS(err); |
| 386 | |
| 387 | ASSERT_EQ(5, ((const unsigned char *) data)[0]); |
| 388 | |
| 389 | xglUnmapMemory(mem[2]); |
| 390 | |
| 391 | for (i = 0; i < 3; i++) |
| 392 | xglFreeMemory(mem[i]); |
| 393 | } |
| 394 | |
Chia-I Wu | ea9367f | 2014-11-23 02:16:45 +0800 | [diff] [blame] | 395 | TEST_F(XglBlitTest, CopyMemoryLarge) |
| 396 | { |
| 397 | const XGL_GPU_SIZE size = 32 * 1024 * 1024; |
| 398 | XGL_GPU_MEMORY src, dst; |
| 399 | XGL_MEMORY_COPY region; |
| 400 | XGL_RESULT err; |
| 401 | void *data; |
| 402 | XGL_UINT i; |
| 403 | |
| 404 | ClearMemoryRefs(); |
| 405 | |
| 406 | src = AddMemory(size, false); |
| 407 | ASSERT_NE((XGL_GPU_MEMORY) XGL_NULL_HANDLE, src); |
| 408 | |
| 409 | err = xglMapMemory(src, 0, &data); |
| 410 | ASSERT_XGL_SUCCESS(err); |
| 411 | for (i = 0; i < size / 4; i++) |
| 412 | ((uint32_t *) data)[i] = i; |
| 413 | xglUnmapMemory(src); |
| 414 | |
| 415 | dst = AddMemory(size, false); |
| 416 | ASSERT_NE((XGL_GPU_MEMORY) XGL_NULL_HANDLE, dst); |
| 417 | |
| 418 | region.srcOffset = 0; |
| 419 | region.destOffset = 0; |
| 420 | region.copySize = size; |
| 421 | |
| 422 | BeginCmd(); |
| 423 | |
| 424 | xglCmdCopyMemory(m_cmd, src, dst, 1, ®ion); |
| 425 | |
| 426 | EndAndSubmitCmd(); |
| 427 | |
| 428 | err = xglMapMemory(dst, 0, &data); |
| 429 | ASSERT_XGL_SUCCESS(err); |
| 430 | |
| 431 | for (i = 0; i < size / 4; i++) |
| 432 | ASSERT_EQ(i, ((const uint32_t *) data)[i]); |
| 433 | |
| 434 | xglUnmapMemory(dst); |
| 435 | xglFreeMemory(src); |
| 436 | xglFreeMemory(dst); |
| 437 | } |
| 438 | |
Chia-I Wu | cb67c65 | 2014-10-21 11:06:26 +0800 | [diff] [blame] | 439 | TEST_F(XglBlitTest, ClearColorImageBasic) |
| 440 | { |
| 441 | const XGL_FLOAT color[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; |
| 442 | const XGL_UINT width = 64; |
| 443 | const XGL_UINT height = 64; |
| 444 | XglImage *img; |
| 445 | XGL_FORMAT format; |
| 446 | XGL_IMAGE_SUBRESOURCE subres; |
| 447 | XGL_IMAGE_SUBRESOURCE_RANGE subres_range; |
| 448 | XGL_IMAGE_STATE_TRANSITION transition; |
| 449 | XGL_RESULT err; |
| 450 | |
| 451 | format.channelFormat = XGL_CH_FMT_R8G8B8A8; |
| 452 | format.numericFormat = XGL_NUM_FMT_UNORM; |
| 453 | |
| 454 | subres.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 455 | subres.mipLevel = 0; |
| 456 | subres.arraySlice = 0; |
| 457 | |
| 458 | subres_range.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 459 | subres_range.baseMipLevel = 0; |
| 460 | subres_range.mipLevels = 1; |
| 461 | subres_range.baseArraySlice = 0; |
| 462 | subres_range.arraySize = 1; |
| 463 | |
| 464 | img = new XglImage(m_device); |
| 465 | img->init(width, height, format, XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 466 | |
| 467 | ClearMemoryRefs(); |
| 468 | AddMemoryRef(img->memory(), false); |
| 469 | |
| 470 | BeginCmd(); |
| 471 | |
| 472 | transition.image = img->image(); |
| 473 | transition.oldState = XGL_IMAGE_STATE_UNINITIALIZED_TARGET; |
| 474 | transition.newState = XGL_IMAGE_STATE_CLEAR; |
| 475 | transition.subresourceRange = subres_range; |
| 476 | xglCmdPrepareImages(m_cmd, 1, &transition); |
| 477 | |
| 478 | xglCmdClearColorImage(m_cmd, img->image(), color, 1, &subres_range); |
| 479 | |
| 480 | EndAndSubmitCmd(); |
| 481 | |
| 482 | { |
| 483 | XGL_SUBRESOURCE_LAYOUT layout; |
Jon Ashburn | 6317c49 | 2014-11-21 11:33:20 -0700 | [diff] [blame] | 484 | XGL_SIZE layout_size= sizeof(layout); |
Chia-I Wu | cb67c65 | 2014-10-21 11:06:26 +0800 | [diff] [blame] | 485 | XGL_UINT x, y; |
| 486 | void *data; |
| 487 | |
| 488 | err = img->MapMemory(&data); |
| 489 | ASSERT_XGL_SUCCESS(err); |
| 490 | |
| 491 | err = xglGetImageSubresourceInfo(img->image(), &subres, |
| 492 | XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout); |
| 493 | ASSERT_XGL_SUCCESS(err); |
| 494 | ASSERT_EQ(sizeof(layout), layout_size); |
| 495 | |
| 496 | for (y = 0; y < height; y++) { |
| 497 | const XGL_UINT *real = (const XGL_UINT *) |
| 498 | ((char *) data + layout.offset + layout.rowPitch * y); |
| 499 | |
| 500 | for (x = 0; x < width; x++) |
| 501 | ASSERT_EQ(0xff00ff00, real[x]); |
| 502 | } |
| 503 | |
| 504 | img->UnmapMemory(); |
| 505 | } |
| 506 | |
| 507 | delete img; |
| 508 | } |
| 509 | |
Chia-I Wu | 8682263 | 2014-11-22 15:09:42 +0800 | [diff] [blame] | 510 | TEST_F(XglBlitTest, CopyImageBasic) |
| 511 | { |
| 512 | const XGL_FLOAT color[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; |
| 513 | const XGL_UINT width = 64; |
| 514 | const XGL_UINT height = 64; |
| 515 | XglImage *src, *dst; |
| 516 | XGL_FORMAT format; |
| 517 | XGL_IMAGE_SUBRESOURCE subres; |
| 518 | XGL_IMAGE_SUBRESOURCE_RANGE subres_range; |
| 519 | XGL_IMAGE_STATE_TRANSITION transition; |
| 520 | XGL_RESULT err; |
| 521 | |
| 522 | format.channelFormat = XGL_CH_FMT_R8G8B8A8; |
| 523 | format.numericFormat = XGL_NUM_FMT_UNORM; |
| 524 | |
| 525 | subres.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 526 | subres.mipLevel = 0; |
| 527 | subres.arraySlice = 0; |
| 528 | |
| 529 | subres_range.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 530 | subres_range.baseMipLevel = 0; |
| 531 | subres_range.mipLevels = 1; |
| 532 | subres_range.baseArraySlice = 0; |
| 533 | subres_range.arraySize = 1; |
| 534 | |
| 535 | src = new XglImage(m_device); |
| 536 | src->init(width, height, format, XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 537 | dst = new XglImage(m_device); |
| 538 | dst->init(width, height, format, XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 539 | |
| 540 | ClearMemoryRefs(); |
| 541 | AddMemoryRef(src->memory(), false); |
| 542 | AddMemoryRef(dst->memory(), false); |
| 543 | |
| 544 | BeginCmd(); |
| 545 | |
| 546 | transition.image = src->image(); |
| 547 | transition.oldState = XGL_IMAGE_STATE_UNINITIALIZED_TARGET; |
| 548 | transition.newState = XGL_IMAGE_STATE_CLEAR; |
| 549 | transition.subresourceRange = subres_range; |
| 550 | xglCmdPrepareImages(m_cmd, 1, &transition); |
| 551 | |
| 552 | xglCmdClearColorImage(m_cmd, src->image(), color, 1, &subres_range); |
| 553 | |
| 554 | transition.oldState = XGL_IMAGE_STATE_CLEAR; |
| 555 | transition.newState = XGL_IMAGE_STATE_DATA_TRANSFER; |
| 556 | xglCmdPrepareImages(m_cmd, 1, &transition); |
| 557 | |
| 558 | transition.image = dst->image(); |
| 559 | transition.oldState = XGL_IMAGE_STATE_UNINITIALIZED_TARGET; |
| 560 | transition.newState = XGL_IMAGE_STATE_DATA_TRANSFER; |
| 561 | xglCmdPrepareImages(m_cmd, 1, &transition); |
| 562 | |
| 563 | XGL_IMAGE_COPY copy; |
| 564 | memset(©, 0, sizeof(copy)); |
| 565 | copy.srcSubresource = subres; |
| 566 | copy.destSubresource = subres; |
| 567 | copy.extent.width = width; |
| 568 | copy.extent.height = height; |
| 569 | copy.extent.depth = 1; |
| 570 | xglCmdCopyImage(m_cmd, src->image(), dst->image(), 1, ©); |
| 571 | |
| 572 | EndAndSubmitCmd(); |
| 573 | |
| 574 | { |
| 575 | XGL_SUBRESOURCE_LAYOUT layout; |
| 576 | XGL_SIZE layout_size; |
| 577 | void *data; |
| 578 | XGL_UINT x, y; |
| 579 | |
| 580 | err = dst->MapMemory(&data); |
| 581 | ASSERT_XGL_SUCCESS(err); |
| 582 | |
| 583 | err = xglGetImageSubresourceInfo(dst->image(), &subres, |
| 584 | XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout); |
| 585 | ASSERT_XGL_SUCCESS(err); |
| 586 | ASSERT_EQ(sizeof(layout), layout_size); |
| 587 | |
| 588 | for (y = 0; y < height; y++) { |
| 589 | const XGL_UINT *real = (const XGL_UINT *) |
| 590 | ((char *) data + layout.offset + layout.rowPitch * y); |
| 591 | |
| 592 | for (x = 0; x < width; x++) |
| 593 | ASSERT_EQ(0xff00ff00, real[x]) << "location:" << x << "," << y << ": 0x" << std::hex << real[x] << '\n'; |
| 594 | } |
| 595 | |
| 596 | dst->UnmapMemory(); |
| 597 | } |
| 598 | |
| 599 | delete src; |
| 600 | delete dst; |
| 601 | } |
| 602 | |
Chia-I Wu | f7ff6b4 | 2014-11-22 16:24:41 +0800 | [diff] [blame^] | 603 | TEST_F(XglBlitTest, CopyMemoryToImageBasic) |
| 604 | { |
| 605 | const XGL_UINT width = 64; |
| 606 | const XGL_UINT height = 64; |
| 607 | XGL_FORMAT format; |
| 608 | XGL_GPU_MEMORY mem; |
| 609 | XglImage *img; |
| 610 | XGL_MEMORY_IMAGE_COPY region; |
| 611 | XGL_RESULT err; |
| 612 | XGL_UINT x, y; |
| 613 | void *data; |
| 614 | |
| 615 | format.channelFormat = XGL_CH_FMT_R8G8B8A8; |
| 616 | format.numericFormat = XGL_NUM_FMT_UNORM; |
| 617 | |
| 618 | ClearMemoryRefs(); |
| 619 | |
| 620 | mem = AddMemory(width * height * 4, true); |
| 621 | ASSERT_NE((XGL_GPU_MEMORY) XGL_NULL_HANDLE, mem); |
| 622 | |
| 623 | err = xglMapMemory(mem, 0, &data); |
| 624 | ASSERT_XGL_SUCCESS(err); |
| 625 | for (y = 0; y < height; y++) { |
| 626 | for (x = 0; x < width; x++) |
| 627 | *((uint32_t *) data + y * width + x) = y << 16 | x; |
| 628 | } |
| 629 | |
| 630 | xglUnmapMemory(mem); |
| 631 | |
| 632 | img = new XglImage(m_device); |
| 633 | img->init(width, height, format, XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 634 | |
| 635 | AddMemoryRef(img->memory(), false); |
| 636 | |
| 637 | BeginCmd(); |
| 638 | |
| 639 | memset(®ion, 0, sizeof(region)); |
| 640 | region.imageSubresource.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 641 | region.imageExtent.width = width; |
| 642 | region.imageExtent.height = height; |
| 643 | region.imageExtent.depth = 1; |
| 644 | xglCmdCopyMemoryToImage(m_cmd, mem, img->image(), 1, ®ion); |
| 645 | |
| 646 | EndAndSubmitCmd(); |
| 647 | |
| 648 | { |
| 649 | XGL_SUBRESOURCE_LAYOUT layout; |
| 650 | XGL_SIZE layout_size = sizeof(layout); |
| 651 | |
| 652 | err = img->MapMemory(&data); |
| 653 | ASSERT_XGL_SUCCESS(err); |
| 654 | |
| 655 | err = xglGetImageSubresourceInfo(img->image(), |
| 656 | ®ion.imageSubresource, XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, |
| 657 | &layout_size, &layout); |
| 658 | ASSERT_XGL_SUCCESS(err); |
| 659 | ASSERT_EQ(sizeof(layout), layout_size); |
| 660 | |
| 661 | for (y = 0; y < height; y++) { |
| 662 | const uint32_t *real = (const uint32_t *) |
| 663 | ((char *) data + layout.offset + layout.rowPitch * y); |
| 664 | |
| 665 | for (x = 0; x < width; x++) { |
| 666 | ASSERT_EQ(y << 16 | x, real[x]) << |
| 667 | "at (" << x << ", " << y << ")\n"; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | img->UnmapMemory(); |
| 672 | } |
| 673 | } |
| 674 | |
Chia-I Wu | cb67c65 | 2014-10-21 11:06:26 +0800 | [diff] [blame] | 675 | TEST_F(XglBlitTest, ClearDepthStencilBasic) |
| 676 | { |
| 677 | const XGL_FLOAT clear_depth = 0.4f; |
| 678 | const XGL_UINT width = 64; |
| 679 | const XGL_UINT height = 64; |
| 680 | XglImage *img; |
| 681 | XGL_FORMAT format; |
| 682 | XGL_IMAGE_SUBRESOURCE subres; |
| 683 | XGL_IMAGE_SUBRESOURCE_RANGE subres_range; |
| 684 | XGL_IMAGE_STATE_TRANSITION transition; |
| 685 | XGL_RESULT err; |
| 686 | |
| 687 | format.channelFormat = XGL_CH_FMT_R32; |
| 688 | format.numericFormat = XGL_NUM_FMT_DS; |
| 689 | |
| 690 | subres.aspect = XGL_IMAGE_ASPECT_DEPTH; |
| 691 | subres.mipLevel = 0; |
| 692 | subres.arraySlice = 0; |
| 693 | |
| 694 | subres_range.aspect = XGL_IMAGE_ASPECT_DEPTH; |
| 695 | subres_range.baseMipLevel = 0; |
| 696 | subres_range.mipLevels = 1; |
| 697 | subres_range.baseArraySlice = 0; |
| 698 | subres_range.arraySize = 1; |
| 699 | |
| 700 | img = new XglImage(m_device); |
| 701 | img->init(width, height, format, XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
| 702 | XGL_OPTIMAL_TILING); |
| 703 | |
| 704 | ClearMemoryRefs(); |
| 705 | AddMemoryRef(img->memory(), false); |
| 706 | |
| 707 | BeginCmd(); |
| 708 | |
| 709 | transition.image = img->image(); |
| 710 | transition.oldState = XGL_IMAGE_STATE_UNINITIALIZED_TARGET; |
| 711 | transition.newState = XGL_IMAGE_STATE_CLEAR; |
| 712 | transition.subresourceRange = subres_range; |
| 713 | xglCmdPrepareImages(m_cmd, 1, &transition); |
| 714 | |
| 715 | xglCmdClearDepthStencil(m_cmd, img->image(), clear_depth, |
| 716 | 0, 1, &subres_range); |
| 717 | |
| 718 | EndAndSubmitCmd(); |
| 719 | |
| 720 | /* |
| 721 | * TODO xglCmdCopyImageToMemory to linearize |
| 722 | * |
| 723 | * This works only because xglMapMemory calls intel_bo_map_gtt_async. |
| 724 | */ |
| 725 | { |
| 726 | XGL_SUBRESOURCE_LAYOUT layout; |
Jon Ashburn | 6317c49 | 2014-11-21 11:33:20 -0700 | [diff] [blame] | 727 | XGL_SIZE layout_size = sizeof(layout); |
Chia-I Wu | cb67c65 | 2014-10-21 11:06:26 +0800 | [diff] [blame] | 728 | XGL_UINT x, y; |
| 729 | void *data; |
| 730 | |
| 731 | err = img->MapMemory(&data); |
| 732 | ASSERT_XGL_SUCCESS(err); |
| 733 | |
| 734 | err = xglGetImageSubresourceInfo(img->image(), &subres, |
| 735 | XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout); |
| 736 | ASSERT_XGL_SUCCESS(err); |
| 737 | ASSERT_EQ(sizeof(layout), layout_size); |
| 738 | |
| 739 | for (y = 0; y < height; y++) { |
| 740 | const float *real = (const float *) |
| 741 | ((char *) data + layout.offset + layout.rowPitch * y); |
| 742 | |
| 743 | for (x = 0; x < width; x++) |
| 744 | ASSERT_EQ(clear_depth, real[x]); |
| 745 | } |
| 746 | |
| 747 | img->UnmapMemory(); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | int main(int argc, char **argv) |
| 752 | { |
| 753 | int result; |
| 754 | |
| 755 | ::testing::InitGoogleTest(&argc, argv); |
| 756 | XglTestFramework::InitArgs(&argc, argv); |
| 757 | |
| 758 | ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment); |
| 759 | |
| 760 | result = RUN_ALL_TESTS(); |
| 761 | |
| 762 | XglTestFramework::Finish(); |
| 763 | return result; |
| 764 | } |