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 evaluation 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"
38 brw_tes_debug_recompile(struct brw_context
*brw
,
39 struct gl_shader_program
*shader_prog
,
40 const struct brw_tes_prog_key
*key
)
42 struct brw_cache_item
*c
= NULL
;
43 const struct brw_tes_prog_key
*old_key
= NULL
;
46 perf_debug("Recompiling tessellation evaluation shader for program %d\n",
49 for (unsigned int i
= 0; i
< brw
->cache
.size
; i
++) {
50 for (c
= brw
->cache
.items
[i
]; c
; c
= c
->next
) {
51 if (c
->cache_id
== BRW_CACHE_TES_PROG
) {
54 if (old_key
->program_string_id
== key
->program_string_id
)
63 perf_debug(" Didn't find previous compile in the shader cache for "
68 found
|= brw_debug_recompile_sampler_key(brw
, &old_key
->tex
, &key
->tex
);
69 found
|= key_debug(brw
, "inputs read", old_key
->inputs_read
,
71 found
|= key_debug(brw
, "patch inputs read", old_key
->patch_inputs_read
,
72 key
->patch_inputs_read
);
75 perf_debug(" Something else\n");
80 brw_codegen_tes_prog(struct brw_context
*brw
,
81 struct gl_shader_program
*shader_prog
,
82 struct brw_tess_eval_program
*tep
,
83 struct brw_tes_prog_key
*key
)
85 const struct brw_compiler
*compiler
= brw
->intelScreen
->compiler
;
86 const struct brw_device_info
*devinfo
= brw
->intelScreen
->devinfo
;
87 struct brw_stage_state
*stage_state
= &brw
->tes
.base
;
88 nir_shader
*nir
= tep
->program
.Base
.nir
;
89 struct brw_tes_prog_data prog_data
;
90 bool start_busy
= false;
91 double start_time
= 0;
93 memset(&prog_data
, 0, sizeof(prog_data
));
95 brw_assign_common_binding_table_offsets(MESA_SHADER_TESS_EVAL
, devinfo
,
96 shader_prog
, &tep
->program
.Base
,
97 &prog_data
.base
.base
, 0);
99 switch (tep
->program
.Spacing
) {
101 prog_data
.partitioning
= BRW_TESS_PARTITIONING_INTEGER
;
103 case GL_FRACTIONAL_ODD
:
104 prog_data
.partitioning
= BRW_TESS_PARTITIONING_ODD_FRACTIONAL
;
106 case GL_FRACTIONAL_EVEN
:
107 prog_data
.partitioning
= BRW_TESS_PARTITIONING_EVEN_FRACTIONAL
;
110 unreachable("invalid domain shader spacing");
113 switch (tep
->program
.PrimitiveMode
) {
115 prog_data
.domain
= BRW_TESS_DOMAIN_QUAD
;
118 prog_data
.domain
= BRW_TESS_DOMAIN_TRI
;
121 prog_data
.domain
= BRW_TESS_DOMAIN_ISOLINE
;
124 unreachable("invalid domain shader primitive mode");
127 if (tep
->program
.PointMode
) {
128 prog_data
.output_topology
= BRW_TESS_OUTPUT_TOPOLOGY_POINT
;
129 } else if (tep
->program
.PrimitiveMode
== GL_ISOLINES
) {
130 prog_data
.output_topology
= BRW_TESS_OUTPUT_TOPOLOGY_LINE
;
132 /* Hardware winding order is backwards from OpenGL */
133 switch (tep
->program
.VertexOrder
) {
135 prog_data
.output_topology
= BRW_TESS_OUTPUT_TOPOLOGY_TRI_CW
;
138 prog_data
.output_topology
= BRW_TESS_OUTPUT_TOPOLOGY_TRI_CCW
;
141 unreachable("invalid domain shader vertex order");
145 /* Allocate the references to the uniforms that will end up in the
146 * prog_data associated with the compiled program, and which will be freed
147 * by the state cache.
149 * Note: param_count needs to be num_uniform_components * 4, since we add
150 * padding around uniform values below vec4 size, so the worst case is that
151 * every uniform is a float which gets padded to the size of a vec4.
153 struct gl_shader
*tes
= shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
];
154 int param_count
= nir
->num_uniforms
;
155 if (!compiler
->scalar_stage
[MESA_SHADER_TESS_EVAL
])
158 prog_data
.base
.base
.param
=
159 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
160 prog_data
.base
.base
.pull_param
=
161 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
162 prog_data
.base
.base
.image_param
=
163 rzalloc_array(NULL
, struct brw_image_param
, tes
->NumImages
);
164 prog_data
.base
.base
.nr_params
= param_count
;
165 prog_data
.base
.base
.nr_image_params
= tes
->NumImages
;
167 prog_data
.base
.cull_distance_mask
=
168 ((1 << tep
->program
.Base
.CullDistanceArraySize
) - 1) <<
169 tep
->program
.Base
.ClipDistanceArraySize
;
171 brw_nir_setup_glsl_uniforms(nir
, shader_prog
, &tep
->program
.Base
,
172 &prog_data
.base
.base
,
173 compiler
->scalar_stage
[MESA_SHADER_TESS_EVAL
]);
175 if (unlikely(INTEL_DEBUG
& DEBUG_TES
))
176 brw_dump_ir("tessellation evaluation", shader_prog
, tes
, NULL
);
179 if (unlikely(INTEL_DEBUG
& DEBUG_SHADER_TIME
))
180 st_index
= brw_get_shader_time_index(brw
, shader_prog
, NULL
, ST_TES
);
182 if (unlikely(brw
->perf_debug
)) {
183 start_busy
= brw
->batch
.last_bo
&& drm_intel_bo_busy(brw
->batch
.last_bo
);
184 start_time
= get_time();
187 void *mem_ctx
= ralloc_context(NULL
);
188 unsigned program_size
;
190 const unsigned *program
=
191 brw_compile_tes(compiler
, brw
, mem_ctx
, key
, &prog_data
, nir
,
192 shader_prog
, st_index
, &program_size
, &error_str
);
193 if (program
== NULL
) {
195 shader_prog
->LinkStatus
= false;
196 ralloc_strcat(&shader_prog
->InfoLog
, error_str
);
199 _mesa_problem(NULL
, "Failed to compile tessellation evaluation shader: "
202 ralloc_free(mem_ctx
);
206 if (unlikely(brw
->perf_debug
)) {
207 struct brw_shader
*btes
= (struct brw_shader
*) tes
;
208 if (btes
->compiled_once
) {
209 brw_tes_debug_recompile(brw
, shader_prog
, key
);
211 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
212 perf_debug("TES compile took %.03f ms and stalled the GPU\n",
213 (get_time() - start_time
) * 1000);
215 btes
->compiled_once
= true;
218 /* Scratch space is used for register spilling */
219 if (prog_data
.base
.base
.total_scratch
) {
220 brw_get_scratch_bo(brw
, &stage_state
->scratch_bo
,
221 prog_data
.base
.base
.total_scratch
*
222 brw
->max_ds_threads
);
225 brw_upload_cache(&brw
->cache
, BRW_CACHE_TES_PROG
,
227 program
, program_size
,
228 &prog_data
, sizeof(prog_data
),
229 &stage_state
->prog_offset
, &brw
->tes
.prog_data
);
230 ralloc_free(mem_ctx
);
237 brw_upload_tes_prog(struct brw_context
*brw
,
238 uint64_t per_vertex_slots
,
239 uint32_t per_patch_slots
)
241 struct gl_context
*ctx
= &brw
->ctx
;
242 struct gl_shader_program
**current
= ctx
->_Shader
->CurrentProgram
;
243 struct brw_stage_state
*stage_state
= &brw
->tes
.base
;
244 struct brw_tes_prog_key key
;
245 /* BRW_NEW_TESS_PROGRAMS */
246 struct brw_tess_eval_program
*tep
=
247 (struct brw_tess_eval_program
*) brw
->tess_eval_program
;
249 if (!brw_state_dirty(brw
,
251 BRW_NEW_TESS_PROGRAMS
))
254 struct gl_program
*prog
= &tep
->program
.Base
;
256 memset(&key
, 0, sizeof(key
));
258 key
.program_string_id
= tep
->id
;
260 /* Ignore gl_TessLevelInner/Outer - we treat them as system values,
261 * not inputs, and they're always present in the URB entry regardless
262 * of whether or not we read them.
264 key
.inputs_read
= per_vertex_slots
&
265 ~(VARYING_BIT_TESS_LEVEL_INNER
| VARYING_BIT_TESS_LEVEL_OUTER
);
266 key
.patch_inputs_read
= per_patch_slots
;
269 brw_populate_sampler_prog_key_data(ctx
, prog
, stage_state
->sampler_count
,
272 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_TES_PROG
,
274 &stage_state
->prog_offset
, &brw
->tes
.prog_data
)) {
275 bool success
= brw_codegen_tes_prog(brw
, current
[MESA_SHADER_TESS_EVAL
],
280 brw
->tes
.base
.prog_data
= &brw
->tes
.prog_data
->base
.base
;
285 brw_tes_precompile(struct gl_context
*ctx
,
286 struct gl_shader_program
*shader_prog
,
287 struct gl_program
*prog
)
289 struct brw_context
*brw
= brw_context(ctx
);
290 struct brw_tes_prog_key key
;
291 uint32_t old_prog_offset
= brw
->tes
.base
.prog_offset
;
292 struct brw_tes_prog_data
*old_prog_data
= brw
->tes
.prog_data
;
295 struct gl_tess_eval_program
*tep
= (struct gl_tess_eval_program
*)prog
;
296 struct brw_tess_eval_program
*btep
= brw_tess_eval_program(tep
);
298 memset(&key
, 0, sizeof(key
));
300 key
.program_string_id
= btep
->id
;
301 key
.inputs_read
= prog
->InputsRead
;
302 key
.patch_inputs_read
= prog
->PatchInputsRead
;
304 if (shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]) {
305 struct gl_program
*tcp
=
306 shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
;
307 key
.inputs_read
|= tcp
->OutputsWritten
;
308 key
.patch_inputs_read
|= tcp
->PatchOutputsWritten
;
311 /* Ignore gl_TessLevelInner/Outer - they're system values. */
312 key
.inputs_read
&= ~(VARYING_BIT_TESS_LEVEL_INNER
|
313 VARYING_BIT_TESS_LEVEL_OUTER
);
315 brw_setup_tex_for_precompile(brw
, &key
.tex
, prog
);
317 success
= brw_codegen_tes_prog(brw
, shader_prog
, btep
, &key
);
319 brw
->tes
.base
.prog_offset
= old_prog_offset
;
320 brw
->tes
.prog_data
= old_prog_data
;