• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*/
2  * Copyright 2013 VMware, Inc.  All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 
26 #include "svga_context.h"
27 #include "svga_link.h"
28 #include "svga_debug.h"
29 
30 #include "tgsi/tgsi_strings.h"
31 
32 
33 #define INVALID_INDEX 255
34 
35 
36 /**
37  * Examine input and output shaders info to link outputs from the
38  * output shader to inputs from the input shader.
39  * Basically, we'll remap input shader's input slots to new numbers
40  * based on semantic name/index of the outputs from the output shader.
41  */
42 void
svga_link_shaders(const struct tgsi_shader_info * outshader_info,const struct tgsi_shader_info * inshader_info,struct shader_linkage * linkage)43 svga_link_shaders(const struct tgsi_shader_info *outshader_info,
44                   const struct tgsi_shader_info *inshader_info,
45                   struct shader_linkage *linkage)
46 {
47    unsigned i, free_slot;
48 
49    for (i = 0; i < ARRAY_SIZE(linkage->input_map); i++) {
50       linkage->input_map[i] = INVALID_INDEX;
51    }
52 
53    for (i = 0; i < ARRAY_SIZE(linkage->prevShader.output_map); i++) {
54       linkage->prevShader.output_map[i] = INVALID_INDEX;
55    }
56 
57    /* Assign input slots for input shader inputs.
58     * Basically, we want to use the same index for the output shader's outputs
59     * and the input shader's inputs that should be linked together.
60     * We'll modify the input shader's inputs to match the output shader.
61     */
62    assert(inshader_info->num_inputs <=
63           ARRAY_SIZE(inshader_info->input_semantic_name));
64 
65    /* free register index that can be used for built-in varyings */
66    free_slot = outshader_info->num_outputs + 1;
67 
68    for (i = 0; i < inshader_info->num_inputs; i++) {
69       enum tgsi_semantic sem_name = inshader_info->input_semantic_name[i];
70       unsigned sem_index = inshader_info->input_semantic_index[i];
71       unsigned j;
72       unsigned out_index;
73 
74       if (sem_name == TGSI_SEMANTIC_PCOORD) {
75          sem_name = TGSI_SEMANTIC_TEXCOORD;
76          sem_index = 0;
77       }
78 
79       /* search output shader outputs for same item */
80       for (j = 0; j < outshader_info->num_outputs; j++) {
81          assert(j < ARRAY_SIZE(outshader_info->output_semantic_name));
82          if (outshader_info->output_semantic_name[j] == sem_name &&
83              outshader_info->output_semantic_index[j] == sem_index) {
84             linkage->input_map[i] = j;
85             linkage->prevShader.output_map[j] = i;
86             break;
87          }
88       }
89 
90       /**
91        * The clip distance inputs come from the output shader's
92        * clip distance shadow copy, so mark the input index to match
93        * the index of the shadow copy.
94        */
95       if (sem_name == TGSI_SEMANTIC_CLIPDIST) {
96          out_index = outshader_info->num_outputs + 1 + sem_index;
97          linkage->input_map[i] = out_index;
98          linkage->prevShader.output_map[out_index] = i;
99          /* make sure free_slot includes this extra output */
100          free_slot = MAX2(free_slot, linkage->input_map[i] + 1);
101       }
102    }
103 
104    /* Find the index for position */
105    linkage->position_index = 0;
106    for (i = 0; i < outshader_info->num_outputs; i++) {
107       if (outshader_info->output_semantic_name[i] == TGSI_SEMANTIC_POSITION) {
108          linkage->position_index = i;
109          break;
110       }
111    }
112 
113    linkage->num_inputs = inshader_info->num_inputs;
114    linkage->prevShader.num_outputs = outshader_info->num_outputs;
115 
116    /* Things like the front-face register are handled here */
117    for (i = 0; i < inshader_info->num_inputs; i++) {
118       if (linkage->input_map[i] == INVALID_INDEX) {
119          unsigned j = free_slot++;
120          linkage->input_map[i] = j;
121          linkage->prevShader.output_map[j] = i;
122       }
123    }
124    linkage->input_map_max = free_slot - 1;
125 
126    /* Debug */
127    if (SVGA_DEBUG & DEBUG_TGSI) {
128       uint64_t reg = 0;
129       uint64_t one = 1;
130 
131       debug_printf(
132       "### linkage info: num_inputs=%d input_map_max=%d prevShader.num_outputs=%d\n",
133                    linkage->num_inputs, linkage->input_map_max,
134                    linkage->prevShader.num_outputs);
135 
136       for (i = 0; i < linkage->num_inputs; i++) {
137 
138          assert(linkage->input_map[i] != INVALID_INDEX);
139 
140          debug_printf("   input[%d] slot %u  %s %u %s\n",
141                       i,
142                       linkage->input_map[i],
143                       tgsi_semantic_names[inshader_info->input_semantic_name[i]],
144                       inshader_info->input_semantic_index[i],
145                       tgsi_interpolate_names[inshader_info->input_interpolate[i]]);
146 
147          /* make sure no repeating register index */
148          assert((reg & (one << linkage->input_map[i])) == 0);
149          reg |= one << linkage->input_map[i];
150       }
151    }
152 }
153