ilo: move MI functions to ilo_builder_mi.h
[mesa.git] / src / gallium / drivers / ilo / ilo_cp.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2013 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.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "intel_winsys.h"
29
30 #include "ilo_builder_mi.h"
31 #include "ilo_shader.h"
32 #include "ilo_cp.h"
33
34 static struct intel_bo *
35 ilo_cp_end_batch(struct ilo_cp *cp, unsigned *used)
36 {
37 struct intel_bo *bo;
38
39 ilo_cp_set_owner(cp, NULL, 0);
40
41 if (!ilo_builder_batch_used(&cp->builder)) {
42 ilo_builder_batch_discard(&cp->builder);
43 return NULL;
44 }
45
46 /* see ilo_cp_space() */
47 assert(ilo_builder_batch_space(&cp->builder) >= 2);
48 ilo_builder_batch_mi_batch_buffer_end(&cp->builder);
49
50 bo = ilo_builder_end(&cp->builder, used);
51
52 /* we have to assume that kernel uploads also failed */
53 if (!bo)
54 ilo_shader_cache_invalidate(cp->shader_cache);
55
56 return bo;
57 }
58
59 /**
60 * Flush the command parser and execute the commands. When the parser buffer
61 * is empty, the callback is not invoked.
62 */
63 void
64 ilo_cp_flush_internal(struct ilo_cp *cp)
65 {
66 const bool do_exec = !(ilo_debug & ILO_DEBUG_NOHW);
67 struct intel_bo *bo;
68 unsigned used;
69 int err;
70
71 bo = ilo_cp_end_batch(cp, &used);
72 if (!bo)
73 return;
74
75 if (likely(do_exec)) {
76 err = intel_winsys_submit_bo(cp->winsys, cp->ring,
77 bo, used, cp->render_ctx, cp->one_off_flags);
78 }
79 else {
80 err = 0;
81 }
82
83 cp->one_off_flags = 0;
84
85 if (!err) {
86 if (cp->last_submitted_bo)
87 intel_bo_unreference(cp->last_submitted_bo);
88 cp->last_submitted_bo = bo;
89 intel_bo_reference(cp->last_submitted_bo);
90
91 if (cp->flush_callback)
92 cp->flush_callback(cp, cp->flush_callback_data);
93 }
94
95 ilo_builder_begin(&cp->builder);
96 }
97
98 /**
99 * Destroy the command parser.
100 */
101 void
102 ilo_cp_destroy(struct ilo_cp *cp)
103 {
104 ilo_builder_reset(&cp->builder);
105
106 intel_winsys_destroy_context(cp->winsys, cp->render_ctx);
107 FREE(cp);
108 }
109
110 /**
111 * Create a command parser.
112 */
113 struct ilo_cp *
114 ilo_cp_create(const struct ilo_dev_info *dev,
115 struct intel_winsys *winsys,
116 struct ilo_shader_cache *shc)
117 {
118 struct ilo_cp *cp;
119
120 cp = CALLOC_STRUCT(ilo_cp);
121 if (!cp)
122 return NULL;
123
124 cp->winsys = winsys;
125 cp->shader_cache = shc;
126 cp->render_ctx = intel_winsys_create_context(winsys);
127 if (!cp->render_ctx) {
128 FREE(cp);
129 return NULL;
130 }
131
132 cp->ring = INTEL_RING_RENDER;
133
134 ilo_builder_init(&cp->builder, dev, winsys);
135
136 if (!ilo_builder_begin(&cp->builder)) {
137 ilo_cp_destroy(cp);
138 return NULL;
139 }
140
141 return cp;
142 }