blob: e2a2eebff2cfcc07344e36fb5c8d19d0ae11fb48 [file] [log] [blame]
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001/*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include <assert.h>
25#include <stdbool.h>
26#include <string.h>
27#include <unistd.h>
28#include <fcntl.h>
29
30#include "private.h"
31
32static int
33anv_env_get_int(const char *name)
34{
35 const char *val = getenv(name);
36
37 if (!val)
38 return 0;
39
40 return strtol(val, NULL, 0);
41}
42
43static VkResult
44fill_physical_device(struct anv_physical_device *device,
45 struct anv_instance *instance,
46 const char *path)
47{
48 int fd;
49
50 fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC);
51 if (fd < 0)
52 return vk_error(VK_ERROR_UNAVAILABLE);
53
54 device->instance = instance;
55 device->path = path;
56
57 device->chipset_id = anv_env_get_int("INTEL_DEVID_OVERRIDE");
58 device->no_hw = false;
59 if (device->chipset_id) {
60 /* INTEL_DEVID_OVERRIDE implies INTEL_NO_HW. */
61 device->no_hw = true;
62 } else {
63 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
64 }
65 if (!device->chipset_id)
66 goto fail;
67
68 device->name = brw_get_device_name(device->chipset_id);
69 device->info = brw_get_device_info(device->chipset_id, -1);
70 if (!device->info)
71 goto fail;
72
73 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT))
74 goto fail;
75
76 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2))
77 goto fail;
78
79 if (!anv_gem_get_param(fd, I915_PARAM_HAS_LLC))
80 goto fail;
81
82 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_CONSTANTS))
83 goto fail;
84
85 close(fd);
86
87 return VK_SUCCESS;
88
89 fail:
90 close(fd);
91
92 return vk_error(VK_ERROR_UNAVAILABLE);
93}
94
95static void *default_alloc(
96 void* pUserData,
97 size_t size,
98 size_t alignment,
99 VkSystemAllocType allocType)
100{
101 return malloc(size);
102}
103
104static void default_free(
105 void* pUserData,
106 void* pMem)
107{
108 free(pMem);
109}
110
111static const VkAllocCallbacks default_alloc_callbacks = {
112 .pUserData = NULL,
113 .pfnAlloc = default_alloc,
114 .pfnFree = default_free
115};
116
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700117VkResult anv_CreateInstance(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700118 const VkInstanceCreateInfo* pCreateInfo,
119 VkInstance* pInstance)
120{
121 struct anv_instance *instance;
122 const VkAllocCallbacks *alloc_callbacks = &default_alloc_callbacks;
123 void *user_data = NULL;
124 VkResult result;
125
126 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
127
128 if (pCreateInfo->pAllocCb) {
129 alloc_callbacks = pCreateInfo->pAllocCb;
130 user_data = pCreateInfo->pAllocCb->pUserData;
131 }
132 instance = alloc_callbacks->pfnAlloc(user_data, sizeof(*instance), 8,
133 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
134 if (!instance)
135 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
136
137 instance->pAllocUserData = alloc_callbacks->pUserData;
138 instance->pfnAlloc = alloc_callbacks->pfnAlloc;
139 instance->pfnFree = alloc_callbacks->pfnFree;
140 instance->apiVersion = pCreateInfo->pAppInfo->apiVersion;
141
142 instance->physicalDeviceCount = 0;
143 result = fill_physical_device(&instance->physicalDevice,
144 instance, "/dev/dri/renderD128");
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700145
Chad Versacea61f3072015-05-20 19:51:10 -0700146 if (result != VK_SUCCESS)
147 return result;
148
149 instance->physicalDeviceCount++;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700150 *pInstance = (VkInstance) instance;
151
152 return VK_SUCCESS;
153}
154
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700155VkResult anv_DestroyInstance(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700156 VkInstance _instance)
157{
158 struct anv_instance *instance = (struct anv_instance *) _instance;
159
160 instance->pfnFree(instance->pAllocUserData, instance);
161
162 return VK_SUCCESS;
163}
164
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700165VkResult anv_EnumeratePhysicalDevices(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700166 VkInstance _instance,
167 uint32_t* pPhysicalDeviceCount,
168 VkPhysicalDevice* pPhysicalDevices)
169{
170 struct anv_instance *instance = (struct anv_instance *) _instance;
171
172 if (*pPhysicalDeviceCount >= 1)
173 pPhysicalDevices[0] = (VkPhysicalDevice) &instance->physicalDevice;
174 *pPhysicalDeviceCount = instance->physicalDeviceCount;
175
176 return VK_SUCCESS;
177}
178
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700179VkResult anv_GetPhysicalDeviceInfo(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700180 VkPhysicalDevice physicalDevice,
181 VkPhysicalDeviceInfoType infoType,
182 size_t* pDataSize,
183 void* pData)
184{
185 struct anv_physical_device *device = (struct anv_physical_device *) physicalDevice;
186 VkPhysicalDeviceProperties *properties;
187 VkPhysicalDevicePerformance *performance;
188 VkPhysicalDeviceQueueProperties *queue_properties;
189 VkPhysicalDeviceMemoryProperties *memory_properties;
Kristian Høgsberga29df712015-05-15 22:04:52 -0700190 VkDisplayPropertiesWSI *display_properties;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700191 uint64_t ns_per_tick = 80;
192
Kristian Høgsberga29df712015-05-15 22:04:52 -0700193 switch ((uint32_t) infoType) {
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700194 case VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES:
195 properties = pData;
Kristian Høgsberg783e6212015-05-17 19:22:52 -0700196
197 *pDataSize = sizeof(*properties);
198 if (pData == NULL)
199 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700200
201 properties->apiVersion = 1;
202 properties->driverVersion = 1;
203 properties->vendorId = 0x8086;
204 properties->deviceId = device->chipset_id;
205 properties->deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
206 strcpy(properties->deviceName, device->name);
207 properties->maxInlineMemoryUpdateSize = 0;
Kristian Høgsberg5286ef72015-05-18 10:17:53 -0700208 properties->maxBoundDescriptorSets = MAX_SETS;
209 properties->maxThreadGroupSize = 512;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700210 properties->timestampFrequency = 1000 * 1000 * 1000 / ns_per_tick;
Kristian Høgsberg5286ef72015-05-18 10:17:53 -0700211 properties->multiColorAttachmentClears = true;
212 properties->maxDescriptorSets = 8;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700213 properties->maxViewports = 16;
214 properties->maxColorAttachments = 8;
215 return VK_SUCCESS;
216
217 case VK_PHYSICAL_DEVICE_INFO_TYPE_PERFORMANCE:
218 performance = pData;
Kristian Høgsberg783e6212015-05-17 19:22:52 -0700219
220 *pDataSize = sizeof(*performance);
221 if (pData == NULL)
222 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700223
224 performance->maxDeviceClock = 1.0;
225 performance->aluPerClock = 1.0;
226 performance->texPerClock = 1.0;
227 performance->primsPerClock = 1.0;
228 performance->pixelsPerClock = 1.0;
229 return VK_SUCCESS;
230
231 case VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES:
232 queue_properties = pData;
Kristian Høgsberg783e6212015-05-17 19:22:52 -0700233
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700234 *pDataSize = sizeof(*queue_properties);
Kristian Høgsberg783e6212015-05-17 19:22:52 -0700235 if (pData == NULL)
236 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700237
238 queue_properties->queueFlags = 0;
239 queue_properties->queueCount = 1;
240 queue_properties->maxAtomicCounters = 0;
Kristian Høgsberg5286ef72015-05-18 10:17:53 -0700241 queue_properties->supportsTimestamps = true;
242 queue_properties->maxMemReferences = 256;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700243 return VK_SUCCESS;
244
245 case VK_PHYSICAL_DEVICE_INFO_TYPE_MEMORY_PROPERTIES:
246 memory_properties = pData;
Kristian Høgsberg783e6212015-05-17 19:22:52 -0700247
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700248 *pDataSize = sizeof(*memory_properties);
Kristian Høgsberg783e6212015-05-17 19:22:52 -0700249 if (pData == NULL)
250 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700251
252 memory_properties->supportsMigration = false;
253 memory_properties->supportsPinning = false;
254 return VK_SUCCESS;
255
Kristian Høgsberga29df712015-05-15 22:04:52 -0700256 case VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI:
257 anv_finishme("VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI");
258
259 *pDataSize = sizeof(*display_properties);
260 if (pData == NULL)
261 return VK_SUCCESS;
262
263 display_properties = pData;
264 display_properties->display = 0;
265 display_properties->physicalResolution = (VkExtent2D) { 0, 0 };
266 return VK_SUCCESS;
267
268 case VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PRESENT_PROPERTIES_WSI:
269 anv_finishme("VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PRESENT_PROPERTIES_WSI");
270 return VK_SUCCESS;
271
272
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700273 default:
274 return VK_UNSUPPORTED;
275 }
276
277}
278
279void * vkGetProcAddr(
280 VkPhysicalDevice physicalDevice,
281 const char* pName)
282{
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700283 return anv_lookup_entrypoint(pName);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700284}
285
286static void
287parse_debug_flags(struct anv_device *device)
288{
289 const char *debug, *p, *end;
290
291 debug = getenv("INTEL_DEBUG");
292 device->dump_aub = false;
293 if (debug) {
294 for (p = debug; *p; p = end + 1) {
295 end = strchrnul(p, ',');
296 if (end - p == 3 && memcmp(p, "aub", 3) == 0)
297 device->dump_aub = true;
298 if (end - p == 5 && memcmp(p, "no_hw", 5) == 0)
299 device->no_hw = true;
300 if (*end == '\0')
301 break;
302 }
303 }
304}
305
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700306static const uint32_t BATCH_SIZE = 1 << 15;
307
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700308VkResult anv_CreateDevice(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700309 VkPhysicalDevice _physicalDevice,
310 const VkDeviceCreateInfo* pCreateInfo,
311 VkDevice* pDevice)
312{
313 struct anv_physical_device *physicalDevice =
314 (struct anv_physical_device *) _physicalDevice;
315 struct anv_instance *instance = physicalDevice->instance;
316 struct anv_device *device;
317
318 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
319
320 device = instance->pfnAlloc(instance->pAllocUserData,
321 sizeof(*device), 8,
322 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
323 if (!device)
324 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
325
326 device->no_hw = physicalDevice->no_hw;
327 parse_debug_flags(device);
328
329 device->instance = physicalDevice->instance;
330 device->fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC);
331 if (device->fd == -1)
332 goto fail_device;
333
334 device->context_id = anv_gem_create_context(device);
335 if (device->context_id == -1)
336 goto fail_fd;
337
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700338 anv_bo_pool_init(&device->batch_bo_pool, device, BATCH_SIZE);
339
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700340 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700341
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700342 anv_state_pool_init(&device->dynamic_state_pool,
343 &device->dynamic_state_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700344
345 anv_block_pool_init(&device->instruction_block_pool, device, 2048);
346 anv_block_pool_init(&device->surface_state_block_pool, device, 2048);
347
Jason Ekstrand923691c2015-05-18 19:56:32 -0700348
349 /* Binding table pointers are only 16 bits so we have to make sure that
350 * they get allocated at the beginning of the surface state BO. To
351 * handle this, we create a separate block pool that works out of the
352 * first 64 KB of the surface state BO.
353 */
354 anv_block_pool_init_slave(&device->binding_table_block_pool,
355 &device->surface_state_block_pool, 32);
356
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700357 anv_state_pool_init(&device->surface_state_pool,
358 &device->surface_state_block_pool);
359
360 device->compiler = anv_compiler_create(device->fd);
361 device->aub_writer = NULL;
362
363 device->info = *physicalDevice->info;
364
365 pthread_mutex_init(&device->mutex, NULL);
366
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -0700367 anv_device_init_meta(device);
368
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700369 *pDevice = (VkDevice) device;
370
371 return VK_SUCCESS;
372
373 fail_fd:
374 close(device->fd);
375 fail_device:
376 anv_device_free(device, device);
377
378 return vk_error(VK_ERROR_UNAVAILABLE);
379}
380
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700381VkResult anv_DestroyDevice(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700382 VkDevice _device)
383{
384 struct anv_device *device = (struct anv_device *) _device;
385
386 anv_compiler_destroy(device->compiler);
387
Jason Ekstrand5ef81f02015-05-25 15:46:48 -0700388
389 anv_bo_pool_finish(&device->batch_bo_pool);
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700390 anv_block_pool_finish(&device->dynamic_state_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700391 anv_block_pool_finish(&device->instruction_block_pool);
392 anv_block_pool_finish(&device->surface_state_block_pool);
393
394 close(device->fd);
395
396 if (device->aub_writer)
397 anv_aub_writer_destroy(device->aub_writer);
398
399 anv_device_free(device, device);
400
401 return VK_SUCCESS;
402}
403
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700404VkResult anv_GetGlobalExtensionInfo(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700405 VkExtensionInfoType infoType,
406 uint32_t extensionIndex,
407 size_t* pDataSize,
408 void* pData)
409{
Kristian Høgsberga29df712015-05-15 22:04:52 -0700410 static const VkExtensionProperties extensions[] = {
411 {
412 .extName = "VK_WSI_LunarG",
413 .version = 3
414 }
415 };
416 uint32_t count = ARRAY_SIZE(extensions);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700417
418 switch (infoType) {
419 case VK_EXTENSION_INFO_TYPE_COUNT:
Kristian Høgsberga29df712015-05-15 22:04:52 -0700420 memcpy(pData, &count, sizeof(count));
421 *pDataSize = sizeof(count);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700422 return VK_SUCCESS;
Kristian Høgsberga29df712015-05-15 22:04:52 -0700423
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700424 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
Kristian Høgsberga29df712015-05-15 22:04:52 -0700425 if (extensionIndex >= count)
426 return vk_error(VK_ERROR_INVALID_EXTENSION);
427
428 memcpy(pData, &extensions[extensionIndex], sizeof(extensions[0]));
429 *pDataSize = sizeof(extensions[0]);
430 return VK_SUCCESS;
431
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700432 default:
433 return VK_UNSUPPORTED;
434 }
435}
436
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700437VkResult anv_GetPhysicalDeviceExtensionInfo(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700438 VkPhysicalDevice physicalDevice,
439 VkExtensionInfoType infoType,
440 uint32_t extensionIndex,
441 size_t* pDataSize,
442 void* pData)
443{
444 uint32_t *count;
445
446 switch (infoType) {
447 case VK_EXTENSION_INFO_TYPE_COUNT:
Kristian Høgsberg783e6212015-05-17 19:22:52 -0700448 *pDataSize = 4;
449 if (pData == NULL)
450 return VK_SUCCESS;
451
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700452 count = pData;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700453 *count = 0;
454 return VK_SUCCESS;
455
456 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
457 return vk_error(VK_ERROR_INVALID_EXTENSION);
458
459 default:
460 return VK_UNSUPPORTED;
461 }
462}
463
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700464VkResult anv_EnumerateLayers(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700465 VkPhysicalDevice physicalDevice,
466 size_t maxStringSize,
467 size_t* pLayerCount,
468 char* const* pOutLayers,
469 void* pReserved)
470{
471 *pLayerCount = 0;
472
473 return VK_SUCCESS;
474}
475
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700476VkResult anv_GetDeviceQueue(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700477 VkDevice _device,
478 uint32_t queueNodeIndex,
479 uint32_t queueIndex,
480 VkQueue* pQueue)
481{
482 struct anv_device *device = (struct anv_device *) _device;
483 struct anv_queue *queue;
484
485 /* FIXME: Should allocate these at device create time. */
486
487 queue = anv_device_alloc(device, sizeof(*queue), 8,
488 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
489 if (queue == NULL)
490 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
491
492 queue->device = device;
493 queue->pool = &device->surface_state_pool;
494
495 queue->completed_serial = anv_state_pool_alloc(queue->pool, 4, 4);
496 *(uint32_t *)queue->completed_serial.map = 0;
497 queue->next_serial = 1;
498
499 *pQueue = (VkQueue) queue;
500
501 return VK_SUCCESS;
502}
503
Jason Ekstrand59def432015-05-27 11:41:28 -0700504VkResult
Jason Ekstrand403266b2015-05-25 17:38:15 -0700505anv_reloc_list_init(struct anv_reloc_list *list, struct anv_device *device)
506{
507 list->num_relocs = 0;
508 list->array_length = 256;
509 list->relocs =
510 anv_device_alloc(device, list->array_length * sizeof(*list->relocs), 8,
511 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
512
513 if (list->relocs == NULL)
514 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
515
516 list->reloc_bos =
517 anv_device_alloc(device, list->array_length * sizeof(*list->reloc_bos), 8,
518 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
519
520 if (list->relocs == NULL) {
521 anv_device_free(device, list->relocs);
522 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
523 }
524
525 return VK_SUCCESS;
526}
527
Jason Ekstrand59def432015-05-27 11:41:28 -0700528void
Jason Ekstrand403266b2015-05-25 17:38:15 -0700529anv_reloc_list_finish(struct anv_reloc_list *list, struct anv_device *device)
530{
531 anv_device_free(device, list->relocs);
532 anv_device_free(device, list->reloc_bos);
533}
534
535static VkResult
536anv_reloc_list_grow(struct anv_reloc_list *list, struct anv_device *device,
537 size_t num_additional_relocs)
538{
539 if (list->num_relocs + num_additional_relocs <= list->array_length)
540 return VK_SUCCESS;
541
542 size_t new_length = list->array_length * 2;
543 while (new_length < list->num_relocs + num_additional_relocs)
544 new_length *= 2;
545
546 struct drm_i915_gem_relocation_entry *new_relocs =
547 anv_device_alloc(device, new_length * sizeof(*list->relocs), 8,
548 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
549 if (new_relocs == NULL)
550 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
551
552 struct anv_bo **new_reloc_bos =
553 anv_device_alloc(device, new_length * sizeof(*list->reloc_bos), 8,
554 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
555 if (new_relocs == NULL) {
556 anv_device_free(device, new_relocs);
557 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
558 }
559
560 memcpy(new_relocs, list->relocs, list->num_relocs * sizeof(*list->relocs));
561 memcpy(new_reloc_bos, list->reloc_bos,
562 list->num_relocs * sizeof(*list->reloc_bos));
563
564 anv_device_free(device, list->relocs);
565 anv_device_free(device, list->reloc_bos);
566
567 list->relocs = new_relocs;
568 list->reloc_bos = new_reloc_bos;
569
570 return VK_SUCCESS;
571}
572
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700573static VkResult
574anv_batch_bo_create(struct anv_device *device, struct anv_batch_bo **bbo_out)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700575{
576 VkResult result;
577
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700578 struct anv_batch_bo *bbo =
579 anv_device_alloc(device, sizeof(*bbo), 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL);
580 if (bbo == NULL)
581 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700582
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700583 bbo->num_relocs = 0;
584 bbo->prev_batch_bo = NULL;
585
586 result = anv_bo_pool_alloc(&device->batch_bo_pool, &bbo->bo);
Jason Ekstrand403266b2015-05-25 17:38:15 -0700587 if (result != VK_SUCCESS) {
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700588 anv_device_free(device, bbo);
Jason Ekstrand403266b2015-05-25 17:38:15 -0700589 return result;
590 }
591
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700592 *bbo_out = bbo;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700593
594 return VK_SUCCESS;
595}
596
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700597static void
598anv_batch_bo_start(struct anv_batch_bo *bbo, struct anv_batch *batch,
599 size_t batch_padding)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700600{
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700601 batch->next = batch->start = bbo->bo.map;
602 batch->end = bbo->bo.map + bbo->bo.size - batch_padding;
603 bbo->first_reloc = batch->relocs.num_relocs;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700604}
605
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700606static void
607anv_batch_bo_finish(struct anv_batch_bo *bbo, struct anv_batch *batch)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700608{
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700609 assert(batch->start == bbo->bo.map);
610 bbo->length = batch->next - batch->start;
611 bbo->num_relocs = batch->relocs.num_relocs - bbo->first_reloc;
612}
613
614static void
615anv_batch_bo_destroy(struct anv_batch_bo *bbo, struct anv_device *device)
616{
617 anv_bo_pool_free(&device->batch_bo_pool, &bbo->bo);
618 anv_device_free(device, bbo);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700619}
620
621void *
622anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords)
623{
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700624 if (batch->next + num_dwords * 4 > batch->end)
625 batch->extend_cb(batch, batch->user_data);
626
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700627 void *p = batch->next;
628
629 batch->next += num_dwords * 4;
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700630 assert(batch->next <= batch->end);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700631
632 return p;
633}
634
635static void
Jason Ekstrand403266b2015-05-25 17:38:15 -0700636anv_reloc_list_append(struct anv_reloc_list *list, struct anv_device *device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700637 struct anv_reloc_list *other, uint32_t offset)
638{
Jason Ekstrand403266b2015-05-25 17:38:15 -0700639 anv_reloc_list_grow(list, device, other->num_relocs);
640 /* TODO: Handle failure */
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700641
Jason Ekstrand403266b2015-05-25 17:38:15 -0700642 memcpy(&list->relocs[list->num_relocs], &other->relocs[0],
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700643 other->num_relocs * sizeof(other->relocs[0]));
Jason Ekstrand403266b2015-05-25 17:38:15 -0700644 memcpy(&list->reloc_bos[list->num_relocs], &other->reloc_bos[0],
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700645 other->num_relocs * sizeof(other->reloc_bos[0]));
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700646
Jason Ekstrand403266b2015-05-25 17:38:15 -0700647 for (uint32_t i = 0; i < other->num_relocs; i++)
648 list->relocs[i + list->num_relocs].offset += offset;
649
650 list->num_relocs += other->num_relocs;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700651}
652
653static uint64_t
Jason Ekstrand403266b2015-05-25 17:38:15 -0700654anv_reloc_list_add(struct anv_reloc_list *list, struct anv_device *device,
655 uint32_t offset, struct anv_bo *target_bo, uint32_t delta)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700656{
657 struct drm_i915_gem_relocation_entry *entry;
658 int index;
659
Jason Ekstrand403266b2015-05-25 17:38:15 -0700660 anv_reloc_list_grow(list, device, 1);
661 /* TODO: Handle failure */
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700662
663 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
664 index = list->num_relocs++;
665 list->reloc_bos[index] = target_bo;
666 entry = &list->relocs[index];
667 entry->target_handle = target_bo->gem_handle;
668 entry->delta = delta;
669 entry->offset = offset;
670 entry->presumed_offset = target_bo->offset;
671 entry->read_domains = 0;
672 entry->write_domain = 0;
673
674 return target_bo->offset + delta;
675}
676
677void
678anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other)
679{
680 uint32_t size, offset;
681
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700682 size = other->next - other->start;
683 assert(size % 4 == 0);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700684
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700685 if (batch->next + size > batch->end)
686 batch->extend_cb(batch, batch->user_data);
687
688 assert(batch->next + size <= batch->end);
689
690 memcpy(batch->next, other->start, size);
691
692 offset = batch->next - batch->start;
693 anv_reloc_list_append(&batch->relocs, batch->device,
694 &other->relocs, offset);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700695
696 batch->next += size;
697}
698
699uint64_t
700anv_batch_emit_reloc(struct anv_batch *batch,
701 void *location, struct anv_bo *bo, uint32_t delta)
702{
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700703 return anv_reloc_list_add(&batch->relocs, batch->device,
704 location - batch->start, bo, delta);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700705}
706
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700707VkResult anv_QueueSubmit(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700708 VkQueue _queue,
709 uint32_t cmdBufferCount,
710 const VkCmdBuffer* pCmdBuffers,
Kristian Høgsberg6afb2642015-05-18 08:49:15 -0700711 VkFence _fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700712{
713 struct anv_queue *queue = (struct anv_queue *) _queue;
714 struct anv_device *device = queue->device;
Kristian Høgsberg6afb2642015-05-18 08:49:15 -0700715 struct anv_fence *fence = (struct anv_fence *) _fence;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700716 int ret;
717
Kristian Høgsbergcb986ef2015-05-12 14:38:12 -0700718 for (uint32_t i = 0; i < cmdBufferCount; i++) {
719 struct anv_cmd_buffer *cmd_buffer =
720 (struct anv_cmd_buffer *) pCmdBuffers[i];
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700721
Kristian Høgsbergcb986ef2015-05-12 14:38:12 -0700722 if (device->dump_aub)
723 anv_cmd_buffer_dump(cmd_buffer);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700724
Kristian Høgsbergcb986ef2015-05-12 14:38:12 -0700725 if (!device->no_hw) {
726 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf);
727 if (ret != 0)
Kristian Høgsberg2b7a0602015-05-12 14:38:58 -0700728 return vk_error(VK_ERROR_UNKNOWN);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700729
Kristian Høgsberg6afb2642015-05-18 08:49:15 -0700730 if (fence) {
731 ret = anv_gem_execbuffer(device, &fence->execbuf);
732 if (ret != 0)
733 return vk_error(VK_ERROR_UNKNOWN);
734 }
735
Kristian Høgsbergcb986ef2015-05-12 14:38:12 -0700736 for (uint32_t i = 0; i < cmd_buffer->bo_count; i++)
737 cmd_buffer->exec2_bos[i]->offset = cmd_buffer->exec2_objects[i].offset;
738 } else {
739 *(uint32_t *)queue->completed_serial.map = cmd_buffer->serial;
740 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700741 }
742
743 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700744}
745
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700746VkResult anv_QueueAddMemReferences(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700747 VkQueue queue,
748 uint32_t count,
749 const VkDeviceMemory* pMems)
750{
751 return VK_SUCCESS;
752}
753
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700754VkResult anv_QueueRemoveMemReferences(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700755 VkQueue queue,
756 uint32_t count,
757 const VkDeviceMemory* pMems)
758{
759 return VK_SUCCESS;
760}
761
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700762VkResult anv_QueueWaitIdle(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700763 VkQueue _queue)
764{
765 struct anv_queue *queue = (struct anv_queue *) _queue;
766
767 return vkDeviceWaitIdle((VkDevice) queue->device);
768}
769
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700770VkResult anv_DeviceWaitIdle(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700771 VkDevice _device)
772{
773 struct anv_device *device = (struct anv_device *) _device;
774 struct anv_state state;
775 struct anv_batch batch;
776 struct drm_i915_gem_execbuffer2 execbuf;
777 struct drm_i915_gem_exec_object2 exec2_objects[1];
778 struct anv_bo *bo = NULL;
779 VkResult result;
780 int64_t timeout;
781 int ret;
782
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700783 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
784 bo = &device->dynamic_state_pool.block_pool->bo;
Jason Ekstrandda8f1482015-05-27 11:42:55 -0700785 batch.start = batch.next = state.map;
786 batch.end = state.map + 32;
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700787 anv_batch_emit(&batch, GEN8_MI_BATCH_BUFFER_END);
788 anv_batch_emit(&batch, GEN8_MI_NOOP);
789
790 exec2_objects[0].handle = bo->gem_handle;
791 exec2_objects[0].relocation_count = 0;
792 exec2_objects[0].relocs_ptr = 0;
793 exec2_objects[0].alignment = 0;
794 exec2_objects[0].offset = bo->offset;
795 exec2_objects[0].flags = 0;
796 exec2_objects[0].rsvd1 = 0;
797 exec2_objects[0].rsvd2 = 0;
798
799 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
800 execbuf.buffer_count = 1;
801 execbuf.batch_start_offset = state.offset;
802 execbuf.batch_len = batch.next - state.map;
803 execbuf.cliprects_ptr = 0;
804 execbuf.num_cliprects = 0;
805 execbuf.DR1 = 0;
806 execbuf.DR4 = 0;
807
808 execbuf.flags =
809 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
810 execbuf.rsvd1 = device->context_id;
811 execbuf.rsvd2 = 0;
812
813 if (!device->no_hw) {
814 ret = anv_gem_execbuffer(device, &execbuf);
815 if (ret != 0) {
816 result = vk_error(VK_ERROR_UNKNOWN);
817 goto fail;
818 }
819
820 timeout = INT64_MAX;
821 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
822 if (ret != 0) {
823 result = vk_error(VK_ERROR_UNKNOWN);
824 goto fail;
825 }
826 }
827
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700828 anv_state_pool_free(&device->dynamic_state_pool, state);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700829
830 return VK_SUCCESS;
831
832 fail:
Kristian Høgsberg0a775e12015-05-13 15:34:34 -0700833 anv_state_pool_free(&device->dynamic_state_pool, state);
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700834
835 return result;
836}
837
838void *
839anv_device_alloc(struct anv_device * device,
840 size_t size,
841 size_t alignment,
842 VkSystemAllocType allocType)
843{
844 return device->instance->pfnAlloc(device->instance->pAllocUserData,
845 size,
846 alignment,
847 allocType);
848}
849
850void
851anv_device_free(struct anv_device * device,
852 void * mem)
853{
854 return device->instance->pfnFree(device->instance->pAllocUserData,
855 mem);
856}
857
858VkResult
859anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
860{
861 bo->gem_handle = anv_gem_create(device, size);
862 if (!bo->gem_handle)
863 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
864
865 bo->map = NULL;
866 bo->index = 0;
867 bo->offset = 0;
868 bo->size = size;
869
870 return VK_SUCCESS;
871}
872
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700873VkResult anv_AllocMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700874 VkDevice _device,
875 const VkMemoryAllocInfo* pAllocInfo,
876 VkDeviceMemory* pMem)
877{
878 struct anv_device *device = (struct anv_device *) _device;
879 struct anv_device_memory *mem;
880 VkResult result;
881
882 assert(pAllocInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO);
883
884 mem = anv_device_alloc(device, sizeof(*mem), 8,
885 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
886 if (mem == NULL)
887 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
888
889 result = anv_bo_init_new(&mem->bo, device, pAllocInfo->allocationSize);
890 if (result != VK_SUCCESS)
891 goto fail;
892
893 *pMem = (VkDeviceMemory) mem;
894
895 return VK_SUCCESS;
896
897 fail:
898 anv_device_free(device, mem);
899
900 return result;
901}
902
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700903VkResult anv_FreeMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700904 VkDevice _device,
905 VkDeviceMemory _mem)
906{
907 struct anv_device *device = (struct anv_device *) _device;
908 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
909
910 if (mem->bo.map)
911 anv_gem_munmap(mem->bo.map, mem->bo.size);
912
913 if (mem->bo.gem_handle != 0)
914 anv_gem_close(device, mem->bo.gem_handle);
915
916 anv_device_free(device, mem);
917
918 return VK_SUCCESS;
919}
920
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700921VkResult anv_SetMemoryPriority(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700922 VkDevice device,
923 VkDeviceMemory mem,
924 VkMemoryPriority priority)
925{
926 return VK_SUCCESS;
927}
928
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700929VkResult anv_MapMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700930 VkDevice _device,
931 VkDeviceMemory _mem,
932 VkDeviceSize offset,
933 VkDeviceSize size,
934 VkMemoryMapFlags flags,
935 void** ppData)
936{
937 struct anv_device *device = (struct anv_device *) _device;
938 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
939
940 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
941 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
942 * at a time is valid. We could just mmap up front and return an offset
943 * pointer here, but that may exhaust virtual memory on 32 bit
944 * userspace. */
945
946 mem->map = anv_gem_mmap(device, mem->bo.gem_handle, offset, size);
947 mem->map_size = size;
948
949 *ppData = mem->map;
950
951 return VK_SUCCESS;
952}
953
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700954VkResult anv_UnmapMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700955 VkDevice _device,
956 VkDeviceMemory _mem)
957{
958 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
959
960 anv_gem_munmap(mem->map, mem->map_size);
961
962 return VK_SUCCESS;
963}
964
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700965VkResult anv_FlushMappedMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700966 VkDevice device,
967 VkDeviceMemory mem,
968 VkDeviceSize offset,
969 VkDeviceSize size)
970{
971 /* clflush here for !llc platforms */
972
973 return VK_SUCCESS;
974}
975
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700976VkResult anv_PinSystemMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700977 VkDevice device,
978 const void* pSysMem,
979 size_t memSize,
980 VkDeviceMemory* pMem)
981{
982 return VK_SUCCESS;
983}
984
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700985VkResult anv_GetMultiDeviceCompatibility(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700986 VkPhysicalDevice physicalDevice0,
987 VkPhysicalDevice physicalDevice1,
988 VkPhysicalDeviceCompatibilityInfo* pInfo)
989{
990 return VK_UNSUPPORTED;
991}
992
Kristian Høgsberg454345d2015-05-17 16:33:48 -0700993VkResult anv_OpenSharedMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -0700994 VkDevice device,
995 const VkMemoryOpenInfo* pOpenInfo,
996 VkDeviceMemory* pMem)
997{
998 return VK_UNSUPPORTED;
999}
1000
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001001VkResult anv_OpenSharedSemaphore(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001002 VkDevice device,
1003 const VkSemaphoreOpenInfo* pOpenInfo,
1004 VkSemaphore* pSemaphore)
1005{
1006 return VK_UNSUPPORTED;
1007}
1008
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001009VkResult anv_OpenPeerMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001010 VkDevice device,
1011 const VkPeerMemoryOpenInfo* pOpenInfo,
1012 VkDeviceMemory* pMem)
1013{
1014 return VK_UNSUPPORTED;
1015}
1016
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001017VkResult anv_OpenPeerImage(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001018 VkDevice device,
1019 const VkPeerImageOpenInfo* pOpenInfo,
1020 VkImage* pImage,
1021 VkDeviceMemory* pMem)
1022{
1023 return VK_UNSUPPORTED;
1024}
1025
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001026VkResult anv_DestroyObject(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001027 VkDevice _device,
1028 VkObjectType objType,
Jason Ekstrand57153da2015-05-22 15:15:08 -07001029 VkObject _object)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001030{
1031 struct anv_device *device = (struct anv_device *) _device;
Jason Ekstrand57153da2015-05-22 15:15:08 -07001032 struct anv_object *object = (struct anv_object *) _object;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001033
Jason Ekstrand57153da2015-05-22 15:15:08 -07001034 switch (objType) {
1035 case VK_OBJECT_TYPE_INSTANCE:
1036 return anv_DestroyInstance((VkInstance) _object);
1037
1038 case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
1039 /* We don't want to actually destroy physical devices */
1040 return VK_SUCCESS;
1041
1042 case VK_OBJECT_TYPE_DEVICE:
1043 assert(_device == (VkDevice) _object);
1044 return anv_DestroyDevice((VkDevice) _object);
1045
1046 case VK_OBJECT_TYPE_QUEUE:
1047 /* TODO */
1048 return VK_SUCCESS;
1049
1050 case VK_OBJECT_TYPE_DEVICE_MEMORY:
1051 return anv_FreeMemory(_device, (VkDeviceMemory) _object);
1052
1053 case VK_OBJECT_TYPE_DESCRIPTOR_POOL:
1054 /* These are just dummys anyway, so we don't need to destroy them */
1055 return VK_SUCCESS;
1056
1057 case VK_OBJECT_TYPE_BUFFER:
1058 case VK_OBJECT_TYPE_BUFFER_VIEW:
1059 case VK_OBJECT_TYPE_IMAGE:
1060 case VK_OBJECT_TYPE_IMAGE_VIEW:
1061 case VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW:
1062 case VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW:
1063 case VK_OBJECT_TYPE_SHADER:
1064 case VK_OBJECT_TYPE_PIPELINE_LAYOUT:
1065 case VK_OBJECT_TYPE_SAMPLER:
1066 case VK_OBJECT_TYPE_DESCRIPTOR_SET:
1067 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT:
1068 case VK_OBJECT_TYPE_DYNAMIC_RS_STATE:
1069 case VK_OBJECT_TYPE_DYNAMIC_CB_STATE:
1070 case VK_OBJECT_TYPE_DYNAMIC_DS_STATE:
1071 case VK_OBJECT_TYPE_RENDER_PASS:
1072 /* These are trivially destroyable */
1073 anv_device_free(device, (void *) _object);
1074 return VK_SUCCESS;
1075
1076 case VK_OBJECT_TYPE_COMMAND_BUFFER:
1077 case VK_OBJECT_TYPE_PIPELINE:
1078 case VK_OBJECT_TYPE_DYNAMIC_VP_STATE:
1079 case VK_OBJECT_TYPE_FENCE:
1080 case VK_OBJECT_TYPE_QUERY_POOL:
1081 case VK_OBJECT_TYPE_FRAMEBUFFER:
1082 (object->destructor)(device, object, objType);
1083 return VK_SUCCESS;
1084
1085 case VK_OBJECT_TYPE_SEMAPHORE:
1086 case VK_OBJECT_TYPE_EVENT:
1087 stub_return(VK_UNSUPPORTED);
1088
1089 default:
1090 unreachable("Invalid object type");
1091 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001092}
1093
1094static void
1095fill_memory_requirements(
1096 VkObjectType objType,
1097 VkObject object,
1098 VkMemoryRequirements * memory_requirements)
1099{
1100 struct anv_buffer *buffer;
1101 struct anv_image *image;
1102
1103 memory_requirements->memPropsAllowed =
1104 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1105 VK_MEMORY_PROPERTY_HOST_DEVICE_COHERENT_BIT |
1106 /* VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | */
1107 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT |
1108 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL |
1109 VK_MEMORY_PROPERTY_SHAREABLE_BIT;
1110
1111 memory_requirements->memPropsRequired = 0;
1112
1113 switch (objType) {
1114 case VK_OBJECT_TYPE_BUFFER:
1115 buffer = (struct anv_buffer *) object;
1116 memory_requirements->size = buffer->size;
1117 memory_requirements->alignment = 16;
1118 break;
1119 case VK_OBJECT_TYPE_IMAGE:
1120 image = (struct anv_image *) object;
1121 memory_requirements->size = image->size;
1122 memory_requirements->alignment = image->alignment;
1123 break;
1124 default:
1125 memory_requirements->size = 0;
1126 break;
1127 }
1128}
1129
Kristian Høgsbergb7fac7a2015-05-17 19:25:28 -07001130static uint32_t
1131get_allocation_count(VkObjectType objType)
1132{
1133 switch (objType) {
1134 case VK_OBJECT_TYPE_BUFFER:
1135 case VK_OBJECT_TYPE_IMAGE:
1136 return 1;
1137 default:
1138 return 0;
1139 }
1140}
1141
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001142VkResult anv_GetObjectInfo(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001143 VkDevice _device,
1144 VkObjectType objType,
1145 VkObject object,
1146 VkObjectInfoType infoType,
1147 size_t* pDataSize,
1148 void* pData)
1149{
1150 VkMemoryRequirements memory_requirements;
Kristian Høgsberg05754542015-05-18 08:50:04 -07001151 uint32_t *count;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001152
1153 switch (infoType) {
1154 case VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS:
Kristian Høgsberg783e6212015-05-17 19:22:52 -07001155 *pDataSize = sizeof(memory_requirements);
1156 if (pData == NULL)
1157 return VK_SUCCESS;
1158
Kristian Høgsberg05754542015-05-18 08:50:04 -07001159 fill_memory_requirements(objType, object, pData);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001160 return VK_SUCCESS;
1161
1162 case VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT:
Kristian Høgsbergb7fac7a2015-05-17 19:25:28 -07001163 *pDataSize = sizeof(count);
1164 if (pData == NULL)
1165 return VK_SUCCESS;
1166
Kristian Høgsberg05754542015-05-18 08:50:04 -07001167 count = pData;
1168 *count = get_allocation_count(objType);
Kristian Høgsbergb7fac7a2015-05-17 19:25:28 -07001169 return VK_SUCCESS;
1170
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001171 default:
1172 return VK_UNSUPPORTED;
1173 }
1174
1175}
1176
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001177VkResult anv_QueueBindObjectMemory(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001178 VkQueue queue,
1179 VkObjectType objType,
1180 VkObject object,
1181 uint32_t allocationIdx,
1182 VkDeviceMemory _mem,
1183 VkDeviceSize memOffset)
1184{
1185 struct anv_buffer *buffer;
1186 struct anv_image *image;
1187 struct anv_device_memory *mem = (struct anv_device_memory *) _mem;
1188
1189 switch (objType) {
1190 case VK_OBJECT_TYPE_BUFFER:
1191 buffer = (struct anv_buffer *) object;
Kristian Høgsberg099faa12015-05-11 22:19:58 -07001192 buffer->bo = &mem->bo;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001193 buffer->offset = memOffset;
1194 break;
1195 case VK_OBJECT_TYPE_IMAGE:
1196 image = (struct anv_image *) object;
Kristian Høgsberg099faa12015-05-11 22:19:58 -07001197 image->bo = &mem->bo;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001198 image->offset = memOffset;
1199 break;
1200 default:
1201 break;
1202 }
1203
1204 return VK_SUCCESS;
1205}
1206
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001207VkResult anv_QueueBindObjectMemoryRange(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001208 VkQueue queue,
1209 VkObjectType objType,
1210 VkObject object,
1211 uint32_t allocationIdx,
1212 VkDeviceSize rangeOffset,
1213 VkDeviceSize rangeSize,
1214 VkDeviceMemory mem,
1215 VkDeviceSize memOffset)
1216{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07001217 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001218}
1219
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001220VkResult anv_QueueBindImageMemoryRange(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001221 VkQueue queue,
1222 VkImage image,
1223 uint32_t allocationIdx,
1224 const VkImageMemoryBindInfo* pBindInfo,
1225 VkDeviceMemory mem,
1226 VkDeviceSize memOffset)
1227{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07001228 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001229}
1230
Jason Ekstrand57153da2015-05-22 15:15:08 -07001231static void
1232anv_fence_destroy(struct anv_device *device,
1233 struct anv_object *object,
1234 VkObjectType obj_type)
1235{
1236 struct anv_fence *fence = (struct anv_fence *) object;
1237
1238 assert(obj_type == VK_OBJECT_TYPE_FENCE);
1239
1240 anv_gem_munmap(fence->bo.map, fence->bo.size);
1241 anv_gem_close(device, fence->bo.gem_handle);
1242 anv_device_free(device, fence);
1243}
1244
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001245VkResult anv_CreateFence(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001246 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001247 const VkFenceCreateInfo* pCreateInfo,
1248 VkFence* pFence)
1249{
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001250 struct anv_device *device = (struct anv_device *) _device;
1251 struct anv_fence *fence;
1252 struct anv_batch batch;
1253 VkResult result;
1254
1255 const uint32_t fence_size = 128;
1256
1257 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1258
1259 fence = anv_device_alloc(device, sizeof(*fence), 8,
1260 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1261 if (fence == NULL)
1262 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1263
1264 result = anv_bo_init_new(&fence->bo, device, fence_size);
1265 if (result != VK_SUCCESS)
1266 goto fail;
1267
Jason Ekstrand57153da2015-05-22 15:15:08 -07001268 fence->base.destructor = anv_fence_destroy;
1269
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001270 fence->bo.map =
1271 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size);
Jason Ekstrandda8f1482015-05-27 11:42:55 -07001272 batch.next = batch.start = fence->bo.map;
1273 batch.end = fence->bo.map + fence->bo.size;
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001274 anv_batch_emit(&batch, GEN8_MI_BATCH_BUFFER_END);
1275 anv_batch_emit(&batch, GEN8_MI_NOOP);
1276
1277 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1278 fence->exec2_objects[0].relocation_count = 0;
1279 fence->exec2_objects[0].relocs_ptr = 0;
1280 fence->exec2_objects[0].alignment = 0;
1281 fence->exec2_objects[0].offset = fence->bo.offset;
1282 fence->exec2_objects[0].flags = 0;
1283 fence->exec2_objects[0].rsvd1 = 0;
1284 fence->exec2_objects[0].rsvd2 = 0;
1285
1286 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1287 fence->execbuf.buffer_count = 1;
1288 fence->execbuf.batch_start_offset = 0;
1289 fence->execbuf.batch_len = batch.next - fence->bo.map;
1290 fence->execbuf.cliprects_ptr = 0;
1291 fence->execbuf.num_cliprects = 0;
1292 fence->execbuf.DR1 = 0;
1293 fence->execbuf.DR4 = 0;
1294
1295 fence->execbuf.flags =
1296 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1297 fence->execbuf.rsvd1 = device->context_id;
1298 fence->execbuf.rsvd2 = 0;
1299
1300 *pFence = (VkQueryPool) fence;
1301
1302 return VK_SUCCESS;
1303
1304 fail:
1305 anv_device_free(device, fence);
1306
1307 return result;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001308}
1309
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001310VkResult anv_ResetFences(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001311 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001312 uint32_t fenceCount,
1313 VkFence* pFences)
1314{
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001315 struct anv_fence **fences = (struct anv_fence **) pFences;
1316
1317 for (uint32_t i; i < fenceCount; i++)
1318 fences[i]->ready = false;
1319
1320 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001321}
1322
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001323VkResult anv_GetFenceStatus(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001324 VkDevice _device,
1325 VkFence _fence)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001326{
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001327 struct anv_device *device = (struct anv_device *) _device;
1328 struct anv_fence *fence = (struct anv_fence *) _fence;
1329 int64_t t = 0;
1330 int ret;
1331
1332 if (fence->ready)
1333 return VK_SUCCESS;
1334
1335 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1336 if (ret == 0) {
1337 fence->ready = true;
1338 return VK_SUCCESS;
1339 }
1340
1341 return VK_NOT_READY;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001342}
1343
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001344VkResult anv_WaitForFences(
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001345 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001346 uint32_t fenceCount,
1347 const VkFence* pFences,
1348 bool32_t waitAll,
1349 uint64_t timeout)
1350{
Kristian Høgsberg6afb2642015-05-18 08:49:15 -07001351 struct anv_device *device = (struct anv_device *) _device;
1352 struct anv_fence **fences = (struct anv_fence **) pFences;
1353 int64_t t = timeout;
1354 int ret;
1355
1356 /* FIXME: handle !waitAll */
1357
1358 for (uint32_t i = 0; i < fenceCount; i++) {
1359 ret = anv_gem_wait(device, fences[i]->bo.gem_handle, &t);
1360 if (ret == -1 && errno == ETIME)
1361 return VK_TIMEOUT;
1362 else if (ret == -1)
1363 return vk_error(VK_ERROR_UNKNOWN);
1364 }
1365
1366 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001367}
1368
1369// Queue semaphore functions
1370
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001371VkResult anv_CreateSemaphore(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001372 VkDevice device,
1373 const VkSemaphoreCreateInfo* pCreateInfo,
1374 VkSemaphore* pSemaphore)
1375{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07001376 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001377}
1378
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001379VkResult anv_QueueSignalSemaphore(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001380 VkQueue queue,
1381 VkSemaphore semaphore)
1382{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07001383 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001384}
1385
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001386VkResult anv_QueueWaitSemaphore(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001387 VkQueue queue,
1388 VkSemaphore semaphore)
1389{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07001390 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001391}
1392
1393// Event functions
1394
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001395VkResult anv_CreateEvent(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001396 VkDevice device,
1397 const VkEventCreateInfo* pCreateInfo,
1398 VkEvent* pEvent)
1399{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07001400 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001401}
1402
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001403VkResult anv_GetEventStatus(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001404 VkDevice device,
1405 VkEvent event)
1406{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07001407 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001408}
1409
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001410VkResult anv_SetEvent(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001411 VkDevice device,
1412 VkEvent event)
1413{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07001414 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001415}
1416
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001417VkResult anv_ResetEvent(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001418 VkDevice device,
1419 VkEvent event)
1420{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07001421 stub_return(VK_UNSUPPORTED);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001422}
1423
1424// Query functions
1425
Jason Ekstrand57153da2015-05-22 15:15:08 -07001426static void
1427anv_query_pool_destroy(struct anv_device *device,
1428 struct anv_object *object,
1429 VkObjectType obj_type)
1430{
1431 struct anv_query_pool *pool = (struct anv_query_pool *) object;
1432
1433 assert(obj_type == VK_OBJECT_TYPE_QUERY_POOL);
1434
1435 anv_gem_munmap(pool->bo.map, pool->bo.size);
1436 anv_gem_close(device, pool->bo.gem_handle);
1437 anv_device_free(device, pool);
1438}
1439
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001440VkResult anv_CreateQueryPool(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001441 VkDevice _device,
1442 const VkQueryPoolCreateInfo* pCreateInfo,
1443 VkQueryPool* pQueryPool)
1444{
1445 struct anv_device *device = (struct anv_device *) _device;
1446 struct anv_query_pool *pool;
1447 VkResult result;
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07001448 size_t size;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001449
1450 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
1451
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07001452 switch (pCreateInfo->queryType) {
1453 case VK_QUERY_TYPE_OCCLUSION:
1454 break;
1455 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
1456 return VK_UNSUPPORTED;
1457 default:
1458 unreachable("");
1459 }
1460
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001461 pool = anv_device_alloc(device, sizeof(*pool), 8,
1462 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1463 if (pool == NULL)
1464 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1465
Jason Ekstrand57153da2015-05-22 15:15:08 -07001466 pool->base.destructor = anv_query_pool_destroy;
1467
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001468 pool->type = pCreateInfo->queryType;
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07001469 size = pCreateInfo->slots * sizeof(struct anv_query_pool_slot);
1470 result = anv_bo_init_new(&pool->bo, device, size);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001471 if (result != VK_SUCCESS)
1472 goto fail;
1473
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07001474 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0, size);
1475
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001476 *pQueryPool = (VkQueryPool) pool;
1477
1478 return VK_SUCCESS;
1479
1480 fail:
1481 anv_device_free(device, pool);
1482
1483 return result;
1484}
1485
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001486VkResult anv_GetQueryPoolResults(
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07001487 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001488 VkQueryPool queryPool,
1489 uint32_t startQuery,
1490 uint32_t queryCount,
1491 size_t* pDataSize,
1492 void* pData,
1493 VkQueryResultFlags flags)
1494{
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07001495 struct anv_device *device = (struct anv_device *) _device;
1496 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
1497 struct anv_query_pool_slot *slot = pool->bo.map;
1498 int64_t timeout = INT64_MAX;
1499 uint32_t *dst32 = pData;
1500 uint64_t *dst64 = pData;
1501 uint64_t result;
1502 int ret;
1503
1504 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
1505 /* Where is the availabilty info supposed to go? */
1506 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
1507 return VK_UNSUPPORTED;
1508 }
1509
1510 assert(pool->type == VK_QUERY_TYPE_OCCLUSION);
1511
1512 if (flags & VK_QUERY_RESULT_64_BIT)
1513 *pDataSize = queryCount * sizeof(uint64_t);
1514 else
1515 *pDataSize = queryCount * sizeof(uint32_t);
1516
1517 if (pData == NULL)
1518 return VK_SUCCESS;
1519
1520 if (flags & VK_QUERY_RESULT_WAIT_BIT) {
1521 ret = anv_gem_wait(device, pool->bo.gem_handle, &timeout);
1522 if (ret == -1)
1523 return vk_error(VK_ERROR_UNKNOWN);
1524 }
1525
1526 for (uint32_t i = 0; i < queryCount; i++) {
1527 result = slot[startQuery + i].end - slot[startQuery + i].begin;
1528 if (flags & VK_QUERY_RESULT_64_BIT) {
1529 *dst64++ = result;
1530 } else {
1531 if (result > UINT32_MAX)
1532 result = UINT32_MAX;
1533 *dst32++ = result;
1534 }
1535 }
1536
1537 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001538}
1539
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001540// Buffer functions
1541
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001542VkResult anv_CreateBuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001543 VkDevice _device,
1544 const VkBufferCreateInfo* pCreateInfo,
1545 VkBuffer* pBuffer)
1546{
1547 struct anv_device *device = (struct anv_device *) _device;
1548 struct anv_buffer *buffer;
1549
1550 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1551
1552 buffer = anv_device_alloc(device, sizeof(*buffer), 8,
1553 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1554 if (buffer == NULL)
1555 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1556
1557 buffer->size = pCreateInfo->size;
Kristian Høgsberg099faa12015-05-11 22:19:58 -07001558 buffer->bo = NULL;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001559 buffer->offset = 0;
1560
1561 *pBuffer = (VkBuffer) buffer;
1562
1563 return VK_SUCCESS;
1564}
1565
1566// Buffer view functions
1567
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001568VkResult anv_CreateBufferView(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001569 VkDevice _device,
1570 const VkBufferViewCreateInfo* pCreateInfo,
1571 VkBufferView* pView)
1572{
1573 struct anv_device *device = (struct anv_device *) _device;
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -07001574 struct anv_buffer *buffer = (struct anv_buffer *) pCreateInfo->buffer;
1575 struct anv_surface_view *view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001576 const struct anv_format *format;
1577
1578 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO);
1579
1580 view = anv_device_alloc(device, sizeof(*view), 8,
1581 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1582 if (view == NULL)
1583 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1584
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -07001585 view->bo = buffer->bo;
1586 view->offset = buffer->offset + pCreateInfo->offset;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001587 view->surface_state =
1588 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -07001589 view->format = pCreateInfo->format;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001590
1591 format = anv_format_for_vk_format(pCreateInfo->format);
1592 /* This assumes RGBA float format. */
1593 uint32_t stride = 4;
1594 uint32_t num_elements = pCreateInfo->range / stride;
1595 struct GEN8_RENDER_SURFACE_STATE surface_state = {
1596 .SurfaceType = SURFTYPE_BUFFER,
1597 .SurfaceArray = false,
1598 .SurfaceFormat = format->format,
1599 .SurfaceVerticalAlignment = VALIGN4,
1600 .SurfaceHorizontalAlignment = HALIGN4,
1601 .TileMode = LINEAR,
1602 .VerticalLineStride = 0,
1603 .VerticalLineStrideOffset = 0,
1604 .SamplerL2BypassModeDisable = true,
1605 .RenderCacheReadWriteMode = WriteOnlyCache,
Kristian Høgsberg0997a7b2015-05-21 14:35:34 -07001606 .MemoryObjectControlState = GEN8_MOCS,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001607 .BaseMipLevel = 0,
1608 .SurfaceQPitch = 0,
1609 .Height = (num_elements >> 7) & 0x3fff,
1610 .Width = num_elements & 0x7f,
1611 .Depth = (num_elements >> 21) & 0x3f,
1612 .SurfacePitch = stride - 1,
1613 .MinimumArrayElement = 0,
1614 .NumberofMultisamples = MULTISAMPLECOUNT_1,
1615 .XOffset = 0,
1616 .YOffset = 0,
1617 .SurfaceMinLOD = 0,
1618 .MIPCountLOD = 0,
1619 .AuxiliarySurfaceMode = AUX_NONE,
1620 .RedClearColor = 0,
1621 .GreenClearColor = 0,
1622 .BlueClearColor = 0,
1623 .AlphaClearColor = 0,
1624 .ShaderChannelSelectRed = SCS_RED,
1625 .ShaderChannelSelectGreen = SCS_GREEN,
1626 .ShaderChannelSelectBlue = SCS_BLUE,
1627 .ShaderChannelSelectAlpha = SCS_ALPHA,
1628 .ResourceMinLOD = 0,
1629 /* FIXME: We assume that the image must be bound at this time. */
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -07001630 .SurfaceBaseAddress = { NULL, view->offset },
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001631 };
1632
1633 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
1634
1635 *pView = (VkImageView) view;
1636
1637 return VK_SUCCESS;
1638}
1639
1640// Sampler functions
1641
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001642VkResult anv_CreateSampler(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001643 VkDevice _device,
1644 const VkSamplerCreateInfo* pCreateInfo,
1645 VkSampler* pSampler)
1646{
1647 struct anv_device *device = (struct anv_device *) _device;
1648 struct anv_sampler *sampler;
1649
Kristian Høgsberg18acfa72015-05-13 13:53:01 -07001650 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001651
1652 sampler = anv_device_alloc(device, sizeof(*sampler), 8,
1653 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1654 if (!sampler)
1655 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1656
Kristian Høgsberga3fd1362015-05-12 21:44:59 -07001657 static const uint32_t vk_to_gen_tex_filter[] = {
1658 [VK_TEX_FILTER_NEAREST] = MAPFILTER_NEAREST,
1659 [VK_TEX_FILTER_LINEAR] = MAPFILTER_LINEAR
1660 };
1661
1662 static const uint32_t vk_to_gen_mipmap_mode[] = {
1663 [VK_TEX_MIPMAP_MODE_BASE] = MIPFILTER_NONE,
1664 [VK_TEX_MIPMAP_MODE_NEAREST] = MIPFILTER_NEAREST,
1665 [VK_TEX_MIPMAP_MODE_LINEAR] = MIPFILTER_LINEAR
1666 };
1667
1668 static const uint32_t vk_to_gen_tex_address[] = {
1669 [VK_TEX_ADDRESS_WRAP] = TCM_WRAP,
1670 [VK_TEX_ADDRESS_MIRROR] = TCM_MIRROR,
1671 [VK_TEX_ADDRESS_CLAMP] = TCM_CLAMP,
1672 [VK_TEX_ADDRESS_MIRROR_ONCE] = TCM_MIRROR_ONCE,
1673 [VK_TEX_ADDRESS_CLAMP_BORDER] = TCM_CLAMP_BORDER,
1674 };
1675
1676 static const uint32_t vk_to_gen_compare_op[] = {
1677 [VK_COMPARE_OP_NEVER] = PREFILTEROPNEVER,
1678 [VK_COMPARE_OP_LESS] = PREFILTEROPLESS,
1679 [VK_COMPARE_OP_EQUAL] = PREFILTEROPEQUAL,
1680 [VK_COMPARE_OP_LESS_EQUAL] = PREFILTEROPLEQUAL,
1681 [VK_COMPARE_OP_GREATER] = PREFILTEROPGREATER,
1682 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPNOTEQUAL,
1683 [VK_COMPARE_OP_GREATER_EQUAL] = PREFILTEROPGEQUAL,
1684 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPALWAYS,
1685 };
1686
1687 if (pCreateInfo->maxAnisotropy > 0)
1688 anv_finishme("missing support for anisotropic filtering");
1689
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001690 struct GEN8_SAMPLER_STATE sampler_state = {
Kristian Høgsberga3fd1362015-05-12 21:44:59 -07001691 .SamplerDisable = false,
1692 .TextureBorderColorMode = DX10OGL,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001693 .LODPreClampMode = 0,
1694 .BaseMipLevel = 0,
Kristian Høgsberga3fd1362015-05-12 21:44:59 -07001695 .MipModeFilter = vk_to_gen_mipmap_mode[pCreateInfo->mipMode],
1696 .MagModeFilter = vk_to_gen_tex_filter[pCreateInfo->magFilter],
1697 .MinModeFilter = vk_to_gen_tex_filter[pCreateInfo->minFilter],
1698 .TextureLODBias = pCreateInfo->mipLodBias * 256,
1699 .AnisotropicAlgorithm = EWAApproximation,
1700 .MinLOD = pCreateInfo->minLod * 256,
1701 .MaxLOD = pCreateInfo->maxLod * 256,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001702 .ChromaKeyEnable = 0,
1703 .ChromaKeyIndex = 0,
1704 .ChromaKeyMode = 0,
Kristian Høgsberga3fd1362015-05-12 21:44:59 -07001705 .ShadowFunction = vk_to_gen_compare_op[pCreateInfo->compareOp],
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001706 .CubeSurfaceControlMode = 0,
1707 .IndirectStatePointer = 0,
Kristian Høgsberga3fd1362015-05-12 21:44:59 -07001708 .LODClampMagnificationMode = MIPNONE,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001709 .MaximumAnisotropy = 0,
1710 .RAddressMinFilterRoundingEnable = 0,
1711 .RAddressMagFilterRoundingEnable = 0,
1712 .VAddressMinFilterRoundingEnable = 0,
1713 .VAddressMagFilterRoundingEnable = 0,
1714 .UAddressMinFilterRoundingEnable = 0,
1715 .UAddressMagFilterRoundingEnable = 0,
1716 .TrilinearFilterQuality = 0,
1717 .NonnormalizedCoordinateEnable = 0,
Kristian Høgsberga3fd1362015-05-12 21:44:59 -07001718 .TCXAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressU],
1719 .TCYAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressV],
1720 .TCZAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressW],
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001721 };
1722
1723 GEN8_SAMPLER_STATE_pack(NULL, sampler->state, &sampler_state);
1724
1725 *pSampler = (VkSampler) sampler;
1726
1727 return VK_SUCCESS;
1728}
1729
1730// Descriptor set functions
1731
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001732VkResult anv_CreateDescriptorSetLayout(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001733 VkDevice _device,
1734 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
1735 VkDescriptorSetLayout* pSetLayout)
1736{
1737 struct anv_device *device = (struct anv_device *) _device;
1738 struct anv_descriptor_set_layout *set_layout;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001739
1740 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
1741
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07001742 uint32_t sampler_count[VK_NUM_SHADER_STAGE] = { 0, };
1743 uint32_t surface_count[VK_NUM_SHADER_STAGE] = { 0, };
1744 uint32_t num_dynamic_buffers = 0;
1745 uint32_t count = 0;
1746 uint32_t s;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001747
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001748 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07001749 switch (pCreateInfo->pBinding[i].descriptorType) {
1750 case VK_DESCRIPTOR_TYPE_SAMPLER:
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07001751 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1752 sampler_count[s] += pCreateInfo->pBinding[i].count;
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07001753 break;
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07001754
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07001755 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07001756 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1757 sampler_count[s] += pCreateInfo->pBinding[i].count;
1758
1759 /* fall through */
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07001760
1761 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1762 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1763 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1764 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1765 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1766 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1767 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1768 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07001769 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1770 surface_count[s] += pCreateInfo->pBinding[i].count;
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07001771 break;
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07001772 default:
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07001773 break;
1774 }
1775
1776 count += pCreateInfo->pBinding[i].count;
1777 }
1778
1779 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1780 switch (pCreateInfo->pBinding[i].descriptorType) {
1781 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1782 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1783 num_dynamic_buffers++;
1784 break;
1785 default:
1786 break;
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07001787 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001788 }
1789
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07001790 uint32_t sampler_total = 0;
1791 uint32_t surface_total = 0;
1792 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
1793 sampler_total += sampler_count[s];
1794 surface_total += surface_count[s];
1795 }
1796
1797 size_t size = sizeof(*set_layout) +
1798 (sampler_total + surface_total) * sizeof(uint32_t);
1799 set_layout = anv_device_alloc(device, size, 8,
1800 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1801 if (!set_layout)
1802 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1803
1804 set_layout->num_dynamic_buffers = num_dynamic_buffers;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001805 set_layout->count = count;
1806
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07001807 uint32_t *p = set_layout->entries;
1808 uint32_t *sampler[VK_NUM_SHADER_STAGE];
1809 uint32_t *surface[VK_NUM_SHADER_STAGE];
1810 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
1811 set_layout->stage[s].surface_count = surface_count[s];
1812 set_layout->stage[s].surface_start = surface[s] = p;
1813 p += surface_count[s];
1814 set_layout->stage[s].sampler_count = sampler_count[s];
1815 set_layout->stage[s].sampler_start = sampler[s] = p;
1816 p += sampler_count[s];
1817 }
1818
1819 uint32_t descriptor = 0;
1820 for (uint32_t i = 0; i < pCreateInfo->count; i++) {
1821 switch (pCreateInfo->pBinding[i].descriptorType) {
1822 case VK_DESCRIPTOR_TYPE_SAMPLER:
1823 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1824 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].count; j++)
1825 *(sampler[s])++ = descriptor + j;
1826 break;
1827
1828 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1829 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1830 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].count; j++)
1831 *(sampler[s])++ = descriptor + j;
1832
1833 /* fallthrough */
1834
1835 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1836 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1837 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1838 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1839 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1840 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1841 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1842 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1843 for_each_bit(s, pCreateInfo->pBinding[i].stageFlags)
1844 for (uint32_t j = 0; j < pCreateInfo->pBinding[i].count; j++) {
1845 *(surface[s])++ = descriptor + j;
1846 }
1847 break;
1848 default:
1849 unreachable("");
1850 }
1851 descriptor += pCreateInfo->pBinding[i].count;
1852 }
1853
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001854 *pSetLayout = (VkDescriptorSetLayout) set_layout;
1855
1856 return VK_SUCCESS;
1857}
1858
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001859VkResult anv_BeginDescriptorPoolUpdate(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001860 VkDevice device,
1861 VkDescriptorUpdateMode updateMode)
1862{
Kristian Høgsberga9f21152015-05-17 18:38:34 -07001863 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001864}
1865
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001866VkResult anv_EndDescriptorPoolUpdate(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001867 VkDevice device,
1868 VkCmdBuffer cmd)
1869{
Kristian Høgsberga9f21152015-05-17 18:38:34 -07001870 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001871}
1872
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001873VkResult anv_CreateDescriptorPool(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001874 VkDevice device,
1875 VkDescriptorPoolUsage poolUsage,
1876 uint32_t maxSets,
1877 const VkDescriptorPoolCreateInfo* pCreateInfo,
1878 VkDescriptorPool* pDescriptorPool)
1879{
Kristian Høgsberga9f21152015-05-17 18:38:34 -07001880 *pDescriptorPool = 1;
1881
1882 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001883}
1884
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001885VkResult anv_ResetDescriptorPool(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001886 VkDevice device,
1887 VkDescriptorPool descriptorPool)
1888{
Kristian Høgsberga9f21152015-05-17 18:38:34 -07001889 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001890}
1891
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001892VkResult anv_AllocDescriptorSets(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001893 VkDevice _device,
1894 VkDescriptorPool descriptorPool,
1895 VkDescriptorSetUsage setUsage,
1896 uint32_t count,
1897 const VkDescriptorSetLayout* pSetLayouts,
1898 VkDescriptorSet* pDescriptorSets,
1899 uint32_t* pCount)
1900{
1901 struct anv_device *device = (struct anv_device *) _device;
1902 const struct anv_descriptor_set_layout *layout;
1903 struct anv_descriptor_set *set;
1904 size_t size;
1905
1906 for (uint32_t i = 0; i < count; i++) {
1907 layout = (struct anv_descriptor_set_layout *) pSetLayouts[i];
Kristian Høgsberga77229c2015-05-13 11:49:30 -07001908 size = sizeof(*set) + layout->count * sizeof(set->descriptors[0]);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001909 set = anv_device_alloc(device, size, 8,
1910 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
1911 if (!set) {
1912 *pCount = i;
1913 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1914 }
1915
Jason Ekstrand0a547512015-05-21 16:33:04 -07001916 /* Descriptor sets may not be 100% filled out so we need to memset to
1917 * ensure that we can properly detect and handle holes.
1918 */
1919 memset(set, 0, size);
1920
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001921 pDescriptorSets[i] = (VkDescriptorSet) set;
1922 }
1923
1924 *pCount = count;
1925
Kristian Høgsbergb4b3bd12015-05-17 18:39:12 -07001926 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001927}
1928
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001929void anv_ClearDescriptorSets(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001930 VkDevice device,
1931 VkDescriptorPool descriptorPool,
1932 uint32_t count,
1933 const VkDescriptorSet* pDescriptorSets)
1934{
1935}
1936
Kristian Høgsberg454345d2015-05-17 16:33:48 -07001937void anv_UpdateDescriptors(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001938 VkDevice _device,
1939 VkDescriptorSet descriptorSet,
1940 uint32_t updateCount,
1941 const void** ppUpdateArray)
1942{
1943 struct anv_descriptor_set *set = (struct anv_descriptor_set *) descriptorSet;
1944 VkUpdateSamplers *update_samplers;
1945 VkUpdateSamplerTextures *update_sampler_textures;
1946 VkUpdateImages *update_images;
1947 VkUpdateBuffers *update_buffers;
1948 VkUpdateAsCopy *update_as_copy;
1949
1950 for (uint32_t i = 0; i < updateCount; i++) {
1951 const struct anv_common *common = ppUpdateArray[i];
1952
1953 switch (common->sType) {
1954 case VK_STRUCTURE_TYPE_UPDATE_SAMPLERS:
1955 update_samplers = (VkUpdateSamplers *) common;
1956
1957 for (uint32_t j = 0; j < update_samplers->count; j++) {
Kristian Høgsberg4f9eaf72015-05-13 14:02:35 -07001958 set->descriptors[update_samplers->binding + j].sampler =
1959 (struct anv_sampler *) update_samplers->pSamplers[j];
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001960 }
1961 break;
1962
1963 case VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES:
1964 /* FIXME: Shouldn't this be *_UPDATE_SAMPLER_IMAGES? */
1965 update_sampler_textures = (VkUpdateSamplerTextures *) common;
1966
1967 for (uint32_t j = 0; j < update_sampler_textures->count; j++) {
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -07001968 set->descriptors[update_sampler_textures->binding + j].view =
1969 (struct anv_surface_view *)
Kristian Høgsberg4f9eaf72015-05-13 14:02:35 -07001970 update_sampler_textures->pSamplerImageViews[j].pImageView->view;
1971 set->descriptors[update_sampler_textures->binding + j].sampler =
1972 (struct anv_sampler *)
1973 update_sampler_textures->pSamplerImageViews[j].sampler;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001974 }
1975 break;
1976
1977 case VK_STRUCTURE_TYPE_UPDATE_IMAGES:
1978 update_images = (VkUpdateImages *) common;
1979
1980 for (uint32_t j = 0; j < update_images->count; j++) {
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -07001981 set->descriptors[update_images->binding + j].view =
1982 (struct anv_surface_view *) update_images->pImageViews[j].view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001983 }
1984 break;
1985
1986 case VK_STRUCTURE_TYPE_UPDATE_BUFFERS:
1987 update_buffers = (VkUpdateBuffers *) common;
1988
1989 for (uint32_t j = 0; j < update_buffers->count; j++) {
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -07001990 set->descriptors[update_buffers->binding + j].view =
1991 (struct anv_surface_view *) update_buffers->pBufferViews[j].view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07001992 }
1993 /* FIXME: descriptor arrays? */
1994 break;
1995
1996 case VK_STRUCTURE_TYPE_UPDATE_AS_COPY:
1997 update_as_copy = (VkUpdateAsCopy *) common;
1998 (void) update_as_copy;
1999 break;
2000
2001 default:
2002 break;
2003 }
2004 }
2005}
2006
2007// State object functions
2008
2009static inline int64_t
2010clamp_int64(int64_t x, int64_t min, int64_t max)
2011{
2012 if (x < min)
2013 return min;
2014 else if (x < max)
2015 return x;
2016 else
2017 return max;
2018}
2019
Jason Ekstrand57153da2015-05-22 15:15:08 -07002020static void
2021anv_dynamic_vp_state_destroy(struct anv_device *device,
2022 struct anv_object *object,
2023 VkObjectType obj_type)
2024{
2025 struct anv_dynamic_vp_state *state = (void *)object;
2026
2027 assert(obj_type == VK_OBJECT_TYPE_DYNAMIC_VP_STATE);
2028
2029 anv_state_pool_free(&device->dynamic_state_pool, state->sf_clip_vp);
2030 anv_state_pool_free(&device->dynamic_state_pool, state->cc_vp);
2031 anv_state_pool_free(&device->dynamic_state_pool, state->scissor);
2032
2033 anv_device_free(device, state);
2034}
2035
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002036VkResult anv_CreateDynamicViewportState(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002037 VkDevice _device,
2038 const VkDynamicVpStateCreateInfo* pCreateInfo,
2039 VkDynamicVpState* pState)
2040{
2041 struct anv_device *device = (struct anv_device *) _device;
2042 struct anv_dynamic_vp_state *state;
2043
2044 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO);
2045
2046 state = anv_device_alloc(device, sizeof(*state), 8,
2047 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2048 if (state == NULL)
2049 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2050
Jason Ekstrand57153da2015-05-22 15:15:08 -07002051 state->base.destructor = anv_dynamic_vp_state_destroy;
2052
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002053 unsigned count = pCreateInfo->viewportAndScissorCount;
Kristian Høgsberg0a775e12015-05-13 15:34:34 -07002054 state->sf_clip_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002055 count * 64, 64);
Kristian Høgsberg0a775e12015-05-13 15:34:34 -07002056 state->cc_vp = anv_state_pool_alloc(&device->dynamic_state_pool,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002057 count * 8, 32);
Kristian Høgsberg0a775e12015-05-13 15:34:34 -07002058 state->scissor = anv_state_pool_alloc(&device->dynamic_state_pool,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002059 count * 32, 32);
2060
2061 for (uint32_t i = 0; i < pCreateInfo->viewportAndScissorCount; i++) {
2062 const VkViewport *vp = &pCreateInfo->pViewports[i];
2063 const VkRect *s = &pCreateInfo->pScissors[i];
2064
2065 struct GEN8_SF_CLIP_VIEWPORT sf_clip_viewport = {
2066 .ViewportMatrixElementm00 = vp->width / 2,
2067 .ViewportMatrixElementm11 = vp->height / 2,
2068 .ViewportMatrixElementm22 = (vp->maxDepth - vp->minDepth) / 2,
2069 .ViewportMatrixElementm30 = vp->originX + vp->width / 2,
2070 .ViewportMatrixElementm31 = vp->originY + vp->height / 2,
2071 .ViewportMatrixElementm32 = (vp->maxDepth + vp->minDepth) / 2,
2072 .XMinClipGuardband = -1.0f,
2073 .XMaxClipGuardband = 1.0f,
2074 .YMinClipGuardband = -1.0f,
2075 .YMaxClipGuardband = 1.0f,
2076 .XMinViewPort = vp->originX,
2077 .XMaxViewPort = vp->originX + vp->width - 1,
2078 .YMinViewPort = vp->originY,
2079 .YMaxViewPort = vp->originY + vp->height - 1,
2080 };
2081
2082 struct GEN8_CC_VIEWPORT cc_viewport = {
2083 .MinimumDepth = vp->minDepth,
2084 .MaximumDepth = vp->maxDepth
2085 };
2086
2087 /* Since xmax and ymax are inclusive, we have to have xmax < xmin or
2088 * ymax < ymin for empty clips. In case clip x, y, width height are all
2089 * 0, the clamps below produce 0 for xmin, ymin, xmax, ymax, which isn't
2090 * what we want. Just special case empty clips and produce a canonical
2091 * empty clip. */
2092 static const struct GEN8_SCISSOR_RECT empty_scissor = {
2093 .ScissorRectangleYMin = 1,
2094 .ScissorRectangleXMin = 1,
2095 .ScissorRectangleYMax = 0,
2096 .ScissorRectangleXMax = 0
2097 };
2098
2099 const int max = 0xffff;
2100 struct GEN8_SCISSOR_RECT scissor = {
2101 /* Do this math using int64_t so overflow gets clamped correctly. */
2102 .ScissorRectangleYMin = clamp_int64(s->offset.y, 0, max),
2103 .ScissorRectangleXMin = clamp_int64(s->offset.x, 0, max),
2104 .ScissorRectangleYMax = clamp_int64((uint64_t) s->offset.y + s->extent.height - 1, 0, max),
2105 .ScissorRectangleXMax = clamp_int64((uint64_t) s->offset.x + s->extent.width - 1, 0, max)
2106 };
2107
2108 GEN8_SF_CLIP_VIEWPORT_pack(NULL, state->sf_clip_vp.map + i * 64, &sf_clip_viewport);
2109 GEN8_CC_VIEWPORT_pack(NULL, state->cc_vp.map + i * 32, &cc_viewport);
2110
2111 if (s->extent.width <= 0 || s->extent.height <= 0) {
2112 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &empty_scissor);
2113 } else {
2114 GEN8_SCISSOR_RECT_pack(NULL, state->scissor.map + i * 32, &scissor);
2115 }
2116 }
2117
2118 *pState = (VkDynamicVpState) state;
2119
2120 return VK_SUCCESS;
2121}
2122
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002123VkResult anv_CreateDynamicRasterState(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002124 VkDevice _device,
2125 const VkDynamicRsStateCreateInfo* pCreateInfo,
2126 VkDynamicRsState* pState)
2127{
2128 struct anv_device *device = (struct anv_device *) _device;
2129 struct anv_dynamic_rs_state *state;
2130
2131 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO);
2132
2133 state = anv_device_alloc(device, sizeof(*state), 8,
2134 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2135 if (state == NULL)
2136 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2137
2138 /* Missing these:
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002139 * float pointFadeThreshold;
2140 * // optional (GL45) - Size of point fade threshold
2141 */
2142
2143 struct GEN8_3DSTATE_SF sf = {
2144 GEN8_3DSTATE_SF_header,
2145 .LineWidth = pCreateInfo->lineWidth,
2146 .PointWidth = pCreateInfo->pointSize,
2147 };
2148
2149 GEN8_3DSTATE_SF_pack(NULL, state->state_sf, &sf);
2150
Kristian Høgsberg99883772015-05-26 09:40:10 -07002151 bool enable_bias = pCreateInfo->depthBias != 0.0f ||
2152 pCreateInfo->slopeScaledDepthBias != 0.0f;
2153 struct GEN8_3DSTATE_RASTER raster = {
2154 .GlobalDepthOffsetEnableSolid = enable_bias,
2155 .GlobalDepthOffsetEnableWireframe = enable_bias,
2156 .GlobalDepthOffsetEnablePoint = enable_bias,
2157 .GlobalDepthOffsetConstant = pCreateInfo->depthBias,
2158 .GlobalDepthOffsetScale = pCreateInfo->slopeScaledDepthBias,
2159 .GlobalDepthOffsetClamp = pCreateInfo->depthBiasClamp
2160 };
2161
2162 GEN8_3DSTATE_RASTER_pack(NULL, state->state_raster, &raster);
2163
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002164 *pState = (VkDynamicRsState) state;
2165
2166 return VK_SUCCESS;
2167}
2168
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002169VkResult anv_CreateDynamicColorBlendState(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002170 VkDevice _device,
2171 const VkDynamicCbStateCreateInfo* pCreateInfo,
2172 VkDynamicCbState* pState)
2173{
2174 struct anv_device *device = (struct anv_device *) _device;
2175 struct anv_dynamic_cb_state *state;
2176
2177 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO);
2178
2179 state = anv_device_alloc(device, sizeof(*state), 8,
2180 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2181 if (state == NULL)
2182 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2183
Kristian Høgsbergb29f4422015-05-26 11:22:12 -07002184 struct GEN8_COLOR_CALC_STATE color_calc_state = {
2185 .BlendConstantColorRed = pCreateInfo->blendConst[0],
2186 .BlendConstantColorGreen = pCreateInfo->blendConst[1],
2187 .BlendConstantColorBlue = pCreateInfo->blendConst[2],
2188 .BlendConstantColorAlpha = pCreateInfo->blendConst[3]
2189 };
2190
2191 GEN8_COLOR_CALC_STATE_pack(NULL, state->state_color_calc, &color_calc_state);
2192
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002193 *pState = (VkDynamicCbState) state;
2194
2195 return VK_SUCCESS;
2196}
2197
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002198VkResult anv_CreateDynamicDepthStencilState(
Kristian Høgsbergcbe7ed42015-05-24 21:19:26 -07002199 VkDevice _device,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002200 const VkDynamicDsStateCreateInfo* pCreateInfo,
2201 VkDynamicDsState* pState)
2202{
Kristian Høgsbergcbe7ed42015-05-24 21:19:26 -07002203 struct anv_device *device = (struct anv_device *) _device;
2204 struct anv_dynamic_ds_state *state;
2205
2206 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO);
2207
2208 state = anv_device_alloc(device, sizeof(*state), 8,
2209 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2210 if (state == NULL)
2211 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2212
2213 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil = {
2214 GEN8_3DSTATE_WM_DEPTH_STENCIL_header,
2215
Kristian Høgsbergcbe7ed42015-05-24 21:19:26 -07002216 /* Is this what we need to do? */
2217 .StencilBufferWriteEnable = pCreateInfo->stencilWriteMask != 0,
2218
2219 .StencilTestMask = pCreateInfo->stencilReadMask,
2220 .StencilWriteMask = pCreateInfo->stencilWriteMask,
2221
2222 .BackfaceStencilTestMask = pCreateInfo->stencilReadMask,
2223 .BackfaceStencilWriteMask = pCreateInfo->stencilWriteMask,
2224 };
2225
2226 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL, state->state_wm_depth_stencil,
2227 &wm_depth_stencil);
2228
Kristian Høgsbergb29f4422015-05-26 11:22:12 -07002229 struct GEN8_COLOR_CALC_STATE color_calc_state = {
2230 .StencilReferenceValue = pCreateInfo->stencilFrontRef,
2231 .BackFaceStencilReferenceValue = pCreateInfo->stencilBackRef
2232 };
2233
2234 GEN8_COLOR_CALC_STATE_pack(NULL, state->state_color_calc, &color_calc_state);
2235
Kristian Høgsbergcbe7ed42015-05-24 21:19:26 -07002236 *pState = (VkDynamicDsState) state;
2237
2238 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002239}
2240
2241// Command buffer functions
2242
Jason Ekstrand57153da2015-05-22 15:15:08 -07002243static void
2244anv_cmd_buffer_destroy(struct anv_device *device,
2245 struct anv_object *object,
2246 VkObjectType obj_type)
2247{
2248 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) object;
2249
2250 assert(obj_type == VK_OBJECT_TYPE_COMMAND_BUFFER);
2251
Jason Ekstrand1c635752015-05-25 18:27:43 -07002252 anv_bo_pool_free(&device->batch_bo_pool, &cmd_buffer->surface_bo);
Jason Ekstrand403266b2015-05-25 17:38:15 -07002253 anv_reloc_list_finish(&cmd_buffer->surface_relocs, device);
Jason Ekstrand57153da2015-05-22 15:15:08 -07002254 anv_state_stream_finish(&cmd_buffer->surface_state_stream);
2255 anv_state_stream_finish(&cmd_buffer->dynamic_state_stream);
2256 anv_state_stream_finish(&cmd_buffer->binding_table_state_stream);
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002257 anv_reloc_list_finish(&cmd_buffer->batch.relocs, device);
Jason Ekstrand57153da2015-05-22 15:15:08 -07002258 anv_device_free(device, cmd_buffer->exec2_objects);
2259 anv_device_free(device, cmd_buffer->exec2_bos);
2260 anv_device_free(device, cmd_buffer);
2261}
2262
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002263static VkResult
2264anv_cmd_buffer_chain_batch(struct anv_batch *batch, void *_data)
2265{
2266 struct anv_cmd_buffer *cmd_buffer = _data;
2267 struct anv_batch_bo *new_bbo, *old_bbo = cmd_buffer->last_batch_bo;
2268
2269 VkResult result = anv_batch_bo_create(cmd_buffer->device, &new_bbo);
2270 if (result != VK_SUCCESS)
2271 return result;
2272
2273 struct GEN8_MI_BATCH_BUFFER_START cmd = {
2274 GEN8_MI_BATCH_BUFFER_START_header,
2275 ._2ndLevelBatchBuffer = _1stlevelbatch,
2276 .AddressSpaceIndicator = ASI_PPGTT,
2277 .BatchBufferStartAddress = { &new_bbo->bo, 0 },
2278 };
2279 GEN8_MI_BATCH_BUFFER_START_pack(batch, batch->next, &cmd);
2280
2281 batch->next += GEN8_MI_BATCH_BUFFER_START_length * 4;
2282
2283 /* Pad out to a 2-dword aligned boundary with zeros */
2284 if ((uintptr_t)batch->next % 8 != 0)
2285 *(uint32_t *)batch->next = 0;
2286
2287 anv_batch_bo_finish(cmd_buffer->last_batch_bo, batch);
2288
2289 new_bbo->prev_batch_bo = old_bbo;
2290 cmd_buffer->last_batch_bo = new_bbo;
2291
2292 anv_batch_bo_start(new_bbo, batch, GEN8_MI_BATCH_BUFFER_START_length * 4);
2293
2294 return VK_SUCCESS;
2295}
2296
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002297VkResult anv_CreateCommandBuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002298 VkDevice _device,
2299 const VkCmdBufferCreateInfo* pCreateInfo,
2300 VkCmdBuffer* pCmdBuffer)
2301{
2302 struct anv_device *device = (struct anv_device *) _device;
2303 struct anv_cmd_buffer *cmd_buffer;
2304 VkResult result;
2305
2306 cmd_buffer = anv_device_alloc(device, sizeof(*cmd_buffer), 8,
2307 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
2308 if (cmd_buffer == NULL)
2309 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2310
Jason Ekstrand57153da2015-05-22 15:15:08 -07002311 cmd_buffer->base.destructor = anv_cmd_buffer_destroy;
2312
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002313 cmd_buffer->device = device;
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07002314 cmd_buffer->rs_state = NULL;
2315 cmd_buffer->vp_state = NULL;
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002316 memset(&cmd_buffer->default_bindings, 0, sizeof(cmd_buffer->default_bindings));
2317 cmd_buffer->bindings = &cmd_buffer->default_bindings;
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07002318
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002319 result = anv_batch_bo_create(device, &cmd_buffer->last_batch_bo);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002320 if (result != VK_SUCCESS)
2321 goto fail;
2322
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002323 result = anv_reloc_list_init(&cmd_buffer->batch.relocs, device);
2324 if (result != VK_SUCCESS)
2325 goto fail_batch_bo;
2326
2327 cmd_buffer->batch.device = device;
2328 cmd_buffer->batch.extend_cb = anv_cmd_buffer_chain_batch;
2329 cmd_buffer->batch.user_data = cmd_buffer;
2330
2331 anv_batch_bo_start(cmd_buffer->last_batch_bo, &cmd_buffer->batch,
2332 GEN8_MI_BATCH_BUFFER_START_length * 4);
2333
Jason Ekstrand1c635752015-05-25 18:27:43 -07002334 result = anv_bo_pool_alloc(&device->batch_bo_pool, &cmd_buffer->surface_bo);
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002335 if (result != VK_SUCCESS)
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002336 goto fail_batch_relocs;
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002337
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002338 /* Start surface_next at 1 so surface offset 0 is invalid. */
2339 cmd_buffer->surface_next = 1;
Jason Ekstrand403266b2015-05-25 17:38:15 -07002340 anv_reloc_list_init(&cmd_buffer->surface_relocs, device);
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002341
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002342 cmd_buffer->exec2_objects = NULL;
2343 cmd_buffer->exec2_bos = NULL;
2344 cmd_buffer->exec2_array_length = 0;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002345
Jason Ekstrand923691c2015-05-18 19:56:32 -07002346 anv_state_stream_init(&cmd_buffer->binding_table_state_stream,
2347 &device->binding_table_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002348 anv_state_stream_init(&cmd_buffer->surface_state_stream,
2349 &device->surface_state_block_pool);
Kristian Høgsberga1ec7892015-05-13 13:51:08 -07002350 anv_state_stream_init(&cmd_buffer->dynamic_state_stream,
Kristian Høgsberg0a775e12015-05-13 15:34:34 -07002351 &device->dynamic_state_block_pool);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002352
2353 cmd_buffer->dirty = 0;
2354 cmd_buffer->vb_dirty = 0;
Jason Ekstrandae8c93e2015-05-25 17:08:11 -07002355 cmd_buffer->pipeline = NULL;
Kristian Høgsberg Kristensen5a317ef2015-05-27 21:45:23 -07002356 cmd_buffer->vp_state = NULL;
2357 cmd_buffer->rs_state = NULL;
2358 cmd_buffer->ds_state = NULL;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002359
2360 *pCmdBuffer = (VkCmdBuffer) cmd_buffer;
2361
2362 return VK_SUCCESS;
2363
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002364 fail_batch_relocs:
2365 anv_reloc_list_finish(&cmd_buffer->batch.relocs, device);
2366 fail_batch_bo:
2367 anv_batch_bo_destroy(cmd_buffer->last_batch_bo, device);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002368 fail:
2369 anv_device_free(device, cmd_buffer);
2370
2371 return result;
2372}
2373
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002374VkResult anv_BeginCommandBuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002375 VkCmdBuffer cmdBuffer,
2376 const VkCmdBufferBeginInfo* pBeginInfo)
2377{
2378 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2379 struct anv_device *device = cmd_buffer->device;
2380
2381 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPELINE_SELECT,
2382 .PipelineSelection = _3D);
2383 anv_batch_emit(&cmd_buffer->batch, GEN8_STATE_SIP);
2384
2385 anv_batch_emit(&cmd_buffer->batch, GEN8_STATE_BASE_ADDRESS,
2386 .GeneralStateBaseAddress = { NULL, 0 },
Kristian Høgsberg0997a7b2015-05-21 14:35:34 -07002387 .GeneralStateMemoryObjectControlState = GEN8_MOCS,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002388 .GeneralStateBaseAddressModifyEnable = true,
2389 .GeneralStateBufferSize = 0xfffff,
2390 .GeneralStateBufferSizeModifyEnable = true,
2391
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002392 .SurfaceStateBaseAddress = { &cmd_buffer->surface_bo, 0 },
Kristian Høgsberg0997a7b2015-05-21 14:35:34 -07002393 .SurfaceStateMemoryObjectControlState = GEN8_MOCS,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002394 .SurfaceStateBaseAddressModifyEnable = true,
2395
Kristian Høgsberg0a775e12015-05-13 15:34:34 -07002396 .DynamicStateBaseAddress = { &device->dynamic_state_block_pool.bo, 0 },
Kristian Høgsberg0997a7b2015-05-21 14:35:34 -07002397 .DynamicStateMemoryObjectControlState = GEN8_MOCS,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002398 .DynamicStateBaseAddressModifyEnable = true,
2399 .DynamicStateBufferSize = 0xfffff,
2400 .DynamicStateBufferSizeModifyEnable = true,
2401
2402 .IndirectObjectBaseAddress = { NULL, 0 },
Kristian Høgsberg0997a7b2015-05-21 14:35:34 -07002403 .IndirectObjectMemoryObjectControlState = GEN8_MOCS,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002404 .IndirectObjectBaseAddressModifyEnable = true,
2405 .IndirectObjectBufferSize = 0xfffff,
2406 .IndirectObjectBufferSizeModifyEnable = true,
2407
2408 .InstructionBaseAddress = { &device->instruction_block_pool.bo, 0 },
Kristian Høgsberg0997a7b2015-05-21 14:35:34 -07002409 .InstructionMemoryObjectControlState = GEN8_MOCS,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002410 .InstructionBaseAddressModifyEnable = true,
2411 .InstructionBufferSize = 0xfffff,
2412 .InstructionBuffersizeModifyEnable = true);
2413
2414 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VF_STATISTICS,
2415 .StatisticsEnable = true);
2416 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_HS, .Enable = false);
2417 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_TE, .TEEnable = false);
2418 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DS, .FunctionEnable = false);
2419 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_STREAMOUT, .SOFunctionEnable = false);
2420
2421 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS,
2422 .ConstantBufferOffset = 0,
2423 .ConstantBufferSize = 4);
2424 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS,
2425 .ConstantBufferOffset = 4,
2426 .ConstantBufferSize = 4);
2427 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS,
2428 .ConstantBufferOffset = 8,
2429 .ConstantBufferSize = 4);
2430
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002431 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_WM_CHROMAKEY,
2432 .ChromaKeyKillEnable = false);
2433 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_SBE_SWIZ);
2434 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_AA_LINE_PARAMETERS);
2435
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002436 return VK_SUCCESS;
2437}
2438
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002439static VkResult
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002440anv_cmd_buffer_add_bo(struct anv_cmd_buffer *cmd_buffer,
2441 struct anv_bo *bo, struct anv_reloc_list *list)
2442{
2443 struct drm_i915_gem_exec_object2 *obj;
2444
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002445 if (bo->index < cmd_buffer->bo_count &&
2446 cmd_buffer->exec2_bos[bo->index] == bo)
2447 return VK_SUCCESS;
2448
2449 if (cmd_buffer->bo_count >= cmd_buffer->exec2_array_length) {
2450 uint32_t new_len = cmd_buffer->exec2_objects ?
2451 cmd_buffer->exec2_array_length * 2 : 64;
2452
2453 struct drm_i915_gem_exec_object2 *new_objects =
2454 anv_device_alloc(cmd_buffer->device, new_len * sizeof(*new_objects),
2455 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL);
2456 if (new_objects == NULL)
2457 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2458
2459 struct anv_bo **new_bos =
2460 anv_device_alloc(cmd_buffer->device, new_len * sizeof(*new_bos),
2461 8, VK_SYSTEM_ALLOC_TYPE_INTERNAL);
2462 if (new_objects == NULL) {
2463 anv_device_free(cmd_buffer->device, new_objects);
2464 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2465 }
2466
2467 if (cmd_buffer->exec2_objects) {
2468 memcpy(new_objects, cmd_buffer->exec2_objects,
2469 cmd_buffer->bo_count * sizeof(*new_objects));
2470 memcpy(new_bos, cmd_buffer->exec2_bos,
2471 cmd_buffer->bo_count * sizeof(*new_bos));
2472 }
2473
2474 cmd_buffer->exec2_objects = new_objects;
2475 cmd_buffer->exec2_bos = new_bos;
2476 cmd_buffer->exec2_array_length = new_len;
2477 }
2478
2479 assert(cmd_buffer->bo_count < cmd_buffer->exec2_array_length);
2480
2481 bo->index = cmd_buffer->bo_count++;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002482 obj = &cmd_buffer->exec2_objects[bo->index];
2483 cmd_buffer->exec2_bos[bo->index] = bo;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002484
2485 obj->handle = bo->gem_handle;
2486 obj->relocation_count = 0;
2487 obj->relocs_ptr = 0;
2488 obj->alignment = 0;
2489 obj->offset = bo->offset;
2490 obj->flags = 0;
2491 obj->rsvd1 = 0;
2492 obj->rsvd2 = 0;
2493
2494 if (list) {
2495 obj->relocation_count = list->num_relocs;
2496 obj->relocs_ptr = (uintptr_t) list->relocs;
2497 }
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002498
2499 return VK_SUCCESS;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002500}
2501
2502static void
2503anv_cmd_buffer_add_validate_bos(struct anv_cmd_buffer *cmd_buffer,
2504 struct anv_reloc_list *list)
2505{
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002506 for (size_t i = 0; i < list->num_relocs; i++)
2507 anv_cmd_buffer_add_bo(cmd_buffer, list->reloc_bos[i], NULL);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002508}
2509
2510static void
2511anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
2512 struct anv_reloc_list *list)
2513{
2514 struct anv_bo *bo;
2515
2516 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
2517 * struct drm_i915_gem_exec_object2 against the bos current offset and if
2518 * all bos haven't moved it will skip relocation processing alltogether.
2519 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
2520 * value of offset so we can set it either way. For that to work we need
2521 * to make sure all relocs use the same presumed offset.
2522 */
2523
2524 for (size_t i = 0; i < list->num_relocs; i++) {
2525 bo = list->reloc_bos[i];
2526 if (bo->offset != list->relocs[i].presumed_offset)
2527 cmd_buffer->need_reloc = true;
2528
2529 list->relocs[i].target_handle = bo->index;
2530 }
2531}
2532
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002533VkResult anv_EndCommandBuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002534 VkCmdBuffer cmdBuffer)
2535{
2536 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2537 struct anv_device *device = cmd_buffer->device;
2538 struct anv_batch *batch = &cmd_buffer->batch;
2539
2540 anv_batch_emit(batch, GEN8_MI_BATCH_BUFFER_END);
2541
2542 /* Round batch up to an even number of dwords. */
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002543 if ((batch->next - batch->start) & 4)
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002544 anv_batch_emit(batch, GEN8_MI_NOOP);
2545
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002546 anv_batch_bo_finish(cmd_buffer->last_batch_bo, &cmd_buffer->batch);
2547
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002548 cmd_buffer->bo_count = 0;
2549 cmd_buffer->need_reloc = false;
2550
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002551 /* Find the first batch bo in the list */
2552 struct anv_batch_bo *batch_bo = cmd_buffer->last_batch_bo;
2553 while (batch_bo->prev_batch_bo)
2554 batch_bo = batch_bo->prev_batch_bo;
2555
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002556 /* Lock for access to bo->index. */
2557 pthread_mutex_lock(&device->mutex);
2558
2559 /* Add block pool bos first so we can add them with their relocs. */
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002560 anv_cmd_buffer_add_bo(cmd_buffer, &cmd_buffer->surface_bo,
2561 &cmd_buffer->surface_relocs);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002562
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002563 anv_cmd_buffer_add_validate_bos(cmd_buffer, &cmd_buffer->surface_relocs);
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002564 anv_cmd_buffer_add_validate_bos(cmd_buffer, &batch->relocs);
2565 anv_cmd_buffer_add_bo(cmd_buffer, &batch_bo->bo, &batch->relocs);
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002566 anv_cmd_buffer_process_relocs(cmd_buffer, &cmd_buffer->surface_relocs);
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002567 anv_cmd_buffer_process_relocs(cmd_buffer, &batch->relocs);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002568
2569 cmd_buffer->execbuf.buffers_ptr = (uintptr_t) cmd_buffer->exec2_objects;
2570 cmd_buffer->execbuf.buffer_count = cmd_buffer->bo_count;
2571 cmd_buffer->execbuf.batch_start_offset = 0;
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002572 cmd_buffer->execbuf.batch_len = batch->next - batch->start;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002573 cmd_buffer->execbuf.cliprects_ptr = 0;
2574 cmd_buffer->execbuf.num_cliprects = 0;
2575 cmd_buffer->execbuf.DR1 = 0;
2576 cmd_buffer->execbuf.DR4 = 0;
2577
2578 cmd_buffer->execbuf.flags = I915_EXEC_HANDLE_LUT;
2579 if (!cmd_buffer->need_reloc)
2580 cmd_buffer->execbuf.flags |= I915_EXEC_NO_RELOC;
2581 cmd_buffer->execbuf.flags |= I915_EXEC_RENDER;
2582 cmd_buffer->execbuf.rsvd1 = device->context_id;
2583 cmd_buffer->execbuf.rsvd2 = 0;
2584
2585 pthread_mutex_unlock(&device->mutex);
2586
2587 return VK_SUCCESS;
2588}
2589
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002590VkResult anv_ResetCommandBuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002591 VkCmdBuffer cmdBuffer)
2592{
2593 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2594
Jason Ekstrandda8f1482015-05-27 11:42:55 -07002595 /* Delete all but the first batch bo */
2596 while (cmd_buffer->last_batch_bo->prev_batch_bo) {
2597 struct anv_batch_bo *prev = cmd_buffer->last_batch_bo->prev_batch_bo;
2598 anv_batch_bo_destroy(cmd_buffer->last_batch_bo, cmd_buffer->device);
2599 cmd_buffer->last_batch_bo = prev;
2600 }
2601 assert(cmd_buffer->last_batch_bo->prev_batch_bo == NULL);
2602
2603 cmd_buffer->batch.relocs.num_relocs = 0;
2604 anv_batch_bo_start(cmd_buffer->last_batch_bo, &cmd_buffer->batch,
2605 GEN8_MI_BATCH_BUFFER_START_length * 4);
2606
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002607 cmd_buffer->surface_next = 0;
2608 cmd_buffer->surface_relocs.num_relocs = 0;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002609
2610 return VK_SUCCESS;
2611}
2612
2613// Command buffer building functions
2614
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002615void anv_CmdBindPipeline(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002616 VkCmdBuffer cmdBuffer,
2617 VkPipelineBindPoint pipelineBindPoint,
2618 VkPipeline _pipeline)
2619{
2620 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
Jason Ekstrand0f0b5ae2015-05-21 16:49:55 -07002621 struct anv_pipeline *pipeline = (struct anv_pipeline *) _pipeline;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002622
Jason Ekstrand0f0b5ae2015-05-21 16:49:55 -07002623 cmd_buffer->pipeline = pipeline;
2624 cmd_buffer->vb_dirty |= pipeline->vb_used;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002625 cmd_buffer->dirty |= ANV_CMD_BUFFER_PIPELINE_DIRTY;
2626}
2627
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002628void anv_CmdBindDynamicStateObject(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002629 VkCmdBuffer cmdBuffer,
2630 VkStateBindPoint stateBindPoint,
2631 VkDynamicStateObject dynamicState)
2632{
2633 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2634 struct anv_dynamic_vp_state *vp_state;
2635
2636 switch (stateBindPoint) {
2637 case VK_STATE_BIND_POINT_VIEWPORT:
2638 vp_state = (struct anv_dynamic_vp_state *) dynamicState;
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07002639 /* We emit state immediately, but set cmd_buffer->vp_state to indicate
2640 * that vp state has been set in this command buffer. */
2641 cmd_buffer->vp_state = vp_state;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002642 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_SCISSOR_STATE_POINTERS,
2643 .ScissorRectPointer = vp_state->scissor.offset);
2644 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_CC,
2645 .CCViewportPointer = vp_state->cc_vp.offset);
2646 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP,
2647 .SFClipViewportPointer = vp_state->sf_clip_vp.offset);
2648 break;
2649 case VK_STATE_BIND_POINT_RASTER:
2650 cmd_buffer->rs_state = (struct anv_dynamic_rs_state *) dynamicState;
2651 cmd_buffer->dirty |= ANV_CMD_BUFFER_RS_DIRTY;
2652 break;
2653 case VK_STATE_BIND_POINT_COLOR_BLEND:
Kristian Høgsberga1d30f82015-05-26 17:12:18 -07002654 cmd_buffer->cb_state = (struct anv_dynamic_cb_state *) dynamicState;
2655 cmd_buffer->dirty |= ANV_CMD_BUFFER_CB_DIRTY;
Kristian Høgsbergcbe7ed42015-05-24 21:19:26 -07002656 break;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002657 case VK_STATE_BIND_POINT_DEPTH_STENCIL:
Kristian Høgsbergcbe7ed42015-05-24 21:19:26 -07002658 cmd_buffer->ds_state = (struct anv_dynamic_ds_state *) dynamicState;
2659 cmd_buffer->dirty |= ANV_CMD_BUFFER_DS_DIRTY;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002660 break;
2661 default:
2662 break;
2663 };
2664}
2665
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002666static struct anv_state
2667anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer,
2668 uint32_t size, uint32_t alignment)
2669{
2670 struct anv_state state;
2671
2672 state.offset = ALIGN_U32(cmd_buffer->surface_next, alignment);
2673 state.map = cmd_buffer->surface_bo.map + state.offset;
2674 state.alloc_size = size;
2675 cmd_buffer->surface_next = state.offset + size;
2676
2677 assert(state.offset + size < cmd_buffer->surface_bo.size);
2678
2679 return state;
2680}
2681
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002682void anv_CmdBindDescriptorSets(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002683 VkCmdBuffer cmdBuffer,
2684 VkPipelineBindPoint pipelineBindPoint,
2685 uint32_t firstSet,
2686 uint32_t setCount,
2687 const VkDescriptorSet* pDescriptorSets,
2688 uint32_t dynamicOffsetCount,
2689 const uint32_t* pDynamicOffsets)
2690{
2691 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002692 struct anv_pipeline_layout *layout = cmd_buffer->pipeline->layout;
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002693 struct anv_bindings *bindings = cmd_buffer->bindings;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002694
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002695 uint32_t offset = 0;
2696 for (uint32_t i = 0; i < setCount; i++) {
2697 struct anv_descriptor_set *set =
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002698 (struct anv_descriptor_set *) pDescriptorSets[i];
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002699 struct anv_descriptor_set_layout *set_layout = layout->set[firstSet + i].layout;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002700
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002701 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
2702 uint32_t *surface_to_desc = set_layout->stage[s].surface_start;
2703 uint32_t *sampler_to_desc = set_layout->stage[s].sampler_start;
2704 uint32_t bias = s == VK_SHADER_STAGE_FRAGMENT ? MAX_RTS : 0;
2705 uint32_t start;
2706
2707 start = bias + layout->set[firstSet + i].surface_start[s];
2708 for (uint32_t b = 0; b < set_layout->stage[s].surface_count; b++) {
2709 struct anv_surface_view *view = set->descriptors[surface_to_desc[b]].view;
Jason Ekstrand0a547512015-05-21 16:33:04 -07002710 if (!view)
2711 continue;
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002712
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002713 struct anv_state state =
2714 anv_cmd_buffer_alloc_surface_state(cmd_buffer, 64, 64);
2715 memcpy(state.map, view->surface_state.map, 64);
2716
Jason Ekstrand519fe762015-05-21 15:55:27 -07002717 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
2718 *(uint64_t *)(state.map + 8 * 4) =
2719 anv_reloc_list_add(&cmd_buffer->surface_relocs,
Jason Ekstrand403266b2015-05-25 17:38:15 -07002720 cmd_buffer->device,
Jason Ekstrand519fe762015-05-21 15:55:27 -07002721 state.offset + 8 * 4,
2722 view->bo, view->offset);
2723
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002724 bindings->descriptors[s].surfaces[start + b] = state.offset;
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002725 }
2726
2727 start = layout->set[firstSet + i].sampler_start[s];
2728 for (uint32_t b = 0; b < set_layout->stage[s].sampler_count; b++) {
2729 struct anv_sampler *sampler = set->descriptors[sampler_to_desc[b]].sampler;
Jason Ekstrand0a547512015-05-21 16:33:04 -07002730 if (!sampler)
2731 continue;
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002732
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002733 memcpy(&bindings->descriptors[s].samplers[start + b],
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002734 sampler->state, sizeof(sampler->state));
2735 }
2736 }
2737
2738 offset += layout->set[firstSet + i].layout->num_dynamic_buffers;
2739 }
2740
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002741 cmd_buffer->dirty |= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY;
2742}
2743
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002744void anv_CmdBindIndexBuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002745 VkCmdBuffer cmdBuffer,
2746 VkBuffer _buffer,
2747 VkDeviceSize offset,
2748 VkIndexType indexType)
2749{
2750 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2751 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
2752
2753 static const uint32_t vk_to_gen_index_type[] = {
2754 [VK_INDEX_TYPE_UINT8] = INDEX_BYTE,
2755 [VK_INDEX_TYPE_UINT16] = INDEX_WORD,
2756 [VK_INDEX_TYPE_UINT32] = INDEX_DWORD,
2757 };
2758
2759 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_INDEX_BUFFER,
2760 .IndexFormat = vk_to_gen_index_type[indexType],
Kristian Høgsberg0997a7b2015-05-21 14:35:34 -07002761 .MemoryObjectControlState = GEN8_MOCS,
Kristian Høgsberg099faa12015-05-11 22:19:58 -07002762 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002763 .BufferSize = buffer->size - offset);
2764}
2765
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002766void anv_CmdBindVertexBuffers(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002767 VkCmdBuffer cmdBuffer,
2768 uint32_t startBinding,
2769 uint32_t bindingCount,
2770 const VkBuffer* pBuffers,
2771 const VkDeviceSize* pOffsets)
2772{
2773 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002774 struct anv_bindings *bindings = cmd_buffer->bindings;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002775
2776 /* We have to defer setting up vertex buffer since we need the buffer
2777 * stride from the pipeline. */
2778
2779 for (uint32_t i = 0; i < bindingCount; i++) {
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002780 bindings->vb[startBinding + i].buffer = (struct anv_buffer *) pBuffers[i];
2781 bindings->vb[startBinding + i].offset = pOffsets[i];
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002782 cmd_buffer->vb_dirty |= 1 << (startBinding + i);
2783 }
2784}
2785
2786static void
2787flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer)
2788{
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002789 struct anv_pipeline_layout *layout = cmd_buffer->pipeline->layout;
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002790 struct anv_bindings *bindings = cmd_buffer->bindings;
2791 uint32_t layers = cmd_buffer->framebuffer->layers;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002792
2793 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002794 uint32_t bias;
2795
2796 if (s == VK_SHADER_STAGE_FRAGMENT) {
2797 bias = MAX_RTS;
2798 layers = cmd_buffer->framebuffer->layers;
2799 } else {
2800 bias = 0;
2801 layers = 0;
2802 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002803
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002804 /* This is a little awkward: layout can be NULL but we still have to
2805 * allocate and set a binding table for the PS stage for render
2806 * targets. */
Jason Ekstrand4223de72015-05-16 10:23:09 -07002807 uint32_t surface_count = layout ? layout->stage[s].surface_count : 0;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002808
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002809 if (layers + surface_count > 0) {
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002810 struct anv_state state;
2811 uint32_t size;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002812
Jason Ekstrand057bef82015-05-16 10:42:51 -07002813 size = (bias + surface_count) * sizeof(uint32_t);
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07002814 state = anv_cmd_buffer_alloc_surface_state(cmd_buffer, size, 32);
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002815 memcpy(state.map, bindings->descriptors[s].surfaces, size);
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002816
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002817 static const uint32_t binding_table_opcodes[] = {
2818 [VK_SHADER_STAGE_VERTEX] = 38,
2819 [VK_SHADER_STAGE_TESS_CONTROL] = 39,
2820 [VK_SHADER_STAGE_TESS_EVALUATION] = 40,
2821 [VK_SHADER_STAGE_GEOMETRY] = 41,
2822 [VK_SHADER_STAGE_FRAGMENT] = 42,
2823 [VK_SHADER_STAGE_COMPUTE] = 0,
2824 };
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002825
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07002826 anv_batch_emit(&cmd_buffer->batch,
2827 GEN8_3DSTATE_BINDING_TABLE_POINTERS_VS,
2828 ._3DCommandSubOpcode = binding_table_opcodes[s],
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002829 .PointertoVSBindingTable = state.offset);
2830 }
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07002831
2832 if (layout && layout->stage[s].sampler_count > 0) {
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002833 struct anv_state state;
2834 size_t size;
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07002835
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002836 size = layout->stage[s].sampler_count * 16;
Kristian Høgsberg610e6292015-05-26 11:50:34 -07002837 state = anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream, size, 32);
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002838 memcpy(state.map, bindings->descriptors[s].samplers, size);
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07002839
2840 static const uint32_t sampler_state_opcodes[] = {
2841 [VK_SHADER_STAGE_VERTEX] = 43,
2842 [VK_SHADER_STAGE_TESS_CONTROL] = 44, /* HS */
2843 [VK_SHADER_STAGE_TESS_EVALUATION] = 45, /* DS */
2844 [VK_SHADER_STAGE_GEOMETRY] = 46,
2845 [VK_SHADER_STAGE_FRAGMENT] = 47,
2846 [VK_SHADER_STAGE_COMPUTE] = 0,
2847 };
2848
2849 anv_batch_emit(&cmd_buffer->batch,
2850 GEN8_3DSTATE_SAMPLER_STATE_POINTERS_VS,
2851 ._3DCommandSubOpcode = sampler_state_opcodes[s],
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07002852 .PointertoVSSamplerState = state.offset);
Kristian Høgsberg83c7e1f2015-05-13 14:43:08 -07002853 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002854 }
2855}
2856
Kristian Høgsbergb29f4422015-05-26 11:22:12 -07002857static struct anv_state
2858anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
2859 uint32_t *a, uint32_t dwords, uint32_t alignment)
2860{
2861 struct anv_device *device = cmd_buffer->device;
2862 struct anv_state state;
2863
2864 state = anv_state_pool_alloc(&device->dynamic_state_pool, dwords * 4, alignment);
2865 memcpy(state.map, a, dwords * 4);
2866
2867 return state;
2868}
2869
2870static struct anv_state
2871anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
2872 uint32_t *a, uint32_t *b, uint32_t dwords, uint32_t alignment)
2873{
2874 struct anv_device *device = cmd_buffer->device;
2875 struct anv_state state;
2876 uint32_t *p;
2877
2878 state = anv_state_pool_alloc(&device->dynamic_state_pool, dwords * 4, alignment);
2879 p = state.map;
2880 for (uint32_t i = 0; i < dwords; i++)
2881 p[i] = a[i] | b[i];
2882
2883 return state;
2884}
2885
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002886static void
2887anv_cmd_buffer_flush_state(struct anv_cmd_buffer *cmd_buffer)
2888{
2889 struct anv_pipeline *pipeline = cmd_buffer->pipeline;
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002890 struct anv_bindings *bindings = cmd_buffer->bindings;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002891 uint32_t *p;
2892
Jason Ekstrand0f0b5ae2015-05-21 16:49:55 -07002893 uint32_t vb_emit = cmd_buffer->vb_dirty & pipeline->vb_used;
2894 const uint32_t num_buffers = __builtin_popcount(vb_emit);
2895 const uint32_t num_dwords = 1 + num_buffers * 4;
2896
2897 if (vb_emit) {
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002898 p = anv_batch_emitn(&cmd_buffer->batch, num_dwords,
2899 GEN8_3DSTATE_VERTEX_BUFFERS);
2900 uint32_t vb, i = 0;
Jason Ekstrand0f0b5ae2015-05-21 16:49:55 -07002901 for_each_bit(vb, vb_emit) {
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07002902 struct anv_buffer *buffer = bindings->vb[vb].buffer;
2903 uint32_t offset = bindings->vb[vb].offset;
Jason Ekstrand0f0b5ae2015-05-21 16:49:55 -07002904
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002905 struct GEN8_VERTEX_BUFFER_STATE state = {
2906 .VertexBufferIndex = vb,
Kristian Høgsberg0997a7b2015-05-21 14:35:34 -07002907 .MemoryObjectControlState = GEN8_MOCS,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002908 .AddressModifyEnable = true,
2909 .BufferPitch = pipeline->binding_stride[vb],
Kristian Høgsberg099faa12015-05-11 22:19:58 -07002910 .BufferStartingAddress = { buffer->bo, buffer->offset + offset },
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002911 .BufferSize = buffer->size - offset
2912 };
2913
2914 GEN8_VERTEX_BUFFER_STATE_pack(&cmd_buffer->batch, &p[1 + i * 4], &state);
2915 i++;
2916 }
2917 }
2918
2919 if (cmd_buffer->dirty & ANV_CMD_BUFFER_PIPELINE_DIRTY)
2920 anv_batch_emit_batch(&cmd_buffer->batch, &pipeline->batch);
2921
2922 if (cmd_buffer->dirty & ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY)
2923 flush_descriptor_sets(cmd_buffer);
2924
Kristian Høgsberg99883772015-05-26 09:40:10 -07002925 if (cmd_buffer->dirty & (ANV_CMD_BUFFER_PIPELINE_DIRTY | ANV_CMD_BUFFER_RS_DIRTY)) {
Kristian Høgsberg55b9b702015-05-11 22:23:38 -07002926 anv_batch_emit_merge(&cmd_buffer->batch,
2927 cmd_buffer->rs_state->state_sf, pipeline->state_sf);
Kristian Høgsberg99883772015-05-26 09:40:10 -07002928 anv_batch_emit_merge(&cmd_buffer->batch,
2929 cmd_buffer->rs_state->state_raster, pipeline->state_raster);
2930 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002931
Kristian Høgsbergcbe7ed42015-05-24 21:19:26 -07002932 if (cmd_buffer->ds_state &&
2933 (cmd_buffer->dirty & (ANV_CMD_BUFFER_PIPELINE_DIRTY | ANV_CMD_BUFFER_DS_DIRTY)))
2934 anv_batch_emit_merge(&cmd_buffer->batch,
2935 cmd_buffer->ds_state->state_wm_depth_stencil,
2936 pipeline->state_wm_depth_stencil);
2937
Kristian Høgsbergb29f4422015-05-26 11:22:12 -07002938 if (cmd_buffer->dirty & (ANV_CMD_BUFFER_CB_DIRTY | ANV_CMD_BUFFER_DS_DIRTY)) {
2939 struct anv_state state;
Kristian Høgsberga1d30f82015-05-26 17:12:18 -07002940 if (cmd_buffer->ds_state == NULL)
2941 state = anv_cmd_buffer_emit_dynamic(cmd_buffer,
2942 cmd_buffer->cb_state->state_color_calc,
2943 GEN8_COLOR_CALC_STATE_length, 32);
2944 else if (cmd_buffer->cb_state == NULL)
2945 state = anv_cmd_buffer_emit_dynamic(cmd_buffer,
2946 cmd_buffer->ds_state->state_color_calc,
2947 GEN8_COLOR_CALC_STATE_length, 32);
2948 else
Kristian Høgsbergb29f4422015-05-26 11:22:12 -07002949 state = anv_cmd_buffer_merge_dynamic(cmd_buffer,
2950 cmd_buffer->ds_state->state_color_calc,
2951 cmd_buffer->cb_state->state_color_calc,
2952 GEN8_COLOR_CALC_STATE_length, 32);
Kristian Høgsbergb29f4422015-05-26 11:22:12 -07002953
2954 anv_batch_emit(&cmd_buffer->batch,
2955 GEN8_3DSTATE_CC_STATE_POINTERS,
2956 .ColorCalcStatePointer = state.offset,
2957 .ColorCalcStatePointerValid = true);
2958 }
2959
Jason Ekstrand0f0b5ae2015-05-21 16:49:55 -07002960 cmd_buffer->vb_dirty &= ~vb_emit;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002961 cmd_buffer->dirty = 0;
2962}
2963
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002964void anv_CmdDraw(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002965 VkCmdBuffer cmdBuffer,
2966 uint32_t firstVertex,
2967 uint32_t vertexCount,
2968 uint32_t firstInstance,
2969 uint32_t instanceCount)
2970{
2971 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2972
2973 anv_cmd_buffer_flush_state(cmd_buffer);
2974
2975 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2976 .VertexAccessType = SEQUENTIAL,
2977 .VertexCountPerInstance = vertexCount,
2978 .StartVertexLocation = firstVertex,
2979 .InstanceCount = instanceCount,
2980 .StartInstanceLocation = firstInstance,
2981 .BaseVertexLocation = 0);
2982}
2983
Kristian Høgsberg454345d2015-05-17 16:33:48 -07002984void anv_CmdDrawIndexed(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07002985 VkCmdBuffer cmdBuffer,
2986 uint32_t firstIndex,
2987 uint32_t indexCount,
2988 int32_t vertexOffset,
2989 uint32_t firstInstance,
2990 uint32_t instanceCount)
2991{
2992 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
2993
2994 anv_cmd_buffer_flush_state(cmd_buffer);
2995
2996 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
2997 .VertexAccessType = RANDOM,
2998 .VertexCountPerInstance = indexCount,
2999 .StartVertexLocation = firstIndex,
3000 .InstanceCount = instanceCount,
3001 .StartInstanceLocation = firstInstance,
3002 .BaseVertexLocation = 0);
3003}
3004
3005static void
3006anv_batch_lrm(struct anv_batch *batch,
3007 uint32_t reg, struct anv_bo *bo, uint32_t offset)
3008{
3009 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_MEM,
3010 .RegisterAddress = reg,
3011 .MemoryAddress = { bo, offset });
3012}
3013
3014static void
3015anv_batch_lri(struct anv_batch *batch, uint32_t reg, uint32_t imm)
3016{
3017 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_IMM,
3018 .RegisterOffset = reg,
3019 .DataDWord = imm);
3020}
3021
3022/* Auto-Draw / Indirect Registers */
3023#define GEN7_3DPRIM_END_OFFSET 0x2420
3024#define GEN7_3DPRIM_START_VERTEX 0x2430
3025#define GEN7_3DPRIM_VERTEX_COUNT 0x2434
3026#define GEN7_3DPRIM_INSTANCE_COUNT 0x2438
3027#define GEN7_3DPRIM_START_INSTANCE 0x243C
3028#define GEN7_3DPRIM_BASE_VERTEX 0x2440
3029
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003030void anv_CmdDrawIndirect(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003031 VkCmdBuffer cmdBuffer,
3032 VkBuffer _buffer,
3033 VkDeviceSize offset,
3034 uint32_t count,
3035 uint32_t stride)
3036{
3037 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3038 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
Kristian Høgsberg099faa12015-05-11 22:19:58 -07003039 struct anv_bo *bo = buffer->bo;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003040 uint32_t bo_offset = buffer->offset + offset;
3041
3042 anv_cmd_buffer_flush_state(cmd_buffer);
3043
3044 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
3045 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
3046 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
3047 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 12);
3048 anv_batch_lri(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, 0);
3049
3050 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
3051 .IndirectParameterEnable = true,
3052 .VertexAccessType = SEQUENTIAL);
3053}
3054
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003055void anv_CmdDrawIndexedIndirect(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003056 VkCmdBuffer cmdBuffer,
3057 VkBuffer _buffer,
3058 VkDeviceSize offset,
3059 uint32_t count,
3060 uint32_t stride)
3061{
3062 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3063 struct anv_buffer *buffer = (struct anv_buffer *) _buffer;
Kristian Høgsberg099faa12015-05-11 22:19:58 -07003064 struct anv_bo *bo = buffer->bo;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003065 uint32_t bo_offset = buffer->offset + offset;
3066
3067 anv_cmd_buffer_flush_state(cmd_buffer);
3068
3069 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_VERTEX_COUNT, bo, bo_offset);
3070 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_INSTANCE_COUNT, bo, bo_offset + 4);
3071 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_VERTEX, bo, bo_offset + 8);
3072 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_BASE_VERTEX, bo, bo_offset + 12);
3073 anv_batch_lrm(&cmd_buffer->batch, GEN7_3DPRIM_START_INSTANCE, bo, bo_offset + 16);
3074
3075 anv_batch_emit(&cmd_buffer->batch, GEN8_3DPRIMITIVE,
3076 .IndirectParameterEnable = true,
3077 .VertexAccessType = RANDOM);
3078}
3079
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003080void anv_CmdDispatch(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003081 VkCmdBuffer cmdBuffer,
3082 uint32_t x,
3083 uint32_t y,
3084 uint32_t z)
3085{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003086 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003087}
3088
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003089void anv_CmdDispatchIndirect(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003090 VkCmdBuffer cmdBuffer,
3091 VkBuffer buffer,
3092 VkDeviceSize offset)
3093{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003094 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003095}
3096
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003097void anv_CmdSetEvent(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003098 VkCmdBuffer cmdBuffer,
3099 VkEvent event,
3100 VkPipeEvent pipeEvent)
3101{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003102 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003103}
3104
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003105void anv_CmdResetEvent(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003106 VkCmdBuffer cmdBuffer,
3107 VkEvent event,
3108 VkPipeEvent pipeEvent)
3109{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003110 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003111}
3112
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003113void anv_CmdWaitEvents(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003114 VkCmdBuffer cmdBuffer,
3115 VkWaitEvent waitEvent,
3116 uint32_t eventCount,
3117 const VkEvent* pEvents,
3118 uint32_t memBarrierCount,
3119 const void** ppMemBarriers)
3120{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003121 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003122}
3123
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003124void anv_CmdPipelineBarrier(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003125 VkCmdBuffer cmdBuffer,
3126 VkWaitEvent waitEvent,
3127 uint32_t pipeEventCount,
3128 const VkPipeEvent* pPipeEvents,
3129 uint32_t memBarrierCount,
3130 const void** ppMemBarriers)
3131{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003132 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003133}
3134
3135static void
3136anv_batch_emit_ps_depth_count(struct anv_batch *batch,
3137 struct anv_bo *bo, uint32_t offset)
3138{
3139 anv_batch_emit(batch, GEN8_PIPE_CONTROL,
3140 .DestinationAddressType = DAT_PPGTT,
3141 .PostSyncOperation = WritePSDepthCount,
3142 .Address = { bo, offset }); /* FIXME: This is only lower 32 bits */
3143}
3144
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003145void anv_CmdBeginQuery(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003146 VkCmdBuffer cmdBuffer,
3147 VkQueryPool queryPool,
3148 uint32_t slot,
3149 VkQueryControlFlags flags)
3150{
3151 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3152 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
3153
3154 switch (pool->type) {
3155 case VK_QUERY_TYPE_OCCLUSION:
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07003156 anv_batch_emit_ps_depth_count(&cmd_buffer->batch, &pool->bo,
3157 slot * sizeof(struct anv_query_pool_slot));
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003158 break;
3159
3160 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003161 default:
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07003162 unreachable("");
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003163 }
3164}
3165
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003166void anv_CmdEndQuery(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003167 VkCmdBuffer cmdBuffer,
3168 VkQueryPool queryPool,
3169 uint32_t slot)
3170{
3171 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3172 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
3173
3174 switch (pool->type) {
3175 case VK_QUERY_TYPE_OCCLUSION:
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07003176 anv_batch_emit_ps_depth_count(&cmd_buffer->batch, &pool->bo,
3177 slot * sizeof(struct anv_query_pool_slot) + 8);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003178 break;
3179
3180 case VK_QUERY_TYPE_PIPELINE_STATISTICS:
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003181 default:
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07003182 unreachable("");
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003183 }
3184}
3185
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003186void anv_CmdResetQueryPool(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003187 VkCmdBuffer cmdBuffer,
3188 VkQueryPool queryPool,
3189 uint32_t startQuery,
3190 uint32_t queryCount)
3191{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003192 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003193}
3194
Kristian Høgsbergae9ac472015-05-18 17:04:32 -07003195#define TIMESTAMP 0x2358
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003196
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003197void anv_CmdWriteTimestamp(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003198 VkCmdBuffer cmdBuffer,
3199 VkTimestampType timestampType,
3200 VkBuffer destBuffer,
3201 VkDeviceSize destOffset)
3202{
3203 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3204 struct anv_buffer *buffer = (struct anv_buffer *) destBuffer;
Kristian Høgsberg099faa12015-05-11 22:19:58 -07003205 struct anv_bo *bo = buffer->bo;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003206
3207 switch (timestampType) {
3208 case VK_TIMESTAMP_TYPE_TOP:
3209 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_STORE_REGISTER_MEM,
3210 .RegisterAddress = TIMESTAMP,
3211 .MemoryAddress = { bo, buffer->offset + destOffset });
Kristian Høgsbergae9ac472015-05-18 17:04:32 -07003212 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_STORE_REGISTER_MEM,
3213 .RegisterAddress = TIMESTAMP + 4,
3214 .MemoryAddress = { bo, buffer->offset + destOffset + 4 });
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003215 break;
3216
3217 case VK_TIMESTAMP_TYPE_BOTTOM:
3218 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
3219 .DestinationAddressType = DAT_PPGTT,
3220 .PostSyncOperation = WriteTimestamp,
3221 .Address = /* FIXME: This is only lower 32 bits */
3222 { bo, buffer->offset + destOffset });
3223 break;
3224
3225 default:
3226 break;
3227 }
3228}
3229
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07003230#define alu_opcode(v) __gen_field((v), 20, 31)
3231#define alu_operand1(v) __gen_field((v), 10, 19)
3232#define alu_operand2(v) __gen_field((v), 0, 9)
3233#define alu(opcode, operand1, operand2) \
3234 alu_opcode(opcode) | alu_operand1(operand1) | alu_operand2(operand2)
3235
3236#define OPCODE_NOOP 0x000
3237#define OPCODE_LOAD 0x080
3238#define OPCODE_LOADINV 0x480
3239#define OPCODE_LOAD0 0x081
3240#define OPCODE_LOAD1 0x481
3241#define OPCODE_ADD 0x100
3242#define OPCODE_SUB 0x101
3243#define OPCODE_AND 0x102
3244#define OPCODE_OR 0x103
3245#define OPCODE_XOR 0x104
3246#define OPCODE_STORE 0x180
3247#define OPCODE_STOREINV 0x580
3248
3249#define OPERAND_R0 0x00
3250#define OPERAND_R1 0x01
3251#define OPERAND_R2 0x02
3252#define OPERAND_R3 0x03
3253#define OPERAND_R4 0x04
3254#define OPERAND_SRCA 0x20
3255#define OPERAND_SRCB 0x21
3256#define OPERAND_ACCU 0x31
3257#define OPERAND_ZF 0x32
3258#define OPERAND_CF 0x33
3259
3260#define CS_GPR(n) (0x2600 + (n) * 8)
3261
3262static void
3263emit_load_alu_reg_u64(struct anv_batch *batch, uint32_t reg,
3264 struct anv_bo *bo, uint32_t offset)
3265{
3266 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_MEM,
3267 .RegisterAddress = reg,
3268 .MemoryAddress = { bo, offset });
3269 anv_batch_emit(batch, GEN8_MI_LOAD_REGISTER_MEM,
3270 .RegisterAddress = reg + 4,
3271 .MemoryAddress = { bo, offset + 4 });
3272}
3273
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003274void anv_CmdCopyQueryPoolResults(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003275 VkCmdBuffer cmdBuffer,
3276 VkQueryPool queryPool,
3277 uint32_t startQuery,
3278 uint32_t queryCount,
3279 VkBuffer destBuffer,
3280 VkDeviceSize destOffset,
3281 VkDeviceSize destStride,
3282 VkQueryResultFlags flags)
3283{
Kristian Høgsberg82ddab42015-05-18 17:03:58 -07003284 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3285 struct anv_query_pool *pool = (struct anv_query_pool *) queryPool;
3286 struct anv_buffer *buffer = (struct anv_buffer *) destBuffer;
3287 uint32_t slot_offset, dst_offset;
3288
3289 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) {
3290 /* Where is the availabilty info supposed to go? */
3291 anv_finishme("VK_QUERY_RESULT_WITH_AVAILABILITY_BIT");
3292 return;
3293 }
3294
3295 assert(pool->type == VK_QUERY_TYPE_OCCLUSION);
3296
3297 /* FIXME: If we're not waiting, should we just do this on the CPU? */
3298 if (flags & VK_QUERY_RESULT_WAIT_BIT)
3299 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
3300 .CommandStreamerStallEnable = true);
3301
3302 dst_offset = buffer->offset + destOffset;
3303 for (uint32_t i = 0; i < queryCount; i++) {
3304
3305 slot_offset = (startQuery + i) * sizeof(struct anv_query_pool_slot);
3306
3307 emit_load_alu_reg_u64(&cmd_buffer->batch, CS_GPR(0), &pool->bo, slot_offset);
3308 emit_load_alu_reg_u64(&cmd_buffer->batch, CS_GPR(1), &pool->bo, slot_offset + 8);
3309
3310 /* FIXME: We need to clamp the result for 32 bit. */
3311
3312 uint32_t *dw = anv_batch_emitn(&cmd_buffer->batch, 5, GEN8_MI_MATH);
3313 dw[1] = alu(OPCODE_LOAD, OPERAND_SRCA, OPERAND_R1);
3314 dw[2] = alu(OPCODE_LOAD, OPERAND_SRCB, OPERAND_R0);
3315 dw[3] = alu(OPCODE_SUB, 0, 0);
3316 dw[4] = alu(OPCODE_STORE, OPERAND_R2, OPERAND_ACCU);
3317
3318 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_STORE_REGISTER_MEM,
3319 .RegisterAddress = CS_GPR(2),
3320 /* FIXME: This is only lower 32 bits */
3321 .MemoryAddress = { buffer->bo, dst_offset });
3322
3323 if (flags & VK_QUERY_RESULT_64_BIT)
3324 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_STORE_REGISTER_MEM,
3325 .RegisterAddress = CS_GPR(2) + 4,
3326 /* FIXME: This is only lower 32 bits */
3327 .MemoryAddress = { buffer->bo, dst_offset + 4 });
3328
3329 dst_offset += destStride;
3330 }
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003331}
3332
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003333void anv_CmdInitAtomicCounters(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003334 VkCmdBuffer cmdBuffer,
3335 VkPipelineBindPoint pipelineBindPoint,
3336 uint32_t startCounter,
3337 uint32_t counterCount,
3338 const uint32_t* pData)
3339{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003340 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003341}
3342
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003343void anv_CmdLoadAtomicCounters(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003344 VkCmdBuffer cmdBuffer,
3345 VkPipelineBindPoint pipelineBindPoint,
3346 uint32_t startCounter,
3347 uint32_t counterCount,
3348 VkBuffer srcBuffer,
3349 VkDeviceSize srcOffset)
3350{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003351 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003352}
3353
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003354void anv_CmdSaveAtomicCounters(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003355 VkCmdBuffer cmdBuffer,
3356 VkPipelineBindPoint pipelineBindPoint,
3357 uint32_t startCounter,
3358 uint32_t counterCount,
3359 VkBuffer destBuffer,
3360 VkDeviceSize destOffset)
3361{
Jason Ekstrandffe9f602015-05-12 13:44:43 -07003362 stub();
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003363}
3364
Jason Ekstrand57153da2015-05-22 15:15:08 -07003365static void
3366anv_framebuffer_destroy(struct anv_device *device,
3367 struct anv_object *object,
3368 VkObjectType obj_type)
3369{
3370 struct anv_framebuffer *fb = (struct anv_framebuffer *)object;
3371
3372 assert(obj_type == VK_OBJECT_TYPE_FRAMEBUFFER);
3373
3374 anv_DestroyObject((VkDevice) device,
3375 VK_OBJECT_TYPE_DYNAMIC_VP_STATE,
3376 fb->vp_state);
3377
3378 anv_device_free(device, fb);
3379}
3380
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003381VkResult anv_CreateFramebuffer(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003382 VkDevice _device,
3383 const VkFramebufferCreateInfo* pCreateInfo,
3384 VkFramebuffer* pFramebuffer)
3385{
3386 struct anv_device *device = (struct anv_device *) _device;
3387 struct anv_framebuffer *framebuffer;
3388
Kristian Høgsberg37743f92015-05-22 22:59:12 -07003389 static const struct anv_depth_stencil_view null_view =
3390 { .depth_format = D16_UNORM, .depth_stride = 0, .stencil_stride = 0 };
3391
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003392 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
3393
3394 framebuffer = anv_device_alloc(device, sizeof(*framebuffer), 8,
3395 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
3396 if (framebuffer == NULL)
3397 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
3398
Jason Ekstrand57153da2015-05-22 15:15:08 -07003399 framebuffer->base.destructor = anv_framebuffer_destroy;
3400
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003401 framebuffer->color_attachment_count = pCreateInfo->colorAttachmentCount;
3402 for (uint32_t i = 0; i < pCreateInfo->colorAttachmentCount; i++) {
3403 framebuffer->color_attachments[i] =
Kristian Høgsbergf5b0f132015-05-13 15:31:26 -07003404 (struct anv_surface_view *) pCreateInfo->pColorAttachments[i].view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003405 }
3406
3407 if (pCreateInfo->pDepthStencilAttachment) {
3408 framebuffer->depth_stencil =
3409 (struct anv_depth_stencil_view *) pCreateInfo->pDepthStencilAttachment->view;
Kristian Høgsberg37743f92015-05-22 22:59:12 -07003410 } else {
3411 framebuffer->depth_stencil = &null_view;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003412 }
3413
3414 framebuffer->sample_count = pCreateInfo->sampleCount;
3415 framebuffer->width = pCreateInfo->width;
3416 framebuffer->height = pCreateInfo->height;
3417 framebuffer->layers = pCreateInfo->layers;
3418
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07003419 vkCreateDynamicViewportState((VkDevice) device,
3420 &(VkDynamicVpStateCreateInfo) {
3421 .sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO,
Jason Ekstrand912944e2015-05-25 17:03:57 -07003422 .viewportAndScissorCount = 1,
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07003423 .pViewports = (VkViewport[]) {
3424 {
3425 .originX = 0,
3426 .originY = 0,
3427 .width = pCreateInfo->width,
3428 .height = pCreateInfo->height,
3429 .minDepth = 0,
3430 .maxDepth = 1
3431 },
3432 },
3433 .pScissors = (VkRect[]) {
3434 { { 0, 0 },
3435 { pCreateInfo->width, pCreateInfo->height } },
3436 }
3437 },
3438 &framebuffer->vp_state);
3439
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003440 *pFramebuffer = (VkFramebuffer) framebuffer;
3441
3442 return VK_SUCCESS;
3443}
3444
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003445VkResult anv_CreateRenderPass(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003446 VkDevice _device,
3447 const VkRenderPassCreateInfo* pCreateInfo,
3448 VkRenderPass* pRenderPass)
3449{
3450 struct anv_device *device = (struct anv_device *) _device;
3451 struct anv_render_pass *pass;
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07003452 size_t size;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003453
3454 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO);
3455
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07003456 size = sizeof(*pass) +
3457 pCreateInfo->layers * sizeof(struct anv_render_pass_layer);
3458 pass = anv_device_alloc(device, size, 8,
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003459 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
3460 if (pass == NULL)
3461 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
3462
3463 pass->render_area = pCreateInfo->renderArea;
3464
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07003465 pass->num_layers = pCreateInfo->layers;
3466
3467 pass->num_clear_layers = 0;
3468 for (uint32_t i = 0; i < pCreateInfo->layers; i++) {
3469 pass->layers[i].color_load_op = pCreateInfo->pColorLoadOps[i];
3470 pass->layers[i].clear_color = pCreateInfo->pColorLoadClearValues[i];
3471 if (pass->layers[i].color_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR)
3472 pass->num_clear_layers++;
3473 }
3474
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003475 *pRenderPass = (VkRenderPass) pass;
3476
3477 return VK_SUCCESS;
3478}
3479
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07003480void
3481anv_cmd_buffer_fill_render_targets(struct anv_cmd_buffer *cmd_buffer)
3482{
3483 struct anv_framebuffer *framebuffer = cmd_buffer->framebuffer;
3484 struct anv_bindings *bindings = cmd_buffer->bindings;
3485
3486 for (uint32_t i = 0; i < framebuffer->color_attachment_count; i++) {
Kristian Høgsberg37743f92015-05-22 22:59:12 -07003487 const struct anv_surface_view *view = framebuffer->color_attachments[i];
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07003488
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07003489 struct anv_state state =
3490 anv_cmd_buffer_alloc_surface_state(cmd_buffer, 64, 64);
3491 memcpy(state.map, view->surface_state.map, 64);
3492
Jason Ekstrand519fe762015-05-21 15:55:27 -07003493 /* The address goes in dwords 8 and 9 of the SURFACE_STATE */
3494 *(uint64_t *)(state.map + 8 * 4) =
3495 anv_reloc_list_add(&cmd_buffer->surface_relocs,
Jason Ekstrand403266b2015-05-25 17:38:15 -07003496 cmd_buffer->device,
Jason Ekstrand519fe762015-05-21 15:55:27 -07003497 state.offset + 8 * 4,
3498 view->bo, view->offset);
3499
Kristian Høgsberga1bd4262015-05-19 14:14:24 -07003500 bindings->descriptors[VK_SHADER_STAGE_FRAGMENT].surfaces[i] = state.offset;
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07003501 }
3502 cmd_buffer->dirty |= ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY;
3503}
3504
Kristian Høgsberg37743f92015-05-22 22:59:12 -07003505static void
3506anv_cmd_buffer_emit_depth_stencil(struct anv_cmd_buffer *cmd_buffer,
3507 struct anv_render_pass *pass)
3508{
3509 const struct anv_depth_stencil_view *view =
3510 cmd_buffer->framebuffer->depth_stencil;
3511
3512 /* FIXME: Implement the PMA stall W/A */
3513
3514 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DEPTH_BUFFER,
3515 .SurfaceType = SURFTYPE_2D,
3516 .DepthWriteEnable = view->depth_stride > 0,
3517 .StencilWriteEnable = view->stencil_stride > 0,
3518 .HierarchicalDepthBufferEnable = false,
3519 .SurfaceFormat = view->depth_format,
3520 .SurfacePitch = view->depth_stride > 0 ? view->depth_stride - 1 : 0,
3521 .SurfaceBaseAddress = { view->bo, view->depth_offset },
3522 .Height = pass->render_area.extent.height - 1,
3523 .Width = pass->render_area.extent.width - 1,
3524 .LOD = 0,
3525 .Depth = 1 - 1,
3526 .MinimumArrayElement = 0,
3527 .DepthBufferObjectControlState = GEN8_MOCS,
3528 .RenderTargetViewExtent = 1 - 1,
3529 .SurfaceQPitch = 0);
3530
3531 /* Disable hierarchial depth buffers. */
3532 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_HIER_DEPTH_BUFFER);
3533
3534 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_STENCIL_BUFFER,
3535 .StencilBufferEnable = view->stencil_stride > 0,
3536 .StencilBufferObjectControlState = GEN8_MOCS,
3537 .SurfacePitch = view->stencil_stride > 0 ? view->stencil_stride - 1 : 0,
3538 .SurfaceBaseAddress = { view->bo, view->stencil_offset },
3539 .SurfaceQPitch = 0);
3540
3541 /* Clear the clear params. */
3542 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_CLEAR_PARAMS);
3543}
3544
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003545void anv_CmdBeginRenderPass(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003546 VkCmdBuffer cmdBuffer,
3547 const VkRenderPassBegin* pRenderPassBegin)
3548{
3549 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *) cmdBuffer;
3550 struct anv_render_pass *pass = (struct anv_render_pass *) pRenderPassBegin->renderPass;
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07003551 struct anv_framebuffer *framebuffer =
3552 (struct anv_framebuffer *) pRenderPassBegin->framebuffer;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003553
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07003554 cmd_buffer->framebuffer = framebuffer;
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003555
3556 anv_batch_emit(&cmd_buffer->batch, GEN8_3DSTATE_DRAWING_RECTANGLE,
3557 .ClippedDrawingRectangleYMin = pass->render_area.offset.y,
3558 .ClippedDrawingRectangleXMin = pass->render_area.offset.x,
3559 .ClippedDrawingRectangleYMax =
3560 pass->render_area.offset.y + pass->render_area.extent.height - 1,
3561 .ClippedDrawingRectangleXMax =
3562 pass->render_area.offset.x + pass->render_area.extent.width - 1,
3563 .DrawingRectangleOriginY = 0,
3564 .DrawingRectangleOriginX = 0);
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07003565
Kristian Høgsberg3b9f32e2015-05-15 17:03:18 -07003566 anv_cmd_buffer_fill_render_targets(cmd_buffer);
Kristian Høgsbergbf096c92015-05-15 15:03:21 -07003567
Kristian Høgsberg37743f92015-05-22 22:59:12 -07003568 anv_cmd_buffer_emit_depth_stencil(cmd_buffer, pass);
3569
Kristian Høgsbergd77c34d2015-05-11 23:25:06 -07003570 anv_cmd_buffer_clear(cmd_buffer, pass);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003571}
3572
Kristian Høgsberg454345d2015-05-17 16:33:48 -07003573void anv_CmdEndRenderPass(
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003574 VkCmdBuffer cmdBuffer,
3575 VkRenderPass renderPass)
3576{
Jason Ekstranda1309c52015-05-13 22:13:05 -07003577 /* Emit a flushing pipe control at the end of a pass. This is kind of a
3578 * hack but it ensures that render targets always actually get written.
3579 * Eventually, we should do flushing based on image format transitions
3580 * or something of that nature.
3581 */
3582 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
3583 anv_batch_emit(&cmd_buffer->batch, GEN8_PIPE_CONTROL,
3584 .PostSyncOperation = NoWrite,
3585 .RenderTargetCacheFlushEnable = true,
3586 .InstructionCacheInvalidateEnable = true,
3587 .DepthCacheFlushEnable = true,
3588 .VFCacheInvalidationEnable = true,
3589 .TextureCacheInvalidationEnable = true,
3590 .CommandStreamerStallEnable = true);
Kristian Høgsberg769785c2015-05-08 22:32:37 -07003591}
Kristian Høgsbergf8866472015-05-15 22:04:15 -07003592
3593void vkCmdDbgMarkerBegin(
3594 VkCmdBuffer cmdBuffer,
3595 const char* pMarker)
3596 __attribute__ ((visibility ("default")));
3597
3598void vkCmdDbgMarkerEnd(
3599 VkCmdBuffer cmdBuffer)
3600 __attribute__ ((visibility ("default")));
3601
3602VkResult vkDbgSetObjectTag(
3603 VkDevice device,
3604 VkObject object,
3605 size_t tagSize,
3606 const void* pTag)
3607 __attribute__ ((visibility ("default")));
3608
3609
3610void vkCmdDbgMarkerBegin(
3611 VkCmdBuffer cmdBuffer,
3612 const char* pMarker)
3613{
3614}
3615
3616void vkCmdDbgMarkerEnd(
3617 VkCmdBuffer cmdBuffer)
3618{
3619}
3620
3621VkResult vkDbgSetObjectTag(
3622 VkDevice device,
3623 VkObject object,
3624 size_t tagSize,
3625 const void* pTag)
3626{
3627 return VK_SUCCESS;
3628}