2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
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"
39 #include "glsl/ralloc.h"
41 #include "brw_context.h"
44 static void brwBindProgram( struct gl_context
*ctx
,
46 struct gl_program
*prog
)
48 struct brw_context
*brw
= brw_context(ctx
);
51 case GL_VERTEX_PROGRAM_ARB
:
52 brw
->state
.dirty
.brw
|= BRW_NEW_VERTEX_PROGRAM
;
54 case GL_FRAGMENT_PROGRAM_ARB
:
55 brw
->state
.dirty
.brw
|= BRW_NEW_FRAGMENT_PROGRAM
;
60 static struct gl_program
*brwNewProgram( struct gl_context
*ctx
,
64 struct brw_context
*brw
= brw_context(ctx
);
67 case GL_VERTEX_PROGRAM_ARB
: {
68 struct brw_vertex_program
*prog
= CALLOC_STRUCT(brw_vertex_program
);
70 prog
->id
= brw
->program_id
++;
72 return _mesa_init_vertex_program( ctx
, &prog
->program
,
79 case GL_FRAGMENT_PROGRAM_ARB
: {
80 struct brw_fragment_program
*prog
= CALLOC_STRUCT(brw_fragment_program
);
82 prog
->id
= brw
->program_id
++;
84 return _mesa_init_fragment_program( ctx
, &prog
->program
,
92 return _mesa_new_program(ctx
, target
, id
);
96 static void brwDeleteProgram( struct gl_context
*ctx
,
97 struct gl_program
*prog
)
99 _mesa_delete_program( ctx
, prog
);
104 brwIsProgramNative(struct gl_context
*ctx
,
106 struct gl_program
*prog
)
112 brwProgramStringNotify(struct gl_context
*ctx
,
114 struct gl_program
*prog
)
116 struct brw_context
*brw
= brw_context(ctx
);
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
);
125 brw
->state
.dirty
.brw
|= BRW_NEW_FRAGMENT_PROGRAM
;
126 newFP
->id
= brw
->program_id
++;
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
);
135 brw
->state
.dirty
.brw
|= BRW_NEW_VERTEX_PROGRAM
;
136 if (newVP
->program
.IsPositionInvariant
) {
137 _mesa_insert_mvp_code(ctx
, &newVP
->program
);
139 newVP
->id
= brw
->program_id
++;
141 /* Also tell tnl about it:
143 _tnl_program_string(ctx
, target
, prog
);
149 /* Per-thread scratch space is a power-of-two multiple of 1KB. */
151 brw_get_scratch_size(int size
)
155 for (i
= 1024; i
< size
; i
*= 2)
162 brw_get_scratch_bo(struct intel_context
*intel
,
163 drm_intel_bo
**scratch_bo
, int size
)
165 drm_intel_bo
*old_bo
= *scratch_bo
;
167 if (old_bo
&& old_bo
->size
< size
) {
168 drm_intel_bo_unreference(old_bo
);
173 *scratch_bo
= drm_intel_bo_alloc(intel
->bufmgr
, "scratch bo", size
, 4096);
177 void brwInitFragProgFuncs( struct dd_function_table
*functions
)
179 assert(functions
->ProgramStringNotify
== _tnl_program_string
);
181 functions
->BindProgram
= brwBindProgram
;
182 functions
->NewProgram
= brwNewProgram
;
183 functions
->DeleteProgram
= brwDeleteProgram
;
184 functions
->IsProgramNative
= brwIsProgramNative
;
185 functions
->ProgramStringNotify
= brwProgramStringNotify
;
187 functions
->NewShader
= brw_new_shader
;
188 functions
->NewShaderProgram
= brw_new_shader_program
;
189 functions
->LinkShader
= brw_link_shader
;
193 brw_init_shader_time(struct brw_context
*brw
)
195 struct intel_context
*intel
= &brw
->intel
;
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
*,
202 brw
->shader_time
.types
= rzalloc_array(brw
, enum shader_time_shader_type
,
204 brw
->shader_time
.cumulative
= rzalloc_array(brw
, uint64_t,
206 brw
->shader_time
.max_entries
= max_entries
;
210 compare_time(const void *a
, const void *b
)
212 uint64_t * const *a_val
= a
;
213 uint64_t * const *b_val
= b
;
215 /* We don't just subtract because we're turning the value to an int. */
216 if (**a_val
< **b_val
)
218 else if (**a_val
== **b_val
)
225 get_written_and_reset(struct brw_context
*brw
, int i
,
226 uint64_t *written
, uint64_t *reset
)
228 enum shader_time_shader_type type
= brw
->shader_time
.types
[i
];
229 assert(type
== ST_VS
|| type
== ST_FS8
|| type
== ST_FS16
);
231 /* Find where we recorded written and reset. */
234 for (wi
= i
; brw
->shader_time
.types
[wi
] != type
+ 1; wi
++)
237 for (ri
= i
; brw
->shader_time
.types
[ri
] != type
+ 2; ri
++)
240 *written
= brw
->shader_time
.cumulative
[wi
];
241 *reset
= brw
->shader_time
.cumulative
[ri
];
245 brw_report_shader_time(struct brw_context
*brw
)
247 if (!brw
->shader_time
.bo
|| !brw
->shader_time
.num_entries
)
250 uint64_t scaled
[brw
->shader_time
.num_entries
];
251 uint64_t *sorted
[brw
->shader_time
.num_entries
];
253 for (int i
= 0; i
< brw
->shader_time
.num_entries
; i
++) {
254 uint64_t written
= 0, reset
= 0;
256 sorted
[i
] = &scaled
[i
];
258 switch (brw
->shader_time
.types
[i
]) {
263 case ST_FS16_WRITTEN
:
265 /* We'll handle these when along with the time. */
272 get_written_and_reset(brw
, i
, &written
, &reset
);
276 /* I sometimes want to print things that aren't the 3 shader times.
277 * Just print the sum in that case.
284 uint64_t time
= brw
->shader_time
.cumulative
[i
];
286 scaled
[i
] = time
/ written
* (written
+ reset
);
295 printf("No shader time collected yet\n");
299 qsort(sorted
, brw
->shader_time
.num_entries
, sizeof(sorted
[0]), compare_time
);
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
;
311 if (brw
->shader_time
.programs
[i
]) {
312 shader_num
= brw
->shader_time
.programs
[i
]->Name
;
315 switch (brw
->shader_time
.types
[i
]) {
317 printf("vs %4d: ", shader_num
);
320 printf("fs8 %4d: ", shader_num
);
323 printf("fs16 %4d: ", shader_num
);
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);
338 brw_collect_shader_time(struct brw_context
*brw
)
340 if (!brw
->shader_time
.bo
)
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.
347 drm_intel_bo_map(brw
->shader_time
.bo
, true);
349 uint32_t *times
= brw
->shader_time
.bo
->virtual;
351 for (int i
= 0; i
< brw
->shader_time
.num_entries
; i
++) {
352 brw
->shader_time
.cumulative
[i
] += times
[i
];
355 /* Zero the BO out to clear it out for our next collection.
357 memset(times
, 0, brw
->shader_time
.bo
->size
);
358 drm_intel_bo_unmap(brw
->shader_time
.bo
);
362 brw_collect_and_report_shader_time(struct brw_context
*brw
)
364 brw_collect_shader_time(brw
);
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();
374 brw_destroy_shader_time(struct brw_context
*brw
)
376 drm_intel_bo_unreference(brw
->shader_time
.bo
);
377 brw
->shader_time
.bo
= NULL
;