2 * Copyright © 2013 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
21 * DEALINGS IN THE SOFTWARE.
25 * \file brw_vec4_tcs.cpp
27 * Tessellaton control shader specific code derived from the vec4_visitor class.
31 #include "brw_vec4_tcs.h"
35 vec4_tcs_visitor::vec4_tcs_visitor(const struct brw_compiler
*compiler
,
37 const struct brw_tcs_prog_key
*key
,
38 struct brw_tcs_prog_data
*prog_data
,
39 const nir_shader
*nir
,
41 int shader_time_index
,
42 const struct brw_vue_map
*input_vue_map
)
43 : vec4_visitor(compiler
, log_data
, &key
->tex
, &prog_data
->base
,
44 nir
, mem_ctx
, false, shader_time_index
),
45 input_vue_map(input_vue_map
), key(key
)
51 vec4_tcs_visitor::emit_nir_code()
53 if (key
->program_string_id
!= 0) {
54 /* We have a real application-supplied TCS, emit real code. */
55 vec4_visitor::emit_nir_code();
57 /* There is no TCS; automatically generate a passthrough shader
58 * that writes the API-specified default tessellation levels and
59 * copies VS outputs to TES inputs.
63 uint64_t varyings
= key
->outputs_written
;
65 src_reg
vertex_offset(this, glsl_type::uint_type
);
66 emit(MUL(dst_reg(vertex_offset
), invocation_id
,
67 brw_imm_ud(prog_data
->vue_map
.num_per_vertex_slots
)));
69 while (varyings
!= 0) {
70 const int varying
= ffsll(varyings
) - 1;
72 unsigned in_offset
= input_vue_map
->varying_to_slot
[varying
];
73 unsigned out_offset
= prog_data
->vue_map
.varying_to_slot
[varying
];
74 assert(out_offset
>= 2);
76 dst_reg
val(this, glsl_type::vec4_type
);
77 emit_input_urb_read(val
, invocation_id
, in_offset
, src_reg());
78 emit_urb_write(src_reg(val
), WRITEMASK_XYZW
, out_offset
,
81 varyings
&= ~BITFIELD64_BIT(varying
);
84 /* Only write the tessellation factors from invocation 0.
85 * There's no point in making other threads do redundant work.
87 emit(CMP(dst_null_d(), invocation_id
, brw_imm_ud(0),
89 emit(IF(BRW_PREDICATE_NORMAL
));
90 emit_urb_write(src_reg(UNIFORM
, 0, glsl_type::vec4_type
),
91 WRITEMASK_XYZW
, 0, src_reg());
92 emit_urb_write(src_reg(UNIFORM
, 1, glsl_type::vec4_type
),
93 WRITEMASK_XYZW
, 1, src_reg());
94 emit(BRW_OPCODE_ENDIF
);
99 vec4_tcs_visitor::nir_setup_system_value_intrinsic(nir_intrinsic_instr
*instr
)
104 vec4_tcs_visitor::make_reg_for_system_value(int location
, const glsl_type
*type
)
111 vec4_tcs_visitor::setup_payload()
115 /* The payload always contains important data in r0, which contains
116 * the URB handles that are passed on to the URB write at the end
121 /* r1.0 - r4.7 may contain the input control point URB handles,
122 * which we use to pull vertex data.
126 /* Push constants may start at r5.0 */
127 reg
= setup_uniforms(reg
);
129 this->first_non_payload_grf
= reg
;
134 vec4_tcs_visitor::emit_prolog()
136 invocation_id
= src_reg(this, glsl_type::uint_type
);
137 emit(TCS_OPCODE_GET_INSTANCE_ID
, dst_reg(invocation_id
));
139 /* HS threads are dispatched with the dispatch mask set to 0xFF.
140 * If there are an odd number of output vertices, then the final
141 * HS instance dispatched will only have its bottom half doing real
142 * work, and so we need to disable the upper half:
144 if (nir
->info
.tcs
.vertices_out
% 2) {
145 emit(CMP(dst_null_d(), invocation_id
,
146 brw_imm_ud(nir
->info
.tcs
.vertices_out
), BRW_CONDITIONAL_L
));
148 /* Matching ENDIF is in emit_thread_end() */
149 emit(IF(BRW_PREDICATE_NORMAL
));
155 vec4_tcs_visitor::emit_thread_end()
157 vec4_instruction
*inst
;
158 current_annotation
= "thread end";
160 if (nir
->info
.tcs
.vertices_out
% 2) {
161 emit(BRW_OPCODE_ENDIF
);
164 if (devinfo
->gen
== 7) {
165 struct brw_tcs_prog_data
*tcs_prog_data
=
166 (struct brw_tcs_prog_data
*) prog_data
;
168 current_annotation
= "release input vertices";
170 /* Synchronize all threads, so we know that no one is still
171 * using the input URB handles.
173 if (tcs_prog_data
->instances
> 1) {
174 dst_reg header
= dst_reg(this, glsl_type::uvec4_type
);
175 emit(TCS_OPCODE_CREATE_BARRIER_HEADER
, header
);
176 emit(SHADER_OPCODE_BARRIER
, dst_null_ud(), src_reg(header
));
179 /* Make thread 0 (invocations <1, 0>) release pairs of ICP handles.
180 * We want to compare the bottom half of invocation_id with 0, but
181 * use that truth value for the top half as well. Unfortunately,
182 * we don't have stride in the vec4 world, nor UV immediates in
183 * align16, so we need an opcode to get invocation_id<0,4,0>.
185 set_condmod(BRW_CONDITIONAL_Z
,
186 emit(TCS_OPCODE_SRC0_010_IS_ZERO
, dst_null_d(),
188 emit(IF(BRW_PREDICATE_NORMAL
));
189 for (unsigned i
= 0; i
< key
->input_vertices
; i
+= 2) {
190 /* If we have an odd number of input vertices, the last will be
191 * unpaired. We don't want to use an interleaved URB write in
194 const bool is_unpaired
= i
== key
->input_vertices
- 1;
196 dst_reg
header(this, glsl_type::uvec4_type
);
197 emit(TCS_OPCODE_RELEASE_INPUT
, header
, brw_imm_ud(i
),
198 brw_imm_ud(is_unpaired
));
200 emit(BRW_OPCODE_ENDIF
);
203 if (unlikely(INTEL_DEBUG
& DEBUG_SHADER_TIME
))
204 emit_shader_time_end();
206 inst
= emit(TCS_OPCODE_THREAD_END
);
213 vec4_tcs_visitor::emit_input_urb_read(const dst_reg
&dst
,
214 const src_reg
&vertex_index
,
215 unsigned base_offset
,
216 const src_reg
&indirect_offset
)
218 vec4_instruction
*inst
;
219 dst_reg
temp(this, glsl_type::ivec4_type
);
220 temp
.type
= dst
.type
;
222 /* Set up the message header to reference the proper parts of the URB */
223 dst_reg header
= dst_reg(this, glsl_type::uvec4_type
);
224 inst
= emit(TCS_OPCODE_SET_INPUT_URB_OFFSETS
, header
, vertex_index
,
226 inst
->force_writemask_all
= true;
228 /* Read into a temporary, ignoring writemasking. */
229 inst
= emit(VEC4_OPCODE_URB_READ
, temp
, src_reg(header
));
230 inst
->offset
= base_offset
;
234 /* Copy the temporary to the destination to deal with writemasking.
236 * Also attempt to deal with gl_PointSize being in the .w component.
238 if (inst
->offset
== 0 && indirect_offset
.file
== BAD_FILE
) {
239 emit(MOV(dst
, swizzle(src_reg(temp
), BRW_SWIZZLE_WWWW
)));
241 emit(MOV(dst
, src_reg(temp
)));
246 vec4_tcs_visitor::emit_output_urb_read(const dst_reg
&dst
,
247 unsigned base_offset
,
248 const src_reg
&indirect_offset
)
250 vec4_instruction
*inst
;
252 /* Set up the message header to reference the proper parts of the URB */
253 dst_reg header
= dst_reg(this, glsl_type::uvec4_type
);
254 inst
= emit(TCS_OPCODE_SET_OUTPUT_URB_OFFSETS
, header
,
255 brw_imm_ud(dst
.writemask
), indirect_offset
);
256 inst
->force_writemask_all
= true;
258 /* Read into a temporary, ignoring writemasking. */
259 vec4_instruction
*read
= emit(VEC4_OPCODE_URB_READ
, dst
, src_reg(header
));
260 read
->offset
= base_offset
;
266 vec4_tcs_visitor::emit_urb_write(const src_reg
&value
,
268 unsigned base_offset
,
269 const src_reg
&indirect_offset
)
274 src_reg
message(this, glsl_type::uvec4_type
, 2);
275 vec4_instruction
*inst
;
277 inst
= emit(TCS_OPCODE_SET_OUTPUT_URB_OFFSETS
, dst_reg(message
),
278 brw_imm_ud(writemask
), indirect_offset
);
279 inst
->force_writemask_all
= true;
280 inst
= emit(MOV(offset(dst_reg(retype(message
, value
.type
)), 1), value
));
281 inst
->force_writemask_all
= true;
283 inst
= emit(TCS_OPCODE_URB_WRITE
, dst_null_f(), message
);
284 inst
->offset
= base_offset
;
290 vec4_tcs_visitor::nir_emit_intrinsic(nir_intrinsic_instr
*instr
)
292 switch (instr
->intrinsic
) {
293 case nir_intrinsic_load_invocation_id
:
294 emit(MOV(get_nir_dest(instr
->dest
, BRW_REGISTER_TYPE_UD
),
297 case nir_intrinsic_load_primitive_id
:
298 emit(TCS_OPCODE_GET_PRIMITIVE_ID
,
299 get_nir_dest(instr
->dest
, BRW_REGISTER_TYPE_UD
));
301 case nir_intrinsic_load_patch_vertices_in
:
302 emit(MOV(get_nir_dest(instr
->dest
, BRW_REGISTER_TYPE_D
),
303 brw_imm_d(key
->input_vertices
)));
305 case nir_intrinsic_load_per_vertex_input
: {
306 src_reg indirect_offset
= get_indirect_offset(instr
);
307 unsigned imm_offset
= instr
->const_index
[0];
309 nir_const_value
*vertex_const
= nir_src_as_const_value(instr
->src
[0]);
310 src_reg vertex_index
=
311 vertex_const
? src_reg(brw_imm_ud(vertex_const
->u32
[0]))
312 : get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_UD
, 1);
314 dst_reg dst
= get_nir_dest(instr
->dest
, BRW_REGISTER_TYPE_D
);
315 dst
.writemask
= brw_writemask_for_size(instr
->num_components
);
317 emit_input_urb_read(dst
, vertex_index
, imm_offset
, indirect_offset
);
320 case nir_intrinsic_load_input
:
321 unreachable("nir_lower_io should use load_per_vertex_input intrinsics");
323 case nir_intrinsic_load_output
:
324 case nir_intrinsic_load_per_vertex_output
: {
325 src_reg indirect_offset
= get_indirect_offset(instr
);
326 unsigned imm_offset
= instr
->const_index
[0];
328 dst_reg dst
= get_nir_dest(instr
->dest
, BRW_REGISTER_TYPE_D
);
329 dst
.writemask
= brw_writemask_for_size(instr
->num_components
);
331 if (imm_offset
== 0 && indirect_offset
.file
== BAD_FILE
) {
332 dst
.type
= BRW_REGISTER_TYPE_F
;
334 /* This is a read of gl_TessLevelInner[], which lives in the
335 * Patch URB header. The layout depends on the domain.
337 switch (key
->tes_primitive_mode
) {
339 /* DWords 3-2 (reversed); use offset 0 and WZYX swizzle. */
340 dst_reg
tmp(this, glsl_type::vec4_type
);
341 emit_output_urb_read(tmp
, 0, src_reg());
342 emit(MOV(writemask(dst
, WRITEMASK_XY
),
343 swizzle(src_reg(tmp
), BRW_SWIZZLE_WZYX
)));
347 /* DWord 4; use offset 1 but normal swizzle/writemask. */
348 emit_output_urb_read(writemask(dst
, WRITEMASK_X
), 1, src_reg());
351 /* All channels are undefined. */
354 unreachable("Bogus tessellation domain");
356 } else if (imm_offset
== 1 && indirect_offset
.file
== BAD_FILE
) {
357 dst
.type
= BRW_REGISTER_TYPE_F
;
358 unsigned swiz
= BRW_SWIZZLE_WZYX
;
360 /* This is a read of gl_TessLevelOuter[], which lives in the
361 * high 4 DWords of the Patch URB header, in reverse order.
363 switch (key
->tes_primitive_mode
) {
365 dst
.writemask
= WRITEMASK_XYZW
;
368 dst
.writemask
= WRITEMASK_XYZ
;
371 /* Isolines are not reversed; swizzle .zw -> .xy */
372 swiz
= BRW_SWIZZLE_ZWZW
;
373 dst
.writemask
= WRITEMASK_XY
;
376 unreachable("Bogus tessellation domain");
379 dst_reg
tmp(this, glsl_type::vec4_type
);
380 emit_output_urb_read(tmp
, 1, src_reg());
381 emit(MOV(dst
, swizzle(src_reg(tmp
), swiz
)));
383 emit_output_urb_read(dst
, imm_offset
, indirect_offset
);
387 case nir_intrinsic_store_output
:
388 case nir_intrinsic_store_per_vertex_output
: {
389 src_reg value
= get_nir_src(instr
->src
[0]);
390 unsigned mask
= instr
->const_index
[1];
391 unsigned swiz
= BRW_SWIZZLE_XYZW
;
393 src_reg indirect_offset
= get_indirect_offset(instr
);
394 unsigned imm_offset
= instr
->const_index
[0];
396 if (indirect_offset
.file
== BAD_FILE
) {
397 if (imm_offset
== 0) {
398 value
.type
= BRW_REGISTER_TYPE_F
;
401 (1 << tesslevel_inner_components(key
->tes_primitive_mode
)) - 1;
403 /* This is a write to gl_TessLevelInner[], which lives in the
404 * Patch URB header. The layout depends on the domain.
406 switch (key
->tes_primitive_mode
) {
408 /* gl_TessLevelInner[].xy lives at DWords 3-2 (reversed).
409 * We use an XXYX swizzle to reverse put .xy in the .wz
410 * channels, and use a .zw writemask.
412 swiz
= BRW_SWIZZLE4(0, 0, 1, 0);
413 mask
= writemask_for_backwards_vector(mask
);
416 /* gl_TessLevelInner[].x lives at DWord 4, so we set the
417 * writemask to X and bump the URB offset by 1.
422 /* Skip; gl_TessLevelInner[] doesn't exist for isolines. */
425 unreachable("Bogus tessellation domain");
427 } else if (imm_offset
== 1) {
428 value
.type
= BRW_REGISTER_TYPE_F
;
431 (1 << tesslevel_outer_components(key
->tes_primitive_mode
)) - 1;
433 /* This is a write to gl_TessLevelOuter[] which lives in the
434 * Patch URB Header at DWords 4-7. However, it's reversed, so
435 * instead of .xyzw we have .wzyx.
437 if (key
->tes_primitive_mode
== GL_ISOLINES
) {
438 /* Isolines .xy should be stored in .zw, in order. */
439 swiz
= BRW_SWIZZLE4(0, 0, 0, 1);
442 /* Other domains are reversed; store .wzyx instead of .xyzw. */
443 swiz
= BRW_SWIZZLE_WZYX
;
444 mask
= writemask_for_backwards_vector(mask
);
449 emit_urb_write(swizzle(value
, swiz
), mask
,
450 imm_offset
, indirect_offset
);
454 case nir_intrinsic_barrier
: {
455 dst_reg header
= dst_reg(this, glsl_type::uvec4_type
);
456 emit(TCS_OPCODE_CREATE_BARRIER_HEADER
, header
);
457 emit(SHADER_OPCODE_BARRIER
, dst_null_ud(), src_reg(header
));
462 vec4_visitor::nir_emit_intrinsic(instr
);
467 extern "C" const unsigned *
468 brw_compile_tcs(const struct brw_compiler
*compiler
,
471 const struct brw_tcs_prog_key
*key
,
472 struct brw_tcs_prog_data
*prog_data
,
473 const nir_shader
*src_shader
,
474 int shader_time_index
,
475 unsigned *final_assembly_size
,
478 const struct brw_device_info
*devinfo
= compiler
->devinfo
;
479 struct brw_vue_prog_data
*vue_prog_data
= &prog_data
->base
;
480 const bool is_scalar
= compiler
->scalar_stage
[MESA_SHADER_TESS_CTRL
];
482 nir_shader
*nir
= nir_shader_clone(mem_ctx
, src_shader
);
483 nir
->info
.outputs_written
= key
->outputs_written
;
484 nir
->info
.patch_outputs_written
= key
->patch_outputs_written
;
486 struct brw_vue_map input_vue_map
;
487 brw_compute_vue_map(devinfo
, &input_vue_map
,
488 nir
->info
.inputs_read
& ~VARYING_BIT_PRIMITIVE_ID
,
491 brw_compute_tess_vue_map(&vue_prog_data
->vue_map
,
492 nir
->info
.outputs_written
,
493 nir
->info
.patch_outputs_written
);
495 nir
= brw_nir_apply_sampler_key(nir
, devinfo
, &key
->tex
, is_scalar
);
496 brw_nir_lower_vue_inputs(nir
, is_scalar
, &input_vue_map
);
497 brw_nir_lower_tcs_outputs(nir
, &vue_prog_data
->vue_map
);
498 nir
= brw_postprocess_nir(nir
, compiler
->devinfo
, is_scalar
);
500 prog_data
->instances
= DIV_ROUND_UP(nir
->info
.tcs
.vertices_out
, 2);
502 /* Compute URB entry size. The maximum allowed URB entry size is 32k.
503 * That divides up as follows:
505 * 32 bytes for the patch header (tessellation factors)
506 * 480 bytes for per-patch varyings (a varying component is 4 bytes and
507 * gl_MaxTessPatchComponents = 120)
508 * 16384 bytes for per-vertex varyings (a varying component is 4 bytes,
509 * gl_MaxPatchVertices = 32 and
510 * gl_MaxTessControlOutputComponents = 128)
512 * 15808 bytes left for varying packing overhead
514 const int num_per_patch_slots
= vue_prog_data
->vue_map
.num_per_patch_slots
;
515 const int num_per_vertex_slots
= vue_prog_data
->vue_map
.num_per_vertex_slots
;
516 unsigned output_size_bytes
= 0;
517 /* Note that the patch header is counted in num_per_patch_slots. */
518 output_size_bytes
+= num_per_patch_slots
* 16;
519 output_size_bytes
+= nir
->info
.tcs
.vertices_out
* num_per_vertex_slots
* 16;
521 assert(output_size_bytes
>= 1);
522 if (output_size_bytes
> GEN7_MAX_HS_URB_ENTRY_SIZE_BYTES
)
525 /* URB entry sizes are stored as a multiple of 64 bytes. */
526 vue_prog_data
->urb_entry_size
= ALIGN(output_size_bytes
, 64) / 64;
528 /* HS does not use the usual payload pushing from URB to GRFs,
529 * because we don't have enough registers for a full-size payload, and
530 * the hardware is broken on Haswell anyway.
532 vue_prog_data
->urb_read_length
= 0;
534 if (unlikely(INTEL_DEBUG
& DEBUG_TCS
)) {
535 fprintf(stderr
, "TCS Input ");
536 brw_print_vue_map(stderr
, &input_vue_map
);
537 fprintf(stderr
, "TCS Output ");
538 brw_print_vue_map(stderr
, &vue_prog_data
->vue_map
);
541 vec4_tcs_visitor
v(compiler
, log_data
, key
, prog_data
,
542 nir
, mem_ctx
, shader_time_index
, &input_vue_map
);
545 *error_str
= ralloc_strdup(mem_ctx
, v
.fail_msg
);
549 if (unlikely(INTEL_DEBUG
& DEBUG_TCS
))
550 v
.dump_instructions();
552 return brw_vec4_generate_assembly(compiler
, log_data
, mem_ctx
, nir
,
553 &prog_data
->base
, v
.cfg
,
554 final_assembly_size
);
558 } /* namespace brw */