1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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
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 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
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"
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"
60 * Delete a vertex program variant. Note the caller must unlink
61 * the variant from the linked list.
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
71 draw_delete_vertex_shader( st->draw, vpv->draw_shader );
75 st_free_tokens(vpv->tgsi.tokens);
83 * Clean out any old compilations:
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);
97 stvp->variants = NULL;
103 * Delete a fragment program variant. Note the caller must unlink
104 * the variant from the linked list.
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);
112 _mesa_free_parameter_list(fpv->parameters);
119 * Free all variants of a fragment program.
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);
132 stfp->variants = NULL;
137 * Delete a geometry program variant. Note the caller must unlink
138 * the variant from the linked list.
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);
151 * Free all variants of a geometry program.
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);
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.
178 st_prepare_vertex_program(struct gl_context *ctx,
179 struct st_vertex_program *stvp)
183 stvp->num_inputs = 0;
184 stvp->num_outputs = 0;
186 if (stvp->Base.IsPositionInvariant)
187 _mesa_insert_mvp_code(ctx, &stvp->Base);
189 if (!stvp->glsl_to_tgsi)
190 assert(stvp->Base.Base.NumInstructions > 1);
193 * Determine number of inputs, the mappings between VERT_ATTRIB_x
194 * and TGSI generic input indexes, plus input attrib semantic info.
196 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
197 if (stvp->Base.Base.InputsRead & (1 << attr)) {
198 stvp->input_to_index[attr] = stvp->num_inputs;
199 stvp->index_to_input[stvp->num_inputs] = attr;
203 /* bit of a hack, presetup potentially unused edgeflag input */
204 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
205 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
207 /* Compute mapping of vertex program outputs to slots.
209 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
210 if ((stvp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) == 0) {
211 stvp->result_to_output[attr] = ~0;
214 unsigned slot = stvp->num_outputs++;
216 stvp->result_to_output[attr] = slot;
219 case VERT_RESULT_HPOS:
220 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
221 stvp->output_semantic_index[slot] = 0;
223 case VERT_RESULT_COL0:
224 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
225 stvp->output_semantic_index[slot] = 0;
227 case VERT_RESULT_COL1:
228 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
229 stvp->output_semantic_index[slot] = 1;
231 case VERT_RESULT_BFC0:
232 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
233 stvp->output_semantic_index[slot] = 0;
235 case VERT_RESULT_BFC1:
236 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
237 stvp->output_semantic_index[slot] = 1;
239 case VERT_RESULT_FOGC:
240 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
241 stvp->output_semantic_index[slot] = 0;
243 case VERT_RESULT_PSIZ:
244 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
245 stvp->output_semantic_index[slot] = 0;
247 case VERT_RESULT_EDGE:
251 case VERT_RESULT_TEX0:
252 case VERT_RESULT_TEX1:
253 case VERT_RESULT_TEX2:
254 case VERT_RESULT_TEX3:
255 case VERT_RESULT_TEX4:
256 case VERT_RESULT_TEX5:
257 case VERT_RESULT_TEX6:
258 case VERT_RESULT_TEX7:
259 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
260 stvp->output_semantic_index[slot] = attr - VERT_RESULT_TEX0;
263 case VERT_RESULT_VAR0:
265 assert(attr < VERT_RESULT_MAX);
266 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
267 stvp->output_semantic_index[slot] = (FRAG_ATTRIB_VAR0 -
275 /* similar hack to above, presetup potentially unused edgeflag output */
276 stvp->result_to_output[VERT_RESULT_EDGE] = stvp->num_outputs;
277 stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
278 stvp->output_semantic_index[stvp->num_outputs] = 0;
283 * Translate a vertex program to create a new variant.
285 static struct st_vp_variant *
286 st_translate_vertex_program(struct st_context *st,
287 struct st_vertex_program *stvp,
288 const struct st_vp_variant_key *key)
290 struct st_vp_variant *vpv = CALLOC_STRUCT(st_vp_variant);
291 struct pipe_context *pipe = st->pipe;
292 struct ureg_program *ureg;
293 enum pipe_error error;
294 unsigned num_outputs;
296 st_prepare_vertex_program(st->ctx, stvp);
298 if (!stvp->glsl_to_tgsi)
300 _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_OUTPUT);
301 _mesa_remove_output_reads(&stvp->Base.Base, PROGRAM_VARYING);
304 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
312 vpv->num_inputs = stvp->num_inputs;
313 num_outputs = stvp->num_outputs;
314 if (key->passthrough_edgeflags) {
319 if (ST_DEBUG & DEBUG_MESA) {
320 _mesa_print_program(&stvp->Base.Base);
321 _mesa_print_program_parameters(st->ctx, &stvp->Base.Base);
325 if (stvp->glsl_to_tgsi)
326 error = st_translate_program(st->ctx,
327 TGSI_PROCESSOR_VERTEX,
333 stvp->input_to_index,
334 NULL, /* input semantic name */
335 NULL, /* input semantic index */
336 NULL, /* interp mode */
339 stvp->result_to_output,
340 stvp->output_semantic_name,
341 stvp->output_semantic_index,
342 key->passthrough_edgeflags );
344 error = st_translate_mesa_program(st->ctx,
345 TGSI_PROCESSOR_VERTEX,
350 stvp->input_to_index,
351 NULL, /* input semantic name */
352 NULL, /* input semantic index */
356 stvp->result_to_output,
357 stvp->output_semantic_name,
358 stvp->output_semantic_index,
359 key->passthrough_edgeflags );
364 vpv->tgsi.tokens = ureg_get_tokens( ureg, NULL );
365 if (!vpv->tgsi.tokens)
368 ureg_destroy( ureg );
370 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->tgsi);
372 if (ST_DEBUG & DEBUG_TGSI) {
373 tgsi_dump( vpv->tgsi.tokens, 0 );
380 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__);
381 _mesa_print_program(&stvp->Base.Base);
384 ureg_destroy( ureg );
390 * Find/create a vertex program variant.
392 struct st_vp_variant *
393 st_get_vp_variant(struct st_context *st,
394 struct st_vertex_program *stvp,
395 const struct st_vp_variant_key *key)
397 struct st_vp_variant *vpv;
399 /* Search for existing variant */
400 for (vpv = stvp->variants; vpv; vpv = vpv->next) {
401 if (memcmp(&vpv->key, key, sizeof(*key)) == 0) {
408 vpv = st_translate_vertex_program(st, stvp, key);
410 /* insert into list */
411 vpv->next = stvp->variants;
412 stvp->variants = vpv;
420 * Translate Mesa fragment shader attributes to TGSI attributes.
421 * \return GL_TRUE if color output should be written to all render targets,
425 st_prepare_fragment_program(struct gl_context *ctx,
426 struct st_fragment_program *stfp)
429 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
430 GLboolean write_all = GL_FALSE;
433 * Convert Mesa program inputs to TGSI input register semantics.
435 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
436 if (inputsRead & (1 << attr)) {
437 const GLuint slot = stfp->num_inputs++;
439 stfp->input_to_index[attr] = slot;
442 case FRAG_ATTRIB_WPOS:
443 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
444 stfp->input_semantic_index[slot] = 0;
445 stfp->interp_mode[slot] = TGSI_INTERPOLATE_LINEAR;
447 case FRAG_ATTRIB_COL0:
448 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
449 stfp->input_semantic_index[slot] = 0;
450 stfp->interp_mode[slot] = TGSI_INTERPOLATE_LINEAR;
452 case FRAG_ATTRIB_COL1:
453 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
454 stfp->input_semantic_index[slot] = 1;
455 stfp->interp_mode[slot] = TGSI_INTERPOLATE_LINEAR;
457 case FRAG_ATTRIB_FOGC:
458 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
459 stfp->input_semantic_index[slot] = 0;
460 stfp->interp_mode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
462 case FRAG_ATTRIB_FACE:
463 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
464 stfp->input_semantic_index[slot] = 0;
465 stfp->interp_mode[slot] = TGSI_INTERPOLATE_CONSTANT;
467 /* In most cases, there is nothing special about these
468 * inputs, so adopt a convention to use the generic
469 * semantic name and the mesa FRAG_ATTRIB_ number as the
472 * All that is required is that the vertex shader labels
473 * its own outputs similarly, and that the vertex shader
474 * generates at least every output required by the
475 * fragment shader plus fixed-function hardware (such as
478 * There is no requirement that semantic indexes start at
479 * zero or be restricted to a particular range -- nobody
480 * should be building tables based on semantic index.
482 case FRAG_ATTRIB_PNTC:
483 case FRAG_ATTRIB_TEX0:
484 case FRAG_ATTRIB_TEX1:
485 case FRAG_ATTRIB_TEX2:
486 case FRAG_ATTRIB_TEX3:
487 case FRAG_ATTRIB_TEX4:
488 case FRAG_ATTRIB_TEX5:
489 case FRAG_ATTRIB_TEX6:
490 case FRAG_ATTRIB_TEX7:
491 case FRAG_ATTRIB_VAR0:
493 /* Actually, let's try and zero-base this just for
494 * readability of the generated TGSI.
496 assert(attr >= FRAG_ATTRIB_TEX0);
497 stfp->input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0);
498 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
499 if (attr == FRAG_ATTRIB_PNTC)
500 stfp->interp_mode[slot] = TGSI_INTERPOLATE_LINEAR;
502 stfp->interp_mode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
507 stfp->input_to_index[attr] = -1;
512 * Semantics and mapping for outputs
516 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
518 /* if z is written, emit that first */
519 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
520 stfp->output_semantic_name[stfp->num_outputs] = TGSI_SEMANTIC_POSITION;
521 stfp->output_semantic_index[stfp->num_outputs] = 0;
522 stfp->result_to_output[FRAG_RESULT_DEPTH] = stfp->num_outputs;
524 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
527 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {
528 stfp->output_semantic_name[stfp->num_outputs] = TGSI_SEMANTIC_STENCIL;
529 stfp->output_semantic_index[stfp->num_outputs] = 0;
530 stfp->result_to_output[FRAG_RESULT_STENCIL] = stfp->num_outputs;
532 outputsWritten &= ~(1 << FRAG_RESULT_STENCIL);
535 /* handle remaning outputs (color) */
536 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
537 if (outputsWritten & BITFIELD64_BIT(attr)) {
539 case FRAG_RESULT_DEPTH:
540 case FRAG_RESULT_STENCIL:
544 case FRAG_RESULT_COLOR:
545 write_all = GL_TRUE; /* fallthrough */
547 assert(attr == FRAG_RESULT_COLOR ||
548 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
549 stfp->output_semantic_name[stfp->num_outputs] = TGSI_SEMANTIC_COLOR;
550 stfp->output_semantic_index[stfp->num_outputs] = numColors;
551 stfp->result_to_output[attr] = stfp->num_outputs;
566 * Translate a Mesa fragment shader into a TGSI shader using extra info in
568 * \return new fragment program variant
570 static struct st_fp_variant *
571 st_translate_fragment_program(struct st_context *st,
572 struct st_fragment_program *stfp,
573 const struct st_fp_variant_key *key)
575 struct pipe_context *pipe = st->pipe;
576 struct st_fp_variant *variant = CALLOC_STRUCT(st_fp_variant);
577 GLboolean deleteFP = GL_FALSE;
582 assert(!(key->bitmap && key->drawpixels));
586 /* glBitmap drawing */
587 struct gl_fragment_program *fp; /* we free this temp program below */
589 st_make_bitmap_fragment_program(st, &stfp->Base,
590 &fp, &variant->bitmap_sampler);
592 variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
593 stfp = st_fragment_program(fp);
596 else if (key->drawpixels) {
597 /* glDrawPixels drawing */
598 struct gl_fragment_program *fp; /* we free this temp program below */
600 if (key->drawpixels_z || key->drawpixels_stencil) {
601 fp = st_make_drawpix_z_stencil_program(st, key->drawpixels_z,
602 key->drawpixels_stencil);
606 st_make_drawpix_fragment_program(st, &stfp->Base, &fp);
607 variant->parameters = _mesa_clone_parameter_list(fp->Base.Parameters);
610 stfp = st_fragment_program(fp);
614 if (!stfp->tgsi.tokens) {
615 /* need to translate Mesa instructions to TGSI now */
616 struct ureg_program *ureg;
617 GLboolean write_all = st_prepare_fragment_program(st->ctx, stfp);
619 if (!stfp->glsl_to_tgsi)
620 _mesa_remove_output_reads(&stfp->Base.Base, PROGRAM_OUTPUT);
622 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
626 if (ST_DEBUG & DEBUG_MESA) {
627 _mesa_print_program(&stfp->Base.Base);
628 _mesa_print_program_parameters(st->ctx, &stfp->Base.Base);
631 if (write_all == GL_TRUE)
632 ureg_property_fs_color0_writes_all_cbufs(ureg, 1);
634 if (stfp->glsl_to_tgsi)
635 st_translate_program(st->ctx,
636 TGSI_PROCESSOR_FRAGMENT,
642 stfp->input_to_index,
643 stfp->input_semantic_name,
644 stfp->input_semantic_index,
648 stfp->result_to_output,
649 stfp->output_semantic_name,
650 stfp->output_semantic_index, FALSE );
652 st_translate_mesa_program(st->ctx,
653 TGSI_PROCESSOR_FRAGMENT,
658 stfp->input_to_index,
659 stfp->input_semantic_name,
660 stfp->input_semantic_index,
664 stfp->result_to_output,
665 stfp->output_semantic_name,
666 stfp->output_semantic_index, FALSE );
668 stfp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
669 ureg_destroy( ureg );
672 /* fill in variant */
673 variant->driver_shader = pipe->create_fs_state(pipe, &stfp->tgsi);
676 if (ST_DEBUG & DEBUG_TGSI) {
677 tgsi_dump( stfp->tgsi.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
682 /* Free the temporary program made above */
683 struct gl_fragment_program *fp = &stfp->Base;
684 _mesa_reference_fragprog(st->ctx, &fp, NULL);
692 * Translate fragment program if needed.
694 struct st_fp_variant *
695 st_get_fp_variant(struct st_context *st,
696 struct st_fragment_program *stfp,
697 const struct st_fp_variant_key *key)
699 struct st_fp_variant *fpv;
701 /* Search for existing variant */
702 for (fpv = stfp->variants; fpv; fpv = fpv->next) {
703 if (memcmp(&fpv->key, key, sizeof(*key)) == 0) {
710 fpv = st_translate_fragment_program(st, stfp, key);
712 /* insert into list */
713 fpv->next = stfp->variants;
714 stfp->variants = fpv;
723 * Translate a geometry program to create a new variant.
725 static struct st_gp_variant *
726 st_translate_geometry_program(struct st_context *st,
727 struct st_geometry_program *stgp,
728 const struct st_gp_variant_key *key)
730 GLuint inputMapping[GEOM_ATTRIB_MAX];
731 GLuint outputMapping[GEOM_RESULT_MAX];
732 struct pipe_context *pipe = st->pipe;
734 const GLbitfield inputsRead = stgp->Base.Base.InputsRead;
736 GLuint num_generic = 0;
738 uint gs_num_inputs = 0;
739 uint gs_builtin_inputs = 0;
740 uint gs_array_offset = 0;
742 ubyte gs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
743 ubyte gs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
744 uint gs_num_outputs = 0;
748 struct ureg_program *ureg;
750 struct st_gp_variant *gpv;
752 gpv = CALLOC_STRUCT(st_gp_variant);
756 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_OUTPUT);
757 _mesa_remove_output_reads(&stgp->Base.Base, PROGRAM_VARYING);
759 ureg = ureg_create( TGSI_PROCESSOR_GEOMETRY );
765 /* which vertex output goes to the first geometry input */
768 memset(inputMapping, 0, sizeof(inputMapping));
769 memset(outputMapping, 0, sizeof(outputMapping));
772 * Convert Mesa program inputs to TGSI input register semantics.
774 for (attr = 0; attr < GEOM_ATTRIB_MAX; attr++) {
775 if (inputsRead & (1 << attr)) {
776 const GLuint slot = gs_num_inputs;
780 inputMapping[attr] = slot;
782 stgp->input_map[slot + gs_array_offset] = vslot - gs_builtin_inputs;
783 stgp->input_to_index[attr] = vslot;
784 stgp->index_to_input[vslot] = attr;
787 if (attr != GEOM_ATTRIB_PRIMITIVE_ID) {
788 gs_array_offset += 2;
793 debug_printf("input map at %d = %d\n",
794 slot + gs_array_offset, stgp->input_map[slot + gs_array_offset]);
798 case GEOM_ATTRIB_PRIMITIVE_ID:
799 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_PRIMID;
800 stgp->input_semantic_index[slot] = 0;
802 case GEOM_ATTRIB_POSITION:
803 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
804 stgp->input_semantic_index[slot] = 0;
806 case GEOM_ATTRIB_COLOR0:
807 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
808 stgp->input_semantic_index[slot] = 0;
810 case GEOM_ATTRIB_COLOR1:
811 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
812 stgp->input_semantic_index[slot] = 1;
814 case GEOM_ATTRIB_FOG_FRAG_COORD:
815 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
816 stgp->input_semantic_index[slot] = 0;
818 case GEOM_ATTRIB_TEX_COORD:
819 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
820 stgp->input_semantic_index[slot] = num_generic++;
822 case GEOM_ATTRIB_VAR0:
825 stgp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
826 stgp->input_semantic_index[slot] = num_generic++;
831 /* initialize output semantics to defaults */
832 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
833 gs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
834 gs_output_semantic_index[i] = 0;
839 * Determine number of outputs, the (default) output register
840 * mapping and the semantic information for each output.
842 for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
843 if (stgp->Base.Base.OutputsWritten & BITFIELD64_BIT(attr)) {
846 slot = gs_num_outputs;
848 outputMapping[attr] = slot;
851 case GEOM_RESULT_POS:
853 gs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
854 gs_output_semantic_index[slot] = 0;
856 case GEOM_RESULT_COL0:
857 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
858 gs_output_semantic_index[slot] = 0;
860 case GEOM_RESULT_COL1:
861 gs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
862 gs_output_semantic_index[slot] = 1;
864 case GEOM_RESULT_SCOL0:
865 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
866 gs_output_semantic_index[slot] = 0;
868 case GEOM_RESULT_SCOL1:
869 gs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
870 gs_output_semantic_index[slot] = 1;
872 case GEOM_RESULT_FOGC:
873 gs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
874 gs_output_semantic_index[slot] = 0;
876 case GEOM_RESULT_PSIZ:
877 gs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
878 gs_output_semantic_index[slot] = 0;
880 case GEOM_RESULT_TEX0:
881 case GEOM_RESULT_TEX1:
882 case GEOM_RESULT_TEX2:
883 case GEOM_RESULT_TEX3:
884 case GEOM_RESULT_TEX4:
885 case GEOM_RESULT_TEX5:
886 case GEOM_RESULT_TEX6:
887 case GEOM_RESULT_TEX7:
889 case GEOM_RESULT_VAR0:
892 assert(slot < Elements(gs_output_semantic_name));
893 /* use default semantic info */
894 gs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
895 gs_output_semantic_index[slot] = num_generic++;
900 assert(gs_output_semantic_name[0] == TGSI_SEMANTIC_POSITION);
902 /* find max output slot referenced to compute gs_num_outputs */
903 for (attr = 0; attr < GEOM_RESULT_MAX; attr++) {
904 if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot)
905 maxSlot = outputMapping[attr];
907 gs_num_outputs = maxSlot + 1;
912 printf("outputMapping? %d\n", outputMapping ? 1 : 0);
914 printf("attr -> slot\n");
915 for (i = 0; i < 16; i++) {
916 printf(" %2d %3d\n", i, outputMapping[i]);
919 printf("slot sem_name sem_index\n");
920 for (i = 0; i < gs_num_outputs; i++) {
921 printf(" %2d %d %d\n",
923 gs_output_semantic_name[i],
924 gs_output_semantic_index[i]);
929 /* free old shader state, if any */
930 if (stgp->tgsi.tokens) {
931 st_free_tokens(stgp->tgsi.tokens);
932 stgp->tgsi.tokens = NULL;
935 ureg_property_gs_input_prim(ureg, stgp->Base.InputType);
936 ureg_property_gs_output_prim(ureg, stgp->Base.OutputType);
937 ureg_property_gs_max_vertices(ureg, stgp->Base.VerticesOut);
939 st_translate_mesa_program(st->ctx,
940 TGSI_PROCESSOR_GEOMETRY,
946 stgp->input_semantic_name,
947 stgp->input_semantic_index,
952 gs_output_semantic_name,
953 gs_output_semantic_index,
956 stgp->num_inputs = gs_num_inputs;
957 stgp->tgsi.tokens = ureg_get_tokens( ureg, NULL );
958 ureg_destroy( ureg );
960 /* fill in new variant */
961 gpv->driver_shader = pipe->create_gs_state(pipe, &stgp->tgsi);
964 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
965 _mesa_print_program(&stgp->Base.Base);
969 if (ST_DEBUG & DEBUG_TGSI) {
970 tgsi_dump(stgp->tgsi.tokens, 0);
979 * Get/create geometry program variant.
981 struct st_gp_variant *
982 st_get_gp_variant(struct st_context *st,
983 struct st_geometry_program *stgp,
984 const struct st_gp_variant_key *key)
986 struct st_gp_variant *gpv;
988 /* Search for existing variant */
989 for (gpv = stgp->variants; gpv; gpv = gpv->next) {
990 if (memcmp(&gpv->key, key, sizeof(*key)) == 0) {
997 gpv = st_translate_geometry_program(st, stgp, key);
999 /* insert into list */
1000 gpv->next = stgp->variants;
1001 stgp->variants = gpv;
1012 * Debug- print current shader text
1015 st_print_shaders(struct gl_context *ctx)
1017 struct gl_shader_program *shProg[3] = {
1018 ctx->Shader.CurrentVertexProgram,
1019 ctx->Shader.CurrentGeometryProgram,
1020 ctx->Shader.CurrentFragmentProgram,
1024 for (j = 0; j < 3; j++) {
1027 if (shProg[j] == NULL)
1030 for (i = 0; i < shProg[j]->NumShaders; i++) {
1031 struct gl_shader *sh;
1033 switch (shProg[j]->Shaders[i]->Type) {
1034 case GL_VERTEX_SHADER:
1035 sh = (i != 0) ? NULL : shProg[j]->Shaders[i];
1037 case GL_GEOMETRY_SHADER_ARB:
1038 sh = (i != 1) ? NULL : shProg[j]->Shaders[i];
1040 case GL_FRAGMENT_SHADER:
1041 sh = (i != 2) ? NULL : shProg[j]->Shaders[i];
1050 printf("GLSL shader %u of %u:\n", i, shProg[j]->NumShaders);
1051 printf("%s\n", sh->Source);
1059 * Vert/Geom/Frag programs have per-context variants. Free all the
1060 * variants attached to the given program which match the given context.
1063 destroy_program_variants(struct st_context *st, struct gl_program *program)
1068 switch (program->Target) {
1069 case GL_VERTEX_PROGRAM_ARB:
1071 struct st_vertex_program *stvp = (struct st_vertex_program *) program;
1072 struct st_vp_variant *vpv, **prevPtr = &stvp->variants;
1074 for (vpv = stvp->variants; vpv; ) {
1075 struct st_vp_variant *next = vpv->next;
1076 if (vpv->key.st == st) {
1077 /* unlink from list */
1079 /* destroy this variant */
1080 delete_vp_variant(st, vpv);
1083 prevPtr = &vpv->next;
1089 case GL_FRAGMENT_PROGRAM_ARB:
1091 struct st_fragment_program *stfp =
1092 (struct st_fragment_program *) program;
1093 struct st_fp_variant *fpv, **prevPtr = &stfp->variants;
1095 for (fpv = stfp->variants; fpv; ) {
1096 struct st_fp_variant *next = fpv->next;
1097 if (fpv->key.st == st) {
1098 /* unlink from list */
1100 /* destroy this variant */
1101 delete_fp_variant(st, fpv);
1104 prevPtr = &fpv->next;
1110 case MESA_GEOMETRY_PROGRAM:
1112 struct st_geometry_program *stgp =
1113 (struct st_geometry_program *) program;
1114 struct st_gp_variant *gpv, **prevPtr = &stgp->variants;
1116 for (gpv = stgp->variants; gpv; ) {
1117 struct st_gp_variant *next = gpv->next;
1118 if (gpv->key.st == st) {
1119 /* unlink from list */
1121 /* destroy this variant */
1122 delete_gp_variant(st, gpv);
1125 prevPtr = &gpv->next;
1132 _mesa_problem(NULL, "Unexpected program target 0x%x in "
1133 "destroy_program_variants_cb()", program->Target);
1139 * Callback for _mesa_HashWalk. Free all the shader's program variants
1140 * which match the given context.
1143 destroy_shader_program_variants_cb(GLuint key, void *data, void *userData)
1145 struct st_context *st = (struct st_context *) userData;
1146 struct gl_shader *shader = (struct gl_shader *) data;
1148 switch (shader->Type) {
1149 case GL_SHADER_PROGRAM_MESA:
1151 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
1154 for (i = 0; i < shProg->NumShaders; i++) {
1155 destroy_program_variants(st, shProg->Shaders[i]->Program);
1158 destroy_program_variants(st, (struct gl_program *)
1159 shProg->VertexProgram);
1160 destroy_program_variants(st, (struct gl_program *)
1161 shProg->FragmentProgram);
1162 destroy_program_variants(st, (struct gl_program *)
1163 shProg->GeometryProgram);
1166 case GL_VERTEX_SHADER:
1167 case GL_FRAGMENT_SHADER:
1168 case GL_GEOMETRY_SHADER:
1170 destroy_program_variants(st, shader->Program);
1180 * Callback for _mesa_HashWalk. Free all the program variants which match
1181 * the given context.
1184 destroy_program_variants_cb(GLuint key, void *data, void *userData)
1186 struct st_context *st = (struct st_context *) userData;
1187 struct gl_program *program = (struct gl_program *) data;
1188 destroy_program_variants(st, program);
1193 * Walk over all shaders and programs to delete any variants which
1194 * belong to the given context.
1195 * This is called during context tear-down.
1198 st_destroy_program_variants(struct st_context *st)
1200 /* ARB vert/frag program */
1201 _mesa_HashWalk(st->ctx->Shared->Programs,
1202 destroy_program_variants_cb, st);
1204 /* GLSL vert/frag/geom shaders */
1205 _mesa_HashWalk(st->ctx->Shared->ShaderObjects,
1206 destroy_shader_program_variants_cb, st);