• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/glsl/gl_nir.h"
44 #include "compiler/glsl/gl_nir_linker.h"
45 #include "compiler/nir/nir.h"
46 #include "compiler/nir/nir_serialize.h"
47 #include "draw/draw_context.h"
48 
49 #include "pipe/p_context.h"
50 #include "pipe/p_defines.h"
51 #include "pipe/p_shader_tokens.h"
52 #include "draw/draw_context.h"
53 #include "tgsi/tgsi_dump.h"
54 #include "tgsi/tgsi_parse.h"
55 #include "tgsi/tgsi_ureg.h"
56 #include "nir/nir_to_tgsi.h"
57 
58 #include "util/u_memory.h"
59 
60 #include "st_debug.h"
61 #include "st_cb_bitmap.h"
62 #include "st_cb_drawpixels.h"
63 #include "st_context.h"
64 #include "st_program.h"
65 #include "st_atifs_to_nir.h"
66 #include "st_nir.h"
67 #include "st_shader_cache.h"
68 #include "st_util.h"
69 #include "cso_cache/cso_context.h"
70 
71 
72 static void
73 destroy_program_variants(struct st_context *st, struct gl_program *target);
74 
75 static void
set_affected_state_flags(uint64_t * states,struct gl_program * prog,uint64_t new_constants,uint64_t new_sampler_views,uint64_t new_samplers,uint64_t new_images,uint64_t new_ubos,uint64_t new_ssbos,uint64_t new_atomics)76 set_affected_state_flags(uint64_t *states,
77                          struct gl_program *prog,
78                          uint64_t new_constants,
79                          uint64_t new_sampler_views,
80                          uint64_t new_samplers,
81                          uint64_t new_images,
82                          uint64_t new_ubos,
83                          uint64_t new_ssbos,
84                          uint64_t new_atomics)
85 {
86    if (prog->Parameters->NumParameters)
87       *states |= new_constants;
88 
89    if (prog->info.num_textures)
90       *states |= new_sampler_views | new_samplers;
91 
92    if (prog->info.num_images)
93       *states |= new_images;
94 
95    if (prog->info.num_ubos)
96       *states |= new_ubos;
97 
98    if (prog->info.num_ssbos)
99       *states |= new_ssbos;
100 
101    if (prog->info.num_abos)
102       *states |= new_atomics;
103 }
104 
105 /**
106  * This determines which states will be updated when the shader is bound.
107  */
108 void
st_set_prog_affected_state_flags(struct gl_program * prog)109 st_set_prog_affected_state_flags(struct gl_program *prog)
110 {
111    uint64_t *states;
112 
113    switch (prog->info.stage) {
114    case MESA_SHADER_VERTEX:
115       states = &prog->affected_states;
116 
117       *states = ST_NEW_VS_STATE |
118                 ST_NEW_RASTERIZER |
119                 ST_NEW_VERTEX_ARRAYS;
120 
121       set_affected_state_flags(states, prog,
122                                ST_NEW_VS_CONSTANTS,
123                                ST_NEW_VS_SAMPLER_VIEWS,
124                                ST_NEW_VS_SAMPLERS,
125                                ST_NEW_VS_IMAGES,
126                                ST_NEW_VS_UBOS,
127                                ST_NEW_VS_SSBOS,
128                                ST_NEW_VS_ATOMICS);
129       break;
130 
131    case MESA_SHADER_TESS_CTRL:
132       states = &prog->affected_states;
133 
134       *states = ST_NEW_TCS_STATE;
135 
136       set_affected_state_flags(states, prog,
137                                ST_NEW_TCS_CONSTANTS,
138                                ST_NEW_TCS_SAMPLER_VIEWS,
139                                ST_NEW_TCS_SAMPLERS,
140                                ST_NEW_TCS_IMAGES,
141                                ST_NEW_TCS_UBOS,
142                                ST_NEW_TCS_SSBOS,
143                                ST_NEW_TCS_ATOMICS);
144       break;
145 
146    case MESA_SHADER_TESS_EVAL:
147       states = &prog->affected_states;
148 
149       *states = ST_NEW_TES_STATE |
150                 ST_NEW_RASTERIZER;
151 
152       set_affected_state_flags(states, prog,
153                                ST_NEW_TES_CONSTANTS,
154                                ST_NEW_TES_SAMPLER_VIEWS,
155                                ST_NEW_TES_SAMPLERS,
156                                ST_NEW_TES_IMAGES,
157                                ST_NEW_TES_UBOS,
158                                ST_NEW_TES_SSBOS,
159                                ST_NEW_TES_ATOMICS);
160       break;
161 
162    case MESA_SHADER_GEOMETRY:
163       states = &prog->affected_states;
164 
165       *states = ST_NEW_GS_STATE |
166                 ST_NEW_RASTERIZER;
167 
168       set_affected_state_flags(states, prog,
169                                ST_NEW_GS_CONSTANTS,
170                                ST_NEW_GS_SAMPLER_VIEWS,
171                                ST_NEW_GS_SAMPLERS,
172                                ST_NEW_GS_IMAGES,
173                                ST_NEW_GS_UBOS,
174                                ST_NEW_GS_SSBOS,
175                                ST_NEW_GS_ATOMICS);
176       break;
177 
178    case MESA_SHADER_FRAGMENT:
179       states = &prog->affected_states;
180 
181       /* gl_FragCoord and glDrawPixels always use constants. */
182       *states = ST_NEW_FS_STATE |
183                 ST_NEW_SAMPLE_SHADING |
184                 ST_NEW_FS_CONSTANTS;
185 
186       set_affected_state_flags(states, prog,
187                                ST_NEW_FS_CONSTANTS,
188                                ST_NEW_FS_SAMPLER_VIEWS,
189                                ST_NEW_FS_SAMPLERS,
190                                ST_NEW_FS_IMAGES,
191                                ST_NEW_FS_UBOS,
192                                ST_NEW_FS_SSBOS,
193                                ST_NEW_FS_ATOMICS);
194       break;
195 
196    case MESA_SHADER_COMPUTE:
197       states = &prog->affected_states;
198 
199       *states = ST_NEW_CS_STATE;
200 
201       set_affected_state_flags(states, prog,
202                                ST_NEW_CS_CONSTANTS,
203                                ST_NEW_CS_SAMPLER_VIEWS,
204                                ST_NEW_CS_SAMPLERS,
205                                ST_NEW_CS_IMAGES,
206                                ST_NEW_CS_UBOS,
207                                ST_NEW_CS_SSBOS,
208                                ST_NEW_CS_ATOMICS);
209       break;
210 
211    default:
212       unreachable("unhandled shader stage");
213    }
214 }
215 
216 
217 /**
218  * Delete a shader variant.  Note the caller must unlink the variant from
219  * the linked list.
220  */
221 static void
delete_variant(struct st_context * st,struct st_variant * v,GLenum target)222 delete_variant(struct st_context *st, struct st_variant *v, GLenum target)
223 {
224    if (v->driver_shader) {
225       if (target == GL_VERTEX_PROGRAM_ARB &&
226           ((struct st_common_variant*)v)->key.is_draw_shader) {
227          /* Draw shader. */
228          draw_delete_vertex_shader(st->draw, v->driver_shader);
229       } else if (st->has_shareable_shaders || v->st == st) {
230          /* The shader's context matches the calling context, or we
231           * don't care.
232           */
233          switch (target) {
234          case GL_VERTEX_PROGRAM_ARB:
235             st->pipe->delete_vs_state(st->pipe, v->driver_shader);
236             break;
237          case GL_TESS_CONTROL_PROGRAM_NV:
238             st->pipe->delete_tcs_state(st->pipe, v->driver_shader);
239             break;
240          case GL_TESS_EVALUATION_PROGRAM_NV:
241             st->pipe->delete_tes_state(st->pipe, v->driver_shader);
242             break;
243          case GL_GEOMETRY_PROGRAM_NV:
244             st->pipe->delete_gs_state(st->pipe, v->driver_shader);
245             break;
246          case GL_FRAGMENT_PROGRAM_ARB:
247             st->pipe->delete_fs_state(st->pipe, v->driver_shader);
248             break;
249          case GL_COMPUTE_PROGRAM_NV:
250             st->pipe->delete_compute_state(st->pipe, v->driver_shader);
251             break;
252          default:
253             unreachable("bad shader type in delete_basic_variant");
254          }
255       } else {
256          /* We can't delete a shader with a context different from the one
257           * that created it.  Add it to the creating context's zombie list.
258           */
259          enum pipe_shader_type type =
260             pipe_shader_type_from_mesa(_mesa_program_enum_to_shader_stage(target));
261 
262          st_save_zombie_shader(v->st, type, v->driver_shader);
263       }
264    }
265 
266    FREE(v);
267 }
268 
269 static void
st_unbind_program(struct st_context * st,struct gl_program * p)270 st_unbind_program(struct st_context *st, struct gl_program *p)
271 {
272    /* Unbind the shader in cso_context and re-bind in st/mesa. */
273    switch (p->info.stage) {
274    case MESA_SHADER_VERTEX:
275       cso_set_vertex_shader_handle(st->cso_context, NULL);
276       st->dirty |= ST_NEW_VS_STATE;
277       break;
278    case MESA_SHADER_TESS_CTRL:
279       cso_set_tessctrl_shader_handle(st->cso_context, NULL);
280       st->dirty |= ST_NEW_TCS_STATE;
281       break;
282    case MESA_SHADER_TESS_EVAL:
283       cso_set_tesseval_shader_handle(st->cso_context, NULL);
284       st->dirty |= ST_NEW_TES_STATE;
285       break;
286    case MESA_SHADER_GEOMETRY:
287       cso_set_geometry_shader_handle(st->cso_context, NULL);
288       st->dirty |= ST_NEW_GS_STATE;
289       break;
290    case MESA_SHADER_FRAGMENT:
291       cso_set_fragment_shader_handle(st->cso_context, NULL);
292       st->dirty |= ST_NEW_FS_STATE;
293       break;
294    case MESA_SHADER_COMPUTE:
295       cso_set_compute_shader_handle(st->cso_context, NULL);
296       st->dirty |= ST_NEW_CS_STATE;
297       break;
298    default:
299       unreachable("invalid shader type");
300    }
301 }
302 
303 /**
304  * Free all basic program variants.
305  */
306 void
st_release_variants(struct st_context * st,struct gl_program * p)307 st_release_variants(struct st_context *st, struct gl_program *p)
308 {
309    struct st_variant *v;
310 
311    /* If we are releasing shaders, re-bind them, because we don't
312     * know which shaders are bound in the driver.
313     */
314    if (p->variants)
315       st_unbind_program(st, p);
316 
317    for (v = p->variants; v; ) {
318       struct st_variant *next = v->next;
319       delete_variant(st, v, p->Target);
320       v = next;
321    }
322 
323    p->variants = NULL;
324 
325    if (p->state.tokens) {
326       ureg_free_tokens(p->state.tokens);
327       p->state.tokens = NULL;
328    }
329 
330    /* Note: Any setup of ->ir.nir that has had pipe->create_*_state called on
331     * it has resulted in the driver taking ownership of the NIR.  Those
332     * callers should be NULLing out the nir field in any pipe_shader_state
333     * that might have this called in order to indicate that.
334     *
335     * GLSL IR and ARB programs will have set gl_program->nir to the same
336     * shader as ir->ir.nir, so it will be freed by _mesa_delete_program().
337     */
338 }
339 
340 /**
341  * Free all basic program variants and unref program.
342  */
343 void
st_release_program(struct st_context * st,struct gl_program ** p)344 st_release_program(struct st_context *st, struct gl_program **p)
345 {
346    if (!*p)
347       return;
348 
349    destroy_program_variants(st, *p);
350    _mesa_reference_program(st->ctx, p, NULL);
351 }
352 
353 void
st_finalize_nir_before_variants(struct nir_shader * nir)354 st_finalize_nir_before_variants(struct nir_shader *nir)
355 {
356    NIR_PASS_V(nir, nir_split_var_copies);
357    NIR_PASS_V(nir, nir_lower_var_copies);
358    if (nir->options->lower_all_io_to_temps ||
359        nir->options->lower_all_io_to_elements ||
360        nir->info.stage == MESA_SHADER_VERTEX ||
361        nir->info.stage == MESA_SHADER_GEOMETRY) {
362       NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
363    } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
364       NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, true);
365    }
366 
367    /* st_nir_assign_vs_in_locations requires correct shader info. */
368    nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
369 
370    st_nir_assign_vs_in_locations(nir);
371 }
372 
373 static void
st_prog_to_nir_postprocess(struct st_context * st,nir_shader * nir,struct gl_program * prog)374 st_prog_to_nir_postprocess(struct st_context *st, nir_shader *nir,
375                            struct gl_program *prog)
376 {
377    struct pipe_screen *screen = st->screen;
378 
379    NIR_PASS_V(nir, nir_lower_regs_to_ssa);
380    nir_validate_shader(nir, "after st/ptn lower_regs_to_ssa");
381 
382    /* Lower outputs to temporaries to avoid reading from output variables (which
383     * is permitted by the language but generally not implemented in HW).
384     */
385    NIR_PASS_V(nir, nir_lower_io_to_temporaries,
386                nir_shader_get_entrypoint(nir),
387                true, false);
388    NIR_PASS_V(nir, nir_lower_global_vars_to_local);
389 
390    NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, prog, screen);
391    NIR_PASS_V(nir, nir_lower_system_values);
392    NIR_PASS_V(nir, nir_lower_compute_system_values, NULL);
393 
394    /* Optimise NIR */
395    NIR_PASS_V(nir, nir_opt_constant_folding);
396    gl_nir_opts(nir);
397    st_finalize_nir_before_variants(nir);
398 
399    if (st->allow_st_finalize_nir_twice) {
400       char *msg = st_finalize_nir(st, prog, NULL, nir, true, true);
401       free(msg);
402    }
403 
404    nir_validate_shader(nir, "after st/glsl finalize_nir");
405 }
406 
407 /**
408  * Translate ARB (asm) program to NIR
409  */
410 static nir_shader *
st_translate_prog_to_nir(struct st_context * st,struct gl_program * prog,gl_shader_stage stage)411 st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog,
412                          gl_shader_stage stage)
413 {
414    const struct nir_shader_compiler_options *options =
415       st_get_nir_compiler_options(st, prog->info.stage);
416 
417    /* Translate to NIR */
418    nir_shader *nir = prog_to_nir(st->ctx, prog, options);
419 
420    st_prog_to_nir_postprocess(st, nir, prog);
421 
422    return nir;
423 }
424 
425 /**
426  * Prepare st_vertex_program info.
427  *
428  * attrib_to_index is an optional mapping from a vertex attrib to a shader
429  * input index.
430  */
431 void
st_prepare_vertex_program(struct gl_program * prog)432 st_prepare_vertex_program(struct gl_program *prog)
433 {
434    struct gl_vertex_program *stvp = (struct gl_vertex_program *)prog;
435 
436    stvp->num_inputs = util_bitcount64(prog->info.inputs_read);
437    stvp->vert_attrib_mask = prog->info.inputs_read;
438 
439    /* Compute mapping of vertex program outputs to slots. */
440    memset(stvp->result_to_output, ~0, sizeof(stvp->result_to_output));
441    unsigned num_outputs = 0;
442    for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
443       if (prog->info.outputs_written & BITFIELD64_BIT(attr))
444          stvp->result_to_output[attr] = num_outputs++;
445    }
446    /* pre-setup potentially unused edgeflag output */
447    stvp->result_to_output[VARYING_SLOT_EDGE] = num_outputs;
448 }
449 
450 void
st_translate_stream_output_info(struct gl_program * prog)451 st_translate_stream_output_info(struct gl_program *prog)
452 {
453    struct gl_transform_feedback_info *info = prog->sh.LinkedTransformFeedback;
454    if (!info)
455       return;
456 
457    /* Determine the (default) output register mapping for each output. */
458    unsigned num_outputs = 0;
459    ubyte output_mapping[VARYING_SLOT_TESS_MAX];
460    memset(output_mapping, 0, sizeof(output_mapping));
461 
462    for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
463       /* this output was added by mesa/st and should not be tracked for xfb:
464        * drivers must check var->data.explicit_location to find the original output
465        * and only emit that one for xfb
466        */
467       if (prog->skip_pointsize_xfb && attr == VARYING_SLOT_PSIZ)
468          continue;
469       if (prog->info.outputs_written & BITFIELD64_BIT(attr))
470          output_mapping[attr] = num_outputs++;
471    }
472 
473    /* Translate stream output info. */
474    struct pipe_stream_output_info *so_info =
475       &prog->state.stream_output;
476 
477    for (unsigned i = 0; i < info->NumOutputs; i++) {
478       so_info->output[i].register_index =
479          output_mapping[info->Outputs[i].OutputRegister];
480       so_info->output[i].start_component = info->Outputs[i].ComponentOffset;
481       so_info->output[i].num_components = info->Outputs[i].NumComponents;
482       so_info->output[i].output_buffer = info->Outputs[i].OutputBuffer;
483       so_info->output[i].dst_offset = info->Outputs[i].DstOffset;
484       so_info->output[i].stream = info->Outputs[i].StreamId;
485    }
486 
487    for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
488       so_info->stride[i] = info->Buffers[i].Stride;
489    }
490    so_info->num_outputs = info->NumOutputs;
491 }
492 
493 /**
494  * Creates a driver shader from a NIR shader.  Takes ownership of the
495  * passed nir_shader.
496  */
497 struct pipe_shader_state *
st_create_nir_shader(struct st_context * st,struct pipe_shader_state * state)498 st_create_nir_shader(struct st_context *st, struct pipe_shader_state *state)
499 {
500    struct pipe_context *pipe = st->pipe;
501    struct pipe_screen *screen = st->screen;
502 
503    assert(state->type == PIPE_SHADER_IR_NIR);
504    nir_shader *nir = state->ir.nir;
505    struct shader_info info = nir->info;
506    gl_shader_stage stage = nir->info.stage;
507    enum pipe_shader_type sh = pipe_shader_type_from_mesa(stage);
508 
509    if (ST_DEBUG & DEBUG_PRINT_IR) {
510       fprintf(stderr, "NIR before handing off to driver:\n");
511       nir_print_shader(nir, stderr);
512    }
513 
514    if (PIPE_SHADER_IR_NIR !=
515        screen->get_shader_param(screen, sh, PIPE_SHADER_CAP_PREFERRED_IR)) {
516       /* u_screen.c defaults to images as deref enabled for some reason (which
517        * is what radeonsi wants), but nir-to-tgsi requires lowered images.
518        */
519       if (screen->get_param(screen, PIPE_CAP_NIR_IMAGES_AS_DEREF))
520          NIR_PASS_V(nir, gl_nir_lower_images, false);
521 
522       state->type = PIPE_SHADER_IR_TGSI;
523       state->tokens = nir_to_tgsi(nir, screen);
524 
525       if (ST_DEBUG & DEBUG_PRINT_IR) {
526          fprintf(stderr, "TGSI for driver after nir-to-tgsi:\n");
527          tgsi_dump(state->tokens, 0);
528          fprintf(stderr, "\n");
529       }
530    }
531 
532    struct pipe_shader_state *shader;
533    switch (stage) {
534    case MESA_SHADER_VERTEX:
535       shader = pipe->create_vs_state(pipe, state);
536       break;
537    case MESA_SHADER_TESS_CTRL:
538       shader = pipe->create_tcs_state(pipe, state);
539       break;
540    case MESA_SHADER_TESS_EVAL:
541       shader = pipe->create_tes_state(pipe, state);
542       break;
543    case MESA_SHADER_GEOMETRY:
544       shader = pipe->create_gs_state(pipe, state);
545       break;
546    case MESA_SHADER_FRAGMENT:
547       shader = pipe->create_fs_state(pipe, state);
548       break;
549    case MESA_SHADER_COMPUTE: {
550       struct pipe_compute_state cs = {0};
551       cs.ir_type = state->type;
552       cs.req_local_mem = info.shared_size;
553 
554       if (state->type == PIPE_SHADER_IR_NIR)
555          cs.prog = state->ir.nir;
556       else
557          cs.prog = state->tokens;
558 
559       shader = pipe->create_compute_state(pipe, &cs);
560       break;
561    }
562    default:
563       unreachable("unsupported shader stage");
564       return NULL;
565    }
566 
567    if (state->type == PIPE_SHADER_IR_TGSI)
568       tgsi_free_tokens(state->tokens);
569 
570    return shader;
571 }
572 
573 /**
574  * Translate a vertex program.
575  */
576 static bool
st_translate_vertex_program(struct st_context * st,struct gl_program * prog)577 st_translate_vertex_program(struct st_context *st,
578                             struct gl_program *prog)
579 {
580    /* ARB_vp: */
581    if (prog->arb.IsPositionInvariant)
582       _mesa_insert_mvp_code(st->ctx, prog);
583 
584    /* This determines which states will be updated when the assembly
585       * shader is bound.
586       */
587    prog->affected_states = ST_NEW_VS_STATE |
588                            ST_NEW_RASTERIZER |
589                            ST_NEW_VERTEX_ARRAYS;
590 
591    if (prog->Parameters->NumParameters)
592       prog->affected_states |= ST_NEW_VS_CONSTANTS;
593 
594    if (prog->nir)
595       ralloc_free(prog->nir);
596 
597    if (prog->serialized_nir) {
598       free(prog->serialized_nir);
599       prog->serialized_nir = NULL;
600    }
601 
602    prog->state.type = PIPE_SHADER_IR_NIR;
603    prog->nir = st_translate_prog_to_nir(st, prog,
604                                           MESA_SHADER_VERTEX);
605    prog->info = prog->nir->info;
606 
607    st_prepare_vertex_program(prog);
608    return true;
609 }
610 
611 static struct nir_shader *
get_nir_shader(struct st_context * st,struct gl_program * prog)612 get_nir_shader(struct st_context *st, struct gl_program *prog)
613 {
614    if (prog->nir) {
615       nir_shader *nir = prog->nir;
616 
617       /* The first shader variant takes ownership of NIR, so that there is
618        * no cloning. Additional shader variants are always generated from
619        * serialized NIR to save memory.
620        */
621       prog->nir = NULL;
622       assert(prog->serialized_nir && prog->serialized_nir_size);
623       return nir;
624    }
625 
626    struct blob_reader blob_reader;
627    const struct nir_shader_compiler_options *options =
628       st_get_nir_compiler_options(st, prog->info.stage);
629 
630    blob_reader_init(&blob_reader, prog->serialized_nir, prog->serialized_nir_size);
631    return nir_deserialize(NULL, options, &blob_reader);
632 }
633 
634 static void
lower_ucp(struct st_context * st,struct nir_shader * nir,unsigned ucp_enables,struct gl_program_parameter_list * params)635 lower_ucp(struct st_context *st,
636           struct nir_shader *nir,
637           unsigned ucp_enables,
638           struct gl_program_parameter_list *params)
639 {
640    if (nir->info.outputs_written & VARYING_BIT_CLIP_DIST0)
641       NIR_PASS_V(nir, nir_lower_clip_disable, ucp_enables);
642    else {
643       struct pipe_screen *screen = st->screen;
644       bool can_compact = screen->get_param(screen,
645                                            PIPE_CAP_NIR_COMPACT_ARRAYS);
646       bool use_eye = st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] != NULL;
647 
648       gl_state_index16 clipplane_state[MAX_CLIP_PLANES][STATE_LENGTH] = {{0}};
649       for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
650          if (use_eye) {
651             clipplane_state[i][0] = STATE_CLIPPLANE;
652             clipplane_state[i][1] = i;
653          } else {
654             clipplane_state[i][0] = STATE_CLIP_INTERNAL;
655             clipplane_state[i][1] = i;
656          }
657          _mesa_add_state_reference(params, clipplane_state[i]);
658       }
659 
660       if (nir->info.stage == MESA_SHADER_VERTEX ||
661           nir->info.stage == MESA_SHADER_TESS_EVAL) {
662          NIR_PASS_V(nir, nir_lower_clip_vs, ucp_enables,
663                     true, can_compact, clipplane_state);
664       } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
665          NIR_PASS_V(nir, nir_lower_clip_gs, ucp_enables,
666                     can_compact, clipplane_state);
667       }
668 
669       NIR_PASS_V(nir, nir_lower_io_to_temporaries,
670                  nir_shader_get_entrypoint(nir), true, false);
671       NIR_PASS_V(nir, nir_lower_global_vars_to_local);
672    }
673 }
674 
675 static struct st_common_variant *
st_create_common_variant(struct st_context * st,struct gl_program * prog,const struct st_common_variant_key * key)676 st_create_common_variant(struct st_context *st,
677                          struct gl_program *prog,
678                          const struct st_common_variant_key *key)
679 {
680    struct st_common_variant *v = CALLOC_STRUCT(st_common_variant);
681    struct pipe_shader_state state = {0};
682 
683    static const gl_state_index16 point_size_state[STATE_LENGTH] =
684       { STATE_POINT_SIZE_CLAMPED, 0 };
685    struct gl_program_parameter_list *params = prog->Parameters;
686 
687    v->key = *key;
688 
689    state.stream_output = prog->state.stream_output;
690 
691    bool finalize = false;
692 
693    state.type = PIPE_SHADER_IR_NIR;
694    state.ir.nir = get_nir_shader(st, prog);
695    const nir_shader_compiler_options *options = ((nir_shader *)state.ir.nir)->options;
696 
697    if (key->clamp_color) {
698       NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
699       finalize = true;
700    }
701    if (key->passthrough_edgeflags) {
702       NIR_PASS_V(state.ir.nir, nir_lower_passthrough_edgeflags);
703       finalize = true;
704    }
705 
706    if (key->export_point_size) {
707       /* if flag is set, shader must export psiz */
708       _mesa_add_state_reference(params, point_size_state);
709       NIR_PASS_V(state.ir.nir, nir_lower_point_size_mov,
710                   point_size_state);
711 
712       finalize = true;
713    }
714 
715    if (key->lower_ucp) {
716       assert(!options->unify_interfaces);
717       lower_ucp(st, state.ir.nir, key->lower_ucp, params);
718       finalize = true;
719    }
720 
721    if (st->emulate_gl_clamp &&
722          (key->gl_clamp[0] || key->gl_clamp[1] || key->gl_clamp[2])) {
723       nir_lower_tex_options tex_opts = {0};
724       tex_opts.saturate_s = key->gl_clamp[0];
725       tex_opts.saturate_t = key->gl_clamp[1];
726       tex_opts.saturate_r = key->gl_clamp[2];
727       NIR_PASS_V(state.ir.nir, nir_lower_tex, &tex_opts);
728    }
729 
730    if (finalize || !st->allow_st_finalize_nir_twice) {
731       char *msg = st_finalize_nir(st, prog, prog->shader_program, state.ir.nir,
732                                     true, false);
733       free(msg);
734 
735       /* Clip lowering and edgeflags may have introduced new varyings, so
736        * update the inputs_read/outputs_written. However, with
737        * unify_interfaces set (aka iris) the non-SSO varyings layout is
738        * decided at link time with outputs_written updated so the two line
739        * up.  A driver with this flag set may not use any of the lowering
740        * passes that would change the varyings, so skip to make sure we don't
741        * break its linkage.
742        */
743       if (!options->unify_interfaces) {
744          nir_shader_gather_info(state.ir.nir,
745                                  nir_shader_get_entrypoint(state.ir.nir));
746       }
747    }
748 
749    if (key->is_draw_shader)
750       v->base.driver_shader = draw_create_vertex_shader(st->draw, &state);
751    else
752       v->base.driver_shader = st_create_nir_shader(st, &state);
753 
754    return v;
755 }
756 
757 static void
st_add_variant(struct st_variant ** list,struct st_variant * v)758 st_add_variant(struct st_variant **list, struct st_variant *v)
759 {
760    struct st_variant *first = *list;
761 
762    /* Make sure that the default variant stays the first in the list, and insert
763     * any later variants in as the second entry.
764     */
765    if (first) {
766       v->next = first->next;
767       first->next = v;
768    } else {
769       *list = v;
770    }
771 }
772 
773 /**
774  * Find/create a vertex program variant.
775  */
776 struct st_common_variant *
st_get_common_variant(struct st_context * st,struct gl_program * prog,const struct st_common_variant_key * key)777 st_get_common_variant(struct st_context *st,
778                       struct gl_program *prog,
779                       const struct st_common_variant_key *key)
780 {
781    struct st_common_variant *v;
782 
783    /* Search for existing variant */
784    for (v = st_common_variant(prog->variants); v;
785         v = st_common_variant(v->base.next)) {
786       if (memcmp(&v->key, key, sizeof(*key)) == 0) {
787          break;
788       }
789    }
790 
791    if (!v) {
792       if (prog->variants != NULL) {
793          _mesa_perf_debug(st->ctx, MESA_DEBUG_SEVERITY_MEDIUM,
794                           "Compiling %s shader variant (%s%s%s%s%s%s)",
795                           _mesa_shader_stage_to_string(prog->info.stage),
796                           key->passthrough_edgeflags ? "edgeflags," : "",
797                           key->clamp_color ? "clamp_color," : "",
798                           key->export_point_size ? "point_size," : "",
799                           key->lower_ucp ? "ucp," : "",
800                           key->is_draw_shader ? "draw," : "",
801                           key->gl_clamp[0] || key->gl_clamp[1] || key->gl_clamp[2] ? "GL_CLAMP," : "");
802       }
803 
804       /* create now */
805       v = st_create_common_variant(st, prog, key);
806       if (v) {
807          v->base.st = key->st;
808 
809          if (prog->info.stage == MESA_SHADER_VERTEX) {
810             struct gl_vertex_program *vp = (struct gl_vertex_program *)prog;
811 
812             v->vert_attrib_mask =
813                vp->vert_attrib_mask |
814                (key->passthrough_edgeflags ? VERT_BIT_EDGEFLAG : 0);
815          }
816 
817          st_add_variant(&prog->variants, &v->base);
818       }
819    }
820 
821    return v;
822 }
823 
824 
825 /**
826  * Translate a non-GLSL Mesa fragment shader into a NIR shader.
827  */
828 static bool
st_translate_fragment_program(struct st_context * st,struct gl_program * fp)829 st_translate_fragment_program(struct st_context *st,
830                               struct gl_program *fp)
831 {
832    /* This determines which states will be updated when the assembly
833     * shader is bound.
834     *
835     * fragment.position and glDrawPixels always use constants.
836     */
837    fp->affected_states = ST_NEW_FS_STATE |
838                            ST_NEW_SAMPLE_SHADING |
839                            ST_NEW_FS_CONSTANTS;
840 
841    if (fp->ati_fs) {
842       /* Just set them for ATI_fs unconditionally. */
843       fp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
844                                  ST_NEW_FS_SAMPLERS;
845    } else {
846       /* ARB_fp */
847       if (fp->SamplersUsed)
848          fp->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
849                                     ST_NEW_FS_SAMPLERS;
850    }
851 
852    /* Translate to NIR.  ATI_fs translates at variant time. */
853    if (!fp->ati_fs) {
854       nir_shader *nir =
855          st_translate_prog_to_nir(st, fp, MESA_SHADER_FRAGMENT);
856 
857       if (fp->nir)
858          ralloc_free(fp->nir);
859       if (fp->serialized_nir) {
860          free(fp->serialized_nir);
861          fp->serialized_nir = NULL;
862       }
863       fp->state.type = PIPE_SHADER_IR_NIR;
864       fp->nir = nir;
865    }
866 
867    return true;
868 }
869 
870 static struct st_fp_variant *
st_create_fp_variant(struct st_context * st,struct gl_program * fp,const struct st_fp_variant_key * key)871 st_create_fp_variant(struct st_context *st,
872                      struct gl_program *fp,
873                      const struct st_fp_variant_key *key)
874 {
875    struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
876    struct pipe_shader_state state = {0};
877    struct gl_program_parameter_list *params = fp->Parameters;
878    static const gl_state_index16 texcoord_state[STATE_LENGTH] =
879       { STATE_CURRENT_ATTRIB, VERT_ATTRIB_TEX0 };
880    static const gl_state_index16 scale_state[STATE_LENGTH] =
881       { STATE_PT_SCALE };
882    static const gl_state_index16 bias_state[STATE_LENGTH] =
883       { STATE_PT_BIAS };
884    static const gl_state_index16 alpha_ref_state[STATE_LENGTH] =
885       { STATE_ALPHA_REF };
886 
887    if (!variant)
888       return NULL;
889 
890    /* Translate ATI_fs to NIR at variant time because that's when we have the
891     * texture types.
892     */
893    if (fp->ati_fs) {
894       const struct nir_shader_compiler_options *options =
895          st_get_nir_compiler_options(st, MESA_SHADER_FRAGMENT);
896 
897       nir_shader *s = st_translate_atifs_program(fp->ati_fs, key, fp, options);
898 
899       st_prog_to_nir_postprocess(st, s, fp);
900 
901       state.ir.nir = s;
902    } else {
903       state.ir.nir = get_nir_shader(st, fp);
904    }
905    state.type = PIPE_SHADER_IR_NIR;
906 
907    bool finalize = false;
908 
909    if (key->clamp_color) {
910       NIR_PASS_V(state.ir.nir, nir_lower_clamp_color_outputs);
911       finalize = true;
912    }
913 
914    if (key->lower_flatshade) {
915       NIR_PASS_V(state.ir.nir, nir_lower_flatshade);
916       finalize = true;
917    }
918 
919    if (key->lower_alpha_func != COMPARE_FUNC_ALWAYS) {
920       _mesa_add_state_reference(params, alpha_ref_state);
921       NIR_PASS_V(state.ir.nir, nir_lower_alpha_test, key->lower_alpha_func,
922                   false, alpha_ref_state);
923       finalize = true;
924    }
925 
926    if (key->lower_two_sided_color) {
927       bool face_sysval = st->ctx->Const.GLSLFrontFacingIsSysVal;
928       NIR_PASS_V(state.ir.nir, nir_lower_two_sided_color, face_sysval);
929       finalize = true;
930    }
931 
932    if (key->persample_shading) {
933       nir_shader *shader = state.ir.nir;
934       nir_foreach_shader_in_variable(var, shader)
935          var->data.sample = true;
936       finalize = true;
937    }
938 
939    if (key->lower_texcoord_replace) {
940       bool point_coord_is_sysval = st->ctx->Const.GLSLPointCoordIsSysVal;
941       NIR_PASS_V(state.ir.nir, nir_lower_texcoord_replace,
942                   key->lower_texcoord_replace, point_coord_is_sysval, false);
943       finalize = true;
944    }
945 
946    if (st->emulate_gl_clamp &&
947          (key->gl_clamp[0] || key->gl_clamp[1] || key->gl_clamp[2])) {
948       nir_lower_tex_options tex_opts = {0};
949       tex_opts.saturate_s = key->gl_clamp[0];
950       tex_opts.saturate_t = key->gl_clamp[1];
951       tex_opts.saturate_r = key->gl_clamp[2];
952       NIR_PASS_V(state.ir.nir, nir_lower_tex, &tex_opts);
953       finalize = true;
954    }
955 
956    assert(!(key->bitmap && key->drawpixels));
957 
958    /* glBitmap */
959    if (key->bitmap) {
960       nir_lower_bitmap_options options = {0};
961 
962       variant->bitmap_sampler = ffs(~fp->SamplersUsed) - 1;
963       options.sampler = variant->bitmap_sampler;
964       options.swizzle_xxxx = st->bitmap.tex_format == PIPE_FORMAT_R8_UNORM;
965 
966       NIR_PASS_V(state.ir.nir, nir_lower_bitmap, &options);
967       finalize = true;
968    }
969 
970    /* glDrawPixels (color only) */
971    if (key->drawpixels) {
972       nir_lower_drawpixels_options options = {{0}};
973       unsigned samplers_used = fp->SamplersUsed;
974 
975       /* Find the first unused slot. */
976       variant->drawpix_sampler = ffs(~samplers_used) - 1;
977       options.drawpix_sampler = variant->drawpix_sampler;
978       samplers_used |= (1 << variant->drawpix_sampler);
979 
980       options.pixel_maps = key->pixelMaps;
981       if (key->pixelMaps) {
982          variant->pixelmap_sampler = ffs(~samplers_used) - 1;
983          options.pixelmap_sampler = variant->pixelmap_sampler;
984       }
985 
986       options.scale_and_bias = key->scaleAndBias;
987       if (key->scaleAndBias) {
988          _mesa_add_state_reference(params, scale_state);
989          memcpy(options.scale_state_tokens, scale_state,
990                   sizeof(options.scale_state_tokens));
991          _mesa_add_state_reference(params, bias_state);
992          memcpy(options.bias_state_tokens, bias_state,
993                   sizeof(options.bias_state_tokens));
994       }
995 
996       _mesa_add_state_reference(params, texcoord_state);
997       memcpy(options.texcoord_state_tokens, texcoord_state,
998                sizeof(options.texcoord_state_tokens));
999 
1000       NIR_PASS_V(state.ir.nir, nir_lower_drawpixels, &options);
1001       finalize = true;
1002    }
1003 
1004    bool need_lower_tex_src_plane = false;
1005 
1006    if (unlikely(key->external.lower_nv12 || key->external.lower_iyuv ||
1007                   key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
1008                   key->external.lower_ayuv || key->external.lower_xyuv ||
1009                   key->external.lower_yuv || key->external.lower_yu_yv ||
1010                   key->external.lower_y41x)) {
1011 
1012       st_nir_lower_samplers(st->screen, state.ir.nir,
1013                               fp->shader_program, fp);
1014 
1015       nir_lower_tex_options options = {0};
1016       options.lower_y_uv_external = key->external.lower_nv12;
1017       options.lower_y_u_v_external = key->external.lower_iyuv;
1018       options.lower_xy_uxvx_external = key->external.lower_xy_uxvx;
1019       options.lower_yx_xuxv_external = key->external.lower_yx_xuxv;
1020       options.lower_ayuv_external = key->external.lower_ayuv;
1021       options.lower_xyuv_external = key->external.lower_xyuv;
1022       options.lower_yuv_external = key->external.lower_yuv;
1023       options.lower_yu_yv_external = key->external.lower_yu_yv;
1024       options.lower_y41x_external = key->external.lower_y41x;
1025       options.bt709_external = key->external.bt709;
1026       options.bt2020_external = key->external.bt2020;
1027       options.yuv_full_range_external = key->external.yuv_full_range;
1028       NIR_PASS_V(state.ir.nir, nir_lower_tex, &options);
1029       finalize = true;
1030       need_lower_tex_src_plane = true;
1031    }
1032 
1033    if (finalize || !st->allow_st_finalize_nir_twice) {
1034       char *msg = st_finalize_nir(st, fp, fp->shader_program, state.ir.nir,
1035                                     false, false);
1036       free(msg);
1037    }
1038 
1039    /* This pass needs to happen *after* nir_lower_sampler */
1040    if (unlikely(need_lower_tex_src_plane)) {
1041       NIR_PASS_V(state.ir.nir, st_nir_lower_tex_src_plane,
1042                   ~fp->SamplersUsed,
1043                   key->external.lower_nv12 | key->external.lower_xy_uxvx |
1044                      key->external.lower_yx_xuxv,
1045                   key->external.lower_iyuv);
1046       finalize = true;
1047    }
1048 
1049    if (finalize || !st->allow_st_finalize_nir_twice) {
1050       /* Some of the lowering above may have introduced new varyings */
1051       nir_shader_gather_info(state.ir.nir,
1052                               nir_shader_get_entrypoint(state.ir.nir));
1053 
1054       struct pipe_screen *screen = st->screen;
1055       if (screen->finalize_nir) {
1056          char *msg = screen->finalize_nir(screen, state.ir.nir);
1057          free(msg);
1058       }
1059    }
1060 
1061    variant->base.driver_shader = st_create_nir_shader(st, &state);
1062    variant->key = *key;
1063 
1064    return variant;
1065 }
1066 
1067 /**
1068  * Translate fragment program if needed.
1069  */
1070 struct st_fp_variant *
st_get_fp_variant(struct st_context * st,struct gl_program * fp,const struct st_fp_variant_key * key)1071 st_get_fp_variant(struct st_context *st,
1072                   struct gl_program *fp,
1073                   const struct st_fp_variant_key *key)
1074 {
1075    struct st_fp_variant *fpv;
1076 
1077    /* Search for existing variant */
1078    for (fpv = st_fp_variant(fp->variants); fpv;
1079         fpv = st_fp_variant(fpv->base.next)) {
1080       if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
1081          break;
1082       }
1083    }
1084 
1085    if (!fpv) {
1086       /* create new */
1087 
1088       if (fp->variants != NULL) {
1089          _mesa_perf_debug(st->ctx, MESA_DEBUG_SEVERITY_MEDIUM,
1090                           "Compiling fragment shader variant (%s%s%s%s%s%s%s%s%s%s%s%s%s)",
1091                           key->bitmap ? "bitmap," : "",
1092                           key->drawpixels ? "drawpixels," : "",
1093                           key->scaleAndBias ? "scale_bias," : "",
1094                           key->pixelMaps ? "pixel_maps," : "",
1095                           key->clamp_color ? "clamp_color," : "",
1096                           key->persample_shading ? "persample_shading," : "",
1097                           key->fog ? "fog," : "",
1098                           key->lower_two_sided_color ? "twoside," : "",
1099                           key->lower_flatshade ? "flatshade," : "",
1100                           key->lower_texcoord_replace ? "texcoord_replace," : "",
1101                           key->lower_alpha_func ? "alpha_compare," : "",
1102                           /* skipped ATI_fs targets */
1103                           fp->ExternalSamplersUsed ? "external?," : "",
1104                           key->gl_clamp[0] || key->gl_clamp[1] || key->gl_clamp[2] ? "GL_CLAMP," : "");
1105       }
1106 
1107       fpv = st_create_fp_variant(st, fp, key);
1108       if (fpv) {
1109          fpv->base.st = key->st;
1110 
1111          st_add_variant(&fp->variants, &fpv->base);
1112       }
1113    }
1114 
1115    return fpv;
1116 }
1117 
1118 /**
1119  * Vert/Geom/Frag programs have per-context variants.  Free all the
1120  * variants attached to the given program which match the given context.
1121  */
1122 static void
destroy_program_variants(struct st_context * st,struct gl_program * p)1123 destroy_program_variants(struct st_context *st, struct gl_program *p)
1124 {
1125    if (!p || p == &_mesa_DummyProgram)
1126       return;
1127 
1128    struct st_variant *v, **prevPtr = &p->variants;
1129    bool unbound = false;
1130 
1131    for (v = p->variants; v; ) {
1132       struct st_variant *next = v->next;
1133       if (v->st == st) {
1134          if (!unbound) {
1135             st_unbind_program(st, p);
1136             unbound = true;
1137          }
1138 
1139          /* unlink from list */
1140          *prevPtr = next;
1141          /* destroy this variant */
1142          delete_variant(st, v, p->Target);
1143       }
1144       else {
1145          prevPtr = &v->next;
1146       }
1147       v = next;
1148    }
1149 }
1150 
1151 
1152 /**
1153  * Callback for _mesa_HashWalk.  Free all the shader's program variants
1154  * which match the given context.
1155  */
1156 static void
destroy_shader_program_variants_cb(void * data,void * userData)1157 destroy_shader_program_variants_cb(void *data, void *userData)
1158 {
1159    struct st_context *st = (struct st_context *) userData;
1160    struct gl_shader *shader = (struct gl_shader *) data;
1161 
1162    switch (shader->Type) {
1163    case GL_SHADER_PROGRAM_MESA:
1164       {
1165          struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1166          GLuint i;
1167 
1168          for (i = 0; i < ARRAY_SIZE(shProg->_LinkedShaders); i++) {
1169             if (shProg->_LinkedShaders[i])
1170                destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1171          }
1172       }
1173       break;
1174    case GL_VERTEX_SHADER:
1175    case GL_FRAGMENT_SHADER:
1176    case GL_GEOMETRY_SHADER:
1177    case GL_TESS_CONTROL_SHADER:
1178    case GL_TESS_EVALUATION_SHADER:
1179    case GL_COMPUTE_SHADER:
1180       break;
1181    default:
1182       assert(0);
1183    }
1184 }
1185 
1186 
1187 /**
1188  * Callback for _mesa_HashWalk.  Free all the program variants which match
1189  * the given context.
1190  */
1191 static void
destroy_program_variants_cb(void * data,void * userData)1192 destroy_program_variants_cb(void *data, void *userData)
1193 {
1194    struct st_context *st = (struct st_context *) userData;
1195    struct gl_program *program = (struct gl_program *) data;
1196    destroy_program_variants(st, program);
1197 }
1198 
1199 
1200 /**
1201  * Walk over all shaders and programs to delete any variants which
1202  * belong to the given context.
1203  * This is called during context tear-down.
1204  */
1205 void
st_destroy_program_variants(struct st_context * st)1206 st_destroy_program_variants(struct st_context *st)
1207 {
1208    /* If shaders can be shared with other contexts, the last context will
1209     * call DeleteProgram on all shaders, releasing everything.
1210     */
1211    if (st->has_shareable_shaders)
1212       return;
1213 
1214    /* ARB vert/frag program */
1215    _mesa_HashWalk(st->ctx->Shared->Programs,
1216                   destroy_program_variants_cb, st);
1217 
1218    /* GLSL vert/frag/geom shaders */
1219    _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1220                   destroy_shader_program_variants_cb, st);
1221 }
1222 
1223 bool
st_can_add_pointsize_to_program(struct st_context * st,struct gl_program * prog)1224 st_can_add_pointsize_to_program(struct st_context *st, struct gl_program *prog)
1225 {
1226    nir_shader *nir = prog->nir;
1227    if (!nir)
1228       return true; //fixedfunction
1229    assert(nir->info.stage == MESA_SHADER_VERTEX ||
1230           nir->info.stage == MESA_SHADER_TESS_EVAL ||
1231           nir->info.stage == MESA_SHADER_GEOMETRY);
1232    if (nir->info.outputs_written & VARYING_BIT_PSIZ)
1233       return false;
1234    unsigned max_components = nir->info.stage == MESA_SHADER_GEOMETRY ?
1235                              st->ctx->Const.MaxGeometryTotalOutputComponents :
1236                              st->ctx->Const.Program[nir->info.stage].MaxOutputComponents;
1237    unsigned num_components = 0;
1238    unsigned needed_components = nir->info.stage == MESA_SHADER_GEOMETRY ? nir->info.gs.vertices_out : 1;
1239    nir_foreach_shader_out_variable(var, nir) {
1240       num_components += glsl_count_dword_slots(var->type, false);
1241    }
1242    /* Ensure that there is enough attribute space to emit at least one primitive */
1243    if (nir->info.stage == MESA_SHADER_GEOMETRY) {
1244       if (num_components + needed_components > st->ctx->Const.Program[nir->info.stage].MaxOutputComponents)
1245          return false;
1246       num_components *= nir->info.gs.vertices_out;
1247    }
1248 
1249    return num_components + needed_components <= max_components;
1250 }
1251 
1252 /**
1253  * Compile one shader variant.
1254  */
1255 static void
st_precompile_shader_variant(struct st_context * st,struct gl_program * prog)1256 st_precompile_shader_variant(struct st_context *st,
1257                              struct gl_program *prog)
1258 {
1259    switch (prog->Target) {
1260    case GL_VERTEX_PROGRAM_ARB:
1261    case GL_TESS_CONTROL_PROGRAM_NV:
1262    case GL_TESS_EVALUATION_PROGRAM_NV:
1263    case GL_GEOMETRY_PROGRAM_NV:
1264    case GL_COMPUTE_PROGRAM_NV: {
1265       struct st_common_variant_key key;
1266 
1267       memset(&key, 0, sizeof(key));
1268 
1269       if (st->ctx->API == API_OPENGL_COMPAT &&
1270           st->clamp_vert_color_in_shader &&
1271           (prog->info.outputs_written & (VARYING_SLOT_COL0 |
1272                                          VARYING_SLOT_COL1 |
1273                                          VARYING_SLOT_BFC0 |
1274                                          VARYING_SLOT_BFC1))) {
1275          key.clamp_color = true;
1276       }
1277 
1278       key.st = st->has_shareable_shaders ? NULL : st;
1279       st_get_common_variant(st, prog, &key);
1280       break;
1281    }
1282 
1283    case GL_FRAGMENT_PROGRAM_ARB: {
1284       struct st_fp_variant_key key;
1285 
1286       memset(&key, 0, sizeof(key));
1287 
1288       key.st = st->has_shareable_shaders ? NULL : st;
1289       key.lower_alpha_func = COMPARE_FUNC_ALWAYS;
1290       if (prog->ati_fs) {
1291          for (int i = 0; i < ARRAY_SIZE(key.texture_index); i++)
1292             key.texture_index[i] = TEXTURE_2D_INDEX;
1293       }
1294       st_get_fp_variant(st, prog, &key);
1295       break;
1296    }
1297 
1298    default:
1299       assert(0);
1300    }
1301 }
1302 
1303 void
st_serialize_nir(struct gl_program * prog)1304 st_serialize_nir(struct gl_program *prog)
1305 {
1306    if (!prog->serialized_nir) {
1307       struct blob blob;
1308       size_t size;
1309 
1310       blob_init(&blob);
1311       nir_serialize(&blob, prog->nir, false);
1312       blob_finish_get_buffer(&blob, &prog->serialized_nir, &size);
1313       prog->serialized_nir_size = size;
1314    }
1315 }
1316 
1317 void
st_finalize_program(struct st_context * st,struct gl_program * prog)1318 st_finalize_program(struct st_context *st, struct gl_program *prog)
1319 {
1320    if (st->current_program[prog->info.stage] == prog) {
1321       if (prog->info.stage == MESA_SHADER_VERTEX) {
1322          st->ctx->Array.NewVertexElements = true;
1323          st->dirty |= ST_NEW_VERTEX_PROGRAM(st, prog);
1324       } else {
1325          st->dirty |= prog->affected_states;
1326       }
1327    }
1328 
1329    if (prog->nir) {
1330       nir_sweep(prog->nir);
1331 
1332       /* This is only needed for ARB_vp/fp programs and when the disk cache
1333        * is disabled. If the disk cache is enabled, GLSL programs are
1334        * serialized in write_nir_to_cache.
1335        */
1336       st_serialize_nir(prog);
1337    }
1338 
1339    /* Always create the default variant of the program. */
1340    st_precompile_shader_variant(st, prog);
1341 }
1342 
1343 /**
1344  * Called when the program's text/code is changed.  We have to free
1345  * all shader variants and corresponding gallium shaders when this happens.
1346  */
1347 GLboolean
st_program_string_notify(struct gl_context * ctx,GLenum target,struct gl_program * prog)1348 st_program_string_notify( struct gl_context *ctx,
1349                           GLenum target,
1350                           struct gl_program *prog )
1351 {
1352    struct st_context *st = st_context(ctx);
1353 
1354    /* GLSL-to-NIR should not end up here. */
1355    assert(!prog->shader_program);
1356 
1357    st_release_variants(st, prog);
1358 
1359    if (target == GL_FRAGMENT_PROGRAM_ARB ||
1360        target == GL_FRAGMENT_SHADER_ATI) {
1361       if (target == GL_FRAGMENT_SHADER_ATI) {
1362          assert(prog->ati_fs);
1363          assert(prog->ati_fs->Program == prog);
1364 
1365          st_init_atifs_prog(ctx, prog);
1366       }
1367 
1368       if (!st_translate_fragment_program(st, prog))
1369          return false;
1370    } else if (target == GL_VERTEX_PROGRAM_ARB) {
1371       if (!st_translate_vertex_program(st, prog))
1372          return false;
1373       if (st->lower_point_size && st_can_add_pointsize_to_program(st, prog)) {
1374          prog->skip_pointsize_xfb = true;
1375          NIR_PASS_V(prog->nir, st_nir_add_point_size);
1376       }
1377    }
1378 
1379    st_finalize_program(st, prog);
1380    return GL_TRUE;
1381 }
1382