OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrVkGpu.h" | 8 #include "GrVkGpu.h" |
9 | 9 |
10 #include "GrContextOptions.h" | 10 #include "GrContextOptions.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 #include "vk/GrVkInterface.h" | 36 #include "vk/GrVkInterface.h" |
37 | 37 |
38 #define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X) | 38 #define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X) |
39 #define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X) | 39 #define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X) |
40 #define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X) | 40 #define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X) |
41 | 41 |
42 //////////////////////////////////////////////////////////////////////////////// | 42 //////////////////////////////////////////////////////////////////////////////// |
43 // Stuff used to set up a GrVkGpu secrectly for now. | 43 // Stuff used to set up a GrVkGpu secrectly for now. |
44 | 44 |
| 45 |
| 46 #ifdef ENABLE_VK_LAYERS |
| 47 VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback( |
| 48 VkDebugReportFlagsEXT flags, |
| 49 VkDebugReportObjectTypeEXT objectType, |
| 50 uint64_t object, |
| 51 size_t location, |
| 52 int32_t messageCode, |
| 53 const char* pLayerPrefix, |
| 54 const char* pMessage, |
| 55 void* pUserData) { |
| 56 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { |
| 57 SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode,
pMessage); |
| 58 } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) { |
| 59 SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCod
e, pMessage); |
| 60 } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) { |
| 61 SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messa
geCode, pMessage); |
| 62 } else { |
| 63 SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, message
Code, pMessage); |
| 64 } |
| 65 return VK_FALSE; |
| 66 } |
| 67 |
| 68 const char* kEnabledLayerNames[] = { |
| 69 // elements of VK_LAYER_LUNARG_standard_validation |
| 70 "VK_LAYER_LUNARG_threading", |
| 71 "VK_LAYER_LUNARG_param_checker", |
| 72 "VK_LAYER_LUNARG_device_limits", |
| 73 "VK_LAYER_LUNARG_object_tracker", |
| 74 "VK_LAYER_LUNARG_image", |
| 75 "VK_LAYER_LUNARG_mem_tracker", |
| 76 "VK_LAYER_LUNARG_draw_state", |
| 77 "VK_LAYER_LUNARG_swapchain", |
| 78 "VK_LAYER_GOOGLE_unique_objects", |
| 79 // not included in standard_validation |
| 80 //"VK_LAYER_LUNARG_api_dump", |
| 81 }; |
| 82 const char* kEnabledInstanceExtensionNames[] = { |
| 83 VK_EXT_DEBUG_REPORT_EXTENSION_NAME |
| 84 }; |
| 85 |
| 86 bool verify_instance_layers() { |
| 87 // make sure we can actually use the extensions and layers above |
| 88 uint32_t extensionCount; |
| 89 VkResult res = vkEnumerateInstanceExtensionProperties(nullptr, &extensionCou
nt, nullptr); |
| 90 if (VK_SUCCESS != res) { |
| 91 return false; |
| 92 } |
| 93 VkExtensionProperties* extensions = new VkExtensionProperties[extensionCount
]; |
| 94 res = vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, exten
sions); |
| 95 if (VK_SUCCESS != res) { |
| 96 return false; |
| 97 } |
| 98 int instanceExtensionsFound = 0; |
| 99 for (uint32_t j = 0; j < ARRAYSIZE(kEnabledInstanceExtensionNames); ++j) { |
| 100 for (uint32_t i = 0; i < extensionCount; ++i) { |
| 101 if (!strncmp(extensions[i].extensionName, kEnabledInstanceExtensionN
ames[j], |
| 102 strlen(kEnabledInstanceExtensionNames[j]))) { |
| 103 ++instanceExtensionsFound; |
| 104 break; |
| 105 } |
| 106 } |
| 107 } |
| 108 delete[] extensions; |
| 109 |
| 110 uint32_t layerCount; |
| 111 res = vkEnumerateInstanceLayerProperties(&layerCount, nullptr); |
| 112 if (VK_SUCCESS != res) { |
| 113 return false; |
| 114 } |
| 115 VkLayerProperties* layers = new VkLayerProperties[layerCount]; |
| 116 res = vkEnumerateInstanceLayerProperties(&layerCount, layers); |
| 117 if (VK_SUCCESS != res) { |
| 118 return false; |
| 119 } |
| 120 int instanceLayersFound = 0; |
| 121 for (uint32_t j = 0; j < ARRAYSIZE(kEnabledLayerNames); ++j) { |
| 122 for (uint32_t i = 0; i < layerCount; ++i) { |
| 123 if (!strncmp(layers[i].layerName, kEnabledLayerNames[j], |
| 124 strlen(kEnabledLayerNames[j]))) { |
| 125 ++instanceLayersFound; |
| 126 break; |
| 127 } |
| 128 } |
| 129 } |
| 130 delete[] layers; |
| 131 |
| 132 return instanceExtensionsFound == ARRAYSIZE(kEnabledInstanceExtensionNames)
&& |
| 133 instanceLayersFound == ARRAYSIZE(kEnabledLayerNames); |
| 134 } |
| 135 |
| 136 bool verify_device_layers(VkPhysicalDevice physDev) { |
| 137 uint32_t layerCount; |
| 138 VkResult res = vkEnumerateDeviceLayerProperties(physDev, &layerCount, nullpt
r); |
| 139 if (VK_SUCCESS != res) { |
| 140 return false; |
| 141 } |
| 142 VkLayerProperties* layers = new VkLayerProperties[layerCount]; |
| 143 res = vkEnumerateDeviceLayerProperties(physDev, &layerCount, layers); |
| 144 if (VK_SUCCESS != res) { |
| 145 return false; |
| 146 } |
| 147 int deviceLayersFound = 0; |
| 148 for (uint32_t j = 0; j < ARRAYSIZE(kEnabledLayerNames); ++j) { |
| 149 for (uint32_t i = 0; i < layerCount; ++i) { |
| 150 if (!strncmp(layers[i].layerName, kEnabledLayerNames[j], |
| 151 strlen(kEnabledLayerNames[j]))) { |
| 152 ++deviceLayersFound; |
| 153 break; |
| 154 } |
| 155 } |
| 156 } |
| 157 delete[] layers; |
| 158 |
| 159 return deviceLayersFound == ARRAYSIZE(kEnabledLayerNames); |
| 160 } |
| 161 #endif |
| 162 |
45 // For now the VkGpuCreate is using the same signature as GL. This is mostly for
ease of | 163 // For now the VkGpuCreate is using the same signature as GL. This is mostly for
ease of |
46 // hiding this code from offical skia. In the end the VkGpuCreate will not take
a GrBackendContext | 164 // hiding this code from offical skia. In the end the VkGpuCreate will not take
a GrBackendContext |
47 // and mostly likely would take an optional device and queues to use. | 165 // and mostly likely would take an optional device and queues to use. |
48 GrGpu* vk_gpu_create(GrBackendContext backendContext, const GrContextOptions& op
tions, | 166 GrGpu* vk_gpu_create(GrBackendContext backendContext, const GrContextOptions& op
tions, |
49 GrContext* context) { | 167 GrContext* context) { |
50 // Below is Vulkan setup code that normal would be done by a client, but wil
l do here for now | 168 // Below is Vulkan setup code that normal would be done by a client, but wil
l do here for now |
51 // for testing purposes. | 169 // for testing purposes. |
52 VkPhysicalDevice physDev; | 170 VkPhysicalDevice physDev; |
53 VkDevice device; | 171 VkDevice device; |
54 VkInstance inst; | 172 VkInstance inst; |
55 VkResult err; | 173 VkResult err; |
56 | 174 |
57 const VkApplicationInfo app_info = { | 175 const VkApplicationInfo app_info = { |
58 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType | 176 VK_STRUCTURE_TYPE_APPLICATION_INFO, // sType |
59 nullptr, // pNext | 177 nullptr, // pNext |
60 "vktest", // pApplicationName | 178 "vktest", // pApplicationName |
61 0, // applicationVersion | 179 0, // applicationVersion |
62 "vktest", // pEngineName | 180 "vktest", // pEngineName |
63 0, // engineVerison | 181 0, // engineVerison |
64 VK_API_VERSION, // apiVersion | 182 kGrVkMinimumVersion, // apiVersion |
65 }; | 183 }; |
| 184 |
| 185 const char** enabledLayerNames = nullptr; |
| 186 int enabledLayerCount = 0; |
| 187 const char** enabledInstanceExtensionNames = nullptr; |
| 188 int enabledInstanceExtensionCount = 0; |
| 189 #ifdef ENABLE_VK_LAYERS |
| 190 if (verify_instance_layers()) { |
| 191 enabledLayerNames = kEnabledLayerNames; |
| 192 enabledLayerCount = ARRAYSIZE(kEnabledLayerNames); |
| 193 enabledInstanceExtensionNames = kEnabledInstanceExtensionNames; |
| 194 enabledInstanceExtensionCount = ARRAYSIZE(kEnabledInstanceExtensionNames
); |
| 195 } |
| 196 #endif |
| 197 |
66 const VkInstanceCreateInfo instance_create = { | 198 const VkInstanceCreateInfo instance_create = { |
67 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType | 199 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType |
68 nullptr, // pNext | 200 nullptr, // pNext |
69 0, // flags | 201 0, // flags |
70 &app_info, // pApplicationInfo | 202 &app_info, // pApplicationInfo |
71 0, // enabledLayerNameCount | 203 enabledLayerCount, // enabledLayerNameCount |
72 nullptr, // ppEnabledLayerNames | 204 enabledLayerNames, // ppEnabledLayerNames |
73 0, // enabledExtensionNameCount | 205 enabledInstanceExtensionCount, // enabledExtensionNameCount |
74 nullptr, // ppEnabledExtensionNames | 206 enabledInstanceExtensionNames, // ppEnabledExtensionNames |
75 }; | 207 }; |
| 208 |
76 err = vkCreateInstance(&instance_create, nullptr, &inst); | 209 err = vkCreateInstance(&instance_create, nullptr, &inst); |
77 if (err < 0) { | 210 if (err < 0) { |
78 SkDebugf("vkCreateInstanced failed: %d\n", err); | 211 SkDebugf("vkCreateInstanced failed: %d\n", err); |
79 SkFAIL("failing"); | 212 SkFAIL("failing"); |
80 } | 213 } |
81 | 214 |
82 uint32_t gpuCount; | 215 uint32_t gpuCount; |
83 err = vkEnumeratePhysicalDevices(inst, &gpuCount, nullptr); | 216 err = vkEnumeratePhysicalDevices(inst, &gpuCount, nullptr); |
84 if (err) { | 217 if (err) { |
85 SkDebugf("vkEnumeratePhysicalDevices failed: %d\n", err); | 218 SkDebugf("vkEnumeratePhysicalDevices failed: %d\n", err); |
(...skipping 22 matching lines...) Expand all Loading... |
108 // iterate to find the graphics queue | 241 // iterate to find the graphics queue |
109 uint32_t graphicsQueueIndex = -1; | 242 uint32_t graphicsQueueIndex = -1; |
110 for (uint32_t i = 0; i < queueCount; i++) { | 243 for (uint32_t i = 0; i < queueCount; i++) { |
111 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { | 244 if (queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
112 graphicsQueueIndex = i; | 245 graphicsQueueIndex = i; |
113 break; | 246 break; |
114 } | 247 } |
115 } | 248 } |
116 SkASSERT(graphicsQueueIndex < queueCount); | 249 SkASSERT(graphicsQueueIndex < queueCount); |
117 | 250 |
| 251 #ifdef ENABLE_VK_LAYERS |
| 252 // unlikely that the device will have different layers than the instance, bu
t good to check |
| 253 if (!verify_device_layers(physDev)) { |
| 254 enabledLayerNames = nullptr; |
| 255 enabledLayerCount = 0; |
| 256 } |
| 257 #endif |
| 258 |
118 float queuePriorities[1] = { 0.0 }; | 259 float queuePriorities[1] = { 0.0 }; |
119 const VkDeviceQueueCreateInfo queueInfo = { | 260 const VkDeviceQueueCreateInfo queueInfo = { |
120 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType | 261 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType |
121 nullptr, // pNext | 262 nullptr, // pNext |
122 0, // VkDeviceQueueCreateFlags | 263 0, // VkDeviceQueueCreateFlags |
123 0, // queueFamilyIndex | 264 0, // queueFamilyIndex |
124 1, // queueCount | 265 1, // queueCount |
125 queuePriorities, // pQueuePriorities | 266 queuePriorities, // pQueuePriorities |
126 }; | 267 }; |
127 const VkDeviceCreateInfo deviceInfo = { | 268 const VkDeviceCreateInfo deviceInfo = { |
128 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType | 269 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType |
129 nullptr, // pNext | 270 nullptr, // pNext |
130 0, // VkDeviceCreateFlags | 271 0, // VkDeviceCreateFlags |
131 1, // queueCreateInfoCount | 272 1, // queueCreateInfoCount |
132 &queueInfo, // pQueueCreateInfos | 273 &queueInfo, // pQueueCreateInfos |
133 0, // layerCount | 274 enabledLayerCount, // layerCount |
134 nullptr, // ppEnabledLayerNames | 275 enabledLayerNames, // ppEnabledLayerNames |
135 0, // extensionCount | 276 0, // extensionCount |
136 nullptr, // ppEnabledExtensionNames | 277 nullptr, // ppEnabledExtensionNames |
137 nullptr // ppEnabledFeatures | 278 nullptr // ppEnabledFeatures |
138 }; | 279 }; |
139 | 280 |
140 err = vkCreateDevice(physDev, &deviceInfo, nullptr, &device); | 281 err = vkCreateDevice(physDev, &deviceInfo, nullptr, &device); |
141 if (err) { | 282 if (err) { |
142 SkDebugf("CreateDevice failed: %d\n", err); | 283 SkDebugf("CreateDevice failed: %d\n", err); |
143 SkFAIL("failing"); | 284 SkFAIL("failing"); |
144 } | 285 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 fVkCaps.reset(new GrVkCaps(options, fInterface, physDev)); | 321 fVkCaps.reset(new GrVkCaps(options, fInterface, physDev)); |
181 fCaps.reset(SkRef(fVkCaps.get())); | 322 fCaps.reset(SkRef(fVkCaps.get())); |
182 | 323 |
183 fResourceProvider.init(); | 324 fResourceProvider.init(); |
184 | 325 |
185 fCurrentCmdBuffer = fResourceProvider.createCommandBuffer(); | 326 fCurrentCmdBuffer = fResourceProvider.createCommandBuffer(); |
186 SkASSERT(fCurrentCmdBuffer); | 327 SkASSERT(fCurrentCmdBuffer); |
187 fCurrentCmdBuffer->begin(this); | 328 fCurrentCmdBuffer->begin(this); |
188 VK_CALL(GetPhysicalDeviceMemoryProperties(physDev, &fPhysDevMemProps)); | 329 VK_CALL(GetPhysicalDeviceMemoryProperties(physDev, &fPhysDevMemProps)); |
189 | 330 |
| 331 #ifdef ENABLE_VK_LAYERS |
| 332 if (fInterface->hasInstanceExtension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME)) { |
| 333 /* Setup callback creation information */ |
| 334 VkDebugReportCallbackCreateInfoEXT callbackCreateInfo; |
| 335 callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EX
T; |
| 336 callbackCreateInfo.pNext = nullptr; |
| 337 callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | |
| 338 VK_DEBUG_REPORT_WARNING_BIT_EXT | |
| 339 //VK_DEBUG_REPORT_INFORMATION_BIT_EXT | |
| 340 //VK_DEBUG_REPORT_DEBUG_BIT_EXT | |
| 341 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT; |
| 342 callbackCreateInfo.pfnCallback = &DebugReportCallback; |
| 343 callbackCreateInfo.pUserData = nullptr; |
| 344 |
| 345 /* Register the callback */ |
| 346 GR_VK_CALL_ERRCHECK(fInterface, CreateDebugReportCallbackEXT(inst, &call
backCreateInfo, |
| 347 nullptr, &f
Callback)); |
| 348 } |
| 349 #endif |
190 } | 350 } |
191 | 351 |
192 GrVkGpu::~GrVkGpu() { | 352 GrVkGpu::~GrVkGpu() { |
193 shaderc_compiler_release(fCompiler); | 353 shaderc_compiler_release(fCompiler); |
194 fCurrentCmdBuffer->end(this); | 354 fCurrentCmdBuffer->end(this); |
195 fCurrentCmdBuffer->unref(this); | 355 fCurrentCmdBuffer->unref(this); |
196 | 356 |
197 // wait for all commands to finish | 357 // wait for all commands to finish |
198 VK_CALL(QueueWaitIdle(fQueue)); | 358 VK_CALL(QueueWaitIdle(fQueue)); |
199 | 359 |
200 // must call this just before we destroy the VkDevice | 360 // must call this just before we destroy the VkDevice |
201 fResourceProvider.destroyResources(); | 361 fResourceProvider.destroyResources(); |
202 | 362 |
| 363 #ifdef SK_DEBUG |
| 364 VK_CALL(DestroyDebugReportCallbackEXT(fVkInstance, fCallback, nullptr)); |
| 365 #endif |
| 366 |
203 VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr)); | 367 VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr)); |
204 VK_CALL(DestroyDevice(fDevice, nullptr)); | 368 VK_CALL(DestroyDevice(fDevice, nullptr)); |
205 VK_CALL(DestroyInstance(fVkInstance, nullptr)); | 369 VK_CALL(DestroyInstance(fVkInstance, nullptr)); |
206 } | 370 } |
207 | 371 |
208 /////////////////////////////////////////////////////////////////////////////// | 372 /////////////////////////////////////////////////////////////////////////////// |
209 | 373 |
210 void GrVkGpu::submitCommandBuffer(SyncQueue sync) { | 374 void GrVkGpu::submitCommandBuffer(SyncQueue sync) { |
211 SkASSERT(fCurrentCmdBuffer); | 375 SkASSERT(fCurrentCmdBuffer); |
212 fCurrentCmdBuffer->end(this); | 376 fCurrentCmdBuffer->end(this); |
(...skipping 1251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1464 int set_a_break_pt_here = 9; | 1628 int set_a_break_pt_here = 9; |
1465 aglSwapBuffers(aglGetCurrentContext()); | 1629 aglSwapBuffers(aglGetCurrentContext()); |
1466 #elif defined(SK_BUILD_FOR_WIN32) | 1630 #elif defined(SK_BUILD_FOR_WIN32) |
1467 SwapBuf(); | 1631 SwapBuf(); |
1468 int set_a_break_pt_here = 9; | 1632 int set_a_break_pt_here = 9; |
1469 SwapBuf(); | 1633 SwapBuf(); |
1470 #endif | 1634 #endif |
1471 #endif | 1635 #endif |
1472 } | 1636 } |
1473 | 1637 |
OLD | NEW |