2 * Copyright © 2014 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.
27 * Tessellation control shader state upload code.
30 #include "brw_context.h"
32 #include "brw_program.h"
33 #include "brw_shader.h"
34 #include "brw_state.h"
35 #include "program/prog_parameter.h"
36 #include "nir_builder.h"
39 create_passthrough_tcs(const struct brw_compiler
*compiler
,
40 const nir_shader_compiler_options
*options
,
41 const struct brw_tcs_prog_key
*key
)
44 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_TESS_CTRL
, options
);
45 nir_shader
*nir
= b
.shader
;
47 nir_intrinsic_instr
*load
;
48 nir_intrinsic_instr
*store
;
49 nir_ssa_def
*zero
= nir_imm_int(&b
, 0);
50 nir_ssa_def
*invoc_id
=
51 nir_load_system_value(&b
, nir_intrinsic_load_invocation_id
, 0);
53 nir
->info
->inputs_read
= key
->outputs_written
;
54 nir
->info
->outputs_written
= key
->outputs_written
;
55 nir
->info
->tcs
.vertices_out
= key
->input_vertices
;
56 nir
->info
->name
= ralloc_strdup(nir
, "passthrough");
57 nir
->num_uniforms
= 8 * sizeof(uint32_t);
59 var
= nir_variable_create(nir
, nir_var_uniform
, glsl_vec4_type(), "hdr_0");
60 var
->data
.location
= 0;
61 var
= nir_variable_create(nir
, nir_var_uniform
, glsl_vec4_type(), "hdr_1");
62 var
->data
.location
= 1;
64 /* Write the patch URB header. */
65 for (int i
= 0; i
<= 1; i
++) {
66 load
= nir_intrinsic_instr_create(nir
, nir_intrinsic_load_uniform
);
67 load
->num_components
= 4;
68 load
->src
[0] = nir_src_for_ssa(zero
);
69 nir_ssa_dest_init(&load
->instr
, &load
->dest
, 4, 32, NULL
);
70 nir_intrinsic_set_base(load
, i
* 4 * sizeof(uint32_t));
71 nir_builder_instr_insert(&b
, &load
->instr
);
73 store
= nir_intrinsic_instr_create(nir
, nir_intrinsic_store_output
);
74 store
->num_components
= 4;
75 store
->src
[0] = nir_src_for_ssa(&load
->dest
.ssa
);
76 store
->src
[1] = nir_src_for_ssa(zero
);
77 nir_intrinsic_set_base(store
, VARYING_SLOT_TESS_LEVEL_INNER
- i
);
78 nir_intrinsic_set_write_mask(store
, WRITEMASK_XYZW
);
79 nir_builder_instr_insert(&b
, &store
->instr
);
82 /* Copy inputs to outputs. */
83 uint64_t varyings
= key
->outputs_written
;
85 while (varyings
!= 0) {
86 const int varying
= ffsll(varyings
) - 1;
88 load
= nir_intrinsic_instr_create(nir
,
89 nir_intrinsic_load_per_vertex_input
);
90 load
->num_components
= 4;
91 load
->src
[0] = nir_src_for_ssa(invoc_id
);
92 load
->src
[1] = nir_src_for_ssa(zero
);
93 nir_ssa_dest_init(&load
->instr
, &load
->dest
, 4, 32, NULL
);
94 nir_intrinsic_set_base(load
, varying
);
95 nir_builder_instr_insert(&b
, &load
->instr
);
97 store
= nir_intrinsic_instr_create(nir
,
98 nir_intrinsic_store_per_vertex_output
);
99 store
->num_components
= 4;
100 store
->src
[0] = nir_src_for_ssa(&load
->dest
.ssa
);
101 store
->src
[1] = nir_src_for_ssa(invoc_id
);
102 store
->src
[2] = nir_src_for_ssa(zero
);
103 nir_intrinsic_set_base(store
, varying
);
104 nir_intrinsic_set_write_mask(store
, WRITEMASK_XYZW
);
105 nir_builder_instr_insert(&b
, &store
->instr
);
107 varyings
&= ~BITFIELD64_BIT(varying
);
110 nir_validate_shader(nir
);
112 nir
= brw_preprocess_nir(compiler
, nir
);
118 brw_tcs_debug_recompile(struct brw_context
*brw
,
119 struct gl_shader_program
*shader_prog
,
120 const struct brw_tcs_prog_key
*key
)
122 struct brw_cache_item
*c
= NULL
;
123 const struct brw_tcs_prog_key
*old_key
= NULL
;
126 perf_debug("Recompiling tessellation control shader for program %d\n",
129 for (unsigned int i
= 0; i
< brw
->cache
.size
; i
++) {
130 for (c
= brw
->cache
.items
[i
]; c
; c
= c
->next
) {
131 if (c
->cache_id
== BRW_CACHE_TCS_PROG
) {
134 if (old_key
->program_string_id
== key
->program_string_id
)
143 perf_debug(" Didn't find previous compile in the shader cache for "
148 found
|= key_debug(brw
, "input vertices", old_key
->input_vertices
,
149 key
->input_vertices
);
150 found
|= key_debug(brw
, "outputs written", old_key
->outputs_written
,
151 key
->outputs_written
);
152 found
|= key_debug(brw
, "patch outputs written", old_key
->patch_outputs_written
,
153 key
->patch_outputs_written
);
154 found
|= key_debug(brw
, "TES primitive mode", old_key
->tes_primitive_mode
,
155 key
->tes_primitive_mode
);
156 found
|= key_debug(brw
, "quads and equal_spacing workaround",
157 old_key
->quads_workaround
, key
->quads_workaround
);
158 found
|= brw_debug_recompile_sampler_key(brw
, &old_key
->tex
, &key
->tex
);
161 perf_debug(" Something else\n");
166 brw_codegen_tcs_prog(struct brw_context
*brw
,
167 struct gl_shader_program
*shader_prog
,
168 struct brw_tess_ctrl_program
*tcp
,
169 struct brw_tcs_prog_key
*key
)
171 struct gl_context
*ctx
= &brw
->ctx
;
172 const struct brw_compiler
*compiler
= brw
->screen
->compiler
;
173 const struct gen_device_info
*devinfo
= compiler
->devinfo
;
174 struct brw_stage_state
*stage_state
= &brw
->tcs
.base
;
176 struct brw_tcs_prog_data prog_data
;
177 bool start_busy
= false;
178 double start_time
= 0;
181 nir
= tcp
->program
.Base
.nir
;
183 /* Create a dummy nir_shader. We won't actually use NIR code to
184 * generate assembly (it's easier to generate assembly directly),
185 * but the whole compiler assumes one of these exists.
187 const nir_shader_compiler_options
*options
=
188 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_TESS_CTRL
].NirOptions
;
189 nir
= create_passthrough_tcs(compiler
, options
, key
);
192 memset(&prog_data
, 0, sizeof(prog_data
));
194 /* Allocate the references to the uniforms that will end up in the
195 * prog_data associated with the compiled program, and which will be freed
196 * by the state cache.
198 * Note: param_count needs to be num_uniform_components * 4, since we add
199 * padding around uniform values below vec4 size, so the worst case is that
200 * every uniform is a float which gets padded to the size of a vec4.
202 struct gl_linked_shader
*tcs
= shader_prog
?
203 shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
] : NULL
;
204 int param_count
= nir
->num_uniforms
/ 4;
206 prog_data
.base
.base
.param
=
207 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
208 prog_data
.base
.base
.pull_param
=
209 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
210 prog_data
.base
.base
.nr_params
= param_count
;
213 brw_assign_common_binding_table_offsets(MESA_SHADER_TESS_CTRL
, devinfo
,
214 shader_prog
, &tcp
->program
.Base
,
215 &prog_data
.base
.base
, 0);
217 prog_data
.base
.base
.image_param
=
218 rzalloc_array(NULL
, struct brw_image_param
, tcs
->NumImages
);
219 prog_data
.base
.base
.nr_image_params
= tcs
->NumImages
;
221 brw_nir_setup_glsl_uniforms(nir
, shader_prog
, &tcp
->program
.Base
,
222 &prog_data
.base
.base
,
223 compiler
->scalar_stage
[MESA_SHADER_TESS_CTRL
]);
225 /* Upload the Patch URB Header as the first two uniforms.
226 * Do the annoying scrambling so the shader doesn't have to.
228 const float **param
= (const float **) prog_data
.base
.base
.param
;
229 static float zero
= 0.0f
;
230 for (int i
= 0; i
< 8; i
++)
233 if (key
->tes_primitive_mode
== GL_QUADS
) {
234 for (int i
= 0; i
< 4; i
++)
235 param
[7 - i
] = &ctx
->TessCtrlProgram
.patch_default_outer_level
[i
];
237 param
[3] = &ctx
->TessCtrlProgram
.patch_default_inner_level
[0];
238 param
[2] = &ctx
->TessCtrlProgram
.patch_default_inner_level
[1];
239 } else if (key
->tes_primitive_mode
== GL_TRIANGLES
) {
240 for (int i
= 0; i
< 3; i
++)
241 param
[7 - i
] = &ctx
->TessCtrlProgram
.patch_default_outer_level
[i
];
243 param
[4] = &ctx
->TessCtrlProgram
.patch_default_inner_level
[0];
245 assert(key
->tes_primitive_mode
== GL_ISOLINES
);
246 param
[7] = &ctx
->TessCtrlProgram
.patch_default_outer_level
[1];
247 param
[6] = &ctx
->TessCtrlProgram
.patch_default_outer_level
[0];
251 if (unlikely(INTEL_DEBUG
& DEBUG_TCS
) && tcs
)
252 brw_dump_ir("tessellation control", shader_prog
, tcs
, NULL
);
255 if (unlikely(INTEL_DEBUG
& DEBUG_SHADER_TIME
))
256 st_index
= brw_get_shader_time_index(brw
, shader_prog
, NULL
, ST_TCS
);
258 if (unlikely(brw
->perf_debug
)) {
259 start_busy
= brw
->batch
.last_bo
&& drm_intel_bo_busy(brw
->batch
.last_bo
);
260 start_time
= get_time();
263 void *mem_ctx
= ralloc_context(NULL
);
264 unsigned program_size
;
266 const unsigned *program
=
267 brw_compile_tcs(compiler
, brw
, mem_ctx
, key
, &prog_data
, nir
, st_index
,
268 &program_size
, &error_str
);
269 if (program
== NULL
) {
271 shader_prog
->LinkStatus
= false;
272 ralloc_strcat(&shader_prog
->InfoLog
, error_str
);
277 _mesa_problem(NULL
, "Failed to compile tessellation control shader: "
280 ralloc_free(mem_ctx
);
284 if (unlikely(brw
->perf_debug
)) {
285 struct brw_shader
*btcs
= (struct brw_shader
*) tcs
;
287 if (btcs
->compiled_once
) {
288 brw_tcs_debug_recompile(brw
, shader_prog
, key
);
290 btcs
->compiled_once
= true;
292 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
293 perf_debug("TCS compile took %.03f ms and stalled the GPU\n",
294 (get_time() - start_time
) * 1000);
298 /* Scratch space is used for register spilling */
299 brw_alloc_stage_scratch(brw
, stage_state
,
300 prog_data
.base
.base
.total_scratch
,
301 devinfo
->max_tcs_threads
);
303 brw_upload_cache(&brw
->cache
, BRW_CACHE_TCS_PROG
,
305 program
, program_size
,
306 &prog_data
, sizeof(prog_data
),
307 &stage_state
->prog_offset
, &brw
->tcs
.base
.prog_data
);
308 ralloc_free(mem_ctx
);
316 brw_tcs_populate_key(struct brw_context
*brw
,
317 struct brw_tcs_prog_key
*key
)
319 struct brw_tess_ctrl_program
*tcp
=
320 (struct brw_tess_ctrl_program
*) brw
->tess_ctrl_program
;
321 struct brw_tess_eval_program
*tep
=
322 (struct brw_tess_eval_program
*) brw
->tess_eval_program
;
323 struct gl_program
*prog
= &tcp
->program
.Base
;
324 struct gl_program
*tes_prog
= &tep
->program
.Base
;
326 uint64_t per_vertex_slots
= tes_prog
->info
.inputs_read
;
327 uint32_t per_patch_slots
= tes_prog
->info
.patch_inputs_read
;
329 memset(key
, 0, sizeof(*key
));
332 per_vertex_slots
|= prog
->info
.outputs_written
;
333 per_patch_slots
|= prog
->info
.patch_outputs_written
;
336 if (brw
->gen
< 8 || !tcp
)
337 key
->input_vertices
= brw
->ctx
.TessCtrlProgram
.patch_vertices
;
338 key
->outputs_written
= per_vertex_slots
;
339 key
->patch_outputs_written
= per_patch_slots
;
341 /* We need to specialize our code generation for tessellation levels
342 * based on the domain the DS is expecting to tessellate.
344 key
->tes_primitive_mode
= tep
->program
.PrimitiveMode
;
345 key
->quads_workaround
= brw
->gen
< 9 &&
346 tep
->program
.PrimitiveMode
== GL_QUADS
&&
347 tep
->program
.Spacing
== GL_EQUAL
;
350 key
->program_string_id
= tcp
->id
;
353 brw_populate_sampler_prog_key_data(&brw
->ctx
, prog
, &key
->tex
);
355 key
->outputs_written
= tes_prog
->info
.inputs_read
;
360 brw_upload_tcs_prog(struct brw_context
*brw
)
362 struct gl_shader_program
**current
= brw
->ctx
._Shader
->CurrentProgram
;
363 struct brw_stage_state
*stage_state
= &brw
->tcs
.base
;
364 struct brw_tcs_prog_key key
;
365 /* BRW_NEW_TESS_PROGRAMS */
366 struct brw_tess_ctrl_program
*tcp
=
367 (struct brw_tess_ctrl_program
*) brw
->tess_ctrl_program
;
368 MAYBE_UNUSED
struct brw_tess_eval_program
*tep
=
369 (struct brw_tess_eval_program
*) brw
->tess_eval_program
;
372 if (!brw_state_dirty(brw
,
374 BRW_NEW_PATCH_PRIMITIVE
|
375 BRW_NEW_TESS_PROGRAMS
))
378 brw_tcs_populate_key(brw
, &key
);
380 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_TCS_PROG
,
382 &stage_state
->prog_offset
,
383 &brw
->tcs
.base
.prog_data
)) {
384 bool success
= brw_codegen_tcs_prog(brw
, current
[MESA_SHADER_TESS_CTRL
],
393 brw_tcs_precompile(struct gl_context
*ctx
,
394 struct gl_shader_program
*shader_prog
,
395 struct gl_program
*prog
)
397 struct brw_context
*brw
= brw_context(ctx
);
398 struct brw_tcs_prog_key key
;
399 uint32_t old_prog_offset
= brw
->tcs
.base
.prog_offset
;
400 struct brw_stage_prog_data
*old_prog_data
= brw
->tcs
.base
.prog_data
;
403 struct gl_tess_ctrl_program
*tcp
= (struct gl_tess_ctrl_program
*)prog
;
404 struct brw_tess_ctrl_program
*btcp
= brw_tess_ctrl_program(tcp
);
405 const struct gl_linked_shader
*tes
=
406 shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
408 memset(&key
, 0, sizeof(key
));
410 key
.program_string_id
= btcp
->id
;
411 brw_setup_tex_for_precompile(brw
, &key
.tex
, prog
);
413 /* Guess that the input and output patches have the same dimensionality. */
415 key
.input_vertices
= shader_prog
->
416 _LinkedShaders
[MESA_SHADER_TESS_CTRL
]->info
.TessCtrl
.VerticesOut
;
420 key
.tes_primitive_mode
= tes
->info
.TessEval
.PrimitiveMode
;
421 key
.quads_workaround
= brw
->gen
< 9 &&
422 tes
->info
.TessEval
.PrimitiveMode
== GL_QUADS
&&
423 tes
->info
.TessEval
.Spacing
== GL_EQUAL
;
425 key
.tes_primitive_mode
= GL_TRIANGLES
;
428 key
.outputs_written
= prog
->nir
->info
->outputs_written
;
429 key
.patch_outputs_written
= prog
->nir
->info
->patch_outputs_written
;
431 success
= brw_codegen_tcs_prog(brw
, shader_prog
, btcp
, &key
);
433 brw
->tcs
.base
.prog_offset
= old_prog_offset
;
434 brw
->tcs
.base
.prog_data
= old_prog_data
;