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"
31 #include "compiler/brw_nir.h"
32 #include "brw_program.h"
33 #include "brw_state.h"
34 #include "program/prog_parameter.h"
37 brw_codegen_tes_prog(struct brw_context
*brw
,
38 struct brw_program
*tep
,
39 struct brw_tes_prog_key
*key
)
41 const struct brw_compiler
*compiler
= brw
->screen
->compiler
;
42 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
43 struct brw_stage_state
*stage_state
= &brw
->tes
.base
;
44 struct brw_tes_prog_data prog_data
;
45 bool start_busy
= false;
46 double start_time
= 0;
48 memset(&prog_data
, 0, sizeof(prog_data
));
50 void *mem_ctx
= ralloc_context(NULL
);
52 nir_shader
*nir
= nir_shader_clone(mem_ctx
, tep
->program
.nir
);
54 brw_assign_common_binding_table_offsets(devinfo
, &tep
->program
,
55 &prog_data
.base
.base
, 0);
57 brw_nir_setup_glsl_uniforms(mem_ctx
, nir
, &tep
->program
,
59 compiler
->scalar_stage
[MESA_SHADER_TESS_EVAL
]);
60 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
,
61 prog_data
.base
.base
.ubo_ranges
);
64 if (unlikely(INTEL_DEBUG
& DEBUG_SHADER_TIME
))
65 st_index
= brw_get_shader_time_index(brw
, &tep
->program
, ST_TES
, true);
67 if (unlikely(brw
->perf_debug
)) {
68 start_busy
= brw
->batch
.last_bo
&& brw_bo_busy(brw
->batch
.last_bo
);
69 start_time
= get_time();
72 struct brw_vue_map input_vue_map
;
73 brw_compute_tess_vue_map(&input_vue_map
, key
->inputs_read
,
74 key
->patch_inputs_read
);
77 const unsigned *program
=
78 brw_compile_tes(compiler
, brw
, mem_ctx
, key
, &input_vue_map
, &prog_data
,
79 nir
, &tep
->program
, st_index
, &error_str
);
80 if (program
== NULL
) {
81 tep
->program
.sh
.data
->LinkStatus
= LINKING_FAILURE
;
82 ralloc_strcat(&tep
->program
.sh
.data
->InfoLog
, error_str
);
84 _mesa_problem(NULL
, "Failed to compile tessellation evaluation shader: "
91 if (unlikely(brw
->perf_debug
)) {
92 if (tep
->compiled_once
) {
93 brw_debug_recompile(brw
, MESA_SHADER_TESS_EVAL
, tep
->program
.Id
,
94 key
->program_string_id
, key
);
96 if (start_busy
&& !brw_bo_busy(brw
->batch
.last_bo
)) {
97 perf_debug("TES compile took %.03f ms and stalled the GPU\n",
98 (get_time() - start_time
) * 1000);
100 tep
->compiled_once
= true;
103 /* Scratch space is used for register spilling */
104 brw_alloc_stage_scratch(brw
, stage_state
,
105 prog_data
.base
.base
.total_scratch
);
107 /* The param and pull_param arrays will be freed by the shader cache. */
108 ralloc_steal(NULL
, prog_data
.base
.base
.param
);
109 ralloc_steal(NULL
, prog_data
.base
.base
.pull_param
);
110 brw_upload_cache(&brw
->cache
, BRW_CACHE_TES_PROG
,
112 program
, prog_data
.base
.base
.program_size
,
113 &prog_data
, sizeof(prog_data
),
114 &stage_state
->prog_offset
, &brw
->tes
.base
.prog_data
);
115 ralloc_free(mem_ctx
);
121 brw_tes_populate_key(struct brw_context
*brw
,
122 struct brw_tes_prog_key
*key
)
124 struct brw_program
*tcp
=
125 (struct brw_program
*) brw
->programs
[MESA_SHADER_TESS_CTRL
];
126 struct brw_program
*tep
=
127 (struct brw_program
*) brw
->programs
[MESA_SHADER_TESS_EVAL
];
128 struct gl_program
*prog
= &tep
->program
;
130 uint64_t per_vertex_slots
= prog
->info
.inputs_read
;
131 uint32_t per_patch_slots
= prog
->info
.patch_inputs_read
;
133 memset(key
, 0, sizeof(*key
));
135 key
->program_string_id
= tep
->id
;
137 /* The TCS may have additional outputs which aren't read by the
138 * TES (possibly for cross-thread communication). These need to
139 * be stored in the Patch URB Entry as well.
142 struct gl_program
*tcp_prog
= &tcp
->program
;
143 per_vertex_slots
|= tcp_prog
->info
.outputs_written
&
144 ~(VARYING_BIT_TESS_LEVEL_INNER
| VARYING_BIT_TESS_LEVEL_OUTER
);
145 per_patch_slots
|= tcp_prog
->info
.patch_outputs_written
;
148 key
->inputs_read
= per_vertex_slots
;
149 key
->patch_inputs_read
= per_patch_slots
;
152 brw_populate_sampler_prog_key_data(&brw
->ctx
, prog
, &key
->tex
);
156 brw_upload_tes_prog(struct brw_context
*brw
)
158 struct brw_stage_state
*stage_state
= &brw
->tes
.base
;
159 struct brw_tes_prog_key key
;
160 /* BRW_NEW_TESS_PROGRAMS */
161 struct brw_program
*tep
=
162 (struct brw_program
*) brw
->programs
[MESA_SHADER_TESS_EVAL
];
164 if (!brw_state_dirty(brw
,
166 BRW_NEW_TESS_PROGRAMS
))
169 brw_tes_populate_key(brw
, &key
);
171 if (brw_search_cache(&brw
->cache
, BRW_CACHE_TES_PROG
, &key
, sizeof(key
),
172 &stage_state
->prog_offset
, &brw
->tes
.base
.prog_data
,
176 if (brw_disk_cache_upload_program(brw
, MESA_SHADER_TESS_EVAL
))
179 tep
= (struct brw_program
*) brw
->programs
[MESA_SHADER_TESS_EVAL
];
180 tep
->id
= key
.program_string_id
;
182 MAYBE_UNUSED
bool success
= brw_codegen_tes_prog(brw
, tep
, &key
);
187 brw_tes_populate_default_key(const struct brw_compiler
*compiler
,
188 struct brw_tes_prog_key
*key
,
189 struct gl_shader_program
*sh_prog
,
190 struct gl_program
*prog
)
192 const struct gen_device_info
*devinfo
= compiler
->devinfo
;
193 struct brw_program
*btep
= brw_program(prog
);
195 memset(key
, 0, sizeof(*key
));
197 key
->program_string_id
= btep
->id
;
198 key
->inputs_read
= prog
->nir
->info
.inputs_read
;
199 key
->patch_inputs_read
= prog
->nir
->info
.patch_inputs_read
;
201 if (sh_prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]) {
202 struct gl_program
*tcp
=
203 sh_prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]->Program
;
204 key
->inputs_read
|= tcp
->nir
->info
.outputs_written
&
205 ~(VARYING_BIT_TESS_LEVEL_INNER
| VARYING_BIT_TESS_LEVEL_OUTER
);
206 key
->patch_inputs_read
|= tcp
->nir
->info
.patch_outputs_written
;
209 brw_setup_tex_for_precompile(devinfo
, &key
->tex
, prog
);
213 brw_tes_precompile(struct gl_context
*ctx
,
214 struct gl_shader_program
*shader_prog
,
215 struct gl_program
*prog
)
217 struct brw_context
*brw
= brw_context(ctx
);
218 const struct brw_compiler
*compiler
= brw
->screen
->compiler
;
219 struct brw_tes_prog_key key
;
220 uint32_t old_prog_offset
= brw
->tes
.base
.prog_offset
;
221 struct brw_stage_prog_data
*old_prog_data
= brw
->tes
.base
.prog_data
;
224 struct brw_program
*btep
= brw_program(prog
);
226 brw_tes_populate_default_key(compiler
, &key
, shader_prog
, prog
);
228 success
= brw_codegen_tes_prog(brw
, btep
, &key
);
230 brw
->tes
.base
.prog_offset
= old_prog_offset
;
231 brw
->tes
.base
.prog_data
= old_prog_data
;