blob: fd62f0e9b9329e9d22d2e61327142e2ffda5dfb3 [file] [log] [blame]
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -06001// 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
54// Verify XGL driver initialization
55
56#include <stdlib.h>
57#include <stdio.h>
58#include <stdbool.h>
59#include <string.h>
60
61#include <xgl.h>
62#include "gtest-1.7.0/include/gtest/gtest.h"
Chia-I Wufe84fe12014-12-29 15:39:23 +080063#include "xgltestbinding.h"
64#include "test_common.h"
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -060065
66class XglImageTest : public ::testing::Test {
67public:
Mark Lobodzinski17caf572015-01-29 08:55:56 -060068 void CreateImage(uint32_t w, uint32_t h);
Courtney Goeltzenleuchter80ea59a2014-08-14 17:41:57 -060069 void DestroyImage();
70
71 void CreateImageView(XGL_IMAGE_VIEW_CREATE_INFO* pCreateInfo,
72 XGL_IMAGE_VIEW* pView);
73 void DestroyImageView(XGL_IMAGE_VIEW imageView);
Chia-I Wufe84fe12014-12-29 15:39:23 +080074 XGL_DEVICE device() {return m_device->obj();}
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -060075
76protected:
Chia-I Wufe84fe12014-12-29 15:39:23 +080077 xgl_testing::Device *m_device;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -060078 XGL_APPLICATION_INFO app_info;
Chia-I Wuaf11d922014-12-28 14:37:25 +080079 XGL_PHYSICAL_GPU objs[XGL_MAX_PHYSICAL_GPUS];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060080 uint32_t gpu_count;
Jon Ashburn1e464892015-01-29 15:48:00 -070081 XGL_INSTANCE inst;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -060082 XGL_IMAGE m_image;
Jon Ashburn7e781952015-01-16 09:37:43 -070083 XGL_GPU_MEMORY *m_image_mem;
Mark Lobodzinski17caf572015-01-29 08:55:56 -060084 uint32_t m_num_mem;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -060085
86 virtual void SetUp() {
87 XGL_RESULT err;
88
89 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
90 this->app_info.pNext = NULL;
Chia-I Wuf1a5a742014-12-27 15:16:07 +080091 this->app_info.pAppName = "base";
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -060092 this->app_info.appVersion = 1;
Chia-I Wuf1a5a742014-12-27 15:16:07 +080093 this->app_info.pEngineName = "unittest";
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -060094 this->app_info.engineVersion = 1;
Courtney Goeltzenleuchter5adee1c2015-02-23 17:40:15 -070095 this->app_info.apiVersion = XGL_API_VERSION;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -060096
Jon Ashburn1e464892015-01-29 15:48:00 -070097 err = xglCreateInstance(&app_info, NULL, &this->inst);
98 ASSERT_XGL_SUCCESS(err);
99 err = xglEnumerateGpus(this->inst, XGL_MAX_PHYSICAL_GPUS,
100 &this->gpu_count, objs);
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600101 ASSERT_XGL_SUCCESS(err);
Jon Ashburnbf843b22014-11-26 11:06:49 -0700102 ASSERT_GE(this->gpu_count, 1) << "No GPU available";
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600103
Chia-I Wufe84fe12014-12-29 15:39:23 +0800104 this->m_device = new xgl_testing::Device(objs[0]);
105 this->m_device->init();
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600106 }
107
108 virtual void TearDown() {
Jon Ashburn1e464892015-01-29 15:48:00 -0700109 xglDestroyInstance(this->inst);
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600110 }
111};
112
113
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600114void XglImageTest::CreateImage(uint32_t w, uint32_t h)
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600115{
116 XGL_RESULT err;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600117 uint32_t mipCount;
118 size_t size;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600119 XGL_FORMAT fmt;
120 XGL_FORMAT_PROPERTIES image_fmt;
121
122 mipCount = 0;
123
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600124 uint32_t _w = w;
125 uint32_t _h = h;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600126 while( ( _w > 0 ) || ( _h > 0 ) )
127 {
128 _w >>= 1;
129 _h >>= 1;
130 mipCount++;
131 }
132
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700133 fmt = XGL_FMT_R8G8B8A8_UINT;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600134 // TODO: Pick known good format rather than just expect common format
135 /*
136 * XXX: What should happen if given NULL HANDLE for the pData argument?
137 * We're not requesting XGL_INFO_TYPE_MEMORY_REQUIREMENTS so there is
138 * an expectation that pData is a valid pointer.
139 * However, why include a returned size value? That implies that the
140 * amount of data may vary and that doesn't work well for using a
141 * fixed structure.
142 */
Jon Ashburne494a1a2014-09-25 14:36:58 -0600143 size = sizeof(image_fmt);
Courtney Goeltzenleuchtercc5eb3a2014-08-19 18:35:50 -0600144 err = xglGetFormatInfo(this->device(), fmt,
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600145 XGL_INFO_TYPE_FORMAT_PROPERTIES,
146 &size, &image_fmt);
147 ASSERT_XGL_SUCCESS(err);
148
149 // typedef struct _XGL_IMAGE_CREATE_INFO
150 // {
151 // XGL_STRUCTURE_TYPE sType; // Must be XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600152 // const void* pNext; // Pointer to next structure.
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600153 // XGL_IMAGE_TYPE imageType;
154 // XGL_FORMAT format;
155 // XGL_EXTENT3D extent;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600156 // uint32_t mipLevels;
157 // uint32_t arraySize;
158 // uint32_t samples;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600159 // XGL_IMAGE_TILING tiling;
160 // XGL_FLAGS usage; // XGL_IMAGE_USAGE_FLAGS
161 // XGL_FLAGS flags; // XGL_IMAGE_CREATE_FLAGS
162 // } XGL_IMAGE_CREATE_INFO;
163
164
165 XGL_IMAGE_CREATE_INFO imageCreateInfo = {};
166 imageCreateInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
167 imageCreateInfo.imageType = XGL_IMAGE_2D;
168 imageCreateInfo.format = fmt;
169 imageCreateInfo.arraySize = 1;
170 imageCreateInfo.extent.width = w;
171 imageCreateInfo.extent.height = h;
172 imageCreateInfo.extent.depth = 1;
173 imageCreateInfo.mipLevels = mipCount;
174 imageCreateInfo.samples = 1;
175 imageCreateInfo.tiling = XGL_LINEAR_TILING;
176
177 // Image usage flags
178 // typedef enum _XGL_IMAGE_USAGE_FLAGS
179 // {
180 // XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT = 0x00000001,
181 // XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT = 0x00000002,
182 // XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT = 0x00000004,
183 // XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT = 0x00000008,
184 // } XGL_IMAGE_USAGE_FLAGS;
185 imageCreateInfo.usage = XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
186
187 // XGL_RESULT XGLAPI xglCreateImage(
188 // XGL_DEVICE device,
189 // const XGL_IMAGE_CREATE_INFO* pCreateInfo,
190 // XGL_IMAGE* pImage);
Courtney Goeltzenleuchtercc5eb3a2014-08-19 18:35:50 -0600191 err = xglCreateImage(device(), &imageCreateInfo, &m_image);
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600192 ASSERT_XGL_SUCCESS(err);
193
Jon Ashburn7e781952015-01-16 09:37:43 -0700194 XGL_MEMORY_REQUIREMENTS *mem_req;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600195 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburn8a5da752015-01-19 15:00:26 -0700196 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600197 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
198 uint32_t num_allocations = 0;
199 size_t num_alloc_size = sizeof(num_allocations);
Jon Ashburn8a5da752015-01-19 15:00:26 -0700200 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
201 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
202 .pNext = NULL,
203 };
204 XGL_MEMORY_ALLOC_INFO mem_info = {};
205 mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
206 mem_info.pNext = &img_alloc;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600207
Jon Ashburn7e781952015-01-16 09:37:43 -0700208 err = xglGetObjectInfo(m_image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
209 &num_alloc_size, &num_allocations);
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600210 ASSERT_XGL_SUCCESS(err);
Jon Ashburn7e781952015-01-16 09:37:43 -0700211 ASSERT_EQ(num_alloc_size,sizeof(num_allocations));
212 mem_req = (XGL_MEMORY_REQUIREMENTS *) malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
213 m_image_mem = (XGL_GPU_MEMORY *) malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
214 m_num_mem = num_allocations;
215 err = xglGetObjectInfo(m_image,
216 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
217 &mem_reqs_size, mem_req);
218 ASSERT_XGL_SUCCESS(err);
219 ASSERT_EQ(mem_reqs_size, num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburn8a5da752015-01-19 15:00:26 -0700220 err = xglGetObjectInfo(m_image,
221 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
222 &img_reqs_size, &img_reqs);
223 ASSERT_XGL_SUCCESS(err);
224 ASSERT_EQ(img_reqs_size, sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
225 img_alloc.usage = img_reqs.usage;
226 img_alloc.formatClass = img_reqs.formatClass;
227 img_alloc.samples = img_reqs.samples;
Jon Ashburn7e781952015-01-16 09:37:43 -0700228
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600229 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburn7e781952015-01-16 09:37:43 -0700230 ASSERT_NE(0, mem_req[i].size) << "xglGetObjectInfo (Image): Failed - expect images to require memory";
Jon Ashburn7e781952015-01-16 09:37:43 -0700231 mem_info.allocationSize = mem_req[i].size;
Jon Ashburne4792432015-01-20 13:55:32 -0700232 mem_info.memProps = XGL_MEMORY_PROPERTY_SHAREABLE_BIT;
Jon Ashburne01ff7a2015-01-20 15:06:59 -0700233 mem_info.memType = XGL_MEMORY_TYPE_IMAGE;
Jon Ashburnf505c8d2015-01-20 16:38:02 -0700234 mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
Jon Ashburn7e781952015-01-16 09:37:43 -0700235
236 /* allocate memory */
237 err = xglAllocMemory(device(), &mem_info, &m_image_mem[i]);
238 ASSERT_XGL_SUCCESS(err);
239
240 /* bind memory */
241 err = xglBindObjectMemory(m_image, i, m_image_mem[i], 0);
242 ASSERT_XGL_SUCCESS(err);
243 }
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600244}
245
246void XglImageTest::DestroyImage()
247{
Jon Ashburn7e781952015-01-16 09:37:43 -0700248 XGL_RESULT err;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600249 // All done with image memory, clean up
Jon Ashburn9b6eae52015-01-15 10:39:19 -0700250 ASSERT_XGL_SUCCESS(xglBindObjectMemory(m_image, 0, XGL_NULL_HANDLE, 0));
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600251
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600252 for (uint32_t i = 0 ; i < m_num_mem; i++) {
Jon Ashburn7e781952015-01-16 09:37:43 -0700253 err = xglFreeMemory(m_image_mem[i]);
254 ASSERT_XGL_SUCCESS(err);
255 }
256
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600257
258 ASSERT_XGL_SUCCESS(xglDestroyObject(m_image));
259}
260
Courtney Goeltzenleuchter80ea59a2014-08-14 17:41:57 -0600261void XglImageTest::CreateImageView(XGL_IMAGE_VIEW_CREATE_INFO *pCreateInfo,
262 XGL_IMAGE_VIEW *pView)
263{
264 pCreateInfo->image = this->m_image;
Courtney Goeltzenleuchtercc5eb3a2014-08-19 18:35:50 -0600265 ASSERT_XGL_SUCCESS(xglCreateImageView(device(), pCreateInfo, pView));
Courtney Goeltzenleuchter80ea59a2014-08-14 17:41:57 -0600266}
267
268void XglImageTest::DestroyImageView(XGL_IMAGE_VIEW imageView)
269{
270 ASSERT_XGL_SUCCESS(xglDestroyObject(imageView));
271}
272
273TEST_F(XglImageTest, CreateImageViewTest) {
274 XGL_FORMAT fmt;
275 XGL_IMAGE_VIEW imageView;
Courtney Goeltzenleuchter80ea59a2014-08-14 17:41:57 -0600276
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700277 fmt = XGL_FMT_R8G8B8A8_UINT;
Courtney Goeltzenleuchter80ea59a2014-08-14 17:41:57 -0600278
279 CreateImage(512, 256);
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600280
281 // typedef struct _XGL_IMAGE_VIEW_CREATE_INFO
282 // {
283 // XGL_STRUCTURE_TYPE sType; // Must be XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600284 // const void* pNext; // Pointer to next structure
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600285 // XGL_IMAGE image;
286 // XGL_IMAGE_VIEW_TYPE viewType;
287 // XGL_FORMAT format;
288 // XGL_CHANNEL_MAPPING channels;
289 // XGL_IMAGE_SUBRESOURCE_RANGE subresourceRange;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600290 // float minLod;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600291 // } XGL_IMAGE_VIEW_CREATE_INFO;
292 XGL_IMAGE_VIEW_CREATE_INFO viewInfo = {};
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600293 viewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600294 viewInfo.viewType = XGL_IMAGE_VIEW_2D;
295 viewInfo.format = fmt;
296
297 viewInfo.channels.r = XGL_CHANNEL_SWIZZLE_R;
298 viewInfo.channels.g = XGL_CHANNEL_SWIZZLE_G;
299 viewInfo.channels.b = XGL_CHANNEL_SWIZZLE_B;
300 viewInfo.channels.a = XGL_CHANNEL_SWIZZLE_A;
301
302 viewInfo.subresourceRange.baseArraySlice = 0;
303 viewInfo.subresourceRange.arraySize = 1;
304 viewInfo.subresourceRange.baseMipLevel = 0;
305 viewInfo.subresourceRange.mipLevels = 1;
306 viewInfo.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
307
308 // XGL_RESULT XGLAPI xglCreateImageView(
309 // XGL_DEVICE device,
310 // const XGL_IMAGE_VIEW_CREATE_INFO* pCreateInfo,
311 // XGL_IMAGE_VIEW* pView);
312
Courtney Goeltzenleuchter80ea59a2014-08-14 17:41:57 -0600313 CreateImageView(&viewInfo, &imageView);
314
315 DestroyImageView(imageView);
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600316}
317
318int main(int argc, char **argv) {
319 ::testing::InitGoogleTest(&argc, argv);
Chia-I Wub76e0fa2014-12-28 14:27:28 +0800320 xgl_testing::set_error_callback(test_error_callback);
Courtney Goeltzenleuchter3470bdf2014-08-13 17:53:57 -0600321 return RUN_ALL_TESTS();
322}