blob: bfdc7becbdc2c07c407877e0a2f105ff9c64a77f [file] [log] [blame]
Chia-I Wu09142132014-08-11 15:42:55 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wu09142132014-08-11 15:42:55 +08003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 * Courtney Goeltzenleuchter <courtney@lunarg.com>
Chia-I Wu09142132014-08-11 15:42:55 +080027 */
28
Chia-I Wu730e5362014-08-19 12:15:09 +080029#include "genhw/genhw.h"
30#include "kmd/winsys.h"
31#include "dev.h"
Chia-I Wu343b1372014-08-20 16:39:20 +080032#include "mem.h"
Chia-I Wu730e5362014-08-19 12:15:09 +080033#include "obj.h"
Chia-I Wu00a23b22014-08-20 15:28:08 +080034#include "cmd_priv.h"
Jon Ashburnc04b4dc2015-01-08 18:48:10 -070035#include "fb.h"
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060036#include "vk_debug_marker_lunarg.h"
Chia-I Wu09142132014-08-11 15:42:55 +080037
Chia-I Wu3c3edc02014-09-09 10:32:59 +080038/**
39 * Free all resources used by a writer. Note that the initial size is not
40 * reset.
41 */
42static void cmd_writer_reset(struct intel_cmd *cmd,
43 enum intel_cmd_writer_type which)
Chia-I Wu730e5362014-08-19 12:15:09 +080044{
Chia-I Wu68f319d2014-09-09 09:43:21 +080045 struct intel_cmd_writer *writer = &cmd->writers[which];
Chia-I Wu730e5362014-08-19 12:15:09 +080046
Chia-I Wu3c3edc02014-09-09 10:32:59 +080047 if (writer->ptr) {
48 intel_bo_unmap(writer->bo);
49 writer->ptr = NULL;
Chia-I Wu730e5362014-08-19 12:15:09 +080050 }
51
Chia-I Wucb2dc0d2015-03-05 16:19:42 -070052 intel_bo_unref(writer->bo);
53 writer->bo = NULL;
Chia-I Wu3c3edc02014-09-09 10:32:59 +080054
Chia-I Wue24c3292014-08-21 14:05:23 +080055 writer->used = 0;
Chia-I Wu00b51a82014-09-09 12:07:37 +080056
Chia-I Wuf98dd882015-02-10 04:17:47 +080057 writer->sba_offset = 0;
58
Chia-I Wu00b51a82014-09-09 12:07:37 +080059 if (writer->items) {
Chia-I Wuf9c81ef2015-02-22 13:49:15 +080060 intel_free(cmd, writer->items);
Courtney Goeltzenleuchter2ba70162014-09-25 18:14:53 -060061 writer->items = NULL;
Chia-I Wu00b51a82014-09-09 12:07:37 +080062 writer->item_alloc = 0;
63 writer->item_used = 0;
64 }
Chia-I Wu3c3edc02014-09-09 10:32:59 +080065}
66
67/**
68 * Discard everything written so far.
69 */
70static void cmd_writer_discard(struct intel_cmd *cmd,
71 enum intel_cmd_writer_type which)
72{
73 struct intel_cmd_writer *writer = &cmd->writers[which];
74
75 intel_bo_truncate_relocs(writer->bo, 0);
76 writer->used = 0;
Chia-I Wu00b51a82014-09-09 12:07:37 +080077 writer->item_used = 0;
Chia-I Wu3c3edc02014-09-09 10:32:59 +080078}
79
80static struct intel_bo *alloc_writer_bo(struct intel_winsys *winsys,
81 enum intel_cmd_writer_type which,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060082 size_t size)
Chia-I Wu3c3edc02014-09-09 10:32:59 +080083{
84 static const char *writer_names[INTEL_CMD_WRITER_COUNT] = {
85 [INTEL_CMD_WRITER_BATCH] = "batch",
Chia-I Wu15cccf72015-02-10 04:07:40 +080086 [INTEL_CMD_WRITER_SURFACE] = "surface",
87 [INTEL_CMD_WRITER_STATE] = "state",
Chia-I Wu3c3edc02014-09-09 10:32:59 +080088 [INTEL_CMD_WRITER_INSTRUCTION] = "instruction",
89 };
90
Chia-I Wucb2dc0d2015-03-05 16:19:42 -070091 return intel_winsys_alloc_bo(winsys, writer_names[which], size, true);
Chia-I Wu3c3edc02014-09-09 10:32:59 +080092}
93
94/**
95 * Allocate and map the buffer for writing.
96 */
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060097static VkResult cmd_writer_alloc_and_map(struct intel_cmd *cmd,
Chia-I Wu3c3edc02014-09-09 10:32:59 +080098 enum intel_cmd_writer_type which)
99{
100 struct intel_cmd_writer *writer = &cmd->writers[which];
101 struct intel_bo *bo;
102
103 bo = alloc_writer_bo(cmd->dev->winsys, which, writer->size);
104 if (bo) {
Chia-I Wucb2dc0d2015-03-05 16:19:42 -0700105 intel_bo_unref(writer->bo);
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800106 writer->bo = bo;
107 } else if (writer->bo) {
108 /* reuse the old bo */
109 cmd_writer_discard(cmd, which);
110 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600111 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800112 }
113
114 writer->used = 0;
Chia-I Wu00b51a82014-09-09 12:07:37 +0800115 writer->item_used = 0;
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800116
117 writer->ptr = intel_bo_map(writer->bo, true);
118 if (!writer->ptr)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600119 return VK_ERROR_UNKNOWN;
Chia-I Wu730e5362014-08-19 12:15:09 +0800120
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600121 return VK_SUCCESS;
Chia-I Wu730e5362014-08-19 12:15:09 +0800122}
123
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800124/**
125 * Unmap the buffer for submission.
126 */
127static void cmd_writer_unmap(struct intel_cmd *cmd,
128 enum intel_cmd_writer_type which)
Chia-I Wu5e25c272014-08-21 20:19:12 +0800129{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800130 struct intel_cmd_writer *writer = &cmd->writers[which];
131
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800132 intel_bo_unmap(writer->bo);
133 writer->ptr = NULL;
134}
135
136/**
137 * Grow a mapped writer to at least \p new_size. Failures are handled
138 * silently.
139 */
140void cmd_writer_grow(struct intel_cmd *cmd,
141 enum intel_cmd_writer_type which,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600142 size_t new_size)
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800143{
144 struct intel_cmd_writer *writer = &cmd->writers[which];
145 struct intel_bo *new_bo;
146 void *new_ptr;
147
148 if (new_size < writer->size << 1)
149 new_size = writer->size << 1;
150 /* STATE_BASE_ADDRESS requires page-aligned buffers */
Chia-I Wu72292b72014-09-09 10:48:33 +0800151 new_size = u_align(new_size, 4096);
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800152
153 new_bo = alloc_writer_bo(cmd->dev->winsys, which, new_size);
154 if (!new_bo) {
155 cmd_writer_discard(cmd, which);
Tony Barbour8205d902015-04-16 15:59:00 -0600156 cmd_fail(cmd, VK_ERROR_OUT_OF_DEVICE_MEMORY);
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800157 return;
158 }
159
160 /* map and copy the data over */
161 new_ptr = intel_bo_map(new_bo, true);
162 if (!new_ptr) {
Chia-I Wucb2dc0d2015-03-05 16:19:42 -0700163 intel_bo_unref(new_bo);
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800164 cmd_writer_discard(cmd, which);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600165 cmd_fail(cmd, VK_ERROR_UNKNOWN);
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800166 return;
167 }
168
Chia-I Wu72292b72014-09-09 10:48:33 +0800169 memcpy(new_ptr, writer->ptr, writer->used);
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800170
171 intel_bo_unmap(writer->bo);
Chia-I Wucb2dc0d2015-03-05 16:19:42 -0700172 intel_bo_unref(writer->bo);
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800173
174 writer->size = new_size;
175 writer->bo = new_bo;
176 writer->ptr = new_ptr;
Chia-I Wu5e25c272014-08-21 20:19:12 +0800177}
178
Chia-I Wu00b51a82014-09-09 12:07:37 +0800179/**
180 * Record an item for later decoding.
181 */
182void cmd_writer_record(struct intel_cmd *cmd,
183 enum intel_cmd_writer_type which,
184 enum intel_cmd_item_type type,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600185 size_t offset, size_t size)
Chia-I Wu00b51a82014-09-09 12:07:37 +0800186{
187 struct intel_cmd_writer *writer = &cmd->writers[which];
188 struct intel_cmd_item *item;
189
190 if (writer->item_used == writer->item_alloc) {
191 const unsigned new_alloc = (writer->item_alloc) ?
192 writer->item_alloc << 1 : 256;
193 struct intel_cmd_item *items;
194
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800195 items = intel_alloc(cmd, sizeof(writer->items[0]) * new_alloc,
Tony Barbour8205d902015-04-16 15:59:00 -0600196 0, VK_SYSTEM_ALLOC_TYPE_DEBUG);
Chia-I Wu00b51a82014-09-09 12:07:37 +0800197 if (!items) {
198 writer->item_used = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600199 cmd_fail(cmd, VK_ERROR_OUT_OF_HOST_MEMORY);
Chia-I Wu00b51a82014-09-09 12:07:37 +0800200 return;
201 }
202
203 memcpy(items, writer->items,
204 sizeof(writer->items[0]) * writer->item_alloc);
205
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800206 intel_free(cmd, writer->items);
Chia-I Wu00b51a82014-09-09 12:07:37 +0800207
208 writer->items = items;
209 writer->item_alloc = new_alloc;
210 }
211
212 item = &writer->items[writer->item_used++];
213 item->type = type;
214 item->offset = offset;
215 item->size = size;
216}
217
Chia-I Wu5e25c272014-08-21 20:19:12 +0800218static void cmd_writer_patch(struct intel_cmd *cmd,
Chia-I Wu68f319d2014-09-09 09:43:21 +0800219 enum intel_cmd_writer_type which,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600220 size_t offset, uint32_t val)
Chia-I Wu5e25c272014-08-21 20:19:12 +0800221{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800222 struct intel_cmd_writer *writer = &cmd->writers[which];
223
Chia-I Wu72292b72014-09-09 10:48:33 +0800224 assert(offset + sizeof(val) <= writer->used);
225 *((uint32_t *) ((char *) writer->ptr + offset)) = val;
Chia-I Wu5e25c272014-08-21 20:19:12 +0800226}
227
Chia-I Wu730e5362014-08-19 12:15:09 +0800228static void cmd_reset(struct intel_cmd *cmd)
229{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600230 uint32_t i;
Chia-I Wu68f319d2014-09-09 09:43:21 +0800231
232 for (i = 0; i < INTEL_CMD_WRITER_COUNT; i++)
233 cmd_writer_reset(cmd, i);
Chia-I Wue97aa0e2014-08-27 12:51:26 +0800234
Chia-I Wua57761b2014-10-14 14:27:44 +0800235 if (cmd->bind.shader_cache.entries)
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800236 intel_free(cmd, cmd->bind.shader_cache.entries);
Chia-I Wua57761b2014-10-14 14:27:44 +0800237
Chia-I Wu862c5572015-03-28 15:23:55 +0800238 if (cmd->bind.dset.graphics_data.set_offsets)
239 intel_free(cmd, cmd->bind.dset.graphics_data.set_offsets);
240 if (cmd->bind.dset.graphics_data.dynamic_offsets)
241 intel_free(cmd, cmd->bind.dset.graphics_data.dynamic_offsets);
242 if (cmd->bind.dset.compute_data.set_offsets)
243 intel_free(cmd, cmd->bind.dset.compute_data.set_offsets);
244 if (cmd->bind.dset.compute_data.dynamic_offsets)
245 intel_free(cmd, cmd->bind.dset.compute_data.dynamic_offsets);
Chia-I Wuf8385062015-01-04 16:27:24 +0800246
Chia-I Wue97aa0e2014-08-27 12:51:26 +0800247 memset(&cmd->bind, 0, sizeof(cmd->bind));
248
Chia-I Wu343b1372014-08-20 16:39:20 +0800249 cmd->reloc_used = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600250 cmd->result = VK_SUCCESS;
Chia-I Wu730e5362014-08-19 12:15:09 +0800251}
252
253static void cmd_destroy(struct intel_obj *obj)
254{
255 struct intel_cmd *cmd = intel_cmd_from_obj(obj);
256
257 intel_cmd_destroy(cmd);
258}
259
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600260VkResult intel_cmd_create(struct intel_dev *dev,
261 const VkCmdBufferCreateInfo *info,
Chia-I Wu730e5362014-08-19 12:15:09 +0800262 struct intel_cmd **cmd_ret)
263{
Chia-I Wu63883292014-08-25 13:50:26 +0800264 int pipeline_select;
Chia-I Wu730e5362014-08-19 12:15:09 +0800265 struct intel_cmd *cmd;
266
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700267 switch (info->queueNodeIndex) {
268 case INTEL_GPU_ENGINE_3D:
Chia-I Wu63883292014-08-25 13:50:26 +0800269 pipeline_select = GEN6_PIPELINE_SELECT_DW0_SELECT_3D;
270 break;
Chia-I Wu63883292014-08-25 13:50:26 +0800271 default:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600272 return VK_ERROR_INVALID_VALUE;
Chia-I Wu63883292014-08-25 13:50:26 +0800273 break;
274 }
275
Chia-I Wu545c2e12015-02-22 13:19:54 +0800276 cmd = (struct intel_cmd *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600277 sizeof(*cmd), dev->base.dbg, VK_OBJECT_TYPE_COMMAND_BUFFER, info, 0);
Chia-I Wu730e5362014-08-19 12:15:09 +0800278 if (!cmd)
Tony Barbour8205d902015-04-16 15:59:00 -0600279 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu730e5362014-08-19 12:15:09 +0800280
281 cmd->obj.destroy = cmd_destroy;
282
283 cmd->dev = dev;
Chia-I Wu0b784442014-08-25 22:54:16 +0800284 cmd->scratch_bo = dev->cmd_scratch_bo;
Chia-I Wu63883292014-08-25 13:50:26 +0800285 cmd->pipeline_select = pipeline_select;
Chia-I Wue24c3292014-08-21 14:05:23 +0800286
Chia-I Wue0cdd832014-08-25 12:38:56 +0800287 /*
Courtney Goeltzenleuchter304a1f82015-04-07 16:23:00 -0600288 * XXX This is not quite right. intel_gpu sets maxMemReferences to
Chia-I Wue0cdd832014-08-25 12:38:56 +0800289 * batch_buffer_reloc_count, but we may emit up to two relocs, for start
290 * and end offsets, for each referenced memories.
291 */
Chia-I Wu343b1372014-08-20 16:39:20 +0800292 cmd->reloc_count = dev->gpu->batch_buffer_reloc_count;
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800293 cmd->relocs = intel_alloc(cmd, sizeof(cmd->relocs[0]) * cmd->reloc_count,
Tony Barbour8205d902015-04-16 15:59:00 -0600294 4096, VK_SYSTEM_ALLOC_TYPE_INTERNAL);
Chia-I Wu343b1372014-08-20 16:39:20 +0800295 if (!cmd->relocs) {
296 intel_cmd_destroy(cmd);
Tony Barbour8205d902015-04-16 15:59:00 -0600297 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu343b1372014-08-20 16:39:20 +0800298 }
Chia-I Wu730e5362014-08-19 12:15:09 +0800299
300 *cmd_ret = cmd;
301
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600302 return VK_SUCCESS;
Chia-I Wu730e5362014-08-19 12:15:09 +0800303}
304
305void intel_cmd_destroy(struct intel_cmd *cmd)
306{
307 cmd_reset(cmd);
Chia-I Wue24c3292014-08-21 14:05:23 +0800308
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800309 intel_free(cmd, cmd->relocs);
Chia-I Wu730e5362014-08-19 12:15:09 +0800310 intel_base_destroy(&cmd->obj.base);
311}
312
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600313VkResult intel_cmd_begin(struct intel_cmd *cmd, const VkCmdBufferBeginInfo *info)
Chia-I Wu730e5362014-08-19 12:15:09 +0800314{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600315 VkResult ret;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600316 uint32_t i;
Chia-I Wu730e5362014-08-19 12:15:09 +0800317
318 cmd_reset(cmd);
319
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800320 if (cmd->flags != info->flags) {
321 cmd->flags = info->flags;
Chia-I Wu68f319d2014-09-09 09:43:21 +0800322 cmd->writers[INTEL_CMD_WRITER_BATCH].size = 0;
Chia-I Wu730e5362014-08-19 12:15:09 +0800323 }
324
Chia-I Wu68f319d2014-09-09 09:43:21 +0800325 if (!cmd->writers[INTEL_CMD_WRITER_BATCH].size) {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600326 const uint32_t size = cmd->dev->gpu->max_batch_buffer_size / 2;
327 uint32_t divider = 1;
Chia-I Wu24565ee2014-08-21 20:24:31 +0800328
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800329 if (info->flags & VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT)
Chia-I Wu24565ee2014-08-21 20:24:31 +0800330 divider *= 4;
331
Chia-I Wu68f319d2014-09-09 09:43:21 +0800332 cmd->writers[INTEL_CMD_WRITER_BATCH].size = size / divider;
Chia-I Wu15cccf72015-02-10 04:07:40 +0800333 cmd->writers[INTEL_CMD_WRITER_SURFACE].size = size / divider / 2;
334 cmd->writers[INTEL_CMD_WRITER_STATE].size = size / divider / 2;
Chia-I Wu72292b72014-09-09 10:48:33 +0800335 cmd->writers[INTEL_CMD_WRITER_INSTRUCTION].size = 16384 / divider;
Chia-I Wu24565ee2014-08-21 20:24:31 +0800336 }
337
Chia-I Wu68f319d2014-09-09 09:43:21 +0800338 for (i = 0; i < INTEL_CMD_WRITER_COUNT; i++) {
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800339 ret = cmd_writer_alloc_and_map(cmd, i);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600340 if (ret != VK_SUCCESS) {
Chia-I Wu68f319d2014-09-09 09:43:21 +0800341 cmd_reset(cmd);
342 return ret;
343 }
Chia-I Wu24565ee2014-08-21 20:24:31 +0800344 }
345
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800346 cmd_batch_begin(cmd);
347
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600348 return VK_SUCCESS;
Chia-I Wu730e5362014-08-19 12:15:09 +0800349}
350
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600351VkResult intel_cmd_end(struct intel_cmd *cmd)
Chia-I Wu730e5362014-08-19 12:15:09 +0800352{
353 struct intel_winsys *winsys = cmd->dev->winsys;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600354 uint32_t i;
Chia-I Wu730e5362014-08-19 12:15:09 +0800355
Chia-I Wub8762122014-12-01 22:51:03 +0800356 /* no matching intel_cmd_begin() */
357 if (!cmd->writers[INTEL_CMD_WRITER_BATCH].ptr)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600358 return VK_ERROR_INCOMPLETE_COMMAND_BUFFER;
Chia-I Wub8762122014-12-01 22:51:03 +0800359
Chia-I Wue24c3292014-08-21 14:05:23 +0800360 cmd_batch_end(cmd);
Chia-I Wu730e5362014-08-19 12:15:09 +0800361
Chia-I Wu343b1372014-08-20 16:39:20 +0800362 /* TODO we need a more "explicit" winsys */
Chia-I Wufdfb8ed2014-08-21 15:40:07 +0800363 for (i = 0; i < cmd->reloc_used; i++) {
Chia-I Wu343b1372014-08-20 16:39:20 +0800364 const struct intel_cmd_reloc *reloc = &cmd->relocs[i];
Chia-I Wu68f319d2014-09-09 09:43:21 +0800365 const struct intel_cmd_writer *writer = &cmd->writers[reloc->which];
Chia-I Wu343b1372014-08-20 16:39:20 +0800366 uint64_t presumed_offset;
367 int err;
368
Chia-I Wud7d1e482014-10-18 13:25:10 +0800369 /*
370 * Once a bo is used as a reloc target, libdrm_intel disallows more
371 * relocs to be added to it. That may happen when
372 * INTEL_CMD_RELOC_TARGET_IS_WRITER is set. We have to process them
373 * in another pass.
374 */
375 if (reloc->flags & INTEL_CMD_RELOC_TARGET_IS_WRITER)
376 continue;
377
Chia-I Wu72292b72014-09-09 10:48:33 +0800378 err = intel_bo_add_reloc(writer->bo, reloc->offset,
Chia-I Wud7d1e482014-10-18 13:25:10 +0800379 (struct intel_bo *) reloc->target, reloc->target_offset,
Chia-I Wu32a22462014-08-26 14:13:46 +0800380 reloc->flags, &presumed_offset);
Chia-I Wu343b1372014-08-20 16:39:20 +0800381 if (err) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600382 cmd_fail(cmd, VK_ERROR_UNKNOWN);
Chia-I Wu343b1372014-08-20 16:39:20 +0800383 break;
384 }
385
386 assert(presumed_offset == (uint64_t) (uint32_t) presumed_offset);
Chia-I Wu72292b72014-09-09 10:48:33 +0800387 cmd_writer_patch(cmd, reloc->which, reloc->offset,
Chia-I Wue24c3292014-08-21 14:05:23 +0800388 (uint32_t) presumed_offset);
Chia-I Wu343b1372014-08-20 16:39:20 +0800389 }
Chia-I Wud7d1e482014-10-18 13:25:10 +0800390 for (i = 0; i < cmd->reloc_used; i++) {
391 const struct intel_cmd_reloc *reloc = &cmd->relocs[i];
392 const struct intel_cmd_writer *writer = &cmd->writers[reloc->which];
393 uint64_t presumed_offset;
394 int err;
395
396 if (!(reloc->flags & INTEL_CMD_RELOC_TARGET_IS_WRITER))
397 continue;
398
399 err = intel_bo_add_reloc(writer->bo, reloc->offset,
400 cmd->writers[reloc->target].bo, reloc->target_offset,
401 reloc->flags & ~INTEL_CMD_RELOC_TARGET_IS_WRITER,
402 &presumed_offset);
403 if (err) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600404 cmd_fail(cmd, VK_ERROR_UNKNOWN);
Chia-I Wud7d1e482014-10-18 13:25:10 +0800405 break;
406 }
407
408 assert(presumed_offset == (uint64_t) (uint32_t) presumed_offset);
409 cmd_writer_patch(cmd, reloc->which, reloc->offset,
410 (uint32_t) presumed_offset);
411 }
Chia-I Wu343b1372014-08-20 16:39:20 +0800412
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800413 for (i = 0; i < INTEL_CMD_WRITER_COUNT; i++)
414 cmd_writer_unmap(cmd, i);
Chia-I Wu730e5362014-08-19 12:15:09 +0800415
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600416 if (cmd->result != VK_SUCCESS)
Chia-I Wu04966702014-08-20 15:05:03 +0800417 return cmd->result;
Chia-I Wue24c3292014-08-21 14:05:23 +0800418
Chia-I Wu68f319d2014-09-09 09:43:21 +0800419 if (intel_winsys_can_submit_bo(winsys,
420 &cmd->writers[INTEL_CMD_WRITER_BATCH].bo, 1))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600421 return VK_SUCCESS;
Chia-I Wu730e5362014-08-19 12:15:09 +0800422 else
Courtney Goeltzenleuchterac834522015-05-01 17:56:13 -0600423 return VK_ERROR_UNKNOWN;
Chia-I Wu730e5362014-08-19 12:15:09 +0800424}
425
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600426ICD_EXPORT VkResult VKAPI vkCreateCommandBuffer(
427 VkDevice device,
428 const VkCmdBufferCreateInfo* pCreateInfo,
429 VkCmdBuffer* pCmdBuffer)
Chia-I Wu09142132014-08-11 15:42:55 +0800430{
Chia-I Wu730e5362014-08-19 12:15:09 +0800431 struct intel_dev *dev = intel_dev(device);
432
433 return intel_cmd_create(dev, pCreateInfo,
434 (struct intel_cmd **) pCmdBuffer);
Chia-I Wu09142132014-08-11 15:42:55 +0800435}
436
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600437ICD_EXPORT VkResult VKAPI vkBeginCommandBuffer(
438 VkCmdBuffer cmdBuffer,
439 const VkCmdBufferBeginInfo *info)
Chia-I Wu09142132014-08-11 15:42:55 +0800440{
Chia-I Wu730e5362014-08-19 12:15:09 +0800441 struct intel_cmd *cmd = intel_cmd(cmdBuffer);
442
Jon Ashburnc04b4dc2015-01-08 18:48:10 -0700443 return intel_cmd_begin(cmd, info);
Chia-I Wu09142132014-08-11 15:42:55 +0800444}
445
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600446ICD_EXPORT VkResult VKAPI vkEndCommandBuffer(
447 VkCmdBuffer cmdBuffer)
Chia-I Wu09142132014-08-11 15:42:55 +0800448{
Chia-I Wu730e5362014-08-19 12:15:09 +0800449 struct intel_cmd *cmd = intel_cmd(cmdBuffer);
450
451 return intel_cmd_end(cmd);
Chia-I Wu09142132014-08-11 15:42:55 +0800452}
453
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600454ICD_EXPORT VkResult VKAPI vkResetCommandBuffer(
455 VkCmdBuffer cmdBuffer)
Chia-I Wu09142132014-08-11 15:42:55 +0800456{
Chia-I Wu730e5362014-08-19 12:15:09 +0800457 struct intel_cmd *cmd = intel_cmd(cmdBuffer);
458
459 cmd_reset(cmd);
460
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600461 return VK_SUCCESS;
Chia-I Wu09142132014-08-11 15:42:55 +0800462}