i965: Print a total time for the different shader stages.
[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 (http://www.tungstengraphics.com) 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 <keith@tungstengraphics.com>
30 */
31
32 #include "main/imports.h"
33 #include "main/enums.h"
34 #include "main/shaderobj.h"
35 #include "program/prog_parameter.h"
36 #include "program/program.h"
37 #include "program/programopt.h"
38 #include "tnl/tnl.h"
39 #include "glsl/ralloc.h"
40
41 #include "brw_context.h"
42 #include "brw_wm.h"
43
44 static void brwBindProgram( struct gl_context *ctx,
45 GLenum target,
46 struct gl_program *prog )
47 {
48 struct brw_context *brw = brw_context(ctx);
49
50 switch (target) {
51 case GL_VERTEX_PROGRAM_ARB:
52 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
53 break;
54 case GL_FRAGMENT_PROGRAM_ARB:
55 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
56 break;
57 }
58 }
59
60 static struct gl_program *brwNewProgram( struct gl_context *ctx,
61 GLenum target,
62 GLuint id )
63 {
64 struct brw_context *brw = brw_context(ctx);
65
66 switch (target) {
67 case GL_VERTEX_PROGRAM_ARB: {
68 struct brw_vertex_program *prog = CALLOC_STRUCT(brw_vertex_program);
69 if (prog) {
70 prog->id = brw->program_id++;
71
72 return _mesa_init_vertex_program( ctx, &prog->program,
73 target, id );
74 }
75 else
76 return NULL;
77 }
78
79 case GL_FRAGMENT_PROGRAM_ARB: {
80 struct brw_fragment_program *prog = CALLOC_STRUCT(brw_fragment_program);
81 if (prog) {
82 prog->id = brw->program_id++;
83
84 return _mesa_init_fragment_program( ctx, &prog->program,
85 target, id );
86 }
87 else
88 return NULL;
89 }
90
91 default:
92 return _mesa_new_program(ctx, target, id);
93 }
94 }
95
96 static void brwDeleteProgram( struct gl_context *ctx,
97 struct gl_program *prog )
98 {
99 _mesa_delete_program( ctx, prog );
100 }
101
102
103 static GLboolean
104 brwIsProgramNative(struct gl_context *ctx,
105 GLenum target,
106 struct gl_program *prog)
107 {
108 return true;
109 }
110
111 static GLboolean
112 brwProgramStringNotify(struct gl_context *ctx,
113 GLenum target,
114 struct gl_program *prog)
115 {
116 struct brw_context *brw = brw_context(ctx);
117
118 if (target == GL_FRAGMENT_PROGRAM_ARB) {
119 struct gl_fragment_program *fprog = (struct gl_fragment_program *) prog;
120 struct brw_fragment_program *newFP = brw_fragment_program(fprog);
121 const struct brw_fragment_program *curFP =
122 brw_fragment_program_const(brw->fragment_program);
123
124 if (newFP == curFP)
125 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
126 newFP->id = brw->program_id++;
127 }
128 else if (target == GL_VERTEX_PROGRAM_ARB) {
129 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
130 struct brw_vertex_program *newVP = brw_vertex_program(vprog);
131 const struct brw_vertex_program *curVP =
132 brw_vertex_program_const(brw->vertex_program);
133
134 if (newVP == curVP)
135 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
136 if (newVP->program.IsPositionInvariant) {
137 _mesa_insert_mvp_code(ctx, &newVP->program);
138 }
139 newVP->id = brw->program_id++;
140
141 /* Also tell tnl about it:
142 */
143 _tnl_program_string(ctx, target, prog);
144 }
145
146 return true;
147 }
148
149 /* Per-thread scratch space is a power-of-two multiple of 1KB. */
150 int
151 brw_get_scratch_size(int size)
152 {
153 int i;
154
155 for (i = 1024; i < size; i *= 2)
156 ;
157
158 return i;
159 }
160
161 void
162 brw_get_scratch_bo(struct intel_context *intel,
163 drm_intel_bo **scratch_bo, int size)
164 {
165 drm_intel_bo *old_bo = *scratch_bo;
166
167 if (old_bo && old_bo->size < size) {
168 drm_intel_bo_unreference(old_bo);
169 old_bo = NULL;
170 }
171
172 if (!old_bo) {
173 *scratch_bo = drm_intel_bo_alloc(intel->bufmgr, "scratch bo", size, 4096);
174 }
175 }
176
177 void brwInitFragProgFuncs( struct dd_function_table *functions )
178 {
179 assert(functions->ProgramStringNotify == _tnl_program_string);
180
181 functions->BindProgram = brwBindProgram;
182 functions->NewProgram = brwNewProgram;
183 functions->DeleteProgram = brwDeleteProgram;
184 functions->IsProgramNative = brwIsProgramNative;
185 functions->ProgramStringNotify = brwProgramStringNotify;
186
187 functions->NewShader = brw_new_shader;
188 functions->NewShaderProgram = brw_new_shader_program;
189 functions->LinkShader = brw_link_shader;
190 }
191
192 void
193 brw_init_shader_time(struct brw_context *brw)
194 {
195 struct intel_context *intel = &brw->intel;
196
197 const int max_entries = 4096;
198 brw->shader_time.bo = drm_intel_bo_alloc(intel->bufmgr, "shader time",
199 max_entries * 4, 4096);
200 brw->shader_time.programs = rzalloc_array(brw, struct gl_shader_program *,
201 max_entries);
202 brw->shader_time.types = rzalloc_array(brw, enum shader_time_shader_type,
203 max_entries);
204 brw->shader_time.cumulative = rzalloc_array(brw, uint64_t,
205 max_entries);
206 brw->shader_time.max_entries = max_entries;
207 }
208
209 static int
210 compare_time(const void *a, const void *b)
211 {
212 uint64_t * const *a_val = a;
213 uint64_t * const *b_val = b;
214
215 /* We don't just subtract because we're turning the value to an int. */
216 if (**a_val < **b_val)
217 return -1;
218 else if (**a_val == **b_val)
219 return 0;
220 else
221 return 1;
222 }
223
224 static void
225 get_written_and_reset(struct brw_context *brw, int i,
226 uint64_t *written, uint64_t *reset)
227 {
228 enum shader_time_shader_type type = brw->shader_time.types[i];
229 assert(type == ST_VS || type == ST_FS8 || type == ST_FS16);
230
231 /* Find where we recorded written and reset. */
232 int wi, ri;
233
234 for (wi = i; brw->shader_time.types[wi] != type + 1; wi++)
235 ;
236
237 for (ri = i; brw->shader_time.types[ri] != type + 2; ri++)
238 ;
239
240 *written = brw->shader_time.cumulative[wi];
241 *reset = brw->shader_time.cumulative[ri];
242 }
243
244 static void
245 print_shader_time_line(const char *name, int shader_num,
246 uint64_t time, uint64_t total)
247 {
248 printf("%s", name);
249 for (int i = strlen(name); i < 10; i++)
250 printf(" ");
251 printf("%4d: ", shader_num);
252
253 printf("%16lld (%7.2f Gcycles) %4.1f%%\n",
254 (long long)time,
255 (double)time / 1000000000.0,
256 (double)time / total * 100.0);
257 }
258
259 static void
260 brw_report_shader_time(struct brw_context *brw)
261 {
262 if (!brw->shader_time.bo || !brw->shader_time.num_entries)
263 return;
264
265 uint64_t scaled[brw->shader_time.num_entries];
266 uint64_t *sorted[brw->shader_time.num_entries];
267 uint64_t total_by_type[ST_FS16 + 1];
268 memset(total_by_type, 0, sizeof(total_by_type));
269 double total = 0;
270 for (int i = 0; i < brw->shader_time.num_entries; i++) {
271 uint64_t written = 0, reset = 0;
272 enum shader_time_shader_type type = brw->shader_time.types[i];
273
274 sorted[i] = &scaled[i];
275
276 switch (type) {
277 case ST_VS_WRITTEN:
278 case ST_VS_RESET:
279 case ST_FS8_WRITTEN:
280 case ST_FS8_RESET:
281 case ST_FS16_WRITTEN:
282 case ST_FS16_RESET:
283 /* We'll handle these when along with the time. */
284 scaled[i] = 0;
285 continue;
286
287 case ST_VS:
288 case ST_FS8:
289 case ST_FS16:
290 get_written_and_reset(brw, i, &written, &reset);
291 break;
292
293 default:
294 /* I sometimes want to print things that aren't the 3 shader times.
295 * Just print the sum in that case.
296 */
297 written = 1;
298 reset = 0;
299 break;
300 }
301
302 uint64_t time = brw->shader_time.cumulative[i];
303 if (written) {
304 scaled[i] = time / written * (written + reset);
305 } else {
306 scaled[i] = time;
307 }
308
309 switch (type) {
310 case ST_VS:
311 case ST_FS8:
312 case ST_FS16:
313 total_by_type[type] += scaled[i];
314 break;
315 default:
316 break;
317 }
318
319 total += scaled[i];
320 }
321
322 if (total == 0) {
323 printf("No shader time collected yet\n");
324 return;
325 }
326
327 qsort(sorted, brw->shader_time.num_entries, sizeof(sorted[0]), compare_time);
328
329 printf("\n");
330 printf("type ID cycles spent %% of total\n");
331 for (int s = 0; s < brw->shader_time.num_entries; s++) {
332 /* Work back from the sorted pointers times to a time to print. */
333 int i = sorted[s] - scaled;
334
335 if (scaled[i] == 0)
336 continue;
337
338 int shader_num = -1;
339 if (brw->shader_time.programs[i]) {
340 shader_num = brw->shader_time.programs[i]->Name;
341 }
342
343 switch (brw->shader_time.types[i]) {
344 case ST_VS:
345 print_shader_time_line("vs", shader_num, scaled[i], total);
346 break;
347 case ST_FS8:
348 print_shader_time_line("fs8", shader_num, scaled[i], total);
349 break;
350 case ST_FS16:
351 print_shader_time_line("fs16", shader_num, scaled[i], total);
352 break;
353 default:
354 print_shader_time_line("other", shader_num, scaled[i], total);
355 break;
356 }
357 }
358
359 printf("\n");
360 print_shader_time_line("total vs", -1, total_by_type[ST_VS], total);
361 print_shader_time_line("total fs8", -1, total_by_type[ST_FS8], total);
362 print_shader_time_line("total fs16", -1, total_by_type[ST_FS16], total);
363 }
364
365 static void
366 brw_collect_shader_time(struct brw_context *brw)
367 {
368 if (!brw->shader_time.bo)
369 return;
370
371 /* This probably stalls on the last rendering. We could fix that by
372 * delaying reading the reports, but it doesn't look like it's a big
373 * overhead compared to the cost of tracking the time in the first place.
374 */
375 drm_intel_bo_map(brw->shader_time.bo, true);
376
377 uint32_t *times = brw->shader_time.bo->virtual;
378
379 for (int i = 0; i < brw->shader_time.num_entries; i++) {
380 brw->shader_time.cumulative[i] += times[i];
381 }
382
383 /* Zero the BO out to clear it out for our next collection.
384 */
385 memset(times, 0, brw->shader_time.bo->size);
386 drm_intel_bo_unmap(brw->shader_time.bo);
387 }
388
389 void
390 brw_collect_and_report_shader_time(struct brw_context *brw)
391 {
392 brw_collect_shader_time(brw);
393
394 if (brw->shader_time.report_time == 0 ||
395 get_time() - brw->shader_time.report_time >= 1.0) {
396 brw_report_shader_time(brw);
397 brw->shader_time.report_time = get_time();
398 }
399 }
400
401 void
402 brw_destroy_shader_time(struct brw_context *brw)
403 {
404 drm_intel_bo_unreference(brw->shader_time.bo);
405 brw->shader_time.bo = NULL;
406 }