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 "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 NIR_PASS(_, nir, nir_lower_compute_system_values, NULL);
386
387 /* Optimise NIR */
388 NIR_PASS(_, nir, nir_opt_constant_folding);
389 gl_nir_opts(nir);
390 st_finalize_nir_before_variants(nir);
391
392 if (st->allow_st_finalize_nir_twice) {
393 char *msg = st_finalize_nir(st, prog, NULL, nir, true, true);
394 free(msg);
395 }
396
397 nir_validate_shader(nir, "after st/glsl finalize_nir");
398 }
399
400 /**
401 * Translate ARB (asm) program to NIR
402 */
403 static nir_shader *
st_translate_prog_to_nir(struct st_context * st,struct gl_program * prog,gl_shader_stage stage)404 st_translate_prog_to_nir(struct st_context *st, struct gl_program *prog,
405 gl_shader_stage stage)
406 {
407 const struct nir_shader_compiler_options *options =
408 st_get_nir_compiler_options(st, prog->info.stage);
409
410 /* Translate to NIR */
411 nir_shader *nir = prog_to_nir(st->ctx, prog, options);
412
413 return nir;
414 }
415
416 /**
417 * Prepare st_vertex_program info.
418 *
419 * attrib_to_index is an optional mapping from a vertex attrib to a shader
420 * input index.
421 */
422 void
st_prepare_vertex_program(struct gl_program * prog)423 st_prepare_vertex_program(struct gl_program *prog)
424 {
425 struct gl_vertex_program *stvp = (struct gl_vertex_program *)prog;
426
427 stvp->num_inputs = util_bitcount64(prog->info.inputs_read);
428 stvp->vert_attrib_mask = prog->info.inputs_read;
429
430 /* Compute mapping of vertex program outputs to slots. */
431 memset(stvp->result_to_output, ~0, sizeof(stvp->result_to_output));
432 unsigned num_outputs = 0;
433 for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
434 if (prog->info.outputs_written & BITFIELD64_BIT(attr))
435 stvp->result_to_output[attr] = num_outputs++;
436 }
437 /* pre-setup potentially unused edgeflag output */
438 stvp->result_to_output[VARYING_SLOT_EDGE] = num_outputs;
439 }
440
441 void
st_translate_stream_output_info(struct gl_program * prog)442 st_translate_stream_output_info(struct gl_program *prog)
443 {
444 struct gl_transform_feedback_info *info = prog->sh.LinkedTransformFeedback;
445 if (!info)
446 return;
447
448 /* Determine the (default) output register mapping for each output. */
449 unsigned num_outputs = 0;
450 uint8_t output_mapping[VARYING_SLOT_TESS_MAX];
451 memset(output_mapping, 0, sizeof(output_mapping));
452
453 for (unsigned attr = 0; attr < VARYING_SLOT_MAX; attr++) {
454 /* this output was added by mesa/st and should not be tracked for xfb:
455 * drivers must check var->data.explicit_location to find the original output
456 * and only emit that one for xfb
457 */
458 if (prog->skip_pointsize_xfb && attr == VARYING_SLOT_PSIZ)
459 continue;
460 if (prog->info.outputs_written & BITFIELD64_BIT(attr))
461 output_mapping[attr] = num_outputs++;
462 }
463
464 /* Translate stream output info. */
465 struct pipe_stream_output_info *so_info =
466 &prog->state.stream_output;
467
468 if (!num_outputs) {
469 so_info->num_outputs = 0;
470 return;
471 }
472
473 for (unsigned i = 0; i < info->NumOutputs; i++) {
474 so_info->output[i].register_index =
475 output_mapping[info->Outputs[i].OutputRegister];
476 so_info->output[i].start_component = info->Outputs[i].ComponentOffset;
477 so_info->output[i].num_components = info->Outputs[i].NumComponents;
478 so_info->output[i].output_buffer = info->Outputs[i].OutputBuffer;
479 so_info->output[i].dst_offset = info->Outputs[i].DstOffset;
480 so_info->output[i].stream = info->Outputs[i].StreamId;
481 }
482
483 for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
484 so_info->stride[i] = info->Buffers[i].Stride;
485 }
486 so_info->num_outputs = info->NumOutputs;
487 }
488
489 /**
490 * Creates a driver shader from a NIR shader. Takes ownership of the
491 * passed nir_shader.
492 */
493 struct pipe_shader_state *
st_create_nir_shader(struct st_context * st,struct pipe_shader_state * state)494 st_create_nir_shader(struct st_context *st, struct pipe_shader_state *state)
495 {
496 struct pipe_context *pipe = st->pipe;
497
498 assert(state->type == PIPE_SHADER_IR_NIR);
499 nir_shader *nir = state->ir.nir;
500 gl_shader_stage stage = nir->info.stage;
501
502 if (ST_DEBUG & DEBUG_PRINT_IR) {
503 fprintf(stderr, "NIR before handing off to driver:\n");
504 nir_print_shader(nir, stderr);
505 }
506
507 struct pipe_shader_state *shader;
508 switch (stage) {
509 case MESA_SHADER_VERTEX:
510 shader = pipe->create_vs_state(pipe, state);
511 break;
512 case MESA_SHADER_TESS_CTRL:
513 shader = pipe->create_tcs_state(pipe, state);
514 break;
515 case MESA_SHADER_TESS_EVAL:
516 shader = pipe->create_tes_state(pipe, state);
517 break;
518 case MESA_SHADER_GEOMETRY:
519 shader = pipe->create_gs_state(pipe, state);
520 break;
521 case MESA_SHADER_FRAGMENT:
522 shader = pipe->create_fs_state(pipe, state);
523 break;
524 case MESA_SHADER_COMPUTE: {
525 /* We'd like to use this for all stages but we need to rework streamout in
526 * gallium first.
527 */
528 shader = pipe_shader_from_nir(pipe, nir);
529 break;
530 }
531 default:
532 unreachable("unsupported shader stage");
533 return NULL;
534 }
535
536 return shader;
537 }
538
539 /**
540 * Translate a vertex program.
541 */
542 static bool
st_translate_vertex_program(struct st_context * st,struct gl_program * prog)543 st_translate_vertex_program(struct st_context *st,
544 struct gl_program *prog)
545 {
546 /* This determines which states will be updated when the assembly
547 * shader is bound.
548 */
549 prog->affected_states = ST_NEW_VS_STATE |
550 ST_NEW_RASTERIZER |
551 ST_NEW_VERTEX_ARRAYS;
552
553 if (prog->Parameters->NumParameters)
554 prog->affected_states |= ST_NEW_VS_CONSTANTS;
555
556 if (prog->arb.Instructions && prog->nir)
557 ralloc_free(prog->nir);
558
559 if (prog->serialized_nir) {
560 free(prog->serialized_nir);
561 prog->serialized_nir = NULL;
562 }
563
564 prog->state.type = PIPE_SHADER_IR_NIR;
565 if (prog->arb.Instructions)
566 prog->nir = st_translate_prog_to_nir(st, prog,
567 MESA_SHADER_VERTEX);
568 st_prog_to_nir_postprocess(st, prog->nir, prog);
569 prog->info = prog->nir->info;
570
571 st_prepare_vertex_program(prog);
572 return true;
573 }
574
575 static struct nir_shader *
get_nir_shader(struct st_context * st,struct gl_program * prog)576 get_nir_shader(struct st_context *st, struct gl_program *prog)
577 {
578 if (prog->nir) {
579 nir_shader *nir = prog->nir;
580
581 /* The first shader variant takes ownership of NIR, so that there is
582 * no cloning. Additional shader variants are always generated from
583 * serialized NIR to save memory.
584 */
585 prog->nir = NULL;
586 assert(prog->serialized_nir && prog->serialized_nir_size);
587 return nir;
588 }
589
590 struct blob_reader blob_reader;
591 const struct nir_shader_compiler_options *options =
592 st_get_nir_compiler_options(st, prog->info.stage);
593
594 blob_reader_init(&blob_reader, prog->serialized_nir, prog->serialized_nir_size);
595 return nir_deserialize(NULL, options, &blob_reader);
596 }
597
598 static void
lower_ucp(struct st_context * st,struct nir_shader * nir,unsigned ucp_enables,struct gl_program_parameter_list * params)599 lower_ucp(struct st_context *st,
600 struct nir_shader *nir,
601 unsigned ucp_enables,
602 struct gl_program_parameter_list *params)
603 {
604 if (nir->info.outputs_written & VARYING_BIT_CLIP_DIST0)
605 NIR_PASS(_, nir, nir_lower_clip_disable, ucp_enables);
606 else {
607 struct pipe_screen *screen = st->screen;
608 bool can_compact = screen->get_param(screen,
609 PIPE_CAP_NIR_COMPACT_ARRAYS);
610 bool use_eye = st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] != NULL;
611
612 gl_state_index16 clipplane_state[MAX_CLIP_PLANES][STATE_LENGTH] = {{0}};
613 for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
614 if (use_eye) {
615 clipplane_state[i][0] = STATE_CLIPPLANE;
616 clipplane_state[i][1] = i;
617 } else {
618 clipplane_state[i][0] = STATE_CLIP_INTERNAL;
619 clipplane_state[i][1] = i;
620 }
621 _mesa_add_state_reference(params, clipplane_state[i]);
622 }
623
624 if (nir->info.stage == MESA_SHADER_VERTEX ||
625 nir->info.stage == MESA_SHADER_TESS_EVAL) {
626 NIR_PASS(_, nir, nir_lower_clip_vs, ucp_enables,
627 true, can_compact, clipplane_state);
628 } else if (nir->info.stage == MESA_SHADER_GEOMETRY) {
629 NIR_PASS(_, nir, nir_lower_clip_gs, ucp_enables,
630 can_compact, clipplane_state);
631 }
632
633 NIR_PASS(_, nir, nir_lower_io_to_temporaries,
634 nir_shader_get_entrypoint(nir), true, false);
635 NIR_PASS(_, nir, nir_lower_global_vars_to_local);
636 }
637 }
638
639 static struct st_common_variant *
st_create_common_variant(struct st_context * st,struct gl_program * prog,const struct st_common_variant_key * key)640 st_create_common_variant(struct st_context *st,
641 struct gl_program *prog,
642 const struct st_common_variant_key *key)
643 {
644 MESA_TRACE_FUNC();
645
646 struct st_common_variant *v = CALLOC_STRUCT(st_common_variant);
647 struct pipe_shader_state state = {0};
648
649 static const gl_state_index16 point_size_state[STATE_LENGTH] =
650 { STATE_POINT_SIZE_CLAMPED, 0 };
651 struct gl_program_parameter_list *params = prog->Parameters;
652
653 v->key = *key;
654
655 state.stream_output = prog->state.stream_output;
656
657 bool finalize = false;
658
659 state.type = PIPE_SHADER_IR_NIR;
660 state.ir.nir = get_nir_shader(st, prog);
661 const nir_shader_compiler_options *options = ((nir_shader *)state.ir.nir)->options;
662
663 if (key->clamp_color) {
664 NIR_PASS(_, state.ir.nir, nir_lower_clamp_color_outputs);
665 finalize = true;
666 }
667 if (key->passthrough_edgeflags) {
668 NIR_PASS(_, state.ir.nir, nir_lower_passthrough_edgeflags);
669 finalize = true;
670 }
671
672 if (key->export_point_size) {
673 /* if flag is set, shader must export psiz */
674 _mesa_add_state_reference(params, point_size_state);
675 NIR_PASS(_, state.ir.nir, nir_lower_point_size_mov,
676 point_size_state);
677
678 finalize = true;
679 }
680
681 if (key->lower_ucp) {
682 assert(!options->unify_interfaces);
683 lower_ucp(st, state.ir.nir, key->lower_ucp, params);
684 finalize = true;
685 }
686
687 if (st->emulate_gl_clamp &&
688 (key->gl_clamp[0] || key->gl_clamp[1] || key->gl_clamp[2])) {
689 nir_lower_tex_options tex_opts = {0};
690 tex_opts.saturate_s = key->gl_clamp[0];
691 tex_opts.saturate_t = key->gl_clamp[1];
692 tex_opts.saturate_r = key->gl_clamp[2];
693 NIR_PASS(_, state.ir.nir, nir_lower_tex, &tex_opts);
694 }
695
696 if (finalize || !st->allow_st_finalize_nir_twice) {
697 char *msg = st_finalize_nir(st, prog, prog->shader_program, state.ir.nir,
698 true, false);
699 free(msg);
700
701 /* Clip lowering and edgeflags may have introduced new varyings, so
702 * update the inputs_read/outputs_written. However, with
703 * unify_interfaces set (aka iris) the non-SSO varyings layout is
704 * decided at link time with outputs_written updated so the two line
705 * up. A driver with this flag set may not use any of the lowering
706 * passes that would change the varyings, so skip to make sure we don't
707 * break its linkage.
708 */
709 if (!options->unify_interfaces) {
710 nir_shader_gather_info(state.ir.nir,
711 nir_shader_get_entrypoint(state.ir.nir));
712 }
713 }
714
715 if (key->is_draw_shader) {
716 NIR_PASS(_, state.ir.nir, gl_nir_lower_images, false);
717 v->base.driver_shader = draw_create_vertex_shader(st->draw, &state);
718 }
719 else
720 v->base.driver_shader = st_create_nir_shader(st, &state);
721
722 return v;
723 }
724
725 static void
st_add_variant(struct st_variant ** list,struct st_variant * v)726 st_add_variant(struct st_variant **list, struct st_variant *v)
727 {
728 struct st_variant *first = *list;
729
730 /* Make sure that the default variant stays the first in the list, and insert
731 * any later variants in as the second entry.
732 */
733 if (first) {
734 v->next = first->next;
735 first->next = v;
736 } else {
737 *list = v;
738 }
739 }
740
741 /**
742 * Find/create a vertex program variant.
743 */
744 struct st_common_variant *
st_get_common_variant(struct st_context * st,struct gl_program * prog,const struct st_common_variant_key * key)745 st_get_common_variant(struct st_context *st,
746 struct gl_program *prog,
747 const struct st_common_variant_key *key)
748 {
749 struct st_common_variant *v;
750
751 /* Search for existing variant */
752 for (v = st_common_variant(prog->variants); v;
753 v = st_common_variant(v->base.next)) {
754 if (memcmp(&v->key, key, sizeof(*key)) == 0) {
755 break;
756 }
757 }
758
759 if (!v) {
760 if (prog->variants != NULL) {
761 _mesa_perf_debug(st->ctx, MESA_DEBUG_SEVERITY_MEDIUM,
762 "Compiling %s shader variant (%s%s%s%s%s%s)",
763 _mesa_shader_stage_to_string(prog->info.stage),
764 key->passthrough_edgeflags ? "edgeflags," : "",
765 key->clamp_color ? "clamp_color," : "",
766 key->export_point_size ? "point_size," : "",
767 key->lower_ucp ? "ucp," : "",
768 key->is_draw_shader ? "draw," : "",
769 key->gl_clamp[0] || key->gl_clamp[1] || key->gl_clamp[2] ? "GL_CLAMP," : "");
770 }
771
772 /* create now */
773 v = st_create_common_variant(st, prog, key);
774 if (v) {
775 v->base.st = key->st;
776
777 if (prog->info.stage == MESA_SHADER_VERTEX) {
778 struct gl_vertex_program *vp = (struct gl_vertex_program *)prog;
779
780 v->vert_attrib_mask =
781 vp->vert_attrib_mask |
782 (key->passthrough_edgeflags ? VERT_BIT_EDGEFLAG : 0);
783 }
784
785 st_add_variant(&prog->variants, &v->base);
786 }
787 }
788
789 return v;
790 }
791
792
793 /**
794 * Translate a non-GLSL Mesa fragment shader into a NIR shader.
795 */
796 static bool
st_translate_fragment_program(struct st_context * st,struct gl_program * prog)797 st_translate_fragment_program(struct st_context *st,
798 struct gl_program *prog)
799 {
800 /* This determines which states will be updated when the assembly
801 * shader is bound.
802 *
803 * fragment.position and glDrawPixels always use constants.
804 */
805 prog->affected_states = ST_NEW_FS_STATE |
806 ST_NEW_SAMPLE_SHADING |
807 ST_NEW_FS_CONSTANTS;
808
809 if (prog->ati_fs) {
810 /* Just set them for ATI_fs unconditionally. */
811 prog->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
812 ST_NEW_FS_SAMPLERS;
813 } else {
814 /* ARB_fp */
815 if (prog->SamplersUsed)
816 prog->affected_states |= ST_NEW_FS_SAMPLER_VIEWS |
817 ST_NEW_FS_SAMPLERS;
818 }
819
820 /* Translate to NIR. */
821 if (prog->nir && prog->arb.Instructions)
822 ralloc_free(prog->nir);
823
824 if (prog->serialized_nir) {
825 free(prog->serialized_nir);
826 prog->serialized_nir = NULL;
827 }
828
829 prog->state.type = PIPE_SHADER_IR_NIR;
830 if (prog->arb.Instructions) {
831 prog->nir = st_translate_prog_to_nir(st, prog,
832 MESA_SHADER_FRAGMENT);
833 } else if (prog->ati_fs) {
834 const struct nir_shader_compiler_options *options =
835 st_get_nir_compiler_options(st, MESA_SHADER_FRAGMENT);
836
837 assert(!prog->nir);
838 prog->nir = st_translate_atifs_program(prog->ati_fs, prog, options);
839 }
840 st_prog_to_nir_postprocess(st, prog->nir, prog);
841
842 prog->info = prog->nir->info;
843 if (prog->ati_fs) {
844 /* ATI_fs will lower fixed function fog at variant time, after the FF vertex
845 * prog has been generated. So we have to always declare a read of FOGC so
846 * that FF vp feeds it to us just in case.
847 */
848 prog->info.inputs_read |= VARYING_BIT_FOGC;
849 }
850
851 return true;
852 }
853
854 static struct st_fp_variant *
st_create_fp_variant(struct st_context * st,struct gl_program * fp,const struct st_fp_variant_key * key)855 st_create_fp_variant(struct st_context *st,
856 struct gl_program *fp,
857 const struct st_fp_variant_key *key)
858 {
859 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
860 struct pipe_shader_state state = {0};
861 struct gl_program_parameter_list *params = fp->Parameters;
862 static const gl_state_index16 texcoord_state[STATE_LENGTH] =
863 { STATE_CURRENT_ATTRIB, VERT_ATTRIB_TEX0 };
864 static const gl_state_index16 scale_state[STATE_LENGTH] =
865 { STATE_PT_SCALE };
866 static const gl_state_index16 bias_state[STATE_LENGTH] =
867 { STATE_PT_BIAS };
868 static const gl_state_index16 alpha_ref_state[STATE_LENGTH] =
869 { STATE_ALPHA_REF };
870
871 if (!variant)
872 return NULL;
873
874 MESA_TRACE_FUNC();
875
876 /* Translate ATI_fs to NIR at variant time because that's when we have the
877 * texture types.
878 */
879 state.ir.nir = get_nir_shader(st, fp);
880 state.type = PIPE_SHADER_IR_NIR;
881
882 bool finalize = false;
883
884 if (fp->ati_fs) {
885 if (key->fog) {
886 NIR_PASS(_, state.ir.nir, st_nir_lower_fog, key->fog, fp->Parameters);
887 NIR_PASS(_, state.ir.nir, nir_lower_io_to_temporaries,
888 nir_shader_get_entrypoint(state.ir.nir),
889 true, false);
890 nir_lower_global_vars_to_local(state.ir.nir);
891 }
892
893 NIR_PASS(_, state.ir.nir, st_nir_lower_atifs_samplers, key->texture_index);
894
895 finalize = true;
896 }
897
898 if (key->clamp_color) {
899 NIR_PASS(_, state.ir.nir, nir_lower_clamp_color_outputs);
900 finalize = true;
901 }
902
903 if (key->lower_flatshade) {
904 NIR_PASS(_, state.ir.nir, nir_lower_flatshade);
905 finalize = true;
906 }
907
908 if (key->lower_alpha_func != COMPARE_FUNC_ALWAYS) {
909 _mesa_add_state_reference(params, alpha_ref_state);
910 NIR_PASS(_, state.ir.nir, nir_lower_alpha_test, key->lower_alpha_func,
911 false, alpha_ref_state);
912 finalize = true;
913 }
914
915 if (key->lower_two_sided_color) {
916 bool face_sysval = st->ctx->Const.GLSLFrontFacingIsSysVal;
917 NIR_PASS(_, state.ir.nir, nir_lower_two_sided_color, face_sysval);
918 finalize = true;
919 }
920
921 if (key->persample_shading) {
922 nir_shader *shader = state.ir.nir;
923 nir_foreach_shader_in_variable(var, shader)
924 var->data.sample = true;
925
926 /* In addition to requiring per-sample interpolation, sample shading
927 * changes the behaviour of gl_SampleMaskIn, so we need per-sample shading
928 * even if there are no shader-in variables at all. In that case,
929 * uses_sample_shading won't be set by glsl_to_nir. We need to do so here.
930 */
931 shader->info.fs.uses_sample_shading = true;
932
933 finalize = true;
934 }
935
936 if (st->emulate_gl_clamp &&
937 (key->gl_clamp[0] || key->gl_clamp[1] || key->gl_clamp[2])) {
938 nir_lower_tex_options tex_opts = {0};
939 tex_opts.saturate_s = key->gl_clamp[0];
940 tex_opts.saturate_t = key->gl_clamp[1];
941 tex_opts.saturate_r = key->gl_clamp[2];
942 NIR_PASS(_, state.ir.nir, nir_lower_tex, &tex_opts);
943 finalize = true;
944 }
945
946 assert(!(key->bitmap && key->drawpixels));
947
948 /* glBitmap */
949 if (key->bitmap) {
950 nir_lower_bitmap_options options = {0};
951
952 variant->bitmap_sampler = ffs(~fp->SamplersUsed) - 1;
953 options.sampler = variant->bitmap_sampler;
954 options.swizzle_xxxx = st->bitmap.tex_format == PIPE_FORMAT_R8_UNORM;
955
956 NIR_PASS(_, state.ir.nir, nir_lower_bitmap, &options);
957 finalize = true;
958 }
959
960 /* glDrawPixels (color only) */
961 if (key->drawpixels) {
962 nir_lower_drawpixels_options options = {{0}};
963 unsigned samplers_used = fp->SamplersUsed;
964
965 /* Find the first unused slot. */
966 variant->drawpix_sampler = ffs(~samplers_used) - 1;
967 options.drawpix_sampler = variant->drawpix_sampler;
968 samplers_used |= (1 << variant->drawpix_sampler);
969
970 options.pixel_maps = key->pixelMaps;
971 if (key->pixelMaps) {
972 variant->pixelmap_sampler = ffs(~samplers_used) - 1;
973 options.pixelmap_sampler = variant->pixelmap_sampler;
974 }
975
976 options.scale_and_bias = key->scaleAndBias;
977 if (key->scaleAndBias) {
978 _mesa_add_state_reference(params, scale_state);
979 memcpy(options.scale_state_tokens, scale_state,
980 sizeof(options.scale_state_tokens));
981 _mesa_add_state_reference(params, bias_state);
982 memcpy(options.bias_state_tokens, bias_state,
983 sizeof(options.bias_state_tokens));
984 }
985
986 _mesa_add_state_reference(params, texcoord_state);
987 memcpy(options.texcoord_state_tokens, texcoord_state,
988 sizeof(options.texcoord_state_tokens));
989
990 NIR_PASS(_, state.ir.nir, nir_lower_drawpixels, &options);
991 finalize = true;
992 }
993
994 bool need_lower_tex_src_plane = false;
995
996 if (unlikely(key->external.lower_nv12 || key->external.lower_nv21 ||
997 key->external.lower_iyuv ||
998 key->external.lower_xy_uxvx || key->external.lower_yx_xuxv ||
999 key->external.lower_yx_xvxu || key->external.lower_xy_vxux ||
1000 key->external.lower_ayuv || key->external.lower_xyuv ||
1001 key->external.lower_yuv || key->external.lower_yu_yv ||
1002 key->external.lower_yv_yu || key->external.lower_y41x)) {
1003
1004 st_nir_lower_samplers(st->screen, state.ir.nir,
1005 fp->shader_program, fp);
1006
1007 nir_lower_tex_options options = {0};
1008 options.lower_y_uv_external = key->external.lower_nv12;
1009 options.lower_y_vu_external = key->external.lower_nv21;
1010 options.lower_y_u_v_external = key->external.lower_iyuv;
1011 options.lower_xy_uxvx_external = key->external.lower_xy_uxvx;
1012 options.lower_xy_vxux_external = key->external.lower_xy_vxux;
1013 options.lower_yx_xuxv_external = key->external.lower_yx_xuxv;
1014 options.lower_yx_xvxu_external = key->external.lower_yx_xvxu;
1015 options.lower_ayuv_external = key->external.lower_ayuv;
1016 options.lower_xyuv_external = key->external.lower_xyuv;
1017 options.lower_yuv_external = key->external.lower_yuv;
1018 options.lower_yu_yv_external = key->external.lower_yu_yv;
1019 options.lower_yv_yu_external = key->external.lower_yv_yu;
1020 options.lower_y41x_external = key->external.lower_y41x;
1021 options.bt709_external = key->external.bt709;
1022 options.bt2020_external = key->external.bt2020;
1023 options.yuv_full_range_external = key->external.yuv_full_range;
1024 NIR_PASS(_, state.ir.nir, nir_lower_tex, &options);
1025 finalize = true;
1026 need_lower_tex_src_plane = true;
1027 }
1028
1029 if (finalize || !st->allow_st_finalize_nir_twice) {
1030 char *msg = st_finalize_nir(st, fp, fp->shader_program, state.ir.nir,
1031 false, false);
1032 free(msg);
1033 }
1034
1035 /* This pass needs to happen *after* nir_lower_sampler */
1036 if (unlikely(need_lower_tex_src_plane)) {
1037 NIR_PASS(_, state.ir.nir, st_nir_lower_tex_src_plane,
1038 ~fp->SamplersUsed,
1039 key->external.lower_nv12 | key->external.lower_nv21 |
1040 key->external.lower_xy_uxvx | key->external.lower_xy_vxux |
1041 key->external.lower_yx_xuxv | key->external.lower_yx_xvxu,
1042 key->external.lower_iyuv);
1043 finalize = true;
1044 }
1045
1046 /* It is undefined behavior when an ARB assembly uses SHADOW2D target
1047 * with a texture in not depth format. In this case NVIDIA automatically
1048 * replaces SHADOW sampler with a normal sampler and some games like
1049 * Penumbra Overture which abuses this UB (issues/8425) works fine but
1050 * breaks with mesa. Replace the shadow sampler with a normal one here
1051 */
1052 if (!fp->shader_program && ~key->depth_textures & fp->ShadowSamplers) {
1053 NIR_PASS(_, state.ir.nir, nir_remove_tex_shadow,
1054 ~key->depth_textures & fp->ShadowSamplers);
1055 finalize = true;
1056 }
1057
1058 if (finalize || !st->allow_st_finalize_nir_twice) {
1059 /* Some of the lowering above may have introduced new varyings */
1060 nir_shader_gather_info(state.ir.nir,
1061 nir_shader_get_entrypoint(state.ir.nir));
1062
1063 struct pipe_screen *screen = st->screen;
1064 if (screen->finalize_nir) {
1065 char *msg = screen->finalize_nir(screen, state.ir.nir);
1066 free(msg);
1067 }
1068 }
1069
1070 variant->base.driver_shader = st_create_nir_shader(st, &state);
1071 variant->key = *key;
1072
1073 return variant;
1074 }
1075
1076 /**
1077 * Translate fragment program if needed.
1078 */
1079 struct st_fp_variant *
st_get_fp_variant(struct st_context * st,struct gl_program * fp,const struct st_fp_variant_key * key)1080 st_get_fp_variant(struct st_context *st,
1081 struct gl_program *fp,
1082 const struct st_fp_variant_key *key)
1083 {
1084 struct st_fp_variant *fpv;
1085
1086 /* Search for existing variant */
1087 for (fpv = st_fp_variant(fp->variants); fpv;
1088 fpv = st_fp_variant(fpv->base.next)) {
1089 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
1090 break;
1091 }
1092 }
1093
1094 if (!fpv) {
1095 /* create new */
1096
1097 if (fp->variants != NULL) {
1098 _mesa_perf_debug(st->ctx, MESA_DEBUG_SEVERITY_MEDIUM,
1099 "Compiling fragment shader variant (%s%s%s%s%s%s%s%s%s%s%s%s%s%d)",
1100 key->bitmap ? "bitmap," : "",
1101 key->drawpixels ? "drawpixels," : "",
1102 key->scaleAndBias ? "scale_bias," : "",
1103 key->pixelMaps ? "pixel_maps," : "",
1104 key->clamp_color ? "clamp_color," : "",
1105 key->persample_shading ? "persample_shading," : "",
1106 key->fog ? "fog," : "",
1107 key->lower_two_sided_color ? "twoside," : "",
1108 key->lower_flatshade ? "flatshade," : "",
1109 key->lower_alpha_func != COMPARE_FUNC_ALWAYS ? "alpha_compare," : "",
1110 /* skipped ATI_fs targets */
1111 fp->ExternalSamplersUsed ? "external?," : "",
1112 key->gl_clamp[0] || key->gl_clamp[1] || key->gl_clamp[2] ? "GL_CLAMP," : "",
1113 "depth_textures=", key->depth_textures);
1114 }
1115
1116 fpv = st_create_fp_variant(st, fp, key);
1117 if (fpv) {
1118 fpv->base.st = key->st;
1119
1120 st_add_variant(&fp->variants, &fpv->base);
1121 }
1122 }
1123
1124 return fpv;
1125 }
1126
1127 /**
1128 * Vert/Geom/Frag programs have per-context variants. Free all the
1129 * variants attached to the given program which match the given context.
1130 */
1131 static void
destroy_program_variants(struct st_context * st,struct gl_program * p)1132 destroy_program_variants(struct st_context *st, struct gl_program *p)
1133 {
1134 if (!p || p == &_mesa_DummyProgram)
1135 return;
1136
1137 struct st_variant *v, **prevPtr = &p->variants;
1138 bool unbound = false;
1139
1140 for (v = p->variants; v; ) {
1141 struct st_variant *next = v->next;
1142 if (v->st == st) {
1143 if (!unbound) {
1144 st_unbind_program(st, p);
1145 unbound = true;
1146 }
1147
1148 /* unlink from list */
1149 *prevPtr = next;
1150 /* destroy this variant */
1151 delete_variant(st, v, p->Target);
1152 }
1153 else {
1154 prevPtr = &v->next;
1155 }
1156 v = next;
1157 }
1158 }
1159
1160
1161 /**
1162 * Callback for _mesa_HashWalk. Free all the shader's program variants
1163 * which match the given context.
1164 */
1165 static void
destroy_shader_program_variants_cb(void * data,void * userData)1166 destroy_shader_program_variants_cb(void *data, void *userData)
1167 {
1168 struct st_context *st = (struct st_context *) userData;
1169 struct gl_shader *shader = (struct gl_shader *) data;
1170
1171 switch (shader->Type) {
1172 case GL_SHADER_PROGRAM_MESA:
1173 {
1174 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1175 GLuint i;
1176
1177 for (i = 0; i < ARRAY_SIZE(shProg->_LinkedShaders); i++) {
1178 if (shProg->_LinkedShaders[i])
1179 destroy_program_variants(st, shProg->_LinkedShaders[i]->Program);
1180 }
1181 }
1182 break;
1183 case GL_VERTEX_SHADER:
1184 case GL_FRAGMENT_SHADER:
1185 case GL_GEOMETRY_SHADER:
1186 case GL_TESS_CONTROL_SHADER:
1187 case GL_TESS_EVALUATION_SHADER:
1188 case GL_COMPUTE_SHADER:
1189 break;
1190 default:
1191 assert(0);
1192 }
1193 }
1194
1195
1196 /**
1197 * Callback for _mesa_HashWalk. Free all the program variants which match
1198 * the given context.
1199 */
1200 static void
destroy_program_variants_cb(void * data,void * userData)1201 destroy_program_variants_cb(void *data, void *userData)
1202 {
1203 struct st_context *st = (struct st_context *) userData;
1204 struct gl_program *program = (struct gl_program *) data;
1205 destroy_program_variants(st, program);
1206 }
1207
1208
1209 /**
1210 * Walk over all shaders and programs to delete any variants which
1211 * belong to the given context.
1212 * This is called during context tear-down.
1213 */
1214 void
st_destroy_program_variants(struct st_context * st)1215 st_destroy_program_variants(struct st_context *st)
1216 {
1217 /* If shaders can be shared with other contexts, the last context will
1218 * call DeleteProgram on all shaders, releasing everything.
1219 */
1220 if (st->has_shareable_shaders)
1221 return;
1222
1223 /* ARB vert/frag program */
1224 _mesa_HashWalk(&st->ctx->Shared->Programs,
1225 destroy_program_variants_cb, st);
1226
1227 /* GLSL vert/frag/geom shaders */
1228 _mesa_HashWalk(&st->ctx->Shared->ShaderObjects,
1229 destroy_shader_program_variants_cb, st);
1230 }
1231
1232 /**
1233 * Compile one shader variant.
1234 */
1235 static void
st_precompile_shader_variant(struct st_context * st,struct gl_program * prog)1236 st_precompile_shader_variant(struct st_context *st,
1237 struct gl_program *prog)
1238 {
1239 switch (prog->Target) {
1240 case GL_VERTEX_PROGRAM_ARB:
1241 case GL_TESS_CONTROL_PROGRAM_NV:
1242 case GL_TESS_EVALUATION_PROGRAM_NV:
1243 case GL_GEOMETRY_PROGRAM_NV:
1244 case GL_COMPUTE_PROGRAM_NV: {
1245 struct st_common_variant_key key;
1246
1247 memset(&key, 0, sizeof(key));
1248
1249 if (_mesa_is_desktop_gl_compat(st->ctx) &&
1250 st->clamp_vert_color_in_shader &&
1251 (prog->info.outputs_written & (VARYING_SLOT_COL0 |
1252 VARYING_SLOT_COL1 |
1253 VARYING_SLOT_BFC0 |
1254 VARYING_SLOT_BFC1))) {
1255 key.clamp_color = true;
1256 }
1257
1258 key.st = st->has_shareable_shaders ? NULL : st;
1259 st_get_common_variant(st, prog, &key);
1260 break;
1261 }
1262
1263 case GL_FRAGMENT_PROGRAM_ARB: {
1264 struct st_fp_variant_key key;
1265
1266 memset(&key, 0, sizeof(key));
1267
1268 key.st = st->has_shareable_shaders ? NULL : st;
1269 key.lower_alpha_func = COMPARE_FUNC_ALWAYS;
1270 if (prog->ati_fs) {
1271 for (int i = 0; i < ARRAY_SIZE(key.texture_index); i++)
1272 key.texture_index[i] = TEXTURE_2D_INDEX;
1273 }
1274
1275 /* Shadow samplers require texture in depth format, which we lower to
1276 * non-shadow if necessary for ARB programs
1277 */
1278 if (!prog->shader_program)
1279 key.depth_textures = prog->ShadowSamplers;
1280
1281 st_get_fp_variant(st, prog, &key);
1282 break;
1283 }
1284
1285 default:
1286 assert(0);
1287 }
1288 }
1289
1290 void
st_serialize_nir(struct gl_program * prog)1291 st_serialize_nir(struct gl_program *prog)
1292 {
1293 if (!prog->serialized_nir) {
1294 struct blob blob;
1295 size_t size;
1296
1297 blob_init(&blob);
1298 nir_serialize(&blob, prog->nir, false);
1299 blob_finish_get_buffer(&blob, &prog->serialized_nir, &size);
1300 prog->serialized_nir_size = size;
1301 }
1302 }
1303
1304 void
st_finalize_program(struct st_context * st,struct gl_program * prog)1305 st_finalize_program(struct st_context *st, struct gl_program *prog)
1306 {
1307 struct gl_context *ctx = st->ctx;
1308 bool is_bound = false;
1309
1310 MESA_TRACE_FUNC();
1311
1312 if (prog->info.stage == MESA_SHADER_VERTEX)
1313 is_bound = prog == ctx->VertexProgram._Current;
1314 else if (prog->info.stage == MESA_SHADER_TESS_CTRL)
1315 is_bound = prog == ctx->TessCtrlProgram._Current;
1316 else if (prog->info.stage == MESA_SHADER_TESS_EVAL)
1317 is_bound = prog == ctx->TessEvalProgram._Current;
1318 else if (prog->info.stage == MESA_SHADER_GEOMETRY)
1319 is_bound = prog == ctx->GeometryProgram._Current;
1320 else if (prog->info.stage == MESA_SHADER_FRAGMENT)
1321 is_bound = prog == ctx->FragmentProgram._Current;
1322 else if (prog->info.stage == MESA_SHADER_COMPUTE)
1323 is_bound = prog == ctx->ComputeProgram._Current;
1324
1325 if (is_bound) {
1326 if (prog->info.stage == MESA_SHADER_VERTEX) {
1327 ctx->Array.NewVertexElements = true;
1328 ctx->NewDriverState |= ST_NEW_VERTEX_PROGRAM(ctx, prog);
1329 } else {
1330 ctx->NewDriverState |= prog->affected_states;
1331 }
1332 }
1333
1334 if (prog->nir) {
1335 nir_sweep(prog->nir);
1336
1337 /* This is only needed for ARB_vp/fp programs and when the disk cache
1338 * is disabled. If the disk cache is enabled, GLSL programs are
1339 * serialized in write_nir_to_cache.
1340 */
1341 st_serialize_nir(prog);
1342 }
1343
1344 /* Always create the default variant of the program. */
1345 st_precompile_shader_variant(st, prog);
1346 }
1347
1348 /**
1349 * Called when the program's text/code is changed. We have to free
1350 * all shader variants and corresponding gallium shaders when this happens.
1351 */
1352 GLboolean
st_program_string_notify(struct gl_context * ctx,GLenum target,struct gl_program * prog)1353 st_program_string_notify( struct gl_context *ctx,
1354 GLenum target,
1355 struct gl_program *prog )
1356 {
1357 struct st_context *st = st_context(ctx);
1358
1359 /* GLSL-to-NIR should not end up here. */
1360 assert(!prog->shader_program);
1361
1362 st_release_variants(st, prog);
1363
1364 if (target == GL_FRAGMENT_PROGRAM_ARB ||
1365 target == GL_FRAGMENT_SHADER_ATI) {
1366 if (!st_translate_fragment_program(st, prog))
1367 return false;
1368 } else if (target == GL_VERTEX_PROGRAM_ARB) {
1369 if (!st_translate_vertex_program(st, prog))
1370 return false;
1371 if (st->lower_point_size &&
1372 gl_nir_can_add_pointsize_to_program(&st->ctx->Const, prog)) {
1373 prog->skip_pointsize_xfb = true;
1374 NIR_PASS(_, prog->nir, gl_nir_add_point_size);
1375 }
1376 }
1377
1378 st_finalize_program(st, prog);
1379 return GL_TRUE;
1380 }
1381