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"
31 #include "compiler/brw_nir.h"
32 #include "brw_program.h"
33 #include "brw_state.h"
34 #include "program/prog_parameter.h"
35 #include "nir_builder.h"
38 brw_tcs_debug_recompile(struct brw_context
*brw
, struct gl_program
*prog
,
39 const struct brw_tcs_prog_key
*key
)
41 perf_debug("Recompiling tessellation control shader for program %d\n",
45 const struct brw_tcs_prog_key
*old_key
=
46 brw_find_previous_compile(&brw
->cache
, BRW_CACHE_TCS_PROG
,
47 key
->program_string_id
);
50 perf_debug(" Didn't find previous compile in the shader cache for "
55 found
|= key_debug(brw
, "input vertices", old_key
->input_vertices
,
57 found
|= key_debug(brw
, "outputs written", old_key
->outputs_written
,
58 key
->outputs_written
);
59 found
|= key_debug(brw
, "patch outputs written", old_key
->patch_outputs_written
,
60 key
->patch_outputs_written
);
61 found
|= key_debug(brw
, "TES primitive mode", old_key
->tes_primitive_mode
,
62 key
->tes_primitive_mode
);
63 found
|= key_debug(brw
, "quads and equal_spacing workaround",
64 old_key
->quads_workaround
, key
->quads_workaround
);
65 found
|= brw_debug_recompile_sampler_key(brw
, &old_key
->tex
, &key
->tex
);
68 perf_debug(" Something else\n");
73 brw_codegen_tcs_prog(struct brw_context
*brw
, struct brw_program
*tcp
,
74 struct brw_program
*tep
, struct brw_tcs_prog_key
*key
)
76 struct gl_context
*ctx
= &brw
->ctx
;
77 const struct brw_compiler
*compiler
= brw
->screen
->compiler
;
78 const struct gen_device_info
*devinfo
= compiler
->devinfo
;
79 struct brw_stage_state
*stage_state
= &brw
->tcs
.base
;
81 struct brw_tcs_prog_data prog_data
;
82 bool start_busy
= false;
83 double start_time
= 0;
85 void *mem_ctx
= ralloc_context(NULL
);
87 nir
= tcp
->program
.nir
;
89 const nir_shader_compiler_options
*options
=
90 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_TESS_CTRL
].NirOptions
;
91 nir
= brw_nir_create_passthrough_tcs(mem_ctx
, compiler
, options
, key
);
94 memset(&prog_data
, 0, sizeof(prog_data
));
97 brw_assign_common_binding_table_offsets(devinfo
, &tcp
->program
,
98 &prog_data
.base
.base
, 0);
100 brw_nir_setup_glsl_uniforms(mem_ctx
, nir
, &tcp
->program
,
101 &prog_data
.base
.base
,
102 compiler
->scalar_stage
[MESA_SHADER_TESS_CTRL
]);
103 brw_nir_analyze_ubo_ranges(compiler
, tcp
->program
.nir
, NULL
,
104 prog_data
.base
.base
.ubo_ranges
);
106 /* Upload the Patch URB Header as the first two uniforms.
107 * Do the annoying scrambling so the shader doesn't have to.
109 assert(nir
->num_uniforms
== 32);
110 prog_data
.base
.base
.param
= rzalloc_array(mem_ctx
, uint32_t, 8);
111 prog_data
.base
.base
.nr_params
= 8;
113 uint32_t *param
= prog_data
.base
.base
.param
;
114 for (int i
= 0; i
< 8; i
++)
115 param
[i
] = BRW_PARAM_BUILTIN_ZERO
;
117 if (key
->tes_primitive_mode
== GL_QUADS
) {
118 for (int i
= 0; i
< 4; i
++)
119 param
[7 - i
] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X
+ i
;
121 param
[3] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X
;
122 param
[2] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_Y
;
123 } else if (key
->tes_primitive_mode
== GL_TRIANGLES
) {
124 for (int i
= 0; i
< 3; i
++)
125 param
[7 - i
] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X
+ i
;
127 param
[4] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X
;
129 assert(key
->tes_primitive_mode
== GL_ISOLINES
);
130 param
[7] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_Y
;
131 param
[6] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X
;
136 if (unlikely((INTEL_DEBUG
& DEBUG_SHADER_TIME
) && tep
))
137 st_index
= brw_get_shader_time_index(brw
, &tep
->program
, ST_TCS
, true);
139 if (unlikely(brw
->perf_debug
)) {
140 start_busy
= brw
->batch
.last_bo
&& brw_bo_busy(brw
->batch
.last_bo
);
141 start_time
= get_time();
145 const unsigned *program
=
146 brw_compile_tcs(compiler
, brw
, mem_ctx
, key
, &prog_data
, nir
, st_index
,
148 if (program
== NULL
) {
150 tep
->program
.sh
.data
->LinkStatus
= LINKING_FAILURE
;
151 ralloc_strcat(&tep
->program
.sh
.data
->InfoLog
, error_str
);
154 _mesa_problem(NULL
, "Failed to compile tessellation control shader: "
157 ralloc_free(mem_ctx
);
161 if (unlikely(brw
->perf_debug
)) {
163 if (tcp
->compiled_once
) {
164 brw_tcs_debug_recompile(brw
, &tcp
->program
, key
);
166 tcp
->compiled_once
= true;
169 if (start_busy
&& !brw_bo_busy(brw
->batch
.last_bo
)) {
170 perf_debug("TCS compile took %.03f ms and stalled the GPU\n",
171 (get_time() - start_time
) * 1000);
175 /* Scratch space is used for register spilling */
176 brw_alloc_stage_scratch(brw
, stage_state
,
177 prog_data
.base
.base
.total_scratch
);
179 /* The param and pull_param arrays will be freed by the shader cache. */
180 ralloc_steal(NULL
, prog_data
.base
.base
.param
);
181 ralloc_steal(NULL
, prog_data
.base
.base
.pull_param
);
182 brw_upload_cache(&brw
->cache
, BRW_CACHE_TCS_PROG
,
184 program
, prog_data
.base
.base
.program_size
,
185 &prog_data
, sizeof(prog_data
),
186 &stage_state
->prog_offset
, &brw
->tcs
.base
.prog_data
);
187 ralloc_free(mem_ctx
);
193 brw_tcs_populate_key(struct brw_context
*brw
,
194 struct brw_tcs_prog_key
*key
)
196 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
197 struct brw_program
*tcp
=
198 (struct brw_program
*) brw
->programs
[MESA_SHADER_TESS_CTRL
];
199 struct brw_program
*tep
=
200 (struct brw_program
*) brw
->programs
[MESA_SHADER_TESS_EVAL
];
201 struct gl_program
*tes_prog
= &tep
->program
;
203 uint64_t per_vertex_slots
= tes_prog
->info
.inputs_read
;
204 uint32_t per_patch_slots
= tes_prog
->info
.patch_inputs_read
;
206 memset(key
, 0, sizeof(*key
));
209 struct gl_program
*prog
= &tcp
->program
;
210 per_vertex_slots
|= prog
->info
.outputs_written
;
211 per_patch_slots
|= prog
->info
.patch_outputs_written
;
214 if (devinfo
->gen
< 8 || !tcp
)
215 key
->input_vertices
= brw
->ctx
.TessCtrlProgram
.patch_vertices
;
216 key
->outputs_written
= per_vertex_slots
;
217 key
->patch_outputs_written
= per_patch_slots
;
219 /* We need to specialize our code generation for tessellation levels
220 * based on the domain the DS is expecting to tessellate.
222 key
->tes_primitive_mode
= tep
->program
.info
.tess
.primitive_mode
;
223 key
->quads_workaround
= devinfo
->gen
< 9 &&
224 tep
->program
.info
.tess
.primitive_mode
== GL_QUADS
&&
225 tep
->program
.info
.tess
.spacing
== TESS_SPACING_EQUAL
;
228 key
->program_string_id
= tcp
->id
;
231 brw_populate_sampler_prog_key_data(&brw
->ctx
, &tcp
->program
, &key
->tex
);
236 brw_upload_tcs_prog(struct brw_context
*brw
)
238 struct brw_stage_state
*stage_state
= &brw
->tcs
.base
;
239 struct brw_tcs_prog_key key
;
240 /* BRW_NEW_TESS_PROGRAMS */
241 struct brw_program
*tcp
=
242 (struct brw_program
*) brw
->programs
[MESA_SHADER_TESS_CTRL
];
243 MAYBE_UNUSED
struct brw_program
*tep
=
244 (struct brw_program
*) brw
->programs
[MESA_SHADER_TESS_EVAL
];
247 if (!brw_state_dirty(brw
,
249 BRW_NEW_PATCH_PRIMITIVE
|
250 BRW_NEW_TESS_PROGRAMS
))
253 brw_tcs_populate_key(brw
, &key
);
255 if (brw_search_cache(&brw
->cache
, BRW_CACHE_TCS_PROG
, &key
, sizeof(key
),
256 &stage_state
->prog_offset
, &brw
->tcs
.base
.prog_data
,
260 if (brw_disk_cache_upload_program(brw
, MESA_SHADER_TESS_CTRL
))
263 tcp
= (struct brw_program
*) brw
->programs
[MESA_SHADER_TESS_CTRL
];
265 tcp
->id
= key
.program_string_id
;
267 MAYBE_UNUSED
bool success
= brw_codegen_tcs_prog(brw
, tcp
, tep
, &key
);
272 brw_tcs_populate_default_key(const struct gen_device_info
*devinfo
,
273 struct brw_tcs_prog_key
*key
,
274 struct gl_shader_program
*sh_prog
,
275 struct gl_program
*prog
)
277 struct brw_program
*btcp
= brw_program(prog
);
278 const struct gl_linked_shader
*tes
=
279 sh_prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
281 memset(key
, 0, sizeof(*key
));
283 key
->program_string_id
= btcp
->id
;
284 brw_setup_tex_for_precompile(devinfo
, &key
->tex
, prog
);
286 /* Guess that the input and output patches have the same dimensionality. */
287 if (devinfo
->gen
< 8)
288 key
->input_vertices
= prog
->info
.tess
.tcs_vertices_out
;
291 key
->tes_primitive_mode
= tes
->Program
->info
.tess
.primitive_mode
;
292 key
->quads_workaround
= devinfo
->gen
< 9 &&
293 tes
->Program
->info
.tess
.primitive_mode
== GL_QUADS
&&
294 tes
->Program
->info
.tess
.spacing
== TESS_SPACING_EQUAL
;
296 key
->tes_primitive_mode
= GL_TRIANGLES
;
299 key
->outputs_written
= prog
->nir
->info
.outputs_written
;
300 key
->patch_outputs_written
= prog
->nir
->info
.patch_outputs_written
;
304 brw_tcs_precompile(struct gl_context
*ctx
,
305 struct gl_shader_program
*shader_prog
,
306 struct gl_program
*prog
)
308 struct brw_context
*brw
= brw_context(ctx
);
309 struct brw_tcs_prog_key key
;
310 uint32_t old_prog_offset
= brw
->tcs
.base
.prog_offset
;
311 struct brw_stage_prog_data
*old_prog_data
= brw
->tcs
.base
.prog_data
;
314 struct brw_program
*btcp
= brw_program(prog
);
315 const struct gl_linked_shader
*tes
=
316 shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
317 struct brw_program
*btep
= tes
? brw_program(tes
->Program
) : NULL
;
319 brw_tcs_populate_default_key(&brw
->screen
->devinfo
, &key
, shader_prog
, prog
);
321 success
= brw_codegen_tcs_prog(brw
, btcp
, btep
, &key
);
323 brw
->tcs
.base
.prog_offset
= old_prog_offset
;
324 brw
->tcs
.base
.prog_data
= old_prog_data
;