glsl: Add assert to check input to strcmp.
[mesa.git] / src / mesa / shader / prog_parameter_layout.c
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file prog_parameter_layout.c
26 * \brief Helper functions to layout storage for program parameters
27 *
28 * \author Ian Romanick <ian.d.romanick@intel.com>
29 */
30
31 #include "main/mtypes.h"
32 #include "prog_parameter.h"
33 #include "prog_parameter_layout.h"
34 #include "prog_instruction.h"
35 #include "program_parser.h"
36
37 unsigned
38 _mesa_combine_swizzles(unsigned base, unsigned applied)
39 {
40 unsigned swiz = 0;
41 unsigned i;
42
43 for (i = 0; i < 4; i++) {
44 const unsigned s = GET_SWZ(applied, i);
45
46 swiz |= ((s <= SWIZZLE_W) ? GET_SWZ(base, s) : s) << (i * 3);
47 }
48
49 return swiz;
50 }
51
52
53 /**
54 * Copy indirect access array from one parameter list to another
55 *
56 * \param src Parameter array copied from
57 * \param dst Parameter array copied to
58 * \param first Index of first element in \c src to copy
59 * \param count Number of elements to copy
60 *
61 * \return
62 * The location in \c dst of the first element copied from \c src on
63 * success. -1 on failure.
64 *
65 * \warning
66 * This function assumes that there is already enough space available in
67 * \c dst to hold all of the elements that will be copied over.
68 */
69 static int
70 copy_indirect_accessed_array(struct gl_program_parameter_list *src,
71 struct gl_program_parameter_list *dst,
72 unsigned first, unsigned count)
73 {
74 const int base = dst->NumParameters;
75 unsigned i, j;
76
77 for (i = first; i < (first + count); i++) {
78 struct gl_program_parameter *curr = & src->Parameters[i];
79
80 if (curr->Type == PROGRAM_CONSTANT) {
81 j = dst->NumParameters;
82 } else {
83 for (j = 0; j < dst->NumParameters; j++) {
84 if (memcmp(dst->Parameters[j].StateIndexes, curr->StateIndexes,
85 sizeof(curr->StateIndexes)) == 0) {
86 return -1;
87 }
88 }
89 }
90
91 assert(j == dst->NumParameters);
92
93 /* copy src parameter [i] to dest parameter [j] */
94 memcpy(& dst->Parameters[j], curr,
95 sizeof(dst->Parameters[j]));
96 memcpy(dst->ParameterValues[j], src->ParameterValues[i],
97 sizeof(GLfloat) * 4);
98
99 /* Pointer to the string name was copied. Null-out src param name
100 * to prevent double free later.
101 */
102 curr->Name = NULL;
103
104 dst->NumParameters++;
105 }
106
107 return base;
108 }
109
110
111 /**
112 * XXX description???
113 * \return GL_TRUE for success, GL_FALSE for failure
114 */
115 GLboolean
116 _mesa_layout_parameters(struct asm_parser_state *state)
117 {
118 struct gl_program_parameter_list *layout;
119 struct asm_instruction *inst;
120 unsigned i;
121
122 layout =
123 _mesa_new_parameter_list_sized(state->prog->Parameters->NumParameters);
124
125 /* PASS 1: Move any parameters that are accessed indirectly from the
126 * original parameter list to the new parameter list.
127 */
128 for (inst = state->inst_head; inst != NULL; inst = inst->next) {
129 for (i = 0; i < 3; i++) {
130 if (inst->SrcReg[i].Base.RelAddr) {
131 /* Only attempt to add the to the new parameter list once.
132 */
133 if (!inst->SrcReg[i].Symbol->pass1_done) {
134 const int new_begin =
135 copy_indirect_accessed_array(state->prog->Parameters, layout,
136 inst->SrcReg[i].Symbol->param_binding_begin,
137 inst->SrcReg[i].Symbol->param_binding_length);
138
139 if (new_begin < 0) {
140 return GL_FALSE;
141 }
142
143 inst->SrcReg[i].Symbol->param_binding_begin = new_begin;
144 inst->SrcReg[i].Symbol->pass1_done = 1;
145 }
146
147 /* Previously the Index was just the offset from the parameter
148 * array. Now that the base of the parameter array is known, the
149 * index can be updated to its actual value.
150 */
151 inst->Base.SrcReg[i] = inst->SrcReg[i].Base;
152 inst->Base.SrcReg[i].Index +=
153 inst->SrcReg[i].Symbol->param_binding_begin;
154 }
155 }
156 }
157
158 /* PASS 2: Move any parameters that are not accessed indirectly from the
159 * original parameter list to the new parameter list.
160 */
161 for (inst = state->inst_head; inst != NULL; inst = inst->next) {
162 for (i = 0; i < 3; i++) {
163 const struct gl_program_parameter *p;
164 const int idx = inst->SrcReg[i].Base.Index;
165 unsigned swizzle = SWIZZLE_NOOP;
166
167 /* All relative addressed operands were processed on the first
168 * pass. Just skip them here.
169 */
170 if (inst->SrcReg[i].Base.RelAddr) {
171 continue;
172 }
173
174 if ((inst->SrcReg[i].Base.File <= PROGRAM_VARYING )
175 || (inst->SrcReg[i].Base.File >= PROGRAM_WRITE_ONLY)) {
176 continue;
177 }
178
179 inst->Base.SrcReg[i] = inst->SrcReg[i].Base;
180 p = & state->prog->Parameters->Parameters[idx];
181
182 switch (p->Type) {
183 case PROGRAM_CONSTANT: {
184 const float *const v =
185 state->prog->Parameters->ParameterValues[idx];
186
187 inst->Base.SrcReg[i].Index =
188 _mesa_add_unnamed_constant(layout, v, p->Size, & swizzle);
189
190 inst->Base.SrcReg[i].Swizzle =
191 _mesa_combine_swizzles(swizzle, inst->Base.SrcReg[i].Swizzle);
192 break;
193 }
194
195 case PROGRAM_STATE_VAR:
196 inst->Base.SrcReg[i].Index =
197 _mesa_add_state_reference(layout, p->StateIndexes);
198 break;
199
200 default:
201 break;
202 }
203
204 inst->SrcReg[i].Base.File = p->Type;
205 inst->Base.SrcReg[i].File = p->Type;
206 }
207 }
208
209 _mesa_free_parameter_list(state->prog->Parameters);
210 state->prog->Parameters = layout;
211
212 return GL_TRUE;
213 }