i965: Consolidate BRW_NEW_TESS_{CTRL,EVAL}_PROGRAM flags.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_tes.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_tes.c
26 *
27 * Tessellation evaluation 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_tes_debug_recompile(struct brw_context *brw,
39 struct gl_shader_program *shader_prog,
40 const struct brw_tes_prog_key *key)
41 {
42 struct brw_cache_item *c = NULL;
43 const struct brw_tes_prog_key *old_key = NULL;
44 bool found = false;
45
46 perf_debug("Recompiling tessellation evaluation 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_TES_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 |= brw_debug_recompile_sampler_key(brw, &old_key->tex, &key->tex);
69
70 if (!found) {
71 perf_debug(" Something else\n");
72 }
73 }
74
75 static bool
76 brw_codegen_tes_prog(struct brw_context *brw,
77 struct gl_shader_program *shader_prog,
78 struct brw_tess_eval_program *tep,
79 struct brw_tes_prog_key *key)
80 {
81 const struct brw_compiler *compiler = brw->intelScreen->compiler;
82 const struct brw_device_info *devinfo = brw->intelScreen->devinfo;
83 struct brw_stage_state *stage_state = &brw->tes.base;
84 nir_shader *nir = tep->program.Base.nir;
85 struct brw_tes_prog_data prog_data;
86 bool start_busy = false;
87 double start_time = 0;
88
89 memset(&prog_data, 0, sizeof(prog_data));
90
91 brw_assign_common_binding_table_offsets(MESA_SHADER_TESS_EVAL, devinfo,
92 shader_prog, &tep->program.Base,
93 &prog_data.base.base, 0);
94
95 switch (tep->program.Spacing) {
96 case GL_EQUAL:
97 prog_data.partitioning = BRW_TESS_PARTITIONING_INTEGER;
98 break;
99 case GL_FRACTIONAL_ODD:
100 prog_data.partitioning = BRW_TESS_PARTITIONING_ODD_FRACTIONAL;
101 break;
102 case GL_FRACTIONAL_EVEN:
103 prog_data.partitioning = BRW_TESS_PARTITIONING_EVEN_FRACTIONAL;
104 break;
105 default:
106 unreachable("invalid domain shader spacing");
107 }
108
109 switch (tep->program.PrimitiveMode) {
110 case GL_QUADS:
111 prog_data.domain = BRW_TESS_DOMAIN_QUAD;
112 break;
113 case GL_TRIANGLES:
114 prog_data.domain = BRW_TESS_DOMAIN_TRI;
115 break;
116 case GL_ISOLINES:
117 prog_data.domain = BRW_TESS_DOMAIN_ISOLINE;
118 break;
119 default:
120 unreachable("invalid domain shader primitive mode");
121 }
122
123 if (tep->program.PointMode) {
124 prog_data.output_topology = BRW_TESS_OUTPUT_TOPOLOGY_POINT;
125 } else if (tep->program.PrimitiveMode == GL_ISOLINES) {
126 prog_data.output_topology = BRW_TESS_OUTPUT_TOPOLOGY_LINE;
127 } else {
128 /* Hardware winding order is backwards from OpenGL */
129 switch (tep->program.VertexOrder) {
130 case GL_CCW:
131 prog_data.output_topology = BRW_TESS_OUTPUT_TOPOLOGY_TRI_CW;
132 break;
133 case GL_CW:
134 prog_data.output_topology = BRW_TESS_OUTPUT_TOPOLOGY_TRI_CCW;
135 break;
136 default:
137 unreachable("invalid domain shader vertex order");
138 }
139 }
140
141 /* Allocate the references to the uniforms that will end up in the
142 * prog_data associated with the compiled program, and which will be freed
143 * by the state cache.
144 *
145 * Note: param_count needs to be num_uniform_components * 4, since we add
146 * padding around uniform values below vec4 size, so the worst case is that
147 * every uniform is a float which gets padded to the size of a vec4.
148 */
149 struct gl_shader *tes = shader_prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
150 int param_count = nir->num_uniforms;
151 if (!compiler->scalar_stage[MESA_SHADER_TESS_EVAL])
152 param_count *= 4;
153
154 prog_data.base.base.param =
155 rzalloc_array(NULL, const gl_constant_value *, param_count);
156 prog_data.base.base.pull_param =
157 rzalloc_array(NULL, const gl_constant_value *, param_count);
158 prog_data.base.base.image_param =
159 rzalloc_array(NULL, struct brw_image_param, tes->NumImages);
160 prog_data.base.base.nr_params = param_count;
161 prog_data.base.base.nr_image_params = tes->NumImages;
162
163 brw_nir_setup_glsl_uniforms(nir, shader_prog, &tep->program.Base,
164 &prog_data.base.base,
165 compiler->scalar_stage[MESA_SHADER_TESS_EVAL]);
166
167 if (unlikely(INTEL_DEBUG & DEBUG_TES))
168 brw_dump_ir("tessellation evaluation", shader_prog, tes, NULL);
169
170 int st_index = -1;
171 if (unlikely(INTEL_DEBUG & DEBUG_SHADER_TIME))
172 st_index = brw_get_shader_time_index(brw, shader_prog, NULL, ST_TES);
173
174 if (unlikely(brw->perf_debug)) {
175 start_busy = brw->batch.last_bo && drm_intel_bo_busy(brw->batch.last_bo);
176 start_time = get_time();
177 }
178
179 void *mem_ctx = ralloc_context(NULL);
180 unsigned program_size;
181 char *error_str;
182 const unsigned *program =
183 brw_compile_tes(compiler, brw, mem_ctx, key, &prog_data, nir,
184 shader_prog, st_index, &program_size, &error_str);
185 if (program == NULL) {
186 if (shader_prog) {
187 shader_prog->LinkStatus = false;
188 ralloc_strcat(&shader_prog->InfoLog, error_str);
189 }
190
191 _mesa_problem(NULL, "Failed to compile tessellation evaluation shader: "
192 "%s\n", error_str);
193
194 ralloc_free(mem_ctx);
195 return false;
196 }
197
198 if (unlikely(brw->perf_debug)) {
199 struct brw_shader *btes = (struct brw_shader *) tes;
200 if (btes->compiled_once) {
201 brw_tes_debug_recompile(brw, shader_prog, key);
202 }
203 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
204 perf_debug("TES compile took %.03f ms and stalled the GPU\n",
205 (get_time() - start_time) * 1000);
206 }
207 btes->compiled_once = true;
208 }
209
210 /* Scratch space is used for register spilling */
211 if (prog_data.base.base.total_scratch) {
212 brw_get_scratch_bo(brw, &stage_state->scratch_bo,
213 prog_data.base.base.total_scratch *
214 brw->max_ds_threads);
215 }
216
217 brw_upload_cache(&brw->cache, BRW_CACHE_TES_PROG,
218 key, sizeof(*key),
219 program, program_size,
220 &prog_data, sizeof(prog_data),
221 &stage_state->prog_offset, &brw->tes.prog_data);
222 ralloc_free(mem_ctx);
223
224 return true;
225 }
226
227
228 void
229 brw_upload_tes_prog(struct brw_context *brw)
230 {
231 struct gl_context *ctx = &brw->ctx;
232 struct gl_shader_program **current = ctx->_Shader->CurrentProgram;
233 struct brw_stage_state *stage_state = &brw->tes.base;
234 struct brw_tes_prog_key key;
235 /* BRW_NEW_TESS_PROGRAMS */
236 struct brw_tess_eval_program *tep =
237 (struct brw_tess_eval_program *) brw->tess_eval_program;
238
239 if (!brw_state_dirty(brw,
240 _NEW_TEXTURE,
241 BRW_NEW_TESS_PROGRAMS))
242 return;
243
244 struct gl_program *prog = &tep->program.Base;
245
246 memset(&key, 0, sizeof(key));
247
248 key.program_string_id = tep->id;
249
250 /* _NEW_TEXTURE */
251 brw_populate_sampler_prog_key_data(ctx, prog, stage_state->sampler_count,
252 &key.tex);
253
254 if (!brw_search_cache(&brw->cache, BRW_CACHE_TES_PROG,
255 &key, sizeof(key),
256 &stage_state->prog_offset, &brw->tes.prog_data)) {
257 bool success = brw_codegen_tes_prog(brw, current[MESA_SHADER_TESS_EVAL],
258 tep, &key);
259 assert(success);
260 (void)success;
261 }
262 brw->tes.base.prog_data = &brw->tes.prog_data->base.base;
263 }
264
265
266 bool
267 brw_tes_precompile(struct gl_context *ctx,
268 struct gl_shader_program *shader_prog,
269 struct gl_program *prog)
270 {
271 struct brw_context *brw = brw_context(ctx);
272 struct brw_tes_prog_key key;
273 uint32_t old_prog_offset = brw->tes.base.prog_offset;
274 struct brw_tes_prog_data *old_prog_data = brw->tes.prog_data;
275 bool success;
276
277 struct gl_tess_eval_program *tep = (struct gl_tess_eval_program *)prog;
278 struct brw_tess_eval_program *btep = brw_tess_eval_program(tep);
279
280 memset(&key, 0, sizeof(key));
281
282 key.program_string_id = btep->id;
283 brw_setup_tex_for_precompile(brw, &key.tex, prog);
284
285 success = brw_codegen_tes_prog(brw, shader_prog, btep, &key);
286
287 brw->tes.base.prog_offset = old_prog_offset;
288 brw->tes.prog_data = old_prog_data;
289
290 return success;
291 }