blob: 3ef9c52fe49f589c15b9271ce9bea2ffcfde429b [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 Wu513ae5b2015-07-01 19:04:59 +0800285 cmd->primary = (info->level == VK_CMD_BUFFER_LEVEL_PRIMARY);
Chia-I Wu63883292014-08-25 13:50:26 +0800286 cmd->pipeline_select = pipeline_select;
Chia-I Wue24c3292014-08-21 14:05:23 +0800287
Chia-I Wue0cdd832014-08-25 12:38:56 +0800288 /*
Courtney Goeltzenleuchter304a1f82015-04-07 16:23:00 -0600289 * XXX This is not quite right. intel_gpu sets maxMemReferences to
Chia-I Wue0cdd832014-08-25 12:38:56 +0800290 * batch_buffer_reloc_count, but we may emit up to two relocs, for start
291 * and end offsets, for each referenced memories.
292 */
Chia-I Wu343b1372014-08-20 16:39:20 +0800293 cmd->reloc_count = dev->gpu->batch_buffer_reloc_count;
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800294 cmd->relocs = intel_alloc(cmd, sizeof(cmd->relocs[0]) * cmd->reloc_count,
Tony Barbour8205d902015-04-16 15:59:00 -0600295 4096, VK_SYSTEM_ALLOC_TYPE_INTERNAL);
Chia-I Wu343b1372014-08-20 16:39:20 +0800296 if (!cmd->relocs) {
297 intel_cmd_destroy(cmd);
Tony Barbour8205d902015-04-16 15:59:00 -0600298 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu343b1372014-08-20 16:39:20 +0800299 }
Chia-I Wu730e5362014-08-19 12:15:09 +0800300
301 *cmd_ret = cmd;
302
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600303 return VK_SUCCESS;
Chia-I Wu730e5362014-08-19 12:15:09 +0800304}
305
306void intel_cmd_destroy(struct intel_cmd *cmd)
307{
308 cmd_reset(cmd);
Chia-I Wue24c3292014-08-21 14:05:23 +0800309
Chia-I Wuf9c81ef2015-02-22 13:49:15 +0800310 intel_free(cmd, cmd->relocs);
Chia-I Wu730e5362014-08-19 12:15:09 +0800311 intel_base_destroy(&cmd->obj.base);
312}
313
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600314VkResult intel_cmd_begin(struct intel_cmd *cmd, const VkCmdBufferBeginInfo *info)
Chia-I Wu730e5362014-08-19 12:15:09 +0800315{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600316 VkResult ret;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600317 uint32_t i;
Chia-I Wu730e5362014-08-19 12:15:09 +0800318
319 cmd_reset(cmd);
320
Chia-I Wu513ae5b2015-07-01 19:04:59 +0800321 if (cmd->primary) {
322 if (info->renderPass || info->framebuffer)
323 return VK_ERROR_INVALID_VALUE;
324 } else {
325 if (!info->renderPass || !info->framebuffer)
326 return VK_ERROR_INVALID_VALUE;
327
328 cmd_begin_render_pass(cmd,
329 intel_render_pass(info->renderPass),
330 intel_fb(info->framebuffer),
331 VK_RENDER_PASS_CONTENTS_INLINE);
332 }
333
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800334 if (cmd->flags != info->flags) {
335 cmd->flags = info->flags;
Chia-I Wu68f319d2014-09-09 09:43:21 +0800336 cmd->writers[INTEL_CMD_WRITER_BATCH].size = 0;
Chia-I Wu730e5362014-08-19 12:15:09 +0800337 }
338
Chia-I Wu68f319d2014-09-09 09:43:21 +0800339 if (!cmd->writers[INTEL_CMD_WRITER_BATCH].size) {
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600340 const uint32_t size = cmd->dev->gpu->max_batch_buffer_size / 2;
341 uint32_t divider = 1;
Chia-I Wu24565ee2014-08-21 20:24:31 +0800342
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800343 if (info->flags & VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT)
Chia-I Wu24565ee2014-08-21 20:24:31 +0800344 divider *= 4;
345
Chia-I Wu68f319d2014-09-09 09:43:21 +0800346 cmd->writers[INTEL_CMD_WRITER_BATCH].size = size / divider;
Chia-I Wu15cccf72015-02-10 04:07:40 +0800347 cmd->writers[INTEL_CMD_WRITER_SURFACE].size = size / divider / 2;
348 cmd->writers[INTEL_CMD_WRITER_STATE].size = size / divider / 2;
Chia-I Wu72292b72014-09-09 10:48:33 +0800349 cmd->writers[INTEL_CMD_WRITER_INSTRUCTION].size = 16384 / divider;
Chia-I Wu24565ee2014-08-21 20:24:31 +0800350 }
351
Chia-I Wu68f319d2014-09-09 09:43:21 +0800352 for (i = 0; i < INTEL_CMD_WRITER_COUNT; i++) {
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800353 ret = cmd_writer_alloc_and_map(cmd, i);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600354 if (ret != VK_SUCCESS) {
Chia-I Wu68f319d2014-09-09 09:43:21 +0800355 cmd_reset(cmd);
356 return ret;
357 }
Chia-I Wu24565ee2014-08-21 20:24:31 +0800358 }
359
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800360 cmd_batch_begin(cmd);
361
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600362 return VK_SUCCESS;
Chia-I Wu730e5362014-08-19 12:15:09 +0800363}
364
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600365VkResult intel_cmd_end(struct intel_cmd *cmd)
Chia-I Wu730e5362014-08-19 12:15:09 +0800366{
367 struct intel_winsys *winsys = cmd->dev->winsys;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600368 uint32_t i;
Chia-I Wu730e5362014-08-19 12:15:09 +0800369
Chia-I Wub8762122014-12-01 22:51:03 +0800370 /* no matching intel_cmd_begin() */
371 if (!cmd->writers[INTEL_CMD_WRITER_BATCH].ptr)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600372 return VK_ERROR_INCOMPLETE_COMMAND_BUFFER;
Chia-I Wub8762122014-12-01 22:51:03 +0800373
Chia-I Wue24c3292014-08-21 14:05:23 +0800374 cmd_batch_end(cmd);
Chia-I Wu730e5362014-08-19 12:15:09 +0800375
Chia-I Wu343b1372014-08-20 16:39:20 +0800376 /* TODO we need a more "explicit" winsys */
Chia-I Wufdfb8ed2014-08-21 15:40:07 +0800377 for (i = 0; i < cmd->reloc_used; i++) {
Chia-I Wu343b1372014-08-20 16:39:20 +0800378 const struct intel_cmd_reloc *reloc = &cmd->relocs[i];
Chia-I Wu68f319d2014-09-09 09:43:21 +0800379 const struct intel_cmd_writer *writer = &cmd->writers[reloc->which];
Chia-I Wu343b1372014-08-20 16:39:20 +0800380 uint64_t presumed_offset;
381 int err;
382
Chia-I Wud7d1e482014-10-18 13:25:10 +0800383 /*
384 * Once a bo is used as a reloc target, libdrm_intel disallows more
385 * relocs to be added to it. That may happen when
386 * INTEL_CMD_RELOC_TARGET_IS_WRITER is set. We have to process them
387 * in another pass.
388 */
389 if (reloc->flags & INTEL_CMD_RELOC_TARGET_IS_WRITER)
390 continue;
391
Chia-I Wu72292b72014-09-09 10:48:33 +0800392 err = intel_bo_add_reloc(writer->bo, reloc->offset,
Chia-I Wud7d1e482014-10-18 13:25:10 +0800393 (struct intel_bo *) reloc->target, reloc->target_offset,
Chia-I Wu32a22462014-08-26 14:13:46 +0800394 reloc->flags, &presumed_offset);
Chia-I Wu343b1372014-08-20 16:39:20 +0800395 if (err) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600396 cmd_fail(cmd, VK_ERROR_UNKNOWN);
Chia-I Wu343b1372014-08-20 16:39:20 +0800397 break;
398 }
399
400 assert(presumed_offset == (uint64_t) (uint32_t) presumed_offset);
Chia-I Wu72292b72014-09-09 10:48:33 +0800401 cmd_writer_patch(cmd, reloc->which, reloc->offset,
Chia-I Wue24c3292014-08-21 14:05:23 +0800402 (uint32_t) presumed_offset);
Chia-I Wu343b1372014-08-20 16:39:20 +0800403 }
Chia-I Wud7d1e482014-10-18 13:25:10 +0800404 for (i = 0; i < cmd->reloc_used; i++) {
405 const struct intel_cmd_reloc *reloc = &cmd->relocs[i];
406 const struct intel_cmd_writer *writer = &cmd->writers[reloc->which];
407 uint64_t presumed_offset;
408 int err;
409
410 if (!(reloc->flags & INTEL_CMD_RELOC_TARGET_IS_WRITER))
411 continue;
412
413 err = intel_bo_add_reloc(writer->bo, reloc->offset,
414 cmd->writers[reloc->target].bo, reloc->target_offset,
415 reloc->flags & ~INTEL_CMD_RELOC_TARGET_IS_WRITER,
416 &presumed_offset);
417 if (err) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600418 cmd_fail(cmd, VK_ERROR_UNKNOWN);
Chia-I Wud7d1e482014-10-18 13:25:10 +0800419 break;
420 }
421
422 assert(presumed_offset == (uint64_t) (uint32_t) presumed_offset);
423 cmd_writer_patch(cmd, reloc->which, reloc->offset,
424 (uint32_t) presumed_offset);
425 }
Chia-I Wu343b1372014-08-20 16:39:20 +0800426
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800427 for (i = 0; i < INTEL_CMD_WRITER_COUNT; i++)
428 cmd_writer_unmap(cmd, i);
Chia-I Wu730e5362014-08-19 12:15:09 +0800429
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600430 if (cmd->result != VK_SUCCESS)
Chia-I Wu04966702014-08-20 15:05:03 +0800431 return cmd->result;
Chia-I Wue24c3292014-08-21 14:05:23 +0800432
Chia-I Wu68f319d2014-09-09 09:43:21 +0800433 if (intel_winsys_can_submit_bo(winsys,
434 &cmd->writers[INTEL_CMD_WRITER_BATCH].bo, 1))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600435 return VK_SUCCESS;
Chia-I Wu730e5362014-08-19 12:15:09 +0800436 else
Courtney Goeltzenleuchterac834522015-05-01 17:56:13 -0600437 return VK_ERROR_UNKNOWN;
Chia-I Wu730e5362014-08-19 12:15:09 +0800438}
439
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600440ICD_EXPORT VkResult VKAPI vkCreateCommandBuffer(
441 VkDevice device,
442 const VkCmdBufferCreateInfo* pCreateInfo,
443 VkCmdBuffer* pCmdBuffer)
Chia-I Wu09142132014-08-11 15:42:55 +0800444{
Chia-I Wu730e5362014-08-19 12:15:09 +0800445 struct intel_dev *dev = intel_dev(device);
446
447 return intel_cmd_create(dev, pCreateInfo,
448 (struct intel_cmd **) pCmdBuffer);
Chia-I Wu09142132014-08-11 15:42:55 +0800449}
450
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600451ICD_EXPORT VkResult VKAPI vkBeginCommandBuffer(
452 VkCmdBuffer cmdBuffer,
453 const VkCmdBufferBeginInfo *info)
Chia-I Wu09142132014-08-11 15:42:55 +0800454{
Chia-I Wu730e5362014-08-19 12:15:09 +0800455 struct intel_cmd *cmd = intel_cmd(cmdBuffer);
456
Jon Ashburnc04b4dc2015-01-08 18:48:10 -0700457 return intel_cmd_begin(cmd, info);
Chia-I Wu09142132014-08-11 15:42:55 +0800458}
459
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600460ICD_EXPORT VkResult VKAPI vkEndCommandBuffer(
461 VkCmdBuffer cmdBuffer)
Chia-I Wu09142132014-08-11 15:42:55 +0800462{
Chia-I Wu730e5362014-08-19 12:15:09 +0800463 struct intel_cmd *cmd = intel_cmd(cmdBuffer);
464
465 return intel_cmd_end(cmd);
Chia-I Wu09142132014-08-11 15:42:55 +0800466}
467
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600468ICD_EXPORT VkResult VKAPI vkResetCommandBuffer(
469 VkCmdBuffer cmdBuffer)
Chia-I Wu09142132014-08-11 15:42:55 +0800470{
Chia-I Wu730e5362014-08-19 12:15:09 +0800471 struct intel_cmd *cmd = intel_cmd(cmdBuffer);
472
473 cmd_reset(cmd);
474
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600475 return VK_SUCCESS;
Chia-I Wu09142132014-08-11 15:42:55 +0800476}