ilo: rename 3d_pipeline to render
[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_upload_mgr.h"
29
30 #include "ilo_blit.h"
31 #include "ilo_blitter.h"
32 #include "ilo_cp.h"
33 #include "ilo_draw.h"
34 #include "ilo_gpgpu.h"
35 #include "ilo_query.h"
36 #include "ilo_render.h"
37 #include "ilo_resource.h"
38 #include "ilo_screen.h"
39 #include "ilo_shader.h"
40 #include "ilo_state.h"
41 #include "ilo_transfer.h"
42 #include "ilo_video.h"
43 #include "ilo_context.h"
44
45 static void
46 ilo_context_cp_submitted(struct ilo_cp *cp, void *data)
47 {
48 struct ilo_context *ilo = ilo_context(data);
49
50 /* invalidate the pipeline */
51 ilo_render_invalidate(ilo->render,
52 ILO_RENDER_INVALIDATE_BATCH_BO |
53 ILO_RENDER_INVALIDATE_STATE_BO |
54 ILO_RENDER_INVALIDATE_KERNEL_BO);
55 }
56
57 static void
58 ilo_flush(struct pipe_context *pipe,
59 struct pipe_fence_handle **f,
60 unsigned flags)
61 {
62 struct ilo_context *ilo = ilo_context(pipe);
63
64 ilo_cp_submit(ilo->cp,
65 (flags & PIPE_FLUSH_END_OF_FRAME) ? "frame end" : "user request");
66
67 if (f) {
68 *f = (struct pipe_fence_handle *)
69 ilo_fence_create(pipe->screen, ilo->cp->last_submitted_bo);
70 }
71 }
72
73 static void
74 ilo_render_condition(struct pipe_context *pipe,
75 struct pipe_query *query,
76 boolean condition,
77 uint mode)
78 {
79 struct ilo_context *ilo = ilo_context(pipe);
80
81 /* reference count? */
82 ilo->render_condition.query = query;
83 ilo->render_condition.condition = condition;
84 ilo->render_condition.mode = mode;
85 }
86
87 bool
88 ilo_skip_rendering(struct ilo_context *ilo)
89 {
90 uint64_t result;
91 bool wait;
92
93 if (!ilo->render_condition.query)
94 return false;
95
96 switch (ilo->render_condition.mode) {
97 case PIPE_RENDER_COND_WAIT:
98 case PIPE_RENDER_COND_BY_REGION_WAIT:
99 wait = true;
100 break;
101 case PIPE_RENDER_COND_NO_WAIT:
102 case PIPE_RENDER_COND_BY_REGION_NO_WAIT:
103 default:
104 wait = false;
105 break;
106 }
107
108 if (ilo->base.get_query_result(&ilo->base, ilo->render_condition.query,
109 wait, (union pipe_query_result *) &result))
110 return ((bool) result == ilo->render_condition.condition);
111 else
112 return false;
113 }
114
115 static void
116 ilo_context_destroy(struct pipe_context *pipe)
117 {
118 struct ilo_context *ilo = ilo_context(pipe);
119
120 ilo_state_vector_cleanup(&ilo->state_vector);
121
122 if (ilo->uploader)
123 u_upload_destroy(ilo->uploader);
124
125 if (ilo->blitter)
126 ilo_blitter_destroy(ilo->blitter);
127 if (ilo->render)
128 ilo_render_destroy(ilo->render);
129 if (ilo->shader_cache)
130 ilo_shader_cache_destroy(ilo->shader_cache);
131 if (ilo->cp)
132 ilo_cp_destroy(ilo->cp);
133
134 util_slab_destroy(&ilo->transfer_mempool);
135
136 FREE(ilo);
137 }
138
139 static struct pipe_context *
140 ilo_context_create(struct pipe_screen *screen, void *priv)
141 {
142 struct ilo_screen *is = ilo_screen(screen);
143 struct ilo_context *ilo;
144
145 ilo = CALLOC_STRUCT(ilo_context);
146 if (!ilo)
147 return NULL;
148
149 ilo->winsys = is->winsys;
150 ilo->dev = &is->dev;
151
152 /*
153 * initialize first, otherwise it may not be safe to call
154 * ilo_context_destroy() on errors
155 */
156 util_slab_create(&ilo->transfer_mempool,
157 sizeof(struct ilo_transfer), 64, UTIL_SLAB_SINGLETHREADED);
158
159 ilo->shader_cache = ilo_shader_cache_create();
160 ilo->cp = ilo_cp_create(ilo->dev, ilo->winsys, ilo->shader_cache);
161 if (ilo->cp)
162 ilo->render = ilo_render_create(&ilo->cp->builder);
163
164 if (!ilo->cp || !ilo->shader_cache || !ilo->render) {
165 ilo_context_destroy(&ilo->base);
166 return NULL;
167 }
168
169 ilo_cp_set_submit_callback(ilo->cp,
170 ilo_context_cp_submitted, (void *) ilo);
171
172 ilo->base.screen = screen;
173 ilo->base.priv = priv;
174
175 ilo->base.destroy = ilo_context_destroy;
176 ilo->base.flush = ilo_flush;
177 ilo->base.render_condition = ilo_render_condition;
178
179 ilo_init_draw_functions(ilo);
180 ilo_init_query_functions(ilo);
181 ilo_init_state_functions(ilo);
182 ilo_init_blit_functions(ilo);
183 ilo_init_transfer_functions(ilo);
184 ilo_init_video_functions(ilo);
185 ilo_init_gpgpu_functions(ilo);
186
187 ilo_init_draw(ilo);
188 ilo_state_vector_init(ilo->dev, &ilo->state_vector);
189
190 /*
191 * These must be called last as u_upload/u_blitter are clients of the pipe
192 * context.
193 */
194 ilo->uploader = u_upload_create(&ilo->base, 1024 * 1024, 16,
195 PIPE_BIND_CONSTANT_BUFFER | PIPE_BIND_INDEX_BUFFER);
196 if (!ilo->uploader) {
197 ilo_context_destroy(&ilo->base);
198 return NULL;
199 }
200
201 ilo->blitter = ilo_blitter_create(ilo);
202 if (!ilo->blitter) {
203 ilo_context_destroy(&ilo->base);
204 return NULL;
205 }
206
207 return &ilo->base;
208 }
209
210 /**
211 * Initialize context-related functions.
212 */
213 void
214 ilo_init_context_functions(struct ilo_screen *is)
215 {
216 is->base.context_create = ilo_context_create;
217 }