ilo: hook up pipe context 3D functions
[mesa.git] / src / gallium / drivers / ilo / ilo_context.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 "util/u_blitter.h"
29 #include "intel_chipset.h"
30
31 #include "ilo_3d.h"
32 #include "ilo_blit.h"
33 #include "ilo_cp.h"
34 #include "ilo_gpgpu.h"
35 #include "ilo_query.h"
36 #include "ilo_resource.h"
37 #include "ilo_screen.h"
38 #include "ilo_shader.h"
39 #include "ilo_state.h"
40 #include "ilo_video.h"
41 #include "ilo_context.h"
42
43 static void
44 ilo_context_new_cp_batch(struct ilo_cp *cp, void *data)
45 {
46 struct ilo_context *ilo = ilo_context(data);
47
48 if (cp->ring == ILO_CP_RING_RENDER)
49 ilo_3d_new_cp_batch(ilo->hw3d);
50 }
51
52 static void
53 ilo_context_pre_cp_flush(struct ilo_cp *cp, void *data)
54 {
55 struct ilo_context *ilo = ilo_context(data);
56
57 if (cp->ring == ILO_CP_RING_RENDER)
58 ilo_3d_pre_cp_flush(ilo->hw3d);
59 }
60
61 static void
62 ilo_context_post_cp_flush(struct ilo_cp *cp, void *data)
63 {
64 struct ilo_context *ilo = ilo_context(data);
65
66 if (ilo->last_cp_bo)
67 ilo->last_cp_bo->unreference(ilo->last_cp_bo);
68
69 /* remember the just flushed bo, on which fences could wait */
70 ilo->last_cp_bo = cp->bo;
71 ilo->last_cp_bo->reference(ilo->last_cp_bo);
72
73 if (cp->ring == ILO_CP_RING_RENDER)
74 ilo_3d_post_cp_flush(ilo->hw3d);
75 }
76
77 static void
78 ilo_flush(struct pipe_context *pipe,
79 struct pipe_fence_handle **f,
80 enum pipe_flush_flags flags)
81 {
82 struct ilo_context *ilo = ilo_context(pipe);
83
84 if (f) {
85 struct ilo_fence *fence;
86
87 fence = CALLOC_STRUCT(ilo_fence);
88 if (fence) {
89 pipe_reference_init(&fence->reference, 1);
90
91 /* reference the batch bo that we want to wait on */
92 if (ilo_cp_empty(ilo->cp))
93 fence->bo = ilo->last_cp_bo;
94 else
95 fence->bo = ilo->cp->bo;
96
97 if (fence->bo)
98 fence->bo->reference(fence->bo);
99 }
100
101 *f = (struct pipe_fence_handle *) fence;
102 }
103
104 ilo_cp_flush(ilo->cp);
105 }
106
107 static void
108 ilo_context_destroy(struct pipe_context *pipe)
109 {
110 struct ilo_context *ilo = ilo_context(pipe);
111
112 if (ilo->last_cp_bo)
113 ilo->last_cp_bo->unreference(ilo->last_cp_bo);
114
115 if (ilo->blitter)
116 util_blitter_destroy(ilo->blitter);
117 if (ilo->hw3d)
118 ilo_3d_destroy(ilo->hw3d);
119 if (ilo->shader_cache)
120 ilo_shader_cache_destroy(ilo->shader_cache);
121 if (ilo->cp)
122 ilo_cp_destroy(ilo->cp);
123
124 FREE(ilo);
125 }
126
127 static struct pipe_context *
128 ilo_context_create(struct pipe_screen *screen, void *priv)
129 {
130 struct ilo_screen *is = ilo_screen(screen);
131 struct ilo_context *ilo;
132
133 ilo = CALLOC_STRUCT(ilo_context);
134 if (!ilo)
135 return NULL;
136
137 ilo->winsys = is->winsys;
138 ilo->devid = is->devid;
139 ilo->gen = is->gen;
140
141 if (IS_SNB_GT1(ilo->devid) ||
142 IS_IVB_GT1(ilo->devid) ||
143 IS_HSW_GT1(ilo->devid) ||
144 IS_BAYTRAIL(ilo->devid))
145 ilo->gt = 1;
146 else if (IS_SNB_GT2(ilo->devid) ||
147 IS_IVB_GT2(ilo->devid) ||
148 IS_HSW_GT2(ilo->devid))
149 ilo->gt = 2;
150 else
151 ilo->gt = 0;
152
153 /* stolen from classic i965 */
154 /* WM maximum threads is number of EUs times number of threads per EU. */
155 if (ilo->gen >= ILO_GEN(7)) {
156 if (ilo->gt == 1) {
157 ilo->max_wm_threads = 48;
158 ilo->max_vs_threads = 36;
159 ilo->max_gs_threads = 36;
160 ilo->urb.size = 128;
161 ilo->urb.max_vs_entries = 512;
162 ilo->urb.max_gs_entries = 192;
163 } else if (ilo->gt == 2) {
164 ilo->max_wm_threads = 172;
165 ilo->max_vs_threads = 128;
166 ilo->max_gs_threads = 128;
167 ilo->urb.size = 256;
168 ilo->urb.max_vs_entries = 704;
169 ilo->urb.max_gs_entries = 320;
170 } else {
171 assert(!"Unknown gen7 device.");
172 }
173 } else if (ilo->gen == ILO_GEN(6)) {
174 if (ilo->gt == 2) {
175 ilo->max_wm_threads = 80;
176 ilo->max_vs_threads = 60;
177 ilo->max_gs_threads = 60;
178 ilo->urb.size = 64; /* volume 5c.5 section 5.1 */
179 ilo->urb.max_vs_entries = 256; /* volume 2a (see 3DSTATE_URB) */
180 ilo->urb.max_gs_entries = 256;
181 } else {
182 ilo->max_wm_threads = 40;
183 ilo->max_vs_threads = 24;
184 ilo->max_gs_threads = 21; /* conservative; 24 if rendering disabled */
185 ilo->urb.size = 32; /* volume 5c.5 section 5.1 */
186 ilo->urb.max_vs_entries = 256; /* volume 2a (see 3DSTATE_URB) */
187 ilo->urb.max_gs_entries = 256;
188 }
189 }
190
191 ilo->cp = ilo_cp_create(ilo->winsys, is->has_llc);
192 ilo->shader_cache = ilo_shader_cache_create(ilo->winsys);
193 if (ilo->cp)
194 ilo->hw3d = ilo_3d_create(ilo->cp, ilo->gen, ilo->gt);
195
196 if (!ilo->cp || !ilo->shader_cache || !ilo->hw3d) {
197 ilo_context_destroy(&ilo->base);
198 return NULL;
199 }
200
201 ilo_cp_set_hook(ilo->cp, ILO_CP_HOOK_NEW_BATCH,
202 ilo_context_new_cp_batch, (void *) ilo);
203 ilo_cp_set_hook(ilo->cp, ILO_CP_HOOK_PRE_FLUSH,
204 ilo_context_pre_cp_flush, (void *) ilo);
205 ilo_cp_set_hook(ilo->cp, ILO_CP_HOOK_POST_FLUSH,
206 ilo_context_post_cp_flush, (void *) ilo);
207
208 ilo->dirty = ILO_DIRTY_ALL;
209
210 ilo->base.screen = screen;
211 ilo->base.priv = priv;
212
213 ilo->base.destroy = ilo_context_destroy;
214 ilo->base.flush = ilo_flush;
215
216 ilo_init_3d_functions(ilo);
217 ilo_init_query_functions(ilo);
218 ilo_init_state_functions(ilo);
219 ilo_init_blit_functions(ilo);
220 ilo_init_transfer_functions(ilo);
221 ilo_init_video_functions(ilo);
222 ilo_init_gpgpu_functions(ilo);
223
224 /* this must be called last as u_blitter is a client of the pipe context */
225 ilo->blitter = util_blitter_create(&ilo->base);
226 if (!ilo->blitter) {
227 ilo_context_destroy(&ilo->base);
228 return NULL;
229 }
230
231 return &ilo->base;
232 }
233
234 /**
235 * Initialize context-related functions.
236 */
237 void
238 ilo_init_context_functions(struct ilo_screen *is)
239 {
240 is->base.context_create = ilo_context_create;
241 }