blob: 81592672402d9680ca0dc79551fefe8ae90ad845 [file] [log] [blame]
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -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
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -060030
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
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -060054// Verify XGL driver initialization
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -060055
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -060056#include <stdlib.h>
57#include <stdio.h>
58#include <stdbool.h>
59#include <string.h>
60
61#include <xgl.h>
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -060062#include "gtest-1.7.0/include/gtest/gtest.h"
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -060063
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -060064// #include "gtest/gtest.h"
65#include "xglgpu.h"
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -060066
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -060067class XglTest : public ::testing::Test {
68public:
69 XglGpu *gpu;
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -060070
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -060071protected:
72 XGL_APPLICATION_INFO app_info;
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -060073 XGL_PHYSICAL_GPU objs[MAX_GPUS];
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -060074 XGL_UINT gpu_count;
75
76 virtual void SetUp() {
77 XGL_RESULT err;
78
79 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
80 this->app_info.pNext = NULL;
81 this->app_info.pAppName = (const XGL_CHAR *) "base";
82 this->app_info.appVersion = 1;
83 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
84 this->app_info.engineVersion = 1;
85 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
86
87 err = xglInitAndEnumerateGpus(&app_info, NULL,
88 MAX_GPUS, &this->gpu_count, objs);
89 ASSERT_XGL_SUCCESS(err);
90 ASSERT_GE(1, this->gpu_count) << "No GPU available";
91
92 this->gpu = new XglGpu(0, objs[0]);
93 }
94
95 virtual void TearDown() {
96 xglInitAndEnumerateGpus(&this->app_info, NULL, 0, &gpu_count, NULL);
97 }
98};
99
100TEST(Initialization, xglInitAndEnumerateGpus) {
101 XGL_APPLICATION_INFO app_info = {};
102 XGL_PHYSICAL_GPU objs[MAX_GPUS];
103 XGL_UINT gpu_count;
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -0600104 XGL_RESULT err;
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600105 XglGpu *gpu;
106
107 app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
108 app_info.pNext = NULL;
109 app_info.pAppName = (const XGL_CHAR *) "base";
110 app_info.appVersion = 1;
111 app_info.pEngineName = (const XGL_CHAR *) "unittest";
112 app_info.engineVersion = 1;
113 app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -0600114
115 err = xglInitAndEnumerateGpus(&app_info, NULL,
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600116 MAX_GPUS, &gpu_count, objs);
117 ASSERT_XGL_SUCCESS(err);
118 ASSERT_GE(1, gpu_count) << "No GPU available";
119
120 // TODO: Iterate over all GPUs
121 gpu = new XglGpu(0, objs[0]);
122
123 // TODO: Verify destroy functions
124}
125
126TEST_F(XglTest, AllocMemory) {
127 XGL_RESULT err;
128 XGL_MEMORY_ALLOC_INFO alloc_info = {};
129 XGL_GPU_MEMORY gpu_mem;
130 XGL_UINT8 *pData;
131
132 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
133 alloc_info.allocationSize = 1024 * 1024; // 1MB
134 alloc_info.alignment = 0;
135 alloc_info.heapCount = 1;
136 alloc_info.heaps[0] = 0; // TODO: Reference other heaps
137
138 // TODO: Pick heap properties indicated by heap info
139 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
140
141 // TODO: Try variety of memory priorities
142 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
143
144 err = xglAllocMemory(this->gpu->devObj, &alloc_info, &gpu_mem);
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600145 ASSERT_XGL_SUCCESS(err);
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -0600146
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600147 err = xglMapMemory(gpu_mem, 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600148 ASSERT_XGL_SUCCESS(err);
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600149
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600150 memset(pData, 0x55, alloc_info.allocationSize);
151 EXPECT_EQ(0x55, pData[0]) << "Memory read not same a write";
152
153 err = xglUnmapMemory(gpu_mem);
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600154 ASSERT_XGL_SUCCESS(err);
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600155
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600156 err = xglFreeMemory(gpu_mem);
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600157 ASSERT_XGL_SUCCESS(err);
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600158}
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600159
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600160TEST_F(XglTest, Event) {
161 XGL_EVENT_CREATE_INFO event_info;
162 XGL_EVENT event;
163 XGL_UINT data_size;
164 XGL_MEMORY_REQUIREMENTS mem_req;
165 XGL_RESULT err;
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600166
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600167 // typedef struct _XGL_EVENT_CREATE_INFO
168 // {
169 // XGL_STRUCTURE_TYPE sType; // Must be XGL_STRUCTURE_TYPE_EVENT_CREATE_INFO
170 // const XGL_VOID* pNext; // Pointer to next structure
171 // XGL_FLAGS flags; // Reserved
172 // } XGL_EVENT_CREATE_INFO;
173 memset(&event_info, 0, sizeof(event_info));
174 event_info.sType = XGL_STRUCTURE_TYPE_EVENT_CREATE_INFO;
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600175
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600176 err = xglCreateEvent(this->gpu->devObj, &event_info, &event);
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600177 ASSERT_XGL_SUCCESS(err);
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600178
179 err = xglGetObjectInfo(event, XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
180 &data_size, &mem_req);
181 ASSERT_XGL_SUCCESS(err);
182
183 // XGL_RESULT XGLAPI xglAllocMemory(
184 // XGL_DEVICE device,
185 // const XGL_MEMORY_ALLOC_INFO* pAllocInfo,
186 // XGL_GPU_MEMORY* pMem);
187 XGL_MEMORY_ALLOC_INFO mem_info;
188 XGL_GPU_MEMORY event_mem;
189
190 ASSERT_LE(1, mem_req.size) << "xglGetObjectInfo (Event): Failed - expect events to require memory";
191
192 memset(&mem_info, 0, sizeof(mem_info));
193 mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
194 mem_info.allocationSize = mem_req.size;
195 mem_info.alignment = mem_req.alignment;
196 mem_info.heapCount = mem_req.heapCount;
197 memcpy(mem_info.heaps, mem_req.heaps, sizeof(XGL_UINT)*XGL_MAX_MEMORY_HEAPS);
198 mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
199 mem_info.flags = XGL_MEMORY_ALLOC_SHAREABLE_BIT;
200 err = xglAllocMemory(this->gpu->devObj, &mem_info, &event_mem);
201 ASSERT_XGL_SUCCESS(err);
202
203 err = xglBindObjectMemory(event, event_mem, 0);
204 ASSERT_XGL_SUCCESS(err);
205
206 err = xglResetEvent(event);
207 ASSERT_XGL_SUCCESS(err);
208
209 err = xglGetEventStatus(event);
210 ASSERT_EQ(XGL_EVENT_RESET, err);
211
212 err = xglSetEvent(event);
213 ASSERT_XGL_SUCCESS(err);
214
215 err = xglGetEventStatus(event);
216 ASSERT_EQ(XGL_EVENT_SET, err);
217
218 // TODO: Test actual synchronization with command buffer event.
219
220 // All done with event memory, clean up
221 err = xglBindObjectMemory(event, XGL_NULL_HANDLE, 0);
222 ASSERT_XGL_SUCCESS(err);
223
224 err = xglDestroyObject(event);
225 ASSERT_XGL_SUCCESS(err);
226}
227
228TEST_F(XglTest, Fence) {
229 XGL_RESULT err;
230 XGL_FENCE_CREATE_INFO fence_info;
231 XGL_FENCE fence;
232
233 memset(&fence_info, 0, sizeof(fence_info));
234
235 // typedef struct _XGL_FENCE_CREATE_INFO
236 // {
237 // XGL_STRUCTURE_TYPE sType; // Must be XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO
238 // const XGL_VOID* pNext; // Pointer to next structure
239 // XGL_FLAGS flags; // Reserved
240 fence_info.sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO;
241
242 err = xglCreateFence(this->gpu->devObj, &fence_info, &fence);
243 ASSERT_XGL_SUCCESS(err);
244
245 err = xglGetFenceStatus(fence);
246 // We've not submitted this fence on a command buffer so should get
247 // XGL_ERROR_UNAVAILABLE
248 EXPECT_EQ(XGL_ERROR_UNAVAILABLE, err);
249
250 // Test glxWaitForFences
251 // XGL_RESULT XGLAPI xglWaitForFences(
252 // XGL_DEVICE device,
253 // XGL_UINT fenceCount,
254 // const XGL_FENCE* pFences,
255 // XGL_BOOL waitAll,
256 // XGL_UINT64 timeout);
257 err = xglWaitForFences(this->gpu->devObj, 1, &fence, XGL_TRUE, 0);
258 EXPECT_EQ(XGL_ERROR_UNAVAILABLE, err);
259
260 // TODO: Attached to command buffer and test GetFenceStatus
261 // TODO: Add some commands and submit the command buffer
262
263 err = xglDestroyObject(fence);
264 ASSERT_XGL_SUCCESS(err);
265
266}
267
268#define MAX_QUERY_SLOTS 10
269
270TEST_F(XglTest, Query) {
271 XGL_QUERY_POOL_CREATE_INFO query_info;
272 XGL_QUERY_POOL query_pool;
273 XGL_UINT data_size;
274 XGL_MEMORY_REQUIREMENTS mem_req;
275 XGL_UINT query_result_size;
276 XGL_UINT *query_result_data;
277 XGL_RESULT err;
278
279// typedef enum _XGL_QUERY_TYPE
280// {
281// XGL_QUERY_OCCLUSION = 0x00000000,
282// XGL_QUERY_PIPELINE_STATISTICS = 0x00000001,
283
284// XGL_QUERY_TYPE_BEGIN_RANGE = XGL_QUERY_OCCLUSION,
285// XGL_QUERY_TYPE_END_RANGE = XGL_QUERY_PIPELINE_STATISTICS,
286// XGL_NUM_QUERY_TYPE = (XGL_QUERY_TYPE_END_RANGE - XGL_QUERY_TYPE_BEGIN_RANGE + 1),
287// XGL_MAX_ENUM(_XGL_QUERY_TYPE)
288// } XGL_QUERY_TYPE;
289
290// typedef struct _XGL_QUERY_POOL_CREATE_INFO
291// {
292// XGL_STRUCTURE_TYPE sType; // Must be XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO
293// const XGL_VOID* pNext; // Pointer to next structure
294// XGL_QUERY_TYPE queryType;
295// XGL_UINT slots;
296// } XGL_QUERY_POOL_CREATE_INFO;
297
298 memset(&query_info, 0, sizeof(query_info));
299 query_info.sType = XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
300 query_info.queryType = XGL_QUERY_OCCLUSION;
301 query_info.slots = MAX_QUERY_SLOTS;
302
303// XGL_RESULT XGLAPI xglCreateQueryPool(
304// XGL_DEVICE device,
305// const XGL_QUERY_POOL_CREATE_INFO* pCreateInfo,
306// XGL_QUERY_POOL* pQueryPool);
307
308 err = xglCreateQueryPool(this->gpu->devObj, &query_info, &query_pool);
309 ASSERT_XGL_SUCCESS(err);
310
311 err = xglGetObjectInfo(query_pool, XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
312 &data_size, &mem_req);
313 ASSERT_XGL_SUCCESS(err);
314 ASSERT_LT(0, data_size) << "Invalid data_size";
315
316 // XGL_RESULT XGLAPI xglAllocMemory(
317 // XGL_DEVICE device,
318 // const XGL_MEMORY_ALLOC_INFO* pAllocInfo,
319 // XGL_GPU_MEMORY* pMem);
320 XGL_MEMORY_ALLOC_INFO mem_info;
321 XGL_GPU_MEMORY query_mem;
322
323 memset(&mem_info, 0, sizeof(mem_info));
324 mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
325 // TODO: Is a simple multiple all that's needed here?
326 mem_info.allocationSize = mem_req.size * MAX_QUERY_SLOTS;
327 mem_info.alignment = mem_req.alignment;
328 mem_info.heapCount = mem_req.heapCount;
329 memcpy(mem_info.heaps, mem_req.heaps, sizeof(XGL_UINT)*XGL_MAX_MEMORY_HEAPS);
330 mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
331
332 // TODO: are the flags right?
333 // TODO: Should this be pinned? Or maybe a separate test with pinned.
334 mem_info.flags = XGL_MEMORY_ALLOC_SHAREABLE_BIT;
335 err = xglAllocMemory(this->gpu->devObj, &mem_info, &query_mem);
336 ASSERT_XGL_SUCCESS(err);
337
338 err = xglBindObjectMemory(query_pool, query_mem, 0);
339 ASSERT_XGL_SUCCESS(err);
340
341 // TODO: Test actual synchronization with command buffer event.
342 // TODO: Create command buffer
343 // TODO: xglCmdResetQueryPool
344 // TODO: xglCmdBeginQuery
345 // TODO: commands
346 // TOOD: xglCmdEndQuery
347
348 err = xglGetQueryPoolResults(query_pool, 0, MAX_QUERY_SLOTS,
349 &query_result_size, XGL_NULL_HANDLE);
350 ASSERT_XGL_SUCCESS(err);
351
352 if (query_result_size > 0) {
353 query_result_data = new XGL_UINT [query_result_size];
354 err = xglGetQueryPoolResults(query_pool, 0, MAX_QUERY_SLOTS,
355 &query_result_size, query_result_data);
356 ASSERT_XGL_SUCCESS(err);
357
358 // TODO: Test Query result data.
359
360 }
361
362 // All done with QueryPool memory, clean up
363 err = xglBindObjectMemory(query_pool, XGL_NULL_HANDLE, 0);
364 ASSERT_XGL_SUCCESS(err);
365
366 err = xglDestroyObject(query_pool);
367 ASSERT_XGL_SUCCESS(err);
Courtney Goeltzenleuchter6b814b32014-08-12 09:59:36 -0600368
369}
370
371int main(int argc, char **argv) {
Courtney Goeltzenleuchtered56abc2014-08-12 14:12:22 -0600372 ::testing::InitGoogleTest(&argc, argv);
373 return RUN_ALL_TESTS();
Courtney Goeltzenleuchter7087dde2014-08-11 18:19:35 -0600374}