2 * Copyright (c) 2016 Intel Corporation
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:
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
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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 /** @file hsw_queryobj.c
27 * Support for query buffer objects (GL_ARB_query_buffer_object) on Haswell+.
29 #include "brw_context.h"
30 #include "brw_defines.h"
31 #include "intel_batchbuffer.h"
32 #include "intel_buffer_objects.h"
38 mult_gpr0_by_80(struct brw_context
*brw
)
40 static const uint32_t maths
[] = {
41 MI_MATH_ALU2(LOAD
, SRCA
, R0
),
42 MI_MATH_ALU2(LOAD
, SRCB
, R0
),
44 MI_MATH_ALU2(STORE
, R1
, ACCU
),
45 MI_MATH_ALU2(LOAD
, SRCA
, R1
),
46 MI_MATH_ALU2(LOAD
, SRCB
, R1
),
48 MI_MATH_ALU2(STORE
, R1
, ACCU
),
49 MI_MATH_ALU2(LOAD
, SRCA
, R1
),
50 MI_MATH_ALU2(LOAD
, SRCB
, R1
),
52 MI_MATH_ALU2(STORE
, R1
, ACCU
),
53 MI_MATH_ALU2(LOAD
, SRCA
, R1
),
54 MI_MATH_ALU2(LOAD
, SRCB
, R1
),
56 /* GPR1 = 16 * GPR0 */
57 MI_MATH_ALU2(STORE
, R1
, ACCU
),
58 MI_MATH_ALU2(LOAD
, SRCA
, R1
),
59 MI_MATH_ALU2(LOAD
, SRCB
, R1
),
61 MI_MATH_ALU2(STORE
, R2
, ACCU
),
62 MI_MATH_ALU2(LOAD
, SRCA
, R2
),
63 MI_MATH_ALU2(LOAD
, SRCB
, R2
),
65 /* GPR2 = 64 * GPR0 */
66 MI_MATH_ALU2(STORE
, R2
, ACCU
),
67 MI_MATH_ALU2(LOAD
, SRCA
, R1
),
68 MI_MATH_ALU2(LOAD
, SRCB
, R2
),
70 /* GPR0 = 80 * GPR0 */
71 MI_MATH_ALU2(STORE
, R0
, ACCU
),
74 BEGIN_BATCH(1 + ARRAY_SIZE(maths
));
75 OUT_BATCH(HSW_MI_MATH
| (1 + ARRAY_SIZE(maths
) - 2));
77 for (int m
= 0; m
< ARRAY_SIZE(maths
); m
++)
84 * GPR0 = GPR0 & ((1ull << n) - 1);
87 keep_gpr0_lower_n_bits(struct brw_context
*brw
, uint32_t n
)
89 static const uint32_t maths
[] = {
90 MI_MATH_ALU2(LOAD
, SRCA
, R0
),
91 MI_MATH_ALU2(LOAD
, SRCB
, R1
),
93 MI_MATH_ALU2(STORE
, R0
, ACCU
),
97 brw_load_register_imm64(brw
, HSW_CS_GPR(1), (1ull << n
) - 1);
99 BEGIN_BATCH(1 + ARRAY_SIZE(maths
));
100 OUT_BATCH(HSW_MI_MATH
| (1 + ARRAY_SIZE(maths
) - 2));
102 for (int m
= 0; m
< ARRAY_SIZE(maths
); m
++)
112 shl_gpr0_by_30_bits(struct brw_context
*brw
)
114 /* First we mask 34 bits of GPR0 to prevent overflow */
115 keep_gpr0_lower_n_bits(brw
, 34);
117 static const uint32_t shl_maths
[] = {
118 MI_MATH_ALU2(LOAD
, SRCA
, R0
),
119 MI_MATH_ALU2(LOAD
, SRCB
, R0
),
121 MI_MATH_ALU2(STORE
, R0
, ACCU
),
124 const uint32_t outer_count
= 5;
125 const uint32_t inner_count
= 6;
126 STATIC_ASSERT(outer_count
* inner_count
== 30);
127 const uint32_t cmd_len
= 1 + inner_count
* ARRAY_SIZE(shl_maths
);
128 const uint32_t batch_len
= cmd_len
* outer_count
;
130 BEGIN_BATCH(batch_len
);
132 /* We'll emit 5 commands, each shifting GPR0 left by 6 bits, for a total of
135 for (int o
= 0; o
< outer_count
; o
++) {
136 /* Submit one MI_MATH to shift left by 6 bits */
137 OUT_BATCH(HSW_MI_MATH
| (cmd_len
- 2));
138 for (int i
= 0; i
< inner_count
; i
++)
139 for (int m
= 0; m
< ARRAY_SIZE(shl_maths
); m
++)
140 OUT_BATCH(shl_maths
[m
]);
149 * Note that the upper 30 bits of GPR0 are lost!
152 shr_gpr0_by_2_bits(struct brw_context
*brw
)
154 shl_gpr0_by_30_bits(brw
);
155 brw_load_register_reg(brw
, HSW_CS_GPR(0), HSW_CS_GPR(0) + 4);
156 brw_load_register_imm32(brw
, HSW_CS_GPR(0) + 4, 0);
160 * GPR0 = (GPR0 == 0) ? 0 : 1;
163 gpr0_to_bool(struct brw_context
*brw
)
165 static const uint32_t maths
[] = {
166 MI_MATH_ALU2(LOAD
, SRCA
, R0
),
167 MI_MATH_ALU1(LOAD0
, SRCB
),
169 MI_MATH_ALU2(STOREINV
, R0
, ZF
),
170 MI_MATH_ALU2(LOAD
, SRCA
, R0
),
171 MI_MATH_ALU2(LOAD
, SRCB
, R1
),
173 MI_MATH_ALU2(STORE
, R0
, ACCU
),
176 brw_load_register_imm64(brw
, HSW_CS_GPR(1), 1ull);
178 BEGIN_BATCH(1 + ARRAY_SIZE(maths
));
179 OUT_BATCH(HSW_MI_MATH
| (1 + ARRAY_SIZE(maths
) - 2));
181 for (int m
= 0; m
< ARRAY_SIZE(maths
); m
++)
188 load_overflow_data_to_cs_gprs(struct brw_context
*brw
,
189 struct brw_query_object
*query
,
192 int offset
= idx
* sizeof(uint64_t) * 4;
194 brw_load_register_mem64(brw
, HSW_CS_GPR(1), query
->bo
, offset
);
196 offset
+= sizeof(uint64_t);
197 brw_load_register_mem64(brw
, HSW_CS_GPR(2), query
->bo
, offset
);
199 offset
+= sizeof(uint64_t);
200 brw_load_register_mem64(brw
, HSW_CS_GPR(3), query
->bo
, offset
);
202 offset
+= sizeof(uint64_t);
203 brw_load_register_mem64(brw
, HSW_CS_GPR(4), query
->bo
, offset
);
213 calc_overflow_for_stream(struct brw_context
*brw
)
215 static const uint32_t maths
[] = {
216 MI_MATH_ALU2(LOAD
, SRCA
, R4
),
217 MI_MATH_ALU2(LOAD
, SRCB
, R3
),
219 MI_MATH_ALU2(STORE
, R3
, ACCU
),
220 MI_MATH_ALU2(LOAD
, SRCA
, R2
),
221 MI_MATH_ALU2(LOAD
, SRCB
, R1
),
223 MI_MATH_ALU2(STORE
, R1
, ACCU
),
224 MI_MATH_ALU2(LOAD
, SRCA
, R3
),
225 MI_MATH_ALU2(LOAD
, SRCB
, R1
),
227 MI_MATH_ALU2(STORE
, R1
, ACCU
),
228 MI_MATH_ALU2(LOAD
, SRCA
, R1
),
229 MI_MATH_ALU2(LOAD
, SRCB
, R0
),
231 MI_MATH_ALU2(STORE
, R0
, ACCU
),
234 BEGIN_BATCH(1 + ARRAY_SIZE(maths
));
235 OUT_BATCH(HSW_MI_MATH
| (1 + ARRAY_SIZE(maths
) - 2));
237 for (int m
= 0; m
< ARRAY_SIZE(maths
); m
++)
244 calc_overflow_to_gpr0(struct brw_context
*brw
, struct brw_query_object
*query
,
247 brw_load_register_imm64(brw
, HSW_CS_GPR(0), 0ull);
249 for (int i
= 0; i
< count
; i
++) {
250 load_overflow_data_to_cs_gprs(brw
, query
, i
);
251 calc_overflow_for_stream(brw
);
256 * Take a query and calculate whether there was overflow during transform
257 * feedback. Store the result in the gpr0 register.
260 hsw_overflow_result_to_gpr0(struct brw_context
*brw
,
261 struct brw_query_object
*query
,
264 calc_overflow_to_gpr0(brw
, query
, count
);
269 hsw_result_to_gpr0(struct gl_context
*ctx
, struct brw_query_object
*query
,
270 struct gl_buffer_object
*buf
, intptr_t offset
,
271 GLenum pname
, GLenum ptype
)
273 struct brw_context
*brw
= brw_context(ctx
);
274 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
277 assert(pname
!= GL_QUERY_TARGET
);
279 if (pname
== GL_QUERY_RESULT_AVAILABLE
) {
280 /* The query result availability is stored at offset 0 of the buffer. */
281 brw_load_register_mem64(brw
,
284 2 * sizeof(uint64_t));
288 if (pname
== GL_QUERY_RESULT
) {
289 /* Since GL_QUERY_RESULT_NO_WAIT wasn't used, they want us to stall to
290 * make sure the query is available.
292 brw_emit_pipe_control_flush(brw
,
293 PIPE_CONTROL_CS_STALL
|
294 PIPE_CONTROL_STALL_AT_SCOREBOARD
);
297 if (query
->Base
.Target
== GL_TIMESTAMP
) {
298 brw_load_register_mem64(brw
,
301 0 * sizeof(uint64_t));
302 } else if (query
->Base
.Target
== GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB
303 || query
->Base
.Target
== GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB
) {
304 /* Don't do anything in advance here, since the math for this is a little
308 brw_load_register_mem64(brw
,
311 0 * sizeof(uint64_t));
312 brw_load_register_mem64(brw
,
315 1 * sizeof(uint64_t));
318 OUT_BATCH(HSW_MI_MATH
| (5 - 2));
320 OUT_BATCH(MI_MATH_ALU2(LOAD
, SRCA
, R2
));
321 OUT_BATCH(MI_MATH_ALU2(LOAD
, SRCB
, R1
));
322 OUT_BATCH(MI_MATH_ALU0(SUB
));
323 OUT_BATCH(MI_MATH_ALU2(STORE
, R0
, ACCU
));
328 switch (query
->Base
.Target
) {
329 case GL_FRAGMENT_SHADER_INVOCATIONS_ARB
:
330 /* Implement the "WaDividePSInvocationCountBy4:HSW,BDW" workaround:
331 * "Invocation counter is 4 times actual. WA: SW to divide HW reported
332 * PS Invocations value by 4."
334 * Prior to Haswell, invocation count was counted by the WM, and it
335 * buggily counted invocations in units of subspans (2x2 unit). To get the
336 * correct value, the CS multiplied this by 4. With HSW the logic moved,
337 * and correctly emitted the number of pixel shader invocations, but,
338 * whomever forgot to undo the multiply by 4.
340 if (devinfo
->gen
== 8 || devinfo
->is_haswell
)
341 shr_gpr0_by_2_bits(brw
);
343 case GL_TIME_ELAPSED
:
345 mult_gpr0_by_80(brw
);
346 if (query
->Base
.Target
== GL_TIMESTAMP
) {
347 keep_gpr0_lower_n_bits(brw
, 36);
350 case GL_ANY_SAMPLES_PASSED
:
351 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE
:
354 case GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB
:
355 hsw_overflow_result_to_gpr0(brw
, query
, 1);
357 case GL_TRANSFORM_FEEDBACK_OVERFLOW_ARB
:
358 hsw_overflow_result_to_gpr0(brw
, query
, MAX_VERTEX_STREAMS
);
364 * Store immediate data into the user buffer using the requested size.
367 store_query_result_imm(struct brw_context
*brw
, struct brw_bo
*bo
,
368 uint32_t offset
, GLenum ptype
, uint64_t imm
)
372 case GL_UNSIGNED_INT
:
373 brw_store_data_imm32(brw
, bo
, offset
, imm
);
376 case GL_UNSIGNED_INT64_ARB
:
377 brw_store_data_imm64(brw
, bo
, offset
, imm
);
380 unreachable("Unexpected result type");
385 set_predicate(struct brw_context
*brw
, struct brw_bo
*query_bo
)
387 brw_load_register_imm64(brw
, MI_PREDICATE_SRC1
, 0ull);
389 /* Load query availability into SRC0 */
390 brw_load_register_mem64(brw
, MI_PREDICATE_SRC0
, query_bo
,
391 2 * sizeof(uint64_t));
393 /* predicate = !(query_availability == 0); */
395 OUT_BATCH(GEN7_MI_PREDICATE
|
396 MI_PREDICATE_LOADOP_LOADINV
|
397 MI_PREDICATE_COMBINEOP_SET
|
398 MI_PREDICATE_COMPAREOP_SRCS_EQUAL
);
403 * Store data from the register into the user buffer using the requested size.
404 * The write also enables the predication to prevent writing the result if the
405 * query has not finished yet.
408 store_query_result_reg(struct brw_context
*brw
, struct brw_bo
*bo
,
409 uint32_t offset
, GLenum ptype
, uint32_t reg
,
410 const bool pipelined
)
412 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
413 uint32_t cmd_size
= devinfo
->gen
>= 8 ? 4 : 3;
414 uint32_t dwords
= (ptype
== GL_INT
|| ptype
== GL_UNSIGNED_INT
) ? 1 : 2;
415 assert(devinfo
->gen
>= 6);
417 BEGIN_BATCH(dwords
* cmd_size
);
418 for (int i
= 0; i
< dwords
; i
++) {
419 OUT_BATCH(MI_STORE_REGISTER_MEM
|
420 (pipelined
? MI_STORE_REGISTER_MEM_PREDICATE
: 0) |
422 OUT_BATCH(reg
+ 4 * i
);
423 if (devinfo
->gen
>= 8) {
424 OUT_RELOC64(bo
, RELOC_WRITE
, offset
+ 4 * i
);
426 OUT_RELOC(bo
, RELOC_WRITE
| RELOC_NEEDS_GGTT
, offset
+ 4 * i
);
433 hsw_store_query_result(struct gl_context
*ctx
, struct gl_query_object
*q
,
434 struct gl_buffer_object
*buf
, intptr_t offset
,
435 GLenum pname
, GLenum ptype
)
437 struct brw_context
*brw
= brw_context(ctx
);
438 struct brw_query_object
*query
= (struct brw_query_object
*)q
;
439 struct intel_buffer_object
*bo
= intel_buffer_object(buf
);
440 const bool pipelined
= brw_is_query_pipelined(query
);
442 if (pname
== GL_QUERY_TARGET
) {
443 store_query_result_imm(brw
, bo
->buffer
, offset
, ptype
,
446 } else if (pname
== GL_QUERY_RESULT_AVAILABLE
&& !pipelined
) {
447 store_query_result_imm(brw
, bo
->buffer
, offset
, ptype
, 1ull);
448 } else if (query
->bo
) {
449 /* The query bo still around. Therefore, we:
451 * 1. Compute the current result in GPR0
452 * 2. Set the command streamer predicate based on query availability
453 * 3. (With predication) Write GPR0 to the requested buffer
455 hsw_result_to_gpr0(ctx
, query
, buf
, offset
, pname
, ptype
);
457 set_predicate(brw
, query
->bo
);
458 store_query_result_reg(brw
, bo
->buffer
, offset
, ptype
, HSW_CS_GPR(0),
461 /* The query bo is gone, so the query must have been processed into
462 * client memory. In this case we can fill the buffer location with the
463 * requested data using MI_STORE_DATA_IMM.
466 case GL_QUERY_RESULT_AVAILABLE
:
467 store_query_result_imm(brw
, bo
->buffer
, offset
, ptype
, 1ull);
469 case GL_QUERY_RESULT_NO_WAIT
:
470 case GL_QUERY_RESULT
:
471 store_query_result_imm(brw
, bo
->buffer
, offset
, ptype
,
475 unreachable("Unexpected result type");
481 /* Initialize hsw+-specific query object functions. */
482 void hsw_init_queryobj_functions(struct dd_function_table
*functions
)
484 gen6_init_queryobj_functions(functions
);
485 functions
->StoreQueryResult
= hsw_store_query_result
;