virgl: add virgl_context/sampler_view/so_target() upcast wrappers
[mesa.git] / src / gallium / drivers / virgl / virgl_query.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
24 #include "util/u_memory.h"
25 #include "util/u_inlines.h"
26 #include "virgl_resource.h"
27 #include "virgl_context.h"
28 #include "virgl_encode.h"
29
30 struct virgl_query {
31 uint32_t handle;
32 struct virgl_resource *buf;
33
34 unsigned index;
35 unsigned type;
36 unsigned result_size;
37 unsigned result_gotten_sent;
38 };
39
40 static void virgl_render_condition(struct pipe_context *ctx,
41 struct pipe_query *q,
42 boolean condition,
43 uint mode)
44 {
45 struct virgl_context *vctx = virgl_context(ctx);
46 struct virgl_query *query = (struct virgl_query *)q;
47 uint32_t handle = 0;
48 if (q)
49 handle = query->handle;
50 virgl_encoder_render_condition(vctx, handle, condition, mode);
51 }
52
53 static struct pipe_query *virgl_create_query(struct pipe_context *ctx,
54 unsigned query_type, unsigned index)
55 {
56 struct virgl_context *vctx = virgl_context(ctx);
57 struct virgl_query *query;
58 uint32_t handle;
59
60 query = CALLOC_STRUCT(virgl_query);
61 if (!query)
62 return NULL;
63
64 query->buf = (struct virgl_resource *)pipe_buffer_create(ctx->screen, PIPE_BIND_CUSTOM,
65 PIPE_USAGE_STAGING, sizeof(struct virgl_host_query_state));
66 if (!query->buf) {
67 FREE(query);
68 return NULL;
69 }
70
71 handle = virgl_object_assign_handle();
72 query->type = query_type;
73 query->index = index;
74 query->handle = handle;
75 query->buf->clean = FALSE;
76 virgl_encoder_create_query(vctx, handle, query_type, index, query->buf, 0);
77
78 return (struct pipe_query *)query;
79 }
80
81 static void virgl_destroy_query(struct pipe_context *ctx,
82 struct pipe_query *q)
83 {
84 struct virgl_context *vctx = virgl_context(ctx);
85 struct virgl_query *query = (struct virgl_query *)q;
86
87 virgl_encode_delete_object(vctx, query->handle, VIRGL_OBJECT_QUERY);
88
89 pipe_resource_reference((struct pipe_resource **)&query->buf, NULL);
90 FREE(query);
91 }
92
93 static boolean virgl_begin_query(struct pipe_context *ctx,
94 struct pipe_query *q)
95 {
96 struct virgl_context *vctx = virgl_context(ctx);
97 struct virgl_query *query = (struct virgl_query *)q;
98
99 query->buf->clean = FALSE;
100 virgl_encoder_begin_query(vctx, query->handle);
101 return true;
102 }
103
104 static void virgl_end_query(struct pipe_context *ctx,
105 struct pipe_query *q)
106 {
107 struct virgl_context *vctx = virgl_context(ctx);
108 struct virgl_query *query = (struct virgl_query *)q;
109 struct pipe_box box;
110
111 uint32_t qs = VIRGL_QUERY_STATE_WAIT_HOST;
112 u_box_1d(0, 4, &box);
113 virgl_transfer_inline_write(ctx, &query->buf->u.b, 0, PIPE_TRANSFER_WRITE,
114 &box, &qs, 0, 0);
115
116
117 virgl_encoder_end_query(vctx, query->handle);
118 }
119
120 static boolean virgl_get_query_result(struct pipe_context *ctx,
121 struct pipe_query *q,
122 boolean wait,
123 union pipe_query_result *result)
124 {
125 struct virgl_context *vctx = virgl_context(ctx);
126 struct virgl_query *query = (struct virgl_query *)q;
127 struct pipe_transfer *transfer;
128 struct virgl_host_query_state *host_state;
129
130 /* ask host for query result */
131 if (!query->result_gotten_sent) {
132 query->result_gotten_sent = 1;
133 virgl_encoder_get_query_result(vctx, query->handle, 0);
134 ctx->flush(ctx, NULL, 0);
135 }
136
137 /* do we have to flush? */
138 /* now we can do the transfer to get the result back? */
139 remap:
140 host_state = pipe_buffer_map(ctx, &query->buf->u.b,
141 PIPE_TRANSFER_READ, &transfer);
142
143 if (host_state->query_state != VIRGL_QUERY_STATE_DONE) {
144 pipe_buffer_unmap(ctx, transfer);
145 if (wait)
146 goto remap;
147 else
148 return FALSE;
149 }
150
151 if (query->type == PIPE_QUERY_TIMESTAMP || query->type == PIPE_QUERY_TIME_ELAPSED)
152 result->u64 = host_state->result;
153 else
154 result->u64 = (uint32_t)host_state->result;
155
156 pipe_buffer_unmap(ctx, transfer);
157 query->result_gotten_sent = 0;
158 return TRUE;
159 }
160
161 void virgl_init_query_functions(struct virgl_context *vctx)
162 {
163 vctx->base.render_condition = virgl_render_condition;
164 vctx->base.create_query = virgl_create_query;
165 vctx->base.destroy_query = virgl_destroy_query;
166 vctx->base.begin_query = virgl_begin_query;
167 vctx->base.end_query = virgl_end_query;
168 vctx->base.get_query_result = virgl_get_query_result;
169 }