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"
38 brw_tcs_debug_recompile(struct brw_context
*brw
,
39 struct gl_shader_program
*shader_prog
,
40 const struct brw_tcs_prog_key
*key
)
42 struct brw_cache_item
*c
= NULL
;
43 const struct brw_tcs_prog_key
*old_key
= NULL
;
46 perf_debug("Recompiling tessellation control 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_TCS_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
|= key_debug(brw
, "input vertices", old_key
->input_vertices
,
70 found
|= key_debug(brw
, "TES primitive mode", old_key
->tes_primitive_mode
,
71 key
->tes_primitive_mode
);
72 found
|= brw_debug_recompile_sampler_key(brw
, &old_key
->tex
, &key
->tex
);
75 perf_debug(" Something else\n");
80 brw_codegen_tcs_prog(struct brw_context
*brw
,
81 struct gl_shader_program
*shader_prog
,
82 struct brw_tess_ctrl_program
*tcp
,
83 struct brw_tcs_prog_key
*key
)
85 const struct brw_compiler
*compiler
= brw
->intelScreen
->compiler
;
86 struct brw_stage_state
*stage_state
= &brw
->tcs
.base
;
87 nir_shader
*nir
= tcp
->program
.Base
.nir
;
88 struct brw_tcs_prog_data prog_data
;
89 bool start_busy
= false;
90 double start_time
= 0;
92 memset(&prog_data
, 0, sizeof(prog_data
));
94 /* Allocate the references to the uniforms that will end up in the
95 * prog_data associated with the compiled program, and which will be freed
98 * Note: param_count needs to be num_uniform_components * 4, since we add
99 * padding around uniform values below vec4 size, so the worst case is that
100 * every uniform is a float which gets padded to the size of a vec4.
102 struct gl_shader
*tcs
= shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
];
103 int param_count
= nir
->num_uniforms
;
104 if (!compiler
->scalar_stage
[MESA_SHADER_TESS_CTRL
])
107 prog_data
.base
.base
.param
=
108 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
109 prog_data
.base
.base
.pull_param
=
110 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
111 prog_data
.base
.base
.image_param
=
112 rzalloc_array(NULL
, struct brw_image_param
, tcs
->NumImages
);
113 prog_data
.base
.base
.nr_params
= param_count
;
114 prog_data
.base
.base
.nr_image_params
= tcs
->NumImages
;
116 brw_nir_setup_glsl_uniforms(nir
, shader_prog
, &tcp
->program
.Base
,
117 &prog_data
.base
.base
, false);
119 if (unlikely(INTEL_DEBUG
& DEBUG_TCS
))
120 brw_dump_ir("tessellation control", shader_prog
, tcs
, NULL
);
123 if (unlikely(INTEL_DEBUG
& DEBUG_SHADER_TIME
))
124 st_index
= brw_get_shader_time_index(brw
, shader_prog
, NULL
, ST_TCS
);
126 if (unlikely(brw
->perf_debug
)) {
127 start_busy
= brw
->batch
.last_bo
&& drm_intel_bo_busy(brw
->batch
.last_bo
);
128 start_time
= get_time();
131 void *mem_ctx
= ralloc_context(NULL
);
132 unsigned program_size
;
134 const unsigned *program
=
135 brw_compile_tcs(compiler
, brw
, mem_ctx
, key
, &prog_data
, nir
, st_index
,
136 &program_size
, &error_str
);
137 if (program
== NULL
) {
139 shader_prog
->LinkStatus
= false;
140 ralloc_strcat(&shader_prog
->InfoLog
, error_str
);
143 _mesa_problem(NULL
, "Failed to compile tessellation control shader: "
146 ralloc_free(mem_ctx
);
150 if (unlikely(brw
->perf_debug
)) {
151 struct brw_shader
*btcs
= (struct brw_shader
*) tcs
;
152 if (btcs
->compiled_once
) {
153 brw_tcs_debug_recompile(brw
, shader_prog
, key
);
155 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
156 perf_debug("TCS compile took %.03f ms and stalled the GPU\n",
157 (get_time() - start_time
) * 1000);
159 btcs
->compiled_once
= true;
162 /* Scratch space is used for register spilling */
163 if (prog_data
.base
.base
.total_scratch
) {
164 brw_get_scratch_bo(brw
, &stage_state
->scratch_bo
,
165 prog_data
.base
.base
.total_scratch
*
166 brw
->max_hs_threads
);
169 brw_upload_cache(&brw
->cache
, BRW_CACHE_TCS_PROG
,
171 program
, program_size
,
172 &prog_data
, sizeof(prog_data
),
173 &stage_state
->prog_offset
, &brw
->tcs
.prog_data
);
174 ralloc_free(mem_ctx
);
181 brw_upload_tcs_prog(struct brw_context
*brw
)
183 struct gl_context
*ctx
= &brw
->ctx
;
184 struct gl_shader_program
**current
= ctx
->_Shader
->CurrentProgram
;
185 struct brw_stage_state
*stage_state
= &brw
->tcs
.base
;
186 struct brw_tcs_prog_key key
;
187 /* BRW_NEW_TESS_CTRL_PROGRAM */
188 struct brw_tess_ctrl_program
*tcp
=
189 (struct brw_tess_ctrl_program
*) brw
->tess_ctrl_program
;
191 if (!brw_state_dirty(brw
,
193 BRW_NEW_PATCH_PRIMITIVE
|
194 BRW_NEW_TESS_CTRL_PROGRAM
|
195 BRW_NEW_TESS_EVAL_PROGRAM
))
199 /* Other state atoms had better not try to access prog_data, since
200 * there's no HS program.
202 brw
->tcs
.prog_data
= NULL
;
203 brw
->tcs
.base
.prog_data
= NULL
;
207 struct gl_program
*prog
= &tcp
->program
.Base
;
209 memset(&key
, 0, sizeof(key
));
211 key
.program_string_id
= tcp
->id
;
213 key
.input_vertices
= ctx
->TessCtrlProgram
.patch_vertices
;
216 brw_populate_sampler_prog_key_data(ctx
, prog
, stage_state
->sampler_count
,
219 /* BRW_NEW_TESS_EVAL_PROGRAM */
220 /* We need to specialize our code generation for tessellation levels
221 * based on the domain the DS is expecting to tessellate.
223 struct brw_tess_eval_program
*tep
=
224 (struct brw_tess_eval_program
*) brw
->tess_eval_program
;
226 key
.tes_primitive_mode
= tep
->program
.PrimitiveMode
;
228 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_TCS_PROG
,
230 &stage_state
->prog_offset
, &brw
->tcs
.prog_data
)) {
231 bool success
= brw_codegen_tcs_prog(brw
, current
[MESA_SHADER_TESS_CTRL
],
236 brw
->tcs
.base
.prog_data
= &brw
->tcs
.prog_data
->base
.base
;
241 brw_tcs_precompile(struct gl_context
*ctx
,
242 struct gl_shader_program
*shader_prog
,
243 struct gl_program
*prog
)
245 struct brw_context
*brw
= brw_context(ctx
);
246 struct brw_tcs_prog_key key
;
247 uint32_t old_prog_offset
= brw
->tcs
.base
.prog_offset
;
248 struct brw_tcs_prog_data
*old_prog_data
= brw
->tcs
.prog_data
;
251 struct gl_tess_ctrl_program
*tcp
= (struct gl_tess_ctrl_program
*)prog
;
252 struct brw_tess_ctrl_program
*btcp
= brw_tess_ctrl_program(tcp
);
254 memset(&key
, 0, sizeof(key
));
256 key
.program_string_id
= btcp
->id
;
257 brw_setup_tex_for_precompile(brw
, &key
.tex
, prog
);
259 /* Guess that the input and output patches have the same dimensionality. */
260 key
.input_vertices
= shader_prog
->TessCtrl
.VerticesOut
;
262 key
.tes_primitive_mode
= GL_TRIANGLES
;
264 success
= brw_codegen_tcs_prog(brw
, shader_prog
, btcp
, &key
);
266 brw
->tcs
.base
.prog_offset
= old_prog_offset
;
267 brw
->tcs
.prog_data
= old_prog_data
;