nv50: add DXTn formats
[mesa.git] / src / gallium / drivers / nv50 / nv50_query.c
1 /*
2 * Copyright 2008 Ben Skeggs
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "pipe/p_context.h"
24 #include "pipe/p_inlines.h"
25
26 #include "nv50_context.h"
27
28 struct nv50_query {
29 struct pipe_buffer *buffer;
30 unsigned type;
31 boolean ready;
32 uint64_t result;
33 };
34
35 static INLINE struct nv50_query *
36 nv50_query(struct pipe_query *pipe)
37 {
38 return (struct nv50_query *)pipe;
39 }
40
41 static struct pipe_query *
42 nv50_query_create(struct pipe_context *pipe, unsigned type)
43 {
44 struct pipe_winsys *ws = pipe->winsys;
45 struct nv50_query *q = CALLOC_STRUCT(nv50_query);
46
47 assert (q->type == PIPE_QUERY_OCCLUSION_COUNTER);
48 q->type = type;
49
50 q->buffer = ws->buffer_create(ws, 256, 0, 16);
51 if (!q->buffer) {
52 FREE(q);
53 return NULL;
54 }
55
56 return (struct pipe_query *)q;
57 }
58
59 static void
60 nv50_query_destroy(struct pipe_context *pipe, struct pipe_query *pq)
61 {
62 struct nv50_query *q = nv50_query(pq);
63
64 if (q) {
65 pipe_buffer_reference(pipe, &q->buffer, NULL);
66 FREE(q);
67 }
68 }
69
70 static void
71 nv50_query_begin(struct pipe_context *pipe, struct pipe_query *pq)
72 {
73 struct nv50_context *nv50 = nv50_context(pipe);
74 struct nv50_query *q = nv50_query(pq);
75
76 BEGIN_RING(tesla, 0x1530, 1);
77 OUT_RING (1);
78 BEGIN_RING(tesla, 0x1514, 1);
79 OUT_RING (1);
80
81 q->ready = FALSE;
82 }
83
84 static void
85 nv50_query_end(struct pipe_context *pipe, struct pipe_query *pq)
86 {
87 struct nv50_context *nv50 = nv50_context(pipe);
88 struct nv50_query *q = nv50_query(pq);
89
90 BEGIN_RING(tesla, 0x1b00, 4);
91 OUT_RELOCh(q->buffer, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
92 OUT_RELOCl(q->buffer, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
93 OUT_RING (0x00000000);
94 OUT_RING (0x0100f002);
95 FIRE_RING (NULL);
96 }
97
98 static boolean
99 nv50_query_result(struct pipe_context *pipe, struct pipe_query *pq,
100 boolean wait, uint64_t *result)
101 {
102 struct pipe_winsys *ws = pipe->winsys;
103 struct nv50_query *q = nv50_query(pq);
104
105 /*XXX: Want to be able to return FALSE here instead of blocking
106 * until the result is available..
107 */
108
109 if (!q->ready) {
110 uint32_t *map = ws->buffer_map(ws, q->buffer,
111 PIPE_BUFFER_USAGE_CPU_READ);
112 q->result = map[1];
113 q->ready = TRUE;
114 ws->buffer_unmap(ws, q->buffer);
115 }
116
117 *result = q->result;
118 return q->ready;
119 }
120
121 void
122 nv50_init_query_functions(struct nv50_context *nv50)
123 {
124 nv50->pipe.create_query = nv50_query_create;
125 nv50->pipe.destroy_query = nv50_query_destroy;
126 nv50->pipe.begin_query = nv50_query_begin;
127 nv50->pipe.end_query = nv50_query_end;
128 nv50->pipe.get_query_result = nv50_query_result;
129 }