2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics 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 <keithw@vmware.com>
33 #include "main/imports.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_print.h"
36 #include "program/program.h"
37 #include "program/programopt.h"
39 #include "util/ralloc.h"
40 #include "compiler/glsl/ir.h"
42 #include "brw_program.h"
43 #include "brw_context.h"
44 #include "brw_shader.h"
46 #include "intel_batchbuffer.h"
49 get_new_program_id(struct intel_screen
*screen
)
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
);
58 static struct gl_program
*brwNewProgram( struct gl_context
*ctx
,
62 struct brw_context
*brw
= brw_context(ctx
);
65 case GL_VERTEX_PROGRAM_ARB
: {
66 struct brw_vertex_program
*prog
= CALLOC_STRUCT(brw_vertex_program
);
68 prog
->id
= get_new_program_id(brw
->intelScreen
);
70 return _mesa_init_gl_program(&prog
->program
.Base
, target
, id
);
76 case GL_FRAGMENT_PROGRAM_ARB
: {
77 struct brw_fragment_program
*prog
= CALLOC_STRUCT(brw_fragment_program
);
79 prog
->id
= get_new_program_id(brw
->intelScreen
);
81 return _mesa_init_gl_program(&prog
->program
.Base
, target
, id
);
87 case GL_GEOMETRY_PROGRAM_NV
: {
88 struct brw_geometry_program
*prog
= CALLOC_STRUCT(brw_geometry_program
);
90 prog
->id
= get_new_program_id(brw
->intelScreen
);
92 return _mesa_init_gl_program(&prog
->program
.Base
, target
, id
);
98 case GL_TESS_CONTROL_PROGRAM_NV
: {
99 struct brw_tess_ctrl_program
*prog
= CALLOC_STRUCT(brw_tess_ctrl_program
);
101 prog
->id
= get_new_program_id(brw
->intelScreen
);
103 return _mesa_init_gl_program(&prog
->program
.Base
, target
, id
);
109 case GL_TESS_EVALUATION_PROGRAM_NV
: {
110 struct brw_tess_eval_program
*prog
= CALLOC_STRUCT(brw_tess_eval_program
);
112 prog
->id
= get_new_program_id(brw
->intelScreen
);
114 return _mesa_init_gl_program(&prog
->program
.Base
, target
, id
);
120 case GL_COMPUTE_PROGRAM_NV
: {
121 struct brw_compute_program
*prog
= CALLOC_STRUCT(brw_compute_program
);
123 prog
->id
= get_new_program_id(brw
->intelScreen
);
125 return _mesa_init_gl_program(&prog
->program
.Base
, target
, id
);
132 unreachable("Unsupported target in brwNewProgram()");
136 static void brwDeleteProgram( struct gl_context
*ctx
,
137 struct gl_program
*prog
)
139 _mesa_delete_program( ctx
, prog
);
144 brwProgramStringNotify(struct gl_context
*ctx
,
146 struct gl_program
*prog
)
148 struct brw_context
*brw
= brw_context(ctx
);
149 const struct brw_compiler
*compiler
= brw
->intelScreen
->compiler
;
152 case GL_FRAGMENT_PROGRAM_ARB
: {
153 struct gl_fragment_program
*fprog
= (struct gl_fragment_program
*) prog
;
154 struct brw_fragment_program
*newFP
= brw_fragment_program(fprog
);
155 const struct brw_fragment_program
*curFP
=
156 brw_fragment_program_const(brw
->fragment_program
);
159 brw
->ctx
.NewDriverState
|= BRW_NEW_FRAGMENT_PROGRAM
;
160 newFP
->id
= get_new_program_id(brw
->intelScreen
);
162 brw_add_texrect_params(prog
);
164 prog
->nir
= brw_create_nir(brw
, NULL
, prog
, MESA_SHADER_FRAGMENT
, true);
166 brw_fs_precompile(ctx
, NULL
, prog
);
169 case GL_VERTEX_PROGRAM_ARB
: {
170 struct gl_vertex_program
*vprog
= (struct gl_vertex_program
*) prog
;
171 struct brw_vertex_program
*newVP
= brw_vertex_program(vprog
);
172 const struct brw_vertex_program
*curVP
=
173 brw_vertex_program_const(brw
->vertex_program
);
176 brw
->ctx
.NewDriverState
|= BRW_NEW_VERTEX_PROGRAM
;
177 if (newVP
->program
.IsPositionInvariant
) {
178 _mesa_insert_mvp_code(ctx
, &newVP
->program
);
180 newVP
->id
= get_new_program_id(brw
->intelScreen
);
182 /* Also tell tnl about it:
184 _tnl_program_string(ctx
, target
, prog
);
186 brw_add_texrect_params(prog
);
188 prog
->nir
= brw_create_nir(brw
, NULL
, prog
, MESA_SHADER_VERTEX
,
189 compiler
->scalar_stage
[MESA_SHADER_VERTEX
]);
191 brw_vs_precompile(ctx
, NULL
, prog
);
196 * driver->ProgramStringNotify is only called for ARB programs, fixed
197 * function vertex programs, and ir_to_mesa (which isn't used by the
198 * i965 back-end). Therefore, even after geometry shaders are added,
199 * this function should only ever be called with a target of
200 * GL_VERTEX_PROGRAM_ARB or GL_FRAGMENT_PROGRAM_ARB.
202 unreachable("Unexpected target in brwProgramStringNotify");
209 brw_memory_barrier(struct gl_context
*ctx
, GLbitfield barriers
)
211 struct brw_context
*brw
= brw_context(ctx
);
212 unsigned bits
= (PIPE_CONTROL_DATA_CACHE_INVALIDATE
|
213 PIPE_CONTROL_NO_WRITE
|
214 PIPE_CONTROL_CS_STALL
);
215 assert(brw
->gen
>= 7 && brw
->gen
<= 9);
217 if (barriers
& (GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT
|
218 GL_ELEMENT_ARRAY_BARRIER_BIT
|
219 GL_COMMAND_BARRIER_BIT
))
220 bits
|= PIPE_CONTROL_VF_CACHE_INVALIDATE
;
222 if (barriers
& GL_UNIFORM_BARRIER_BIT
)
223 bits
|= (PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
|
224 PIPE_CONTROL_CONST_CACHE_INVALIDATE
);
226 if (barriers
& GL_TEXTURE_FETCH_BARRIER_BIT
)
227 bits
|= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
;
229 if (barriers
& GL_TEXTURE_UPDATE_BARRIER_BIT
)
230 bits
|= PIPE_CONTROL_RENDER_TARGET_FLUSH
;
232 if (barriers
& GL_FRAMEBUFFER_BARRIER_BIT
)
233 bits
|= (PIPE_CONTROL_DEPTH_CACHE_FLUSH
|
234 PIPE_CONTROL_RENDER_TARGET_FLUSH
);
236 /* Typed surface messages are handled by the render cache on IVB, so we
237 * need to flush it too.
239 if (brw
->gen
== 7 && !brw
->is_haswell
)
240 bits
|= PIPE_CONTROL_RENDER_TARGET_FLUSH
;
242 brw_emit_pipe_control_flush(brw
, bits
);
246 brw_add_texrect_params(struct gl_program
*prog
)
248 for (int texunit
= 0; texunit
< BRW_MAX_TEX_UNIT
; texunit
++) {
249 if (!(prog
->TexturesUsed
[texunit
] & (1 << TEXTURE_RECT_INDEX
)))
252 int tokens
[STATE_LENGTH
] = {
260 _mesa_add_state_reference(prog
->Parameters
, (gl_state_index
*)tokens
);
265 brw_get_scratch_bo(struct brw_context
*brw
,
266 drm_intel_bo
**scratch_bo
, int size
)
268 drm_intel_bo
*old_bo
= *scratch_bo
;
270 if (old_bo
&& old_bo
->size
< size
) {
271 drm_intel_bo_unreference(old_bo
);
276 *scratch_bo
= drm_intel_bo_alloc(brw
->bufmgr
, "scratch bo", size
, 4096);
280 void brwInitFragProgFuncs( struct dd_function_table
*functions
)
282 assert(functions
->ProgramStringNotify
== _tnl_program_string
);
284 functions
->NewProgram
= brwNewProgram
;
285 functions
->DeleteProgram
= brwDeleteProgram
;
286 functions
->ProgramStringNotify
= brwProgramStringNotify
;
288 functions
->NewShader
= brw_new_shader
;
289 functions
->LinkShader
= brw_link_shader
;
291 functions
->MemoryBarrier
= brw_memory_barrier
;
294 struct shader_times
{
301 brw_init_shader_time(struct brw_context
*brw
)
303 const int max_entries
= 2048;
304 brw
->shader_time
.bo
=
305 drm_intel_bo_alloc(brw
->bufmgr
, "shader time",
306 max_entries
* SHADER_TIME_STRIDE
* 3, 4096);
307 brw
->shader_time
.names
= rzalloc_array(brw
, const char *, max_entries
);
308 brw
->shader_time
.ids
= rzalloc_array(brw
, int, max_entries
);
309 brw
->shader_time
.types
= rzalloc_array(brw
, enum shader_time_shader_type
,
311 brw
->shader_time
.cumulative
= rzalloc_array(brw
, struct shader_times
,
313 brw
->shader_time
.max_entries
= max_entries
;
317 compare_time(const void *a
, const void *b
)
319 uint64_t * const *a_val
= a
;
320 uint64_t * const *b_val
= b
;
322 /* We don't just subtract because we're turning the value to an int. */
323 if (**a_val
< **b_val
)
325 else if (**a_val
== **b_val
)
332 print_shader_time_line(const char *stage
, const char *name
,
333 int shader_num
, uint64_t time
, uint64_t total
)
335 fprintf(stderr
, "%-6s%-18s", stage
, name
);
338 fprintf(stderr
, "%4d: ", shader_num
);
340 fprintf(stderr
, " : ");
342 fprintf(stderr
, "%16lld (%7.2f Gcycles) %4.1f%%\n",
344 (double)time
/ 1000000000.0,
345 (double)time
/ total
* 100.0);
349 brw_report_shader_time(struct brw_context
*brw
)
351 if (!brw
->shader_time
.bo
|| !brw
->shader_time
.num_entries
)
354 uint64_t scaled
[brw
->shader_time
.num_entries
];
355 uint64_t *sorted
[brw
->shader_time
.num_entries
];
356 uint64_t total_by_type
[ST_CS
+ 1];
357 memset(total_by_type
, 0, sizeof(total_by_type
));
359 for (int i
= 0; i
< brw
->shader_time
.num_entries
; i
++) {
360 uint64_t written
= 0, reset
= 0;
361 enum shader_time_shader_type type
= brw
->shader_time
.types
[i
];
363 sorted
[i
] = &scaled
[i
];
373 written
= brw
->shader_time
.cumulative
[i
].written
;
374 reset
= brw
->shader_time
.cumulative
[i
].reset
;
378 /* I sometimes want to print things that aren't the 3 shader times.
379 * Just print the sum in that case.
386 uint64_t time
= brw
->shader_time
.cumulative
[i
].time
;
388 scaled
[i
] = time
/ written
* (written
+ reset
);
401 total_by_type
[type
] += scaled
[i
];
411 fprintf(stderr
, "No shader time collected yet\n");
415 qsort(sorted
, brw
->shader_time
.num_entries
, sizeof(sorted
[0]), compare_time
);
417 fprintf(stderr
, "\n");
418 fprintf(stderr
, "type ID cycles spent %% of total\n");
419 for (int s
= 0; s
< brw
->shader_time
.num_entries
; s
++) {
421 /* Work back from the sorted pointers times to a time to print. */
422 int i
= sorted
[s
] - scaled
;
427 int shader_num
= brw
->shader_time
.ids
[i
];
428 const char *shader_name
= brw
->shader_time
.names
[i
];
430 switch (brw
->shader_time
.types
[i
]) {
457 print_shader_time_line(stage
, shader_name
, shader_num
,
461 fprintf(stderr
, "\n");
462 print_shader_time_line("total", "vs", 0, total_by_type
[ST_VS
], total
);
463 print_shader_time_line("total", "tcs", 0, total_by_type
[ST_TCS
], total
);
464 print_shader_time_line("total", "tes", 0, total_by_type
[ST_TES
], total
);
465 print_shader_time_line("total", "gs", 0, total_by_type
[ST_GS
], total
);
466 print_shader_time_line("total", "fs8", 0, total_by_type
[ST_FS8
], total
);
467 print_shader_time_line("total", "fs16", 0, total_by_type
[ST_FS16
], total
);
468 print_shader_time_line("total", "cs", 0, total_by_type
[ST_CS
], total
);
472 brw_collect_shader_time(struct brw_context
*brw
)
474 if (!brw
->shader_time
.bo
)
477 /* This probably stalls on the last rendering. We could fix that by
478 * delaying reading the reports, but it doesn't look like it's a big
479 * overhead compared to the cost of tracking the time in the first place.
481 drm_intel_bo_map(brw
->shader_time
.bo
, true);
482 void *bo_map
= brw
->shader_time
.bo
->virtual;
484 for (int i
= 0; i
< brw
->shader_time
.num_entries
; i
++) {
485 uint32_t *times
= bo_map
+ i
* 3 * SHADER_TIME_STRIDE
;
487 brw
->shader_time
.cumulative
[i
].time
+= times
[SHADER_TIME_STRIDE
* 0 / 4];
488 brw
->shader_time
.cumulative
[i
].written
+= times
[SHADER_TIME_STRIDE
* 1 / 4];
489 brw
->shader_time
.cumulative
[i
].reset
+= times
[SHADER_TIME_STRIDE
* 2 / 4];
492 /* Zero the BO out to clear it out for our next collection.
494 memset(bo_map
, 0, brw
->shader_time
.bo
->size
);
495 drm_intel_bo_unmap(brw
->shader_time
.bo
);
499 brw_collect_and_report_shader_time(struct brw_context
*brw
)
501 brw_collect_shader_time(brw
);
503 if (brw
->shader_time
.report_time
== 0 ||
504 get_time() - brw
->shader_time
.report_time
>= 1.0) {
505 brw_report_shader_time(brw
);
506 brw
->shader_time
.report_time
= get_time();
511 * Chooses an index in the shader_time buffer and sets up tracking information
514 * Note that this holds on to references to the underlying programs, which may
515 * change their lifetimes compared to normal operation.
518 brw_get_shader_time_index(struct brw_context
*brw
,
519 struct gl_shader_program
*shader_prog
,
520 struct gl_program
*prog
,
521 enum shader_time_shader_type type
)
523 int shader_time_index
= brw
->shader_time
.num_entries
++;
524 assert(shader_time_index
< brw
->shader_time
.max_entries
);
525 brw
->shader_time
.types
[shader_time_index
] = type
;
527 int id
= shader_prog
? shader_prog
->Name
: prog
->Id
;
531 } else if (!shader_prog
) {
533 } else if (shader_prog
->Label
) {
534 name
= ralloc_strdup(brw
->shader_time
.names
, shader_prog
->Label
);
539 brw
->shader_time
.names
[shader_time_index
] = name
;
540 brw
->shader_time
.ids
[shader_time_index
] = id
;
542 return shader_time_index
;
546 brw_destroy_shader_time(struct brw_context
*brw
)
548 drm_intel_bo_unreference(brw
->shader_time
.bo
);
549 brw
->shader_time
.bo
= NULL
;
553 brw_stage_prog_data_free(const void *p
)
555 struct brw_stage_prog_data
*prog_data
= (struct brw_stage_prog_data
*)p
;
557 ralloc_free(prog_data
->param
);
558 ralloc_free(prog_data
->pull_param
);
559 ralloc_free(prog_data
->image_param
);
563 brw_dump_ir(const char *stage
, struct gl_shader_program
*shader_prog
,
564 struct gl_shader
*shader
, struct gl_program
*prog
)
569 "GLSL IR for native %s shader %d:\n",
570 stage
, shader_prog
->Name
);
571 _mesa_print_ir(stderr
, shader
->ir
, NULL
);
572 fprintf(stderr
, "\n\n");
575 fprintf(stderr
, "ARB_%s_program %d ir for native %s shader\n",
576 stage
, prog
->Id
, stage
);
577 _mesa_print_program(prog
);
582 brw_setup_tex_for_precompile(struct brw_context
*brw
,
583 struct brw_sampler_prog_key_data
*tex
,
584 struct gl_program
*prog
)
586 const bool has_shader_channel_select
= brw
->is_haswell
|| brw
->gen
>= 8;
587 unsigned sampler_count
= _mesa_fls(prog
->SamplersUsed
);
588 for (unsigned i
= 0; i
< sampler_count
; i
++) {
589 if (!has_shader_channel_select
&& (prog
->ShadowSamplers
& (1 << i
))) {
590 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
592 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
594 /* Color sampler: assume no swizzling. */
595 tex
->swizzles
[i
] = SWIZZLE_XYZW
;