Merge branch 'gallium-front-ccw'
[mesa.git] / src / gallium / drivers / softpipe / sp_query.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* Author:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include "draw/draw_context.h"
33 #include "os/os_time.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_memory.h"
36 #include "sp_context.h"
37 #include "sp_query.h"
38 #include "sp_state.h"
39
40 struct softpipe_query {
41 unsigned type;
42 uint64_t start;
43 uint64_t end;
44 };
45
46
47 static struct softpipe_query *softpipe_query( struct pipe_query *p )
48 {
49 return (struct softpipe_query *)p;
50 }
51
52 static struct pipe_query *
53 softpipe_create_query(struct pipe_context *pipe,
54 unsigned type)
55 {
56 struct softpipe_query* sq;
57
58 assert(type == PIPE_QUERY_OCCLUSION_COUNTER || type == PIPE_QUERY_TIME_ELAPSED);
59 sq = CALLOC_STRUCT( softpipe_query );
60 sq->type = type;
61
62 return (struct pipe_query *)sq;
63 }
64
65
66 static void
67 softpipe_destroy_query(struct pipe_context *pipe, struct pipe_query *q)
68 {
69 FREE(q);
70 }
71
72
73 static void
74 softpipe_begin_query(struct pipe_context *pipe, struct pipe_query *q)
75 {
76 struct softpipe_context *softpipe = softpipe_context( pipe );
77 struct softpipe_query *sq = softpipe_query(q);
78
79 switch (sq->type) {
80 case PIPE_QUERY_OCCLUSION_COUNTER:
81 sq->start = softpipe->occlusion_count;
82 break;
83 case PIPE_QUERY_TIME_ELAPSED:
84 sq->start = 1000*os_time_get();
85 break;
86 default:
87 assert(0);
88 break;
89 }
90 softpipe->active_query_count++;
91 softpipe->dirty |= SP_NEW_QUERY;
92 }
93
94
95 static void
96 softpipe_end_query(struct pipe_context *pipe, struct pipe_query *q)
97 {
98 struct softpipe_context *softpipe = softpipe_context( pipe );
99 struct softpipe_query *sq = softpipe_query(q);
100
101 softpipe->active_query_count--;
102 switch (sq->type) {
103 case PIPE_QUERY_OCCLUSION_COUNTER:
104 sq->end = softpipe->occlusion_count;
105 break;
106 case PIPE_QUERY_TIME_ELAPSED:
107 sq->end = 1000*os_time_get();
108 break;
109 default:
110 assert(0);
111 break;
112 }
113 softpipe->dirty |= SP_NEW_QUERY;
114 }
115
116
117 static boolean
118 softpipe_get_query_result(struct pipe_context *pipe,
119 struct pipe_query *q,
120 boolean wait,
121 uint64_t *result )
122 {
123 struct softpipe_query *sq = softpipe_query(q);
124 *result = sq->end - sq->start;
125 return TRUE;
126 }
127
128
129 /**
130 * Called by rendering function to check rendering is conditional.
131 * \return TRUE if we should render, FALSE if we should skip rendering
132 */
133 boolean
134 softpipe_check_render_cond(struct softpipe_context *sp)
135 {
136 struct pipe_context *pipe = &sp->pipe;
137 boolean b, wait;
138 uint64_t result;
139
140 if (!sp->render_cond_query) {
141 return TRUE; /* no query predicate, draw normally */
142 }
143
144 wait = (sp->render_cond_mode == PIPE_RENDER_COND_WAIT ||
145 sp->render_cond_mode == PIPE_RENDER_COND_BY_REGION_WAIT);
146
147 b = pipe->get_query_result(pipe, sp->render_cond_query, wait, &result);
148 if (b)
149 return result > 0;
150 else
151 return TRUE;
152 }
153
154
155 void softpipe_init_query_funcs(struct softpipe_context *softpipe )
156 {
157 softpipe->pipe.create_query = softpipe_create_query;
158 softpipe->pipe.destroy_query = softpipe_destroy_query;
159 softpipe->pipe.begin_query = softpipe_begin_query;
160 softpipe->pipe.end_query = softpipe_end_query;
161 softpipe->pipe.get_query_result = softpipe_get_query_result;
162 }
163
164