st/mesa: factor ucp-lowering logic into helper
[mesa.git] / src / mesa / state_tracker / st_program.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 * Brian Paul
31 */
32
33
34 #include "main/errors.h"
35
36 #include "main/hash.h"
37 #include "main/mtypes.h"
38 #include "program/prog_parameter.h"
39 #include "program/prog_print.h"
40 #include "program/prog_to_nir.h"
41 #include "program/programopt.h"
42
43 #include "compiler/nir/nir.h"
44 #include "compiler/nir/nir_serialize.h"
45 #include "draw/draw_context.h"
46
47 #include "pipe/p_context.h"
48 #include "pipe/p_defines.h"
49 #include "pipe/p_shader_tokens.h"
50 #include "draw/draw_context.h"
51 #include "tgsi/tgsi_dump.h"
52 #include "tgsi/tgsi_emulate.h"
53 #include "tgsi/tgsi_parse.h"
54 #include "tgsi/tgsi_ureg.h"
55
56 #include "util/u_memory.h"
57
58 #include "st_debug.h"
59 #include "st_cb_bitmap.h"
60 #include "st_cb_drawpixels.h"
61 #include "st_context.h"
62 #include "st_tgsi_lower_depth_clamp.h"
63 #include "st_tgsi_lower_yuv.h"
64 #include "st_program.h"
65 #include "st_mesa_to_tgsi.h"
66 #include "st_atifs_to_tgsi.h"
67 #include "st_nir.h"
68 #include "st_shader_cache.h"
69 #include "st_util.h"
70 #include "cso_cache/cso_context.h"
71
72
73 static void
74 destroy_program_variants(struct st_context *st, struct gl_program *target);
75
76 static void
77 set_affected_state_flags(uint64_t *states,
78 struct gl_program *prog,
79 uint64_t new_constants,
80 uint64_t new_sampler_views,
81 uint64_t new_samplers,
82 uint64_t new_images,
83 uint64_t new_ubos,
84 uint64_t new_ssbos,
85 uint64_t new_atomics)
86 {
87 if (prog->Parameters->NumParameters)
88 *states |= new_constants;
89
90 if (prog->info.num_textures)
91 *states |= new_sampler_views | new_samplers;
92
93 if (prog->info.num_images)
94 *states |= new_images;
95
96 if (prog->info.num_ubos)
97 *states |= new_ubos;
98
99 if (prog->info.num_ssbos)
100 *states |= new_ssbos;
101
102 if (prog->info.num_abos)
103 *states |= new_atomics;
104 }
105
106 /**
107 * This determines which states will be updated when the shader is bound.
108 */
109 void
110 st_set_prog_affected_state_flags(struct gl_program *prog)
111 {
112 uint64_t *states;
113
114 switch (prog->info.stage) {
115 case MESA_SHADER_VERTEX:
116 states = &((struct st_program*)prog)->affected_states;
117
118 *states = ST_NEW_VS_STATE |
119 ST_NEW_RASTERIZER |
120 ST_NEW_VERTEX_ARRAYS;
121
122 set_affected_state_flags(states, prog,
123 ST_NEW_VS_CONSTANTS,
124 ST_NEW_VS_SAMPLER_VIEWS,
125 ST_NEW_VS_SAMPLERS,
126 ST_NEW_VS_IMAGES,
127 ST_NEW_VS_UBOS,
128 ST_NEW_VS_SSBOS,
129 ST_NEW_VS_ATOMICS);
130 break;
131
132 case MESA_SHADER_TESS_CTRL:
133 states = &(st_program(prog))->affected_states;
134
135 *states = ST_NEW_TCS_STATE;
136
137 set_affected_state_flags(states, prog,
138 ST_NEW_TCS_CONSTANTS,
139 ST_NEW_TCS_SAMPLER_VIEWS,
140 ST_NEW_TCS_SAMPLERS,
141 ST_NEW_TCS_IMAGES,
142 ST_NEW_TCS_UBOS,
143 ST_NEW_TCS_SSBOS,
144 ST_NEW_TCS_ATOMICS);
145 break;
146
147 case MESA_SHADER_TESS_EVAL:
148 states = &(st_program(prog))->affected_states;
149
150 *states = ST_NEW_TES_STATE |
151 ST_NEW_RASTERIZER;
152
153 set_affected_state_flags(states, prog,
154 ST_NEW_TES_CONSTANTS,
155 ST_NEW_TES_SAMPLER_VIEWS,
156 ST_NEW_TES_SAMPLERS,
157 ST_NEW_TES_IMAGES,
158 ST_NEW_TES_UBOS,
159 ST_NEW_TES_SSBOS,
160 ST_NEW_TES_ATOMICS);
161 break;
162
163 case MESA_SHADER_GEOMETRY:
164 states = &(st_program(prog))->affected_states;
165
166 *states = ST_NEW_GS_STATE |
167 ST_NEW_RASTERIZER;
168
169 set_affected_state_flags(states, prog,
170 ST_NEW_GS_CONSTANTS,
171 ST_NEW_GS_SAMPLER_VIEWS,
172 ST_NEW_GS_SAMPLERS,
173 ST_NEW_GS_IMAGES,
174 ST_NEW_GS_UBOS,
175 ST_NEW_GS_SSBOS,
176 ST_NEW_GS_ATOMICS);
177 break;
178
179 case MESA_SHADER_FRAGMENT:
180 states = &((struct st_program*)prog)->affected_states;
181
182 /* gl_FragCoord and glDrawPixels always use constants. */
183 *states = ST_NEW_FS_STATE |
184 ST_NEW_SAMPLE_SHADING |
185 ST_NEW_FS_CONSTANTS;
186
187 set_affected_state_flags(states, prog,
188 ST_NEW_FS_CONSTANTS,
189 ST_NEW_FS_SAMPLER_VIEWS,
190 ST_NEW_FS_SAMPLERS,
191 ST_NEW_FS_IMAGES,
192 ST_NEW_FS_UBOS,
193 ST_NEW_FS_SSBOS,
194 ST_NEW_FS_ATOMICS);
195 break;
196
197 case MESA_SHADER_COMPUTE:
198 states = &((struct st_program*)prog)->affected_states;
199
200 *states = ST_NEW_CS_STATE;
201
202 set_affected_state_flags(states, prog,
203 ST_NEW_CS_CONSTANTS,
204 ST_NEW_CS_SAMPLER_VIEWS,
205 ST_NEW_CS_SAMPLERS,
206 ST_NEW_CS_IMAGES,
207 ST_NEW_CS_UBOS,
208 ST_NEW_CS_SSBOS,
209 ST_NEW_CS_ATOMICS);
210 break;
211
212 default:
213 unreachable("unhandled shader stage");
214 }
215 }
216
217
218 /**
219 * Delete a shader variant. Note the caller must unlink the variant from
220 * the linked list.
221 */
222 static void
223 delete_variant(struct st_context *st, struct st_variant *v, GLenum target)
224 {
225 if (v->driver_shader) {
226 if (target == GL_VERTEX_PROGRAM_ARB &&
227 ((struct st_common_variant*)v)->key.is_draw_shader) {
228 /* Draw shader. */
229 draw_delete_vertex_shader(st->draw, v->driver_shader);
230 } else if (st->has_shareable_shaders || v->st == st) {
231 /* The shader's context matches the calling context, or we
232 * don't care.
233 */
234 switch (target) {
235 case GL_VERTEX_PROGRAM_ARB:
236 st->pipe->delete_vs_state(st->pipe, v->driver_shader);
237 break;
238 case GL_TESS_CONTROL_PROGRAM_NV:
239 st->pipe->delete_tcs_state(st->pipe, v->driver_shader);
240 break;
241 case GL_TESS_EVALUATION_PROGRAM_NV:
242 st->pipe->delete_tes_state(st->pipe, v->driver_shader);
243 break;
244 case GL_GEOMETRY_PROGRAM_NV:
245 st->pipe->delete_gs_state(st->pipe, v->driver_shader);
246 break;
247 case GL_FRAGMENT_PROGRAM_ARB:
248 st->pipe->delete_fs_state(st->pipe, v->driver_shader);
249 break;
250 case GL_COMPUTE_PROGRAM_NV:
251 st->pipe->delete_compute_state(st->pipe, v->driver_shader);
252 break;
253 default:
254 unreachable("bad shader type in delete_basic_variant");
255 }
256 } else {
257 /* We can't delete a shader with a context different from the one
258 * that created it. Add it to the creating context's zombie list.
259 */
260 enum pipe_shader_type type =
261 pipe_shader_type_from_mesa(_mesa_program_enum_to_shader_stage(target));
262
263 st_save_zombie_shader(v->st, type, v->driver_shader);
264 }
265 }
266
267 free(v);
268 }
269
270 static void
271 st_unbind_program(struct st_context *st, struct st_program *p)
272 {
273 /* Unbind the shader in cso_context and re-bind in st/mesa. */
274 switch (p->Base.info.stage) {
275 case MESA_SHADER_VERTEX:
276 cso_set_vertex_shader_handle(st->cso_context, NULL);
277 st->dirty |= ST_NEW_VS_STATE;
278 break;
279 case MESA_SHADER_TESS_CTRL:
280 cso_set_tessctrl_shader_handle(st->cso_context, NULL);
281 st->dirty |= ST_NEW_TCS_STATE;
282 break;
283 case MESA_SHADER_TESS_EVAL:
284 cso_set_tesseval_shader_handle(st->cso_context, NULL);
285 st->dirty |= ST_NEW_TES_STATE;
286 break;
287 case MESA_SHADER_GEOMETRY:
288 cso_set_geometry_shader_handle(st->cso_context, NULL);
289 st->dirty |= ST_NEW_GS_STATE;
290 break;
291 case MESA_SHADER_FRAGMENT:
292 cso_set_fragment_shader_handle(st->cso_context, NULL);
293 st->dirty |= ST_NEW_FS_STATE;
294 break;
295 case MESA_SHADER_COMPUTE:
296 cso_set_compute_shader_handle(st->cso_context, NULL);
297 st->dirty |= ST_NEW_CS_STATE;
298 break;
299 default:
300 unreachable("invalid shader type");
301 }
302 }
303
304 /**
305 * Free all basic program variants.
306 */
307 void
308 st_release_variants(struct st_context *st, struct st_program *p)
309 {
310 struct st_variant *v;
311
312 /* If we are releasing shaders, re-bind them, because we don't
313 * know which shaders are bound in the driver.
314 */
315 if (p->variants)
316 st_unbind_program(st, p);
317
318 for (v = p->variants; v; ) {
319 struct st_variant *next = v->next;
320 delete_variant(st, v, p->Base.Target);
321 v = next;
322 }
323
324 p->variants = NULL;
325
326 if (p->state.tokens) {
327 ureg_free_tokens(p->state.tokens);
328 p->state.tokens = NULL;
329 }
330
331 /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
332 * it has resulted in the driver taking ownership of the NIR. Those
333 * callers should be NULLing out the nir field in any pipe_shader_state
334 * that might have this called in order to indicate that.
335 *
336 * GLSL IR and ARB programs will have set gl_program->nir to the same
337 * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
338 */
339 }
340
341 /**
342 * Free all basic program variants and unref program.
343 */
344 void
345 st_release_program(struct st_context *st, struct st_program **p)
346 {
347 if (!*p)
348 return;
349
350 destroy_program_variants(st, &((*p)->Base));
351 st_reference_prog(st, p, NULL);
352 }
353
354 void
355 st_finalize_nir_before_variants(struct nir_shader *nir)
356 {
357 NIR_PASS_V(nir, nir_opt_access);
358
359 NIR_PASS_V(nir, nir_split_var_copies);
360 NIR_PASS_V(nir, nir_lower_var_copies);
361 if (nir->options->lower_all_io_to_temps ||
362 nir->options->lower_all_io_to_elements ||
363 nir->info.stage == MESA_SHADER_VERTEX ||
364 nir->info.stage == MESA_SHADER_GEOMETRY) {
365 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
366 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
367 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, true);
368 }
369
370 st_nir_assign_vs_in_locations(nir);
371 }
372
373 /**
374 * Translate ARB (asm) program to NIR
375 */
376 static nir_shader *
377 st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog,
378 gl_shader_stage stage)
379 {
380 struct pipe_screen *screen = st->pipe->screen;
381 const struct gl_shader_compiler_options *options =
382 &st->ctx->Const.ShaderCompilerOptions[stage];
383
384 /* Translate to NIR */
385 nir_shader *nir = prog_to_nir(prog, options->NirOptions);
386 NIR_PASS_V(nir, nir_lower_regs_to_ssa); /* turn registers into SSA */
387 nir_validate_shader(nir, "after st/ptn lower_regs_to_ssa");
388
389 NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, prog, screen);
390 NIR_PASS_V(nir, nir_lower_system_values);
391
392 /* Optimise NIR */
393 NIR_PASS_V(nir, nir_opt_constant_folding);
394 st_nir_opts(nir);
395 st_finalize_nir_before_variants(nir);
396
397 if (st->allow_st_finalize_nir_twice)
398 st_finalize_nir(st, prog, NULL, nir, true);
399
400 nir_validate_shader(nir, "after st/glsl finalize_nir");
401
402 return nir;
403 }
404
405 void
406 st_prepare_vertex_program(struct st_program *stp)
407 {
408 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
409
410 stvp->num_inputs = 0;
411 memset(stvp->input_to_index, ~0, sizeof(stvp->input_to_index));
412 memset(stvp->result_to_output, ~0, sizeof(stvp->result_to_output));
413
414 /* Determine number of inputs, the mappings between VERT_ATTRIB_x
415 * and TGSI generic input indexes, plus input attrib semantic info.
416 */
417 for (unsigned attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
418 if ((stp->Base.info.inputs_read & BITFIELD64_BIT(attr)) != 0) {
419 stvp->input_to_index[attr] = stvp->num_inputs;
420 stvp->index_to_input[stvp->num_inputs] = attr;
421 stvp->num_inputs++;
422
423 if ((stp->Base.DualSlotInputs & BITFIELD64_BIT(attr)) != 0) {
424 /* add placeholder for second part of a double attribute */
425 stvp->index_to_input[stvp->num_inputs] = ST_DOUBLE_ATTRIB_PLACEHOLDER;
426 stvp->num_inputs++;
427 }
428 }
429 }
430 /* pre-setup potentially unused edgeflag input */
431 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
432 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
433
434 /* Compute mapping of vertex program outputs to slots. */
435 unsigned num_outputs = 0;
436 for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
437 if (stp->Base.info.outputs_written & BITFIELD64_BIT(attr))
438 stvp->result_to_output[attr] = num_outputs++;
439 }
440 /* pre-setup potentially unused edgeflag output */
441 stvp->result_to_output[VARYING_SLOT_EDGE] = num_outputs;
442 }
443
444 void
445 st_translate_stream_output_info(struct gl_program *prog)
446 {
447 struct gl_transform_feedback_info *info = prog->sh.LinkedTransformFeedback;
448 if (!info)
449 return;
450
451 /* Determine the (default) output register mapping for each output. */
452 unsigned num_outputs = 0;
453 ubyte output_mapping[VARYING_SLOT_TESS_MAX];
454 memset(output_mapping, 0, sizeof(output_mapping));
455
456 for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
457 if (prog->info.outputs_written & BITFIELD64_BIT(attr))
458 output_mapping[attr] = num_outputs++;
459 }
460
461 /* Translate stream output info. */
462 struct pipe_stream_output_info *so_info =
463 &((struct st_program*)prog)->state.stream_output;
464
465 for (unsigned i = 0; i < info->NumOutputs; i++) {
466 so_info->output[i].register_index =
467 output_mapping[info->Outputs[i].OutputRegister];
468 so_info->output[i].start_component = info->Outputs[i].ComponentOffset;
469 so_info->output[i].num_components = info->Outputs[i].NumComponents;
470 so_info->output[i].output_buffer = info->Outputs[i].OutputBuffer;
471 so_info->output[i].dst_offset = info->Outputs[i].DstOffset;
472 so_info->output[i].stream = info->Outputs[i].StreamId;
473 }
474
475 for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
476 so_info->stride[i] = info->Buffers[i].Stride;
477 }
478 so_info->num_outputs = info->NumOutputs;
479 }
480
481 /**
482 * Translate a vertex program.
483 */
484 bool
485 st_translate_vertex_program(struct st_context *st,
486 struct st_program *stp)
487 {
488 struct ureg_program *ureg;
489 enum pipe_error error;
490 unsigned num_outputs = 0;
491 unsigned attr;
492 ubyte output_semantic_name[VARYING_SLOT_MAX] = {0};
493 ubyte output_semantic_index[VARYING_SLOT_MAX] = {0};
494
495 if (stp->Base.arb.IsPositionInvariant)
496 _mesa_insert_mvp_code(st->ctx, &stp->Base);
497
498 /* ARB_vp: */
499 if (!stp->glsl_to_tgsi) {
500 _mesa_remove_output_reads(&stp->Base, PROGRAM_OUTPUT);
501
502 /* This determines which states will be updated when the assembly
503 * shader is bound.
504 */
505 stp->affected_states = ST_NEW_VS_STATE |
506 ST_NEW_RASTERIZER |
507 ST_NEW_VERTEX_ARRAYS;
508
509 if (stp->Base.Parameters->NumParameters)
510 stp->affected_states |= ST_NEW_VS_CONSTANTS;
511
512 /* Translate to NIR if preferred. */
513 if (st->pipe->screen->get_shader_param(st->pipe->screen,
514 PIPE_SHADER_VERTEX,
515 PIPE_SHADER_CAP_PREFERRED_IR)) {
516 assert(!stp->glsl_to_tgsi);
517
518 if (stp->Base.nir)
519 ralloc_free(stp->Base.nir);
520
521 if (stp->serialized_nir) {
522 free(stp->serialized_nir);
523 stp->serialized_nir = NULL;
524 }
525
526 stp->state.type = PIPE_SHADER_IR_NIR;
527 stp->Base.nir = st_translate_prog_to_nir(st, &stp->Base,
528 MESA_SHADER_VERTEX);
529
530 /* We must update stp->Base.info after translation and before
531 * st_prepare_vertex_program is called, because inputs_read
532 * may become outdated after NIR optimization passes.
533 *
534 * For ffvp/ARB_vp inputs_read is populated based
535 * on declared attributes without taking their usage into
536 * consideration. When creating shader variants we expect
537 * that their inputs_read would match the base ones for
538 * input mapping to work properly.
539 */
540 nir_shader_gather_info(stp->Base.nir,
541 nir_shader_get_entrypoint(stp->Base.nir));
542 st_nir_assign_vs_in_locations(stp->Base.nir);
543 stp->Base.info = stp->Base.nir->info;
544
545 /* For st_draw_feedback, we need to generate TGSI too if draw doesn't
546 * use LLVM.
547 */
548 if (draw_has_llvm()) {
549 st_prepare_vertex_program(stp);
550 return true;
551 }
552 }
553 }
554
555 st_prepare_vertex_program(stp);
556
557 /* Get semantic names and indices. */
558 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
559 if (stp->Base.info.outputs_written & BITFIELD64_BIT(attr)) {
560 unsigned slot = num_outputs++;
561 unsigned semantic_name, semantic_index;
562 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
563 &semantic_name, &semantic_index);
564 output_semantic_name[slot] = semantic_name;
565 output_semantic_index[slot] = semantic_index;
566 }
567 }
568 /* pre-setup potentially unused edgeflag output */
569 output_semantic_name[num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
570 output_semantic_index[num_outputs] = 0;
571
572 ureg = ureg_create_with_screen(PIPE_SHADER_VERTEX, st->pipe->screen);
573 if (ureg == NULL)
574 return false;
575
576 if (stp->Base.info.clip_distance_array_size)
577 ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
578 stp->Base.info.clip_distance_array_size);
579 if (stp->Base.info.cull_distance_array_size)
580 ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
581 stp->Base.info.cull_distance_array_size);
582
583 if (ST_DEBUG & DEBUG_MESA) {
584 _mesa_print_program(&stp->Base);
585 _mesa_print_program_parameters(st->ctx, &stp->Base);
586 debug_printf("\n");
587 }
588
589 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
590
591 if (stp->glsl_to_tgsi) {
592 error = st_translate_program(st->ctx,
593 PIPE_SHADER_VERTEX,
594 ureg,
595 stp->glsl_to_tgsi,
596 &stp->Base,
597 /* inputs */
598 stvp->num_inputs,
599 stvp->input_to_index,
600 NULL, /* inputSlotToAttr */
601 NULL, /* input semantic name */
602 NULL, /* input semantic index */
603 NULL, /* interp mode */
604 /* outputs */
605 num_outputs,
606 stvp->result_to_output,
607 output_semantic_name,
608 output_semantic_index);
609
610 st_translate_stream_output_info(&stp->Base);
611
612 free_glsl_to_tgsi_visitor(stp->glsl_to_tgsi);
613 } else
614 error = st_translate_mesa_program(st->ctx,
615 PIPE_SHADER_VERTEX,
616 ureg,
617 &stp->Base,
618 /* inputs */
619 stvp->num_inputs,
620 stvp->input_to_index,
621 NULL, /* input semantic name */
622 NULL, /* input semantic index */
623 NULL,
624 /* outputs */
625 num_outputs,
626 stvp->result_to_output,
627 output_semantic_name,
628 output_semantic_index);
629
630 if (error) {
631 debug_printf("%s: failed to translate Mesa program:\n", __func__);
632 _mesa_print_program(&stp->Base);
633 debug_assert(0);
634 return false;
635 }
636
637 stp->state.tokens = ureg_get_tokens(ureg, NULL);
638 ureg_destroy(ureg);
639
640 if (stp->glsl_to_tgsi) {
641 stp->glsl_to_tgsi = NULL;
642 st_store_ir_in_disk_cache(st, &stp->Base, false);
643 }
644
645 return stp->state.tokens != NULL;
646 }
647
648 static struct nir_shader *
649 get_nir_shader(struct st_context *st, struct st_program *stp)
650 {
651 if (stp->Base.nir) {
652 nir_shader *nir = stp->Base.nir;
653
654 /* The first shader variant takes ownership of NIR, so that there is
655 * no cloning. Additional shader variants are always generated from
656 * serialized NIR to save memory.
657 */
658 stp->Base.nir = NULL;
659 assert(stp->serialized_nir && stp->serialized_nir_size);
660 return nir;
661 }
662
663 struct blob_reader blob_reader;
664 const struct nir_shader_compiler_options *options =
665 st->ctx->Const.ShaderCompilerOptions[stp->Base.info.stage].NirOptions;
666
667 blob_reader_init(&blob_reader, stp->serialized_nir, stp->serialized_nir_size);
668 return nir_deserialize(NULL, options, &blob_reader);
669 }
670
671 static void
672 lower_ucp(struct st_context *st,
673 struct nir_shader *nir,
674 unsigned ucp_enables,
675 struct gl_program_parameter_list *params)
676 {
677 if (nir->info.outputs_written & VARYING_BIT_CLIP_DIST0)
678 NIR_PASS_V(nir, nir_lower_clip_disable, ucp_enables);
679 else {
680 struct pipe_screen *screen = st->pipe->screen;
681 bool can_compact = screen->get_param(screen,
682 PIPE_CAP_NIR_COMPACT_ARRAYS);
683 bool use_eye = st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] != NULL;
684
685 gl_state_index16 clipplane_state[MAX_CLIP_PLANES][STATE_LENGTH];
686 for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
687 if (use_eye) {
688 clipplane_state[i][0] = STATE_CLIPPLANE;
689 clipplane_state[i][1] = i;
690 } else {
691 clipplane_state[i][0] = STATE_INTERNAL;
692 clipplane_state[i][1] = STATE_CLIP_INTERNAL;
693 clipplane_state[i][2] = i;
694 }
695 _mesa_add_state_reference(params, clipplane_state[i]);
696 }
697
698 NIR_PASS_V(nir, nir_lower_clip_vs, ucp_enables,
699 true, can_compact, clipplane_state);
700 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
701 nir_shader_get_entrypoint(nir), true, false);
702 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
703 }
704 }
705
706 static const gl_state_index16 depth_range_state[STATE_LENGTH] =
707 { STATE_DEPTH_RANGE };
708
709 static struct st_common_variant *
710 st_create_vp_variant(struct st_context *st,
711 struct st_program *stvp,
712 const struct st_common_variant_key *key)
713 {
714 struct st_common_variant *vpv = CALLOC_STRUCT(st_common_variant);
715 struct pipe_context *pipe = st->pipe;
716 struct pipe_shader_state state = {0};
717
718 static const gl_state_index16 point_size_state[STATE_LENGTH] =
719 { STATE_INTERNAL, STATE_POINT_SIZE_CLAMPED, 0 };
720 struct gl_program_parameter_list *params = stvp->Base.Parameters;
721
722 vpv->key = *key;
723
724 state.stream_output = stvp->state.stream_output;
725
726 if (stvp->state.type == PIPE_SHADER_IR_NIR &&
727 (!key->is_draw_shader || draw_has_llvm())) {
728 bool finalize = false;
729
730 state.type = PIPE_SHADER_IR_NIR;
731 state.ir.nir = get_nir_shader(st, stvp);
732 if (key->clamp_color) {
733 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
734 finalize = true;
735 }
736 if (key->passthrough_edgeflags) {
737 NIR_PASS_V(state.ir.nir, nir_lower_passthrough_edgeflags);
738 finalize = true;
739 }
740
741 if (key->lower_point_size) {
742 _mesa_add_state_reference(params, point_size_state);
743 NIR_PASS_V(state.ir.nir, nir_lower_point_size_mov,
744 point_size_state);
745 finalize = true;
746 }
747
748 if (key->lower_ucp) {
749 lower_ucp(st, state.ir.nir, key->lower_ucp, params);
750 finalize = true;
751 }
752
753 if (finalize || !st->allow_st_finalize_nir_twice) {
754 st_finalize_nir(st, &stvp->Base, stvp->shader_program, state.ir.nir,
755 true);
756
757 /* Some of the lowering above may have introduced new varyings */
758 nir_shader_gather_info(state.ir.nir,
759 nir_shader_get_entrypoint(state.ir.nir));
760 }
761
762 if (ST_DEBUG & DEBUG_PRINT_IR)
763 nir_print_shader(state.ir.nir, stderr);
764
765 if (key->is_draw_shader)
766 vpv->base.driver_shader = draw_create_vertex_shader(st->draw, &state);
767 else
768 vpv->base.driver_shader = pipe->create_vs_state(pipe, &state);
769
770 return vpv;
771 }
772
773 state.type = PIPE_SHADER_IR_TGSI;
774 state.tokens = tgsi_dup_tokens(stvp->state.tokens);
775
776 /* Emulate features. */
777 if (key->clamp_color || key->passthrough_edgeflags) {
778 const struct tgsi_token *tokens;
779 unsigned flags =
780 (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
781 (key->passthrough_edgeflags ? TGSI_EMU_PASSTHROUGH_EDGEFLAG : 0);
782
783 tokens = tgsi_emulate(state.tokens, flags);
784
785 if (tokens) {
786 tgsi_free_tokens(state.tokens);
787 state.tokens = tokens;
788 } else {
789 fprintf(stderr, "mesa: cannot emulate deprecated features\n");
790 }
791 }
792
793 if (key->lower_depth_clamp) {
794 unsigned depth_range_const =
795 _mesa_add_state_reference(params, depth_range_state);
796
797 const struct tgsi_token *tokens;
798 tokens = st_tgsi_lower_depth_clamp(state.tokens, depth_range_const,
799 key->clip_negative_one_to_one);
800 if (tokens != state.tokens)
801 tgsi_free_tokens(state.tokens);
802 state.tokens = tokens;
803 }
804
805 if (ST_DEBUG & DEBUG_PRINT_IR)
806 tgsi_dump(state.tokens, 0);
807
808 if (key->is_draw_shader)
809 vpv->base.driver_shader = draw_create_vertex_shader(st->draw, &state);
810 else
811 vpv->base.driver_shader = pipe->create_vs_state(pipe, &state);
812
813 if (state.tokens) {
814 tgsi_free_tokens(state.tokens);
815 }
816
817 return vpv;
818 }
819
820
821 /**
822 * Find/create a vertex program variant.
823 */
824 struct st_common_variant *
825 st_get_vp_variant(struct st_context *st,
826 struct st_program *stp,
827 const struct st_common_variant_key *key)
828 {
829 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
830 struct st_common_variant *vpv;
831
832 /* Search for existing variant */
833 for (vpv = st_common_variant(stp->variants); vpv;
834 vpv = st_common_variant(vpv->base.next)) {
835 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
836 break;
837 }
838 }
839
840 if (!vpv) {
841 /* create now */
842 vpv = st_create_vp_variant(st, stp, key);
843 if (vpv) {
844 vpv->base.st = key->st;
845
846 unsigned num_inputs = stvp->num_inputs + key->passthrough_edgeflags;
847 for (unsigned index = 0; index < num_inputs; ++index) {
848 unsigned attr = stvp->index_to_input[index];
849 if (attr == ST_DOUBLE_ATTRIB_PLACEHOLDER)
850 continue;
851 vpv->vert_attrib_mask |= 1u << attr;
852 }
853
854 /* insert into list */
855 vpv->base.next = stp->variants;
856 stp->variants = &vpv->base;
857 }
858 }
859
860 return vpv;
861 }
862
863
864 /**
865 * Translate a Mesa fragment shader into a TGSI shader.
866 */
867 bool
868 st_translate_fragment_program(struct st_context *st,
869 struct st_program *stfp)
870 {
871 /* Non-GLSL programs: */
872 if (!stfp->glsl_to_tgsi) {
873 _mesa_remove_output_reads(&stfp->Base, PROGRAM_OUTPUT);
874 if (st->ctx->Const.GLSLFragCoordIsSysVal)
875 _mesa_program_fragment_position_to_sysval(&stfp->Base);
876
877 /* This determines which states will be updated when the assembly
878 * shader is bound.
879 *
880 * fragment.position and glDrawPixels always use constants.
881 */
882 stfp->affected_states = ST_NEW_FS_STATE |
883 ST_NEW_SAMPLE_SHADING |
884 ST_NEW_FS_CONSTANTS;
885
886 if (stfp->ati_fs) {
887 /* Just set them for ATI_fs unconditionally. */
888 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
889 ST_NEW_FS_SAMPLERS;
890 } else {
891 /* ARB_fp */
892 if (stfp->Base.SamplersUsed)
893 stfp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
894 ST_NEW_FS_SAMPLERS;
895 }
896
897 /* Translate to NIR. */
898 if (!stfp->ati_fs &&
899 st->pipe->screen->get_shader_param(st->pipe->screen,
900 PIPE_SHADER_FRAGMENT,
901 PIPE_SHADER_CAP_PREFERRED_IR)) {
902 nir_shader *nir =
903 st_translate_prog_to_nir(st, &stfp->Base, MESA_SHADER_FRAGMENT);
904
905 if (stfp->Base.nir)
906 ralloc_free(stfp->Base.nir);
907 if (stfp->serialized_nir) {
908 free(stfp->serialized_nir);
909 stfp->serialized_nir = NULL;
910 }
911 stfp->state.type = PIPE_SHADER_IR_NIR;
912 stfp->Base.nir = nir;
913 return true;
914 }
915 }
916
917 ubyte outputMapping[2 * FRAG_RESULT_MAX];
918 ubyte inputMapping[VARYING_SLOT_MAX];
919 ubyte inputSlotToAttr[VARYING_SLOT_MAX];
920 ubyte interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
921 GLuint attr;
922 GLbitfield64 inputsRead;
923 struct ureg_program *ureg;
924
925 GLboolean write_all = GL_FALSE;
926
927 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
928 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
929 uint fs_num_inputs = 0;
930
931 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
932 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
933 uint fs_num_outputs = 0;
934
935 memset(inputSlotToAttr, ~0, sizeof(inputSlotToAttr));
936
937 /*
938 * Convert Mesa program inputs to TGSI input register semantics.
939 */
940 inputsRead = stfp->Base.info.inputs_read;
941 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
942 if ((inputsRead & BITFIELD64_BIT(attr)) != 0) {
943 const GLuint slot = fs_num_inputs++;
944
945 inputMapping[attr] = slot;
946 inputSlotToAttr[slot] = attr;
947
948 switch (attr) {
949 case VARYING_SLOT_POS:
950 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
951 input_semantic_index[slot] = 0;
952 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
953 break;
954 case VARYING_SLOT_COL0:
955 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
956 input_semantic_index[slot] = 0;
957 interpMode[slot] = stfp->glsl_to_tgsi ?
958 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
959 break;
960 case VARYING_SLOT_COL1:
961 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
962 input_semantic_index[slot] = 1;
963 interpMode[slot] = stfp->glsl_to_tgsi ?
964 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_COLOR;
965 break;
966 case VARYING_SLOT_FOGC:
967 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
968 input_semantic_index[slot] = 0;
969 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
970 break;
971 case VARYING_SLOT_FACE:
972 input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
973 input_semantic_index[slot] = 0;
974 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
975 break;
976 case VARYING_SLOT_PRIMITIVE_ID:
977 input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
978 input_semantic_index[slot] = 0;
979 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
980 break;
981 case VARYING_SLOT_LAYER:
982 input_semantic_name[slot] = TGSI_SEMANTIC_LAYER;
983 input_semantic_index[slot] = 0;
984 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
985 break;
986 case VARYING_SLOT_VIEWPORT:
987 input_semantic_name[slot] = TGSI_SEMANTIC_VIEWPORT_INDEX;
988 input_semantic_index[slot] = 0;
989 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
990 break;
991 case VARYING_SLOT_CLIP_DIST0:
992 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
993 input_semantic_index[slot] = 0;
994 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
995 break;
996 case VARYING_SLOT_CLIP_DIST1:
997 input_semantic_name[slot] = TGSI_SEMANTIC_CLIPDIST;
998 input_semantic_index[slot] = 1;
999 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
1000 break;
1001 case VARYING_SLOT_CULL_DIST0:
1002 case VARYING_SLOT_CULL_DIST1:
1003 /* these should have been lowered by GLSL */
1004 assert(0);
1005 break;
1006 /* In most cases, there is nothing special about these
1007 * inputs, so adopt a convention to use the generic
1008 * semantic name and the mesa VARYING_SLOT_ number as the
1009 * index.
1010 *
1011 * All that is required is that the vertex shader labels
1012 * its own outputs similarly, and that the vertex shader
1013 * generates at least every output required by the
1014 * fragment shader plus fixed-function hardware (such as
1015 * BFC).
1016 *
1017 * However, some drivers may need us to identify the PNTC and TEXi
1018 * varyings if, for example, their capability to replace them with
1019 * sprite coordinates is limited.
1020 */
1021 case VARYING_SLOT_PNTC:
1022 if (st->needs_texcoord_semantic) {
1023 input_semantic_name[slot] = TGSI_SEMANTIC_PCOORD;
1024 input_semantic_index[slot] = 0;
1025 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
1026 break;
1027 }
1028 /* fall through */
1029 case VARYING_SLOT_TEX0:
1030 case VARYING_SLOT_TEX1:
1031 case VARYING_SLOT_TEX2:
1032 case VARYING_SLOT_TEX3:
1033 case VARYING_SLOT_TEX4:
1034 case VARYING_SLOT_TEX5:
1035 case VARYING_SLOT_TEX6:
1036 case VARYING_SLOT_TEX7:
1037 if (st->needs_texcoord_semantic) {
1038 input_semantic_name[slot] = TGSI_SEMANTIC_TEXCOORD;
1039 input_semantic_index[slot] = attr - VARYING_SLOT_TEX0;
1040 interpMode[slot] = stfp->glsl_to_tgsi ?
1041 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
1042 break;
1043 }
1044 /* fall through */
1045 case VARYING_SLOT_VAR0:
1046 default:
1047 /* Semantic indices should be zero-based because drivers may choose
1048 * to assign a fixed slot determined by that index.
1049 * This is useful because ARB_separate_shader_objects uses location
1050 * qualifiers for linkage, and if the semantic index corresponds to
1051 * these locations, linkage passes in the driver become unecessary.
1052 *
1053 * If needs_texcoord_semantic is true, no semantic indices will be
1054 * consumed for the TEXi varyings, and we can base the locations of
1055 * the user varyings on VAR0. Otherwise, we use TEX0 as base index.
1056 */
1057 assert(attr >= VARYING_SLOT_VAR0 || attr == VARYING_SLOT_PNTC ||
1058 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7));
1059 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
1060 input_semantic_index[slot] = st_get_generic_varying_index(st, attr);
1061 if (attr == VARYING_SLOT_PNTC)
1062 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
1063 else {
1064 interpMode[slot] = stfp->glsl_to_tgsi ?
1065 TGSI_INTERPOLATE_COUNT : TGSI_INTERPOLATE_PERSPECTIVE;
1066 }
1067 break;
1068 }
1069 }
1070 else {
1071 inputMapping[attr] = -1;
1072 }
1073 }
1074
1075 /*
1076 * Semantics and mapping for outputs
1077 */
1078 GLbitfield64 outputsWritten = stfp->Base.info.outputs_written;
1079
1080 /* if z is written, emit that first */
1081 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
1082 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
1083 fs_output_semantic_index[fs_num_outputs] = 0;
1084 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
1085 fs_num_outputs++;
1086 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
1087 }
1088
1089 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
1090 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
1091 fs_output_semantic_index[fs_num_outputs] = 0;
1092 outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
1093 fs_num_outputs++;
1094 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
1095 }
1096
1097 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) {
1098 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_SAMPLEMASK;
1099 fs_output_semantic_index[fs_num_outputs] = 0;
1100 outputMapping[FRAG_RESULT_SAMPLE_MASK] = fs_num_outputs;
1101 fs_num_outputs++;
1102 outputsWritten &= ~(1 << FRAG_RESULT_SAMPLE_MASK);
1103 }
1104
1105 /* handle remaining outputs (color) */
1106 for (attr = 0; attr < ARRAY_SIZE(outputMapping); attr++) {
1107 const GLbitfield64 written = attr < FRAG_RESULT_MAX ? outputsWritten :
1108 stfp->Base.SecondaryOutputsWritten;
1109 const unsigned loc = attr % FRAG_RESULT_MAX;
1110
1111 if (written & BITFIELD64_BIT(loc)) {
1112 switch (loc) {
1113 case FRAG_RESULT_DEPTH:
1114 case FRAG_RESULT_STENCIL:
1115 case FRAG_RESULT_SAMPLE_MASK:
1116 /* handled above */
1117 assert(0);
1118 break;
1119 case FRAG_RESULT_COLOR:
1120 write_all = GL_TRUE; /* fallthrough */
1121 default: {
1122 int index;
1123 assert(loc == FRAG_RESULT_COLOR ||
1124 (FRAG_RESULT_DATA0 <= loc && loc < FRAG_RESULT_MAX));
1125
1126 index = (loc == FRAG_RESULT_COLOR) ? 0 : (loc - FRAG_RESULT_DATA0);
1127
1128 if (attr >= FRAG_RESULT_MAX) {
1129 /* Secondary color for dual source blending. */
1130 assert(index == 0);
1131 index++;
1132 }
1133
1134 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
1135 fs_output_semantic_index[fs_num_outputs] = index;
1136 outputMapping[attr] = fs_num_outputs;
1137 break;
1138 }
1139 }
1140
1141 fs_num_outputs++;
1142 }
1143 }
1144
1145 ureg = ureg_create_with_screen(PIPE_SHADER_FRAGMENT, st->pipe->screen);
1146 if (ureg == NULL)
1147 return false;
1148
1149 if (ST_DEBUG & DEBUG_MESA) {
1150 _mesa_print_program(&stfp->Base);
1151 _mesa_print_program_parameters(st->ctx, &stfp->Base);
1152 debug_printf("\n");
1153 }
1154 if (write_all == GL_TRUE)
1155 ureg_property(ureg, TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS, 1);
1156
1157 if (stfp->Base.info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
1158 switch (stfp->Base.info.fs.depth_layout) {
1159 case FRAG_DEPTH_LAYOUT_ANY:
1160 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1161 TGSI_FS_DEPTH_LAYOUT_ANY);
1162 break;
1163 case FRAG_DEPTH_LAYOUT_GREATER:
1164 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1165 TGSI_FS_DEPTH_LAYOUT_GREATER);
1166 break;
1167 case FRAG_DEPTH_LAYOUT_LESS:
1168 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1169 TGSI_FS_DEPTH_LAYOUT_LESS);
1170 break;
1171 case FRAG_DEPTH_LAYOUT_UNCHANGED:
1172 ureg_property(ureg, TGSI_PROPERTY_FS_DEPTH_LAYOUT,
1173 TGSI_FS_DEPTH_LAYOUT_UNCHANGED);
1174 break;
1175 default:
1176 assert(0);
1177 }
1178 }
1179
1180 if (stfp->glsl_to_tgsi) {
1181 st_translate_program(st->ctx,
1182 PIPE_SHADER_FRAGMENT,
1183 ureg,
1184 stfp->glsl_to_tgsi,
1185 &stfp->Base,
1186 /* inputs */
1187 fs_num_inputs,
1188 inputMapping,
1189 inputSlotToAttr,
1190 input_semantic_name,
1191 input_semantic_index,
1192 interpMode,
1193 /* outputs */
1194 fs_num_outputs,
1195 outputMapping,
1196 fs_output_semantic_name,
1197 fs_output_semantic_index);
1198
1199 free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
1200 } else if (stfp->ati_fs)
1201 st_translate_atifs_program(ureg,
1202 stfp->ati_fs,
1203 &stfp->Base,
1204 /* inputs */
1205 fs_num_inputs,
1206 inputMapping,
1207 input_semantic_name,
1208 input_semantic_index,
1209 interpMode,
1210 /* outputs */
1211 fs_num_outputs,
1212 outputMapping,
1213 fs_output_semantic_name,
1214 fs_output_semantic_index);
1215 else
1216 st_translate_mesa_program(st->ctx,
1217 PIPE_SHADER_FRAGMENT,
1218 ureg,
1219 &stfp->Base,
1220 /* inputs */
1221 fs_num_inputs,
1222 inputMapping,
1223 input_semantic_name,
1224 input_semantic_index,
1225 interpMode,
1226 /* outputs */
1227 fs_num_outputs,
1228 outputMapping,
1229 fs_output_semantic_name,
1230 fs_output_semantic_index);
1231
1232 stfp->state.tokens = ureg_get_tokens(ureg, NULL);
1233 ureg_destroy(ureg);
1234
1235 if (stfp->glsl_to_tgsi) {
1236 stfp->glsl_to_tgsi = NULL;
1237 st_store_ir_in_disk_cache(st, &stfp->Base, false);
1238 }
1239
1240 return stfp->state.tokens != NULL;
1241 }
1242
1243 static struct st_fp_variant *
1244 st_create_fp_variant(struct st_context *st,
1245 struct st_program *stfp,
1246 const struct st_fp_variant_key *key)
1247 {
1248 struct pipe_context *pipe = st->pipe;
1249 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
1250 struct pipe_shader_state state = {0};
1251 struct gl_program_parameter_list *params = stfp->Base.Parameters;
1252 static const gl_state_index16 texcoord_state[STATE_LENGTH] =
1253 { STATE_INTERNAL, STATE_CURRENT_ATTRIB, VERT_ATTRIB_TEX0 };
1254 static const gl_state_index16 scale_state[STATE_LENGTH] =
1255 { STATE_INTERNAL, STATE_PT_SCALE };
1256 static const gl_state_index16 bias_state[STATE_LENGTH] =
1257 { STATE_INTERNAL, STATE_PT_BIAS };
1258 static const gl_state_index16 alpha_ref_state[STATE_LENGTH] =
1259 { STATE_INTERNAL, STATE_ALPHA_REF };
1260
1261 if (!variant)
1262 return NULL;
1263
1264 if (stfp->state.type == PIPE_SHADER_IR_NIR) {
1265 bool finalize = false;
1266
1267 state.type = PIPE_SHADER_IR_NIR;
1268 state.ir.nir = get_nir_shader(st, stfp);
1269
1270 if (key->clamp_color) {
1271 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
1272 finalize = true;
1273 }
1274
1275 if (key->lower_flatshade) {
1276 NIR_PASS_V(state.ir.nir, nir_lower_flatshade);
1277 finalize = true;
1278 }
1279
1280 if (key->lower_alpha_func != COMPARE_FUNC_NEVER) {
1281 _mesa_add_state_reference(params, alpha_ref_state);
1282 NIR_PASS_V(state.ir.nir, nir_lower_alpha_test, key->lower_alpha_func,
1283 false, alpha_ref_state);
1284 finalize = true;
1285 }
1286
1287 if (key->lower_two_sided_color) {
1288 bool face_sysval = st->ctx->Const.GLSLFrontFacingIsSysVal;
1289 NIR_PASS_V(state.ir.nir, nir_lower_two_sided_color, face_sysval);
1290 finalize = true;
1291 }
1292
1293 if (key->persample_shading) {
1294 nir_shader *shader = state.ir.nir;
1295 nir_foreach_shader_in_variable(var, shader)
1296 var->data.sample = true;
1297 finalize = true;
1298 }
1299
1300 assert(!(key->bitmap && key->drawpixels));
1301
1302 /* glBitmap */
1303 if (key->bitmap) {
1304 nir_lower_bitmap_options options = {0};
1305
1306 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1307 options.sampler = variant->bitmap_sampler;
1308 options.swizzle_xxxx = st->bitmap.tex_format == PIPE_FORMAT_R8_UNORM;
1309
1310 NIR_PASS_V(state.ir.nir, nir_lower_bitmap, &options);
1311 finalize = true;
1312 }
1313
1314 /* glDrawPixels (color only) */
1315 if (key->drawpixels) {
1316 nir_lower_drawpixels_options options = {{0}};
1317 unsigned samplers_used = stfp->Base.SamplersUsed;
1318
1319 /* Find the first unused slot. */
1320 variant->drawpix_sampler = ffs(~samplers_used) - 1;
1321 options.drawpix_sampler = variant->drawpix_sampler;
1322 samplers_used |= (1 << variant->drawpix_sampler);
1323
1324 options.pixel_maps = key->pixelMaps;
1325 if (key->pixelMaps) {
1326 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1327 options.pixelmap_sampler = variant->pixelmap_sampler;
1328 }
1329
1330 options.scale_and_bias = key->scaleAndBias;
1331 if (key->scaleAndBias) {
1332 _mesa_add_state_reference(params, scale_state);
1333 memcpy(options.scale_state_tokens, scale_state,
1334 sizeof(options.scale_state_tokens));
1335 _mesa_add_state_reference(params, bias_state);
1336 memcpy(options.bias_state_tokens, bias_state,
1337 sizeof(options.bias_state_tokens));
1338 }
1339
1340 _mesa_add_state_reference(params, texcoord_state);
1341 memcpy(options.texcoord_state_tokens, texcoord_state,
1342 sizeof(options.texcoord_state_tokens));
1343
1344 NIR_PASS_V(state.ir.nir, nir_lower_drawpixels, &options);
1345 finalize = true;
1346 }
1347
1348 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1349 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
1350 key->external.lower_ayuv || key->external.lower_xyuv)) {
1351
1352 st_nir_lower_samplers(pipe->screen, state.ir.nir,
1353 stfp->shader_program, &stfp->Base);
1354
1355 nir_lower_tex_options options = {0};
1356 options.lower_y_uv_external = key->external.lower_nv12;
1357 options.lower_y_u_v_external = key->external.lower_iyuv;
1358 options.lower_xy_uxvx_external = key->external.lower_xy_uxvx;
1359 options.lower_yx_xuxv_external = key->external.lower_yx_xuxv;
1360 options.lower_ayuv_external = key->external.lower_ayuv;
1361 options.lower_xyuv_external = key->external.lower_xyuv;
1362 NIR_PASS_V(state.ir.nir, nir_lower_tex, &options);
1363 finalize = true;
1364 }
1365
1366 if (finalize || !st->allow_st_finalize_nir_twice) {
1367 st_finalize_nir(st, &stfp->Base, stfp->shader_program, state.ir.nir,
1368 false);
1369 }
1370
1371 /* This pass needs to happen *after* nir_lower_sampler */
1372 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1373 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
1374 key->external.lower_ayuv || key->external.lower_xyuv)) {
1375 NIR_PASS_V(state.ir.nir, st_nir_lower_tex_src_plane,
1376 ~stfp->Base.SamplersUsed,
1377 key->external.lower_nv12 || key->external.lower_xy_uxvx ||
1378 key->external.lower_yx_xuxv,
1379 key->external.lower_iyuv);
1380 finalize = true;
1381 }
1382
1383 if (finalize || !st->allow_st_finalize_nir_twice) {
1384 /* Some of the lowering above may have introduced new varyings */
1385 nir_shader_gather_info(state.ir.nir,
1386 nir_shader_get_entrypoint(state.ir.nir));
1387
1388 struct pipe_screen *screen = pipe->screen;
1389 if (screen->finalize_nir)
1390 screen->finalize_nir(screen, state.ir.nir, false);
1391 }
1392
1393 if (ST_DEBUG & DEBUG_PRINT_IR)
1394 nir_print_shader(state.ir.nir, stderr);
1395
1396 variant->base.driver_shader = pipe->create_fs_state(pipe, &state);
1397 variant->key = *key;
1398
1399 return variant;
1400 }
1401
1402 state.tokens = stfp->state.tokens;
1403
1404 assert(!(key->bitmap && key->drawpixels));
1405
1406 /* Fix texture targets and add fog for ATI_fs */
1407 if (stfp->ati_fs) {
1408 const struct tgsi_token *tokens = st_fixup_atifs(state.tokens, key);
1409
1410 if (tokens)
1411 state.tokens = tokens;
1412 else
1413 fprintf(stderr, "mesa: cannot post-process ATI_fs\n");
1414 }
1415
1416 /* Emulate features. */
1417 if (key->clamp_color || key->persample_shading) {
1418 const struct tgsi_token *tokens;
1419 unsigned flags =
1420 (key->clamp_color ? TGSI_EMU_CLAMP_COLOR_OUTPUTS : 0) |
1421 (key->persample_shading ? TGSI_EMU_FORCE_PERSAMPLE_INTERP : 0);
1422
1423 tokens = tgsi_emulate(state.tokens, flags);
1424
1425 if (tokens) {
1426 if (state.tokens != stfp->state.tokens)
1427 tgsi_free_tokens(state.tokens);
1428 state.tokens = tokens;
1429 } else
1430 fprintf(stderr, "mesa: cannot emulate deprecated features\n");
1431 }
1432
1433 /* glBitmap */
1434 if (key->bitmap) {
1435 const struct tgsi_token *tokens;
1436
1437 variant->bitmap_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1438
1439 tokens = st_get_bitmap_shader(state.tokens,
1440 st->internal_target,
1441 variant->bitmap_sampler,
1442 st->needs_texcoord_semantic,
1443 st->bitmap.tex_format ==
1444 PIPE_FORMAT_R8_UNORM);
1445
1446 if (tokens) {
1447 if (state.tokens != stfp->state.tokens)
1448 tgsi_free_tokens(state.tokens);
1449 state.tokens = tokens;
1450 } else
1451 fprintf(stderr, "mesa: cannot create a shader for glBitmap\n");
1452 }
1453
1454 /* glDrawPixels (color only) */
1455 if (key->drawpixels) {
1456 const struct tgsi_token *tokens;
1457 unsigned scale_const = 0, bias_const = 0, texcoord_const = 0;
1458
1459 /* Find the first unused slot. */
1460 variant->drawpix_sampler = ffs(~stfp->Base.SamplersUsed) - 1;
1461
1462 if (key->pixelMaps) {
1463 unsigned samplers_used = stfp->Base.SamplersUsed |
1464 (1 << variant->drawpix_sampler);
1465
1466 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
1467 }
1468
1469 if (key->scaleAndBias) {
1470 scale_const = _mesa_add_state_reference(params, scale_state);
1471 bias_const = _mesa_add_state_reference(params, bias_state);
1472 }
1473
1474 texcoord_const = _mesa_add_state_reference(params, texcoord_state);
1475
1476 tokens = st_get_drawpix_shader(state.tokens,
1477 st->needs_texcoord_semantic,
1478 key->scaleAndBias, scale_const,
1479 bias_const, key->pixelMaps,
1480 variant->drawpix_sampler,
1481 variant->pixelmap_sampler,
1482 texcoord_const, st->internal_target);
1483
1484 if (tokens) {
1485 if (state.tokens != stfp->state.tokens)
1486 tgsi_free_tokens(state.tokens);
1487 state.tokens = tokens;
1488 } else
1489 fprintf(stderr, "mesa: cannot create a shader for glDrawPixels\n");
1490 }
1491
1492 if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1493 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv)) {
1494 const struct tgsi_token *tokens;
1495
1496 /* samplers inserted would conflict, but this should be unpossible: */
1497 assert(!(key->bitmap || key->drawpixels));
1498
1499 tokens = st_tgsi_lower_yuv(state.tokens,
1500 ~stfp->Base.SamplersUsed,
1501 key->external.lower_nv12 ||
1502 key->external.lower_xy_uxvx ||
1503 key->external.lower_yx_xuxv,
1504 key->external.lower_iyuv);
1505 if (tokens) {
1506 if (state.tokens != stfp->state.tokens)
1507 tgsi_free_tokens(state.tokens);
1508 state.tokens = tokens;
1509 } else {
1510 fprintf(stderr, "mesa: cannot create a shader for samplerExternalOES\n");
1511 }
1512 }
1513
1514 if (key->lower_depth_clamp) {
1515 unsigned depth_range_const = _mesa_add_state_reference(params, depth_range_state);
1516
1517 const struct tgsi_token *tokens;
1518 tokens = st_tgsi_lower_depth_clamp_fs(state.tokens, depth_range_const);
1519 if (state.tokens != stfp->state.tokens)
1520 tgsi_free_tokens(state.tokens);
1521 state.tokens = tokens;
1522 }
1523
1524 if (ST_DEBUG & DEBUG_PRINT_IR)
1525 tgsi_dump(state.tokens, 0);
1526
1527 /* fill in variant */
1528 variant->base.driver_shader = pipe->create_fs_state(pipe, &state);
1529 variant->key = *key;
1530
1531 if (state.tokens != stfp->state.tokens)
1532 tgsi_free_tokens(state.tokens);
1533 return variant;
1534 }
1535
1536 /**
1537 * Translate fragment program if needed.
1538 */
1539 struct st_fp_variant *
1540 st_get_fp_variant(struct st_context *st,
1541 struct st_program *stfp,
1542 const struct st_fp_variant_key *key)
1543 {
1544 struct st_fp_variant *fpv;
1545
1546 /* Search for existing variant */
1547 for (fpv = st_fp_variant(stfp->variants); fpv;
1548 fpv = st_fp_variant(fpv->base.next)) {
1549 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
1550 break;
1551 }
1552 }
1553
1554 if (!fpv) {
1555 /* create new */
1556 fpv = st_create_fp_variant(st, stfp, key);
1557 if (fpv) {
1558 fpv->base.st = key->st;
1559
1560 if (key->bitmap || key->drawpixels) {
1561 /* Regular variants should always come before the
1562 * bitmap & drawpixels variants, (unless there
1563 * are no regular variants) so that
1564 * st_update_fp can take a fast path when
1565 * shader_has_one_variant is set.
1566 */
1567 if (!stfp->variants) {
1568 stfp->variants = &fpv->base;
1569 } else {
1570 /* insert into list after the first one */
1571 fpv->base.next = stfp->variants->next;
1572 stfp->variants->next = &fpv->base;
1573 }
1574 } else {
1575 /* insert into list */
1576 fpv->base.next = stfp->variants;
1577 stfp->variants = &fpv->base;
1578 }
1579 }
1580 }
1581
1582 return fpv;
1583 }
1584
1585 /**
1586 * Translate a program. This is common code for geometry and tessellation
1587 * shaders.
1588 */
1589 bool
1590 st_translate_common_program(struct st_context *st,
1591 struct st_program *stp)
1592 {
1593 struct gl_program *prog = &stp->Base;
1594 enum pipe_shader_type stage =
1595 pipe_shader_type_from_mesa(stp->Base.info.stage);
1596 struct ureg_program *ureg = ureg_create_with_screen(stage, st->pipe->screen);
1597
1598 if (ureg == NULL)
1599 return false;
1600
1601 switch (stage) {
1602 case PIPE_SHADER_TESS_CTRL:
1603 ureg_property(ureg, TGSI_PROPERTY_TCS_VERTICES_OUT,
1604 stp->Base.info.tess.tcs_vertices_out);
1605 break;
1606
1607 case PIPE_SHADER_TESS_EVAL:
1608 if (stp->Base.info.tess.primitive_mode == GL_ISOLINES)
1609 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE, GL_LINES);
1610 else
1611 ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE,
1612 stp->Base.info.tess.primitive_mode);
1613
1614 STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
1615 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
1616 PIPE_TESS_SPACING_FRACTIONAL_ODD);
1617 STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
1618 PIPE_TESS_SPACING_FRACTIONAL_EVEN);
1619
1620 ureg_property(ureg, TGSI_PROPERTY_TES_SPACING,
1621 (stp->Base.info.tess.spacing + 1) % 3);
1622
1623 ureg_property(ureg, TGSI_PROPERTY_TES_VERTEX_ORDER_CW,
1624 !stp->Base.info.tess.ccw);
1625 ureg_property(ureg, TGSI_PROPERTY_TES_POINT_MODE,
1626 stp->Base.info.tess.point_mode);
1627 break;
1628
1629 case PIPE_SHADER_GEOMETRY:
1630 ureg_property(ureg, TGSI_PROPERTY_GS_INPUT_PRIM,
1631 stp->Base.info.gs.input_primitive);
1632 ureg_property(ureg, TGSI_PROPERTY_GS_OUTPUT_PRIM,
1633 stp->Base.info.gs.output_primitive);
1634 ureg_property(ureg, TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
1635 stp->Base.info.gs.vertices_out);
1636 ureg_property(ureg, TGSI_PROPERTY_GS_INVOCATIONS,
1637 stp->Base.info.gs.invocations);
1638 break;
1639
1640 default:
1641 break;
1642 }
1643
1644 ubyte inputSlotToAttr[VARYING_SLOT_TESS_MAX];
1645 ubyte inputMapping[VARYING_SLOT_TESS_MAX];
1646 ubyte outputMapping[VARYING_SLOT_TESS_MAX];
1647 GLuint attr;
1648
1649 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
1650 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
1651 uint num_inputs = 0;
1652
1653 ubyte output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
1654 ubyte output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
1655 uint num_outputs = 0;
1656
1657 GLint i;
1658
1659 memset(inputSlotToAttr, 0, sizeof(inputSlotToAttr));
1660 memset(inputMapping, 0, sizeof(inputMapping));
1661 memset(outputMapping, 0, sizeof(outputMapping));
1662 memset(&stp->state, 0, sizeof(stp->state));
1663
1664 if (prog->info.clip_distance_array_size)
1665 ureg_property(ureg, TGSI_PROPERTY_NUM_CLIPDIST_ENABLED,
1666 prog->info.clip_distance_array_size);
1667 if (prog->info.cull_distance_array_size)
1668 ureg_property(ureg, TGSI_PROPERTY_NUM_CULLDIST_ENABLED,
1669 prog->info.cull_distance_array_size);
1670
1671 /*
1672 * Convert Mesa program inputs to TGSI input register semantics.
1673 */
1674 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1675 if ((prog->info.inputs_read & BITFIELD64_BIT(attr)) == 0)
1676 continue;
1677
1678 unsigned slot = num_inputs++;
1679
1680 inputMapping[attr] = slot;
1681 inputSlotToAttr[slot] = attr;
1682
1683 unsigned semantic_name, semantic_index;
1684 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1685 &semantic_name, &semantic_index);
1686 input_semantic_name[slot] = semantic_name;
1687 input_semantic_index[slot] = semantic_index;
1688 }
1689
1690 /* Also add patch inputs. */
1691 for (attr = 0; attr < 32; attr++) {
1692 if (prog->info.patch_inputs_read & (1u << attr)) {
1693 GLuint slot = num_inputs++;
1694 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1695
1696 inputMapping[patch_attr] = slot;
1697 inputSlotToAttr[slot] = patch_attr;
1698 input_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1699 input_semantic_index[slot] = attr;
1700 }
1701 }
1702
1703 /* initialize output semantics to defaults */
1704 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
1705 output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
1706 output_semantic_index[i] = 0;
1707 }
1708
1709 /*
1710 * Determine number of outputs, the (default) output register
1711 * mapping and the semantic information for each output.
1712 */
1713 for (attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1714 if (prog->info.outputs_written & BITFIELD64_BIT(attr)) {
1715 GLuint slot = num_outputs++;
1716
1717 outputMapping[attr] = slot;
1718
1719 unsigned semantic_name, semantic_index;
1720 tgsi_get_gl_varying_semantic(attr, st->needs_texcoord_semantic,
1721 &semantic_name, &semantic_index);
1722 output_semantic_name[slot] = semantic_name;
1723 output_semantic_index[slot] = semantic_index;
1724 }
1725 }
1726
1727 /* Also add patch outputs. */
1728 for (attr = 0; attr < 32; attr++) {
1729 if (prog->info.patch_outputs_written & (1u << attr)) {
1730 GLuint slot = num_outputs++;
1731 GLuint patch_attr = VARYING_SLOT_PATCH0 + attr;
1732
1733 outputMapping[patch_attr] = slot;
1734 output_semantic_name[slot] = TGSI_SEMANTIC_PATCH;
1735 output_semantic_index[slot] = attr;
1736 }
1737 }
1738
1739 st_translate_program(st->ctx,
1740 stage,
1741 ureg,
1742 stp->glsl_to_tgsi,
1743 prog,
1744 /* inputs */
1745 num_inputs,
1746 inputMapping,
1747 inputSlotToAttr,
1748 input_semantic_name,
1749 input_semantic_index,
1750 NULL,
1751 /* outputs */
1752 num_outputs,
1753 outputMapping,
1754 output_semantic_name,
1755 output_semantic_index);
1756
1757 stp->state.tokens = ureg_get_tokens(ureg, NULL);
1758
1759 ureg_destroy(ureg);
1760
1761 st_translate_stream_output_info(prog);
1762
1763 st_store_ir_in_disk_cache(st, prog, false);
1764
1765 if (ST_DEBUG & DEBUG_PRINT_IR && ST_DEBUG & DEBUG_MESA)
1766 _mesa_print_program(prog);
1767
1768 free_glsl_to_tgsi_visitor(stp->glsl_to_tgsi);
1769 stp->glsl_to_tgsi = NULL;
1770 return true;
1771 }
1772
1773
1774 /**
1775 * Get/create a basic program variant.
1776 */
1777 struct st_variant *
1778 st_get_common_variant(struct st_context *st,
1779 struct st_program *prog,
1780 const struct st_common_variant_key *key)
1781 {
1782 struct pipe_context *pipe = st->pipe;
1783 struct st_variant *v;
1784 struct pipe_shader_state state = {0};
1785
1786 /* Search for existing variant */
1787 for (v = prog->variants; v; v = v->next) {
1788 if (memcmp(&st_common_variant(v)->key, key, sizeof(*key)) == 0)
1789 break;
1790 }
1791
1792 if (!v) {
1793 /* create new */
1794 v = (struct st_variant*)CALLOC_STRUCT(st_common_variant);
1795 if (v) {
1796 if (prog->state.type == PIPE_SHADER_IR_NIR) {
1797 bool finalize = false;
1798
1799 state.type = PIPE_SHADER_IR_NIR;
1800 state.ir.nir = get_nir_shader(st, prog);
1801
1802 if (key->clamp_color) {
1803 NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
1804 finalize = true;
1805 }
1806
1807 state.stream_output = prog->state.stream_output;
1808
1809 if (finalize || !st->allow_st_finalize_nir_twice) {
1810 st_finalize_nir(st, &prog->Base, prog->shader_program,
1811 state.ir.nir, true);
1812 }
1813
1814 if (ST_DEBUG & DEBUG_PRINT_IR)
1815 nir_print_shader(state.ir.nir, stderr);
1816 } else {
1817 if (key->lower_depth_clamp) {
1818 struct gl_program_parameter_list *params = prog->Base.Parameters;
1819
1820 unsigned depth_range_const =
1821 _mesa_add_state_reference(params, depth_range_state);
1822
1823 const struct tgsi_token *tokens;
1824 tokens =
1825 st_tgsi_lower_depth_clamp(prog->state.tokens,
1826 depth_range_const,
1827 key->clip_negative_one_to_one);
1828
1829 if (tokens != prog->state.tokens)
1830 tgsi_free_tokens(prog->state.tokens);
1831
1832 prog->state.tokens = tokens;
1833 }
1834 state = prog->state;
1835
1836 if (ST_DEBUG & DEBUG_PRINT_IR)
1837 tgsi_dump(state.tokens, 0);
1838 }
1839 /* fill in new variant */
1840 switch (prog->Base.info.stage) {
1841 case MESA_SHADER_TESS_CTRL:
1842 v->driver_shader = pipe->create_tcs_state(pipe, &state);
1843 break;
1844 case MESA_SHADER_TESS_EVAL:
1845 v->driver_shader = pipe->create_tes_state(pipe, &state);
1846 break;
1847 case MESA_SHADER_GEOMETRY:
1848 v->driver_shader = pipe->create_gs_state(pipe, &state);
1849 break;
1850 case MESA_SHADER_COMPUTE: {
1851 struct pipe_compute_state cs = {0};
1852 cs.ir_type = state.type;
1853 cs.req_local_mem = prog->Base.info.cs.shared_size;
1854
1855 if (state.type == PIPE_SHADER_IR_NIR)
1856 cs.prog = state.ir.nir;
1857 else
1858 cs.prog = state.tokens;
1859
1860 v->driver_shader = pipe->create_compute_state(pipe, &cs);
1861 break;
1862 }
1863 default:
1864 assert(!"unhandled shader type");
1865 free(v);
1866 return NULL;
1867 }
1868
1869 st_common_variant(v)->key = *key;
1870 v->st = key->st;
1871
1872 /* insert into list */
1873 v->next = prog->variants;
1874 prog->variants = v;
1875 }
1876 }
1877
1878 return v;
1879 }
1880
1881
1882 /**
1883 * Vert/Geom/Frag programs have per-context variants. Free all the
1884 * variants attached to the given program which match the given context.
1885 */
1886 static void
1887 destroy_program_variants(struct st_context *st, struct gl_program *target)
1888 {
1889 if (!target || target == &_mesa_DummyProgram)
1890 return;
1891
1892 struct st_program *p = st_program(target);
1893 struct st_variant *v, **prevPtr = &p->variants;
1894 bool unbound = false;
1895
1896 for (v = p->variants; v; ) {
1897 struct st_variant *next = v->next;
1898 if (v->st == st) {
1899 if (!unbound) {
1900 st_unbind_program(st, p);
1901 unbound = true;
1902 }
1903
1904 /* unlink from list */
1905 *prevPtr = next;
1906 /* destroy this variant */
1907 delete_variant(st, v, target->Target);
1908 }
1909 else {
1910 prevPtr = &v->next;
1911 }
1912 v = next;
1913 }
1914 }
1915
1916
1917 /**
1918 * Callback for _mesa_HashWalk. Free all the shader's program variants
1919 * which match the given context.
1920 */
1921 static void
1922 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1923 {
1924 struct st_context *st = (struct st_context *) userData;
1925 struct gl_shader *shader = (struct gl_shader *) data;
1926
1927 switch (shader->Type) {
1928 case GL_SHADER_PROGRAM_MESA:
1929 {
1930 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1931 GLuint i;
1932
1933 for (i = 0; i < ARRAY_SIZE(shProg->_LinkedShaders); i++) {
1934 if (shProg->_LinkedShaders[i])
1935 destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1936 }
1937 }
1938 break;
1939 case GL_VERTEX_SHADER:
1940 case GL_FRAGMENT_SHADER:
1941 case GL_GEOMETRY_SHADER:
1942 case GL_TESS_CONTROL_SHADER:
1943 case GL_TESS_EVALUATION_SHADER:
1944 case GL_COMPUTE_SHADER:
1945 break;
1946 default:
1947 assert(0);
1948 }
1949 }
1950
1951
1952 /**
1953 * Callback for _mesa_HashWalk. Free all the program variants which match
1954 * the given context.
1955 */
1956 static void
1957 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1958 {
1959 struct st_context *st = (struct st_context *) userData;
1960 struct gl_program *program = (struct gl_program *) data;
1961 destroy_program_variants(st, program);
1962 }
1963
1964
1965 /**
1966 * Walk over all shaders and programs to delete any variants which
1967 * belong to the given context.
1968 * This is called during context tear-down.
1969 */
1970 void
1971 st_destroy_program_variants(struct st_context *st)
1972 {
1973 /* If shaders can be shared with other contexts, the last context will
1974 * call DeleteProgram on all shaders, releasing everything.
1975 */
1976 if (st->has_shareable_shaders)
1977 return;
1978
1979 /* ARB vert/frag program */
1980 _mesa_HashWalk(st->ctx->Shared->Programs,
1981 destroy_program_variants_cb, st);
1982
1983 /* GLSL vert/frag/geom shaders */
1984 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1985 destroy_shader_program_variants_cb, st);
1986 }
1987
1988
1989 /**
1990 * Compile one shader variant.
1991 */
1992 static void
1993 st_precompile_shader_variant(struct st_context *st,
1994 struct gl_program *prog)
1995 {
1996 switch (prog->Target) {
1997 case GL_VERTEX_PROGRAM_ARB: {
1998 struct st_program *p = (struct st_program *)prog;
1999 struct st_common_variant_key key;
2000
2001 memset(&key, 0, sizeof(key));
2002
2003 key.st = st->has_shareable_shaders ? NULL : st;
2004 st_get_vp_variant(st, p, &key);
2005 break;
2006 }
2007
2008 case GL_FRAGMENT_PROGRAM_ARB: {
2009 struct st_program *p = (struct st_program *)prog;
2010 struct st_fp_variant_key key;
2011
2012 memset(&key, 0, sizeof(key));
2013
2014 key.st = st->has_shareable_shaders ? NULL : st;
2015 st_get_fp_variant(st, p, &key);
2016 break;
2017 }
2018
2019 case GL_TESS_CONTROL_PROGRAM_NV:
2020 case GL_TESS_EVALUATION_PROGRAM_NV:
2021 case GL_GEOMETRY_PROGRAM_NV:
2022 case GL_COMPUTE_PROGRAM_NV: {
2023 struct st_program *p = st_program(prog);
2024 struct st_common_variant_key key;
2025
2026 memset(&key, 0, sizeof(key));
2027
2028 key.st = st->has_shareable_shaders ? NULL : st;
2029 st_get_common_variant(st, p, &key);
2030 break;
2031 }
2032
2033 default:
2034 assert(0);
2035 }
2036 }
2037
2038 void
2039 st_serialize_nir(struct st_program *stp)
2040 {
2041 if (!stp->serialized_nir) {
2042 struct blob blob;
2043 size_t size;
2044
2045 blob_init(&blob);
2046 nir_serialize(&blob, stp->Base.nir, false);
2047 blob_finish_get_buffer(&blob, &stp->serialized_nir, &size);
2048 stp->serialized_nir_size = size;
2049 }
2050 }
2051
2052 void
2053 st_finalize_program(struct st_context *st, struct gl_program *prog)
2054 {
2055 if (st->current_program[prog->info.stage] == prog) {
2056 if (prog->info.stage == MESA_SHADER_VERTEX)
2057 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, (struct st_program *)prog);
2058 else
2059 st->dirty |= ((struct st_program *)prog)->affected_states;
2060 }
2061
2062 if (prog->nir) {
2063 nir_sweep(prog->nir);
2064
2065 /* This is only needed for ARB_vp/fp programs and when the disk cache
2066 * is disabled. If the disk cache is enabled, GLSL programs are
2067 * serialized in write_nir_to_cache.
2068 */
2069 st_serialize_nir(st_program(prog));
2070 }
2071
2072 /* Create Gallium shaders now instead of on demand. */
2073 if (ST_DEBUG & DEBUG_PRECOMPILE ||
2074 st->shader_has_one_variant[prog->info.stage])
2075 st_precompile_shader_variant(st, prog);
2076 }