blob: 1f00f98df5d5b597fe4b43a4ceeaed2554bcd16a [file] [log] [blame]
Chia-I Wue18ff1b2014-08-07 13:38:51 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wue18ff1b2014-08-07 13:38:51 +080026 */
27
28#include "dev.h"
29#include "mem.h"
30#include "query.h"
31
32static void query_destroy(struct intel_obj *obj)
33{
34 struct intel_query *query = intel_query_from_obj(obj);
35
36 intel_query_destroy(query);
37}
38
39static XGL_RESULT query_get_info(struct intel_base *base, int type,
40 XGL_SIZE *size, XGL_VOID *data)
41{
42 struct intel_query *query = intel_query_from_base(base);
43 XGL_RESULT ret = XGL_SUCCESS;
44
45 switch (type) {
46 case XGL_INFO_TYPE_MEMORY_REQUIREMENTS:
47 {
48 XGL_MEMORY_REQUIREMENTS *mem_req = data;
49
Jon Ashburn408daec2014-12-05 09:23:52 -070050 *size = sizeof(XGL_MEMORY_REQUIREMENTS);
51 if (data == NULL)
52 return ret;
Chia-I Wue18ff1b2014-08-07 13:38:51 +080053 mem_req->size = query->slot_stride * query->slot_count;
54 mem_req->alignment = 64;
55 mem_req->heapCount = 1;
56 mem_req->heaps[0] = 0;
57
Chia-I Wue18ff1b2014-08-07 13:38:51 +080058 }
59 break;
60 default:
61 ret = intel_base_get_info(base, type, size, data);
62 break;
63 }
64
65 return ret;
66}
67
68XGL_RESULT intel_query_create(struct intel_dev *dev,
69 const XGL_QUERY_POOL_CREATE_INFO *info,
70 struct intel_query **query_ret)
71{
72 struct intel_query *query;
73
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -060074 query = (struct intel_query *) intel_base_create(dev, sizeof(*query),
Chia-I Wue18ff1b2014-08-07 13:38:51 +080075 dev->base.dbg, XGL_DBG_OBJECT_QUERY_POOL, info, 0);
76 if (!query)
77 return XGL_ERROR_OUT_OF_MEMORY;
78
79 query->type = info->queryType;
80 query->slot_count = info->slots;
81
Chia-I Wu659650f2014-08-07 14:11:49 +080082 /*
83 * For each query type, the GPU will be asked to write the values of some
84 * registers to a buffer before and after a sequence of commands. We will
85 * compare the differences to get the query results.
86 */
Chia-I Wue18ff1b2014-08-07 13:38:51 +080087 switch (info->queryType) {
88 case XGL_QUERY_OCCLUSION:
Chia-I Wu759fa2e2014-08-30 18:44:47 +080089 query->slot_stride = u_align(sizeof(uint64_t) * 2, 64);
Chia-I Wue18ff1b2014-08-07 13:38:51 +080090 break;
91 case XGL_QUERY_PIPELINE_STATISTICS:
92 query->slot_stride =
Chia-I Wu2132b562014-08-08 21:52:26 +080093 u_align(sizeof(XGL_PIPELINE_STATISTICS_DATA) * 2, 64);
Chia-I Wue18ff1b2014-08-07 13:38:51 +080094 break;
95 default:
96 break;
97 }
98
99 if (!query->slot_stride) {
100 intel_query_destroy(query);
101 return XGL_ERROR_INVALID_VALUE;
102 }
103
104 query->obj.base.get_info = query_get_info;
105 query->obj.destroy = query_destroy;
106
107 *query_ret = query;
108
109 return XGL_SUCCESS;
110}
111
112void intel_query_destroy(struct intel_query *query)
113{
114 intel_base_destroy(&query->obj.base);
115}
116
Chia-I Wu659650f2014-08-07 14:11:49 +0800117static void
118query_process_occlusion(const struct intel_query *query,
119 XGL_UINT count, const uint8_t *raw,
120 uint64_t *results)
121{
122 XGL_UINT i;
123
124 for (i = 0; i < count; i++) {
125 const uint32_t *pair = (const uint32_t *) raw;
126
127 results[i] = pair[1] - pair[0];
128 raw += query->slot_stride;
129 }
130}
131
132static void
133query_process_pipeline_statistics(const struct intel_query *query,
134 XGL_UINT count, const uint8_t *raw,
135 XGL_PIPELINE_STATISTICS_DATA *results)
136{
137 const XGL_UINT num_regs = sizeof(results[0]) / sizeof(uint64_t);
138 XGL_UINT i, j;
139
140 for (i = 0; i < count; i++) {
141 const uint64_t *before = (const uint64_t *) raw;
142 const uint64_t *after = before + num_regs;
143 uint64_t *dst = (uint64_t *) (results + i);
144
145 for (j = 0; j < num_regs; j++)
146 dst[j] = after[j] - before[j];
147
148 raw += query->slot_stride;
149 }
150}
151
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800152XGL_RESULT intel_query_get_results(struct intel_query *query,
153 XGL_UINT slot_start, XGL_UINT slot_count,
154 void *results)
155{
156 const uint8_t *ptr;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800157
158 if (!query->obj.mem)
159 return XGL_ERROR_MEMORY_NOT_BOUND;
160
161 if (intel_mem_is_busy(query->obj.mem))
162 return XGL_NOT_READY;
163
164 ptr = (const uint8_t *) intel_mem_map_sync(query->obj.mem, false);
165 if (!ptr)
166 return XGL_ERROR_MEMORY_MAP_FAILED;
167
168 ptr += query->obj.offset + query->slot_stride * slot_start;
169
170 switch (query->type) {
171 case XGL_QUERY_OCCLUSION:
Chia-I Wu659650f2014-08-07 14:11:49 +0800172 query_process_occlusion(query, slot_count, ptr, results);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800173 break;
174 case XGL_QUERY_PIPELINE_STATISTICS:
Chia-I Wu659650f2014-08-07 14:11:49 +0800175 query_process_pipeline_statistics(query, slot_count, ptr, results);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800176 break;
177 default:
178 assert(0);
179 break;
180 }
181
182 intel_mem_unmap(query->obj.mem);
183
184 return XGL_SUCCESS;
185}
186
187XGL_RESULT XGLAPI intelCreateQueryPool(
188 XGL_DEVICE device,
189 const XGL_QUERY_POOL_CREATE_INFO* pCreateInfo,
190 XGL_QUERY_POOL* pQueryPool)
191{
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800192 struct intel_dev *dev = intel_dev(device);
193
194 return intel_query_create(dev, pCreateInfo,
195 (struct intel_query **) pQueryPool);
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800196}
197
198XGL_RESULT XGLAPI intelGetQueryPoolResults(
199 XGL_QUERY_POOL queryPool,
200 XGL_UINT startQuery,
201 XGL_UINT queryCount,
202 XGL_SIZE* pDataSize,
203 XGL_VOID* pData)
204{
Chia-I Wuf667a6a2014-08-07 14:15:01 +0800205 struct intel_query *query = intel_query(queryPool);
206
207 switch (query->type) {
208 case XGL_QUERY_OCCLUSION:
209 *pDataSize = sizeof(uint64_t) * queryCount;
210 break;
211 case XGL_QUERY_PIPELINE_STATISTICS:
212 *pDataSize = sizeof(XGL_PIPELINE_STATISTICS_DATA) * queryCount;
213 break;
214 default:
215 return XGL_ERROR_INVALID_HANDLE;
216 break;
217 }
218
219 if (pData)
220 return intel_query_get_results(query, startQuery, queryCount, pData);
221 else
222 return XGL_SUCCESS;
Chia-I Wue18ff1b2014-08-07 13:38:51 +0800223}