revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / mesa / state_tracker / st_program.c
blobfca187a7f7c483f19e5fe157920f057e25d8c501
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
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.
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 TUNGSTEN GRAPHICS 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.
26 **************************************************************************/
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Brian Paul
34 #include "main/imports.h"
35 #include "main/hash.h"
36 #include "main/mfeatures.h"
37 #include "main/mtypes.h"
38 #include "program/prog_parameter.h"
39 #include "program/prog_print.h"
40 #include "program/programopt.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_shader_tokens.h"
45 #include "draw/draw_context.h"
46 #include "tgsi/tgsi_dump.h"
47 #include "tgsi/tgsi_ureg.h"
49 #include "st_debug.h"
50 #include "st_cb_bitmap.h"
51 #include "st_cb_drawpixels.h"
52 #include "st_context.h"
53 #include "st_program.h"
54 #include "st_mesa_to_tgsi.h"
55 #include "cso_cache/cso_context.h"
59 /**
60 * Delete a vertex program variant. Note the caller must unlink
61 * the variant from the linked list.
63 static void
64 delete_vp_variant(struct st_context *st, struct st_vp_variant *vpv)
66 if (vpv->driver_shader)
67 cso_delete_vertex_shader(st->cso_context, vpv->driver_shader);
69 #if FEATURE_feedback || FEATURE_rastpos
70 if (vpv->draw_shader)
71 draw_delete_vertex_shader( st->draw, vpv->draw_shader );
72 #endif
74 if (vpv->tgsi.tokens)
75 st_free_tokens(vpv->tgsi.tokens);
77 FREE( vpv );
82 /**
83 * Clean out any old compilations:
85 void
86 st_release_vp_variants( struct st_context *st,
87 struct st_vertex_program *stvp )
89 struct st_vp_variant *vpv;
91 for (vpv = stvp->variants; vpv; ) {
92 struct st_vp_variant *next = vpv->next;
93 delete_vp_variant(st, vpv);
94 vpv = next;
97 stvp->variants = NULL;
103 * Delete a fragment program variant. Note the caller must unlink
104 * the variant from the linked list.
106 static void
107 delete_fp_variant(struct st_context *st, struct st_fp_variant *fpv)
109 if (fpv->driver_shader)
110 cso_delete_fragment_shader(st->cso_context, fpv->driver_shader);
111 if (fpv->parameters)
112 _mesa_free_parameter_list(fpv->parameters);
114 FREE(fpv);
119 * Free all variants of a fragment program.
121 void
122 st_release_fp_variants(struct st_context *st, struct st_fragment_program *stfp)
124 struct st_fp_variant *fpv;
126 for (fpv = stfp->variants; fpv; ) {
127 struct st_fp_variant *next = fpv->next;
128 delete_fp_variant(st, fpv);
129 fpv = next;
132 stfp->variants = NULL;
137 * Delete a geometry program variant. Note the caller must unlink
138 * the variant from the linked list.
140 static void
141 delete_gp_variant(struct st_context *st, struct st_gp_variant *gpv)
143 if (gpv->driver_shader)
144 cso_delete_geometry_shader(st->cso_context, gpv->driver_shader);
146 FREE(gpv);
151 * Free all variants of a geometry program.
153 void
154 st_release_gp_variants(struct st_context *st, struct st_geometry_program *stgp)
156 struct st_gp_variant *gpv;
158 for (gpv = stgp->variants; gpv; ) {
159 struct st_gp_variant *next = gpv->next;
160 delete_gp_variant(st, gpv);
161 gpv = next;
164 stgp->variants = NULL;
171 * Translate a Mesa vertex shader into a TGSI shader.
172 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
173 * to TGSI output slots
174 * \param tokensOut destination for TGSI tokens
175 * \return pointer to cached pipe_shader object.
177 static void
178 st_prepare_vertex_program(struct st_context *st,
179 struct st_vertex_program *stvp)
181 GLuint attr;
183 stvp->num_inputs = 0;
184 stvp->num_outputs = 0;
186 if (stvp->Base.IsPositionInvariant)
187 _mesa_insert_mvp_code(st->ctx, &stvp->Base);
189 assert(stvp->Base.Base.NumInstructions > 1);
192 * Determine number of inputs, the mappings between VERT_ATTRIB_x
193 * and TGSI generic input indexes, plus input attrib semantic info.
195 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
196 if (stvp->Base.Base.InputsRead & (1 << attr)) {
197 stvp->input_to_index[attr] = stvp->num_inputs;
198 stvp->index_to_input[stvp->num_inputs] = attr;
199 stvp->num_inputs++;
202 /* bit of a hack, presetup potentially unused edgeflag input */
203 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
204 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
206 /* Compute mapping of vertex program outputs to slots.
208 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
209 if ((stvp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) == 0) {
210 stvp->result_to_output[attr] = ~0;
212 else {
213 unsigned slot = stvp->num_outputs++;
215 stvp->result_to_output[attr] = slot;
217 switch (attr) {
218 case VERT_RESULT_HPOS:
219 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
220 stvp->output_semantic_index[slot] = 0;
221 break;
222 case VERT_RESULT_COL0:
223 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
224 stvp->output_semantic_index[slot] = 0;
225 break;
226 case VERT_RESULT_COL1:
227 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
228 stvp->output_semantic_index[slot] = 1;
229 break;
230 case VERT_RESULT_BFC0:
231 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
232 stvp->output_semantic_index[slot] = 0;
233 break;
234 case VERT_RESULT_BFC1:
235 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
236 stvp->output_semantic_index[slot] = 1;
237 break;
238 case VERT_RESULT_FOGC:
239 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
240 stvp->output_semantic_index[slot] = 0;
241 break;
242 case VERT_RESULT_PSIZ:
243 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
244 stvp->output_semantic_index[slot] = 0;
245 break;
246 case VERT_RESULT_EDGE:
247 assert(0);
248 break;
250 case VERT_RESULT_TEX0:
251 case VERT_RESULT_TEX1:
252 case VERT_RESULT_TEX2:
253 case VERT_RESULT_TEX3:
254 case VERT_RESULT_TEX4:
255 case VERT_RESULT_TEX5:
256 case VERT_RESULT_TEX6:
257 case VERT_RESULT_TEX7:
258 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
259 stvp->output_semantic_index[slot] = attr - VERT_RESULT_TEX0;
260 break;
262 case VERT_RESULT_VAR0:
263 default:
264 assert(attr < VERT_RESULT_MAX);
265 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
266 stvp->output_semantic_index[slot] = (FRAG_ATTRIB_VAR0 -
267 FRAG_ATTRIB_TEX0 +
268 attr -
269 VERT_RESULT_VAR0);
270 break;
274 /* similar hack to above, presetup potentially unused edgeflag output */
275 stvp->result_to_output[VERT_RESULT_EDGE] = stvp->num_outputs;
276 stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
277 stvp->output_semantic_index[stvp->num_outputs] = 0;
282 * Translate a vertex program to create a new variant.
284 static struct st_vp_variant *
285 st_translate_vertex_program(struct st_context *st,
286 struct st_vertex_program *stvp,
287 const struct st_vp_variant_key *key)
289 struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant);
290 struct pipe_context *pipe = st->pipe;
291 struct ureg_program *ureg;
292 enum pipe_error error;
293 unsigned num_outputs;
295 st_prepare_vertex_program( st, stvp );
297 _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_OUTPUT);
298 _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_VARYING);
300 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
301 if (ureg == NULL) {
302 FREE(vpv);
303 return NULL;
306 vpv->key = *key;
308 vpv->num_inputs = stvp->num_inputs;
309 num_outputs = stvp->num_outputs;
310 if (key->passthrough_edgeflags) {
311 vpv->num_inputs++;
312 num_outputs++;
315 if (ST_DEBUG & DEBUG_MESA) {
316 _mesa_print_program(&stvp->Base.Base);
317 _mesa_print_program_parameters(st->ctx, &stvp->Base.Base);
318 debug_printf("\n");
321 error = st_translate_mesa_program(st->ctx,
322 TGSI_PROCESSOR_VERTEX,
323 ureg,
324 &stvp->Base.Base,
325 /* inputs */
326 vpv->num_inputs,
327 stvp->input_to_index,
328 NULL, /* input semantic name */
329 NULL, /* input semantic index */
330 NULL,
331 /* outputs */
332 num_outputs,
333 stvp->result_to_output,
334 stvp->output_semantic_name,
335 stvp->output_semantic_index,
336 key->passthrough_edgeflags );
338 if (error)
339 goto fail;
341 vpv->tgsi.tokens = ureg_get_tokens( ureg, NULL );
342 if (!vpv->tgsi.tokens)
343 goto fail;
345 ureg_destroy( ureg );
347 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
349 if (ST_DEBUG & DEBUG_TGSI) {
350 tgsi_dump( vpv->tgsi.tokens, 0 );
351 debug_printf("\n");
354 return vpv;
356 fail:
357 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__);
358 _mesa_print_program(&stvp->Base.Base);
359 debug_assert(0);
361 ureg_destroy( ureg );
362 return NULL;
367 * Find/create a vertex program variant.
369 struct st_vp_variant *
370 st_get_vp_variant(struct st_context *st,
371 struct st_vertex_program *stvp,
372 const struct st_vp_variant_key *key)
374 struct st_vp_variant *vpv;
376 /* Search for existing variant */
377 for (vpv = stvp->variants; vpv; vpv = vpv->next) {
378 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
379 break;
383 if (!vpv) {
384 /* create now */
385 vpv = st_translate_vertex_program(st, stvp, key);
386 if (vpv) {
387 /* insert into list */
388 vpv->next = stvp->variants;
389 stvp->variants = vpv;
393 return vpv;
398 * Translate a Mesa fragment shader into a TGSI shader using extra info in
399 * the key.
400 * \return new fragment program variant
402 static struct st_fp_variant *
403 st_translate_fragment_program(struct st_context *st,
404 struct st_fragment_program *stfp,
405 const struct st_fp_variant_key *key)
407 struct pipe_context *pipe = st->pipe;
408 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
409 GLboolean deleteFP = GL_FALSE;
411 if (!variant)
412 return NULL;
414 assert(!(key->bitmap && key->drawpixels));
416 #if FEATURE_drawpix
417 if (key->bitmap) {
418 /* glBitmap drawing */
419 struct gl_fragment_program *fp; /* we free this temp program below */
421 st_make_bitmap_fragment_program(st, &stfp->Base,
422 &fp, &variant->bitmap_sampler);
424 variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
425 stfp = st_fragment_program(fp);
426 deleteFP = GL_TRUE;
428 else if (key->drawpixels) {
429 /* glDrawPixels drawing */
430 struct gl_fragment_program *fp; /* we free this temp program below */
432 if (key->drawpixels_z || key->drawpixels_stencil) {
433 fp = st_make_drawpix_z_stencil_program(st, key->drawpixels_z,
434 key->drawpixels_stencil);
436 else {
437 /* RGBA */
438 st_make_drawpix_fragment_program(st, &stfp->Base, &fp);
439 variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
440 deleteFP = GL_TRUE;
442 stfp = st_fragment_program(fp);
444 #endif
446 if (!stfp->tgsi.tokens) {
447 /* need to translate Mesa instructions to TGSI now */
448 GLuint outputMapping[FRAG_RESULT_MAX];
449 GLuint inputMapping[FRAG_ATTRIB_MAX];
450 GLuint interpMode[PIPE_MAX_SHADER_INPUTS]; /* XXX size? */
451 GLuint attr;
452 enum pipe_error error;
453 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
454 struct ureg_program *ureg;
455 GLboolean write_all = GL_FALSE;
457 ubyte input_semantic_name[PIPE_MAX_SHADER_INPUTS];
458 ubyte input_semantic_index[PIPE_MAX_SHADER_INPUTS];
459 uint fs_num_inputs = 0;
461 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
462 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
463 uint fs_num_outputs = 0;
466 _mesa_remove_output_reads(&stfp->Base.Base, PROGRAM_OUTPUT);
469 * Convert Mesa program inputs to TGSI input register semantics.
471 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
472 if (inputsRead & (1 << attr)) {
473 const GLuint slot = fs_num_inputs++;
475 inputMapping[attr] = slot;
477 switch (attr) {
478 case FRAG_ATTRIB_WPOS:
479 input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
480 input_semantic_index[slot] = 0;
481 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
482 break;
483 case FRAG_ATTRIB_COL0:
484 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
485 input_semantic_index[slot] = 0;
486 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
487 break;
488 case FRAG_ATTRIB_COL1:
489 input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
490 input_semantic_index[slot] = 1;
491 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
492 break;
493 case FRAG_ATTRIB_FOGC:
494 input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
495 input_semantic_index[slot] = 0;
496 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
497 break;
498 case FRAG_ATTRIB_FACE:
499 input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
500 input_semantic_index[slot] = 0;
501 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
502 break;
503 /* In most cases, there is nothing special about these
504 * inputs, so adopt a convention to use the generic
505 * semantic name and the mesa FRAG_ATTRIB_ number as the
506 * index.
508 * All that is required is that the vertex shader labels
509 * its own outputs similarly, and that the vertex shader
510 * generates at least every output required by the
511 * fragment shader plus fixed-function hardware (such as
512 * BFC).
514 * There is no requirement that semantic indexes start at
515 * zero or be restricted to a particular range -- nobody
516 * should be building tables based on semantic index.
518 case FRAG_ATTRIB_PNTC:
519 case FRAG_ATTRIB_TEX0:
520 case FRAG_ATTRIB_TEX1:
521 case FRAG_ATTRIB_TEX2:
522 case FRAG_ATTRIB_TEX3:
523 case FRAG_ATTRIB_TEX4:
524 case FRAG_ATTRIB_TEX5:
525 case FRAG_ATTRIB_TEX6:
526 case FRAG_ATTRIB_TEX7:
527 case FRAG_ATTRIB_VAR0:
528 default:
529 /* Actually, let's try and zero-base this just for
530 * readability of the generated TGSI.
532 assert(attr >= FRAG_ATTRIB_TEX0);
533 input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0);
534 input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
535 if (attr == FRAG_ATTRIB_PNTC)
536 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
537 else
538 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
539 break;
542 else {
543 inputMapping[attr] = -1;
548 * Semantics and mapping for outputs
551 uint numColors = 0;
552 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
554 /* if z is written, emit that first */
555 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
556 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
557 fs_output_semantic_index[fs_num_outputs] = 0;
558 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
559 fs_num_outputs++;
560 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
563 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
564 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_STENCIL;
565 fs_output_semantic_index[fs_num_outputs] = 0;
566 outputMapping[FRAG_RESULT_STENCIL] = fs_num_outputs;
567 fs_num_outputs++;
568 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
571 /* handle remaning outputs (color) */
572 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
573 if (outputsWritten & BITFIELD64_BIT(attr)) {
574 switch (attr) {
575 case FRAG_RESULT_DEPTH:
576 case FRAG_RESULT_STENCIL:
577 /* handled above */
578 assert(0);
579 break;
580 case FRAG_RESULT_COLOR:
581 write_all = GL_TRUE; /* fallthrough */
582 default:
583 assert(attr == FRAG_RESULT_COLOR ||
584 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
585 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
586 fs_output_semantic_index[fs_num_outputs] = numColors;
587 outputMapping[attr] = fs_num_outputs;
588 numColors++;
589 break;
592 fs_num_outputs++;
597 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
598 if (ureg == NULL)
599 return NULL;
601 if (ST_DEBUG & DEBUG_MESA) {
602 _mesa_print_program(&stfp->Base.Base);
603 _mesa_print_program_parameters(st->ctx, &stfp->Base.Base);
604 debug_printf("\n");
606 if (write_all == GL_TRUE)
607 ureg_property_fs_color0_writes_all_cbufs(ureg, 1);
609 error = st_translate_mesa_program(st->ctx,
610 TGSI_PROCESSOR_FRAGMENT,
611 ureg,
612 &stfp->Base.Base,
613 /* inputs */
614 fs_num_inputs,
615 inputMapping,
616 input_semantic_name,
617 input_semantic_index,
618 interpMode,
619 /* outputs */
620 fs_num_outputs,
621 outputMapping,
622 fs_output_semantic_name,
623 fs_output_semantic_index, FALSE );
624 (void)error; /* ignored */
626 stfp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
627 ureg_destroy( ureg );
630 /* fill in variant */
631 variant->driver_shader = pipe->create_fs_state(pipe, &stfp->tgsi);
632 variant->key = *key;
634 if (ST_DEBUG & DEBUG_TGSI) {
635 tgsi_dump( stfp->tgsi.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
636 debug_printf("\n");
639 if (deleteFP) {
640 /* Free the temporary program made above */
641 struct gl_fragment_program *fp = &stfp->Base;
642 _mesa_reference_fragprog(st->ctx, &fp, NULL);
645 return variant;
650 * Translate fragment program if needed.
652 struct st_fp_variant *
653 st_get_fp_variant(struct st_context *st,
654 struct st_fragment_program *stfp,
655 const struct st_fp_variant_key *key)
657 struct st_fp_variant *fpv;
659 /* Search for existing variant */
660 for (fpv = stfp->variants; fpv; fpv = fpv->next) {
661 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
662 break;
666 if (!fpv) {
667 /* create new */
668 fpv = st_translate_fragment_program(st, stfp, key);
669 if (fpv) {
670 /* insert into list */
671 fpv->next = stfp->variants;
672 stfp->variants = fpv;
676 return fpv;
681 * Translate a geometry program to create a new variant.
683 static struct st_gp_variant *
684 st_translate_geometry_program(struct st_context *st,
685 struct st_geometry_program *stgp,
686 const struct st_gp_variant_key *key)
688 GLuint inputMapping[GEOM_ATTRIB_MAX];
689 GLuint outputMapping[GEOM_RESULT_MAX];
690 struct pipe_context *pipe = st->pipe;
691 enum pipe_error error;
692 GLuint attr;
693 const GLbitfield inputsRead = stgp->Base.Base.InputsRead;
694 GLuint vslot = 0;
695 GLuint num_generic = 0;
697 uint gs_num_inputs = 0;
698 uint gs_builtin_inputs = 0;
699 uint gs_array_offset = 0;
701 ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
702 ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
703 uint gs_num_outputs = 0;
705 GLint i;
706 GLuint maxSlot = 0;
707 struct ureg_program *ureg;
709 struct st_gp_variant *gpv;
711 gpv = CALLOC_STRUCT(st_gp_variant);
712 if (!gpv)
713 return NULL;
715 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_OUTPUT);
716 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_VARYING);
718 ureg = ureg_create( TGSI_PROCESSOR_GEOMETRY );
719 if (ureg == NULL) {
720 FREE(gpv);
721 return NULL;
724 /* which vertex output goes to the first geometry input */
725 vslot = 0;
727 memset(inputMapping, 0, sizeof(inputMapping));
728 memset(outputMapping, 0, sizeof(outputMapping));
731 * Convert Mesa program inputs to TGSI input register semantics.
733 for (attr = 0; attr < GEOM_ATTRIB_MAX; attr++) {
734 if (inputsRead & (1 << attr)) {
735 const GLuint slot = gs_num_inputs;
737 gs_num_inputs++;
739 inputMapping[attr] = slot;
741 stgp->input_map[slot + gs_array_offset] = vslot - gs_builtin_inputs;
742 stgp->input_to_index[attr] = vslot;
743 stgp->index_to_input[vslot] = attr;
744 ++vslot;
746 if (attr != GEOM_ATTRIB_PRIMITIVE_ID) {
747 gs_array_offset += 2;
748 } else
749 ++gs_builtin_inputs;
751 #if 0
752 debug_printf("input map at %d = %d\n",
753 slot + gs_array_offset, stgp->input_map[slot + gs_array_offset]);
754 #endif
756 switch (attr) {
757 case GEOM_ATTRIB_PRIMITIVE_ID:
758 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
759 stgp->input_semantic_index[slot] = 0;
760 break;
761 case GEOM_ATTRIB_POSITION:
762 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
763 stgp->input_semantic_index[slot] = 0;
764 break;
765 case GEOM_ATTRIB_COLOR0:
766 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
767 stgp->input_semantic_index[slot] = 0;
768 break;
769 case GEOM_ATTRIB_COLOR1:
770 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
771 stgp->input_semantic_index[slot] = 1;
772 break;
773 case GEOM_ATTRIB_FOG_FRAG_COORD:
774 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
775 stgp->input_semantic_index[slot] = 0;
776 break;
777 case GEOM_ATTRIB_TEX_COORD:
778 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
779 stgp->input_semantic_index[slot] = num_generic++;
780 break;
781 case GEOM_ATTRIB_VAR0:
782 /* fall-through */
783 default:
784 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
785 stgp->input_semantic_index[slot] = num_generic++;
790 /* initialize output semantics to defaults */
791 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
792 gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
793 gs_output_semantic_index[i] = 0;
796 num_generic = 0;
798 * Determine number of outputs, the (default) output register
799 * mapping and the semantic information for each output.
801 for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
802 if (stgp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) {
803 GLuint slot;
805 slot = gs_num_outputs;
806 gs_num_outputs++;
807 outputMapping[attr] = slot;
809 switch (attr) {
810 case GEOM_RESULT_POS:
811 assert(slot == 0);
812 gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
813 gs_output_semantic_index[slot] = 0;
814 break;
815 case GEOM_RESULT_COL0:
816 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
817 gs_output_semantic_index[slot] = 0;
818 break;
819 case GEOM_RESULT_COL1:
820 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
821 gs_output_semantic_index[slot] = 1;
822 break;
823 case GEOM_RESULT_SCOL0:
824 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
825 gs_output_semantic_index[slot] = 0;
826 break;
827 case GEOM_RESULT_SCOL1:
828 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
829 gs_output_semantic_index[slot] = 1;
830 break;
831 case GEOM_RESULT_FOGC:
832 gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
833 gs_output_semantic_index[slot] = 0;
834 break;
835 case GEOM_RESULT_PSIZ:
836 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
837 gs_output_semantic_index[slot] = 0;
838 break;
839 case GEOM_RESULT_TEX0:
840 case GEOM_RESULT_TEX1:
841 case GEOM_RESULT_TEX2:
842 case GEOM_RESULT_TEX3:
843 case GEOM_RESULT_TEX4:
844 case GEOM_RESULT_TEX5:
845 case GEOM_RESULT_TEX6:
846 case GEOM_RESULT_TEX7:
847 /* fall-through */
848 case GEOM_RESULT_VAR0:
849 /* fall-through */
850 default:
851 assert(slot < Elements(gs_output_semantic_name));
852 /* use default semantic info */
853 gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
854 gs_output_semantic_index[slot] = num_generic++;
859 assert(gs_output_semantic_name[0] == TGSI_SEMANTIC_POSITION);
861 /* find max output slot referenced to compute gs_num_outputs */
862 for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
863 if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot)
864 maxSlot = outputMapping[attr];
866 gs_num_outputs = maxSlot + 1;
868 #if 0 /* debug */
870 GLuint i;
871 printf("outputMapping? %d\n", outputMapping ? 1 : 0);
872 if (outputMapping) {
873 printf("attr -> slot\n");
874 for (i = 0; i < 16; i++) {
875 printf(" %2d %3d\n", i, outputMapping[i]);
878 printf("slot sem_name sem_index\n");
879 for (i = 0; i < gs_num_outputs; i++) {
880 printf(" %2d %d %d\n",
882 gs_output_semantic_name[i],
883 gs_output_semantic_index[i]);
886 #endif
888 /* free old shader state, if any */
889 if (stgp->tgsi.tokens) {
890 st_free_tokens(stgp->tgsi.tokens);
891 stgp->tgsi.tokens = NULL;
894 ureg_property_gs_input_prim(ureg, stgp->Base.InputType);
895 ureg_property_gs_output_prim(ureg, stgp->Base.OutputType);
896 ureg_property_gs_max_vertices(ureg, stgp->Base.VerticesOut);
898 error = st_translate_mesa_program(st->ctx,
899 TGSI_PROCESSOR_GEOMETRY,
900 ureg,
901 &stgp->Base.Base,
902 /* inputs */
903 gs_num_inputs,
904 inputMapping,
905 stgp->input_semantic_name,
906 stgp->input_semantic_index,
907 NULL,
908 /* outputs */
909 gs_num_outputs,
910 outputMapping,
911 gs_output_semantic_name,
912 gs_output_semantic_index,
913 FALSE);
914 (void)error; /* ignored */
916 stgp->num_inputs = gs_num_inputs;
917 stgp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
918 ureg_destroy( ureg );
920 /* fill in new variant */
921 gpv->driver_shader = pipe->create_gs_state(pipe, &stgp->tgsi);
922 gpv->key = *key;
924 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
925 _mesa_print_program(&stgp->Base.Base);
926 debug_printf("\n");
929 if (ST_DEBUG & DEBUG_TGSI) {
930 tgsi_dump(stgp->tgsi.tokens, 0);
931 debug_printf("\n");
934 return gpv;
939 * Get/create geometry program variant.
941 struct st_gp_variant *
942 st_get_gp_variant(struct st_context *st,
943 struct st_geometry_program *stgp,
944 const struct st_gp_variant_key *key)
946 struct st_gp_variant *gpv;
948 /* Search for existing variant */
949 for (gpv = stgp->variants; gpv; gpv = gpv->next) {
950 if (memcmp(&gpv->key, key, sizeof(*key)) == 0) {
951 break;
955 if (!gpv) {
956 /* create new */
957 gpv = st_translate_geometry_program(st, stgp, key);
958 if (gpv) {
959 /* insert into list */
960 gpv->next = stgp->variants;
961 stgp->variants = gpv;
965 return gpv;
972 * Debug- print current shader text
974 void
975 st_print_shaders(struct gl_context *ctx)
977 struct gl_shader_program *shProg[3] = {
978 ctx->Shader.CurrentVertexProgram,
979 ctx->Shader.CurrentGeometryProgram,
980 ctx->Shader.CurrentFragmentProgram,
982 unsigned j;
984 for (j = 0; j < 3; j++) {
985 unsigned i;
987 if (shProg[j] == NULL)
988 continue;
990 for (i = 0; i < shProg[j]->NumShaders; i++) {
991 struct gl_shader *sh;
993 switch (shProg[j]->Shaders[i]->Type) {
994 case GL_VERTEX_SHADER:
995 sh = (i != 0) ? NULL : shProg[j]->Shaders[i];
996 break;
997 case GL_GEOMETRY_SHADER_ARB:
998 sh = (i != 1) ? NULL : shProg[j]->Shaders[i];
999 break;
1000 case GL_FRAGMENT_SHADER:
1001 sh = (i != 2) ? NULL : shProg[j]->Shaders[i];
1002 break;
1003 default:
1004 assert(0);
1005 sh = NULL;
1006 break;
1009 if (sh != NULL) {
1010 printf("GLSL shader %u of %u:\n", i, shProg[j]->NumShaders);
1011 printf("%s\n", sh->Source);
1019 * Vert/Geom/Frag programs have per-context variants. Free all the
1020 * variants attached to the given program which match the given context.
1022 static void
1023 destroy_program_variants(struct st_context *st, struct gl_program *program)
1025 if (!program)
1026 return;
1028 switch (program->Target) {
1029 case GL_VERTEX_PROGRAM_ARB:
1031 struct st_vertex_program *stvp = (struct st_vertex_program *) program;
1032 struct st_vp_variant *vpv, **prevPtr = &stvp->variants;
1034 for (vpv = stvp->variants; vpv; ) {
1035 struct st_vp_variant *next = vpv->next;
1036 if (vpv->key.st == st) {
1037 /* unlink from list */
1038 *prevPtr = next;
1039 /* destroy this variant */
1040 delete_vp_variant(st, vpv);
1042 else {
1043 prevPtr = &vpv->next;
1045 vpv = next;
1048 break;
1049 case GL_FRAGMENT_PROGRAM_ARB:
1051 struct st_fragment_program *stfp =
1052 (struct st_fragment_program *) program;
1053 struct st_fp_variant *fpv, **prevPtr = &stfp->variants;
1055 for (fpv = stfp->variants; fpv; ) {
1056 struct st_fp_variant *next = fpv->next;
1057 if (fpv->key.st == st) {
1058 /* unlink from list */
1059 *prevPtr = next;
1060 /* destroy this variant */
1061 delete_fp_variant(st, fpv);
1063 else {
1064 prevPtr = &fpv->next;
1066 fpv = next;
1069 break;
1070 case MESA_GEOMETRY_PROGRAM:
1072 struct st_geometry_program *stgp =
1073 (struct st_geometry_program *) program;
1074 struct st_gp_variant *gpv, **prevPtr = &stgp->variants;
1076 for (gpv = stgp->variants; gpv; ) {
1077 struct st_gp_variant *next = gpv->next;
1078 if (gpv->key.st == st) {
1079 /* unlink from list */
1080 *prevPtr = next;
1081 /* destroy this variant */
1082 delete_gp_variant(st, gpv);
1084 else {
1085 prevPtr = &gpv->next;
1087 gpv = next;
1090 break;
1091 default:
1092 _mesa_problem(NULL, "Unexpected program target 0x%x in "
1093 "destroy_program_variants_cb()", program->Target);
1099 * Callback for _mesa_HashWalk. Free all the shader's program variants
1100 * which match the given context.
1102 static void
1103 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1105 struct st_context *st = (struct st_context *) userData;
1106 struct gl_shader *shader = (struct gl_shader *) data;
1108 switch (shader->Type) {
1109 case GL_SHADER_PROGRAM_MESA:
1111 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1112 GLuint i;
1114 for (i = 0; i < shProg->NumShaders; i++) {
1115 destroy_program_variants(st, shProg->Shaders[i]->Program);
1118 destroy_program_variants(st, (struct gl_program *)
1119 shProg->VertexProgram);
1120 destroy_program_variants(st, (struct gl_program *)
1121 shProg->FragmentProgram);
1122 destroy_program_variants(st, (struct gl_program *)
1123 shProg->GeometryProgram);
1125 break;
1126 case GL_VERTEX_SHADER:
1127 case GL_FRAGMENT_SHADER:
1128 case GL_GEOMETRY_SHADER:
1130 destroy_program_variants(st, shader->Program);
1132 break;
1133 default:
1134 assert(0);
1140 * Callback for _mesa_HashWalk. Free all the program variants which match
1141 * the given context.
1143 static void
1144 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1146 struct st_context *st = (struct st_context *) userData;
1147 struct gl_program *program = (struct gl_program *) data;
1148 destroy_program_variants(st, program);
1153 * Walk over all shaders and programs to delete any variants which
1154 * belong to the given context.
1155 * This is called during context tear-down.
1157 void
1158 st_destroy_program_variants(struct st_context *st)
1160 /* ARB vert/frag program */
1161 _mesa_HashWalk(st->ctx->Shared->Programs,
1162 destroy_program_variants_cb, st);
1164 /* GLSL vert/frag/geom shaders */
1165 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1166 destroy_shader_program_variants_cb, st);