i965: Move up fs_inst::regs_written to backend_instruction.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_program.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 */
31
32 #include <pthread.h>
33 #include "main/imports.h"
34 #include "main/enums.h"
35 #include "main/shaderobj.h"
36 #include "program/prog_parameter.h"
37 #include "program/prog_print.h"
38 #include "program/program.h"
39 #include "program/programopt.h"
40 #include "tnl/tnl.h"
41 #include "util/ralloc.h"
42 #include "glsl/ir.h"
43
44 #include "brw_context.h"
45 #include "brw_shader.h"
46 #include "brw_wm.h"
47
48 static unsigned
49 get_new_program_id(struct intel_screen *screen)
50 {
51 static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
52 pthread_mutex_lock(&m);
53 unsigned id = screen->program_id++;
54 pthread_mutex_unlock(&m);
55 return id;
56 }
57
58 static struct gl_program *brwNewProgram( struct gl_context *ctx,
59 GLenum target,
60 GLuint id )
61 {
62 struct brw_context *brw = brw_context(ctx);
63
64 switch (target) {
65 case GL_VERTEX_PROGRAM_ARB: {
66 struct brw_vertex_program *prog = CALLOC_STRUCT(brw_vertex_program);
67 if (prog) {
68 prog->id = get_new_program_id(brw->intelScreen);
69
70 return _mesa_init_vertex_program( ctx, &prog->program,
71 target, id );
72 }
73 else
74 return NULL;
75 }
76
77 case GL_FRAGMENT_PROGRAM_ARB: {
78 struct brw_fragment_program *prog = CALLOC_STRUCT(brw_fragment_program);
79 if (prog) {
80 prog->id = get_new_program_id(brw->intelScreen);
81
82 return _mesa_init_fragment_program( ctx, &prog->program,
83 target, id );
84 }
85 else
86 return NULL;
87 }
88
89 case MESA_GEOMETRY_PROGRAM: {
90 struct brw_geometry_program *prog = CALLOC_STRUCT(brw_geometry_program);
91 if (prog) {
92 prog->id = get_new_program_id(brw->intelScreen);
93
94 return _mesa_init_geometry_program(ctx, &prog->program, target, id);
95 } else {
96 return NULL;
97 }
98 }
99
100 case GL_COMPUTE_PROGRAM_NV: {
101 struct brw_compute_program *prog = CALLOC_STRUCT(brw_compute_program);
102 if (prog) {
103 prog->id = get_new_program_id(brw->intelScreen);
104
105 return _mesa_init_compute_program(ctx, &prog->program, target, id);
106 } else {
107 return NULL;
108 }
109 }
110
111 default:
112 unreachable("Unsupported target in brwNewProgram()");
113 }
114 }
115
116 static void brwDeleteProgram( struct gl_context *ctx,
117 struct gl_program *prog )
118 {
119 _mesa_delete_program( ctx, prog );
120 }
121
122
123 static GLboolean
124 brwProgramStringNotify(struct gl_context *ctx,
125 GLenum target,
126 struct gl_program *prog)
127 {
128 struct brw_context *brw = brw_context(ctx);
129
130 switch (target) {
131 case GL_FRAGMENT_PROGRAM_ARB: {
132 struct gl_fragment_program *fprog = (struct gl_fragment_program *) prog;
133 struct brw_fragment_program *newFP = brw_fragment_program(fprog);
134 const struct brw_fragment_program *curFP =
135 brw_fragment_program_const(brw->fragment_program);
136
137 if (newFP == curFP)
138 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
139 newFP->id = get_new_program_id(brw->intelScreen);
140
141 brw_add_texrect_params(prog);
142
143 brw_fs_precompile(ctx, NULL, prog);
144 break;
145 }
146 case GL_VERTEX_PROGRAM_ARB: {
147 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
148 struct brw_vertex_program *newVP = brw_vertex_program(vprog);
149 const struct brw_vertex_program *curVP =
150 brw_vertex_program_const(brw->vertex_program);
151
152 if (newVP == curVP)
153 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
154 if (newVP->program.IsPositionInvariant) {
155 _mesa_insert_mvp_code(ctx, &newVP->program);
156 }
157 newVP->id = get_new_program_id(brw->intelScreen);
158
159 /* Also tell tnl about it:
160 */
161 _tnl_program_string(ctx, target, prog);
162
163 brw_add_texrect_params(prog);
164
165 brw_vs_precompile(ctx, NULL, prog);
166 break;
167 }
168 default:
169 /*
170 * driver->ProgramStringNotify is only called for ARB programs, fixed
171 * function vertex programs, and ir_to_mesa (which isn't used by the
172 * i965 back-end). Therefore, even after geometry shaders are added,
173 * this function should only ever be called with a target of
174 * GL_VERTEX_PROGRAM_ARB or GL_FRAGMENT_PROGRAM_ARB.
175 */
176 unreachable("Unexpected target in brwProgramStringNotify");
177 }
178
179 return true;
180 }
181
182 void
183 brw_add_texrect_params(struct gl_program *prog)
184 {
185 for (int texunit = 0; texunit < BRW_MAX_TEX_UNIT; texunit++) {
186 if (!(prog->TexturesUsed[texunit] & (1 << TEXTURE_RECT_INDEX)))
187 continue;
188
189 int tokens[STATE_LENGTH] = {
190 STATE_INTERNAL,
191 STATE_TEXRECT_SCALE,
192 texunit,
193 0,
194 0
195 };
196
197 _mesa_add_state_reference(prog->Parameters, (gl_state_index *)tokens);
198 }
199 }
200
201 /* Per-thread scratch space is a power-of-two multiple of 1KB. */
202 int
203 brw_get_scratch_size(int size)
204 {
205 int i;
206
207 for (i = 1024; i < size; i *= 2)
208 ;
209
210 return i;
211 }
212
213 void
214 brw_get_scratch_bo(struct brw_context *brw,
215 drm_intel_bo **scratch_bo, int size)
216 {
217 drm_intel_bo *old_bo = *scratch_bo;
218
219 if (old_bo && old_bo->size < size) {
220 drm_intel_bo_unreference(old_bo);
221 old_bo = NULL;
222 }
223
224 if (!old_bo) {
225 *scratch_bo = drm_intel_bo_alloc(brw->bufmgr, "scratch bo", size, 4096);
226 }
227 }
228
229 void brwInitFragProgFuncs( struct dd_function_table *functions )
230 {
231 assert(functions->ProgramStringNotify == _tnl_program_string);
232
233 functions->NewProgram = brwNewProgram;
234 functions->DeleteProgram = brwDeleteProgram;
235 functions->ProgramStringNotify = brwProgramStringNotify;
236
237 functions->NewShader = brw_new_shader;
238 functions->LinkShader = brw_link_shader;
239 }
240
241 void
242 brw_init_shader_time(struct brw_context *brw)
243 {
244 const int max_entries = 4096;
245 brw->shader_time.bo = drm_intel_bo_alloc(brw->bufmgr, "shader time",
246 max_entries * SHADER_TIME_STRIDE,
247 4096);
248 brw->shader_time.shader_programs = rzalloc_array(brw, struct gl_shader_program *,
249 max_entries);
250 brw->shader_time.programs = rzalloc_array(brw, struct gl_program *,
251 max_entries);
252 brw->shader_time.types = rzalloc_array(brw, enum shader_time_shader_type,
253 max_entries);
254 brw->shader_time.cumulative = rzalloc_array(brw, uint64_t,
255 max_entries);
256 brw->shader_time.max_entries = max_entries;
257 }
258
259 static int
260 compare_time(const void *a, const void *b)
261 {
262 uint64_t * const *a_val = a;
263 uint64_t * const *b_val = b;
264
265 /* We don't just subtract because we're turning the value to an int. */
266 if (**a_val < **b_val)
267 return -1;
268 else if (**a_val == **b_val)
269 return 0;
270 else
271 return 1;
272 }
273
274 static void
275 get_written_and_reset(struct brw_context *brw, int i,
276 uint64_t *written, uint64_t *reset)
277 {
278 enum shader_time_shader_type type = brw->shader_time.types[i];
279 assert(type == ST_VS || type == ST_GS || type == ST_FS8 || type == ST_FS16);
280
281 /* Find where we recorded written and reset. */
282 int wi, ri;
283
284 for (wi = i; brw->shader_time.types[wi] != type + 1; wi++)
285 ;
286
287 for (ri = i; brw->shader_time.types[ri] != type + 2; ri++)
288 ;
289
290 *written = brw->shader_time.cumulative[wi];
291 *reset = brw->shader_time.cumulative[ri];
292 }
293
294 static void
295 print_shader_time_line(const char *stage, const char *name,
296 int shader_num, uint64_t time, uint64_t total)
297 {
298 fprintf(stderr, "%-6s%-18s", stage, name);
299
300 if (shader_num != -1)
301 fprintf(stderr, "%4d: ", shader_num);
302 else
303 fprintf(stderr, " : ");
304
305 fprintf(stderr, "%16lld (%7.2f Gcycles) %4.1f%%\n",
306 (long long)time,
307 (double)time / 1000000000.0,
308 (double)time / total * 100.0);
309 }
310
311 static void
312 brw_report_shader_time(struct brw_context *brw)
313 {
314 if (!brw->shader_time.bo || !brw->shader_time.num_entries)
315 return;
316
317 uint64_t scaled[brw->shader_time.num_entries];
318 uint64_t *sorted[brw->shader_time.num_entries];
319 uint64_t total_by_type[ST_FS16 + 1];
320 memset(total_by_type, 0, sizeof(total_by_type));
321 double total = 0;
322 for (int i = 0; i < brw->shader_time.num_entries; i++) {
323 uint64_t written = 0, reset = 0;
324 enum shader_time_shader_type type = brw->shader_time.types[i];
325
326 sorted[i] = &scaled[i];
327
328 switch (type) {
329 case ST_VS_WRITTEN:
330 case ST_VS_RESET:
331 case ST_GS_WRITTEN:
332 case ST_GS_RESET:
333 case ST_FS8_WRITTEN:
334 case ST_FS8_RESET:
335 case ST_FS16_WRITTEN:
336 case ST_FS16_RESET:
337 /* We'll handle these when along with the time. */
338 scaled[i] = 0;
339 continue;
340
341 case ST_VS:
342 case ST_GS:
343 case ST_FS8:
344 case ST_FS16:
345 get_written_and_reset(brw, i, &written, &reset);
346 break;
347
348 default:
349 /* I sometimes want to print things that aren't the 3 shader times.
350 * Just print the sum in that case.
351 */
352 written = 1;
353 reset = 0;
354 break;
355 }
356
357 uint64_t time = brw->shader_time.cumulative[i];
358 if (written) {
359 scaled[i] = time / written * (written + reset);
360 } else {
361 scaled[i] = time;
362 }
363
364 switch (type) {
365 case ST_VS:
366 case ST_GS:
367 case ST_FS8:
368 case ST_FS16:
369 total_by_type[type] += scaled[i];
370 break;
371 default:
372 break;
373 }
374
375 total += scaled[i];
376 }
377
378 if (total == 0) {
379 fprintf(stderr, "No shader time collected yet\n");
380 return;
381 }
382
383 qsort(sorted, brw->shader_time.num_entries, sizeof(sorted[0]), compare_time);
384
385 fprintf(stderr, "\n");
386 fprintf(stderr, "type ID cycles spent %% of total\n");
387 for (int s = 0; s < brw->shader_time.num_entries; s++) {
388 const char *shader_name;
389 const char *stage;
390 /* Work back from the sorted pointers times to a time to print. */
391 int i = sorted[s] - scaled;
392 struct gl_shader_program *prog = brw->shader_time.shader_programs[i];
393
394 if (scaled[i] == 0)
395 continue;
396
397 int shader_num = -1;
398 if (prog) {
399 shader_num = prog->Name;
400
401 /* The fixed function fragment shader generates GLSL IR with a Name
402 * of 0, and nothing else does.
403 */
404 if (prog->Label) {
405 shader_name = prog->Label;
406 } else if (shader_num == 0 &&
407 (brw->shader_time.types[i] == ST_FS8 ||
408 brw->shader_time.types[i] == ST_FS16)) {
409 shader_name = "ff";
410 shader_num = -1;
411 } else {
412 shader_name = "glsl";
413 }
414 } else if (brw->shader_time.programs[i]) {
415 shader_num = brw->shader_time.programs[i]->Id;
416 if (shader_num == 0) {
417 shader_name = "ff";
418 shader_num = -1;
419 } else {
420 shader_name = "prog";
421 }
422 } else {
423 shader_name = "other";
424 }
425
426 switch (brw->shader_time.types[i]) {
427 case ST_VS:
428 stage = "vs";
429 break;
430 case ST_GS:
431 stage = "gs";
432 break;
433 case ST_FS8:
434 stage = "fs8";
435 break;
436 case ST_FS16:
437 stage = "fs16";
438 break;
439 default:
440 stage = "other";
441 break;
442 }
443
444 print_shader_time_line(stage, shader_name, shader_num,
445 scaled[i], total);
446 }
447
448 fprintf(stderr, "\n");
449 print_shader_time_line("total", "vs", -1, total_by_type[ST_VS], total);
450 print_shader_time_line("total", "gs", -1, total_by_type[ST_GS], total);
451 print_shader_time_line("total", "fs8", -1, total_by_type[ST_FS8], total);
452 print_shader_time_line("total", "fs16", -1, total_by_type[ST_FS16], total);
453 }
454
455 static void
456 brw_collect_shader_time(struct brw_context *brw)
457 {
458 if (!brw->shader_time.bo)
459 return;
460
461 /* This probably stalls on the last rendering. We could fix that by
462 * delaying reading the reports, but it doesn't look like it's a big
463 * overhead compared to the cost of tracking the time in the first place.
464 */
465 drm_intel_bo_map(brw->shader_time.bo, true);
466
467 uint32_t *times = brw->shader_time.bo->virtual;
468
469 for (int i = 0; i < brw->shader_time.num_entries; i++) {
470 brw->shader_time.cumulative[i] += times[i * SHADER_TIME_STRIDE / 4];
471 }
472
473 /* Zero the BO out to clear it out for our next collection.
474 */
475 memset(times, 0, brw->shader_time.bo->size);
476 drm_intel_bo_unmap(brw->shader_time.bo);
477 }
478
479 void
480 brw_collect_and_report_shader_time(struct brw_context *brw)
481 {
482 brw_collect_shader_time(brw);
483
484 if (brw->shader_time.report_time == 0 ||
485 get_time() - brw->shader_time.report_time >= 1.0) {
486 brw_report_shader_time(brw);
487 brw->shader_time.report_time = get_time();
488 }
489 }
490
491 /**
492 * Chooses an index in the shader_time buffer and sets up tracking information
493 * for our printouts.
494 *
495 * Note that this holds on to references to the underlying programs, which may
496 * change their lifetimes compared to normal operation.
497 */
498 int
499 brw_get_shader_time_index(struct brw_context *brw,
500 struct gl_shader_program *shader_prog,
501 struct gl_program *prog,
502 enum shader_time_shader_type type)
503 {
504 struct gl_context *ctx = &brw->ctx;
505
506 int shader_time_index = brw->shader_time.num_entries++;
507 assert(shader_time_index < brw->shader_time.max_entries);
508 brw->shader_time.types[shader_time_index] = type;
509
510 _mesa_reference_shader_program(ctx,
511 &brw->shader_time.shader_programs[shader_time_index],
512 shader_prog);
513
514 _mesa_reference_program(ctx,
515 &brw->shader_time.programs[shader_time_index],
516 prog);
517
518 return shader_time_index;
519 }
520
521 void
522 brw_destroy_shader_time(struct brw_context *brw)
523 {
524 drm_intel_bo_unreference(brw->shader_time.bo);
525 brw->shader_time.bo = NULL;
526 }
527
528 void
529 brw_mark_surface_used(struct brw_stage_prog_data *prog_data,
530 unsigned surf_index)
531 {
532 assert(surf_index < BRW_MAX_SURFACES);
533
534 prog_data->binding_table.size_bytes =
535 MAX2(prog_data->binding_table.size_bytes, (surf_index + 1) * 4);
536 }
537
538 bool
539 brw_stage_prog_data_compare(const struct brw_stage_prog_data *a,
540 const struct brw_stage_prog_data *b)
541 {
542 /* Compare all the struct up to the pointers. */
543 if (memcmp(a, b, offsetof(struct brw_stage_prog_data, param)))
544 return false;
545
546 if (memcmp(a->param, b->param, a->nr_params * sizeof(void *)))
547 return false;
548
549 if (memcmp(a->pull_param, b->pull_param, a->nr_pull_params * sizeof(void *)))
550 return false;
551
552 return true;
553 }
554
555 void
556 brw_stage_prog_data_free(const void *p)
557 {
558 struct brw_stage_prog_data *prog_data = (struct brw_stage_prog_data *)p;
559
560 ralloc_free(prog_data->param);
561 ralloc_free(prog_data->pull_param);
562 }
563
564 void
565 brw_dump_ir(const char *stage, struct gl_shader_program *shader_prog,
566 struct gl_shader *shader, struct gl_program *prog)
567 {
568 if (shader_prog) {
569 fprintf(stderr,
570 "GLSL IR for native %s shader %d:\n", stage, shader_prog->Name);
571 _mesa_print_ir(stderr, shader->ir, NULL);
572 fprintf(stderr, "\n\n");
573 } else {
574 fprintf(stderr, "ARB_%s_program %d ir for native %s shader\n",
575 stage, prog->Id, stage);
576 _mesa_print_program(prog);
577 }
578 }