5de6f4859ad3d8ce3dbb08eb89e6acbf00289f97
[mesa.git] / src / mesa / drivers / dri / i965 / brw_tcs.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file brw_tcs.c
26 *
27 * Tessellation control shader state upload code.
28 */
29
30 #include "brw_context.h"
31 #include "brw_nir.h"
32 #include "brw_program.h"
33 #include "brw_shader.h"
34 #include "brw_state.h"
35 #include "program/prog_parameter.h"
36
37 static void
38 brw_tcs_debug_recompile(struct brw_context *brw,
39 struct gl_shader_program *shader_prog,
40 const struct brw_tcs_prog_key *key)
41 {
42 struct brw_cache_item *c = NULL;
43 const struct brw_tcs_prog_key *old_key = NULL;
44 bool found = false;
45
46 perf_debug("Recompiling tessellation control shader for program %d\n",
47 shader_prog->Name);
48
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) {
52 old_key = c->key;
53
54 if (old_key->program_string_id == key->program_string_id)
55 break;
56 }
57 }
58 if (c)
59 break;
60 }
61
62 if (!c) {
63 perf_debug(" Didn't find previous compile in the shader cache for "
64 "debug\n");
65 return;
66 }
67
68 found |= key_debug(brw, "input vertices", old_key->input_vertices,
69 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);
73
74 if (!found) {
75 perf_debug(" Something else\n");
76 }
77 }
78
79 static bool
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)
84 {
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;
91
92 memset(&prog_data, 0, sizeof(prog_data));
93
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
96 * by the state cache.
97 *
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.
101 */
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])
105 param_count *= 4;
106
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;
115
116 brw_nir_setup_glsl_uniforms(nir, shader_prog, &tcp->program.Base,
117 &prog_data.base.base, false);
118
119 if (unlikely(INTEL_DEBUG & DEBUG_TCS))
120 brw_dump_ir("tessellation control", shader_prog, tcs, NULL);
121
122 int st_index = -1;
123 if (unlikely(INTEL_DEBUG & DEBUG_SHADER_TIME))
124 st_index = brw_get_shader_time_index(brw, shader_prog, NULL, ST_TCS);
125
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();
129 }
130
131 void *mem_ctx = ralloc_context(NULL);
132 unsigned program_size;
133 char *error_str;
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) {
138 if (shader_prog) {
139 shader_prog->LinkStatus = false;
140 ralloc_strcat(&shader_prog->InfoLog, error_str);
141 }
142
143 _mesa_problem(NULL, "Failed to compile tessellation control shader: "
144 "%s\n", error_str);
145
146 ralloc_free(mem_ctx);
147 return false;
148 }
149
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);
154 }
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);
158 }
159 btcs->compiled_once = true;
160 }
161
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);
167 }
168
169 brw_upload_cache(&brw->cache, BRW_CACHE_TCS_PROG,
170 key, sizeof(*key),
171 program, program_size,
172 &prog_data, sizeof(prog_data),
173 &stage_state->prog_offset, &brw->tcs.prog_data);
174 ralloc_free(mem_ctx);
175
176 return true;
177 }
178
179
180 void
181 brw_upload_tcs_prog(struct brw_context *brw)
182 {
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_PROGRAMS */
188 struct brw_tess_ctrl_program *tcp =
189 (struct brw_tess_ctrl_program *) brw->tess_ctrl_program;
190 struct brw_tess_eval_program *tep =
191 (struct brw_tess_eval_program *) brw->tess_eval_program;
192 assert(tep);
193
194 if (!brw_state_dirty(brw,
195 _NEW_TEXTURE,
196 BRW_NEW_PATCH_PRIMITIVE |
197 BRW_NEW_TESS_PROGRAMS))
198 return;
199
200 struct gl_program *prog = &tcp->program.Base;
201
202 memset(&key, 0, sizeof(key));
203
204 key.program_string_id = tcp->id;
205
206 key.input_vertices = ctx->TessCtrlProgram.patch_vertices;
207
208 /* _NEW_TEXTURE */
209 brw_populate_sampler_prog_key_data(ctx, prog, stage_state->sampler_count,
210 &key.tex);
211
212 /* We need to specialize our code generation for tessellation levels
213 * based on the domain the DS is expecting to tessellate.
214 */
215 key.tes_primitive_mode = tep->program.PrimitiveMode;
216
217 if (!brw_search_cache(&brw->cache, BRW_CACHE_TCS_PROG,
218 &key, sizeof(key),
219 &stage_state->prog_offset, &brw->tcs.prog_data)) {
220 bool success = brw_codegen_tcs_prog(brw, current[MESA_SHADER_TESS_CTRL],
221 tcp, &key);
222 assert(success);
223 (void)success;
224 }
225 brw->tcs.base.prog_data = &brw->tcs.prog_data->base.base;
226 }
227
228
229 bool
230 brw_tcs_precompile(struct gl_context *ctx,
231 struct gl_shader_program *shader_prog,
232 struct gl_program *prog)
233 {
234 struct brw_context *brw = brw_context(ctx);
235 struct brw_tcs_prog_key key;
236 uint32_t old_prog_offset = brw->tcs.base.prog_offset;
237 struct brw_tcs_prog_data *old_prog_data = brw->tcs.prog_data;
238 bool success;
239
240 struct gl_tess_ctrl_program *tcp = (struct gl_tess_ctrl_program *)prog;
241 struct brw_tess_ctrl_program *btcp = brw_tess_ctrl_program(tcp);
242
243 memset(&key, 0, sizeof(key));
244
245 key.program_string_id = btcp->id;
246 brw_setup_tex_for_precompile(brw, &key.tex, prog);
247
248 /* Guess that the input and output patches have the same dimensionality. */
249 key.input_vertices = shader_prog->TessCtrl.VerticesOut;
250
251 key.tes_primitive_mode = GL_TRIANGLES;
252
253 success = brw_codegen_tcs_prog(brw, shader_prog, btcp, &key);
254
255 brw->tcs.base.prog_offset = old_prog_offset;
256 brw->tcs.prog_data = old_prog_data;
257
258 return success;
259 }