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 
29 /**
30  * glBegin/EndQuery interface to pipe
31  *
32  * \author Brian Paul
33  */
34 
35 
36 #include "main/imports.h"
37 #include "main/context.h"
38 #include "main/mfeatures.h"
39 
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "pipe/p_screen.h"
43 #include "st_context.h"
44 #include "st_cb_queryobj.h"
45 #include "st_cb_bitmap.h"
46 
47 
48 #if FEATURE_queryobj
49 
50 static struct gl_query_object *
st_NewQueryObject(struct gl_context * ctx,GLuint id)51 st_NewQueryObject(struct gl_context *ctx, GLuint id)
52 {
53    struct st_query_object *stq = ST_CALLOC_STRUCT(st_query_object);
54    if (stq) {
55       stq->base.Id = id;
56       stq->base.Ready = GL_TRUE;
57       stq->pq = NULL;
58       stq->type = PIPE_QUERY_TYPES; /* an invalid value */
59       return &stq->base;
60    }
61    return NULL;
62 }
63 
64 
65 
66 static void
st_DeleteQuery(struct gl_context * ctx,struct gl_query_object * q)67 st_DeleteQuery(struct gl_context *ctx, struct gl_query_object *q)
68 {
69    struct pipe_context *pipe = st_context(ctx)->pipe;
70    struct st_query_object *stq = st_query_object(q);
71 
72    if (stq->pq) {
73       pipe->destroy_query(pipe, stq->pq);
74       stq->pq = NULL;
75    }
76 
77    free(stq);
78 }
79 
80 
81 static void
st_BeginQuery(struct gl_context * ctx,struct gl_query_object * q)82 st_BeginQuery(struct gl_context *ctx, struct gl_query_object *q)
83 {
84    struct pipe_context *pipe = st_context(ctx)->pipe;
85    struct st_query_object *stq = st_query_object(q);
86    unsigned type;
87 
88    st_flush_bitmap_cache(st_context(ctx));
89 
90    /* convert GL query type to Gallium query type */
91    switch (q->Target) {
92    case GL_ANY_SAMPLES_PASSED:
93       /* fall-through */
94    case GL_SAMPLES_PASSED_ARB:
95       type = PIPE_QUERY_OCCLUSION_COUNTER;
96       break;
97    case GL_PRIMITIVES_GENERATED:
98       type = PIPE_QUERY_PRIMITIVES_GENERATED;
99       break;
100    case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
101       type = PIPE_QUERY_PRIMITIVES_EMITTED;
102       break;
103    case GL_TIME_ELAPSED_EXT:
104       type = PIPE_QUERY_TIME_ELAPSED;
105       break;
106    default:
107       assert(0 && "unexpected query target in st_BeginQuery()");
108       return;
109    }
110 
111    if (stq->pq && stq->type != type) {
112       /* free old query of different type */
113       pipe->destroy_query(pipe, stq->pq);
114       stq->pq = NULL;
115       stq->type = PIPE_QUERY_TYPES; /* an invalid value */
116    }
117 
118    if (!stq->pq) {
119       stq->pq = pipe->create_query(pipe, type);
120       stq->type = type;
121    }
122 
123    assert(stq->type == type);
124 
125    pipe->begin_query(pipe, stq->pq);
126 }
127 
128 
129 static void
st_EndQuery(struct gl_context * ctx,struct gl_query_object * q)130 st_EndQuery(struct gl_context *ctx, struct gl_query_object *q)
131 {
132    struct pipe_context *pipe = st_context(ctx)->pipe;
133    struct st_query_object *stq = st_query_object(q);
134 
135    st_flush_bitmap_cache(st_context(ctx));
136 
137    if (q->Target == GL_TIMESTAMP && !stq->pq) {
138       stq->pq = pipe->create_query(pipe, PIPE_QUERY_TIMESTAMP);
139       stq->type = PIPE_QUERY_TIMESTAMP;
140    }
141 
142    pipe->end_query(pipe, stq->pq);
143 }
144 
145 
146 static void
st_WaitQuery(struct gl_context * ctx,struct gl_query_object * q)147 st_WaitQuery(struct gl_context *ctx, struct gl_query_object *q)
148 {
149    struct pipe_context *pipe = st_context(ctx)->pipe;
150    struct st_query_object *stq = st_query_object(q);
151 
152    /* this function should only be called if we don't have a ready result */
153    assert(!stq->base.Ready);
154 
155    while (!stq->base.Ready &&
156 	  !pipe->get_query_result(pipe,
157 				  stq->pq,
158 				  TRUE,
159 				  (void*)&q->Result))
160    {
161       /* nothing */
162    }
163 
164    q->Ready = GL_TRUE;
165 }
166 
167 
168 static void
st_CheckQuery(struct gl_context * ctx,struct gl_query_object * q)169 st_CheckQuery(struct gl_context *ctx, struct gl_query_object *q)
170 {
171    struct pipe_context *pipe = st_context(ctx)->pipe;
172    struct st_query_object *stq = st_query_object(q);
173    assert(!q->Ready);   /* we should not get called if Ready is TRUE */
174    q->Ready = pipe->get_query_result(pipe, stq->pq, FALSE, (void*)&q->Result);
175 }
176 
177 
178 static uint64_t
st_GetTimestamp(struct gl_context * ctx)179 st_GetTimestamp(struct gl_context *ctx)
180 {
181    struct pipe_screen *screen = st_context(ctx)->pipe->screen;
182 
183    return screen->get_timestamp(screen);
184 }
185 
186 
st_init_query_functions(struct dd_function_table * functions)187 void st_init_query_functions(struct dd_function_table *functions)
188 {
189    functions->NewQueryObject = st_NewQueryObject;
190    functions->DeleteQuery = st_DeleteQuery;
191    functions->BeginQuery = st_BeginQuery;
192    functions->EndQuery = st_EndQuery;
193    functions->WaitQuery = st_WaitQuery;
194    functions->CheckQuery = st_CheckQuery;
195    functions->GetTimestamp = st_GetTimestamp;
196 }
197 
198 #endif /* FEATURE_queryobj */
199