ilo: rename ILO_DEBUG=3d
[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 const struct ilo_cp_owner ilo_cp_default_owner;
35
36 static void
37 ilo_cp_release_owner(struct ilo_cp *cp)
38 {
39 if (cp->owner != &ilo_cp_default_owner) {
40 const struct ilo_cp_owner *owner = cp->owner;
41
42 cp->owner = &ilo_cp_default_owner;
43
44 assert(ilo_cp_space(cp) >= owner->reserve);
45 owner->release(cp, owner->data);
46 }
47 }
48
49 /**
50 * Set the parser owner. If this is a new owner or a new ring, the old owner
51 * is released and the new owner's own() is called.
52 *
53 * The parser may implicitly submit if there is a ring change or there is not
54 * enough space for the new owner.
55 */
56 void
57 ilo_cp_set_owner(struct ilo_cp *cp, enum intel_ring_type ring,
58 const struct ilo_cp_owner *owner)
59 {
60 if (!owner)
61 owner = &ilo_cp_default_owner;
62
63 if (cp->ring != ring) {
64 ilo_cp_submit(cp, "ring change");
65 cp->ring = ring;
66 }
67
68 if (cp->owner != owner) {
69 ilo_cp_release_owner(cp);
70
71 /* multiply by 2 because there are own() and release() */
72 if (ilo_cp_space(cp) < owner->reserve * 2) {
73 ilo_cp_submit(cp, "new owner");
74 assert(ilo_cp_space(cp) >= owner->reserve * 2);
75 }
76
77 cp->owner = owner;
78
79 assert(ilo_cp_space(cp) >= owner->reserve);
80 cp->owner->own(cp, cp->owner->data);
81 }
82 }
83
84 static struct intel_bo *
85 ilo_cp_end_batch(struct ilo_cp *cp, unsigned *used)
86 {
87 struct intel_bo *bo;
88
89 ilo_cp_release_owner(cp);
90
91 if (!ilo_builder_batch_used(&cp->builder)) {
92 ilo_builder_batch_discard(&cp->builder);
93 return NULL;
94 }
95
96 /* see ilo_cp_space() */
97 assert(ilo_builder_batch_space(&cp->builder) >= 2);
98 gen6_mi_batch_buffer_end(&cp->builder);
99
100 bo = ilo_builder_end(&cp->builder, used);
101
102 /* we have to assume that kernel uploads also failed */
103 if (!bo)
104 ilo_shader_cache_invalidate(cp->shader_cache);
105
106 return bo;
107 }
108
109 /**
110 * Flush the command parser and execute the commands. When the parser buffer
111 * is empty, the callback is not invoked.
112 */
113 void
114 ilo_cp_submit_internal(struct ilo_cp *cp)
115 {
116 const bool do_exec = !(ilo_debug & ILO_DEBUG_NOHW);
117 struct intel_bo *bo;
118 unsigned used;
119 int err;
120
121 bo = ilo_cp_end_batch(cp, &used);
122 if (!bo)
123 return;
124
125 if (likely(do_exec)) {
126 err = intel_winsys_submit_bo(cp->winsys, cp->ring,
127 bo, used, cp->render_ctx, cp->one_off_flags);
128 }
129 else {
130 err = 0;
131 }
132
133 cp->one_off_flags = 0;
134
135 if (!err) {
136 if (cp->last_submitted_bo)
137 intel_bo_unreference(cp->last_submitted_bo);
138 cp->last_submitted_bo = bo;
139 intel_bo_reference(cp->last_submitted_bo);
140
141 if (ilo_debug & ILO_DEBUG_BATCH)
142 ilo_builder_decode(&cp->builder);
143
144 if (cp->submit_callback)
145 cp->submit_callback(cp, cp->submit_callback_data);
146 }
147
148 ilo_builder_begin(&cp->builder);
149 }
150
151 /**
152 * Destroy the command parser.
153 */
154 void
155 ilo_cp_destroy(struct ilo_cp *cp)
156 {
157 ilo_builder_reset(&cp->builder);
158
159 intel_winsys_destroy_context(cp->winsys, cp->render_ctx);
160 FREE(cp);
161 }
162
163 /**
164 * Create a command parser.
165 */
166 struct ilo_cp *
167 ilo_cp_create(const struct ilo_dev_info *dev,
168 struct intel_winsys *winsys,
169 struct ilo_shader_cache *shc)
170 {
171 struct ilo_cp *cp;
172
173 cp = CALLOC_STRUCT(ilo_cp);
174 if (!cp)
175 return NULL;
176
177 cp->winsys = winsys;
178 cp->shader_cache = shc;
179 cp->render_ctx = intel_winsys_create_context(winsys);
180 if (!cp->render_ctx) {
181 FREE(cp);
182 return NULL;
183 }
184
185 cp->ring = INTEL_RING_RENDER;
186 cp->owner = &ilo_cp_default_owner;
187
188 ilo_builder_init(&cp->builder, dev, winsys);
189
190 if (!ilo_builder_begin(&cp->builder)) {
191 ilo_cp_destroy(cp);
192 return NULL;
193 }
194
195 return cp;
196 }