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