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