virgl: introduce and use virgl_transfer/texture/resource inline wrappers
[mesa.git] / src / gallium / drivers / virgl / virgl_streamout.c
1 /*
2 * Copyright 2014, 2015 Red Hat.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include "util/u_memory.h"
24 #include "util/u_inlines.h"
25 #include "virgl_context.h"
26 #include "virgl_encode.h"
27 #include "virgl_resource.h"
28
29 static struct pipe_stream_output_target *virgl_create_so_target(
30 struct pipe_context *ctx,
31 struct pipe_resource *buffer,
32 unsigned buffer_offset,
33 unsigned buffer_size)
34 {
35 struct virgl_context *vctx = virgl_context(ctx);
36 struct virgl_resource *res = virgl_resource(buffer);
37 struct virgl_so_target *t = CALLOC_STRUCT(virgl_so_target);
38 uint32_t handle;
39
40 if (!t)
41 return NULL;
42 handle = virgl_object_assign_handle();
43
44 t->base.reference.count = 1;
45 t->base.context = ctx;
46 pipe_resource_reference(&t->base.buffer, buffer);
47 t->base.buffer_offset = buffer_offset;
48 t->base.buffer_size = buffer_size;
49 t->handle = handle;
50 res->clean = FALSE;
51 virgl_encoder_create_so_target(vctx, handle, res, buffer_offset, buffer_size);
52 return &t->base;
53 }
54
55 static void virgl_destroy_so_target(struct pipe_context *ctx,
56 struct pipe_stream_output_target *target)
57 {
58 struct virgl_context *vctx = virgl_context(ctx);
59 struct virgl_so_target *t = virgl_so_target(target);
60
61 pipe_resource_reference(&t->base.buffer, NULL);
62 virgl_encode_delete_object(vctx, t->handle, VIRGL_OBJECT_STREAMOUT_TARGET);
63 FREE(t);
64 }
65
66 static void virgl_set_so_targets(struct pipe_context *ctx,
67 unsigned num_targets,
68 struct pipe_stream_output_target **targets,
69 const unsigned *offset)
70 {
71 struct virgl_context *vctx = virgl_context(ctx);
72 int i;
73 for (i = 0; i < num_targets; i++) {
74 pipe_resource_reference(&vctx->so_targets[i].base.buffer, targets[i]->buffer);
75 }
76 for (i = num_targets; i < vctx->num_so_targets; i++)
77 pipe_resource_reference(&vctx->so_targets[i].base.buffer, NULL);
78 vctx->num_so_targets = num_targets;
79 virgl_encoder_set_so_targets(vctx, num_targets, targets, 0);//append_bitmask);
80 }
81
82 void virgl_init_so_functions(struct virgl_context *vctx)
83 {
84 vctx->base.create_stream_output_target = virgl_create_so_target;
85 vctx->base.stream_output_target_destroy = virgl_destroy_so_target;
86 vctx->base.set_stream_output_targets = virgl_set_so_targets;
87 }