• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014-2018 NVIDIA Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <inttypes.h>
25 #include <stdlib.h>
26 
27 #include "util/u_debug.h"
28 #include "util/u_draw.h"
29 #include "util/u_inlines.h"
30 #include "util/u_upload_mgr.h"
31 
32 #include "tegra_context.h"
33 #include "tegra_resource.h"
34 #include "tegra_screen.h"
35 
36 static void
tegra_destroy(struct pipe_context * pcontext)37 tegra_destroy(struct pipe_context *pcontext)
38 {
39    struct tegra_context *context = to_tegra_context(pcontext);
40 
41    if (context->base.stream_uploader)
42       u_upload_destroy(context->base.stream_uploader);
43 
44    context->gpu->destroy(context->gpu);
45    free(context);
46 }
47 
48 static void
tegra_draw_vbo(struct pipe_context * pcontext,const struct pipe_draw_info * pinfo,unsigned drawid_offset,const struct pipe_draw_indirect_info * pindirect,const struct pipe_draw_start_count_bias * draws,unsigned num_draws)49 tegra_draw_vbo(struct pipe_context *pcontext,
50                const struct pipe_draw_info *pinfo,
51                unsigned drawid_offset,
52                const struct pipe_draw_indirect_info *pindirect,
53                const struct pipe_draw_start_count_bias *draws,
54                unsigned num_draws)
55 {
56    if (num_draws > 1) {
57       util_draw_multi(pcontext, pinfo, drawid_offset, pindirect, draws, num_draws);
58       return;
59    }
60 
61    if (!pindirect && (!draws[0].count || !pinfo->instance_count))
62       return;
63 
64    struct tegra_context *context = to_tegra_context(pcontext);
65    struct pipe_draw_indirect_info indirect;
66    struct pipe_draw_info info;
67 
68    if (pinfo && ((pindirect && pindirect->buffer) || pinfo->index_size)) {
69       memcpy(&info, pinfo, sizeof(info));
70 
71       if (pindirect && pindirect->buffer) {
72          memcpy(&indirect, pindirect, sizeof(indirect));
73          indirect.buffer = tegra_resource_unwrap(pindirect->buffer);
74          indirect.indirect_draw_count = tegra_resource_unwrap(pindirect->indirect_draw_count);
75          pindirect = &indirect;
76       }
77 
78       if (pinfo->index_size && !pinfo->has_user_indices)
79          info.index.resource = tegra_resource_unwrap(info.index.resource);
80 
81       pinfo = &info;
82    }
83 
84    context->gpu->draw_vbo(context->gpu, pinfo, drawid_offset, pindirect, draws, num_draws);
85 }
86 
87 static void
tegra_render_condition(struct pipe_context * pcontext,struct pipe_query * query,bool condition,unsigned int mode)88 tegra_render_condition(struct pipe_context *pcontext,
89                        struct pipe_query *query,
90                        bool condition,
91                        unsigned int mode)
92 {
93    struct tegra_context *context = to_tegra_context(pcontext);
94 
95    context->gpu->render_condition(context->gpu, query, condition, mode);
96 }
97 
98 static struct pipe_query *
tegra_create_query(struct pipe_context * pcontext,unsigned int query_type,unsigned int index)99 tegra_create_query(struct pipe_context *pcontext, unsigned int query_type,
100                    unsigned int index)
101 {
102    struct tegra_context *context = to_tegra_context(pcontext);
103 
104    return context->gpu->create_query(context->gpu, query_type, index);
105 }
106 
107 static struct pipe_query *
tegra_create_batch_query(struct pipe_context * pcontext,unsigned int num_queries,unsigned int * queries)108 tegra_create_batch_query(struct pipe_context *pcontext,
109                          unsigned int num_queries,
110                          unsigned int *queries)
111 {
112    struct tegra_context *context = to_tegra_context(pcontext);
113 
114    return context->gpu->create_batch_query(context->gpu, num_queries,
115                                            queries);
116 }
117 
118 static void
tegra_destroy_query(struct pipe_context * pcontext,struct pipe_query * query)119 tegra_destroy_query(struct pipe_context *pcontext, struct pipe_query *query)
120 {
121    struct tegra_context *context = to_tegra_context(pcontext);
122 
123    context->gpu->destroy_query(context->gpu, query);
124 }
125 
126 static bool
tegra_begin_query(struct pipe_context * pcontext,struct pipe_query * query)127 tegra_begin_query(struct pipe_context *pcontext, struct pipe_query *query)
128 {
129    struct tegra_context *context = to_tegra_context(pcontext);
130 
131    return context->gpu->begin_query(context->gpu, query);
132 }
133 
134 static bool
tegra_end_query(struct pipe_context * pcontext,struct pipe_query * query)135 tegra_end_query(struct pipe_context *pcontext, struct pipe_query *query)
136 {
137    struct tegra_context *context = to_tegra_context(pcontext);
138 
139    return context->gpu->end_query(context->gpu, query);
140 }
141 
142 static bool
tegra_get_query_result(struct pipe_context * pcontext,struct pipe_query * query,bool wait,union pipe_query_result * result)143 tegra_get_query_result(struct pipe_context *pcontext,
144                        struct pipe_query *query,
145                        bool wait,
146                        union pipe_query_result *result)
147 {
148    struct tegra_context *context = to_tegra_context(pcontext);
149 
150    return context->gpu->get_query_result(context->gpu, query, wait,
151                      result);
152 }
153 
154 static void
tegra_get_query_result_resource(struct pipe_context * pcontext,struct pipe_query * query,bool wait,enum pipe_query_value_type result_type,int index,struct pipe_resource * resource,unsigned int offset)155 tegra_get_query_result_resource(struct pipe_context *pcontext,
156                                 struct pipe_query *query,
157                                 bool wait,
158                                 enum pipe_query_value_type result_type,
159                                 int index,
160                                 struct pipe_resource *resource,
161                                 unsigned int offset)
162 {
163    struct tegra_context *context = to_tegra_context(pcontext);
164 
165    context->gpu->get_query_result_resource(context->gpu, query, wait,
166                                            result_type, index, resource,
167                                            offset);
168 }
169 
170 static void
tegra_set_active_query_state(struct pipe_context * pcontext,bool enable)171 tegra_set_active_query_state(struct pipe_context *pcontext, bool enable)
172 {
173    struct tegra_context *context = to_tegra_context(pcontext);
174 
175    context->gpu->set_active_query_state(context->gpu, enable);
176 }
177 
178 static void *
tegra_create_blend_state(struct pipe_context * pcontext,const struct pipe_blend_state * cso)179 tegra_create_blend_state(struct pipe_context *pcontext,
180                          const struct pipe_blend_state *cso)
181 {
182    struct tegra_context *context = to_tegra_context(pcontext);
183 
184    return context->gpu->create_blend_state(context->gpu, cso);
185 }
186 
187 static void
tegra_bind_blend_state(struct pipe_context * pcontext,void * so)188 tegra_bind_blend_state(struct pipe_context *pcontext, void *so)
189 {
190    struct tegra_context *context = to_tegra_context(pcontext);
191 
192    context->gpu->bind_blend_state(context->gpu, so);
193 }
194 
195 static void
tegra_delete_blend_state(struct pipe_context * pcontext,void * so)196 tegra_delete_blend_state(struct pipe_context *pcontext, void *so)
197 {
198    struct tegra_context *context = to_tegra_context(pcontext);
199 
200    context->gpu->delete_blend_state(context->gpu, so);
201 }
202 
203 static void *
tegra_create_sampler_state(struct pipe_context * pcontext,const struct pipe_sampler_state * cso)204 tegra_create_sampler_state(struct pipe_context *pcontext,
205                            const struct pipe_sampler_state *cso)
206 {
207    struct tegra_context *context = to_tegra_context(pcontext);
208 
209    return context->gpu->create_sampler_state(context->gpu, cso);
210 }
211 
212 static void
tegra_bind_sampler_states(struct pipe_context * pcontext,unsigned shader,unsigned start_slot,unsigned num_samplers,void ** samplers)213 tegra_bind_sampler_states(struct pipe_context *pcontext, unsigned shader,
214                           unsigned start_slot, unsigned num_samplers,
215                           void **samplers)
216 {
217    struct tegra_context *context = to_tegra_context(pcontext);
218 
219    context->gpu->bind_sampler_states(context->gpu, shader, start_slot,
220                                      num_samplers, samplers);
221 }
222 
223 static void
tegra_delete_sampler_state(struct pipe_context * pcontext,void * so)224 tegra_delete_sampler_state(struct pipe_context *pcontext, void *so)
225 {
226    struct tegra_context *context = to_tegra_context(pcontext);
227 
228    context->gpu->delete_sampler_state(context->gpu, so);
229 }
230 
231 static void *
tegra_create_rasterizer_state(struct pipe_context * pcontext,const struct pipe_rasterizer_state * cso)232 tegra_create_rasterizer_state(struct pipe_context *pcontext,
233                               const struct pipe_rasterizer_state *cso)
234 {
235    struct tegra_context *context = to_tegra_context(pcontext);
236 
237    return context->gpu->create_rasterizer_state(context->gpu, cso);
238 }
239 
240 static void
tegra_bind_rasterizer_state(struct pipe_context * pcontext,void * so)241 tegra_bind_rasterizer_state(struct pipe_context *pcontext, void *so)
242 {
243    struct tegra_context *context = to_tegra_context(pcontext);
244 
245    context->gpu->bind_rasterizer_state(context->gpu, so);
246 }
247 
248 static void
tegra_delete_rasterizer_state(struct pipe_context * pcontext,void * so)249 tegra_delete_rasterizer_state(struct pipe_context *pcontext, void *so)
250 {
251    struct tegra_context *context = to_tegra_context(pcontext);
252 
253    context->gpu->delete_rasterizer_state(context->gpu, so);
254 }
255 
256 static void *
tegra_create_depth_stencil_alpha_state(struct pipe_context * pcontext,const struct pipe_depth_stencil_alpha_state * cso)257 tegra_create_depth_stencil_alpha_state(struct pipe_context *pcontext,
258                                        const struct pipe_depth_stencil_alpha_state *cso)
259 {
260    struct tegra_context *context = to_tegra_context(pcontext);
261 
262    return context->gpu->create_depth_stencil_alpha_state(context->gpu, cso);
263 }
264 
265 static void
tegra_bind_depth_stencil_alpha_state(struct pipe_context * pcontext,void * so)266 tegra_bind_depth_stencil_alpha_state(struct pipe_context *pcontext, void *so)
267 {
268    struct tegra_context *context = to_tegra_context(pcontext);
269 
270    context->gpu->bind_depth_stencil_alpha_state(context->gpu, so);
271 }
272 
273 static void
tegra_delete_depth_stencil_alpha_state(struct pipe_context * pcontext,void * so)274 tegra_delete_depth_stencil_alpha_state(struct pipe_context *pcontext, void *so)
275 {
276    struct tegra_context *context = to_tegra_context(pcontext);
277 
278    context->gpu->delete_depth_stencil_alpha_state(context->gpu, so);
279 }
280 
281 static void *
tegra_create_fs_state(struct pipe_context * pcontext,const struct pipe_shader_state * cso)282 tegra_create_fs_state(struct pipe_context *pcontext,
283                       const struct pipe_shader_state *cso)
284 {
285    struct tegra_context *context = to_tegra_context(pcontext);
286 
287    return context->gpu->create_fs_state(context->gpu, cso);
288 }
289 
290 static void
tegra_bind_fs_state(struct pipe_context * pcontext,void * so)291 tegra_bind_fs_state(struct pipe_context *pcontext, void *so)
292 {
293    struct tegra_context *context = to_tegra_context(pcontext);
294 
295    context->gpu->bind_fs_state(context->gpu, so);
296 }
297 
298 static void
tegra_delete_fs_state(struct pipe_context * pcontext,void * so)299 tegra_delete_fs_state(struct pipe_context *pcontext, void *so)
300 {
301    struct tegra_context *context = to_tegra_context(pcontext);
302 
303    context->gpu->delete_fs_state(context->gpu, so);
304 }
305 
306 static void *
tegra_create_vs_state(struct pipe_context * pcontext,const struct pipe_shader_state * cso)307 tegra_create_vs_state(struct pipe_context *pcontext,
308                       const struct pipe_shader_state *cso)
309 {
310    struct tegra_context *context = to_tegra_context(pcontext);
311 
312    return context->gpu->create_vs_state(context->gpu, cso);
313 }
314 
315 static void
tegra_bind_vs_state(struct pipe_context * pcontext,void * so)316 tegra_bind_vs_state(struct pipe_context *pcontext, void *so)
317 {
318    struct tegra_context *context = to_tegra_context(pcontext);
319 
320    context->gpu->bind_vs_state(context->gpu, so);
321 }
322 
323 static void
tegra_delete_vs_state(struct pipe_context * pcontext,void * so)324 tegra_delete_vs_state(struct pipe_context *pcontext, void *so)
325 {
326    struct tegra_context *context = to_tegra_context(pcontext);
327 
328    context->gpu->delete_vs_state(context->gpu, so);
329 }
330 
331 static void *
tegra_create_gs_state(struct pipe_context * pcontext,const struct pipe_shader_state * cso)332 tegra_create_gs_state(struct pipe_context *pcontext,
333                       const struct pipe_shader_state *cso)
334 {
335    struct tegra_context *context = to_tegra_context(pcontext);
336 
337    return context->gpu->create_gs_state(context->gpu, cso);
338 }
339 
340 static void
tegra_bind_gs_state(struct pipe_context * pcontext,void * so)341 tegra_bind_gs_state(struct pipe_context *pcontext, void *so)
342 {
343    struct tegra_context *context = to_tegra_context(pcontext);
344 
345    context->gpu->bind_gs_state(context->gpu, so);
346 }
347 
348 static void
tegra_delete_gs_state(struct pipe_context * pcontext,void * so)349 tegra_delete_gs_state(struct pipe_context *pcontext, void *so)
350 {
351    struct tegra_context *context = to_tegra_context(pcontext);
352 
353    context->gpu->delete_gs_state(context->gpu, so);
354 }
355 
356 static void *
tegra_create_tcs_state(struct pipe_context * pcontext,const struct pipe_shader_state * cso)357 tegra_create_tcs_state(struct pipe_context *pcontext,
358                        const struct pipe_shader_state *cso)
359 {
360    struct tegra_context *context = to_tegra_context(pcontext);
361 
362    return context->gpu->create_tcs_state(context->gpu, cso);
363 }
364 
365 static void
tegra_bind_tcs_state(struct pipe_context * pcontext,void * so)366 tegra_bind_tcs_state(struct pipe_context *pcontext, void *so)
367 {
368    struct tegra_context *context = to_tegra_context(pcontext);
369 
370    context->gpu->bind_tcs_state(context->gpu, so);
371 }
372 
373 static void
tegra_delete_tcs_state(struct pipe_context * pcontext,void * so)374 tegra_delete_tcs_state(struct pipe_context *pcontext, void *so)
375 {
376    struct tegra_context *context = to_tegra_context(pcontext);
377 
378    context->gpu->delete_tcs_state(context->gpu, so);
379 }
380 
381 static void *
tegra_create_tes_state(struct pipe_context * pcontext,const struct pipe_shader_state * cso)382 tegra_create_tes_state(struct pipe_context *pcontext,
383                        const struct pipe_shader_state *cso)
384 {
385    struct tegra_context *context = to_tegra_context(pcontext);
386 
387    return context->gpu->create_tes_state(context->gpu, cso);
388 }
389 
390 static void
tegra_bind_tes_state(struct pipe_context * pcontext,void * so)391 tegra_bind_tes_state(struct pipe_context *pcontext, void *so)
392 {
393    struct tegra_context *context = to_tegra_context(pcontext);
394 
395    context->gpu->bind_tes_state(context->gpu, so);
396 }
397 
398 static void
tegra_delete_tes_state(struct pipe_context * pcontext,void * so)399 tegra_delete_tes_state(struct pipe_context *pcontext, void *so)
400 {
401    struct tegra_context *context = to_tegra_context(pcontext);
402 
403    context->gpu->delete_tes_state(context->gpu, so);
404 }
405 
406 static void *
tegra_create_vertex_elements_state(struct pipe_context * pcontext,unsigned num_elements,const struct pipe_vertex_element * elements)407 tegra_create_vertex_elements_state(struct pipe_context *pcontext,
408                                    unsigned num_elements,
409                                    const struct pipe_vertex_element *elements)
410 {
411    struct tegra_context *context = to_tegra_context(pcontext);
412 
413    return context->gpu->create_vertex_elements_state(context->gpu,
414                                                      num_elements,
415                                                      elements);
416 }
417 
418 static void
tegra_bind_vertex_elements_state(struct pipe_context * pcontext,void * so)419 tegra_bind_vertex_elements_state(struct pipe_context *pcontext, void *so)
420 {
421    struct tegra_context *context = to_tegra_context(pcontext);
422 
423    context->gpu->bind_vertex_elements_state(context->gpu, so);
424 }
425 
426 static void
tegra_delete_vertex_elements_state(struct pipe_context * pcontext,void * so)427 tegra_delete_vertex_elements_state(struct pipe_context *pcontext, void *so)
428 {
429    struct tegra_context *context = to_tegra_context(pcontext);
430 
431    context->gpu->delete_vertex_elements_state(context->gpu, so);
432 }
433 
434 static void
tegra_set_blend_color(struct pipe_context * pcontext,const struct pipe_blend_color * color)435 tegra_set_blend_color(struct pipe_context *pcontext,
436                       const struct pipe_blend_color *color)
437 {
438    struct tegra_context *context = to_tegra_context(pcontext);
439 
440    context->gpu->set_blend_color(context->gpu, color);
441 }
442 
443 static void
tegra_set_stencil_ref(struct pipe_context * pcontext,const struct pipe_stencil_ref ref)444 tegra_set_stencil_ref(struct pipe_context *pcontext,
445                       const struct pipe_stencil_ref ref)
446 {
447    struct tegra_context *context = to_tegra_context(pcontext);
448 
449    context->gpu->set_stencil_ref(context->gpu, ref);
450 }
451 
452 static void
tegra_set_sample_mask(struct pipe_context * pcontext,unsigned int mask)453 tegra_set_sample_mask(struct pipe_context *pcontext, unsigned int mask)
454 {
455    struct tegra_context *context = to_tegra_context(pcontext);
456 
457    context->gpu->set_sample_mask(context->gpu, mask);
458 }
459 
460 static void
tegra_set_min_samples(struct pipe_context * pcontext,unsigned int samples)461 tegra_set_min_samples(struct pipe_context *pcontext, unsigned int samples)
462 {
463    struct tegra_context *context = to_tegra_context(pcontext);
464 
465    context->gpu->set_min_samples(context->gpu, samples);
466 }
467 
468 static void
tegra_set_clip_state(struct pipe_context * pcontext,const struct pipe_clip_state * state)469 tegra_set_clip_state(struct pipe_context *pcontext,
470                      const struct pipe_clip_state *state)
471 {
472    struct tegra_context *context = to_tegra_context(pcontext);
473 
474    context->gpu->set_clip_state(context->gpu, state);
475 }
476 
477 static void
tegra_set_constant_buffer(struct pipe_context * pcontext,unsigned int shader,unsigned int index,bool take_ownership,const struct pipe_constant_buffer * buf)478 tegra_set_constant_buffer(struct pipe_context *pcontext, unsigned int shader,
479                           unsigned int index, bool take_ownership,
480                           const struct pipe_constant_buffer *buf)
481 {
482    struct tegra_context *context = to_tegra_context(pcontext);
483    struct pipe_constant_buffer buffer;
484 
485    if (buf && buf->buffer) {
486       memcpy(&buffer, buf, sizeof(buffer));
487       buffer.buffer = tegra_resource_unwrap(buffer.buffer);
488       buf = &buffer;
489    }
490 
491    context->gpu->set_constant_buffer(context->gpu, shader, index, take_ownership, buf);
492 }
493 
494 static void
tegra_set_framebuffer_state(struct pipe_context * pcontext,const struct pipe_framebuffer_state * fb)495 tegra_set_framebuffer_state(struct pipe_context *pcontext,
496                             const struct pipe_framebuffer_state *fb)
497 {
498    struct tegra_context *context = to_tegra_context(pcontext);
499    struct pipe_framebuffer_state state;
500    unsigned i;
501 
502    if (fb) {
503       memcpy(&state, fb, sizeof(state));
504 
505       for (i = 0; i < fb->nr_cbufs; i++)
506          state.cbufs[i] = tegra_surface_unwrap(fb->cbufs[i]);
507 
508       while (i < PIPE_MAX_COLOR_BUFS)
509          state.cbufs[i++] = NULL;
510 
511       state.zsbuf = tegra_surface_unwrap(fb->zsbuf);
512 
513       fb = &state;
514    }
515 
516    context->gpu->set_framebuffer_state(context->gpu, fb);
517 }
518 
519 static void
tegra_set_polygon_stipple(struct pipe_context * pcontext,const struct pipe_poly_stipple * stipple)520 tegra_set_polygon_stipple(struct pipe_context *pcontext,
521                           const struct pipe_poly_stipple *stipple)
522 {
523    struct tegra_context *context = to_tegra_context(pcontext);
524 
525    context->gpu->set_polygon_stipple(context->gpu, stipple);
526 }
527 
528 static void
tegra_set_scissor_states(struct pipe_context * pcontext,unsigned start_slot,unsigned num_scissors,const struct pipe_scissor_state * scissors)529 tegra_set_scissor_states(struct pipe_context *pcontext, unsigned start_slot,
530                          unsigned num_scissors,
531                          const struct pipe_scissor_state *scissors)
532 {
533    struct tegra_context *context = to_tegra_context(pcontext);
534 
535    context->gpu->set_scissor_states(context->gpu, start_slot, num_scissors,
536                                     scissors);
537 }
538 
539 static void
tegra_set_window_rectangles(struct pipe_context * pcontext,bool include,unsigned int num_rectangles,const struct pipe_scissor_state * rectangles)540 tegra_set_window_rectangles(struct pipe_context *pcontext, bool include,
541                             unsigned int num_rectangles,
542                             const struct pipe_scissor_state *rectangles)
543 {
544    struct tegra_context *context = to_tegra_context(pcontext);
545 
546    context->gpu->set_window_rectangles(context->gpu, include, num_rectangles,
547                                        rectangles);
548 }
549 
550 static void
tegra_set_viewport_states(struct pipe_context * pcontext,unsigned start_slot,unsigned num_viewports,const struct pipe_viewport_state * viewports)551 tegra_set_viewport_states(struct pipe_context *pcontext, unsigned start_slot,
552                           unsigned num_viewports,
553                           const struct pipe_viewport_state *viewports)
554 {
555    struct tegra_context *context = to_tegra_context(pcontext);
556 
557    context->gpu->set_viewport_states(context->gpu, start_slot, num_viewports,
558                                      viewports);
559 }
560 
561 static void
tegra_set_sampler_views(struct pipe_context * pcontext,unsigned shader,unsigned start_slot,unsigned num_views,unsigned unbind_num_trailing_slots,bool take_ownership,struct pipe_sampler_view ** pviews)562 tegra_set_sampler_views(struct pipe_context *pcontext, unsigned shader,
563                         unsigned start_slot, unsigned num_views,
564                         unsigned unbind_num_trailing_slots,
565                         bool take_ownership,
566                         struct pipe_sampler_view **pviews)
567 {
568    struct pipe_sampler_view *views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
569    struct tegra_context *context = to_tegra_context(pcontext);
570    unsigned i;
571 
572    for (i = 0; i < num_views; i++)
573       views[i] = tegra_sampler_view_unwrap(pviews[i]);
574 
575    context->gpu->set_sampler_views(context->gpu, shader, start_slot,
576                                    num_views, unbind_num_trailing_slots,
577                                    take_ownership, views);
578 }
579 
580 static void
tegra_set_tess_state(struct pipe_context * pcontext,const float default_outer_level[4],const float default_inner_level[2])581 tegra_set_tess_state(struct pipe_context *pcontext,
582                      const float default_outer_level[4],
583                      const float default_inner_level[2])
584 {
585    struct tegra_context *context = to_tegra_context(pcontext);
586 
587    context->gpu->set_tess_state(context->gpu, default_outer_level,
588                                 default_inner_level);
589 }
590 
591 static void
tegra_set_debug_callback(struct pipe_context * pcontext,const struct pipe_debug_callback * callback)592 tegra_set_debug_callback(struct pipe_context *pcontext,
593                          const struct pipe_debug_callback *callback)
594 {
595    struct tegra_context *context = to_tegra_context(pcontext);
596 
597    context->gpu->set_debug_callback(context->gpu, callback);
598 }
599 
600 static void
tegra_set_shader_buffers(struct pipe_context * pcontext,unsigned int shader,unsigned start,unsigned count,const struct pipe_shader_buffer * buffers,unsigned writable_bitmask)601 tegra_set_shader_buffers(struct pipe_context *pcontext, unsigned int shader,
602                          unsigned start, unsigned count,
603                          const struct pipe_shader_buffer *buffers,
604                          unsigned writable_bitmask)
605 {
606    struct tegra_context *context = to_tegra_context(pcontext);
607 
608    context->gpu->set_shader_buffers(context->gpu, shader, start, count,
609                                     buffers, writable_bitmask);
610 }
611 
612 static void
tegra_set_shader_images(struct pipe_context * pcontext,unsigned int shader,unsigned start,unsigned count,unsigned unbind_num_trailing_slots,const struct pipe_image_view * images)613 tegra_set_shader_images(struct pipe_context *pcontext, unsigned int shader,
614                         unsigned start, unsigned count,
615                         unsigned unbind_num_trailing_slots,
616                         const struct pipe_image_view *images)
617 {
618    struct tegra_context *context = to_tegra_context(pcontext);
619 
620    context->gpu->set_shader_images(context->gpu, shader, start, count,
621                                    unbind_num_trailing_slots, images);
622 }
623 
624 static void
tegra_set_vertex_buffers(struct pipe_context * pcontext,unsigned start_slot,unsigned num_buffers,unsigned unbind_num_trailing_slots,bool take_ownership,const struct pipe_vertex_buffer * buffers)625 tegra_set_vertex_buffers(struct pipe_context *pcontext, unsigned start_slot,
626                          unsigned num_buffers, unsigned unbind_num_trailing_slots,
627                          bool take_ownership,
628                          const struct pipe_vertex_buffer *buffers)
629 {
630    struct tegra_context *context = to_tegra_context(pcontext);
631    struct pipe_vertex_buffer buf[PIPE_MAX_SHADER_INPUTS];
632    unsigned i;
633 
634    if (num_buffers && buffers) {
635       memcpy(buf, buffers, num_buffers * sizeof(struct pipe_vertex_buffer));
636 
637       for (i = 0; i < num_buffers; i++) {
638          if (!buf[i].is_user_buffer)
639             buf[i].buffer.resource = tegra_resource_unwrap(buf[i].buffer.resource);
640       }
641 
642       buffers = buf;
643    }
644 
645    context->gpu->set_vertex_buffers(context->gpu, start_slot, num_buffers,
646                                     unbind_num_trailing_slots,
647                                     take_ownership, buffers);
648 }
649 
650 static struct pipe_stream_output_target *
tegra_create_stream_output_target(struct pipe_context * pcontext,struct pipe_resource * presource,unsigned buffer_offset,unsigned buffer_size)651 tegra_create_stream_output_target(struct pipe_context *pcontext,
652                                   struct pipe_resource *presource,
653                                   unsigned buffer_offset,
654                                   unsigned buffer_size)
655 {
656    struct tegra_resource *resource = to_tegra_resource(presource);
657    struct tegra_context *context = to_tegra_context(pcontext);
658 
659    return context->gpu->create_stream_output_target(context->gpu,
660                                                     resource->gpu,
661                                                     buffer_offset,
662                                                     buffer_size);
663 }
664 
665 static void
tegra_stream_output_target_destroy(struct pipe_context * pcontext,struct pipe_stream_output_target * target)666 tegra_stream_output_target_destroy(struct pipe_context *pcontext,
667                                    struct pipe_stream_output_target *target)
668 {
669    struct tegra_context *context = to_tegra_context(pcontext);
670 
671    context->gpu->stream_output_target_destroy(context->gpu, target);
672 }
673 
674 static void
tegra_set_stream_output_targets(struct pipe_context * pcontext,unsigned num_targets,struct pipe_stream_output_target ** targets,const unsigned * offsets)675 tegra_set_stream_output_targets(struct pipe_context *pcontext,
676                                 unsigned num_targets,
677                                 struct pipe_stream_output_target **targets,
678                                 const unsigned *offsets)
679 {
680    struct tegra_context *context = to_tegra_context(pcontext);
681 
682    context->gpu->set_stream_output_targets(context->gpu, num_targets,
683                                            targets, offsets);
684 }
685 
686 static void
tegra_resource_copy_region(struct pipe_context * pcontext,struct pipe_resource * pdst,unsigned int dst_level,unsigned int dstx,unsigned int dsty,unsigned int dstz,struct pipe_resource * psrc,unsigned int src_level,const struct pipe_box * src_box)687 tegra_resource_copy_region(struct pipe_context *pcontext,
688                            struct pipe_resource *pdst,
689                            unsigned int dst_level,
690                            unsigned int dstx,
691                            unsigned int dsty,
692                            unsigned int dstz,
693                            struct pipe_resource *psrc,
694                            unsigned int src_level,
695                            const struct pipe_box *src_box)
696 {
697    struct tegra_context *context = to_tegra_context(pcontext);
698    struct tegra_resource *dst = to_tegra_resource(pdst);
699    struct tegra_resource *src = to_tegra_resource(psrc);
700 
701    context->gpu->resource_copy_region(context->gpu, dst->gpu, dst_level, dstx,
702                                       dsty, dstz, src->gpu, src_level,
703                                       src_box);
704 }
705 
706 static void
tegra_blit(struct pipe_context * pcontext,const struct pipe_blit_info * pinfo)707 tegra_blit(struct pipe_context *pcontext, const struct pipe_blit_info *pinfo)
708 {
709    struct tegra_context *context = to_tegra_context(pcontext);
710    struct pipe_blit_info info;
711 
712    if (pinfo) {
713       memcpy(&info, pinfo, sizeof(info));
714       info.dst.resource = tegra_resource_unwrap(info.dst.resource);
715       info.src.resource = tegra_resource_unwrap(info.src.resource);
716       pinfo = &info;
717    }
718 
719    context->gpu->blit(context->gpu, pinfo);
720 }
721 
722 static void
tegra_clear(struct pipe_context * pcontext,unsigned buffers,const struct pipe_scissor_state * scissor_state,const union pipe_color_union * color,double depth,unsigned stencil)723 tegra_clear(struct pipe_context *pcontext, unsigned buffers, const struct pipe_scissor_state *scissor_state,
724             const union pipe_color_union *color, double depth,
725             unsigned stencil)
726 {
727    struct tegra_context *context = to_tegra_context(pcontext);
728 
729    context->gpu->clear(context->gpu, buffers, NULL, color, depth, stencil);
730 }
731 
732 static void
tegra_clear_render_target(struct pipe_context * pcontext,struct pipe_surface * pdst,const union pipe_color_union * color,unsigned int dstx,unsigned int dsty,unsigned int width,unsigned int height,bool render_condition)733 tegra_clear_render_target(struct pipe_context *pcontext,
734                           struct pipe_surface *pdst,
735                           const union pipe_color_union *color,
736                           unsigned int dstx,
737                           unsigned int dsty,
738                           unsigned int width,
739                           unsigned int height,
740                           bool render_condition)
741 {
742    struct tegra_context *context = to_tegra_context(pcontext);
743    struct tegra_surface *dst = to_tegra_surface(pdst);
744 
745    context->gpu->clear_render_target(context->gpu, dst->gpu, color, dstx,
746                                      dsty, width, height, render_condition);
747 }
748 
749 static void
tegra_clear_depth_stencil(struct pipe_context * pcontext,struct pipe_surface * pdst,unsigned int flags,double depth,unsigned int stencil,unsigned int dstx,unsigned int dsty,unsigned int width,unsigned int height,bool render_condition)750 tegra_clear_depth_stencil(struct pipe_context *pcontext,
751                           struct pipe_surface *pdst,
752                           unsigned int flags,
753                           double depth,
754                           unsigned int stencil,
755                           unsigned int dstx,
756                           unsigned int dsty,
757                           unsigned int width,
758                           unsigned int height,
759                           bool render_condition)
760 {
761    struct tegra_context *context = to_tegra_context(pcontext);
762    struct tegra_surface *dst = to_tegra_surface(pdst);
763 
764    context->gpu->clear_depth_stencil(context->gpu, dst->gpu, flags, depth,
765                                      stencil, dstx, dsty, width, height,
766                                      render_condition);
767 }
768 
769 static void
tegra_clear_texture(struct pipe_context * pcontext,struct pipe_resource * presource,unsigned int level,const struct pipe_box * box,const void * data)770 tegra_clear_texture(struct pipe_context *pcontext,
771                     struct pipe_resource *presource,
772                     unsigned int level,
773                     const struct pipe_box *box,
774                     const void *data)
775 {
776    struct tegra_resource *resource = to_tegra_resource(presource);
777    struct tegra_context *context = to_tegra_context(pcontext);
778 
779    context->gpu->clear_texture(context->gpu, resource->gpu, level, box, data);
780 }
781 
782 static void
tegra_clear_buffer(struct pipe_context * pcontext,struct pipe_resource * presource,unsigned int offset,unsigned int size,const void * value,int value_size)783 tegra_clear_buffer(struct pipe_context *pcontext,
784                    struct pipe_resource *presource,
785                    unsigned int offset,
786                    unsigned int size,
787                    const void *value,
788                    int value_size)
789 {
790    struct tegra_resource *resource = to_tegra_resource(presource);
791    struct tegra_context *context = to_tegra_context(pcontext);
792 
793    context->gpu->clear_buffer(context->gpu, resource->gpu, offset, size,
794                               value, value_size);
795 }
796 
797 static void
tegra_flush(struct pipe_context * pcontext,struct pipe_fence_handle ** fence,unsigned flags)798 tegra_flush(struct pipe_context *pcontext, struct pipe_fence_handle **fence,
799             unsigned flags)
800 {
801    struct tegra_context *context = to_tegra_context(pcontext);
802 
803    context->gpu->flush(context->gpu, fence, flags);
804 }
805 
806 static void
tegra_create_fence_fd(struct pipe_context * pcontext,struct pipe_fence_handle ** fence,int fd,enum pipe_fd_type type)807 tegra_create_fence_fd(struct pipe_context *pcontext,
808                       struct pipe_fence_handle **fence,
809                       int fd, enum pipe_fd_type type)
810 {
811    struct tegra_context *context = to_tegra_context(pcontext);
812 
813    assert(type == PIPE_FD_TYPE_NATIVE_SYNC);
814    context->gpu->create_fence_fd(context->gpu, fence, fd, type);
815 }
816 
817 static void
tegra_fence_server_sync(struct pipe_context * pcontext,struct pipe_fence_handle * fence)818 tegra_fence_server_sync(struct pipe_context *pcontext,
819                         struct pipe_fence_handle *fence)
820 {
821    struct tegra_context *context = to_tegra_context(pcontext);
822 
823    context->gpu->fence_server_sync(context->gpu, fence);
824 }
825 
826 static struct pipe_sampler_view *
tegra_create_sampler_view(struct pipe_context * pcontext,struct pipe_resource * presource,const struct pipe_sampler_view * template)827 tegra_create_sampler_view(struct pipe_context *pcontext,
828                           struct pipe_resource *presource,
829                           const struct pipe_sampler_view *template)
830 {
831    struct tegra_resource *resource = to_tegra_resource(presource);
832    struct tegra_context *context = to_tegra_context(pcontext);
833    struct tegra_sampler_view *view;
834 
835    view = calloc(1, sizeof(*view));
836    if (!view)
837       return NULL;
838 
839    view->gpu = context->gpu->create_sampler_view(context->gpu, resource->gpu,
840                                                  template);
841    memcpy(&view->base, view->gpu, sizeof(*view->gpu));
842    /* overwrite to prevent reference from being released */
843    view->base.texture = NULL;
844 
845    pipe_reference_init(&view->base.reference, 1);
846    pipe_resource_reference(&view->base.texture, presource);
847    view->base.context = pcontext;
848 
849    return &view->base;
850 }
851 
852 static void
tegra_sampler_view_destroy(struct pipe_context * pcontext,struct pipe_sampler_view * pview)853 tegra_sampler_view_destroy(struct pipe_context *pcontext,
854                            struct pipe_sampler_view *pview)
855 {
856    struct tegra_sampler_view *view = to_tegra_sampler_view(pview);
857 
858    pipe_resource_reference(&view->base.texture, NULL);
859    pipe_sampler_view_reference(&view->gpu, NULL);
860    free(view);
861 }
862 
863 static struct pipe_surface *
tegra_create_surface(struct pipe_context * pcontext,struct pipe_resource * presource,const struct pipe_surface * template)864 tegra_create_surface(struct pipe_context *pcontext,
865                      struct pipe_resource *presource,
866                      const struct pipe_surface *template)
867 {
868    struct tegra_resource *resource = to_tegra_resource(presource);
869    struct tegra_context *context = to_tegra_context(pcontext);
870    struct tegra_surface *surface;
871 
872    surface = calloc(1, sizeof(*surface));
873    if (!surface)
874       return NULL;
875 
876    surface->gpu = context->gpu->create_surface(context->gpu, resource->gpu,
877                                                template);
878    if (!surface->gpu) {
879       free(surface);
880       return NULL;
881    }
882 
883    memcpy(&surface->base, surface->gpu, sizeof(*surface->gpu));
884    /* overwrite to prevent reference from being released */
885    surface->base.texture = NULL;
886 
887    pipe_reference_init(&surface->base.reference, 1);
888    pipe_resource_reference(&surface->base.texture, presource);
889    surface->base.context = &context->base;
890 
891    return &surface->base;
892 }
893 
894 static void
tegra_surface_destroy(struct pipe_context * pcontext,struct pipe_surface * psurface)895 tegra_surface_destroy(struct pipe_context *pcontext,
896                       struct pipe_surface *psurface)
897 {
898    struct tegra_surface *surface = to_tegra_surface(psurface);
899 
900    pipe_resource_reference(&surface->base.texture, NULL);
901    pipe_surface_reference(&surface->gpu, NULL);
902    free(surface);
903 }
904 
905 static void *
tegra_transfer_map(struct pipe_context * pcontext,struct pipe_resource * presource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)906 tegra_transfer_map(struct pipe_context *pcontext,
907                    struct pipe_resource *presource,
908                    unsigned level, unsigned usage,
909                    const struct pipe_box *box,
910                    struct pipe_transfer **ptransfer)
911 {
912    struct tegra_resource *resource = to_tegra_resource(presource);
913    struct tegra_context *context = to_tegra_context(pcontext);
914    struct tegra_transfer *transfer;
915 
916    transfer = calloc(1, sizeof(*transfer));
917    if (!transfer)
918       return NULL;
919 
920    if (presource->target == PIPE_BUFFER) {
921       transfer->map = context->gpu->buffer_map(context->gpu, resource->gpu,
922                                                  level, usage, box,
923                                                  &transfer->gpu);
924    } else {
925       transfer->map = context->gpu->texture_map(context->gpu, resource->gpu,
926                                                  level, usage, box,
927                                                  &transfer->gpu);
928    }
929    memcpy(&transfer->base, transfer->gpu, sizeof(*transfer->gpu));
930    transfer->base.resource = NULL;
931    pipe_resource_reference(&transfer->base.resource, presource);
932 
933    *ptransfer = &transfer->base;
934 
935    return transfer->map;
936 }
937 
938 static void
tegra_transfer_flush_region(struct pipe_context * pcontext,struct pipe_transfer * ptransfer,const struct pipe_box * box)939 tegra_transfer_flush_region(struct pipe_context *pcontext,
940                             struct pipe_transfer *ptransfer,
941                             const struct pipe_box *box)
942 {
943    struct tegra_transfer *transfer = to_tegra_transfer(ptransfer);
944    struct tegra_context *context = to_tegra_context(pcontext);
945 
946    context->gpu->transfer_flush_region(context->gpu, transfer->gpu, box);
947 }
948 
949 static void
tegra_transfer_unmap(struct pipe_context * pcontext,struct pipe_transfer * ptransfer)950 tegra_transfer_unmap(struct pipe_context *pcontext,
951                      struct pipe_transfer *ptransfer)
952 {
953    struct tegra_transfer *transfer = to_tegra_transfer(ptransfer);
954    struct tegra_context *context = to_tegra_context(pcontext);
955 
956    if (ptransfer->resource->target == PIPE_BUFFER)
957       context->gpu->buffer_unmap(context->gpu, transfer->gpu);
958    else
959       context->gpu->texture_unmap(context->gpu, transfer->gpu);
960    pipe_resource_reference(&transfer->base.resource, NULL);
961    free(transfer);
962 }
963 
964 static void
tegra_buffer_subdata(struct pipe_context * pcontext,struct pipe_resource * presource,unsigned usage,unsigned offset,unsigned size,const void * data)965 tegra_buffer_subdata(struct pipe_context *pcontext,
966                      struct pipe_resource *presource,
967                      unsigned usage, unsigned offset,
968                      unsigned size, const void *data)
969 {
970    struct tegra_resource *resource = to_tegra_resource(presource);
971    struct tegra_context *context = to_tegra_context(pcontext);
972 
973    context->gpu->buffer_subdata(context->gpu, resource->gpu, usage, offset,
974                                 size, data);
975 }
976 
977 static void
tegra_texture_subdata(struct pipe_context * pcontext,struct pipe_resource * presource,unsigned level,unsigned usage,const struct pipe_box * box,const void * data,unsigned stride,unsigned layer_stride)978 tegra_texture_subdata(struct pipe_context *pcontext,
979                       struct pipe_resource *presource,
980                       unsigned level,
981                       unsigned usage,
982                       const struct pipe_box *box,
983                       const void *data,
984                       unsigned stride,
985                       unsigned layer_stride)
986 {
987    struct tegra_resource *resource = to_tegra_resource(presource);
988    struct tegra_context *context = to_tegra_context(pcontext);
989 
990    context->gpu->texture_subdata(context->gpu, resource->gpu, level, usage,
991                                  box, data, stride, layer_stride);
992 }
993 
994 static void
tegra_texture_barrier(struct pipe_context * pcontext,unsigned int flags)995 tegra_texture_barrier(struct pipe_context *pcontext, unsigned int flags)
996 {
997    struct tegra_context *context = to_tegra_context(pcontext);
998 
999    context->gpu->texture_barrier(context->gpu, flags);
1000 }
1001 
1002 static void
tegra_memory_barrier(struct pipe_context * pcontext,unsigned int flags)1003 tegra_memory_barrier(struct pipe_context *pcontext, unsigned int flags)
1004 {
1005    struct tegra_context *context = to_tegra_context(pcontext);
1006 
1007    if (!(flags & ~PIPE_BARRIER_UPDATE))
1008       return;
1009 
1010    context->gpu->memory_barrier(context->gpu, flags);
1011 }
1012 
1013 static struct pipe_video_codec *
tegra_create_video_codec(struct pipe_context * pcontext,const struct pipe_video_codec * template)1014 tegra_create_video_codec(struct pipe_context *pcontext,
1015                          const struct pipe_video_codec *template)
1016 {
1017    struct tegra_context *context = to_tegra_context(pcontext);
1018 
1019    return context->gpu->create_video_codec(context->gpu, template);
1020 }
1021 
1022 static struct pipe_video_buffer *
tegra_create_video_buffer(struct pipe_context * pcontext,const struct pipe_video_buffer * template)1023 tegra_create_video_buffer(struct pipe_context *pcontext,
1024                           const struct pipe_video_buffer *template)
1025 {
1026    struct tegra_context *context = to_tegra_context(pcontext);
1027 
1028    return context->gpu->create_video_buffer(context->gpu, template);
1029 }
1030 
1031 static void *
tegra_create_compute_state(struct pipe_context * pcontext,const struct pipe_compute_state * template)1032 tegra_create_compute_state(struct pipe_context *pcontext,
1033                            const struct pipe_compute_state *template)
1034 {
1035    struct tegra_context *context = to_tegra_context(pcontext);
1036 
1037    return context->gpu->create_compute_state(context->gpu, template);
1038 }
1039 
1040 static void
tegra_bind_compute_state(struct pipe_context * pcontext,void * so)1041 tegra_bind_compute_state(struct pipe_context *pcontext, void *so)
1042 {
1043    struct tegra_context *context = to_tegra_context(pcontext);
1044 
1045    context->gpu->bind_compute_state(context->gpu, so);
1046 }
1047 
1048 static void
tegra_delete_compute_state(struct pipe_context * pcontext,void * so)1049 tegra_delete_compute_state(struct pipe_context *pcontext, void *so)
1050 {
1051    struct tegra_context *context = to_tegra_context(pcontext);
1052 
1053    context->gpu->delete_compute_state(context->gpu, so);
1054 }
1055 
1056 static void
tegra_set_compute_resources(struct pipe_context * pcontext,unsigned int start,unsigned int count,struct pipe_surface ** resources)1057 tegra_set_compute_resources(struct pipe_context *pcontext,
1058                             unsigned int start, unsigned int count,
1059                             struct pipe_surface **resources)
1060 {
1061    struct tegra_context *context = to_tegra_context(pcontext);
1062 
1063    /* XXX unwrap resources */
1064 
1065    context->gpu->set_compute_resources(context->gpu, start, count, resources);
1066 }
1067 
1068 static void
tegra_set_global_binding(struct pipe_context * pcontext,unsigned int first,unsigned int count,struct pipe_resource ** resources,uint32_t ** handles)1069 tegra_set_global_binding(struct pipe_context *pcontext, unsigned int first,
1070                          unsigned int count, struct pipe_resource **resources,
1071                          uint32_t **handles)
1072 {
1073    struct tegra_context *context = to_tegra_context(pcontext);
1074 
1075    /* XXX unwrap resources */
1076 
1077    context->gpu->set_global_binding(context->gpu, first, count, resources,
1078                                     handles);
1079 }
1080 
1081 static void
tegra_launch_grid(struct pipe_context * pcontext,const struct pipe_grid_info * info)1082 tegra_launch_grid(struct pipe_context *pcontext,
1083                   const struct pipe_grid_info *info)
1084 {
1085    struct tegra_context *context = to_tegra_context(pcontext);
1086 
1087    /* XXX unwrap info->indirect? */
1088 
1089    context->gpu->launch_grid(context->gpu, info);
1090 }
1091 
1092 static void
tegra_get_sample_position(struct pipe_context * pcontext,unsigned int count,unsigned int index,float * value)1093 tegra_get_sample_position(struct pipe_context *pcontext, unsigned int count,
1094                           unsigned int index, float *value)
1095 {
1096    struct tegra_context *context = to_tegra_context(pcontext);
1097 
1098    context->gpu->get_sample_position(context->gpu, count, index, value);
1099 }
1100 
1101 static uint64_t
tegra_get_timestamp(struct pipe_context * pcontext)1102 tegra_get_timestamp(struct pipe_context *pcontext)
1103 {
1104    struct tegra_context *context = to_tegra_context(pcontext);
1105 
1106    return context->gpu->get_timestamp(context->gpu);
1107 }
1108 
1109 static void
tegra_flush_resource(struct pipe_context * pcontext,struct pipe_resource * presource)1110 tegra_flush_resource(struct pipe_context *pcontext,
1111                      struct pipe_resource *presource)
1112 {
1113    struct tegra_resource *resource = to_tegra_resource(presource);
1114    struct tegra_context *context = to_tegra_context(pcontext);
1115 
1116    context->gpu->flush_resource(context->gpu, resource->gpu);
1117 }
1118 
1119 static void
tegra_invalidate_resource(struct pipe_context * pcontext,struct pipe_resource * presource)1120 tegra_invalidate_resource(struct pipe_context *pcontext,
1121                           struct pipe_resource *presource)
1122 {
1123    struct tegra_resource *resource = to_tegra_resource(presource);
1124    struct tegra_context *context = to_tegra_context(pcontext);
1125 
1126    context->gpu->invalidate_resource(context->gpu, resource->gpu);
1127 }
1128 
1129 static enum pipe_reset_status
tegra_get_device_reset_status(struct pipe_context * pcontext)1130 tegra_get_device_reset_status(struct pipe_context *pcontext)
1131 {
1132    struct tegra_context *context = to_tegra_context(pcontext);
1133 
1134    return context->gpu->get_device_reset_status(context->gpu);
1135 }
1136 
1137 static void
tegra_set_device_reset_callback(struct pipe_context * pcontext,const struct pipe_device_reset_callback * cb)1138 tegra_set_device_reset_callback(struct pipe_context *pcontext,
1139                                 const struct pipe_device_reset_callback *cb)
1140 {
1141    struct tegra_context *context = to_tegra_context(pcontext);
1142 
1143    context->gpu->set_device_reset_callback(context->gpu, cb);
1144 }
1145 
1146 static void
tegra_dump_debug_state(struct pipe_context * pcontext,FILE * stream,unsigned int flags)1147 tegra_dump_debug_state(struct pipe_context *pcontext, FILE *stream,
1148                        unsigned int flags)
1149 {
1150    struct tegra_context *context = to_tegra_context(pcontext);
1151 
1152    context->gpu->dump_debug_state(context->gpu, stream, flags);
1153 }
1154 
1155 static void
tegra_emit_string_marker(struct pipe_context * pcontext,const char * string,int length)1156 tegra_emit_string_marker(struct pipe_context *pcontext, const char *string,
1157                          int length)
1158 {
1159    struct tegra_context *context = to_tegra_context(pcontext);
1160 
1161    context->gpu->emit_string_marker(context->gpu, string, length);
1162 }
1163 
1164 static bool
tegra_generate_mipmap(struct pipe_context * pcontext,struct pipe_resource * presource,enum pipe_format format,unsigned int base_level,unsigned int last_level,unsigned int first_layer,unsigned int last_layer)1165 tegra_generate_mipmap(struct pipe_context *pcontext,
1166                       struct pipe_resource *presource,
1167                       enum pipe_format format,
1168                       unsigned int base_level,
1169                       unsigned int last_level,
1170                       unsigned int first_layer,
1171                       unsigned int last_layer)
1172 {
1173    struct tegra_resource *resource = to_tegra_resource(presource);
1174    struct tegra_context *context = to_tegra_context(pcontext);
1175 
1176    return context->gpu->generate_mipmap(context->gpu, resource->gpu, format,
1177                                         base_level, last_level, first_layer,
1178                                         last_layer);
1179 }
1180 
1181 static uint64_t
tegra_create_texture_handle(struct pipe_context * pcontext,struct pipe_sampler_view * view,const struct pipe_sampler_state * state)1182 tegra_create_texture_handle(struct pipe_context *pcontext,
1183                             struct pipe_sampler_view *view,
1184                             const struct pipe_sampler_state *state)
1185 {
1186    struct tegra_context *context = to_tegra_context(pcontext);
1187 
1188    return context->gpu->create_texture_handle(context->gpu, view, state);
1189 }
1190 
tegra_delete_texture_handle(struct pipe_context * pcontext,uint64_t handle)1191 static void tegra_delete_texture_handle(struct pipe_context *pcontext,
1192                                         uint64_t handle)
1193 {
1194    struct tegra_context *context = to_tegra_context(pcontext);
1195 
1196    context->gpu->delete_texture_handle(context->gpu, handle);
1197 }
1198 
tegra_make_texture_handle_resident(struct pipe_context * pcontext,uint64_t handle,bool resident)1199 static void tegra_make_texture_handle_resident(struct pipe_context *pcontext,
1200                                                uint64_t handle, bool resident)
1201 {
1202    struct tegra_context *context = to_tegra_context(pcontext);
1203 
1204    context->gpu->make_texture_handle_resident(context->gpu, handle, resident);
1205 }
1206 
tegra_create_image_handle(struct pipe_context * pcontext,const struct pipe_image_view * image)1207 static uint64_t tegra_create_image_handle(struct pipe_context *pcontext,
1208                                           const struct pipe_image_view *image)
1209 {
1210    struct tegra_context *context = to_tegra_context(pcontext);
1211 
1212    return context->gpu->create_image_handle(context->gpu, image);
1213 }
1214 
tegra_delete_image_handle(struct pipe_context * pcontext,uint64_t handle)1215 static void tegra_delete_image_handle(struct pipe_context *pcontext,
1216                                       uint64_t handle)
1217 {
1218    struct tegra_context *context = to_tegra_context(pcontext);
1219 
1220    context->gpu->delete_image_handle(context->gpu, handle);
1221 }
1222 
tegra_make_image_handle_resident(struct pipe_context * pcontext,uint64_t handle,unsigned access,bool resident)1223 static void tegra_make_image_handle_resident(struct pipe_context *pcontext,
1224                                              uint64_t handle, unsigned access,
1225                                              bool resident)
1226 {
1227    struct tegra_context *context = to_tegra_context(pcontext);
1228 
1229    context->gpu->make_image_handle_resident(context->gpu, handle, access,
1230                                             resident);
1231 }
1232 
1233 struct pipe_context *
tegra_screen_context_create(struct pipe_screen * pscreen,void * priv,unsigned int flags)1234 tegra_screen_context_create(struct pipe_screen *pscreen, void *priv,
1235                             unsigned int flags)
1236 {
1237    struct tegra_screen *screen = to_tegra_screen(pscreen);
1238    struct tegra_context *context;
1239 
1240    context = calloc(1, sizeof(*context));
1241    if (!context)
1242       return NULL;
1243 
1244    context->gpu = screen->gpu->context_create(screen->gpu, priv, flags);
1245    if (!context->gpu) {
1246       debug_error("failed to create GPU context\n");
1247       goto free;
1248    }
1249 
1250    context->base.screen = &screen->base;
1251    context->base.priv = priv;
1252 
1253    /*
1254     * Create custom stream and const uploaders. Note that technically nouveau
1255     * already creates uploaders that could be reused, but that would make the
1256     * resource unwrapping rather complicate. The reason for that is that both
1257     * uploaders create resources based on the context that they were created
1258     * from, which means that nouveau's uploader will use the nouveau context
1259     * which means that those resources must not be unwrapped. So before each
1260     * resource is unwrapped, the code would need to check that it does not
1261     * correspond to the uploaders' buffers.
1262     *
1263     * However, duplicating the uploaders here sounds worse than it is. The
1264     * default implementation that nouveau uses allocates buffers lazily, and
1265     * since it is never used, no buffers will every be allocated and the only
1266     * memory wasted is that occupied by the nouveau uploader itself.
1267     */
1268    context->base.stream_uploader = u_upload_create_default(&context->base);
1269    if (!context->base.stream_uploader)
1270       goto destroy;
1271 
1272    context->base.const_uploader = context->base.stream_uploader;
1273 
1274    context->base.destroy = tegra_destroy;
1275 
1276    context->base.draw_vbo = tegra_draw_vbo;
1277 
1278    context->base.render_condition = tegra_render_condition;
1279 
1280    context->base.create_query = tegra_create_query;
1281    context->base.create_batch_query = tegra_create_batch_query;
1282    context->base.destroy_query = tegra_destroy_query;
1283    context->base.begin_query = tegra_begin_query;
1284    context->base.end_query = tegra_end_query;
1285    context->base.get_query_result = tegra_get_query_result;
1286    context->base.get_query_result_resource = tegra_get_query_result_resource;
1287    context->base.set_active_query_state = tegra_set_active_query_state;
1288 
1289    context->base.create_blend_state = tegra_create_blend_state;
1290    context->base.bind_blend_state = tegra_bind_blend_state;
1291    context->base.delete_blend_state = tegra_delete_blend_state;
1292 
1293    context->base.create_sampler_state = tegra_create_sampler_state;
1294    context->base.bind_sampler_states = tegra_bind_sampler_states;
1295    context->base.delete_sampler_state = tegra_delete_sampler_state;
1296 
1297    context->base.create_rasterizer_state = tegra_create_rasterizer_state;
1298    context->base.bind_rasterizer_state = tegra_bind_rasterizer_state;
1299    context->base.delete_rasterizer_state = tegra_delete_rasterizer_state;
1300 
1301    context->base.create_depth_stencil_alpha_state = tegra_create_depth_stencil_alpha_state;
1302    context->base.bind_depth_stencil_alpha_state = tegra_bind_depth_stencil_alpha_state;
1303    context->base.delete_depth_stencil_alpha_state = tegra_delete_depth_stencil_alpha_state;
1304 
1305    context->base.create_fs_state = tegra_create_fs_state;
1306    context->base.bind_fs_state = tegra_bind_fs_state;
1307    context->base.delete_fs_state = tegra_delete_fs_state;
1308 
1309    context->base.create_vs_state = tegra_create_vs_state;
1310    context->base.bind_vs_state = tegra_bind_vs_state;
1311    context->base.delete_vs_state = tegra_delete_vs_state;
1312 
1313    context->base.create_gs_state = tegra_create_gs_state;
1314    context->base.bind_gs_state = tegra_bind_gs_state;
1315    context->base.delete_gs_state = tegra_delete_gs_state;
1316 
1317    context->base.create_tcs_state = tegra_create_tcs_state;
1318    context->base.bind_tcs_state = tegra_bind_tcs_state;
1319    context->base.delete_tcs_state = tegra_delete_tcs_state;
1320 
1321    context->base.create_tes_state = tegra_create_tes_state;
1322    context->base.bind_tes_state = tegra_bind_tes_state;
1323    context->base.delete_tes_state = tegra_delete_tes_state;
1324 
1325    context->base.create_vertex_elements_state = tegra_create_vertex_elements_state;
1326    context->base.bind_vertex_elements_state = tegra_bind_vertex_elements_state;
1327    context->base.delete_vertex_elements_state = tegra_delete_vertex_elements_state;
1328 
1329    context->base.set_blend_color = tegra_set_blend_color;
1330    context->base.set_stencil_ref = tegra_set_stencil_ref;
1331    context->base.set_sample_mask = tegra_set_sample_mask;
1332    context->base.set_min_samples = tegra_set_min_samples;
1333    context->base.set_clip_state = tegra_set_clip_state;
1334 
1335    context->base.set_constant_buffer = tegra_set_constant_buffer;
1336    context->base.set_framebuffer_state = tegra_set_framebuffer_state;
1337    context->base.set_polygon_stipple = tegra_set_polygon_stipple;
1338    context->base.set_scissor_states = tegra_set_scissor_states;
1339    context->base.set_window_rectangles = tegra_set_window_rectangles;
1340    context->base.set_viewport_states = tegra_set_viewport_states;
1341    context->base.set_sampler_views = tegra_set_sampler_views;
1342    context->base.set_tess_state = tegra_set_tess_state;
1343 
1344    context->base.set_debug_callback = tegra_set_debug_callback;
1345 
1346    context->base.set_shader_buffers = tegra_set_shader_buffers;
1347    context->base.set_shader_images = tegra_set_shader_images;
1348    context->base.set_vertex_buffers = tegra_set_vertex_buffers;
1349 
1350    context->base.create_stream_output_target = tegra_create_stream_output_target;
1351    context->base.stream_output_target_destroy = tegra_stream_output_target_destroy;
1352    context->base.set_stream_output_targets = tegra_set_stream_output_targets;
1353 
1354    context->base.resource_copy_region = tegra_resource_copy_region;
1355    context->base.blit = tegra_blit;
1356    context->base.clear = tegra_clear;
1357    context->base.clear_render_target = tegra_clear_render_target;
1358    context->base.clear_depth_stencil = tegra_clear_depth_stencil;
1359    context->base.clear_texture = tegra_clear_texture;
1360    context->base.clear_buffer = tegra_clear_buffer;
1361    context->base.flush = tegra_flush;
1362 
1363    context->base.create_fence_fd = tegra_create_fence_fd;
1364    context->base.fence_server_sync = tegra_fence_server_sync;
1365 
1366    context->base.create_sampler_view = tegra_create_sampler_view;
1367    context->base.sampler_view_destroy = tegra_sampler_view_destroy;
1368 
1369    context->base.create_surface = tegra_create_surface;
1370    context->base.surface_destroy = tegra_surface_destroy;
1371 
1372    context->base.buffer_map = tegra_transfer_map;
1373    context->base.texture_map = tegra_transfer_map;
1374    context->base.transfer_flush_region = tegra_transfer_flush_region;
1375    context->base.buffer_unmap = tegra_transfer_unmap;
1376    context->base.texture_unmap = tegra_transfer_unmap;
1377    context->base.buffer_subdata = tegra_buffer_subdata;
1378    context->base.texture_subdata = tegra_texture_subdata;
1379 
1380    context->base.texture_barrier = tegra_texture_barrier;
1381    context->base.memory_barrier = tegra_memory_barrier;
1382 
1383    context->base.create_video_codec = tegra_create_video_codec;
1384    context->base.create_video_buffer = tegra_create_video_buffer;
1385 
1386    context->base.create_compute_state = tegra_create_compute_state;
1387    context->base.bind_compute_state = tegra_bind_compute_state;
1388    context->base.delete_compute_state = tegra_delete_compute_state;
1389    context->base.set_compute_resources = tegra_set_compute_resources;
1390    context->base.set_global_binding = tegra_set_global_binding;
1391    context->base.launch_grid = tegra_launch_grid;
1392    context->base.get_sample_position = tegra_get_sample_position;
1393    context->base.get_timestamp = tegra_get_timestamp;
1394 
1395    context->base.flush_resource = tegra_flush_resource;
1396    context->base.invalidate_resource = tegra_invalidate_resource;
1397 
1398    context->base.get_device_reset_status = tegra_get_device_reset_status;
1399    context->base.set_device_reset_callback = tegra_set_device_reset_callback;
1400    context->base.dump_debug_state = tegra_dump_debug_state;
1401    context->base.emit_string_marker = tegra_emit_string_marker;
1402 
1403    context->base.generate_mipmap = tegra_generate_mipmap;
1404 
1405    context->base.create_texture_handle = tegra_create_texture_handle;
1406    context->base.delete_texture_handle = tegra_delete_texture_handle;
1407    context->base.make_texture_handle_resident = tegra_make_texture_handle_resident;
1408    context->base.create_image_handle = tegra_create_image_handle;
1409    context->base.delete_image_handle = tegra_delete_image_handle;
1410    context->base.make_image_handle_resident = tegra_make_image_handle_resident;
1411 
1412    return &context->base;
1413 
1414 destroy:
1415    context->gpu->destroy(context->gpu);
1416 free:
1417    free(context);
1418    return NULL;
1419 }
1420