blob: c723d19c28272b49a9530344af2fed86a154d1cc [file] [log] [blame]
Courtney Goeltzenleuchter447ed582014-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 Goeltzenleuchtercc9da542014-08-12 14:12:22 -060030
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -060031//
Courtney Goeltzenleuchterfcbe16f2015-10-29 13:50:34 -060032// Copyright (C) 2015 Valve Corporation
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -060033//
34// Permission is hereby granted, free of charge, to any person obtaining a
35// copy of this software and associated documentation files (the "Software"),
36// to deal in the Software without restriction, including without limitation
37// the rights to use, copy, modify, merge, publish, distribute, sublicense,
38// and/or sell copies of the Software, and to permit persons to whom the
39// Software is furnished to do so, subject to the following conditions:
40//
41// The above copyright notice and this permission notice shall be included
42// in all copies or substantial portions of the Software.
43//
44// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
47// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50// DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060051//
52// Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
53// Author: Jon Ashburn <jon@lunarg.com>
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -060054
55
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060056// Verify VK driver initialization
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -060057
Courtney Goeltzenleuchter447ed582014-08-11 18:19:35 -060058#include <stdlib.h>
59#include <stdio.h>
60#include <stdbool.h>
61#include <string.h>
62
David Pinedo9316d3b2015-11-06 12:54:48 -070063#include <vulkan/vulkan.h>
Courtney Goeltzenleuchter447ed582014-08-11 18:19:35 -060064
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060065#include "vktestbinding.h"
Chia-I Wufe84fe12014-12-29 15:39:23 +080066#include "test_common.h"
Cody Northrop054a4702015-03-17 14:54:35 -060067#include "icd-spv.h"
Courtney Goeltzenleuchter447ed582014-08-11 18:19:35 -060068
Jon Ashburn83a64252015-04-15 11:31:12 -060069#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
70
Tony-LunarGd61e1e52015-05-15 12:42:49 -060071class VkTest : public ::testing::Test {
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -060072public:
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -060073 void CreateImageTest();
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -060074 void CreateCommandBufferTest();
75 void CreatePipelineTest();
76 void CreateShaderTest();
Chia-I Wu28e06912015-10-31 00:31:16 +080077 void CreateShader(VkShaderModule *pmodule, VkShaderStageFlagBits stage);
Courtney Goeltzenleuchter447ed582014-08-11 18:19:35 -060078
Chia-I Wuf368b602015-07-03 10:41:20 +080079 VkDevice device() {return m_device->handle();}
Courtney Goeltzenleuchtercc5eb3a2014-08-19 18:35:50 -060080
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -060081protected:
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060082 VkApplicationInfo app_info;
83 VkInstance inst;
Tony Barbourd1c35722015-04-16 15:59:00 -060084 VkPhysicalDevice objs[16];
Mark Lobodzinski17caf572015-01-29 08:55:56 -060085 uint32_t gpu_count;
Chia-I Wufe84fe12014-12-29 15:39:23 +080086
Mark Lobodzinski17caf572015-01-29 08:55:56 -060087 uint32_t m_device_id;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060088 vk_testing::Device *m_device;
Tony Barbourd1c35722015-04-16 15:59:00 -060089 VkPhysicalDeviceProperties props;
Cody Northropd0802882015-08-03 17:04:53 -060090 std::vector<VkQueueFamilyProperties> queue_props;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -070091 uint32_t graphics_queue_node_index;
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -060092
93 virtual void SetUp() {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060094 VkResult err;
Mark Young93ecb1d2016-01-13 13:47:16 -070095 size_t i;
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -060096
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060097 this->app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -060098 this->app_info.pNext = NULL;
Chia-I Wu3432a0c2015-10-27 18:04:07 +080099 this->app_info.pApplicationName = "base";
100 this->app_info.applicationVersion = 1;
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800101 this->app_info.pEngineName = "unittest";
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600102 this->app_info.engineVersion = 1;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600103 this->app_info.apiVersion = VK_API_VERSION;
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600104 VkInstanceCreateInfo inst_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600105 inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jon Ashburnb317fad2015-04-04 14:52:07 -0600106 inst_info.pNext = NULL;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800107 inst_info.pApplicationInfo = &app_info;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700108 inst_info.enabledLayerCount = 0;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600109 inst_info.ppEnabledLayerNames = NULL;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700110 inst_info.enabledExtensionCount = 0;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600111 inst_info.ppEnabledExtensionNames = NULL;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800112 err = vkCreateInstance(&inst_info, NULL, &inst);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600113 ASSERT_VK_SUCCESS(err);
Jon Ashburn83a64252015-04-15 11:31:12 -0600114 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, NULL);
115 ASSERT_VK_SUCCESS(err);
116 ASSERT_LE(this->gpu_count, ARRAY_SIZE(objs)) << "Too many GPUs";
117 err = vkEnumeratePhysicalDevices(inst, &this->gpu_count, objs);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600118 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -0600119 ASSERT_GE(this->gpu_count, (uint32_t) 1) << "No GPU available";
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600120
Chia-I Wufe84fe12014-12-29 15:39:23 +0800121 m_device_id = 0;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600122 this->m_device = new vk_testing::Device(objs[m_device_id]);
Chia-I Wufe84fe12014-12-29 15:39:23 +0800123 this->m_device->init();
124
Chia-I Wu999f0482015-07-03 10:32:05 +0800125 props = m_device->phy().properties();
Chia-I Wufe84fe12014-12-29 15:39:23 +0800126
Chia-I Wu999f0482015-07-03 10:32:05 +0800127 queue_props = this->m_device->phy().queue_properties();
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700128 for (i = 0; i < queue_props.size(); i++) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600129 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Mark Young93ecb1d2016-01-13 13:47:16 -0700130 graphics_queue_node_index = (uint32_t)i;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700131 break;
132 }
133 }
134 ASSERT_LT(i, queue_props.size()) << "Could not find a Queue with Graphics support";
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600135 }
136
137 virtual void TearDown() {
Tony Barbourc2f67f52015-06-02 14:55:46 -0600138 delete m_device;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800139 vkDestroyInstance(inst, NULL);
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600140 }
141};
142
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800143TEST_F(VkTest, AllocateMemory) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600144 VkResult err;
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600145 bool pass;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800146 VkMemoryAllocateInfo alloc_info = {};
Tony Barbourd1c35722015-04-16 15:59:00 -0600147 VkDeviceMemory gpu_mem;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600148 uint8_t *pData;
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600149
Chia-I Wu00ce5402015-11-10 16:21:09 +0800150 alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600151 alloc_info.allocationSize = 1024 * 1024; // 1MB
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -0600152 alloc_info.memoryTypeIndex = 0;
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600153
Tony Barbourcba4ab22015-07-29 14:27:38 -0600154 VkPhysicalDeviceMemoryProperties mem_props;
155 vkGetPhysicalDeviceMemoryProperties(m_device->phy().handle(), &mem_props);
156
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600157 pass = m_device->phy().set_memory_type(((1 << mem_props.memoryTypeCount) - 1), &alloc_info, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
158 ASSERT_TRUE(pass);
Tony Barbourcba4ab22015-07-29 14:27:38 -0600159
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800160 err = vkAllocateMemory(device(), &alloc_info, NULL, &gpu_mem);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600161 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter447ed582014-08-11 18:19:35 -0600162
Mark Lobodzinskif0670cc2016-01-05 15:30:41 -0700163 err = vkMapMemory(device(), gpu_mem, 0, VK_WHOLE_SIZE, 0, (void **) &pData);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600164 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter75011f82014-08-12 09:59:36 -0600165
Mark Young93ecb1d2016-01-13 13:47:16 -0700166 memset(pData, 0x55, (size_t)alloc_info.allocationSize);
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -0600167 EXPECT_EQ(0x55, pData[0]) << "Memory read not same as write";
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600168
Mark Lobodzinski2141f652015-09-07 13:59:43 -0600169 vkUnmapMemory(device(), gpu_mem);
Courtney Goeltzenleuchter75011f82014-08-12 09:59:36 -0600170
Chia-I Wuf7458c52015-10-26 21:10:41 +0800171 vkFreeMemory(device(), gpu_mem, NULL);
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600172}
Courtney Goeltzenleuchter75011f82014-08-12 09:59:36 -0600173
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600174TEST_F(VkTest, Event) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600175 VkEventCreateInfo event_info;
176 VkEvent event;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600177 VkResult err;
Courtney Goeltzenleuchter75011f82014-08-12 09:59:36 -0600178
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600179 // typedef struct VkEventCreateInfo_
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600180 // {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600181 // VkStructureType sType; // Must be VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600182 // const void* pNext; // Pointer to next structure
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600183 // VkFlags flags; // Reserved
184 // } VkEventCreateInfo;
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600185 memset(&event_info, 0, sizeof(event_info));
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600186 event_info.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
Courtney Goeltzenleuchter75011f82014-08-12 09:59:36 -0600187
Chia-I Wuf7458c52015-10-26 21:10:41 +0800188 err = vkCreateEvent(device(), &event_info, NULL, &event);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600189 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600190
Mike Stroyanb050c682015-04-17 12:36:38 -0600191 err = vkResetEvent(device(), event);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600192 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600193
Mike Stroyanb050c682015-04-17 12:36:38 -0600194 err = vkGetEventStatus(device(), event);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600195 ASSERT_EQ(VK_EVENT_RESET, err);
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600196
Mike Stroyanb050c682015-04-17 12:36:38 -0600197 err = vkSetEvent(device(), event);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600198 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600199
Mike Stroyanb050c682015-04-17 12:36:38 -0600200 err = vkGetEventStatus(device(), event);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600201 ASSERT_EQ(VK_EVENT_SET, err);
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600202
203 // TODO: Test actual synchronization with command buffer event.
204
205 // All done with event memory, clean up
Chia-I Wuf7458c52015-10-26 21:10:41 +0800206 vkDestroyEvent(device(), event, NULL);
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600207}
208
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600209void getQueue(vk_testing::Device *device, uint32_t queue_node_index, const char *qname)
Courtney Goeltzenleuchter3dc07f22014-08-14 10:15:09 -0600210{
Cody Northropd2ad0342015-08-05 11:15:02 -0600211 uint32_t que_idx;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600212 VkQueue queue;
Courtney Goeltzenleuchter3dc07f22014-08-14 10:15:09 -0600213
Cody Northropd0802882015-08-03 17:04:53 -0600214 const VkQueueFamilyProperties props = device->phy().queue_properties()[queue_node_index];
Chia-I Wufe84fe12014-12-29 15:39:23 +0800215 for (que_idx = 0; que_idx < props.queueCount; que_idx++) {
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500216 // TODO: Need to add support for separate MEMMGR and work queues, including synchronization
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600217 vkGetDeviceQueue(device->handle(), queue_node_index, que_idx, &queue);
Courtney Goeltzenleuchter3dc07f22014-08-14 10:15:09 -0600218 }
219}
220
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600221void VkTest::CreateImageTest()
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600222{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600223 VkResult err;
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600224 bool pass;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600225 VkImage image;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600226 uint32_t w, h, mipCount;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600227 VkFormat fmt;
228 VkFormatProperties image_fmt;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600229
230 w =512;
231 h = 256;
232 mipCount = 0;
233
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600234 uint32_t _w = w;
235 uint32_t _h = h;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600236 while( ( _w > 0 ) || ( _h > 0 ) )
237 {
238 _w >>= 1;
239 _h >>= 1;
240 mipCount++;
241 }
242
Tony Barbourd1c35722015-04-16 15:59:00 -0600243 fmt = VK_FORMAT_R8G8B8A8_UINT;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600244 // TODO: Pick known good format rather than just expect common format
245 /*
246 * XXX: What should happen if given NULL HANDLE for the pData argument?
Tony Barbourd1c35722015-04-16 15:59:00 -0600247 * We're not requesting VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS so there is
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600248 * an expectation that pData is a valid pointer.
249 * However, why include a returned size value? That implies that the
250 * amount of data may vary and that doesn't work well for using a
251 * fixed structure.
252 */
253
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600254 vkGetPhysicalDeviceFormatProperties(objs[m_device_id], fmt, &image_fmt);
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600255
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600256// typedef struct VkImageCreateInfo_
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600257// {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600258// VkStructureType sType; // Must be VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600259// const void* pNext; // Pointer to next structure.
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600260// VkImageType imageType;
261// VkFormat format;
262// VkExtent3D extent;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600263// uint32_t mipLevels;
264// uint32_t arraySize;
Chia-I Wu5c17c962015-10-31 00:31:16 +0800265// VkSampleCountFlagBits samples;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600266// VkImageTiling tiling;
267// VkFlags usage; // VkImageUsageFlags
268// VkFlags flags; // VkImageCreateFlags
269// } VkImageCreateInfo;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600270
271
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600272 VkImageCreateInfo imageCreateInfo = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600273 imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Tony Barbourd1c35722015-04-16 15:59:00 -0600274 imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600275 imageCreateInfo.format = fmt;
Courtney Goeltzenleuchter5d2aed42015-10-21 17:57:31 -0600276 imageCreateInfo.arrayLayers = 1;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600277 imageCreateInfo.extent.width = w;
278 imageCreateInfo.extent.height = h;
279 imageCreateInfo.extent.depth = 1;
280 imageCreateInfo.mipLevels = mipCount;
Chia-I Wu5c17c962015-10-31 00:31:16 +0800281 imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600282 if (image_fmt.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
283 imageCreateInfo.tiling = VK_IMAGE_TILING_LINEAR;
284 }
285 else if (image_fmt.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
286 imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
287 }
288 else {
289 FAIL() << "Neither Linear nor Optimal allowed for color attachment";
290 }
Cody Northropbb6fdb32015-06-17 08:28:19 -0600291 imageCreateInfo.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
292 VK_IMAGE_USAGE_SAMPLED_BIT;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600293
Chia-I Wu9ab61502015-11-06 06:42:02 +0800294// VKAPI_ATTR VkResult VKAPI_CALL vkCreateImage(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600295// VkDevice device,
296// const VkImageCreateInfo* pCreateInfo,
297// VkImage* pImage);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800298 err = vkCreateImage(device(), &imageCreateInfo, NULL, &image);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600299 ASSERT_VK_SUCCESS(err);
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600300
Courtney Goeltzenleuchter7bf0ff82014-08-14 17:40:57 -0600301 // Verify image resources
Chia-I Wu9ab61502015-11-06 06:42:02 +0800302// VKAPI_ATTR void VKAPI_CALL vkGetImageSubresourceLayout(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600303// VkImage image,
304// const VkImageSubresource* pSubresource,
Tony Barbour59a47322015-06-24 16:06:58 -0600305// VkSubresourceLayout* pLayout);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600306// typedef struct VkSubresourceLayout_
Courtney Goeltzenleuchter7bf0ff82014-08-14 17:40:57 -0600307// {
Tony Barbourd1c35722015-04-16 15:59:00 -0600308// VkDeviceSize offset; // Specified in bytes
309// VkDeviceSize size; // Specified in bytes
310// VkDeviceSize rowPitch; // Specified in bytes
311// VkDeviceSize depthPitch; // Specified in bytes
Jon Ashburn2b2f90c2015-12-30 14:39:53 -0700312// VkDeviceSize arrayPitch; // Specified in bytes
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600313// } VkSubresourceLayout;
Courtney Goeltzenleuchter7bf0ff82014-08-14 17:40:57 -0600314
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600315// typedef struct VkImageSubresource_
Courtney Goeltzenleuchter7bf0ff82014-08-14 17:40:57 -0600316// {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600317// VkImageAspect aspect;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600318// uint32_t mipLevel;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600319// uint32_t arrayLayer;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600320// } VkImageSubresource;
Courtney Goeltzenleuchter7bf0ff82014-08-14 17:40:57 -0600321
Courtney Goeltzenleuchter7bf0ff82014-08-14 17:40:57 -0600322
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600323 if (image_fmt.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
324 VkImageSubresource subresource = {};
Chia-I Wu52b07e72015-10-27 19:55:05 +0800325 subresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600326 subresource.arrayLayer = 0;
Courtney Goeltzenleuchter7bf0ff82014-08-14 17:40:57 -0600327
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600328 _w = w;
329 _h = h;
330 while ((_w > 0) || (_h > 0))
331 {
332 VkSubresourceLayout layout = {};
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600333 vkGetImageSubresourceLayout(device(), image, &subresource, &layout);
Courtney Goeltzenleuchter7bf0ff82014-08-14 17:40:57 -0600334
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600335 // TODO: 4 should be replaced with pixel size for given format
336 EXPECT_LE(_w * 4, layout.rowPitch) << "Pitch does not match expected image pitch";
337 _w >>= 1;
338 _h >>= 1;
339
340 subresource.mipLevel++;
341 }
Courtney Goeltzenleuchter7bf0ff82014-08-14 17:40:57 -0600342 }
343
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600344 VkMemoryRequirements mem_req;
Tony Barbourd1c35722015-04-16 15:59:00 -0600345 VkDeviceMemory image_mem;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600346
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600347 vkGetImageMemoryRequirements(device(), image, &mem_req);
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600348
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600349 if (mem_req.size) {
350
Chia-I Wu9ab61502015-11-06 06:42:02 +0800351 // VKAPI_ATTR VkResult VKAPI_CALL vkAllocateMemory(
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600352 // VkDevice device,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800353 // const VkMemoryAllocateInfo* pAllocateInfo,
354 // VkDeviceMemory* pMemory);
355 VkMemoryAllocateInfo mem_info = {};
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600356
Chia-I Wu00ce5402015-11-10 16:21:09 +0800357 mem_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600358 mem_info.pNext = NULL;
359 mem_info.allocationSize = mem_req.size;
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -0600360 mem_info.memoryTypeIndex = 0;
361
Courtney Goeltzenleuchter1d2f0dd2015-10-22 11:03:31 -0600362 pass = m_device->phy().set_memory_type(mem_req.memoryTypeBits, &mem_info, 0);
363 ASSERT_TRUE(pass);
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -0600364
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800365 err = vkAllocateMemory(device(), &mem_info, NULL, &image_mem);
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600366 ASSERT_VK_SUCCESS(err);
367
Tony Barbour67e99152015-07-10 14:10:27 -0600368 err = vkBindImageMemory(device(), image, image_mem, 0);
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600369 ASSERT_VK_SUCCESS(err);
370 }
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600371
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600372// typedef struct VkImageViewCreateInfo_
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600373// {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600374// VkStructureType sType; // Must be VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600375// const void* pNext; // Pointer to next structure
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600376// VkImage image;
377// VkImageViewType viewType;
378// VkFormat format;
Chia-I Wu1b99bb22015-10-27 19:25:11 +0800379// VkComponentMapping channels;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600380// VkImageSubresourceRange subresourceRange;
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600381// float minLod;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600382// } VkImageViewCreateInfo;
383 VkImageViewCreateInfo viewInfo = {};
384 VkImageView view;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600385 viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600386 viewInfo.image = image;
Tony Barbourd1c35722015-04-16 15:59:00 -0600387 viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600388 viewInfo.format = fmt;
389
Chia-I Wuab83a0e2015-10-27 19:00:15 +0800390 viewInfo.components.r = VK_COMPONENT_SWIZZLE_R;
391 viewInfo.components.g = VK_COMPONENT_SWIZZLE_G;
392 viewInfo.components.b = VK_COMPONENT_SWIZZLE_B;
393 viewInfo.components.a = VK_COMPONENT_SWIZZLE_A;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600394
Courtney Goeltzenleuchter4a261892015-09-10 16:38:41 -0600395 viewInfo.subresourceRange.baseArrayLayer = 0;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800396 viewInfo.subresourceRange.layerCount = 1;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600397 viewInfo.subresourceRange.baseMipLevel = 0;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800398 viewInfo.subresourceRange.levelCount = 1;
Courtney Goeltzenleuchterba724512015-09-10 17:58:54 -0600399 viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600400
Chia-I Wu9ab61502015-11-06 06:42:02 +0800401// VKAPI_ATTR VkResult VKAPI_CALL vkCreateImageView(
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600402// VkDevice device,
403// const VkImageViewCreateInfo* pCreateInfo,
404// VkImageView* pView);
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600405
Chia-I Wuf7458c52015-10-26 21:10:41 +0800406 err = vkCreateImageView(device(), &viewInfo, NULL, &view);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600407 ASSERT_VK_SUCCESS(err) << "vkCreateImageView failed";
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600408
409 // TODO: Test image memory.
410
411 // All done with image memory, clean up
Chia-I Wuf7458c52015-10-26 21:10:41 +0800412 vkDestroyImageView(device(), view, NULL);
413 vkDestroyImage(device(), image, NULL);
Courtney Goeltzenleuchter10642562015-06-22 16:26:54 -0600414
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600415 if (mem_req.size) {
Chia-I Wuf7458c52015-10-26 21:10:41 +0800416 vkFreeMemory(device(), image_mem, NULL);
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600417 }
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600418}
419
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600420TEST_F(VkTest, CreateImage) {
Courtney Goeltzenleuchter794555b2014-08-13 17:55:12 -0600421 CreateImageTest();
Courtney Goeltzenleuchter75011f82014-08-12 09:59:36 -0600422}
423
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600424void VkTest::CreateCommandBufferTest()
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -0600425{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600426 VkResult err;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800427 VkCommandBufferAllocateInfo info = {};
428 VkCommandPool commandPool;
429 VkCommandBuffer commandBuffer;
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -0600430
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800431// typedef struct VkCommandBufferCreateInfo_
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -0600432// {
Chia-I Wu00ce5402015-11-10 16:21:09 +0800433// VkStructureType sType; // Must be VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600434// const void* pNext;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600435// VK_QUEUE_TYPE queueType;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600436// VkFlags flags;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800437// } VkCommandBufferAllocateInfo;
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -0600438
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800439 VkCommandPoolCreateInfo cmd_pool_info;
440 cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrope62183e2015-07-09 18:08:05 -0600441 cmd_pool_info.pNext = NULL,
442 cmd_pool_info.queueFamilyIndex = graphics_queue_node_index;
443 cmd_pool_info.flags = 0,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800444 err = vkCreateCommandPool(device(), &cmd_pool_info, NULL, &commandPool);
Cody Northrope62183e2015-07-09 18:08:05 -0600445 ASSERT_VK_SUCCESS(err) << "vkCreateCommandPool failed";
446
Chia-I Wu00ce5402015-11-10 16:21:09 +0800447 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800448 info.commandPool = commandPool;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700449 info.commandBufferCount = 1;
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800450 info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
451 err = vkAllocateCommandBuffers(device(), &info, &commandBuffer);
452 ASSERT_VK_SUCCESS(err) << "vkAllocateCommandBuffers failed";
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -0600453
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800454 vkFreeCommandBuffers(device(), commandPool, 1, &commandBuffer);
455 vkDestroyCommandPool(device(), commandPool, NULL);
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -0600456}
457
Tony-LunarGd61e1e52015-05-15 12:42:49 -0600458TEST_F(VkTest, TestCommandBuffer) {
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -0600459 CreateCommandBufferTest();
460}
461
Chia-I Wu28e06912015-10-31 00:31:16 +0800462void VkTest::CreateShader(VkShaderModule *pmodule, VkShaderStageFlagBits stage)
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -0600463{
Chia-I Wu8094f192015-10-26 19:22:06 +0800464 uint32_t *code;
Courtney Goeltzenleuchter6863ff42014-08-19 13:34:28 -0600465 uint32_t codeSize;
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600466 struct icd_spv_header *pSPV;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600467 VkResult err;
Courtney Goeltzenleuchter6863ff42014-08-19 13:34:28 -0600468
Chia-I Wu8094f192015-10-26 19:22:06 +0800469 codeSize = sizeof(struct icd_spv_header) + sizeof(uint32_t) * 25;
470 code = (uint32_t *) malloc(codeSize);
Courtney Goeltzenleuchter6863ff42014-08-19 13:34:28 -0600471 ASSERT_TRUE(NULL != code) << "malloc failed!";
472
473 memset(code, 0, codeSize);
474
Cody Northrop3bfd27c2015-03-17 15:55:58 -0600475 // Indicate that this is SPV data.
476 pSPV = (struct icd_spv_header *) code;
477 pSPV->magic = ICD_SPV_MAGIC;
478 pSPV->version = ICD_SPV_VERSION;
Courtney Goeltzenleuchter6863ff42014-08-19 13:34:28 -0600479
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600480 VkShaderModuleCreateInfo moduleCreateInfo;
481 VkShaderModule module;
Courtney Goeltzenleuchter6863ff42014-08-19 13:34:28 -0600482
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600483 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
484 moduleCreateInfo.pNext = NULL;
485 moduleCreateInfo.pCode = code;
486 moduleCreateInfo.codeSize = codeSize;
487 moduleCreateInfo.flags = 0;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800488 err = vkCreateShaderModule(device(), &moduleCreateInfo, NULL, &module);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600489 ASSERT_VK_SUCCESS(err);
490
Chia-I Wu28e06912015-10-31 00:31:16 +0800491 *pmodule = module;
Courtney Goeltzenleuchterde3513a2014-08-20 15:26:55 -0600492}
493
Courtney Goeltzenleuchter75011f82014-08-12 09:59:36 -0600494int main(int argc, char **argv) {
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600495 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600496 vk_testing::set_error_callback(test_error_callback);
Courtney Goeltzenleuchtercc9da542014-08-12 14:12:22 -0600497 return RUN_ALL_TESTS();
Courtney Goeltzenleuchter447ed582014-08-11 18:19:35 -0600498}