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 "main/glspirv.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
37 #include "program/prog_to_nir.h"
38 #include "program/program.h"
39 #include "program/programopt.h"
41 #include "util/ralloc.h"
42 #include "compiler/glsl/ir.h"
43 #include "compiler/glsl/glsl_to_nir.h"
44 #include "compiler/nir/nir_serialize.h"
46 #include "brw_program.h"
47 #include "brw_context.h"
48 #include "compiler/brw_nir.h"
49 #include "brw_defines.h"
50 #include "intel_batchbuffer.h"
53 brw_nir_lower_uniforms(nir_shader
*nir
, bool is_scalar
)
56 nir_assign_var_locations(&nir
->uniforms
, &nir
->num_uniforms
,
57 type_size_scalar_bytes
);
58 return nir_lower_io(nir
, nir_var_uniform
, type_size_scalar_bytes
, 0);
60 nir_assign_var_locations(&nir
->uniforms
, &nir
->num_uniforms
,
61 type_size_vec4_bytes
);
62 return nir_lower_io(nir
, nir_var_uniform
, type_size_vec4_bytes
, 0);
67 brw_create_nir(struct brw_context
*brw
,
68 const struct gl_shader_program
*shader_prog
,
69 struct gl_program
*prog
,
70 gl_shader_stage stage
,
73 struct gl_context
*ctx
= &brw
->ctx
;
74 const nir_shader_compiler_options
*options
=
75 ctx
->Const
.ShaderCompilerOptions
[stage
].NirOptions
;
78 /* First, lower the GLSL/Mesa IR or SPIR-V to NIR */
80 if (shader_prog
->data
->spirv
) {
81 nir
= _mesa_spirv_to_nir(ctx
, shader_prog
, stage
, options
);
83 nir
= glsl_to_nir(shader_prog
, stage
, options
);
87 nir_remove_dead_variables(nir
, nir_var_shader_in
| nir_var_shader_out
);
88 nir_lower_returns(nir
);
89 nir_validate_shader(nir
);
90 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
91 nir_shader_get_entrypoint(nir
), true, false);
93 nir
= prog_to_nir(prog
, options
);
94 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
); /* turn registers into SSA */
96 nir_validate_shader(nir
);
98 /* Lower PatchVerticesIn from system value to uniform. This needs to
99 * happen before brw_preprocess_nir, since that will lower system values
102 * We only do this for TES if no TCS is present, since otherwise we know
103 * the number of vertices in the patch at link time and we can lower it
104 * directly to a constant. We do this in nir_lower_patch_vertices, which
105 * needs to run after brw_nir_preprocess has turned the system values
108 const bool lower_patch_vertices_in_to_uniform
=
109 (stage
== MESA_SHADER_TESS_CTRL
&& brw
->screen
->devinfo
.gen
>= 8) ||
110 (stage
== MESA_SHADER_TESS_EVAL
&&
111 !shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]);
113 if (lower_patch_vertices_in_to_uniform
)
114 brw_nir_lower_patch_vertices_in_to_uniform(nir
);
116 nir
= brw_preprocess_nir(brw
->screen
->compiler
, nir
);
118 if (stage
== MESA_SHADER_TESS_EVAL
&& !lower_patch_vertices_in_to_uniform
) {
119 assert(shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
]);
120 struct gl_linked_shader
*linked_tcs
=
121 shader_prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
];
122 uint32_t patch_vertices
= linked_tcs
->Program
->info
.tess
.tcs_vertices_out
;
123 nir_lower_tes_patch_vertices(nir
, patch_vertices
);
126 if (stage
== MESA_SHADER_FRAGMENT
) {
127 static const struct nir_lower_wpos_ytransform_options wpos_options
= {
128 .state_tokens
= {STATE_INTERNAL
, STATE_FB_WPOS_Y_TRANSFORM
, 0, 0, 0},
129 .fs_coord_pixel_center_integer
= 1,
130 .fs_coord_origin_upper_left
= 1,
133 bool progress
= false;
134 NIR_PASS(progress
, nir
, nir_lower_wpos_ytransform
, &wpos_options
);
136 _mesa_add_state_reference(prog
->Parameters
,
137 wpos_options
.state_tokens
);
141 NIR_PASS_V(nir
, brw_nir_lower_uniforms
, is_scalar
);
147 brw_shader_gather_info(nir_shader
*nir
, struct gl_program
*prog
)
149 nir_shader_gather_info(nir
, nir_shader_get_entrypoint(nir
));
151 /* Copy the info we just generated back into the gl_program */
152 const char *prog_name
= prog
->info
.name
;
153 const char *prog_label
= prog
->info
.label
;
154 prog
->info
= nir
->info
;
155 prog
->info
.name
= prog_name
;
156 prog
->info
.label
= prog_label
;
160 get_new_program_id(struct intel_screen
*screen
)
162 return p_atomic_inc_return(&screen
->program_id
);
165 static struct gl_program
*brwNewProgram(struct gl_context
*ctx
, GLenum target
,
166 GLuint id
, bool is_arb_asm
)
168 struct brw_context
*brw
= brw_context(ctx
);
169 struct brw_program
*prog
= rzalloc(NULL
, struct brw_program
);
172 prog
->id
= get_new_program_id(brw
->screen
);
174 return _mesa_init_gl_program(&prog
->program
, target
, id
, is_arb_asm
);
180 static void brwDeleteProgram( struct gl_context
*ctx
,
181 struct gl_program
*prog
)
183 struct brw_context
*brw
= brw_context(ctx
);
185 /* Beware! prog's refcount has reached zero, and it's about to be freed.
187 * In brw_upload_pipeline_state(), we compare brw->programs[i] to
188 * ctx->FooProgram._Current, and flag BRW_NEW_FOO_PROGRAM if the
189 * pointer has changed.
191 * We cannot leave brw->programs[i] as a dangling pointer to the dead
192 * program. malloc() may allocate the same memory for a new gl_program,
193 * causing us to see matching pointers...but totally different programs.
195 * We cannot set brw->programs[i] to NULL, either. If we've deleted the
196 * active program, Mesa may set ctx->FooProgram._Current to NULL. That
197 * would cause us to see matching pointers (NULL == NULL), and fail to
198 * detect that a program has changed since our last draw.
200 * So, set it to a bogus gl_program pointer that will never match,
201 * causing us to properly reevaluate the state on our next draw.
203 * Getting this wrong causes heisenbugs which are very hard to catch,
204 * as you need a very specific allocation pattern to hit the problem.
206 static const struct gl_program deleted_program
;
208 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
209 if (brw
->programs
[i
] == prog
)
210 brw
->programs
[i
] = (struct gl_program
*) &deleted_program
;
213 _mesa_delete_program( ctx
, prog
);
218 brwProgramStringNotify(struct gl_context
*ctx
,
220 struct gl_program
*prog
)
222 assert(target
== GL_VERTEX_PROGRAM_ARB
|| !prog
->arb
.IsPositionInvariant
);
224 struct brw_context
*brw
= brw_context(ctx
);
225 const struct brw_compiler
*compiler
= brw
->screen
->compiler
;
228 case GL_FRAGMENT_PROGRAM_ARB
: {
229 struct brw_program
*newFP
= brw_program(prog
);
230 const struct brw_program
*curFP
=
231 brw_program_const(brw
->programs
[MESA_SHADER_FRAGMENT
]);
234 brw
->ctx
.NewDriverState
|= BRW_NEW_FRAGMENT_PROGRAM
;
235 newFP
->id
= get_new_program_id(brw
->screen
);
237 prog
->nir
= brw_create_nir(brw
, NULL
, prog
, MESA_SHADER_FRAGMENT
, true);
239 brw_shader_gather_info(prog
->nir
, prog
);
241 brw_fs_precompile(ctx
, prog
);
244 case GL_VERTEX_PROGRAM_ARB
: {
245 struct brw_program
*newVP
= brw_program(prog
);
246 const struct brw_program
*curVP
=
247 brw_program_const(brw
->programs
[MESA_SHADER_VERTEX
]);
250 brw
->ctx
.NewDriverState
|= BRW_NEW_VERTEX_PROGRAM
;
251 if (newVP
->program
.arb
.IsPositionInvariant
) {
252 _mesa_insert_mvp_code(ctx
, &newVP
->program
);
254 newVP
->id
= get_new_program_id(brw
->screen
);
256 /* Also tell tnl about it:
258 _tnl_program_string(ctx
, target
, prog
);
260 prog
->nir
= brw_create_nir(brw
, NULL
, prog
, MESA_SHADER_VERTEX
,
261 compiler
->scalar_stage
[MESA_SHADER_VERTEX
]);
263 brw_shader_gather_info(prog
->nir
, prog
);
265 brw_vs_precompile(ctx
, prog
);
270 * driver->ProgramStringNotify is only called for ARB programs, fixed
271 * function vertex programs, and ir_to_mesa (which isn't used by the
272 * i965 back-end). Therefore, even after geometry shaders are added,
273 * this function should only ever be called with a target of
274 * GL_VERTEX_PROGRAM_ARB or GL_FRAGMENT_PROGRAM_ARB.
276 unreachable("Unexpected target in brwProgramStringNotify");
283 brw_memory_barrier(struct gl_context
*ctx
, GLbitfield barriers
)
285 struct brw_context
*brw
= brw_context(ctx
);
286 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
287 unsigned bits
= PIPE_CONTROL_DATA_CACHE_FLUSH
| PIPE_CONTROL_CS_STALL
;
288 assert(devinfo
->gen
>= 7 && devinfo
->gen
<= 11);
290 if (barriers
& (GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT
|
291 GL_ELEMENT_ARRAY_BARRIER_BIT
|
292 GL_COMMAND_BARRIER_BIT
))
293 bits
|= PIPE_CONTROL_VF_CACHE_INVALIDATE
;
295 if (barriers
& GL_UNIFORM_BARRIER_BIT
)
296 bits
|= (PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
|
297 PIPE_CONTROL_CONST_CACHE_INVALIDATE
);
299 if (barriers
& GL_TEXTURE_FETCH_BARRIER_BIT
)
300 bits
|= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
;
302 if (barriers
& (GL_TEXTURE_UPDATE_BARRIER_BIT
|
303 GL_PIXEL_BUFFER_BARRIER_BIT
))
304 bits
|= (PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
|
305 PIPE_CONTROL_RENDER_TARGET_FLUSH
);
307 if (barriers
& GL_FRAMEBUFFER_BARRIER_BIT
)
308 bits
|= (PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
|
309 PIPE_CONTROL_RENDER_TARGET_FLUSH
);
311 /* Typed surface messages are handled by the render cache on IVB, so we
312 * need to flush it too.
314 if (devinfo
->gen
== 7 && !devinfo
->is_haswell
)
315 bits
|= PIPE_CONTROL_RENDER_TARGET_FLUSH
;
317 brw_emit_pipe_control_flush(brw
, bits
);
321 brw_framebuffer_fetch_barrier(struct gl_context
*ctx
)
323 struct brw_context
*brw
= brw_context(ctx
);
324 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
326 if (!ctx
->Extensions
.EXT_shader_framebuffer_fetch
) {
327 if (devinfo
->gen
>= 6) {
328 brw_emit_pipe_control_flush(brw
,
329 PIPE_CONTROL_RENDER_TARGET_FLUSH
|
330 PIPE_CONTROL_CS_STALL
);
331 brw_emit_pipe_control_flush(brw
,
332 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
);
334 brw_emit_pipe_control_flush(brw
,
335 PIPE_CONTROL_RENDER_TARGET_FLUSH
);
341 brw_get_scratch_bo(struct brw_context
*brw
,
342 struct brw_bo
**scratch_bo
, int size
)
344 struct brw_bo
*old_bo
= *scratch_bo
;
346 if (old_bo
&& old_bo
->size
< size
) {
347 brw_bo_unreference(old_bo
);
353 brw_bo_alloc(brw
->bufmgr
, "scratch bo", size
, BRW_MEMZONE_SCRATCH
);
358 * Reserve enough scratch space for the given stage to hold \p per_thread_size
359 * bytes times the given \p thread_count.
362 brw_alloc_stage_scratch(struct brw_context
*brw
,
363 struct brw_stage_state
*stage_state
,
364 unsigned per_thread_size
)
366 if (stage_state
->per_thread_scratch
>= per_thread_size
)
369 stage_state
->per_thread_scratch
= per_thread_size
;
371 if (stage_state
->scratch_bo
)
372 brw_bo_unreference(stage_state
->scratch_bo
);
374 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
375 unsigned thread_count
;
376 switch(stage_state
->stage
) {
377 case MESA_SHADER_VERTEX
:
378 thread_count
= devinfo
->max_vs_threads
;
380 case MESA_SHADER_TESS_CTRL
:
381 thread_count
= devinfo
->max_tcs_threads
;
383 case MESA_SHADER_TESS_EVAL
:
384 thread_count
= devinfo
->max_tes_threads
;
386 case MESA_SHADER_GEOMETRY
:
387 thread_count
= devinfo
->max_gs_threads
;
389 case MESA_SHADER_FRAGMENT
:
390 thread_count
= devinfo
->max_wm_threads
;
392 case MESA_SHADER_COMPUTE
: {
393 unsigned subslices
= MAX2(brw
->screen
->subslice_total
, 1);
395 /* The documentation for 3DSTATE_PS "Scratch Space Base Pointer" says:
397 * "Scratch Space per slice is computed based on 4 sub-slices. SW must
398 * allocate scratch space enough so that each slice has 4 slices
401 * According to the other driver team, this applies to compute shaders
402 * as well. This is not currently documented at all.
404 * brw->screen->subslice_total is the TOTAL number of subslices
405 * and we wish to view that there are 4 subslices per slice
406 * instead of the actual number of subslices per slice.
408 if (devinfo
->gen
>= 9)
409 subslices
= 4 * brw
->screen
->devinfo
.num_slices
;
411 unsigned scratch_ids_per_subslice
;
412 if (devinfo
->is_haswell
) {
413 /* WaCSScratchSize:hsw
415 * Haswell's scratch space address calculation appears to be sparse
416 * rather than tightly packed. The Thread ID has bits indicating
417 * which subslice, EU within a subslice, and thread within an EU it
418 * is. There's a maximum of two slices and two subslices, so these
419 * can be stored with a single bit. Even though there are only 10 EUs
420 * per subslice, this is stored in 4 bits, so there's an effective
421 * maximum value of 16 EUs. Similarly, although there are only 7
422 * threads per EU, this is stored in a 3 bit number, giving an
423 * effective maximum value of 8 threads per EU.
425 * This means that we need to use 16 * 8 instead of 10 * 7 for the
426 * number of threads per subslice.
428 scratch_ids_per_subslice
= 16 * 8;
429 } else if (devinfo
->is_cherryview
) {
430 /* Cherryview devices have either 6 or 8 EUs per subslice, and each
431 * EU has 7 threads. The 6 EU devices appear to calculate thread IDs
432 * as if it had 8 EUs.
434 scratch_ids_per_subslice
= 8 * 7;
436 scratch_ids_per_subslice
= devinfo
->max_cs_threads
;
439 thread_count
= scratch_ids_per_subslice
* subslices
;
443 unreachable("Unsupported stage!");
446 stage_state
->scratch_bo
=
447 brw_bo_alloc(brw
->bufmgr
, "shader scratch space",
448 per_thread_size
* thread_count
, BRW_MEMZONE_SCRATCH
);
451 void brwInitFragProgFuncs( struct dd_function_table
*functions
)
453 assert(functions
->ProgramStringNotify
== _tnl_program_string
);
455 functions
->NewProgram
= brwNewProgram
;
456 functions
->DeleteProgram
= brwDeleteProgram
;
457 functions
->ProgramStringNotify
= brwProgramStringNotify
;
459 functions
->LinkShader
= brw_link_shader
;
461 functions
->MemoryBarrier
= brw_memory_barrier
;
462 functions
->FramebufferFetchBarrier
= brw_framebuffer_fetch_barrier
;
465 struct shader_times
{
472 brw_init_shader_time(struct brw_context
*brw
)
474 const int max_entries
= 2048;
475 brw
->shader_time
.bo
=
476 brw_bo_alloc(brw
->bufmgr
, "shader time",
477 max_entries
* BRW_SHADER_TIME_STRIDE
* 3,
479 brw
->shader_time
.names
= rzalloc_array(brw
, const char *, max_entries
);
480 brw
->shader_time
.ids
= rzalloc_array(brw
, int, max_entries
);
481 brw
->shader_time
.types
= rzalloc_array(brw
, enum shader_time_shader_type
,
483 brw
->shader_time
.cumulative
= rzalloc_array(brw
, struct shader_times
,
485 brw
->shader_time
.max_entries
= max_entries
;
489 compare_time(const void *a
, const void *b
)
491 uint64_t * const *a_val
= a
;
492 uint64_t * const *b_val
= b
;
494 /* We don't just subtract because we're turning the value to an int. */
495 if (**a_val
< **b_val
)
497 else if (**a_val
== **b_val
)
504 print_shader_time_line(const char *stage
, const char *name
,
505 int shader_num
, uint64_t time
, uint64_t total
)
507 fprintf(stderr
, "%-6s%-18s", stage
, name
);
510 fprintf(stderr
, "%4d: ", shader_num
);
512 fprintf(stderr
, " : ");
514 fprintf(stderr
, "%16lld (%7.2f Gcycles) %4.1f%%\n",
516 (double)time
/ 1000000000.0,
517 (double)time
/ total
* 100.0);
521 brw_report_shader_time(struct brw_context
*brw
)
523 if (!brw
->shader_time
.bo
|| !brw
->shader_time
.num_entries
)
526 uint64_t scaled
[brw
->shader_time
.num_entries
];
527 uint64_t *sorted
[brw
->shader_time
.num_entries
];
528 uint64_t total_by_type
[ST_CS
+ 1];
529 memset(total_by_type
, 0, sizeof(total_by_type
));
531 for (int i
= 0; i
< brw
->shader_time
.num_entries
; i
++) {
532 uint64_t written
= 0, reset
= 0;
533 enum shader_time_shader_type type
= brw
->shader_time
.types
[i
];
535 sorted
[i
] = &scaled
[i
];
546 written
= brw
->shader_time
.cumulative
[i
].written
;
547 reset
= brw
->shader_time
.cumulative
[i
].reset
;
551 /* I sometimes want to print things that aren't the 3 shader times.
552 * Just print the sum in that case.
559 uint64_t time
= brw
->shader_time
.cumulative
[i
].time
;
561 scaled
[i
] = time
/ written
* (written
+ reset
);
575 total_by_type
[type
] += scaled
[i
];
585 fprintf(stderr
, "No shader time collected yet\n");
589 qsort(sorted
, brw
->shader_time
.num_entries
, sizeof(sorted
[0]), compare_time
);
591 fprintf(stderr
, "\n");
592 fprintf(stderr
, "type ID cycles spent %% of total\n");
593 for (int s
= 0; s
< brw
->shader_time
.num_entries
; s
++) {
595 /* Work back from the sorted pointers times to a time to print. */
596 int i
= sorted
[s
] - scaled
;
601 int shader_num
= brw
->shader_time
.ids
[i
];
602 const char *shader_name
= brw
->shader_time
.names
[i
];
604 switch (brw
->shader_time
.types
[i
]) {
634 print_shader_time_line(stage
, shader_name
, shader_num
,
638 fprintf(stderr
, "\n");
639 print_shader_time_line("total", "vs", 0, total_by_type
[ST_VS
], total
);
640 print_shader_time_line("total", "tcs", 0, total_by_type
[ST_TCS
], total
);
641 print_shader_time_line("total", "tes", 0, total_by_type
[ST_TES
], total
);
642 print_shader_time_line("total", "gs", 0, total_by_type
[ST_GS
], total
);
643 print_shader_time_line("total", "fs8", 0, total_by_type
[ST_FS8
], total
);
644 print_shader_time_line("total", "fs16", 0, total_by_type
[ST_FS16
], total
);
645 print_shader_time_line("total", "fs32", 0, total_by_type
[ST_FS32
], total
);
646 print_shader_time_line("total", "cs", 0, total_by_type
[ST_CS
], total
);
650 brw_collect_shader_time(struct brw_context
*brw
)
652 if (!brw
->shader_time
.bo
)
655 /* This probably stalls on the last rendering. We could fix that by
656 * delaying reading the reports, but it doesn't look like it's a big
657 * overhead compared to the cost of tracking the time in the first place.
659 void *bo_map
= brw_bo_map(brw
, brw
->shader_time
.bo
, MAP_READ
| MAP_WRITE
);
661 for (int i
= 0; i
< brw
->shader_time
.num_entries
; i
++) {
662 uint32_t *times
= bo_map
+ i
* 3 * BRW_SHADER_TIME_STRIDE
;
664 brw
->shader_time
.cumulative
[i
].time
+= times
[BRW_SHADER_TIME_STRIDE
* 0 / 4];
665 brw
->shader_time
.cumulative
[i
].written
+= times
[BRW_SHADER_TIME_STRIDE
* 1 / 4];
666 brw
->shader_time
.cumulative
[i
].reset
+= times
[BRW_SHADER_TIME_STRIDE
* 2 / 4];
669 /* Zero the BO out to clear it out for our next collection.
671 memset(bo_map
, 0, brw
->shader_time
.bo
->size
);
672 brw_bo_unmap(brw
->shader_time
.bo
);
676 brw_collect_and_report_shader_time(struct brw_context
*brw
)
678 brw_collect_shader_time(brw
);
680 if (brw
->shader_time
.report_time
== 0 ||
681 get_time() - brw
->shader_time
.report_time
>= 1.0) {
682 brw_report_shader_time(brw
);
683 brw
->shader_time
.report_time
= get_time();
688 * Chooses an index in the shader_time buffer and sets up tracking information
691 * Note that this holds on to references to the underlying programs, which may
692 * change their lifetimes compared to normal operation.
695 brw_get_shader_time_index(struct brw_context
*brw
, struct gl_program
*prog
,
696 enum shader_time_shader_type type
, bool is_glsl_sh
)
698 int shader_time_index
= brw
->shader_time
.num_entries
++;
699 assert(shader_time_index
< brw
->shader_time
.max_entries
);
700 brw
->shader_time
.types
[shader_time_index
] = type
;
705 } else if (is_glsl_sh
) {
706 name
= prog
->info
.label
?
707 ralloc_strdup(brw
->shader_time
.names
, prog
->info
.label
) : "glsl";
712 brw
->shader_time
.names
[shader_time_index
] = name
;
713 brw
->shader_time
.ids
[shader_time_index
] = prog
->Id
;
715 return shader_time_index
;
719 brw_destroy_shader_time(struct brw_context
*brw
)
721 brw_bo_unreference(brw
->shader_time
.bo
);
722 brw
->shader_time
.bo
= NULL
;
726 brw_stage_prog_data_free(const void *p
)
728 struct brw_stage_prog_data
*prog_data
= (struct brw_stage_prog_data
*)p
;
730 ralloc_free(prog_data
->param
);
731 ralloc_free(prog_data
->pull_param
);
735 brw_dump_arb_asm(const char *stage
, struct gl_program
*prog
)
737 fprintf(stderr
, "ARB_%s_program %d ir for native %s shader\n",
738 stage
, prog
->Id
, stage
);
739 _mesa_print_program(prog
);
743 brw_setup_tex_for_precompile(struct brw_context
*brw
,
744 struct brw_sampler_prog_key_data
*tex
,
745 struct gl_program
*prog
)
747 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
748 const bool has_shader_channel_select
= devinfo
->is_haswell
|| devinfo
->gen
>= 8;
749 unsigned sampler_count
= util_last_bit(prog
->SamplersUsed
);
750 for (unsigned i
= 0; i
< sampler_count
; i
++) {
751 if (!has_shader_channel_select
&& (prog
->ShadowSamplers
& (1 << i
))) {
752 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
754 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
756 /* Color sampler: assume no swizzling. */
757 tex
->swizzles
[i
] = SWIZZLE_XYZW
;
763 * Sets up the starting offsets for the groups of binding table entries
764 * common to all pipeline stages.
766 * Unused groups are initialized to 0xd0d0d0d0 to make it obvious that they're
767 * unused but also make sure that addition of small offsets to them will
768 * trigger some of our asserts that surface indices are < BRW_MAX_SURFACES.
771 brw_assign_common_binding_table_offsets(const struct gen_device_info
*devinfo
,
772 const struct gl_program
*prog
,
773 struct brw_stage_prog_data
*stage_prog_data
,
774 uint32_t next_binding_table_offset
)
776 int num_textures
= util_last_bit(prog
->SamplersUsed
);
778 stage_prog_data
->binding_table
.texture_start
= next_binding_table_offset
;
779 next_binding_table_offset
+= num_textures
;
781 if (prog
->info
.num_ubos
) {
782 assert(prog
->info
.num_ubos
<= BRW_MAX_UBO
);
783 stage_prog_data
->binding_table
.ubo_start
= next_binding_table_offset
;
784 next_binding_table_offset
+= prog
->info
.num_ubos
;
786 stage_prog_data
->binding_table
.ubo_start
= 0xd0d0d0d0;
789 if (prog
->info
.num_ssbos
|| prog
->info
.num_abos
) {
790 assert(prog
->info
.num_abos
<= BRW_MAX_ABO
);
791 assert(prog
->info
.num_ssbos
<= BRW_MAX_SSBO
);
792 stage_prog_data
->binding_table
.ssbo_start
= next_binding_table_offset
;
793 next_binding_table_offset
+= prog
->info
.num_abos
+ prog
->info
.num_ssbos
;
795 stage_prog_data
->binding_table
.ssbo_start
= 0xd0d0d0d0;
798 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
799 stage_prog_data
->binding_table
.shader_time_start
= next_binding_table_offset
;
800 next_binding_table_offset
++;
802 stage_prog_data
->binding_table
.shader_time_start
= 0xd0d0d0d0;
805 if (prog
->info
.uses_texture_gather
) {
806 if (devinfo
->gen
>= 8) {
807 stage_prog_data
->binding_table
.gather_texture_start
=
808 stage_prog_data
->binding_table
.texture_start
;
810 stage_prog_data
->binding_table
.gather_texture_start
= next_binding_table_offset
;
811 next_binding_table_offset
+= num_textures
;
814 stage_prog_data
->binding_table
.gather_texture_start
= 0xd0d0d0d0;
817 if (prog
->info
.num_images
) {
818 stage_prog_data
->binding_table
.image_start
= next_binding_table_offset
;
819 next_binding_table_offset
+= prog
->info
.num_images
;
821 stage_prog_data
->binding_table
.image_start
= 0xd0d0d0d0;
824 /* This may or may not be used depending on how the compile goes. */
825 stage_prog_data
->binding_table
.pull_constants_start
= next_binding_table_offset
;
826 next_binding_table_offset
++;
828 /* Plane 0 is just the regular texture section */
829 stage_prog_data
->binding_table
.plane_start
[0] = stage_prog_data
->binding_table
.texture_start
;
831 stage_prog_data
->binding_table
.plane_start
[1] = next_binding_table_offset
;
832 next_binding_table_offset
+= num_textures
;
834 stage_prog_data
->binding_table
.plane_start
[2] = next_binding_table_offset
;
835 next_binding_table_offset
+= num_textures
;
837 /* prog_data->base.binding_table.size will be set by brw_mark_surface_used. */
839 assert(next_binding_table_offset
<= BRW_MAX_SURFACES
);
840 return next_binding_table_offset
;
844 brw_program_serialize_nir(struct gl_context
*ctx
, struct gl_program
*prog
)
846 if (prog
->driver_cache_blob
)
851 nir_serialize(&writer
, prog
->nir
);
852 prog
->driver_cache_blob
= ralloc_size(NULL
, writer
.size
);
853 memcpy(prog
->driver_cache_blob
, writer
.data
, writer
.size
);
854 prog
->driver_cache_blob_size
= writer
.size
;
855 blob_finish(&writer
);
859 brw_program_deserialize_nir(struct gl_context
*ctx
, struct gl_program
*prog
,
860 gl_shader_stage stage
)
863 assert(prog
->driver_cache_blob
&& prog
->driver_cache_blob_size
> 0);
864 const struct nir_shader_compiler_options
*options
=
865 ctx
->Const
.ShaderCompilerOptions
[stage
].NirOptions
;
866 struct blob_reader reader
;
867 blob_reader_init(&reader
, prog
->driver_cache_blob
,
868 prog
->driver_cache_blob_size
);
869 prog
->nir
= nir_deserialize(NULL
, options
, &reader
);
872 if (prog
->driver_cache_blob
) {
873 ralloc_free(prog
->driver_cache_blob
);
874 prog
->driver_cache_blob
= NULL
;
875 prog
->driver_cache_blob_size
= 0;