035824171e3b0aa54c4da24bbd9229892dce1c4e
[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 brw_report_shader_time(struct brw_context *brw)
246 {
247 if (!brw->shader_time.bo || !brw->shader_time.num_entries)
248 return;
249
250 uint64_t scaled[brw->shader_time.num_entries];
251 uint64_t *sorted[brw->shader_time.num_entries];
252 double total = 0;
253 for (int i = 0; i < brw->shader_time.num_entries; i++) {
254 uint64_t written = 0, reset = 0;
255
256 sorted[i] = &scaled[i];
257
258 switch (brw->shader_time.types[i]) {
259 case ST_VS_WRITTEN:
260 case ST_VS_RESET:
261 case ST_FS8_WRITTEN:
262 case ST_FS8_RESET:
263 case ST_FS16_WRITTEN:
264 case ST_FS16_RESET:
265 /* We'll handle these when along with the time. */
266 scaled[i] = 0;
267 continue;
268
269 case ST_VS:
270 case ST_FS8:
271 case ST_FS16:
272 get_written_and_reset(brw, i, &written, &reset);
273 break;
274
275 default:
276 /* I sometimes want to print things that aren't the 3 shader times.
277 * Just print the sum in that case.
278 */
279 written = 1;
280 reset = 0;
281 break;
282 }
283
284 uint64_t time = brw->shader_time.cumulative[i];
285 if (written) {
286 scaled[i] = time / written * (written + reset);
287 } else {
288 scaled[i] = time;
289 }
290
291 total += scaled[i];
292 }
293
294 if (total == 0) {
295 printf("No shader time collected yet\n");
296 return;
297 }
298
299 qsort(sorted, brw->shader_time.num_entries, sizeof(sorted[0]), compare_time);
300
301 printf("\n");
302 printf("type ID cycles spent %% of total\n");
303 for (int s = 0; s < brw->shader_time.num_entries; s++) {
304 /* Work back from the sorted pointers times to a time to print. */
305 int i = sorted[s] - scaled;
306
307 if (scaled[i] == 0)
308 continue;
309
310 int shader_num = -1;
311 if (brw->shader_time.programs[i]) {
312 shader_num = brw->shader_time.programs[i]->Name;
313 }
314
315 switch (brw->shader_time.types[i]) {
316 case ST_VS:
317 printf("vs %4d: ", shader_num);
318 break;
319 case ST_FS8:
320 printf("fs8 %4d: ", shader_num);
321 break;
322 case ST_FS16:
323 printf("fs16 %4d: ", shader_num);
324 break;
325 default:
326 printf("other: ");
327 break;
328 }
329
330 printf("%16lld (%7.2f Gcycles) %4.1f%%\n",
331 (long long)scaled[i],
332 (double)scaled[i] / 1000000000.0,
333 (double)scaled[i] / total * 100.0);
334 }
335 }
336
337 static void
338 brw_collect_shader_time(struct brw_context *brw)
339 {
340 if (!brw->shader_time.bo)
341 return;
342
343 /* This probably stalls on the last rendering. We could fix that by
344 * delaying reading the reports, but it doesn't look like it's a big
345 * overhead compared to the cost of tracking the time in the first place.
346 */
347 drm_intel_bo_map(brw->shader_time.bo, true);
348
349 uint32_t *times = brw->shader_time.bo->virtual;
350
351 for (int i = 0; i < brw->shader_time.num_entries; i++) {
352 brw->shader_time.cumulative[i] += times[i];
353 }
354
355 /* Zero the BO out to clear it out for our next collection.
356 */
357 memset(times, 0, brw->shader_time.bo->size);
358 drm_intel_bo_unmap(brw->shader_time.bo);
359 }
360
361 void
362 brw_collect_and_report_shader_time(struct brw_context *brw)
363 {
364 brw_collect_shader_time(brw);
365
366 if (brw->shader_time.report_time == 0 ||
367 get_time() - brw->shader_time.report_time >= 1.0) {
368 brw_report_shader_time(brw);
369 brw->shader_time.report_time = get_time();
370 }
371 }
372
373 void
374 brw_destroy_shader_time(struct brw_context *brw)
375 {
376 drm_intel_bo_unreference(brw->shader_time.bo);
377 brw->shader_time.bo = NULL;
378 }